From 8c8f0bc931b0b045b1a6ea7acb1ce9d69b08b03a Mon Sep 17 00:00:00 2001 From: GatewayJ <835269233@qq.com> Date: Fri, 13 Nov 2026 22:16:43 +0800 Subject: [PATCH 01/12] feat: add branch Ubuntu sandboxes with CSGClaw and unified MCP --- .gitignore | 4 + Cargo.lock | 8 + apps/agentdrived/Cargo.toml | 7 + apps/agentdrived/src/main.rs | 58 +- apps/agentdrived/src/sandbox.rs | 1065 +++++++ apps/agentdrived/src/sandbox/kernel.rs | 109 + apps/agentdrived/src/sandbox/proxy.rs | 163 ++ apps/agentdrived/src/sandbox/shutdown.rs | 103 + apps/agentdrived/src/sandbox/templates.rs | 145 + build/sandbox/.dockerignore | 2 + build/sandbox/.gitignore | 2 + build/sandbox/64-agentdrive-fonts.conf | 16 + .../90-cube-ubuntu-desktop.gschema.override | 21 + build/sandbox/Dockerfile | 135 + build/sandbox/Xwrapper.config | 2 + build/sandbox/assistant/Dockerfile | 11 + build/sandbox/assistant/assistant-mcp.py | 17 + build/sandbox/assistant/assistant-service.py | 152 + build/sandbox/assistant/runtime.json | 1 + build/sandbox/browser-session.py | 87 + build/sandbox/build-assistant-image.sh | 34 + build/sandbox/build-image.sh | 25 + build/sandbox/chromium | 2 + build/sandbox/chromium.desktop | 12 + build/sandbox/chromium.json | 5 + build/sandbox/desktop_environment.py | 60 + build/sandbox/desktop_mcp.py | 129 + build/sandbox/guest-helper.py | 132 + build/sandbox/guest-init.sh | 60 + build/sandbox/install-chromium.py | 39 + build/sandbox/install-kernels.sh | 19 + build/sandbox/mcp-proxy.py | 64 + build/sandbox/mcp/gateway.mjs | 120 + build/sandbox/mcp/package-lock.json | 1267 +++++++++ build/sandbox/mcp/package.json | 10 + build/sandbox/mcp/playwright.json | 11 + build/sandbox/mcp/providers.json | 17 + build/sandbox/novnc-index.html | 5 + build/sandbox/prepare-network.sh | 37 + build/sandbox/start-desktop.sh | 213 ++ build/sandbox/template-manifest.py | 36 + build/sandbox/xorg.conf | 38 + crates/agentdrive-engine/src/engine.rs | 1 + crates/agentdrive-engine/src/lib.rs | 3 + crates/agentdrive-engine/src/mounts.rs | 3 + .../src/mounts/collaboration.rs | 4 + crates/agentdrive-engine/src/ownership.rs | 1 + crates/agentdrive-engine/src/runtime.rs | 12 + crates/agentdrive-engine/src/sandbox.rs | 506 ++++ crates/agentdrive-mcp/Cargo.toml | 3 + crates/agentdrive-mcp/dashboard/dashboard.js | 19 +- crates/agentdrive-mcp/dashboard/index.html | 4 +- .../agentdrive-mcp/dashboard/management.css | 61 + crates/agentdrive-mcp/dashboard/management.js | 10 +- crates/agentdrive-mcp/dashboard/navigation.js | 9 +- crates/agentdrive-mcp/dashboard/sandbox.js | 306 ++ crates/agentdrive-mcp/dashboard/sandboxes.css | 58 + crates/agentdrive-mcp/dashboard/sandboxes.js | 214 ++ crates/agentdrive-mcp/src/gateway.rs | 11 +- crates/agentdrive-mcp/src/lib.rs | 2 + .../src/sandbox_collaboration_tests.rs | 180 ++ crates/agentdrive-mcp/src/sandbox_proxy.rs | 122 + crates/agentdrive-mcp/src/sandbox_tests.rs | 547 ++++ crates/agentdrive-mcp/src/web.rs | 286 ++ crates/agentdrive-ports/src/api.rs | 1 + crates/agentdrive-ports/src/lib.rs | 3 + crates/agentdrive-ports/src/sandbox.rs | 205 ++ docs/sandbox.md | 241 ++ runtime/sandbox-worker/.gitignore | 1 + runtime/sandbox-worker/Cargo.lock | 2507 +++++++++++++++++ runtime/sandbox-worker/Cargo.toml | 13 + runtime/sandbox-worker/src/main.rs | 49 + tests/dashboard/sandbox.test.cjs | 219 ++ tests/dashboard/sandboxes.test.cjs | 130 + tests/sandbox/.gitignore | 1 + tests/sandbox/chromium_probe.py | 129 + tests/sandbox/delete-e2e.cjs | 83 + tests/sandbox/desktop_probe.py | 49 + tests/sandbox/e2e.cjs | 101 + tests/sandbox/http_proxy_fixture.py | 39 + tests/sandbox/mcp-fixture.py | 27 + tests/sandbox/mcp-gateway.test.mjs | 59 + tests/sandbox/network_probe.py | 94 + tests/sandbox/playwright_probe.py | 135 + tests/sandbox/test_assistant_service.py | 26 + tests/sandbox/test_chromium_installer.py | 59 + tests/sandbox/test_desktop_mcp.py | 95 + tests/sandbox/test_guest_mcp_bridge.py | 48 + tests/sandbox/test_guest_readiness.py | 51 + tests/sandbox/test_mcp_proxy.py | 61 + 90 files changed, 11217 insertions(+), 14 deletions(-) create mode 100644 apps/agentdrived/src/sandbox.rs create mode 100644 apps/agentdrived/src/sandbox/kernel.rs create mode 100644 apps/agentdrived/src/sandbox/proxy.rs create mode 100644 apps/agentdrived/src/sandbox/shutdown.rs create mode 100644 apps/agentdrived/src/sandbox/templates.rs create mode 100644 build/sandbox/.dockerignore create mode 100644 build/sandbox/.gitignore create mode 100644 build/sandbox/64-agentdrive-fonts.conf create mode 100644 build/sandbox/90-cube-ubuntu-desktop.gschema.override create mode 100644 build/sandbox/Dockerfile create mode 100644 build/sandbox/Xwrapper.config create mode 100644 build/sandbox/assistant/Dockerfile create mode 100644 build/sandbox/assistant/assistant-mcp.py create mode 100644 build/sandbox/assistant/assistant-service.py create mode 100644 build/sandbox/assistant/runtime.json create mode 100644 build/sandbox/browser-session.py create mode 100644 build/sandbox/build-assistant-image.sh create mode 100755 build/sandbox/build-image.sh create mode 100644 build/sandbox/chromium create mode 100644 build/sandbox/chromium.desktop create mode 100644 build/sandbox/chromium.json create mode 100644 build/sandbox/desktop_environment.py create mode 100644 build/sandbox/desktop_mcp.py create mode 100644 build/sandbox/guest-helper.py create mode 100755 build/sandbox/guest-init.sh create mode 100644 build/sandbox/install-chromium.py create mode 100644 build/sandbox/install-kernels.sh create mode 100755 build/sandbox/mcp-proxy.py create mode 100644 build/sandbox/mcp/gateway.mjs create mode 100644 build/sandbox/mcp/package-lock.json create mode 100644 build/sandbox/mcp/package.json create mode 100644 build/sandbox/mcp/playwright.json create mode 100644 build/sandbox/mcp/providers.json create mode 100644 build/sandbox/novnc-index.html create mode 100755 build/sandbox/prepare-network.sh create mode 100644 build/sandbox/start-desktop.sh create mode 100644 build/sandbox/template-manifest.py create mode 100644 build/sandbox/xorg.conf create mode 100644 crates/agentdrive-engine/src/sandbox.rs create mode 100644 crates/agentdrive-mcp/dashboard/sandbox.js create mode 100644 crates/agentdrive-mcp/dashboard/sandboxes.css create mode 100644 crates/agentdrive-mcp/dashboard/sandboxes.js create mode 100644 crates/agentdrive-mcp/src/sandbox_collaboration_tests.rs create mode 100644 crates/agentdrive-mcp/src/sandbox_proxy.rs create mode 100644 crates/agentdrive-mcp/src/sandbox_tests.rs create mode 100644 crates/agentdrive-ports/src/sandbox.rs create mode 100644 docs/sandbox.md create mode 100644 runtime/sandbox-worker/.gitignore create mode 100644 runtime/sandbox-worker/Cargo.lock create mode 100644 runtime/sandbox-worker/Cargo.toml create mode 100644 runtime/sandbox-worker/src/main.rs create mode 100644 tests/dashboard/sandbox.test.cjs create mode 100644 tests/dashboard/sandboxes.test.cjs create mode 100644 tests/sandbox/.gitignore create mode 100644 tests/sandbox/chromium_probe.py create mode 100644 tests/sandbox/delete-e2e.cjs create mode 100644 tests/sandbox/desktop_probe.py create mode 100644 tests/sandbox/e2e.cjs create mode 100644 tests/sandbox/http_proxy_fixture.py create mode 100644 tests/sandbox/mcp-fixture.py create mode 100644 tests/sandbox/mcp-gateway.test.mjs create mode 100644 tests/sandbox/network_probe.py create mode 100644 tests/sandbox/playwright_probe.py create mode 100644 tests/sandbox/test_assistant_service.py create mode 100644 tests/sandbox/test_chromium_installer.py create mode 100644 tests/sandbox/test_desktop_mcp.py create mode 100644 tests/sandbox/test_guest_mcp_bridge.py create mode 100644 tests/sandbox/test_guest_readiness.py create mode 100644 tests/sandbox/test_mcp_proxy.py diff --git a/.gitignore b/.gitignore index be3ae90..23ad557 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ /vendor/winfsp_wrs/Cargo.lock /tests/dashboard/node_modules/ +/build/sandbox/mcp/node_modules/ +/build/sandbox/.cache/ +/build/sandbox/**/__pycache__/ +/tests/sandbox/__pycache__/ diff --git a/Cargo.lock b/Cargo.lock index a722a20..0dfc40f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -82,8 +82,11 @@ dependencies = [ "agentdrive-model", "agentdrive-ports", "agentdrive-test-support", + "async-trait", "axum", "bytes", + "hyper", + "hyper-util", "reqwest", "rmcp", "schemars", @@ -219,8 +222,13 @@ dependencies = [ "agentdrive-platform", "agentdrive-ports", "agentdrive-s3", + "async-trait", "axum", + "bytes", "clap", + "serde", + "serde_json", + "tempfile", "tokio", "tokio-util", "tracing", diff --git a/apps/agentdrived/Cargo.toml b/apps/agentdrived/Cargo.toml index e235b75..bc58741 100644 --- a/apps/agentdrived/Cargo.toml +++ b/apps/agentdrived/Cargo.toml @@ -6,6 +6,10 @@ rust-version.workspace = true license.workspace = true [dependencies] +bytes.workspace = true +async-trait.workspace = true +serde.workspace = true +serde_json.workspace = true agentdrive-config.workspace = true axum.workspace = true tokio-util.workspace = true @@ -22,5 +26,8 @@ tokio.workspace = true tracing.workspace = true tracing-subscriber.workspace = true +[dev-dependencies] +tempfile.workspace = true + [lints] workspace = true diff --git a/apps/agentdrived/src/main.rs b/apps/agentdrived/src/main.rs index 538e41c..d2b5f92 100644 --- a/apps/agentdrived/src/main.rs +++ b/apps/agentdrived/src/main.rs @@ -1,3 +1,5 @@ +#[cfg(target_os = "linux")] +mod sandbox; mod settings; use agentdrive_config::{ConfigStore, initialize}; use agentdrive_engine::{Adapters, Engine, EngineConfig}; @@ -5,6 +7,7 @@ use agentdrive_local::LocalBackend; use agentdrive_mcp::{GatewayConfig, router}; use agentdrive_model::*; use agentdrive_platform::{HostDirectories, NativeMountDriver, SystemClock, local_identity}; +use agentdrive_ports::LocalStateStore; use clap::{Parser, Subcommand}; use std::{net::SocketAddr, path::PathBuf, sync::Arc, time::Duration}; use tokio_util::sync::CancellationToken; @@ -83,8 +86,26 @@ async fn serve(settings: Arc) -> Result<()> { let cancellation = CancellationToken::new(); let listener = tokio::net::TcpListener::bind(config.listen).await?; let address = listener.local_addr()?; + let mut services = engine.services.clone(); + #[cfg(target_os = "linux")] + let mut sandbox_monitor = None; + #[cfg(target_os = "linux")] + if let Some(path) = std::env::var_os("AGENTDRIVE_SANDBOX_CONFIG") { + let sandbox_config: sandbox::Config = serde_json::from_reader(std::fs::File::open(path)?)?; + let mount_root = sandbox_config.mount_root.clone(); + let driver = sandbox::NativeSandbox::open(sandbox_config, address.port())?; + let service = agentdrive_engine::SandboxService::new( + engine.runtime.clone(), + services.clone(), + driver, + mount_root, + ); + services.sandbox = Some(service.clone()); + sandbox_monitor = Some(service); + } + let sandbox_service = services.sandbox.clone(); let app = router( - engine.services.clone(), + services, GatewayConfig { allowed_hosts: vec![address.to_string(), format!("localhost:{}", address.port())], settings: settings.clone(), @@ -94,6 +115,25 @@ async fn serve(settings: Arc) -> Result<()> { engine.restore_native_mounts().await?; tracing::info!(endpoint = %format!("http://{address}/mcp"), location = %engine.runtime.location, "AgentDrive is ready"); let mut workers = tokio::task::JoinSet::new(); + #[cfg(target_os = "linux")] + if let Some(sandbox) = sandbox_monitor { + let cancellation = cancellation.clone(); + workers.spawn(async move { + let mut interval = tokio::time::interval(Duration::from_secs(1)); + interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); + loop { + tokio::select! { _ = cancellation.cancelled() => break, _ = interval.tick() => {} } + tokio::select! { + _ = cancellation.cancelled() => break, + result = sandbox.reconcile_exited() => { + if let Err(error) = result { + tracing::warn!(%error, "Sandbox process reconciliation failed"); + } + } + } + } + }); + } { let cancellation = cancellation.clone(); workers.spawn(async move { @@ -176,6 +216,22 @@ async fn serve(settings: Arc) -> Result<()> { tracing::error!(%error, "background worker stopped unexpectedly"); } } + if let Some(sandbox) = sandbox_service { + for mount in local.mounts().await? { + if mount.state != MountState::Ready { + continue; + } + if sandbox + .status(&mount.principal, mount.workspace, mount.branch) + .await? + .is_some() + { + sandbox + .stop(&mount.principal, mount.workspace, mount.branch) + .await?; + } + } + } let mut last_error = None; while let Err(error) = engine.suspend_native_mounts().await { let detail = error.to_string(); diff --git a/apps/agentdrived/src/sandbox.rs b/apps/agentdrived/src/sandbox.rs new file mode 100644 index 0000000..307d40c --- /dev/null +++ b/apps/agentdrived/src/sandbox.rs @@ -0,0 +1,1065 @@ +mod kernel; +mod proxy; +mod shutdown; +mod templates; +use agentdrive_model::*; +use agentdrive_ports::*; +use async_trait::async_trait; +use serde::Deserialize; +use serde_json::json; +use std::{collections::BTreeMap, net::Ipv4Addr, path::PathBuf, process::Stdio, sync::Arc}; +use templates::TemplateConfig; +use tokio::{ + io::{AsyncBufReadExt, AsyncWriteExt, BufReader}, + process::{Child, Command}, + sync::Mutex, +}; + +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Config { + #[serde(default)] + pub templates: Vec, + pub worker: PathBuf, + pub kernel: PathBuf, + pub image: PathBuf, + pub directory: PathBuf, + pub mount_root: PathBuf, + pub novnc_dir: PathBuf, + #[serde(default)] + pub mcp_proxy: Option, + #[serde(default = "default_max_instances")] + pub max_instances: usize, + #[serde(default)] + pub networks: Vec, +} +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct SandboxNetwork { + pub tap: String, + pub guest_ip: Ipv4Addr, + pub gateway: Ipv4Addr, + pub dns: Vec, +} +impl SandboxNetwork { + fn validate(&self) -> Result<()> { + let guest = u32::from(self.guest_ip); + let gateway = u32::from(self.gateway); + if self.tap.is_empty() + || self.tap.len() > 15 + || !self + .tap + .bytes() + .all(|c| c.is_ascii_alphanumeric() || c == b'-' || c == b'_') + || guest & !3 != gateway & !3 + || guest == gateway + || !(1..=2).contains(&(guest & 3)) + || !(1..=2).contains(&(gateway & 3)) + || self.dns.is_empty() + { + return Err(Error::invalid( + "Sandbox network requires a valid TAP, two hosts in one /30 subnet and DNS servers", + )); + } + Ok(()) + } + fn kernel_args(&self) -> String { + let dns = self + .dns + .iter() + .map(ToString::to_string) + .collect::>() + .join(","); + format!( + " agentdrive.ip={}/30 agentdrive.gateway={} agentdrive.dns={dns}", + self.guest_ip, self.gateway + ) + } +} +fn default_max_instances() -> usize { + 2 +} +impl Config { + fn check_capacity(&self, memory_gib: u32) -> Result<()> { + let info = std::fs::read_to_string("/proc/meminfo")?; + let available = info + .lines() + .find(|line| line.starts_with("MemAvailable:")) + .and_then(|line| line.split_whitespace().nth(1)) + .and_then(|value| value.parse::().ok()) + .unwrap_or(0); + if available < (u64::from(memory_gib) + 1) * 1024 * 1024 { + return Err(Error::new( + ErrorCode::CapacityExceeded, + format!("Sandbox requires {memory_gib} GiB of memory plus 1 GiB of host reserve"), + )); + } + Ok(()) + } +} +#[derive(Debug)] +struct Instance { + status: SandboxStatus, + child: Child, + network_slot: Option, + proxy: Option, +} +#[derive(Debug)] +pub struct NativeSandbox { + config: Config, + service_port: u16, + instances: Mutex>, + progress: Mutex>, +} +impl NativeSandbox { + pub fn open(config: Config, service_port: u16) -> Result> { + if service_port == 0 { + return Err(Error::invalid("助手访问端口必须有效")); + } + for path in [&config.worker, &config.image] { + if !path.is_absolute() || !path.is_file() { + return Err(Error::invalid( + "Sandbox worker and image must be existing absolute paths", + )); + } + } + if !config.kernel.is_absolute() { + return Err(Error::invalid("Sandbox 内核必须使用绝对路径")); + } + if !config.directory.is_absolute() + || !config.mount_root.is_absolute() + || !config.novnc_dir.is_absolute() + || !config.novnc_dir.is_dir() + || config.max_instances == 0 + { + return Err(Error::invalid( + "Sandbox directories must be absolute, noVNC assets must exist and max_instances must be positive", + )); + } + if let Some(path) = &config.mcp_proxy + && (!path.is_absolute() || !path.is_file()) + { + return Err(Error::invalid( + "Sandbox MCP proxy must be an existing absolute path", + )); + } + let mut template_ids = std::collections::BTreeSet::from([default_sandbox_template()]); + for template in &config.templates { + template.validate()?; + if !template_ids.insert(template.id.clone()) { + return Err(Error::invalid("模板标识重复")); + } + } + for (index, network) in config.networks.iter().enumerate() { + network.validate()?; + if config.networks[..index].iter().any(|other| { + other.tap == network.tap + || (u32::from(other.guest_ip) & !3) == (u32::from(network.guest_ip) & !3) + }) { + return Err(Error::invalid( + "Sandbox networks must use distinct TAPs and subnets", + )); + } + } + std::fs::create_dir_all(&config.directory)?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&config.directory, std::fs::Permissions::from_mode(0o700))?; + } + Ok(Arc::new(Self { + config, + service_port, + instances: Mutex::new(BTreeMap::new()), + progress: Mutex::new(BTreeMap::new()), + })) + } + async fn step(&self, id: &str, step: &str) { + if let Some(status) = self.progress.lock().await.get_mut(id) { + status.state = SandboxState::Starting; + status.startup_step = Some(step.to_owned()); + status.message = None; + } + } + #[cfg(unix)] + async fn guest(&self, id: &str, port: u32) -> Result> { + let mut stream = + tokio::net::UnixStream::connect(self.config.directory.join(id).join("vsock.sock")) + .await?; + stream + .write_all(format!("CONNECT {port}\n").as_bytes()) + .await?; + let mut stream = BufReader::new(stream); + let mut line = String::new(); + tokio::time::timeout( + std::time::Duration::from_secs(2), + stream.read_line(&mut line), + ) + .await + .map_err(|_| Error::new(ErrorCode::Unavailable, "Guest connection timed out"))??; + if !line.starts_with("OK ") { + return Err(Error::new( + ErrorCode::Unavailable, + "Guest connection failed", + )); + } + Ok(stream) + } +} +#[async_trait] +impl SandboxRuntime for NativeSandbox { + async fn proxy_config(&self, id: &str) -> Result { + proxy::read_config(&self.proxy_path(id)?).await + } + async fn set_proxy_config(&self, id: &str, config: SandboxProxyConfig) -> Result<()> { + if self.resolve_template(id, None).await?.profile != "ubuntu-csgclaw" { + return Err(Error::invalid("当前模板不支持代理配置")); + } + proxy::validate(&config)?; + let path = self.proxy_path(id)?; + tokio::fs::create_dir_all(path.parent().unwrap()).await?; + let temporary = path.with_extension("tmp"); + tokio::fs::write(&temporary, serde_json::to_vec(&config)?).await?; + tokio::fs::rename(temporary, path).await?; + Ok(()) + } + + async fn template(&self) -> Result> { + Ok(Some( + self.configured_templates()[0] + .describe(&self.config) + .await?, + )) + } + async fn templates(&self) -> Result> { + let mut result = Vec::new(); + for template in self.configured_templates() { + result.push(template.describe(&self.config).await?); + } + Ok(result) + } + + async fn mcp_config(&self, id: &str) -> Result { + let proxy = self.config.mcp_proxy.as_ref().ok_or_else(|| { + Error::new( + ErrorCode::Unavailable, + "尚未配置宿主机 MCP 连接程序,请设置 mcp_proxy", + ) + })?; + let branch: BranchId = id + .parse() + .map_err(|_| Error::invalid("Invalid Sandbox branch"))?; + Ok(SandboxMcpConfig { + mcp_servers: BTreeMap::from([( + "ubuntu_desktop".into(), + SandboxMcpServer { + command: "python3".into(), + args: vec![ + proxy.to_string_lossy().into_owned(), + "--socket".into(), + self.config + .directory + .join(branch.to_string()) + .join("vsock.sock") + .to_string_lossy() + .into_owned(), + ], + }, + )]), + }) + } + + async fn report_progress(&self, status: SandboxStatus) { + self.progress.lock().await.insert(status.id.clone(), status); + } + + async fn desktop_asset(&self, path: &str) -> Result { + if std::path::Path::new(path) + .components() + .any(|part| !matches!(part, std::path::Component::Normal(_))) + { + return Err(Error::invalid("Invalid desktop asset path")); + } + let root = self.config.novnc_dir.canonicalize()?; + let file = root.join(path).canonicalize()?; + if !file.starts_with(&root) { + return Err(Error::new( + ErrorCode::PermissionDenied, + "Desktop asset escapes its directory", + )); + } + Ok(tokio::fs::read(file).await?.into()) + } + + async fn connect(&self, id: &str, port: u32) -> Result { + Ok(Box::new(self.guest(id, port).await?)) + } + async fn start(&self, request: SandboxLaunch) -> Result { + let mut instances = self.instances.lock().await; + let id = request.id; + let template = self + .resolve_template(&id, request.template_id.as_deref()) + .await?; + if id != request.binding.branch.to_string() { + return Err(Error::invalid("Sandbox identity does not match branch")); + } + if let Some(instance) = instances.get_mut(&id) + && instance.child.try_wait()?.is_none() + { + return Ok(instance.status.clone()); + } + let mode = kernel::HostMode::detect(std::path::Path::new("/sys/module"))?; + let kernel = self.boot_kernel(&template, mode)?; + tracing::info!(sandbox = %id, ?mode, kernel = %kernel.display(), "Sandbox kernel selected"); + let mut active = 0; + let mut occupied = Vec::new(); + for instance in instances.values_mut() { + if instance.child.try_wait()?.is_none() { + active += 1; + occupied.extend(instance.network_slot); + } + } + if active >= self.config.max_instances { + return Err(Error::new( + ErrorCode::CapacityExceeded, + "Sandbox instance limit reached", + )); + } + let network_slot = if self.config.networks.is_empty() { + None + } else { + Some( + (0..self.config.networks.len()) + .find(|slot| !occupied.contains(slot)) + .ok_or_else(|| { + Error::new( + ErrorCode::CapacityExceeded, + "No Sandbox network interface is available", + ) + })?, + ) + }; + self.config.check_capacity(template.memory_gib)?; + let directory = self.config.directory.join(&id); + tokio::fs::create_dir_all(&directory).await?; + tokio::fs::write( + directory.join("template.json"), + serde_json::to_vec_pretty(&template)?, + ) + .await?; + self.step(&id, "prepare_disk").await; + let disk = directory.join("ubuntu.ext4"); + if !disk.exists() { + let result = Command::new("cp") + .args(["--reflink=auto", "--sparse=always", "--"]) + .arg(&template.image) + .arg(&disk) + .status() + .await?; + if !result.success() { + return Err(Error::new(ErrorCode::Io, "Unable to prepare Sandbox disk")); + } + } + let vsock = directory.join("vsock.sock"); + if vsock.exists() { + tokio::fs::remove_file(&vsock).await?; + } + let mut cmdline = + "console=ttyS0 root=/dev/vda rw init=/usr/local/bin/guest-init.sh".to_owned(); + if let Some(slot) = network_slot { + let network = &self.config.networks[slot]; + let flags = tokio::fs::read_to_string(format!("/sys/class/net/{}/flags", network.tap)) + .await + .map_err(|_| { + Error::new( + ErrorCode::Unavailable, + "Sandbox TAP is missing; run prepare-network.sh", + ) + })?; + if u32::from_str_radix(flags.trim().trim_start_matches("0x"), 16).unwrap_or(0) & 1 == 0 + { + return Err(Error::new( + ErrorCode::Unavailable, + "Sandbox TAP is down; run prepare-network.sh", + )); + } + cmdline.push_str(&network.kernel_args()); + } + let mut config = json!({ + "cpus":{"boot_vcpus":template.vcpus,"max_vcpus":template.vcpus},"memory":{"size":u64::from(template.memory_gib)*1024*1024*1024,"shared":true}, + "payload":{"kernel":kernel,"cmdline":cmdline}, + "disks":[{"path":disk}], + "serial":{"mode":"File","file":directory.join("serial.log")},"console":{"mode":"Null"}, + "fs":[{"tag":"workspace","socket":"","backendfs_config":{"shared_dir":request.binding.path,"read_only":false,"writeback":false}}], + "vsock":{"cid":3,"socket":vsock} + }); + if let Some(slot) = network_slot { + config["net"] = json!([{"tap":self.config.networks[slot].tap, "num_queues":2}]); + } + if template.profile == "ubuntu-csgclaw" { + let state = self + .config + .directory + .parent() + .ok_or_else(|| Error::invalid("实例目录缺少父目录"))? + .join("assistant-state") + .join(&id); + tokio::fs::create_dir_all(&state).await?; + let proxy_config = self.proxy_config(&id).await?; + tokio::fs::write( + state.join("network.json"), + serde_json::to_vec(&json!({"proxy_enabled":proxy_config.url.is_some()}))?, + ) + .await?; + tokio::fs::write( + state.join("assistant.json"), + serde_json::to_vec(&json!({"advertise_base_url":format!( + "http://csgclaw.{}.{}.localhost:{}", + request.binding.workspace, request.binding.branch, self.service_port + )}))?, + ) + .await?; + config["fs"].as_array_mut().unwrap().push(json!({"tag":"assistant-state","socket":"","backendfs_config":{"shared_dir":state,"read_only":false,"writeback":false}})); + } + let proxy = if template.profile == "ubuntu-csgclaw" { + Some(proxy::ProxyRelay::start(&directory, self.proxy_path(&id)?).await?) + } else { + None + }; + let config_path = directory.join("vm.json"); + tokio::fs::write(&config_path, serde_json::to_vec_pretty(&config)?).await?; + self.step(&id, "start_vm").await; + tokio::fs::write(directory.join("serial.log"), b"").await?; + let log = std::fs::File::create(directory.join("worker.log"))?; + let mut child = Command::new(&self.config.worker) + .arg(config_path) + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .stderr(log) + .kill_on_drop(true) + .spawn()?; + self.step(&id, "wait_desktop").await; + let mut last_readiness_error = String::new(); + let ready = tokio::time::timeout(std::time::Duration::from_secs(120), async { + loop { + if let Some(exit) = child.try_wait()? { + return Err(Error::new( + ErrorCode::Unavailable, + format!("Sandbox worker exited: {exit}"), + )); + } + if tokio::fs::read_to_string(directory.join("serial.log")) + .await + .unwrap_or_default() + .contains("Kernel panic") + { + return Err(Error::new( + ErrorCode::Unavailable, + "Ubuntu 启动失败,系统启动程序异常退出,请检查实例启动日志", + )); + } + if let Ok(stream) = self.guest(&id, 7001).await { + let mut response = String::new(); + match tokio::time::timeout( + std::time::Duration::from_secs(30), + BufReader::new(stream).read_line(&mut response), + ) + .await + { + Ok(result) => { + result?; + } + Err(_) => continue, + } + if let Ok(value) = serde_json::from_str::(&response) { + if value.get("ready").and_then(|x| x.as_bool()) == Some(true) { + return Ok(()); + } + if let Some(error) = value.get("error").and_then(|x| x.as_str()) { + last_readiness_error = error.chars().take(1000).collect(); + } + } + } + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + } + }) + .await + .map_err(|_| { + Error::new( + ErrorCode::Unavailable, + if last_readiness_error.is_empty() { + "桌面启动检查超时".to_string() + } else { + format!("桌面启动检查超时:{last_readiness_error}") + }, + ) + })?; + ready?; + let status = SandboxStatus { + template_id: template.id.clone(), + services: template.links(), + id: id.clone(), + workspace: request.binding.workspace, + branch: request.binding.branch, + state: SandboxState::Ready, + desktop_url: Some(format!( + "/api/workspaces/{}/branches/{}/sandbox/desktop/vnc.html?autoconnect=1&resize=scale&path=api/workspaces/{}/branches/{}/sandbox/desktop/websockify", + request.binding.workspace, + request.binding.branch, + request.binding.workspace, + request.binding.branch + )), + message: None, + startup_step: Some("ready".into()), + }; + tokio::fs::write( + directory.join("status.json"), + serde_json::to_vec_pretty(&status)?, + ) + .await?; + self.progress.lock().await.remove(&id); + instances.insert( + id, + Instance { + status: status.clone(), + child, + network_slot, + proxy, + }, + ); + Ok(status) + } + async fn stop(&self, id: &str) -> Result<()> { + self.stop_instance(id, false).await + } + async fn force_stop(&self, id: &str) -> Result<()> { + self.stop_instance(id, true).await + } + async fn delete(&self, id: &str) -> Result<()> { + let branch: BranchId = id + .parse() + .map_err(|_| Error::invalid("Invalid Sandbox identity"))?; + if branch.to_string() != id { + return Err(Error::invalid("Invalid Sandbox identity")); + } + let mut instances = self.instances.lock().await; + if let Some(instance) = instances.get_mut(id) + && instance.child.try_wait()?.is_none() + { + return Err(Error::new( + ErrorCode::Busy, + "请先停止 Sandbox,再删除系统磁盘", + )); + } + let mut progress = self.progress.lock().await; + if progress.get(id).is_some_and(|status| { + matches!( + status.state, + SandboxState::Creating | SandboxState::Starting | SandboxState::Stopping + ) + }) { + return Err(Error::new( + ErrorCode::Busy, + "Sandbox 正在执行操作,请完成后再删除", + )); + } + let directory = self.config.directory.join(id); + match tokio::fs::symlink_metadata(&directory).await { + Ok(metadata) => { + if !metadata.is_dir() || metadata.file_type().is_symlink() { + return Err(Error::invalid( + "Sandbox directory must be a regular directory", + )); + } + let root = self.config.directory.canonicalize()?; + let target = directory.canonicalize()?; + if target.parent() != Some(root.as_path()) { + return Err(Error::invalid("Sandbox directory escapes its root")); + } + let mut protected = vec![ + self.config.image.clone(), + self.config.kernel.clone(), + self.config.worker.clone(), + self.config.mount_root.clone(), + self.config.novnc_dir.clone(), + ]; + protected.extend(self.config.mcp_proxy.clone()); + for template in self.configured_templates() { + protected.extend([ + template.kernel.with_file_name("vmlinux-bm"), + template.kernel.with_file_name("vmlinux-pvm"), + ]); + protected.extend([template.image, template.kernel]); + } + if let Ok(bytes) = tokio::fs::read(directory.join("vm.json")).await { + let value: serde_json::Value = serde_json::from_slice(&bytes)?; + if let Some(filesystems) = value.get("fs").and_then(|value| value.as_array()) { + for filesystem in filesystems { + if let Some(path) = filesystem + .pointer("/backendfs_config/shared_dir") + .and_then(|value| value.as_str()) + { + protected.push(PathBuf::from(path)); + } + } + } + } + for path in protected { + let resolved = if path.exists() { + path.canonicalize()? + } else { + path + }; + if resolved.starts_with(&target) { + return Err(Error::invalid( + "Sandbox directory contains a template or shared files", + )); + } + } + tokio::fs::remove_dir_all(&directory).await?; + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => return Err(error.into()), + } + instances.remove(id); + progress.remove(id); + Ok(()) + } + + async fn status(&self, id: &str) -> Result { + if let Some(status) = self.progress.lock().await.get(id).cloned() { + return Ok(status); + } + let mut instances = self.instances.lock().await; + match instances.get_mut(id) { + Some(instance) => { + if let Some(exit) = instance.child.try_wait()? { + instance.proxy.take(); + if !matches!(instance.status.state, SandboxState::Stopped) { + instance.status.message = Some(format!("Sandbox 进程已退出:{exit}")); + } + instance.status.state = SandboxState::Stopped; + instance.status.desktop_url = None; + instance.status.startup_step = None; + } + Ok(instance.status.clone()) + } + None => { + let path = self.config.directory.join(id).join("status.json"); + let mut status: SandboxStatus = + serde_json::from_slice(&tokio::fs::read(path).await?)?; + status.state = SandboxState::Stopped; + status.desktop_url = None; + Ok(status) + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn deletion_runtime(root: &std::path::Path) -> NativeSandbox { + NativeSandbox { + service_port: 17421, + config: Config { + templates: Vec::new(), + worker: root.join("worker"), + kernel: root.join("kernel"), + image: root.join("template.ext4"), + directory: root.join("instances"), + mount_root: root.join("mounts"), + novnc_dir: root.join("novnc"), + mcp_proxy: None, + max_instances: 2, + networks: Vec::new(), + }, + instances: Mutex::new(BTreeMap::new()), + progress: Mutex::new(BTreeMap::new()), + } + } + + #[tokio::test] + async fn guest_handshake_preserves_coalesced_ready_and_flush_responses() { + use tokio::io::AsyncReadExt; + let root = tempfile::tempdir().unwrap(); + let runtime = deletion_runtime(root.path()); + for (port, payload) in [(7001, "{\"ready\":true}\n"), (7002, "{\"flushed\":true}\n")] { + let id = BranchId::new().to_string(); + let directory = runtime.config.directory.join(&id); + std::fs::create_dir_all(&directory).unwrap(); + let listener = tokio::net::UnixListener::bind(directory.join("vsock.sock")).unwrap(); + let server = tokio::spawn(async move { + let (stream, _) = listener.accept().await.unwrap(); + let mut reader = BufReader::new(stream); + let mut request = String::new(); + reader.read_line(&mut request).await.unwrap(); + assert_eq!(request, format!("CONNECT {port}\n")); + reader + .get_mut() + .write_all(format!("OK {port}\n{payload}").as_bytes()) + .await + .unwrap(); + }); + let mut guest = runtime.guest(&id, port).await.unwrap(); + let mut response = String::new(); + guest.read_to_string(&mut response).await.unwrap(); + server.await.unwrap(); + assert_eq!(response, payload); + } + } + + #[tokio::test] + async fn failed_flush_preserves_worker_until_explicit_force_stop() { + let root = tempfile::tempdir().unwrap(); + let runtime = deletion_runtime(root.path()); + let branch = BranchId::new(); + let id = branch.to_string(); + std::fs::create_dir_all(runtime.config.directory.join(&id)).unwrap(); + let child = Command::new("sleep") + .arg("30") + .kill_on_drop(true) + .spawn() + .unwrap(); + runtime.instances.lock().await.insert( + id.clone(), + Instance { + status: SandboxStatus { + template_id: default_sandbox_template(), + services: Vec::new(), + id: id.clone(), + workspace: WorkspaceId::new(), + branch, + state: SandboxState::Ready, + desktop_url: Some("/desktop".into()), + message: None, + startup_step: None, + }, + child, + network_slot: None, + proxy: None, + }, + ); + assert_eq!( + runtime.stop(&id).await.unwrap_err().code, + ErrorCode::Unavailable + ); + assert!( + runtime + .instances + .lock() + .await + .get_mut(&id) + .unwrap() + .child + .try_wait() + .unwrap() + .is_none() + ); + runtime.force_stop(&id).await.unwrap(); + let status = runtime.status(&id).await.unwrap(); + assert!(matches!(status.state, SandboxState::Stopped)); + assert!(status.desktop_url.is_none()); + assert!(status.message.unwrap().contains("强制停止")); + assert!( + runtime + .instances + .lock() + .await + .get_mut(&id) + .unwrap() + .child + .try_wait() + .unwrap() + .is_some() + ); + runtime.force_stop(&id).await.unwrap(); + } + + #[tokio::test] + async fn template_selection_preserves_existing_version_and_legacy_disks() { + let root = tempfile::tempdir().unwrap(); + let mut runtime = deletion_runtime(root.path()); + std::fs::write(&runtime.config.kernel, "kernel").unwrap(); + std::fs::write(&runtime.config.image, "desktop").unwrap(); + let image = root.path().join("assistant.ext4"); + std::fs::write(&image, "assistant").unwrap(); + runtime.config.templates.push(TemplateConfig { + id: "ubuntu-csgclaw".into(), + version: "v1".into(), + image, + kernel: runtime.config.kernel.clone(), + profile: "ubuntu-csgclaw".into(), + vcpus: 4, + memory_gib: 8, + disk_gib: 16, + }); + let branch = BranchId::new().to_string(); + let selected = runtime + .resolve_template(&branch, Some("ubuntu-csgclaw")) + .await + .unwrap(); + assert_eq!(selected.links()[0].id, "csgclaw"); + let directory = runtime.config.directory.join(&branch); + std::fs::create_dir_all(&directory).unwrap(); + std::fs::write( + directory.join("template.json"), + serde_json::to_vec(&selected).unwrap(), + ) + .unwrap(); + runtime.config.templates[0].version = "v2".into(); + assert_eq!( + runtime + .resolve_template(&branch, None) + .await + .unwrap() + .version, + "v1" + ); + assert_eq!( + runtime + .resolve_template(&branch, Some("ubuntu-desktop")) + .await + .unwrap_err() + .code, + ErrorCode::Busy + ); + std::fs::remove_file(directory.join("template.json")).unwrap(); + std::fs::write(directory.join("ubuntu.ext4"), "legacy disk").unwrap(); + assert_eq!( + runtime.resolve_template(&branch, None).await.unwrap().id, + "ubuntu-desktop" + ); + assert!( + runtime + .resolve_template(&branch, Some("ubuntu-csgclaw")) + .await + .is_err() + ); + } + + #[tokio::test] + async fn existing_sandbox_selects_host_kernel_without_changing_template_or_disk() { + let root = tempfile::tempdir().unwrap(); + let mut runtime = deletion_runtime(root.path()); + std::fs::write(&runtime.config.image, "desktop").unwrap(); + let mut saved = runtime.configured_templates().remove(0); + saved.version = "saved-version".into(); + saved.kernel = root.path().join("retired/vmlinux-pvm"); + saved.vcpus = 2; + let branch = BranchId::new().to_string(); + let directory = runtime.config.directory.join(&branch); + std::fs::create_dir_all(&directory).unwrap(); + let metadata = serde_json::to_vec(&saved).unwrap(); + std::fs::write(directory.join("template.json"), &metadata).unwrap(); + std::fs::write(directory.join("ubuntu.ext4"), "existing files").unwrap(); + let deployed = root.path().join("deployed"); + std::fs::create_dir(&deployed).unwrap(); + runtime.config.kernel = deployed.join("vmlinux"); + for name in ["vmlinux-bm", "vmlinux-pvm"] { + std::fs::write(deployed.join(name), name).unwrap(); + } + for (mode, name) in [ + (kernel::HostMode::Pvm, "vmlinux-pvm"), + (kernel::HostMode::Kvm, "vmlinux-bm"), + ] { + let template = runtime.resolve_template(&branch, None).await.unwrap(); + assert_eq!( + runtime.boot_kernel(&template, mode).unwrap(), + deployed.join(name) + ); + assert_eq!(template.version, "saved-version"); + assert_eq!(template.vcpus, 2); + assert_eq!(template.kernel, saved.kernel); + } + assert_eq!( + std::fs::read(directory.join("template.json")).unwrap(), + metadata + ); + assert_eq!( + std::fs::read(directory.join("ubuntu.ext4")).unwrap(), + b"existing files" + ); + } + + #[tokio::test] + async fn deletion_preserves_shared_files_and_template_and_clears_failed_startup() { + let root = tempfile::tempdir().unwrap(); + let runtime = deletion_runtime(root.path()); + let branch = BranchId::new(); + let id = branch.to_string(); + let directory = runtime.config.directory.join(&id); + let shared = runtime.config.mount_root.join(&id); + std::fs::create_dir_all(&directory).unwrap(); + std::fs::create_dir_all(&shared).unwrap(); + std::fs::write(&runtime.config.image, "template").unwrap(); + std::fs::write(shared.join("keep.txt"), "branch content").unwrap(); + std::fs::write(directory.join("ubuntu.ext4"), "system disk").unwrap(); + std::fs::write( + directory.join("vm.json"), + serde_json::json!({"fs":[{"backendfs_config":{"shared_dir":shared}}]}).to_string(), + ) + .unwrap(); + runtime + .report_progress(SandboxStatus { + template_id: default_sandbox_template(), + services: Vec::new(), + id: id.clone(), + workspace: WorkspaceId::new(), + branch, + state: SandboxState::Error, + desktop_url: None, + message: None, + startup_step: None, + }) + .await; + runtime.delete(&id).await.unwrap(); + assert!(!directory.exists()); + assert_eq!( + std::fs::read_to_string(shared.join("keep.txt")).unwrap(), + "branch content" + ); + assert_eq!( + std::fs::read_to_string(&runtime.config.image).unwrap(), + "template" + ); + assert_eq!( + runtime.status(&id).await.unwrap_err().code, + ErrorCode::NotFound + ); + runtime.delete(&id).await.unwrap(); + } + + #[tokio::test] + async fn deletion_rejects_active_instances_and_unsafe_paths() { + let root = tempfile::tempdir().unwrap(); + let mut runtime = deletion_runtime(root.path()); + let branch = BranchId::new(); + let id = branch.to_string(); + let directory = runtime.config.directory.join(&id); + std::fs::create_dir_all(&directory).unwrap(); + let child = tokio::process::Command::new("sleep") + .arg("30") + .kill_on_drop(true) + .spawn() + .unwrap(); + let status = SandboxStatus { + template_id: default_sandbox_template(), + services: Vec::new(), + id: id.clone(), + workspace: WorkspaceId::new(), + branch, + state: SandboxState::Error, + desktop_url: None, + message: None, + startup_step: None, + }; + runtime.instances.lock().await.insert( + id.clone(), + Instance { + status: status.clone(), + child, + network_slot: None, + proxy: None, + }, + ); + assert_eq!(runtime.delete(&id).await.unwrap_err().code, ErrorCode::Busy); + let mut instance = runtime.instances.lock().await.remove(&id).unwrap(); + instance.child.kill().await.unwrap(); + runtime + .report_progress(SandboxStatus { + state: SandboxState::Starting, + ..status + }) + .await; + assert_eq!(runtime.delete(&id).await.unwrap_err().code, ErrorCode::Busy); + runtime.progress.lock().await.clear(); + assert!(runtime.delete("../template").await.is_err()); + runtime.config.image = directory.join("template.ext4"); + std::fs::write(&runtime.config.image, "protected").unwrap(); + assert!(runtime.delete(&id).await.is_err()); + assert!(runtime.config.image.exists()); + runtime.config.image = root.path().join("template.ext4"); + std::fs::write( + directory.join("vm.json"), + serde_json::json!({"fs":[{"backendfs_config":{"shared_dir":directory}}]}).to_string(), + ) + .unwrap(); + assert!(runtime.delete(&id).await.is_err()); + std::fs::remove_dir_all(&directory).unwrap(); + std::os::unix::fs::symlink(root.path(), &directory).unwrap(); + assert!(runtime.delete(&id).await.is_err()); + assert!(root.path().exists()); + } + + #[test] + fn network_requires_distinct_hosts_in_the_same_subnet() { + let mut network = SandboxNetwork { + tap: "adsb0".into(), + guest_ip: "172.30.80.2".parse().unwrap(), + gateway: "172.30.80.1".parse().unwrap(), + dns: vec!["223.5.5.5".parse().unwrap()], + }; + assert!(network.validate().is_ok()); + assert!( + network + .kernel_args() + .contains("agentdrive.ip=172.30.80.2/30") + ); + network.gateway = network.guest_ip; + assert!(network.validate().is_err()); + network.gateway = "172.30.80.5".parse().unwrap(); + assert!(network.validate().is_err()); + network.gateway = "172.30.80.1".parse().unwrap(); + network.guest_ip = "172.30.80.3".parse().unwrap(); + assert!(network.validate().is_err()); + network.guest_ip = "172.30.80.2".parse().unwrap(); + network.tap = "../eth0".into(); + assert!(network.validate().is_err()); + } + + #[tokio::test] + async fn startup_status_remains_available_while_instance_creation_is_locked() { + let runtime = NativeSandbox { + service_port: 17421, + config: Config { + templates: Vec::new(), + worker: PathBuf::new(), + kernel: PathBuf::new(), + image: PathBuf::new(), + directory: PathBuf::new(), + mount_root: PathBuf::new(), + novnc_dir: PathBuf::new(), + mcp_proxy: None, + max_instances: 2, + networks: Vec::new(), + }, + instances: Mutex::new(BTreeMap::new()), + progress: Mutex::new(BTreeMap::new()), + }; + let branch = BranchId::new(); + runtime + .report_progress(SandboxStatus { + template_id: default_sandbox_template(), + services: Vec::new(), + id: branch.to_string(), + workspace: WorkspaceId::new(), + branch, + state: SandboxState::Starting, + desktop_url: None, + message: None, + startup_step: Some("wait_desktop".into()), + }) + .await; + let _busy = runtime.instances.lock().await; + let status = tokio::time::timeout( + std::time::Duration::from_millis(100), + runtime.status(&branch.to_string()), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(status.startup_step.as_deref(), Some("wait_desktop")); + } +} diff --git a/apps/agentdrived/src/sandbox/kernel.rs b/apps/agentdrived/src/sandbox/kernel.rs new file mode 100644 index 0000000..a0d25fa --- /dev/null +++ b/apps/agentdrived/src/sandbox/kernel.rs @@ -0,0 +1,109 @@ +use agentdrive_model::{Error, ErrorCode, Result}; +use std::path::{Path, PathBuf}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum HostMode { + Kvm, + Pvm, +} + +impl HostMode { + pub(super) fn detect(modules: &Path) -> Result { + if modules.join("kvm_pvm").try_exists()? { + Ok(Self::Pvm) + } else if modules.join("kvm").try_exists()? { + Ok(Self::Kvm) + } else { + Err(Error::new( + ErrorCode::Unavailable, + "宿主机尚未启用 KVM/PVM,请加载对应内核模块后启动 Sandbox", + )) + } + } + + pub(super) fn select_kernel(self, configured: &Path) -> Result { + if !configured.is_absolute() { + return Err(Error::invalid("Sandbox 内核必须使用绝对路径")); + } + let name = match self { + Self::Kvm => "vmlinux-bm", + Self::Pvm => "vmlinux-pvm", + }; + let candidate = configured.with_file_name(name); + if candidate.try_exists()? { + if !candidate.is_file() { + return Err(Error::invalid(format!( + "Sandbox 内核必须是文件:{}", + candidate.display() + ))); + } + return Ok(candidate); + } + // 兼容旧 KVM 配置,同时防止将指向 PVM 内核的符号链接用于 KVM。 + if self == Self::Kvm && configured.try_exists()? { + let target = configured.canonicalize()?; + if target.is_file() && target.file_name().is_some_and(|name| name != "vmlinux-pvm") { + return Ok(configured.to_path_buf()); + } + } + Err(Error::new( + ErrorCode::Unavailable, + format!( + "宿主机运行模式为 {self:?},缺少对应 Sandbox 内核:{}", + candidate.display() + ), + )) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn detects_loaded_backend_and_rejects_missing_modules() { + let root = tempfile::tempdir().unwrap(); + assert_eq!( + HostMode::detect(root.path()).unwrap_err().code, + ErrorCode::Unavailable + ); + std::fs::create_dir(root.path().join("kvm")).unwrap(); + assert_eq!(HostMode::detect(root.path()).unwrap(), HostMode::Kvm); + std::fs::create_dir(root.path().join("kvm_pvm")).unwrap(); + assert_eq!(HostMode::detect(root.path()).unwrap(), HostMode::Pvm); + } + + #[test] + fn selects_matching_kernel_and_preserves_legacy_kvm_support() { + let root = tempfile::tempdir().unwrap(); + let legacy = root.path().join("vmlinux"); + std::fs::write(&legacy, "legacy").unwrap(); + assert_eq!(HostMode::Kvm.select_kernel(&legacy).unwrap(), legacy); + assert_eq!( + HostMode::Pvm.select_kernel(&legacy).unwrap_err().code, + ErrorCode::Unavailable + ); + let pvm = root.path().join("vmlinux-pvm"); + let kvm = root.path().join("vmlinux-bm"); + std::fs::write(&pvm, "pvm").unwrap(); + std::fs::write(&kvm, "kvm").unwrap(); + std::fs::remove_file(&legacy).unwrap(); + assert_eq!(HostMode::Kvm.select_kernel(&legacy).unwrap(), kvm); + assert_eq!(HostMode::Pvm.select_kernel(&legacy).unwrap(), pvm); + assert_eq!(HostMode::Kvm.select_kernel(&pvm).unwrap(), kvm); + std::fs::remove_file(&kvm).unwrap(); + assert!(HostMode::Kvm.select_kernel(&pvm).is_err()); + } + + #[cfg(unix)] + #[test] + fn rejects_pvm_symlink_as_legacy_kvm_kernel() { + let root = tempfile::tempdir().unwrap(); + let legacy = root.path().join("vmlinux"); + let pvm = root.path().join("vmlinux-pvm"); + std::fs::write(&pvm, "pvm").unwrap(); + std::os::unix::fs::symlink(&pvm, &legacy).unwrap(); + assert!(HostMode::Kvm.select_kernel(&legacy).is_err()); + assert_eq!(HostMode::Pvm.select_kernel(&legacy).unwrap(), pvm); + } +} diff --git a/apps/agentdrived/src/sandbox/proxy.rs b/apps/agentdrived/src/sandbox/proxy.rs new file mode 100644 index 0000000..6b0317b --- /dev/null +++ b/apps/agentdrived/src/sandbox/proxy.rs @@ -0,0 +1,163 @@ +use super::*; +use std::path::Path; +use tokio::net::{TcpStream, UnixListener}; + +pub(super) fn validate(config: &SandboxProxyConfig) -> Result> { + let Some(value) = &config.url else { + return Ok(None); + }; + let uri: axum::http::Uri = value.parse().map_err(|_| Error::invalid("代理地址无效"))?; + if uri.scheme_str() != Some("http") + || uri.authority().is_none() + || uri.authority().unwrap().as_str().contains('@') + || uri + .path_and_query() + .is_some_and(|path| path.as_str() != "/") + { + return Err(Error::invalid( + "请输入不含账号、密码或路径的 HTTP 代理地址,例如 http://127.0.0.1:7890", + )); + } + Ok(Some(( + uri.host().unwrap().trim_matches(['[', ']']).into(), + uri.port_u16().unwrap_or(80), + ))) +} + +pub(super) async fn read_config(path: &Path) -> Result { + match tokio::fs::read(path).await { + Ok(bytes) => Ok(serde_json::from_slice(&bytes)?), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + Ok(SandboxProxyConfig::default()) + } + Err(error) => Err(error.into()), + } +} + +impl NativeSandbox { + pub(super) fn proxy_path(&self, id: &str) -> Result { + let branch: BranchId = id.parse().map_err(|_| Error::invalid("Sandbox 分支无效"))?; + if branch.to_string() != id { + return Err(Error::invalid("Sandbox 分支无效")); + } + Ok(self + .config + .directory + .parent() + .ok_or_else(|| Error::invalid("实例目录缺少父目录"))? + .join("proxy-settings") + .join(format!("{id}.json"))) + } +} + +// 专用 vsock 入口只转发到本机用户配置的代理,不开放宿主机网络访问。 +#[derive(Debug)] +pub(super) struct ProxyRelay(tokio::task::JoinHandle<()>); +impl Drop for ProxyRelay { + fn drop(&mut self) { + self.0.abort(); + } +} +impl ProxyRelay { + pub(super) async fn start(directory: &Path, config_path: PathBuf) -> Result { + let target = validate(&read_config(&config_path).await?)?; + let path = directory.join("vsock.sock_18081"); + match tokio::fs::remove_file(&path).await { + Ok(()) => {} + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => return Err(error.into()), + } + let listener = UnixListener::bind(path)?; + Ok(Self(tokio::spawn(async move { + let mut connections = tokio::task::JoinSet::new(); + loop { + tokio::select! { + result = listener.accept(), if connections.len() < 64 => { + let Ok((mut guest, _)) = result else { break }; + let Some((host, port)) = target.clone() else { continue }; + connections.spawn(async move { + if let Ok(Ok(mut upstream)) = tokio::time::timeout( + std::time::Duration::from_secs(10), TcpStream::connect((host.as_str(), port)) + ).await { + let _ = tokio::io::copy_bidirectional(&mut guest, &mut upstream).await; + } + }); + } + _ = connections.join_next(), if !connections.is_empty() => {}, + } + } + }))) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + #[tokio::test] + async fn relay_preserves_connect_bytes_and_stops_with_the_instance() { + let directory = tempfile::tempdir().unwrap(); + let upstream = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let config = directory.path().join("proxy.json"); + tokio::fs::write( + &config, + serde_json::to_vec(&SandboxProxyConfig { + url: Some(format!("http://{}", upstream.local_addr().unwrap())), + }) + .unwrap(), + ) + .await + .unwrap(); + let relay = ProxyRelay::start(directory.path(), config).await.unwrap(); + let mut guest = tokio::net::UnixStream::connect(directory.path().join("vsock.sock_18081")) + .await + .unwrap(); + guest + .write_all(b"CONNECT example.com:443 HTTP/1.1\r\n\r\n") + .await + .unwrap(); + let (mut remote, _) = upstream.accept().await.unwrap(); + let mut request = [0; 36]; + remote.read_exact(&mut request).await.unwrap(); + assert_eq!(&request, b"CONNECT example.com:443 HTTP/1.1\r\n\r\n"); + remote.write_all(b"HTTP/1.1 200 OK\r\n\r\n").await.unwrap(); + let mut response = [0; 19]; + guest.read_exact(&mut response).await.unwrap(); + assert_eq!(&response, b"HTTP/1.1 200 OK\r\n\r\n"); + drop(relay); + assert_eq!( + tokio::time::timeout(std::time::Duration::from_secs(2), guest.read(&mut response)) + .await + .unwrap() + .unwrap(), + 0 + ); + } + + #[test] + fn proxy_configuration_accepts_only_an_http_proxy_authority() { + for value in [ + "socks5://localhost:7890", + "http://user:secret@localhost:7890", + "http://localhost/path", + "http://localhost/?x=1", + "file:///tmp/proxy", + ] { + assert!( + validate(&SandboxProxyConfig { + url: Some(value.into()) + }) + .is_err() + ); + } + assert_eq!( + validate(&SandboxProxyConfig { + url: Some("http://127.0.0.1:7890".into()) + }) + .unwrap(), + Some(("127.0.0.1".into(), 7890)) + ); + assert!(validate(&SandboxProxyConfig::default()).unwrap().is_none()); + } +} diff --git a/apps/agentdrived/src/sandbox/shutdown.rs b/apps/agentdrived/src/sandbox/shutdown.rs new file mode 100644 index 0000000..028ad81 --- /dev/null +++ b/apps/agentdrived/src/sandbox/shutdown.rs @@ -0,0 +1,103 @@ +use super::*; +use std::time::Duration; + +async fn terminate(child: &mut Child, force: bool, timeout: Duration) -> Result<()> { + if child.try_wait()?.is_some() { + return Ok(()); + } + if !force { + let stopped = tokio::time::timeout(timeout, async { + if let Some(stdin) = child.stdin.as_mut() { + stdin.write_all(b"stop\n").await?; + } + child.wait().await + }) + .await; + if matches!(stopped, Ok(Ok(_))) { + return Ok(()); + } + } + child.start_kill()?; + tokio::time::timeout(timeout, child.wait()) + .await + .map_err(|_| { + Error::new( + ErrorCode::Unavailable, + "等待 Sandbox 进程退出超时,请稍后重试", + ) + })??; + Ok(()) +} + +impl NativeSandbox { + pub(super) async fn stop_instance(&self, id: &str, force: bool) -> Result<()> { + let mut instances = self.instances.lock().await; + if let Some(instance) = instances.get_mut(id) { + if !force && instance.child.try_wait()?.is_none() { + let flushed = tokio::time::timeout(Duration::from_secs(30), async { + let mut stream = self.guest(id, 7002).await?; + let mut response = String::new(); + stream.read_line(&mut response).await?; + let value: serde_json::Value = serde_json::from_str(&response)?; + if value.get("flushed").and_then(|value| value.as_bool()) != Some(true) { + return Err(Error::new(ErrorCode::Io, "Sandbox 未确认文件同步")); + } + Ok::<_, Error>(()) + }) + .await; + let error = match flushed { + Ok(Ok(())) => None, + Ok(Err(error)) => Some(error.message), + Err(_) => Some("文件同步超时".into()), + }; + if let Some(error) = error { + instance.status.message = + Some(format!("正常停止失败:{error}。可以重试,或使用强制停止。")); + return Err(Error::new( + ErrorCode::Unavailable, + instance.status.message.clone().unwrap(), + )); + } + } + terminate(&mut instance.child, force, Duration::from_secs(5)).await?; + instance.proxy.take(); + instance.status.state = SandboxState::Stopped; + instance.status.desktop_url = None; + instance.status.startup_step = None; + instance.status.message = + force.then(|| "已强制停止;guest 中尚未同步的修改可能丢失。".into()); + self.progress.lock().await.remove(id); + tokio::fs::write( + self.config.directory.join(id).join("status.json"), + serde_json::to_vec_pretty(&instance.status)?, + ) + .await?; + return Ok(()); + } + let directory = self.config.directory.join(id); + if directory.join("status.json").is_file() { + self.progress.lock().await.remove(id); + return Ok(()); + } + Err(Error::new(ErrorCode::NotFound, "Sandbox does not exist")) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn unresponsive_worker_is_killed_and_reaped_after_timeout() { + let mut child = Command::new("sleep") + .arg("30") + .stdin(Stdio::piped()) + .kill_on_drop(true) + .spawn() + .unwrap(); + terminate(&mut child, false, Duration::from_millis(50)) + .await + .unwrap(); + assert!(child.try_wait().unwrap().is_some()); + } +} diff --git a/apps/agentdrived/src/sandbox/templates.rs b/apps/agentdrived/src/sandbox/templates.rs new file mode 100644 index 0000000..85c4997 --- /dev/null +++ b/apps/agentdrived/src/sandbox/templates.rs @@ -0,0 +1,145 @@ +use super::*; + +#[derive(Clone, Debug, Deserialize, serde::Serialize)] +#[serde(deny_unknown_fields)] +pub struct TemplateConfig { + pub id: String, + pub version: String, + pub image: PathBuf, + pub kernel: PathBuf, + #[serde(default = "default_profile")] + pub profile: String, + #[serde(default = "default_cpus")] + pub vcpus: u32, + #[serde(default = "default_memory")] + pub memory_gib: u32, + #[serde(default = "default_disk")] + pub disk_gib: u32, +} +fn default_profile() -> String { + "ubuntu".into() +} +fn default_cpus() -> u32 { + 4 +} +fn default_memory() -> u32 { + 8 +} +fn default_disk() -> u32 { + 16 +} + +impl TemplateConfig { + pub fn validate(&self) -> Result<()> { + if self.id.is_empty() + || !self + .id + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') + || !matches!(self.profile.as_str(), "ubuntu" | "ubuntu-csgclaw") + || self.version.is_empty() + || self.vcpus == 0 + || self.memory_gib == 0 + || self.disk_gib == 0 + { + return Err(Error::invalid("模板标识、运行配置或资源配置无效")); + } + if !self.image.is_absolute() || !self.image.is_file() || !self.kernel.is_absolute() { + return Err(Error::invalid( + "模板镜像必须为已存在的绝对路径,内核必须使用绝对路径", + )); + } + Ok(()) + } + pub fn links(&self) -> Vec { + if self.profile != "ubuntu-csgclaw" { + return Vec::new(); + } + [("csgclaw", "打开助手")] + .into_iter() + .map(|(id, label)| SandboxServiceLink { + id: id.into(), + label: label.into(), + }) + .collect() + } + pub async fn describe(&self, config: &Config) -> Result { + let manifest = match tokio::fs::read(self.image.with_extension("template.json")).await { + Ok(bytes) => Some(serde_json::from_slice(&bytes)?), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => None, + Err(error) => return Err(error.into()), + }; + let mut capabilities = vec!["desktop".into(), "browser".into(), "mcp".into()]; + capabilities.extend(self.links().into_iter().map(|link| link.id)); + Ok(SandboxTemplate { + id: self.id.clone(), + version: self.version.clone(), + capabilities, + manifest, + vcpus: self.vcpus, + memory_gib: self.memory_gib, + disk_gib: self.disk_gib, + max_instances: config.max_instances, + network_slots: config.networks.len(), + }) + } +} +impl NativeSandbox { + pub(super) fn boot_kernel( + &self, + template: &TemplateConfig, + mode: super::kernel::HostMode, + ) -> Result { + // 实例保留系统盘和模板版本,启动内核采用当前部署配置。 + let configured = self + .configured_templates() + .into_iter() + .find(|item| item.id == template.id); + mode.select_kernel(configured.as_ref().unwrap_or(template).kernel.as_path()) + } + pub(super) fn configured_templates(&self) -> Vec { + let mut templates = vec![TemplateConfig { + id: default_sandbox_template(), + version: "local".into(), + image: self.config.image.clone(), + kernel: self.config.kernel.clone(), + profile: "ubuntu".into(), + vcpus: 4, + memory_gib: 8, + disk_gib: 16, + }]; + templates.extend(self.config.templates.clone()); + templates + } + pub(super) async fn resolve_template( + &self, + id: &str, + requested: Option<&str>, + ) -> Result { + let directory = self.config.directory.join(id); + let saved = directory.join("template.json"); + let template = match tokio::fs::read(&saved).await { + Ok(bytes) => serde_json::from_slice::(&bytes)?, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + let name = if directory.join("ubuntu.ext4").exists() { + default_sandbox_template() + } else { + requested.unwrap_or("ubuntu-desktop").into() + }; + self.configured_templates() + .into_iter() + .find(|item| item.id == name) + .ok_or_else(|| Error::invalid("模板不存在"))? + } + Err(error) => return Err(error.into()), + }; + if requested.is_some_and(|value| value != template.id) { + return Err(Error::new( + ErrorCode::Busy, + "已有 Sandbox 使用其他模板,请删除后重新创建", + )); + } + template.validate()?; + Ok(template) + } +} diff --git a/build/sandbox/.dockerignore b/build/sandbox/.dockerignore new file mode 100644 index 0000000..579aa76 --- /dev/null +++ b/build/sandbox/.dockerignore @@ -0,0 +1,2 @@ +mcp/node_modules +**/__pycache__ diff --git a/build/sandbox/.gitignore b/build/sandbox/.gitignore new file mode 100644 index 0000000..0b6aefa --- /dev/null +++ b/build/sandbox/.gitignore @@ -0,0 +1,2 @@ +.cache/ +__pycache__/ diff --git a/build/sandbox/64-agentdrive-fonts.conf b/build/sandbox/64-agentdrive-fonts.conf new file mode 100644 index 0000000..a2fb9d6 --- /dev/null +++ b/build/sandbox/64-agentdrive-fonts.conf @@ -0,0 +1,16 @@ + + + + + sans-serif + DejaVu SansNoto Sans CJK SCNoto Color Emoji + + + serif + DejaVu SerifNoto Serif CJK SCNoto Color Emoji + + + monospace + DejaVu Sans MonoNoto Sans Mono CJK SCNoto Color Emoji + + diff --git a/build/sandbox/90-cube-ubuntu-desktop.gschema.override b/build/sandbox/90-cube-ubuntu-desktop.gschema.override new file mode 100644 index 0000000..92cd612 --- /dev/null +++ b/build/sandbox/90-cube-ubuntu-desktop.gschema.override @@ -0,0 +1,21 @@ +# Only pin apps that this image actually ships. ubuntu-settings defaults +# include Firefox snap / Thunderbird / LibreOffice / ubiquity, which show +# up as broken dock icons. +[org.gnome.shell] +favorite-apps=['org.gnome.Nautilus.desktop', 'org.gnome.Terminal.desktop', 'chromium.desktop'] +enabled-extensions=['ubuntu-dock@ubuntu.com', 'ding@rastersoft.com'] + +[org.gnome.desktop.interface] +toolkit-accessibility=true +gtk-theme='Yaru' +icon-theme='Yaru' +cursor-theme='Yaru' +font-name='Ubuntu 11' + +[org.gnome.shell.extensions.dash-to-dock] +dock-position='LEFT' +extend-height=true +dock-fixed=true +show-show-apps-button=true +show-apps-at-top=false +disable-overview-on-startup=true diff --git a/build/sandbox/Dockerfile b/build/sandbox/Dockerfile new file mode 100644 index 0000000..40e836b --- /dev/null +++ b/build/sandbox/Dockerfile @@ -0,0 +1,135 @@ +ARG CUBE_BASE_IMAGE=ghcr.io/tencentcloud/cubesandbox-base:2026.16 +FROM node:22-bookworm-slim@sha256:e9f565d32859f0cbfb52125eafe8be39390f24dac302dc3cdf566e8ba3ab21ee AS mcp-build +WORKDIR /opt/agentdrive-mcp +COPY mcp/package.json mcp/package-lock.json ./ +RUN npm ci --ignore-scripts --omit=dev --no-audit --no-fund + +FROM ${CUBE_BASE_IMAGE} + +ARG DEBIAN_FRONTEND=noninteractive +# archive.ubuntu.com is often unusable from China and makes the GNOME +# install look hung for tens of minutes. Override with --build-arg APT_MIRROR= +# to use upstream, or another mirror. +ARG APT_MIRROR=http://mirrors.tencent.com/ubuntu + +ENV DISPLAY=:0 \ + SCREEN_WIDTH=1280 \ + SCREEN_HEIGHT=720 \ + SCREEN_DEPTH=24 \ + VNC_PORT=5900 \ + NOVNC_PORT=6080 \ + DESKTOP_USER=user \ + LANG=C.UTF-8 \ + XDG_SESSION_TYPE=x11 \ + XDG_CURRENT_DESKTOP=ubuntu:GNOME \ + GNOME_SHELL_SESSION_MODE=ubuntu \ + LIBGL_ALWAYS_SOFTWARE=1 \ + GALLIUM_DRIVER=llvmpipe + +RUN if [ -n "${APT_MIRROR}" ]; then \ + sed -i \ + -e "s|http://archive.ubuntu.com/ubuntu|${APT_MIRROR}|g" \ + -e "s|http://security.ubuntu.com/ubuntu|${APT_MIRROR}|g" \ + /etc/apt/sources.list; \ + fi \ + && apt-get -o Acquire::Retries=3 -o Acquire::http::Timeout=20 update \ + && apt-get -o Acquire::Retries=3 -y --no-install-recommends install \ + xserver-xorg-core \ + xserver-xorg-video-dummy \ + xserver-xorg-legacy \ + x11-xserver-utils \ + x11-utils \ + xdg-utils \ + xvfb \ + dbus-x11 \ + dbus-user-session \ + ubuntu-session \ + ubuntu-settings \ + ubuntu-wallpapers \ + gnome-shell \ + gnome-session \ + gnome-terminal \ + nautilus \ + gnome-shell-extension-ubuntu-dock \ + gnome-shell-extension-desktop-icons-ng \ + yaru-theme-gtk \ + yaru-theme-icon \ + yaru-theme-gnome-shell \ + fonts-ubuntu \ + libgl1-mesa-dri \ + mesa-utils \ + librsvg2-common \ + libglib2.0-bin \ + at-spi2-core \ + x11vnc \ + novnc \ + websockify \ + python3 \ + python3-tk \ + python3-xlib \ + gnome-screenshot \ + wmctrl \ + ffmpeg \ + xclip \ + xdotool \ + gedit \ + epiphany-browser \ + fonts-dejavu-core \ + sudo \ + iproute2 \ + procps \ + e2fsprogs \ + ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +RUN id -u user >/dev/null 2>&1 || useradd -m -s /bin/bash user + + +COPY xorg.conf /etc/X11/xorg.conf +COPY Xwrapper.config /etc/X11/Xwrapper.config +COPY start-desktop.sh /usr/local/bin/start-desktop.sh +COPY novnc-index.html /usr/share/novnc/index.html +COPY 90-cube-ubuntu-desktop.gschema.override /usr/share/glib-2.0/schemas/90-cube-ubuntu-desktop.gschema.override + +RUN glib-compile-schemas /usr/share/glib-2.0/schemas \ + && chmod 755 /usr/local/bin/start-desktop.sh + + + +# Keep cubesandbox-base ENTRYPOINT (envd in the background). +CMD ["/usr/local/bin/start-desktop.sh"] + +ARG COMPUTER_USE_VERSION=0.7.53 +ADD https://github.com/agent-sh/computer-use-linux/releases/download/v${COMPUTER_USE_VERSION}/computer-use-linux-x86_64-unknown-linux-gnu /tmp/computer-use-linux +RUN echo "9105b0caefe67881bd9549a51c59252d2f1dfe4306d9cc71be0091cd3085d122 /tmp/computer-use-linux" | sha256sum -c - \ + && install -m 0755 /tmp/computer-use-linux /usr/local/bin/computer-use-linux \ + && rm /tmp/computer-use-linux + +COPY guest-init.sh guest-helper.py desktop_environment.py desktop_mcp.py /usr/local/bin/ +RUN chmod 755 /usr/local/bin/guest-init.sh /usr/local/bin/guest-helper.py + +RUN apt-get -o Acquire::Retries=3 update \ + && apt-get -y --no-install-recommends install \ + fonts-noto-cjk fonts-noto-color-emoji \ + libnss3 libnspr4 libasound2 libgbm1 libcups2 libatk-bridge2.0-0 libxkbcommon0 \ + && rm -rf /var/lib/apt/lists/* + +ARG CHROMIUM_ARCHIVE=https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/1743918/chrome-linux.zip +ADD ${CHROMIUM_ARCHIVE} /tmp/chromium.zip +COPY install-chromium.py chromium.json /tmp/chromium-install/ +RUN python3 /tmp/chromium-install/install-chromium.py /tmp/chromium.zip /tmp/chromium-install/chromium.json /opt/chromium \ + && rm -rf /tmp/chromium.zip /tmp/chromium-install + +COPY chromium /usr/local/bin/chromium +COPY chromium.desktop /usr/share/applications/chromium.desktop +COPY 64-agentdrive-fonts.conf /etc/fonts/conf.d/64-agentdrive-fonts.conf +RUN chmod 755 /usr/local/bin/chromium \ + && update-desktop-database /usr/share/applications \ + && fc-cache -f + +COPY --from=mcp-build /usr/local/bin/node /usr/local/bin/node +COPY --from=mcp-build /opt/agentdrive-mcp /opt/agentdrive-mcp +COPY mcp/gateway.mjs mcp/providers.json mcp/playwright.json /opt/agentdrive-mcp/ +COPY browser-session.py /usr/local/bin/browser-session.py +COPY template-manifest.py /usr/local/bin/template-manifest.py +RUN python3 /usr/local/bin/template-manifest.py "${COMPUTER_USE_VERSION}" > /opt/agentdrive-template.json diff --git a/build/sandbox/Xwrapper.config b/build/sandbox/Xwrapper.config new file mode 100644 index 0000000..ddaa564 --- /dev/null +++ b/build/sandbox/Xwrapper.config @@ -0,0 +1,2 @@ +allowed_users=anybody +needs_root_rights=yes diff --git a/build/sandbox/assistant/Dockerfile b/build/sandbox/assistant/Dockerfile new file mode 100644 index 0000000..14b462a --- /dev/null +++ b/build/sandbox/assistant/Dockerfile @@ -0,0 +1,11 @@ +FROM agentdrive-ubuntu:local +USER root +COPY .cache/csgclaw-release.tar.gz /tmp/csgclaw.tar.gz +RUN echo '80083df3224a5d9727c43ce86e580a998a5009f37f1d8446aee487eaf6ae9b97 /tmp/csgclaw.tar.gz' | sha256sum -c - \ + && tar -xzf /tmp/csgclaw.tar.gz -C /opt && rm /tmp/csgclaw.tar.gz \ + && ln -s /opt/csgclaw/bin/csgclaw /usr/local/bin/csgclaw +COPY guest-init.sh guest-helper.py browser-session.py desktop_environment.py desktop_mcp.py /usr/local/bin/ +COPY assistant/assistant-mcp.py assistant/assistant-service.py /usr/local/bin/ +COPY assistant/runtime.json /etc/agentdrive-runtime.json +COPY mcp/gateway.mjs mcp/providers.json mcp/playwright.json /opt/agentdrive-mcp/ +RUN python3 -c 'import json,datetime; p="/opt/agentdrive-template.json"; d=json.load(open(p)); d["name"]="Ubuntu Desktop + CSGClaw"; d["built_at"]=datetime.datetime.now(datetime.timezone.utc).isoformat(); d["tools"].append("CSGClaw v0.10.0 (Web UI + Agent)"); json.dump(d,open(p,"w"),ensure_ascii=False,indent=2)' diff --git a/build/sandbox/assistant/assistant-mcp.py b/build/sandbox/assistant/assistant-mcp.py new file mode 100644 index 0000000..74c2c97 --- /dev/null +++ b/build/sandbox/assistant/assistant-mcp.py @@ -0,0 +1,17 @@ +"""助手连接桌面用户的统一 MCP,继承实际桌面会话环境。""" +import os +import pwd +import importlib.util +spec = importlib.util.spec_from_file_location("helper", "/usr/local/bin/guest-helper.py") +helper = importlib.util.module_from_spec(spec) +spec.loader.exec_module(helper) +desktop_session, DESKTOP_USER = helper.desktop_session, helper.DESKTOP_USER + +environment = desktop_session() +user = pwd.getpwnam(DESKTOP_USER) +if os.getuid() == 0: + os.setgroups([]) + os.setgid(user.pw_gid) + os.setuid(user.pw_uid) +os.chdir('/workspace') +os.execve('/usr/local/bin/node', ['node', '/opt/agentdrive-mcp/gateway.mjs'], environment) diff --git a/build/sandbox/assistant/assistant-service.py b/build/sandbox/assistant/assistant-service.py new file mode 100644 index 0000000..5ce9215 --- /dev/null +++ b/build/sandbox/assistant/assistant-service.py @@ -0,0 +1,152 @@ +"""启动分支助手及其代理连接,助手数据保存在独立的持久目录。""" +import json +import os +from pathlib import Path +import pwd +import select +import socket +import subprocess +import threading +import time +import urllib.error +import urllib.request +from urllib.parse import urlsplit +from uuid import UUID + +STATE = Path('/agent-state/csgclaw') +USER = pwd.getpwnam('user') +BASE_URL = 'http://127.0.0.1:18080' +CLIENT = urllib.request.build_opener(urllib.request.ProxyHandler({})) + + +def assistant_command(metadata): + origin = metadata['advertise_base_url'] + url = urlsplit(origin) + labels = (url.hostname or '').split('.') + if (url.scheme != 'http' or url.username or url.password or not url.port + or url.path or url.query or url.fragment or len(labels) != 4 + or labels[0] != 'csgclaw' or labels[3] != 'localhost'): + raise ValueError('助手访问地址无效') + if any(str(UUID(value)) != value for value in labels[1:3]): + raise ValueError('助手工作区或分支标识无效') + # 启动参数覆盖已有配置,使访问地址随宿主机端口和分支自动更新。 + return ['/opt/csgclaw/bin/csgclaw', '--endpoint', origin, 'serve', '--no-browser'] + + +def request(path, data=None): + payload = None if data is None else json.dumps(data).encode() + req = urllib.request.Request(BASE_URL + path, data=payload, + headers={'Content-Type': 'application/json'}) + with CLIENT.open(req, timeout=90) as response: + return json.load(response) + + +def ensure_participant(): + try: + request('/api/v1/channels/csgclaw/participants/pt-agentdrive') + except urllib.error.HTTPError as error: + if error.code != 404: + raise + request('/api/v1/channels/csgclaw/participants', { + 'id': 'agentdrive', 'type': 'agent', 'name': 'agentdrive', + 'channel_user': {'ref':'user-agentdrive', 'kind':'local_user_id'}, + 'agent_binding': {'mode':'reuse', 'agent_id':'agent-agentdrive'}, + }) + + +def bootstrap(): + """通过 CSGClaw API 配置工作目录与统一 MCP,保留用户已有设置。""" + while True: + try: + try: + request('/api/v1/agents/agentdrive') + ensure_participant() + return + except urllib.error.HTTPError as error: + if error.code != 404: + raise + request('/api/v1/agents', { + 'id': 'agentdrive', 'name': 'agentdrive', 'runtime': {'name':'codex','sandbox_enabled':False}, + 'description': '操作当前 AgentDrive 分支文件和桌面浏览器', + 'instructions': '工作目录为 /workspace,它对应当前 AgentDrive 分支。文件操作必须保存在此目录。网页操作使用 ubuntu_desktop MCP 中的 browser_* 工具,与桌面共用 Chromium。下载保存到 /workspace/Downloads。桌面操作使用同一 MCP 的桌面工具。', + 'runtime_options': {'local_workspace_dir': '/workspace'}, + 'mcpServers': {'ubuntu_desktop': { + 'command': '/usr/bin/python3', + 'args': ['/usr/local/bin/assistant-mcp.py'], + 'startup_timeout_sec': 60, 'tool_timeout_sec': 120, + }}, + }) + ensure_participant() + return + except Exception as error: + # 模型尚未配置时,保留 Web UI 供用户填写模型配置。 + print('Assistant initialization pending:', type(error).__name__, flush=True) + time.sleep(15) + + +def forward(client): + with client: + try: + with socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM) as upstream: + upstream.settimeout(10) + upstream.connect((2, 18081)) + upstream.settimeout(None) + while True: + readable, _, _ = select.select([client, upstream], [], []) + for source in readable: + data = source.recv(65536) + if not data: + return + (upstream if source is client else client).sendall(data) + except OSError: + return + + +def proxy_listener(): + with socket.socket() as server: + server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + server.bind(('127.0.0.1', 18081)) + server.listen(32) + while True: + client, _ = server.accept() + threading.Thread(target=forward, args=(client,), daemon=True).start() + + +def main(): + if not os.path.ismount('/workspace') or not os.path.ismount('/agent-state'): + raise RuntimeError('助手需要分支目录和状态目录挂载完成') + command = assistant_command(json.loads(Path('/agent-state/assistant.json').read_text())) + STATE.mkdir(mode=0o700, parents=True, exist_ok=True) + os.chown(STATE, USER.pw_uid, USER.pw_gid) + home_state = Path(USER.pw_dir) / '.csgclaw' + if not home_state.exists(): + home_state.symlink_to(STATE, target_is_directory=True) + if home_state.resolve() != STATE: + raise RuntimeError('助手状态目录无效') + config = STATE / 'config.toml' + if not config.exists(): + config.write_text('[server]\nlisten_addr = "127.0.0.1:18080"\nno_auth = true\nshow_upgrade = false\n') + config.chmod(0o600) + os.chown(config, USER.pw_uid, USER.pw_gid) + auth = Path('/agent-state/codex-auth') + if auth.is_dir() and not (Path(USER.pw_dir) / '.codex').exists(): + (Path(USER.pw_dir) / '.codex').symlink_to(auth, target_is_directory=True) + environment = dict(os.environ, HOME=USER.pw_dir, USER='user', LANG='C.UTF-8', + PATH='/opt/csgclaw/bin:/usr/local/bin:/usr/bin:/bin', + NO_PROXY='localhost,127.0.0.1,::1') + if json.loads(Path('/agent-state/network.json').read_text()).get('proxy_enabled'): + environment.update(HTTP_PROXY='http://127.0.0.1:18081', HTTPS_PROXY='http://127.0.0.1:18081', + http_proxy='http://127.0.0.1:18081', https_proxy='http://127.0.0.1:18081') + threading.Thread(target=proxy_listener, daemon=True).start() + threading.Thread(target=bootstrap, daemon=True).start() + while True: + with open('/var/log/csgclaw.log', 'ab') as log: + process = subprocess.Popen(command, + env=environment, cwd='/workspace', user='user', group='user', + extra_groups=[], stdout=log, stderr=log) + process.wait() + time.sleep(3) + + +if __name__ == '__main__': + main() diff --git a/build/sandbox/assistant/runtime.json b/build/sandbox/assistant/runtime.json new file mode 100644 index 0000000..8888316 --- /dev/null +++ b/build/sandbox/assistant/runtime.json @@ -0,0 +1 @@ +{"assistant":"csgclaw","user":"user","session_process":"gnome-session-b","window_manager":"GNOME Shell"} diff --git a/build/sandbox/browser-session.py b/build/sandbox/browser-session.py new file mode 100644 index 0000000..57d2e9d --- /dev/null +++ b/build/sandbox/browser-session.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +"""让桌面入口和浏览器 MCP 共用 Chromium 用户目录与下载目录。""" +import fcntl +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import time +import urllib.request + +BINARY = '/opt/chromium/chrome' +PROFILE = Path.home() / '.config/agentdrive-chromium' +DOWNLOADS = Path('/workspace/Downloads') +ENDPOINT = 'http://127.0.0.1:9222/json/version' + + +def available(): + try: + with urllib.request.urlopen(ENDPOINT, timeout=1) as response: + return bool(json.load(response).get('webSocketDebuggerUrl')) + except (OSError, ValueError): + return False + + +def prepare(): + if not os.path.ismount('/workspace'): + raise RuntimeError('当前分支 /workspace 尚未挂载') + DOWNLOADS.mkdir(exist_ok=True) + previous = Path.home() / '.config/chromium' + if not PROFILE.exists() and previous.is_dir(): + shutil.copytree(previous, PROFILE, ignore=shutil.ignore_patterns('Singleton*', 'Cache', 'Code Cache', 'GPUCache')) + default = PROFILE / 'Default' + default.mkdir(parents=True, exist_ok=True) + preferences = default / 'Preferences' + values = json.loads(preferences.read_text()) if preferences.exists() else {} + values.setdefault('download', {}).update(default_directory=str(DOWNLOADS), prompt_for_download=False, directory_upgrade=True) + values.setdefault('savefile', {})['default_directory'] = str(DOWNLOADS) + temporary = preferences.with_suffix('.agentdrive-tmp') + temporary.write_text(json.dumps(values, ensure_ascii=False)) + temporary.replace(preferences) + + +def command(): + args = [BINARY, '--no-first-run', '--no-default-browser-check', '--password-store=basic', + '--disable-dev-shm-usage', '--force-renderer-accessibility', '--ozone-platform=x11', + '--class=chromium', '--remote-debugging-address=127.0.0.1', '--remote-debugging-port=9222', + '--user-data-dir=' + str(PROFILE)] + network = Path('/agent-state/network.json') + if network.is_file() and json.loads(network.read_text()).get('proxy_enabled'): + args.append('--proxy-server=http://127.0.0.1:18081') + return args + + +def ensure(): + runtime = Path(os.environ.get('XDG_RUNTIME_DIR', '/tmp/runtime-user')) + runtime.mkdir(mode=0o700, exist_ok=True) + with (runtime / 'agentdrive-browser.lock').open('w') as lock: + fcntl.flock(lock, fcntl.LOCK_EX) + if available(): + DOWNLOADS.mkdir(exist_ok=True) + return + prepare() + with (runtime / 'agentdrive-browser.log').open('ab') as log: + process = subprocess.Popen(command() + ['about:blank'], stdout=log, stderr=log, + start_new_session=True) + for _ in range(75): + if available(): + return + if process.poll() not in (None, 0): + raise RuntimeError('Chromium 启动失败,请查看 agentdrive-browser.log') + time.sleep(0.2) + raise RuntimeError('Chromium 调试接口尚未就绪') + + +if __name__ == '__main__': + os.environ['CHROME_DEVEL_SANDBOX'] = '/opt/chromium/chrome_sandbox' + if '--version' in sys.argv or '--help' in sys.argv: + os.execv(BINARY, [BINARY] + sys.argv[1:]) + try: + ensure() + if sys.argv[1:] != ['--ensure']: + os.execv(BINARY, command() + (sys.argv[1:] or ['about:blank'])) + except Exception as error: + print(str(error), file=sys.stderr) + sys.exit(1) diff --git a/build/sandbox/build-assistant-image.sh b/build/sandbox/build-assistant-image.sh new file mode 100644 index 0000000..d3a2210 --- /dev/null +++ b/build/sandbox/build-assistant-image.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +set -euo pipefail +source_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) +output=${1:?usage: build-assistant-image.sh NEW_OUTPUT_DIRECTORY KERNEL_PATH [PVM_KERNEL_PATH]} +kernel=${2:?kernel path is required} +[[ ! -e "$output" ]] || { echo 'Output directory must not exist' >&2; exit 1; } +archive="$source_dir/.cache/csgclaw-release.tar.gz" +mkdir -p "$source_dir/.cache" +if [[ ! -f "$archive" ]]; then + curl --fail --location --retry 3 \ + https://github.com/OpenCSGs/csgclaw/releases/download/v0.10.0/csgclaw_v0.10.0_linux_amd64.tar.gz \ + --output "$archive.download" + mv "$archive.download" "$archive" +fi +printf '%s %s\n' 80083df3224a5d9727c43ce86e580a998a5009f37f1d8446aee487eaf6ae9b97 "$archive" | sha256sum -c - +docker build -f "$source_dir/assistant/Dockerfile" -t agentdrive-ubuntu-csgclaw:local "$source_dir" +mkdir -p -- "$output" +output=$(realpath -- "$output") +bash "$source_dir/install-kernels.sh" "$kernel" "$output" "${@:3}" +container=$(docker create agentdrive-ubuntu-csgclaw:local) +trap 'docker rm -f "$container" >/dev/null 2>&1 || true' EXIT +docker cp -L "$container:/usr/share/novnc" "$output/novnc" +docker cp "$container:/opt/agentdrive-template.json" "$output/ubuntu.template.json" +docker export "$container" -o "$output/rootfs.tar" +docker run --rm --user 0 -v "$output:/out" --entrypoint bash agentdrive-ubuntu-csgclaw:local -c ' +set -euo pipefail +mkdir /out/rootfs +tar -xf /out/rootfs.tar -C /out/rootfs +truncate -s 16G /out/ubuntu.ext4 +mkfs.ext4 -F -d /out/rootfs /out/ubuntu.ext4 +chown --reference=/out/rootfs.tar /out/ubuntu.ext4 +rm -rf /out/rootfs /out/rootfs.tar +' +printf 'Template: %s/ubuntu.ext4\n' "$output" diff --git a/build/sandbox/build-image.sh b/build/sandbox/build-image.sh new file mode 100755 index 0000000..77c4264 --- /dev/null +++ b/build/sandbox/build-image.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euo pipefail +source_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) +output=${1:?usage: build-image.sh NEW_OUTPUT_DIRECTORY KERNEL_PATH [PVM_KERNEL_PATH]} +kernel=${2:?kernel path is required} +if [[ -e "$output" ]]; then echo 'Output directory must not exist' >&2; exit 1; fi +mkdir -p -- "$output" +output=$(realpath -- "$output") +bash "$source_dir/install-kernels.sh" "$kernel" "$output" "${@:3}" +docker build --build-arg "CUBE_BASE_IMAGE=${CUBE_BASE_IMAGE:-ghcr.io/tencentcloud/cubesandbox-base:2026.16}" -t agentdrive-ubuntu:local "$source_dir" +container=$(docker create agentdrive-ubuntu:local) +trap 'docker rm -f "$container" >/dev/null 2>&1 || true' EXIT +docker cp -L "$container:/usr/share/novnc" "$output/novnc" +docker cp "$container:/opt/agentdrive-template.json" "$output/ubuntu.template.json" +docker export "$container" -o "$output/rootfs.tar" +docker run --rm --user 0 -v "$output:/out" --entrypoint bash agentdrive-ubuntu:local -c ' +set -euo pipefail +mkdir /out/rootfs +tar -xf /out/rootfs.tar -C /out/rootfs +truncate -s 16G /out/ubuntu.ext4 +mkfs.ext4 -F -d /out/rootfs /out/ubuntu.ext4 +chown --reference=/out/rootfs.tar /out/ubuntu.ext4 +rm -rf /out/rootfs /out/rootfs.tar +' +printf 'Template: %s/ubuntu.ext4\nKernel: %s/vmlinux\n' "$output" "$output" diff --git a/build/sandbox/chromium b/build/sandbox/chromium new file mode 100644 index 0000000..67706c1 --- /dev/null +++ b/build/sandbox/chromium @@ -0,0 +1,2 @@ +#!/bin/sh +exec /usr/bin/python3 /usr/local/bin/browser-session.py "$@" diff --git a/build/sandbox/chromium.desktop b/build/sandbox/chromium.desktop new file mode 100644 index 0000000..ae74a9d --- /dev/null +++ b/build/sandbox/chromium.desktop @@ -0,0 +1,12 @@ +[Desktop Entry] +Version=1.0 +Type=Application +Name=Chromium +Name[zh_CN]=Chromium 浏览器 +Exec=/usr/local/bin/chromium %U +Icon=/opt/chromium/product_logo_48.png +Terminal=false +Categories=Network;WebBrowser; +MimeType=text/html;x-scheme-handler/http;x-scheme-handler/https; +StartupNotify=true +StartupWMClass=chromium diff --git a/build/sandbox/chromium.json b/build/sandbox/chromium.json new file mode 100644 index 0000000..81742f9 --- /dev/null +++ b/build/sandbox/chromium.json @@ -0,0 +1,5 @@ +{ + "revision": "1743918", + "url": "https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/1743918/chrome-linux.zip", + "sha256": "b53b497b6f7546ed52e1132432bffde4d9da12f3619c3da8ca876e3e7a61841e" +} diff --git a/build/sandbox/desktop_environment.py b/build/sandbox/desktop_environment.py new file mode 100644 index 0000000..2b0f4f1 --- /dev/null +++ b/build/sandbox/desktop_environment.py @@ -0,0 +1,60 @@ +"""读取当前桌面实例的浏览器、分支目录和网络配置。""" +import json +import os +from pathlib import Path +import shutil +import subprocess + + +def environment_info(): + browser = shutil.which('chromium') + opener = shutil.which('xdg-open') + version = None + if browser: + result = subprocess.run([browser, '--version'], capture_output=True, text=True, timeout=5) + if result.returncode == 0: + version = result.stdout.strip() + handlers = {} + if shutil.which('xdg-mime'): + for scheme in ('http', 'https'): + result = subprocess.run( + ['xdg-mime', 'query', 'default', 'x-scheme-handler/' + scheme], + capture_output=True, text=True, timeout=3) + handlers[scheme] = result.stdout.strip() if result.returncode == 0 else '' + routes = Path('/proc/net/route').read_text().splitlines()[1:] + default_route = any(len(parts := line.split()) > 3 and parts[1] == '00000000' + and int(parts[3], 16) & 1 for line in routes) + dns = [] + if Path('/etc/resolv.conf').exists(): + dns = [parts[1] for line in Path('/etc/resolv.conf').read_text().splitlines() + if len(parts := line.split()) >= 2 and parts[0] == 'nameserver'] + fonts = {} + for name, selector in (('chinese', ':lang=zh'), ('emoji', 'Noto Color Emoji')): + result = subprocess.run(['fc-list', selector, 'family'], capture_output=True, + text=True, timeout=5, check=True) + fonts[name] = bool(result.stdout.strip()) + return { + 'browser': {'name': 'Chromium', 'command': browser, 'version': version, 'desktop_id': 'chromium.desktop', + 'open_url_command': opener, 'handlers': handlers}, + 'fonts': fonts, + 'workspace': {'path': '/workspace', 'mounted': os.path.ismount('/workspace')}, + 'network': {'default_route': default_route, 'dns': dns}, + } + + +def environment_instructions(info): + browser = info['browser'] + parts = ['当前工具连接 AgentDrive 的 Ubuntu 桌面,MCP 操作与 VNC 显示同一个桌面。'] + if browser['command']: + parts.append('已安装 Chromium,启动命令为 chromium ,窗口标识为 chromium。') + if browser['open_url_command']: + parts.append('桌面终端可以使用 xdg-open 打开网页。') + parts.append('统一 MCP 的 browser_* 工具通过 Playwright 操作同一个 Chromium。网页导航、标签页、按钮和表单优先使用 browser_*,系统窗口与终端使用桌面工具。浏览器下载保存到 /workspace/Downloads。') + parts.append('在 Chromium 输入中文或表情时,点击输入框后使用 paste_text,并读取输入框确认内容。paste_text 会更新当前桌面的剪贴板。') + parts.append('操作前调用 get_app_state,使用 list_apps 查看运行中的应用。已安装的浏览器及启动入口见上述环境信息;其他应用可以通过桌面应用菜单查找。通过 list_windows 确认目标窗口后再输入;打开网页后检查页面内容和截图。应用尚未运行时,list_apps 可能没有对应条目,判断安装情况时请结合环境信息和桌面应用菜单。') + if info['workspace']['mounted']: + parts.append('当前分支已挂载到 /workspace,桌面的 AgentDrive 文件夹指向这里。需要保存到当前分支的文件请写入 /workspace。') + else: + parts.append('/workspace 当前未挂载,请报告挂载问题。') + parts.append('环境检查结果:' + json.dumps(info, ensure_ascii=False, separators=(',', ':'))) + return '\n'.join(parts) diff --git a/build/sandbox/desktop_mcp.py b/build/sandbox/desktop_mcp.py new file mode 100644 index 0000000..3c49e57 --- /dev/null +++ b/build/sandbox/desktop_mcp.py @@ -0,0 +1,129 @@ +"""为 computer-use-linux 的 MCP 响应补充当前实例信息。""" +import json +import subprocess +import sys +import threading + +from desktop_environment import environment_info, environment_instructions + +PASTE_TOOL = { + 'name': 'paste_text', + 'description': '在已点击的 Chromium 输入框中粘贴文本,支持完整中文和表情。先通过 get_app_state 与 click 选中输入框,并从 list_windows 获取 window_id。工具确认目标窗口后粘贴;完成后读取输入框核对内容。此操作会替换 Sandbox 桌面的剪贴板。', + 'inputSchema': {'type': 'object', 'properties': { + 'text': {'type': 'string'}, + 'window_id': {'type': 'integer', 'minimum': 1}}, + 'required': ['text', 'window_id'], 'additionalProperties': False}, + 'annotations': {'readOnlyHint': False, 'destructiveHint': True, + 'idempotentHint': False, 'openWorldHint': True}, +} + + +def paste_request(message): + if not isinstance(message, dict) or 'id' not in message: + return None + params = message.get('params', {}) + if message.get('method') != 'tools/call' or not isinstance(params, dict) or params.get('name') != 'paste_text': + return None + arguments = params.get('arguments', {}) + if not isinstance(arguments, dict): + raise ValueError('paste_text 参数必须为对象') + value, window = arguments.get('text'), arguments.get('window_id') + if not isinstance(value, str) or isinstance(window, bool) or not isinstance(window, int) or window < 1: + raise ValueError('paste_text 需要 text 和有效的 window_id') + if len(value.encode('utf-8')) > 131072 or '\0' in value: + raise ValueError('粘贴内容超过长度限制或包含无效字符') + return value, {'jsonrpc': '2.0', 'id': message['id'], 'method': 'tools/call', + 'params': {'name': 'press_key', 'arguments': {'window_id': window, 'key': 'ctrl+v'}}} + + +def enrich(message, context): + result = message.get('result') + if not isinstance(result, dict): + return message + if 'protocolVersion' in result and 'serverInfo' in result: + result['instructions'] = context + '\n\n' + result.get('instructions', '') + for tool in result.get('tools', []): + if tool.get('name') in ('list_apps', 'get_app_state'): + tool['description'] = context + '\n\n' + tool.get('description', '') + if tool.get('name') == 'type_text': + tool['description'] = 'Chromium 输入中文或表情时请使用 paste_text,并读取输入框核对结果。\n' + tool.get('description', '') + if 'tools' in result: + result['tools'].append(PASTE_TOOL) + return message + + +def main(): + context = environment_instructions(environment_info()) + child = subprocess.Popen(['/usr/local/bin/computer-use-linux', 'mcp'], + stdin=subprocess.PIPE, stdout=subprocess.PIPE) + output_lock = threading.Lock() + pending_pastes = set() + + def emit(data): + with output_lock: + sys.stdout.buffer.write(data) + sys.stdout.buffer.flush() + + def responses(): + try: + for line in child.stdout: + try: + message = json.loads(line) + if message.get('id') in pending_pastes: + pending_pastes.discard(message['id']) + result = message.get('result', {}) + if result and not result.get('isError'): + for item in result.get('content', []): + if item.get('type') == 'text': + detail = json.loads(item['text']) + detail['action'] = 'paste_text' + detail['message'] = '已发送粘贴操作,请读取输入框核对内容。 ' + detail.get('message', '') + item['text'] = json.dumps(detail, ensure_ascii=False) + result['structuredContent'] = detail + output = (json.dumps(enrich(message, context), ensure_ascii=False) + '\n').encode() + except (ValueError, AttributeError, TypeError): + output = line + emit(output) + except (BrokenPipeError, OSError): + child.terminate() + + reader = threading.Thread(target=responses, daemon=True) + reader.start() + try: + for line in sys.stdin.buffer: + try: + message = json.loads(line) + except ValueError: + child.stdin.write(line) + child.stdin.flush() + continue + try: + paste = paste_request(message) + if paste is not None: + value, request = paste + subprocess.run(['xclip', '-selection', 'clipboard', '-in'], input=value.encode(), + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5, check=True) + pending_pastes.add(message['id']) + line = (json.dumps(request) + '\n').encode() + except (ValueError, subprocess.SubprocessError, OSError) as error: + emit((json.dumps({'jsonrpc': '2.0', 'id': message.get('id'), 'result': { + 'isError': True, 'content': [{'type': 'text', 'text': str(error)}]}}) + '\n').encode()) + continue + child.stdin.write(line) + child.stdin.flush() + child.stdin.close() + code = child.wait(timeout=10) + reader.join(timeout=5) + return code + finally: + if child.poll() is None: + child.terminate() + try: + child.wait(timeout=5) + except subprocess.TimeoutExpired: + child.kill() + child.wait() + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/build/sandbox/guest-helper.py b/build/sandbox/guest-helper.py new file mode 100644 index 0000000..136a90f --- /dev/null +++ b/build/sandbox/guest-helper.py @@ -0,0 +1,132 @@ +#!/usr/bin/python3 +import json +import os +import select +import socket +import subprocess +import threading + + +def runtime_config(): + try: + with open('/etc/agentdrive-runtime.json') as source: + return json.load(source) + except FileNotFoundError: + return {} + + +RUNTIME = runtime_config() +DESKTOP_USER = RUNTIME.get('user', 'user') + + +def desktop_session(): + for entry in os.scandir('/proc'): + if not entry.name.isdigit(): + continue + try: + command = open(entry.path + '/comm').read().strip() + if command == RUNTIME.get('session_process', 'gnome-session-b'): + environment = dict(item.split('=', 1) for item in open(entry.path + '/environ', 'rb').read().decode().split('\0') if '=' in item) + return environment + except (OSError, ValueError): + continue + raise RuntimeError('Desktop session is not ready') + + +def bridge(left, right): + try: + while True: + readable, _, _ = select.select([left, right], [], []) + for source in readable: + data = source.recv(65536) + if not data: + return + (right if source is left else left).sendall(data) + finally: + right.close() + + +def mcp_connection(client, environment): + with open('/var/log/agentdrive-mcp.log', 'ab') as log: + process = subprocess.Popen(['/usr/local/bin/node', '/opt/agentdrive-mcp/gateway.mjs'], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=log, + env=environment, user=DESKTOP_USER, group=DESKTOP_USER, extra_groups=[], cwd='/workspace') + try: + inputs = [client, process.stdout] + while inputs: + readable, _, _ = select.select(inputs, [], []) + for source in readable: + if source is client: + data = client.recv(65536) + if data: + process.stdin.write(data) + process.stdin.flush() + else: + inputs.remove(client) + process.stdin.close() + else: + data = os.read(process.stdout.fileno(), 65536) + if not data: + return + client.sendall(data) + finally: + if not process.stdin.closed: + process.stdin.close() + try: + process.wait(timeout=3) + except subprocess.TimeoutExpired: + process.terminate() + try: + process.wait(timeout=3) + except subprocess.TimeoutExpired: + process.kill() + process.wait() + + +def connection(client, port): + with client: + try: + if port == 7011 and RUNTIME.get('assistant') == 'csgclaw': + upstream = socket.create_connection(('127.0.0.1', 18080), timeout=10) + upstream.settimeout(None) + bridge(client, upstream) + return + if port == 6080: + bridge(client, socket.create_connection(('127.0.0.1', 6080), timeout=10)) + return + if port == 7002: + os.sync() + client.sendall(b'{"flushed":true}\n') + return + environment = desktop_session() + if port == 7000: + mcp_connection(client, environment) + return + manager = subprocess.run(['wmctrl', '-m'], env=environment, user=DESKTOP_USER, group=DESKTOP_USER, extra_groups=[], capture_output=True, timeout=5) + if manager.returncode or RUNTIME.get('window_manager', 'GNOME Shell').encode() not in manager.stdout: + raise RuntimeError('Window manager is not ready') + if not os.path.ismount('/workspace'): + raise RuntimeError('当前分支目录 /workspace 尚未挂载') + with socket.create_connection(('127.0.0.1', 5900), timeout=5) as vnc: + if not vnc.recv(12).startswith(b'RFB '): + raise RuntimeError('Desktop connection is not ready') + with socket.create_connection(('127.0.0.1', 6080), timeout=5): + pass + client.sendall(b'{"ready":true}\n') + except Exception as error: + client.sendall(json.dumps({'ready': False, 'error': str(error)}).encode() + b'\n') + + +def listen(port): + server = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM) + server.bind((socket.VMADDR_CID_ANY, port)) + server.listen(16) + while True: + client, _ = server.accept() + threading.Thread(target=connection, args=(client, port), daemon=True).start() + + +if __name__ == '__main__': + for port in [6080, 7000, 7002] + ([7011] if RUNTIME.get('assistant') == 'csgclaw' else []): + threading.Thread(target=listen, args=(port,), daemon=True).start() + listen(7001) diff --git a/build/sandbox/guest-init.sh b/build/sandbox/guest-init.sh new file mode 100755 index 0000000..7c8bdee --- /dev/null +++ b/build/sandbox/guest-init.sh @@ -0,0 +1,60 @@ +#!/bin/bash +set -eu +mount -t proc proc /proc +mount -t sysfs sysfs /sys +mountpoint -q /dev || mount -t devtmpfs devtmpfs /dev +mkdir -p /dev/pts /run /workspace +mount -t devpts devpts /dev/pts +mount -t tmpfs tmpfs /run +mount -t virtiofs workspace /workspace +ip link set lo up +address= gateway= dns= +for arg in $(cat /proc/cmdline); do + case "$arg" in + agentdrive.ip=*) address=${arg#*=} ;; + agentdrive.gateway=*) gateway=${arg#*=} ;; + agentdrive.dns=*) dns=${arg#*=} ;; + esac +done +if [ -n "$address" ]; then + interface= + for attempt in {1..50}; do + for device in /sys/class/net/*; do + if [ "$(cat "$device/type")" = 1 ] && [ -e "$device/device" ]; then + interface=${device##*/} + break + fi + done + [ -n "$interface" ] && break + sleep 0.1 + done + ip link set "$interface" up + ip address replace "$address" dev "$interface" + ip route replace default via "$gateway" dev "$interface" + rm -f /etc/resolv.conf + for server in ${dns//,/ }; do + printf 'nameserver %s\n' "$server" + done > /etc/resolv.conf +fi +export LANG=C.UTF-8 +printf '127.0.0.1 localhost\n::1 localhost\n' > /etc/hosts +export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin +mkdir -p /home/user/Desktop +chown user:user /home/user/Desktop +if [ ! -e /home/user/Desktop/AgentDrive ] && [ ! -L /home/user/Desktop/AgentDrive ]; then + ln -s /workspace /home/user/Desktop/AgentDrive + chown -h user:user /home/user/Desktop/AgentDrive +fi +export DISPLAY=:0 XDG_SESSION_TYPE=x11 XDG_CURRENT_DESKTOP=ubuntu:GNOME +export GNOME_SHELL_SESSION_MODE=ubuntu LIBGL_ALWAYS_SOFTWARE=1 GALLIUM_DRIVER=llvmpipe +if [ -f /etc/agentdrive-runtime.json ]; then + mkdir -p /agent-state + mount -t virtiofs assistant-state /agent-state + python3 /usr/local/bin/assistant-service.py & +fi +/usr/local/bin/start-desktop.sh & +python3 /usr/local/bin/guest-helper.py & +while true; do + wait -n || true + sleep 1 +done diff --git a/build/sandbox/install-chromium.py b/build/sandbox/install-chromium.py new file mode 100644 index 0000000..93afd62 --- /dev/null +++ b/build/sandbox/install-chromium.py @@ -0,0 +1,39 @@ +"""校验并安装模板使用的 Chromium 官方构建。""" +import hashlib +import json +from pathlib import Path +import shutil +import sys +import tempfile +import zipfile + + +def install(archive_path, manifest_path, destination): + manifest = json.loads(Path(manifest_path).read_text()) + digest = hashlib.sha256() + with open(archive_path, 'rb') as archive: + for chunk in iter(lambda: archive.read(1024 * 1024), b''): + digest.update(chunk) + if digest.hexdigest() != manifest['sha256']: + raise ValueError('Chromium SHA-256 校验失败') + destination = Path(destination) + destination.parent.mkdir(parents=True, exist_ok=True) + if destination.exists(): + raise FileExistsError(destination) + with tempfile.TemporaryDirectory(dir=destination.parent) as temporary: + with zipfile.ZipFile(archive_path) as archive: + for item in archive.infolist(): + target = Path(temporary, item.filename).resolve() + if not target.is_relative_to(Path(temporary).resolve()): + raise ValueError('Chromium 压缩包路径无效') + archive.extractall(temporary) + source = Path(temporary, 'chrome-linux') + for name in ('chrome', 'chrome_crashpad_handler'): + (source / name).chmod(0o755) + (source / 'chrome_sandbox').chmod(0o4755) + shutil.move(str(source), destination) + (destination / 'agentdrive-build.json').write_text(json.dumps(manifest, indent=2) + '\n') + + +if __name__ == '__main__': + install(*sys.argv[1:]) diff --git a/build/sandbox/install-kernels.sh b/build/sandbox/install-kernels.sh new file mode 100644 index 0000000..dae6634 --- /dev/null +++ b/build/sandbox/install-kernels.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -euo pipefail +kernel=${1:?usage: install-kernels.sh KVM_KERNEL OUTPUT_DIRECTORY [PVM_KERNEL]} +output=${2:?output directory is required} +pvm=${3:-$(dirname -- "$kernel")/vmlinux-pvm} +kernel=$(realpath -e -- "$kernel") +if [[ $(basename -- "$kernel") == vmlinux-pvm ]]; then + echo 'KVM_KERNEL must reference the standard KVM kernel' >&2 + exit 1 +fi +if [[ $# -ge 3 && ! -f "$pvm" ]]; then + echo "PVM kernel does not exist: $pvm" >&2 + exit 1 +fi +cp --reflink=auto -- "$kernel" "$output/vmlinux" +cp --reflink=auto -- "$kernel" "$output/vmlinux-bm" +if [[ -f "$pvm" ]]; then + cp --dereference --reflink=auto -- "$pvm" "$output/vmlinux-pvm" +fi diff --git a/build/sandbox/mcp-proxy.py b/build/sandbox/mcp-proxy.py new file mode 100755 index 0000000..ef5acd5 --- /dev/null +++ b/build/sandbox/mcp-proxy.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +"""Connect a host stdio MCP client to a running Sandbox desktop.""" +import argparse +import select +import socket +import sys + + +def connect(path): + channel = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + try: + channel.settimeout(10) + channel.connect(path) + channel.sendall(b'CONNECT 7000\n') + response = bytearray() + while not response.endswith(b'\n'): + byte = channel.recv(1) + if not byte or len(response) >= 128: + raise ConnectionError('Invalid Sandbox connection response') + response.extend(byte) + if not response.startswith(b'OK '): + raise ConnectionError('Sandbox refused the MCP connection') + channel.settimeout(None) + return channel + except BaseException: + channel.close() + raise + + +def forward(channel, source, destination): + inputs = [source, channel] + while True: + ready, _, _ = select.select(inputs, [], []) + for current in ready: + if current is channel: + data = channel.recv(65536) + if not data: + return + destination.write(data) + destination.flush() + else: + data = source.read1(65536) + if data: + channel.sendall(data) + else: + inputs.remove(source) + channel.shutdown(socket.SHUT_WR) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--socket', required=True, help='Absolute path to the instance vsock.sock') + args = parser.parse_args() + try: + with connect(args.socket) as channel: + forward(channel, sys.stdin.buffer, sys.stdout.buffer) + except (OSError, ConnectionError) as error: + print(f'Sandbox MCP connection failed: {error}. Ensure the Sandbox is running.', file=sys.stderr) + return 1 + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/build/sandbox/mcp/gateway.mjs b/build/sandbox/mcp/gateway.mjs new file mode 100644 index 0000000..edfb04b --- /dev/null +++ b/build/sandbox/mcp/gateway.mjs @@ -0,0 +1,120 @@ +import {readFile} from 'node:fs/promises'; +import {execFile} from 'node:child_process'; +import {promisify} from 'node:util'; +import {pathToFileURL} from 'node:url'; +import {Client} from '@modelcontextprotocol/sdk/client/index.js'; +import {StreamableHTTPClientTransport} from '@modelcontextprotocol/sdk/client/streamableHttp.js'; +import {StdioClientTransport} from '@modelcontextprotocol/sdk/client/stdio.js'; +import {Server} from '@modelcontextprotocol/sdk/server/index.js'; +import {StdioServerTransport} from '@modelcontextprotocol/sdk/server/stdio.js'; +import {CallToolRequestSchema, ListToolsRequestSchema, ListRootsRequestSchema, ToolListChangedNotificationSchema, McpError, ErrorCode} from '@modelcontextprotocol/sdk/types.js'; + +const execute = promisify(execFile); +const instructions = '这是 AgentDrive Sandbox 的统一 MCP 入口。browser_* 工具通过 Playwright 操作 VNC 中可见的 Chromium,桌面工具操作同一个 Ubuntu 桌面。网页导航、标签页、按钮和表单优先使用 browser_*;终端和系统窗口使用桌面工具。下载目录为 /workspace/Downloads,文件属于当前 AgentDrive 分支。调用 browser_tabs 可以查看用户已打开的标签页,操作前确认目标标签页。关闭 MCP 连接会保留浏览器和登录状态。请勿并行操作同一个桌面。sandbox_tools_status 可以检查内部工具服务。'; +const statusTool = {name:'sandbox_tools_status',description:'查看统一 MCP 入口的内部服务状态和工具数量。',inputSchema:{type:'object',properties:{},additionalProperties:false},annotations:{readOnlyHint:true}}; + +export async function createGateway(config) { + if (!Array.isArray(config.providers) || !config.providers.length) throw new Error('MCP providers 配置不能为空'); + const identifiers = new Set(); + const providers = config.providers.map(value => { + if (!/^[a-z][a-z0-9_-]*$/.test(value.id) || identifiers.has(value.id) || (!value.url && (typeof value.command !== 'string' || !Array.isArray(value.args) || !value.args.every(arg => typeof arg === 'string'))) || !/^[a-zA-Z0-9_-]*$/.test(value.prefix ?? '')) throw new Error('MCP provider 配置无效或名称重复'); + identifiers.add(value.id); + return {config:value, client:null, transport:null, tools:[], error:null, connecting:null}; + }); + const server = new Server({name:'agentdrive-sandbox',version:'1.0.0'}, {capabilities:{tools:{listChanged:true}},instructions}); + let closing = false, initialized = false, routes = new Map(), queue = Promise.resolve(); + const pendingRequests = new Set(); + function track(promise) { pendingRequests.add(promise); promise.then(() => pendingRequests.delete(promise), () => pendingRequests.delete(promise)); return promise; } + server.oninitialized = () => { initialized = true; }; + async function connect(provider) { + if (closing) throw new Error('MCP 连接正在关闭'); + if (provider.client) return; + if (provider.connecting) return provider.connecting; + provider.connecting = (async () => { + const client = new Client({name:'agentdrive-gateway',version:'1.0.0'}, {capabilities:{roots:{listChanged:false}}}); + client.setRequestHandler(ListRootsRequestSchema, async () => ({roots:[{uri:'file:///workspace',name:'当前 AgentDrive 分支'}]})); + client.setNotificationHandler(ToolListChangedNotificationSchema, async () => { + if (initialized && !closing) await server.sendToolListChanged(); + }); + const transport = provider.config.url ? new StreamableHTTPClientTransport(new URL(provider.config.url)) : new StdioClientTransport({command:provider.config.command,args:provider.config.args,cwd:config.cwd || '/workspace',env:{...process.env,...provider.config.env},stderr:'inherit'}); + provider.transport = transport; + client.onclose = () => { + if (provider.client === client) { provider.client = null; provider.error = '内部 MCP 服务已断开,下次调用时重新连接'; } + }; + try { + await client.connect(transport, {timeout:15000}); + if (closing) { await client.close(); return; } + provider.client = client; provider.error = null; + } catch (error) { + provider.error = error.message; await transport.close(); throw error; + } + })(); + try { await provider.connecting; } finally { provider.connecting = null; } + } + async function catalog() { + await Promise.all(providers.map(async provider => { + try { + await connect(provider); + const tools = [], cursors = new Set(); let cursor; + do { + const result = await provider.client.listTools(cursor ? {cursor} : {}, {timeout:15000}); + tools.push(...result.tools); cursor = result.nextCursor; + if (cursor && cursors.has(cursor)) throw new Error('内部 MCP 服务返回重复分页标记'); + cursors.add(cursor); + } while (cursor); + provider.tools = tools; provider.error = null; + } catch (error) { provider.error = error.message; } + })); + const next = new Map(), tools = [statusTool]; + for (const provider of providers) for (const tool of provider.tools) { + const name = (provider.config.prefix || '') + tool.name; + if (name === statusTool.name || next.has(name)) throw new McpError(ErrorCode.InternalError, `工具名称重复:${name},请在 providers.json 中配置 prefix`); + if (!/^[a-zA-Z0-9_.-]{1,128}$/.test(name)) throw new Error(`工具名称无效:${name}`); + next.set(name, {provider,original:tool.name}); tools.push({...tool,name}); + } + routes = next; return {tools}; + } + server.setRequestHandler(ListToolsRequestSchema, () => track(catalog())); + server.setRequestHandler(CallToolRequestSchema, (request, extra) => { + const invoke = async () => { + if (request.params.name === statusTool.name) { + if (!routes.size) await catalog(); + return {content:[{type:'text',text:JSON.stringify({browser:`Chromium / DISPLAY=${process.env.DISPLAY || ':0'}`,downloads:'/workspace/Downloads',providers:providers.map(provider => ({id:provider.config.id,connected:!!provider.client,tools:provider.tools.length,error:provider.error}))})}]}; + } + if (!routes.has(request.params.name)) await catalog(); + const route = routes.get(request.params.name); + if (!route) throw new McpError(ErrorCode.InvalidParams, `未知工具:${request.params.name}`); + try { + extra.signal.throwIfAborted(); + await connect(route.provider); + const prepare = route.provider.config.prepare; + if (prepare) await execute(prepare[0],prepare.slice(1), {timeout:20000,signal:extra.signal}); + return await route.provider.client.callTool({...request.params,name:route.original}, undefined, {timeout:120000,signal:extra.signal, + onprogress: request.params._meta?.progressToken === undefined ? undefined : progress => server.notification({method:'notifications/progress',params:{...progress,progressToken:request.params._meta.progressToken}})}); + } catch (error) { + return {isError:true,content:[{type:'text',text:`${route.provider.config.id}:${error.message}`}]}; + } + }; + const pending = queue.then(invoke, invoke); queue = pending.catch(() => {}); return track(pending); + }); + async function close() { + if (closing) return; closing = true; + await Promise.allSettled(providers.map(async provider => { + await provider.connecting?.catch(() => {}); + await (provider.client ? provider.client.close() : provider.transport?.close()); + })); + await server.close(); + } + return {server,close,drain:() => Promise.allSettled([...pendingRequests])}; +} + +async function main() { + const config = JSON.parse(await readFile(process.argv[2] || new URL('./providers.json',import.meta.url),'utf8')); + const gateway = await createGateway(config); + const shutdown = () => gateway.close().catch(error => console.error(error.message)); + process.stdin.once('end', () => gateway.drain().then(shutdown)); + process.on('SIGTERM', shutdown); process.on('SIGINT', shutdown); + process.stdout.on('error', shutdown); + await gateway.server.connect(new StdioServerTransport()); +} +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) main().catch(error => { console.error(error.message); process.exitCode = 1; }); diff --git a/build/sandbox/mcp/package-lock.json b/build/sandbox/mcp/package-lock.json new file mode 100644 index 0000000..1eb1c3b --- /dev/null +++ b/build/sandbox/mcp/package-lock.json @@ -0,0 +1,1267 @@ +{ + "name": "agentdrive-sandbox-mcp", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "agentdrive-sandbox-mcp", + "version": "1.0.0", + "dependencies": { + "@modelcontextprotocol/sdk": "1.32.2", + "@playwright/mcp": "0.0.85" + } + }, + "node_modules/@hono/node-server": { + "version": "2.1.8", + "resolved": "https://registry.npmmirror.com/@hono/node-server/-/node-server-2.1.8.tgz", + "integrity": "sha512-jdf7FHh6A1lzjpHHQUgxsg2GeER2vMetnAOfeActzcoli6jamlK+NAwL6Dsr5Fl3BS3tYUQzCdtZjU8Qxv7bEA==", + "license": "MIT", + "engines": { + "node": ">=20" + }, + "peerDependencies": { + "hono": "^4 || ^5.0.0-0" + } + }, + "node_modules/@modelcontextprotocol/sdk": { + "version": "1.32.2", + "resolved": "https://registry.npmmirror.com/@modelcontextprotocol/sdk/-/sdk-1.32.2.tgz", + "integrity": "sha512-SWsVtH/0vULSQStoZ6xxH2K09Li8fXYTMC64ZeogCdpZ3XSi1eCPOByV1OpuJxuI3OcmHhosDWYyii07tpPsLw==", + "license": "MIT", + "dependencies": { + "@hono/node-server": "^1.19.9 || ^2.0.5", + "ajv": "^8.17.1", + "ajv-formats": "^3.0.1", + "content-type": "^1.0.5", + "cors": "^2.8.5", + "cross-spawn": "^7.0.5", + "eventsource": "^3.0.2", + "eventsource-parser": "^3.0.0", + "express": "^5.2.1", + "express-rate-limit": "^8.2.1", + "hono": "^4.11.4", + "jose": "^6.1.3", + "json-schema-typed": "^8.0.2", + "pkce-challenge": "^5.0.0", + "raw-body": "^3.0.0", + "zod": "^3.25 || ^4.0", + "zod-to-json-schema": "^3.25.1" + }, + "engines": { + "node": ">=18" + }, + "peerDependencies": { + "@cfworker/json-schema": "^4.1.1", + "zod": "^3.25 || ^4.0" + }, + "peerDependenciesMeta": { + "@cfworker/json-schema": { + "optional": true + }, + "zod": { + "optional": false + } + } + }, + "node_modules/@playwright/mcp": { + "version": "0.0.85", + "resolved": "https://registry.npmmirror.com/@playwright/mcp/-/mcp-0.0.85.tgz", + "integrity": "sha512-bSfECMbLXhJdWkGhLzNMxTZuazG+9IDJ679gflji3yyD94e7ARnGdqmJmG6lTPB+gYX+OiFLmX5myBl62+wZEw==", + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.66.0-alpha-2026-11-09", + "playwright-core": "1.66.0-alpha-2026-11-09" + }, + "bin": { + "playwright-mcp": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/accepts": { + "version": "2.0.0", + "resolved": "https://registry.npmmirror.com/accepts/-/accepts-2.0.0.tgz", + "integrity": "sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng==", + "license": "MIT", + "dependencies": { + "mime-types": "^3.0.0", + "negotiator": "^1.0.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/ajv": { + "version": "8.20.0", + "resolved": "https://registry.npmmirror.com/ajv/-/ajv-8.20.0.tgz", + "integrity": "sha512-Thbli+OlOj+iMPYFBVBfJ3OmCAnaSyNn4M1vz9T6Gka5Jt9ba/HIR56joy65tY6kx/FCF5VXNB819Y7/GUrBGA==", + "license": "MIT", + "dependencies": { + "fast-deep-equal": "^3.1.3", + "fast-uri": "^3.0.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, + "node_modules/ajv-formats": { + "version": "3.0.1", + "resolved": "https://registry.npmmirror.com/ajv-formats/-/ajv-formats-3.0.1.tgz", + "integrity": "sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ==", + "license": "MIT", + "dependencies": { + "ajv": "^8.0.0" + }, + "peerDependencies": { + "ajv": "^8.0.0" + }, + "peerDependenciesMeta": { + "ajv": { + "optional": true + } + } + }, + "node_modules/body-parser": { + "version": "2.4.0", + "resolved": "https://registry.npmmirror.com/body-parser/-/body-parser-2.4.0.tgz", + "integrity": "sha512-Ifk1KcLrxaGt1occQagQNFNNOqRt1LFuehEnHHzB8Z6cRgIxRU1TBaiIsUiCdA+gy5PL7Y1IX31DNKkRmxtXjQ==", + "license": "MIT", + "dependencies": { + "bytes": "^3.1.2", + "content-type": "^2.1.0", + "debug": "^4.4.3", + "http-errors": "^2.0.1", + "iconv-lite": "^0.7.3", + "on-finished": "^2.4.1", + "qs": "^6.16.1", + "raw-body": "^3.0.2", + "type-is": "^2.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/body-parser/node_modules/content-type": { + "version": "2.1.0", + "resolved": "https://registry.npmmirror.com/content-type/-/content-type-2.1.0.tgz", + "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmmirror.com/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmmirror.com/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/call-bound": { + "version": "1.0.4", + "resolved": "https://registry.npmmirror.com/call-bound/-/call-bound-1.0.4.tgz", + "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "get-intrinsic": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/content-disposition": { + "version": "2.0.1", + "resolved": "https://registry.npmmirror.com/content-disposition/-/content-disposition-2.0.1.tgz", + "integrity": "sha512-e+H0ZXHSWYrENhQzw1LPuP4oF5MzVKmDU6d3hxlvaPEYLLg62MxtQNPRx4SYSuYJSBUgnQIG4HIN2tEtNv7Dog==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/content-type": { + "version": "1.0.5", + "resolved": "https://registry.npmmirror.com/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie": { + "version": "0.7.2", + "resolved": "https://registry.npmmirror.com/cookie/-/cookie-0.7.2.tgz", + "integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie-signature": { + "version": "1.2.2", + "resolved": "https://registry.npmmirror.com/cookie-signature/-/cookie-signature-1.2.2.tgz", + "integrity": "sha512-D76uU73ulSXrD1UXF4KE2TMxVVwhsnCgfAyTg9k8P6KGZjlXKrOLe4dJQKI3Bxi5wjesZoFXJWElNWBjPZMbhg==", + "license": "MIT", + "engines": { + "node": ">=6.6.0" + } + }, + "node_modules/cors": { + "version": "2.8.6", + "resolved": "https://registry.npmmirror.com/cors/-/cors-2.8.6.tgz", + "integrity": "sha512-tJtZBBHA6vjIAaF6EnIaq6laBBP9aq/Y3ouVJjEfoHbRBcHBAHYcMh/w8LDrk2PvIMMq8gmopa5D4V8RmbrxGw==", + "license": "MIT", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/cross-spawn": { + "version": "7.0.6", + "resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", + "license": "MIT", + "dependencies": { + "path-key": "^3.1.0", + "shebang-command": "^2.0.0", + "which": "^2.0.1" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmmirror.com/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/depd": { + "version": "2.0.0", + "resolved": "https://registry.npmmirror.com/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmmirror.com/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmmirror.com/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==", + "license": "MIT" + }, + "node_modules/encodeurl": { + "version": "2.0.3", + "resolved": "https://registry.npmmirror.com/encodeurl/-/encodeurl-2.0.3.tgz", + "integrity": "sha512-mIteYONiEfc4xWf2F1WXovh1c8npzS00vNeiUPdwFF+QlVJni+gNvpQltLMQhob8rew/bWWc/bGTuUr0EUGt0w==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmmirror.com/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmmirror.com/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.2", + "resolved": "https://registry.npmmirror.com/es-object-atoms/-/es-object-atoms-1.1.2.tgz", + "integrity": "sha512-HWcBoN6NileqtSydK2FqHbS/LoDd2pqrnQHLyJzBj4kOp/ky2MWMN694xOfkK8/SnUsW2DH7EfyVlydKCsm1Zw==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmmirror.com/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==", + "license": "MIT" + }, + "node_modules/etag": { + "version": "1.8.1", + "resolved": "https://registry.npmmirror.com/etag/-/etag-1.8.1.tgz", + "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/eventsource": { + "version": "3.0.7", + "resolved": "https://registry.npmmirror.com/eventsource/-/eventsource-3.0.7.tgz", + "integrity": "sha512-CRT1WTyuQoD771GW56XEZFQ/ZoSfWid1alKGDYMmkt2yl8UXrVR4pspqWNEcqKvVIzg6PAltWjxcSSPrboA4iA==", + "license": "MIT", + "dependencies": { + "eventsource-parser": "^3.0.1" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/eventsource-parser": { + "version": "3.1.1", + "resolved": "https://registry.npmmirror.com/eventsource-parser/-/eventsource-parser-3.1.1.tgz", + "integrity": "sha512-EKN1vKAMcZ8MlYMpaNuxN6R9yakzH6uajHcHVTqWJzvu5pWw9DyhbP35HH8MVBQ+dZjAfDxk+A8NiR9KWaXiyQ==", + "license": "MIT", + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/express": { + "version": "5.3.0", + "resolved": "https://registry.npmmirror.com/express/-/express-5.3.0.tgz", + "integrity": "sha512-qCqy1xSoaugxOD4IVHb1ZWdkvCF2W3aE2okKouWj8VwJ+ztskSiXoBnCSJqfKT+cVPJhuJqe9YLbY425+Zh00g==", + "license": "MIT", + "dependencies": { + "accepts": "^2.0.0", + "body-parser": "^2.3.0", + "content-disposition": "^2.0.1", + "content-type": "^2.0.0", + "cookie": "^0.7.1", + "cookie-signature": "^1.2.1", + "debug": "^4.4.0", + "depd": "^2.0.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "finalhandler": "^2.1.0", + "fresh": "^2.0.0", + "http-errors": "^2.0.0", + "merge-descriptors": "^2.0.0", + "mime-types": "^3.0.0", + "on-finished": "^2.4.1", + "once": "^1.4.0", + "parseurl": "^1.3.3", + "proxy-addr": "^2.0.8", + "qs": "^6.16.0", + "range-parser": "^1.2.1", + "router": "^2.2.0", + "send": "^1.1.0", + "serve-static": "^2.2.0", + "statuses": "^2.0.1", + "type-is": "^2.1.0", + "vary": "^1.1.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/express-rate-limit": { + "version": "8.7.4", + "resolved": "https://registry.npmmirror.com/express-rate-limit/-/express-rate-limit-8.7.4.tgz", + "integrity": "sha512-LKBXzX700wz3zAGNNmVnjRI5mEbYkLv7UbMI4+9Xa+JPcg2UZh6AgY/2CZh9RrJjFM5ttC/N1lD+KzyFoZITCw==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.3", + "ip-address": "^10.2.0" + }, + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://github.com/sponsors/express-rate-limit" + }, + "peerDependencies": { + "express": ">= 4.11" + } + }, + "node_modules/express/node_modules/content-type": { + "version": "2.1.0", + "resolved": "https://registry.npmmirror.com/content-type/-/content-type-2.1.0.tgz", + "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmmirror.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", + "license": "MIT" + }, + "node_modules/fast-uri": { + "version": "3.1.9", + "resolved": "https://registry.npmmirror.com/fast-uri/-/fast-uri-3.1.9.tgz", + "integrity": "sha512-hS/0Wt3Ya043oKXs0sIxrMpKQITXhMLIrVAKKXi4gozGC9v7YRWVnXIZzjNPTO02M9BdxLCWqK1yRXgNziVyJQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "BSD-3-Clause" + }, + "node_modules/finalhandler": { + "version": "2.1.1", + "resolved": "https://registry.npmmirror.com/finalhandler/-/finalhandler-2.1.1.tgz", + "integrity": "sha512-S8KoZgRZN+a5rNwqTxlZZePjT/4cnm0ROV70LedRHZ0p8u9fRID0hJUZQpkKLzro8LfmC8sx23bY6tVNxv8pQA==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "on-finished": "^2.4.1", + "parseurl": "^1.3.3", + "statuses": "^2.0.1" + }, + "engines": { + "node": ">= 18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmmirror.com/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/fresh": { + "version": "2.0.0", + "resolved": "https://registry.npmmirror.com/fresh/-/fresh-2.0.0.tgz", + "integrity": "sha512-Rx/WycZ60HOaqLKAi6cHRKKI7zxWbJ31MhntmtwMoaTeF7XFH9hhBp8vITaMidfljRQ6eYWCKkaTK+ykVJHP2A==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmmirror.com/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmmirror.com/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmmirror.com/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmmirror.com/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmmirror.com/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.4", + "resolved": "https://registry.npmmirror.com/hasown/-/hasown-2.0.4.tgz", + "integrity": "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==", + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/hono": { + "version": "4.13.18", + "resolved": "https://registry.npmmirror.com/hono/-/hono-4.13.18.tgz", + "integrity": "sha512-H9q/E1XizaWstJxUW881dmLxEj+t/TrGGnyxINkm4+sK0qUwsMtFdo2BWI9Vs3cIS19MU6b6iCijoUoEf4f3DA==", + "license": "MIT", + "engines": { + "node": ">=16.9.0" + } + }, + "node_modules/http-errors": { + "version": "2.0.1", + "resolved": "https://registry.npmmirror.com/http-errors/-/http-errors-2.0.1.tgz", + "integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==", + "license": "MIT", + "dependencies": { + "depd": "~2.0.0", + "inherits": "~2.0.4", + "setprototypeof": "~1.2.0", + "statuses": "~2.0.2", + "toidentifier": "~1.0.1" + }, + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/iconv-lite": { + "version": "0.7.3", + "resolved": "https://registry.npmmirror.com/iconv-lite/-/iconv-lite-0.7.3.tgz", + "integrity": "sha512-IKXpvIzjnC9XTAUbVBcMfGS0EPaIXtW6v+zr+RRp+hqULEpo0owZax6wyRwPOJbWbzjYspQwusTsfVr0ifh4uQ==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmmirror.com/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" + }, + "node_modules/ip-address": { + "version": "10.7.6", + "resolved": "https://registry.npmmirror.com/ip-address/-/ip-address-10.7.6.tgz", + "integrity": "sha512-00LYkM6byILa9eQIx6qoaXCM40xTLd56AZ4vyaPW3YTyb49VZYPo5xsiAq4tuQ54GH4R7gVdYScJ1B7B77PuTg==", + "license": "MIT", + "engines": { + "node": ">= 12" + } + }, + "node_modules/ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmmirror.com/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/is-promise": { + "version": "4.0.0", + "resolved": "https://registry.npmmirror.com/is-promise/-/is-promise-4.0.0.tgz", + "integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==", + "license": "MIT" + }, + "node_modules/isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmmirror.com/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", + "license": "ISC" + }, + "node_modules/jose": { + "version": "6.2.17", + "resolved": "https://registry.npmmirror.com/jose/-/jose-6.2.17.tgz", + "integrity": "sha512-wOuK9V1yujQFH5S0ZWmLcDZN7+IKh9bUCC2ZPCf2HDlol3WWHy+PPyNjEI75FXRLMp3vDKQcupP59xhVU95hcg==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/panva" + } + }, + "node_modules/json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmmirror.com/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", + "license": "MIT" + }, + "node_modules/json-schema-typed": { + "version": "8.0.2", + "resolved": "https://registry.npmmirror.com/json-schema-typed/-/json-schema-typed-8.0.2.tgz", + "integrity": "sha512-fQhoXdcvc3V28x7C7BMs4P5+kNlgUURe2jmUT1T//oBRMDrqy1QPelJimwZGo7Hg9VPV3EQV5Bnq4hbFy2vetA==", + "license": "BSD-2-Clause" + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmmirror.com/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/media-typer": { + "version": "1.1.1", + "resolved": "https://registry.npmmirror.com/media-typer/-/media-typer-1.1.1.tgz", + "integrity": "sha512-yz3xRaG20c6/BOzvYoDaGtPmGscs7YivItZEEqe6GbwNfHuxu9YNmvnEkMzKldAGY4/80pRcQRZSEnhquk9XuQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/merge-descriptors": { + "version": "2.0.0", + "resolved": "https://registry.npmmirror.com/merge-descriptors/-/merge-descriptors-2.0.0.tgz", + "integrity": "sha512-Snk314V5ayFLhp3fkUREub6WtjBfPdCPY1Ln8/8munuLuiYhsABgBVWsozAG+MWMbVEvcdcpbi9R7ww22l9Q3g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/mime-db": { + "version": "1.54.0", + "resolved": "https://registry.npmmirror.com/mime-db/-/mime-db-1.54.0.tgz", + "integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "3.0.2", + "resolved": "https://registry.npmmirror.com/mime-types/-/mime-types-3.0.2.tgz", + "integrity": "sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A==", + "license": "MIT", + "dependencies": { + "mime-db": "^1.54.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmmirror.com/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/negotiator": { + "version": "1.1.0", + "resolved": "https://registry.npmmirror.com/negotiator/-/negotiator-1.1.0.tgz", + "integrity": "sha512-NMPBRMJgiQHjbd8phG3Vebdx4kZ1H121rbl5IkMqeOsahptB9BKo/d7oJ3zTXqTgagn2bWlNSXkh0QUGM31RYg==", + "license": "MIT", + "dependencies": { + "content-type": "^2.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/negotiator/node_modules/content-type": { + "version": "2.1.0", + "resolved": "https://registry.npmmirror.com/content-type/-/content-type-2.1.0.tgz", + "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmmirror.com/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/object-inspect": { + "version": "1.13.4", + "resolved": "https://registry.npmmirror.com/object-inspect/-/object-inspect-1.13.4.tgz", + "integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/on-finished": { + "version": "2.4.2", + "resolved": "https://registry.npmmirror.com/on-finished/-/on-finished-2.4.2.tgz", + "integrity": "sha512-ATWUv387i+nLD49EiDoOft5scf32Vsdz3ktrQJG9bi7jRFmaXTssvyvj1EmeKVVgvesbqUtvQQvaS2TpTukAZQ==", + "license": "MIT", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmmirror.com/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "license": "ISC", + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmmirror.com/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/path-key": { + "version": "3.1.1", + "resolved": "https://registry.npmmirror.com/path-key/-/path-key-3.1.1.tgz", + "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/path-to-regexp": { + "version": "8.4.2", + "resolved": "https://registry.npmmirror.com/path-to-regexp/-/path-to-regexp-8.4.2.tgz", + "integrity": "sha512-qRcuIdP69NPm4qbACK+aDogI5CBDMi1jKe0ry5rSQJz8JVLsC7jV8XpiJjGRLLol3N+R5ihGYcrPLTno6pAdBA==", + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/pkce-challenge": { + "version": "5.0.1", + "resolved": "https://registry.npmmirror.com/pkce-challenge/-/pkce-challenge-5.0.1.tgz", + "integrity": "sha512-wQ0b/W4Fr01qtpHlqSqspcj3EhBvimsdh0KlHhH8HRZnMsEa0ea2fTULOXOS9ccQr3om+GcGRk4e+isrZWV8qQ==", + "license": "MIT", + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/playwright": { + "version": "1.66.0-alpha-2026-11-09", + "resolved": "https://registry.npmmirror.com/playwright/-/playwright-1.66.0-alpha-2026-11-09.tgz", + "integrity": "sha512-raaHhtTYsm9ydMdGa11MEIwI8KM9RP4CuyvAb/lbd6e3bYhsBHXPMVGvyYJWldQxwUgMYnngY2VY5vCpimB2Dg==", + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.66.0-alpha-2026-11-09" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright-core": { + "version": "1.66.0-alpha-2026-11-09", + "resolved": "https://registry.npmmirror.com/playwright-core/-/playwright-core-1.66.0-alpha-2026-11-09.tgz", + "integrity": "sha512-HZVc/E3nqrasH7PPvJNwc7aCNsKXKjvtViZBpeFfopGOeB0zr3r5q4TKfqzkrdIcCKWCulyLVVGtla/LYbmxUQ==", + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/proxy-addr": { + "version": "2.0.8", + "resolved": "https://registry.npmmirror.com/proxy-addr/-/proxy-addr-2.0.8.tgz", + "integrity": "sha512-5nnx0yGyVUcY6t9RnWcARWtwT9F1D8O9rt08htPvnd49W1IgZtmLkhu9WfMzQj1cFxjHIO6connUNVW5k7AVyQ==", + "license": "MIT", + "dependencies": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + }, + "engines": { + "node": ">= 0.10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/qs": { + "version": "6.17.1", + "resolved": "https://registry.npmmirror.com/qs/-/qs-6.17.1.tgz", + "integrity": "sha512-jd3COkaKVgGUW+ZHyEHCG9bH7RoaaiWuMgzsIBjUMVjLAaWGWpoUAVwljVvBHxRu/2qEHnadLGZrkutYUBPQJA==", + "license": "BSD-3-Clause", + "dependencies": { + "es-define-property": "^1.0.1", + "side-channel": "^1.1.1" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/range-parser": { + "version": "1.3.0", + "resolved": "https://registry.npmmirror.com/range-parser/-/range-parser-1.3.0.tgz", + "integrity": "sha512-hek2mFQpPuI4E1BBKrSto+BU3e3x4xuarsbiwr3+lf7p44juvFMV0XFWQAP3xUyqXA4RrXLIoaSUGbSt056ZMw==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/raw-body": { + "version": "3.0.2", + "resolved": "https://registry.npmmirror.com/raw-body/-/raw-body-3.0.2.tgz", + "integrity": "sha512-K5zQjDllxWkf7Z5xJdV0/B0WTNqx6vxG70zJE4N0kBs4LovmEYWJzQGxC9bS9RAKu3bgM40lrd5zoLJ12MQ5BA==", + "license": "MIT", + "dependencies": { + "bytes": "~3.1.2", + "http-errors": "~2.0.1", + "iconv-lite": "~0.7.0", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/require-from-string": { + "version": "2.0.2", + "resolved": "https://registry.npmmirror.com/require-from-string/-/require-from-string-2.0.2.tgz", + "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/router": { + "version": "2.2.0", + "resolved": "https://registry.npmmirror.com/router/-/router-2.2.0.tgz", + "integrity": "sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.0", + "depd": "^2.0.0", + "is-promise": "^4.0.0", + "parseurl": "^1.3.3", + "path-to-regexp": "^8.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmmirror.com/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" + }, + "node_modules/send": { + "version": "1.2.1", + "resolved": "https://registry.npmmirror.com/send/-/send-1.2.1.tgz", + "integrity": "sha512-1gnZf7DFcoIcajTjTwjwuDjzuz4PPcY2StKPlsGAQ1+YH20IRVrBaXSWmdjowTJ6u8Rc01PoYOGHXfP1mYcZNQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.3", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "fresh": "^2.0.0", + "http-errors": "^2.0.1", + "mime-types": "^3.0.2", + "ms": "^2.1.3", + "on-finished": "^2.4.1", + "range-parser": "^1.2.1", + "statuses": "^2.0.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/serve-static": { + "version": "2.2.1", + "resolved": "https://registry.npmmirror.com/serve-static/-/serve-static-2.2.1.tgz", + "integrity": "sha512-xRXBn0pPqQTVQiC8wyQrKs2MOlX24zQ0POGaj0kultvoOCstBQM5yvOhAVSUwOMjQtTvsPWoNCHfPGwaaQJhTw==", + "license": "MIT", + "dependencies": { + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "parseurl": "^1.3.3", + "send": "^1.2.0" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmmirror.com/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==", + "license": "ISC" + }, + "node_modules/shebang-command": { + "version": "2.0.0", + "resolved": "https://registry.npmmirror.com/shebang-command/-/shebang-command-2.0.0.tgz", + "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", + "license": "MIT", + "dependencies": { + "shebang-regex": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/shebang-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmmirror.com/shebang-regex/-/shebang-regex-3.0.0.tgz", + "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/side-channel": { + "version": "1.1.1", + "resolved": "https://registry.npmmirror.com/side-channel/-/side-channel-1.1.1.tgz", + "integrity": "sha512-6x6dK6zJdpTzF4sQeNYxwtvBzf6Eg4GtlesS94HOvTudUeyK2WXAaIfmDgsyslYrRBeFIlsi54AYsFGUuhmvrQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.4", + "side-channel-list": "^1.0.1", + "side-channel-map": "^1.0.1", + "side-channel-weakmap": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-list": { + "version": "1.0.1", + "resolved": "https://registry.npmmirror.com/side-channel-list/-/side-channel-list-1.0.1.tgz", + "integrity": "sha512-mjn/0bi/oUURjc5Xl7IaWi/OJJJumuoJFQJfDDyO46+hBWsfaVM65TBHq2eoZBhzl9EchxOijpkbRC8SVBQU0w==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.4" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-map": { + "version": "1.0.1", + "resolved": "https://registry.npmmirror.com/side-channel-map/-/side-channel-map-1.0.1.tgz", + "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-weakmap": { + "version": "1.0.2", + "resolved": "https://registry.npmmirror.com/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz", + "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3", + "side-channel-map": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/statuses": { + "version": "2.0.2", + "resolved": "https://registry.npmmirror.com/statuses/-/statuses-2.0.2.tgz", + "integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmmirror.com/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "license": "MIT", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/type-is": { + "version": "2.1.0", + "resolved": "https://registry.npmmirror.com/type-is/-/type-is-2.1.0.tgz", + "integrity": "sha512-faYHw0anBbc/kWF3zFTEnxSFOAGUX9GFbOBthvDdLsIlEoWOFOtS0zgCiQYwIskL9iGXZL3kAXD8OoZ4GmMATA==", + "license": "MIT", + "dependencies": { + "content-type": "^2.0.0", + "media-typer": "^1.1.0", + "mime-types": "^3.0.0" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/type-is/node_modules/content-type": { + "version": "2.1.0", + "resolved": "https://registry.npmmirror.com/content-type/-/content-type-2.1.0.tgz", + "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmmirror.com/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/vary": { + "version": "1.1.2", + "resolved": "https://registry.npmmirror.com/vary/-/vary-1.1.2.tgz", + "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/which": { + "version": "2.0.2", + "resolved": "https://registry.npmmirror.com/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "license": "ISC", + "dependencies": { + "isexe": "^2.0.0" + }, + "bin": { + "node-which": "bin/node-which" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmmirror.com/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "license": "ISC" + }, + "node_modules/zod": { + "version": "4.6.7", + "resolved": "https://registry.npmmirror.com/zod/-/zod-4.6.7.tgz", + "integrity": "sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } + }, + "node_modules/zod-to-json-schema": { + "version": "3.25.2", + "resolved": "https://registry.npmmirror.com/zod-to-json-schema/-/zod-to-json-schema-3.25.2.tgz", + "integrity": "sha512-O/PgfnpT1xKSDeQYSCfRI5Gy3hPf91mKVDuYLUHZJMiDFptvP41MSnWofm8dnCm0256ZNfZIM7DSzuSMAFnjHA==", + "license": "ISC", + "peerDependencies": { + "zod": "^3.25.28 || ^4" + } + } + } +} diff --git a/build/sandbox/mcp/package.json b/build/sandbox/mcp/package.json new file mode 100644 index 0000000..d04f900 --- /dev/null +++ b/build/sandbox/mcp/package.json @@ -0,0 +1,10 @@ +{ + "name": "agentdrive-sandbox-mcp", + "version": "1.0.0", + "private": true, + "type": "module", + "dependencies": { + "@modelcontextprotocol/sdk": "1.32.2", + "@playwright/mcp": "0.0.85" + } +} diff --git a/build/sandbox/mcp/playwright.json b/build/sandbox/mcp/playwright.json new file mode 100644 index 0000000..a59910c --- /dev/null +++ b/build/sandbox/mcp/playwright.json @@ -0,0 +1,11 @@ +{ + "browser": { + "browserName": "chromium", + "cdpEndpoint": "http://127.0.0.1:9222", + "cdpTimeout": 15000 + }, + "outputDir": "/workspace/Downloads", + "outputMaxSize": 0, + "webmcp": false, + "timeouts": {"action": 10000, "navigation": 60000} +} diff --git a/build/sandbox/mcp/providers.json b/build/sandbox/mcp/providers.json new file mode 100644 index 0000000..9416202 --- /dev/null +++ b/build/sandbox/mcp/providers.json @@ -0,0 +1,17 @@ +{ + "providers": [ + { + "id": "desktop", + "prefix": "", + "command": "/usr/bin/python3", + "args": ["/usr/local/bin/desktop_mcp.py"] + }, + { + "id": "browser", + "prefix": "", + "command": "/usr/local/bin/node", + "args": ["/opt/agentdrive-mcp/node_modules/@playwright/mcp/cli.js", "--config", "/opt/agentdrive-mcp/playwright.json"], + "prepare": ["/usr/bin/python3", "/usr/local/bin/browser-session.py", "--ensure"] + } + ] +} diff --git a/build/sandbox/novnc-index.html b/build/sandbox/novnc-index.html new file mode 100644 index 0000000..e86fdd0 --- /dev/null +++ b/build/sandbox/novnc-index.html @@ -0,0 +1,5 @@ + + + +Ubuntu desktop +

Open noVNC

diff --git a/build/sandbox/prepare-network.sh b/build/sandbox/prepare-network.sh new file mode 100755 index 0000000..a074954 --- /dev/null +++ b/build/sandbox/prepare-network.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +set -euo pipefail +owner_uid=${1:?usage: sudo prepare-network.sh AGENTDRIVE_UID} +[[ $EUID == 0 && $owner_uid =~ ^[0-9]+$ ]] || { echo 'Root and a numeric UID are required' >&2; exit 1; } +# Dedicated interfaces and rules; existing host routes and firewall chains are preserved. +for slot in 0 1; do + tap="adsb${slot}" + gateway=$((slot * 4 + 1)) + if ! ip link show "$tap" >/dev/null 2>&1; then + ip tuntap add dev "$tap" mode tap user "$owner_uid" + fi + ip address replace "172.30.80.${gateway}/30" dev "$tap" + ip link set "$tap" up +done +sysctl -w net.ipv4.ip_forward=1 +iptables -w -N ADSB-OUT 2>/dev/null || true +iptables -w -N ADSB-IN 2>/dev/null || true +add_rule() { local chain=$1; shift; iptables -w -C "$chain" "$@" 2>/dev/null || iptables -w -A "$chain" "$@"; } +for cidr in 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 169.254.0.0/16 127.0.0.0/8; do + add_rule ADSB-OUT -d "$cidr" -j REJECT +done +for slot in 0 1; do + guest=$((slot * 4 + 2)) + add_rule ADSB-OUT -i "adsb${slot}" -s "172.30.80.${guest}/32" -j ACCEPT + add_rule ADSB-IN -o "adsb${slot}" -d "172.30.80.${guest}/32" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT +done +add_rule ADSB-OUT -j DROP +add_rule ADSB-IN -j DROP +iptables -w -C FORWARD -i 'adsb+' -j ADSB-OUT 2>/dev/null || iptables -w -I FORWARD 1 -i 'adsb+' -j ADSB-OUT +iptables -w -C FORWARD -o 'adsb+' -j ADSB-IN 2>/dev/null || iptables -w -I FORWARD 1 -o 'adsb+' -j ADSB-IN +iptables -w -C INPUT -i 'adsb+' -j DROP 2>/dev/null || iptables -w -I INPUT 1 -i 'adsb+' -j DROP +iptables -w -t nat -C POSTROUTING -s 172.30.80.0/29 ! -o 'adsb+' -j MASQUERADE 2>/dev/null || iptables -w -t nat -A POSTROUTING -s 172.30.80.0/29 ! -o 'adsb+' -j MASQUERADE +# This configuration provides IPv4 Internet access only. +for chain in INPUT FORWARD; do + ip6tables -w -C "$chain" -i 'adsb+' -j DROP 2>/dev/null || ip6tables -w -I "$chain" 1 -i 'adsb+' -j DROP +done +ip6tables -w -C FORWARD -o 'adsb+' -j DROP 2>/dev/null || ip6tables -w -I FORWARD 1 -o 'adsb+' -j DROP diff --git a/build/sandbox/start-desktop.sh b/build/sandbox/start-desktop.sh new file mode 100644 index 0000000..bc2e5ea --- /dev/null +++ b/build/sandbox/start-desktop.sh @@ -0,0 +1,213 @@ +#!/bin/bash +# Start dummy Xorg (or Xvfb fallback), Ubuntu GNOME, x11vnc, and noVNC. +# Foreground process is this script so cube-entrypoint / docker stop can signal it. +set -euo pipefail + +DISPLAY_NUM="${DISPLAY:-:0}" +SCREEN_WIDTH="${SCREEN_WIDTH:-1280}" +SCREEN_HEIGHT="${SCREEN_HEIGHT:-720}" +SCREEN_DEPTH="${SCREEN_DEPTH:-24}" +VNC_PORT="${VNC_PORT:-5900}" +NOVNC_PORT="${NOVNC_PORT:-6080}" +DESKTOP_USER="${DESKTOP_USER:-user}" +NOVNC_WEB="${NOVNC_WEB:-/usr/share/novnc}" +XORG_LOG="${XORG_LOG:-/var/log/Xorg.0.log}" +FORCE_DUMMY="${FORCE_DUMMY:-0}" +DISPLAY_BACKEND="" + +export DISPLAY="${DISPLAY_NUM}" +export HOME="${HOME:-/home/${DESKTOP_USER}}" +export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp/runtime-${DESKTOP_USER}}" +export XORG_RUN_AS_USER_OK=1 + +mkdir -p /tmp/.X11-unix "${XDG_RUNTIME_DIR}" /var/log "/home/${DESKTOP_USER}" +chmod 1777 /tmp/.X11-unix +chown "${DESKTOP_USER}:${DESKTOP_USER}" "${XDG_RUNTIME_DIR}" "/home/${DESKTOP_USER}" +chmod 700 "${XDG_RUNTIME_DIR}" + +rm -f /tmp/.X0-lock /tmp/.X11-unix/X0 /tmp/.X11-unix/X0.lock + +PIDS=() + +cleanup() { + trap - TERM INT HUP EXIT + for pid in "${PIDS[@]:-}"; do + kill "${pid}" 2>/dev/null || true + done + wait 2>/dev/null || true +} +trap cleanup TERM INT HUP EXIT + +log() { + echo "start-desktop: $*" >&2 +} + +wait_for_display() { + local tries="${1:-50}" + local i + for i in $(seq 1 "${tries}"); do + if [ -S /tmp/.X11-unix/X0 ] && xdpyinfo -display "${DISPLAY_NUM}" >/dev/null 2>&1; then + return 0 + fi + sleep 0.2 + done + return 1 +} + +start_dummy_xorg() { + log "starting Xorg dummy on ${DISPLAY_NUM} (${SCREEN_WIDTH}x${SCREEN_HEIGHT})" + Xorg "${DISPLAY_NUM}" \ + -config /etc/X11/xorg.conf \ + -noreset \ + -nolisten tcp \ + -novtswitch \ + +extension COMPOSITE \ + +extension RANDR \ + +extension RENDER \ + +extension GLX \ + -logfile "${XORG_LOG}" & + PIDS+=("$!") + if wait_for_display 50; then + DISPLAY_BACKEND="dummy-xorg" + return 0 + fi + log "Xorg dummy did not become ready" + if [ -f "${XORG_LOG}" ]; then + log "----- ${XORG_LOG} (tail) -----" + tail -n 80 "${XORG_LOG}" >&2 || true + fi + return 1 +} + +start_xvfb() { + log "falling back to Xvfb on ${DISPLAY_NUM} (${SCREEN_WIDTH}x${SCREEN_HEIGHT}x${SCREEN_DEPTH})" + rm -f /tmp/.X0-lock /tmp/.X11-unix/X0 + Xvfb "${DISPLAY_NUM}" -screen 0 "${SCREEN_WIDTH}x${SCREEN_HEIGHT}x${SCREEN_DEPTH}" -nolisten tcp & + PIDS+=("$!") + if wait_for_display 50; then + DISPLAY_BACKEND="xvfb" + return 0 + fi + log "Xvfb failed to become ready" + return 1 +} + +if ! start_dummy_xorg; then + if [ "${FORCE_DUMMY}" = "1" ]; then + log "FORCE_DUMMY=1 set; not falling back to Xvfb" + exit 1 + fi + # Drop the failed Xorg pid before starting Xvfb. + if [ "${#PIDS[@]}" -gt 0 ]; then + kill "${PIDS[-1]}" 2>/dev/null || true + unset "PIDS[-1]" + fi + start_xvfb +fi + +log "display backend=${DISPLAY_BACKEND}" +xdpyinfo -display "${DISPLAY_NUM}" | awk '/dimensions:/{print "start-desktop: "$0}' >&2 || true + +xhost +local: >/dev/null + +# gnome-session talks to the system bus. Docker / Cube images do not +# start systemd, so bring dbus-daemon --system up ourselves. +if [ ! -S /run/dbus/system_bus_socket ] && [ ! -S /var/run/dbus/system_bus_socket ]; then + log "starting system dbus" + mkdir -p /run/dbus /var/run/dbus + dbus-uuidgen --ensure >/dev/null 2>&1 || true + dbus-daemon --system --fork +fi + +# gnome-shell's LoginManagerSystemd requires org.freedesktop.login1. +# systemd is already a GNOME dependency; logind can run without PID 1. +if ! pgrep -x systemd-logind >/dev/null 2>&1; then + log "starting systemd-logind" + mkdir -p /run/systemd/system + /lib/systemd/systemd-logind & + PIDS+=("$!") + for i in $(seq 1 30); do + if [ -S /run/systemd/private ] || pgrep -x systemd-logind >/dev/null 2>&1; then + if gdbus introspect --system --dest org.freedesktop.login1 \ + --object-path /org/freedesktop/login1 >/dev/null 2>&1; then + break + fi + fi + sleep 0.2 + done +fi + +# Ubuntu GNOME (Yaru + left dock). gnome-shell needs its own session bus +# as ${DESKTOP_USER}, plus software GL on dummy/Xvfb. +log "starting Ubuntu GNOME session as ${DESKTOP_USER}" +sudo -u "${DESKTOP_USER}" -H env \ + DISPLAY="${DISPLAY_NUM}" \ + HOME="/home/${DESKTOP_USER}" \ + XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR}" \ + XDG_SESSION_TYPE=x11 \ + XDG_CURRENT_DESKTOP=ubuntu:GNOME \ + GNOME_SHELL_SESSION_MODE=ubuntu \ + LIBGL_ALWAYS_SOFTWARE=1 \ + GALLIUM_DRIVER=llvmpipe \ + dbus-run-session -- gnome-session --session=ubuntu --disable-acceleration-check & +PIDS+=("$!") + +for i in $(seq 1 50); do + if pgrep -u "${DESKTOP_USER}" -x gnome-shell >/dev/null 2>&1; then + log "gnome-shell is up" + break + fi + sleep 0.3 +done + +apply_user_settings() { + local shell_pid bus + shell_pid="$(pgrep -u "${DESKTOP_USER}" -x gnome-shell | head -n1 || true)" + [ -n "${shell_pid}" ] || return 0 + bus="$(tr '\0' '\n' < "/proc/${shell_pid}/environ" | sed -n 's/^DBUS_SESSION_BUS_ADDRESS=//p' | head -n1 || true)" + [ -n "${bus}" ] || return 0 + sudo -u "${DESKTOP_USER}" -H env \ + DISPLAY="${DISPLAY_NUM}" \ + HOME="/home/${DESKTOP_USER}" \ + DBUS_SESSION_BUS_ADDRESS="${bus}" \ + gsettings set org.gnome.shell favorite-apps \ + "['org.gnome.Nautilus.desktop', 'org.gnome.Terminal.desktop', 'chromium.desktop']" || true + sudo -u "${DESKTOP_USER}" env \ + DISPLAY="${DISPLAY_NUM}" \ + XDG_CURRENT_DESKTOP=ubuntu:GNOME \ + DBUS_SESSION_BUS_ADDRESS="${bus}" \ + xdg-settings set default-web-browser chromium.desktop + for mime in x-scheme-handler/http x-scheme-handler/https text/html; do + sudo -u "${DESKTOP_USER}" xdg-mime default chromium.desktop "$mime" + done +} + +apply_user_settings + +log "starting x11vnc on :${VNC_PORT}" +x11vnc \ + -display "${DISPLAY_NUM}" \ + -rfbport "${VNC_PORT}" \ + -forever \ + -shared \ + -nopw \ + -noxdamage \ + -noxfixes \ + -noxrandr \ + -o /var/log/x11vnc.log & +PIDS+=("$!") + +for i in $(seq 1 40); do + if ss -lnt 2>/dev/null | grep -q ":${VNC_PORT} "; then + break + fi + sleep 0.25 +done + +log "starting noVNC on :${NOVNC_PORT} (web=${NOVNC_WEB})" +websockify --web="${NOVNC_WEB}" "${NOVNC_PORT}" "localhost:${VNC_PORT}" & +PIDS+=("$!") +NOVNC_PID="${PIDS[-1]}" + +log "ready: backend=${DISPLAY_BACKEND} novnc=http://0.0.0.0:${NOVNC_PORT}/" +wait "${NOVNC_PID}" diff --git a/build/sandbox/template-manifest.py b/build/sandbox/template-manifest.py new file mode 100644 index 0000000..635a151 --- /dev/null +++ b/build/sandbox/template-manifest.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +import datetime +import json +import os +import subprocess +import sys + + +def command(*args): + return subprocess.check_output(args, text=True, stderr=subprocess.DEVNULL, timeout=15).strip() + + +def package(name): + return command("dpkg-query", "-W", "-f=${Package} ${Version}", name) + + +os_release = {} +with open("/etc/os-release") as source: + for line in source: + key, _, value = line.strip().partition("=") + os_release[key] = value.strip('"') + +print(json.dumps({ + "name": "Ubuntu Desktop", + "os": os_release["PRETTY_NAME"], + "desktop": command("gnome-shell", "--version") + " / X11", + "browser": command("/opt/chromium/chrome", "--version"), + "tools": ["computer-use-linux " + sys.argv[1], + "Playwright MCP " + json.load(open('/opt/agentdrive-mcp/node_modules/@playwright/mcp/package.json'))['version'], + "Node.js " + command('node', '--version'), + *[package(name) for name in ["gnome-terminal", "nautilus", "gedit", "xdg-utils", "xclip"]]], + "fonts": [package(name) for name in ["fonts-noto-cjk", "fonts-noto-color-emoji"]], + "locale": os.environ.get("LANG", "C.UTF-8"), + "workspace_path": "/workspace", + "built_at": datetime.datetime.now(datetime.timezone.utc).isoformat(), +}, ensure_ascii=False, indent=2)) diff --git a/build/sandbox/xorg.conf b/build/sandbox/xorg.conf new file mode 100644 index 0000000..1b3f0ff --- /dev/null +++ b/build/sandbox/xorg.conf @@ -0,0 +1,38 @@ +# Dummy framebuffer for Cube MicroVM and local Docker. +# 1280x720 keeps x11vnc cheaper than 1080p on software rendering. +Section "ServerFlags" + Option "DontVTSwitch" "true" + Option "AllowMouseOpenFail" "true" + Option "PciForceNone" "true" + Option "AutoAddDevices" "false" + Option "AutoEnableDevices" "false" +EndSection + +Section "Device" + Identifier "DummyDevice" + Driver "dummy" + VideoRam 256000 +EndSection + +Section "Monitor" + Identifier "DummyMonitor" + HorizSync 28.0-80.0 + VertRefresh 48.0-75.0 + Modeline "1280x720" 74.50 1280 1344 1472 1664 720 723 728 748 +EndSection + +Section "Screen" + Identifier "DummyScreen" + Device "DummyDevice" + Monitor "DummyMonitor" + DefaultDepth 24 + SubSection "Display" + Depth 24 + Modes "1280x720" + EndSubSection +EndSection + +Section "ServerLayout" + Identifier "DummyLayout" + Screen "DummyScreen" +EndSection diff --git a/crates/agentdrive-engine/src/engine.rs b/crates/agentdrive-engine/src/engine.rs index a90b86e..ea7af91 100644 --- a/crates/agentdrive-engine/src/engine.rs +++ b/crates/agentdrive-engine/src/engine.rs @@ -144,6 +144,7 @@ impl Engine { versions.set_write_access(Arc::downgrade(&write_access)); collaboration.initialize().await?; let services = Services { + sandbox: None, collaboration: collaboration.clone(), workspace: workspace.clone(), revisions: versions, diff --git a/crates/agentdrive-engine/src/lib.rs b/crates/agentdrive-engine/src/lib.rs index 26c5ced..bd09a5a 100644 --- a/crates/agentdrive-engine/src/lib.rs +++ b/crates/agentdrive-engine/src/lib.rs @@ -52,3 +52,6 @@ pub(crate) use ownership::OwnershipService; pub(crate) use merge::MergeService; pub use engine::{Adapters, Engine, EngineConfig}; + +mod sandbox; +pub use sandbox::SandboxService; diff --git a/crates/agentdrive-engine/src/mounts.rs b/crates/agentdrive-engine/src/mounts.rs index 6e19b03..2847e24 100644 --- a/crates/agentdrive-engine/src/mounts.rs +++ b/crates/agentdrive-engine/src/mounts.rs @@ -274,6 +274,7 @@ impl MountService { async fn pause_old(&self, old: &MountBinding) -> Result<()> { let _gate = self.runtime.lock_branch(old.branch).await; self.runtime.check_mutation(old.branch)?; + self.runtime.check_sandbox(old.branch)?; let mut current = self .runtime .state @@ -650,6 +651,7 @@ impl MountService { ) -> Result { let _gate = self.runtime.lock_branch(target.branch).await; self.runtime.check_mutation(target.branch)?; + self.runtime.check_sandbox(target.branch)?; let _protection = self.runtime.objects.read().await; let branch = self.runtime.branch(target.branch).await?; check_guard(&branch, &target)?; @@ -1428,6 +1430,7 @@ impl RevisionTransition for MountService { let record = { let _gate = self.runtime.lock_branch(target.branch).await; self.runtime.check_mutation(target.branch)?; + self.runtime.check_sandbox(target.branch)?; let branch = self.runtime.branch(target.branch).await?; check_guard(&branch, &target)?; self.revisions diff --git a/crates/agentdrive-engine/src/mounts/collaboration.rs b/crates/agentdrive-engine/src/mounts/collaboration.rs index 8d20a75..1451f02 100644 --- a/crates/agentdrive-engine/src/mounts/collaboration.rs +++ b/crates/agentdrive-engine/src/mounts/collaboration.rs @@ -177,6 +177,10 @@ impl MountService { "collaborative mount was changed by another operation", )); } + if old.branch != next.branch { + self.runtime.check_sandbox(old.branch)?; + self.runtime.check_sandbox(next.branch)?; + } let target = self.runtime.branch(next.branch).await?; if target.generation != next.branch_generation.unwrap_or(next.generation) { return Err(Error::new( diff --git a/crates/agentdrive-engine/src/ownership.rs b/crates/agentdrive-engine/src/ownership.rs index 9ac502c..763fd95 100644 --- a/crates/agentdrive-engine/src/ownership.rs +++ b/crates/agentdrive-engine/src/ownership.rs @@ -49,6 +49,7 @@ impl OwnershipService { let branch = { let _gate = self.runtime.lock_branch(request.guard.branch).await; self.runtime.check_mutation(request.guard.branch)?; + self.runtime.check_sandbox(request.guard.branch)?; let _protection = self.runtime.objects.read().await; let mut branch = self.runtime.branch(request.guard.branch).await?; check_guard(&branch, &request.guard)?; diff --git a/crates/agentdrive-engine/src/runtime.rs b/crates/agentdrive-engine/src/runtime.rs index c7938c8..287382a 100644 --- a/crates/agentdrive-engine/src/runtime.rs +++ b/crates/agentdrive-engine/src/runtime.rs @@ -14,6 +14,7 @@ pub struct Runtime { pub location: LocationId, pub(crate) objects: RwLock<()>, pub(crate) lifecycle: AsyncMutex<()>, + pub(crate) sandbox_pins: Mutex>, pub(crate) stopped: Mutex>, pub(crate) collaboration_blocked: Mutex>, pub(crate) publishing_turns: Mutex>, @@ -45,6 +46,7 @@ impl Runtime { location, objects: RwLock::new(()), lifecycle: AsyncMutex::new(()), + sandbox_pins: Mutex::new(BTreeSet::new()), stopped: Mutex::new(BTreeSet::new()), collaboration_blocked: Mutex::new(BTreeSet::new()), publishing_turns: Mutex::new(BTreeSet::new()), @@ -86,6 +88,16 @@ impl Runtime { Ok(branch) } + pub(crate) fn check_sandbox(&self, branch: BranchId) -> Result<()> { + if self.sandbox_pins.lock().contains(&branch) { + return Err(Error::new( + ErrorCode::BranchBusy, + "Stop the branch Sandbox before changing its binding", + )); + } + Ok(()) + } + pub(crate) fn check_mutation(&self, branch: BranchId) -> Result<()> { if self.collaboration_blocked.lock().contains(&branch) || self.publishing_turns.lock().contains(&branch) diff --git a/crates/agentdrive-engine/src/sandbox.rs b/crates/agentdrive-engine/src/sandbox.rs new file mode 100644 index 0000000..cc039a2 --- /dev/null +++ b/crates/agentdrive-engine/src/sandbox.rs @@ -0,0 +1,506 @@ +use crate::Runtime; +use agentdrive_model::*; +use agentdrive_ports::*; +use async_trait::async_trait; +use std::{path::PathBuf, sync::Arc}; +use tokio::sync::Mutex; + +pub struct SandboxService { + runtime: Arc, + services: Services, + driver: Arc, + mount_root: PathBuf, + operations: Mutex<()>, +} +impl std::fmt::Debug for SandboxService { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SandboxService").finish_non_exhaustive() + } +} +impl SandboxService { + pub async fn reconcile_exited(&self) -> Result<()> { + let Ok(_operation) = self.operations.try_lock() else { + return Ok(()); + }; + let branches: Vec<_> = self.runtime.sandbox_pins.lock().iter().copied().collect(); + for branch in branches { + match self.driver.status(&branch.to_string()).await { + Ok(status) if matches!(status.state, SandboxState::Stopped) => { + self.runtime.sandbox_pins.lock().remove(&branch); + } + Err(error) if error.code == ErrorCode::NotFound => { + self.runtime.sandbox_pins.lock().remove(&branch); + } + Err(error) => return Err(error), + _ => {} + } + } + Ok(()) + } + + pub fn new( + runtime: Arc, + services: Services, + driver: Arc, + mount_root: PathBuf, + ) -> Arc { + Arc::new(Self { + runtime, + services, + driver, + mount_root, + operations: Mutex::new(()), + }) + } +} +#[async_trait] +impl SandboxApi for SandboxService { + async fn proxy_config( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result { + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Write) + .await?; + self.driver.proxy_config(&branch.to_string()).await + } + async fn set_proxy_config( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + config: SandboxProxyConfig, + ) -> Result<()> { + let _operation = self.operations.lock().await; + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Manage) + .await?; + self.driver + .set_proxy_config(&branch.to_string(), config) + .await + } + + async fn delete( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result<()> { + let _operation = self.operations.lock().await; + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Manage) + .await?; + match self.driver.status(&branch.to_string()).await { + Ok(status) if !matches!(status.state, SandboxState::Stopped | SandboxState::Error) => { + return Err(Error::new( + ErrorCode::Busy, + "请先停止 Sandbox,再删除系统磁盘", + )); + } + Err(error) if error.code != ErrorCode::NotFound => return Err(error), + _ => {} + } + if let Some(binding) = self + .runtime + .state + .mounts() + .await? + .into_iter() + .find(|binding| { + binding.branch == branch + && binding.state == MountState::Ready + && binding.access == AccessMode::ReadWrite + }) + { + let saved = self + .services + .mounts + .save( + RequestContext { + principal: actor.clone(), + request_id: format!("sandbox-delete-{}", OperationId::new()), + }, + SaveMount { + message: Some("Save branch before deleting Sandbox".into()), + mount: binding.id, + expected_binding_generation: binding.generation, + durability: Durability::Local, + wait_for_remote_ms: None, + }, + ) + .await?; + if let Some(error) = saved.error { + return Err(error); + } + if !saved.local_saved { + return Err(Error::new( + ErrorCode::Busy, + "分支文件尚未保存,请稍后重试删除", + )); + } + } + self.driver.delete(&branch.to_string()).await?; + self.runtime.sandbox_pins.lock().remove(&branch); + Ok(()) + } + + async fn template(&self, actor: &Principal) -> Result> { + self.services.workspace.list(actor).await?; + self.driver.template().await + } + + async fn templates(&self, actor: &Principal) -> Result> { + self.services.workspace.list(actor).await?; + self.driver.templates().await + } + + async fn service( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + service: &str, + ) -> Result { + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Write) + .await?; + let status = self.driver.status(&branch.to_string()).await?; + if !matches!(status.state, SandboxState::Ready) + || !status.services.iter().any(|item| item.id == service) + { + return Err(Error::new(ErrorCode::Unavailable, "助手服务尚未就绪")); + } + let port = match service { + "csgclaw" => 7011, + _ => return Err(Error::invalid("未知的助手服务")), + }; + self.driver.connect(&branch.to_string(), port).await + } + + async fn mcp_config( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result { + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Write) + .await?; + self.driver.mcp_config(&branch.to_string()).await + } + + async fn desktop_asset( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + path: &str, + ) -> Result { + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Write) + .await?; + self.driver.desktop_asset(path).await + } + + async fn desktop( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result { + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Write) + .await?; + self.driver.connect(&branch.to_string(), 6080).await + } + + async fn create( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result { + self.create_with_template(actor, workspace, branch, None) + .await + } + + async fn create_with_template( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + template_id: Option, + ) -> Result { + let _operation = self.operations.lock().await; + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Write) + .await?; + let result = self + .create_inner(actor, workspace, branch, template_id) + .await; + if let Err(error) = &result + && let Ok(mut status) = self.driver.status(&branch.to_string()).await + { + if !matches!( + status.state, + SandboxState::Creating | SandboxState::Starting + ) { + return result; + } + status.state = SandboxState::Error; + status.message = Some(error.message.clone()); + self.driver.report_progress(status).await; + } + result + } + async fn status( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result> { + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Read) + .await?; + self.reconcile_exited().await?; + match self.driver.status(&branch.to_string()).await { + Ok(status) => Ok(Some(status)), + Err(error) if error.code == ErrorCode::NotFound => Ok(None), + Err(error) => Err(error), + } + } + async fn stop( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result { + self.stop_inner(actor, workspace, branch, false).await + } + async fn force_stop( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result { + self.stop_inner(actor, workspace, branch, true).await + } +} + +impl SandboxService { + async fn stop_inner( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + force: bool, + ) -> Result { + let _operation = self.operations.lock().await; + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Write) + .await?; + if force { + self.driver.force_stop(&branch.to_string()).await?; + } else { + self.driver.stop(&branch.to_string()).await?; + } + self.runtime.sandbox_pins.lock().remove(&branch); + if let Some(binding) = self.runtime.state.mounts().await?.into_iter().find(|m| { + m.branch == branch && m.state == MountState::Ready && m.access == AccessMode::ReadWrite + }) { + let saved = self + .services + .mounts + .save( + RequestContext { + principal: actor.clone(), + request_id: format!("sandbox-stop-{}", OperationId::new()), + }, + SaveMount { + message: Some("Save Sandbox files".into()), + mount: binding.id, + expected_binding_generation: binding.generation, + durability: Durability::Local, + wait_for_remote_ms: None, + }, + ) + .await?; + if let Some(error) = saved.error { + return Err(error); + } + if !saved.local_saved { + return Err(Error::new( + ErrorCode::Busy, + "Sandbox 已停止,但分支保存尚未完成,请稍后重试保存", + )); + } + } + self.driver.status(&branch.to_string()).await + } +} + +struct PendingPin { + runtime: Arc, + branch: BranchId, + retained: bool, +} +impl Drop for PendingPin { + fn drop(&mut self) { + if !self.retained { + self.runtime.sandbox_pins.lock().remove(&self.branch); + } + } +} + +impl SandboxService { + async fn create_inner( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + template_id: Option, + ) -> Result { + self.runtime + .authorize(actor, workspace, Some(branch), Permission::Write) + .await?; + if let Some(id) = &template_id + && !self + .driver + .templates() + .await? + .iter() + .any(|item| &item.id == id) + { + return Err(Error::invalid("模板不存在")); + } + let id = branch.to_string(); + let existing_template = self + .driver + .status(&id) + .await + .map(|s| s.template_id) + .unwrap_or_else(|_| default_sandbox_template()); + if let Ok(existing) = self.driver.status(&id).await + && template_id + .as_ref() + .is_some_and(|id| id != &existing.template_id) + { + return Err(Error::new( + ErrorCode::Busy, + "已有 Sandbox 使用其他模板,请删除后重新创建", + )); + } + match self.driver.status(&id).await { + Ok(status) + if matches!( + status.state, + SandboxState::Ready | SandboxState::Starting | SandboxState::Creating + ) => + { + return Ok(status); + } + Err(error) if error.code != ErrorCode::NotFound => return Err(error), + _ => {} + } + self.driver + .report_progress(SandboxStatus { + template_id: template_id + .clone() + .unwrap_or_else(|| existing_template.clone()), + services: Vec::new(), + id: id.clone(), + workspace, + branch, + state: SandboxState::Creating, + desktop_url: None, + message: None, + startup_step: Some("prepare_branch".into()), + }) + .await; + let value = self.runtime.branch(branch).await?; + if value.owner != self.runtime.location || value.state != BranchState::Writable { + return Err(Error::new( + ErrorCode::BranchBusy, + "Sandbox requires local branch write authority", + )); + } + let binding = self.runtime.state.mounts().await?.into_iter().find(|m| { + m.branch == branch + && m.access == AccessMode::ReadWrite + && m.state != MountState::Released + }); + if binding.is_none() { + self.services + .mounts + .prepare( + RequestContext { + principal: actor.clone(), + request_id: format!("sandbox-mount-{id}-{}", OperationId::new()), + }, + PrepareMount { + automatic: false, + workspace, + branch: Some(branch), + revision: None, + path: self.mount_root.join(&id), + access: AccessMode::ReadWrite, + durability: Durability::Local, + }, + ) + .await?; + } + let _lifecycle = self.runtime.lifecycle.lock().await; + let binding = self + .runtime + .state + .mounts() + .await? + .into_iter() + .find(|m| { + m.branch == branch + && m.access == AccessMode::ReadWrite + && m.state == MountState::Ready + }) + .ok_or_else(|| Error::new(ErrorCode::MountBusy, "Branch mount is not ready"))?; + { + let _branch = self.runtime.lock_branch(branch).await; + let current = self.runtime.branch(branch).await?; + current.check_write(self.runtime.location, value.authority_epoch)?; + if binding + .branch_generation + .is_some_and(|generation| generation != current.generation) + { + return Err(Error::new( + ErrorCode::StaleBinding, + "Branch binding changed before Sandbox startup", + )); + } + self.runtime.sandbox_pins.lock().insert(branch); + } + let mut pin = PendingPin { + runtime: self.runtime.clone(), + branch, + retained: false, + }; + match self + .driver + .start(SandboxLaunch { + id, + binding, + template_id, + }) + .await + { + Ok(status) => { + pin.retained = true; + Ok(status) + } + Err(error) => { + self.runtime.sandbox_pins.lock().remove(&branch); + Err(error) + } + } + } +} diff --git a/crates/agentdrive-mcp/Cargo.toml b/crates/agentdrive-mcp/Cargo.toml index 33287ab..6bc2dbb 100644 --- a/crates/agentdrive-mcp/Cargo.toml +++ b/crates/agentdrive-mcp/Cargo.toml @@ -6,6 +6,8 @@ rust-version.workspace = true license.workspace = true [dependencies] +hyper = { version = "1", features = ["client", "http1"] } +hyper-util = { version = "0.1", features = ["tokio"] } agentdrive-config.workspace = true rmcp.workspace = true axum.workspace = true @@ -22,6 +24,7 @@ tokio-util.workspace = true workspace = true [dev-dependencies] +async-trait.workspace = true agentdrive-engine.workspace = true agentdrive-local.workspace = true agentdrive-test-support.workspace = true diff --git a/crates/agentdrive-mcp/dashboard/dashboard.js b/crates/agentdrive-mcp/dashboard/dashboard.js index f47f04d..f50c496 100644 --- a/crates/agentdrive-mcp/dashboard/dashboard.js +++ b/crates/agentdrive-mcp/dashboard/dashboard.js @@ -1,3 +1,4 @@ +import { createSandboxesPage } from './sandboxes.js'; import { createWorkspaceActions } from './workspace-actions.js'; import { createNavigation } from './navigation.js'; import { createManagementPage } from './management.js'; @@ -124,6 +125,14 @@ async function refresh(force=false) { $('error').hidden = !catalogError; if (catalogError) $('error').textContent = `远端工作区发现失败,当前显示本机记录:${catalogError.message}`; if (isSettings()) return; + if (location.pathname === '/sandboxes') { + $('workspace-name').textContent = '所有工作区'; + await sandboxesPage.load(); + if (request !== generation) return; + $('updated').textContent = `${new Date().toLocaleTimeString('zh-CN', {hour12:false})} 已刷新`; + $('connection').textContent = '已连接本地服务'; + return; + } if (currentWorkspace !== workspace) { clearWorkspace(); currentWorkspace = workspace; } $('empty').hidden = workspaces.length > 0; $('panels').hidden = !workspaces.length || isMerges(); $('merges-panel').hidden = !workspaces.length || !isMerges(); if (!workspaces.length) { snapshot = null; historyList.clear(); revisionViewer.clear(); branchFileViewer.clear(); relationshipsView.clear(); mountsPage.show(null); branchesPage.show(null); archivePage.show(null); mergesView.clear(); $('workspace-name').textContent='暂无工作区'; ['branch-count','mount-count','mounted-branch-count'].forEach(id => $(id).textContent='0'); $('mount-detail').textContent='暂无挂载点'; $('sync-status').textContent='—'; $('sync-detail').textContent='暂无工作区'; } @@ -159,6 +168,7 @@ const workspaceActions = createWorkspaceActions({api, element, changed: async id showPage(); }}); const navigation = createNavigation({element, $, navigate:showPage, deleteWorkspace:workspaceActions.show}); +const sandboxesPage = createSandboxesPage({api, element, container:$('sandboxes-page')}); const settingsPage = createSettingsPage({api, element, $}); const revisionViewer = createRevisionViewer({api, element, onMerge:openMergeRevision}); const historyList = createHistoryList({api, element, list:$('revision-list'), count:$('history-count'), viewer:revisionViewer}); @@ -209,17 +219,18 @@ function clearWorkspace() { function showPage() { clearTimeout(remoteTimer); generation++; loading = false; snapshot = null; $('refresh').disabled = false; $('error').hidden = true; - clearWorkspace(); + clearWorkspace(); sandboxesPage.clear(); selectedBranch = new URLSearchParams(location.search).get('branch'); - const settings = isSettings(), archive = isArchive(), mounts = location.pathname === '/mounts', branches = location.pathname === '/branches'; + const settings = isSettings(), archive = isArchive(), mounts = location.pathname === '/mounts', branches = location.pathname === '/branches', sandboxes = location.pathname === '/sandboxes'; $('archive-page').hidden = !archive; - $('history-page').hidden = settings || archive || mounts || branches; + $('history-page').hidden = settings || archive || mounts || branches || sandboxes; $('mounts-page').hidden = !mounts; $('branches-page').hidden = !branches; $('settings-page').hidden = !settings; + $('sandboxes-page').hidden = !sandboxes; $('history-breadcrumb').hidden = settings; $('settings-breadcrumb').hidden = !settings; navigation.page(); - $('view-mode').textContent = settings ? '全局配置' : mounts || branches ? '工作区管理' : '只读浏览'; + $('view-mode').textContent = sandboxes ? '所有工作区' : settings ? '全局配置' : mounts || branches ? '工作区管理' : '只读浏览'; showHistoryTab(); refreshPage(); } diff --git a/crates/agentdrive-mcp/dashboard/index.html b/crates/agentdrive-mcp/dashboard/index.html index 36007a8..cfd9f39 100644 --- a/crates/agentdrive-mcp/dashboard/index.html +++ b/crates/agentdrive-mcp/dashboard/index.html @@ -8,6 +8,7 @@ + @@ -25,7 +26,7 @@
只读浏览正在连接
@@ -33,6 +34,7 @@ +
WORKSPACE HISTORY

分支与版本.

查看本机与其他节点已同步的分支、来源版本和本机挂载状态。

本地存储
diff --git a/crates/agentdrive-mcp/dashboard/management.css b/crates/agentdrive-mcp/dashboard/management.css index 7ce7ee7..bd5d572 100644 --- a/crates/agentdrive-mcp/dashboard/management.css +++ b/crates/agentdrive-mcp/dashboard/management.css @@ -57,3 +57,64 @@ .command-panel { padding:18px 16px; } .collaboration-mount,.collaboration-description,.collaboration-panel > .command-error,.collaboration-state,.collaboration-help,.collaboration-settings-link { margin-left:16px; margin-right:16px; } } +.sandbox-controls { display:inline-flex; } +.sandbox-trigger { display:inline-flex; align-items:center; gap:8px; } +.sandbox-dot { width:7px; height:7px; border-radius:50%; background:#94a3b8; } +.sandbox-dot[data-state="ready"] { background:#16856b; } +.sandbox-dot[data-state="creating"],.sandbox-dot[data-state="starting"],.sandbox-dot[data-state="stopping"] { background:#3b82f6; } +.sandbox-dot[data-state="error"] { background:#c34242; } +.sandbox-dialog { width:min(640px,calc(100% - 32px)); } +.sandbox-heading { align-items:flex-start; } +.sandbox-eyebrow { color:var(--muted); font-size:10px; letter-spacing:1.5px; margin-bottom:8px; } +.sandbox-heading h2 { font-size:23px; } +.sandbox-identity { margin:8px 0 0; color:var(--muted); font-size:13px; overflow-wrap:anywhere; } +.sandbox-body { padding:24px; } +.sandbox-overview { padding:20px; background:#f6f8fb; border:1px solid var(--line); border-radius:12px; } +.sandbox-badge { display:inline-flex; align-items:center; font-size:12px; font-weight:600; padding:4px 9px; border-radius:6px; background:#e8edf3; color:#546176; } +.sandbox-badge[data-state="ready"] { background:#e0f3eb; color:#127357; } +.sandbox-badge[data-state="starting"],.sandbox-badge[data-state="creating"] { background:#e4edff; color:#255fbb; } +.sandbox-badge[data-state="error"] { background:#fbe9e9; color:#b03939; } +.sandbox-status { margin:12px 0 18px; font-size:13px; line-height:1.8; color:var(--muted); overflow-wrap:anywhere; } +.sandbox-actions,.sandbox-config-actions { display:flex; flex-wrap:wrap; align-items:center; gap:10px; } +.sandbox-actions button,.sandbox-desktop { font-size:13px; padding:10px 16px; border-radius:7px; } +.sandbox-desktop { display:inline-flex; text-decoration:none; align-items:center; justify-content:center; } +.sandbox-stop { color:#9b443e; } +.sandbox-delete-dialog { width:min(500px,calc(100% - 32px)); padding:28px; } +.sandbox-delete-dialog p { line-height:1.7; overflow-wrap:anywhere; } +.sandbox-delete-dialog label { display:grid; gap:10px; margin-top:20px; } +.sandbox-delete-dialog input { width:100%; box-sizing:border-box; padding:10px; border:1px solid #d5dfdc; border-radius:6px; } +.sandbox-delete-dialog .sandbox-actions { justify-content:flex-end; margin-top:20px; } +.sandbox-delete-confirm { background:#a13d36; } +.sandbox-section { border-bottom:1px solid var(--line); } +.sandbox-section > summary { cursor:pointer; padding:20px 0; font-size:14px; font-weight:600; } +.sandbox-section-meta { float:right; margin-right:4px; font-size:12px; font-weight:400; color:var(--muted); } +.sandbox-steps { list-style:none; padding:0 0 16px; margin:0; } +.sandbox-step { display:flex; gap:12px; align-items:center; padding:9px 0; color:var(--muted); font-size:12px; } +.sandbox-step-marker { width:24px; height:24px; border:1px solid #dbe1e9; border-radius:50%; display:inline-flex; align-items:center; justify-content:center; flex:none; font-size:11px; } +.sandbox-step-state { margin-left:auto; padding-left:8px; white-space:nowrap; font-size:11px; } +.sandbox-step.active { color:#2563eb; } +.sandbox-step.active .sandbox-step-marker { border-color:#93b4ed; background:#eef4ff; } +.sandbox-step.complete .sandbox-step-marker { background:#eaf6f0; color:#16856b; border-color:#c9e7da; } +.sandbox-step.error,.sandbox-error { color:#b91c1c; } +.sandbox-section-body { padding-bottom:20px; } +.sandbox-section-body p,.sandbox-guide p { margin:0 0 14px; font-size:12px; color:var(--muted); line-height:1.8; } +.sandbox-config { width:100%; box-sizing:border-box; resize:vertical; min-height:160px; max-height:360px; padding:14px; margin-bottom:12px; border:1px solid var(--line); border-radius:8px; background:#f6f8fb; color:var(--ink); font:11px/1.7 ui-monospace,SFMono-Regular,monospace; } +.sandbox-config-actions button { font-size:12px; padding:8px 12px; } +.sandbox-section-body .sandbox-feedback { margin:10px 0; color:var(--accent); } +.sandbox-feedback:empty { display:none; } +.sandbox-section-body .sandbox-note { margin:14px 0 0; font-size:11px; } +.sandbox-guide { display:grid; grid-template-columns:1fr 1fr; gap:24px; margin-top:22px; } +.sandbox-guide h3 { margin:0 0 8px; font-size:12px; font-weight:600; } +.sandbox-guide p { margin:0; font-size:11px; } +.sandbox-dialog [hidden] { display:none !important; } +@media(max-width:600px) { + .sandbox-body { padding:16px; } + .sandbox-overview { padding:16px; } + .sandbox-guide { grid-template-columns:1fr; gap:18px; } + .sandbox-section-meta { font-size:11px; } +} +.sandbox-heading { position:sticky; top:0; z-index:1; background:var(--paper); } +.sandbox-actions .primary { width:auto; margin:0; } +.sandbox-guide p,.sandbox-step { color:#697586; } +.sandbox-step.active { color:#2563eb; } +.sandbox-step.error { color:#b91c1c; } diff --git a/crates/agentdrive-mcp/dashboard/management.js b/crates/agentdrive-mcp/dashboard/management.js index 3cc60fe..9408932 100644 --- a/crates/agentdrive-mcp/dashboard/management.js +++ b/crates/agentdrive-mcp/dashboard/management.js @@ -1,3 +1,4 @@ +import { createSandboxControl } from './sandbox.js'; import { createCommandPanel } from './commands.js'; import { createRevisionSource } from './history.js'; import { createCollaborationPanel } from './collaboration.js'; @@ -41,6 +42,7 @@ export function createManagementPage({api, element, container, kind, changed, on content.append(operationHost, formPanel); if (!mounts) content.append(listing); container.append(heading, empty, content); const collaboration = mounts && createCollaborationPanel({api, element, container:content}); let snapshot = null, project = null, busy = false; + const sandboxControls = new Map(); const source = !mounts && createRevisionSource({api, element, select:revisionInput, form, changed:updateBusy}); const runner = createCommandPanel({api, element, container:operationHost, scope:kind, changed, setBusy:value => { busy = value; updateBusy(); onBusy(value); }}); @@ -106,8 +108,14 @@ export function createManagementPage({api, element, container, kind, changed, on link(buttons, '挂载到目录', '/mounts', item.id); } link(buttons, '版本历史', '/dashboard', item.id); + const sandboxKey = `${project}/${item.id}`; + if (!sandboxControls.has(sandboxKey)) sandboxControls.set(sandboxKey, createSandboxControl({api, element, workspace:project, branch:item.id, branchName:item.name, workspaceName:snapshot.status.workspace.name})); + const control = sandboxControls.get(sandboxKey); buttons.append(control); control.setBranchName(item.name, snapshot.status.workspace.name); control.refresh(); row.append(copy, buttons); rows.append(row); } + for (const [key, control] of sandboxControls) { + if (!control.isConnected) { control.dispose(); sandboxControls.delete(key); } + } if (focusId) document.getElementById(focusId)?.focus({preventScroll:true}); } function show(data) { @@ -121,7 +129,7 @@ export function createManagementPage({api, element, container, kind, changed, on hint.textContent = mounts ? '目录位于当前 AgentDrive 所在机器。一个分支在本机只能有一个读写挂载点。' : '选择准确的来源内容;创建完成后,可以切换使用或挂载到本机目录。'; sourceOptions(); if (!mounts) renderRows(); runner.show(project); updateBusy(); if (collaboration) collaboration.show(data); } - function clear() { snapshot = null; project = null; rows.replaceChildren(); form.reset(); if (source) source.clear(); if (collaboration) collaboration.clear(); runner.clear(); content.hidden = true; } + function clear() { for (const control of sandboxControls.values()) control.dispose(); sandboxControls.clear(); snapshot = null; project = null; rows.replaceChildren(); form.reset(); if (source) source.clear(); if (collaboration) collaboration.clear(); runner.clear(); content.hidden = true; } form.onsubmit = event => { event.preventDefault(); if (busy || !snapshot) return; const textInput = mounts ? inputs.path : inputs.name; diff --git a/crates/agentdrive-mcp/dashboard/navigation.js b/crates/agentdrive-mcp/dashboard/navigation.js index 1ed17a3..4758a3f 100644 --- a/crates/agentdrive-mcp/dashboard/navigation.js +++ b/crates/agentdrive-mcp/dashboard/navigation.js @@ -4,6 +4,7 @@ export function createNavigation({element, $, navigate, deleteWorkspace}) { ['history-nav', '/dashboard', '分支与版本'], ['mounts-nav', '/mounts', '挂载点管理'], ['branches-nav', '/branches', '分支管理'], + ['sandboxes-nav', '/sandboxes', '沙盒'], ]; let workspaces = [], selected = null, listKey = ''; const compact = window.matchMedia('(max-width: 900px)'); @@ -52,9 +53,9 @@ export function createNavigation({element, $, navigate, deleteWorkspace}) { if (link) follow(event, new URL(link.href)); }); for (const [id, path] of pages) $(id).addEventListener('click', event => { - if (!selected) { event.preventDefault(); return; } + if (!selected && path !== '/sandboxes') { event.preventDefault(); return; } const url = new URL(path, location.origin); - url.searchParams.set('workspace', selected.id); + if (selected) url.searchParams.set('workspace', selected.id); const branch = new URLSearchParams(location.search).get('branch'); if (branch) url.searchParams.set('branch', branch); follow(event, url); @@ -118,8 +119,8 @@ export function createNavigation({element, $, navigate, deleteWorkspace}) { $('workspace-menu-name').textContent = selected?.name || '暂无工作区'; $('workspace-name').textContent = selected?.name || '暂无工作区'; for (const [id,path] of pages) { - $(id).setAttribute('aria-disabled', String(!selected)); - $(id).tabIndex = selected ? 0 : -1; + $(id).setAttribute('aria-disabled', String(!selected && path !== '/sandboxes')); + $(id).tabIndex = selected || path === '/sandboxes' ? 0 : -1; const url = new URL(path, location.origin); if (selected) url.searchParams.set('workspace', selected.id); $(id).href = url.pathname + url.search; diff --git a/crates/agentdrive-mcp/dashboard/sandbox.js b/crates/agentdrive-mcp/dashboard/sandbox.js new file mode 100644 index 0000000..1965e24 --- /dev/null +++ b/crates/agentdrive-mcp/dashboard/sandbox.js @@ -0,0 +1,306 @@ +const startupSteps = [ + ['prepare_branch', '准备当前分支目录'], + ['prepare_disk', '准备 Ubuntu 系统磁盘'], + ['start_vm', '启动 Ubuntu 实例'], + ['wait_desktop', '等待桌面启动'], +]; + +export function createSandboxControl({api, element, workspace, branch, branchName = branch, workspaceName = workspace, inline = false, initial, templates = [], canStart = true, onDeleted = () => {}}) { + const root = element('div', 'sandbox-controls'); + const trigger = element('button', 'secondary sandbox-trigger'); trigger.type = 'button'; + trigger.id = `sandbox-trigger-${workspace}-${branch}`; + trigger.setAttribute('aria-haspopup', 'dialog'); trigger.setAttribute('aria-label', 'Sandbox'); + const dot = element('span', 'sandbox-dot'); dot.setAttribute('aria-hidden', 'true'); + trigger.append(dot, element('span', '', 'Sandbox')); root.append(trigger); + const dialog = element('dialog', 'revision-dialog sandbox-dialog'); + const titleId = `sandbox-title-${workspace}-${branch}`; + dialog.setAttribute('aria-labelledby', titleId); + const header = element('div', 'revision-dialog-heading sandbox-heading'); + const heading = element('div'); + heading.append(element('div', 'sandbox-eyebrow', 'UBUNTU GNOME')); + const title = element('h2', '', 'Sandbox'); title.id = titleId; + const identity = element('p', 'sandbox-identity', branchName); identity.title = branch; + heading.append(title, identity); + const close = element('button', 'revision-dialog-close', '×'); close.type = 'button'; close.setAttribute('aria-label', '关闭 Sandbox'); + header.append(heading, close); + const body = element('div', 'sandbox-body'); + const overview = element('section', 'sandbox-overview'); + const overviewText = element('div'); + const badge = element('span', 'sandbox-badge', '读取状态中'); + const status = element('p', 'sandbox-status'); status.setAttribute('role', 'status'); status.setAttribute('aria-live', 'polite'); + overviewText.append(badge, status); + const actions = element('div', 'sandbox-actions'); + const create = element('button', 'primary', '启动 Sandbox'); create.type = 'button'; create.disabled = true; + const stop = element('button', 'secondary sandbox-stop', '停止 Sandbox'); stop.type = 'button'; stop.hidden = true; + const force = element('button', 'secondary sandbox-stop', '强制停止'); force.type = 'button'; force.hidden = true; + const desktop = element('a', 'primary sandbox-desktop', '打开桌面'); desktop.hidden = true; + desktop.target = '_blank'; desktop.rel = 'noopener'; + const remove = element('button', 'secondary sandbox-stop', '删除 Sandbox'); remove.type = 'button'; remove.hidden = true; + const serviceActions = element("div", "sandbox-service-actions"); + actions.append(create, desktop, stop, force, remove, serviceActions); overview.append(overviewText, actions); + const quickActions = inline ? element('div', 'sandbox-actions') : null; + const quickCreate = create.cloneNode(true), quickStop = stop.cloneNode(true), quickForce = force.cloneNode(true), quickDesktop = desktop.cloneNode(true), quickRemove = remove.cloneNode(true); + if (quickActions) { + trigger.querySelector('span:last-child').textContent = '详情与 MCP'; + trigger.setAttribute('aria-label', '详情与 MCP'); + quickActions.append(quickCreate, quickDesktop, quickStop, quickForce, quickRemove, trigger); root.append(quickActions); + } + const templateChoice = element('label', 'management-field sandbox-template-choice'); + const templateSelect = element('select'); templateSelect.setAttribute('aria-label', 'Sandbox 模板'); + templateChoice.append(element('span', '', '创建模板'), templateSelect); + function setTemplates(values) { + const selected = templateSelect.value; + templates = values; + templateSelect.replaceChildren(...values.map(value => { const option = element('option', '', value.manifest?.name || value.id); option.value = value.id; return option; })); + if (values.some(value => value.id === selected)) templateSelect.value = selected; + else if (values.some(value => value.id === 'ubuntu-csgclaw')) templateSelect.value = 'ubuntu-csgclaw'; + templateChoice.hidden = !!current || values.length < 2; + } + const proxy = element('details', 'sandbox-section'); proxy.hidden = true; + proxy.append(element('summary', '', '代理配置')); + const proxyBody = element('form', 'sandbox-section-body sandbox-proxy-form'); + const proxyLabel = element('label', 'management-field'); + const proxyUrl = element('input'); proxyUrl.type = 'url'; proxyUrl.disabled = true; proxyUrl.placeholder = 'http://127.0.0.1:7890'; proxyUrl.setAttribute('aria-label', 'HTTP 代理地址'); + proxyLabel.append(element('span', '', 'HTTP 代理地址'), proxyUrl); + const saveProxy = element('button', 'secondary', '保存代理配置'); saveProxy.type = 'submit'; saveProxy.disabled = true; + const proxyFeedback = element('p', 'sandbox-feedback'); proxyFeedback.setAttribute('role', 'status'); + proxyBody.append(element('p', '', '支持 HTTP 代理及 HTTPS CONNECT。地址从宿主机访问,留空表示直接联网。保存后,下次启动沙盒时应用到助手和 Chromium。'), proxyLabel, saveProxy, proxyFeedback); + proxy.append(proxyBody); + let proxyLoaded = false; + proxy.ontoggle = async () => { + if (!proxy.open || proxyLoaded) return; + saveProxy.disabled = true; + try { const config = await api(`${endpoint}/proxy`); proxyUrl.value = config.url || ''; proxyLoaded = true; } + catch (error) { proxyFeedback.textContent = error.message; } + finally { saveProxy.disabled = !proxyLoaded; proxyUrl.disabled = !proxyLoaded; } + }; + proxyBody.onsubmit = async event => { + event.preventDefault(); saveProxy.disabled = true; + try { + await api(`${endpoint}/proxy`, {method:'PUT', headers:{'Content-Type':'application/json'}, body:JSON.stringify({url:proxyUrl.value.trim() || null})}); + proxyFeedback.textContent = '已保存,下次启动沙盒时生效。'; + } catch (error) { proxyFeedback.textContent = error.message; } + finally { saveProxy.disabled = false; } + }; + const progress = element('details', 'sandbox-section'); + const progressTitle = element('summary'); + const progressCount = element('span', 'sandbox-section-meta'); + progressTitle.append(element('span', '', '启动步骤'), progressCount); + const steps = element('ol', 'sandbox-steps'); steps.setAttribute('aria-label', 'Sandbox 启动步骤'); + progress.append(progressTitle, steps); + const mcp = element('details', 'sandbox-section sandbox-mcp'); + const mcpTitle = element('summary'); mcpTitle.append(element('span', '', 'MCP 配置'), element('span', 'sandbox-section-meta', '连接你的 Agent')); + const mcpBody = element('div', 'sandbox-section-body'); + mcpBody.append(element('p', '', '在宿主机的 Agent 客户端中添加这一份配置,即可同时使用 Playwright 浏览器工具和 computer-use-linux 桌面工具。两者操作 VNC 中的同一个 Chromium,无需额外添加 MCP 配置。')); + const config = element('textarea', 'sandbox-config'); config.readOnly = true; config.spellcheck = false; config.rows = 10; + config.setAttribute('aria-label', 'Sandbox MCP 配置'); config.hidden = true; + const configActions = element('div', 'sandbox-config-actions'); + const copy = element('button', 'secondary', '复制配置'); copy.type = 'button'; copy.disabled = true; + const download = element('button', 'secondary', '下载 JSON'); download.type = 'button'; download.disabled = true; + const retry = element('button', 'secondary', '重新获取'); retry.type = 'button'; retry.hidden = true; + const feedback = element('p', 'sandbox-feedback'); feedback.setAttribute('aria-live', 'polite'); + configActions.append(copy, download, retry); + mcpBody.append(config, configActions, feedback, element('p', 'sandbox-note', '请保持 Sandbox 运行。实例重新启动后,在 Agent 客户端中重新连接 MCP。配置仅适用于运行 AgentDrive 的同一台主机。')); + mcp.append(mcpTitle, mcpBody); + const guide = element('section', 'sandbox-guide'); + function help(title, description) { const item = element('div'); item.append(element('h3', '', title), element('p', '', description)); guide.append(item); } + help('当前分支文件', '分支自动挂载到 /workspace,桌面的 AgentDrive 文件夹指向此目录。保存到这里的文件会写入当前分支。'); + help('浏览器与下载', '网页、标签页、按钮和表单使用 browser_* 工具;终端和系统窗口使用桌面工具。Chromium 下载保存到 /workspace/Downloads,文件自动写入当前分支。模板带有 CSGClaw 时,可以点击「打开助手」直接聊天;外部 Agent 也可以使用统一 MCP。'); + body.append(overview, templateChoice, proxy, progress, mcp, guide); dialog.append(header, body); + const endpoint = `/api/workspaces/${encodeURIComponent(workspace)}/branches/${encodeURIComponent(branch)}/sandbox`; + const deleteDialog = element('dialog', 'revision-dialog sandbox-delete-dialog'); + const deleteTitle = element('h2', '', '删除 Sandbox'); deleteTitle.id = `${titleId}-delete`; + deleteDialog.setAttribute('aria-labelledby', deleteTitle.id); + const deleteIdentity = element('p', 'sandbox-identity'); + const confirmationLabel = element('label'); + const confirmationText = element('span'); + const confirmation = element('input'); confirmation.autocomplete = 'off'; confirmation.setAttribute('aria-label', '输入分支名称确认删除'); + confirmationLabel.append(confirmationText, confirmation); + const deleteError = element('p', 'sandbox-error'); deleteError.setAttribute('role', 'alert'); + const deleteActions = element('div', 'sandbox-actions'); + const cancelDelete = element('button', 'secondary', '取消'); cancelDelete.type = 'button'; + const confirmDelete = element('button', 'primary sandbox-delete-confirm', '确认删除'); confirmDelete.type = 'button'; + deleteActions.append(cancelDelete, confirmDelete); + const deleteDescription = element('p'); + const deletePreserved = element('p'); + deleteDialog.append(deleteTitle, deleteIdentity, + deleteDescription, deletePreserved, + confirmationLabel, deleteError, deleteActions); + let busy = false, deleting = false, reading = false, disposed = false, timer = null, current = null, enabled = true, epoch = 0, configLoading = false; + let confirmationAction = 'delete'; + function updateConfirmation() { confirmDelete.disabled = busy || (confirmationAction === 'delete' ? remove.disabled : force.disabled) || confirmation.value !== branchName; } + function schedule() { + clearTimeout(timer); + if (!disposed && (dialog.open || busy || ['creating', 'starting', 'stopping'].includes(current?.state))) { + timer = setTimeout(refresh, 1500); + } + } + function show(value, available = enabled) { + const previous = current?.state; + current = value; enabled = available; + const state = value?.state; + templateChoice.hidden = !!value || templates.length < 2; + proxy.hidden = !(value?.services || []).some(service => service.id === 'csgclaw'); + serviceActions.replaceChildren(); + if (state === 'ready') for (const service of value.services || []) { + if (service.id !== 'csgclaw') continue; + const link = element('a', 'secondary sandbox-service-link', service.label); + const url = new URL(location.href); url.hostname = `${service.id}.${workspace}.${branch}.localhost`; url.pathname = '/'; url.search = ''; url.hash = '/new?agent=agent-agentdrive'; + link.href = url.href; link.target = '_blank'; link.rel = 'noopener'; serviceActions.append(link); + } + if (quickActions) { + quickActions.querySelector('.sandbox-service-actions')?.remove(); + quickActions.prepend(serviceActions.cloneNode(true)); + } + const active = ['creating', 'starting', 'stopping'].includes(state); + create.disabled = !enabled || !canStart || busy || active; + create.title = canStart ? '' : '当前分支尚未取得本机写入权限'; + create.hidden = ['creating', 'starting', 'ready', 'stopping'].includes(state); + create.textContent = state === 'error' ? '重试启动' : '启动 Sandbox'; + stop.hidden = state !== 'ready'; stop.disabled = busy; + force.hidden = !['ready', 'error'].includes(state); force.disabled = busy || !enabled || force.hidden; + remove.hidden = !value; remove.disabled = !enabled || busy || !['stopped', 'error'].includes(state); + remove.title = ['stopped', 'error'].includes(state) ? '删除系统磁盘,保留分支文件' : '请先停止 Sandbox'; + updateConfirmation(); + desktop.hidden = state !== 'ready' || !value?.desktop_url; + let invalidDesktop = false; + if (!desktop.hidden) { + try { + const url = new URL(value.desktop_url, location.origin); + if (url.origin !== location.origin) throw new Error(); + desktop.href = url.href; + } catch { desktop.hidden = true; invalidDesktop = true; } + } + const labels = {creating:'正在准备',starting:'启动中',ready:'运行中',stopping:'停止中',stopped:'已停止',error:'启动失败'}; + const label = !enabled ? '未配置' : labels[state] || '未启动'; + badge.textContent = label; badge.dataset.state = state || 'idle'; dot.dataset.state = state || 'idle'; + trigger.title = `Sandbox · ${label}`; + status.textContent = !enabled ? 'Sandbox 运行环境尚未配置' : invalidDesktop ? '桌面地址无效' : value?.message || ({ready:'Ubuntu 桌面已就绪,可以打开桌面或连接 Agent。',stopped:'系统磁盘和分支文件已保留,可以继续启动。',stopping:'正在保存分支文件并停止 Ubuntu。',creating:'正在准备分支目录和 Ubuntu 环境。',starting:'启动完成后即可打开桌面。'}[state]) || '启动独立 Ubuntu 桌面,操作当前分支文件。'; + status.classList.toggle('sandbox-error', state === 'error' || invalidDesktop); + const index = state === 'ready' ? startupSteps.length : ['stopped', 'stopping'].includes(state) ? -1 : startupSteps.findIndex(([id]) => id === value?.startup_step); + progressCount.textContent = state === 'stopped' ? '下次启动时更新' : `${Math.max(index, 0)} / ${startupSteps.length}`; + if (previous !== state && ['creating', 'starting', 'error'].includes(state)) progress.open = true; + steps.replaceChildren(...startupSteps.map(([id, label], position) => { + const stepState = position < index ? 'complete' : position === index ? state === 'error' ? 'error' : 'active' : 'pending'; + const row = element('li', `sandbox-step ${stepState}`); row.dataset.step = id; row.dataset.state = stepState; + row.append(element('span', 'sandbox-step-marker', stepState === 'complete' ? '✓' : stepState === 'error' ? '!' : String(position + 1)), element('span', '', label), element('span', 'sandbox-step-state', {complete:'已完成',active:'进行中',error:'失败',pending:'等待'}[stepState])); + return row; + })); + if (quickActions) { + for (const [source, target] of [[create, quickCreate], [stop, quickStop], [force, quickForce], [desktop, quickDesktop], [remove, quickRemove]]) { + target.textContent = source.textContent; target.hidden = source.hidden; + if (source.tagName === 'BUTTON') { target.disabled = source.disabled; target.title = source.title; } + else if (!source.hidden) target.href = source.href; + } + } + schedule(); + } + async function refresh() { + if (reading || disposed || deleting) return; + reading = true; const version = epoch; + try { const result = await api(endpoint); if (!disposed && version === epoch) show(result.sandbox, result.enabled); } + catch (error) { if (!disposed && version === epoch) { status.textContent = error.message; badge.textContent = '状态获取失败'; create.disabled = true; quickCreate.disabled = true; } } + finally { reading = false; schedule(); } + } + async function run(method, templateId) { + if (busy || disposed) return; + busy = true; epoch++; + show(method === 'POST' ? {...current,state:'creating',startup_step:'prepare_branch'} : {...current,state:'stopping'}); + try { const result = await api(endpoint, {method, ...(templateId ? {headers:{"Content-Type":"application/json"}, body:JSON.stringify({template_id:templateId})} : {})}); busy = false; epoch++; if (!disposed) show(result); } + catch (error) { + busy = false; epoch++; + if (disposed) return; + if (method === 'DELETE') { + try { const result = await api(endpoint); if (!disposed) show({...result.sandbox,message:error.message}, result.enabled); return; } + catch { /* 状态无法读取时保留失败信息和强制停止入口。 */ } + } + if (!disposed) show({...current,state:'error',message:error.message}); + } + } + async function loadConfig() { + if (configLoading || disposed) return; + configLoading = true; feedback.textContent = '正在获取配置…'; retry.hidden = true; + try { + const result = await api(`${endpoint}/mcp-config`); + if (disposed) return; + config.value = JSON.stringify(result, null, 2); config.hidden = false; + copy.disabled = false; download.disabled = false; feedback.textContent = ''; + } catch (error) { if (!disposed) { feedback.textContent = error.message; retry.hidden = false; } } + finally { configLoading = false; } + } + function openConfirmation(action) { + if ((action === 'delete' ? remove.disabled : force.disabled) || busy) return; + confirmationAction = action; + const removing = action === 'delete'; + deleteTitle.textContent = removing ? '删除 Sandbox' : '强制停止 Sandbox'; + confirmDelete.textContent = removing ? '确认删除' : '确认强制停止'; + deleteDescription.textContent = removing ? '删除后无法恢复 Ubuntu 系统磁盘、已安装的软件、浏览器登录状态和实例日志。' : '强制停止会终止 Ubuntu 进程,尚未同步的修改可能丢失。正常停止失败时可以使用此操作。'; + deletePreserved.textContent = removing ? '当前分支、版本历史以及 /workspace 中的全部文件(包括 Downloads)会保留。之后可以使用当前模板重新创建 Sandbox。' : '系统磁盘和已写入当前分支的文件会保留,停止后可以重新启动。'; + deleteIdentity.textContent = `工作区:${workspaceName} · 分支:${branchName} (${branch})`; + confirmationText.textContent = `请输入分支名称 ${branchName} 确认${removing ? '删除' : '强制停止'}`; + confirmation.setAttribute('aria-label', removing ? '输入分支名称确认删除' : '输入分支名称确认强制停止'); + confirmation.value = ''; deleteError.textContent = ''; updateConfirmation(); + if (!deleteDialog.isConnected) document.body.append(deleteDialog); + deleteDialog.showModal(); confirmation.focus(); + } + remove.onclick = () => openConfirmation('delete'); + force.onclick = () => openConfirmation('force-stop'); + quickRemove.onclick = remove.onclick; + quickForce.onclick = force.onclick; + confirmation.oninput = updateConfirmation; + cancelDelete.onclick = () => deleteDialog.close(); + deleteDialog.addEventListener('cancel', event => { if (deleting) event.preventDefault(); }); + confirmDelete.onclick = async () => { + if (confirmDelete.disabled || busy) return; + busy = true; deleting = true; epoch++; show(current); + const removing = confirmationAction === 'delete'; + confirmation.disabled = true; cancelDelete.disabled = true; confirmDelete.textContent = removing ? '正在删除…' : '正在强制停止…'; deleteError.textContent = ''; + let result; + try { result = await api(`${endpoint}/${confirmationAction}`, {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({confirm_branch:branch})}); } + catch (error) { + busy = false; deleting = false; epoch++; + if (!disposed) { deleteError.textContent = error.message; confirmation.disabled = false; cancelDelete.disabled = false; confirmDelete.textContent = removing ? '确认删除' : '确认强制停止'; show(current); } + return; + } + busy = false; deleting = false; epoch++; + if (disposed) return; + confirmation.disabled = false; cancelDelete.disabled = false; confirmDelete.textContent = '确认删除'; + if (!removing) { show(result); deleteDialog.close(); return; } + config.value = ''; config.hidden = true; copy.disabled = true; download.disabled = true; mcp.open = false; + show(null); deleteDialog.close(); dialog.close(); onDeleted(); + }; + mcp.ontoggle = () => { if (mcp.open && !config.value) loadConfig(); }; + retry.onclick = loadConfig; + copy.onclick = async () => { + try { await navigator.clipboard.writeText(config.value); feedback.textContent = '配置已复制'; } + catch { config.focus(); config.select(); feedback.textContent = '请按 Ctrl+C 或 ⌘C 复制选中的配置。'; } + }; + download.onclick = () => { + const url = URL.createObjectURL(new Blob([config.value + '\n'], {type:'application/json'})); + const link = document.createElement('a'); link.href = url; link.download = `sandbox-mcp-${branch}.json`; link.click(); + setTimeout(() => URL.revokeObjectURL(url), 1000); + }; + function open() { + if (!dialog.isConnected) document.body.append(dialog); + if (!dialog.open) dialog.showModal(); + refresh(); + if (!current && !templates.length) api('/api/sandboxes').then(data => { if (!disposed && data.templates) setTemplates(data.templates); }).catch(() => {}); + } + trigger.onclick = open; + close.onclick = () => dialog.close(); + dialog.addEventListener('click', event => { + const bounds = dialog.getBoundingClientRect(); + if (event.target === dialog && (event.clientX < bounds.left || event.clientX > bounds.right || event.clientY < bounds.top || event.clientY > bounds.bottom)) dialog.close(); + }); + dialog.addEventListener('close', () => { if (!disposed && trigger.isConnected) trigger.focus(); schedule(); }); + create.onclick = () => run('POST', current ? undefined : templateSelect.value || undefined); stop.onclick = () => run('DELETE'); + quickCreate.onclick = create.onclick; quickStop.onclick = stop.onclick; + root.refresh = refresh; + root.start = templateId => { if (!create.disabled && !create.hidden) { open(); return run('POST', templateId); } }; + root.update = (value, available, allowed = canStart) => { canStart = allowed; if (!busy && !disposed) show(value, available); }; + root.setBranchName = (name, workspaceLabel = workspaceName) => { branchName = name; workspaceName = workspaceLabel; identity.textContent = `${workspaceName} / ${branchName}`; updateConfirmation(); }; + root.dispose = () => { disposed = true; clearTimeout(timer); dialog.close(); dialog.remove(); deleteDialog.close(); deleteDialog.remove(); }; + setTemplates(templates); + if (initial) show(initial.sandbox, initial.enabled); else refresh(); + return root; +} diff --git a/crates/agentdrive-mcp/dashboard/sandboxes.css b/crates/agentdrive-mcp/dashboard/sandboxes.css new file mode 100644 index 0000000..67a7cbd --- /dev/null +++ b/crates/agentdrive-mcp/dashboard/sandboxes.css @@ -0,0 +1,58 @@ +.sandboxes-page { max-width:1500px; margin:auto; } +.sandboxes-page .page-heading { align-items:center; gap:24px; } +.sandboxes-page .page-heading > button { flex-shrink:0; width:auto; padding:11px 18px; border-radius:7px; } +.sandbox-summary { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); border:1px solid var(--line); border-radius:10px; background:var(--paper); margin-bottom:28px; } +.sandbox-summary > div { padding:22px 24px; display:flex; flex-direction:column; gap:12px; } +.sandbox-summary > div + div { border-left:1px solid var(--line); } +.sandbox-summary span { font-size:12px; color:var(--muted); } +.sandbox-summary strong { font-size:28px; font-weight:600; } +.sandbox-page-tabs { display:flex; gap:28px; border-bottom:1px solid var(--line); margin-bottom:24px; } +.sandbox-page-tabs button { background:none; border:0; border-bottom:2px solid transparent; padding:0 2px 15px; color:var(--muted); font-size:14px; } +.sandbox-page-tabs button[aria-selected=true] { border-color:var(--accent); color:var(--accent); font-weight:600; } +.sandbox-filters { display:flex; flex-wrap:wrap; gap:12px; margin-bottom:20px; } +.sandbox-filters input,.sandbox-filters select { min-width:0; border:1px solid var(--line); background:var(--paper); border-radius:7px; padding:11px 13px; color:var(--ink); font:inherit; font-size:13px; } +.sandbox-filters input { flex:1; min-width:180px; } +.sandbox-catalog { display:grid; grid-template-columns:repeat(auto-fill,minmax(min(100%,370px),1fr)); gap:18px; } +.sandbox-card { padding:22px; min-width:0; } +.sandbox-card-heading { display:flex; align-items:flex-start; justify-content:space-between; gap:12px; } +.sandbox-card h3 { font-size:15px; line-height:1.5; margin:0; overflow-wrap:anywhere; } +.sandbox-card .sandbox-badge { flex-shrink:0; } +.sandbox-card-identity { color:var(--muted); font-size:12px; margin:10px 0 18px; overflow-wrap:anywhere; } +.sandbox-card-message { color:var(--muted); font-size:12px; line-height:1.8; min-height:44px; overflow-wrap:anywhere; } +.sandbox-card .sandbox-actions { gap:8px; } +.sandbox-card .sandbox-actions button,.sandbox-card .sandbox-desktop { padding:8px 11px; font-size:12px; } +.sandbox-card-footer { display:flex; align-items:center; justify-content:space-between; padding-top:15px; margin-top:18px; border-top:1px solid var(--line); } +.sandbox-card-footer code { font-size:11px; color:var(--muted); } +.sandbox-empty { padding:48px 24px; text-align:center; font-size:13px; line-height:1.8; color:var(--muted); } +.sandbox-template { padding:28px; } +.sandbox-template-heading { display:flex; align-items:center; gap:14px; margin-bottom:26px; flex-wrap:wrap; } +.sandbox-template-heading h2 { font-size:20px; margin:0; } +.sandbox-template-icon { font-size:28px; color:var(--accent); background:#e7f2ec; border-radius:10px; padding:8px 14px; } +.sandbox-template-fields { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:24px 40px; } +.sandbox-template-fields > div { border-top:1px solid var(--line); padding-top:16px; min-width:0; } +.sandbox-template-fields dt { font-size:12px; color:var(--muted); margin-bottom:10px; } +.sandbox-template-fields dd { font-size:13px; margin:0; line-height:1.8; white-space:pre-line; overflow-wrap:anywhere; } +.sandbox-template-note { margin:26px 0 0; padding:16px; border-radius:7px; background:#f5f8f6; color:var(--muted); font-size:12px; line-height:1.9; } +.sandbox-create-dialog { width:min(560px,calc(100% - 32px)); } +.sandbox-create-dialog h2 { font-size:20px; margin:0; } +.sandbox-create-form { display:grid; gap:20px; padding:24px; } +.sandbox-create-form p { margin:0; font-size:13px; line-height:1.8; color:var(--muted); } +.sandbox-create-form .sandbox-create-template { padding:12px 14px; background:#f5f8f6; border-radius:7px; } +.sandbox-create-form .sandbox-actions { justify-content:flex-end; padding-top:8px; } +@media(max-width:650px) { + .sandboxes-page .page-heading { flex-direction:column; align-items:flex-start; } + .sandbox-summary { grid-template-columns:repeat(2,minmax(0,1fr)); } + .sandbox-summary > div { padding:18px; } + .sandbox-summary > div:nth-child(3) { border-left:0; } + .sandbox-summary > div:nth-child(n+3) { border-top:1px solid var(--line); } + .sandbox-template-fields { grid-template-columns:1fr; } + .sandbox-template { padding:20px; } + .sandbox-filters select { flex:1; max-width:100%; } +} + +.sandbox-template-list { display:grid; gap:20px; } +.sandbox-template-heading > div:nth-child(2) { flex:1; min-width:0; } +.sandbox-template-heading h2 { margin:4px 0; } +.sandbox-capabilities { display:flex; flex-wrap:wrap; gap:8px; margin:18px 0; } +.sandbox-service-actions { display:flex; flex-wrap:wrap; gap:8px; width:100%; padding-top:10px; } +.sandbox-service-link { text-decoration:none; } diff --git a/crates/agentdrive-mcp/dashboard/sandboxes.js b/crates/agentdrive-mcp/dashboard/sandboxes.js new file mode 100644 index 0000000..521acd3 --- /dev/null +++ b/crates/agentdrive-mcp/dashboard/sandboxes.js @@ -0,0 +1,214 @@ +import { createSandboxControl } from './sandbox.js'; + +const labels = {ready:'运行中',creating:'准备中',starting:'启动中',stopping:'停止中',stopped:'已停止',error:'启动失败',idle:'尚未创建'}; + +export function createSandboxesPage({api, element, container}) { + let generation = 0, data = null, templateKey = '', workspaceKey = ''; + const cards = new Map(); + const heading = element('div', 'page-heading'); + const intro = element('div'); + intro.append(element('div', 'eyebrow', 'SANDBOXES'), element('h1', '', '沙盒'), element('p', '', '管理所有工作区的 Ubuntu 桌面,在独立环境中操作分支文件。')); + const create = element('button', 'primary', '创建沙盒'); create.type = 'button'; create.disabled = true; + heading.append(intro, create); + const notice = element('p', 'error-banner'); notice.hidden = true; notice.setAttribute('role', 'alert'); + const stats = element('section', 'sandbox-summary'); stats.setAttribute('aria-label', '沙盒统计'); + const counters = ['已创建', '运行中', '启动或停止中', '运行上限'].map(title => { + const item = element('div'); const value = element('strong', '', '—'); item.append(element('span', '', title), value); stats.append(item); return value; + }); + const tabs = element('div', 'sandbox-page-tabs'); tabs.setAttribute('role', 'tablist'); tabs.setAttribute('aria-label', '沙盒视图'); + const instancesTab = element('button', '', '沙盒实例'); const templateTab = element('button', '', '模板内容'); + const instances = element('section'); instances.id = 'sandbox-instances'; instances.setAttribute('role', 'tabpanel'); + const template = element('section', 'sandbox-template-list'); template.id = 'sandbox-template'; template.setAttribute('role', 'tabpanel'); + function selectTab(index) { + instances.hidden = index !== 0; template.hidden = index !== 1; + [instancesTab, templateTab].forEach((tab, i) => { tab.setAttribute('aria-selected', String(i === index)); tab.tabIndex = i === index ? 0 : -1; }); + } + [instancesTab, templateTab].forEach((tab, i) => { + tab.type = 'button'; tab.id = `sandbox-tab-${i}`; tab.setAttribute('role', 'tab'); tab.setAttribute('aria-controls', [instances, template][i].id); + [instances, template][i].setAttribute('aria-labelledby', tab.id); + tab.onclick = () => selectTab(i); + tab.onkeydown = event => { + if (['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) { + event.preventDefault(); const next = event.key === 'Home' ? 0 : event.key === 'End' ? 1 : 1 - i; + selectTab(next); [instancesTab, templateTab][next].focus(); + } + }; + tabs.append(tab); + }); + const filters = element('div', 'sandbox-filters'); + const search = element('input'); search.type = 'search'; search.placeholder = '搜索分支或工作区'; search.setAttribute('aria-label', '搜索沙盒'); + const workspace = element('select'); workspace.setAttribute('aria-label', '筛选工作区'); + const state = element('select'); state.setAttribute('aria-label', '筛选沙盒状态'); + for (const [value, label] of [['existing','全部已创建'],['ready','运行中'],['pending','启动或停止中'],['stopped','已停止'],['error','启动失败'],['idle','可创建的分支']]) { + const option = element('option', '', label); option.value = value; state.append(option); + } + filters.append(search, workspace, state); + const list = element('div', 'sandbox-catalog'); + const empty = element('div', 'sandbox-empty panel', '正在读取沙盒…'); empty.setAttribute('role', 'status'); + instances.append(filters, list, empty); + container.append(heading, notice, stats, tabs, instances, template); selectTab(0); + create.setAttribute('aria-haspopup', 'dialog'); + const createDialog = element('dialog', 'revision-dialog sandbox-create-dialog'); + createDialog.setAttribute('aria-labelledby', 'sandbox-create-title'); + const createHeader = element('div', 'revision-dialog-heading'); + const createTitle = element('h2', '', '创建沙盒'); createTitle.id = 'sandbox-create-title'; + const closeCreate = element('button', 'revision-dialog-close', '×'); closeCreate.type = 'button'; closeCreate.setAttribute('aria-label', '关闭创建沙盒'); + createHeader.append(createTitle, closeCreate); + const createForm = element('form', 'sandbox-create-form'); + createForm.append(element('p', '', '选择工作区和分支,创建 Ubuntu 桌面。分支文件会自动挂载到 /workspace。')); + const targetWorkspace = element('select'); targetWorkspace.id = 'sandbox-create-workspace'; + const targetBranch = element('select'); targetBranch.id = 'sandbox-create-branch'; + const targetTemplate = element('select'); targetTemplate.id = 'sandbox-create-template'; + for (const [name, input] of [['工作区', targetWorkspace], ['分支', targetBranch], ['模板', targetTemplate]]) { + input.setAttribute('aria-label', name); + const label = element('label', 'management-field'); label.htmlFor = input.id; label.append(element('span', '', name), input); createForm.append(label); + } + const templateInfo = element('p', 'sandbox-create-template'); + const createFeedback = element('p', 'sandbox-create-feedback'); createFeedback.setAttribute('role', 'status'); + const branchesLink = element('a', 'management-link', '前往分支管理'); + const createActions = element('div', 'sandbox-actions'); + const cancelCreate = element('button', 'secondary', '取消'); cancelCreate.type = 'button'; + const submitCreate = element('button', 'primary', '创建并启动'); submitCreate.type = 'submit'; + createActions.append(cancelCreate, submitCreate); + createForm.append(templateInfo, createFeedback, branchesLink, createActions); createDialog.append(createHeader, createForm); + function options(select, values, preferred, placeholder) { + const key = JSON.stringify(values); + if (select.dataset.options !== key) { + select.replaceChildren(); + for (const [id, label] of values.length ? values : [['', placeholder]]) { const option = element('option', '', label); option.value = id; select.append(option); } + select.dataset.options = key; + } + select.value = values.some(([id]) => id === preferred) ? preferred : values[0]?.[0] || ''; + select.disabled = !values.length; + } + function updateCreate() { + if (!createDialog.open || !data) return; + const allWorkspaces = [...new Map(data.entries.map(entry => [entry.workspace, entry.workspace_name])).entries()]; + options(targetWorkspace, allWorkspaces, targetWorkspace.value, '暂无工作区'); + const available = data.entries.filter(entry => entry.workspace === targetWorkspace.value && entry.can_start && !entry.sandbox); + options(targetBranch, available.map(entry => [entry.branch, entry.branch_name]), targetBranch.value, '没有可创建沙盒的分支'); + const active = data.entries.filter(entry => ['ready', 'creating', 'starting', 'stopping'].includes(entry.sandbox?.state)).length; + const limit = data.template?.max_instances; + const full = limit != null && active >= limit; + submitCreate.disabled = !data.enabled || !available.length || full; + createFeedback.textContent = !data.enabled ? 'Sandbox 运行环境尚未配置。' : !allWorkspaces.length ? '请先创建工作区和分支。' : !available.length ? '此工作区没有可用分支。每个分支对应一个沙盒;可以选择其他工作区,或前往分支管理创建新分支。' : full ? `当前已达到 ${limit} 个运行实例的上限。请先停止一个沙盒,再创建新沙盒。` : '创建后会显示启动步骤,桌面就绪后可以打开桌面或获取 MCP 配置。'; + branchesLink.hidden = !targetWorkspace.value || available.length > 0; + branchesLink.href = `/branches?workspace=${encodeURIComponent(targetWorkspace.value)}`; + const templates = data.templates?.length ? data.templates : data.template ? [{id:'ubuntu-desktop',...data.template}] : []; + options(targetTemplate, templates.map(item => [item.id,item.manifest?.name || item.id]), targetTemplate.value || templates.find(item => item.capabilities?.includes('csgclaw'))?.id, '暂无可用模板'); + const value = templates.find(item => item.id === targetTemplate.value); + submitCreate.disabled ||= !value; + templateInfo.textContent = value ? `模板:${value.manifest?.name || 'Ubuntu Desktop'} · ${value.vcpus} vCPU · ${value.memory_gib} GiB 内存 · ${value.disk_gib} GiB 系统磁盘` : '模板:当前配置的 Ubuntu 桌面'; + } + create.onclick = () => { + if (!createDialog.isConnected) document.body.append(createDialog); + createDialog.showModal(); + options(targetWorkspace, [...new Map((data?.entries || []).map(entry => [entry.workspace, entry.workspace_name])).entries()], workspace.value || new URLSearchParams(location.search).get('workspace'), '暂无工作区'); + updateCreate(); targetWorkspace.focus(); + }; + targetWorkspace.onchange = targetTemplate.onchange = updateCreate; + closeCreate.onclick = cancelCreate.onclick = () => createDialog.close(); + createDialog.addEventListener('close', () => { if (create.isConnected && !container.hidden && !document.querySelector('dialog[open]')) create.focus(); }); + createForm.onsubmit = event => { + event.preventDefault(); updateCreate(); + if (submitCreate.disabled) return; + const card = cards.get(`${targetWorkspace.value}/${targetBranch.value}`); + if (!card || card.entry.sandbox || !card.entry.can_start) return; + createDialog.close(); selectTab(0); state.value = 'existing'; search.value = ''; workspace.value = targetWorkspace.value; + card.entry.sandbox = {state:'creating', startup_step:'prepare_branch'}; filter(); + card.control.start(targetTemplate.value); + }; + search.oninput = filter; workspace.onchange = filter; state.onchange = filter; + function filter() { + let count = 0; + const query = search.value.trim().toLocaleLowerCase(); + for (const {node, entry} of cards.values()) { + const current = entry.sandbox?.state || 'idle'; + const matches = state.value === 'existing' ? !!entry.sandbox : state.value === 'pending' ? ['creating','starting','stopping'].includes(current) : state.value === 'idle' ? !entry.sandbox && entry.can_start : current === state.value; + node.hidden = !matches || (!!workspace.value && workspace.value !== entry.workspace) || !`${entry.workspace_name} ${entry.branch_name} ${entry.branch}`.toLocaleLowerCase().includes(query); + if (!node.hidden) count++; + } + empty.hidden = count > 0; + empty.textContent = !data ? '正在读取沙盒…' : !data.enabled ? 'Sandbox 运行环境尚未配置。配置完成后即可创建沙盒。' : state.value === 'idle' ? '没有符合条件的分支。可以在分支管理中创建分支,或启动已停止的沙盒。' : '没有符合条件的沙盒。点击「创建沙盒」选择工作区分支。'; + } + function renderTemplate() { + const values = data.templates?.length ? data.templates : data.template ? [{id:'ubuntu-desktop',...data.template}] : []; + const key = JSON.stringify([data.enabled, values]); if (templateKey === key) return; templateKey = key; + template.replaceChildren(); + if (!values.length) { template.append(element('p', 'sandbox-empty panel', '尚未配置模板')); return; } + for (const value of values) { + const card = element('article', 'sandbox-template panel'); + const header = element('div', 'sandbox-template-heading'); + const title = element('div'); title.append(element('div', 'sandbox-eyebrow', value.id),element('h2', '', value.manifest?.name || value.id)); + const use = element('button', 'primary', '使用此模板创建'); use.type = 'button'; use.disabled = !data.enabled; + use.onclick = () => { create.click(); targetTemplate.value = value.id; updateCreate(); }; + header.append(element('div', 'sandbox-template-icon', '▧'),title,use); card.append(header); + const capabilities = element('div', 'sandbox-capabilities'); + const labels = {desktop:'桌面',browser:'浏览器',mcp:'统一 MCP',openclaw:'OpenClaw',csgclaw:'CSGClaw',environment:'开发环境'}; + for (const capability of value.capabilities || ['desktop','browser','mcp']) capabilities.append(element('span','pill',labels[capability] || capability)); + card.append(capabilities); + const fields = element('dl', 'sandbox-template-fields'); + function field(name, content) { const group = element('div'); group.append(element('dt','',name),element('dd','',content)); fields.append(group); } + const manifest = value.manifest; + if (manifest) { + field('操作系统',manifest.os); field('桌面环境',manifest.desktop); field('默认浏览器',manifest.browser); + field('语言环境',manifest.locale); field('字体',manifest.fonts.join('\n')); field('桌面工具',manifest.tools.join('\n')); + field('分支目录',manifest.workspace_path); field('清单生成时间',new Date(manifest.built_at).toLocaleString('zh-CN',{hour12:false})); + } + field('每个实例的资源',`${value.vcpus} vCPU · ${value.memory_gib} GiB 内存 · ${value.disk_gib} GiB 系统磁盘`); + field('模板版本',value.version || 'local'); + field('并发与网络',`最多 ${value.max_instances} 个运行实例 · ${value.network_slots} 个已配置网络接口`); + card.append(fields,element('p','sandbox-template-note','分支文件自动挂载到 /workspace。已有沙盒继续使用创建时的系统磁盘。')); template.append(card); + } + } + function render() { + notice.hidden = !data.errors.length; notice.textContent = data.errors.join(';'); + create.disabled = !data.enabled; + const existing = data.entries.filter(entry => entry.sandbox); + counters[0].textContent = existing.length; + counters[1].textContent = existing.filter(entry => entry.sandbox.state === 'ready').length; + counters[2].textContent = existing.filter(entry => ['creating','starting','stopping'].includes(entry.sandbox.state)).length; + counters[3].textContent = data.template?.max_instances ?? '—'; + const workspaces = [...new Map(data.entries.map(entry => [entry.workspace, entry.workspace_name])).entries()]; + const key = JSON.stringify(workspaces); + if (workspaceKey !== key) { + const selected = workspace.value; workspace.replaceChildren(); + for (const [id, name] of [['', '所有工作区'], ...workspaces]) { const option = element('option', '', name); option.value = id; workspace.append(option); } + workspace.value = workspaces.some(([id]) => id === selected) ? selected : ''; workspaceKey = key; + } + const retained = new Set(); + for (const entry of data.entries) { + const id = `${entry.workspace}/${entry.branch}`; retained.add(id); + let card = cards.get(id); + if (!card) { + const node = element('article', 'sandbox-card panel'); node.dataset.branch = entry.branch; + const top = element('div', 'sandbox-card-heading'); const title = element('h3'); const badge = element('span', 'sandbox-badge'); top.append(title, badge); + const identity = element('p', 'sandbox-card-identity'); const message = element('p', 'sandbox-card-message'); + const control = createSandboxControl({api, element, workspace:entry.workspace, branch:entry.branch, branchName:entry.branch_name, workspaceName:entry.workspace_name, inline:true, templates:data.templates || [], initial:{sandbox:entry.sandbox,enabled:data.enabled}, canStart:entry.can_start, onDeleted:() => { + generation++; + const current = data?.entries.find(item => item.workspace === entry.workspace && item.branch === entry.branch); + if (current) { current.sandbox = null; render(); } + load(); + }}); + const footer = element('div', 'sandbox-card-footer'); const branchLink = element('a', 'management-link', '查看分支'); + branchLink.href = `/branches?workspace=${encodeURIComponent(entry.workspace)}&branch=${encodeURIComponent(entry.branch)}`; + footer.append(element('code', '', '/workspace'), branchLink); + node.append(top, identity, message, control, footer); list.append(node); + card = {node, title, badge, identity, message, control}; cards.set(id, card); + } + card.entry = entry; card.title.textContent = entry.branch_name; card.identity.textContent = `${entry.workspace_name} · ${entry.branch.slice(0, 8)}${entry.sandbox?.template_id ? ' · ' + ((data.templates || []).find(item => item.id === entry.sandbox.template_id)?.manifest?.name || entry.sandbox.template_id) : ''}`; + const state = entry.sandbox?.state || 'idle'; card.badge.textContent = labels[state] || state; card.badge.dataset.state = state; + card.message.textContent = entry.sandbox?.message || (state === 'idle' ? '创建 Ubuntu 桌面,自动挂载此分支文件。' : state === 'stopped' ? '系统磁盘已保留,可以继续启动。' : '分支文件自动挂载到 /workspace。'); + card.control.setBranchName(entry.branch_name, entry.workspace_name); card.control.update(entry.sandbox, data.enabled, entry.can_start); + } + for (const [id, card] of cards) if (!retained.has(id)) { card.control.dispose(); card.node.remove(); cards.delete(id); } + filter(); renderTemplate(data.template); updateCreate(); + } + async function load() { + const request = ++generation; + try { const result = await api('/api/sandboxes'); if (request === generation) { data = result; render(); } } + catch (error) { if (request === generation) { notice.hidden = false; notice.textContent = `沙盒数据刷新失败:${error.message}`; if (!data) empty.textContent = '暂时无法读取沙盒,请点击页面右上方刷新按钮重试。'; } } + } + function clear() { generation++; createDialog.close(); createDialog.remove(); for (const card of cards.values()) card.control.dispose(); cards.clear(); list.replaceChildren(); data = null; templateKey = ''; workspaceKey = ''; } + return {load, clear}; +} diff --git a/crates/agentdrive-mcp/src/gateway.rs b/crates/agentdrive-mcp/src/gateway.rs index 266f595..8543e7e 100644 --- a/crates/agentdrive-mcp/src/gateway.rs +++ b/crates/agentdrive-mcp/src/gateway.rs @@ -44,7 +44,7 @@ pub fn router(services: Services, config: GatewayConfig) -> Result { .with_json_response(true) .with_cancellation_token(config.cancellation.clone()), ); - let api = crate::web::api(services) + let api = crate::web::api(services.clone()) .merge(crate::web::settings_api(config.settings.clone())) .merge( Router::new() @@ -53,10 +53,15 @@ pub fn router(services: Services, config: GatewayConfig) -> Result { ) .layer(axum::extract::DefaultBodyLimit::max(8 * 1024 * 1024)) .route_layer(axum::middleware::from_fn_with_state( - Arc::new(config), + Arc::new(config.clone()), local_request, )); - Ok(api.merge(crate::web::assets())) + Ok(api + .merge(crate::web::assets()) + .layer(axum::middleware::from_fn_with_state( + (services, Arc::new(config)), + crate::sandbox_proxy::service_origin, + ))) } async fn handle(State(transport): State>, request: Request) -> Response { diff --git a/crates/agentdrive-mcp/src/lib.rs b/crates/agentdrive-mcp/src/lib.rs index 8bd392a..4f83027 100644 --- a/crates/agentdrive-mcp/src/lib.rs +++ b/crates/agentdrive-mcp/src/lib.rs @@ -8,3 +8,5 @@ mod workspace_view; pub use client::{Client, stdio_proxy}; pub use gateway::{GatewayConfig, router}; pub use server::{McpServer, tool_catalog}; + +mod sandbox_proxy; diff --git a/crates/agentdrive-mcp/src/sandbox_collaboration_tests.rs b/crates/agentdrive-mcp/src/sandbox_collaboration_tests.rs new file mode 100644 index 0000000..9455c4a --- /dev/null +++ b/crates/agentdrive-mcp/src/sandbox_collaboration_tests.rs @@ -0,0 +1,180 @@ +use super::*; + +#[tokio::test] +async fn running_sandbox_allows_turn_handoff_and_defers_recovery_branch_switch() { + let remote = Arc::new(MemoryRemote::default()); + let f = fixture_with_remote(Some(remote.clone())).await; + f.sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap(); + let mount = f.local.mounts().await.unwrap().remove(0); + f.engine + .services + .collaboration + .configure( + &f.owner, + ConfigureCollaboration { + mount: mount.id, + expected_binding_generation: Some(mount.generation), + enabled: true, + }, + ) + .await + .unwrap(); + f.engine.collaborate_due().await.unwrap(); + for (turn, phase) in [ + ("first", ExecutionPhase::Started), + ("first", ExecutionPhase::Completed), + ("second", ExecutionPhase::Started), + ] { + f.engine + .services + .collaboration + .execution_event( + &f.owner, + CollaborationEvent { + mount: mount.id, + source: "sandbox-test".into(), + thread_id: "thread".into(), + turn_id: turn.into(), + phase, + }, + ) + .await + .unwrap(); + f.engine.collaborate_due().await.unwrap(); + if phase == ExecutionPhase::Completed { + assert_eq!( + f.local.branch(f.branch).await.unwrap().unwrap().state, + BranchState::Available + ); + } + } + let current = f.local.mount(mount.id).await.unwrap().unwrap(); + assert_eq!(current.branch, f.branch); + assert!(current.generation > mount.generation); + f.engine + .services + .files + .create( + &FsContext { + mount: current.id, + generation: current.generation, + identity: LocalIdentity { + uid: 1000, + gid: 1000, + }, + }, + InodeId::ROOT, + "local-work.txt", + FileKind::File, + 0o644, + ) + .await + .unwrap(); + + let branch = f.local.branch(f.branch).await.unwrap().unwrap(); + let saved = f + .engine + .services + .revisions + .commit( + RequestContext { + principal: f.owner.clone(), + request_id: OperationId::new().to_string(), + }, + CommitRevision { + message: None, + guard: BranchGuard::from(&branch), + kind: CommitKind::Commit, + durability: Durability::Local, + }, + ) + .await + .unwrap(); + assert!(saved.error.is_none(), "{saved:?}"); + let key = RefKey::Branch { + workspace: f.workspace, + branch: f.branch, + }; + let previous = remote.get(&key).await.unwrap().unwrap(); + let mut replacement: BranchRef = decode(&previous.bytes).unwrap(); + replacement.owner = LocationId::new(); + replacement.authority_epoch += 1; + replacement.source_generation += 1; + assert!(matches!( + remote + .compare_exchange(RefUpdate { + key, + expected_version: Some(previous.version), + bytes: encode(&replacement).unwrap().into(), + }) + .await + .unwrap(), + CasOutcome::Applied { .. } + )); + let sync = f + .engine + .services + .replica + .sync( + RequestContext { + principal: f.owner.clone(), + request_id: OperationId::new().to_string(), + }, + SyncRequest { + workspace: f.workspace, + branches: Some(vec![f.branch]), + direction: SyncDirection::Push, + }, + ) + .await; + match sync { + Err(error) => assert_eq!(error.code, ErrorCode::StaleAuthority), + Ok(record) => assert_eq!(record.error.unwrap().code, ErrorCode::StaleAuthority), + } + assert_eq!( + f.engine.collaborate_due().await.unwrap_err().code, + ErrorCode::BranchBusy + ); + assert_eq!( + f.local.mount(mount.id).await.unwrap().unwrap().branch, + f.branch + ); + assert!(matches!( + f.driver.status(&f.branch.to_string()).await.unwrap().state, + SandboxState::Ready + )); + // Stopping releases the binding even when saving reports authority recovery. + let stopped = f.sandbox.stop(&f.owner, f.workspace, f.branch).await; + if let Err(error) = stopped { + assert_eq!(error.code, ErrorCode::RecoveryRequired); + } + f.engine.collaborate_due().await.unwrap(); + let recovered = f.local.mount(mount.id).await.unwrap().unwrap(); + assert_ne!(recovered.branch, f.branch); + assert_eq!(recovered.path, mount.path); + let entries = f + .engine + .services + .files + .readdir( + &FsContext { + mount: recovered.id, + generation: recovered.generation, + identity: LocalIdentity { + uid: 1000, + gid: 1000, + }, + }, + InodeId::ROOT, + ) + .await + .unwrap(); + assert!(entries.iter().any(|entry| entry.name == "local-work.txt")); + assert!(matches!( + f.driver.status(&f.branch.to_string()).await.unwrap().state, + SandboxState::Stopped + )); +} diff --git a/crates/agentdrive-mcp/src/sandbox_proxy.rs b/crates/agentdrive-mcp/src/sandbox_proxy.rs new file mode 100644 index 0000000..1557fd4 --- /dev/null +++ b/crates/agentdrive-mcp/src/sandbox_proxy.rs @@ -0,0 +1,122 @@ +use crate::gateway::GatewayConfig; +use agentdrive_model::{BranchId, ErrorCode, WorkspaceId}; +use agentdrive_ports::Services; +use axum::{ + extract::{Request, State}, + http::{StatusCode, header}, + response::{Html, IntoResponse, Response}, +}; +use std::sync::Arc; + +fn starting_page(html: bool) -> Response { + if !html { + return (StatusCode::SERVICE_UNAVAILABLE, "助手正在启动,请稍后重试").into_response(); + } + (StatusCode::SERVICE_UNAVAILABLE, [(header::CACHE_CONTROL, "no-store")], Html( + "CSGClaw 正在启动

CSGClaw 正在启动

Ubuntu 已经启动,正在准备助手服务。页面会自动重试。

立即重试
" + )).into_response() +} + +// 每个实例的助手使用独立 localhost 来源,页面资源与 WebSocket 保持原路径。 +pub(crate) async fn service_origin( + State((services, config)): State<(Services, Arc)>, + mut request: Request, + next: axum::middleware::Next, +) -> Response { + let html = request + .headers() + .get(header::ACCEPT) + .and_then(|value| value.to_str().ok()) + .is_some_and(|value| value.contains("text/html")); + let host = request + .headers() + .get(header::HOST) + .and_then(|v| v.to_str().ok()) + .unwrap_or("") + .to_owned(); + let Some((hostname, port)) = host.rsplit_once(':') else { + return next.run(request).await; + }; + let Some(identity) = hostname.strip_suffix(".localhost") else { + return next.run(request).await; + }; + let parts: Vec<_> = identity.split('.').collect(); + if parts.len() != 3 { + return (StatusCode::FORBIDDEN, "助手地址无效").into_response(); + } + if !config + .allowed_hosts + .iter() + .any(|value| value == &format!("127.0.0.1:{port}") || value == &format!("localhost:{port}")) + { + return (StatusCode::FORBIDDEN, "助手端口无效").into_response(); + } + if let Some(origin) = request.headers().get(header::ORIGIN) + && origin.to_str().ok() != Some(format!("http://{host}").as_str()) + { + return (StatusCode::FORBIDDEN, "助手来源无效").into_response(); + } + let (Ok(workspace), Ok(branch)) = ( + parts[1].parse::(), + parts[2].parse::(), + ) else { + return (StatusCode::BAD_REQUEST, "助手分支无效").into_response(); + }; + let Some(runtime) = services.sandbox else { + return (StatusCode::SERVICE_UNAVAILABLE, "Sandbox 尚未配置").into_response(); + }; + let stream = match runtime + .service( + &config.settings.startup().principal, + workspace, + branch, + parts[0], + ) + .await + { + Ok(stream) => stream, + Err(error) => { + return match error.code { + ErrorCode::PermissionDenied => { + (StatusCode::FORBIDDEN, error.message).into_response() + } + ErrorCode::NotFound => (StatusCode::NOT_FOUND, error.message).into_response(), + _ => starting_page(html), + }; + } + }; + request.headers_mut().remove("forwarded"); + request.headers_mut().remove("x-forwarded-for"); + let client_upgrade = hyper::upgrade::on(&mut request); + let (mut sender, connection) = + match hyper::client::conn::http1::handshake(hyper_util::rt::TokioIo::new(stream)).await { + Ok(value) => value, + Err(_) => return starting_page(html), + }; + tokio::spawn(async move { + let _ = connection.with_upgrades().await; + }); + let mut response = match sender.send_request(request).await { + Ok(response) => response, + Err(_) => return starting_page(html), + }; + if response.status() == StatusCode::SWITCHING_PROTOCOLS { + let guest_upgrade = hyper::upgrade::on(&mut response); + tokio::spawn(async move { + if let (Ok(client), Ok(guest)) = tokio::join!(client_upgrade, guest_upgrade) { + let _ = tokio::io::copy_bidirectional( + &mut hyper_util::rt::TokioIo::new(client), + &mut hyper_util::rt::TokioIo::new(guest), + ) + .await; + } + }); + } + response + .headers_mut() + .insert(header::CACHE_CONTROL, "no-store".parse().unwrap()); + response + .headers_mut() + .insert(header::X_CONTENT_TYPE_OPTIONS, "nosniff".parse().unwrap()); + response.map(axum::body::Body::new) +} diff --git a/crates/agentdrive-mcp/src/sandbox_tests.rs b/crates/agentdrive-mcp/src/sandbox_tests.rs new file mode 100644 index 0000000..f7ec303 --- /dev/null +++ b/crates/agentdrive-mcp/src/sandbox_tests.rs @@ -0,0 +1,547 @@ +use super::*; +use agentdrive_config::{LiveConfig, RuntimeConfig}; +use agentdrive_engine::{Adapters, Engine, EngineConfig, SandboxService}; +use agentdrive_local::LocalBackend; +use agentdrive_test_support::{ManualClock, MemoryMountDriver, MemoryRemote, NoHostDirectories}; +use async_trait::async_trait; +use std::collections::BTreeMap; +use tokio::sync::Mutex; + +#[path = "sandbox_collaboration_tests.rs"] +mod collaboration; + +#[derive(Default)] +struct RuntimeStub { + states: Mutex>, + fail_stop: std::sync::atomic::AtomicBool, + start_gate: Mutex>>, + starting: tokio::sync::Notify, +} +#[async_trait] +impl SandboxRuntime for RuntimeStub { + async fn mcp_config(&self, id: &str) -> Result { + Ok(SandboxMcpConfig { + mcp_servers: BTreeMap::from([( + "ubuntu_desktop".into(), + SandboxMcpServer { + command: "python3".into(), + args: vec![id.into()], + }, + )]), + }) + } + + async fn desktop_asset(&self, _: &str) -> Result { + Err(Error::new(ErrorCode::NotFound, "missing")) + } + async fn connect(&self, _: &str, _: u32) -> Result { + Err(Error::new(ErrorCode::Unavailable, "no guest")) + } + async fn start(&self, request: SandboxLaunch) -> Result { + if let Some(gate) = self.start_gate.lock().await.take() { + self.starting.notify_one(); + gate.notified().await; + } + let status = SandboxStatus { + template_id: default_sandbox_template(), + services: Vec::new(), + id: request.id.clone(), + workspace: request.binding.workspace, + branch: request.binding.branch, + state: SandboxState::Ready, + desktop_url: None, + message: None, + startup_step: None, + }; + assert!( + self.states + .lock() + .await + .insert(request.id, status.clone()) + .is_none_or(|previous| matches!(previous.state, SandboxState::Stopped)), + "duplicate runtime launch" + ); + Ok(status) + } + async fn stop(&self, id: &str) -> Result<()> { + if self.fail_stop.load(std::sync::atomic::Ordering::Relaxed) { + return Err(Error::new( + ErrorCode::Unavailable, + "guest helper unavailable", + )); + } + self.states.lock().await.get_mut(id).unwrap().state = SandboxState::Stopped; + Ok(()) + } + async fn force_stop(&self, id: &str) -> Result<()> { + self.states.lock().await.get_mut(id).unwrap().state = SandboxState::Stopped; + Ok(()) + } + async fn delete(&self, id: &str) -> Result<()> { + self.states.lock().await.remove(id); + Ok(()) + } + async fn status(&self, id: &str) -> Result { + self.states + .lock() + .await + .get(id) + .cloned() + .ok_or_else(|| Error::new(ErrorCode::NotFound, "missing")) + } +} + +struct Fixture { + _directory: tempfile::TempDir, + local: Arc, + engine: Arc, + owner: Principal, + workspace: WorkspaceId, + branch: BranchId, + driver: Arc, + sandbox: Arc, +} +async fn fixture() -> Fixture { + fixture_with_remote(None).await +} +async fn fixture_with_remote(remote: Option>) -> Fixture { + let directory = tempfile::tempdir().unwrap(); + let local = Arc::new(LocalBackend::open(directory.path()).unwrap()); + let engine = Engine::open( + Adapters { + state: local.clone(), + objects: local.clone(), + working: local.clone(), + remote_objects: remote + .clone() + .map(|remote| remote as Arc), + remote_refs: remote.map(|remote| remote as Arc), + clock: Arc::new(ManualClock::default()), + mounts: Arc::new(MemoryMountDriver::default()), + directories: Arc::new(NoHostDirectories), + }, + EngineConfig { + live: LiveConfig::new(RuntimeConfig::default()).unwrap(), + identity: LocalIdentity { + uid: 1000, + gid: 1000, + }, + }, + ) + .await + .unwrap(); + let owner: Principal = "owner".to_owned().try_into().unwrap(); + let server = crate::McpServer::new(engine.services.clone(), owner.clone()); + let created = server + .dispatch( + "workspace_create", + json!({"request_id":OperationId::new(),"request":{"name":"sandbox"}}), + ) + .await + .unwrap(); + let workspace: WorkspaceId = serde_json::from_value(created["workspace"].clone()).unwrap(); + let branch: BranchId = serde_json::from_value(created["branch"].clone()).unwrap(); + let driver = Arc::new(RuntimeStub::default()); + let sandbox = SandboxService::new( + engine.runtime.clone(), + engine.services.clone(), + driver.clone(), + directory.path().join("mounts"), + ); + Fixture { + _directory: directory, + local, + engine, + owner, + workspace, + branch, + driver, + sandbox, + } +} + +#[tokio::test] +async fn sandbox_authorizes_branch_and_pins_binding_until_stop() { + let Fixture { + _directory, + local, + engine, + owner, + workspace, + branch, + sandbox, + .. + } = fixture().await; + let stranger: Principal = "stranger".to_owned().try_into().unwrap(); + assert_eq!( + sandbox + .create(&stranger, workspace, branch) + .await + .unwrap_err() + .code, + ErrorCode::PermissionDenied + ); + assert_eq!( + sandbox + .mcp_config(&stranger, workspace, branch) + .await + .unwrap_err() + .code, + ErrorCode::PermissionDenied + ); + assert_eq!( + sandbox + .mcp_config(&owner, workspace, branch) + .await + .unwrap() + .mcp_servers["ubuntu_desktop"] + .args[0], + branch.to_string() + ); + let first = sandbox.create(&owner, workspace, branch).await.unwrap(); + assert!( + sandbox + .create_with_template(&owner, workspace, branch, Some("missing".into())) + .await + .is_err() + ); + assert!(matches!( + sandbox + .status(&owner, workspace, branch) + .await + .unwrap() + .unwrap() + .state, + SandboxState::Ready + )); + assert_eq!( + sandbox + .set_proxy_config(&stranger, workspace, branch, SandboxProxyConfig::default()) + .await + .unwrap_err() + .code, + ErrorCode::PermissionDenied + ); + + let mut services = engine.services.clone(); + services.sandbox = Some(sandbox.clone()); + let catalog = sandboxes(State(services.clone()), Extension(owner.clone())) + .await + .map_err(|error| error.0) + .unwrap() + .0; + assert_eq!(catalog["entries"].as_array().unwrap().len(), 1); + assert_eq!(catalog["entries"][0]["sandbox"]["id"], first.id); + assert_eq!(catalog["entries"][0]["can_start"], true); + let other_catalog = sandboxes(State(services.clone()), Extension(stranger.clone())) + .await + .map_err(|error| error.0) + .unwrap() + .0; + assert!(other_catalog["entries"].as_array().unwrap().is_empty()); + assert_eq!( + sandbox.create(&owner, workspace, branch).await.unwrap().id, + first.id + ); + assert_eq!( + sandbox + .create(&owner, WorkspaceId::new(), branch) + .await + .unwrap_err() + .code, + ErrorCode::NotFound + ); + let mount = local + .mounts() + .await + .unwrap() + .into_iter() + .find(|m| m.branch == branch) + .unwrap(); + let result = engine + .services + .mounts + .unmount( + RequestContext { + principal: owner.clone(), + request_id: OperationId::new().to_string(), + }, + mount.id, + mount.generation, + ) + .await; + match result { + Err(error) => assert_eq!(error.code, ErrorCode::BranchBusy), + Ok(record) => assert_eq!(record.error.unwrap().code, ErrorCode::BranchBusy), + } + assert_eq!( + sandbox + .delete(&stranger, workspace, branch) + .await + .unwrap_err() + .code, + ErrorCode::PermissionDenied + ); + assert_eq!( + sandbox + .delete(&owner, WorkspaceId::new(), branch) + .await + .unwrap_err() + .code, + ErrorCode::NotFound + ); + assert_eq!( + sandbox + .delete(&owner, workspace, branch) + .await + .unwrap_err() + .code, + ErrorCode::Busy + ); + sandbox.stop(&owner, workspace, branch).await.unwrap(); + let mismatch = sandbox_delete( + State(services.clone()), + Extension(owner.clone()), + Path((workspace, branch)), + Json(SandboxDeleteRequest { + confirm_branch: BranchId::new(), + }), + ) + .await; + assert!(mismatch.is_err()); + assert!( + sandbox + .status(&owner, workspace, branch) + .await + .unwrap() + .is_some() + ); + let deleted = sandbox_delete( + State(services.clone()), + Extension(owner.clone()), + Path((workspace, branch)), + Json(SandboxDeleteRequest { + confirm_branch: branch, + }), + ) + .await + .map_err(|error| error.0) + .unwrap() + .0; + assert_eq!(deleted["deleted"], true); + assert!( + sandbox + .status(&owner, workspace, branch) + .await + .unwrap() + .is_none() + ); + sandbox.delete(&owner, workspace, branch).await.unwrap(); + assert_eq!( + local + .mounts() + .await + .unwrap() + .into_iter() + .find(|item| item.id == mount.id) + .unwrap() + .state, + MountState::Ready + ); + sandbox.create(&owner, workspace, branch).await.unwrap(); + sandbox.stop(&owner, workspace, branch).await.unwrap(); + let record = engine + .services + .mounts + .unmount( + RequestContext { + principal: owner, + request_id: OperationId::new().to_string(), + }, + mount.id, + mount.generation, + ) + .await + .unwrap(); + assert!(record.error.is_none(), "{record:?}"); +} + +async fn unmount(f: &Fixture) -> Result { + let mount = f + .local + .mounts() + .await? + .into_iter() + .find(|m| m.branch == f.branch) + .unwrap(); + f.engine + .services + .mounts + .unmount( + RequestContext { + principal: f.owner.clone(), + request_id: OperationId::new().to_string(), + }, + mount.id, + mount.generation, + ) + .await +} + +#[tokio::test] +async fn sandbox_restarts_with_restored_native_mount() { + let f = fixture().await; + f.sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap(); + let original = f.local.mounts().await.unwrap().remove(0); + f.sandbox + .stop(&f.owner, f.workspace, f.branch) + .await + .unwrap(); + f.engine.suspend_native_mounts().await.unwrap(); + assert_eq!( + f.local.mounts().await.unwrap()[0].state, + MountState::Suspended + ); + f.engine.flush_local().await.unwrap(); + f.engine.restore_native_mounts().await.unwrap(); + let restored = f.local.mounts().await.unwrap().remove(0); + assert_eq!(restored.state, MountState::Ready); + assert_eq!(restored.id, original.id); + assert_eq!(restored.path, original.path); + assert_eq!(restored.branch, original.branch); + f.sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap(); + match unmount(&f).await { + Err(error) => assert_eq!(error.code, ErrorCode::BranchBusy), + Ok(record) => assert_eq!(record.error.unwrap().code, ErrorCode::BranchBusy), + } + f.sandbox + .stop(&f.owner, f.workspace, f.branch) + .await + .unwrap(); + let record = unmount(&f).await.unwrap(); + assert!(record.error.is_none(), "{record:?}"); +} + +#[tokio::test] +async fn sandbox_worker_exit_releases_branch_without_explicit_stop() { + let f = fixture().await; + f.sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap(); + f.driver.stop(&f.branch.to_string()).await.unwrap(); + f.sandbox.reconcile_exited().await.unwrap(); + assert!(matches!( + f.sandbox + .status(&f.owner, f.workspace, f.branch) + .await + .unwrap() + .unwrap() + .state, + SandboxState::Stopped + )); + let record = unmount(&f).await.unwrap(); + assert!(record.error.is_none(), "{record:?}"); +} + +#[tokio::test] +async fn sandbox_force_stop_checks_confirmation_and_authority_then_releases_branch() { + let f = fixture().await; + f.sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap(); + f.driver + .fail_stop + .store(true, std::sync::atomic::Ordering::Relaxed); + assert!( + f.sandbox + .stop(&f.owner, f.workspace, f.branch) + .await + .is_err() + ); + let stranger: Principal = "stranger".to_owned().try_into().unwrap(); + assert_eq!( + f.sandbox + .force_stop(&stranger, f.workspace, f.branch) + .await + .unwrap_err() + .code, + ErrorCode::PermissionDenied + ); + let mut services = f.engine.services.clone(); + services.sandbox = Some(f.sandbox.clone()); + assert!( + sandbox_force_stop( + State(services.clone()), + Extension(f.owner.clone()), + Path((f.workspace, f.branch)), + Json(SandboxDeleteRequest { + confirm_branch: BranchId::new() + }) + ) + .await + .is_err() + ); + assert!(matches!( + f.driver.status(&f.branch.to_string()).await.unwrap().state, + SandboxState::Ready + )); + let Json(status) = sandbox_force_stop( + State(services), + Extension(f.owner.clone()), + Path((f.workspace, f.branch)), + Json(SandboxDeleteRequest { + confirm_branch: f.branch, + }), + ) + .await + .map_err(|e| e.0) + .unwrap(); + assert!(matches!(status.state, SandboxState::Stopped)); + let record = unmount(&f).await.unwrap(); + assert!(record.error.is_none(), "{record:?}"); +} + +#[tokio::test] +async fn sandbox_reconciliation_does_not_release_a_restarting_branch() { + let f = fixture().await; + f.sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap(); + f.driver.stop(&f.branch.to_string()).await.unwrap(); + f.sandbox.reconcile_exited().await.unwrap(); + let gate = Arc::new(tokio::sync::Notify::new()); + *f.driver.start_gate.lock().await = Some(gate.clone()); + let service = f.sandbox.clone(); + let owner = f.owner.clone(); + let workspace = f.workspace; + let branch = f.branch; + let starting = tokio::spawn(async move { service.create(&owner, workspace, branch).await }); + f.driver.starting.notified().await; + tokio::time::timeout( + std::time::Duration::from_secs(1), + f.sandbox.reconcile_exited(), + ) + .await + .unwrap() + .unwrap(); + gate.notify_one(); + starting.await.unwrap().unwrap(); + let error = match unmount(&f).await { + Ok(record) => record.error.unwrap(), + Err(error) => error, + }; + assert_eq!(error.code, ErrorCode::BranchBusy); + f.sandbox + .stop(&f.owner, f.workspace, f.branch) + .await + .unwrap(); +} diff --git a/crates/agentdrive-mcp/src/web.rs b/crates/agentdrive-mcp/src/web.rs index 347c35a..91bb958 100644 --- a/crates/agentdrive-mcp/src/web.rs +++ b/crates/agentdrive-mcp/src/web.rs @@ -20,6 +20,33 @@ mod merge_history_tests; pub(crate) fn api(services: Services) -> Router { Router::new() .merge(crate::collaboration::api()) + .route("/api/sandboxes", get(sandboxes)) + .route( + "/api/workspaces/{workspace}/branches/{branch}/sandbox/force-stop", + post(sandbox_force_stop), + ) + .route( + "/api/workspaces/{workspace}/branches/{branch}/sandbox/proxy", + get(sandbox_proxy_config).put(sandbox_set_proxy_config), + ) + .route( + "/api/workspaces/{workspace}/branches/{branch}/sandbox/delete", + post(sandbox_delete), + ) + .route( + "/api/workspaces/{workspace}/branches/{branch}/sandbox/desktop/{*path}", + get(sandbox_desktop), + ) + .route( + "/api/workspaces/{workspace}/branches/{branch}/sandbox/mcp-config", + get(sandbox_mcp_config), + ) + .route( + "/api/workspaces/{workspace}/branches/{branch}/sandbox", + get(sandbox_status) + .post(sandbox_create) + .delete(sandbox_stop), + ) .route("/api/workspaces", get(workspaces)) .route("/api/workspaces/{workspace}", get(workspace)) .route("/api/workspaces/{workspace}/archive", get(archive)) @@ -114,6 +141,10 @@ async fn update_settings( pub(crate) fn assets() -> Router { Router::new() + .route("/sandboxes", get(|| async { Html(include_str!("../dashboard/index.html")) })) + .route("/sandboxes.js", get(|| async { ([(header::CONTENT_TYPE, "text/javascript; charset=utf-8")], include_str!("../dashboard/sandboxes.js")) })) + .route("/sandboxes.css", get(|| async { ([(header::CONTENT_TYPE, "text/css; charset=utf-8")], include_str!("../dashboard/sandboxes.css")) })) + .route("/sandbox.js", get(|| async { ([(header::CONTENT_TYPE, "text/javascript; charset=utf-8")], include_str!("../dashboard/sandbox.js")) })) .route("/favicon.svg", get(|| async { ([(header::CONTENT_TYPE, "image/svg+xml")], include_str!("../dashboard/favicon.svg")) })) .route("/", get(|| async { Html(include_str!("../dashboard/index.html")) })) .route("/archive", get(|| async { Html(include_str!("../dashboard/index.html")) })) @@ -577,3 +608,258 @@ async fn lifecycle( }; Ok(Json(value)) } + +async fn sandboxes( + State(services): State, + Extension(actor): Extension, +) -> std::result::Result, ApiError> { + let workspaces = services.workspace.list(&actor).await?; + let mut entries = Vec::new(); + let mut errors = Vec::new(); + let mut template = None; + let mut templates = Vec::new(); + if let Some(runtime) = &services.sandbox { + match runtime.templates(&actor).await { + Ok(value) => templates = value, + Err(error) => errors.push(error.message), + } + match runtime.template(&actor).await { + Ok(value) => template = value, + Err(error) => errors.push(format!("模板信息:{}", error.message)), + } + for workspace in workspaces { + let status = match services.workspace.status(&actor, workspace.id).await { + Ok(value) => value, + Err(error) => { + errors.push(format!("{}:{}", workspace.name, error.message)); + continue; + } + }; + for branch in status.branches { + if branch.state == BranchState::Deleted { + continue; + } + match runtime.status(&actor, workspace.id, branch.id).await { + Ok(sandbox) => entries.push(json!({ + "workspace":workspace.id,"workspace_name":workspace.name, + "branch":branch.id,"branch_name":branch.name,"sandbox":sandbox, + "can_start":branch.state == BranchState::Writable && branch.owner == status.location, + })), + Err(error) if error.code == ErrorCode::PermissionDenied => {}, + Err(error) => errors.push(format!("{} / {}:{}", workspace.name, branch.name, error.message)), + } + } + } + } + Ok(Json( + json!({"enabled":services.sandbox.is_some(),"entries":entries,"template":template,"templates":templates,"errors":errors}), + )) +} + +async fn sandbox_mcp_config( + State(services): State, + Extension(actor): Extension, + Path((workspace, branch)): Path<(WorkspaceId, BranchId)>, +) -> std::result::Result, ApiError> { + let runtime = services + .sandbox + .ok_or_else(|| Error::new(ErrorCode::Unavailable, "Sandbox runtime is not configured"))?; + Ok(Json(runtime.mcp_config(&actor, workspace, branch).await?)) +} + +async fn sandbox_status( + State(services): State, + Extension(actor): Extension, + Path((workspace, branch)): Path<(WorkspaceId, BranchId)>, +) -> std::result::Result, ApiError> { + services.workspace.status(&actor, workspace).await?; + match services.sandbox { + Some(runtime) => Ok(Json( + json!({"enabled":true,"sandbox":runtime.status(&actor, workspace, branch).await?}), + )), + None => Ok(Json(json!({"enabled":false,"sandbox":null}))), + } +} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct SandboxCreateRequest { + template_id: Option, +} + +async fn sandbox_create( + State(services): State, + Extension(actor): Extension, + Path((workspace, branch)): Path<(WorkspaceId, BranchId)>, + request: Option>, +) -> std::result::Result, ApiError> { + let runtime = services + .sandbox + .ok_or_else(|| Error::new(ErrorCode::Unavailable, "Sandbox runtime is not configured"))?; + Ok(Json( + runtime + .create_with_template( + &actor, + workspace, + branch, + request.and_then(|request| request.0.template_id), + ) + .await?, + )) +} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct SandboxDeleteRequest { + confirm_branch: BranchId, +} + +async fn sandbox_delete( + State(services): State, + Extension(actor): Extension, + Path((workspace, branch)): Path<(WorkspaceId, BranchId)>, + Json(request): Json, +) -> std::result::Result, ApiError> { + if request.confirm_branch != branch { + return Err(Error::invalid("删除确认与目标分支不一致").into()); + } + let runtime = services + .sandbox + .ok_or_else(|| Error::new(ErrorCode::Unavailable, "Sandbox runtime is not configured"))?; + runtime.delete(&actor, workspace, branch).await?; + Ok(Json( + json!({"deleted":true,"workspace":workspace,"branch":branch}), + )) +} + +async fn sandbox_stop( + State(services): State, + Extension(actor): Extension, + Path((workspace, branch)): Path<(WorkspaceId, BranchId)>, +) -> std::result::Result, ApiError> { + let runtime = services + .sandbox + .ok_or_else(|| Error::new(ErrorCode::Unavailable, "Sandbox runtime is not configured"))?; + Ok(Json(runtime.stop(&actor, workspace, branch).await?)) +} + +async fn sandbox_force_stop( + State(services): State, + Extension(actor): Extension, + Path((workspace, branch)): Path<(WorkspaceId, BranchId)>, + Json(request): Json, +) -> std::result::Result, ApiError> { + if request.confirm_branch != branch { + return Err(Error::invalid("强制停止确认与目标分支不一致").into()); + } + let runtime = services + .sandbox + .ok_or_else(|| Error::new(ErrorCode::Unavailable, "Sandbox 尚未配置"))?; + Ok(Json(runtime.force_stop(&actor, workspace, branch).await?)) +} + +async fn sandbox_desktop( + State(services): State, + Extension(actor): Extension, + Path((workspace, branch, path)): Path<(WorkspaceId, BranchId, String)>, + mut request: axum::extract::Request, +) -> std::result::Result { + let runtime = services + .sandbox + .ok_or_else(|| Error::new(ErrorCode::Unavailable, "Sandbox runtime is not configured"))?; + if path != "websockify" { + let content = runtime + .desktop_asset(&actor, workspace, branch, &path) + .await?; + let mime = match path.rsplit('.').next().unwrap_or("") { + "html" => "text/html; charset=utf-8", + "js" | "mjs" => "text/javascript; charset=utf-8", + "css" => "text/css; charset=utf-8", + "svg" => "image/svg+xml", + "png" => "image/png", + "ico" => "image/x-icon", + "json" => "application/json", + "woff" => "font/woff", + "woff2" => "font/woff2", + _ => "application/octet-stream", + }; + return Ok(( + [ + (header::CONTENT_TYPE, mime), + (header::X_CONTENT_TYPE_OPTIONS, "nosniff"), + ], + content, + ) + .into_response()); + } + if !request + .headers() + .get(header::UPGRADE) + .is_some_and(|value| value.as_bytes().eq_ignore_ascii_case(b"websocket")) + { + return Err(Error::invalid("Desktop connection requires WebSocket").into()); + } + request.headers_mut().remove(header::COOKIE); + request.headers_mut().remove(header::AUTHORIZATION); + let stream = runtime.desktop(&actor, workspace, branch).await?; + let client_upgrade = hyper::upgrade::on(&mut request); + let query = request + .uri() + .query() + .map(|q| format!("?{q}")) + .unwrap_or_default(); + *request.uri_mut() = format!("/{path}{query}") + .parse() + .map_err(|_| Error::invalid("Invalid desktop path"))?; + let (mut sender, connection) = + hyper::client::conn::http1::handshake(hyper_util::rt::TokioIo::new(stream)) + .await + .map_err(|error| Error::new(ErrorCode::Unavailable, error.to_string()))?; + tokio::spawn(async move { + let _ = connection.with_upgrades().await; + }); + let mut response = sender + .send_request(request) + .await + .map_err(|error| Error::new(ErrorCode::Unavailable, error.to_string()))?; + if response.status() == StatusCode::SWITCHING_PROTOCOLS { + let guest_upgrade = hyper::upgrade::on(&mut response); + tokio::spawn(async move { + if let (Ok(client), Ok(guest)) = tokio::join!(client_upgrade, guest_upgrade) { + let _ = tokio::io::copy_bidirectional( + &mut hyper_util::rt::TokioIo::new(client), + &mut hyper_util::rt::TokioIo::new(guest), + ) + .await; + } + }); + } + Ok(response.map(axum::body::Body::new)) +} + +#[cfg(test)] +#[path = "sandbox_tests.rs"] +mod sandbox_tests; + +async fn sandbox_proxy_config( + State(services): State, + Extension(actor): Extension, + Path((workspace, branch)): Path<(WorkspaceId, BranchId)>, +) -> std::result::Result, ApiError> { + let runtime = services + .sandbox + .ok_or_else(|| Error::new(ErrorCode::Unavailable, "Sandbox 尚未配置"))?; + Ok(Json(runtime.proxy_config(&actor, workspace, branch).await?)) +} +async fn sandbox_set_proxy_config( + State(services): State, + Extension(actor): Extension, + Path((workspace, branch)): Path<(WorkspaceId, BranchId)>, + Json(config): Json, +) -> std::result::Result, ApiError> { + let runtime = services + .sandbox + .ok_or_else(|| Error::new(ErrorCode::Unavailable, "Sandbox 尚未配置"))?; + runtime + .set_proxy_config(&actor, workspace, branch, config) + .await?; + Ok(Json(json!({"saved":true,"restart_required":true}))) +} diff --git a/crates/agentdrive-ports/src/api.rs b/crates/agentdrive-ports/src/api.rs index 05a4ba3..595bdfb 100644 --- a/crates/agentdrive-ports/src/api.rs +++ b/crates/agentdrive-ports/src/api.rs @@ -516,6 +516,7 @@ pub struct ReadBranchFile { #[derive(Clone)] pub struct Services { + pub sandbox: Option>, pub collaboration: Arc, pub workspace: Arc, pub revisions: Arc, diff --git a/crates/agentdrive-ports/src/lib.rs b/crates/agentdrive-ports/src/lib.rs index 70fdbcd..b7e7919 100644 --- a/crates/agentdrive-ports/src/lib.rs +++ b/crates/agentdrive-ports/src/lib.rs @@ -21,3 +21,6 @@ pub use relationships::*; pub use services::*; pub use storage::*; pub use synchronization::*; + +mod sandbox; +pub use sandbox::*; diff --git a/crates/agentdrive-ports/src/sandbox.rs b/crates/agentdrive-ports/src/sandbox.rs new file mode 100644 index 0000000..8dfce85 --- /dev/null +++ b/crates/agentdrive-ports/src/sandbox.rs @@ -0,0 +1,205 @@ +use agentdrive_model::{BranchId, MountBinding, Principal, Result, WorkspaceId}; +use async_trait::async_trait; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum SandboxState { + Creating, + Starting, + Ready, + Stopping, + Stopped, + Error, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SandboxStatus { + #[serde(default = "default_sandbox_template")] + pub template_id: String, + #[serde(default)] + pub services: Vec, + pub id: String, + pub workspace: WorkspaceId, + pub branch: BranchId, + pub state: SandboxState, + pub desktop_url: Option, + pub message: Option, + #[serde(default)] + pub startup_step: Option, +} + +#[derive(Clone, Debug)] +pub struct SandboxLaunch { + pub template_id: Option, + pub id: String, + pub binding: MountBinding, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SandboxMcpServer { + pub command: String, + pub args: Vec, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SandboxMcpConfig { + #[serde(rename = "mcpServers")] + pub mcp_servers: std::collections::BTreeMap, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SandboxTemplateManifest { + pub name: String, + pub os: String, + pub desktop: String, + pub browser: String, + pub tools: Vec, + pub fonts: Vec, + pub locale: String, + pub workspace_path: String, + pub built_at: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SandboxTemplate { + pub id: String, + pub version: String, + pub capabilities: Vec, + pub manifest: Option, + pub vcpus: u32, + pub memory_gib: u32, + pub disk_gib: u32, + pub max_instances: usize, + pub network_slots: usize, +} + +pub fn default_sandbox_template() -> String { + "ubuntu-desktop".into() +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SandboxServiceLink { + pub id: String, + pub label: String, +} + +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct SandboxProxyConfig { + pub url: Option, +} + +/// Runtime adapters must export only the supplied binding and retain its identity. +#[async_trait] +pub trait SandboxRuntime: Send + Sync { + async fn proxy_config(&self, _id: &str) -> Result { + Ok(SandboxProxyConfig::default()) + } + async fn set_proxy_config(&self, _id: &str, _config: SandboxProxyConfig) -> Result<()> { + Err(agentdrive_model::Error::invalid("当前模板不支持代理配置")) + } + async fn template(&self) -> Result> { + Ok(None) + } + async fn templates(&self) -> Result> { + Ok(self.template().await?.into_iter().collect()) + } + async fn mcp_config(&self, id: &str) -> Result; + + async fn report_progress(&self, _status: SandboxStatus) {} + async fn desktop_asset(&self, path: &str) -> Result; + async fn connect(&self, id: &str, port: u32) -> Result; + async fn start(&self, request: SandboxLaunch) -> Result; + async fn stop(&self, id: &str) -> Result<()>; + async fn force_stop(&self, id: &str) -> Result<()>; + async fn delete(&self, id: &str) -> Result<()>; + async fn status(&self, id: &str) -> Result; +} + +#[async_trait] +pub trait SandboxApi: Send + Sync { + async fn proxy_config( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result; + async fn set_proxy_config( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + config: SandboxProxyConfig, + ) -> Result<()>; + async fn delete( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result<()>; + async fn template(&self, actor: &Principal) -> Result>; + async fn templates(&self, actor: &Principal) -> Result>; + async fn service( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + service: &str, + ) -> Result; + async fn create_with_template( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + template_id: Option, + ) -> Result; + async fn mcp_config( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result; + + async fn desktop_asset( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + path: &str, + ) -> Result; + async fn desktop( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result; + async fn create( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result; + async fn status( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result>; + async fn stop( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result; + async fn force_stop( + &self, + actor: &Principal, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result; +} + +pub trait SandboxStream: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + Unpin {} +impl SandboxStream for T {} +pub type SandboxConnection = Box; diff --git a/docs/sandbox.md b/docs/sandbox.md new file mode 100644 index 0000000..9794ec8 --- /dev/null +++ b/docs/sandbox.md @@ -0,0 +1,241 @@ +# 分支 Ubuntu Sandbox + +分支管理页面提供创建、打开桌面、停止和重新启动功能。每个分支使用独立 Ubuntu 系统磁盘,`/workspace` 通过 virtio-fs 连接 AgentDrive 的可写 FUSE 挂载。实例由 agentdrived 的内部 worker 管理,worker 链接 CubeSandbox hypervisor。 + +正常停止会等待 guest 同步文件。同步失败时实例继续运行,页面显示原因,并提供「强制停止」;输入分支名称确认后会终止进程,尚未同步的修改可能丢失。进程退出等待有超时限制。worker 异常退出后,后台检查会释放分支保护,允许卸载或切换分支;重新启动期间仍保留分支保护。 + +CSGClaw 的登录回调使用当前实例的 `csgclaw.<工作区>.<分支>.localhost:` 地址。AgentDrive 写入实例的助手启动信息,启动脚本通过 CSGClaw 的 `--endpoint` 参数传入地址;已有实例的旧回调配置会被启动参数覆盖。更换 AgentDrive 端口后重新启动 Sandbox 即可更新。STG 登录沿用相同代理入口。 + +运行主机需要 Linux x86_64、已启用的 KVM 或 PVM、FUSE、至少 9 GiB 可用内存,以及可用的 Docker 镜像构建环境。每个实例使用 4 vCPU、8 GiB 内存和 16 GiB 按需分配的系统磁盘。默认同时运行两个实例,可以通过 `max_instances` 调整数量。 + +## 桌面使用 + +在分支页面点击「Sandbox」按钮,在弹框中点击「启动 Sandbox」。启动完成后点击「打开桌面」。弹框集中展示实例状态、启动与停止操作、启动步骤、MCP 配置及使用说明;配置可以直接复制或下载为 JSON。左侧应用栏提供文件管理器、Terminal 和 Chromium。浏览器命令为 `chromium`;Terminal 的命令为 `gnome-terminal`。也可以在应用菜单中搜索应用名称。 + +当前分支自动挂载到 Ubuntu 的 `/workspace`。桌面上的 `AgentDrive` 文件夹入口指向该目录。在终端执行 `cd /workspace` 即可操作当前分支文件。`/home/user` 是 Ubuntu 的个人目录,需要进入分支的文件应保存到 `/workspace`。实例始终绑定创建时的分支,在网页选择其他分支不会改变已有实例的挂载。 + +## Agent 与 MCP + +模板安装 `xdg-utils`,桌面启动时为用户配置 Chromium 为 HTTP、HTTPS 和 HTML 的默认应用。在桌面终端执行 `xdg-open https://example.com` 即可打开网页。 + +Chromium 来自官方 Linux 构建,版本与 SHA-256 记录在 `build/sandbox/chromium.json`,构建模板时校验下载内容。该构建需要随模板更新。程序以桌面用户身份运行,并保留 Chromium 自身的进程隔离。模板包含 `fonts-noto-cjk` 和 `fonts-noto-color-emoji`,支持中文与表情显示。 + +当前桌面会话通过脚本启动,Chromium 使用 `basic` 凭据存储,以便直接启动浏览器。该模式没有系统密钥环提供的独立凭据加密;实例系统磁盘需要按用户数据保护。 + +中文和表情输入使用桌面 MCP 的 `paste_text`:通过 `get_app_state` 和 `click` 选中输入框,使用 `list_windows` 返回的 `window_id`,然后传入文本。此工具更新 Sandbox 剪贴板,由 computer-use-linux 确认窗口焦点后发送粘贴快捷键。输入完成后读取输入框核对内容。`Ctrl+T` 可以创建标签页,网页按钮可以通过元素信息或坐标点击。 + +桌面 MCP 初始化响应,以及 `list_apps`、`get_app_state` 的工具描述,会附带当前实例检查到的浏览器命令、URL 入口、分支挂载和网络配置。客户端读取工具列表即可获得这些信息。`list_apps` 返回运行中的应用;Agent 根据环境信息打开浏览器,其他已安装应用通过桌面应用菜单查找,操作后检查窗口、页面内容和截图。 + +启动页面显示准备分支目录、准备系统磁盘、启动 Ubuntu、等待桌面四个步骤。就绪检查确认 `/workspace` 挂载、窗口管理器和 VNC 连接;浏览器、字体和工具由模板构建及端到端测试验证。启动失败时,页面显示最近一次检查的原因。 + +Sandbox 对外提供一个统一 MCP 入口,内部运行 computer-use-linux 和 Playwright MCP。沿用 `ubuntu_desktop` 配置名称,现有客户端无需增加条目。当前提供 20 个桌面工具、25 个浏览器工具以及 `sandbox_tools_status`,总计 46 个工具。 + +- `browser_*`:网页导航、标签页、页面快照、点击、填写表单和下载。 +- 原有桌面工具:截图、键盘鼠标、终端和系统窗口,名称保持不变。 +- `sandbox_tools_status`:检查内部服务连接状态和工具数量。 + +宿主机只需要以下一份 stdio MCP 配置。页面「MCP 配置」可以直接复制本分支的实际配置: + +```json +{ + "mcpServers": { + "ubuntu_desktop": { + "command": "python3", + "args": [ + "/absolute/path/csgdrive/build/sandbox/mcp-proxy.py", + "--socket", + "/absolute/path/agentdrive/sandboxes/BRANCH_ID/vsock.sock" + ] + } + } +} +``` + +`mcp-proxy.py` 将标准输入输出连接到实例的 vsock 端口 7000,再由 `gateway.mjs` 分发工具请求。需要在运行 AgentDrive 的同一台主机、同一用户下执行。Sandbox 必须已启动;更新或重新启动实例后,在 Agent 客户端重新连接这一份 MCP 配置即可刷新工具列表。模型账号与 API Key 由 Agent 客户端管理。 + +Chromium 在 Ubuntu 的 `DISPLAY=:0` 运行,用户目录固定为 `/home/user/.config/agentdrive-chromium`。首次更新时复制已有 Chromium 用户目录,保留原目录。桌面图标、`chromium`、`xdg-open` 和 Playwright MCP 共用这个浏览器。调试接口仅监听 guest 内的 `127.0.0.1:9222`,Agent 通过统一 MCP 入口操作浏览器。MCP 断开会释放工具子进程,保留 Chromium 与已打开标签页。 + +浏览器默认下载目录为 `/workspace/Downloads`,Playwright 输出目录也设置在此处,下载内容会直接进入当前分支。输出文件不启用自动删除。Playwright 操作时网页会在 VNC 中变化;系统鼠标指针移动由桌面工具控制。多个客户端共享同一桌面,操作时需要协调输入。 + +可以给 Agent 下达:「打开 Python 官网,新建标签页,点击指定按钮,并将下载文件保存到当前分支。」安装在 Ubuntu 内的 Agent 可以直接启动 `/usr/local/bin/node /opt/agentdrive-mcp/gateway.mjs` 连接相同工具入口。 + +内部服务在 `build/sandbox/mcp/providers.json` 注册。新增服务时填写 `id`、`command`、`args`,用 `prefix` 区分工具名称,再安装对应程序并构建模板。Agent 的配置保持不变,重新连接后获得新的工具列表。工具名称冲突会明确报错,单个服务断开时其他服务仍可使用。当前统一入口合并 MCP tools,转发原始参数、结构化结果、图片和工具错误,支持分页工具列表、请求取消及连接清理。 + +## 构建 + +安装 Rust,以及发行版的 `libseccomp-dev`、`libcap-ng-dev`。构建命令: + +```bash +cargo build --release -p agentdrived -p agentdrive +cargo build --release --locked --manifest-path runtime/sandbox-worker/Cargo.toml +``` + +worker 使用 CubeSandbox v0.7.3 对应 commit,并固定 linux-loader 0.13.0。Ubuntu 模板安装 computer-use-linux 0.7.53,下载文件使用 SHA-256 验证。Playwright MCP 固定为 0.0.85,MCP SDK 固定为 1.32.2;Node.js 构建镜像固定 digest,npm 依赖通过 package-lock.json 和 npm ci 安装。 + +准备 CubeSandbox v0.7.3 的兼容 `vmlinux`,使用下列命令构建模板。输出目录需要尚未存在: + +```bash +build/sandbox/build-image.sh /absolute/path/template /absolute/path/vmlinux +``` + +桌面构建文件参考 [CubeSandbox ubuntu-desktop](https://github.com/TencentCloud/CubeSandbox/tree/v0.7.3/examples/ubuntu-desktop)。系统磁盘直接启动 guest-init.sh,guest helper 通过 vsock 提供桌面连接、截图检查、文件刷新和 MCP 标准输入输出。 + +## 启动配置 + +每次启动 Sandbox 时,AgentDrive 检查宿主机已经加载的内核模块:存在 `kvm_pvm` 时使用 PVM,否则在存在 `kvm` 时使用 KVM。宿主机需要预先启用对应模块;程序不会安装宿主机内核或重新启动电脑。 + +内核选择使用配置中 `kernel` 所在目录的 `vmlinux-bm`(KVM)或 `vmlinux-pvm`(PVM)。KVM 兼容旧配置中的内核文件;PVM 必须提供 `vmlinux-pvm`,缺少文件时启动会返回明确错误。内核目录应当提供与 worker 兼容的两个版本。构建脚本自动复制输入内核同目录的 `vmlinux-pvm`,也可以使用第三个参数指定 PVM 内核文件。 + +已有 Sandbox 保留系统盘、模板版本、资源和助手数据。启动时按照模板 ID 查找当前部署配置中的内核位置,再根据宿主机模式选择内核;已保存的 `template.json` 中的内核路径不会限制模式切换。已经移除的模板使用保存路径所在目录查找内核。运行中的 Sandbox 在下次启动时应用变化。实际使用的内核记录在实例 `vm.json` 的 `payload.kernel` 和服务日志中。 + +创建配置文件,全部路径使用绝对路径: + +```json +{ + "worker": "/absolute/path/csgdrive/runtime/sandbox-worker/target/release/agentdrive-sandbox-worker", + "kernel": "/absolute/path/template/vmlinux", + "image": "/absolute/path/template/ubuntu.ext4", + "directory": "/absolute/path/agentdrive/sandboxes", + "mount_root": "/absolute/path/agentdrive/mounts", + "novnc_dir": "/absolute/path/template/novnc", + "mcp_proxy": "/absolute/path/csgdrive/build/sandbox/mcp-proxy.py", + "max_instances": 2, + "networks": [ + {"tap": "adsb0", "guest_ip": "172.30.80.2", "gateway": "172.30.80.1", "dns": ["223.5.5.5", "1.1.1.1"]}, + {"tap": "adsb1", "guest_ip": "172.30.80.6", "gateway": "172.30.80.5", "dns": ["223.5.5.5", "1.1.1.1"]} + ] +} +``` + +准备外网接口,参数使用运行 agentdrived 的用户 UID: + +```bash +sudo bash build/sandbox/prepare-network.sh "$(id -u)" +``` + +脚本创建 `adsb0` 和 `adsb1`,使用 `172.30.80.0/29` 地址范围。运行前应确认该范围未被其他网络使用。每个运行中的实例占用一个接口,停止后释放接口。接口由普通用户打开,网络参数通过内核命令行传给 Ubuntu,启动时配置地址、默认路由和 DNS。省略 `networks` 时实例保持离线;配置了网络但接口不可用时,页面显示启动错误。 + +转发规则允许实例访问公网及接收响应,阻止访问宿主机、私有网络和其他实例。主机重新启动后需要重新运行准备脚本,或者将脚本配置为 systemd 启动服务。新建实例使用当前模板;已有系统磁盘需要在停止状态下更新所需程序和配置。 + +`mount_root` 必须位于 AgentDrive 配置的 `mount_roots` 内。运行用户需要访问 `/dev/kvm` 与 `/dev/fuse`。 + +```bash +AGENTDRIVE_SANDBOX_CONFIG=/absolute/path/sandbox.json \ + target/release/agentdrived serve --config /absolute/path/agentdrive.json +``` + +打开分支管理页面,点击所需分支的「Sandbox」,在弹框中点击「启动 Sandbox」。启动步骤依次展示准备当前分支目录、准备 Ubuntu 系统磁盘、启动 Ubuntu 实例、等待桌面启动、检查浏览器、字体和桌面工具。进度来自后台实际状态,启动期间持续更新;失败时保留具体步骤和原因,并提供重试入口。实例经过窗口管理器、桌面连接和真实截图检查后显示「打开桌面」。页面切换分支不会改变实例绑定。浏览器使用宿主机上的 noVNC 静态文件,guest 仅提供桌面 WebSocket 数据。 + +运行中的 Sandbox 会阻止关联挂载被卸载、重新绑定或通过版本恢复替换。停止操作等待 guest 刷新文件,停止 worker,并调用 AgentDrive 保存流程。停止后系统磁盘继续保留。守护进程重新启动后,已有实例显示停止状态,可继续使用原系统磁盘启动。 + +## 验证 + +以下脚本通过 guest 内的 computer-use-linux MCP 打开 gedit,输入验证内容,通过 Ctrl+S 保存,然后从宿主机分支挂载读取文件并比对内容。脚本创建独立文件名,保存截图和 MCP 调用记录: + +```bash +python3 tests/sandbox/desktop_probe.py \ + --socket /absolute/path/sandboxes/BRANCH_ID/vsock.sock \ + --mount /absolute/path/agentdrive/mounts/BRANCH_ID \ + --artifacts /absolute/path/verification +``` + +## 当前支持范围 + +已接入本地分支创建、桌面访问、文件共享、停止与重新启动。远端分支需要已经取得本机写入权限。实例通过 vsock 连接宿主机,通过独立 TAP 网卡和 NAT 访问外网。Ubuntu 模板包含 Chromium 浏览器。宿主机 MCP 客户端通过 `mcp-proxy.py` 连接内部 vsock 端口 7000。用户接管输入控制和跨主机迁移尚未提供。 + +实例状态保存在各实例目录,操作执行期间由 agentdrived 管理。当前创建请求等待启动结果返回。Ubuntu 图形应用中尚未保存的编辑内容需要用户保存后再停止实例。 + +完整浏览器端到端测试会创建独立工作区,通过页面创建分支和实例,验证桌面、MCP 文件保存、分支隔离及停止后重新启动。启用 `networks` 时,还会通过 computer-use-linux 验证首次启动和重新启动后的 HTTPS 网页访问。测试结束后停止测试实例,保留验证文件和记录: + +```bash +node tests/sandbox/e2e.cjs +``` + +默认连接 `http://127.0.0.1:17421`,可以使用 `AGENTDRIVE_E2E_URL` 和 `AGENTDRIVE_SANDBOX_CONFIG` 指定测试环境。 + +通过 computer-use-linux 操作浏览器地址栏、读取网页内容并保存截图: + +```bash +python3 tests/sandbox/network_probe.py \ + --socket /absolute/path/sandboxes/BRANCH_ID/vsock.sock \ + --artifacts /absolute/path/network-verification +``` + +## 沙盒管理页面 + +侧边栏「沙盒」打开 `/sandboxes`,显示当前用户可以访问的所有工作区的本机沙盒。支持工作区、状态和名称筛选,提供启动、停止、打开桌面,以及启动步骤与 MCP 配置。点击「创建沙盒」打开弹框,选择工作区和尚未创建沙盒的可写分支,点击「创建并启动」后直接显示启动进度。弹框会提示没有可用分支或运行实例达到上限的情况。 + +「模板内容」显示当前配置模板的系统、桌面、浏览器、字体、工具版本及资源配置。构建镜像时从已安装的软件生成清单,`build-image.sh` 将清单输出到系统镜像旁边的 `ubuntu.template.json`。运行时按照镜像文件名读取同目录的 `.template.json` 文件;没有清单时,页面明确提示详情尚未提供。已有实例继续使用原系统磁盘,模板清单描述新建实例使用的模板。 + +`GET /api/sandboxes` 返回实例目录与模板信息,使用现有工作区和分支权限检查。模板清单不包含宿主机文件路径或 MCP 配置。 + +## 统一 MCP 端到端验证 + +验证脚本从 AgentDrive API 获取单个 MCP 配置,通过同一连接调用浏览器和桌面工具。检查外网网页、新标签页、页面按钮、VNC 桌面截图、下载文件内容以及 MCP 重连后的标签页。测试页面使用唯一文件名,结束后删除页面文件,下载证明保留在当前分支的 Downloads 目录。 + +```bash +python3 tests/sandbox/playwright_probe.py \ + --workspace WORKSPACE_ID --branch BRANCH_ID \ + --mount /absolute/path/agentdrive/mounts/BRANCH_ID \ + --artifacts /absolute/path/verification +``` + +统一入口和通信通道的自动化测试: + +```bash +npm ci --prefix build/sandbox/mcp --ignore-scripts +node --test tests/sandbox/mcp-gateway.test.mjs +python3 -m unittest discover -s tests/sandbox -p 'test_*.py' +``` + +### 删除 Sandbox + +沙盒页面的实例卡片和分支页面的 Sandbox 弹框均提供“删除 Sandbox”。运行中的实例需要先停止;已停止或启动失败的实例可以删除。确认弹框展示工作区、分支和删除范围,并要求输入完整分支名称。 + +删除会清除该实例的 Ubuntu 系统磁盘、安装的软件、浏览器登录状态及实例日志。AgentDrive 分支、版本历史、挂载目录以及 `/workspace` 中的文件(包括 `Downloads`)全部保留。后续启动使用当前模板创建新的系统磁盘。 + +接口为 `POST /api/workspaces/{workspace}/branches/{branch}/sandbox/delete`,JSON 请求体为 `{"confirm_branch":"分支 UUID"}`。接口检查管理权限和分支归属,拒绝删除正在运行或正在启动的实例,并在删除前保存仍然挂载的分支文件。重复删除同一实例可以安全重试。原有 `DELETE /api/workspaces/{workspace}/branches/{branch}/sandbox` 仍用于停止实例。 + +### 启动就绪检查 + +启动页面展示四个步骤:准备分支目录、准备 Ubuntu 系统磁盘、启动 Ubuntu 实例、等待桌面启动。就绪检查只确认 GNOME 会话、窗口管理器、VNC 和 noVNC 服务以及 `/workspace` 挂载。启动期间跳过浏览器、字体和 MCP 软件检查,也不执行截图或预先打开 Chromium。桌面辅助功能在模板中默认启用,Playwright 连接时按需打开共用的 Chromium。软件功能通过模板验证脚本单独测试。 + + +## Ubuntu GNOME 与 CSGClaw + +`ubuntu-csgclaw` 模板在现有 GNOME 镜像中预装 CSGClaw v0.10.0 完整发布包,包括 Web UI 与 Agent 执行程序。发布包 SHA-256 在构建脚本中校验,模型凭据不写入模板。需要已经构建的 `agentdrive-ubuntu:local` 基础镜像: + +```bash +bash build/sandbox/build-assistant-image.sh /absolute/path/ubuntu-csgclaw /absolute/path/vmlinux +``` + +在 `AGENTDRIVE_SANDBOX_CONFIG` 文件中添加以下 `templates` 条目,重新启动 AgentDrive: + +```json +{ + "templates": [{ + "id": "ubuntu-csgclaw", + "version": "csgclaw-v0.10.0-gnome-1", + "profile": "ubuntu-csgclaw", + "image": "/absolute/path/ubuntu-csgclaw/ubuntu.ext4", + "kernel": "/absolute/path/ubuntu-csgclaw/vmlinux", + "vcpus": 4, + "memory_gib": 8, + "disk_gib": 16 + }] +} +``` + +在「模板内容」选择 Ubuntu Desktop + CSGClaw,或在创建沙盒时选择该模板。每个实例记录创建时的模板及版本,重新启动使用原系统磁盘。现有普通 Ubuntu 实例继续使用原模板;可以在新分支创建带助手的沙盒。 + +「打开助手」进入 CSGClaw 完整 Web UI 的新建任务页面,并选中 `agentdrive`。助手工作目录为 `/workspace`,默认接入一份 `ubuntu_desktop` MCP,包含桌面与浏览器工具。无需打开 VNC,聊天即可操作当前分支文件和 Chromium。首次使用需要在 CSGClaw 的模型设置中配置可用模型;程序会在模型配置完成后自动创建分支助手及聊天身份。模型配置、助手与聊天记录保存在实例目录旁的 `assistant-state/BRANCH_ID/csgclaw` 中,独立于分支内容和系统磁盘。 + +Web UI 使用 `csgclaw.WORKSPACE_ID.BRANCH_ID.localhost:AGENTDRIVE_PORT` 地址。AgentDrive 校验分支权限和请求来源,通过 vsock 转发 HTTP、聊天流和 WebSocket。每个分支使用独立浏览器来源。当前入口供同一台主机使用,Ubuntu 的助手端口保持监听 `127.0.0.1:18080`。 + +## 分支代理配置 + +沙盒详情中的「代理配置」接受 HTTP 代理地址,支持通过 CONNECT 访问 HTTPS。地址由宿主机解析,例如 `http://127.0.0.1:7890` 可以使用宿主机上的代理。代理地址需要不含账号、密码、路径和查询参数;留空表示直接联网。保存后在下次启动沙盒时生效,正在进行的任务不会被自动中断。 + +AgentDrive 通过实例专用 vsock 端口 18081 转发到配置的代理。Ubuntu 中的代理入口为 `127.0.0.1:18081`;CSGClaw 继承 `HTTP_PROXY`、`HTTPS_PROXY`,Chromium 使用 `--proxy-server`。本地 CSGClaw、MCP 和 CDP 连接保持使用回环地址。代理设置存放在宿主机 `proxy-settings/BRANCH_ID.json`,该目录不共享给 Ubuntu。停止沙盒会终止对应的代理转发任务。 + +删除沙盒保留 `/workspace` 分支文件、助手状态和代理设置;同一分支重新创建带助手的沙盒后可继续使用。助手状态包含模型凭据及聊天内容,管理这些数据时应与本机账号数据采用相同的访问权限。 diff --git a/runtime/sandbox-worker/.gitignore b/runtime/sandbox-worker/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/runtime/sandbox-worker/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/runtime/sandbox-worker/Cargo.lock b/runtime/sandbox-worker/Cargo.lock new file mode 100644 index 0000000..71e0fba --- /dev/null +++ b/runtime/sandbox-worker/Cargo.lock @@ -0,0 +1,2507 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "acpi_tables" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "vm-memory 0.16.2", + "zerocopy 0.6.6", +] + +[[package]] +name = "addr2line" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e567177890eb1617b1f774005b66b26b2377afd138a2ca37aae7d8f0c81429d4" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "agentdrive-sandbox-worker" +version = "0.1.0" +dependencies = [ + "cube-hypervisor", + "linux-loader", + "serde_json", +] + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + +[[package]] +name = "android_system_properties" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" +dependencies = [ + "libc", +] + +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" + +[[package]] +name = "api_client" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "arc-swap" +version = "1.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b5d51209f6de83d37a0b33d320acb8da88823e42a94c075140708f032a5" +dependencies = [ + "rustversion", +] + +[[package]] +name = "arch" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "anyhow", + "byteorder", + "fdt", + "hypervisor", + "libc", + "linux-loader", + "log", + "logging", + "serde", + "serde_json", + "thiserror 1.0.69", + "uuid", + "vm-fdt", + "vm-memory 0.16.2", + "vm-migration", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "atomic-polyfill" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" +dependencies = [ + "critical-section", +] + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "backtrace" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f4220217f3288537b83684130f42fad43320b1c3b805f1910d683d52ded24f" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-link", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "block_util" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "io-uring", + "libc", + "log", + "qcow", + "serde", + "thiserror 1.0.69", + "vhdx", + "virtio-bindings 0.1.0", + "virtio-queue 0.14.0", + "vm-memory 0.16.2", + "vm-virtio", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "capng" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a26766f93f07f7e8b8309ed2824fa2a68f5d12d219de855e24688e9fbe89e85" +dependencies = [ + "bitflags 1.3.2", + "libc", +] + +[[package]] +name = "cc" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ff2a8126fff0f8c839080d619675a585e61720bb12d2baceae45769fd66f155" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cpufeatures", + "rand_core 0.10.1", +] + +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link", +] + +[[package]] +name = "clap" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", + "terminal_size", +] + +[[package]] +name = "clap_derive" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "clap_lex" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" + +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror 2.0.21", +] + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32c" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f132a176423ee852e8f0a35d43088d1850966b50745be818a88b76f64b1c6cb" +dependencies = [ + "rustc_version", +] + +[[package]] +name = "critical-section" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" + +[[package]] +name = "crossbeam-channel" +version = "0.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98b0cc327b5bc766e7fda9c9260cc0fa81b43a8e240440422dff70788e3f9ef1" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + +[[package]] +name = "cube-hypervisor" +version = "28.0.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "anyhow", + "api_client", + "arc-swap", + "chrono", + "clap", + "epoll", + "event_monitor", + "event_notifier", + "hypervisor", + "lazy_static", + "libc", + "log", + "log_json", + "logging", + "option_parser", + "rlimit", + "seccompiler", + "serde", + "serde_json", + "signal-hook", + "slog", + "slog-scope", + "thiserror 1.0.69", + "tpm", + "tracer", + "vmm", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "darling" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee" +dependencies = [ + "darling_core 0.20.11", + "darling_macro 0.20.11", +] + +[[package]] +name = "darling" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed17f5901b6630b993ca003def43f2f8ef4014fc13b047b57aad617ff32bc2ec" +dependencies = [ + "darling_core 0.24.1", + "darling_macro 0.24.1", +] + +[[package]] +name = "darling_core" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d00b9596d185e565c2207a0b01f8bd1a135483d02d9b7b0a54b11da8d53412e" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 2.0.119", +] + +[[package]] +name = "darling_core" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6837e2cf7485aaae18f86181d2f0e9a7ed297a025e220aeabf63fdebd3a2ddff" +dependencies = [ + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 3.0.10", +] + +[[package]] +name = "darling_macro" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" +dependencies = [ + "darling_core 0.20.11", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "darling_macro" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ac7135c3ef02b2f7833bbeb1be5ba7f966dcde8a87c6b87f65a778d71a02785" +dependencies = [ + "darling_core 0.24.1", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "deranged" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e" + +[[package]] +name = "devices" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "acpi_tables", + "anyhow", + "arch", + "bitflags 2.13.2", + "byteorder", + "event_monitor", + "event_notifier", + "hypervisor", + "libc", + "log", + "logging", + "pci", + "phf", + "serde", + "thiserror 1.0.69", + "tpm", + "vm-allocator", + "vm-device", + "vm-memory 0.16.2", + "vm-migration", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + +[[package]] +name = "env_logger" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "epoll" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e74d68fe2927dbf47aa976d14d93db9b23dced457c7bb2bdc6925a16d31b736e" +dependencies = [ + "bitflags 2.13.2", + "libc", +] + +[[package]] +name = "erased-serde" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c138974f9d5e7fe373eb04df7cae98833802ae4b11c24ac7039a21d5af4b26c" +dependencies = [ + "serde", +] + +[[package]] +name = "errno" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "error-chain" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" +dependencies = [ + "version_check", +] + +[[package]] +name = "event_monitor" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "libc", + "serde", + "serde_json", +] + +[[package]] +name = "event_notifier" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "lazy_static", + "log", +] + +[[package]] +name = "fdt" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784a4df722dc6267a04af36895398f59d21d07dce47232adf31ec0ff2fa45e67" + +[[package]] +name = "find-msvc-tools" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a31d2a3fbaaeb2af2368bbdd904aa8e812d3c04a1ee10d3171f52d556e5d0a3" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-executor" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "031b47cf1a3c6cc8bc2fc76cd437f521619387907d469316e7c0bc278f1f5432" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" + +[[package]] +name = "futures-macro" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "futures-sink" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "rand_core 0.10.1", +] + +[[package]] +name = "gimli" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1033caf0b349c518623b5396bfb2cf0bddf44f0306d543a250e5743297aafd10" + +[[package]] +name = "hash32" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" +dependencies = [ + "byteorder", +] + +[[package]] +name = "heapless" +version = "0.7.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" +dependencies = [ + "atomic-polyfill", + "hash32", + "rustc_version", + "serde", + "spin", + "stable_deref_trait", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284" + +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi", +] + +[[package]] +name = "humantime" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15cdd26707701c53297e2fa6afb323d55fbc1d0810c3aec078ae3ef0424c3c15" + +[[package]] +name = "hypervisor" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "anyhow", + "byteorder", + "iced-x86", + "kvm-bindings", + "kvm-ioctls", + "libc", + "log", + "serde", + "serde_with 2.3.3", + "thiserror 1.0.69", + "vfio-ioctls", + "vm-memory 0.14.1", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "iced-x86" +version = "1.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c23ad4d19dca41180e835ef6fbd2c1ee655777eca086ad80dd517e0513322879" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "io-uring" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd1e1a01cfb924fd8c5c43b6827965db394f5a3a16c599ce03452266e1cf984c" +dependencies = [ + "bitflags 1.3.2", + "libc", +] + +[[package]] +name = "is-terminal" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itoa" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" + +[[package]] +name = "js-sys" +version = "0.3.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7883d941dae510fb2d978fc3fe018c71c9e2892fd38854de3e8b92c2e5ad9cc5" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "kvm-bindings" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ac3147c9763fd8fa7865a90d6aee87f157b59167145b38e671bbc66b116f1e8" +dependencies = [ + "serde", + "vmm-sys-util 0.12.1", + "zerocopy 0.7.35", +] + +[[package]] +name = "kvm-ioctls" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bedae2ca4a531bebe311abaf9691f5cc14eaa21475243caa2e39c43bb872947d" +dependencies = [ + "bitflags 2.13.2", + "kvm-bindings", + "libc", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "lazy_static" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "libseccomp-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a7cbbd4ad467251987c6e5b47d53b11a5a05add08f2447a9e2d70aef1e0d138" + +[[package]] +name = "linux-loader" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "870c3814345f050991f99869417779f6062542bcf4ed81db7a1b926ad1306638" +dependencies = [ + "vm-memory 0.16.2", +] + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" + +[[package]] +name = "log_json" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "libc", + "serde", + "serde_json", +] + +[[package]] +name = "logging" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "lazy_static", + "serde", + "slog", + "slog-async", + "slog-term", +] + +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "micro_http" +version = "0.1.0" +source = "git+https://github.com/firecracker-microvm/micro-http?branch=main#7975fe0e59000e8c41cb1d58fbd39ffa102e5b27" +dependencies = [ + "libc", + "vmm-sys-util 0.15.0", +] + +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "net_gen" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "net_util" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "epoll", + "getrandom 0.2.17", + "libc", + "log", + "net_gen", + "rate_limiter", + "serde", + "serde_json", + "thiserror 1.0.69", + "virtio-bindings 0.1.0", + "virtio-queue 0.14.0", + "vm-memory 0.16.2", + "vm-virtio", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "num-conv" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_threads" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cabcc654086861f9e1bc5115258de62ef92c79b20f4615a1b0cc57c4e82027d4" +dependencies = [ + "libc", +] + +[[package]] +name = "object" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd229a0361b9d0d4396176e02d65897f487eebeab7caa6d443855ee152ca0b9c" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "option_parser" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "pci" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "anyhow", + "byteorder", + "hypervisor", + "libc", + "log", + "serde", + "thiserror 1.0.69", + "vfio-bindings 0.3.1", + "vfio-ioctls", + "vfio_user", + "vm-allocator", + "vm-device", + "vm-memory 0.16.2", + "vm-migration", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "phf" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078" +dependencies = [ + "phf_macros", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d" +dependencies = [ + "phf_shared", + "rand 0.8.8", +] + +[[package]] +name = "phf_macros" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f84ac04429c13a7ff43785d75ad27569f2951ce0ffd30a3321230db2fc727216" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "phf_shared" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" +dependencies = [ + "siphasher", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "heapless", + "serde", +] + +[[package]] +name = "powerfmt" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "qcow" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "byteorder", + "libc", + "log", + "remain", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +dependencies = [ + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rate_limiter" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "libc", + "log", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "remain" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b297d1baead74bf76361e49ed016060bd829633361b68500d2e7ea3e66ab6a4" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "rlimit" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8a29d87a652dc4d43c586328706bb5cdff211f3f39a530f240b53f7221dab8e" +dependencies = [ + "libc", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" +dependencies = [ + "bitflags 2.13.2", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "seccompiler" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f6575e3c2b3a0fe2ef3e53855b6a8dead7c29f783da5e123d378c8c6a89017e" +dependencies = [ + "libc", +] + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "sendfd" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8fa82bbd0ce6c16273ec6c310d3973f8b8f8665ecfe0ca02a49b3ea61b2418c" +dependencies = [ + "libc", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_with" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07ff71d2c147a7b57362cead5e22f772cd52f6ab31cfcd9edcd7f6aeb2a0afbe" +dependencies = [ + "serde", + "serde_with_macros 2.3.3", +] + +[[package]] +name = "serde_with" +version = "3.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9adc193c780ef8f159aee8b61e2d5801aaa555e6eb0947fe45530ec506296f" +dependencies = [ + "serde_core", + "serde_with_macros 3.24.0", +] + +[[package]] +name = "serde_with_macros" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "881b6f881b17d13214e5d494c939ebab463d01264ce1811e9d4ac3a882e7695f" +dependencies = [ + "darling 0.20.11", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "serde_with_macros" +version = "3.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e17bbc68e28663bbbb90df47e058aa7eda4fb445b89fe70457bb94fbccf6e49" +dependencies = [ + "darling 0.24.1", + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "serial_buffer" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "slog" +version = "2.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b3b8565691b22d2bdfc066426ed48f837fc0c5f2c8cad8d9718f7f99d6995c1" +dependencies = [ + "anyhow", + "erased-serde", + "rustversion", + "serde_core", +] + +[[package]] +name = "slog-async" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72c8038f898a2c79507940990f05386455b3a317d8f18d4caea7cbc3d5096b84" +dependencies = [ + "crossbeam-channel", + "slog", + "take_mut", + "thread_local", +] + +[[package]] +name = "slog-scope" +version = "4.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42b76cf645c92e7850d5a1c9205ebf2864bd32c0ab3e978e6daad51fedf7ef54" +dependencies = [ + "arc-swap", + "lazy_static", + "slog", +] + +[[package]] +name = "slog-term" +version = "2.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cb1fc680b38eed6fad4c02b3871c09d2c81db8c96aa4e9c0a34904c830f09b5" +dependencies = [ + "chrono", + "is-terminal", + "slog", + "term", + "thread_local", + "time", +] + +[[package]] +name = "smallvec" +version = "1.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "149309498a5f7334b12ddae0cad8aba81ce785fe91755068867e017cf98fee46" + +[[package]] +name = "socket2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "spin" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" +dependencies = [ + "lock_api", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syslog" +version = "6.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc7e95b5b795122fafe6519e27629b5ab4232c73ebb2428f568e82b1a457ad3" +dependencies = [ + "error-chain", + "hostname", + "libc", + "log", + "time", +] + +[[package]] +name = "take_mut" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" + +[[package]] +name = "term" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8c27177b12a6399ffc08b98f76f7c9a1f4fe9fc967c784c5a071fa8d93cf7e1" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "terminal_size" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874" +dependencies = [ + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl 2.0.21", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "thread_local" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "time" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134" +dependencies = [ + "deranged", + "libc", + "num-conv", + "num_threads", + "powerfmt", + "serde_core", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" + +[[package]] +name = "time-macros" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e689342a48d2ea927c87ea50cabf8594854bf940e9310208848d680d668ed85" +dependencies = [ + "num-conv", + "time-core", +] + +[[package]] +name = "tokio" +version = "1.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tpm" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "anyhow", + "byteorder", + "libc", + "log", + "thiserror 1.0.69", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "tracer" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "libc", + "log", + "once_cell", + "serde", + "serde_json", +] + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "uuid" +version = "1.29.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bed99139f7dac9b5cfe5f17be4d66dc7079a34dee1d74bcc77217119240e267c" +dependencies = [ + "getrandom 0.4.3", + "js-sys", + "rand 0.10.3", + "wasm-bindgen", +] + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "vfio-bindings" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43449b404c488f70507dca193debd4bea361fe8089869b947adc19720e464bce" +dependencies = [ + "vmm-sys-util 0.15.0", +] + +[[package]] +name = "vfio-bindings" +version = "0.4.0" +source = "git+https://github.com/rust-vmm/vfio?rev=64171f3#64171f3da1af7926adf162ecf545cb05fb9243c9" + +[[package]] +name = "vfio-ioctls" +version = "0.2.0" +source = "git+https://github.com/rust-vmm/vfio?rev=64171f3#64171f3da1af7926adf162ecf545cb05fb9243c9" +dependencies = [ + "byteorder", + "kvm-bindings", + "kvm-ioctls", + "libc", + "log", + "thiserror 1.0.69", + "vfio-bindings 0.4.0", + "vm-memory 0.14.1", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "vfio_user" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "libc", + "log", + "serde", + "serde_derive", + "serde_json", + "thiserror 1.0.69", + "vfio-bindings 0.3.1", + "vm-memory 0.14.1", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "vhdx" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "byteorder", + "crc32c", + "libc", + "log", + "remain", + "thiserror 1.0.69", + "uuid", +] + +[[package]] +name = "vhost" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6be08d1166d41a78861ad50212ab3f9eca0729c349ac3a7a8f557c62406b87cc" +dependencies = [ + "bitflags 2.13.2", + "libc", + "vm-memory 0.14.1", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "vhost" +version = "0.12.1" +source = "git+https://github.com/rust-vmm/vhost?rev=d983ae0#d983ae07f78663b7d24059667376992460b571a2" +dependencies = [ + "bitflags 2.13.2", + "libc", + "uuid", + "vm-memory 0.16.2", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "vhost-user-backend" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f0ffb1dd8e00a708a0e2c32d5efec5812953819888591fff9ff68236b8a5096" +dependencies = [ + "libc", + "log", + "vhost 0.11.0", + "virtio-bindings 0.2.7", + "virtio-queue 0.12.0", + "vm-memory 0.14.1", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "virtio-bindings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff512178285488516ed85f15b5d0113a7cdb89e9e8a760b269ae4f02b84bd6b" + +[[package]] +name = "virtio-bindings" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "091f1f09cfbf2a78563b562e7a949465cce1aef63b6065645188d995162f8868" + +[[package]] +name = "virtio-devices" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "anyhow", + "arc-swap", + "block_util", + "byteorder", + "epoll", + "event_monitor", + "io-uring", + "libc", + "log", + "net_gen", + "net_util", + "pci", + "rate_limiter", + "seccompiler", + "sendfd", + "serde", + "serde_json", + "serde_with 3.24.0", + "serial_buffer", + "thiserror 1.0.69", + "vhost 0.12.1", + "virtio-bindings 0.1.0", + "virtio-queue 0.14.0", + "virtiofsd", + "vm-allocator", + "vm-device", + "vm-memory 0.16.2", + "vm-migration", + "vm-virtio", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "virtio-queue" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3f69a13d6610db9312acbb438b0390362af905d37634a2106be70c0f734986d" +dependencies = [ + "log", + "virtio-bindings 0.2.7", + "vm-memory 0.14.1", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "virtio-queue" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07d8406e7250c934462de585d8f2d2781c31819bca1fbb7c5e964ca6bbaabfe8" +dependencies = [ + "log", + "virtio-bindings 0.2.7", + "vm-memory 0.14.1", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "virtio-queue" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872e2f3fbd70a7e6f01689720cce3d5c2c5efe52b484dd07b674246ada0e9a8d" +dependencies = [ + "log", + "virtio-bindings 0.2.7", + "vm-memory 0.16.2", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "virtiofsd" +version = "1.11.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "anyhow", + "bitflags 1.3.2", + "capng", + "clap", + "env_logger", + "futures", + "libc", + "libseccomp-sys", + "log", + "postcard", + "rate_limiter", + "serde", + "syslog", + "vhost 0.11.0", + "vhost-user-backend", + "virtio-bindings 0.2.7", + "virtio-queue 0.14.0", + "vm-memory 0.16.2", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "vm-allocator" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "arch", + "libc", + "vm-memory 0.16.2", +] + +[[package]] +name = "vm-device" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "anyhow", + "hypervisor", + "serde", + "thiserror 1.0.69", + "vfio-ioctls", + "vm-memory 0.16.2", + "vmm-sys-util 0.12.1", +] + +[[package]] +name = "vm-fdt" +version = "0.3.0" +source = "git+https://github.com/rust-vmm/vm-fdt?branch=main#ef5bd734f5f66fb07722d766981adbc915f0d941" + +[[package]] +name = "vm-memory" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c3aba5064cc5f6f7740cddc8dae34d2d9a311cac69b60d942af7f3ab8fc49f4" +dependencies = [ + "arc-swap", + "libc", + "thiserror 1.0.69", + "winapi", +] + +[[package]] +name = "vm-memory" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd5e56d48353c5f54ef50bd158a0452fc82f5383da840f7b8efc31695dd3b9d" +dependencies = [ + "arc-swap", + "libc", + "thiserror 1.0.69", + "winapi", +] + +[[package]] +name = "vm-migration" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "anyhow", + "serde", + "serde_json", + "thiserror 1.0.69", + "vm-memory 0.16.2", +] + +[[package]] +name = "vm-virtio" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "log", + "virtio-queue 0.14.0", + "vm-memory 0.16.2", +] + +[[package]] +name = "vmm" +version = "0.1.0" +source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" +dependencies = [ + "acpi_tables", + "anyhow", + "arc-swap", + "arch", + "async-trait", + "bitflags 2.13.2", + "block_util", + "chrono", + "clap", + "devices", + "epoll", + "event_monitor", + "event_notifier", + "hypervisor", + "lazy_static", + "libc", + "linux-loader", + "log", + "logging", + "micro_http", + "net_util", + "once_cell", + "option_parser", + "pci", + "qcow", + "seccompiler", + "serde", + "serde_json", + "serial_buffer", + "signal-hook", + "thiserror 1.0.69", + "tokio", + "tracer", + "uuid", + "vfio-ioctls", + "vfio_user", + "vhdx", + "virtio-devices", + "virtio-queue 0.11.0", + "virtiofsd", + "vm-allocator", + "vm-device", + "vm-memory 0.16.2", + "vm-migration", + "vm-virtio", + "vmm-sys-util 0.12.1", + "zerocopy 0.6.6", +] + +[[package]] +name = "vmm-sys-util" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1435039746e20da4f8d507a72ee1b916f7b4b05af7a91c093d2c6561934ede" +dependencies = [ + "bitflags 1.3.2", + "libc", + "serde", + "serde_derive", +] + +[[package]] +name = "vmm-sys-util" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "506c62fdf617a5176827c2f9afbcf1be155b03a9b4bf9617a60dbc07e3a1642f" +dependencies = [ + "bitflags 1.3.2", + "libc", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasm-bindgen" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb54f33acc68fd454578d9820b0bde1a1a3d17aa17bb7b6595806d02886d409" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e29d0c35b16e224a7eeb5cd2d25e3e1968fbd65604117b44d3b789d00ee8535" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f501a8bc3719dba86ef8ae4728879c08001bea749eb1333ac5b91e040e2a6b7" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.10", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23f0c9c52aa7cd7d77769a4cfe2a9adb1b331f489a41d912ce14513d5ab995c6" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zerocopy" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854e949ac82d619ee9a14c66a1b674ac730422372ccb759ce0c39cabcf2bf8e6" +dependencies = [ + "byteorder", + "zerocopy-derive 0.6.6", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive 0.7.35", +] + +[[package]] +name = "zerocopy-derive" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "125139de3f6b9d625c39e2efdd73d41bdac468ccd556556440e322be0e1bbd91" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/runtime/sandbox-worker/Cargo.toml b/runtime/sandbox-worker/Cargo.toml new file mode 100644 index 0000000..04e4c4b --- /dev/null +++ b/runtime/sandbox-worker/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "agentdrive-sandbox-worker" +version = "0.1.0" +edition = "2021" +license = "Apache-2.0" + +[workspace] + +[dependencies] +cube-hypervisor = { git = "https://github.com/TencentCloud/CubeSandbox", rev = "4411c96ba8658a959de34693f54f4918db2a432f", features = ["lib_support"] } +serde_json = "1" +# CubeSandbox v0.7.3 uses vm-memory 0.16; linux-loader 0.13.2 uses 0.17. +linux-loader = "=0.13.0" diff --git a/runtime/sandbox-worker/src/main.rs b/runtime/sandbox-worker/src/main.rs new file mode 100644 index 0000000..8a30b02 --- /dev/null +++ b/runtime/sandbox-worker/src/main.rs @@ -0,0 +1,49 @@ +use cube_hypervisor::{vm_config::VmConfig, vmm_config::VmmConfig, ApiRequest, VmmInstance}; +use std::io::{self, BufRead, Write}; + +fn main() -> Result<(), Box> { + let path = std::env::args() + .nth(1) + .ok_or("expected VM configuration path")?; + let directory = std::path::Path::new(&path) + .parent() + .ok_or("missing instance directory")?; + let lock = std::fs::OpenOptions::new() + .create(true) + .write(true) + .truncate(false) + .open(directory.join("worker.lock"))?; + lock.try_lock()?; + let config: VmConfig = serde_json::from_reader(std::fs::File::open(path)?)?; + let vmm = VmmInstance::new(VmmConfig { + log_stderr: true, + log_json_file: None, + ..Default::default() + })?; + vmm.send_request(ApiRequest::VmCreate(Box::new(config))) + .map_err(|e| format!("{e:?}"))? + .map_err(|e| format!("{e:?}"))?; + vmm.send_request(ApiRequest::VmBoot) + .map_err(|e| format!("{e:?}"))? + .map_err(|e| format!("{e:?}"))?; + println!("{{\"state\":\"booting\"}}"); + io::stdout().flush()?; + // Closing the parent channel terminates this instance and releases its resources. + for command in io::stdin().lock().lines() { + match command?.as_str() { + "stop" => break, + "ping" => { + vmm.send_request(ApiRequest::VmmPing) + .map_err(|e| format!("{e:?}"))? + .map_err(|e| format!("{e:?}"))?; + println!("{{\"state\":\"alive\"}}"); + io::stdout().flush()?; + } + _ => return Err("unknown worker command".into()), + } + } + vmm.send_request(ApiRequest::VmDelete) + .map_err(|e| format!("{e:?}"))? + .map_err(|e| format!("{e:?}"))?; + Ok(()) +} diff --git a/tests/dashboard/sandbox.test.cjs b/tests/dashboard/sandbox.test.cjs new file mode 100644 index 0000000..aff8876 --- /dev/null +++ b/tests/dashboard/sandbox.test.cjs @@ -0,0 +1,219 @@ +const {test, before, after} = require('node:test'); +const assert = require('node:assert/strict'); +const {createServer} = require('node:http'); +const {readFile} = require('node:fs/promises'); +const path = require('node:path'); +const {chromium} = require('playwright'); +let server, browser, endpoint; +before(async () => { + const source = await readFile(path.join(__dirname, '../../crates/agentdrive-mcp/dashboard/sandbox.js')); + server = createServer((req,res) => req.url === '/sandbox.js' + ? res.writeHead(200,{'Content-Type':'text/javascript'}).end(source) + : res.writeHead(200,{'Content-Type':'text/html'}).end('
')); + await new Promise(resolve => server.listen(0,'127.0.0.1',resolve)); + endpoint = `http://127.0.0.1:${server.address().port}`; + browser = await chromium.launch({headless:true}); +}); +after(async()=>{ await browser?.close(); if(server) await new Promise(resolve=>server.close(resolve)); }); +async function setup(t, enabled = true, desktop = '/desktop') { + const page=await browser.newPage();t.after(()=>page.close());await page.goto(endpoint); + await page.evaluate(async ({enabled,desktop}) => { + const {createSandboxControl}=await import('/sandbox.js'); + window.calls=[]; + let sandbox = null; + const api=async(url,options={})=>{ + window.calls.push({url,method:options.method||'GET',...(options.body?{body:JSON.parse(options.body)}:{})}); + if (url.endsWith('/proxy')) { + if (!options.method && window.holdProxy) await new Promise(resolve => { window.finishProxy = resolve; }); + return {url:window.proxyUrl || null}; + } + if (url.endsWith('/delete')) { + if (window.failDelete) throw new Error('系统磁盘暂时无法删除'); + if (window.holdDelete) await new Promise(resolve => { window.finishDelete = resolve; }); + sandbox = null; return {deleted:true}; + } + if (url.endsWith('/force-stop')) { sandbox={...sandbox,state:'stopped',message:'已强制停止'}; return sandbox; } + if (url.endsWith('/mcp-config')) return {mcpServers:{ubuntu_desktop:{command:'python3',args:['/proxy.py','--socket','/branch-a/vsock.sock']}}}; + if (options.method) { + if (options.method === 'DELETE' && window.failStop) throw new Error('正常停止失败,请使用强制停止'); + sandbox={state:options.method==='POST'?'ready':'stopped',desktop_url:desktop}; return sandbox; + } + return {enabled,sandbox}; + }; + const element=(tag,className='',text='')=>{const e=document.createElement(tag);e.className=className;e.textContent=text;return e;}; + document.querySelector('main').append(createSandboxControl({api,element,workspace:'workspace-a',branch:'branch-a'})); + },{enabled,desktop}); + await page.getByRole('button',{name:'Sandbox',exact:true}).click(); + return page; +} +test('Sandbox creation targets its branch and exposes desktop and stop actions',async t=>{ + const page=await setup(t); + await page.getByRole('button',{name:'启动 Sandbox',exact:true}).click(); + assert(await page.getByRole('link',{name:'打开桌面'}).isVisible()); + await page.getByRole('button',{name:'停止 Sandbox'}).click(); + assert(await page.getByRole('button',{name:'启动 Sandbox'}).isVisible()); + const calls=(await page.evaluate(()=>window.calls)).filter(c=>c.url !== '/api/sandboxes'); + assert.deepEqual(calls.map(c=>c.method),['GET','GET','POST','DELETE']); + assert(calls.every(c=>c.url==='/api/workspaces/workspace-a/branches/branch-a/sandbox')); +}); + +test('Deletion requires stopped state and exact confirmation, supports cancellation and retry', async t => { + const page = await setup(t); + await page.getByRole('button', {name:'启动 Sandbox', exact:true}).click(); + assert(await page.getByRole('button', {name:'删除 Sandbox', exact:true}).isDisabled()); + await page.getByRole('button', {name:'停止 Sandbox'}).click(); + await page.getByRole('button', {name:'删除 Sandbox', exact:true}).click(); + const modal = page.getByRole('dialog', {name:'删除 Sandbox', exact:true}); + assert.match(await modal.textContent(), /workspace.*Downloads/); + const confirm = modal.getByRole('button', {name:'确认删除'}); + assert(await confirm.isDisabled()); + await modal.getByRole('textbox').fill('wrong-branch'); assert(await confirm.isDisabled()); + await modal.getByRole('textbox').fill('branch-a'); + await modal.getByRole('button', {name:'取消'}).click(); + await page.getByRole('button', {name:'删除 Sandbox', exact:true}).click(); + await page.keyboard.press('Escape'); + assert.equal((await page.evaluate(() => window.calls)).filter(c => c.url.endsWith('/delete')).length, 0); + await page.getByRole('button', {name:'删除 Sandbox', exact:true}).click(); + await modal.getByRole('textbox').fill('branch-a'); + await page.evaluate(() => { window.failDelete = true; }); + await confirm.click(); + assert.match(await modal.getByRole('alert').textContent(), /暂时无法删除/); + await page.evaluate(() => { window.failDelete = false; window.holdDelete = true; }); + await confirm.click(); + assert(await modal.getByRole('button', {name:'正在删除…'}).isDisabled()); + await page.keyboard.press('Escape'); assert(await modal.isVisible()); + await page.evaluate(() => window.finishDelete()); + await modal.waitFor({state:'hidden'}); + const deleted = (await page.evaluate(() => window.calls)).filter(c => c.url.endsWith('/delete')); + assert.equal(deleted.length, 2); + assert.deepEqual(deleted[1], {url:'/api/workspaces/workspace-a/branches/branch-a/sandbox/delete',method:'POST',body:{confirm_branch:'branch-a'}}); + await page.getByRole('button', {name:'Sandbox', exact:true}).click(); + assert(!(await page.getByRole('button', {name:'删除 Sandbox', exact:true}).isVisible())); + await page.getByRole('button', {name:'启动 Sandbox', exact:true}).click(); + assert(await page.getByRole('link', {name:'打开桌面'}).isVisible()); +}); +test('Unavailable runtime disables creation',async t=>{ + const page=await setup(t,false); + assert(await page.getByRole('button',{name:'启动 Sandbox'}).isDisabled()); + assert.match(await page.getByRole('status').textContent(),/尚未配置/); +}); + +test('Failed normal stop offers explicit force confirmation and preserves restart', async t => { + const page = await setup(t); + await page.getByRole('button', {name:'启动 Sandbox', exact:true}).click(); + await page.evaluate(() => { window.failStop = true; }); + await page.getByRole('button', {name:'停止 Sandbox', exact:true}).click(); + await page.getByRole('button', {name:'强制停止', exact:true}).click(); + const modal = page.getByRole('dialog', {name:'强制停止 Sandbox', exact:true}); + const confirm = modal.getByRole('button', {name:'确认强制停止', exact:true}); + assert(await confirm.isDisabled()); + await modal.getByRole('textbox').fill('branch-a'); + await confirm.click(); + assert(await page.getByRole('button', {name:'启动 Sandbox', exact:true}).isVisible()); + assert(await page.getByRole('button', {name:'删除 Sandbox', exact:true}).isEnabled()); + const call = await page.evaluate(() => window.calls.find(item => item.url.endsWith('/force-stop'))); + assert.deepEqual(call.body, {confirm_branch:'branch-a'}); + assert.equal(call.method, 'POST'); +}); +test('Desktop links must use the AgentDrive origin',async t=>{ + const page=await setup(t,true,'https://example.org/desktop'); + await page.getByRole('button',{name:'启动 Sandbox',exact:true}).click(); + assert(!(await page.getByRole('link',{name:'打开桌面'}).isVisible())); + assert.match(await page.getByRole('status').textContent(),/地址无效/); +}); + +test('Startup polling displays the active step and retains failures', async t => { + const page = await browser.newPage(); t.after(() => page.close()); await page.goto(endpoint); + await page.evaluate(async () => { + const {createSandboxControl} = await import('/sandbox.js'); + window.startupState = {enabled:true,sandbox:null}; + const api = async (url, options={}) => options.method === 'POST' + ? new Promise(resolve => { window.finishStartup = resolve; }) + : window.startupState; + const element = (tag, className='', text='') => { const e=document.createElement(tag);e.className=className;e.textContent=text;return e; }; + document.querySelector('main').append(createSandboxControl({api,element,workspace:'w',branch:'b'})); + }); + await page.getByRole('button',{name:'Sandbox',exact:true}).click(); + await page.getByRole('button',{name:'启动 Sandbox',exact:true}).click(); + await page.evaluate(() => { window.startupState.sandbox={state:'starting',startup_step:'wait_desktop'}; }); + await page.waitForSelector('[data-step="wait_desktop"][data-state="active"]'); + assert.equal(await page.locator('[data-state="complete"]').count(),3); + assert.equal(await page.locator('.sandbox-step').count(),4); + assert.equal(await page.locator('[data-step="verify_tools"]').count(),0); + await page.evaluate(() => { + const failure={state:'error',startup_step:'wait_desktop',message:'Ubuntu 启动失败'}; + window.startupState.sandbox=failure; window.finishStartup(failure); + }); + await page.waitForSelector('[data-step="wait_desktop"][data-state="error"]'); + assert(await page.getByRole('button',{name:'重试启动'}).isVisible()); + assert.match(await page.getByRole('status').textContent(),/Ubuntu 启动失败/); +}); + + +test('Dialog keeps branch rows compact and restores focus after Escape',async t=>{ + const page=await setup(t); + assert(await page.getByRole('dialog').isVisible()); + assert.equal(await page.locator('main .sandbox-controls > button').count(),1); + assert.equal(await page.locator('main .sandbox-steps').count(),0); + await page.keyboard.press('Escape'); + assert.equal(await page.getByRole('dialog').count(),0); + assert(await page.getByRole('button',{name:'Sandbox',exact:true}).evaluate(e=>e===document.activeElement)); + await page.getByRole('button',{name:'Sandbox',exact:true}).click(); + await page.evaluate(()=>{ + const control=document.querySelector('.sandbox-controls'); + document.querySelector('main').replaceChildren(); + document.querySelector('main').append(control); + }); + assert(await page.getByRole('dialog').isVisible()); + await page.getByRole('button',{name:'关闭 Sandbox'}).click(); + assert.equal(await page.getByRole('dialog').count(),0); +}); + +test('MCP configuration is fetched for the branch and can be downloaded',async t=>{ + const page=await setup(t); + await page.getByText('MCP 配置',{exact:true}).click(); + const config=page.getByRole('textbox',{name:'Sandbox MCP 配置'}); + await config.waitFor(); + assert.equal(JSON.parse(await config.inputValue()).mcpServers.ubuntu_desktop.args[2],'/branch-a/vsock.sock'); + const download=page.waitForEvent('download'); + await page.getByRole('button',{name:'下载 JSON'}).click(); + assert.equal((await download).suggestedFilename(),'sandbox-mcp-branch-a.json'); + assert((await page.evaluate(()=>window.calls)).some(c=>c.url==='/api/workspaces/workspace-a/branches/branch-a/sandbox/mcp-config')); +}); + +test('Dialog reopens a running Sandbox and disposes its overlay',async t=>{ + const page=await setup(t); + await page.evaluate(()=>{ + const control=document.querySelector('.sandbox-controls'); + window.control=control; + }); + await page.getByRole('button',{name:'启动 Sandbox',exact:true}).click(); + await page.getByRole('button',{name:'关闭 Sandbox'}).click(); + await page.getByRole('button',{name:'Sandbox',exact:true}).click(); + assert(await page.getByRole('link',{name:'打开桌面'}).isVisible()); + await page.evaluate(()=>window.control.dispose()); + assert.equal(await page.locator('dialog').count(),0); +}); + +test('assistant opens the full CSGClaw UI and proxy input waits for saved settings', async t => { + const page = await setup(t); + await page.evaluate(() => { + window.holdProxy = true; + document.querySelector('.sandbox-controls').update({state:'ready',template_id:'ubuntu-csgclaw',services:[{id:'csgclaw',label:'打开助手'}]}, true); + }); + const link = page.getByRole('link', {name:'打开助手',exact:true}); + const url = new URL(await link.getAttribute('href')); + assert.equal(url.hostname, 'csgclaw.workspace-a.branch-a.localhost'); + assert.equal(url.hash, '#/new?agent=agent-agentdrive'); + await page.getByText('代理配置',{exact:true}).click(); + const input = page.getByRole('textbox',{name:'HTTP 代理地址'}); + assert(await input.isDisabled()); + assert(await page.getByRole('button',{name:'保存代理配置'}).isDisabled()); + await page.waitForFunction(() => !!window.finishProxy); + await page.evaluate(() => window.finishProxy()); + await input.fill('http://127.0.0.1:7890'); + await page.getByRole('button',{name:'保存代理配置'}).click(); + await page.getByText('已保存,下次启动沙盒时生效。',{exact:true}).waitFor(); + const saved = (await page.evaluate(() => window.calls)).find(call => call.method === 'PUT'); + assert.deepEqual(saved.body, {url:'http://127.0.0.1:7890'}); +}); diff --git a/tests/dashboard/sandboxes.test.cjs b/tests/dashboard/sandboxes.test.cjs new file mode 100644 index 0000000..f805243 --- /dev/null +++ b/tests/dashboard/sandboxes.test.cjs @@ -0,0 +1,130 @@ +const {test,before,after}=require('node:test'); +const assert=require('node:assert/strict'); +const {createServer}=require('node:http'); +const {readFile}=require('node:fs/promises'); +const path=require('node:path'); +const {chromium}=require('playwright'); +let server,browser,endpoint; +before(async()=>{ + const directory=path.resolve(__dirname,'../../crates/agentdrive-mcp/dashboard'); + server=createServer(async(req,res)=>{ + const pathname=new URL(req.url,'http://localhost').pathname; + const name=['/sandboxes','/branches'].includes(pathname)?'index.html':pathname.slice(1); + if(!/^[a-z.-]+$/.test(name))return res.writeHead(404).end(); + try{const body=await readFile(path.join(directory,name));res.writeHead(200,{'Content-Type':{'.html':'text/html','.js':'text/javascript','.css':'text/css'}[path.extname(name)]||'text/plain'}).end(body);}catch{res.writeHead(404).end();} + }); + await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));endpoint=`http://127.0.0.1:${server.address().port}`; + browser=await chromium.launch({headless:true}); +}); +after(async()=>{await browser?.close();if(server)await new Promise(resolve=>server.close(resolve));}); +async function setup(t,options={}){ + const page=await browser.newPage({viewport:{width:1440,height:1000}}),errors=[],calls=[]; + const data={enabled:true,errors:[],entries:[ + {workspace:'alpha',workspace_name:'项目 Alpha',branch:'main-a',branch_name:'main',can_start:true,sandbox:{state:'ready',desktop_url:'/desktop-a'}}, + {workspace:'beta',workspace_name:'项目 Beta',branch:'branch-b',branch_name:'feature',can_start:true,sandbox:{state:'stopped'}}, + {workspace:'beta',workspace_name:'项目 Beta',branch:'new-b',branch_name:'new-branch',can_start:true,sandbox:null}, + ],template:{max_instances:2,network_slots:2,vcpus:4,memory_gib:8,disk_gib:16,manifest:{name:'Ubuntu Desktop',os:'Ubuntu 22.04',desktop:'GNOME / X11',browser:'Chromium 160',locale:'C.UTF-8',fonts:['Noto CJK','Noto Color Emoji'],tools:['computer-use-linux 0.7.53'],workspace_path:'/workspace',built_at:'2026-11-13T00:00:00Z'}},...options}; + page.on('pageerror',e=>errors.push(e.message));t.after(async()=>{await page.close();assert.deepEqual(errors,[]);}); + await page.route('**/api/**',async route=>{ + const url=new URL(route.request().url()),method=route.request().method();calls.push({path:url.pathname,method}); + if(url.pathname==='/api/workspaces')return route.fulfill({json:data.enabled?[{id:'alpha',name:'项目 Alpha'},{id:'beta',name:'项目 Beta'}]:[]}); + if(url.pathname==='/api/sandboxes')return data.failure?route.fulfill({status:503,json:{error:{message:'服务暂时不可用'}}}):route.fulfill({json:data}); + if(url.pathname.endsWith('/mcp-config'))return route.fulfill({json:{mcpServers:{ubuntu_desktop:{command:'python3',args:[url.pathname]}}}}); + const entry=data.entries.find(e=>url.pathname.includes(`/branches/${e.branch}/sandbox`)); + if(entry && url.pathname.endsWith('/delete')) { entry.sandbox=null; return route.fulfill({json:{deleted:true}}); } + if(entry){if(method!=='GET')entry.sandbox={state:method==='POST'?'ready':'stopped',desktop_url:'/desktop-b'};return route.fulfill({json:method==='GET'?{enabled:data.enabled,sandbox:entry.sandbox}:entry.sandbox});} + return route.fulfill({status:404,json:{error:{message:'Not found'}}}); + }); + await page.goto(endpoint+'/sandboxes');await page.waitForFunction(()=>document.querySelector('#updated').textContent.includes('已刷新')); + return {page,data,calls}; +} +test('catalog lists every workspace and controls the selected branch',async t=>{ + const {page,calls}=await setup(t); + assert.equal(await page.locator('#sandboxes-nav').getAttribute('aria-current'),'page'); + assert.equal(await page.locator('.sandbox-card:visible').count(),2); + const card=page.locator('[data-branch="branch-b"]');await card.getByRole('button',{name:'启动 Sandbox',exact:true}).click(); + await card.getByRole('link',{name:'打开桌面'}).waitFor(); + await card.getByRole('button',{name:'详情与 MCP'}).click(); + await page.getByText('MCP 配置',{exact:true}).click(); + assert.match(await page.getByRole('textbox',{name:'Sandbox MCP 配置'}).inputValue(),/branches\/branch-b\/sandbox/); + await page.getByRole('button',{name:'关闭 Sandbox'}).click(); + await card.getByRole('button',{name:'停止 Sandbox'}).click(); + assert.deepEqual(calls.filter(c=>c.method!=='GET'),[{path:'/api/workspaces/beta/branches/branch-b/sandbox',method:'POST'},{path:'/api/workspaces/beta/branches/branch-b/sandbox',method:'DELETE'}]); +}); +test('deletion removes the card from existing instances and makes the branch available for creation',async t=>{ + const {page,calls}=await setup(t); + const card=page.locator('[data-branch="branch-b"]'); + await card.getByRole('button',{name:'删除 Sandbox',exact:true}).click(); + const dialog=page.getByRole('dialog',{name:'删除 Sandbox',exact:true}); + assert.match(await dialog.textContent(), /项目 Beta.*feature.*branch-b/); + await dialog.getByRole('textbox').fill('feature'); + await dialog.getByRole('button',{name:'确认删除'}).click(); + await card.waitFor({state:'hidden'}); + assert.equal(await page.locator('.sandbox-card:visible').count(),1); + assert.deepEqual(calls.filter(c=>c.method!=='GET'),[{path:'/api/workspaces/beta/branches/branch-b/sandbox/delete',method:'POST'}]); + await page.getByRole('button',{name:'创建沙盒',exact:true}).click(); + const create=page.getByRole('dialog',{name:'创建沙盒',exact:true}); + await create.getByLabel('工作区',{exact:true}).selectOption('beta'); + await create.getByLabel('分支',{exact:true}).selectOption('branch-b'); + assert(await create.getByRole('button',{name:'创建并启动'}).isEnabled()); +}); +test('refresh preserves filters and the open dialog; creation selects an unused branch',async t=>{ + const {page}=await setup(t); + await page.getByRole('combobox',{name:'筛选工作区'}).selectOption('beta'); + await page.getByRole('button',{name:'详情与 MCP'}).filter({visible:true}).click(); + await page.waitForTimeout(2200);assert(await page.getByRole('dialog').isVisible()); + await page.keyboard.press('Escape');assert.equal(await page.getByRole('combobox',{name:'筛选工作区'}).inputValue(),'beta'); + await page.getByRole('button',{name:'创建沙盒',exact:true}).click(); + const dialog=page.getByRole('dialog',{name:'创建沙盒',exact:true}); + assert(await dialog.isVisible()); + assert.equal(await dialog.getByLabel('工作区',{exact:true}).inputValue(),'beta'); + assert.equal(await dialog.getByLabel('分支',{exact:true}).inputValue(),'new-b'); + await page.waitForTimeout(2200); + assert.equal(await dialog.getByLabel('分支',{exact:true}).inputValue(),'new-b'); + await dialog.getByRole('button',{name:'创建并启动',exact:true}).click(); + const progress=page.getByRole('dialog',{name:'Sandbox',exact:true}); + await progress.getByRole('link',{name:'打开桌面'}).waitFor(); + assert.match(await progress.locator('.sandbox-identity').textContent(),/new-branch/); + await progress.getByRole('button',{name:'关闭 Sandbox'}).click(); + await page.getByRole('combobox',{name:'筛选沙盒状态'}).selectOption('ready'); + await page.getByRole('button',{name:'刷新数据'}).click(); + await page.locator('[data-branch="new-b"]:visible').waitFor(); +}); +test('creation dialog explains unavailable branches and capacity without sending requests',async t=>{ + const {page,data,calls}=await setup(t); + await page.getByRole('button',{name:'创建沙盒',exact:true}).click(); + const dialog=page.getByRole('dialog',{name:'创建沙盒',exact:true}); + assert(await dialog.getByRole('button',{name:'创建并启动'}).isDisabled()); + assert.match(await dialog.getByRole('status').textContent(),/没有可用分支/); + assert(await dialog.getByRole('link',{name:'前往分支管理'}).isVisible()); + await dialog.getByLabel('工作区',{exact:true}).selectOption('beta'); + assert.equal(await dialog.getByLabel('分支',{exact:true}).locator('option').count(),1); + assert(await dialog.getByRole('button',{name:'创建并启动'}).isEnabled()); + data.template.max_instances=1; + await page.waitForTimeout(2200); + assert(await dialog.getByRole('button',{name:'创建并启动'}).isDisabled()); + assert.match(await dialog.getByRole('status').textContent(),/已达到 1 个运行实例/); + await page.keyboard.press('Escape'); + assert(await page.getByRole('button',{name:'创建沙盒',exact:true}).evaluate(e=>e===document.activeElement)); + assert.equal(calls.filter(c=>c.method!=='GET').length,0); +}); +test('template shows installed versions, resources, and stays selected across refresh',async t=>{ + const {page}=await setup(t); + await page.getByRole('tab',{name:'模板内容'}).click(); + await page.getByRole('button',{name:'刷新数据'}).click(); + assert(await page.getByRole('tabpanel',{name:'模板内容'}).isVisible()); + assert.match(await page.locator('#sandbox-template').textContent(),/Chromium 160/); + assert.match(await page.locator('#sandbox-template').textContent(),/computer-use-linux 0.7.53/); + assert.match(await page.locator('#sandbox-template').textContent(),/Noto CJK/); + assert.match(await page.locator('#sandbox-template').textContent(),/4 vCPU · 8 GiB/); + await page.setViewportSize({width:600,height:900}); + assert(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth), JSON.stringify(await page.evaluate(()=>[...document.querySelectorAll('body *')].filter(e=>e.getBoundingClientRect().right>innerWidth+1).map(e=>[e.tagName,e.id,e.className,e.getBoundingClientRect().width]).slice(0,25)))); +}); +test('unconfigured runtime and failed refresh remain visible',async t=>{ + const {page,data}=await setup(t,{enabled:false,entries:[],template:null}); + assert.equal(await page.locator('#sandboxes-nav').getAttribute('aria-disabled'),'false'); + assert(await page.getByRole('button',{name:'创建沙盒',exact:true}).isDisabled()); + assert.match(await page.locator('#sandbox-instances .sandbox-empty').textContent(),/尚未配置/); + data.failure=true;await page.getByRole('button',{name:'刷新数据'}).click(); + await page.getByRole('alert').filter({hasText:'沙盒数据刷新失败'}).waitFor(); +}); diff --git a/tests/sandbox/.gitignore b/tests/sandbox/.gitignore new file mode 100644 index 0000000..a348e50 --- /dev/null +++ b/tests/sandbox/.gitignore @@ -0,0 +1 @@ +/__pycache__/ diff --git a/tests/sandbox/chromium_probe.py b/tests/sandbox/chromium_probe.py new file mode 100644 index 0000000..4b92fbc --- /dev/null +++ b/tests/sandbox/chromium_probe.py @@ -0,0 +1,129 @@ +"""通过桌面 MCP 验证 Chromium 的中文显示、鼠标点击和中文输入。""" +import argparse +import base64 +import json +from pathlib import Path +import socket +import time +import uuid + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument('--socket', required=True) +parser.add_argument('--mount', required=True) +parser.add_argument('--artifacts', required=True) +args = parser.parse_args() +artifacts = Path(args.artifacts) +artifacts.mkdir(parents=True, exist_ok=True) +filename = 'chromium-font-check-' + uuid.uuid4().hex + '.html' +fixture = Path(args.mount) / filename +fixture.write_text(''' +Chromium 中文字体验证 + +

中文字体验证:开放传神 · AgentDrive

+

简体中文:浏览器可以正常显示中文。

+

繁體中文:瀏覽器可以正常顯示中文。

+

表情符号:😀 🚀 ✅ 🌏

+
+
+

等待点击按钮

+''') +connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) +connection.settimeout(60) +connection.connect(args.socket) +stream = connection.makefile('rwb', buffering=0) +stream.write(b'CONNECT 7000\n') +assert stream.readline().startswith(b'OK ') +request_id = 0 +calls = [] + + +def rpc(method, params): + global request_id + request_id += 1 + stream.write((json.dumps({'jsonrpc': '2.0', 'id': request_id, + 'method': method, 'params': params}) + '\n').encode()) + while True: + response = json.loads(stream.readline()) + if response.get('id') == request_id: + assert 'error' not in response, response + return response['result'] + + +def call(name, arguments, screenshot=None): + result = rpc('tools/call', {'name': name, 'arguments': arguments}) + assert not result.get('isError'), result + text = [item['text'] for item in result.get('content', []) if item['type'] == 'text'] + calls.append({'tool': name, 'arguments': arguments, 'result': text}) + (artifacts / 'calls.json').write_text(json.dumps(calls, ensure_ascii=False, indent=2)) + for item in result.get('content', []): + if item['type'] == 'image' and screenshot: + (artifacts / screenshot).write_bytes(base64.b64decode(item['data'])) + print(name, flush=True) + return json.loads(text[0]) if text else {} + + +try: + initialization = rpc('initialize', {'protocolVersion': '2024-11-05', 'capabilities': {}, + 'clientInfo': {'name': 'chromium-font-test', 'version': '1'}}) + assert 'Chromium' in initialization['instructions'] + stream.write(b'{"jsonrpc":"2.0","method":"notifications/initialized"}\n') + call('get_app_state', {'include_screenshot': False}) + call('press_key', {'key': 'Escape'}) + call('press_key', {'key': 'alt+F2'}) + time.sleep(1) + call('type_text', {'text': 'xdg-open file:///workspace/' + filename}) + call('press_key', {'key': 'Return'}) + deadline = time.monotonic() + 45 + while True: + time.sleep(2) + windows = call('list_windows', {})['windows'] + if any(window.get('wm_class') == 'chromium' for window in windows): + break + assert time.monotonic() < deadline, windows + call('press_key', {'wm_class': 'chromium', 'key': 'ctrl+t'}) + call('type_text', {'wm_class': 'chromium', 'text': 'file:///workspace/' + filename}) + call('press_key', {'wm_class': 'chromium', 'key': 'Return'}) + deadline = time.monotonic() + 45 + while True: + time.sleep(2) + state = call('get_app_state', {'wm_class': 'chromium', 'include_screenshot': False, + 'max_depth': 20, 'max_nodes': 300}) + nodes = state.get('accessibility_tree', []) + fields = [node for node in nodes if node.get('name') == '中文输入' + and ('editable' in node.get('states', []) or node.get('supports_editable_text'))] + if fields: + break + assert time.monotonic() < deadline, state + call('list_windows', {}) + call('click', {'element_index': fields[0]['index'], 'snapshot_id': state['snapshot_id']}) + expected = 'Chromium 中文输入成功:当前分支文件可访问。' + call('type_text', {'wm_class': 'chromium', 'text': 'Chromium input test'}) + state = call('get_app_state', {'wm_class': 'chromium', 'include_screenshot': False, + 'max_depth': 20, 'max_nodes': 300}) + field = next(node for node in state['accessibility_tree'] + if node.get('name') == '中文输入' and node.get('role') == 'entry') + assert field.get('text', {}).get('content') == 'Chromium input test', field + windows = call('list_windows', {})['windows'] + window = next(window for window in windows if window.get('wm_class') == 'chromium' and window.get('focused')) + call('press_key', {'window_id': window['window_id'], 'key': 'ctrl+a'}) + call('paste_text', {'window_id': window['window_id'], 'text': expected}) + state = call('get_app_state', {'wm_class': 'chromium', 'include_screenshot': False, + 'max_depth': 20, 'max_nodes': 300}) + assert any(expected in json.dumps(node, ensure_ascii=False) + for node in state.get('accessibility_tree', [])), state + button = next(node for node in state['accessibility_tree'] if node.get('name') == '点击验证') + call('click', {'element_index': button['index'], 'snapshot_id': state['snapshot_id']}) + state = call('get_app_state', {'wm_class': 'chromium', 'include_screenshot': True, + 'max_depth': 20, 'max_nodes': 300}, 'chinese-fonts.png') + assert any('按钮点击成功' in json.dumps(node, ensure_ascii=False) + for node in state.get('accessibility_tree', [])), state + (artifacts / 'report.json').write_text(json.dumps({ + 'verified': True, 'browser': 'Chromium', 'chinese_input': expected, + 'new_tab': True, 'button_clicked': True, + 'workspace_file': filename, 'method': 'computer-use-linux MCP'}, ensure_ascii=False, indent=2)) + print('VERIFIED: Chromium 中文输入及当前分支网页访问成功') +finally: + connection.close() + fixture.unlink(missing_ok=True) diff --git a/tests/sandbox/delete-e2e.cjs b/tests/sandbox/delete-e2e.cjs new file mode 100644 index 0000000..2ee3c38 --- /dev/null +++ b/tests/sandbox/delete-e2e.cjs @@ -0,0 +1,83 @@ +// Run against a local daemon with one available Sandbox slot. +const {chromium} = require('../dashboard/node_modules/playwright'); +const {execFile} = require('node:child_process'); +const {promisify} = require('node:util'); +const fs = require('node:fs/promises'); +const path = require('node:path'); +const assert = require('node:assert/strict'); +const {randomUUID} = require('node:crypto'); +const root = path.resolve(__dirname, '../..'); +const base = process.env.AGENTDRIVE_E2E_URL || 'http://127.0.0.1:17421'; +const artifacts = path.join(root, '.agentdrive/verification/sandbox-delete'); +async function request(url, method='GET', body) { + const response = await fetch(base+url, {method, headers:{Origin:base,'Content-Type':'application/json'}, body:body===undefined?undefined:JSON.stringify(body), signal:AbortSignal.timeout(150000)}); + const value = await response.json(); + assert(response.ok, JSON.stringify({status:response.status,value})); return value; +} +async function main() { + await fs.mkdir(artifacts,{recursive:true}); + const config = JSON.parse(await fs.readFile(path.join(root,'.agentdrive/sandbox/runtime.json'),'utf8')); + const templateBefore = await fs.stat(config.image); + const fixture = JSON.parse((await promisify(execFile)(path.join(root,'target/debug/agentdrive'), ['--endpoint',base+'/mcp','call','workspace_create','--request-id',randomUUID(),'--json',JSON.stringify({name:`Sandbox deletion E2E ${new Date().toISOString()}`})])).stdout); + assert(!fixture.error,JSON.stringify(fixture)); + const {workspace,branch} = fixture; + const endpoint = `/api/workspaces/${workspace}/branches/${branch}/sandbox`; + const directory = path.join(config.directory,branch), mount = path.join(config.mount_root,branch); + const report = {workspace,branch,checks:[]}; + const record = name => {report.checks.push(name); console.log(name);}; + const browser = await chromium.launch({headless:true}); + const page = await browser.newPage({viewport:{width:1440,height:1000}}); + const errors=[]; page.on('pageerror',e=>errors.push(e.message)); + try { + await request(endpoint,'POST'); + const marker = `preserved branch file ${randomUUID()}\n`; + await fs.mkdir(path.join(mount,'Downloads'),{recursive:true}); + await fs.writeFile(path.join(mount,'Downloads/delete-preservation.txt'),marker); + const rejected = await fetch(base+endpoint+'/delete',{method:'POST',headers:{Origin:base,'Content-Type':'application/json'},body:JSON.stringify({confirm_branch:branch})}); + assert(!rejected.ok); assert.equal((await request(endpoint)).sandbox.state,'ready'); + record('running_instance_deletion_rejected'); + await request(endpoint,'DELETE'); + const before = await request(`/api/workspaces/${workspace}`); + const history = before.revisions.map(r=>r.id); + await page.goto(base+'/sandboxes'); + const card = page.locator(`.sandbox-card[data-branch="${branch}"]`); + await card.getByRole('button',{name:'删除 Sandbox',exact:true}).click(); + const dialog = page.getByRole('dialog',{name:'删除 Sandbox',exact:true}); + await dialog.getByRole('textbox').fill('main'); + await page.screenshot({path:path.join(artifacts,'confirmation.png'),fullPage:true}); + await dialog.getByRole('button',{name:'取消'}).click(); + await fs.access(path.join(directory,'ubuntu.ext4')); record('cancellation_preserves_disk'); + await card.getByRole('button',{name:'删除 Sandbox',exact:true}).click(); + await dialog.getByRole('textbox').fill('main'); + const deleted = page.waitForResponse(r=>r.url().endsWith(endpoint+'/delete') && r.request().method()==='POST'); + await dialog.getByRole('button',{name:'确认删除'}).click(); + assert((await deleted).ok()); await card.waitFor({state:'hidden'}); + await assert.rejects(fs.access(directory)); + assert.equal((await request(endpoint)).sandbox,null); + assert.equal(await fs.readFile(path.join(mount,'Downloads/delete-preservation.txt'),'utf8'),marker); + const after = await request(`/api/workspaces/${workspace}`); + assert(after.branches.some(b=>b.id===branch)); + assert(history.every(id=>after.revisions.some(r=>r.id===id))); + assert(after.status.mounts.some(m=>m.branch===branch && m.state==='ready')); + record('deletion_removes_disk_and_preserves_branch_files_history_and_mount'); + const templateAfter = await fs.stat(config.image); + assert.equal(templateAfter.mtimeMs,templateBefore.mtimeMs); assert.equal(templateAfter.size,templateBefore.size); + record('template_unchanged'); + await page.getByRole('combobox',{name:'筛选沙盒状态'}).selectOption('idle'); + await card.getByRole('button',{name:'启动 Sandbox',exact:true}).click(); + await card.getByRole('link',{name:'打开桌面'}).waitFor({timeout:150000}); + await fs.access(path.join(directory,'ubuntu.ext4')); + assert.equal(await fs.readFile(path.join(mount,'Downloads/delete-preservation.txt'),'utf8'),marker); + record('recreation_uses_template_and_retains_branch_file'); + assert.deepEqual(errors,[]); + await request(endpoint,'DELETE'); await request(endpoint+'/delete','POST',{confirm_branch:branch}); + record('test_instance_cleaned_up'); + await page.getByRole('combobox',{name:'筛选沙盒状态'}).selectOption('existing'); + await page.screenshot({path:path.join(artifacts,'catalog.png'),fullPage:true}); + report.passed=true; + } finally { + await fs.writeFile(path.join(artifacts,'report.json'),JSON.stringify(report,null,2)); + await browser.close(); + } +} +main().catch(error=>{console.error(error);process.exitCode=1;}); diff --git a/tests/sandbox/desktop_probe.py b/tests/sandbox/desktop_probe.py new file mode 100644 index 0000000..bf7d93b --- /dev/null +++ b/tests/sandbox/desktop_probe.py @@ -0,0 +1,49 @@ +import socket,json,time,base64,argparse,uuid +from pathlib import Path +parser=argparse.ArgumentParser(description='Verify guest desktop input and branch file persistence through MCP') +parser.add_argument('--socket',required=True) +parser.add_argument('--mount',required=True) +parser.add_argument('--artifacts',required=True) +args=parser.parse_args() +artifacts=Path(args.artifacts);artifacts.mkdir(parents=True,exist_ok=True) +mount=Path(args.mount) +filename='desktop-proof-'+uuid.uuid4().hex+'.txt' +s=socket.socket(socket.AF_UNIX,socket.SOCK_STREAM);s.settimeout(45);s.connect(args.socket);f=s.makefile('rwb',buffering=0) +f.write(b'CONNECT 7000\n');assert f.readline().startswith(b'OK ') +i=0;report=[] +def rpc(method,params): + global i + i+=1;f.write((json.dumps(dict(jsonrpc='2.0',id=i,method=method,params=params))+'\n').encode()) + while True: + value=json.loads(f.readline()) + if value.get('id')==i:return value +rpc('initialize',dict(protocolVersion='2024-11-05',capabilities={},clientInfo=dict(name='agentdrive-integration-test',version='1'))) +f.write(b'{"jsonrpc":"2.0","method":"notifications/initialized"}\n') +def call(name,args): + result=rpc('tools/call',dict(name=name,arguments=args)) + assert 'error' not in result,result + result=result['result'];assert not result.get('isError'),result + texts=[c['text'] for c in result.get('content',[]) if c['type']=='text'];report.append(dict(tool=name,args=args,result=texts));print(name,str(texts)[:220],flush=True) + for c in result.get('content',[]): + if c['type']=='image':(artifacts/'desktop.png').write_bytes(base64.b64decode(c['data'])) + return texts +call('get_app_state',{'include_screenshot':False}) +call('setup_accessibility',{}) +call('press_key',{'key':'Escape'}) +call('press_key',{'key':'alt+F2'}) +time.sleep(1) +call('type_text',{'text':'gedit /workspace/'+filename}) +call('press_key',{'key':'Return'}) +time.sleep(6) +call('list_windows',{}) +call('get_app_state',{'app_name_or_bundle_identifier':'gedit','include_screenshot':False}) +call('type_text',{'wm_class':'gedit','text':'Created through computer-use-linux inside Ubuntu.\nVerification: '+filename+'\n'}) +call('press_key',{'wm_class':'gedit','key':'ctrl+s'}) +time.sleep(3) +call('get_app_state',{'app_name_or_bundle_identifier':'gedit','include_screenshot':True}) +p=mount/filename +assert p.exists(),'File did not reach the branch mount' +text=p.read_text();assert filename in text,text +(artifacts/'report.json').write_text(json.dumps(dict(mount=str(mount),file=filename,content=text,calls=report),indent=2)) +print('VERIFIED BRANCH FILE:',text) +s.close() diff --git a/tests/sandbox/e2e.cjs b/tests/sandbox/e2e.cjs new file mode 100644 index 0000000..8a5aa4e --- /dev/null +++ b/tests/sandbox/e2e.cjs @@ -0,0 +1,101 @@ +// Run against a real local daemon and KVM-backed Ubuntu template. +const {chromium} = require('../dashboard/node_modules/playwright'); +const {execFile} = require('node:child_process'); +const {promisify} = require('node:util'); +const fs = require('node:fs/promises'); +const path = require('node:path'); +const assert = require('node:assert/strict'); +const exec = promisify(execFile); +const root = path.resolve(__dirname, '../..'); +const endpoint = process.env.AGENTDRIVE_E2E_URL || 'http://127.0.0.1:17421'; +const configPath = process.env.AGENTDRIVE_SANDBOX_CONFIG || path.join(root,'.agentdrive/sandbox/runtime.json'); +const runId = new Date().toISOString().replace(/[:.]/g,'-'); +const artifacts = path.join(root,'.agentdrive/verification',`e2e-${runId}`); +async function main() { + await fs.mkdir(artifacts,{recursive:true}); + const config=JSON.parse(await fs.readFile(configPath,'utf8')); + const report={started_at:new Date().toISOString(),endpoint,checks:[],startup_samples:[]}; + const record=(name,detail={})=>{report.checks.push({name,...detail});console.log(name,JSON.stringify(detail));}; + const api=async(url,options={})=>{const response=await fetch(endpoint+url,{...options,signal:AbortSignal.timeout(150000)});assert(response.ok,`${response.status}: ${await response.clone().text()}`);return response;}; + const fixture=JSON.parse((await exec(path.join(root,'target/debug/agentdrive'),['--endpoint',endpoint+'/mcp','call','workspace_create','--request-id',`e2e-${runId}`,'--json',JSON.stringify({name:`Sandbox E2E ${runId}`})])).stdout); + assert(!fixture.error,JSON.stringify(fixture)); + const workspace=fixture.workspace, sourceBranch=fixture.branch; + report.workspace=workspace;report.source_branch=sourceBranch; + const browser=await chromium.launch({headless:true}); + const context=await browser.newContext({viewport:{width:1440,height:1080}}); + await context.tracing.start({screenshots:true,snapshots:true,sources:true}); + const page=await context.newPage(); + const pageErrors=[];context.on('page',p=>p.on('pageerror',e=>pageErrors.push(e.message)));page.on('pageerror',e=>pageErrors.push(e.message)); + let timer; + try { + await page.goto(`${endpoint}/branches?workspace=${workspace}`); + const name=`feat/e2e-${runId}`; + await page.getByRole('textbox',{name:'分支名称',exact:true}).fill(name); + await page.getByRole('button',{name:'创建分支',exact:true}).click(); + const row=page.locator('article.management-row').filter({has:page.getByRole('heading',{name,exact:true})}); + await row.waitFor({timeout:30000}); + const branch=await row.getAttribute('data-branch');report.branch=branch; + const sandboxPath=`/api/workspaces/${workspace}/branches/${branch}/sandbox`; + const filesPath=id=>`/api/workspaces/${workspace}/branches/${id}`; + record('branch_created_in_browser',{branch}); + let sampling=false; + timer=setInterval(async()=>{ + if(sampling)return;sampling=true;const start=Date.now(); + try {const result=await (await fetch(endpoint+sandboxPath,{signal:AbortSignal.timeout(2000)})).json();report.startup_samples.push({elapsed_ms:Date.now()-start,...result.sandbox});}catch(e){report.startup_samples.push({error:e.message});}finally{sampling=false;} + },200); + await row.getByRole('button',{name:'Sandbox',exact:true}).click(); + const controls=page.getByRole('dialog'); + await controls.getByRole('button',{name:'启动 Sandbox',exact:true}).click(); + await controls.getByRole('link',{name:'打开桌面'}).waitFor({timeout:150000}); + clearInterval(timer);timer=null; + assert.equal(await controls.locator('[data-state="complete"]').count(),4); + assert(report.startup_samples.some(s=>['creating','starting'].includes(s.state)),'Startup status was never observable'); + assert(report.startup_samples.every(s=>!s.error),'Status polling stalled'); + await page.screenshot({path:path.join(artifacts,'branch-ready.png'),fullPage:true}); + const first=await (await api(sandboxPath)).json(); + const repeated=await (await api(sandboxPath,{method:'POST'})).json(); + assert.equal(first.sandbox.id,repeated.id); + record('sandbox_created_with_live_progress',{steps:4,reused_id:repeated.id}); + async function openDesktop(label){ + const popupPromise=page.waitForEvent('popup');await controls.getByRole('link',{name:'打开桌面'}).click(); + const desktop=await popupPromise;await desktop.waitForFunction(()=>document.title.endsWith(' - noVNC'),null,{timeout:30000}); + await desktop.screenshot({path:path.join(artifacts,`${label}.png`)});record(label,{title:await desktop.title()});return desktop; + } + let desktop=await openDesktop('desktop_connected'); + async function verifyNetwork(label) { + if (!config.networks?.length) return; + const output=await exec('python3',[path.join(root,'tests/sandbox/network_probe.py'),'--socket',path.join(config.directory,branch,'vsock.sock'),'--artifacts',path.join(artifacts,label)],{timeout:180000,maxBuffer:4*1024*1024}); + await fs.writeFile(path.join(artifacts,`${label}.log`),output.stdout+output.stderr); + const vm=JSON.parse(await fs.readFile(path.join(config.directory,branch,'vm.json'),'utf8')); + record(label,{tap:vm.net[0].tap,websites:['https://example.com','https://www.python.org/']}); + } + await verifyNetwork('browser_internet_access'); + const probeDir=path.join(artifacts,'computer-use'); + const probe=await exec('python3',[path.join(root,'tests/sandbox/desktop_probe.py'),'--socket',path.join(config.directory,branch,'vsock.sock'),'--mount',path.join(config.mount_root,branch),'--artifacts',probeDir],{timeout:90000,maxBuffer:4*1024*1024}); + await fs.writeFile(path.join(artifacts,'computer-use.log'),probe.stdout+probe.stderr); + const result=JSON.parse(await fs.readFile(path.join(probeDir,'report.json'),'utf8')); + async function readSaved(){ + const listing=await (await api(filesPath(branch)+'/files?path=%2F')).json(); + const query=new URLSearchParams({path:'/'+result.file,version:listing.version,offset:'0',size:'65536'}); + const content=await (await api(filesPath(branch)+'/file?'+query)).text();assert.equal(content,result.content);return content; + } + await readSaved(); + const sourceFiles=await (await api(filesPath(sourceBranch)+'/files?path=%2F')).json(); + assert(!JSON.stringify(sourceFiles.entries).includes(result.file),'Guest file escaped into source branch'); + record('computer_use_saved_current_branch_file',{file:result.file,content:result.content}); + record('source_branch_unchanged'); + await desktop.screenshot({path:path.join(artifacts,'desktop-file-saved.png')});await desktop.close(); + await controls.getByRole('button',{name:'停止 Sandbox'}).click(); + await controls.getByRole('button',{name:'启动 Sandbox',exact:true}).waitFor({timeout:40000}); + assert.equal((await (await api(sandboxPath)).json()).sandbox.state,'stopped');await readSaved();record('stop_preserved_branch_file'); + await controls.getByRole('button',{name:'启动 Sandbox',exact:true}).click(); + await controls.getByRole('link',{name:'打开桌面'}).waitFor({timeout:150000}); + assert.equal((await (await api(sandboxPath)).json()).sandbox.id,repeated.id); + desktop=await openDesktop('desktop_reconnected_after_restart');await readSaved();await desktop.close();record('restart_preserved_instance_and_file'); + await verifyNetwork('browser_internet_after_restart'); + await controls.getByRole('button',{name:'停止 Sandbox'}).click();await controls.getByRole('button',{name:'启动 Sandbox',exact:true}).waitFor({timeout:40000}); + assert.deepEqual(pageErrors,[]);report.result='passed';report.finished_at=new Date().toISOString(); + } catch(error) {report.result='failed';report.error=error.stack;await page.screenshot({path:path.join(artifacts,'failure.png')}).catch(()=>{});throw error;} + finally {clearInterval(timer);await fs.writeFile(path.join(artifacts,'report.json'),JSON.stringify(report,null,2));await context.tracing.stop({path:path.join(artifacts,'trace.zip')});await browser.close();console.log('Artifacts:',artifacts);} +} +main().catch(error=>{console.error(error);process.exitCode=1;}); diff --git a/tests/sandbox/http_proxy_fixture.py b/tests/sandbox/http_proxy_fixture.py new file mode 100644 index 0000000..f741c16 --- /dev/null +++ b/tests/sandbox/http_proxy_fixture.py @@ -0,0 +1,39 @@ +"""仅监听本机的代理验证服务,记录目标地址,不记录请求头或内容。""" +import argparse +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +import select +import socket + + +class Proxy(BaseHTTPRequestHandler): + def do_CONNECT(self): + host, port = self.path.rsplit(':', 1) + try: + remote = socket.create_connection((host, int(port)), timeout=10) + except OSError: + self.send_error(502) + return + print('CONNECT', self.path, flush=True) + self.send_response(200) + self.end_headers() + remote.settimeout(None) + with remote: + while True: + readable, _, _ = select.select([self.connection, remote], [], [], 60) + if not readable: + return + for source in readable: + data = source.recv(65536) + if not data: + return + (remote if source is self.connection else self.connection).sendall(data) + + def log_message(self, *_args): + pass + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--port', type=int, default=18089) + args = parser.parse_args() + ThreadingHTTPServer(('127.0.0.1', args.port), Proxy).serve_forever() diff --git a/tests/sandbox/mcp-fixture.py b/tests/sandbox/mcp-fixture.py new file mode 100644 index 0000000..bda2629 --- /dev/null +++ b/tests/sandbox/mcp-fixture.py @@ -0,0 +1,27 @@ +"""用于验证统一入口的 MCP 子进程。""" +import json +import sys +import time + +for line in sys.stdin: + request = json.loads(line) + if 'id' not in request: + continue + method = request['method'] + if method == 'initialize': + result = {'protocolVersion': request['params']['protocolVersion'], + 'serverInfo': {'name': sys.argv[1], 'version': '1'}, 'capabilities': {'tools': {}}} + elif method == 'tools/list': + second = request.get('params', {}).get('cursor') == 'second' + result = {'tools': [{'name': 'last' if second else 'echo', 'description': '验证工具', + 'inputSchema': {'type': 'object', 'properties': {'value': {'type': 'string'}}}}]} + if not second: + result['nextCursor'] = 'second' + else: + time.sleep(0.05) + params = request.get('params', {}) + result = {'content': [{'type': 'text', 'text': json.dumps({'provider': sys.argv[1], 'name': params.get('name'), 'arguments': params.get('arguments')})}, + {'type': 'image', 'mimeType': 'image/png', 'data': 'dGVzdA=='}]} + if params.get('arguments', {}).get('value') == 'failure': + result['isError'] = True + print(json.dumps({'jsonrpc': '2.0', 'id': request['id'], 'result': result}), flush=True) diff --git a/tests/sandbox/mcp-gateway.test.mjs b/tests/sandbox/mcp-gateway.test.mjs new file mode 100644 index 0000000..b203636 --- /dev/null +++ b/tests/sandbox/mcp-gateway.test.mjs @@ -0,0 +1,59 @@ +import {test} from 'node:test'; +import assert from 'node:assert/strict'; +import {fileURLToPath} from 'node:url'; +import {mkdtemp,writeFile,rm} from 'node:fs/promises'; +import {tmpdir} from 'node:os'; +import {join} from 'node:path'; +import {Client} from '../../build/sandbox/mcp/node_modules/@modelcontextprotocol/sdk/dist/esm/client/index.js'; +import {InMemoryTransport} from '../../build/sandbox/mcp/node_modules/@modelcontextprotocol/sdk/dist/esm/inMemory.js'; +import {StdioClientTransport} from '../../build/sandbox/mcp/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js'; +import {createGateway} from '../../build/sandbox/mcp/gateway.mjs'; + +const fixture=fileURLToPath(new URL('./mcp-fixture.py',import.meta.url)); +const provider=(id,prefix='')=>({id,prefix,command:'python3',args:[fixture,id]}); +async function setup(t,providers){ + const gateway=await createGateway({cwd:tmpdir(),providers}); + const [left,right]=InMemoryTransport.createLinkedPair(); + const client=new Client({name:'test-agent',version:'1'}); + t.after(async()=>{await gateway.close();await client.close();}); + await gateway.server.connect(left);await client.connect(right); + return client; +} +test('one connection aggregates paginated tools from three providers and preserves results',async t=>{ + const client=await setup(t,[provider('desktop'),provider('browser','browser_'),provider('files','files_')]); + const {tools}=await client.listTools();assert.equal(tools.length,7); + assert.equal(new Set(tools.map(t=>t.name)).size,7); + for(const [name,id] of [['echo','desktop'],['browser_echo','browser'],['files_last','files']]){ + const result=await client.callTool({name,arguments:{value:'中文😀'}}); + const value=JSON.parse(result.content[0].text);assert.equal(value.provider,id);assert.equal(value.arguments.value,'中文😀');assert.equal(value.name,name.split('_').at(-1)); + assert.equal(result.content[1].data,'dGVzdA=='); + } + assert.equal((await client.callTool({name:'browser_echo',arguments:{value:'failure'}})).isError,true); + const status=JSON.parse((await client.callTool({name:'sandbox_tools_status'})).content[0].text); + assert(status.providers.every(p=>p.connected)); +}); +test('duplicate tool names are reported and unknown tools are rejected',async t=>{ + const client=await setup(t,[provider('desktop'),provider('browser')]); + await assert.rejects(()=>client.listTools(),/工具名称重复/); + const single=await setup(t,[provider('desktop')]); + await assert.rejects(()=>single.callTool({name:'missing'}),/未知工具/); +}); +test('one unavailable provider leaves other tools usable and reports its failure',async t=>{ + const client=await setup(t,[provider('desktop'),{id:'broken',command:'/missing-mcp-server',args:[],prefix:'broken_'}]); + assert.equal((await client.listTools()).tools.length,3); + assert(!(await client.callTool({name:'echo',arguments:{value:'works'}})).isError); + const status=JSON.parse((await client.callTool({name:'sandbox_tools_status'})).content[0].text); + assert.equal(status.providers[1].connected,false);assert(status.providers[1].error); +}); +test('stdio connection negotiates once and disconnect closes its children',async t=>{ + const directory=await mkdtemp(join(tmpdir(),'agentdrive-gateway-')); + const config=join(directory,'providers.json');await writeFile(config,JSON.stringify({cwd:directory,providers:[provider('desktop'),provider('browser','browser_')]})); + const transport=new StdioClientTransport({command:process.execPath,args:[fileURLToPath(new URL('../../build/sandbox/mcp/gateway.mjs',import.meta.url)),config],stderr:'pipe'}); + const client=new Client({name:'external-agent',version:'1'}); + t.after(async()=>{await client.close();await rm(directory,{recursive:true,force:true});}); + await client.connect(transport);assert.equal(client.getServerVersion().name,'agentdrive-sandbox'); + assert.equal((await client.listTools()).tools.length,5); + const pid=transport.pid;await client.close(); + await new Promise(resolve=>setTimeout(resolve,200)); + assert.throws(()=>process.kill(pid,0),/ESRCH/); +}); diff --git a/tests/sandbox/network_probe.py b/tests/sandbox/network_probe.py new file mode 100644 index 0000000..159f022 --- /dev/null +++ b/tests/sandbox/network_probe.py @@ -0,0 +1,94 @@ +"""Verify outbound HTTPS using the guest browser and computer-use-linux MCP.""" +import argparse +import base64 +import json +import socket +import time +import uuid +from pathlib import Path + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument('--socket', required=True) +parser.add_argument('--artifacts', required=True) +args = parser.parse_args() +artifacts = Path(args.artifacts) +artifacts.mkdir(parents=True, exist_ok=True) +connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) +connection.settimeout(60) +connection.connect(args.socket) +stream = connection.makefile('rwb', buffering=0) +stream.write(b'CONNECT 7000\n') +assert stream.readline().startswith(b'OK ') +request_id = 0 +report = [] + + +def rpc(method, params): + global request_id + request_id += 1 + stream.write((json.dumps(dict(jsonrpc='2.0', id=request_id, + method=method, params=params)) + '\n').encode()) + while True: + line = stream.readline() + assert line, 'MCP connection closed' + response = json.loads(line) + if response.get('id') == request_id: + assert 'error' not in response, response + return response['result'] + + +def call(name, arguments, screenshot=None): + result = rpc('tools/call', dict(name=name, arguments=arguments)) + assert not result.get('isError'), result + texts = [entry['text'] for entry in result.get('content', []) if entry['type'] == 'text'] + report.append(dict(tool=name, arguments=arguments, result=texts)) + (artifacts / 'calls.json').write_text(json.dumps(report, indent=2)) + for entry in result.get('content', []): + if entry['type'] == 'image' and screenshot: + (artifacts / screenshot).write_bytes(base64.b64decode(entry['data'])) + print(name, str(texts)[:300], flush=True) + return '\n'.join(texts) + + +initialization = rpc('initialize', dict(protocolVersion='2024-11-05', capabilities={}, + clientInfo=dict(name='agentdrive-network-test', version='1'))) +assert all(value in initialization['instructions'] for value in ('chromium', 'xdg-open', '/workspace')) +stream.write(b'{"jsonrpc":"2.0","method":"notifications/initialized"}\n') +tools = rpc('tools/list', {})['tools'] +assert 'chromium' in next(tool['description'] for tool in tools if tool['name'] == 'list_apps') +call('get_app_state', {'include_screenshot': False}) +call('list_apps', {}) +call('setup_accessibility', {}) +call('press_key', {'key': 'Escape'}) +call('press_key', {'key': 'alt+F2'}) +time.sleep(1) +call('type_text', {'text': 'xdg-open https://example.com'}) +call('press_key', {'key': 'Return'}) +time.sleep(8) +call('list_windows', {}) +for index, (url, expected) in enumerate([ + ('https://example.com', 'Example Domain'), + ('https://www.python.org/', 'Welcome to Python.org'), + ('https://opencsg.com/', 'OpenCSG'), +]): + call('press_key', {'key': 'ctrl+l', 'wm_class': 'chromium'}) + target = url + ('&' if '?' in url else '?') + 'agentdrive_probe=' + uuid.uuid4().hex + call('type_text', {'text': target, 'wm_class': 'chromium'}) + call('press_key', {'key': 'Return', 'wm_class': 'chromium'}) + deadline = time.monotonic() + 60 + while True: + time.sleep(3) + state = call('get_app_state', {'app_name_or_bundle_identifier': 'chromium', + 'include_screenshot': False}) + tree = json.loads(state).get('accessibility_tree', []) + if any(node.get('role') == 'frame' and expected.lower() in node.get('name', '').lower() for node in tree): + break + assert time.monotonic() < deadline, f'Browser did not load {url}: {state}' + call('get_app_state', {'app_name_or_bundle_identifier': 'chromium', + 'include_screenshot': True}, f'website-{index + 1}.png') +(artifacts / 'report.json').write_text(json.dumps(dict( + verified=True, websites=['https://example.com', 'https://www.python.org/', 'https://opencsg.com/'], + method='computer-use-linux MCP keyboard input and browser accessibility', + calls=report), indent=2)) +connection.close() +print('VERIFIED: Ubuntu browser loaded all HTTPS websites through computer-use-linux') diff --git a/tests/sandbox/playwright_probe.py b/tests/sandbox/playwright_probe.py new file mode 100644 index 0000000..66695c0 --- /dev/null +++ b/tests/sandbox/playwright_probe.py @@ -0,0 +1,135 @@ +"""通过同一份 MCP 配置验证浏览器、桌面和分支下载。""" +import argparse +import base64 +import json +from pathlib import Path +import re +import subprocess +import time +import urllib.request +import uuid + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument('--base', default='http://127.0.0.1:17421') +parser.add_argument('--workspace', required=True) +parser.add_argument('--branch', required=True) +parser.add_argument('--mount', required=True) +parser.add_argument('--artifacts', required=True) +args = parser.parse_args() +artifacts = Path(args.artifacts) +artifacts.mkdir(parents=True, exist_ok=True) +mount = Path(args.mount) +identifier = uuid.uuid4().hex +filename = 'playwright-proof-' + identifier + '.txt' +fixture = mount / ('playwright-proof-' + identifier + '.html') +payload = 'AgentDrive Playwright MCP 下载验证\n' + identifier + '\n' +fixture.write_text('''AgentDrive Playwright Verification + +

AgentDrive Playwright 验证

等待点击

+ +''') +endpoint = f'{args.base}/api/workspaces/{args.workspace}/branches/{args.branch}/sandbox/mcp-config' +config = json.load(urllib.request.urlopen(endpoint)) +assert len(config['mcpServers']) == 1, config +spec = next(iter(config['mcpServers'].values())) +report = {'branch': args.branch, 'mcp_servers': list(config['mcpServers']), 'calls': []} +process = None +sequence = 0 + + +def start(): + global process + process = subprocess.Popen([spec['command'], *spec['args']], stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=(artifacts / 'proxy.log').open('ab'), text=True, bufsize=1) + response = rpc('initialize', {'protocolVersion': '2024-11-05', 'capabilities': {}, + 'clientInfo': {'name': 'agentdrive-playwright-verification', 'version': '1'}}) + assert response['serverInfo']['name'] == 'agentdrive-sandbox', response + process.stdin.write(json.dumps({'jsonrpc': '2.0', 'method': 'notifications/initialized'}) + '\n') + process.stdin.flush() + return response + + +def rpc(method, params): + global sequence + sequence += 1 + process.stdin.write(json.dumps({'jsonrpc': '2.0', 'id': sequence, 'method': method, 'params': params}) + '\n') + process.stdin.flush() + while True: + line = process.stdout.readline() + assert line, 'MCP 连接提前关闭' + value = json.loads(line) + if value.get('id') == sequence: + assert 'error' not in value, value + return value['result'] + + +def call(name, arguments=None): + result = rpc('tools/call', {'name': name, 'arguments': arguments or {}}) + assert not result.get('isError'), result + texts = '\n'.join(item['text'] for item in result.get('content', []) if item['type'] == 'text') + report['calls'].append({'tool': name, 'arguments': arguments, 'text': texts}) + for item in result.get('content', []): + if item['type'] == 'image': + (artifacts / (name + '.png')).write_bytes(base64.b64decode(item['data'])) + print(name, texts[:240], flush=True) + return texts + + +def click_button(label): + snapshot = call('browser_snapshot') + match = re.search(r'button "' + re.escape(label) + r'".*?\[ref=([^\]]+)\]', snapshot) + assert match, snapshot + return call('browser_click', {'element': label, 'target': match.group(1)}) + + +try: + report['initialize'] = start() + tools = rpc('tools/list', {})['tools'] + report['tools'] = [tool['name'] for tool in tools] + assert len(set(report['tools'])) == len(tools) + assert {'browser_navigate', 'browser_tabs', 'browser_click', 'list_windows', 'get_app_state', 'paste_text'} <= set(report['tools']) + call('sandbox_tools_status') + assert 'Example Domain' in call('browser_navigate', {'url': 'https://example.com'}) + call('browser_tabs', {'action': 'new'}) + assert 'Python' in call('browser_navigate', {'url': 'https://www.python.org'}) + call('browser_tabs', {'action': 'new'}) + call('browser_evaluate', {'function': '() => { document.open(); document.write(' + json.dumps(fixture.read_text()) + '); document.close(); return document.title; }'}) + click_button('点击验证') + assert '点击成功' in call('browser_snapshot') + windows = call('list_windows') + assert 'chromium' in windows.lower(), windows + desktop = call('get_app_state', {'app_name_or_bundle_identifier': 'chromium', 'include_screenshot': True}) + assert 'AgentDrive Playwright' in desktop, desktop + click_button('下载到当前分支') + expected = mount / 'Downloads' / filename + for _ in range(100): + if expected.exists() and expected.read_text() == payload: + break + time.sleep(.2) + else: + raise AssertionError('下载文件没有进入当前分支的 Downloads:' + str(list((mount / 'Downloads').glob('*')))) + report['download'] = {'path': str(expected), 'content': expected.read_text()} + print('BRANCH DOWNLOAD VERIFIED', expected, flush=True) + call('browser_tabs', {'action': 'list'}) + process.stdin.close() + process.wait(timeout=15) + assert process.returncode == 0 + start() + rpc('tools/list', {}) + assert 'AgentDrive Playwright Verification' in call('browser_tabs', {'action': 'list'}) + call('get_app_state', {'app_name_or_bundle_identifier': 'chromium', 'include_screenshot': True}) + report['reconnect_preserves_browser'] = True + (artifacts / 'report.json').write_text(json.dumps(report, ensure_ascii=False, indent=2)) + print('UNIFIED MCP VERIFIED', flush=True) +finally: + if process and process.poll() is None: + if not process.stdin.closed: + process.stdin.close() + try: + process.wait(timeout=15) + except subprocess.TimeoutExpired: + process.terminate() + fixture.unlink(missing_ok=True) diff --git a/tests/sandbox/test_assistant_service.py b/tests/sandbox/test_assistant_service.py new file mode 100644 index 0000000..61030d5 --- /dev/null +++ b/tests/sandbox/test_assistant_service.py @@ -0,0 +1,26 @@ +"""验证助手使用实例提供的访问地址,并覆盖已有配置中的旧地址。""" +import importlib.util +from pathlib import Path +import types +import unittest +from unittest.mock import patch + +spec = importlib.util.spec_from_file_location('assistant_service', Path(__file__).resolve().parents[2] / 'build/sandbox/assistant/assistant-service.py') +assistant = importlib.util.module_from_spec(spec) +with patch('pwd.getpwnam', return_value=types.SimpleNamespace(pw_uid=1000, pw_gid=1000, pw_dir='/home/user')): + spec.loader.exec_module(assistant) + + +class AssistantAddressTest(unittest.TestCase): + def test_each_instance_and_port_use_their_current_origin(self): + for port in (17421, 18090): + origin = f'http://csgclaw.b9e2497d-fefd-4f70-a3e5-4e330a22a1ab.25dd1802-2701-4ccb-9f5f-407268695f37.localhost:{port}' + command = assistant.assistant_command({'advertise_base_url': origin}) + self.assertEqual(command, ['/opt/csgclaw/bin/csgclaw', '--endpoint', origin, 'serve', '--no-browser']) + + def test_rejects_external_or_malformed_origins(self): + for value in ['http://example.com:17421', 'http://127.0.0.1:18080', + 'http://csgclaw.invalid.branch.localhost:17421', + 'http://csgclaw.b9e2497d-fefd-4f70-a3e5-4e330a22a1ab.25dd1802-2701-4ccb-9f5f-407268695f37.localhost:17421/path']: + with self.subTest(value=value), self.assertRaises(ValueError): + assistant.assistant_command({'advertise_base_url': value}) diff --git a/tests/sandbox/test_chromium_installer.py b/tests/sandbox/test_chromium_installer.py new file mode 100644 index 0000000..3e37075 --- /dev/null +++ b/tests/sandbox/test_chromium_installer.py @@ -0,0 +1,59 @@ +"""验证 Chromium 安装包校验和安装权限。""" +import hashlib +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest +import zipfile + +spec = importlib.util.spec_from_file_location( + 'install_chromium', Path(__file__).resolve().parents[2] / 'build/sandbox/install-chromium.py') +installer = importlib.util.module_from_spec(spec) +spec.loader.exec_module(installer) + + +class ChromiumInstallerTest(unittest.TestCase): + def bundle(self, root, extra=None): + archive = root / 'chromium.zip' + with zipfile.ZipFile(archive, 'w') as output: + for name in ('chrome', 'chrome_crashpad_handler', 'chrome_sandbox'): + output.writestr('chrome-linux/' + name, b'fixture') + if extra: + output.writestr(extra, b'fixture') + manifest = root / 'chromium.json' + manifest.write_text(json.dumps({'revision': 'fixture', + 'sha256': hashlib.sha256(archive.read_bytes()).hexdigest()})) + return archive, manifest + + def test_checksum_mismatch_leaves_destination_absent(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + archive, manifest = self.bundle(root) + archive.write_bytes(archive.read_bytes() + b'corrupted') + with self.assertRaises(ValueError): + installer.install(archive, manifest, root / 'installed') + self.assertFalse((root / 'installed').exists()) + + def test_browser_and_sandbox_permissions_and_provenance(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + archive, manifest = self.bundle(root) + destination = root / 'installed' + installer.install(archive, manifest, destination) + self.assertEqual((destination / 'chrome').stat().st_mode & 0o7777, 0o755) + self.assertEqual((destination / 'chrome_sandbox').stat().st_mode & 0o7777, 0o4755) + self.assertEqual(json.loads((destination / 'agentdrive-build.json').read_text()), + json.loads(manifest.read_text())) + + def test_archive_paths_cannot_escape_extraction_directory(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + archive, manifest = self.bundle(root, '../outside') + with self.assertRaises(ValueError): + installer.install(archive, manifest, root / 'installed') + self.assertFalse((root / 'outside').exists()) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/sandbox/test_desktop_mcp.py b/tests/sandbox/test_desktop_mcp.py new file mode 100644 index 0000000..071a18c --- /dev/null +++ b/tests/sandbox/test_desktop_mcp.py @@ -0,0 +1,95 @@ +"""验证 MCP 环境说明保留上游响应及工具接口。""" +import copy +import json +import sys +import subprocess +import tempfile +import unittest +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[2] / 'build/sandbox')) +from desktop_environment import environment_instructions +from desktop_mcp import enrich, paste_request + + +class DesktopMCPTest(unittest.TestCase): + def test_stream_adapter_drains_responses_after_client_eof(self): + with tempfile.TemporaryDirectory() as directory: + server = Path(directory) / 'server.py' + server.write_text('''import json,sys,time +for line in sys.stdin: + request=json.loads(line) + time.sleep(0.02) + result={'tools':[]} if request['method']=='tools/list' else {'content':[{'type':'text','text':'unchanged'}]} + print(json.dumps({'jsonrpc':'2.0','id':request['id'],'result':result}),flush=True) +''') + source = str(Path(__file__).resolve().parents[2] / 'build/sandbox') + program = ('import sys;sys.path.insert(0,sys.argv[1]);import desktop_mcp as m;' + 'm.environment_info=lambda: {};m.environment_instructions=lambda info: "environment";' + 'original=m.subprocess.Popen;' + 'm.subprocess.Popen=lambda command,**kwargs: original([sys.executable,sys.argv[2]],**kwargs);' + 'sys.exit(m.main())') + messages = [{'jsonrpc': '2.0', 'id': 1, 'method': 'tools/list'}, + {'jsonrpc': '2.0', 'id': 2, 'method': 'tools/call'}] + result = subprocess.run([sys.executable, '-c', program, source, str(server)], + input=''.join(json.dumps(m)+'\n' for m in messages), + text=True, capture_output=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stderr) + output = [json.loads(line) for line in result.stdout.splitlines()] + self.assertEqual([item['id'] for item in output], [1, 2]) + self.assertEqual(output[0]['result']['tools'][0]['name'], 'paste_text') + self.assertEqual(output[1]['result']['content'][0]['text'], 'unchanged') + + def test_paste_preserves_request_and_window_identity(self): + original = {'jsonrpc': '2.0', 'id': 'paste-1', 'method': 'tools/call', + 'params': {'name': 'paste_text', 'arguments': {'window_id': 123, 'text': '中文😀'}}} + value, request = paste_request(original) + self.assertEqual(value, '中文😀') + self.assertEqual(request['id'], 'paste-1') + self.assertEqual(request['params'], {'name': 'press_key', 'arguments': {'window_id': 123, 'key': 'ctrl+v'}}) + self.assertEqual(original['params']['name'], 'paste_text') + + def test_paste_requires_an_explicit_window(self): + for window in (None, True, 0, '123'): + with self.assertRaises(ValueError): + paste_request({'id': 1, 'method': 'tools/call', 'params': { + 'name': 'paste_text', 'arguments': {'text': '中文', 'window_id': window}}}) + + def test_initialization_preserves_capabilities_and_upstream_instructions(self): + message = {'jsonrpc': '2.0', 'id': 3, 'result': { + 'protocolVersion': '2024-11-05', 'serverInfo': {'name': 'computer-use-linux'}, + 'capabilities': {'tools': {}}, 'instructions': 'upstream guidance'}} + result = enrich(message, 'desktop environment') + self.assertEqual(result['id'], 3) + self.assertEqual(result['result']['capabilities'], {'tools': {}}) + self.assertEqual(result['result']['instructions'], 'desktop environment\n\nupstream guidance') + + def test_environment_is_visible_in_discovery_without_changing_tool_schema(self): + tool = {'name': 'list_apps', 'description': 'installed applications', + 'inputSchema': {'type': 'object'}, 'annotations': {'readOnlyHint': True}} + original = copy.deepcopy(tool) + output = enrich({'result': {'tools': [tool]}}, 'Chromium: chromium')['result']['tools'][0] + self.assertIn('chromium', output['description']) + self.assertTrue(output['description'].endswith(original['description'])) + for key in ('inputSchema', 'annotations', 'name'): + self.assertEqual(output[key], original[key]) + + def test_screenshots_errors_and_notifications_are_unchanged(self): + for message in ( + {'id': 4, 'result': {'content': [{'type': 'image', 'data': 'image bytes'}]}}, + {'id': 5, 'error': {'code': -32602, 'message': 'invalid arguments'}}, + {'method': 'notifications/message', 'params': {'data': 'status'}}, + ): + self.assertEqual(enrich(copy.deepcopy(message), 'environment'), message) + + def test_missing_browser_and_mount_are_reported_without_claiming_availability(self): + info = {'browser': {'command': None, 'open_url_command': None}, + 'workspace': {'mounted': False}, 'network': {}} + context = environment_instructions(info) + self.assertNotIn('已安装 Chromium', context) + self.assertNotIn('已挂载到 /workspace', context) + self.assertIn('/workspace 当前未挂载', context) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/sandbox/test_guest_mcp_bridge.py b/tests/sandbox/test_guest_mcp_bridge.py new file mode 100644 index 0000000..f43b9a8 --- /dev/null +++ b/tests/sandbox/test_guest_mcp_bridge.py @@ -0,0 +1,48 @@ +"""验证统一 MCP 使用标准管道传输并处理客户端 EOF。""" +import importlib.util +import json +from pathlib import Path +import socket +import subprocess +import sys +import tempfile +import threading +import unittest +from unittest.mock import patch + +spec = importlib.util.spec_from_file_location('guest_helper', Path(__file__).resolve().parents[2] / 'build/sandbox/guest-helper.py') +helper = importlib.util.module_from_spec(spec) +spec.loader.exec_module(helper) + + +class GuestMcpBridgeTest(unittest.TestCase): + def test_client_half_close_receives_complete_response_and_releases_process(self): + left, right = socket.socketpair() + left.settimeout(5) + original = subprocess.Popen + children = [] + def launch(command, **kwargs): + self.assertEqual(kwargs['stdin'], subprocess.PIPE) + self.assertEqual(kwargs['stdout'], subprocess.PIPE) + child = original([sys.executable, '-c', 'import sys,time;data=sys.stdin.buffer.read();time.sleep(0.05);sys.stdout.buffer.write(data)'], stdin=kwargs['stdin'], stdout=kwargs['stdout'], stderr=kwargs['stderr']) + children.append(child) + return child + with tempfile.TemporaryFile() as log, patch.object(helper, 'open', return_value=log, create=True), patch.object(helper.subprocess, 'Popen', side_effect=launch): + thread = threading.Thread(target=helper.mcp_connection, args=(right, {})) + thread.start() + payload = json.dumps({'id': 1, 'result': {'content': [{'type': 'text', 'text': '中文验证'}]}}).encode() + b'\n' + left.sendall(payload) + left.shutdown(socket.SHUT_WR) + response = bytearray() + while len(response) < len(payload): + response.extend(left.recv(65536)) + thread.join(5) + self.assertFalse(thread.is_alive()) + self.assertEqual(response, payload) + self.assertIsNotNone(children[0].poll()) + left.close() + right.close() + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/sandbox/test_guest_readiness.py b/tests/sandbox/test_guest_readiness.py new file mode 100644 index 0000000..daf89b4 --- /dev/null +++ b/tests/sandbox/test_guest_readiness.py @@ -0,0 +1,51 @@ +"""验证启动只等待桌面连接和当前分支目录。""" +import importlib.util +import json +from pathlib import Path +import socket +import subprocess +import unittest +from unittest.mock import MagicMock, patch + +spec = importlib.util.spec_from_file_location('guest_helper', Path(__file__).resolve().parents[2] / 'build/sandbox/guest-helper.py') +helper = importlib.util.module_from_spec(spec) +spec.loader.exec_module(helper) + + +class GuestReadinessTest(unittest.TestCase): + def probe(self, mounted=True, manager=True, banner=b'RFB 003.008\n', connection_error=False): + client, server = socket.socketpair() + client.settimeout(2) + connection = MagicMock() + connection.__enter__.return_value.recv.return_value = banner + result = subprocess.CompletedProcess(['wmctrl', '-m'], 0 if manager else 1, b'GNOME Shell', b'') + try: + with patch.object(helper, 'desktop_session', return_value={'DISPLAY': ':0'}), \ + patch.object(helper.os.path, 'ismount', return_value=mounted), \ + patch.object(helper.subprocess, 'run', return_value=result) as run, \ + patch.object(helper.socket, 'create_connection', return_value=connection, + side_effect=OSError('连接尚未就绪') if connection_error else None) as connect: + helper.connection(server, 7001) + response = json.loads(client.recv(4096)) + self.assertEqual(run.call_count, 1) + self.assertEqual(run.call_args.args[0], ['wmctrl', '-m']) + return response, connect.call_args_list + finally: + client.close() + server.close() + + def test_ready_without_browser_launch_software_inspection_or_screenshot(self): + response, connections = self.probe() + self.assertEqual(response, {'ready': True}) + self.assertEqual([call.args[0] for call in connections], [('127.0.0.1', 5900), ('127.0.0.1', 6080)]) + + def test_incomplete_desktop_or_branch_mount_is_not_ready(self): + for options in ({'mounted': False}, {'manager': False}, {'banner': b'closed'}, {'connection_error': True}): + with self.subTest(options=options): + response, _ = self.probe(**options) + self.assertFalse(response['ready']) + self.assertTrue(response['error']) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/sandbox/test_mcp_proxy.py b/tests/sandbox/test_mcp_proxy.py new file mode 100644 index 0000000..25eaeea --- /dev/null +++ b/tests/sandbox/test_mcp_proxy.py @@ -0,0 +1,61 @@ +"""Check byte forwarding and connection failures in the stdio bridge.""" +import socket +import subprocess +import sys +import tempfile +import threading +import unittest +from pathlib import Path + +PROXY = Path(__file__).resolve().parents[2] / 'build/sandbox/mcp-proxy.py' + + +class ProxyTest(unittest.TestCase): + def test_preserves_response_after_handshake_and_drains_after_stdin_eof(self): + with tempfile.TemporaryDirectory() as directory: + path = str(Path(directory) / 'vsock.sock') + with socket.socket(socket.AF_UNIX) as server: + server.bind(path) + server.listen(1) + observed = [] + request = b'{"jsonrpc":"2.0","id":1,"method":"tools/list"}\n' * 4000 + response = b'{"jsonrpc":"2.0","id":1,"result":{"tools":[]}}\n' + + def guest(): + with server.accept()[0] as connection: + handshake = bytearray() + while not handshake.endswith(b'\n'): + handshake.extend(connection.recv(1)) + observed.append(bytes(handshake)) + connection.sendall(b'OK 1234\n' + response) + data = bytearray() + while True: + chunk = connection.recv(65536) + if not chunk: + break + data.extend(chunk) + observed.append(bytes(data)) + connection.sendall(response) + + thread = threading.Thread(target=guest, daemon=True) + thread.start() + result = subprocess.run([sys.executable, str(PROXY), '--socket', path], + input=request, capture_output=True, timeout=10) + thread.join(5) + self.assertFalse(thread.is_alive()) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(observed, [b'CONNECT 7000\n', request]) + self.assertEqual(result.stdout, response * 2) + + def test_missing_instance_reports_to_stderr(self): + with tempfile.TemporaryDirectory() as directory: + result = subprocess.run([sys.executable, str(PROXY), '--socket', + str(Path(directory) / 'missing.sock')], + input=b'', capture_output=True, timeout=10) + self.assertEqual(result.returncode, 1) + self.assertEqual(result.stdout, b'') + self.assertIn(b'Ensure the Sandbox is running', result.stderr) + + +if __name__ == '__main__': + unittest.main() From b0e0135a0db1c23e632d657e08ad32b0597a16a2 Mon Sep 17 00:00:00 2001 From: GatewayJ <835269233@qq.com> Date: Fri, 13 Nov 2026 22:32:34 +0800 Subject: [PATCH 02/12] feat: refresh dashboard styling and responsive interactions --- crates/agentdrive-mcp/dashboard/archive.css | 102 ++--- crates/agentdrive-mcp/dashboard/dashboard.css | 363 +++++++++--------- crates/agentdrive-mcp/dashboard/dashboard.js | 9 +- crates/agentdrive-mcp/dashboard/favicon.svg | 2 +- crates/agentdrive-mcp/dashboard/index.html | 33 +- .../agentdrive-mcp/dashboard/management.css | 58 +-- crates/agentdrive-mcp/dashboard/merges.css | 46 +-- .../agentdrive-mcp/dashboard/navigation.css | 152 ++++---- crates/agentdrive-mcp/dashboard/navigation.js | 12 +- crates/agentdrive-mcp/dashboard/sandbox.js | 8 +- crates/agentdrive-mcp/dashboard/sandboxes.css | 10 +- crates/agentdrive-mcp/dashboard/ui.css | 131 +++++++ crates/agentdrive-mcp/dashboard/ui.js | 36 ++ crates/agentdrive-mcp/src/web.rs | 2 + tests/dashboard/navigation.test.cjs | 55 +++ tests/dashboard/sandbox.test.cjs | 4 +- 16 files changed, 631 insertions(+), 392 deletions(-) create mode 100644 crates/agentdrive-mcp/dashboard/ui.css create mode 100644 crates/agentdrive-mcp/dashboard/ui.js diff --git a/crates/agentdrive-mcp/dashboard/archive.css b/crates/agentdrive-mcp/dashboard/archive.css index 01f33f2..c51d15c 100644 --- a/crates/agentdrive-mcp/dashboard/archive.css +++ b/crates/agentdrive-mcp/dashboard/archive.css @@ -1,78 +1,78 @@ /* The archive keeps project branches, file navigation and reading in one place. */ -.archive-page {max-width:1800px; padding:32px;} +.archive-page {max-width:1280px; padding:32px;} .archive-heading {margin-bottom:24px; align-items:flex-end; gap:20px;} .archive-heading h1 {font-size:30px; letter-spacing:-.8px;} -.archive-heading p {font-size:13px; line-height:1.7; color:#748279;} -.archive-scope {border:1px solid #dce8df; border-radius:20px; padding:7px 11px; font-size:11px; color:#587365; background:#f2f7f3; white-space:nowrap;} -.archive-workspace {display:grid; grid-template-columns:218px minmax(0,1fr); border:1px solid #dfe6e1; border-radius:12px; background:#fff; min-height:650px; overflow:hidden; box-shadow:0 6px 28px #263b2a04;} -.archive-branches {background:#f8faf8; border-right:1px solid #e3e9e4; padding:20px 12px; min-width:0;} +.archive-heading p {font-size:13px; line-height:1.7; color:var(--muted);} +.archive-scope {border:1px solid var(--line); border-radius:20px; padding:7px 11px; font-size:12px; color:var(--muted); background:var(--surface-muted); white-space:nowrap;} +.archive-workspace {display:grid; grid-template-columns:218px minmax(0,1fr); border:1px solid var(--line); border-radius:12px; background:var(--paper); min-height:650px; overflow:hidden; box-shadow:none;} +.archive-branches {background:var(--surface-muted); border-right:1px solid var(--line); padding:20px 12px; min-width:0;} .archive-list-heading {display:flex; align-items:center; justify-content:space-between; padding:0 8px 16px;} .archive-list-heading h2 {font-size:13px; margin:0; font-weight:600;} -.archive-branch-search,.file-filter {width:100%; min-width:0; border:1px solid #e0e6e1; padding:10px; border-radius:6px; background:#fff; color:#42584b; font-size:12px;} +.archive-branch-search,.file-filter {width:100%; min-width:0; border:1px solid var(--line); padding:10px; border-radius:6px; background:var(--paper); color:var(--ink); font-size:12px;} .archive-branch-search {margin-bottom:16px;} .archive-branch-list {max-height:60vh; overflow:auto;} -.archive-branch {width:100%; border:1px solid transparent; background:none; padding:12px 10px; border-radius:7px; text-align:left; margin-bottom:6px; color:#596b61;} -.archive-branch:hover {background:#edf3ee;} -.archive-branch.active {background:#e9f3ed; border-color:#bcd5c5; color:#23674b;} +.archive-branch {width:100%; border:1px solid transparent; background:none; padding:12px 10px; border-radius:7px; text-align:left; margin-bottom:6px; color:var(--muted);} +.archive-branch:hover {background:var(--surface-muted);} +.archive-branch.active {background:var(--line); border-color:var(--muted); color:var(--accent-hover);} .archive-branch-name {display:flex; align-items:flex-start; gap:9px; font-weight:600; font-size:12px; line-height:1.7; overflow-wrap:anywhere;} -.archive-branch-symbol {font-size:20px; font-weight:400; line-height:22px; color:#759e84;} -.archive-branch-meta {display:flex; gap:7px; flex-wrap:wrap; font-size:10px; margin:8px 0 0 24px; color:#87938c; line-height:1.6;} -.archive-warning {color:#ad7832;} -.archive-mount {color:#478768;} -.archive-branch-note {font-size:10px; line-height:1.9; color:#8b968f; margin:22px 8px 0;} +.archive-branch-symbol {font-size:20px; font-weight:400; line-height:22px; color:var(--accent);} +.archive-branch-meta {display:flex; gap:7px; flex-wrap:wrap; font-size:12px; margin:8px 0 0 24px; color:var(--muted); line-height:1.6;} +.archive-warning {color:var(--warning);} +.archive-mount {color:var(--accent);} +.archive-branch-note {font-size:12px; line-height:1.9; color:var(--muted); margin:22px 8px 0;} .archive-content {min-width:0;} .archive-branch-header {display:flex; gap:18px; justify-content:space-between; align-items:center; padding:23px 25px 20px;} .archive-branch-header h2 {margin:0; font-size:20px; font-weight:600; overflow-wrap:anywhere;} -.archive-branch-header p {margin:8px 0 0; color:#8b978f; font-size:11px;} -.archive-history-link {color:#52806a; font-size:11px; white-space:nowrap; text-decoration:none;} +.archive-branch-header p {margin:8px 0 0; color:var(--muted); font-size:12px;} +.archive-history-link {color:var(--accent); font-size:12px; white-space:nowrap; text-decoration:none;} .archive-history-link:hover {text-decoration:underline;} -.archive-states {display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); margin:0 25px 20px; background:#f7faf8; border:1px solid #e6ede7; border-radius:8px;} +.archive-states {display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); margin:0 25px 20px; background:var(--surface-muted); border:1px solid var(--line); border-radius:8px;} .archive-state {display:flex; flex-direction:column; gap:7px; padding:14px 16px; min-width:0;} -.archive-state+.archive-state {border-left:1px solid #e4ece5;} -.archive-state-label {font-size:10px; color:#8c988f;} -.archive-state strong {font-size:12px; font-weight:500; color:#477159;} -.archive-state.attention strong {color:#a77830;} -.archive-state-description {font-size:10px; color:#909b93; line-height:1.5;} -.archive-files {border-top:1px solid #e7ece8;} +.archive-state+.archive-state {border-left:1px solid var(--line);} +.archive-state-label {font-size:12px; color:var(--muted);} +.archive-state strong {font-size:12px; font-weight:500; color:var(--accent);} +.archive-state.attention strong {color:var(--warning);} +.archive-state-description {font-size:12px; color:var(--muted); line-height:1.5;} +.archive-files {border-top:1px solid var(--line);} .archive-files .branch-files-body {padding:0;} -.archive-files .branch-files-toolbar {padding:14px 20px; margin:0; border-bottom:1px solid #edf0ed; gap:10px;} -.archive-files .branch-directory {font-family:inherit; font-size:12px; color:#64786b;} -.archive-files .branch-files-toolbar .secondary {font-size:11px; padding:7px 10px;} -.archive-files .branch-files-body>.changes-status {margin:0; padding:10px 20px; color:#8b978e; font-size:10px; background:#fbfcfb; border-bottom:1px solid #edf0ed;} +.archive-files .branch-files-toolbar {padding:14px 20px; margin:0; border-bottom:1px solid var(--line); gap:10px;} +.archive-files .branch-directory {font-family:inherit; font-size:12px; color:var(--muted);} +.archive-files .branch-files-toolbar .secondary {font-size:12px; padding:7px 10px;} +.archive-files .branch-files-body>.changes-status {margin:0; padding:10px 20px; color:var(--muted); font-size:12px; background:var(--surface-muted); border-bottom:1px solid var(--line);} .archive-files .changes-layout {display:grid; grid-template-columns:minmax(180px,28%) minmax(0,1fr); gap:0; align-items:stretch; min-height:450px;} -.archive-files .changed-files {padding:14px 10px; border-right:1px solid #e9ede9; min-width:0;} +.archive-files .changed-files {padding:14px 10px; border-right:1px solid var(--line); min-width:0;} .archive-files .branch-file-preview {padding:20px 24px 28px; min-width:0;} .archive-files .branch-file-list {max-height:57vh;} .archive-files .changed-file {border-radius:6px; border-bottom:0; margin-bottom:3px; padding:10px 8px; font-size:12px; gap:8px;} -.archive-files .changed-file.active {background:#edf5ef; color:#2f7150;} -.file-filter {margin-bottom:9px; padding:9px; font-size:11px;} -.hidden-files-toggle {display:flex; align-items:center; gap:5px; color:#96a098; font-size:10px; margin:0 0 12px;} -.hidden-files-toggle input {accent-color:#418269; width:13px; height:13px; margin:0;} -.file-symbol {width:22px; height:23px; border-radius:4px; display:inline-grid; place-items:center; flex-shrink:0; font:10px/1 ui-monospace,monospace; color:#7a9584; background:#f0f4f1;} -.file-symbol.folder {color:#b79754; background:#faf5e8; font-size:15px;} +.archive-files .changed-file.active {background:var(--surface-muted); color:var(--accent-hover);} +.file-filter {margin-bottom:9px; padding:9px; font-size:12px;} +.hidden-files-toggle {display:flex; align-items:center; gap:5px; color:var(--muted); font-size:12px; margin:0 0 12px;} +.hidden-files-toggle input {accent-color:var(--accent); width:13px; height:13px; margin:0;} +.file-symbol {width:22px; height:23px; border-radius:4px; display:inline-grid; place-items:center; flex-shrink:0; font:12px/1 ui-monospace,monospace; color:var(--muted); background:var(--surface-muted);} +.file-symbol.folder {color:var(--warning); background:var(--surface-muted); font-size:15px;} .archive-files .branch-file-path {font-size:15px; margin:0 0 10px;} -.archive-files .diff-metadata {font-size:10px; color:#9ba69e;} -.archive-files .branch-file-preview>.changes-status {font-size:10px; color:#9ba69e;} -.archive-footer {font-size:11px; color:#97a297; line-height:1.8; margin:18px 0 0;} -.preview-switcher {display:flex; gap:4px; border-bottom:1px solid #e8ede8; margin:16px 0; padding-bottom:8px;} -.preview-mode {border:0; background:transparent; color:#8f9b91; padding:5px 13px; border-radius:5px; font-size:11px;} -.preview-mode.active {background:#eaf3ed; color:#3e7755;} -.document-preview {font-size:13px; line-height:1.95; color:#4b5b50; overflow-wrap:anywhere; padding:0 4px; max-height:58vh; overflow:auto;} -.document-preview h1 {font-size:23px; line-height:1.5; margin:16px 0; color:#2f4235; letter-spacing:-.5px;} -.document-preview h2 {font-size:17px; margin:24px 0 12px; color:#405445;} +.archive-files .diff-metadata {font-size:12px; color:var(--muted);} +.archive-files .branch-file-preview>.changes-status {font-size:12px; color:var(--muted);} +.archive-footer {font-size:12px; color:var(--muted); line-height:1.8; margin:18px 0 0;} +.preview-switcher {display:flex; gap:4px; border-bottom:1px solid var(--line); margin:16px 0; padding-bottom:8px;} +.preview-mode {border:0; background:transparent; color:var(--muted); padding:5px 13px; border-radius:5px; font-size:12px;} +.preview-mode.active {background:var(--line); color:var(--accent);} +.document-preview {font-size:13px; line-height:1.95; color:var(--ink); overflow-wrap:anywhere; padding:0 4px; max-height:58vh; overflow:auto;} +.document-preview h1 {font-size:23px; line-height:1.5; margin:16px 0; color:var(--ink); letter-spacing:-.5px;} +.document-preview h2 {font-size:17px; margin:24px 0 12px; color:var(--ink);} .document-preview h3 {font-size:14px; margin:20px 0 10px;} .document-preview p {margin:9px 0;} .document-preview ul,.document-preview ol {padding-left:24px;} .document-preview li {padding:3px 0;} -.document-preview code {font-size:11px; padding:2px 4px; background:#f0f4f1; border-radius:3px;} -.document-code {white-space:pre-wrap; overflow-wrap:anywhere; padding:14px; border-radius:6px; background:#f5f7f5; font-size:12px;} -.document-preview blockquote {border-left:3px solid #b8d4c2; padding:8px 14px; margin:14px 0; background:#f7faf7; color:#7b8b7e;} +.document-preview code {font-size:12px; padding:2px 4px; background:var(--surface-muted); border-radius:3px;} +.document-code {white-space:pre-wrap; overflow-wrap:anywhere; padding:14px; border-radius:6px; background:var(--surface-muted); font-size:12px;} +.document-preview blockquote {border-left:3px solid var(--muted); padding:8px 14px; margin:14px 0; background:var(--surface-muted); color:var(--muted);} .document-table-wrap {overflow:auto; max-width:100%;} .document-table {width:100%; border-collapse:collapse; font-size:12px;} -.document-table th,.document-table td {padding:9px 12px; border:1px solid #e4eae5; text-align:left;} -.document-table th {background:#f2f7f3; font-weight:500; color:#50715b;} +.document-table th,.document-table td {padding:9px 12px; border:1px solid var(--line); text-align:left;} +.document-table th {background:var(--surface-muted); font-weight:500; color:var(--muted);} .global-error {margin:22px 32px 0;} @media(min-width:1700px) {.archive-workspace {grid-template-columns:250px minmax(0,1fr);} .archive-files .changes-layout {grid-template-columns:270px minmax(0,1fr);}} -@media(max-width:1200px) {.archive-page {padding:24px;} .archive-workspace {grid-template-columns:190px minmax(0,1fr);} .archive-state {padding:12px 10px;} .archive-state-description {font-size:9px;} .archive-files .changes-layout {grid-template-columns:170px minmax(0,1fr);} .archive-files .branch-file-preview {padding:18px;} .archive-scope {display:none;}} -@media(max-width:1000px) {.archive-workspace {grid-template-columns:1fr;} .archive-branches {border-right:0; border-bottom:1px solid #e3e9e4; padding:16px;} .archive-list-heading {padding-bottom:10px;} .archive-branch-search {max-width:260px; margin-bottom:10px;} .archive-branch-list {display:flex; gap:8px; overflow:auto; max-height:none;} .archive-branch {flex:0 0 180px; margin:0;} .archive-branch-note {margin:10px 0 0;} .archive-states {margin-left:20px; margin-right:20px;}} -@media(max-width:600px) { .nav-link {flex:1; justify-content:center; padding:9px 5px; margin:0; font-size:11px; gap:5px;} .nav-link .branch-icon {font-size:16px;} .archive-page {padding:24px 14px;} .archive-heading h1 {font-size:26px;} .archive-heading p {font-size:11px;} .archive-branch-header {padding:18px 15px; flex-wrap:wrap;} .archive-states {margin:0 12px 16px;} .archive-state {padding:10px 8px;} .archive-state strong {font-size:10px;} .archive-state-description {display:none;} .archive-files .changes-layout {grid-template-columns:1fr;} .archive-files .changed-files {border-right:0; border-bottom:1px solid #e9ede9; padding:12px 15px;} .archive-files .branch-file-list {max-height:200px;} .archive-files .branch-files-toolbar {padding:12px;} .archive-files .branch-file-preview {padding:18px 16px;} .document-preview {max-height:none;} .archive-footer {font-size:10px;} .global-error {margin:14px 14px 0;}} +@media(max-width:1200px) {.archive-page {padding:24px;} .archive-workspace {grid-template-columns:190px minmax(0,1fr);} .archive-state {padding:12px 10px;} .archive-state-description {font-size:12px;} .archive-files .changes-layout {grid-template-columns:170px minmax(0,1fr);} .archive-files .branch-file-preview {padding:18px;} .archive-scope {display:none;}} +@media(max-width:1000px) {.archive-workspace {grid-template-columns:1fr;} .archive-branches {border-right:0; border-bottom:1px solid var(--line); padding:16px;} .archive-list-heading {padding-bottom:10px;} .archive-branch-search {max-width:260px; margin-bottom:10px;} .archive-branch-list {display:flex; gap:8px; overflow:auto; max-height:none;} .archive-branch {flex:0 0 180px; margin:0;} .archive-branch-note {margin:10px 0 0;} .archive-states {margin-left:20px; margin-right:20px;}} +@media(max-width:600px) { .nav-link {flex:1; justify-content:center; padding:9px 5px; margin:0; font-size:12px; gap:5px;} .nav-link .branch-icon {font-size:16px;} .archive-page {padding:24px 14px;} .archive-heading h1 {font-size:26px;} .archive-heading p {font-size:12px;} .archive-branch-header {padding:18px 15px; flex-wrap:wrap;} .archive-states {margin:0 12px 16px;} .archive-state {padding:10px 8px;} .archive-state strong {font-size:12px;} .archive-state-description {display:none;} .archive-files .changes-layout {grid-template-columns:1fr;} .archive-files .changed-files {border-right:0; border-bottom:1px solid var(--line); padding:12px 15px;} .archive-files .branch-file-list {max-height:200px;} .archive-files .branch-files-toolbar {padding:12px;} .archive-files .branch-file-preview {padding:18px 16px;} .document-preview {max-height:none;} .archive-footer {font-size:12px;} .global-error {margin:14px 14px 0;}} diff --git a/crates/agentdrive-mcp/dashboard/dashboard.css b/crates/agentdrive-mcp/dashboard/dashboard.css index 5c109cc..cdc4a24 100644 --- a/crates/agentdrive-mcp/dashboard/dashboard.css +++ b/crates/agentdrive-mcp/dashboard/dashboard.css @@ -1,14 +1,26 @@ +/* Shared palette and interaction primitives. */ :root { color-scheme:light; - --bg:#f6f7f9; - --paper:#fff; - --ink:#202733; - --muted:#858d9a; - --line:#e9edf2; - --green:#22846a; - --green-bg:#edf7f3; - --nav:#171f2b; - font-family:Inter,-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif; + --bg:#F8FAFC; + --paper:#FFFFFF; + --ink:#0F172A; + --muted:#64748B; + --line:#E2E8F0; + --surface-muted:#F1F5F9; + --accent:#047857; + --accent-hover:#065F46; + --accent-line:#A7F3D0; + --green:var(--accent); + --green-bg:#ECFDF5; + --status:#10B981; + --danger:#B91C1C; + --danger-bg:#FEF2F2; + --danger-line:#FECACA; + --warning:#92400E; + --warning-bg:#FFFBEB; + --warning-line:#FDE68A; + --motion:all .2s cubic-bezier(.16,1,.3,1); + font-family:Inter,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI","Noto Sans CJK SC","PingFang SC","Microsoft YaHei",sans-serif; font-synthesis:none; } * { @@ -18,23 +30,25 @@ body { margin:0; color:var(--ink); background:var(--bg); - font-size:13px; + font-size:14px; + line-height:1.5; + font-variant-numeric:tabular-nums; } -button,input,select { +button,input,select,textarea { font:inherit; } button,select { cursor:pointer; } button { - transition:background .15s,color .15s; + transition:var(--motion); } button:focus-visible,input:focus-visible,select:focus-visible,a:focus-visible { - outline:2px solid #42ad91; + outline:2px solid var(--accent); outline-offset:3px; } button:disabled { - cursor:wait; + cursor:not-allowed; opacity:.5; } [hidden] { @@ -49,7 +63,7 @@ button:disabled { display:flex; gap:10px; align-items:center; - color:#fff; + color:var(--paper); text-decoration:none; font-size:21px; font-weight:650; @@ -58,38 +72,27 @@ button:disabled { .brand-mark { width:30px; height:33px; - border:1px solid #486259; + border:1px solid var(--ink); border-radius:8px; display:grid; place-items:center; - background:#28483f; - color:#b0ecd6; + background:var(--ink); + color:var(--accent-line); font-size:23px; font-weight:600; font-family:monospace; } - - - - - .branch-icon { font-size:23px; line-height:17px; } - - - - - - .connection-dot { display:inline-block; width:6px; height:6px; - background:#4caf8f; + background:var(--accent); border-radius:50%; flex-shrink:0; } @@ -99,78 +102,78 @@ main { min-width:0; } .topbar { - height:72px; + height:64px; border-bottom:1px solid var(--line); - background:#fff; + background:var(--paper); display:flex; align-items:center; justify-content:space-between; padding:0 38px; } .breadcrumb { - color:#9198a4; + color:var(--muted); font-size:12px; } .breadcrumb span { padding:0 13px; - color:#c7ced6; + color:var(--muted); } .breadcrumb strong { font-weight:500; - color:#4c5868; + color:var(--ink); } .topbar-right { display:flex; align-items:center; gap:15px; color:var(--muted); - font-size:10px; + font-size:12px; } .read-only { - color:#667181; - background:#f3f5f7; - border:1px solid #e9edf1; + color:var(--muted); + background:var(--surface-muted); + border:1px solid var(--line); border-radius:4px; padding:4px 7px; } .refresh { - background:#fff; - border:1px solid #e4e8ee; - color:#6e7a89; + background:var(--paper); + border:1px solid var(--line); + color:var(--muted); border-radius:6px; width:29px; height:29px; font-size:19px; } .refresh:hover { - background:#f3f7f5; + background:var(--surface-muted); } .content { - max-width:1600px; + max-width:1280px; margin:auto; - padding:38px; + padding:36px 32px 48px; } .page-heading { display:flex; align-items:center; justify-content:space-between; - margin-bottom:29px; + margin-bottom:32px; } .eyebrow { - font-size:9px; + font-size:12px; letter-spacing:2.2px; - color:#829087; + color:var(--muted); font-weight:650; margin-bottom:11px; } h1 { - font-size:29px; - letter-spacing:-1px; + font-size:30px; + letter-spacing:-.02em; margin:0; font-weight:650; } .heading-dot { - color:#2d9a7c; + color:var(--accent); margin-left:3px; } .page-heading p { @@ -179,10 +182,10 @@ h1 { margin:11px 0 0; } .location-label { - font-size:11px; + font-size:12px; display:flex; gap:8px; - color:#7b8794; + color:var(--muted); align-items:center; padding-top:15px; } @@ -196,17 +199,17 @@ h1 { background:var(--paper); border:1px solid var(--line); border-radius:9px; - padding:19px 21px 17px; + padding:24px; min-width:0; } .stat-label { display:flex; justify-content:space-between; - font-size:11px; - color:#7b8492; + font-size:12px; + color:var(--muted); } .stat-label span { - color:#a9b1bc; + color:var(--muted); font-size:17px; line-height:15px; } @@ -226,8 +229,8 @@ h1 { letter-spacing:-.4px; } .stat small { - font-size:10px; - color:#939ba6; + font-size:12px; + color:var(--muted); } .panels { display:grid; @@ -254,60 +257,60 @@ h1 { } .panel-heading p { margin:8px 0 0; - font-size:10px; - color:#969da7; + font-size:12px; + color:var(--muted); } .count-badge { display:inline-block; - background:#f1f3f6; - color:#818a97; + background:var(--surface-muted); + color:var(--muted); border-radius:5px; min-width:24px; padding:3px 6px; text-align:center; - font-size:10px; + font-size:12px; font-weight:500; } -.graph-legend { display:flex; flex-wrap:wrap; gap:12px; padding:0 22px 18px; color:#85919f; font-size:11px; border-bottom:1px solid var(--line); } +.graph-legend { display:flex; flex-wrap:wrap; gap:12px; padding:0 22px 18px; color:var(--muted); font-size:12px; border-bottom:1px solid var(--line); } .graph-legend span { display:flex; align-items:center; gap:6px; } -.legend-commit { width:8px; height:8px; border:2px solid #7b91ac; border-radius:50%; } +.legend-commit { width:8px; height:8px; border:2px solid var(--muted); border-radius:50%; } .branch-tree { padding:20px; max-height:590px; overflow:auto; } .branch-group,.origin-timeline { list-style:none; padding:0; margin:0; } .branch-item { min-width:0; padding:5px 0; } -.origin-timeline { margin:6px 0 8px 14px; padding-left:20px; border-left:2px solid #dce8e1; } +.origin-timeline { margin:6px 0 8px 14px; padding-left:20px; border-left:2px solid var(--line); } .origin-fork { position:relative; padding-top:12px; } .origin-fork>.origin-node { margin-left:-27px; } -.origin-fork>.branch-group { margin:10px 0 0 4px; border-left:1px solid #dae3ef; padding-left:16px; } +.origin-fork>.branch-group { margin:10px 0 0 4px; border-left:1px solid var(--line); padding-left:16px; } .origin-fork>.branch-group>.branch-item { position:relative; } -.origin-fork>.branch-group>.branch-item:before { content:""; position:absolute; left:-17px; top:35px; width:17px; border-top:1px solid #dae3ef; } +.origin-fork>.branch-group>.branch-item:before { content:""; position:absolute; left:-17px; top:35px; width:17px; border-top:1px solid var(--line); } .origin-node { display:flex; align-items:flex-start; gap:10px; min-width:0; } -.origin-dot { flex-shrink:0; width:12px; height:12px; border:3px solid #849ab7; border-radius:50%; background:white; margin-top:5px; } +.origin-dot { flex-shrink:0; width:12px; height:12px; border:3px solid var(--muted); border-radius:50%; background:white; margin-top:5px; } .origin-copy { min-width:0; } -.origin-revision { color:#526e98; border:0; border-radius:4px; padding:2px 5px; margin-left:-5px; background:#f1f5fb; font:11px/1.7 ui-monospace,monospace; text-align:left; } -.origin-revision:hover { background:#e5edf8; } +.origin-revision { color:var(--muted); border:0; border-radius:4px; padding:2px 5px; margin-left:-5px; background:var(--surface-muted); font:12px/1.7 ui-monospace,monospace; text-align:left; } +.origin-revision:hover { background:var(--line); } .origin-revision:disabled { cursor:default; opacity:.8; } -.origin-message { display:block; font-size:10px; line-height:1.6; color:#8994a3; overflow-wrap:anywhere; padding-top:4px; } -.origin-caption { font-size:10px; color:#8994a3; margin:0 0 7px; overflow-wrap:anywhere; } -.branch-button { background:#f9fbfa; border:1px solid #e3eae6; border-radius:8px; width:100%; text-align:left; display:flex; align-items:center; gap:10px; padding:12px 11px; margin:10px 0 0; color:#586a63; min-width:0; } -.branch-button:hover { background:#f0f7f3; } -.branch-button.selected { background:#edf7f3; border-color:#77b69e; color:#246b55; box-shadow:0 0 0 1px #77b69e33; } -.branch-button:disabled { cursor:default; opacity:1; background:#f6f7f9; color:#8b95a2; } -.branch-symbol { font-size:21px; flex-shrink:0; color:#77a58f; } +.origin-message { display:block; font-size:12px; line-height:1.6; color:var(--muted); overflow-wrap:anywhere; padding-top:4px; } +.origin-caption { font-size:12px; color:var(--muted); margin:0 0 7px; overflow-wrap:anywhere; } +.branch-button { background:var(--surface-muted); border:1px solid var(--line); border-radius:8px; width:100%; text-align:left; display:flex; align-items:center; gap:10px; padding:12px 11px; margin:10px 0 0; color:var(--muted); min-width:0; } +.branch-button:hover { background:var(--surface-muted); } +.branch-button.selected { background:var(--surface-muted); border-color:var(--accent); color:var(--accent-hover); box-shadow:none; } +.branch-button:disabled { cursor:default; opacity:1; background:var(--surface-muted); color:var(--muted); } +.branch-symbol { font-size:21px; flex-shrink:0; color:var(--accent); } .branch-copy { min-width:0; flex:1; } .branch-name { display:block; font-size:12px; overflow-wrap:anywhere; line-height:1.6; font-weight:600; } .branch-badges { display:flex; gap:5px; flex-wrap:wrap; justify-content:flex-end; max-width:80px; } -.default-mark,.mount-mark,.branch-unavailable { display:inline-block; font-size:9px; border-radius:4px; padding:3px 5px; white-space:nowrap; } -.default-mark { background:#edf0f7; color:#6b7491; } -.mount-mark { background:#d8eee4; color:#39785e; } -.branch-unavailable { background:#eceff3; color:#7b8492; } -.tree-footer { padding:14px 20px; border-top:1px solid var(--line); font-size:10px; line-height:1.8; color:#929ca7; background:#fcfdfd; } +.default-mark,.mount-mark,.branch-unavailable { display:inline-block; font-size:12px; border-radius:4px; padding:3px 5px; white-space:nowrap; } +.default-mark { background:var(--surface-muted); color:var(--muted); } +.mount-mark { background:var(--line); color:var(--accent); } +.branch-unavailable { background:var(--line); color:var(--muted); } +.tree-footer { padding:14px 20px; border-top:1px solid var(--line); font-size:12px; line-height:1.8; color:var(--muted); background:var(--surface-muted); } .mounts-panel { grid-column:1/-1; } .mount-table-wrap { overflow:auto; } -.mount-path { display:block; font:11px/1.8 ui-monospace,monospace; color:#52616d; overflow-wrap:anywhere; } -.mount-state { display:inline-block; border-radius:4px; padding:4px 6px; font-size:10px; color:#a27b35; background:#fff5e3; white-space:nowrap; } -.mount-state.ready { color:#30785a; background:#e9f6ef; } -.mount-state.recovery_required { color:#b45b4f; background:#fff0ed; } -.panel-empty { padding:12px 22px 26px; margin:0; font-size:12px; color:#8994a3; } +.mount-path { display:block; font:12px/1.8 ui-monospace,monospace; color:var(--muted); overflow-wrap:anywhere; } +.mount-state { display:inline-block; border-radius:4px; padding:4px 6px; font-size:12px; color:var(--warning); background:var(--surface-muted); white-space:nowrap; } +.mount-state.ready { color:var(--accent); background:var(--line); } +.mount-state.recovery_required { color:var(--danger); background:var(--surface-muted); } +.panel-empty { padding:12px 22px 26px; margin:0; font-size:12px; color:var(--muted); } .history-heading { padding:23px 25px 20px; display:flex; @@ -317,8 +320,8 @@ h1 { gap:15px; } .selected-branch-label { - font-size:9px; - color:#a0a6af; + font-size:12px; + color:var(--muted); letter-spacing:.5px; } .history-heading h2 { @@ -328,35 +331,35 @@ h1 { word-break:break-word; } .branch-meta { - font-size:10px; - color:#99a1ad; + font-size:12px; + color:var(--muted); display:flex; gap:9px; flex-wrap:wrap; } .branch-meta code { - color:#7f8b97; - font-size:9px; + color:var(--muted); + font-size:12px; } .pill { white-space:nowrap; display:inline-block; background:var(--green-bg); - border:1px solid #d5ebdf; - color:#3d8a6b; + border:1px solid var(--line); + color:var(--accent); border-radius:5px; - font-size:9px; + font-size:12px; padding:5px 8px; } .pill.paused { - background:#fff7e9; - border-color:#eddfc5; - color:#a88c50; + background:var(--surface-muted); + border-color:var(--warning-line); + color:var(--warning); } .pill.autosaved { - background:#f1f5fb; - border-color:#dae4f2; - color:#526e98; + background:var(--surface-muted); + border-color:var(--line); + color:var(--muted); } .history-toolbar { display:flex; @@ -372,7 +375,7 @@ h1 { gap:9px; align-items:center; } -.history-hint { font-size:10px; color:var(--muted); } +.history-hint { font-size:12px; color:var(--muted); } .revision-list { padding:0 14px 14px; max-height:430px; @@ -383,7 +386,7 @@ h1 { cursor:pointer; font:inherit; color:inherit; - background:#fff; + background:var(--paper); border:1px solid transparent; border-radius:7px; display:flex; @@ -395,7 +398,7 @@ h1 { } .revision-row:focus-visible { outline:2px solid var(--green); outline-offset:-2px; } .revision-row:hover { - background:#f8faf9; + background:var(--surface-muted); } .revision-node { width:24px; @@ -411,7 +414,7 @@ h1 { position:absolute; height:calc(100% + 26px); width:1px; - background:#e2e9e5; + background:var(--line); top:10px; } .revision-row:last-child .revision-node:before { @@ -421,9 +424,9 @@ h1 { content:""; width:7px; height:7px; - border:2px solid #83b39e; + border:2px solid var(--accent); border-radius:50%; - background:#fff; + background:var(--paper); position:relative; } .revision-copy { @@ -434,7 +437,7 @@ h1 { font-size:13px; line-height:18px; font-weight:500; - color:#465260; + color:var(--ink); display:block; white-space:nowrap; overflow:hidden; @@ -444,17 +447,17 @@ h1 { display:flex; gap:9px; align-items:center; - font-size:9px; - color:#a0a8b1; + font-size:12px; + color:var(--muted); margin-top:6px; } .revision-sub code { - font-size:9px; - color:#81908b; + font-size:12px; + color:var(--muted); } .revision-time { - font-size:9px; - color:#9ba4ae; + font-size:12px; + color:var(--muted); text-align:right; white-space:nowrap; padding-top:3px; @@ -462,8 +465,8 @@ h1 { } .revision-type { display:block; - color:#a6aeb9; - font-size:8px; + color:var(--muted); + font-size:12px; } .revision-detail { padding:24px; @@ -472,47 +475,47 @@ h1 { display:grid; grid-template-columns:75px minmax(0,1fr); gap:10px 12px; - font-size:11px; + font-size:12px; margin:0; } .detail-grid dt { - color:#98a1ac; + color:var(--muted); } .detail-grid dd { margin:0; - color:#657282; + color:var(--muted); overflow-wrap:anywhere; line-height:1.6; } .detail-grid code { - font-size:9px; + font-size:12px; } .page-footer { display:flex; justify-content:space-between; - font-size:9px; - color:#a1aab6; + font-size:12px; + color:var(--muted); margin-top:25px; } .footer-separator { padding:0 8px; - color:#c4cbd3; + color:var(--muted); } .empty-state { padding:70px 20px; text-align:center; color:var(--muted); - background:#fff; + background:var(--paper); border:1px solid var(--line); border-radius:10px; } .empty-state>span { font-size:35px; - color:#559b80; + color:var(--accent); } .empty-state h2 { font-size:17px; - color:#526171; + color:var(--muted); } .empty-state p { font-size:12px; @@ -520,42 +523,34 @@ h1 { .no-revisions { padding:42px 20px; text-align:center; - color:#9aa4af; - font-size:11px; + color:var(--muted); + font-size:12px; } .error-banner { - border:1px solid #ecd7ca; - background:#fff8f3; - color:#a36548; + border:1px solid var(--line); + background:var(--surface-muted); + color:var(--warning); padding:12px 16px; border-radius:7px; margin-bottom:18px; font-size:12px; } - - - - - - .primary { - background:#2c765c; - color:#fff; - border:0; - border-radius:7px; - display:flex; - justify-content:space-between; - width:100%; - padding:13px; - margin-top:15px; + background:var(--accent-hover); + color:var(--paper); + border:1px solid transparent; + border-radius:8px; + display:inline-flex; + justify-content:center; + width:auto; + padding:10px 16px; + margin:0; } .primary:hover { - background:#225d48; + background:var(--accent-hover); } - - @media(max-width:1100px) { .content { @@ -569,7 +564,7 @@ h1 { gap:15px; } .stat { - padding:17px 15px; + padding:20px; } .location-label { @@ -582,7 +577,6 @@ h1 { } @media(max-width:850px) { - .content { padding:22px; } @@ -598,7 +592,6 @@ h1 { max-height:480px; } - .topbar-right #updated { display:none; } @@ -611,10 +604,6 @@ h1 { display:block; } - - - - .topbar { height:55px; padding:0 20px; @@ -642,7 +631,7 @@ h1 { padding:20px; } .revision-time { - font-size:8px; + font-size:12px; } .revision-sub { gap:6px; @@ -663,25 +652,25 @@ h1 { margin-bottom:8px; border:1px solid transparent; border-radius:7px; - color:#a5b2c2; + color:var(--muted); text-decoration:none; } -.nav-link:hover { background:#263240; } -.nav-link.nav-active { background:#273c39; border-color:#37534c; color:#b8e5d6; } +.nav-link:hover { background:var(--ink); } +.nav-link.nav-active { background:var(--ink); border-color:var(--ink); color:var(--accent-line); } .settings-content { max-width:1250px; } .settings-panel { padding:25px; margin-bottom:20px; } .settings-heading { display:flex; align-items:start; justify-content:space-between; gap:18px; margin-bottom:23px; } .settings-heading h2 { font-size:15px; margin:0 0 9px; font-weight:600; } -.settings-heading p,.settings-help { color:#778491; line-height:1.8; font-size:12px; margin:0; } +.settings-heading p,.settings-help { color:var(--muted); line-height:1.8; font-size:12px; margin:0; } .settings-help { margin-top:16px; } .cache-capacity { max-width:320px; } -.settings-content label { display:block; color:#526171; font-size:12px; } +.settings-content label { display:block; color:var(--muted); font-size:12px; } .settings-content input,.settings-content textarea,.settings-content select { display:block; width:100%; min-width:0; - border:1px solid #dce3e8; - background:#fff; + border:1px solid var(--line); + background:var(--paper); border-radius:6px; padding:11px; margin-top:9px; @@ -690,20 +679,20 @@ h1 { line-height:1.6; } .settings-content textarea { resize:vertical; } -.settings-content textarea:focus-visible { outline:2px solid #42ad91; outline-offset:3px; } -.settings-content small { display:block; font-size:11px; line-height:1.7; color:#89939e; margin-top:8px; } +.settings-content textarea:focus-visible { outline:2px solid var(--accent); outline-offset:3px; } +.settings-content small { display:block; font-size:12px; line-height:1.7; color:var(--muted); margin-top:8px; } .settings-content fieldset { border:0; margin:0; padding:0; min-width:0; } .input-unit { position:relative; display:block; } .input-unit input { padding-right:50px; } -.input-unit>span { position:absolute; right:12px; top:13px; color:#8a96a0; font-size:11px; } -.secondary { background:#fff; border:1px solid #dce4e0; border-radius:6px; padding:10px 14px; color:#537262; white-space:nowrap; } -.secondary:hover { background:#f2f7f4; } -.settings-notice { border:1px solid #cee5d9; background:#eff8f3; color:#36795c; padding:13px 16px; line-height:1.8; border-radius:7px; margin-bottom:20px; } +.input-unit>span { position:absolute; right:12px; top:13px; color:var(--muted); font-size:12px; } +.secondary { background:var(--paper); border:1px solid var(--line); border-radius:6px; padding:10px 14px; color:var(--muted); white-space:nowrap; } +.secondary:hover { background:var(--surface-muted); } +.settings-notice { border:1px solid var(--line); background:var(--surface-muted); color:var(--accent); padding:13px 16px; line-height:1.8; border-radius:7px; margin-bottom:20px; } .settings-actions { display:flex; align-items:center; gap:12px; margin:23px 0 30px; } .settings-actions>div { margin-right:auto; } -.settings-actions strong { font-size:12px; color:#64746b; font-weight:500; } +.settings-actions strong { font-size:12px; color:var(--muted); font-weight:500; } .settings-actions .primary { width:auto; margin:0; padding:12px 22px; white-space:nowrap; } -.settings-actions small { font-family:ui-monospace,monospace; font-size:10px; } +.settings-actions small { font-family:ui-monospace,monospace; font-size:12px; } #startup-values { grid-template-columns:100px minmax(0,1fr); row-gap:15px; } #startup-values dd { white-space:pre-wrap; } @media(max-width:650px) { @@ -711,7 +700,7 @@ h1 { .nav-link { margin:0; flex:1; justify-content:center; font-size:12px; } .settings-panel { padding:20px; } .settings-heading { gap:10px; } - .settings-heading p { font-size:11px; } + .settings-heading p { font-size:12px; } .settings-actions { flex-wrap:wrap; } .settings-actions>div { width:100%; } #startup-values { grid-template-columns:80px minmax(0,1fr); } @@ -728,11 +717,11 @@ h1 { .settings-content .storage-check input { width:auto; margin:0; } .revision-changes{padding:24px;min-width:0} -.comparison-summary{margin:0 0 14px;font-size:12px;color:#64748b;overflow-wrap:anywhere} -.changes-status{font-size:12px;color:#64748b;line-height:1.6}.changes-error{font-size:12px;color:#b91c1c;overflow-wrap:anywhere} +.comparison-summary{margin:0 0 14px;font-size:12px;color:var(--muted);overflow-wrap:anywhere} +.changes-status{font-size:12px;color:var(--muted);line-height:1.6}.changes-error{font-size:12px;color:var(--danger);overflow-wrap:anywhere} .changes-layout{display:grid;grid-template-columns:minmax(180px,30%) minmax(0,1fr);gap:16px;align-items:start}.changed-files{min-width:0}.changed-file-list{max-height:480px;overflow:auto;margin-bottom:10px} -.changed-file{width:100%;border:0;border-bottom:1px solid #eef2f6;background:transparent;display:flex;gap:8px;text-align:left;padding:10px 6px;cursor:pointer;font-size:12px;align-items:flex-start}.changed-file:hover,.changed-file.active{background:#eff6ff}.changed-file:focus-visible{outline:2px solid #2563eb;outline-offset:-2px}.changed-path{overflow-wrap:anywhere}.change-kind{flex-shrink:0;color:#64748b}.change-kind.added{color:#15803d}.change-kind.deleted{color:#b91c1c}.change-kind.modified,.change-kind.renamed{color:#1d4ed8} -.diff-preview{min-width:0}.diff-path{font:600 13px ui-monospace,monospace;overflow-wrap:anywhere;margin:8px 0}.diff-metadata{display:grid;grid-template-columns:auto 1fr;gap:6px 10px;font-size:11px;color:#64748b}.diff-metadata dd{margin:0;overflow-wrap:anywhere}.diff-lines{overflow:auto;max-height:560px;border:1px solid #e2e8f0;border-radius:6px;font:12px/1.7 ui-monospace,monospace}.diff-line{display:flex;min-width:max-content}.diff-line.added{background:#e6ffed}.diff-line.removed{background:#ffeef0}.line-number{display:inline-block;width:4em;flex-shrink:0;text-align:right;padding-right:10px;color:#64748b;user-select:none}.line-sign{width:1.5em;flex-shrink:0}.line-text{white-space:pre;padding-right:12px;font:inherit}.diff-hunk{background:#eff6ff;padding:6px 10px;color:#475569;white-space:nowrap}.missing-newline{color:#64748b;padding-left:9em;font-size:11px} +.changed-file{width:100%;border:0;border-bottom:1px solid var(--surface-muted);background:transparent;display:flex;gap:8px;text-align:left;padding:10px 6px;cursor:pointer;font-size:12px;align-items:flex-start}.changed-file:hover,.changed-file.active{background:var(--surface-muted)}.changed-file:focus-visible{outline:2px solid var(--muted);outline-offset:-2px}.changed-path{overflow-wrap:anywhere}.change-kind{flex-shrink:0;color:var(--muted)}.change-kind.added{color:var(--accent-hover)}.change-kind.deleted{color:var(--danger)}.change-kind.modified,.change-kind.renamed{color:var(--muted)} +.diff-preview{min-width:0}.diff-path{font:600 13px ui-monospace,monospace;overflow-wrap:anywhere;margin:8px 0}.diff-metadata{display:grid;grid-template-columns:auto 1fr;gap:6px 10px;font-size:12px;color:var(--muted)}.diff-metadata dd{margin:0;overflow-wrap:anywhere}.diff-lines{overflow:auto;max-height:560px;border:1px solid var(--line);border-radius:6px;font:12px/1.7 ui-monospace,monospace}.diff-line{display:flex;min-width:max-content}.diff-line.added{background:var(--green-bg)}.diff-line.removed{background:var(--danger-bg)}.line-number{display:inline-block;width:4em;flex-shrink:0;text-align:right;padding-right:10px;color:var(--muted);user-select:none}.line-sign{width:1.5em;flex-shrink:0}.line-text{white-space:pre;padding-right:12px;font:inherit}.diff-hunk{background:var(--surface-muted);padding:6px 10px;color:var(--ink);white-space:nowrap}.missing-newline{color:var(--muted);padding-left:9em;font-size:12px} @media(max-width:900px){.changes-layout{grid-template-columns:1fr}.changed-file-list{max-height:240px}.revision-changes{padding:16px}} .revision-dialog { @@ -744,7 +733,7 @@ h1 { border-radius:14px; background:var(--paper); color:var(--ink); - box-shadow:0 24px 80px #152a3440; + box-shadow:0 8px 32px rgba(15,23,42,.08); overflow:auto; } .revision-dialog::backdrop { background:#18232f70; } @@ -767,19 +756,19 @@ body:has(.revision-dialog[open]) { overflow:hidden; } .revision-dialog .detail-grid { grid-template-columns:1fr; } .revision-changes-section>.revision-section-title { margin:18px 16px 0; } } -.revision-dialog-subtitle { margin:8px 0 0; color:#657282; font-size:12px; overflow-wrap:anywhere; } +.revision-dialog-subtitle { margin:8px 0 0; color:var(--muted); font-size:12px; overflow-wrap:anywhere; } .revision-dialog-close { width:32px; height:32px; flex-shrink:0; border:0; border-radius:6px; - background:#f3f5f7; - color:#657282; + background:var(--surface-muted); + color:var(--muted); font-size:24px; line-height:1; } -.revision-dialog-close:hover { background:#e5eaee; } +.revision-dialog-close:hover { background:var(--line); } @media(max-width:600px) { .revision-row { gap:7px; padding:13px 4px; } .revision-node { width:15px; } @@ -809,7 +798,7 @@ body:has(.revision-dialog[open]) { overflow:hidden; } .branch-file-list {max-height:55vh; overflow-y:auto;} .branch-file-kind {font-size:12px; color:var(--muted); flex-shrink:0;} .branch-file-path {overflow-wrap:anywhere; margin-top:0;} -.branch-file-content {white-space:pre-wrap; overflow-wrap:anywhere; max-height:52vh; overflow:auto; font:13px/1.6 ui-monospace,monospace; background:#f7f9fb; padding:16px; border-radius:8px; tab-size:4;} +.branch-file-content {white-space:pre-wrap; overflow-wrap:anywhere; max-height:52vh; overflow:auto; font:13px/1.6 ui-monospace,monospace; background:var(--surface-muted); padding:16px; border-radius:8px; tab-size:4;} .branch-files-dialog .changes-layout {align-items:start;} @media (max-width:700px) {.branch-files-body {padding:16px;} .branch-file-list {max-height:28vh;}} @@ -822,5 +811,5 @@ body:has(.revision-dialog[open]) { overflow:hidden; } .relationship-table th { color:var(--muted); font-size:12px; font-weight:500; } .relationship-table td { max-width:280px; font-size:13px; overflow-wrap:anywhere; } .relationship-table small,.relationship-table .relationship-id,.relationship-table .mount-state { display:block; margin-top:5px; } -.relationship-table small,.relationship-id { color:var(--muted); font-size:11px; } +.relationship-table small,.relationship-id { color:var(--muted); font-size:12px; } .relationship-more { margin:12px 20px 20px; } diff --git a/crates/agentdrive-mcp/dashboard/dashboard.js b/crates/agentdrive-mcp/dashboard/dashboard.js index f50c496..d93eeec 100644 --- a/crates/agentdrive-mcp/dashboard/dashboard.js +++ b/crates/agentdrive-mcp/dashboard/dashboard.js @@ -1,3 +1,4 @@ +import { decorateIcon, initializeIcons } from './ui.js'; import { createSandboxesPage } from './sandboxes.js'; import { createWorkspaceActions } from './workspace-actions.js'; import { createNavigation } from './navigation.js'; @@ -12,6 +13,8 @@ import { createHistoryList } from './history.js'; import { workspaceSyncStatus } from './sync-status.js'; import { createMergesView } from './merges.js'; const $ = (id) => document.getElementById(id); +initializeIcons(); +document.querySelector('.skip-link').onclick = event => { event.preventDefault(); $('main-content').focus(); }; sessionStorage.removeItem('agentdrive-token'); if (new URLSearchParams(location.hash.slice(1)).has('token')) history.replaceState(null, '', location.pathname + location.search); let snapshot = null; @@ -27,6 +30,8 @@ function element(tag, className, text) { const node = document.createElement(tag); if (className) node.className = className; if (text !== undefined) node.textContent = text; + if (tag === 'h3' && text) node.title = text; + decorateIcon(node, className, text); return node; } function short(id) { return id ? id.slice(0, 8) : '—'; } @@ -161,7 +166,7 @@ async function refresh(force=false) { } catch (error) { if (request !== generation) return; $('error').textContent = error.message; $('error').hidden = false; $('connection').textContent = '连接需要检查'; - } finally { if (request === generation) { loading = false; $('refresh').disabled = false; } } + } finally { if (request === generation) { loading = false; $('refresh').disabled = false; $('app').classList.remove('is-loading'); $('main-content').setAttribute('aria-busy', 'false'); } } } const workspaceActions = createWorkspaceActions({api, element, changed: async id => { if (!isSettings() && currentWorkspace === id) history.replaceState(null, '', '/archive'); @@ -217,6 +222,8 @@ function clearWorkspace() { historyList.clear(); revisionViewer.clear(); branchFileViewer.clear(); archivePage.clear(); relationshipsView.clear(); mountsPage.clear(); branchesPage.clear(); mergesView.clear(); } function showPage() { + $('app').classList.add('is-loading'); + $('main-content').setAttribute('aria-busy', 'true'); clearTimeout(remoteTimer); generation++; loading = false; snapshot = null; $('refresh').disabled = false; $('error').hidden = true; clearWorkspace(); sandboxesPage.clear(); diff --git a/crates/agentdrive-mcp/dashboard/favicon.svg b/crates/agentdrive-mcp/dashboard/favicon.svg index b8b9e84..f96e2bd 100644 --- a/crates/agentdrive-mcp/dashboard/favicon.svg +++ b/crates/agentdrive-mcp/dashboard/favicon.svg @@ -1 +1 @@ - + diff --git a/crates/agentdrive-mcp/dashboard/index.html b/crates/agentdrive-mcp/dashboard/index.html index cfd9f39..0f82190 100644 --- a/crates/agentdrive-mcp/dashboard/index.html +++ b/crates/agentdrive-mcp/dashboard/index.html @@ -12,8 +12,10 @@ + +
-
-
只读浏览正在连接
+
+
只读浏览正在连接
+
+

正在读取工作区数据…

+ +
@@ -38,18 +51,18 @@
WORKSPACE HISTORY

分支与版本.

查看本机与其他节点已同步的分支、来源版本和本机挂载状态。

本地存储
-
工作分支 ⑂
—当前工作区的有效分支
-
挂载点 ▣
—等待挂载数据
-
被挂载的分支 ⑂
—按就绪挂载的分支去重
-
远程同步 ↗
—等待同步状态
+
工作分支
—当前工作区的有效分支
+
挂载点
—等待挂载数据
+
被挂载的分支
—按就绪挂载的分支去重
+
远程同步
—等待同步状态
- +
-

分支树

连接点标明创建分支时的来源版本

来源版本⑂ 工作分支点击分支查看历史 · 双击本机分支预览文件
+

分支树

连接点标明创建分支时的来源版本

来源版本工作分支点击分支查看历史 · 双击本机分支预览文件
所选分支

—

—

版本历史 0

双击版本查看详情
diff --git a/crates/agentdrive-mcp/dashboard/management.css b/crates/agentdrive-mcp/dashboard/management.css index bd5d572..d1e1d5e 100644 --- a/crates/agentdrive-mcp/dashboard/management.css +++ b/crates/agentdrive-mcp/dashboard/management.css @@ -11,7 +11,7 @@ .management-row-copy { min-width:0; } .management-row h3 { margin:0 0 8px; font-size:15px; overflow-wrap:anywhere; } .management-row p { color:var(--muted); font-size:12px; line-height:1.7; margin:0; overflow-wrap:anywhere; } -.management-id { display:block; margin-top:8px; font-size:11px; color:var(--muted); } +.management-id { display:block; margin-top:8px; font-size:12px; color:var(--muted); } .management-actions { display:flex; flex-wrap:wrap; align-items:center; gap:10px; flex-shrink:0; } .management-actions .secondary { font-size:12px; padding:9px 13px; } .management-link { color:var(--accent); text-decoration:none; font-size:12px; padding:8px 0; } @@ -20,7 +20,7 @@ .command-panel h2 { margin:0; font-size:17px; } .command-status { font-size:14px; line-height:1.7; } .command-flags { display:flex; flex-wrap:wrap; gap:8px; } -.command-error { color:#9b443e; overflow-wrap:anywhere; font-size:13px; line-height:1.7; } +.command-error { color:var(--danger); overflow-wrap:anywhere; font-size:13px; line-height:1.7; } .command-note { color:var(--muted); font-size:12px; line-height:1.7; } .command-panel details { font-size:12px; color:var(--muted); margin-top:16px; } .command-panel summary { cursor:pointer; } @@ -40,7 +40,7 @@ .collaboration-panel { padding-bottom:24px; } .collaboration-description,.collaboration-panel > .command-error { margin:0 24px 18px; font-size:13px; line-height:1.7; color:var(--muted); } .collaboration-settings-link { display:inline-block; margin:0 24px 18px; color:var(--green); } -.collaboration-panel > .command-error { color:#9b443e; } +.collaboration-panel > .command-error { color:var(--danger); } .collaboration-mount { margin:0 24px 20px; } .collaboration-state { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:14px 24px; margin:0 24px; padding-top:20px; border-top:1px solid var(--line); } .collaboration-detail { min-width:0; display:flex; flex-direction:column; gap:7px; font-size:13px; overflow-wrap:anywhere; } @@ -59,62 +59,62 @@ } .sandbox-controls { display:inline-flex; } .sandbox-trigger { display:inline-flex; align-items:center; gap:8px; } -.sandbox-dot { width:7px; height:7px; border-radius:50%; background:#94a3b8; } -.sandbox-dot[data-state="ready"] { background:#16856b; } -.sandbox-dot[data-state="creating"],.sandbox-dot[data-state="starting"],.sandbox-dot[data-state="stopping"] { background:#3b82f6; } -.sandbox-dot[data-state="error"] { background:#c34242; } +.sandbox-dot { width:7px; height:7px; border-radius:50%; background:var(--muted); } +.sandbox-dot[data-state="ready"] { background:var(--accent-hover); } +.sandbox-dot[data-state="creating"],.sandbox-dot[data-state="starting"],.sandbox-dot[data-state="stopping"] { background:var(--muted); } +.sandbox-dot[data-state="error"] { background:var(--danger); } .sandbox-dialog { width:min(640px,calc(100% - 32px)); } .sandbox-heading { align-items:flex-start; } -.sandbox-eyebrow { color:var(--muted); font-size:10px; letter-spacing:1.5px; margin-bottom:8px; } +.sandbox-eyebrow { color:var(--muted); font-size:12px; letter-spacing:1.5px; margin-bottom:8px; } .sandbox-heading h2 { font-size:23px; } .sandbox-identity { margin:8px 0 0; color:var(--muted); font-size:13px; overflow-wrap:anywhere; } .sandbox-body { padding:24px; } -.sandbox-overview { padding:20px; background:#f6f8fb; border:1px solid var(--line); border-radius:12px; } -.sandbox-badge { display:inline-flex; align-items:center; font-size:12px; font-weight:600; padding:4px 9px; border-radius:6px; background:#e8edf3; color:#546176; } -.sandbox-badge[data-state="ready"] { background:#e0f3eb; color:#127357; } -.sandbox-badge[data-state="starting"],.sandbox-badge[data-state="creating"] { background:#e4edff; color:#255fbb; } -.sandbox-badge[data-state="error"] { background:#fbe9e9; color:#b03939; } +.sandbox-overview { padding:20px; background:var(--surface-muted); border:1px solid var(--line); border-radius:12px; } +.sandbox-badge { display:inline-flex; align-items:center; font-size:12px; font-weight:600; padding:4px 9px; border-radius:6px; background:var(--line); color:var(--muted); } +.sandbox-badge[data-state="ready"] { background:var(--line); color:var(--accent-hover); } +.sandbox-badge[data-state="starting"],.sandbox-badge[data-state="creating"] { background:var(--surface-muted); color:var(--muted); } +.sandbox-badge[data-state="error"] { background:var(--surface-muted); color:var(--danger); } .sandbox-status { margin:12px 0 18px; font-size:13px; line-height:1.8; color:var(--muted); overflow-wrap:anywhere; } .sandbox-actions,.sandbox-config-actions { display:flex; flex-wrap:wrap; align-items:center; gap:10px; } .sandbox-actions button,.sandbox-desktop { font-size:13px; padding:10px 16px; border-radius:7px; } .sandbox-desktop { display:inline-flex; text-decoration:none; align-items:center; justify-content:center; } -.sandbox-stop { color:#9b443e; } +.sandbox-stop { color:var(--danger); } .sandbox-delete-dialog { width:min(500px,calc(100% - 32px)); padding:28px; } .sandbox-delete-dialog p { line-height:1.7; overflow-wrap:anywhere; } .sandbox-delete-dialog label { display:grid; gap:10px; margin-top:20px; } -.sandbox-delete-dialog input { width:100%; box-sizing:border-box; padding:10px; border:1px solid #d5dfdc; border-radius:6px; } +.sandbox-delete-dialog input { width:100%; box-sizing:border-box; padding:10px; border:1px solid var(--line); border-radius:6px; } .sandbox-delete-dialog .sandbox-actions { justify-content:flex-end; margin-top:20px; } -.sandbox-delete-confirm { background:#a13d36; } +.sandbox-delete-confirm { background:var(--danger); } .sandbox-section { border-bottom:1px solid var(--line); } .sandbox-section > summary { cursor:pointer; padding:20px 0; font-size:14px; font-weight:600; } .sandbox-section-meta { float:right; margin-right:4px; font-size:12px; font-weight:400; color:var(--muted); } .sandbox-steps { list-style:none; padding:0 0 16px; margin:0; } .sandbox-step { display:flex; gap:12px; align-items:center; padding:9px 0; color:var(--muted); font-size:12px; } -.sandbox-step-marker { width:24px; height:24px; border:1px solid #dbe1e9; border-radius:50%; display:inline-flex; align-items:center; justify-content:center; flex:none; font-size:11px; } -.sandbox-step-state { margin-left:auto; padding-left:8px; white-space:nowrap; font-size:11px; } -.sandbox-step.active { color:#2563eb; } -.sandbox-step.active .sandbox-step-marker { border-color:#93b4ed; background:#eef4ff; } -.sandbox-step.complete .sandbox-step-marker { background:#eaf6f0; color:#16856b; border-color:#c9e7da; } -.sandbox-step.error,.sandbox-error { color:#b91c1c; } +.sandbox-step-marker { width:24px; height:24px; border:1px solid var(--line); border-radius:50%; display:inline-flex; align-items:center; justify-content:center; flex:none; font-size:12px; } +.sandbox-step-state { margin-left:auto; padding-left:8px; white-space:nowrap; font-size:12px; } +.sandbox-step.active { color:var(--muted); } +.sandbox-step.active .sandbox-step-marker { border-color:var(--muted); background:var(--surface-muted); } +.sandbox-step.complete .sandbox-step-marker { background:var(--surface-muted); color:var(--accent-hover); border-color:var(--line); } +.sandbox-step.error,.sandbox-error { color:var(--danger); } .sandbox-section-body { padding-bottom:20px; } .sandbox-section-body p,.sandbox-guide p { margin:0 0 14px; font-size:12px; color:var(--muted); line-height:1.8; } -.sandbox-config { width:100%; box-sizing:border-box; resize:vertical; min-height:160px; max-height:360px; padding:14px; margin-bottom:12px; border:1px solid var(--line); border-radius:8px; background:#f6f8fb; color:var(--ink); font:11px/1.7 ui-monospace,SFMono-Regular,monospace; } +.sandbox-config { width:100%; box-sizing:border-box; resize:vertical; min-height:160px; max-height:360px; padding:14px; margin-bottom:12px; border:1px solid var(--line); border-radius:8px; background:var(--surface-muted); color:var(--ink); font:12px/1.7 ui-monospace,SFMono-Regular,monospace; } .sandbox-config-actions button { font-size:12px; padding:8px 12px; } .sandbox-section-body .sandbox-feedback { margin:10px 0; color:var(--accent); } .sandbox-feedback:empty { display:none; } -.sandbox-section-body .sandbox-note { margin:14px 0 0; font-size:11px; } +.sandbox-section-body .sandbox-note { margin:14px 0 0; font-size:12px; } .sandbox-guide { display:grid; grid-template-columns:1fr 1fr; gap:24px; margin-top:22px; } .sandbox-guide h3 { margin:0 0 8px; font-size:12px; font-weight:600; } -.sandbox-guide p { margin:0; font-size:11px; } +.sandbox-guide p { margin:0; font-size:12px; } .sandbox-dialog [hidden] { display:none !important; } @media(max-width:600px) { .sandbox-body { padding:16px; } .sandbox-overview { padding:16px; } .sandbox-guide { grid-template-columns:1fr; gap:18px; } - .sandbox-section-meta { font-size:11px; } + .sandbox-section-meta { font-size:12px; } } .sandbox-heading { position:sticky; top:0; z-index:1; background:var(--paper); } .sandbox-actions .primary { width:auto; margin:0; } -.sandbox-guide p,.sandbox-step { color:#697586; } -.sandbox-step.active { color:#2563eb; } -.sandbox-step.error { color:#b91c1c; } +.sandbox-guide p,.sandbox-step { color:var(--muted); } +.sandbox-step.active { color:var(--muted); } +.sandbox-step.error { color:var(--danger); } diff --git a/crates/agentdrive-mcp/dashboard/merges.css b/crates/agentdrive-mcp/dashboard/merges.css index a4505e8..e745e7f 100644 --- a/crates/agentdrive-mcp/dashboard/merges.css +++ b/crates/agentdrive-mcp/dashboard/merges.css @@ -1,23 +1,23 @@ .history-tabs { display:flex; gap:6px; border-bottom:1px solid var(--line); margin-bottom:20px; } -.history-tabs button { border:0; border-bottom:3px solid transparent; background:transparent; color:#687382; padding:12px 22px; font-weight:600; } +.history-tabs button { border:0; border-bottom:3px solid transparent; background:transparent; color:var(--muted); padding:12px 22px; font-weight:600; } .history-tabs button[aria-selected="true"] { color:var(--green); border-bottom-color:var(--green); } .merge-heading,.merge-filters { padding:22px 24px 0; } .merge-heading { display:flex; justify-content:space-between; } .merge-heading h2 { display:flex; gap:10px; align-items:center; margin:0; font-size:16px; } -.merge-heading p,.merge-muted { color:#7b8492; font-size:12px; line-height:1.7; } +.merge-heading p,.merge-muted { color:var(--muted); font-size:12px; line-height:1.7; } .merge-filter-main,.merge-filter-fields { display:grid; grid-template-columns:repeat(2,minmax(150px,1fr)) auto; gap:14px; align-items:end; } .merge-filter-actions { display:flex; flex-wrap:wrap; gap:8px; } -.merge-filter-extra { margin:16px 0 0; color:#657282; } +.merge-filter-extra { margin:16px 0 0; color:var(--muted); } .merge-filter-extra summary { cursor:pointer; width:max-content; } .merge-filter-fields { grid-template-columns:repeat(3,minmax(0,1fr)); padding-top:14px; } .merge-filters input,.merge-filters select { width:100%; min-width:0; } -.merge-list-status { margin:16px 24px; color:#7b8492; line-height:1.7; } +.merge-list-status { margin:16px 24px; color:var(--muted); line-height:1.7; } #merges-reload { margin:0 24px 16px; } .merge-revision-filter { margin:16px 24px; display:flex; align-items:center; gap:12px; } .merge-clear-filter,.merge-text-button,.merge-version-link { border:0; background:transparent; color:var(--green); padding:0; text-decoration:underline; text-underline-offset:3px; } .merge-table { width:100%; table-layout:fixed; border-collapse:collapse; font-size:12px; } .merge-table th,.merge-table td { text-align:left; padding:18px 16px; border-top:1px solid var(--line); vertical-align:top; overflow-wrap:anywhere; } -.merge-table th { color:#858d9a; font-weight:500; } +.merge-table th { color:var(--muted); font-weight:500; } .merge-table th:first-child { width:30%; padding-left:24px; } .merge-table td:first-child { padding-left:24px; } .merge-table th:nth-child(2) { width:18%; } @@ -25,50 +25,50 @@ .merge-table th:nth-child(4) { width:15%; } .merge-table th:nth-child(5) { width:14%; } .merge-table th:last-child { width:10%; } -.merge-table small { display:block; margin-top:7px; color:#858d9a; line-height:1.5; } +.merge-table small { display:block; margin-top:7px; color:var(--muted); line-height:1.5; } .merge-open { display:block; width:100%; text-align:left; padding:0; border:0; background:transparent; color:inherit; line-height:1.6; } .merge-open strong { font-size:13px; } .merge-open:hover strong { color:var(--green); text-decoration:underline; } -.merge-inline-revisions { display:flex; gap:8px; margin-top:7px; color:#858d9a; } -.merge-revision { font-size:11px; color:#748091; overflow-wrap:anywhere; } +.merge-inline-revisions { display:flex; gap:8px; margin-top:7px; color:var(--muted); } +.merge-revision { font-size:12px; color:var(--muted); overflow-wrap:anywhere; } .merge-state { white-space:normal; line-height:1.5; } -.merge-state-error { color:#a33d3d; background:#fbeeee; } +.merge-state-error { color:var(--danger); background:var(--surface-muted); } .merge-confirmed { color:var(--green); } -.merge-error-code,.merge-event-error { color:#a33d3d; } -.merge-empty { padding:30px; text-align:center; color:#858d9a; } +.merge-error-code,.merge-event-error { color:var(--danger); } +.merge-empty { padding:30px; text-align:center; color:var(--muted); } #merges-more { margin:20px 24px; } .merge-revision-mark { color:var(--green); font-weight:600; } .merge-dialog { width:min(1100px,calc(100% - 40px)); } -.merge-dialog .revision-dialog-heading { position:sticky; top:0; background:#fff; z-index:1; border-bottom:1px solid var(--line); } -.merge-detail-feedback { padding:0 24px; color:#a33d3d; line-height:1.7; } +.merge-dialog .revision-dialog-heading { position:sticky; top:0; background:var(--paper); z-index:1; border-bottom:1px solid var(--line); } +.merge-detail-feedback { padding:0 24px; color:var(--danger); line-height:1.7; } .merge-detail-feedback:empty { display:none; } .merge-dialog > .secondary { margin:8px 24px 20px; } .merge-overview,.merge-timeline-section { padding:20px 24px; } .merge-detail-states { display:flex; flex-wrap:wrap; gap:8px; } -.merge-history-warning { background:#fff7e5; color:#8d6628; padding:10px 14px; border-radius:6px; font-size:12px; line-height:1.7; } +.merge-history-warning { background:var(--surface-muted); color:var(--warning); padding:10px 14px; border-radius:6px; font-size:12px; line-height:1.7; } .merge-version-relation { display:grid; grid-template-columns:1fr 36px 1fr; gap:10px; align-items:center; margin:18px 0; } .merge-parent-versions { display:grid; gap:10px; } -.merge-version-card { display:flex; flex-direction:column; gap:8px; border:1px solid var(--line); background:#f8fbfa; border-radius:8px; padding:14px; min-width:0; overflow-wrap:anywhere; } +.merge-version-card { display:flex; flex-direction:column; gap:8px; border:1px solid var(--line); background:var(--surface-muted); border-radius:8px; padding:14px; min-width:0; overflow-wrap:anywhere; } .merge-version-link { text-align:left; width:max-content; max-width:100%; font:12px ui-monospace,monospace; } .merge-relation-arrow { text-align:center; color:var(--green); font-size:24px; } -.merge-metadata summary,.merge-event-identifiers summary { cursor:pointer; color:#7b8492; width:max-content; } +.merge-metadata summary,.merge-event-identifiers summary { cursor:pointer; color:var(--muted); width:max-content; } .merge-metadata .detail-grid { padding:18px 0 0; } .merge-metadata .detail-field dd { overflow-wrap:anywhere; } .merge-timeline-section,.merge-changes-section { border-top:1px solid var(--line); } .merge-timeline { margin:0; padding:0; list-style:none; } -.merge-event { padding:0 0 22px 22px; border-left:2px solid #dcebe5; position:relative; } +.merge-event { padding:0 0 22px 22px; border-left:2px solid var(--line); position:relative; } .merge-event:last-child { padding-bottom:0; } -.merge-event:before { content:""; position:absolute; top:3px; left:-6px; width:10px; height:10px; background:white; border:2px solid #78b39d; border-radius:50%; } +.merge-event:before { content:""; position:absolute; top:3px; left:-6px; width:10px; height:10px; background:white; border:2px solid var(--accent); border-radius:50%; } .merge-event-heading { display:flex; align-items:baseline; flex-wrap:wrap; gap:10px; line-height:1.6; } -.merge-event-heading time { margin-left:auto; color:#89929e; font-size:11px; } -.merge-event-copy { display:flex; flex-wrap:wrap; gap:8px 14px; align-items:center; padding-top:8px; font-size:12px; color:#687382; } +.merge-event-heading time { margin-left:auto; color:var(--muted); font-size:12px; } +.merge-event-copy { display:flex; flex-wrap:wrap; gap:8px 14px; align-items:center; padding-top:8px; font-size:12px; color:var(--muted); } .merge-event-copy > p,.merge-event-copy > details,.merge-event-copy > ul { width:100%; margin:0; } .merge-event-paths { padding:0; list-style:none; } .merge-event-paths li { display:flex; flex-wrap:wrap; gap:10px; margin:6px 0; overflow-wrap:anywhere; } .merge-event-identifiers pre { white-space:pre-wrap; overflow-wrap:anywhere; line-height:1.8; } .merge-changes-section > .revision-section-title { margin:20px 24px 16px; } -.merge-comparison-label { display:flex; flex-wrap:wrap; align-items:center; gap:12px; margin:0 24px; color:#657282; } -.merge-comparison-label select { flex:1; min-width:0; border:1px solid var(--line); border-radius:6px; padding:10px; background:#fff; color:var(--ink); } +.merge-comparison-label { display:flex; flex-wrap:wrap; align-items:center; gap:12px; margin:0 24px; color:var(--muted); } +.merge-comparison-label select { flex:1; min-width:0; border:1px solid var(--line); border-radius:6px; padding:10px; background:var(--paper); color:var(--ink); } @media(max-width:1000px) { .merge-filter-main { grid-template-columns:repeat(2,minmax(0,1fr)); } .merge-filter-actions { grid-column:1/-1; } @@ -78,7 +78,7 @@ .merge-table tr { border-top:1px solid var(--line); padding:14px 24px; } .merge-table td,.merge-table td:first-child { border:0; padding:6px 0; } .merge-table td:not(:first-child) { display:grid; grid-template-columns:105px minmax(0,1fr); align-items:start; gap:4px 8px; } - .merge-table td:not(:first-child):before { content:attr(data-label); color:#858d9a; grid-column:1; } + .merge-table td:not(:first-child):before { content:attr(data-label); color:var(--muted); grid-column:1; } .merge-table td:not(:first-child) > * { grid-column:2; } .merge-table td:not(:first-child) > :first-child { grid-row:1; } } diff --git a/crates/agentdrive-mcp/dashboard/navigation.css b/crates/agentdrive-mcp/dashboard/navigation.css index ede0856..921df91 100644 --- a/crates/agentdrive-mcp/dashboard/navigation.css +++ b/crates/agentdrive-mcp/dashboard/navigation.css @@ -1,104 +1,96 @@ -.app { --workspace-width:250px; --menu-width:186px; display:flex; } +/* Workspace rail and contextual navigation share the light surface palette. */ +.app { --workspace-width:220px; --menu-width:184px; display:flex; } .app.workspaces-collapsed { --workspace-width:64px; } -.workspace-sidebar { position:fixed; inset:0 auto 0 0; width:var(--workspace-width); z-index:30; display:flex; flex-direction:column; background:var(--nav); color:#c2cbd6; border-right:1px solid #303b49; } -.workspace-brand { display:flex; align-items:center; height:76px; flex-shrink:0; padding:0 17px; gap:8px; } -.workspace-brand .brand { min-width:0; gap:9px; font-size:19px; } -.workspace-brand .brand-mark { flex-shrink:0; } -.workspace-toggle { margin-left:auto; width:25px; height:28px; flex-shrink:0; color:#a9b8c6; background:#263240; border:1px solid #3a4756; border-radius:5px; font-size:23px; line-height:1; } -.workspace-toggle:hover { color:white; background:#354556; } -.workspace-list-heading { display:flex; justify-content:space-between; align-items:center; padding:15px 20px 13px; font-size:11px; color:#99a7b8; } -.workspace-list-heading .count-badge { background:#273240; color:#b2becc; } -.workspace-search { margin:0 13px 14px; width:calc(100% - 26px); padding:10px 11px; border:1px solid #354352; border-radius:6px; background:#202b39; color:#e8eef3; } -.workspace-search::placeholder { color:#8997a8; } -.workspace-list { margin:0; padding:0 10px 14px; overflow-y:auto; flex:1; min-height:0; scrollbar-width:thin; scrollbar-color:#465668 transparent; } -.workspace-link { display:flex; align-items:center; gap:11px; min-height:51px; padding:10px; margin-bottom:5px; border:1px solid transparent; border-radius:7px; color:#b4c0ce; text-decoration:none; } -.workspace-link:hover { background:#222f3d; color:white; } -.workspace-link.workspace-active { background:#28413c; border-color:#3f6459; color:#d2f0e4; } -.workspace-avatar { flex:0 0 27px; width:27px; height:29px; display:grid; place-items:center; border:1px solid #41505f; border-radius:6px; background:#2a3746; color:#b6c8d6; font-size:12px; font-weight:600; } -.workspace-active .workspace-avatar { background:#356150; border-color:#568570; color:#c6efdb; } -.workspace-title { font-size:12px; line-height:1.55; overflow-wrap:anywhere; } -.workspace-list-empty { padding:0 20px; color:#91a0b3; font-size:12px; } -.workspace-sidebar-footer { margin-top:auto; padding:12px; border-top:1px solid #303b49; } -.global-settings { display:flex; align-items:center; gap:12px; padding:11px 12px; color:#bdc8d4; border:1px solid transparent; border-radius:6px; text-decoration:none; font-size:12px; } -.global-settings:hover,.global-settings.nav-active { color:#d4eddf; background:#28413c; border-color:#3f6459; } -.global-settings .branch-icon { font-size:21px; } -.connection-status { display:flex; align-items:center; gap:8px; color:#8495a6; font-size:10px; padding:14px 12px 3px; } -.workspace-menu { position:fixed; inset:0 auto 0 var(--workspace-width); width:var(--menu-width); padding:32px 14px; background:#fbfcfc; border-right:1px solid #e0e7e4; overflow:auto; } -.workspace-menu-heading { padding:0 10px 24px; border-bottom:1px solid #e6ebe8; } -.workspace-menu-heading small { display:block; font-size:10px; letter-spacing:1px; color:#89948f; margin-bottom:10px; } -.workspace-menu-heading strong { font-size:14px; line-height:1.6; overflow-wrap:anywhere; font-weight:600; } +.workspace-sidebar { position:fixed; inset:0 auto 0 0; width:var(--workspace-width); z-index:30; display:flex; flex-direction:column; background:var(--paper); border-right:1px solid var(--line); } +.workspace-brand { display:flex; align-items:center; height:72px; flex-shrink:0; padding:0 16px; gap:8px; } +.workspace-brand .brand { min-width:0; gap:10px; color:var(--ink); font-size:17px; letter-spacing:-.02em; } +.workspace-brand .brand-mark { flex-shrink:0; width:28px; height:28px; background:var(--ink); color:var(--paper); border:0; border-radius:8px; font-size:16px; font-weight:600; line-height:1; } +.workspace-toggle { margin-left:auto; width:28px; height:32px; flex-shrink:0; display:grid; place-items:center; color:var(--muted); background:transparent; border:1px solid transparent; border-radius:6px; } +.workspace-toggle:hover { color:var(--ink); background:var(--surface-muted); } +.workspace-list-heading { display:flex; justify-content:space-between; align-items:center; padding:20px 20px 12px; font-size:12px; color:var(--muted); } +.workspace-search { margin:0 12px 16px; width:calc(100% - 24px); padding:9px 12px; border:1px solid var(--line); border-radius:8px; background:var(--bg); color:var(--ink); font-size:12px; } +.workspace-search::placeholder { color:var(--muted); } +.workspace-list { margin:0; padding:0 8px 16px; overflow-y:auto; flex:1; min-height:0; scrollbar-width:thin; scrollbar-color:var(--line) transparent; } +.workspace-row { position:relative; } +.workspace-link { display:flex; align-items:center; gap:10px; min-height:48px; padding:10px 40px 10px 10px; margin-bottom:4px; border:1px solid transparent; border-radius:8px; color:var(--muted); text-decoration:none; } +.workspace-link:hover { background:var(--bg); color:var(--ink); } +.workspace-link.workspace-active { background:var(--surface-muted); color:var(--ink); border-color:var(--line); } +.workspace-avatar { flex:0 0 28px; width:28px; height:28px; display:grid; place-items:center; border:1px solid var(--line); border-radius:7px; background:var(--paper); color:var(--muted); font-size:12px; font-weight:600; } +.workspace-active .workspace-avatar { color:var(--ink); } +.workspace-title { font-size:13px; line-height:1.5; min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } +.workspace-list-empty { padding:16px 20px; color:var(--muted); font-size:12px; } +.workspace-sidebar-footer { margin-top:auto; padding:12px; border-top:1px solid var(--line); } +.global-settings { display:flex; align-items:center; gap:10px; padding:10px; color:var(--muted); border:1px solid transparent; border-radius:8px; text-decoration:none; font-size:13px; } +.global-settings:hover,.global-settings.nav-active { color:var(--ink); background:var(--surface-muted); } +.connection-status { display:flex; align-items:center; gap:8px; color:var(--muted); font-size:11px; padding:16px 10px 4px; } +.workspace-menu { position:fixed; inset:0 auto 0 var(--workspace-width); width:var(--menu-width); padding:28px 12px; background:var(--bg); border-right:1px solid var(--line); overflow:auto; } +.workspace-menu-heading { padding:0 12px 24px; border-bottom:1px solid var(--line); } +.workspace-menu-heading small { display:block; font-size:11px; color:var(--muted); margin-bottom:8px; } +.workspace-menu-heading strong { display:block; font-size:14px; line-height:1.6; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; font-weight:600; } .workspace-menu nav { margin-top:24px; } -.workspace-menu .nav-link { display:flex; align-items:center; gap:10px; padding:12px 10px; margin:0 0 8px; border:1px solid transparent; border-radius:6px; color:#64746e; text-decoration:none; font-size:12px; } -.workspace-menu .nav-link:hover { background:#f0f5f2; } -.workspace-menu .nav-link.nav-active { background:#e7f2ec; border-color:#c5dfd0; color:#276b51; font-weight:600; } -.workspace-menu .branch-icon { font-size:19px; width:20px; text-align:center; } -.workspace-menu .nav-link[aria-disabled=true] { opacity:.5; cursor:default; } +.workspace-menu .nav-link { display:flex; align-items:center; gap:10px; min-height:44px; padding:10px 12px; margin:0 0 6px; border:1px solid transparent; border-radius:8px; color:var(--muted); text-decoration:none; font-size:13px; white-space:nowrap; } +.workspace-menu .nav-link:hover { background:var(--surface-muted); color:var(--ink); } +.workspace-menu .nav-link.nav-active { background:var(--paper); border-color:var(--line); color:var(--ink); font-weight:600; } +.workspace-menu .nav-link[aria-disabled=true] { opacity:.45; cursor:default; } .app main { margin-left:calc(var(--workspace-width) + var(--menu-width)); flex:1; min-width:0; } .app.settings-view main { margin-left:var(--workspace-width); } -.app .topbar { padding:0 26px; gap:14px; } +.app .topbar { padding:0 32px; gap:16px; } .app .breadcrumb { display:flex; align-items:center; min-width:0; } .app .breadcrumb strong { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } .app .breadcrumb span { padding:0 10px; flex-shrink:0; } -.app #page-name { flex-shrink:0; font-size:11px; } +.app #page-name { flex-shrink:0; font-size:12px; } .app .topbar-right { flex-shrink:0; } .workspace-backdrop { display:none; } .workspaces-collapsed .brand-name,.workspaces-collapsed .workspace-list-heading,.workspaces-collapsed .workspace-search,.workspaces-collapsed .workspace-title,.workspaces-collapsed .workspace-list-empty,.workspaces-collapsed .settings-label-text,.workspaces-collapsed #connection { display:none; } -.workspaces-collapsed .workspace-brand { flex-direction:column; height:114px; padding:22px 0 14px; gap:14px; } -.workspaces-collapsed .workspace-toggle { margin:0; width:27px; height:24px; } -.workspaces-collapsed .workspace-list { padding:0 7px 12px; } -.workspaces-collapsed .workspace-link { padding:10px; justify-content:center; } -.workspaces-collapsed .workspace-sidebar-footer { padding:12px 7px; } -.workspaces-collapsed .global-settings { padding:11px 0; justify-content:center; } -.workspaces-collapsed .connection-status { padding:13px 0 1px; justify-content:center; } +.workspaces-collapsed .workspace-brand { flex-direction:column; height:116px; padding:22px 0 16px; gap:16px; } +.workspaces-collapsed .workspace-toggle { margin:0; } +.workspaces-collapsed .workspace-list { padding:0 6px 12px; } +.workspaces-collapsed .workspace-row .workspace-link { padding:10px; justify-content:center; } +.workspaces-collapsed .workspace-sidebar-footer { padding:12px 6px; } +.workspaces-collapsed .global-settings { padding:10px 0; justify-content:center; } +.workspaces-collapsed .connection-status { padding:16px 0 4px; justify-content:center; } +.workspace-delete-button { position:absolute; right:8px; top:50%; translate:0 -50%; width:28px; height:28px; display:grid; place-items:center; color:var(--muted); border:0; background:transparent; border-radius:6px; opacity:0; } +.workspace-row:hover .workspace-delete-button,.workspace-row:focus-within .workspace-delete-button { opacity:1; } +.workspace-delete-button:hover { background:var(--danger-bg); color:var(--danger); } +.workspaces-collapsed .workspace-delete-button { display:none; } +.revision-dialog.workspace-delete-dialog { max-width:560px; padding:24px; } +.workspace-delete-dialog h2 { margin:0 0 16px; font-size:20px; } +.workspace-delete-dialog p { line-height:1.7; } +.workspace-delete-dialog label { display:flex; flex-direction:column; gap:8px; margin:20px 0; } +.workspace-delete-dialog input { width:100%; padding:10px; border:1px solid var(--line); border-radius:8px; } +.workspace-delete-name { font-weight:600; overflow-wrap:anywhere; } +.workspace-delete-confirm { color:var(--paper); background:var(--danger); border:1px solid var(--danger); border-radius:8px; padding:10px 16px; } +.workspace-delete-dialog .management-actions { justify-content:flex-end; } +.workspace-delete-dialog .command-error:empty { display:none; } +@media(hover:none) { .workspace-delete-button { opacity:1; } } @media(min-width:901px) and (max-width:1300px) { - .app { --workspace-width:218px; --menu-width:170px; } + .app { --workspace-width:196px; --menu-width:164px; } .app.workspaces-collapsed { --workspace-width:64px; } - .workspace-brand { padding:0 13px; } - .workspace-brand .brand { font-size:17px; } - .app .panels { grid-template-columns:1fr; } - .app .archive-workspace { grid-template-columns:1fr; } - .app .archive-branches { border-right:0; border-bottom:1px solid #e3e9e4; } + .workspace-brand { padding:0 12px; } + .workspace-brand .brand { font-size:15px; } + .workspace-menu .nav-link { padding:10px 8px; gap:8px; } + .app .panels,.app .archive-workspace { grid-template-columns:1fr; } + .app .archive-branches { border-right:0; border-bottom:1px solid var(--line); } .app .archive-branch-list { display:flex; gap:8px; max-height:180px; overflow:auto; } .app .archive-branch { flex:0 0 180px; } } @media(max-width:900px) { - .app { --workspace-width:56px; --menu-width:0px; } - .app.workspaces-collapsed { --workspace-width:56px; } - .workspace-sidebar { width:min(280px, calc(100vw - 36px)); } + .app,.app.workspaces-collapsed { --workspace-width:56px; --menu-width:0px; } + .workspace-sidebar { width:min(300px,calc(100vw - 24px)); } .workspaces-collapsed .workspace-sidebar { width:56px; } .workspaces-collapsed .workspace-list { padding:0 4px 12px; } - .workspaces-collapsed .workspace-link { padding:9px; } - .workspace-backdrop:not([hidden]) { display:block; position:fixed; inset:0; z-index:25; background:#101e2e66; border:0; } - .workspace-menu { position:fixed; inset:0 0 auto 56px; width:auto; padding:12px 15px 0; z-index:20; border-bottom:1px solid #e0e7e4; border-right:0; } + .workspaces-collapsed .workspace-row .workspace-link { padding:10px 8px; } + .workspace-backdrop:not([hidden]) { display:block; position:fixed; inset:0; z-index:25; background:rgba(15,23,42,.28); border:0; border-radius:0; } + .workspace-menu { position:fixed; inset:0 0 auto 56px; width:auto; padding:12px 16px 0; z-index:20; border-bottom:1px solid var(--line); border-right:0; background:var(--paper); } .workspace-menu-heading { padding:0; border:0; } .workspace-menu-heading small { display:none; } - .workspace-menu-heading strong { display:block; font-size:12px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } - .workspace-menu nav { display:flex; gap:4px; margin-top:8px; overflow:auto; } - .workspace-menu .nav-link { flex-shrink:0; font-size:11px; padding:9px 6px; gap:4px; margin-bottom:8px; } - .workspace-menu .branch-icon { font-size:16px; width:16px; } + .workspace-menu-heading strong { font-size:12px; } + .workspace-menu nav { display:flex; gap:4px; margin-top:8px; overflow-x:auto; scrollbar-width:none; } + .workspace-menu .nav-link { flex:0 0 auto; min-height:40px; font-size:12px; padding:8px; gap:6px; margin-bottom:8px; } .app main { padding-top:90px; } .app.settings-view main { padding-top:0; } - .app .topbar { padding:0 16px; height:55px; } - .app #page-name,.app #page-divider,.app #view-mode { display:none; } - .app .breadcrumb { font-size:11px; } - .app .content { padding:24px 16px; } - .app .topbar-right #updated { display:none; } + .app .topbar { padding:0 16px; height:56px; } + .app #page-name,.app #page-divider,.app #view-mode,.app .topbar-right #updated { display:none; } + .app .content { padding:24px 16px 32px; } .app .panels { grid-template-columns:1fr; } } - -.workspace-row { position:relative; } -.workspace-row .workspace-link { padding-right:48px; } -.workspace-delete-button { position:absolute; right:6px; top:50%; transform:translateY(-50%); color:#eab6b0; border:1px solid #70544f; background:transparent; border-radius:4px; padding:5px; font-size:11px; } -.workspace-delete-button:hover,.workspace-delete-button:focus-visible { background:#603c39; color:white; } -.workspaces-collapsed .workspace-delete-button { display:none; } -.workspaces-collapsed .workspace-row .workspace-link { padding-right:10px; } -.revision-dialog.workspace-delete-dialog { max-width:560px; padding:24px; } -.workspace-delete-dialog h2 { margin:0 0 16px; font-size:20px; } -.workspace-delete-dialog p { line-height:1.7; } -.workspace-delete-dialog label { display:flex; flex-direction:column; gap:8px; margin:20px 0; } -.workspace-delete-dialog input { width:100%; padding:10px; border:1px solid #d3dcd8; border-radius:5px; } -.workspace-delete-name { font-weight:600; overflow-wrap:anywhere; } -.workspace-delete-confirm { color:white; background:#ac3931; border:1px solid #ac3931; border-radius:5px; padding:9px 14px; } -.workspace-delete-confirm:disabled { opacity:.45; } -.workspace-delete-dialog .management-actions { justify-content:flex-end; } -.workspace-delete-dialog .command-error:empty { display:none; } diff --git a/crates/agentdrive-mcp/dashboard/navigation.js b/crates/agentdrive-mcp/dashboard/navigation.js index 4758a3f..707ae7f 100644 --- a/crates/agentdrive-mcp/dashboard/navigation.js +++ b/crates/agentdrive-mcp/dashboard/navigation.js @@ -1,3 +1,5 @@ +import { icon } from './ui.js'; + export function createNavigation({element, $, navigate, deleteWorkspace}) { const pages = [ ['archive-nav', '/archive', '档案室'], @@ -7,6 +9,7 @@ export function createNavigation({element, $, navigate, deleteWorkspace}) { ['sandboxes-nav', '/sandboxes', '沙盒'], ]; let workspaces = [], selected = null, listKey = ''; + let activePath = ''; const compact = window.matchMedia('(max-width: 900px)'); let collapsed = localStorage.getItem('agentdrive-workspaces-collapsed'); collapsed = collapsed === null ? compact.matches : collapsed === 'true'; @@ -18,12 +21,15 @@ export function createNavigation({element, $, navigate, deleteWorkspace}) { $('workspace-toggle').setAttribute('aria-expanded', String(!value)); $('workspace-toggle').setAttribute('aria-label', label); $('workspace-toggle').title = label; - $('workspace-toggle').textContent = value ? '›' : '‹'; + $('workspace-toggle').replaceChildren(icon(value ? 'chevronRight' : 'chevronLeft')); $('workspace-backdrop').hidden = value; if (persist) localStorage.setItem('agentdrive-workspaces-collapsed', String(value)); if (value) { $('workspace-search').value = ''; filter(); } } - compact.addEventListener('change', event => { if (event.matches) setCollapsed(true, false); }); + function revealCurrentPage() { + if (compact.matches) requestAnimationFrame(() => $('workspace-menu').querySelector('[aria-current="page"]')?.scrollIntoView({block:'nearest', inline:'nearest'})); + } + compact.addEventListener('change', event => { if (event.matches) { setCollapsed(true, false); revealCurrentPage(); } }); $('workspace-toggle').onclick = () => setCollapsed(!collapsed); $('workspace-backdrop').onclick = () => setCollapsed(true); $('workspace-sidebar').addEventListener('keydown', event => { @@ -74,6 +80,7 @@ export function createNavigation({element, $, navigate, deleteWorkspace}) { if (current) $(id).setAttribute('aria-current', 'page'); else $(id).removeAttribute('aria-current'); } $('page-name').textContent = active[2]; + if (activePath !== location.pathname) { activePath = location.pathname; revealCurrentPage(); } document.title = `AgentDrive · ${settings ? '设置' : active[2]}`; } function update(items) { @@ -117,6 +124,7 @@ export function createNavigation({element, $, navigate, deleteWorkspace}) { } $('workspace-total').textContent = items.length; $('workspace-menu-name').textContent = selected?.name || '暂无工作区'; + $('workspace-menu-name').title = selected?.name || ''; $('workspace-name').textContent = selected?.name || '暂无工作区'; for (const [id,path] of pages) { $(id).setAttribute('aria-disabled', String(!selected && path !== '/sandboxes')); diff --git a/crates/agentdrive-mcp/dashboard/sandbox.js b/crates/agentdrive-mcp/dashboard/sandbox.js index 1965e24..7f076de 100644 --- a/crates/agentdrive-mcp/dashboard/sandbox.js +++ b/crates/agentdrive-mcp/dashboard/sandbox.js @@ -128,6 +128,7 @@ export function createSandboxControl({api, element, workspace, branch, branchNam confirmationLabel, deleteError, deleteActions); let busy = false, deleting = false, reading = false, disposed = false, timer = null, current = null, enabled = true, epoch = 0, configLoading = false; let confirmationAction = 'delete'; + let stopFailed = false; function updateConfirmation() { confirmDelete.disabled = busy || (confirmationAction === 'delete' ? remove.disabled : force.disabled) || confirmation.value !== branchName; } function schedule() { clearTimeout(timer); @@ -148,6 +149,7 @@ export function createSandboxControl({api, element, workspace, branch, branchNam const url = new URL(location.href); url.hostname = `${service.id}.${workspace}.${branch}.localhost`; url.pathname = '/'; url.search = ''; url.hash = '/new?agent=agent-agentdrive'; link.href = url.href; link.target = '_blank'; link.rel = 'noopener'; serviceActions.append(link); } + serviceActions.hidden = !serviceActions.childElementCount; if (quickActions) { quickActions.querySelector('.sandbox-service-actions')?.remove(); quickActions.prepend(serviceActions.cloneNode(true)); @@ -158,8 +160,8 @@ export function createSandboxControl({api, element, workspace, branch, branchNam create.hidden = ['creating', 'starting', 'ready', 'stopping'].includes(state); create.textContent = state === 'error' ? '重试启动' : '启动 Sandbox'; stop.hidden = state !== 'ready'; stop.disabled = busy; - force.hidden = !['ready', 'error'].includes(state); force.disabled = busy || !enabled || force.hidden; - remove.hidden = !value; remove.disabled = !enabled || busy || !['stopped', 'error'].includes(state); + force.hidden = state !== 'error' && !(state === 'ready' && (stopFailed || value?.message?.startsWith('正常停止失败'))); force.disabled = busy || !enabled || force.hidden; + remove.hidden = !['stopped', 'error'].includes(state); remove.disabled = !enabled || busy || remove.hidden; remove.title = ['stopped', 'error'].includes(state) ? '删除系统磁盘,保留分支文件' : '请先停止 Sandbox'; updateConfirmation(); desktop.hidden = state !== 'ready' || !value?.desktop_url; @@ -204,6 +206,7 @@ export function createSandboxControl({api, element, workspace, branch, branchNam } async function run(method, templateId) { if (busy || disposed) return; + stopFailed = false; busy = true; epoch++; show(method === 'POST' ? {...current,state:'creating',startup_step:'prepare_branch'} : {...current,state:'stopping'}); try { const result = await api(endpoint, {method, ...(templateId ? {headers:{"Content-Type":"application/json"}, body:JSON.stringify({template_id:templateId})} : {})}); busy = false; epoch++; if (!disposed) show(result); } @@ -211,6 +214,7 @@ export function createSandboxControl({api, element, workspace, branch, branchNam busy = false; epoch++; if (disposed) return; if (method === 'DELETE') { + stopFailed = true; try { const result = await api(endpoint); if (!disposed) show({...result.sandbox,message:error.message}, result.enabled); return; } catch { /* 状态无法读取时保留失败信息和强制停止入口。 */ } } diff --git a/crates/agentdrive-mcp/dashboard/sandboxes.css b/crates/agentdrive-mcp/dashboard/sandboxes.css index 67a7cbd..d0ab675 100644 --- a/crates/agentdrive-mcp/dashboard/sandboxes.css +++ b/crates/agentdrive-mcp/dashboard/sandboxes.css @@ -1,4 +1,4 @@ -.sandboxes-page { max-width:1500px; margin:auto; } +.sandboxes-page { max-width:1280px; margin:auto; } .sandboxes-page .page-heading { align-items:center; gap:24px; } .sandboxes-page .page-heading > button { flex-shrink:0; width:auto; padding:11px 18px; border-radius:7px; } .sandbox-summary { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); border:1px solid var(--line); border-radius:10px; background:var(--paper); margin-bottom:28px; } @@ -22,22 +22,22 @@ .sandbox-card .sandbox-actions { gap:8px; } .sandbox-card .sandbox-actions button,.sandbox-card .sandbox-desktop { padding:8px 11px; font-size:12px; } .sandbox-card-footer { display:flex; align-items:center; justify-content:space-between; padding-top:15px; margin-top:18px; border-top:1px solid var(--line); } -.sandbox-card-footer code { font-size:11px; color:var(--muted); } +.sandbox-card-footer code { font-size:12px; color:var(--muted); } .sandbox-empty { padding:48px 24px; text-align:center; font-size:13px; line-height:1.8; color:var(--muted); } .sandbox-template { padding:28px; } .sandbox-template-heading { display:flex; align-items:center; gap:14px; margin-bottom:26px; flex-wrap:wrap; } .sandbox-template-heading h2 { font-size:20px; margin:0; } -.sandbox-template-icon { font-size:28px; color:var(--accent); background:#e7f2ec; border-radius:10px; padding:8px 14px; } +.sandbox-template-icon { font-size:28px; color:var(--accent); background:var(--line); border-radius:10px; padding:8px 14px; } .sandbox-template-fields { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:24px 40px; } .sandbox-template-fields > div { border-top:1px solid var(--line); padding-top:16px; min-width:0; } .sandbox-template-fields dt { font-size:12px; color:var(--muted); margin-bottom:10px; } .sandbox-template-fields dd { font-size:13px; margin:0; line-height:1.8; white-space:pre-line; overflow-wrap:anywhere; } -.sandbox-template-note { margin:26px 0 0; padding:16px; border-radius:7px; background:#f5f8f6; color:var(--muted); font-size:12px; line-height:1.9; } +.sandbox-template-note { margin:26px 0 0; padding:16px; border-radius:7px; background:var(--surface-muted); color:var(--muted); font-size:12px; line-height:1.9; } .sandbox-create-dialog { width:min(560px,calc(100% - 32px)); } .sandbox-create-dialog h2 { font-size:20px; margin:0; } .sandbox-create-form { display:grid; gap:20px; padding:24px; } .sandbox-create-form p { margin:0; font-size:13px; line-height:1.8; color:var(--muted); } -.sandbox-create-form .sandbox-create-template { padding:12px 14px; background:#f5f8f6; border-radius:7px; } +.sandbox-create-form .sandbox-create-template { padding:12px 14px; background:var(--surface-muted); border-radius:7px; } .sandbox-create-form .sandbox-actions { justify-content:flex-end; padding-top:8px; } @media(max-width:650px) { .sandboxes-page .page-heading { flex-direction:column; align-items:flex-start; } diff --git a/crates/agentdrive-mcp/dashboard/ui.css b/crates/agentdrive-mcp/dashboard/ui.css new file mode 100644 index 0000000..563debd --- /dev/null +++ b/crates/agentdrive-mcp/dashboard/ui.css @@ -0,0 +1,131 @@ +/* Accessible interaction states and shared content treatments. */ +:where(button,a,input,select,textarea,summary,[role="tab"],.sandbox-card,.management-row) { transition:var(--motion); } +:where(button,a,[role="button"]):not(:disabled):active { transform:scale(.98); } +:where(input,select,textarea) { color:var(--ink); accent-color:var(--accent); } +:where(input,select,textarea):not(:disabled):hover { border-color:#CBD5E1; } +:where(button,a,input,select,textarea,summary,[tabindex]):focus-visible { outline:2px solid var(--accent); outline-offset:3px; } +:where(input,select,textarea):focus { outline:0; border-color:var(--accent); box-shadow:0 0 0 3px rgba(16,185,129,.12); } +:where(button,a)[aria-disabled="true"] { cursor:not-allowed; } +:where(button,a).primary { min-height:40px; align-items:center; gap:8px; font-weight:600; } +:where(button,a).secondary { min-height:36px; display:inline-flex; align-items:center; justify-content:center; gap:8px; border-radius:8px; background:var(--paper); color:var(--ink); border:1px solid var(--line); } +:where(button,a).secondary:hover { background:var(--bg); border-color:#CBD5E1; } +.primary:disabled,.secondary:disabled { opacity:.45; } +:where(h1,h2,h3) { letter-spacing:-.02em; line-height:1.3; } +:where(p,small) { line-height:1.6; } +.page-heading { gap:24px; } +.page-heading > div { min-width:0; } +.page-heading h1 { font-weight:700; } +.heading-dot { display:none; } +.eyebrow { color:var(--muted); letter-spacing:.12em; font-size:10px; margin-bottom:10px; } +.ui-icon { width:18px; height:18px; flex-shrink:0; display:inline-block; vertical-align:middle; } +.branch-icon,.branch-symbol,.archive-branch-symbol { display:inline-flex; align-items:center; justify-content:center; color:var(--muted); line-height:1; } +.connection-dot { background:var(--status); } +.refresh,.revision-dialog-close { display:inline-grid; place-items:center; width:36px; height:36px; } +.stat-label,.stat small,.stat strong,.sandbox-summary strong { font-variant-numeric:tabular-nums; } +.stat strong { font-weight:650; margin:16px 0 8px; } +.panel-heading { padding:24px; } +.panel-heading h2 { font-size:15px; } +.panel-heading p { font-size:12px; } +.graph-legend { padding:0 24px 20px; } +.panel,.archive-workspace { border-radius:12px; } +.panel-empty,.no-revisions { padding:32px 24px; line-height:1.7; } +.empty-state { padding:56px 24px; } +.empty-state > span { display:inline-grid; place-items:center; width:48px; height:48px; background:var(--surface-muted); border:1px solid var(--line); border-radius:12px; color:var(--muted); } +.empty-state .ui-icon { width:24px; height:24px; } +.empty-state h2 { color:var(--ink); } +.revision-dialog { box-shadow:0 8px 32px rgba(15,23,42,.08); } +.revision-dialog::backdrop { background:rgba(15,23,42,.28); } +.revision-dialog-heading { padding:24px; } +.revision-dialog-heading > div { min-width:0; } +.revision-dialog-close { border:1px solid var(--line); background:var(--paper); } +.revision-dialog-close:hover { background:var(--surface-muted); } +.truncate { overflow:hidden; white-space:nowrap; text-overflow:ellipsis; min-width:0; } +.management-row:hover { background:var(--bg); } +.management-row-copy { flex:1; } +.management-actions { justify-content:flex-end; max-width:100%; } +.management-row { flex-wrap:wrap; padding:24px; } +.management-row h3,.sandbox-card h3 { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } +.sandbox-card h3 { min-width:0; } +.sandbox-card { padding:24px; } +.sandbox-card:hover { border-color:#CBD5E1; } +.sandbox-card-message { margin-bottom:20px; } +.sandbox-card .sandbox-actions { align-items:center; } +.sandbox-overview { background:var(--bg); } +.sandbox-step.active,.sandbox-step.active .sandbox-step-marker { color:var(--accent); } +.sandbox-step.active .sandbox-step-marker { background:var(--green-bg); border-color:var(--accent-line); } +.sandbox-template-icon { display:grid; place-items:center; width:48px; height:48px; padding:12px; background:var(--surface-muted); color:var(--muted); border:1px solid var(--line); } +.sandbox-template-icon .ui-icon { width:24px; height:24px; } +.sandbox-template-heading h2 { font-size:18px; } +.sandbox-card-footer { gap:12px; } +.sandbox-filters input { min-width:min(100%,180px); } +.sandbox-section summary { list-style:none; display:flex; align-items:center; gap:8px; } +.sandbox-section summary::-webkit-details-marker { display:none; } +.sandbox-section summary:after { content:""; width:6px; height:6px; border-right:1.75px solid currentColor; border-bottom:1.75px solid currentColor; transform:rotate(45deg); margin-left:4px; transition:var(--motion); } +.sandbox-section[open] summary:after { transform:rotate(225deg); } +.sandbox-section-meta { margin-left:auto; } +.archive-page { padding:36px 32px 48px; } +.archive-heading h1 { font-size:30px; letter-spacing:-.02em; } +.archive-branch-header { padding:24px; } +.archive-workspace { box-shadow:none; } +.archive-branches { background:var(--bg); padding:24px 12px; } +.archive-states { background:var(--bg); } +.archive-state strong { color:var(--ink); } +.document-preview { color:var(--ink); line-height:1.8; } +.document-preview :is(h1,h2,h3) { color:var(--ink); } +.archive-files .branch-file-preview { padding:24px; } +.file-symbol { background:transparent; color:var(--muted); } +.file-symbol.folder { background:transparent; color:var(--muted); } +.page-footer { flex-wrap:wrap; gap:8px; font-size:12px; margin-top:28px; } +.skip-link { position:fixed; top:12px; left:12px; z-index:100; background:var(--ink); color:var(--paper); border-radius:8px; padding:10px 16px; transform:translateY(-180%); } +.skip-link:focus { transform:none; } +.page-loading { display:none; } +.is-loading .page-loading { display:block; } +.is-loading main > .content:not(.page-loading) { display:none; } +.loading-label { color:var(--muted); font-size:13px; margin:0 0 24px; } +.skeleton { height:12px; border-radius:6px; background:var(--line); animation:skeleton-pulse 1.5s ease-in-out infinite; } +.skeleton-title { width:180px; height:30px; margin-bottom:16px; } +.skeleton-description { width:min(360px,80%); margin-bottom:32px; } +.skeleton-grid { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:20px; } +.skeleton-card { padding:24px; border:1px solid var(--line); border-radius:12px; background:var(--paper); } +.skeleton-card .skeleton { margin-bottom:20px; } +.skeleton-card .skeleton:last-child { width:60%; margin:32px 0 0; } +@keyframes skeleton-pulse { 50% { opacity:.45; } } +@media(max-width:1100px) { .management-actions { justify-content:flex-start; } } +@media(max-width:650px) { + .page-heading { flex-wrap:wrap; gap:16px; } + .page-heading h1,.archive-heading h1 { font-size:26px; } + .page-heading p { font-size:13px; } + .stat,.sandbox-summary > div { padding:20px; } + .stat strong { font-size:26px; } + .panel-heading,.revision-dialog-heading,.sandbox-body,.sandbox-overview,.management-row,.sandbox-card,.sandbox-template,.sandbox-create-form { padding:20px; } + .management-form { padding:0 20px 20px; } + .sandbox-filters { flex-direction:column; } + .sandbox-filters input,.sandbox-filters select { width:100%; } + .sandbox-actions { gap:8px; } + .sandbox-actions button,.sandbox-desktop { min-height:40px; } + .sandbox-section-meta { font-size:11px; } + .archive-state { padding:12px 8px; } + .archive-states { margin:0 16px 20px; } + .archive-branch-header,.archive-files .branch-file-preview { padding:20px; } + .skeleton-grid { grid-template-columns:1fr; } + .detail-field { grid-template-columns:1fr; gap:4px; } +} +@media(prefers-reduced-motion:reduce) { + *,*::before,*::after { transition:none!important; animation:none!important; scroll-behavior:auto!important; } +} +.sandbox-badge[data-state="ready"] { color:var(--accent); background:var(--green-bg); } +.sandbox-badge[data-state="starting"],.sandbox-badge[data-state="creating"] { color:var(--accent); background:var(--green-bg); } +.sandbox-badge[data-state="error"] { color:var(--danger); background:var(--danger-bg); } +.sandbox-dot[data-state="ready"],.sandbox-dot[data-state="creating"],.sandbox-dot[data-state="starting"],.sandbox-dot[data-state="stopping"] { background:var(--status); } +.sandbox-card-message { min-height:0; } +.sandbox-card .sandbox-controls { width:100%; } +.sandbox-service-actions { padding-top:0; } +.error-banner { color:var(--danger); background:var(--danger-bg); border-color:var(--danger-line); } +.archive-branch.active { color:var(--ink); background:var(--paper); border-color:var(--line); } +.branch-button.selected { color:var(--ink); background:var(--paper); border-color:var(--accent); } +.archive-branch-name > span:last-child { min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } +.archive-branch-header > div { min-width:0; } +.archive-branch-header h2 { overflow:hidden; white-space:nowrap; text-overflow:ellipsis; } +.mount-state.ready { color:var(--accent); background:var(--green-bg); } +.mount-state.recovery_required { color:var(--danger); background:var(--danger-bg); } +.sandbox-stop:hover { color:var(--danger); border-color:var(--danger-line); background:var(--danger-bg); } diff --git a/crates/agentdrive-mcp/dashboard/ui.js b/crates/agentdrive-mcp/dashboard/ui.js new file mode 100644 index 0000000..f1ff3ec --- /dev/null +++ b/crates/agentdrive-mcp/dashboard/ui.js @@ -0,0 +1,36 @@ +const paths = { + archive:['M4 4h16v4H4z','M5 8v12h14V8','M9 12h6'], + branch:['M6 3v12','M18 6v3a6 6 0 0 1-6 6H6','M6 15a3 3 0 1 0 0 6 3 3 0 0 0 0-6','M18 3a3 3 0 1 0 0 6 3 3 0 0 0 0-6'], + history:['M3 11a9 9 0 1 1 2.6 7.4','M3 4v7h7','M12 7v5l3 2'], + drive:['M5 4h14l3 12v4H2v-4z','M2 16h20','M6 18h.01M10 18h.01'], + box:['M12 3 3 8v9l9 5 9-5V8z','M3 8l9 5 9-5','M12 13v9','M7.5 5.5l9 5'], + settings:['M9 3h6l1 3 3 1 2 5-2 5-3 1-1 3H9l-1-3-3-1-2-5 2-5 3-1z','M15 12a3 3 0 1 0-6 0 3 3 0 0 0 6 0'], + refresh:['M20 7a9 9 0 0 0-15-2L3 7','M3 3v4h4','M4 17a9 9 0 0 0 15 2l2-2','M17 17h4v4'], + chevronLeft:['m14 6-6 6 6 6'], + chevronRight:['m10 6 6 6-6 6'], + close:['m6 6 12 12','M18 6 6 18'], + trash:['M3 6h18','M9 6V3h6v3','M5 6l1 15h12l1-15','M10 10v7M14 10v7'], + folder:['M3 5h6l2 2h10v13H3z'], + file:['M5 3h9l5 5v13H5z','M14 3v6h5','M9 13h6M9 17h6'], + external:['M14 3h7v7','m21 3-9 9','M10 3H3v18h18v-7'], +}; + +export function icon(name) { + const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + for (const [key,value] of Object.entries({viewBox:'0 0 24 24',fill:'none',stroke:'currentColor','stroke-width':'1.75','stroke-linecap':'round','stroke-linejoin':'round','aria-hidden':'true',focusable:'false',class:'ui-icon'})) svg.setAttribute(key,value); + for (const d of paths[name] || paths.file) { + const path = document.createElementNS(svg.namespaceURI, 'path'); path.setAttribute('d',d); svg.append(path); + } + return svg; +} + +export function decorateIcon(node, className, text) { + const names = {'branch-symbol':'branch','archive-branch-symbol':'branch','sandbox-template-icon':'box','revision-dialog-close':'close','workspace-delete-button':'trash'}; + const name = className?.split(' ').map(value => names[value]).find(Boolean); + if (name) node.replaceChildren(icon(name)); + else if (className?.split(' ').includes('file-symbol')) node.replaceChildren(icon(text === '▰' || text === '▸' ? 'folder' : 'file')); +} + +export function initializeIcons() { + for (const node of document.querySelectorAll('[data-icon]')) node.replaceChildren(icon(node.dataset.icon)); +} diff --git a/crates/agentdrive-mcp/src/web.rs b/crates/agentdrive-mcp/src/web.rs index 91bb958..ea4ac97 100644 --- a/crates/agentdrive-mcp/src/web.rs +++ b/crates/agentdrive-mcp/src/web.rs @@ -159,6 +159,8 @@ pub(crate) fn assets() -> Router { .route("/workspace-actions.js", get(|| async { ([(header::CONTENT_TYPE, "text/javascript; charset=utf-8")], include_str!("../dashboard/workspace-actions.js")) })) .route("/navigation.js", get(|| async { ([(header::CONTENT_TYPE, "text/javascript; charset=utf-8")], include_str!("../dashboard/navigation.js")) })) .route("/navigation.css", get(|| async { ([(header::CONTENT_TYPE, "text/css; charset=utf-8")], include_str!("../dashboard/navigation.css")) })) + .route("/ui.js", get(|| async { ([(header::CONTENT_TYPE, "text/javascript; charset=utf-8")], include_str!("../dashboard/ui.js")) })) + .route("/ui.css", get(|| async { ([(header::CONTENT_TYPE, "text/css; charset=utf-8")], include_str!("../dashboard/ui.css")) })) .route("/dashboard.js", get(|| async { ([(header::CONTENT_TYPE, "text/javascript; charset=utf-8")], include_str!("../dashboard/dashboard.js")) })) .route("/history.js", get(|| async { ([(header::CONTENT_TYPE, "text/javascript; charset=utf-8")], include_str!("../dashboard/history.js")) })) .route("/merges.js", get(|| async { ([(header::CONTENT_TYPE, "text/javascript; charset=utf-8")], include_str!("../dashboard/merges.js")) })) diff --git a/tests/dashboard/navigation.test.cjs b/tests/dashboard/navigation.test.cjs index f6660ed..fc00931 100644 --- a/tests/dashboard/navigation.test.cjs +++ b/tests/dashboard/navigation.test.cjs @@ -64,6 +64,11 @@ test('root opens the first workspace archive without sign-in or remembered selec assert.equal(await page.locator('#archive-nav').getAttribute('aria-current'),'page'); assert.equal(await page.locator('[data-workspace=first]').getAttribute('aria-current'),'true'); assert(requests.some(url => url.pathname === '/api/workspaces/first/archive')); + const beforeSkip = page.url(); + await page.locator('.skip-link').focus(); + await page.keyboard.press('Enter'); + assert.equal(await page.locator('#main-content').evaluate(node => node === document.activeElement),true); + assert.equal(page.url(),beforeSkip); }); test('workspace selection resets the view and browser history restores workspace and page', async t => { @@ -211,3 +216,53 @@ test('failed remote discovery keeps local workspaces usable and cancel does not await page.locator('.workspace-delete-dialog').getByRole('button',{name:'取消',exact:true}).click(); assert.equal(data.deletions,undefined); }); + +test('initial loading has a reduced-motion placeholder and background refresh preserves content', async t => { + const {page,data} = await setup(t); + let release; + const gate = new Promise(resolve => { release = resolve; }); + t.after(() => release()); + data.pending = () => gate; + await page.emulateMedia({reducedMotion:'reduce'}); + await page.goto(endpoint+'/archive?workspace=first'); + await page.locator('.page-loading').waitFor({state:'visible'}); + assert.equal(await page.locator('#main-content').getAttribute('aria-busy'),'true'); + assert.equal(await page.locator('.skeleton').first().evaluate(node => getComputedStyle(node).animationName),'none'); + release(); await settled(page); + assert.equal(await page.locator('.page-loading').isVisible(),false); + assert.equal(await page.locator('#main-content').getAttribute('aria-busy'),'false'); + await page.locator('#workspace-search').fill('Alpha'); + const refreshed = page.waitForResponse(response => new URL(response.url()).pathname === '/api/workspaces'); + await page.locator('#refresh').click(); await refreshed; await settled(page); + assert.equal(await page.locator('#workspace-search').inputValue(),'Alpha'); + assert.equal(await page.locator('.page-loading').isVisible(),false); +}); + +test('long workspace names fit all page layouts and compact navigation reveals the current page', async t => { + const name = '工作区名称与跨机器协作资料'.repeat(12); + const {page} = await setup(t,{workspaces:[{id:'first',name}]}); + for (const width of [320,390,768,1280,1920]) { + await page.setViewportSize({width,height:900}); + for (const route of ['/archive','/dashboard','/branches','/mounts','/settings']) { + await page.goto(endpoint+route+'?workspace=first'); await settled(page); + assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth <= innerWidth),true,`${route} at ${width}px`); + const content = await page.locator('.content:visible:not(.page-loading)').first().boundingBox(); + assert(content.width <= 1280,`${route} exceeds the content width limit`); + if (width <= 900 && route !== '/settings') { + const link = await page.locator('#workspace-menu [aria-current="page"]').boundingBox(); + assert(link.x >= 56 && link.x + link.width <= width + 1,`${route} current navigation is outside the viewport`); + } + } + } +}); + +test('loading failure leaves an actionable refresh control and an accessible error', async t => { + const {page} = await setup(t,{listError:true}); + await page.goto(endpoint+'/archive'); + await page.locator('#error').filter({hasText:'Workspaces unavailable'}).waitFor(); + await page.waitForFunction(()=>!document.querySelector('#refresh').disabled); + assert.equal(await page.locator('.page-loading').isVisible(),false); + assert.equal(await page.locator('#main-content').getAttribute('aria-busy'),'false'); + assert(await page.getByRole('alert').filter({hasText:'Workspaces unavailable'}).isVisible()); + assert(await page.getByRole('button',{name:'刷新数据'}).isEnabled()); +}); diff --git a/tests/dashboard/sandbox.test.cjs b/tests/dashboard/sandbox.test.cjs index aff8876..fbe9334 100644 --- a/tests/dashboard/sandbox.test.cjs +++ b/tests/dashboard/sandbox.test.cjs @@ -60,7 +60,7 @@ test('Sandbox creation targets its branch and exposes desktop and stop actions', test('Deletion requires stopped state and exact confirmation, supports cancellation and retry', async t => { const page = await setup(t); await page.getByRole('button', {name:'启动 Sandbox', exact:true}).click(); - assert(await page.getByRole('button', {name:'删除 Sandbox', exact:true}).isDisabled()); + assert.equal(await page.getByRole('button', {name:'删除 Sandbox', exact:true}).count(),0); await page.getByRole('button', {name:'停止 Sandbox'}).click(); await page.getByRole('button', {name:'删除 Sandbox', exact:true}).click(); const modal = page.getByRole('dialog', {name:'删除 Sandbox', exact:true}); @@ -101,6 +101,8 @@ test('Unavailable runtime disables creation',async t=>{ test('Failed normal stop offers explicit force confirmation and preserves restart', async t => { const page = await setup(t); await page.getByRole('button', {name:'启动 Sandbox', exact:true}).click(); + assert.equal(await page.getByRole('button', {name:'强制停止', exact:true}).count(), 0); + assert.equal(await page.getByRole('button', {name:'删除 Sandbox', exact:true}).count(), 0); await page.evaluate(() => { window.failStop = true; }); await page.getByRole('button', {name:'停止 Sandbox', exact:true}).click(); await page.getByRole('button', {name:'强制停止', exact:true}).click(); From 3757db83e27604f4206c2700aa7708f86148a64a Mon Sep 17 00:00:00 2001 From: GatewayJ <835269233@qq.com> Date: Fri, 13 Nov 2026 22:48:48 +0800 Subject: [PATCH 03/12] style: refine status badges and secondary action hierarchy --- crates/agentdrive-mcp/dashboard/archive.css | 2 +- crates/agentdrive-mcp/dashboard/dashboard.css | 34 +++++------ .../agentdrive-mcp/dashboard/management.css | 8 +-- crates/agentdrive-mcp/dashboard/merges.css | 2 +- crates/agentdrive-mcp/dashboard/ui.css | 58 +++++++++++++------ 5 files changed, 59 insertions(+), 45 deletions(-) diff --git a/crates/agentdrive-mcp/dashboard/archive.css b/crates/agentdrive-mcp/dashboard/archive.css index c51d15c..d8f4b71 100644 --- a/crates/agentdrive-mcp/dashboard/archive.css +++ b/crates/agentdrive-mcp/dashboard/archive.css @@ -3,7 +3,7 @@ .archive-heading {margin-bottom:24px; align-items:flex-end; gap:20px;} .archive-heading h1 {font-size:30px; letter-spacing:-.8px;} .archive-heading p {font-size:13px; line-height:1.7; color:var(--muted);} -.archive-scope {border:1px solid var(--line); border-radius:20px; padding:7px 11px; font-size:12px; color:var(--muted); background:var(--surface-muted); white-space:nowrap;} +.archive-scope { padding:7px 11px; font-size:12px; color:var(--muted); white-space:nowrap;} .archive-workspace {display:grid; grid-template-columns:218px minmax(0,1fr); border:1px solid var(--line); border-radius:12px; background:var(--paper); min-height:650px; overflow:hidden; box-shadow:none;} .archive-branches {background:var(--surface-muted); border-right:1px solid var(--line); padding:20px 12px; min-width:0;} .archive-list-heading {display:flex; align-items:center; justify-content:space-between; padding:0 8px 16px;} diff --git a/crates/agentdrive-mcp/dashboard/dashboard.css b/crates/agentdrive-mcp/dashboard/dashboard.css index cdc4a24..3164046 100644 --- a/crates/agentdrive-mcp/dashboard/dashboard.css +++ b/crates/agentdrive-mcp/dashboard/dashboard.css @@ -4,7 +4,11 @@ --bg:#F8FAFC; --paper:#FFFFFF; --ink:#0F172A; - --muted:#64748B; + --muted:#475569; + --secondary-ink:#334155; + --panel-padding:24px; + --radius-control:8px; + --radius-panel:calc(var(--radius-control) + var(--panel-padding)); --line:#E2E8F0; --surface-muted:#F1F5F9; --accent:#047857; @@ -131,9 +135,6 @@ main { } .read-only { color:var(--muted); - background:var(--surface-muted); - border:1px solid var(--line); - border-radius:4px; padding:4px 7px; } .refresh { @@ -262,9 +263,7 @@ h1 { } .count-badge { display:inline-block; - background:var(--surface-muted); color:var(--muted); - border-radius:5px; min-width:24px; padding:3px 6px; text-align:center; @@ -299,17 +298,17 @@ h1 { .branch-copy { min-width:0; flex:1; } .branch-name { display:block; font-size:12px; overflow-wrap:anywhere; line-height:1.6; font-weight:600; } .branch-badges { display:flex; gap:5px; flex-wrap:wrap; justify-content:flex-end; max-width:80px; } -.default-mark,.mount-mark,.branch-unavailable { display:inline-block; font-size:12px; border-radius:4px; padding:3px 5px; white-space:nowrap; } -.default-mark { background:var(--surface-muted); color:var(--muted); } -.mount-mark { background:var(--line); color:var(--accent); } -.branch-unavailable { background:var(--line); color:var(--muted); } +.default-mark,.mount-mark,.branch-unavailable { display:inline-block; font-size:12px; padding:3px 5px; white-space:nowrap; } +.default-mark { color:var(--muted); } +.mount-mark { color:var(--accent); } +.branch-unavailable { color:var(--muted); } .tree-footer { padding:14px 20px; border-top:1px solid var(--line); font-size:12px; line-height:1.8; color:var(--muted); background:var(--surface-muted); } .mounts-panel { grid-column:1/-1; } .mount-table-wrap { overflow:auto; } .mount-path { display:block; font:12px/1.8 ui-monospace,monospace; color:var(--muted); overflow-wrap:anywhere; } -.mount-state { display:inline-block; border-radius:4px; padding:4px 6px; font-size:12px; color:var(--warning); background:var(--surface-muted); white-space:nowrap; } -.mount-state.ready { color:var(--accent); background:var(--line); } -.mount-state.recovery_required { color:var(--danger); background:var(--surface-muted); } +.mount-state { display:inline-block; padding:4px 6px; font-size:12px; color:var(--warning); white-space:nowrap; } +.mount-state.ready { color:var(--accent); } +.mount-state.recovery_required { color:var(--danger); } .panel-empty { padding:12px 22px 26px; margin:0; font-size:12px; color:var(--muted); } .history-heading { padding:23px 25px 20px; @@ -344,21 +343,14 @@ h1 { .pill { white-space:nowrap; display:inline-block; - background:var(--green-bg); - border:1px solid var(--line); color:var(--accent); - border-radius:5px; font-size:12px; padding:5px 8px; } .pill.paused { - background:var(--surface-muted); - border-color:var(--warning-line); color:var(--warning); } .pill.autosaved { - background:var(--surface-muted); - border-color:var(--line); color:var(--muted); } .history-toolbar { @@ -810,6 +802,6 @@ body:has(.revision-dialog[open]) { overflow:hidden; } .relationship-table th,.relationship-table td { padding:12px 16px; border-bottom:1px solid var(--line); vertical-align:top; } .relationship-table th { color:var(--muted); font-size:12px; font-weight:500; } .relationship-table td { max-width:280px; font-size:13px; overflow-wrap:anywhere; } -.relationship-table small,.relationship-table .relationship-id,.relationship-table .mount-state { display:block; margin-top:5px; } +.relationship-table small,.relationship-table .relationship-id { display:block; margin-top:5px; } .relationship-table small,.relationship-id { color:var(--muted); font-size:12px; } .relationship-more { margin:12px 20px 20px; } diff --git a/crates/agentdrive-mcp/dashboard/management.css b/crates/agentdrive-mcp/dashboard/management.css index d1e1d5e..043d9b0 100644 --- a/crates/agentdrive-mcp/dashboard/management.css +++ b/crates/agentdrive-mcp/dashboard/management.css @@ -70,10 +70,10 @@ .sandbox-identity { margin:8px 0 0; color:var(--muted); font-size:13px; overflow-wrap:anywhere; } .sandbox-body { padding:24px; } .sandbox-overview { padding:20px; background:var(--surface-muted); border:1px solid var(--line); border-radius:12px; } -.sandbox-badge { display:inline-flex; align-items:center; font-size:12px; font-weight:600; padding:4px 9px; border-radius:6px; background:var(--line); color:var(--muted); } -.sandbox-badge[data-state="ready"] { background:var(--line); color:var(--accent-hover); } -.sandbox-badge[data-state="starting"],.sandbox-badge[data-state="creating"] { background:var(--surface-muted); color:var(--muted); } -.sandbox-badge[data-state="error"] { background:var(--surface-muted); color:var(--danger); } +.sandbox-badge { display:inline-flex; align-items:center; font-size:12px; font-weight:600; padding:4px 9px; color:var(--muted); } +.sandbox-badge[data-state="ready"] { color:var(--accent-hover); } +.sandbox-badge[data-state="starting"],.sandbox-badge[data-state="creating"] { color:var(--muted); } +.sandbox-badge[data-state="error"] { color:var(--danger); } .sandbox-status { margin:12px 0 18px; font-size:13px; line-height:1.8; color:var(--muted); overflow-wrap:anywhere; } .sandbox-actions,.sandbox-config-actions { display:flex; flex-wrap:wrap; align-items:center; gap:10px; } .sandbox-actions button,.sandbox-desktop { font-size:13px; padding:10px 16px; border-radius:7px; } diff --git a/crates/agentdrive-mcp/dashboard/merges.css b/crates/agentdrive-mcp/dashboard/merges.css index e745e7f..eed8f8d 100644 --- a/crates/agentdrive-mcp/dashboard/merges.css +++ b/crates/agentdrive-mcp/dashboard/merges.css @@ -32,7 +32,7 @@ .merge-inline-revisions { display:flex; gap:8px; margin-top:7px; color:var(--muted); } .merge-revision { font-size:12px; color:var(--muted); overflow-wrap:anywhere; } .merge-state { white-space:normal; line-height:1.5; } -.merge-state-error { color:var(--danger); background:var(--surface-muted); } +.merge-state-error { color:var(--danger); } .merge-confirmed { color:var(--green); } .merge-error-code,.merge-event-error { color:var(--danger); } .merge-empty { padding:30px; text-align:center; color:var(--muted); } diff --git a/crates/agentdrive-mcp/dashboard/ui.css b/crates/agentdrive-mcp/dashboard/ui.css index 563debd..a4e5568 100644 --- a/crates/agentdrive-mcp/dashboard/ui.css +++ b/crates/agentdrive-mcp/dashboard/ui.css @@ -7,10 +7,11 @@ :where(input,select,textarea):focus { outline:0; border-color:var(--accent); box-shadow:0 0 0 3px rgba(16,185,129,.12); } :where(button,a)[aria-disabled="true"] { cursor:not-allowed; } :where(button,a).primary { min-height:40px; align-items:center; gap:8px; font-weight:600; } -:where(button,a).secondary { min-height:36px; display:inline-flex; align-items:center; justify-content:center; gap:8px; border-radius:8px; background:var(--paper); color:var(--ink); border:1px solid var(--line); } -:where(button,a).secondary:hover { background:var(--bg); border-color:#CBD5E1; } -.primary:disabled,.secondary:disabled { opacity:.45; } -:where(h1,h2,h3) { letter-spacing:-.02em; line-height:1.3; } +:where(button,a).secondary { min-height:36px; display:inline-flex; align-items:center; justify-content:center; gap:8px; border-radius:var(--radius-control); background:transparent; color:var(--secondary-ink); border:1px solid transparent; } +:where(button,a).secondary:not(:disabled):is(:hover,:focus-visible) { background:var(--surface-muted); color:var(--ink); border-color:var(--line); } +.primary:disabled { opacity:.6; } +.secondary:disabled { opacity:1; color:#64748B; } +:where(h1,h2,h3) { color:var(--ink); letter-spacing:-.02em; line-height:1.3; } :where(p,small) { line-height:1.6; } .page-heading { gap:24px; } .page-heading > div { min-width:0; } @@ -23,11 +24,11 @@ .refresh,.revision-dialog-close { display:inline-grid; place-items:center; width:36px; height:36px; } .stat-label,.stat small,.stat strong,.sandbox-summary strong { font-variant-numeric:tabular-nums; } .stat strong { font-weight:650; margin:16px 0 8px; } -.panel-heading { padding:24px; } +.panel-heading { padding:var(--panel-padding); } .panel-heading h2 { font-size:15px; } .panel-heading p { font-size:12px; } .graph-legend { padding:0 24px 20px; } -.panel,.archive-workspace { border-radius:12px; } +.panel,.archive-workspace,.stat,.sandbox-summary,.empty-state,.skeleton-card,.revision-dialog { border-radius:var(--radius-panel); } .panel-empty,.no-revisions { padding:32px 24px; line-height:1.7; } .empty-state { padding:56px 24px; } .empty-state > span { display:inline-grid; place-items:center; width:48px; height:48px; background:var(--surface-muted); border:1px solid var(--line); border-radius:12px; color:var(--muted); } @@ -37,8 +38,8 @@ .revision-dialog::backdrop { background:rgba(15,23,42,.28); } .revision-dialog-heading { padding:24px; } .revision-dialog-heading > div { min-width:0; } -.revision-dialog-close { border:1px solid var(--line); background:var(--paper); } -.revision-dialog-close:hover { background:var(--surface-muted); } +.revision-dialog-close,.refresh { border:1px solid transparent; background:transparent; color:var(--secondary-ink); border-radius:var(--radius-control); } +.revision-dialog-close:hover,.refresh:hover { background:var(--surface-muted); border-color:var(--line); } .truncate { overflow:hidden; white-space:nowrap; text-overflow:ellipsis; min-width:0; } .management-row:hover { background:var(--bg); } .management-row-copy { flex:1; } @@ -46,11 +47,11 @@ .management-row { flex-wrap:wrap; padding:24px; } .management-row h3,.sandbox-card h3 { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } .sandbox-card h3 { min-width:0; } -.sandbox-card { padding:24px; } +.sandbox-card { padding:var(--panel-padding); } .sandbox-card:hover { border-color:#CBD5E1; } .sandbox-card-message { margin-bottom:20px; } .sandbox-card .sandbox-actions { align-items:center; } -.sandbox-overview { background:var(--bg); } +.sandbox-overview { background:var(--bg); border-radius:var(--radius-control); } .sandbox-step.active,.sandbox-step.active .sandbox-step-marker { color:var(--accent); } .sandbox-step.active .sandbox-step-marker { background:var(--green-bg); border-color:var(--accent-line); } .sandbox-template-icon { display:grid; place-items:center; width:48px; height:48px; padding:12px; background:var(--surface-muted); color:var(--muted); border:1px solid var(--line); } @@ -86,12 +87,13 @@ .skeleton-title { width:180px; height:30px; margin-bottom:16px; } .skeleton-description { width:min(360px,80%); margin-bottom:32px; } .skeleton-grid { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:20px; } -.skeleton-card { padding:24px; border:1px solid var(--line); border-radius:12px; background:var(--paper); } +.skeleton-card { padding:var(--panel-padding); border:1px solid var(--line); border-radius:var(--radius-panel); background:var(--paper); } .skeleton-card .skeleton { margin-bottom:20px; } .skeleton-card .skeleton:last-child { width:60%; margin:32px 0 0; } @keyframes skeleton-pulse { 50% { opacity:.45; } } @media(max-width:1100px) { .management-actions { justify-content:flex-start; } } @media(max-width:650px) { + :root { --panel-padding:20px; } .page-heading { flex-wrap:wrap; gap:16px; } .page-heading h1,.archive-heading h1 { font-size:26px; } .page-heading p { font-size:13px; } @@ -113,10 +115,9 @@ @media(prefers-reduced-motion:reduce) { *,*::before,*::after { transition:none!important; animation:none!important; scroll-behavior:auto!important; } } -.sandbox-badge[data-state="ready"] { color:var(--accent); background:var(--green-bg); } -.sandbox-badge[data-state="starting"],.sandbox-badge[data-state="creating"] { color:var(--accent); background:var(--green-bg); } -.sandbox-badge[data-state="error"] { color:var(--danger); background:var(--danger-bg); } -.sandbox-dot[data-state="ready"],.sandbox-dot[data-state="creating"],.sandbox-dot[data-state="starting"],.sandbox-dot[data-state="stopping"] { background:var(--status); } +.sandbox-badge[data-state="ready"] { color:var(--accent); } +.sandbox-badge[data-state="error"] { color:var(--danger); } +.sandbox-dot[data-state="ready"] { background:var(--status); } .sandbox-card-message { min-height:0; } .sandbox-card .sandbox-controls { width:100%; } .sandbox-service-actions { padding-top:0; } @@ -126,6 +127,27 @@ .archive-branch-name > span:last-child { min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } .archive-branch-header > div { min-width:0; } .archive-branch-header h2 { overflow:hidden; white-space:nowrap; text-overflow:ellipsis; } -.mount-state.ready { color:var(--accent); background:var(--green-bg); } -.mount-state.recovery_required { color:var(--danger); background:var(--danger-bg); } -.sandbox-stop:hover { color:var(--danger); border-color:var(--danger-line); background:var(--danger-bg); } +.mount-state.ready { color:var(--accent); } +.mount-state.recovery_required { color:var(--danger); } +.secondary.sandbox-stop:not(:disabled):is(:hover,:focus-visible) { color:var(--danger); border-color:var(--danger-line); background:var(--danger-bg); } + +/* Status colors also define the translucent badge fill and border. */ +.read-only,.count-badge,.default-mark,.mount-mark,.branch-unavailable,.mount-state,.pill,.sandbox-badge,.archive-scope { + display:inline-flex; align-items:center; justify-content:center; gap:6px; width:fit-content; max-width:100%; min-height:24px; + padding:2px 9px; border-radius:999px; font-size:12px; font-weight:500; line-height:1.5; vertical-align:middle; + background:color-mix(in srgb,currentColor 10%,transparent); + border:1px solid color-mix(in srgb,currentColor 20%,transparent); +} +.count-badge { min-width:24px; padding-inline:7px; } +.sandbox-badge:is([data-state="starting"],[data-state="creating"],[data-state="stopping"]) { color:var(--warning); } +.sandbox-dot:is([data-state="creating"],[data-state="starting"],[data-state="stopping"]) { background:var(--warning); } +.relationship-table .mount-state { margin-top:5px; } +.stat-label,.sandbox-summary span,.management-field,.settings-content label,.sandbox-template-fields dt,.selected-branch-label,.detail-grid dt,.merge-table th,.archive-state-label { color:var(--secondary-ink); } +.detail-grid dd,.branch-name,.archive-branch-name,.sandbox-section summary { color:var(--ink); } +.workspace-link,.workspace-menu .nav-link,.global-settings { color:var(--secondary-ink); } +.management-link { display:inline-flex; align-items:center; justify-content:center; min-height:36px; gap:8px; padding:8px 10px; border-radius:var(--radius-control); color:var(--secondary-ink); } +.management-link:hover { color:var(--ink); background:var(--surface-muted); text-decoration:none; } +.stat-label,.sandbox-card-heading { align-items:center; } +.workspace-link { border-radius:calc(7px + 10px); } +.sandbox-template { padding:var(--panel-padding); } +.settings-panel { padding:var(--panel-padding); } From 8986a4c193386ee9e5786b7e3e992651eeae979f Mon Sep 17 00:00:00 2001 From: GatewayJ <835269233@qq.com> Date: Fri, 13 Nov 2026 22:53:43 +0800 Subject: [PATCH 04/12] fix: organize sandbox actions and responsive form layouts --- .../agentdrive-mcp/dashboard/management.css | 13 +++++++++++- crates/agentdrive-mcp/dashboard/management.js | 1 + crates/agentdrive-mcp/dashboard/sandbox.js | 21 +++++++++++++------ crates/agentdrive-mcp/dashboard/sandboxes.css | 20 +++++++++++------- crates/agentdrive-mcp/dashboard/ui.css | 5 +---- 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/crates/agentdrive-mcp/dashboard/management.css b/crates/agentdrive-mcp/dashboard/management.css index 043d9b0..b08d723 100644 --- a/crates/agentdrive-mcp/dashboard/management.css +++ b/crates/agentdrive-mcp/dashboard/management.css @@ -1,10 +1,12 @@ -.management-content { display:grid; gap:24px; } +.management-content { display:grid; gap:24px; container:management / inline-size; } .management-heading .location-label { white-space:normal; overflow-wrap:anywhere; } .management-create .panel-heading,.management-list .panel-heading { display:flex; align-items:center; gap:12px; } .management-form { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:18px; padding:0 24px 24px; align-items:start; } +#branch-create-form { grid-template-columns:repeat(2,minmax(0,1fr)); } .management-field { display:flex; flex-direction:column; gap:8px; min-width:0; font-size:13px; color:var(--muted); } .management-field input,.management-field select { width:100%; min-width:0; min-height:44px; border:1px solid var(--line); background:var(--paper); border-radius:8px; padding:12px; font:inherit; color:var(--ink); } .revision-source-search { min-width:0; display:grid; gap:8px; } +.revision-source-search .secondary { justify-self:start; } .management-hint { grid-column:1 / -1; margin:0; color:var(--muted); font-size:12px; line-height:1.7; } .management-form .primary { justify-self:start; padding:12px 20px; } .management-row { display:flex; align-items:center; justify-content:space-between; gap:20px; padding:20px 24px; border-top:1px solid var(--line); } @@ -76,6 +78,9 @@ .sandbox-badge[data-state="error"] { color:var(--danger); } .sandbox-status { margin:12px 0 18px; font-size:13px; line-height:1.8; color:var(--muted); overflow-wrap:anywhere; } .sandbox-actions,.sandbox-config-actions { display:flex; flex-wrap:wrap; align-items:center; gap:10px; } +.sandbox-instance-actions { display:grid; gap:8px; width:100%; min-width:0; } +.sandbox-launch-actions,.sandbox-manage-actions { display:grid; grid-template-columns:repeat(auto-fit,minmax(min(100%,120px),1fr)); gap:8px; min-width:0; } +.sandbox-instance-actions :is(button,a) { min-width:0; min-height:40px; padding:8px; font-size:13px; border-radius:var(--radius-control); white-space:normal; text-align:center; } .sandbox-actions button,.sandbox-desktop { font-size:13px; padding:10px 16px; border-radius:7px; } .sandbox-desktop { display:inline-flex; text-decoration:none; align-items:center; justify-content:center; } .sandbox-stop { color:var(--danger); } @@ -85,6 +90,12 @@ .sandbox-delete-dialog input { width:100%; box-sizing:border-box; padding:10px; border:1px solid var(--line); border-radius:6px; } .sandbox-delete-dialog .sandbox-actions { justify-content:flex-end; margin-top:20px; } .sandbox-delete-confirm { background:var(--danger); } +@container management (max-width:760px) { + #branch-create-form,.management-form { grid-template-columns:minmax(0,1fr); } + .management-row { flex-direction:column; align-items:stretch; } + .management-row .management-actions { display:grid; grid-template-columns:repeat(auto-fit,minmax(min(100%,110px),1fr)); width:100%; gap:8px; } + .management-row .management-actions > *,.management-row .sandbox-trigger { width:100%; } +} .sandbox-section { border-bottom:1px solid var(--line); } .sandbox-section > summary { cursor:pointer; padding:20px 0; font-size:14px; font-weight:600; } .sandbox-section-meta { float:right; margin-right:4px; font-size:12px; font-weight:400; color:var(--muted); } diff --git a/crates/agentdrive-mcp/dashboard/management.js b/crates/agentdrive-mcp/dashboard/management.js index 9408932..a0ee38c 100644 --- a/crates/agentdrive-mcp/dashboard/management.js +++ b/crates/agentdrive-mcp/dashboard/management.js @@ -30,6 +30,7 @@ export function createManagementPage({api, element, container, kind, changed, on field('access', '访问方式'); for (const [value, label] of [['read_write','读写'],['read_only','只读']]) inputs.access.append(new Option(label, value)); } else { field('name', '分支名称', 'text').placeholder = 'feat/new-task'; inputs.name.maxLength = 256; } + if (!mounts) form.insertBefore(inputs.name.closest('label'), revisionInput.closest('label')); const hint = element('p', 'management-hint'); const submit = element('button', 'primary', mounts ? '创建挂载点' : '创建分支'); submit.type = 'submit'; form.append(hint, submit); formPanel.append(formHeader, form); diff --git a/crates/agentdrive-mcp/dashboard/sandbox.js b/crates/agentdrive-mcp/dashboard/sandbox.js index 7f076de..b4520ac 100644 --- a/crates/agentdrive-mcp/dashboard/sandbox.js +++ b/crates/agentdrive-mcp/dashboard/sandbox.js @@ -29,21 +29,28 @@ export function createSandboxControl({api, element, workspace, branch, branchNam const badge = element('span', 'sandbox-badge', '读取状态中'); const status = element('p', 'sandbox-status'); status.setAttribute('role', 'status'); status.setAttribute('aria-live', 'polite'); overviewText.append(badge, status); - const actions = element('div', 'sandbox-actions'); + const actions = element('div', 'sandbox-instance-actions'); + const launchActions = element('div', 'sandbox-launch-actions'); + const manageActions = element('div', 'sandbox-manage-actions'); const create = element('button', 'primary', '启动 Sandbox'); create.type = 'button'; create.disabled = true; const stop = element('button', 'secondary sandbox-stop', '停止 Sandbox'); stop.type = 'button'; stop.hidden = true; const force = element('button', 'secondary sandbox-stop', '强制停止'); force.type = 'button'; force.hidden = true; const desktop = element('a', 'primary sandbox-desktop', '打开桌面'); desktop.hidden = true; desktop.target = '_blank'; desktop.rel = 'noopener'; const remove = element('button', 'secondary sandbox-stop', '删除 Sandbox'); remove.type = 'button'; remove.hidden = true; - const serviceActions = element("div", "sandbox-service-actions"); - actions.append(create, desktop, stop, force, remove, serviceActions); overview.append(overviewText, actions); - const quickActions = inline ? element('div', 'sandbox-actions') : null; + const serviceActions = element('div', 'sandbox-service-actions'); + launchActions.append(create, desktop, serviceActions); manageActions.append(stop, force, remove); + actions.append(launchActions, manageActions); overview.append(overviewText, actions); + const quickActions = inline ? element('div', 'sandbox-instance-actions') : null; + const quickLaunch = element('div', 'sandbox-launch-actions'); + const quickManage = element('div', 'sandbox-manage-actions'); const quickCreate = create.cloneNode(true), quickStop = stop.cloneNode(true), quickForce = force.cloneNode(true), quickDesktop = desktop.cloneNode(true), quickRemove = remove.cloneNode(true); if (quickActions) { trigger.querySelector('span:last-child').textContent = '详情与 MCP'; trigger.setAttribute('aria-label', '详情与 MCP'); - quickActions.append(quickCreate, quickDesktop, quickStop, quickForce, quickRemove, trigger); root.append(quickActions); + quickLaunch.append(quickCreate, quickDesktop); + quickManage.append(trigger, quickStop, quickForce, quickRemove); + quickActions.append(quickLaunch, quickManage); root.append(quickActions); } const templateChoice = element('label', 'management-field sandbox-template-choice'); const templateSelect = element('select'); templateSelect.setAttribute('aria-label', 'Sandbox 模板'); @@ -152,7 +159,7 @@ export function createSandboxControl({api, element, workspace, branch, branchNam serviceActions.hidden = !serviceActions.childElementCount; if (quickActions) { quickActions.querySelector('.sandbox-service-actions')?.remove(); - quickActions.prepend(serviceActions.cloneNode(true)); + quickLaunch.append(serviceActions.cloneNode(true)); } const active = ['creating', 'starting', 'stopping'].includes(state); create.disabled = !enabled || !canStart || busy || active; @@ -173,6 +180,8 @@ export function createSandboxControl({api, element, workspace, branch, branchNam desktop.href = url.href; } catch { desktop.hidden = true; invalidDesktop = true; } } + launchActions.hidden = quickLaunch.hidden = create.hidden && desktop.hidden && serviceActions.hidden; + manageActions.hidden = stop.hidden && force.hidden && remove.hidden; const labels = {creating:'正在准备',starting:'启动中',ready:'运行中',stopping:'停止中',stopped:'已停止',error:'启动失败'}; const label = !enabled ? '未配置' : labels[state] || '未启动'; badge.textContent = label; badge.dataset.state = state || 'idle'; dot.dataset.state = state || 'idle'; diff --git a/crates/agentdrive-mcp/dashboard/sandboxes.css b/crates/agentdrive-mcp/dashboard/sandboxes.css index d0ab675..8671e9a 100644 --- a/crates/agentdrive-mcp/dashboard/sandboxes.css +++ b/crates/agentdrive-mcp/dashboard/sandboxes.css @@ -1,4 +1,4 @@ -.sandboxes-page { max-width:1280px; margin:auto; } +.sandboxes-page { max-width:1280px; margin:auto; container:sandboxes / inline-size; } .sandboxes-page .page-heading { align-items:center; gap:24px; } .sandboxes-page .page-heading > button { flex-shrink:0; width:auto; padding:11px 18px; border-radius:7px; } .sandbox-summary { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); border:1px solid var(--line); border-radius:10px; background:var(--paper); margin-bottom:28px; } @@ -9,18 +9,17 @@ .sandbox-page-tabs { display:flex; gap:28px; border-bottom:1px solid var(--line); margin-bottom:24px; } .sandbox-page-tabs button { background:none; border:0; border-bottom:2px solid transparent; padding:0 2px 15px; color:var(--muted); font-size:14px; } .sandbox-page-tabs button[aria-selected=true] { border-color:var(--accent); color:var(--accent); font-weight:600; } -.sandbox-filters { display:flex; flex-wrap:wrap; gap:12px; margin-bottom:20px; } +.sandbox-filters { display:grid; grid-template-columns:minmax(0,1fr) minmax(140px,220px) minmax(120px,160px); gap:12px; margin-bottom:20px; } .sandbox-filters input,.sandbox-filters select { min-width:0; border:1px solid var(--line); background:var(--paper); border-radius:7px; padding:11px 13px; color:var(--ink); font:inherit; font-size:13px; } -.sandbox-filters input { flex:1; min-width:180px; } +.sandbox-filters input { width:100%; } .sandbox-catalog { display:grid; grid-template-columns:repeat(auto-fill,minmax(min(100%,370px),1fr)); gap:18px; } -.sandbox-card { padding:22px; min-width:0; } +.sandbox-card { display:flex; flex-direction:column; padding:22px; min-width:0; } +.sandbox-card .sandbox-controls { margin-top:auto; } .sandbox-card-heading { display:flex; align-items:flex-start; justify-content:space-between; gap:12px; } .sandbox-card h3 { font-size:15px; line-height:1.5; margin:0; overflow-wrap:anywhere; } .sandbox-card .sandbox-badge { flex-shrink:0; } .sandbox-card-identity { color:var(--muted); font-size:12px; margin:10px 0 18px; overflow-wrap:anywhere; } .sandbox-card-message { color:var(--muted); font-size:12px; line-height:1.8; min-height:44px; overflow-wrap:anywhere; } -.sandbox-card .sandbox-actions { gap:8px; } -.sandbox-card .sandbox-actions button,.sandbox-card .sandbox-desktop { padding:8px 11px; font-size:12px; } .sandbox-card-footer { display:flex; align-items:center; justify-content:space-between; padding-top:15px; margin-top:18px; border-top:1px solid var(--line); } .sandbox-card-footer code { font-size:12px; color:var(--muted); } .sandbox-empty { padding:48px 24px; text-align:center; font-size:13px; line-height:1.8; color:var(--muted); } @@ -54,5 +53,12 @@ .sandbox-template-heading > div:nth-child(2) { flex:1; min-width:0; } .sandbox-template-heading h2 { margin:4px 0; } .sandbox-capabilities { display:flex; flex-wrap:wrap; gap:8px; margin:18px 0; } -.sandbox-service-actions { display:flex; flex-wrap:wrap; gap:8px; width:100%; padding-top:10px; } +.sandbox-service-actions { display:contents; } .sandbox-service-link { text-decoration:none; } +@container sandboxes (max-width:640px) { + .sandbox-filters { grid-template-columns:repeat(2,minmax(0,1fr)); } + .sandbox-filters input { grid-column:1 / -1; } +} +@container sandboxes (max-width:280px) { + .sandbox-filters { grid-template-columns:minmax(0,1fr); } +} diff --git a/crates/agentdrive-mcp/dashboard/ui.css b/crates/agentdrive-mcp/dashboard/ui.css index a4e5568..5fc47f1 100644 --- a/crates/agentdrive-mcp/dashboard/ui.css +++ b/crates/agentdrive-mcp/dashboard/ui.css @@ -50,7 +50,6 @@ .sandbox-card { padding:var(--panel-padding); } .sandbox-card:hover { border-color:#CBD5E1; } .sandbox-card-message { margin-bottom:20px; } -.sandbox-card .sandbox-actions { align-items:center; } .sandbox-overview { background:var(--bg); border-radius:var(--radius-control); } .sandbox-step.active,.sandbox-step.active .sandbox-step-marker { color:var(--accent); } .sandbox-step.active .sandbox-step-marker { background:var(--green-bg); border-color:var(--accent-line); } @@ -58,7 +57,7 @@ .sandbox-template-icon .ui-icon { width:24px; height:24px; } .sandbox-template-heading h2 { font-size:18px; } .sandbox-card-footer { gap:12px; } -.sandbox-filters input { min-width:min(100%,180px); } +.sandbox-filters input { min-width:0; } .sandbox-section summary { list-style:none; display:flex; align-items:center; gap:8px; } .sandbox-section summary::-webkit-details-marker { display:none; } .sandbox-section summary:after { content:""; width:6px; height:6px; border-right:1.75px solid currentColor; border-bottom:1.75px solid currentColor; transform:rotate(45deg); margin-left:4px; transition:var(--motion); } @@ -101,7 +100,6 @@ .stat strong { font-size:26px; } .panel-heading,.revision-dialog-heading,.sandbox-body,.sandbox-overview,.management-row,.sandbox-card,.sandbox-template,.sandbox-create-form { padding:20px; } .management-form { padding:0 20px 20px; } - .sandbox-filters { flex-direction:column; } .sandbox-filters input,.sandbox-filters select { width:100%; } .sandbox-actions { gap:8px; } .sandbox-actions button,.sandbox-desktop { min-height:40px; } @@ -120,7 +118,6 @@ .sandbox-dot[data-state="ready"] { background:var(--status); } .sandbox-card-message { min-height:0; } .sandbox-card .sandbox-controls { width:100%; } -.sandbox-service-actions { padding-top:0; } .error-banner { color:var(--danger); background:var(--danger-bg); border-color:var(--danger-line); } .archive-branch.active { color:var(--ink); background:var(--paper); border-color:var(--line); } .branch-button.selected { color:var(--ink); background:var(--paper); border-color:var(--accent); } From 4e7c7f6a6015408798c638461366fdbc2f5fbc69 Mon Sep 17 00:00:00 2001 From: GatewayJ <835269233@qq.com> Date: Fri, 13 Nov 2026 23:07:09 +0800 Subject: [PATCH 05/12] style: display sandbox templates in responsive cards --- crates/agentdrive-mcp/dashboard/sandboxes.css | 30 +++++++++++-------- crates/agentdrive-mcp/dashboard/sandboxes.js | 14 +++++---- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/crates/agentdrive-mcp/dashboard/sandboxes.css b/crates/agentdrive-mcp/dashboard/sandboxes.css index 8671e9a..2a8cc54 100644 --- a/crates/agentdrive-mcp/dashboard/sandboxes.css +++ b/crates/agentdrive-mcp/dashboard/sandboxes.css @@ -12,7 +12,7 @@ .sandbox-filters { display:grid; grid-template-columns:minmax(0,1fr) minmax(140px,220px) minmax(120px,160px); gap:12px; margin-bottom:20px; } .sandbox-filters input,.sandbox-filters select { min-width:0; border:1px solid var(--line); background:var(--paper); border-radius:7px; padding:11px 13px; color:var(--ink); font:inherit; font-size:13px; } .sandbox-filters input { width:100%; } -.sandbox-catalog { display:grid; grid-template-columns:repeat(auto-fill,minmax(min(100%,370px),1fr)); gap:18px; } +.sandbox-catalog,.sandbox-template-list { display:grid; grid-template-columns:repeat(auto-fill,minmax(min(100%,370px),1fr)); gap:18px; } .sandbox-card { display:flex; flex-direction:column; padding:22px; min-width:0; } .sandbox-card .sandbox-controls { margin-top:auto; } .sandbox-card-heading { display:flex; align-items:flex-start; justify-content:space-between; gap:12px; } @@ -23,15 +23,22 @@ .sandbox-card-footer { display:flex; align-items:center; justify-content:space-between; padding-top:15px; margin-top:18px; border-top:1px solid var(--line); } .sandbox-card-footer code { font-size:12px; color:var(--muted); } .sandbox-empty { padding:48px 24px; text-align:center; font-size:13px; line-height:1.8; color:var(--muted); } -.sandbox-template { padding:28px; } -.sandbox-template-heading { display:flex; align-items:center; gap:14px; margin-bottom:26px; flex-wrap:wrap; } -.sandbox-template-heading h2 { font-size:20px; margin:0; } -.sandbox-template-icon { font-size:28px; color:var(--accent); background:var(--line); border-radius:10px; padding:8px 14px; } -.sandbox-template-fields { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:24px 40px; } -.sandbox-template-fields > div { border-top:1px solid var(--line); padding-top:16px; min-width:0; } -.sandbox-template-fields dt { font-size:12px; color:var(--muted); margin-bottom:10px; } -.sandbox-template-fields dd { font-size:13px; margin:0; line-height:1.8; white-space:pre-line; overflow-wrap:anywhere; } -.sandbox-template-note { margin:26px 0 0; padding:16px; border-radius:7px; background:var(--surface-muted); color:var(--muted); font-size:12px; line-height:1.9; } +.sandbox-template { display:flex; flex-direction:column; min-width:0; padding:24px; } +.sandbox-template-heading { display:flex; align-items:center; gap:14px; } +.sandbox-template-heading h2 { font-size:18px; margin:4px 0; overflow-wrap:anywhere; } +.sandbox-template-heading .sandbox-eyebrow { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } +.sandbox-template-icon { flex-shrink:0; font-size:28px; color:var(--accent); background:var(--line); border-radius:10px; padding:8px 14px; } +.sandbox-template-overview,.sandbox-template-fields { display:grid; gap:16px; margin:0; } +.sandbox-template-overview { padding:20px 0; border-top:1px solid var(--line); } +.sandbox-template-overview > div,.sandbox-template-fields > div { min-width:0; } +.sandbox-template-overview dt,.sandbox-template-fields dt { font-size:12px; color:var(--muted); margin-bottom:4px; } +.sandbox-template-overview dd,.sandbox-template-fields dd { font-size:13px; color:var(--ink); margin:0; line-height:1.6; white-space:pre-line; overflow-wrap:anywhere; font-variant-numeric:tabular-nums; } +.sandbox-template-details { border-top:1px solid var(--line); margin-bottom:20px; } +.sandbox-template-details summary { padding:14px 0; font-size:13px; color:var(--secondary-ink); cursor:pointer; } +.sandbox-template-details summary:hover { color:var(--accent); } +.sandbox-template-note { margin:16px 0 0; padding:14px; border-radius:8px; background:var(--surface-muted); color:var(--muted); font-size:12px; line-height:1.8; } +.sandbox-template-actions { margin-top:auto; } +.sandbox-template-actions button { width:100%; } .sandbox-create-dialog { width:min(560px,calc(100% - 32px)); } .sandbox-create-dialog h2 { font-size:20px; margin:0; } .sandbox-create-form { display:grid; gap:20px; padding:24px; } @@ -44,14 +51,11 @@ .sandbox-summary > div { padding:18px; } .sandbox-summary > div:nth-child(3) { border-left:0; } .sandbox-summary > div:nth-child(n+3) { border-top:1px solid var(--line); } - .sandbox-template-fields { grid-template-columns:1fr; } .sandbox-template { padding:20px; } .sandbox-filters select { flex:1; max-width:100%; } } -.sandbox-template-list { display:grid; gap:20px; } .sandbox-template-heading > div:nth-child(2) { flex:1; min-width:0; } -.sandbox-template-heading h2 { margin:4px 0; } .sandbox-capabilities { display:flex; flex-wrap:wrap; gap:8px; margin:18px 0; } .sandbox-service-actions { display:contents; } .sandbox-service-link { text-decoration:none; } diff --git a/crates/agentdrive-mcp/dashboard/sandboxes.js b/crates/agentdrive-mcp/dashboard/sandboxes.js index 521acd3..00ba730 100644 --- a/crates/agentdrive-mcp/dashboard/sandboxes.js +++ b/crates/agentdrive-mcp/dashboard/sandboxes.js @@ -142,23 +142,27 @@ export function createSandboxesPage({api, element, container}) { const title = element('div'); title.append(element('div', 'sandbox-eyebrow', value.id),element('h2', '', value.manifest?.name || value.id)); const use = element('button', 'primary', '使用此模板创建'); use.type = 'button'; use.disabled = !data.enabled; use.onclick = () => { create.click(); targetTemplate.value = value.id; updateCreate(); }; - header.append(element('div', 'sandbox-template-icon', '▧'),title,use); card.append(header); + header.append(element('div', 'sandbox-template-icon', '▧'),title); card.append(header); const capabilities = element('div', 'sandbox-capabilities'); const labels = {desktop:'桌面',browser:'浏览器',mcp:'统一 MCP',openclaw:'OpenClaw',csgclaw:'CSGClaw',environment:'开发环境'}; for (const capability of value.capabilities || ['desktop','browser','mcp']) capabilities.append(element('span','pill',labels[capability] || capability)); card.append(capabilities); + const overview = element('dl', 'sandbox-template-overview'); const fields = element('dl', 'sandbox-template-fields'); - function field(name, content) { const group = element('div'); group.append(element('dt','',name),element('dd','',content)); fields.append(group); } + function field(name, content, target = fields) { const group = element('div'); group.append(element('dt','',name),element('dd','',content)); target.append(group); } const manifest = value.manifest; if (manifest) { - field('操作系统',manifest.os); field('桌面环境',manifest.desktop); field('默认浏览器',manifest.browser); + field('操作系统',manifest.os,overview); field('桌面环境',manifest.desktop,overview); field('默认浏览器',manifest.browser,overview); field('语言环境',manifest.locale); field('字体',manifest.fonts.join('\n')); field('桌面工具',manifest.tools.join('\n')); field('分支目录',manifest.workspace_path); field('清单生成时间',new Date(manifest.built_at).toLocaleString('zh-CN',{hour12:false})); } - field('每个实例的资源',`${value.vcpus} vCPU · ${value.memory_gib} GiB 内存 · ${value.disk_gib} GiB 系统磁盘`); + field('每个实例的资源',`${value.vcpus} vCPU · ${value.memory_gib} GiB 内存 · ${value.disk_gib} GiB 系统磁盘`,overview); field('模板版本',value.version || 'local'); field('并发与网络',`最多 ${value.max_instances} 个运行实例 · ${value.network_slots} 个已配置网络接口`); - card.append(fields,element('p','sandbox-template-note','分支文件自动挂载到 /workspace。已有沙盒继续使用创建时的系统磁盘。')); template.append(card); + const details = element('details', 'sandbox-template-details'); + details.append(element('summary', '', '查看完整配置'),fields,element('p','sandbox-template-note','分支文件自动挂载到 /workspace。已有沙盒继续使用创建时的系统磁盘。')); + const actions = element('div', 'sandbox-template-actions'); actions.append(use); + card.append(overview,details,actions); template.append(card); } } function render() { From bfe6f09be06c0061f7b426919fecc73d9f514eb6 Mon Sep 17 00:00:00 2001 From: GatewayJ <835269233@qq.com> Date: Fri, 13 Nov 2026 23:27:54 +0800 Subject: [PATCH 06/12] style: show template details in a dialog beside creation action --- crates/agentdrive-mcp/dashboard/sandboxes.css | 14 +++++++---- crates/agentdrive-mcp/dashboard/sandboxes.js | 23 +++++++++++++++---- tests/dashboard/sandboxes.test.cjs | 16 +++++++++++++ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/crates/agentdrive-mcp/dashboard/sandboxes.css b/crates/agentdrive-mcp/dashboard/sandboxes.css index 2a8cc54..bc7821d 100644 --- a/crates/agentdrive-mcp/dashboard/sandboxes.css +++ b/crates/agentdrive-mcp/dashboard/sandboxes.css @@ -33,12 +33,15 @@ .sandbox-template-overview > div,.sandbox-template-fields > div { min-width:0; } .sandbox-template-overview dt,.sandbox-template-fields dt { font-size:12px; color:var(--muted); margin-bottom:4px; } .sandbox-template-overview dd,.sandbox-template-fields dd { font-size:13px; color:var(--ink); margin:0; line-height:1.6; white-space:pre-line; overflow-wrap:anywhere; font-variant-numeric:tabular-nums; } -.sandbox-template-details { border-top:1px solid var(--line); margin-bottom:20px; } -.sandbox-template-details summary { padding:14px 0; font-size:13px; color:var(--secondary-ink); cursor:pointer; } -.sandbox-template-details summary:hover { color:var(--accent); } +.sandbox-template-dialog { width:min(720px,calc(100% - 32px)); } +.sandbox-template-dialog .revision-dialog-heading { position:sticky; top:0; z-index:1; background:var(--paper); } +.sandbox-template-dialog h2 { font-size:20px; margin:6px 0 0; overflow-wrap:anywhere; } +.sandbox-template-dialog-body { padding:var(--panel-padding); } +.sandbox-template-dialog .sandbox-template-fields { grid-template-columns:repeat(2,minmax(0,1fr)); gap:24px; } +.sandbox-template-dialog .sandbox-template-fields > div { border-bottom:1px solid var(--line); padding-bottom:16px; } .sandbox-template-note { margin:16px 0 0; padding:14px; border-radius:8px; background:var(--surface-muted); color:var(--muted); font-size:12px; line-height:1.8; } -.sandbox-template-actions { margin-top:auto; } -.sandbox-template-actions button { width:100%; } +.sandbox-template-actions { display:grid; grid-template-columns:minmax(0,1fr) minmax(0,1.5fr); gap:8px; padding-top:18px; border-top:1px solid var(--line); margin-top:auto; } +.sandbox-template-actions button { width:100%; min-width:0; padding:10px 8px; } .sandbox-create-dialog { width:min(560px,calc(100% - 32px)); } .sandbox-create-dialog h2 { font-size:20px; margin:0; } .sandbox-create-form { display:grid; gap:20px; padding:24px; } @@ -52,6 +55,7 @@ .sandbox-summary > div:nth-child(3) { border-left:0; } .sandbox-summary > div:nth-child(n+3) { border-top:1px solid var(--line); } .sandbox-template { padding:20px; } + .sandbox-template-dialog .sandbox-template-fields { grid-template-columns:minmax(0,1fr); } .sandbox-filters select { flex:1; max-width:100%; } } diff --git a/crates/agentdrive-mcp/dashboard/sandboxes.js b/crates/agentdrive-mcp/dashboard/sandboxes.js index 00ba730..3ed5850 100644 --- a/crates/agentdrive-mcp/dashboard/sandboxes.js +++ b/crates/agentdrive-mcp/dashboard/sandboxes.js @@ -159,10 +159,23 @@ export function createSandboxesPage({api, element, container}) { field('每个实例的资源',`${value.vcpus} vCPU · ${value.memory_gib} GiB 内存 · ${value.disk_gib} GiB 系统磁盘`,overview); field('模板版本',value.version || 'local'); field('并发与网络',`最多 ${value.max_instances} 个运行实例 · ${value.network_slots} 个已配置网络接口`); - const details = element('details', 'sandbox-template-details'); - details.append(element('summary', '', '查看完整配置'),fields,element('p','sandbox-template-note','分支文件自动挂载到 /workspace。已有沙盒继续使用创建时的系统磁盘。')); - const actions = element('div', 'sandbox-template-actions'); actions.append(use); - card.append(overview,details,actions); template.append(card); + const details = element('button', 'secondary', '查看详情'); details.type = 'button'; details.setAttribute('aria-haspopup', 'dialog'); + const dialog = element('dialog', 'revision-dialog sandbox-template-dialog'); + dialog.setAttribute('aria-label', `${manifest?.name || value.id} · 模板详情`); + const dialogHeader = element('div', 'revision-dialog-heading'); + const dialogTitle = element('div'); dialogTitle.append(element('div', 'sandbox-eyebrow', '模板详情'),element('h2', '', manifest?.name || value.id)); + const close = element('button', 'revision-dialog-close', '×'); close.type = 'button'; close.setAttribute('aria-label', '关闭模板详情'); + dialogHeader.append(dialogTitle,close); + const body = element('div', 'sandbox-template-dialog-body'); + const configuration = element('dl', 'sandbox-template-fields'); + configuration.append(...Array.from(overview.children, group => group.cloneNode(true)),...fields.children); + body.append(configuration,element('p','sandbox-template-note','分支文件自动挂载到 /workspace。已有沙盒继续使用创建时的系统磁盘。')); + dialog.append(dialogHeader,body); + details.onclick = () => dialog.showModal(); + close.onclick = () => dialog.close(); + dialog.addEventListener('close', () => { if (details.isConnected && !template.hidden) details.focus(); }); + const actions = element('div', 'sandbox-template-actions'); actions.append(details,use); + card.append(overview,actions,dialog); template.append(card); } } function render() { @@ -213,6 +226,6 @@ export function createSandboxesPage({api, element, container}) { try { const result = await api('/api/sandboxes'); if (request === generation) { data = result; render(); } } catch (error) { if (request === generation) { notice.hidden = false; notice.textContent = `沙盒数据刷新失败:${error.message}`; if (!data) empty.textContent = '暂时无法读取沙盒,请点击页面右上方刷新按钮重试。'; } } } - function clear() { generation++; createDialog.close(); createDialog.remove(); for (const card of cards.values()) card.control.dispose(); cards.clear(); list.replaceChildren(); data = null; templateKey = ''; workspaceKey = ''; } + function clear() { generation++; createDialog.close(); createDialog.remove(); for (const dialog of template.querySelectorAll('dialog')) dialog.close(); template.replaceChildren(); for (const card of cards.values()) card.control.dispose(); cards.clear(); list.replaceChildren(); data = null; templateKey = ''; workspaceKey = ''; } return {load, clear}; } diff --git a/tests/dashboard/sandboxes.test.cjs b/tests/dashboard/sandboxes.test.cjs index f805243..82e5359 100644 --- a/tests/dashboard/sandboxes.test.cjs +++ b/tests/dashboard/sandboxes.test.cjs @@ -111,6 +111,22 @@ test('creation dialog explains unavailable branches and capacity without sending test('template shows installed versions, resources, and stays selected across refresh',async t=>{ const {page}=await setup(t); await page.getByRole('tab',{name:'模板内容'}).click(); + const details=page.getByRole('button',{name:'查看详情',exact:true}); + await details.click(); + const dialog=page.getByRole('dialog',{name:'Ubuntu Desktop · 模板详情',exact:true}); + assert(await dialog.isVisible()); + assert(await dialog.getByText('computer-use-linux 0.7.53',{exact:true}).isVisible()); + assert(await dialog.getByText('Chromium 160',{exact:true}).isVisible()); + await page.waitForTimeout(2200); + assert(await dialog.isVisible()); + await page.setViewportSize({width:320,height:700}); + assert(await dialog.evaluate(e=>e.scrollWidth<=e.clientWidth)); + await page.keyboard.press('Escape'); + assert.equal(await dialog.isVisible(),false); + assert(await details.evaluate(e=>e===document.activeElement)); + await details.click(); + await dialog.getByRole('button',{name:'关闭模板详情'}).click(); + assert.equal(await dialog.isVisible(),false); await page.getByRole('button',{name:'刷新数据'}).click(); assert(await page.getByRole('tabpanel',{name:'模板内容'}).isVisible()); assert.match(await page.locator('#sandbox-template').textContent(),/Chromium 160/); From 8c192eae1937688044ac4ae5b72315598d9ca13f Mon Sep 17 00:00:00 2001 From: GatewayJ <835269233@qq.com> Date: Sun, 15 Nov 2026 12:11:05 +0800 Subject: [PATCH 07/12] fix: recover interrupted branch mounts before sandbox startup --- crates/agentdrive-engine/src/mounts.rs | 9 + crates/agentdrive-engine/src/sandbox.rs | 78 ++++++- crates/agentdrive-mcp/dashboard/sandbox.js | 4 +- crates/agentdrive-mcp/src/sandbox_tests.rs | 259 ++++++++++++++++++++- docs/sandbox.md | 2 + tests/dashboard/sandbox.test.cjs | 15 ++ 6 files changed, 358 insertions(+), 9 deletions(-) diff --git a/crates/agentdrive-engine/src/mounts.rs b/crates/agentdrive-engine/src/mounts.rs index 2847e24..e905674 100644 --- a/crates/agentdrive-engine/src/mounts.rs +++ b/crates/agentdrive-engine/src/mounts.rs @@ -403,6 +403,15 @@ impl MountService { } } NativeMountState::Unknown => { + let mut failed = ready; + failed.state = MountState::RecoveryRequired; + self.runtime + .state + .commit(LocalCommit { + mounts: vec![failed], + ..Default::default() + }) + .await?; return Err(Error::new( ErrorCode::MountBusy, "native mount identity cannot be confirmed", diff --git a/crates/agentdrive-engine/src/sandbox.rs b/crates/agentdrive-engine/src/sandbox.rs index cc039a2..56c884b 100644 --- a/crates/agentdrive-engine/src/sandbox.rs +++ b/crates/agentdrive-engine/src/sandbox.rs @@ -355,6 +355,31 @@ impl Drop for PendingPin { } impl SandboxService { + async fn check_pending_mount_operations( + &self, + workspace: WorkspaceId, + branch: BranchId, + ) -> Result<()> { + if self + .runtime + .state + .operations(workspace) + .await? + .iter() + .any(|record| { + record.pending.is_some() + && (record.branch == Some(branch) + || matches!(record.pending, Some(PendingAction::BranchSwitch { .. }))) + }) + { + return Err(Error::new( + ErrorCode::Busy, + "当前分支有未完成操作,请在挂载管理中完成该操作后重试", + )); + } + Ok(()) + } + async fn create_inner( &self, actor: &Principal, @@ -431,8 +456,22 @@ impl SandboxService { && m.access == AccessMode::ReadWrite && m.state != MountState::Released }); - if binding.is_none() { - self.services + let needs_recovery = binding.as_ref().is_some_and(|binding| { + matches!( + binding.state, + MountState::RecoveryRequired | MountState::Suspended + ) + }); + if binding.is_none() || needs_recovery { + if needs_recovery { + self.check_pending_mount_operations(workspace, branch) + .await?; + } + let path = binding + .as_ref() + .map_or_else(|| self.mount_root.join(&id), |binding| binding.path.clone()); + let prepared = self + .services .mounts .prepare( RequestContext { @@ -444,14 +483,36 @@ impl SandboxService { workspace, branch: Some(branch), revision: None, - path: self.mount_root.join(&id), + path: path.clone(), access: AccessMode::ReadWrite, - durability: Durability::Local, + durability: binding + .as_ref() + .map_or(Durability::Local, |binding| binding.durability), }, ) - .await?; + .await; + let prepared = prepared.and_then(|record| match record.error { + Some(error) => Err(error), + None if record.mount_ready == Some(true) => Ok(record), + None => Err(Error::new( + ErrorCode::MountBusy, + "挂载操作尚未完成,请在挂载管理中检查操作状态", + )), + }); + prepared.map_err(|mut error| { + error.message = format!( + "分支目录挂载失败({}):{}。处理后点击“恢复挂载”重试。", + path.display(), + error.message + ); + error + })?; } let _lifecycle = self.runtime.lifecycle.lock().await; + if needs_recovery { + self.check_pending_mount_operations(workspace, branch) + .await?; + } let binding = self .runtime .state @@ -463,7 +524,12 @@ impl SandboxService { && m.access == AccessMode::ReadWrite && m.state == MountState::Ready }) - .ok_or_else(|| Error::new(ErrorCode::MountBusy, "Branch mount is not ready"))?; + .ok_or_else(|| { + Error::new( + ErrorCode::MountBusy, + "分支挂载尚未就绪,请在挂载管理中完成未结束的操作,再点击“恢复挂载”重试", + ) + })?; { let _branch = self.runtime.lock_branch(branch).await; let current = self.runtime.branch(branch).await?; diff --git a/crates/agentdrive-mcp/dashboard/sandbox.js b/crates/agentdrive-mcp/dashboard/sandbox.js index b4520ac..de145e9 100644 --- a/crates/agentdrive-mcp/dashboard/sandbox.js +++ b/crates/agentdrive-mcp/dashboard/sandbox.js @@ -165,7 +165,9 @@ export function createSandboxControl({api, element, workspace, branch, branchNam create.disabled = !enabled || !canStart || busy || active; create.title = canStart ? '' : '当前分支尚未取得本机写入权限'; create.hidden = ['creating', 'starting', 'ready', 'stopping'].includes(state); - create.textContent = state === 'error' ? '重试启动' : '启动 Sandbox'; + const recoverMount = state === 'error' && value?.startup_step === 'prepare_branch'; + create.textContent = recoverMount ? '恢复挂载' : state === 'error' ? '重试启动' : '启动 Sandbox'; + if (recoverMount && canStart) create.title = '恢复当前分支挂载后继续启动 Sandbox'; stop.hidden = state !== 'ready'; stop.disabled = busy; force.hidden = state !== 'error' && !(state === 'ready' && (stopFailed || value?.message?.startsWith('正常停止失败'))); force.disabled = busy || !enabled || force.hidden; remove.hidden = !['stopped', 'error'].includes(state); remove.disabled = !enabled || busy || remove.hidden; diff --git a/crates/agentdrive-mcp/src/sandbox_tests.rs b/crates/agentdrive-mcp/src/sandbox_tests.rs index f7ec303..20143d2 100644 --- a/crates/agentdrive-mcp/src/sandbox_tests.rs +++ b/crates/agentdrive-mcp/src/sandbox_tests.rs @@ -58,7 +58,10 @@ impl SandboxRuntime for RuntimeStub { .lock() .await .insert(request.id, status.clone()) - .is_none_or(|previous| matches!(previous.state, SandboxState::Stopped)), + .is_none_or(|previous| matches!( + previous.state, + SandboxState::Stopped | SandboxState::Creating + )), "duplicate runtime launch" ); Ok(status) @@ -89,6 +92,9 @@ impl SandboxRuntime for RuntimeStub { .cloned() .ok_or_else(|| Error::new(ErrorCode::NotFound, "missing")) } + async fn report_progress(&self, status: SandboxStatus) { + self.states.lock().await.insert(status.id.clone(), status); + } } struct Fixture { @@ -100,6 +106,7 @@ struct Fixture { branch: BranchId, driver: Arc, sandbox: Arc, + mounts: Arc, } async fn fixture() -> Fixture { fixture_with_remote(None).await @@ -107,6 +114,7 @@ async fn fixture() -> Fixture { async fn fixture_with_remote(remote: Option>) -> Fixture { let directory = tempfile::tempdir().unwrap(); let local = Arc::new(LocalBackend::open(directory.path()).unwrap()); + let mounts = Arc::new(MemoryMountDriver::default()); let engine = Engine::open( Adapters { state: local.clone(), @@ -117,7 +125,7 @@ async fn fixture_with_remote(remote: Option>) -> Fixture { .map(|remote| remote as Arc), remote_refs: remote.map(|remote| remote as Arc), clock: Arc::new(ManualClock::default()), - mounts: Arc::new(MemoryMountDriver::default()), + mounts: mounts.clone(), directories: Arc::new(NoHostDirectories), }, EngineConfig { @@ -157,9 +165,256 @@ async fn fixture_with_remote(remote: Option>) -> Fixture { branch, driver, sandbox, + mounts, } } +async fn interrupted_mount(f: &Fixture, state: MountState) -> MountBinding { + let prepared = f + .engine + .services + .mounts + .prepare( + RequestContext { + principal: f.owner.clone(), + request_id: OperationId::new().to_string(), + }, + PrepareMount { + automatic: false, + workspace: f.workspace, + branch: Some(f.branch), + revision: None, + path: f._directory.path().join("custom-branch"), + access: AccessMode::ReadWrite, + durability: Durability::Local, + }, + ) + .await + .unwrap(); + assert!(prepared.error.is_none()); + let mut binding = f.local.mounts().await.unwrap().pop().unwrap(); + f.mounts.unmount(&binding).await.unwrap(); + binding.state = state; + f.local + .commit(LocalCommit { + mounts: vec![binding.clone()], + ..Default::default() + }) + .await + .unwrap(); + binding +} + +#[tokio::test] +async fn sandbox_recovers_interrupted_and_suspended_mounts_at_the_original_path() { + for state in [MountState::RecoveryRequired, MountState::Suspended] { + let f = fixture().await; + let binding = interrupted_mount(&f, state).await; + let started = f + .sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap(); + assert!(matches!(started.state, SandboxState::Ready)); + let mounts = f.local.mounts().await.unwrap(); + assert_eq!(mounts.len(), 1); + assert_eq!(mounts[0].id, binding.id); + assert_eq!(mounts[0].path, binding.path); + assert_eq!(mounts[0].generation, binding.generation); + assert_eq!(mounts[0].state, MountState::Ready); + assert_eq!( + f.mounts.status(&mounts[0]).await.unwrap(), + NativeMountState::Mounted + ); + } +} + +#[tokio::test] +async fn sandbox_mount_recovery_reports_the_native_error_and_can_retry() { + let f = fixture().await; + let binding = interrupted_mount(&f, MountState::RecoveryRequired).await; + f.mounts + .fail_mount + .store(true, std::sync::atomic::Ordering::SeqCst); + let error = f + .sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap_err(); + assert!(error.message.contains("injected mount failure")); + assert!(error.message.contains(binding.path.to_str().unwrap())); + let status = f + .sandbox + .status(&f.owner, f.workspace, f.branch) + .await + .unwrap() + .unwrap(); + assert!(matches!(status.state, SandboxState::Error)); + assert_eq!(status.startup_step.as_deref(), Some("prepare_branch")); + assert_eq!(status.message.as_deref(), Some(error.message.as_str())); + assert_eq!( + f.local.mount(binding.id).await.unwrap().unwrap().state, + MountState::RecoveryRequired + ); + assert!(matches!( + f.sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap() + .state, + SandboxState::Ready + )); +} + +#[tokio::test] +async fn sandbox_initial_mount_failure_reports_the_operation_error() { + let f = fixture().await; + f.mounts + .fail_mount + .store(true, std::sync::atomic::Ordering::SeqCst); + let error = f + .sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap_err(); + assert_eq!(error.code, ErrorCode::Unavailable); + assert!(error.message.contains("injected mount failure")); + let status = f + .sandbox + .status(&f.owner, f.workspace, f.branch) + .await + .unwrap() + .unwrap(); + assert!(matches!(status.state, SandboxState::Error)); + assert_eq!(status.message.as_deref(), Some(error.message.as_str())); +} + +#[tokio::test] +async fn sandbox_mount_recovery_checks_binding_owner_and_generation() { + for owner_changed in [true, false] { + let f = fixture().await; + let mut binding = interrupted_mount(&f, MountState::RecoveryRequired).await; + if owner_changed { + binding.principal = "other".to_string().try_into().unwrap(); + } else { + binding.branch_generation = Some(100); + } + f.local + .commit(LocalCommit { + mounts: vec![binding.clone()], + ..Default::default() + }) + .await + .unwrap(); + let error = f + .sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap_err(); + assert_eq!( + error.code, + if owner_changed { + ErrorCode::PermissionDenied + } else { + ErrorCode::StaleBinding + } + ); + assert_eq!( + f.mounts.status(&binding).await.unwrap(), + NativeMountState::Absent + ); + assert_eq!(f.local.mount(binding.id).await.unwrap().unwrap(), binding); + } +} + +#[tokio::test] +async fn sandbox_mount_recovery_preserves_pending_branch_operations() { + let f = fixture().await; + let binding = interrupted_mount(&f, MountState::RecoveryRequired).await; + let mut pending = f + .local + .operations(f.workspace) + .await + .unwrap() + .into_iter() + .find(|r| r.name == "mount_prepare") + .unwrap(); + pending.phase = OperationPhase::RecoveryRequired; + pending.pending = Some(PendingAction::Transfer { + target: LocationId::new(), + guard: BranchGuard::from(&f.local.branch(f.branch).await.unwrap().unwrap()), + }); + f.local + .commit(LocalCommit { + operations: vec![pending.clone()], + ..Default::default() + }) + .await + .unwrap(); + let error = f + .sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap_err(); + assert_eq!(error.code, ErrorCode::Busy); + assert!(error.message.contains("未完成操作")); + assert_eq!( + f.mounts.status(&binding).await.unwrap(), + NativeMountState::Absent + ); + assert_eq!( + f.local.mount(binding.id).await.unwrap().unwrap().state, + MountState::RecoveryRequired + ); + assert_eq!( + f.local + .operations(f.workspace) + .await + .unwrap() + .into_iter() + .find(|r| r.id == pending.id) + .unwrap() + .pending, + pending.pending + ); +} + +#[tokio::test] +async fn sandbox_mount_recovery_rejects_an_unrecognized_native_binding() { + let f = fixture().await; + let binding = interrupted_mount(&f, MountState::RecoveryRequired).await; + let mut other = binding.clone(); + other.generation += 1; + f.mounts + .mount( + MountSpec { + binding: other.clone(), + }, + f.engine.services.files.clone(), + ) + .await + .unwrap(); + let error = f + .sandbox + .create(&f.owner, f.workspace, f.branch) + .await + .unwrap_err(); + assert_eq!(error.code, ErrorCode::MountBusy); + assert!( + error + .message + .contains("native mount identity cannot be confirmed") + ); + assert_eq!( + f.mounts.status(&other).await.unwrap(), + NativeMountState::Mounted + ); + assert_eq!( + f.local.mount(binding.id).await.unwrap().unwrap().state, + MountState::RecoveryRequired + ); +} + #[tokio::test] async fn sandbox_authorizes_branch_and_pins_binding_until_stop() { let Fixture { diff --git a/docs/sandbox.md b/docs/sandbox.md index 9794ec8..37b4254 100644 --- a/docs/sandbox.md +++ b/docs/sandbox.md @@ -200,6 +200,8 @@ python3 -m unittest discover -s tests/sandbox -p 'test_*.py' 启动页面展示四个步骤:准备分支目录、准备 Ubuntu 系统磁盘、启动 Ubuntu 实例、等待桌面启动。就绪检查只确认 GNOME 会话、窗口管理器、VNC 和 noVNC 服务以及 `/workspace` 挂载。启动期间跳过浏览器、字体和 MCP 软件检查,也不执行截图或预先打开 Chromium。桌面辅助功能在模板中默认启用,Playwright 连接时按需打开共用的 Chromium。软件功能通过模板验证脚本单独测试。 +启动 Sandbox 时,已有分支挂载处于 `recovery_required` 或 `suspended` 状态会自动尝试恢复,继续使用原目录和持久化配置。恢复流程检查挂载所属用户、分支版本及本机目录状态;存在未完成的分支操作时,需要先在挂载管理中处理该操作。挂载失败会显示目录和具体原因,页面提供“恢复挂载”按钮,处理问题后点击即可重新恢复并继续启动 Ubuntu。无法确认本机挂载身份时保留 `recovery_required` 状态。 + ## Ubuntu GNOME 与 CSGClaw diff --git a/tests/dashboard/sandbox.test.cjs b/tests/dashboard/sandbox.test.cjs index fbe9334..c3789e6 100644 --- a/tests/dashboard/sandbox.test.cjs +++ b/tests/dashboard/sandbox.test.cjs @@ -57,6 +57,21 @@ test('Sandbox creation targets its branch and exposes desktop and stop actions', assert(calls.every(c=>c.url==='/api/workspaces/workspace-a/branches/branch-a/sandbox')); }); +test('Mount recovery shows the failure reason and retries startup for the same branch',async t=>{ + const page=await setup(t); + await page.evaluate(()=>document.querySelector('.sandbox-controls').update({ + state:'error',startup_step:'prepare_branch',message:'分支目录挂载失败:目录被占用。处理后点击“恢复挂载”重试。', + },true)); + const recover=page.getByRole('button',{name:'恢复挂载',exact:true}); + assert(await recover.isVisible()); + assert.match(await page.getByRole('status').textContent(),/目录被占用/); + await recover.click(); + assert(await page.getByRole('link',{name:'打开桌面'}).isVisible()); + assert.deepEqual((await page.evaluate(()=>window.calls)).filter(c=>c.method==='POST'),[ + {url:'/api/workspaces/workspace-a/branches/branch-a/sandbox',method:'POST'}, + ]); +}); + test('Deletion requires stopped state and exact confirmation, supports cancellation and retry', async t => { const page = await setup(t); await page.getByRole('button', {name:'启动 Sandbox', exact:true}).click(); From c6a2e1c9a90d9b5dc84adcf39e70657ea30a12f5 Mon Sep 17 00:00:00 2001 From: GatewayJ <835269233@qq.com> Date: Sun, 15 Nov 2026 15:45:45 +0800 Subject: [PATCH 08/12] fix: recover disconnected mounts without blocking on obsolete operations --- crates/agentdrive-engine/src/mounts.rs | 62 ++++--- crates/agentdrive-engine/src/sandbox.rs | 35 ++-- crates/agentdrive-mcp/src/sandbox_tests.rs | 61 +++++++ crates/agentdrive-platform/src/fuse.rs | 40 ++++- crates/agentdrive-platform/src/native.rs | 12 ++ .../agentdrive-platform/tests/native_mount.rs | 4 + .../tests/native_mount/disconnected.rs | 153 ++++++++++++++++++ crates/agentdrive-ports/src/platform.rs | 5 + docs/sandbox.md | 4 + 9 files changed, 324 insertions(+), 52 deletions(-) create mode 100644 crates/agentdrive-platform/tests/native_mount/disconnected.rs diff --git a/crates/agentdrive-engine/src/mounts.rs b/crates/agentdrive-engine/src/mounts.rs index e905674..6f5c47b 100644 --- a/crates/agentdrive-engine/src/mounts.rs +++ b/crates/agentdrive-engine/src/mounts.rs @@ -377,46 +377,40 @@ impl MountService { ..Default::default() }) .await?; - match self.driver.status(&ready).await? { - NativeMountState::Mounted => (), - NativeMountState::Absent => { - if let Err(error) = self - .driver - .mount( - MountSpec { - binding: ready.clone(), - }, - self.files.clone(), - ) - .await - { - let mut failed = ready.clone(); - failed.state = MountState::RecoveryRequired; - self.runtime - .state - .commit(LocalCommit { - mounts: vec![failed], - ..Default::default() - }) - .await?; - return Err(error); - } + let installed = async { + let state = self.driver.status(&ready).await?; + if state == NativeMountState::Mounted { + return Ok(()); } - NativeMountState::Unknown => { - let mut failed = ready; - failed.state = MountState::RecoveryRequired; - self.runtime - .state - .commit(LocalCommit { - mounts: vec![failed], - ..Default::default() - }) - .await?; + if state == NativeMountState::Unknown + && !self.driver.detach_disconnected(&ready).await? + { return Err(Error::new( ErrorCode::MountBusy, "native mount identity cannot be confirmed", )); } + self.driver + .mount( + MountSpec { + binding: ready.clone(), + }, + self.files.clone(), + ) + .await + } + .await; + if let Err(error) = installed { + let mut failed = ready; + failed.state = MountState::RecoveryRequired; + self.runtime + .state + .commit(LocalCommit { + mounts: vec![failed], + ..Default::default() + }) + .await?; + return Err(error); } Ok(ready) } diff --git a/crates/agentdrive-engine/src/sandbox.rs b/crates/agentdrive-engine/src/sandbox.rs index 56c884b..c90d035 100644 --- a/crates/agentdrive-engine/src/sandbox.rs +++ b/crates/agentdrive-engine/src/sandbox.rs @@ -355,21 +355,24 @@ impl Drop for PendingPin { } impl SandboxService { - async fn check_pending_mount_operations( - &self, - workspace: WorkspaceId, - branch: BranchId, - ) -> Result<()> { + async fn check_pending_mount_operations(&self, binding: &MountBinding) -> Result<()> { if self .runtime .state - .operations(workspace) + .operations(binding.workspace) .await? .iter() - .any(|record| { - record.pending.is_some() - && (record.branch == Some(branch) - || matches!(record.pending, Some(PendingAction::BranchSwitch { .. }))) + .any(|record| match &record.pending { + Some(PendingAction::MountChange { old, new }) => old + .iter() + .chain(new.iter()) + .any(|mount| mount.id == binding.id && mount.generation == binding.generation), + Some(PendingAction::BranchSwitch { .. }) => true, + Some(PendingAction::ApplyRevision { target, .. }) => { + target.branch == binding.branch + } + Some(PendingAction::Transfer { .. }) => record.branch == Some(binding.branch), + None => false, }) { return Err(Error::new( @@ -463,9 +466,8 @@ impl SandboxService { ) }); if binding.is_none() || needs_recovery { - if needs_recovery { - self.check_pending_mount_operations(workspace, branch) - .await?; + if let Some(binding) = binding.as_ref().filter(|_| needs_recovery) { + self.check_pending_mount_operations(binding).await?; } let path = binding .as_ref() @@ -509,10 +511,6 @@ impl SandboxService { })?; } let _lifecycle = self.runtime.lifecycle.lock().await; - if needs_recovery { - self.check_pending_mount_operations(workspace, branch) - .await?; - } let binding = self .runtime .state @@ -530,6 +528,9 @@ impl SandboxService { "分支挂载尚未就绪,请在挂载管理中完成未结束的操作,再点击“恢复挂载”重试", ) })?; + if needs_recovery { + self.check_pending_mount_operations(&binding).await?; + } { let _branch = self.runtime.lock_branch(branch).await; let current = self.runtime.branch(branch).await?; diff --git a/crates/agentdrive-mcp/src/sandbox_tests.rs b/crates/agentdrive-mcp/src/sandbox_tests.rs index 20143d2..ab4547c 100644 --- a/crates/agentdrive-mcp/src/sandbox_tests.rs +++ b/crates/agentdrive-mcp/src/sandbox_tests.rs @@ -415,6 +415,67 @@ async fn sandbox_mount_recovery_rejects_an_unrecognized_native_binding() { ); } +#[tokio::test] +async fn sandbox_mount_recovery_checks_the_binding_referenced_by_pending_unmounts() { + for current_binding in [false, true] { + let f = fixture().await; + let binding = interrupted_mount(&f, MountState::RecoveryRequired).await; + let mut old = binding.clone(); + if !current_binding { + old.id = MountId::new(); + } + old.state = MountState::Ready; + let mut pending = f + .local + .operations(f.workspace) + .await + .unwrap() + .into_iter() + .find(|r| r.name == "mount_prepare") + .unwrap(); + pending.name = "mount_unmount".into(); + pending.id = OperationId::new(); + pending.request_id = OperationId::new().to_string(); + pending.phase = OperationPhase::WaitingForQuiesce; + pending.pending = Some(PendingAction::MountChange { + old: Some(old), + new: None, + }); + f.local + .commit(LocalCommit { + operations: vec![pending.clone()], + ..Default::default() + }) + .await + .unwrap(); + let result = f.sandbox.create(&f.owner, f.workspace, f.branch).await; + if current_binding { + assert_eq!(result.unwrap_err().code, ErrorCode::Busy); + assert_eq!( + f.mounts.status(&binding).await.unwrap(), + NativeMountState::Absent + ); + } else { + assert!(matches!(result.unwrap().state, SandboxState::Ready)); + assert_eq!( + f.local.mount(binding.id).await.unwrap().unwrap().state, + MountState::Ready + ); + } + assert_eq!( + f.local + .operations(f.workspace) + .await + .unwrap() + .into_iter() + .find(|r| r.id == pending.id) + .unwrap() + .pending, + pending.pending + ); + } +} + #[tokio::test] async fn sandbox_authorizes_branch_and_pins_binding_until_stop() { let Fixture { diff --git a/crates/agentdrive-platform/src/fuse.rs b/crates/agentdrive-platform/src/fuse.rs index 42ef88b..d58e4f2 100644 --- a/crates/agentdrive-platform/src/fuse.rs +++ b/crates/agentdrive-platform/src/fuse.rs @@ -135,6 +135,36 @@ impl Driver { Ok(current.binding.open_handles()) } + pub async fn detach_disconnected(&self, binding: &MountBinding) -> Result { + if self + .mounts + .lock() + .get(&binding.id) + .is_some_and(|current| !current.session.guard.is_finished()) + { + return Ok(false); + } + let binding_copy = binding.clone(); + let disconnected = tokio::task::spawn_blocking(move || { + let expected = ( + "fuse.agentdrive".to_owned(), + format!("agentdrive:{}", binding_copy.id), + ); + if native_mount(&binding_copy.path)? != Some(expected) { + return Ok::<_, Error>(false); + } + Ok(std::fs::symlink_metadata(&binding_copy.path) + .is_err_and(|error| error.raw_os_error() == Some(libc::ENOTCONN))) + }) + .await + .map_err(|error| Error::new(ErrorCode::Io, error.to_string()))??; + if !disconnected { + return Ok(false); + } + self.unmount(binding).await?; + Ok(true) + } + pub async fn rebind(&self, previous: &MountBinding, next: &MountBinding) -> Result<()> { let mounts = self.mounts.lock(); let current = mounts @@ -227,6 +257,10 @@ fn helper_unmount_error(output: &std::process::Output) -> Error { } fn native_source(path: &Path) -> Result> { + Ok(native_mount(path)?.map(|(_, source)| source)) +} + +fn native_mount(path: &Path) -> Result> { use std::os::unix::ffi::OsStrExt; let table = std::fs::read_to_string("/proc/self/mountinfo")?; for line in table.lines() { @@ -260,7 +294,11 @@ fn native_source(path: &Path) -> Result> { } } if decoded == path.as_os_str().as_bytes() { - return Ok(right.split_whitespace().nth(1).map(str::to_owned)); + let mut fields = right.split_whitespace(); + return Ok(fields + .next() + .zip(fields.next()) + .map(|(kind, source)| (kind.to_owned(), source.to_owned()))); } } Ok(None) diff --git a/crates/agentdrive-platform/src/native.rs b/crates/agentdrive-platform/src/native.rs index c46db51..21d90cf 100644 --- a/crates/agentdrive-platform/src/native.rs +++ b/crates/agentdrive-platform/src/native.rs @@ -170,6 +170,18 @@ impl MountDriver for NativeMountDriver { } } + async fn detach_disconnected(&self, binding: &MountBinding) -> Result { + self.normalize_path(&binding.path).await?; + #[cfg(target_os = "linux")] + { + self.implementation.detach_disconnected(binding).await + } + #[cfg(not(target_os = "linux"))] + { + Ok(false) + } + } + fn capability(&self) -> &'static str { #[cfg(target_os = "linux")] { diff --git a/crates/agentdrive-platform/tests/native_mount.rs b/crates/agentdrive-platform/tests/native_mount.rs index 68013d5..f3b7351 100644 --- a/crates/agentdrive-platform/tests/native_mount.rs +++ b/crates/agentdrive-platform/tests/native_mount.rs @@ -13,6 +13,10 @@ use std::{ #[path = "native_mount/collaboration.rs"] mod collaboration; +#[cfg(target_os = "linux")] +#[path = "native_mount/disconnected.rs"] +mod disconnected; + fn user_entries(path: &std::path::Path) -> usize { std::fs::read_dir(path) .unwrap() diff --git a/crates/agentdrive-platform/tests/native_mount/disconnected.rs b/crates/agentdrive-platform/tests/native_mount/disconnected.rs new file mode 100644 index 0000000..3cfcfdb --- /dev/null +++ b/crates/agentdrive-platform/tests/native_mount/disconnected.rs @@ -0,0 +1,153 @@ +use super::*; +use std::{ + path::PathBuf, + process::{Child, Command, Stdio}, + time::Duration, +}; + +struct MountedChild { + child: Child, + path: PathBuf, +} +impl Drop for MountedChild { + fn drop(&mut self) { + let _ = self.child.kill(); + let _ = self.child.wait(); + let _ = Command::new("fusermount3") + .arg("-u") + .arg(&self.path) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status(); + } +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +#[ignore = "requires Linux FUSE and mount permission"] +async fn only_disconnected_matching_mounts_can_be_detached() { + let root = tempfile::tempdir().unwrap(); + let path = root.path().join("mounts/working"); + let mut process = MountedChild { + child: Command::new(std::env::current_exe().unwrap()) + .args([ + "--ignored", + "--exact", + "disconnected::disconnected_mount_child", + ]) + .env("AGENTDRIVE_DISCONNECTED_TEST_ROOT", root.path()) + .stdout(Stdio::null()) + .spawn() + .unwrap(), + path: path.clone(), + }; + let binding_path = root.path().join("binding"); + for _ in 0..200 { + if binding_path.exists() { + break; + } + assert!( + process.child.try_wait().unwrap().is_none(), + "mount helper exited" + ); + tokio::time::sleep(Duration::from_millis(50)).await; + } + let binding: MountBinding = decode(&std::fs::read(binding_path).unwrap()).unwrap(); + let driver = NativeMountDriver::new(vec![root.path().join("mounts")]).unwrap(); + assert!(!driver.detach_disconnected(&binding).await.unwrap()); + assert!(path.read_dir().is_ok()); + process.child.kill().unwrap(); + process.child.wait().unwrap(); + assert_eq!( + std::fs::symlink_metadata(&path).unwrap_err().raw_os_error(), + Some(libc::ENOTCONN) + ); + let mut other = binding.clone(); + other.id = MountId::new(); + assert!(!driver.detach_disconnected(&other).await.unwrap()); + assert_eq!( + driver.status(&binding).await.unwrap(), + NativeMountState::Unknown + ); + assert!(driver.detach_disconnected(&binding).await.unwrap()); + assert_eq!( + driver.status(&binding).await.unwrap(), + NativeMountState::Absent + ); + assert!(path.read_dir().is_ok()); + assert!(!driver.detach_disconnected(&binding).await.unwrap()); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +#[ignore = "subprocess fixture for the disconnected mount test"] +async fn disconnected_mount_child() { + let Some(root) = std::env::var_os("AGENTDRIVE_DISCONNECTED_TEST_ROOT") else { + return; + }; + let root = PathBuf::from(root); + let local = Arc::new(LocalBackend::open(root.join("state")).unwrap()); + let mounts = root.join("mounts"); + let driver = NativeMountDriver::new(vec![mounts.clone()]).unwrap(); + let engine = Engine::open( + Adapters { + state: local.clone(), + objects: local.clone(), + working: local, + remote_objects: None, + remote_refs: None, + clock: Arc::new(SystemClock::default()), + mounts: driver, + directories: HostDirectories::new(vec![]).unwrap(), + }, + EngineConfig { + live: LiveConfig::new(RuntimeConfig::default()).unwrap(), + identity: local_identity(), + }, + ) + .await + .unwrap(); + let created = success( + engine + .services + .workspace + .create( + context(), + CreateWorkspace { + name: "disconnected mount".into(), + grants: vec![], + max_file_bytes: None, + max_working_bytes: None, + max_inodes: None, + }, + ) + .await + .unwrap(), + ); + let Some(CommandResult::Workspace { workspace }) = created.result else { + panic!("missing workspace"); + }; + let mounted = success( + engine + .services + .mounts + .prepare( + context(), + PrepareMount { + automatic: false, + workspace: workspace.id, + branch: None, + revision: None, + path: mounts.join("working"), + access: AccessMode::ReadWrite, + durability: Durability::Local, + }, + ) + .await + .unwrap(), + ); + let Some(CommandResult::Mount { binding }) = mounted.result else { + panic!("missing binding"); + }; + std::fs::write(root.join("binding.tmp"), encode(&binding).unwrap()).unwrap(); + std::fs::rename(root.join("binding.tmp"), root.join("binding")).unwrap(); + std::future::pending::<()>().await; +} diff --git a/crates/agentdrive-ports/src/platform.rs b/crates/agentdrive-ports/src/platform.rs index 7912db9..1b1c3d8 100644 --- a/crates/agentdrive-ports/src/platform.rs +++ b/crates/agentdrive-ports/src/platform.rs @@ -64,6 +64,11 @@ pub trait MountDriver: Send + Sync { )) } async fn unmount(&self, binding: &MountBinding) -> Result<()>; + /// 仅解除能够确认属于此绑定、且连接已经中断的本机挂载。 + /// 返回 false 时保留原挂载,由调用方报告无法恢复。 + async fn detach_disconnected(&self, _binding: &MountBinding) -> Result { + Ok(false) + } async fn status(&self, binding: &MountBinding) -> Result; fn capability(&self) -> &'static str; } diff --git a/docs/sandbox.md b/docs/sandbox.md index 37b4254..3c05659 100644 --- a/docs/sandbox.md +++ b/docs/sandbox.md @@ -119,6 +119,8 @@ sudo bash build/sandbox/prepare-network.sh "$(id -u)" `mount_root` 必须位于 AgentDrive 配置的 `mount_roots` 内。运行用户需要访问 `/dev/kvm` 与 `/dev/fuse`。 +文件共享和助手会使用较多文件句柄。通过 systemd 运行时,在服务的 `[Service]` 中设置 `LimitNOFILE=65536`;通过终端启动时,执行 `ulimit -n 65536`。worker 继承该限制。上限过低会导致 `Too many open files`,影响桌面和 MCP 连接。 + ```bash AGENTDRIVE_SANDBOX_CONFIG=/absolute/path/sandbox.json \ target/release/agentdrived serve --config /absolute/path/agentdrive.json @@ -202,6 +204,8 @@ python3 -m unittest discover -s tests/sandbox -p 'test_*.py' 启动 Sandbox 时,已有分支挂载处于 `recovery_required` 或 `suspended` 状态会自动尝试恢复,继续使用原目录和持久化配置。恢复流程检查挂载所属用户、分支版本及本机目录状态;存在未完成的分支操作时,需要先在挂载管理中处理该操作。挂载失败会显示目录和具体原因,页面提供“恢复挂载”按钮,处理问题后点击即可重新恢复并继续启动 Ubuntu。无法确认本机挂载身份时保留 `recovery_required` 状态。 +未完成的挂载操作按照挂载 ID 和版本检查,已被新挂载替代的旧卸载记录会保留为历史记录。Linux 下,恢复流程会确认残留挂载的文件系统类型为 `fuse.agentdrive`、来源为当前挂载 ID,且目录访问返回 `ENOTCONN`,随后正常卸载并重新挂载。运行中的挂载和身份不匹配的挂载会保留;目录被占用时会显示具体错误,关闭占用后可以重试。 + ## Ubuntu GNOME 与 CSGClaw From 203d6e8ad5fbf9a998d401fac35217566649116f Mon Sep 17 00:00:00 2001 From: gatewayj <835269233@qq.com> Date: Sun, 15 Nov 2026 18:37:58 +0800 Subject: [PATCH 09/12] feat: vendor sandbox hypervisor and include desktop image viewer --- .gitignore | 2 + Cargo.toml | 2 +- Makefile | 6 +- THIRD_PARTY_NOTICES.md | 13 + .../90-cube-ubuntu-desktop.gschema.override | 1 + build/sandbox/Dockerfile | 5 +- build/sandbox/SOURCES.md | 11 + build/sandbox/build-image.sh | 6 +- build/sandbox/install-kernels.sh | 14 + build/sandbox/licenses/CubeSandbox-LICENSE | 468 + build/sandbox/mimeapps.list | 8 + build/sandbox/start-desktop.sh | 2 +- build/sandbox/template-manifest.py | 2 +- docs/sandbox.md | 14 +- runtime/sandbox-worker/Cargo.lock | 29 - runtime/sandbox-worker/Cargo.toml | 2 +- tests/sandbox/check_sources.py | 40 + tests/sandbox/network_probe.py | 22 +- tests/sandbox/playwright_probe.py | 9 +- tests/sandbox/test_install_kernels.py | 52 + .../.github/ISSUE_TEMPLATE/bug_report.md | 34 + vendor/cube-hypervisor/.github/dependabot.yml | 18 + .../.github/workflows/audit.yaml | 15 + .../.github/workflows/build.yaml | 58 + .../.github/workflows/dco.yaml | 18 + .../.github/workflows/docker-image.yaml | 59 + .../.github/workflows/fuzz-build.yaml | 29 + .../.github/workflows/quality.yaml | 104 + .../.github/workflows/release.yaml | 141 + vendor/cube-hypervisor/.gitignore | 7 + vendor/cube-hypervisor/.rustfmt.toml | 1 + vendor/cube-hypervisor/CODEOWNERS | 2 + vendor/cube-hypervisor/CODE_OF_CONDUCT.md | 73 + vendor/cube-hypervisor/CONTRIBUTING.md | 116 + vendor/cube-hypervisor/CREDITS.md | 26 + vendor/cube-hypervisor/Cargo.lock | 2875 ++++ vendor/cube-hypervisor/Cargo.toml | 117 + vendor/cube-hypervisor/Jenkinsfile | 348 + vendor/cube-hypervisor/LICENSE-APACHE | 202 + vendor/cube-hypervisor/LICENSE-BSD-3-Clause | 27 + vendor/cube-hypervisor/LICENSE-CUBESANDBOX | 468 + vendor/cube-hypervisor/MAINTAINERS.md | 7 + vendor/cube-hypervisor/Makefile | 4 + vendor/cube-hypervisor/README.agentdrive.md | 13 + vendor/cube-hypervisor/README.md | 346 + vendor/cube-hypervisor/UPSTREAM.json | 397 + vendor/cube-hypervisor/acpi_tables/Cargo.toml | 9 + vendor/cube-hypervisor/acpi_tables/src/aml.rs | 1894 +++ vendor/cube-hypervisor/acpi_tables/src/lib.rs | 12 + .../cube-hypervisor/acpi_tables/src/rsdp.rs | 68 + vendor/cube-hypervisor/acpi_tables/src/sdt.rs | 152 + vendor/cube-hypervisor/api_client/Cargo.toml | 8 + vendor/cube-hypervisor/api_client/src/lib.rs | 250 + vendor/cube-hypervisor/arch/Cargo.toml | 29 + .../cube-hypervisor/arch/src/aarch64/fdt.rs | 815 ++ .../arch/src/aarch64/layout.rs | 140 + .../cube-hypervisor/arch/src/aarch64/mod.rs | 240 + .../cube-hypervisor/arch/src/aarch64/regs.rs | 44 + .../cube-hypervisor/arch/src/aarch64/uefi.rs | 44 + vendor/cube-hypervisor/arch/src/lib.rs | 177 + .../arch/src/x86_64/cpuid_filter.rs | 599 + .../arch/src/x86_64/interrupts.rs | 39 + .../cube-hypervisor/arch/src/x86_64/layout.rs | 125 + vendor/cube-hypervisor/arch/src/x86_64/mod.rs | 1416 ++ .../cube-hypervisor/arch/src/x86_64/mpspec.rs | 112 + .../arch/src/x86_64/mptable.rs | 414 + .../cube-hypervisor/arch/src/x86_64/regs.rs | 212 + .../cube-hypervisor/arch/src/x86_64/smbios.rs | 321 + .../arch/src/x86_64/tdx/mod.rs | 453 + vendor/cube-hypervisor/block_util/Cargo.toml | 23 + .../block_util/src/async_io.rs | 146 + .../block_util/src/fixed_vhd_async.rs | 110 + .../block_util/src/fixed_vhd_sync.rs | 107 + vendor/cube-hypervisor/block_util/src/lib.rs | 1004 ++ .../block_util/src/qcow_sync.rs | 103 + .../block_util/src/raw_async.rs | 167 + .../block_util/src/raw_sync.rs | 352 + vendor/cube-hypervisor/block_util/src/vhd.rs | 221 + .../block_util/src/vhdx_sync.rs | 102 + vendor/cube-hypervisor/build.rs | 47 + vendor/cube-hypervisor/devices/Cargo.toml | 35 + vendor/cube-hypervisor/devices/src/acpi.rs | 231 + vendor/cube-hypervisor/devices/src/gic.rs | 169 + .../devices/src/interrupt_controller.rs | 63 + vendor/cube-hypervisor/devices/src/ioapic.rs | 435 + vendor/cube-hypervisor/devices/src/ivshmem.rs | 378 + .../devices/src/legacy/cmos.rs | 153 + .../devices/src/legacy/debug_port.rs | 86 + .../devices/src/legacy/fwdebug.rs | 43 + .../devices/src/legacy/gpio_pl061.rs | 456 + .../devices/src/legacy/i8042.rs | 45 + .../cube-hypervisor/devices/src/legacy/mod.rs | 39 + .../devices/src/legacy/rtc_pl031.rs | 584 + .../devices/src/legacy/serial.rs | 516 + .../devices/src/legacy/sys_ctrl.rs | 123 + .../devices/src/legacy/uart_pl011.rs | 534 + vendor/cube-hypervisor/devices/src/lib.rs | 98 + vendor/cube-hypervisor/devices/src/pvpanic.rs | 255 + vendor/cube-hypervisor/devices/src/tpm.rs | 470 + vendor/cube-hypervisor/docs/COPYING.docs | 399 + vendor/cube-hypervisor/docs/api.md | 383 + vendor/cube-hypervisor/docs/arm64.md | 152 + vendor/cube-hypervisor/docs/balloon.md | 76 + vendor/cube-hypervisor/docs/building.md | 82 + vendor/cube-hypervisor/docs/cpu.md | 211 + vendor/cube-hypervisor/docs/custom-image.md | 160 + vendor/cube-hypervisor/docs/debug-port.md | 61 + vendor/cube-hypervisor/docs/device_model.md | 217 + vendor/cube-hypervisor/docs/fs.md | 98 + vendor/cube-hypervisor/docs/fuzzing.md | 35 + vendor/cube-hypervisor/docs/gdb.md | 47 + vendor/cube-hypervisor/docs/hotplug.md | 250 + vendor/cube-hypervisor/docs/intel_sgx.md | 55 + vendor/cube-hypervisor/docs/intel_tdx.md | 123 + vendor/cube-hypervisor/docs/io_throttling.md | 44 + vendor/cube-hypervisor/docs/iommu.md | 268 + vendor/cube-hypervisor/docs/live_migration.md | 139 + vendor/cube-hypervisor/docs/logging.md | 43 + vendor/cube-hypervisor/docs/macvtap-bridge.md | 32 + vendor/cube-hypervisor/docs/memory.md | 581 + .../docs/performance_metrics.md | 29 + vendor/cube-hypervisor/docs/profiling.md | 64 + vendor/cube-hypervisor/docs/releases.md | 103 + vendor/cube-hypervisor/docs/seccomp.md | 68 + .../cube-hypervisor/docs/snapshot_restore.md | 124 + vendor/cube-hypervisor/docs/tpm.md | 122 + vendor/cube-hypervisor/docs/tracing.md | 42 + vendor/cube-hypervisor/docs/uefi.md | 39 + vendor/cube-hypervisor/docs/vdpa.md | 154 + vendor/cube-hypervisor/docs/vfio-user.md | 79 + vendor/cube-hypervisor/docs/vfio.md | 125 + .../docs/vhost-user-blk-testing.md | 122 + .../docs/vhost-user-net-testing.md | 145 + vendor/cube-hypervisor/docs/virtiofs-root.md | 92 + vendor/cube-hypervisor/docs/vsock.md | 84 + vendor/cube-hypervisor/docs/windows.md | 347 + .../cube-hypervisor/event_monitor/Cargo.toml | 10 + .../cube-hypervisor/event_monitor/src/lib.rs | 82 + .../cube-hypervisor/event_notifier/Cargo.toml | 10 + .../cube-hypervisor/event_notifier/src/lib.rs | 82 + vendor/cube-hypervisor/fix_lock.py | 28 + vendor/cube-hypervisor/fuzz/.gitignore | 4 + vendor/cube-hypervisor/fuzz/Cargo.toml | 113 + .../fuzz/fuzz_targets/balloon.rs | 153 + .../fuzz/fuzz_targets/block.rs | 129 + .../cube-hypervisor/fuzz/fuzz_targets/cmos.rs | 48 + .../fuzz/fuzz_targets/console.rs | 194 + .../fuzz/fuzz_targets/http_api.rs | 194 + .../fuzz/fuzz_targets/iommu.rs | 145 + .../cube-hypervisor/fuzz/fuzz_targets/mem.rs | 179 + .../cube-hypervisor/fuzz/fuzz_targets/pmem.rs | 151 + .../cube-hypervisor/fuzz/fuzz_targets/qcow.rs | 49 + .../cube-hypervisor/fuzz/fuzz_targets/rng.rs | 135 + .../fuzz/fuzz_targets/serial.rs | 73 + .../cube-hypervisor/fuzz/fuzz_targets/vhdx.rs | 53 + .../fuzz/fuzz_targets/watchdog.rs | 100 + vendor/cube-hypervisor/hypervisor/Cargo.toml | 35 + .../hypervisor/src/arch/aarch64/gic.rs | 68 + .../hypervisor/src/arch/aarch64/mod.rs | 3 + .../hypervisor/src/arch/emulator/mod.rs | 157 + .../hypervisor/src/arch/mod.rs | 23 + .../src/arch/x86/emulator/instructions/cmp.rs | 331 + .../src/arch/x86/emulator/instructions/mod.rs | 153 + .../src/arch/x86/emulator/instructions/mov.rs | 620 + .../arch/x86/emulator/instructions/movs.rs | 145 + .../src/arch/x86/emulator/instructions/or.rs | 83 + .../hypervisor/src/arch/x86/emulator/mod.rs | 860 ++ .../hypervisor/src/arch/x86/gdt.rs | 141 + .../hypervisor/src/arch/x86/mod.rs | 403 + .../hypervisor/src/arch/x86/msr_index.rs | 543 + .../hypervisor/src/arch/x86/regs.rs | 34 + vendor/cube-hypervisor/hypervisor/src/cpu.rs | 429 + .../cube-hypervisor/hypervisor/src/device.rs | 28 + .../hypervisor/src/hypervisor.rs | 136 + .../src/kvm/aarch64/gic/dist_regs.rs | 192 + .../src/kvm/aarch64/gic/icc_regs.rs | 212 + .../hypervisor/src/kvm/aarch64/gic/mod.rs | 603 + .../src/kvm/aarch64/gic/redist_regs.rs | 226 + .../hypervisor/src/kvm/aarch64/mod.rs | 122 + .../cube-hypervisor/hypervisor/src/kvm/mod.rs | 2403 ++++ .../hypervisor/src/kvm/x86_64/mod.rs | 343 + vendor/cube-hypervisor/hypervisor/src/lib.rs | 187 + .../hypervisor/src/mshv/mod.rs | 1208 ++ .../hypervisor/src/mshv/x86_64/mod.rs | 319 + vendor/cube-hypervisor/hypervisor/src/vm.rs | 361 + vendor/cube-hypervisor/log_json/Cargo.toml | 10 + vendor/cube-hypervisor/log_json/src/lib.rs | 57 + vendor/cube-hypervisor/logging/Cargo.toml | 12 + vendor/cube-hypervisor/logging/src/lib.rs | 279 + vendor/cube-hypervisor/net_gen/Cargo.toml | 8 + vendor/cube-hypervisor/net_gen/src/if_tun.rs | 326 + vendor/cube-hypervisor/net_gen/src/iff.rs | 1226 ++ vendor/cube-hypervisor/net_gen/src/inn.rs | 294 + vendor/cube-hypervisor/net_gen/src/lib.rs | 63 + vendor/cube-hypervisor/net_gen/src/sockios.rs | 95 + vendor/cube-hypervisor/net_util/Cargo.toml | 26 + .../net_util/src/ctrl_queue.rs | 177 + vendor/cube-hypervisor/net_util/src/lib.rs | 204 + vendor/cube-hypervisor/net_util/src/mac.rs | 195 + .../cube-hypervisor/net_util/src/open_tap.rs | 149 + .../net_util/src/queue_pair.rs | 566 + vendor/cube-hypervisor/net_util/src/tap.rs | 914 ++ .../cube-hypervisor/option_parser/Cargo.toml | 5 + .../cube-hypervisor/option_parser/src/lib.rs | 409 + vendor/cube-hypervisor/pci/Cargo.toml | 30 + vendor/cube-hypervisor/pci/src/bus.rs | 474 + .../cube-hypervisor/pci/src/configuration.rs | 1244 ++ vendor/cube-hypervisor/pci/src/device.rs | 131 + vendor/cube-hypervisor/pci/src/lib.rs | 183 + vendor/cube-hypervisor/pci/src/msi.rs | 298 + vendor/cube-hypervisor/pci/src/msix.rs | 579 + vendor/cube-hypervisor/pci/src/vfio.rs | 1720 +++ vendor/cube-hypervisor/pci/src/vfio_user.rs | 643 + .../performance-metrics/Cargo.toml | 18 + .../performance-metrics/build.rs | 26 + .../performance-metrics/src/main.rs | 606 + .../src/performance_tests.rs | 547 + vendor/cube-hypervisor/qcow/Cargo.toml | 16 + vendor/cube-hypervisor/qcow/src/qcow.rs | 2623 ++++ .../cube-hypervisor/qcow/src/qcow_raw_file.rs | 149 + vendor/cube-hypervisor/qcow/src/raw_file.rs | 353 + vendor/cube-hypervisor/qcow/src/refcount.rs | 252 + vendor/cube-hypervisor/qcow/src/vec_cache.rs | 185 + .../cube-hypervisor/rate_limiter/Cargo.toml | 9 + .../cube-hypervisor/rate_limiter/src/lib.rs | 918 ++ vendor/cube-hypervisor/release-notes.md | 2226 +++ vendor/cube-hypervisor/resources/Dockerfile | 133 + .../resources/linux-config-aarch64 | 3091 ++++ .../resources/linux-config-x86_64 | 3029 ++++ .../scripts/ch-trace-visualiser.py | 98 + .../scripts/check-image-compatability.sh | 215 + .../cube-hypervisor/scripts/common-aarch64.sh | 49 + .../scripts/create-cloud-init.sh | 9 + vendor/cube-hypervisor/scripts/dev_cli.sh | 665 + .../cube-hypervisor/scripts/prepare_vdpa.sh | 33 + .../scripts/run_integration_tests_aarch64.sh | 288 + .../run_integration_tests_live_migration.sh | 110 + .../run_integration_tests_rate_limiter.sh | 89 + .../scripts/run_integration_tests_sgx.sh | 33 + .../scripts/run_integration_tests_vfio.sh | 67 + .../run_integration_tests_windows_aarch64.sh | 60 + .../run_integration_tests_windows_x86_64.sh | 60 + .../scripts/run_integration_tests_x86_64.sh | 326 + vendor/cube-hypervisor/scripts/run_metrics.sh | 123 + .../scripts/run_openapi_tests.sh | 5 + .../cube-hypervisor/scripts/run_unit_tests.sh | 24 + .../cube-hypervisor/scripts/sha1sums-aarch64 | 8 + .../cube-hypervisor/scripts/sha1sums-x86_64 | 7 + vendor/cube-hypervisor/scripts/test-util.sh | 137 + ...windows-11-iot-enterprise-aarch64.raw.sha1 | 1 + .../cube-hypervisor/serial_buffer/Cargo.toml | 5 + .../cube-hypervisor/serial_buffer/src/lib.rs | 115 + vendor/cube-hypervisor/src/bin/ch-remote.rs | 787 ++ vendor/cube-hypervisor/src/bin/cube-cpuid.rs | 248 + vendor/cube-hypervisor/src/common.rs | 257 + vendor/cube-hypervisor/src/lib.rs | 359 + vendor/cube-hypervisor/src/main.rs | 1831 +++ vendor/cube-hypervisor/src/vmm_config.rs | 64 + .../test_data/cloud-init/ubuntu/meta-data | 2 + .../cloud-init/ubuntu/network-config | 26 + .../test_data/cloud-init/ubuntu/user-data | 76 + vendor/cube-hypervisor/test_infra/Cargo.toml | 16 + vendor/cube-hypervisor/test_infra/src/lib.rs | 1821 +++ vendor/cube-hypervisor/tests/integration.rs | 11745 ++++++++++++++++ vendor/cube-hypervisor/tpm/Cargo.toml | 14 + vendor/cube-hypervisor/tpm/src/emulator.rs | 461 + vendor/cube-hypervisor/tpm/src/lib.rs | 440 + vendor/cube-hypervisor/tpm/src/socket.rs | 144 + vendor/cube-hypervisor/tracer/Cargo.toml | 15 + vendor/cube-hypervisor/tracer/src/lib.rs | 18 + vendor/cube-hypervisor/tracer/src/tracer.rs | 174 + .../cube-hypervisor/tracer/src/tracer_noop.rs | 17 + vendor/cube-hypervisor/vfio_user/Cargo.toml | 19 + vendor/cube-hypervisor/vfio_user/src/lib.rs | 783 ++ vendor/cube-hypervisor/vhdx/Cargo.toml | 15 + vendor/cube-hypervisor/vhdx/src/lib.rs | 30 + vendor/cube-hypervisor/vhdx/src/vhdx.rs | 212 + vendor/cube-hypervisor/vhdx/src/vhdx_bat.rs | 104 + .../cube-hypervisor/vhdx/src/vhdx_header.rs | 483 + vendor/cube-hypervisor/vhdx/src/vhdx_io.rs | 224 + .../cube-hypervisor/vhdx/src/vhdx_metadata.rs | 319 + .../vhost_user_block/Cargo.toml | 24 + .../vhost_user_block/src/lib.rs | 556 + .../vhost_user_block/src/main.rs | 36 + .../cube-hypervisor/vhost_user_net/Cargo.toml | 22 + .../cube-hypervisor/vhost_user_net/src/lib.rs | 420 + .../vhost_user_net/src/main.rs | 33 + .../cube-hypervisor/virtio-devices/Cargo.toml | 40 + .../virtio-devices/src/balloon.rs | 950 ++ .../virtio-devices/src/block.rs | 912 ++ .../virtio-devices/src/console.rs | 821 ++ .../virtio-devices/src/device.rs | 396 + .../virtio-devices/src/epoll_helper.rs | 343 + .../cube-hypervisor/virtio-devices/src/fs.rs | 1368 ++ .../virtio-devices/src/iommu.rs | 1097 ++ .../cube-hypervisor/virtio-devices/src/lib.rs | 186 + .../cube-hypervisor/virtio-devices/src/mem.rs | 1008 ++ .../cube-hypervisor/virtio-devices/src/net.rs | 862 ++ .../virtio-devices/src/pmem.rs | 479 + .../cube-hypervisor/virtio-devices/src/rng.rs | 326 + .../virtio-devices/src/seccomp_filters.rs | 577 + .../virtio-devices/src/thread_helper.rs | 66 + .../virtio-devices/src/transport/mod.rs | 13 + .../src/transport/pci_common_config.rs | 414 + .../src/transport/pci_device.rs | 1254 ++ .../virtio-devices/src/vdpa.rs | 586 + .../virtio-devices/src/vhost_user/blk.rs | 399 + .../virtio-devices/src/vhost_user/fs.rs | 400 + .../virtio-devices/src/vhost_user/mod.rs | 537 + .../virtio-devices/src/vhost_user/net.rs | 435 + .../src/vhost_user/vu_common_ctrl.rs | 594 + .../src/vsock/csm/connection.rs | 1254 ++ .../virtio-devices/src/vsock/csm/mod.rs | 129 + .../virtio-devices/src/vsock/csm/txbuf.rs | 272 + .../virtio-devices/src/vsock/device.rs | 927 ++ .../virtio-devices/src/vsock/mod.rs | 396 + .../virtio-devices/src/vsock/packet.rs | 803 ++ .../virtio-devices/src/vsock/unix/mod.rs | 61 + .../virtio-devices/src/vsock/unix/muxer.rs | 2126 +++ .../src/vsock/unix/muxer_killq.rs | 140 + .../src/vsock/unix/muxer_rxq.rs | 145 + .../virtio-devices/src/vsock/unix/stream.rs | 222 + .../virtio-devices/src/watchdog.rs | 419 + vendor/cube-hypervisor/virtiofsd/.gitignore | 2 + .../cube-hypervisor/virtiofsd/.gitlab-ci.yml | 48 + .../virtiofsd/50-virtiofsd.json | 8 + .../cube-hypervisor/virtiofsd/CONTRIBUTING.md | 73 + vendor/cube-hypervisor/virtiofsd/Cargo.toml | 41 + .../cube-hypervisor/virtiofsd/LICENSE-APACHE | 202 + .../virtiofsd/LICENSE-BSD-3-Clause | 27 + vendor/cube-hypervisor/virtiofsd/README.md | 554 + .../virtiofsd/doc/xattr-mapping.md | 193 + .../docs/preserialization-storeonly-design.md | 388 + vendor/cube-hypervisor/virtiofsd/rustfmt.toml | 2 + .../virtiofsd/src/descriptor_utils.rs | 969 ++ .../virtiofsd/src/file_traits.rs | 161 + .../virtiofsd/src/filesystem.rs | 1306 ++ .../virtiofsd/src/fs_cache_req_handler.rs | 62 + vendor/cube-hypervisor/virtiofsd/src/fuse.rs | 1418 ++ vendor/cube-hypervisor/virtiofsd/src/idmap.rs | 130 + vendor/cube-hypervisor/virtiofsd/src/lib.rs | 91 + .../cube-hypervisor/virtiofsd/src/limits.rs | 86 + .../cube-hypervisor/virtiofsd/src/macros.rs | 34 + vendor/cube-hypervisor/virtiofsd/src/main.rs | 1462 ++ vendor/cube-hypervisor/virtiofsd/src/oslib.rs | 479 + .../virtiofsd/src/passthrough/credentials.rs | 174 + .../device_state/deserialization.rs | 695 + .../src/passthrough/device_state/mod.rs | 196 + .../device_state/preserialization.rs | 1781 +++ .../passthrough/device_state/serialization.rs | 357 + .../passthrough/device_state/serialized.rs | 121 + .../virtiofsd/src/passthrough/file_handle.rs | 199 + .../virtiofsd/src/passthrough/inode_store.rs | 599 + .../virtiofsd/src/passthrough/mod.rs | 2698 ++++ .../virtiofsd/src/passthrough/mount_fd.rs | 414 + .../virtiofsd/src/passthrough/read_only.rs | 475 + .../virtiofsd/src/passthrough/stat.rs | 152 + .../src/passthrough/stat/file_status.rs | 54 + .../virtiofsd/src/passthrough/util.rs | 180 + .../virtiofsd/src/passthrough/xattrmap.rs | 854 ++ .../cube-hypervisor/virtiofsd/src/read_dir.rs | 158 + .../cube-hypervisor/virtiofsd/src/sandbox.rs | 676 + .../cube-hypervisor/virtiofsd/src/seccomp.rs | 215 + .../cube-hypervisor/virtiofsd/src/server.rs | 2211 +++ vendor/cube-hypervisor/virtiofsd/src/util.rs | 190 + .../cube-hypervisor/vm-allocator/Cargo.toml | 12 + .../vm-allocator/src/address.rs | 403 + .../cube-hypervisor/vm-allocator/src/gsi.rs | 108 + .../cube-hypervisor/vm-allocator/src/lib.rs | 21 + .../vm-allocator/src/system.rs | 165 + vendor/cube-hypervisor/vm-device/Cargo.toml | 20 + vendor/cube-hypervisor/vm-device/src/bus.rs | 359 + .../vm-device/src/dma_mapping/mod.rs | 17 + .../vm-device/src/dma_mapping/vfio.rs | 73 + .../vm-device/src/interrupt/mod.rs | 156 + vendor/cube-hypervisor/vm-device/src/lib.rs | 62 + .../cube-hypervisor/vm-migration/Cargo.toml | 12 + .../cube-hypervisor/vm-migration/src/lib.rs | 337 + .../vm-migration/src/protocol.rs | 293 + vendor/cube-hypervisor/vm-virtio/Cargo.toml | 13 + .../vm-virtio/src/checked_descriptor.rs | 185 + vendor/cube-hypervisor/vm-virtio/src/lib.rs | 152 + vendor/cube-hypervisor/vm-virtio/src/queue.rs | 247 + vendor/cube-hypervisor/vmm/Cargo.toml | 65 + vendor/cube-hypervisor/vmm/src/acpi.rs | 868 ++ .../vmm/src/api/http/http_endpoint.rs | 220 + .../cube-hypervisor/vmm/src/api/http/mod.rs | 369 + vendor/cube-hypervisor/vmm/src/api/mod.rs | 710 + .../vmm/src/api/openapi/cloud-hypervisor.yaml | 1197 ++ vendor/cube-hypervisor/vmm/src/api/service.rs | 90 + vendor/cube-hypervisor/vmm/src/clone3.rs | 27 + vendor/cube-hypervisor/vmm/src/config.rs | 3975 ++++++ vendor/cube-hypervisor/vmm/src/coredump.rs | 346 + vendor/cube-hypervisor/vmm/src/cpu.rs | 2902 ++++ .../cube-hypervisor/vmm/src/device_manager.rs | 5098 +++++++ vendor/cube-hypervisor/vmm/src/device_tree.rs | 363 + vendor/cube-hypervisor/vmm/src/gdb.rs | 544 + vendor/cube-hypervisor/vmm/src/interrupt.rs | 303 + vendor/cube-hypervisor/vmm/src/lib.rs | 2839 ++++ .../cube-hypervisor/vmm/src/memory_manager.rs | 3205 +++++ vendor/cube-hypervisor/vmm/src/migration.rs | 188 + .../cube-hypervisor/vmm/src/pagemap_anon.rs | 565 + .../vmm/src/pagemap_anon_bench.rs | 178 + vendor/cube-hypervisor/vmm/src/pci_segment.rs | 426 + .../vmm/src/seccomp_filters.rs | 1004 ++ .../cube-hypervisor/vmm/src/serial_manager.rs | 325 + .../vmm/src/sigwinch_listener.rs | 149 + vendor/cube-hypervisor/vmm/src/soft_dirty.rs | 834 ++ vendor/cube-hypervisor/vmm/src/vm.rs | 3486 +++++ vendor/cube-hypervisor/vmm/src/vm_config.rs | 654 + vendor/cube-hypervisor/workflows/1check.sh | 40 + vendor/cube-hypervisor/workflows/2upload.sh | 33 + 412 files changed, 161875 insertions(+), 51 deletions(-) create mode 100644 THIRD_PARTY_NOTICES.md create mode 100644 build/sandbox/SOURCES.md create mode 100644 build/sandbox/licenses/CubeSandbox-LICENSE create mode 100644 build/sandbox/mimeapps.list create mode 100644 tests/sandbox/check_sources.py create mode 100644 tests/sandbox/test_install_kernels.py create mode 100644 vendor/cube-hypervisor/.github/ISSUE_TEMPLATE/bug_report.md create mode 100644 vendor/cube-hypervisor/.github/dependabot.yml create mode 100644 vendor/cube-hypervisor/.github/workflows/audit.yaml create mode 100644 vendor/cube-hypervisor/.github/workflows/build.yaml create mode 100644 vendor/cube-hypervisor/.github/workflows/dco.yaml create mode 100644 vendor/cube-hypervisor/.github/workflows/docker-image.yaml create mode 100644 vendor/cube-hypervisor/.github/workflows/fuzz-build.yaml create mode 100644 vendor/cube-hypervisor/.github/workflows/quality.yaml create mode 100644 vendor/cube-hypervisor/.github/workflows/release.yaml create mode 100644 vendor/cube-hypervisor/.gitignore create mode 100644 vendor/cube-hypervisor/.rustfmt.toml create mode 100644 vendor/cube-hypervisor/CODEOWNERS create mode 100644 vendor/cube-hypervisor/CODE_OF_CONDUCT.md create mode 100644 vendor/cube-hypervisor/CONTRIBUTING.md create mode 100644 vendor/cube-hypervisor/CREDITS.md create mode 100644 vendor/cube-hypervisor/Cargo.lock create mode 100644 vendor/cube-hypervisor/Cargo.toml create mode 100644 vendor/cube-hypervisor/Jenkinsfile create mode 100644 vendor/cube-hypervisor/LICENSE-APACHE create mode 100644 vendor/cube-hypervisor/LICENSE-BSD-3-Clause create mode 100644 vendor/cube-hypervisor/LICENSE-CUBESANDBOX create mode 100644 vendor/cube-hypervisor/MAINTAINERS.md create mode 100644 vendor/cube-hypervisor/Makefile create mode 100644 vendor/cube-hypervisor/README.agentdrive.md create mode 100644 vendor/cube-hypervisor/README.md create mode 100644 vendor/cube-hypervisor/UPSTREAM.json create mode 100644 vendor/cube-hypervisor/acpi_tables/Cargo.toml create mode 100644 vendor/cube-hypervisor/acpi_tables/src/aml.rs create mode 100644 vendor/cube-hypervisor/acpi_tables/src/lib.rs create mode 100644 vendor/cube-hypervisor/acpi_tables/src/rsdp.rs create mode 100644 vendor/cube-hypervisor/acpi_tables/src/sdt.rs create mode 100644 vendor/cube-hypervisor/api_client/Cargo.toml create mode 100644 vendor/cube-hypervisor/api_client/src/lib.rs create mode 100644 vendor/cube-hypervisor/arch/Cargo.toml create mode 100644 vendor/cube-hypervisor/arch/src/aarch64/fdt.rs create mode 100644 vendor/cube-hypervisor/arch/src/aarch64/layout.rs create mode 100644 vendor/cube-hypervisor/arch/src/aarch64/mod.rs create mode 100644 vendor/cube-hypervisor/arch/src/aarch64/regs.rs create mode 100644 vendor/cube-hypervisor/arch/src/aarch64/uefi.rs create mode 100644 vendor/cube-hypervisor/arch/src/lib.rs create mode 100644 vendor/cube-hypervisor/arch/src/x86_64/cpuid_filter.rs create mode 100644 vendor/cube-hypervisor/arch/src/x86_64/interrupts.rs create mode 100644 vendor/cube-hypervisor/arch/src/x86_64/layout.rs create mode 100644 vendor/cube-hypervisor/arch/src/x86_64/mod.rs create mode 100644 vendor/cube-hypervisor/arch/src/x86_64/mpspec.rs create mode 100644 vendor/cube-hypervisor/arch/src/x86_64/mptable.rs create mode 100644 vendor/cube-hypervisor/arch/src/x86_64/regs.rs create mode 100644 vendor/cube-hypervisor/arch/src/x86_64/smbios.rs create mode 100644 vendor/cube-hypervisor/arch/src/x86_64/tdx/mod.rs create mode 100644 vendor/cube-hypervisor/block_util/Cargo.toml create mode 100644 vendor/cube-hypervisor/block_util/src/async_io.rs create mode 100644 vendor/cube-hypervisor/block_util/src/fixed_vhd_async.rs create mode 100644 vendor/cube-hypervisor/block_util/src/fixed_vhd_sync.rs create mode 100644 vendor/cube-hypervisor/block_util/src/lib.rs create mode 100644 vendor/cube-hypervisor/block_util/src/qcow_sync.rs create mode 100644 vendor/cube-hypervisor/block_util/src/raw_async.rs create mode 100644 vendor/cube-hypervisor/block_util/src/raw_sync.rs create mode 100644 vendor/cube-hypervisor/block_util/src/vhd.rs create mode 100644 vendor/cube-hypervisor/block_util/src/vhdx_sync.rs create mode 100644 vendor/cube-hypervisor/build.rs create mode 100644 vendor/cube-hypervisor/devices/Cargo.toml create mode 100644 vendor/cube-hypervisor/devices/src/acpi.rs create mode 100644 vendor/cube-hypervisor/devices/src/gic.rs create mode 100644 vendor/cube-hypervisor/devices/src/interrupt_controller.rs create mode 100644 vendor/cube-hypervisor/devices/src/ioapic.rs create mode 100644 vendor/cube-hypervisor/devices/src/ivshmem.rs create mode 100644 vendor/cube-hypervisor/devices/src/legacy/cmos.rs create mode 100644 vendor/cube-hypervisor/devices/src/legacy/debug_port.rs create mode 100644 vendor/cube-hypervisor/devices/src/legacy/fwdebug.rs create mode 100644 vendor/cube-hypervisor/devices/src/legacy/gpio_pl061.rs create mode 100644 vendor/cube-hypervisor/devices/src/legacy/i8042.rs create mode 100644 vendor/cube-hypervisor/devices/src/legacy/mod.rs create mode 100644 vendor/cube-hypervisor/devices/src/legacy/rtc_pl031.rs create mode 100644 vendor/cube-hypervisor/devices/src/legacy/serial.rs create mode 100644 vendor/cube-hypervisor/devices/src/legacy/sys_ctrl.rs create mode 100644 vendor/cube-hypervisor/devices/src/legacy/uart_pl011.rs create mode 100644 vendor/cube-hypervisor/devices/src/lib.rs create mode 100644 vendor/cube-hypervisor/devices/src/pvpanic.rs create mode 100644 vendor/cube-hypervisor/devices/src/tpm.rs create mode 100644 vendor/cube-hypervisor/docs/COPYING.docs create mode 100644 vendor/cube-hypervisor/docs/api.md create mode 100644 vendor/cube-hypervisor/docs/arm64.md create mode 100644 vendor/cube-hypervisor/docs/balloon.md create mode 100644 vendor/cube-hypervisor/docs/building.md create mode 100644 vendor/cube-hypervisor/docs/cpu.md create mode 100644 vendor/cube-hypervisor/docs/custom-image.md create mode 100644 vendor/cube-hypervisor/docs/debug-port.md create mode 100644 vendor/cube-hypervisor/docs/device_model.md create mode 100644 vendor/cube-hypervisor/docs/fs.md create mode 100644 vendor/cube-hypervisor/docs/fuzzing.md create mode 100644 vendor/cube-hypervisor/docs/gdb.md create mode 100644 vendor/cube-hypervisor/docs/hotplug.md create mode 100644 vendor/cube-hypervisor/docs/intel_sgx.md create mode 100644 vendor/cube-hypervisor/docs/intel_tdx.md create mode 100644 vendor/cube-hypervisor/docs/io_throttling.md create mode 100644 vendor/cube-hypervisor/docs/iommu.md create mode 100644 vendor/cube-hypervisor/docs/live_migration.md create mode 100644 vendor/cube-hypervisor/docs/logging.md create mode 100644 vendor/cube-hypervisor/docs/macvtap-bridge.md create mode 100644 vendor/cube-hypervisor/docs/memory.md create mode 100644 vendor/cube-hypervisor/docs/performance_metrics.md create mode 100644 vendor/cube-hypervisor/docs/profiling.md create mode 100755 vendor/cube-hypervisor/docs/releases.md create mode 100644 vendor/cube-hypervisor/docs/seccomp.md create mode 100644 vendor/cube-hypervisor/docs/snapshot_restore.md create mode 100644 vendor/cube-hypervisor/docs/tpm.md create mode 100644 vendor/cube-hypervisor/docs/tracing.md create mode 100644 vendor/cube-hypervisor/docs/uefi.md create mode 100644 vendor/cube-hypervisor/docs/vdpa.md create mode 100644 vendor/cube-hypervisor/docs/vfio-user.md create mode 100644 vendor/cube-hypervisor/docs/vfio.md create mode 100644 vendor/cube-hypervisor/docs/vhost-user-blk-testing.md create mode 100644 vendor/cube-hypervisor/docs/vhost-user-net-testing.md create mode 100644 vendor/cube-hypervisor/docs/virtiofs-root.md create mode 100644 vendor/cube-hypervisor/docs/vsock.md create mode 100644 vendor/cube-hypervisor/docs/windows.md create mode 100644 vendor/cube-hypervisor/event_monitor/Cargo.toml create mode 100644 vendor/cube-hypervisor/event_monitor/src/lib.rs create mode 100644 vendor/cube-hypervisor/event_notifier/Cargo.toml create mode 100644 vendor/cube-hypervisor/event_notifier/src/lib.rs create mode 100644 vendor/cube-hypervisor/fix_lock.py create mode 100644 vendor/cube-hypervisor/fuzz/.gitignore create mode 100644 vendor/cube-hypervisor/fuzz/Cargo.toml create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/balloon.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/block.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/cmos.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/console.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/http_api.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/iommu.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/mem.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/pmem.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/qcow.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/rng.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/serial.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/vhdx.rs create mode 100644 vendor/cube-hypervisor/fuzz/fuzz_targets/watchdog.rs create mode 100644 vendor/cube-hypervisor/hypervisor/Cargo.toml create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/aarch64/gic.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/aarch64/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/emulator/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/cmp.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mov.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/movs.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/or.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/x86/gdt.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/x86/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/x86/msr_index.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/arch/x86/regs.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/cpu.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/device.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/hypervisor.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/dist_regs.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/icc_regs.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/redist_regs.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/kvm/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/kvm/x86_64/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/lib.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/mshv/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/mshv/x86_64/mod.rs create mode 100644 vendor/cube-hypervisor/hypervisor/src/vm.rs create mode 100644 vendor/cube-hypervisor/log_json/Cargo.toml create mode 100644 vendor/cube-hypervisor/log_json/src/lib.rs create mode 100644 vendor/cube-hypervisor/logging/Cargo.toml create mode 100644 vendor/cube-hypervisor/logging/src/lib.rs create mode 100644 vendor/cube-hypervisor/net_gen/Cargo.toml create mode 100644 vendor/cube-hypervisor/net_gen/src/if_tun.rs create mode 100644 vendor/cube-hypervisor/net_gen/src/iff.rs create mode 100644 vendor/cube-hypervisor/net_gen/src/inn.rs create mode 100644 vendor/cube-hypervisor/net_gen/src/lib.rs create mode 100644 vendor/cube-hypervisor/net_gen/src/sockios.rs create mode 100644 vendor/cube-hypervisor/net_util/Cargo.toml create mode 100644 vendor/cube-hypervisor/net_util/src/ctrl_queue.rs create mode 100644 vendor/cube-hypervisor/net_util/src/lib.rs create mode 100644 vendor/cube-hypervisor/net_util/src/mac.rs create mode 100644 vendor/cube-hypervisor/net_util/src/open_tap.rs create mode 100644 vendor/cube-hypervisor/net_util/src/queue_pair.rs create mode 100644 vendor/cube-hypervisor/net_util/src/tap.rs create mode 100644 vendor/cube-hypervisor/option_parser/Cargo.toml create mode 100644 vendor/cube-hypervisor/option_parser/src/lib.rs create mode 100644 vendor/cube-hypervisor/pci/Cargo.toml create mode 100644 vendor/cube-hypervisor/pci/src/bus.rs create mode 100644 vendor/cube-hypervisor/pci/src/configuration.rs create mode 100644 vendor/cube-hypervisor/pci/src/device.rs create mode 100644 vendor/cube-hypervisor/pci/src/lib.rs create mode 100644 vendor/cube-hypervisor/pci/src/msi.rs create mode 100644 vendor/cube-hypervisor/pci/src/msix.rs create mode 100644 vendor/cube-hypervisor/pci/src/vfio.rs create mode 100644 vendor/cube-hypervisor/pci/src/vfio_user.rs create mode 100644 vendor/cube-hypervisor/performance-metrics/Cargo.toml create mode 100644 vendor/cube-hypervisor/performance-metrics/build.rs create mode 100644 vendor/cube-hypervisor/performance-metrics/src/main.rs create mode 100644 vendor/cube-hypervisor/performance-metrics/src/performance_tests.rs create mode 100644 vendor/cube-hypervisor/qcow/Cargo.toml create mode 100644 vendor/cube-hypervisor/qcow/src/qcow.rs create mode 100644 vendor/cube-hypervisor/qcow/src/qcow_raw_file.rs create mode 100644 vendor/cube-hypervisor/qcow/src/raw_file.rs create mode 100644 vendor/cube-hypervisor/qcow/src/refcount.rs create mode 100644 vendor/cube-hypervisor/qcow/src/vec_cache.rs create mode 100644 vendor/cube-hypervisor/rate_limiter/Cargo.toml create mode 100644 vendor/cube-hypervisor/rate_limiter/src/lib.rs create mode 100644 vendor/cube-hypervisor/release-notes.md create mode 100644 vendor/cube-hypervisor/resources/Dockerfile create mode 100644 vendor/cube-hypervisor/resources/linux-config-aarch64 create mode 100644 vendor/cube-hypervisor/resources/linux-config-x86_64 create mode 100755 vendor/cube-hypervisor/scripts/ch-trace-visualiser.py create mode 100755 vendor/cube-hypervisor/scripts/check-image-compatability.sh create mode 100644 vendor/cube-hypervisor/scripts/common-aarch64.sh create mode 100755 vendor/cube-hypervisor/scripts/create-cloud-init.sh create mode 100755 vendor/cube-hypervisor/scripts/dev_cli.sh create mode 100755 vendor/cube-hypervisor/scripts/prepare_vdpa.sh create mode 100755 vendor/cube-hypervisor/scripts/run_integration_tests_aarch64.sh create mode 100755 vendor/cube-hypervisor/scripts/run_integration_tests_live_migration.sh create mode 100755 vendor/cube-hypervisor/scripts/run_integration_tests_rate_limiter.sh create mode 100755 vendor/cube-hypervisor/scripts/run_integration_tests_sgx.sh create mode 100755 vendor/cube-hypervisor/scripts/run_integration_tests_vfio.sh create mode 100755 vendor/cube-hypervisor/scripts/run_integration_tests_windows_aarch64.sh create mode 100755 vendor/cube-hypervisor/scripts/run_integration_tests_windows_x86_64.sh create mode 100755 vendor/cube-hypervisor/scripts/run_integration_tests_x86_64.sh create mode 100755 vendor/cube-hypervisor/scripts/run_metrics.sh create mode 100755 vendor/cube-hypervisor/scripts/run_openapi_tests.sh create mode 100755 vendor/cube-hypervisor/scripts/run_unit_tests.sh create mode 100644 vendor/cube-hypervisor/scripts/sha1sums-aarch64 create mode 100644 vendor/cube-hypervisor/scripts/sha1sums-x86_64 create mode 100644 vendor/cube-hypervisor/scripts/test-util.sh create mode 100644 vendor/cube-hypervisor/scripts/windows-11-iot-enterprise-aarch64.raw.sha1 create mode 100644 vendor/cube-hypervisor/serial_buffer/Cargo.toml create mode 100644 vendor/cube-hypervisor/serial_buffer/src/lib.rs create mode 100644 vendor/cube-hypervisor/src/bin/ch-remote.rs create mode 100644 vendor/cube-hypervisor/src/bin/cube-cpuid.rs create mode 100644 vendor/cube-hypervisor/src/common.rs create mode 100644 vendor/cube-hypervisor/src/lib.rs create mode 100644 vendor/cube-hypervisor/src/main.rs create mode 100644 vendor/cube-hypervisor/src/vmm_config.rs create mode 100644 vendor/cube-hypervisor/test_data/cloud-init/ubuntu/meta-data create mode 100644 vendor/cube-hypervisor/test_data/cloud-init/ubuntu/network-config create mode 100644 vendor/cube-hypervisor/test_data/cloud-init/ubuntu/user-data create mode 100644 vendor/cube-hypervisor/test_infra/Cargo.toml create mode 100644 vendor/cube-hypervisor/test_infra/src/lib.rs create mode 100644 vendor/cube-hypervisor/tests/integration.rs create mode 100644 vendor/cube-hypervisor/tpm/Cargo.toml create mode 100644 vendor/cube-hypervisor/tpm/src/emulator.rs create mode 100644 vendor/cube-hypervisor/tpm/src/lib.rs create mode 100644 vendor/cube-hypervisor/tpm/src/socket.rs create mode 100644 vendor/cube-hypervisor/tracer/Cargo.toml create mode 100644 vendor/cube-hypervisor/tracer/src/lib.rs create mode 100644 vendor/cube-hypervisor/tracer/src/tracer.rs create mode 100644 vendor/cube-hypervisor/tracer/src/tracer_noop.rs create mode 100644 vendor/cube-hypervisor/vfio_user/Cargo.toml create mode 100644 vendor/cube-hypervisor/vfio_user/src/lib.rs create mode 100644 vendor/cube-hypervisor/vhdx/Cargo.toml create mode 100644 vendor/cube-hypervisor/vhdx/src/lib.rs create mode 100644 vendor/cube-hypervisor/vhdx/src/vhdx.rs create mode 100644 vendor/cube-hypervisor/vhdx/src/vhdx_bat.rs create mode 100644 vendor/cube-hypervisor/vhdx/src/vhdx_header.rs create mode 100644 vendor/cube-hypervisor/vhdx/src/vhdx_io.rs create mode 100644 vendor/cube-hypervisor/vhdx/src/vhdx_metadata.rs create mode 100644 vendor/cube-hypervisor/vhost_user_block/Cargo.toml create mode 100644 vendor/cube-hypervisor/vhost_user_block/src/lib.rs create mode 100644 vendor/cube-hypervisor/vhost_user_block/src/main.rs create mode 100644 vendor/cube-hypervisor/vhost_user_net/Cargo.toml create mode 100644 vendor/cube-hypervisor/vhost_user_net/src/lib.rs create mode 100644 vendor/cube-hypervisor/vhost_user_net/src/main.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/Cargo.toml create mode 100644 vendor/cube-hypervisor/virtio-devices/src/balloon.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/block.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/console.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/device.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/epoll_helper.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/fs.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/iommu.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/lib.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/mem.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/net.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/pmem.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/rng.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/seccomp_filters.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/thread_helper.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/transport/mod.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/transport/pci_common_config.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/transport/pci_device.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vdpa.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vhost_user/blk.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vhost_user/fs.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vhost_user/mod.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vhost_user/net.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vhost_user/vu_common_ctrl.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/csm/connection.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/csm/mod.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/csm/txbuf.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/device.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/mod.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/packet.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/unix/mod.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_killq.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_rxq.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/vsock/unix/stream.rs create mode 100644 vendor/cube-hypervisor/virtio-devices/src/watchdog.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/.gitignore create mode 100644 vendor/cube-hypervisor/virtiofsd/.gitlab-ci.yml create mode 100644 vendor/cube-hypervisor/virtiofsd/50-virtiofsd.json create mode 100644 vendor/cube-hypervisor/virtiofsd/CONTRIBUTING.md create mode 100644 vendor/cube-hypervisor/virtiofsd/Cargo.toml create mode 100644 vendor/cube-hypervisor/virtiofsd/LICENSE-APACHE create mode 100644 vendor/cube-hypervisor/virtiofsd/LICENSE-BSD-3-Clause create mode 100644 vendor/cube-hypervisor/virtiofsd/README.md create mode 100644 vendor/cube-hypervisor/virtiofsd/doc/xattr-mapping.md create mode 100644 vendor/cube-hypervisor/virtiofsd/docs/preserialization-storeonly-design.md create mode 100644 vendor/cube-hypervisor/virtiofsd/rustfmt.toml create mode 100644 vendor/cube-hypervisor/virtiofsd/src/descriptor_utils.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/file_traits.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/filesystem.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/fs_cache_req_handler.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/fuse.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/idmap.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/lib.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/limits.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/macros.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/main.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/oslib.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/credentials.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/deserialization.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/mod.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/preserialization.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialization.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialized.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/file_handle.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/inode_store.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/mod.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/mount_fd.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/read_only.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/stat.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/stat/file_status.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/util.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/passthrough/xattrmap.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/read_dir.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/sandbox.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/seccomp.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/server.rs create mode 100644 vendor/cube-hypervisor/virtiofsd/src/util.rs create mode 100644 vendor/cube-hypervisor/vm-allocator/Cargo.toml create mode 100644 vendor/cube-hypervisor/vm-allocator/src/address.rs create mode 100644 vendor/cube-hypervisor/vm-allocator/src/gsi.rs create mode 100644 vendor/cube-hypervisor/vm-allocator/src/lib.rs create mode 100644 vendor/cube-hypervisor/vm-allocator/src/system.rs create mode 100644 vendor/cube-hypervisor/vm-device/Cargo.toml create mode 100644 vendor/cube-hypervisor/vm-device/src/bus.rs create mode 100644 vendor/cube-hypervisor/vm-device/src/dma_mapping/mod.rs create mode 100644 vendor/cube-hypervisor/vm-device/src/dma_mapping/vfio.rs create mode 100644 vendor/cube-hypervisor/vm-device/src/interrupt/mod.rs create mode 100644 vendor/cube-hypervisor/vm-device/src/lib.rs create mode 100644 vendor/cube-hypervisor/vm-migration/Cargo.toml create mode 100644 vendor/cube-hypervisor/vm-migration/src/lib.rs create mode 100644 vendor/cube-hypervisor/vm-migration/src/protocol.rs create mode 100644 vendor/cube-hypervisor/vm-virtio/Cargo.toml create mode 100644 vendor/cube-hypervisor/vm-virtio/src/checked_descriptor.rs create mode 100644 vendor/cube-hypervisor/vm-virtio/src/lib.rs create mode 100644 vendor/cube-hypervisor/vm-virtio/src/queue.rs create mode 100644 vendor/cube-hypervisor/vmm/Cargo.toml create mode 100644 vendor/cube-hypervisor/vmm/src/acpi.rs create mode 100644 vendor/cube-hypervisor/vmm/src/api/http/http_endpoint.rs create mode 100644 vendor/cube-hypervisor/vmm/src/api/http/mod.rs create mode 100644 vendor/cube-hypervisor/vmm/src/api/mod.rs create mode 100644 vendor/cube-hypervisor/vmm/src/api/openapi/cloud-hypervisor.yaml create mode 100644 vendor/cube-hypervisor/vmm/src/api/service.rs create mode 100644 vendor/cube-hypervisor/vmm/src/clone3.rs create mode 100644 vendor/cube-hypervisor/vmm/src/config.rs create mode 100644 vendor/cube-hypervisor/vmm/src/coredump.rs create mode 100644 vendor/cube-hypervisor/vmm/src/cpu.rs create mode 100644 vendor/cube-hypervisor/vmm/src/device_manager.rs create mode 100644 vendor/cube-hypervisor/vmm/src/device_tree.rs create mode 100644 vendor/cube-hypervisor/vmm/src/gdb.rs create mode 100644 vendor/cube-hypervisor/vmm/src/interrupt.rs create mode 100644 vendor/cube-hypervisor/vmm/src/lib.rs create mode 100644 vendor/cube-hypervisor/vmm/src/memory_manager.rs create mode 100644 vendor/cube-hypervisor/vmm/src/migration.rs create mode 100644 vendor/cube-hypervisor/vmm/src/pagemap_anon.rs create mode 100644 vendor/cube-hypervisor/vmm/src/pagemap_anon_bench.rs create mode 100644 vendor/cube-hypervisor/vmm/src/pci_segment.rs create mode 100644 vendor/cube-hypervisor/vmm/src/seccomp_filters.rs create mode 100644 vendor/cube-hypervisor/vmm/src/serial_manager.rs create mode 100644 vendor/cube-hypervisor/vmm/src/sigwinch_listener.rs create mode 100644 vendor/cube-hypervisor/vmm/src/soft_dirty.rs create mode 100644 vendor/cube-hypervisor/vmm/src/vm.rs create mode 100644 vendor/cube-hypervisor/vmm/src/vm_config.rs create mode 100755 vendor/cube-hypervisor/workflows/1check.sh create mode 100755 vendor/cube-hypervisor/workflows/2upload.sh diff --git a/.gitignore b/.gitignore index 23ad557..59af0fc 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ /vendor/winfsp_wrs/target/ /vendor/winfsp_wrs/Cargo.lock +/vendor/cube-hypervisor/target/ +/runtime/sandbox-worker/target/ /tests/dashboard/node_modules/ /build/sandbox/mcp/node_modules/ diff --git a/Cargo.toml b/Cargo.toml index 8e2a7a0..d31afca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = ["apps/*", "crates/*"] resolver = "3" -exclude = ["vendor/winfsp_wrs"] +exclude = ["vendor/winfsp_wrs", "vendor/cube-hypervisor"] [workspace.package] version = "0.1.0" diff --git a/Makefile b/Makefile index a9dfe0a..1d03f97 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ TARGETS ?= --all-targets FEATURES ?= CARGO_FLAGS = --locked --workspace $(if $(FEATURES),--features $(FEATURES),) -.PHONY: all build check test fmt fmt-check clippy architecture verify clean +.PHONY: all build check test fmt fmt-check clippy architecture verify sandbox-verify clean all: build build: +$(CARGO) build $(CARGO_FLAGS) @@ -21,5 +21,9 @@ clippy: architecture: $(PYTHON) tests/architecture.py verify: fmt-check clippy test architecture +sandbox-verify: + $(PYTHON) tests/sandbox/check_sources.py + +$(CARGO) check --locked --manifest-path runtime/sandbox-worker/Cargo.toml + $(PYTHON) -m unittest discover -s tests/sandbox -p 'test_*.py' clean: $(CARGO) clean diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md new file mode 100644 index 0000000..3e0595a --- /dev/null +++ b/THIRD_PARTY_NOTICES.md @@ -0,0 +1,13 @@ +# Sandbox 第三方代码说明 + +AgentDrive 的 Sandbox worker 包含 TencentCloud/CubeSandbox 的 hypervisor 源码,固定来源为 commit `4411c96ba8658a959de34693f54f4918db2a432f`。代码存放在 `vendor/cube-hypervisor/`,该目录保留 Cloud Hypervisor、Tencent、rust-vmm、virtiofsd 及其他贡献者的原有版权声明。 + +- 来源记录和完整文件校验值:`vendor/cube-hypervisor/UPSTREAM.json`。 +- CubeSandbox 许可证:`vendor/cube-hypervisor/LICENSE-CUBESANDBOX`。 +- Hypervisor 许可证:`vendor/cube-hypervisor/LICENSE-APACHE`、`LICENSE-BSD-3-Clause`。 +- virtiofsd 许可证:`vendor/cube-hypervisor/virtiofsd/LICENSE-APACHE`、`LICENSE-BSD-3-Clause`。 +- 原文件中的单独声明继续适用。 + +Ubuntu 桌面模板中的部分配置和启动文件来源于同一 commit 的 `examples/ubuntu-desktop/`。具体文件与 AgentDrive 修改记录见 `build/sandbox/SOURCES.md`,许可证见 `build/sandbox/licenses/CubeSandbox-LICENSE`。 + +分发 Sandbox worker 时,应同时提供本说明、上述适用许可证和原有归属声明。分发 Ubuntu 模板及 Linux 内核时,还需要按照其中各组件的许可证提供相应材料。Ubuntu 软件包自带的版权文件保留在模板的 `/usr/share/doc/` 中。 diff --git a/build/sandbox/90-cube-ubuntu-desktop.gschema.override b/build/sandbox/90-cube-ubuntu-desktop.gschema.override index 92cd612..e4e852c 100644 --- a/build/sandbox/90-cube-ubuntu-desktop.gschema.override +++ b/build/sandbox/90-cube-ubuntu-desktop.gschema.override @@ -1,3 +1,4 @@ +# AgentDrive 修改及来源见 SOURCES.md。 # Only pin apps that this image actually ships. ubuntu-settings defaults # include Firefox snap / Thunderbird / LibreOffice / ubiquity, which show # up as broken dock icons. diff --git a/build/sandbox/Dockerfile b/build/sandbox/Dockerfile index 40e836b..43bef88 100644 --- a/build/sandbox/Dockerfile +++ b/build/sandbox/Dockerfile @@ -1,3 +1,4 @@ +# AgentDrive 修改及来源见 SOURCES.md。 ARG CUBE_BASE_IMAGE=ghcr.io/tencentcloud/cubesandbox-base:2026.16 FROM node:22-bookworm-slim@sha256:e9f565d32859f0cbfb52125eafe8be39390f24dac302dc3cdf566e8ba3ab21ee AS mcp-build WORKDIR /opt/agentdrive-mcp @@ -110,7 +111,7 @@ RUN chmod 755 /usr/local/bin/guest-init.sh /usr/local/bin/guest-helper.py RUN apt-get -o Acquire::Retries=3 update \ && apt-get -y --no-install-recommends install \ - fonts-noto-cjk fonts-noto-color-emoji \ + fonts-noto-cjk fonts-noto-color-emoji eog \ libnss3 libnspr4 libasound2 libgbm1 libcups2 libatk-bridge2.0-0 libxkbcommon0 \ && rm -rf /var/lib/apt/lists/* @@ -123,6 +124,7 @@ RUN python3 /tmp/chromium-install/install-chromium.py /tmp/chromium.zip /tmp/chr COPY chromium /usr/local/bin/chromium COPY chromium.desktop /usr/share/applications/chromium.desktop COPY 64-agentdrive-fonts.conf /etc/fonts/conf.d/64-agentdrive-fonts.conf +COPY mimeapps.list /etc/xdg/mimeapps.list RUN chmod 755 /usr/local/bin/chromium \ && update-desktop-database /usr/share/applications \ && fc-cache -f @@ -132,4 +134,5 @@ COPY --from=mcp-build /opt/agentdrive-mcp /opt/agentdrive-mcp COPY mcp/gateway.mjs mcp/providers.json mcp/playwright.json /opt/agentdrive-mcp/ COPY browser-session.py /usr/local/bin/browser-session.py COPY template-manifest.py /usr/local/bin/template-manifest.py +COPY SOURCES.md licenses/ /usr/share/doc/agentdrive-sandbox/ RUN python3 /usr/local/bin/template-manifest.py "${COMPUTER_USE_VERSION}" > /opt/agentdrive-template.json diff --git a/build/sandbox/SOURCES.md b/build/sandbox/SOURCES.md new file mode 100644 index 0000000..533ff52 --- /dev/null +++ b/build/sandbox/SOURCES.md @@ -0,0 +1,11 @@ +# Sandbox 模板来源 + +基础镜像:`ghcr.io/tencentcloud/cubesandbox-base:2026.16`,Ubuntu 22.04,保留预装的 envd 及 CubeSandbox 启动文件。 + +桌面文件来自 TencentCloud/CubeSandbox commit `4411c96ba8658a959de34693f54f4918db2a432f` 的 `examples/ubuntu-desktop/`。`xorg.conf`、`Xwrapper.config`、`novnc-index.html` 保留原内容;AgentDrive 修改了 `Dockerfile`、`start-desktop.sh` 和 `90-cube-ubuntu-desktop.gschema.override`,增加浏览器、图片查看器、字体、统一 MCP 和桌面设置。修改记录日期:2026-11-15。许可证见 `CubeSandbox-LICENSE`(源码目录中位于 `licenses/`)。 + +来源:https://github.com/TencentCloud/CubeSandbox/tree/4411c96ba8658a959de34693f54f4918db2a432f/examples/ubuntu-desktop 。原有版权和归属声明继续保留。Ubuntu 软件包的许可证文件位于 `/usr/share/doc//copyright`。 + +AgentDrive 预装程序的版本和校验值见 `Dockerfile`、`chromium.json`、`mcp/package-lock.json` 和 `assistant/Dockerfile`。CSGClaw 使用 v0.10.0 发布包。模板保留程序归档中的许可证文件。 + +运行入口为 AgentDrive 的 `guest-init.sh`。Linux 内核由部署者提供,`kernels.json` 保存 KVM/PVM 内核文件的 SHA-256。对应源码、构建配置和许可证资料应随部署产物保存。 diff --git a/build/sandbox/build-image.sh b/build/sandbox/build-image.sh index 77c4264..f7ed2db 100755 --- a/build/sandbox/build-image.sh +++ b/build/sandbox/build-image.sh @@ -7,7 +7,11 @@ if [[ -e "$output" ]]; then echo 'Output directory must not exist' >&2; exit 1; mkdir -p -- "$output" output=$(realpath -- "$output") bash "$source_dir/install-kernels.sh" "$kernel" "$output" "${@:3}" -docker build --build-arg "CUBE_BASE_IMAGE=${CUBE_BASE_IMAGE:-ghcr.io/tencentcloud/cubesandbox-base:2026.16}" -t agentdrive-ubuntu:local "$source_dir" +build_args=(--build-arg "CUBE_BASE_IMAGE=${CUBE_BASE_IMAGE:-ghcr.io/tencentcloud/cubesandbox-base:2026.16}") +if [[ -n "${CHROMIUM_ARCHIVE:-}" ]]; then + build_args+=(--build-arg "CHROMIUM_ARCHIVE=$CHROMIUM_ARCHIVE") +fi +docker build "${build_args[@]}" -t agentdrive-ubuntu:local "$source_dir" container=$(docker create agentdrive-ubuntu:local) trap 'docker rm -f "$container" >/dev/null 2>&1 || true' EXIT docker cp -L "$container:/usr/share/novnc" "$output/novnc" diff --git a/build/sandbox/install-kernels.sh b/build/sandbox/install-kernels.sh index dae6634..734df0f 100644 --- a/build/sandbox/install-kernels.sh +++ b/build/sandbox/install-kernels.sh @@ -17,3 +17,17 @@ cp --reflink=auto -- "$kernel" "$output/vmlinux-bm" if [[ -f "$pvm" ]]; then cp --dereference --reflink=auto -- "$pvm" "$output/vmlinux-pvm" fi +python3 - "$output" <<'PY' +import hashlib +import json +import pathlib +import sys + +directory = pathlib.Path(sys.argv[1]) +kernels = {} +for mode, name in [("kvm", "vmlinux-bm"), ("pvm", "vmlinux-pvm")]: + path = directory / name + if path.is_file(): + kernels[mode] = {"file": name, "sha256": hashlib.sha256(path.read_bytes()).hexdigest()} +(directory / "kernels.json").write_text(json.dumps(kernels, indent=2) + "\n") +PY diff --git a/build/sandbox/licenses/CubeSandbox-LICENSE b/build/sandbox/licenses/CubeSandbox-LICENSE new file mode 100644 index 0000000..b6aa16c --- /dev/null +++ b/build/sandbox/licenses/CubeSandbox-LICENSE @@ -0,0 +1,468 @@ +Tencent is pleased to support the open source community by making Cube Sandbox available. + +Copyright (C) 2026 Tencent. All rights reserved. + +The open-source software included in this distribution may have been modified by Tencent (“Tencent Modifications”). All Tencent Modifications are Copyright (C) Tencent. + +Cube Sandbox is licensed under the Apache-2.0 except for the third-party components listed below. + + +Terms of the Apache-2.0: +-------------------------------------------------------------------- +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +Dependencies and Licenses: + +This open-source project, Cube Sandbox, builds upon the following open-source software components, each of which remains licensed under its original license. Certain software may include modifications made by Tencent (“Tencent Modifications”), which are Copyright (C) Tencent. + +In case you believe there have been errors in the attribution below, you may submit the concerns to us for review and correction. + + +Open Source Software Licensed under the Apache-2.0 +-------------------------------------------------------------------- +1. kata-containers +Copyright (c) kata-containers original author and authors +Please note this software has been modified by Tencent in this distribution. + +2. cloud-hypervisor +Copyright (c) cloud-hypervisor original author and authors +Please note this software has been modified by Tencent in this distribution. + + +Terms of the Apache-2.0 +-------------------------------------------------------------------- +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + +Open Source Software Licensed under the BSD-2-Clause +-------------------------------------------------------------------- +1. cilium/bpf +Copyright Authors of Cilium. All rights reserved. +cilium/bpf is made available under a dual license: BSD-2-Clause OR GPL-2.0. We have elected to use it under the terms of BSD-2-Clause. + + +Terms of the BSD-2-Clause +-------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/build/sandbox/mimeapps.list b/build/sandbox/mimeapps.list new file mode 100644 index 0000000..494b2c8 --- /dev/null +++ b/build/sandbox/mimeapps.list @@ -0,0 +1,8 @@ +[Default Applications] +image/png=org.gnome.eog.desktop; +image/jpeg=org.gnome.eog.desktop; +image/gif=org.gnome.eog.desktop; +image/bmp=org.gnome.eog.desktop; +image/tiff=org.gnome.eog.desktop; +image/svg+xml=org.gnome.eog.desktop; +image/x-icon=org.gnome.eog.desktop; diff --git a/build/sandbox/start-desktop.sh b/build/sandbox/start-desktop.sh index bc2e5ea..87fe196 100644 --- a/build/sandbox/start-desktop.sh +++ b/build/sandbox/start-desktop.sh @@ -1,6 +1,6 @@ #!/bin/bash # Start dummy Xorg (or Xvfb fallback), Ubuntu GNOME, x11vnc, and noVNC. -# Foreground process is this script so cube-entrypoint / docker stop can signal it. +# 来源和本地修改记录见 SOURCES.md;前台进程负责处理停止信号。 set -euo pipefail DISPLAY_NUM="${DISPLAY:-:0}" diff --git a/build/sandbox/template-manifest.py b/build/sandbox/template-manifest.py index 635a151..f0e38a2 100644 --- a/build/sandbox/template-manifest.py +++ b/build/sandbox/template-manifest.py @@ -28,7 +28,7 @@ def package(name): "tools": ["computer-use-linux " + sys.argv[1], "Playwright MCP " + json.load(open('/opt/agentdrive-mcp/node_modules/@playwright/mcp/package.json'))['version'], "Node.js " + command('node', '--version'), - *[package(name) for name in ["gnome-terminal", "nautilus", "gedit", "xdg-utils", "xclip"]]], + *[package(name) for name in ["gnome-terminal", "nautilus", "gedit", "eog", "xdg-utils", "xclip"]]], "fonts": [package(name) for name in ["fonts-noto-cjk", "fonts-noto-color-emoji"]], "locale": os.environ.get("LANG", "C.UTF-8"), "workspace_path": "/workspace", diff --git a/docs/sandbox.md b/docs/sandbox.md index 3c05659..5f619b5 100644 --- a/docs/sandbox.md +++ b/docs/sandbox.md @@ -14,6 +14,8 @@ CSGClaw 的登录回调使用当前实例的 `csgclaw.<工作区>.<分支>.local 当前分支自动挂载到 Ubuntu 的 `/workspace`。桌面上的 `AgentDrive` 文件夹入口指向该目录。在终端执行 `cd /workspace` 即可操作当前分支文件。`/home/user` 是 Ubuntu 的个人目录,需要进入分支的文件应保存到 `/workspace`。实例始终绑定创建时的分支,在网页选择其他分支不会改变已有实例的挂载。 +模板预装 GNOME Image Viewer(`eog`)。在文件管理器中双击 PNG、JPEG 等图片,或执行 `xdg-open /workspace/example.png`,即可在桌面查看图片。默认打开方式写入 `/etc/xdg/mimeapps.list`;用户可以在文件属性中选择其他应用。已有实例保留自己的系统磁盘,需要在实例中安装 `eog` 或重新创建实例才能获得新增程序。 + ## Agent 与 MCP 模板安装 `xdg-utils`,桌面启动时为用户配置 Chromium 为 HTTP、HTTPS 和 HTML 的默认应用。在桌面终端执行 `xdg-open https://example.com` 即可打开网页。 @@ -70,15 +72,21 @@ cargo build --release -p agentdrived -p agentdrive cargo build --release --locked --manifest-path runtime/sandbox-worker/Cargo.toml ``` -worker 使用 CubeSandbox v0.7.3 对应 commit,并固定 linux-loader 0.13.0。Ubuntu 模板安装 computer-use-linux 0.7.53,下载文件使用 SHA-256 验证。Playwright MCP 固定为 0.0.85,MCP SDK 固定为 1.32.2;Node.js 构建镜像固定 digest,npm 依赖通过 package-lock.json 和 npm ci 安装。 +worker 使用 `vendor/cube-hypervisor` 中的本地源码,通过独立 Cargo workspace 构建。源码来自 CubeSandbox commit `4411c96ba8658a959de34693f54f4918db2a432f`,原文件、PVM/KVM 支持及内置 virtio-fs 实现均保留。来源、文件校验值和许可证位于该目录,`make sandbox-verify` 检查来源记录、worker 编译和模板相关测试。linux-loader 固定为 0.13.0。 + +Ubuntu 模板继续基于腾讯的 `ghcr.io/tencentcloud/cubesandbox-base:2026.16` 镜像。使用 `CUBE_BASE_IMAGE` 可以指定同一镜像的内部镜像仓库地址。桌面文件来源和修改记录见 `build/sandbox/SOURCES.md`,该说明及上游许可证随模板安装到 `/usr/share/doc/agentdrive-sandbox/`。 -准备 CubeSandbox v0.7.3 的兼容 `vmlinux`,使用下列命令构建模板。输出目录需要尚未存在: +Ubuntu 模板安装 computer-use-linux 0.7.53,下载文件使用 SHA-256 验证。Playwright MCP 固定为 0.0.85,MCP SDK 固定为 1.32.2;Node.js 构建镜像固定 digest,npm 依赖通过 package-lock.json 和 npm ci 安装。 + +准备与导入的 hypervisor 兼容的 KVM 内核,以及需要使用 PVM 时对应的内核。使用下列命令构建模板,输出目录需要尚未存在: ```bash build/sandbox/build-image.sh /absolute/path/template /absolute/path/vmlinux ``` -桌面构建文件参考 [CubeSandbox ubuntu-desktop](https://github.com/TencentCloud/CubeSandbox/tree/v0.7.3/examples/ubuntu-desktop)。系统磁盘直接启动 guest-init.sh,guest helper 通过 vsock 提供桌面连接、截图检查、文件刷新和 MCP 标准输入输出。 +构建脚本生成 `kernels.json`,记录 KVM/PVM 内核文件的 SHA-256;内核源码、配置及许可证资料需要随部署产物保存。系统磁盘直接启动 guest-init.sh,guest helper 通过 vsock 提供桌面连接、截图检查、文件刷新和 MCP 标准输入输出。 + +CSGClaw 模板通过 `build/sandbox/build-assistant-image.sh` 在本地构建的 `agentdrive-ubuntu:local` 上增加助手。先完成桌面模板构建,再执行助手构建脚本。更新部署时切换模板路径和版本;已有实例保留原系统磁盘,新建实例使用新模板。替换 worker 后需要重启服务及实例。 ## 启动配置 diff --git a/runtime/sandbox-worker/Cargo.lock b/runtime/sandbox-worker/Cargo.lock index 71e0fba..68c9abc 100644 --- a/runtime/sandbox-worker/Cargo.lock +++ b/runtime/sandbox-worker/Cargo.lock @@ -5,7 +5,6 @@ version = 4 [[package]] name = "acpi_tables" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "vm-memory 0.16.2", "zerocopy 0.6.6", @@ -112,7 +111,6 @@ checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" [[package]] name = "api_client" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "vmm-sys-util 0.12.1", ] @@ -129,7 +127,6 @@ dependencies = [ [[package]] name = "arch" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "anyhow", "byteorder", @@ -205,7 +202,6 @@ checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" [[package]] name = "block_util" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "io-uring", "libc", @@ -393,7 +389,6 @@ checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" [[package]] name = "cube-hypervisor" version = "28.0.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "anyhow", "api_client", @@ -502,7 +497,6 @@ checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e" [[package]] name = "devices" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "acpi_tables", "anyhow", @@ -593,7 +587,6 @@ dependencies = [ [[package]] name = "event_monitor" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "libc", "serde", @@ -603,7 +596,6 @@ dependencies = [ [[package]] name = "event_notifier" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "lazy_static", "log", @@ -799,7 +791,6 @@ checksum = "15cdd26707701c53297e2fa6afb323d55fbc1d0810c3aec078ae3ef0424c3c15" [[package]] name = "hypervisor" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "anyhow", "byteorder", @@ -973,7 +964,6 @@ checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" [[package]] name = "log_json" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "libc", "serde", @@ -983,7 +973,6 @@ dependencies = [ [[package]] name = "logging" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "lazy_static", "serde", @@ -1036,7 +1025,6 @@ dependencies = [ [[package]] name = "net_gen" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "vmm-sys-util 0.12.1", ] @@ -1044,7 +1032,6 @@ dependencies = [ [[package]] name = "net_util" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "epoll", "getrandom 0.2.17", @@ -1110,7 +1097,6 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" [[package]] name = "option_parser" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" [[package]] name = "parking_lot" @@ -1138,7 +1124,6 @@ dependencies = [ [[package]] name = "pci" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "anyhow", "byteorder", @@ -1236,7 +1221,6 @@ dependencies = [ [[package]] name = "qcow" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "byteorder", "libc", @@ -1295,7 +1279,6 @@ checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" [[package]] name = "rate_limiter" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "libc", "log", @@ -1514,7 +1497,6 @@ dependencies = [ [[package]] name = "serial_buffer" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" [[package]] name = "shlex" @@ -1822,7 +1804,6 @@ dependencies = [ [[package]] name = "tpm" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "anyhow", "byteorder", @@ -1835,7 +1816,6 @@ dependencies = [ [[package]] name = "tracer" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "libc", "log", @@ -1907,7 +1887,6 @@ dependencies = [ [[package]] name = "vfio_user" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "libc", "log", @@ -1923,7 +1902,6 @@ dependencies = [ [[package]] name = "vhdx" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "byteorder", "crc32c", @@ -1988,7 +1966,6 @@ checksum = "091f1f09cfbf2a78563b562e7a949465cce1aef63b6065645188d995162f8868" [[package]] name = "virtio-devices" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "anyhow", "arc-swap", @@ -2061,7 +2038,6 @@ dependencies = [ [[package]] name = "virtiofsd" version = "1.11.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "anyhow", "bitflags 1.3.2", @@ -2087,7 +2063,6 @@ dependencies = [ [[package]] name = "vm-allocator" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "arch", "libc", @@ -2097,7 +2072,6 @@ dependencies = [ [[package]] name = "vm-device" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "anyhow", "hypervisor", @@ -2140,7 +2114,6 @@ dependencies = [ [[package]] name = "vm-migration" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "anyhow", "serde", @@ -2152,7 +2125,6 @@ dependencies = [ [[package]] name = "vm-virtio" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "log", "virtio-queue 0.14.0", @@ -2162,7 +2134,6 @@ dependencies = [ [[package]] name = "vmm" version = "0.1.0" -source = "git+https://github.com/TencentCloud/CubeSandbox?rev=4411c96ba8658a959de34693f54f4918db2a432f#4411c96ba8658a959de34693f54f4918db2a432f" dependencies = [ "acpi_tables", "anyhow", diff --git a/runtime/sandbox-worker/Cargo.toml b/runtime/sandbox-worker/Cargo.toml index 04e4c4b..9bdf8aa 100644 --- a/runtime/sandbox-worker/Cargo.toml +++ b/runtime/sandbox-worker/Cargo.toml @@ -7,7 +7,7 @@ license = "Apache-2.0" [workspace] [dependencies] -cube-hypervisor = { git = "https://github.com/TencentCloud/CubeSandbox", rev = "4411c96ba8658a959de34693f54f4918db2a432f", features = ["lib_support"] } +cube-hypervisor = { path = "../../vendor/cube-hypervisor", features = ["lib_support"] } serde_json = "1" # CubeSandbox v0.7.3 uses vm-memory 0.16; linux-loader 0.13.2 uses 0.17. linux-loader = "=0.13.0" diff --git a/tests/sandbox/check_sources.py b/tests/sandbox/check_sources.py new file mode 100644 index 0000000..adbcbd7 --- /dev/null +++ b/tests/sandbox/check_sources.py @@ -0,0 +1,40 @@ +"""校验导入源码的完整性、许可证和 worker 的依赖来源。""" +import hashlib +import json +from pathlib import Path +import subprocess + +ROOT = Path(__file__).resolve().parents[2] + + +def main(): + vendor = ROOT / 'vendor/cube-hypervisor' + source = json.loads((vendor / 'UPSTREAM.json').read_text()) + failures = [] + for name, expected in source['files_sha256'].items(): + path = vendor / name + if not path.is_file() or hashlib.sha256(path.read_bytes()).hexdigest() != expected: + failures.append(f'导入文件发生变化,需要更新来源记录:{name}') + for name in ['LICENSE-APACHE', 'LICENSE-BSD-3-Clause', 'LICENSE-CUBESANDBOX', + 'virtiofsd/LICENSE-APACHE', 'virtiofsd/LICENSE-BSD-3-Clause']: + if name not in source['files_sha256']: + failures.append(f'来源记录缺少许可证:{name}') + worker = ROOT / 'runtime/sandbox-worker' + metadata = json.loads(subprocess.check_output([ + 'cargo', 'metadata', '--locked', '--offline', '--no-deps', '--format-version=1', + '--manifest-path', str(worker / 'Cargo.toml'), + ], text=True)) + package = next(p for p in metadata['packages'] if p['name'] == 'agentdrive-sandbox-worker') + dependency = next(d for d in package['dependencies'] if d['name'] == 'cube-hypervisor') + if dependency.get('path') != str(vendor) or dependency.get('source'): + failures.append('worker 必须使用导入的 hypervisor 源码') + for line in (worker / 'Cargo.lock').read_text().splitlines(): + if line.startswith('source = ') and 'cubesandbox' in line.lower(): + failures.append('依赖锁文件仍然引用远端 CubeSandbox') + if failures: + raise SystemExit('\n'.join(failures)) + print(f'已验证 {len(source["files_sha256"])} 个导入文件、许可证和 worker 依赖来源') + + +if __name__ == '__main__': + main() diff --git a/tests/sandbox/network_probe.py b/tests/sandbox/network_probe.py index 159f022..93ca424 100644 --- a/tests/sandbox/network_probe.py +++ b/tests/sandbox/network_probe.py @@ -52,10 +52,13 @@ def call(name, arguments, screenshot=None): initialization = rpc('initialize', dict(protocolVersion='2024-11-05', capabilities={}, clientInfo=dict(name='agentdrive-network-test', version='1'))) -assert all(value in initialization['instructions'] for value in ('chromium', 'xdg-open', '/workspace')) +assert initialization['serverInfo']['name'] == 'agentdrive-sandbox', initialization +assert all(value in initialization['instructions'].lower() + for value in ('chromium', 'browser_*', '/workspace')), initialization stream.write(b'{"jsonrpc":"2.0","method":"notifications/initialized"}\n') tools = rpc('tools/list', {})['tools'] -assert 'chromium' in next(tool['description'] for tool in tools if tool['name'] == 'list_apps') +desktop_description = next(tool['description'] for tool in tools if tool['name'] == 'list_apps') +assert all(value in desktop_description for value in ('chromium', 'xdg-open', '/workspace')), desktop_description call('get_app_state', {'include_screenshot': False}) call('list_apps', {}) call('setup_accessibility', {}) @@ -65,26 +68,29 @@ def call(name, arguments, screenshot=None): call('type_text', {'text': 'xdg-open https://example.com'}) call('press_key', {'key': 'Return'}) time.sleep(8) -call('list_windows', {}) +windows = json.loads(call('list_windows', {}))['windows'] +browser = next(window for window in windows + if 'chromium' in window.get('wm_class', '').lower()) +target_window = {'window_id': browser['window_id']} for index, (url, expected) in enumerate([ ('https://example.com', 'Example Domain'), ('https://www.python.org/', 'Welcome to Python.org'), ('https://opencsg.com/', 'OpenCSG'), ]): - call('press_key', {'key': 'ctrl+l', 'wm_class': 'chromium'}) + call('press_key', {'key': 'ctrl+l', **target_window}) target = url + ('&' if '?' in url else '?') + 'agentdrive_probe=' + uuid.uuid4().hex - call('type_text', {'text': target, 'wm_class': 'chromium'}) - call('press_key', {'key': 'Return', 'wm_class': 'chromium'}) + call('type_text', {'text': target, **target_window}) + call('press_key', {'key': 'Return', **target_window}) deadline = time.monotonic() + 60 while True: time.sleep(3) - state = call('get_app_state', {'app_name_or_bundle_identifier': 'chromium', + state = call('get_app_state', {**target_window, 'include_screenshot': False}) tree = json.loads(state).get('accessibility_tree', []) if any(node.get('role') == 'frame' and expected.lower() in node.get('name', '').lower() for node in tree): break assert time.monotonic() < deadline, f'Browser did not load {url}: {state}' - call('get_app_state', {'app_name_or_bundle_identifier': 'chromium', + call('get_app_state', {**target_window, 'include_screenshot': True}, f'website-{index + 1}.png') (artifacts / 'report.json').write_text(json.dumps(dict( verified=True, websites=['https://example.com', 'https://www.python.org/', 'https://opencsg.com/'], diff --git a/tests/sandbox/playwright_probe.py b/tests/sandbox/playwright_probe.py index 66695c0..4c39b77 100644 --- a/tests/sandbox/playwright_probe.py +++ b/tests/sandbox/playwright_probe.py @@ -99,9 +99,10 @@ def click_button(label): call('browser_evaluate', {'function': '() => { document.open(); document.write(' + json.dumps(fixture.read_text()) + '); document.close(); return document.title; }'}) click_button('点击验证') assert '点击成功' in call('browser_snapshot') - windows = call('list_windows') - assert 'chromium' in windows.lower(), windows - desktop = call('get_app_state', {'app_name_or_bundle_identifier': 'chromium', 'include_screenshot': True}) + windows = json.loads(call('list_windows'))['windows'] + browser = next(window for window in windows + if 'chromium' in window.get('wm_class', '').lower()) + desktop = call('get_app_state', {'window_id': browser['window_id'], 'include_screenshot': True}) assert 'AgentDrive Playwright' in desktop, desktop click_button('下载到当前分支') expected = mount / 'Downloads' / filename @@ -120,7 +121,7 @@ def click_button(label): start() rpc('tools/list', {}) assert 'AgentDrive Playwright Verification' in call('browser_tabs', {'action': 'list'}) - call('get_app_state', {'app_name_or_bundle_identifier': 'chromium', 'include_screenshot': True}) + call('get_app_state', {'window_id': browser['window_id'], 'include_screenshot': True}) report['reconnect_preserves_browser'] = True (artifacts / 'report.json').write_text(json.dumps(report, ensure_ascii=False, indent=2)) print('UNIFIED MCP VERIFIED', flush=True) diff --git a/tests/sandbox/test_install_kernels.py b/tests/sandbox/test_install_kernels.py new file mode 100644 index 0000000..f8e007d --- /dev/null +++ b/tests/sandbox/test_install_kernels.py @@ -0,0 +1,52 @@ +"""验证两个内核版本的复制、校验记录和参数检查。""" +import hashlib +import json +from pathlib import Path +import subprocess +import tempfile +import unittest + +SCRIPT = Path(__file__).resolve().parents[2] / 'build/sandbox/install-kernels.sh' + + +class InstallKernelsTest(unittest.TestCase): + def test_copies_both_variants_and_records_checksums(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + output = root / 'output' + output.mkdir() + for name in ['vmlinux-bm', 'vmlinux-pvm']: + (root / name).write_bytes(name.encode()) + subprocess.run(['bash', str(SCRIPT), str(root / 'vmlinux-bm'), str(output)], check=True) + manifest = json.loads((output / 'kernels.json').read_text()) + for mode, name in [('kvm', 'vmlinux-bm'), ('pvm', 'vmlinux-pvm')]: + self.assertEqual((output / name).read_bytes(), name.encode()) + self.assertEqual(manifest[mode], { + 'file': name, 'sha256': hashlib.sha256(name.encode()).hexdigest(), + }) + self.assertEqual((output / 'vmlinux').read_bytes(), b'vmlinux-bm') + + def test_kvm_only_and_invalid_explicit_pvm(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + output = root / 'output' + output.mkdir() + kernel = root / 'vmlinux' + kernel.write_bytes(b'kvm') + command = ['bash', str(SCRIPT), str(kernel), str(output)] + subprocess.run(command, check=True) + self.assertEqual(set(json.loads((output / 'kernels.json').read_text())), {'kvm'}) + result = subprocess.run(command + [str(root / 'missing')], capture_output=True) + self.assertNotEqual(result.returncode, 0) + + def test_rejects_pvm_as_kvm(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + pvm = root / 'vmlinux-pvm' + pvm.write_bytes(b'pvm') + result = subprocess.run(['bash', str(SCRIPT), str(pvm), str(root)], capture_output=True) + self.assertNotEqual(result.returncode, 0) + + +if __name__ == '__main__': + unittest.main() diff --git a/vendor/cube-hypervisor/.github/ISSUE_TEMPLATE/bug_report.md b/vendor/cube-hypervisor/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..7fc8dc7 --- /dev/null +++ b/vendor/cube-hypervisor/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,34 @@ +--- +name: Bug report +about: File a bug report +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behaviour: + +**Version** + +Output of `cloud-hypervisor --version`: + +Did you build from source, if so build command line (e.g. features): + +**VM configuration** + +What command line did you run (or JSON config data): + +Guest OS version details: + +Host OS version details: + +**Logs** + +Output of `cloud-hypervisor -v` from either standard error or via `--log-file`: + +Linux kernel output: diff --git a/vendor/cube-hypervisor/.github/dependabot.yml b/vendor/cube-hypervisor/.github/dependabot.yml new file mode 100644 index 0000000..0b03830 --- /dev/null +++ b/vendor/cube-hypervisor/.github/dependabot.yml @@ -0,0 +1,18 @@ +version: 2 +updates: +- package-ecosystem: cargo + directory: "/" + schedule: + interval: daily + open-pull-requests-limit: 1 + allow: + - dependency-type: direct + - dependency-type: indirect +- package-ecosystem: cargo + directory: "/fuzz" + schedule: + interval: daily + open-pull-requests-limit: 1 + allow: + - dependency-type: direct + - dependency-type: indirect diff --git a/vendor/cube-hypervisor/.github/workflows/audit.yaml b/vendor/cube-hypervisor/.github/workflows/audit.yaml new file mode 100644 index 0000000..17e3fe4 --- /dev/null +++ b/vendor/cube-hypervisor/.github/workflows/audit.yaml @@ -0,0 +1,15 @@ +name: Cloud Hypervisor Dependency Audit +on: + pull_request: + paths: + - '**/Cargo.toml' + - '**/Cargo.lock' +jobs: + security_audit: + name: Audit + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/audit-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/vendor/cube-hypervisor/.github/workflows/build.yaml b/vendor/cube-hypervisor/.github/workflows/build.yaml new file mode 100644 index 0000000..15824bb --- /dev/null +++ b/vendor/cube-hypervisor/.github/workflows/build.yaml @@ -0,0 +1,58 @@ +name: Cloud Hypervisor Build +on: [pull_request, create] + +jobs: + build: + if: github.event_name == 'pull_request' + name: Build + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + rust: + - stable + - beta + - nightly + - "1.60" + target: + - x86_64-unknown-linux-gnu + - x86_64-unknown-linux-musl + steps: + - name: Code checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Install musl-gcc + run: sudo apt install -y musl-tools + + - name: Install Rust toolchain (${{ matrix.rust }}) + uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + target: ${{ matrix.target }} + override: true + + - name: Build (default features) + run: cargo rustc --locked --bin cloud-hypervisor -- -D warnings + + - name: Build (kvm) + run: cargo rustc --locked --bin cloud-hypervisor --no-default-features --features "kvm" -- -D warnings + + - name: Build (default features + tdx) + run: cargo rustc --locked --bin cloud-hypervisor --features "tdx" -- -D warnings + + - name: Build (default features + guest_debug) + run: cargo rustc --locked --bin cloud-hypervisor --features "guest_debug" -- -D warnings + + - name: Build (mshv) + run: cargo rustc --locked --bin cloud-hypervisor --no-default-features --features "mshv" -- -D warnings + + - name: Build (mshv + kvm) + run: cargo rustc --locked --bin cloud-hypervisor --no-default-features --features "mshv,kvm" -- -D warnings + + - name: Release Build (default features) + run: cargo build --locked --all --release --target=${{ matrix.target }} + + - name: Check build did not modify any files + run: test -z "$(git status --porcelain)" diff --git a/vendor/cube-hypervisor/.github/workflows/dco.yaml b/vendor/cube-hypervisor/.github/workflows/dco.yaml new file mode 100644 index 0000000..531eaf5 --- /dev/null +++ b/vendor/cube-hypervisor/.github/workflows/dco.yaml @@ -0,0 +1,18 @@ +name: DCO +on: + pull_request: +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.x + uses: actions/setup-python@v1 + with: + python-version: '3.x' + - name: Check DCO + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + pip3 install -U dco-check + dco-check -e "49699333+dependabot[bot]@users.noreply.github.com" diff --git a/vendor/cube-hypervisor/.github/workflows/docker-image.yaml b/vendor/cube-hypervisor/.github/workflows/docker-image.yaml new file mode 100644 index 0000000..ddfdc14 --- /dev/null +++ b/vendor/cube-hypervisor/.github/workflows/docker-image.yaml @@ -0,0 +1,59 @@ +name: Cloud Hypervisor's Docker image update + +on: + push: + branches: main + paths: resources/Dockerfile + pull_request: + paths: resources/Dockerfile + +jobs: + main: + runs-on: ubuntu-latest + steps: + - name: Code checkout + uses: actions/checkout@v2 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to DockerHub + if: ${{ github.event_name == 'push' }} + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Docker meta + id: meta + uses: docker/metadata-action@v3 + with: + # list of Docker images to use as base name for tags + images: cloudhypervisor/dev + # generate Docker tags based on the following events/attributes + tags: | + type=raw,value={{date 'YYYYMMDD'}}-0 + type=sha + + - name: Build and push + if: ${{ github.event_name == 'push' }} + uses: docker/build-push-action@v2 + with: + file: ./resources/Dockerfile + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + + - name: Build only + if: ${{ github.event_name == 'pull_request' }} + uses: docker/build-push-action@v2 + with: + file: ./resources/Dockerfile + platforms: linux/amd64,linux/arm64 + tags: ${{ steps.meta.outputs.tags }} + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} diff --git a/vendor/cube-hypervisor/.github/workflows/fuzz-build.yaml b/vendor/cube-hypervisor/.github/workflows/fuzz-build.yaml new file mode 100644 index 0000000..d7ebba0 --- /dev/null +++ b/vendor/cube-hypervisor/.github/workflows/fuzz-build.yaml @@ -0,0 +1,29 @@ +name: Cloud Hypervisor Cargo Fuzz Build +on: [pull_request, create] + +jobs: + build: + if: github.event_name == 'pull_request' + name: Cargo Fuzz Build + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - nightly + target: + - x86_64-unknown-linux-gnu + steps: + - name: Code checkout + uses: actions/checkout@v2 + - name: Install Rust toolchain (${{ matrix.rust }}) + uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + target: ${{ matrix.target }} + override: true + - name: Install Cargo fuzz + # Temporary fix for cargo-fuzz on latest nightly: https://github.com/rust-fuzz/cargo-fuzz/issues/276 + #run: cargo install cargo-fuzz + run: cargo install --git https://github.com/rust-fuzz/cargo-fuzz --rev b4df3e58f767b5cad8d1aa6753961003f56f3609 + - name: Cargo Fuzz Build + run: cargo fuzz build diff --git a/vendor/cube-hypervisor/.github/workflows/quality.yaml b/vendor/cube-hypervisor/.github/workflows/quality.yaml new file mode 100644 index 0000000..b77fc51 --- /dev/null +++ b/vendor/cube-hypervisor/.github/workflows/quality.yaml @@ -0,0 +1,104 @@ +name: Cloud Hypervisor Quality Checks +on: [pull_request, create] + +jobs: + build: + if: github.event_name == 'pull_request' + name: Quality (clippy, rustfmt) + runs-on: ubuntu-latest + continue-on-error: ${{ matrix.experimental }} + strategy: + fail-fast: false + matrix: + rust: + - stable + target: + - x86_64-unknown-linux-gnu + - aarch64-unknown-linux-gnu + experimental: [false] + include: + - rust: beta + target: x86_64-unknown-linux-gnu + experimental: true + - rust: beta + target: aarch64-unknown-linux-gnu + experimental: true + steps: + - name: Code checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Install Rust toolchain (${{ matrix.rust }}) + uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + target: ${{ matrix.target }} + override: true + components: rustfmt, clippy + + - name: Debug Check (default features) + if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }} + run: | + set -e + commits=$(git rev-list origin/${{ github.base_ref }}..${{ github.sha }}) + for commit in $commits; do git checkout $commit; cargo check --tests --all --target=${{ matrix.target }}; done + git checkout ${{ github.sha }} + + - name: Formatting (rustfmt) + run: cargo fmt -- --check + + - name: Clippy (kvm) + uses: actions-rs/cargo@v1 + with: + use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} + command: clippy + args: --locked --all --all-targets --no-default-features --tests --features "kvm" -- -D warnings + + - name: Clippy (default features) + uses: actions-rs/cargo@v1 + with: + use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} + command: clippy + args: --locked --all --all-targets --tests -- -D warnings + + - name: Clippy (default features + guest_debug) + uses: actions-rs/cargo@v1 + with: + use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} + command: clippy + args: --locked --all --all-targets --tests --features "guest_debug" -- -D warnings + + - name: Clippy (default features + tracing) + uses: actions-rs/cargo@v1 + with: + use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} + command: clippy + args: --locked --all --all-targets --tests --features "tracing" -- -D warnings + + - name: Clippy (mshv) + if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }} + uses: actions-rs/cargo@v1 + with: + use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} + command: clippy + args: --locked --all --all-targets --no-default-features --tests --features "mshv" -- -D warnings + + - name: Clippy (mshv + kvm) + if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }} + uses: actions-rs/cargo@v1 + with: + use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} + command: clippy + args: --locked --all --all-targets --no-default-features --tests --features "mshv,kvm" -- -D warnings + + - name: Clippy (kvm + tdx) + if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }} + uses: actions-rs/cargo@v1 + with: + use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} + command: clippy + args: --locked --all --all-targets --no-default-features --tests --features "tdx,kvm" -- -D warnings + + - name: Check build did not modify any files + run: test -z "$(git status --porcelain)" diff --git a/vendor/cube-hypervisor/.github/workflows/release.yaml b/vendor/cube-hypervisor/.github/workflows/release.yaml new file mode 100644 index 0000000..5e49600 --- /dev/null +++ b/vendor/cube-hypervisor/.github/workflows/release.yaml @@ -0,0 +1,141 @@ +name: Cloud Hypervisor Release +on: [create] + +jobs: + release: + if: github.event_name == 'create' && github.event.ref_type == 'tag' + name: Release + runs-on: ubuntu-latest + steps: + - name: Code checkout + uses: actions/checkout@v2 + - name: Install musl-gcc + run: sudo apt install -y musl-tools + - name: Create release directory + run: rsync -rv --exclude=.git . ../cloud-hypervisor-${{ github.event.ref }} + - name: Install Rust toolchain (x86_64-unknown-linux-gnu) + uses: actions-rs/toolchain@v1 + with: + toolchain: "1.62" + target: x86_64-unknown-linux-gnu + - name: Install Rust toolchain (x86_64-unknown-linux-musl) + uses: actions-rs/toolchain@v1 + with: + toolchain: "1.62" + target: x86_64-unknown-linux-musl + - name: Build + uses: actions-rs/cargo@v1 + with: + toolchain: "1.62" + command: build + args: --all --release --target=x86_64-unknown-linux-gnu + - name: Static Build + uses: actions-rs/cargo@v1 + with: + toolchain: "1.62" + command: build + args: --all --release --target=x86_64-unknown-linux-musl + - name: Strip cloud-hypervisor binaries + run: strip target/*/release/cloud-hypervisor + - name: Install Rust toolchain (aarch64-unknown-linux-musl) + uses: actions-rs/toolchain@v1 + with: + toolchain: "1.62" + target: aarch64-unknown-linux-musl + override: true + - name: Static Build (AArch64) + uses: actions-rs/cargo@v1 + with: + use-cross: true + command: build + args: --all --release --target=aarch64-unknown-linux-musl + - name: Vendor + working-directory: ../cloud-hypervisor-${{ github.event.ref }} + run: | + mkdir ../vendor-cargo-home + export CARGO_HOME=$(realpath ../vendor-cargo-home) + mkdir .cargo + cargo vendor > .cargo/config.toml + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: ${{ github.ref }} + draft: true + prerelease: true + - name: Create vendored source archive + working-directory: ../ + run: tar cJf cloud-hypervisor-${{ github.event.ref }}.tar.xz cloud-hypervisor-${{ github.event.ref }} + - name: Upload cloud-hypervisor vendored source archive + id: upload-release-cloud-hypervisor-vendored-sources + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ../cloud-hypervisor-${{ github.event.ref }}.tar.xz + asset_name: cloud-hypervisor-${{ github.event.ref }}.tar.xz + asset_content_type: application/x-xz + - name: Upload cloud-hypervisor + id: upload-release-cloud-hypervisor + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: target/x86_64-unknown-linux-gnu/release/cloud-hypervisor + asset_name: cloud-hypervisor + asset_content_type: application/octet-stream + - name: Upload static cloud-hypervisor + id: upload-release-static-cloud-hypervisor + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: target/x86_64-unknown-linux-musl/release/cloud-hypervisor + asset_name: cloud-hypervisor-static + asset_content_type: application/octet-stream + - name: Upload ch-remote + id: upload-release-ch-remote + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: target/x86_64-unknown-linux-gnu/release/ch-remote + asset_name: ch-remote + asset_content_type: application/octet-stream + - name: Upload static-ch-remote + id: upload-release-static-ch-remote + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: target/x86_64-unknown-linux-musl/release/ch-remote + asset_name: ch-remote-static + asset_content_type: application/octet-stream + - name: Upload static AArch64 cloud-hypervisor + id: upload-release-static-aarch64-cloud-hypervisor + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: target/aarch64-unknown-linux-musl/release/cloud-hypervisor + asset_name: cloud-hypervisor-static-aarch64 + asset_content_type: application/octet-stream + - name: Upload static AArch64 ch-remote + id: upload-release-static-aarch64-ch-remote + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: target/aarch64-unknown-linux-musl/release/ch-remote + asset_name: ch-remote-static-aarch64 + asset_content_type: application/octet-stream diff --git a/vendor/cube-hypervisor/.gitignore b/vendor/cube-hypervisor/.gitignore new file mode 100644 index 0000000..ae6d262 --- /dev/null +++ b/vendor/cube-hypervisor/.gitignore @@ -0,0 +1,7 @@ +/build +/.cargo +/target +**/*.rs.bk +**/Cargo.lock +**/rusty-tags.vi +/rpm/SOURCES diff --git a/vendor/cube-hypervisor/.rustfmt.toml b/vendor/cube-hypervisor/.rustfmt.toml new file mode 100644 index 0000000..36c419b --- /dev/null +++ b/vendor/cube-hypervisor/.rustfmt.toml @@ -0,0 +1 @@ +edition = "2021" \ No newline at end of file diff --git a/vendor/cube-hypervisor/CODEOWNERS b/vendor/cube-hypervisor/CODEOWNERS new file mode 100644 index 0000000..b68f4d5 --- /dev/null +++ b/vendor/cube-hypervisor/CODEOWNERS @@ -0,0 +1,2 @@ +# Add the list of code owners here (using their GitHub username) +* @cloud-hypervisor/cloud-hypervisor-reviewers diff --git a/vendor/cube-hypervisor/CODE_OF_CONDUCT.md b/vendor/cube-hypervisor/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..c05ae67 --- /dev/null +++ b/vendor/cube-hypervisor/CODE_OF_CONDUCT.md @@ -0,0 +1,73 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, as +contributors and maintainers we pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include but is not limited to: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or + advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address without explicit consent +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at otc.community@intel.com. All complaints +will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at: https://www.contributor-covenant.org/version/1/4/code-of-conduct/ + +[homepage]: https://www.contributor-covenant.org diff --git a/vendor/cube-hypervisor/CONTRIBUTING.md b/vendor/cube-hypervisor/CONTRIBUTING.md new file mode 100644 index 0000000..a740e99 --- /dev/null +++ b/vendor/cube-hypervisor/CONTRIBUTING.md @@ -0,0 +1,116 @@ +# Contributing to Cloud Hypervisor + +Cloud Hypervisor is an open source project licensed under the [Apache v2 +License](https://opensource.org/licenses/Apache-2.0) and the [BSD 3 +Clause](https://opensource.org/licenses/BSD-3-Clause) license. Individual files +contain details of their licensing and changes to that file are under the same +license unless the contribution changes the license of the file. When importing +code from a third party project (e.g. Firecracker or CrosVM) please respect the +license of those projects. + +New code should be under the [Apache v2 +License](https://opensource.org/licenses/Apache-2.0). + +## Coding Style + +We follow the [Rust Style](https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/guide.md) +convention and enforce it through the Continuous Integration (CI) process calling into `rustfmt` +for each submitted Pull Request (PR). + +## Basic Checks + +Please consider creating the following hook as `.git/hooks/pre-commit` in order +to ensure basic correctness of your code. You can extend this further if you +have specific features that you regularly develop against. + +```sh +#!/bin/sh + +cargo fmt -- --check || exit 1 +cargo check --locked --all --all-targets --tests || exit 1 +cargo clippy --locked --all --all-targets --tests -- -D warnings || exit 1 +``` + +You will need to `chmod +x .git/hooks/pre-commit` to have it run on every +commit you make. + +## Certificate of Origin + +In order to get a clear contribution chain of trust we use the [signed-off-by language](https://01.org/community/signed-process) +used by the Linux kernel project. + +## Patch format + +Beside the signed-off-by footer, we expect each patch to comply with the following format: + +``` +: Change summary + +More detailed explanation of your changes: Why and how. +Wrap it to 72 characters. +See http://chris.beams.io/posts/git-commit/ +for some more good pieces of advice. + +Signed-off-by: +``` + +For example: + +``` +vm-virtio: Reset underlying device on driver request + +If the driver triggers a reset by writing zero into the status register +then reset the underlying device if supported. A device reset also +requires resetting various aspects of the queue. + +In order to be able to do a subsequent reactivate it is required to +reclaim certain resources (interrupt and queue EventFDs.) If a device +reset is requested by the driver but the underlying device does not +support it then generate an error as the driver would not be able to +configure it anyway. + +Signed-off-by: Rob Bradford +``` + +## Pull requests + +Cloud Hypervisor uses the “fork-and-pull” development model. Follow these steps if +you want to merge your changes to `cloud-hypervisor`: + +1. Fork the [cloud-hypervisor](https://github.com/cloud-hypervisor/cloud-hypervisor) project + into your github organization. +2. Within your fork, create a branch for your contribution. +3. [Create a pull request](https://help.github.com/articles/creating-a-pull-request-from-a-fork/) + against the main branch of the Cloud Hypervisor repository. +4. To update your pull request amend existing commits whenever applicable and + then push the new changes to your pull request branch. +5. Once the pull request is approved it can be integrated. + +## Issue tracking + +If you have a problem, please let us know. We recommend using +[github issues](https://github.com/cloud-hypervisor/cloud-hypervisor/issues/new) for formally +reporting and documenting them. + +To quickly and informally bring something up to us, you can also reach out on [Slack](https://cloud-hypervisor.slack.com). + +## Closing issues + +You can either close issues manually by adding the fixing commit SHA1 to the issue +comments or by adding the `Fixes` keyword to your commit message: + +``` +serial: Set terminal in raw mode + +In order to have proper output from the serial, we need to setup the +terminal in raw mode. When the VM is shutting down, it is also the +VMM responsibility to set the terminal back into canonical mode if we +don't want to get any weird behavior from the shell. + +Fixes #88 + +Signed-off-by: Sebastien Boeuf +``` + +Then, after the corresponding PR is merged, Github will automatically close that issue when parsing the +[commit message](https://help.github.com/articles/closing-issues-via-commit-messages/). diff --git a/vendor/cube-hypervisor/CREDITS.md b/vendor/cube-hypervisor/CREDITS.md new file mode 100644 index 0000000..3001c89 --- /dev/null +++ b/vendor/cube-hypervisor/CREDITS.md @@ -0,0 +1,26 @@ +# Cloud Hypervisor Credits and Thanks + +The Cloud Hypervisor code is based on the [rust-vmm](https://github.com/rust-vmm), [Firecracker](https://firecracker-microvm.github.io/) and [crosvm](https://chromium.googlesource.com/chromiumos/platform/crosvm/) +projects implementations. + +Our thanks go to the contributors of those projects: + +## crosvm + +* [Zach Reizner](https://github.com/zachreizner) +* [Dylan Reid](https://github.com/dgreid) +* [Daniel Verkamp](https://github.com/danielverkamp) +* [Stephen Barber](https://github.com/smibarber) +* [Chirantan Ekbote](https://github.com/jynnantonix) +* [Jason D. Clinton](https://github.com/jclinton) +* Sonny Rao + +## Firecracker + +See the [Firecracker CREDITS](https://github.com/firecracker-microvm/firecracker/blob/master/CREDITS.md). + +## rust-vmm + +* [Andreea Florescu](https://github.com/andreeaflorescu) <> +* [Paolo Bonzini](https://github.com/zachreizner) +* [Jiang Liu](https://github.com/jiangliu) diff --git a/vendor/cube-hypervisor/Cargo.lock b/vendor/cube-hypervisor/Cargo.lock new file mode 100644 index 0000000..f15d8f3 --- /dev/null +++ b/vendor/cube-hypervisor/Cargo.lock @@ -0,0 +1,2875 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "acpi_tables" +version = "0.1.0" +dependencies = [ + "vm-memory 0.16.1", + "zerocopy 0.6.6", +] + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + +[[package]] +name = "api_client" +version = "0.1.0" +dependencies = [ + "vmm-sys-util", +] + +[[package]] +name = "arc-swap" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" + +[[package]] +name = "arch" +version = "0.1.0" +dependencies = [ + "anyhow", + "byteorder", + "fdt", + "hypervisor", + "libc", + "linux-loader", + "log", + "logging", + "serde", + "serde_json", + "thiserror", + "uuid", + "vm-fdt", + "vm-memory 0.16.1", + "vm-migration", + "vmm-sys-util", +] + +[[package]] +name = "async-trait" +version = "0.1.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "atomic-polyfill" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" +dependencies = [ + "critical-section", +] + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "backtrace" +version = "0.3.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6806a6321ec58106fea15becdad98371e28d92ccbc7c8f1b3b6dd724fe8f1002" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets 0.52.6", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "block_util" +version = "0.1.0" +dependencies = [ + "io-uring", + "libc", + "log", + "qcow", + "serde", + "thiserror", + "vhdx", + "virtio-bindings 0.1.0", + "virtio-queue 0.14.0", + "vm-memory 0.16.1", + "vm-virtio", + "vmm-sys-util", +] + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" + +[[package]] +name = "capng" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a26766f93f07f7e8b8309ed2824fa2a68f5d12d219de855e24688e9fbe89e85" +dependencies = [ + "bitflags 1.3.2", + "libc", +] + +[[package]] +name = "cc" +version = "1.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9d013ecb737093c0e86b151a7b837993cf9ec6c502946cfb44bedc392421e0b" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets 0.52.6", +] + +[[package]] +name = "clap" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac495e00dcec98c83465d5ad66c5c4fabd652fd6686e7c6269b117e729a6f17b" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c77ed9a32a62e6ca27175d00d29d05ca32e396ea1eb5fb01d8256b669cec7663" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim 0.10.0", + "terminal_size", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + +[[package]] +name = "cobs" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15" + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "crc32c" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a47af21622d091a8f0fb295b88bc886ac74efcc613efc19f5d0b21de5c89e47" +dependencies = [ + "rustc_version", +] + +[[package]] +name = "critical-section" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" + +[[package]] +name = "crossbeam-channel" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[package]] +name = "cube-hypervisor" +version = "28.0.0" +dependencies = [ + "anyhow", + "api_client", + "arc-swap", + "chrono", + "clap", + "dirs", + "epoll", + "event_monitor", + "event_notifier", + "hypervisor", + "lazy_static", + "libc", + "log", + "log_json", + "logging", + "net_util", + "once_cell", + "option_parser", + "rlimit", + "seccompiler", + "serde", + "serde_json", + "signal-hook", + "slog", + "slog-scope", + "test_infra", + "thiserror", + "tpm", + "tracer", + "vm-migration", + "vmm", + "vmm-sys-util", + "wait-timeout", +] + +[[package]] +name = "darling" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim 0.11.1", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", +] + +[[package]] +name = "devices" +version = "0.1.0" +dependencies = [ + "acpi_tables", + "anyhow", + "arch", + "bitflags 2.6.0", + "byteorder", + "event_monitor", + "event_notifier", + "hypervisor", + "libc", + "log", + "logging", + "pci", + "phf", + "serde", + "thiserror", + "tpm", + "vm-allocator", + "vm-device", + "vm-memory 0.16.1", + "vm-migration", + "vmm-sys-util", +] + +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + +[[package]] +name = "env_logger" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "epoll" +version = "4.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74351c3392ea1ff6cd2628e0042d268ac2371cb613252ff383b6dfa50d22fa79" +dependencies = [ + "bitflags 2.6.0", + "libc", +] + +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "error-chain" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" +dependencies = [ + "version_check", +] + +[[package]] +name = "event_monitor" +version = "0.1.0" +dependencies = [ + "libc", + "serde", + "serde_json", +] + +[[package]] +name = "event_notifier" +version = "0.1.0" +dependencies = [ + "lazy_static", + "log", +] + +[[package]] +name = "fdt" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784a4df722dc6267a04af36895398f59d21d07dce47232adf31ec0ff2fa45e67" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", + "num_cpus", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "gdbstub" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4e02bf1b1a624d96925c608f1b268d82a76cbc587ce9e59f7c755e9ea11c75c" +dependencies = [ + "bitflags 1.3.2", + "cfg-if", + "log", + "managed", + "num-traits", + "paste", +] + +[[package]] +name = "gdbstub_arch" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eecb536c55c43593a00dde9074dbbdb0e81ce5f20dbca921400f8779c21dea9c" +dependencies = [ + "gdbstub", + "num-traits", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "hash32" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" +dependencies = [ + "byteorder", +] + +[[package]] +name = "heapless" +version = "0.7.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" +dependencies = [ + "atomic-polyfill", + "hash32", + "rustc_version", + "serde", + "spin", + "stable_deref_trait", +] + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "hypervisor" +version = "0.1.0" +dependencies = [ + "anyhow", + "byteorder", + "env_logger", + "iced-x86", + "kvm-bindings", + "kvm-ioctls", + "libc", + "log", + "mshv-bindings", + "mshv-ioctls", + "serde", + "serde_with 2.3.3", + "thiserror", + "vfio-ioctls", + "vm-memory 0.14.1", + "vmm-sys-util", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "iced-x86" +version = "1.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c447cff8c7f384a7d4f741cfcff32f75f3ad02b406432e8d6c878d56b1edf6b" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "instant" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "io-uring" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd1e1a01cfb924fd8c5c43b6827965db394f5a3a16c599ce03452266e1cf984c" +dependencies = [ + "bitflags 1.3.2", + "libc", +] + +[[package]] +name = "ipnetwork" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf466541e9d546596ee94f9f69590f89473455f88372423e0008fc1a7daf100e" +dependencies = [ + "serde", +] + +[[package]] +name = "is-terminal" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" +dependencies = [ + "hermit-abi 0.4.0", + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "js-sys" +version = "0.3.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "kvm-bindings" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a82e7e8725a39a0015e511a46cc1f7d90cecc180db1610c4d0d4339a9e48bd21" +dependencies = [ + "serde", + "vmm-sys-util", + "zerocopy 0.7.35", +] + +[[package]] +name = "kvm-ioctls" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bedae2ca4a531bebe311abaf9691f5cc14eaa21475243caa2e39c43bb872947d" +dependencies = [ + "bitflags 2.6.0", + "kvm-bindings", + "libc", + "vmm-sys-util", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.175" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543" + +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags 2.6.0", + "libc", +] + +[[package]] +name = "libseccomp-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a7cbbd4ad467251987c6e5b47d53b11a5a05add08f2447a9e2d70aef1e0d138" + +[[package]] +name = "libssh2-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dc8a030b787e2119a731f1951d6a773e2280c660f8ec4b0f5e1505a386e71ee" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "libz-sys" +version = "1.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d16453e800a8cf6dd2fc3eb4bc99b786a9b90c663b8559a5b1a041bf89e472" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "linux-loader" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "870c3814345f050991f99869417779f6062542bcf4ed81db7a1b926ad1306638" +dependencies = [ + "vm-memory 0.16.1", +] + +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + +[[package]] +name = "lock_api" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "log_json" +version = "0.1.0" +dependencies = [ + "libc", + "serde", + "serde_json", +] + +[[package]] +name = "logging" +version = "0.1.0" +dependencies = [ + "lazy_static", + "serde", + "slog", + "slog-async", + "slog-term", +] + +[[package]] +name = "managed" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ca88d725a0a943b096803bd34e73a4437208b6077654cc4ecb2947a5f91618d" + +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "micro_http" +version = "0.1.0" +source = "git+https://github.com/firecracker-microvm/micro-http?branch=main#8182cd5523b63ceb52ad9d0e7eb6fb95683e6d1b" +dependencies = [ + "libc", + "vmm-sys-util", +] + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.59.0", +] + +[[package]] +name = "mshv-bindings" +version = "0.2.0" +source = "git+https://github.com/rust-vmm/mshv?tag=v0.2.0#dd0a9f5ab9c32673e88d6de200af788dbfca6a72" +dependencies = [ + "libc", + "num_enum", + "serde", + "serde_derive", + "vmm-sys-util", + "zerocopy 0.7.35", +] + +[[package]] +name = "mshv-ioctls" +version = "0.2.0" +source = "git+https://github.com/rust-vmm/mshv?tag=v0.2.0#dd0a9f5ab9c32673e88d6de200af788dbfca6a72" +dependencies = [ + "libc", + "mshv-bindings", + "thiserror", + "vmm-sys-util", +] + +[[package]] +name = "net_gen" +version = "0.1.0" +dependencies = [ + "vmm-sys-util", +] + +[[package]] +name = "net_util" +version = "0.1.0" +dependencies = [ + "epoll", + "getrandom", + "libc", + "log", + "net_gen", + "once_cell", + "pnet", + "pnet_datalink", + "rate_limiter", + "serde", + "serde_json", + "thiserror", + "virtio-bindings 0.1.0", + "virtio-queue 0.14.0", + "vm-memory 0.16.1", + "vm-virtio", + "vmm-sys-util", +] + +[[package]] +name = "no-std-net" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43794a0ace135be66a25d3ae77d41b91615fb68ae937f904090203e81f755b65" + +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi 0.3.9", + "libc", +] + +[[package]] +name = "num_enum" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e613fc340b2220f734a8595782c551f1250e969d87d3be1ae0579e8d4065179" +dependencies = [ + "num_enum_derive", +] + +[[package]] +name = "num_enum_derive" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "num_threads" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9" +dependencies = [ + "libc", +] + +[[package]] +name = "object" +version = "0.36.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "openssl-src" +version = "300.3.2+3.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a211a18d945ef7e648cc6e0058f4c548ee46aab922ea203e0d30e966ea23647b" +dependencies = [ + "cc", +] + +[[package]] +name = "openssl-sys" +version = "0.9.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f9e8deee91df40a943c71b917e5874b951d32a802526c85721ce3b776c929d6" +dependencies = [ + "cc", + "libc", + "openssl-src", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "option_parser" +version = "0.1.0" + +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core 0.8.6", +] + +[[package]] +name = "parking_lot" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13" +dependencies = [ + "lock_api", + "parking_lot_core 0.9.11", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall 0.2.16", + "smallvec", + "winapi", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.5.17", + "smallvec", + "windows-targets 0.52.6", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pci" +version = "0.1.0" +dependencies = [ + "anyhow", + "byteorder", + "hypervisor", + "libc", + "log", + "serde", + "thiserror", + "vfio-bindings 0.3.1", + "vfio-ioctls", + "vfio_user", + "vm-allocator", + "vm-device", + "vm-memory 0.16.1", + "vm-migration", + "vmm-sys-util", +] + +[[package]] +name = "performance-metrics" +version = "0.1.0" +dependencies = [ + "clap", + "dirs", + "serde", + "serde_json", + "test_infra", + "thiserror", + "wait-timeout", +] + +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +dependencies = [ + "phf_macros", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_macros" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" + +[[package]] +name = "pnet" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "130c5b738eeda2dc5796fe2671e49027e6935e817ab51b930a36ec9e6a206a64" +dependencies = [ + "ipnetwork", + "pnet_base", + "pnet_datalink", + "pnet_packet", + "pnet_sys", + "pnet_transport", +] + +[[package]] +name = "pnet_base" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe4cf6fb3ab38b68d01ab2aea03ed3d1132b4868fa4e06285f29f16da01c5f4c" +dependencies = [ + "no-std-net", +] + +[[package]] +name = "pnet_datalink" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad5854abf0067ebbd3967f7d45ebc8976ff577ff0c7bd101c4973ae3c70f98fe" +dependencies = [ + "ipnetwork", + "libc", + "pnet_base", + "pnet_sys", + "winapi", +] + +[[package]] +name = "pnet_macros" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "688b17499eee04a0408aca0aa5cba5fc86401d7216de8a63fdf7a4c227871804" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn", +] + +[[package]] +name = "pnet_macros_support" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eea925b72f4bd37f8eab0f221bbe4c78b63498350c983ffa9dd4bcde7e030f56" +dependencies = [ + "pnet_base", +] + +[[package]] +name = "pnet_packet" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9a005825396b7fe7a38a8e288dbc342d5034dac80c15212436424fef8ea90ba" +dependencies = [ + "glob", + "pnet_base", + "pnet_macros", + "pnet_macros_support", +] + +[[package]] +name = "pnet_sys" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "417c0becd1b573f6d544f73671070b039051e5ad819cc64aa96377b536128d00" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "pnet_transport" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2637e14d7de974ee2f74393afccbc8704f3e54e6eb31488715e72481d1662cc3" +dependencies = [ + "libc", + "pnet_base", + "pnet_packet", + "pnet_sys", +] + +[[package]] +name = "postcard" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f7f0a8d620d71c457dd1d47df76bb18960378da56af4527aaa10f515eee732e" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "heapless", + "serde", +] + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy 0.7.35", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "qcow" +version = "0.1.0" +dependencies = [ + "byteorder", + "libc", + "log", + "remain", + "vmm-sys-util", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca0ecfa931c29007047d1bc58e623ab12e5590e8c7cc53200d5202b69266d8a" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rate_limiter" +version = "0.1.0" +dependencies = [ + "libc", + "log", + "vmm-sys-util", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5407465600fb0548f1442edf71dd20683c6ed326200ace4b1ef0763521bb3b77" +dependencies = [ + "bitflags 2.6.0", +] + +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + +[[package]] +name = "regex" +version = "1.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "remain" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46aef80f842736de545ada6ec65b81ee91504efd6853f4b96de7414c42ae7443" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "rlimit" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8a29d87a652dc4d43c586328706bb5cdff211f3f39a530f240b53f7221dab8e" +dependencies = [ + "libc", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "0.38.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f55e80d50763938498dd5ebb18647174e0c76dc38c5505294bb224624f30f36" +dependencies = [ + "bitflags 2.6.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "seccompiler" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f6575e3c2b3a0fe2ef3e53855b6a8dead7c29f783da5e123d378c8c6a89017e" +dependencies = [ + "libc", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + +[[package]] +name = "sendfd" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b183bfd5b1bc64ab0c1ef3ee06b008a9ef1b68a7d3a99ba566fbfe7a7c6d745b" +dependencies = [ + "libc", +] + +[[package]] +name = "serde" +version = "1.0.209" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.209" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "serde_with" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07ff71d2c147a7b57362cead5e22f772cd52f6ab31cfcd9edcd7f6aeb2a0afbe" +dependencies = [ + "serde", + "serde_with_macros 2.3.3", +] + +[[package]] +name = "serde_with" +version = "3.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cecfa94848272156ea67b2b1a53f20fc7bc638c4a46d2f8abde08f05f4b857" +dependencies = [ + "serde", + "serde_derive", + "serde_with_macros 3.9.0", +] + +[[package]] +name = "serde_with_macros" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "881b6f881b17d13214e5d494c939ebab463d01264ce1811e9d4ac3a882e7695f" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_with_macros" +version = "3.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8fee4991ef4f274617a51ad4af30519438dacb2f56ac773b08a1922ff743350" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serial_buffer" +version = "0.1.0" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "slog" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8347046d4ebd943127157b94d63abb990fcf729dc4e9978927fdf4ac3c998d06" + +[[package]] +name = "slog-async" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72c8038f898a2c79507940990f05386455b3a317d8f18d4caea7cbc3d5096b84" +dependencies = [ + "crossbeam-channel", + "slog", + "take_mut", + "thread_local", +] + +[[package]] +name = "slog-scope" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f95a4b4c3274cd2869549da82b57ccc930859bdbf5bcea0424bc5f140b3c786" +dependencies = [ + "arc-swap", + "lazy_static", + "slog", +] + +[[package]] +name = "slog-term" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6e022d0b998abfe5c3782c1f03551a596269450ccd677ea51c56f8b214610e8" +dependencies = [ + "is-terminal", + "slog", + "term", + "thread_local", + "time", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "socket2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +dependencies = [ + "lock_api", +] + +[[package]] +name = "ssh2" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7fe461910559f6d5604c3731d00d2aafc4a83d1665922e280f42f9a168d5455" +dependencies = [ + "bitflags 1.3.2", + "libc", + "libssh2-sys", + "parking_lot 0.11.2", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syslog" +version = "6.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc7e95b5b795122fafe6519e27629b5ab4232c73ebb2428f568e82b1a457ad3" +dependencies = [ + "error-chain", + "hostname", + "libc", + "log", + "time", +] + +[[package]] +name = "take_mut" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" + +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "terminal_size" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" +dependencies = [ + "rustix", + "windows-sys 0.48.0", +] + +[[package]] +name = "test_infra" +version = "0.1.0" +dependencies = [ + "dirs", + "epoll", + "libc", + "once_cell", + "serde", + "serde_json", + "ssh2", + "vmm-sys-util", + "wait-timeout", +] + +[[package]] +name = "thiserror" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "time" +version = "0.3.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +dependencies = [ + "deranged", + "itoa", + "libc", + "num-conv", + "num_threads", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +dependencies = [ + "num-conv", + "time-core", +] + +[[package]] +name = "tokio" +version = "1.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "parking_lot 0.12.4", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tpm" +version = "0.1.0" +dependencies = [ + "anyhow", + "byteorder", + "libc", + "log", + "thiserror", + "vmm-sys-util", +] + +[[package]] +name = "tracer" +version = "0.1.0" +dependencies = [ + "libc", + "log", + "once_cell", + "serde", + "serde_json", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "uuid" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" +dependencies = [ + "getrandom", + "rand", + "uuid-macro-internal", +] + +[[package]] +name = "uuid-macro-internal" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b91f57fe13a38d0ce9e28a03463d8d3c2468ed03d75375110ec71d93b449a08" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "vfio-bindings" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43449b404c488f70507dca193debd4bea361fe8089869b947adc19720e464bce" +dependencies = [ + "vmm-sys-util", +] + +[[package]] +name = "vfio-bindings" +version = "0.4.0" +source = "git+https://github.com/rust-vmm/vfio?rev=64171f3#64171f3da1af7926adf162ecf545cb05fb9243c9" + +[[package]] +name = "vfio-ioctls" +version = "0.2.0" +source = "git+https://github.com/rust-vmm/vfio?rev=64171f3#64171f3da1af7926adf162ecf545cb05fb9243c9" +dependencies = [ + "byteorder", + "kvm-bindings", + "kvm-ioctls", + "libc", + "log", + "mshv-bindings", + "mshv-ioctls", + "thiserror", + "vfio-bindings 0.4.0", + "vm-memory 0.14.1", + "vmm-sys-util", +] + +[[package]] +name = "vfio_user" +version = "0.1.0" +dependencies = [ + "libc", + "log", + "serde", + "serde_derive", + "serde_json", + "thiserror", + "vfio-bindings 0.3.1", + "vm-memory 0.14.1", + "vmm-sys-util", +] + +[[package]] +name = "vhdx" +version = "0.1.0" +dependencies = [ + "byteorder", + "crc32c", + "libc", + "log", + "remain", + "thiserror", + "uuid", +] + +[[package]] +name = "vhost" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6be08d1166d41a78861ad50212ab3f9eca0729c349ac3a7a8f557c62406b87cc" +dependencies = [ + "bitflags 2.6.0", + "libc", + "vm-memory 0.14.1", + "vmm-sys-util", +] + +[[package]] +name = "vhost" +version = "0.12.1" +source = "git+https://github.com/rust-vmm/vhost?rev=d983ae0#d983ae07f78663b7d24059667376992460b571a2" +dependencies = [ + "bitflags 2.6.0", + "libc", + "uuid", + "vm-memory 0.16.1", + "vmm-sys-util", +] + +[[package]] +name = "vhost-user-backend" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f0ffb1dd8e00a708a0e2c32d5efec5812953819888591fff9ff68236b8a5096" +dependencies = [ + "libc", + "log", + "vhost 0.11.0", + "virtio-bindings 0.2.4", + "virtio-queue 0.12.0", + "vm-memory 0.14.1", + "vmm-sys-util", +] + +[[package]] +name = "vhost-user-backend" +version = "0.16.1" +source = "git+https://github.com/rust-vmm/vhost?rev=d983ae0#d983ae07f78663b7d24059667376992460b571a2" +dependencies = [ + "libc", + "log", + "vhost 0.12.1", + "virtio-bindings 0.2.4", + "virtio-queue 0.14.0", + "vm-memory 0.16.1", + "vmm-sys-util", +] + +[[package]] +name = "vhost_user_block" +version = "0.1.0" +dependencies = [ + "block_util", + "clap", + "env_logger", + "epoll", + "libc", + "log", + "option_parser", + "qcow", + "vhost 0.12.1", + "vhost-user-backend 0.16.1", + "virtio-bindings 0.1.0", + "virtio-queue 0.14.0", + "vm-memory 0.16.1", + "vmm-sys-util", +] + +[[package]] +name = "vhost_user_net" +version = "0.1.0" +dependencies = [ + "clap", + "env_logger", + "epoll", + "libc", + "log", + "net_util", + "option_parser", + "vhost 0.12.1", + "vhost-user-backend 0.16.1", + "virtio-bindings 0.1.0", + "vm-memory 0.16.1", + "vmm-sys-util", +] + +[[package]] +name = "virtio-bindings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff512178285488516ed85f15b5d0113a7cdb89e9e8a760b269ae4f02b84bd6b" + +[[package]] +name = "virtio-bindings" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1711e61c00f8cb450bd15368152a1e37a12ef195008ddc7d0f4812f9e2b30a68" + +[[package]] +name = "virtio-devices" +version = "0.1.0" +dependencies = [ + "anyhow", + "arc-swap", + "block_util", + "byteorder", + "epoll", + "event_monitor", + "io-uring", + "libc", + "log", + "net_gen", + "net_util", + "pci", + "rate_limiter", + "seccompiler", + "sendfd", + "serde", + "serde_json", + "serde_with 3.9.0", + "serial_buffer", + "thiserror", + "vhost 0.12.1", + "virtio-bindings 0.1.0", + "virtio-queue 0.14.0", + "virtiofsd", + "vm-allocator", + "vm-device", + "vm-memory 0.16.1", + "vm-migration", + "vm-virtio", + "vmm-sys-util", +] + +[[package]] +name = "virtio-queue" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3f69a13d6610db9312acbb438b0390362af905d37634a2106be70c0f734986d" +dependencies = [ + "log", + "virtio-bindings 0.2.4", + "vm-memory 0.14.1", + "vmm-sys-util", +] + +[[package]] +name = "virtio-queue" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07d8406e7250c934462de585d8f2d2781c31819bca1fbb7c5e964ca6bbaabfe8" +dependencies = [ + "log", + "virtio-bindings 0.2.4", + "vm-memory 0.14.1", + "vmm-sys-util", +] + +[[package]] +name = "virtio-queue" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872e2f3fbd70a7e6f01689720cce3d5c2c5efe52b484dd07b674246ada0e9a8d" +dependencies = [ + "log", + "virtio-bindings 0.2.4", + "vm-memory 0.16.1", + "vmm-sys-util", +] + +[[package]] +name = "virtiofsd" +version = "1.11.0" +dependencies = [ + "anyhow", + "bitflags 1.3.2", + "capng", + "clap", + "env_logger", + "futures", + "libc", + "libseccomp-sys", + "log", + "postcard", + "rate_limiter", + "serde", + "syslog", + "vhost 0.11.0", + "vhost-user-backend 0.15.0", + "virtio-bindings 0.2.4", + "virtio-queue 0.14.0", + "vm-memory 0.16.1", + "vmm-sys-util", +] + +[[package]] +name = "vm-allocator" +version = "0.1.0" +dependencies = [ + "arch", + "libc", + "vm-memory 0.16.1", +] + +[[package]] +name = "vm-device" +version = "0.1.0" +dependencies = [ + "anyhow", + "hypervisor", + "serde", + "thiserror", + "vfio-ioctls", + "vm-memory 0.16.1", + "vmm-sys-util", +] + +[[package]] +name = "vm-fdt" +version = "0.3.0" +source = "git+https://github.com/rust-vmm/vm-fdt?branch=main#ef5bd734f5f66fb07722d766981adbc915f0d941" + +[[package]] +name = "vm-memory" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c3aba5064cc5f6f7740cddc8dae34d2d9a311cac69b60d942af7f3ab8fc49f4" +dependencies = [ + "arc-swap", + "bitflags 2.6.0", + "libc", + "thiserror", + "vmm-sys-util", + "winapi", +] + +[[package]] +name = "vm-memory" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1720e7240cdc739f935456eb77f370d7e9b2a3909204da1e2b47bef1137a013" +dependencies = [ + "arc-swap", + "bitflags 2.6.0", + "libc", + "thiserror", + "vmm-sys-util", + "winapi", +] + +[[package]] +name = "vm-migration" +version = "0.1.0" +dependencies = [ + "anyhow", + "serde", + "serde_json", + "thiserror", + "vm-memory 0.16.1", +] + +[[package]] +name = "vm-virtio" +version = "0.1.0" +dependencies = [ + "log", + "virtio-queue 0.14.0", + "vm-memory 0.16.1", +] + +[[package]] +name = "vmm" +version = "0.1.0" +dependencies = [ + "acpi_tables", + "anyhow", + "arc-swap", + "arch", + "async-trait", + "bitflags 2.6.0", + "block_util", + "chrono", + "clap", + "devices", + "epoll", + "event_monitor", + "event_notifier", + "gdbstub", + "gdbstub_arch", + "hypervisor", + "lazy_static", + "libc", + "linux-loader", + "log", + "logging", + "micro_http", + "net_util", + "once_cell", + "option_parser", + "pci", + "qcow", + "seccompiler", + "serde", + "serde_json", + "serial_buffer", + "signal-hook", + "thiserror", + "tokio", + "tracer", + "uuid", + "vfio-ioctls", + "vfio_user", + "vhdx", + "virtio-devices", + "virtio-queue 0.11.0", + "virtiofsd", + "vm-allocator", + "vm-device", + "vm-memory 0.16.1", + "vm-migration", + "vm-virtio", + "vmm-sys-util", + "zerocopy 0.6.6", +] + +[[package]] +name = "vmm-sys-util" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1435039746e20da4f8d507a72ee1b916f7b4b05af7a91c093d2c6561934ede" +dependencies = [ + "bitflags 1.3.2", + "libc", + "serde", + "serde_derive", +] + +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zerocopy" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854e949ac82d619ee9a14c66a1b674ac730422372ccb759ce0c39cabcf2bf8e6" +dependencies = [ + "byteorder", + "zerocopy-derive 0.6.6", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive 0.7.35", +] + +[[package]] +name = "zerocopy-derive" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "125139de3f6b9d625c39e2efdd73d41bdac468ccd556556440e322be0e1bbd91" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/vendor/cube-hypervisor/Cargo.toml b/vendor/cube-hypervisor/Cargo.toml new file mode 100644 index 0000000..2f90e8d --- /dev/null +++ b/vendor/cube-hypervisor/Cargo.toml @@ -0,0 +1,117 @@ +[package] +name = "cube-hypervisor" +version = "28.0.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" +default-run = "cube-hypervisor" +build = "build.rs" +license = "LICENSE-APACHE & LICENSE-BSD-3-Clause" +description = "Open source Virtual Machine Monitor (VMM) that runs on top of KVM" +homepage = "https://github.com/cloud-hypervisor/cloud-hypervisor" +# Minimum buildable version: +# Keep in sync with version in .github/workflows/build.yaml +rust-version = "1.77.0" + +[profile.release] +codegen-units = 1 +lto = true +opt-level = "s" + +[dependencies] +anyhow = "1.0.86" +api_client = { path = "api_client" } +arc-swap = "1.1" +chrono = "0.4.0" +clap = { version = "4.4.7", features = ["wrap_help", "cargo", "string"] } +epoll = "4.3.1" +event_monitor = { path = "event_monitor" } +event_notifier = { path = "event_notifier" } +hypervisor = { path = "hypervisor" } +lazy_static = "1.2" +libc = "0.2.137" +log = { version = "0.4.17", features = ["std"] } +log_json = { path = "log_json" } +logging = { path = "logging" } +option_parser = { path = "option_parser" } +rlimit = "0.9.1" +seccompiler = "0.3.0" +serde = { workspace = true, features = ["rc", "derive"] } +serde_json = "1.0.87" +signal-hook = "0.3.14" +slog = "2.7.0" +slog-scope = "4.4.0" +thiserror = "1.0.37" +tpm = { path = "tpm" } +tracer = { path = "tracer" } +vmm = { path = "vmm" } +vmm-sys-util = "0.12.1" + +[build-dependencies] +clap = { version = "4.4.7", features = ["cargo"] } + + +[dev-dependencies] +dirs = "4.0.0" +net_util = { path = "net_util" } +once_cell = "1.16.0" +serde_json = "1.0.87" +test_infra = { path = "test_infra" } +wait-timeout = "0.2.0" +vm-migration = { path = "vm-migration" } + +[features] +default = ["kvm"] +guest_debug = ["vmm/guest_debug"] +kvm = ["vmm/kvm"] +lib_support = ["vmm/lib_support"] +logger_debug = [] +mshv = ["vmm/mshv"] +tdx = ["vmm/tdx"] +tracing = ["vmm/tracing", "tracer/tracing"] + +[workspace] +members = [ + "api_client", + "arch", + "block_util", + "devices", + "event_monitor", + "event_notifier", + "hypervisor", + "logging", + "net_gen", + "net_util", + "option_parser", + "pci", + "performance-metrics", + "qcow", + "serial_buffer", + "test_infra", + "tracer", + "vfio_user", + "vhdx", + "vhost_user_block", + "vhost_user_net", + "virtio-devices", + "vm-allocator", + "vm-device", + "vm-migration", + "vm-virtio", + "vmm", + "rate_limiter", + "virtiofsd", +] + +[workspace.dependencies] +linux-loader = "0.13.0" +seccompiler = "0.3.0" +serde = "1.0.208" +vfio-ioctls = { git = "https://github.com/rust-vmm/vfio", rev = "64171f3", default-features = false } +vhost = { git = "https://github.com/rust-vmm/vhost", rev = "d983ae0" } +vhost-user-backend = { git = "https://github.com/rust-vmm/vhost", rev = "d983ae0" } +virtio-bindings = "0.1.0" +virtio-queue = "0.14.0" +virtiofsd = { path = "virtiofsd" } +vm-fdt = { git = "https://github.com/rust-vmm/vm-fdt", branch = "main" } +vm-memory = "0.16.1" +vmm-sys-util = "0.12.1" \ No newline at end of file diff --git a/vendor/cube-hypervisor/Jenkinsfile b/vendor/cube-hypervisor/Jenkinsfile new file mode 100644 index 0000000..9705e87 --- /dev/null +++ b/vendor/cube-hypervisor/Jenkinsfile @@ -0,0 +1,348 @@ +def runWorkers = true +pipeline { + agent none + stages { + stage('Early checks') { + agent { node { label 'built-in' } } + stages { + stage('Checkout') { + steps { + checkout scm + } + } + stage('Check for documentation only changes') { + when { + expression { + return docsFileOnly() + } + } + steps { + script { + runWorkers = false + echo 'Documentation only changes, no need to run the CI' + } + } + } + stage('Check for fuzzer files only changes') { + when { + expression { + return fuzzFileOnly() + } + } + steps { + script { + runWorkers = false + echo 'Fuzzer cargo files only changes, no need to run the CI' + } + } + } + stage('Check for RFC/WIP builds') { + when { + changeRequest comparator: 'REGEXP', title: '.*(rfc|RFC|wip|WIP).*' + beforeAgent true + } + steps { + error('Failing as this is marked as a WIP or RFC PR.') + } + } + stage('Cancel older builds') { + when { not { branch 'main' } } + steps { + cancelPreviousBuilds() + } + } + } + } + stage('Build') { + parallel { + stage('Worker build') { + agent { node { label 'jammy' } } + when { + beforeAgent true + expression { + return runWorkers + } + } + stages { + stage('Checkout') { + steps { + checkout scm + } + } + stage('Prepare environment') { + steps { + sh 'scripts/prepare_vdpa.sh' + } + } + stage('Run OpenAPI tests') { + steps { + sh 'scripts/run_openapi_tests.sh' + } + } + stage('Run unit tests') { + steps { + sh 'scripts/dev_cli.sh tests --unit' + } + } + stage('Run integration tests') { + options { + timeout(time: 1, unit: 'HOURS') + } + steps { + sh 'sudo modprobe openvswitch' + sh 'scripts/dev_cli.sh tests --integration' + } + } + stage('Run live-migration integration tests') { + options { + timeout(time: 1, unit: 'HOURS') + } + steps { + sh 'sudo modprobe openvswitch' + sh 'scripts/dev_cli.sh tests --integration-live-migration' + } + } + stage('Run unit tests for musl') { + steps { + sh 'scripts/dev_cli.sh tests --unit --libc musl' + } + } + stage('Run integration tests for musl') { + options { + timeout(time: 1, unit: 'HOURS') + } + steps { + sh 'sudo modprobe openvswitch' + sh 'scripts/dev_cli.sh tests --integration --libc musl' + } + } + stage('Run live-migration integration tests for musl') { + options { + timeout(time: 1, unit: 'HOURS') + } + steps { + sh 'sudo modprobe openvswitch' + sh 'scripts/dev_cli.sh tests --integration-live-migration --libc musl' + } + } + } + } + stage('AArch64 worker build') { + agent { node { label 'bionic-arm64' } } + when { + beforeAgent true + expression { + return runWorkers + } + } + environment { + AZURE_CONNECTION_STRING = credentials('46b4e7d6-315f-4cc1-8333-b58780863b9b') + } + stages { + stage('Checkout') { + steps { + checkout scm + } + } + stage('Run unit tests') { + steps { + sh 'scripts/dev_cli.sh tests --unit --libc musl' + } + } + stage('Run integration tests') { + options { + timeout(time: 1, unit: 'HOURS') + } + steps { + sh 'sudo modprobe openvswitch' + sh 'scripts/dev_cli.sh tests --integration --libc musl' + } + } + stage('Install azure-cli') { + steps { + installAzureCli('bionic', 'arm64') + } + } + stage('Download Windows image') { + steps { + sh '''#!/bin/bash -x + IMG_BASENAME=windows-11-iot-enterprise-aarch64.raw + IMG_PATH=$HOME/workloads/$IMG_BASENAME + IMG_GZ_PATH=$HOME/workloads/$IMG_BASENAME.gz + IMG_GZ_BLOB_NAME=windows-11-iot-enterprise-aarch64-9-min.raw.gz + cp "scripts/$IMG_BASENAME.sha1" "$HOME/workloads/" + pushd "$HOME/workloads" + if sha1sum "$IMG_BASENAME.sha1" --check; then + exit + fi + popd + mkdir -p "$HOME/workloads" + az storage blob download \ + --container-name private-images \ + --file "$IMG_GZ_PATH" \ + --name "$IMG_GZ_BLOB_NAME" \ + --connection-string "$AZURE_CONNECTION_STRING" + gzip -d $IMG_GZ_PATH + ''' + } + } + stage('Run Windows guest integration tests') { + options { + timeout(time: 1, unit: 'HOURS') + } + steps { + sh 'scripts/dev_cli.sh tests --integration-windows --libc musl' + } + } + } + post { + always { + sh "sudo chown -R jenkins.jenkins ${WORKSPACE}" + deleteDir() + } + } + } + stage('Worker build - Windows guest') { + agent { node { label 'jammy' } } + when { + beforeAgent true + expression { + return runWorkers + } + } + environment { + AZURE_CONNECTION_STRING = credentials('46b4e7d6-315f-4cc1-8333-b58780863b9b') + } + stages { + stage('Checkout') { + steps { + checkout scm + } + } + stage('Install azure-cli') { + steps { + installAzureCli('jammy', 'amd64') + } + } + stage('Download assets') { + steps { + sh "mkdir ${env.HOME}/workloads" + sh 'az storage blob download --container-name private-images --file "$HOME/workloads/windows-server-2019.raw" --name windows-server-2019.raw --connection-string "$AZURE_CONNECTION_STRING"' + } + } + stage('Run Windows guest integration tests') { + options { + timeout(time: 1, unit: 'HOURS') + } + steps { + sh 'scripts/dev_cli.sh tests --integration-windows' + } + } + stage('Run Windows guest integration tests for musl') { + options { + timeout(time: 1, unit: 'HOURS') + } + steps { + sh 'scripts/dev_cli.sh tests --integration-windows --libc musl' + } + } + } + } + stage('Worker build - Metrics') { + agent { node { label 'jammy-metrics' } } + when { + branch 'main' + beforeAgent true + expression { + return runWorkers + } + } + environment { + METRICS_PUBLISH_KEY = credentials('52e0945f-ce7a-43d1-87af-67d1d87cc40f') + } + stages { + stage('Checkout') { + steps { + checkout scm + } + } + stage('Run metrics tests') { + options { + timeout(time: 1, unit: 'HOURS') + } + steps { + sh 'scripts/dev_cli.sh tests --metrics -- -- --report-file /root/workloads/metrics.json' + } + } + stage('Upload metrics report') { + steps { + sh 'curl -X PUT https://cloud-hypervisor-metrics.azurewebsites.net/api/publishmetrics -H "x-functions-key: $METRICS_PUBLISH_KEY" -T ~/workloads/metrics.json' + } + } + } + } + } + } + } + post { + regression { + script { + if (env.BRANCH_NAME == 'main') { + slackSend(color: '#ff0000', message: '"main" branch build is now failing', channel: '#jenkins-ci') + } + } + } + fixed { + script { + if (env.BRANCH_NAME == 'main') { + slackSend(color: '#00ff00', message: '"main" branch build is now fixed', channel: '#jenkins-ci') + } + } + } + } +} + +def cancelPreviousBuilds() { + // Check for other instances of this particular build, cancel any that are older than the current one + def jobName = env.JOB_NAME + def currentBuildNumber = env.BUILD_NUMBER.toInteger() + def currentJob = Jenkins.instance.getItemByFullName(jobName) + + // Loop through all instances of this particular job/branch + for (def build : currentJob.builds) { + if (build.isBuilding() && (build.number.toInteger() < currentBuildNumber)) { + echo "Older build still queued. Sending kill signal to build number: ${build.number}" + build.doStop() + } + } +} + +def installAzureCli(distro, arch) { + sh 'sudo apt install -y ca-certificates curl apt-transport-https lsb-release gnupg' + sh 'curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null' + sh "echo \"deb [arch=${arch}] https://packages.microsoft.com/repos/azure-cli/ ${distro} main\" | sudo tee /etc/apt/sources.list.d/azure-cli.list" + sh 'sudo apt update' + sh 'sudo apt install -y azure-cli' +} + +def boolean docsFileOnly() { + if (env.CHANGE_TARGET == null) { + return false + } + + return sh( + returnStatus: true, + script: "git diff --name-only origin/${env.CHANGE_TARGET}... | grep -v '\\.md'" + ) != 0 +} + +def boolean fuzzFileOnly() { + if (env.CHANGE_TARGET == null) { + return false + } + + return sh( + returnStatus: true, + script: "git diff --name-only origin/${env.CHANGE_TARGET}... | grep -v -E 'fuzz/'" + ) != 0 +} diff --git a/vendor/cube-hypervisor/LICENSE-APACHE b/vendor/cube-hypervisor/LICENSE-APACHE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/vendor/cube-hypervisor/LICENSE-APACHE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/cube-hypervisor/LICENSE-BSD-3-Clause b/vendor/cube-hypervisor/LICENSE-BSD-3-Clause new file mode 100644 index 0000000..8bafca3 --- /dev/null +++ b/vendor/cube-hypervisor/LICENSE-BSD-3-Clause @@ -0,0 +1,27 @@ +// Copyright 2017 The Chromium OS Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/cube-hypervisor/LICENSE-CUBESANDBOX b/vendor/cube-hypervisor/LICENSE-CUBESANDBOX new file mode 100644 index 0000000..b6aa16c --- /dev/null +++ b/vendor/cube-hypervisor/LICENSE-CUBESANDBOX @@ -0,0 +1,468 @@ +Tencent is pleased to support the open source community by making Cube Sandbox available. + +Copyright (C) 2026 Tencent. All rights reserved. + +The open-source software included in this distribution may have been modified by Tencent (“Tencent Modifications”). All Tencent Modifications are Copyright (C) Tencent. + +Cube Sandbox is licensed under the Apache-2.0 except for the third-party components listed below. + + +Terms of the Apache-2.0: +-------------------------------------------------------------------- +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +Dependencies and Licenses: + +This open-source project, Cube Sandbox, builds upon the following open-source software components, each of which remains licensed under its original license. Certain software may include modifications made by Tencent (“Tencent Modifications”), which are Copyright (C) Tencent. + +In case you believe there have been errors in the attribution below, you may submit the concerns to us for review and correction. + + +Open Source Software Licensed under the Apache-2.0 +-------------------------------------------------------------------- +1. kata-containers +Copyright (c) kata-containers original author and authors +Please note this software has been modified by Tencent in this distribution. + +2. cloud-hypervisor +Copyright (c) cloud-hypervisor original author and authors +Please note this software has been modified by Tencent in this distribution. + + +Terms of the Apache-2.0 +-------------------------------------------------------------------- +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + +Open Source Software Licensed under the BSD-2-Clause +-------------------------------------------------------------------- +1. cilium/bpf +Copyright Authors of Cilium. All rights reserved. +cilium/bpf is made available under a dual license: BSD-2-Clause OR GPL-2.0. We have elected to use it under the terms of BSD-2-Clause. + + +Terms of the BSD-2-Clause +-------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/cube-hypervisor/MAINTAINERS.md b/vendor/cube-hypervisor/MAINTAINERS.md new file mode 100644 index 0000000..631f169 --- /dev/null +++ b/vendor/cube-hypervisor/MAINTAINERS.md @@ -0,0 +1,7 @@ +# Maintainers + +- Sebastien Boeuf - @sboeuf +- Robert Bradford - @rbradford +- Samuel Ortiz - @sameo +- Wei Liu - @liuw +- Michael Zhao - @michael2012z diff --git a/vendor/cube-hypervisor/Makefile b/vendor/cube-hypervisor/Makefile new file mode 100644 index 0000000..cc8c25b --- /dev/null +++ b/vendor/cube-hypervisor/Makefile @@ -0,0 +1,4 @@ +.PHONY: fmt + +fmt: + cargo fmt diff --git a/vendor/cube-hypervisor/README.agentdrive.md b/vendor/cube-hypervisor/README.agentdrive.md new file mode 100644 index 0000000..bb5cda4 --- /dev/null +++ b/vendor/cube-hypervisor/README.agentdrive.md @@ -0,0 +1,13 @@ +# AgentDrive 使用的 hypervisor 源码 + +来源:https://github.com/TencentCloud/CubeSandbox + +固定 commit:`4411c96ba8658a959de34693f54f4918db2a432f`(v0.7.3)。导入范围为该版本的完整 `hypervisor/` 目录,并附带仓库根目录许可证 `LICENSE-CUBESANDBOX`。原文件内容和权限保留,文件校验值记录在 `UPSTREAM.json`。 + +`runtime/sandbox-worker` 通过 Cargo 本地路径依赖使用本目录。保留独立 Cargo workspace、模块名称、KVM/PVM 实现、内置 virtio-fs 和原有构建文件。导入时没有修改上游代码。 + +许可证以原文件声明及 `LICENSE-APACHE`、`LICENSE-BSD-3-Clause`、`virtiofsd/LICENSE-*` 为准。CubeSandbox 的 Tencent 修改及 Cloud Hypervisor、rust-vmm、virtiofsd 等上游版权声明保留。 + +更新源码时需要记录新 commit,更新来源文件校验值,审查上游修改,执行 worker 构建以及桌面、MCP、分支文件和内核选择验证。本目录的修改应在此记录,并在被修改文件中注明。 + +校验命令:`python3 tests/sandbox/check_sources.py`。 diff --git a/vendor/cube-hypervisor/README.md b/vendor/cube-hypervisor/README.md new file mode 100644 index 0000000..6cd3571 --- /dev/null +++ b/vendor/cube-hypervisor/README.md @@ -0,0 +1,346 @@ +- [1. What is Cloud Hypervisor?](#1-what-is-cloud-hypervisor) + - [Objectives](#objectives) + - [High Level](#high-level) + - [Architectures](#architectures) + - [Guest OS](#guest-os) +- [2. Getting Started](#2-getting-started) + - [Host OS](#host-os) + - [Use Pre-built Binaries](#use-pre-built-binaries) + - [Packages](#packages) + - [Building from Source](#building-from-source) + - [Booting Linux](#booting-linux) + - [Firmware Booting](#firmware-booting) + - [Custom Kernel and Disk Image](#custom-kernel-and-disk-image) + - [Building your Kernel](#building-your-kernel) + - [Disk image](#disk-image) + - [Booting the guest VM](#booting-the-guest-vm) +- [3. Status](#3-status) + - [Hot Plug](#hot-plug) + - [Device Model](#device-model) + - [Roadmap](#roadmap) +- [4. Relationship with _Rust VMM_ Project](#4-relationship-with-rust-vmm-project) + - [Differences with Firecracker and crosvm](#differences-with-firecracker-and-crosvm) +- [5. Community](#5-community) + - [Contribute](#contribute) + - [Slack](#slack) + - [Mailing list](#mailing-list) + - [Security issues](#security-issues) + +# 1. What is Cloud Hypervisor? + +Cloud Hypervisor is an open source Virtual Machine Monitor (VMM) that runs on +top of the [KVM](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt) +hypervisor and the Microsoft Hypervisor (MSHV). + +The project focuses on running modern, _Cloud Workloads_, on specific, common, +hardware architectures. In this case _Cloud Workloads_ refers to those that are +run by customers inside a Cloud Service Provider. This means modern operating +systems with most I/O handled by +paravirtualised devices (e.g. _virtio_), no requirement for legacy devices, and +64-bit CPUs. + +Cloud Hypervisor is implemented in [Rust](https://www.rust-lang.org/) and is +based on the [Rust VMM](https://github.com/rust-vmm) crates. + +## Objectives + +### High Level + +- Runs on KVM or MSHV +- Minimal emulation +- Low latency +- Low memory footprint +- Low complexity +- High performance +- Small attack surface +- 64-bit support only +- CPU, memory, PCI hotplug +- Machine to machine migration + +### Architectures + +Cloud Hypervisor supports the `x86-64` and `AArch64` architectures. There are +minor differences in functionality between the two architectures +(see [#1125](https://github.com/cloud-hypervisor/cloud-hypervisor/issues/1125)). + +### Guest OS + +Cloud Hypervisor supports `64-bit Linux` and Windows 10/Windows Server 2019. + +# 2. Getting Started + +The following sections describe how to build and run Cloud Hypervisor on the +`x86-64` platform. For getting started on the `AArch64` platform, please refer +to the +[AArch64 documentation](docs/arm64.md). + +## Host OS + +For required KVM functionality the minimum host kernel version is 4.11. For +adequate performance the minimum recommended host kernel version is 5.6. The +majority of the CI currently tests with kernel version 5.15. + +## Use Pre-built Binaries + +The recommended approach to getting started with Cloud Hypervisor is by using a +pre-built binary. Binaries are available for the [latest +release](https://github.com/cloud-hypervisor/cloud-hypervisor/releases/latest). +Use `cloud-hypervisor-static` for `x86-64` or `cloud-hypervisor-static-aarch64` +for `AArch64` platform. + +## Packages + +For convenience, packages are also available targeting some popular Linux +distributions. This is thanks to the [Open Build +Service](https://build.opensuse.org). The [OBS +README](https://github.com/cloud-hypervisor/obs-packaging) explains how to +enable the repository in a supported Linux distribution and install Cloud Hypervisor +and accompanying packages. Please report any packaging issues in the +[obs-packaging](https://github.com/cloud-hypervisor/obs-packaging) repository. + +## Building from Source + +Please see the [instructions for building from source](docs/building.md) if you +do not wish to use the pre-built binaries. + +## Booting Linux + +The instructions below are for the `x86-64` platform. For `AArch64` please see +the [AArch64 specific documentation](docs/arm64.md). + +Cloud Hypervisor supports direct kernel boot (if the kernel is built with PVH +support) or booting via a firmware (either [Rust Hypervisor +Firmware](https://github.com/cloud-hypervisor/rust-hypervisor-firmware) or an +edk2 UEFI firmware called `CLOUDHV`.) + +Binary builds of the firmware files are available for the latest release of +[Rust Hyperivor +Firmware](https://github.com/cloud-hypervisor/rust-hypervisor-firmware/releases/latest) +and [our edk2 +repository](https://github.com/cloud-hypervisor/edk2/releases/latest) + +The choice of firmware depends on your guest OS choice; some experimentation +may be required. + +### Firmware Booting + +Cloud Hypervisor supports booting disk images containing all needed components +to run cloud workloads, a.k.a. cloud images. + +The following sample commands will download an Ubuntu Cloud image, converting +it into a format that Cloud Hypervisor can use and a firmware to boot the image +with. + +```shell +$ wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img +$ qemu-img convert -p -f qcow2 -O raw focal-server-cloudimg-amd64.img focal-server-cloudimg-amd64.raw +$ wget https://github.com/cloud-hypervisor/rust-hypervisor-firmware/releases/download/0.4.2/hypervisor-fw +``` + +The Ubuntu cloud images do not ship with a default password so it necessary to +use a `cloud-init` disk image to customise the image on the first boot. A basic +`cloud-init` image is generated by this [script](scripts/create-cloud-init.sh). +This seeds the image with a default username/password of `cloud/cloud123`. It +is only necessary to add this disk image on the first boot. + +```shell +$ sudo setcap cap_net_admin+ep ./cloud-hypervisor +$ ./create-cloud-init.sh +$ ./cloud-hypervisor \ + --kernel ./hypervisor-fw \ + --disk path=focal-server-cloudimg-amd64.raw path=/tmp/ubuntu-cloudinit.img \ + --cpus boot=4 \ + --memory size=1024M \ + --net "tap=,mac=,ip=,mask=" +``` + +If access to the firmware messages or interaction with the boot loader (e.g. +GRUB) is required then it necessary to switch to the serial console instead of +`virtio-console`. + +```shell +$ ./cloud-hypervisor \ + --kernel ./hypervisor-fw \ + --disk path=focal-server-cloudimg-amd64.raw path=/tmp/ubuntu-cloudinit.img \ + --cpus boot=4 \ + --memory size=1024M \ + --net "tap=,mac=,ip=,mask=" \ + --serial tty \ + --console off +``` + +### Custom Kernel and Disk Image + +#### Building your Kernel + +Cloud Hypervisor also supports direct kernel boot into a `vmlinux` ELF kernel (compiled with PVH support). In order to support development there is a custom branch; however provided the required options are enabled any recent kernel will suffice. + +To build the kernel: + +```shell +# Clone the Cloud Hypervisor Linux branch +$ git clone --depth 1 https://github.com/cloud-hypervisor/linux.git -b ch-5.15.12 linux-cloud-hypervisor +$ pushd linux-cloud-hypervisor +# Use the cloud-hypervisor kernel config to build your kernel +$ wget https://raw.githubusercontent.com/cloud-hypervisor/cloud-hypervisor/main/resources/linux-config-x86_64 +$ cp linux-config-x86_64 .config +$ KCFLAGS="-Wa,-mx86-used-note=no" make bzImage -j `nproc` +$ popd +``` + +The `vmlinux` kernel image will then be located at +`linux-cloud-hypervisor/arch/x86/boot/compressed/vmlinux.bin`. + +#### Disk image + +For the disk image the same Ubuntu image as before can be used. This contains +an `ext4` root filesystem. + +```shell +$ wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img +$ qemu-img convert -p -f qcow2 -O raw focal-server-cloudimg-amd64.img focal-server-cloudimg-amd64.raw +``` + +#### Booting the guest VM + +These sample commands boot the disk image using the custom kernel whilst also +supplying the desired kernel command line. + +```shell +$ sudo setcap cap_net_admin+ep ./cloud-hypervisor +$ ./create-cloud-init.sh +$ ./cloud-hypervisor \ + --kernel ./linux-cloud-hypervisor/arch/x86/boot/compressed/vmlinux.bin \ + --disk path=focal-server-cloudimg-amd64.raw path=/tmp/ubuntu-cloudinit.img \ + --cmdline "console=hvc0 root=/dev/vda1 rw" \ + --cpus boot=4 \ + --memory size=1024M \ + --net "tap=,mac=,ip=,mask=" +``` + +If earlier kernel messages are required the serial console should be used instead of `virtio-console`. + +```cloud-hypervisor \ + --kernel ./linux-cloud-hypervisor/arch/x86/boot/compressed/vmlinux.bin \ + --console off \ + --serial tty \ + --disk path=focal-server-cloudimg-amd64.raw \ + --cmdline "console=ttyS0 root=/dev/vda1 rw" \ + --cpus boot=4 \ + --memory size=1024M \ + --net "tap=,mac=,ip=,mask=" +``` + +# 3. Status + +Cloud Hypervisor is under active development. The following stability +guarantees are currently made: + +* The API (including command line options) will not be removed or changed in a + breaking way without a minimum of 2 major releases notice. Where possible + warnings will be given about the use of deprecated functionality and the + deprecations will be documented in the release notes. + +* Point releases will be made between individual releases where there are + substantial bug fixes or security issues that need to be fixed. These point + releases will only include bug fixes. + +Currently the following items are **not** guaranteed across updates: + +* Snapshot/restore is not supported across different versions +* Live migration is not supported across different versions +* The following features are considered experimental and may change + substantially between releases: TDX, vfio-user, vDPA. + +Further details can be found in the [release documentation](docs/releases.md). + +As of 2022-10-13, the following cloud images are supported: + +- [Ubuntu Bionic](https://cloud-images.ubuntu.com/bionic/current/) (bionic-server-cloudimg-amd64.img) +- [Ubuntu Focal](https://cloud-images.ubuntu.com/focal/current/) (focal-server-cloudimg-amd64.img) +- [Ubuntu Jammy](https://cloud-images.ubuntu.com/jammy/current/) (jammy-server-cloudimg-amd64.img ) +- [Fedora 36](https://fedora.mirrorservice.org/fedora/linux/releases/36/Cloud/x86_64/images/) (Fedora-Cloud-Base-36-1.5.x86_64.raw.xz) + +Direct kernel boot to userspace should work with a rootfs from most +distributions although you may need to enable exotic filesystem types in the +reference kernel configuration (e.g. XFS or btrfs.) + +## Hot Plug + +Cloud Hypervisor supports hotplug of CPUs, passthrough devices (VFIO), +`virtio-{net,block,pmem,fs,vsock}` and memory resizing. This +[document](docs/hotplug.md) details how to add devices to a running VM. + +## Device Model + +Details of the device model can be found in this +[documentation](docs/device_model.md). + +## Roadmap + +The project roadmap is tracked through a [GitHub +project](https://github.com/orgs/cloud-hypervisor/projects/6). + +# 4. Relationship with _Rust VMM_ Project + +In order to satisfy the design goal of having a high-performance, +security-focused hypervisor the decision was made to use the +[Rust](https://www.rust-lang.org/) programming language. The language's strong +focus on memory and thread safety makes it an ideal candidate for implementing +VMMs. + +Instead of implementing the VMM components from scratch, Cloud Hypervisor is +importing the [Rust VMM](https://github.com/rust-vmm) crates, and sharing code +and architecture together with other VMMs like e.g. Amazon's +[Firecracker](https://firecracker-microvm.github.io/) and Google's +[crosvm](https://chromium.googlesource.com/chromiumos/platform/crosvm/). + +Cloud Hypervisor embraces the _Rust VMM_ project's goals, which is to be able +to share and re-use as many virtualization crates as possible. + +## Differences with Firecracker and crosvm + +A large part of the Cloud Hypervisor code is based on either the Firecracker or +the crosvm project's implementations. Both of these are VMMs written in Rust +with a focus on safety and security, like Cloud Hypervisor. + +The goal of the Cloud Hypervisor project differs from the aforementioned +projects in that it aims to be a general purpose VMM for _Cloud Workloads_ and +not limited to container/serverless or client workloads. + +The Cloud Hypervisor community thanks the communities of both the Firecracker +and crosvm projects for their excellent work. + +# 5. Community + +The Cloud Hypervisor project follows the governance, and community guidelines +described in the [Community](https://github.com/cloud-hypervisor/community) +repository. + +## Contribute + +The project strongly believes in building a global, diverse and collaborative +community around the Cloud Hypervisor project. Anyone who is interested in +[contributing](CONTRIBUTING.md) to the project is welcome to participate. + +Contributing to a open source project like Cloud Hypervisor covers a lot more +than just sending code. Testing, documentation, pull request +reviews, bug reports, feature requests, project improvement suggestions, etc, +are all equal and welcome means of contribution. See the +[CONTRIBUTING](CONTRIBUTING.md) document for more details. + +## Slack + +Get an [invite to our Slack channel](https://join.slack.com/t/cloud-hypervisor/shared_invite/enQtNjY3MTE3MDkwNDQ4LWQ1MTA1ZDVmODkwMWQ1MTRhYzk4ZGNlN2UwNTI3ZmFlODU0OTcwOWZjMTkwZDExYWE3YjFmNzgzY2FmNDAyMjI) +and [join us on Slack](https://cloud-hypervisor.slack.com/). + +## Mailing list + +Please report bugs using the [GitHub issue +tracker](https://github.com/cloud-hypervisor/cloud-hypervisor/issues) but for +broader community discussions you may use our [mailing +list](https://lists.cloudhypervisor.org/g/dev/). + +## Security issues + +Please contact the maintainers listed in the MAINTAINERS.md file with security issues. diff --git a/vendor/cube-hypervisor/UPSTREAM.json b/vendor/cube-hypervisor/UPSTREAM.json new file mode 100644 index 0000000..0c0401c --- /dev/null +++ b/vendor/cube-hypervisor/UPSTREAM.json @@ -0,0 +1,397 @@ +{ + "repository": "https://github.com/TencentCloud/CubeSandbox", + "commit": "4411c96ba8658a959de34693f54f4918db2a432f", + "subdirectory": "hypervisor", + "files_sha256": { + ".github/ISSUE_TEMPLATE/bug_report.md": "1431ccf406fe8166b2343356eadda8599a6e319bd2a426c5fa30df0f210cea2f", + ".github/dependabot.yml": "de1d3dc68293e7a659b24f4a2eddb145f192a1c4d3d8feb2b60a380f613268d1", + ".github/workflows/audit.yaml": "9dee536653e08abc37de9af3fd8c18627a1e9e3c11b87af2a402c12ab68a4e2a", + ".github/workflows/build.yaml": "3aa8e42c65ce1ecee233eff828a9ecb30774503823bceba7a3638fa9df77c689", + ".github/workflows/dco.yaml": "e182d7108113eeab24c6f314414feeca817a6f1423e448ffef9da76c20e79b16", + ".github/workflows/docker-image.yaml": "f9e43e1a924b85855ad1d662cd4b1b01c447cf022fc7c9b7c54b998e34629c87", + ".github/workflows/fuzz-build.yaml": "7c56b4b00e4608c330d80926345fe3d61738d996abc499f9e236a78d80708679", + ".github/workflows/quality.yaml": "4628c1daf474fe73085eb664204b4227aa9d89cbed771d77437b652f7a793607", + ".github/workflows/release.yaml": "cc82cb07218229982abf2e7dc0e40f893e7dc334d1c93084f351eea3d83b0aa4", + ".gitignore": "d394da81affc73405ae8831ecff1dd4353318548963f7c13d84a449f77e4dc08", + ".rustfmt.toml": "fb15e8e45ebf082778eb6032eafb480cb52624bb8d415ef6db398571c3ae53ad", + "CODEOWNERS": "b68dc6affcc8052e5bdb72c492bf270d4dde2087975b44bbb4249a1b57385c7b", + "CODE_OF_CONDUCT.md": "b816184a8177ca81bfd99cb7079e331e663fb857e1ecdd4db4bc74a14bd8d9f5", + "CONTRIBUTING.md": "2a87725b792b96921cae13a71c33244f20e5347201d69b24ba296cff1602b470", + "CREDITS.md": "d1e0422b13f2b5941a8e86727e670ff36194c8bb0d3450a05ce3eec69f380f82", + "Cargo.lock": "bc28d5e163867aa13002e818208a260bf52b3256f1e2b0a04ea0e480ec4a249b", + "Cargo.toml": "e820f6f29d4432d873877bc0177f7b02f2174887cf1d999170df1a688af1d23f", + "Jenkinsfile": "2059916c273bd73a3f83fa6893024c83836d899ae550d166fd070d9c7b8ef1e5", + "LICENSE-APACHE": "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", + "LICENSE-BSD-3-Clause": "a6d3ebd1c2f37d4fd83d0676621f695fc0cc2d8c6e646cdbb831b46e0650c208", + "LICENSE-CUBESANDBOX": "ec4e72bcae6fbd76307517767f046f9c724fbf8aaee735b60310b21e46cd959a", + "MAINTAINERS.md": "15574558454c222fcbc5a534faf31593a6a576dc3c74f7a0ef9e885b7b6590a8", + "Makefile": "7d3ffbc9237062e58446659bd0f9d438ac0e457d777fef5113e373241d1995c4", + "README.md": "0cfff81b75903bd01ae1f3b5cd8383a0b4ddfc149c172b17bd313ba28d038bed", + "acpi_tables/Cargo.toml": "bf04a480d0777abf0bffda66dc85f384286cba2d87b3e611df5de368d727480d", + "acpi_tables/src/aml.rs": "cb4d12214cefec958baeba4161ed767046de8d595d1737af9576bc945f4f6a0a", + "acpi_tables/src/lib.rs": "7880506439fda93f832f80ccdef2becd2299ff8c5722a80d00cada98670ad6a3", + "acpi_tables/src/rsdp.rs": "d809605471f10fae67031d7ec2658ae97d9bc7235697f6779feda68c0e849ab5", + "acpi_tables/src/sdt.rs": "773f2dac07ec01f5ec03d356d63539ebbf986af6c7574183ae1289134d50988b", + "api_client/Cargo.toml": "2e3eb20ad1b051a7e45502667a95f0c2fbe9574ad28f6092cb0344dabd453df0", + "api_client/src/lib.rs": "c2429288a035f5fdc8d93c87d37c8332e72a435088e04e926272552c4fb1d173", + "arch/Cargo.toml": "de66bae2b2b0e2f63b7f694e4edc2638882436448a9c65fc7acc23f6c3d7b910", + "arch/src/aarch64/fdt.rs": "137a9bcb14894f7cf730c72cab1e18f4922871215061fc963e0a108ee5f1fbf8", + "arch/src/aarch64/layout.rs": "287f73e4d1fdf3d9a3220c4cd7a012b90d0c7e301f924cfd7b08ee1d08992ce4", + "arch/src/aarch64/mod.rs": "ebea1195d2c5bb67db4073e05add66510c5ba70dbe475df1e8905469d90d2dfb", + "arch/src/aarch64/regs.rs": "d83a71e0cb67a408910108d26a0832cd2677f9db0c68d213c04f978d7fff50a3", + "arch/src/aarch64/uefi.rs": "58b7f0268c3f73853116ebe9a7216ecc6aadc4b4a1f80758c1a8f63d0d906415", + "arch/src/lib.rs": "3e723781a63cad6228677edc405e0d4f2ec58a854501a4c36b25ca3d42b90e81", + "arch/src/x86_64/cpuid_filter.rs": "f61c2c6b62b4c1b1a55ef5c53bc9defb0e97557064a75cd3a0897fc16a6c5410", + "arch/src/x86_64/interrupts.rs": "179a30491461e5af26d5a4ba4116669d6ec070499cbd13f16bc87351a19314dc", + "arch/src/x86_64/layout.rs": "aa3e219e91b063df8cd23b9a33c1172b2fdf43a4524941fa630a2d1d3a7b1e96", + "arch/src/x86_64/mod.rs": "1a6a54ed38cca65bebe583a173b098ad95ba419285d9388d926911c4987e9de8", + "arch/src/x86_64/mpspec.rs": "e11e0e13ab6babd095ab2ed803ce355abca2c168a5ad1506b58b5a3f50082eb4", + "arch/src/x86_64/mptable.rs": "da5e318ef808fa66684727c35a1a64f4122364fd56a92db615e9ec7b50eb5d48", + "arch/src/x86_64/regs.rs": "2398b9bbdfef8b5c0b03241e1153b3466a9ebf10a40866cf9ad37c62f66177ea", + "arch/src/x86_64/smbios.rs": "167ec3ca057fd57747d52debfd2382c4e33c61e252985a6791fe20420de17097", + "arch/src/x86_64/tdx/mod.rs": "39678b3161e7c8df84b14affa78dff19b1fa13366597b751e893c3573e8471f9", + "block_util/Cargo.toml": "1af5a47ae751814d1c7ef891ea39249b5c08de64bed820730fc875e2cd6e6e32", + "block_util/src/async_io.rs": "03f60734437f771248e50e30447390eec94b9fd383e9a922501189bdac8e2d1b", + "block_util/src/fixed_vhd_async.rs": "9d3b05f115be82be2b203d2bbbc2eee2e4f32bc212e37be13d2d0f5ba7158ccb", + "block_util/src/fixed_vhd_sync.rs": "d610786bb87fc81bf188c8c1a54150ad384691c46825a4d7aca33194f59f1582", + "block_util/src/lib.rs": "af25178904a85db8fd63c18892742513875c515e256bbef0d86d62087c87102b", + "block_util/src/qcow_sync.rs": "4aca655c79c768d2d085581d314007773d91db30adbcc3a4b51bfa12a55702c4", + "block_util/src/raw_async.rs": "54b76f81da1c786142dd0be21b1296dfc10cf2c2a5c7faf605adaf26b516a74a", + "block_util/src/raw_sync.rs": "101e44ca80103f375aca832d022a8463a235a3f4f99a1013feb4d8bc4fce30bd", + "block_util/src/vhd.rs": "f1f28f32e0fea91107be6e1d66f7ea24c2f85248c21b678c20696e5d47b76e3e", + "block_util/src/vhdx_sync.rs": "b43133b7647568a36c325b8f43414df38c88d0c3cd310ee72a7fecba772ba8ad", + "build.rs": "95515e4539367961c579bfd0e7ac5e7e2598565079e4ebf289b741ab306b1977", + "devices/Cargo.toml": "59cc9bc48173433b00f895cba64681b044fd17f9c05619edf8a58d28b0dd2303", + "devices/src/acpi.rs": "1527e65aa0e1bed6db2d7b3bb1e202dcfa175e6c6443032b119659972e35189d", + "devices/src/gic.rs": "cefb5b4dfb76e5e1e65d54faa17bdca0e6162d978ec1752cfa808ba9da46f9d1", + "devices/src/interrupt_controller.rs": "95a68f7a0f7539cc22545ce56a734e9575865499723cc62549919045286db692", + "devices/src/ioapic.rs": "7d718ac9a4f5b360de04d55d2ec9ad2de265bfc49434bad4fa56441f33d3c63e", + "devices/src/ivshmem.rs": "486aba99ccb07d6b15d3c64f34661521765269d70e98562f46b6facb7ef14d3f", + "devices/src/legacy/cmos.rs": "f081f3881270514dc0672564e7586bbf574576a2f8221f0de2449c63a8c7b248", + "devices/src/legacy/debug_port.rs": "3cba31dcc25e69fc90328bc80376f211567a4924dec52df3b0d7a812147b2117", + "devices/src/legacy/fwdebug.rs": "a67c86bed3a689ac719fddad5239471705a72ea4a9892bf3d868f5c3c743719d", + "devices/src/legacy/gpio_pl061.rs": "5ae4ab3f3b8195f21a9e6d28721e5c0aad307c739cae0e88ae89f17c283e510c", + "devices/src/legacy/i8042.rs": "03cc8937838bb74effb9b188d8922a9bd3fdb1b7fc49017742729c04612da722", + "devices/src/legacy/mod.rs": "8de7c64bab1b85e99f9bec947cdc657e670761037f07e59a862c5970fedc406d", + "devices/src/legacy/rtc_pl031.rs": "c1db9cd4487615af2e78382092ffe1cd183e5822759bfaa7cd502200a47f32db", + "devices/src/legacy/serial.rs": "b4897e8d2469719281856b77f72112344fb4d1554e6848e5adf3631887f219c1", + "devices/src/legacy/sys_ctrl.rs": "7ccea5ee720cc2d715a20c11b925f34a59db000909ab967f964a7af2a42685c4", + "devices/src/legacy/uart_pl011.rs": "3dfa6989b45d4809a01ff40801ad152b5439076beddc329de4e91a839cce113b", + "devices/src/lib.rs": "c4752bf5f187e5dfa93c69030ac6b42b9f1aaeb4f4cd1472f111c8e85ad6d673", + "devices/src/pvpanic.rs": "c929e631580b64e95a2938ecfa2282bf9ff63207ca1ab15b91355e9add28f6e7", + "devices/src/tpm.rs": "cdf812180cff76e4aa729006c36c01c5e017fc5611933addf54652530f772a59", + "docs/COPYING.docs": "f85683427e83cf33106e030b03294bdacd7439b4660ccfe93b09b96c525922e6", + "docs/api.md": "28a766dc09a64e40ed6c357380699056a1200ff73a53f32ebf2ff22b29ab8c72", + "docs/arm64.md": "be3cecdd4ec5af92d46c0ddc0bac1a370c1c92f93280e14f32a5f73010059a61", + "docs/balloon.md": "31f38f803b5630519a1bb24034357b913d45d071de952527abda6955373daf41", + "docs/building.md": "856c7bd69cf939025ae52787baee6648fe6ed2f3d0806f93a9f6f4ca9e1c95a8", + "docs/cpu.md": "24e90ed0e80a6e652e1991db2dd10078983be864db5aae54c99bc891574292a4", + "docs/custom-image.md": "01ea6e9266ad9e92d5d96086db98898a409854aa50e9d5ec21ed30693962f98d", + "docs/debug-port.md": "343f7d2e7a2c3735e2cb59c6395ef494b11cf3bd7ccaa9f3feb76fe0debc778c", + "docs/device_model.md": "af7f92a38c1467ca845f79a6a9272a8f95feed1563ecffd9419d419bd90aaba1", + "docs/fs.md": "f386859c2ccc13f4c260be11318ca71973f8a197f6aa5ac6318853ed8cdd4739", + "docs/fuzzing.md": "ece23f84c3ee331747ab91c9d0eb797ffcd91cca86d6b126dfe5c9e26646b795", + "docs/gdb.md": "b06918ca5937f6c78300e94ce07f7a8680d6124ea1ce5ec4525d0f9f48f771c4", + "docs/hotplug.md": "4927c6015e34e048a1b60f30ac333ffd26e7dedc240133c747a885a1d63a47d2", + "docs/intel_sgx.md": "7b703a16e5a71858186ae3986cbdf6178c7bdcacd9aa7fc16a02bb289753ce6f", + "docs/intel_tdx.md": "353fd3cd0db95aadc20f6ddf04a92d2e301deed95b8a4ae6aa853dc4984b99a1", + "docs/io_throttling.md": "d46f68aadaf04ed0efc894a5173673b585b94f482148f56dbaff8e302da3740b", + "docs/iommu.md": "9ea871e1f00759920e8aec0a645e0c33c194d1498e17a25b175ea55f8b1a95dc", + "docs/live_migration.md": "0c640e93d92f1b48123648a06fef2a7c6caa7f50bdf8ab07f886a162edd8820d", + "docs/logging.md": "b3814a06e1e6cfe29d64324cf200286f464944cf3be0469f0dbb95e3577c4f6b", + "docs/macvtap-bridge.md": "cf29b36d7345f35992eb5af2838f505f81077f9cd59b3392eaa5cc39eaf29dee", + "docs/memory.md": "7460f724c14b21664351891b7da3a06d9edc573b215a275ea0c60338dbadef97", + "docs/performance_metrics.md": "580121c5a8c8779dc516f57aeae9d5f4e500cecc6db3d6ea2b70f72095820c03", + "docs/profiling.md": "f9f9178f63fd68f6dd18c8237e19023f3990faa7879338f350dd084e12336440", + "docs/releases.md": "e5d7201b03b7d3adc5e1066fb94a1cfbf4e2fd86a29f47a7ae9974a376cf514c", + "docs/seccomp.md": "199290981c0cf72f4253d0746408546b933013aa6341468745693263f6e54bf2", + "docs/snapshot_restore.md": "49f5db85171f182c130e9feb3a06b5c95b3f3baa22fcf9a14505eb2e67409e5c", + "docs/tpm.md": "6a1c1c8af33e0133cc51ad904b134f702fa603ab126238d593c237d3de3d489b", + "docs/tracing.md": "6bfce5c7ec9fe48a35abdf14c1ee447387bcab4cff9fa0456b66b82b0c680530", + "docs/uefi.md": "23a7f725bf83ad2b28fd5fdb6e09d8f8c01cad358e98c6a2c4fa899bc249e722", + "docs/vdpa.md": "9928281adb01a1ae1ccc8b46b28b138f3eb39703b229d8d6522d94c54dea82b7", + "docs/vfio-user.md": "f30f7e80038790e75aba7efa7761102a7aed45724b680149ba21130803d329f4", + "docs/vfio.md": "58a2e0f852cd782a8b69568ca9a5714ae4f6d74802652ddf906b92f0a41982b3", + "docs/vhost-user-blk-testing.md": "1eef86377372fc4446822e791063b1244f84ce7151c87e7c9c969af88d11c05a", + "docs/vhost-user-net-testing.md": "a96c9e8a2397d6ffdf0625453d82c5d21bcd20b8554ffdc821507e53b3f6849e", + "docs/virtiofs-root.md": "da8f5b09ff70d7cf6991e70163e9a13583bd1876bdb8b3c5880a80627fc79d01", + "docs/vsock.md": "4776ba1dd9906d1c0e0ba6271c839a8af4b2fc0914891fe578e3b539f5d9abf8", + "docs/windows.md": "2fc5b5392b13cc364360331841431b2724a3173ff141dafe3c875ab4742d9d69", + "event_monitor/Cargo.toml": "bffaa8d518175f8f1de1da50eb519d7790e057a3226ba163e918c78ca94dbb26", + "event_monitor/src/lib.rs": "665fdccc63674e42ee5591a7fdcc127ce6c9cabe81781a8c75db0594ab7596dd", + "event_notifier/Cargo.toml": "f660555a1609a8e07b3fd5eed73b2f857a30bb6e639cc418b98382378912f193", + "event_notifier/src/lib.rs": "ceb3035698fc3c5e92976ad9fb99c06b2b68adf1a21dc1400aff4daedd36298b", + "fix_lock.py": "79e31156bc38e75accb76011ae3c2382d6502cecf6a22f827a3c8e524492f7b7", + "fuzz/.gitignore": "254510c2c60d231ec3a5ecf9fe7f0c9fd5152ae7a09e2cebefaee9ec01a4b6c4", + "fuzz/Cargo.toml": "7822135cdee8d8ae72273c6f3168eb6f98fcb0aa07b8260479322a99608d8e0b", + "fuzz/fuzz_targets/balloon.rs": "a14840169804c5068925c2ee9a9f87ebcbc2316c0337df32e3e929bbc3465a62", + "fuzz/fuzz_targets/block.rs": "8ae510b436e7d71cb17668a55b873027a9764b055ace63cfaa5ae30df4d86142", + "fuzz/fuzz_targets/cmos.rs": "95c79a9017a07206f2a8489ea4ecacd7e8d6a4d5e928e9d0ef2d9860ba9cc65f", + "fuzz/fuzz_targets/console.rs": "91a8f9e45272cfb5187e6d79a9abc2d427026a331d19afd08f94df575b4d54e8", + "fuzz/fuzz_targets/http_api.rs": "ebb3e163efe7bca7de35e8d22158c09f9561e315462d5589af62a5a69a40a099", + "fuzz/fuzz_targets/iommu.rs": "e1aa001974128e01fb530f7c8f9bb07ae6e62648104495ee55e852ad0e8758ed", + "fuzz/fuzz_targets/mem.rs": "32b637505c0788332f30c45f832d56f1540199df493b8bfda4856a917acb1db8", + "fuzz/fuzz_targets/pmem.rs": "529e5b4ff0ffd841a0b9ee6c3281fc985f1956d1cc8e04e07e4f0a1f3d7ed24b", + "fuzz/fuzz_targets/qcow.rs": "bf309af241b6c6b0d39c50681eb783d7b5bd2c179f290a8c79769f0f508c943b", + "fuzz/fuzz_targets/rng.rs": "e9482de609daf590523491498ea5b3b60a8fdaffbe1c0b69be944589a20481a0", + "fuzz/fuzz_targets/serial.rs": "7d899e131c3eedea1edaf24bfefc86879e94195fc18f2cc9364a61cdc2a28147", + "fuzz/fuzz_targets/vhdx.rs": "5cd766e943be97d424172ccc258018d402046cb01f6614ef5d1c4c13e2d90dd1", + "fuzz/fuzz_targets/watchdog.rs": "9e145e0cd79f04042316778231c47ca5bd8eab7e66e80e479335d4df44bd6bdc", + "hypervisor/Cargo.toml": "1648e5a6bfc5e54483ecfa0fd6252cd159fcb98559e0a8b72c229a33183f900b", + "hypervisor/src/arch/aarch64/gic.rs": "fd5540e02b7f18c063f3b97c3b53668d64fb81e1619709350fc4f983943fef0d", + "hypervisor/src/arch/aarch64/mod.rs": "a0dcd44df2af6b7472873f795cd1dd2d1e2dfea250db9f13cb303100c6755ae1", + "hypervisor/src/arch/emulator/mod.rs": "096e825f8c80f8ffc3d8261dc9aed66cf83d05722ff82b640b7868a519f8fcd6", + "hypervisor/src/arch/mod.rs": "5b0b9ee61a1f7b1db7731166d9cbd201d6d739aaff8ddbe64496f02f9e24dc9f", + "hypervisor/src/arch/x86/emulator/instructions/cmp.rs": "6d665f28124e100219e7c1a2ebc83cdbf8a92c3acc2ebeffc6744993e771568f", + "hypervisor/src/arch/x86/emulator/instructions/mod.rs": "9d730f48fad5f1538929f544e1ba4682b74760ac96ef6b6b0a9b8e8e4211f18f", + "hypervisor/src/arch/x86/emulator/instructions/mov.rs": "41fc13a1b700011fc42f4e887130a287763096eaf85e7b295f1b34305f5e7292", + "hypervisor/src/arch/x86/emulator/instructions/movs.rs": "d3f9a45f3b682eed05bb7bf43b19c135d13e0471188a055b86ccb102cfeaaad7", + "hypervisor/src/arch/x86/emulator/instructions/or.rs": "f13c7445b0a1396d87a678057b0d4a0ca94716d7237072328dc8c0d7abb79015", + "hypervisor/src/arch/x86/emulator/mod.rs": "e2b8c66d3115f11c09ac8fbe740eac7fa513cf6fd31c16395ce38be0809e0007", + "hypervisor/src/arch/x86/gdt.rs": "7c5a45cf5d3290e49ad6412b1a5c2c520ae87832d5d90dd4e282659cf336934d", + "hypervisor/src/arch/x86/mod.rs": "82462872b275d58732220d1d2d94e8404124fb36ff97ab591ee9d1ae6d9ecef7", + "hypervisor/src/arch/x86/msr_index.rs": "c770541b9237e9e804a764c5c1070be42a58f7c3853683235be6b21bffb0ee3c", + "hypervisor/src/arch/x86/regs.rs": "9b66855eb0eb70817f4cb9bdbe42efeb60adbb297b1c62bcff614cdbdb5512cb", + "hypervisor/src/cpu.rs": "ef5079787ba0129d444eae2946c64e377eeab1af1ede4674d89ad8ff9e166ed1", + "hypervisor/src/device.rs": "2ab1299a85cba2d8b921876f7031fe578cd53fae06f7b6f55244b942431ffced", + "hypervisor/src/hypervisor.rs": "0d69e3fb39e357d6e1c956f1949aba2287efc5a93423e3d03df8fa9118b4a66b", + "hypervisor/src/kvm/aarch64/gic/dist_regs.rs": "259dfdab1921d4e2a3a647ea882b0a713503ac4555100afb78d58c5e65ee940b", + "hypervisor/src/kvm/aarch64/gic/icc_regs.rs": "410eb5dac6455141f0dca9cd12270faa12154165aaa80a8aea20dacdbc6d6d8c", + "hypervisor/src/kvm/aarch64/gic/mod.rs": "d4b1bfebc9a3053d77319312b8c1c57b4256e08e0beff7196dc9e3c7203fe283", + "hypervisor/src/kvm/aarch64/gic/redist_regs.rs": "ba3fc9dd586c5f78eb6f3821b68d8753771667d4e1c3cf706358679764729c4f", + "hypervisor/src/kvm/aarch64/mod.rs": "13d6daf06b0612e1f09b56b1c82944c68cb5f85303613641246e23c40391a84c", + "hypervisor/src/kvm/mod.rs": "80ca9730c3495288fa7e622101d6c7e01b1b8adde4083bad18efeb12f1a018bb", + "hypervisor/src/kvm/x86_64/mod.rs": "c76ca1b2a5ec14fa4312844770398112ca13e85d83374d5e462cfd55c4807ec6", + "hypervisor/src/lib.rs": "7ebff35843c24d490f6d79fa49d2ccbaed05ab9c8d2db5de63f40be9c399822d", + "hypervisor/src/mshv/mod.rs": "8c9ac8f5ac498fedff8a6a46cae2c9d23c2e277f68ba0520da304ff88cdfe6fe", + "hypervisor/src/mshv/x86_64/mod.rs": "35d480cfb73e5fdac78d344f088cc64784a3b2407b52c72f37c7bb849175d752", + "hypervisor/src/vm.rs": "21af42055ef2c7fdffc0be622cd6c69f4c8414b81d499127b731b740d053dbfb", + "log_json/Cargo.toml": "099c1609e2b62dc3be5118f30551c41a2e99e2da2ad99be4d10c9eede7c13ad7", + "log_json/src/lib.rs": "6f81be94782764eee577eeba289badc9eca776826874e220d4f46e026526c0e5", + "logging/Cargo.toml": "2a8b3cc828d24bc6794bcca6537322be4f746a44328dfaa6f307ed304f93c5ff", + "logging/src/lib.rs": "c8f5f2f9ca0bc6edea37b96bf8a6774688296267cfdd64d9eb9a941a356f9482", + "net_gen/Cargo.toml": "89f816b46067a1f4e52aee496b3efb5d2c064a8c885af221e8511d62e0314db4", + "net_gen/src/if_tun.rs": "adcfa9bbfc797e34c8b0db51c8623895b05a5150cd2a427a5bbde639bd89f92d", + "net_gen/src/iff.rs": "3e06e3cfa8f8e79a46d2eee926711670dd6198956863e32d68ac0da5d04bc37d", + "net_gen/src/inn.rs": "6ff377b89d01b2b2ed2279da4ab3b64d67a924ac5d59364736bad74657b2cda8", + "net_gen/src/lib.rs": "dfaf5b184949c832169e85eb8f2584ea86b4dd842a83adbaedfcac834f432cd8", + "net_gen/src/sockios.rs": "ed67acfb05b68e3b9bb1062959923f9e4deffac272473513356246a8fe3cfd6c", + "net_util/Cargo.toml": "d2617c889a7d1044c2db6ecd35e33bd1c24fe287672661f684e760a5e60b31cb", + "net_util/src/ctrl_queue.rs": "20778a3fa4554e25b5ec88248c0ce3a633fe37c83f3c882bdb1bec80b822a4b5", + "net_util/src/lib.rs": "7fe02390374bf72a4353be24187937a7983055372d59cc718454df3e22080716", + "net_util/src/mac.rs": "158ffc8bf70f3f64b1f17ca473400372889a99017cb583863b844c793f3a0c82", + "net_util/src/open_tap.rs": "b281c397042554500c81083102c45bb4d1fea9e0d74a7e5363fed9f8ac0a5ed9", + "net_util/src/queue_pair.rs": "774c80314e454faba5f7ace102e28e1bfe1c5a644095f9eb2cc36e54caf5b21b", + "net_util/src/tap.rs": "21bccaeccf354dacae3b13c6cc67525ed85f98f00b4330f1dec5510076102903", + "option_parser/Cargo.toml": "018985906a2160b25fbdd5f56b7111010e4c1b3b0de36c7a1e2a8fd99675be84", + "option_parser/src/lib.rs": "1db3516582e181f5694b82b8892ff0f473cc45bbb7db59a0f231981f9c98ac62", + "pci/Cargo.toml": "990c4a658cd8a2c7864481e76a465ff498296eac5bcbee5e2c2a57cdcb3afbab", + "pci/src/bus.rs": "1948e25c49052fb203b5195d3367aec767d168f03afba3a5eda09658eb562fde", + "pci/src/configuration.rs": "1bbeb3834579052fc64b82834b695d8cd322b2bb8947d8b8f5b027e244c046d6", + "pci/src/device.rs": "3909a114e4eebedcb53365d32f257fe51e72453d7ce431c902e4645b59c7bdac", + "pci/src/lib.rs": "7637e4b34b2ef6c81d3e27c5fa5f64ca7c071e74675125be9414c3a2f6c7ea67", + "pci/src/msi.rs": "5cd09ee3fd90b87b315ed77e8d67944ad78f4fd8919c8a63f6e3d6d491baff24", + "pci/src/msix.rs": "8317c47be240b1f887973e44b8391ebdeb39025ce73144a9637d5b967d0bc186", + "pci/src/vfio.rs": "07c77329db74b0ed5a1619d0f8a8067ecf87314a0ed312c549bb4fa24a837bc2", + "pci/src/vfio_user.rs": "068df7f6ecf2c105a12961ede61515a7d3255aabefd55114012f326f26b2cbb2", + "performance-metrics/Cargo.toml": "a935ece1b3b972c7787058af0288413f0c78dd6c2b9ab871c35b030f90531c89", + "performance-metrics/build.rs": "5f5d71be555466aa2013f2bfe0020a33c55b7fc41599fba5a8510ab25347cbd2", + "performance-metrics/src/main.rs": "43a181837c3955ce2e2a06db41821cc1cabbe283cd287f14021f5bc0eaade0cc", + "performance-metrics/src/performance_tests.rs": "d6f6562ebe1ec12fb20eea0ad8431dd40f87c4a7483bbdbd672066628ed286e4", + "qcow/Cargo.toml": "6c8798a10801cadae7c2fcd670e63765fffaf0584004c0aff1a78cfa02d9047a", + "qcow/src/qcow.rs": "06359fe17faa4534af8a9b15dc8c7f8d4ed8c4fd0312f032c29fbcbcde20f401", + "qcow/src/qcow_raw_file.rs": "ad040d3846e50f741dfa067fa909768b983fde0299dc39cfbf54ae790fbfc44e", + "qcow/src/raw_file.rs": "8de596934798c379df45952e41c593fe61212c326b3c12bfbf18106b0e8b2e51", + "qcow/src/refcount.rs": "cec95bc62fae380e3af8ceef1d03bf1509518ad42c4975b48c4843e4b0c953a8", + "qcow/src/vec_cache.rs": "7fcafd0d4041e501e763fe35d893dc00de86bd8374a22e3f24996a0d79d32707", + "rate_limiter/Cargo.toml": "3b806f6a0e5839ae9aefdfe27fadda8310d3a124dcd7749c51d20d1b98a5110c", + "rate_limiter/src/lib.rs": "7d1c19d9fdcee3852d0b58232ba2a2617412ec9d4a03450ddcb6f0fbe3a58701", + "release-notes.md": "ebeadae2b39735b051b4c4d92aaf56cb309cdbc6505c1444c2b533e0af1fd25f", + "resources/Dockerfile": "f87c8b29e388d1b80434d83a8a3a59f6a27db0e1b7b9042353f837ece84f6f99", + "resources/linux-config-aarch64": "f7cf2cad8030c6f8153af5d3b4a4680d5ba7c00590defebbfd97f68b9055aa44", + "resources/linux-config-x86_64": "0011b153ce41d80d6c89a51914b57d731d66db72023e1fb8863afc4784918ba8", + "scripts/ch-trace-visualiser.py": "03c6fa18a6673cd0666813b89380617666dd68cc1e49684abc064af145d64571", + "scripts/check-image-compatability.sh": "355449c26cb86b378d0adbfee0d97e971e38daffb133589233d6e199f20a707d", + "scripts/common-aarch64.sh": "1c627114b17f30d5599499b18192f54b51d7298ab0f635b0639d3139ee54b7fd", + "scripts/create-cloud-init.sh": "dcb002bbd4e58ef0256775ad06f8a3577b679d2c016077921cf67361af4020c2", + "scripts/dev_cli.sh": "181a5d92b6a1e3a98afd6be5b9fafff7065551240cd1c9a3f0bb174226ff0641", + "scripts/prepare_vdpa.sh": "53287477cf2f7edfc0dbff19516525860145e5522da65272982165714b19cba0", + "scripts/run_integration_tests_aarch64.sh": "f335c25761baba53fa3f7519eb06a9e21f2642aa5b1b772420b5c505784bfd54", + "scripts/run_integration_tests_live_migration.sh": "17bdd99db908c6fd02cf40e577312aa9f446cc5fa34cedfcad1a3ee626bbf200", + "scripts/run_integration_tests_rate_limiter.sh": "da61851fa4d5ada4beca201418824460911c035fe13e4a90225cb963b34d225d", + "scripts/run_integration_tests_sgx.sh": "4e1350c2208ec8acd463ae83d957053a395f79f3df5c769cbb86773ccb138f42", + "scripts/run_integration_tests_vfio.sh": "833cad7eeeeb89fe8d7da440e25016ffd30fa67a7203044a2b688a60f6972813", + "scripts/run_integration_tests_windows_aarch64.sh": "c193749bbe18f1e355a43dd3061e72a596f6cfc1d36ac862223667c2711114ca", + "scripts/run_integration_tests_windows_x86_64.sh": "5d878cd35f1e780541e18147923e0206708b935148628d82461c87f2ea5bd365", + "scripts/run_integration_tests_x86_64.sh": "7e2760a504680ea21ee8b5d0a72bcd3333dba4d21c88fc8aaaba60fca33580da", + "scripts/run_metrics.sh": "d16d0c8d45cae1ad0f03efc50beb2cb9ba2fb0b7a4f3e0e1ba58b90c2fbb9325", + "scripts/run_openapi_tests.sh": "479c9e69233bc3e1f7387d0c05c842376af65554f46c0c3f96ee32f7b6ffef6b", + "scripts/run_unit_tests.sh": "b10585b5fb1bc2d03c8680269622476a22112b42d3f2911ecce2c83fb624826b", + "scripts/sha1sums-aarch64": "3b707ecfc59c6714df6ecdc555702fc29f90edeb893b7f2e51e2ce2d48f30b45", + "scripts/sha1sums-x86_64": "a42f0684e892f1d89dab68d81ebecb74dfb2d1bfb4d1d3e30a7de31482bc5fbe", + "scripts/test-util.sh": "8eb206b721cfba4c5ad2b7038fc4775680506935eeb0fbe4e2f4fc5e3e614ffd", + "scripts/windows-11-iot-enterprise-aarch64.raw.sha1": "ca24ac46b9081ca5abe6d5700eb2e7acd277c2d749a0f156735f76a3858b00a5", + "serial_buffer/Cargo.toml": "955cae283035aed442bd8f1b0e1d7bb82acb44fee4e0a55664abadca0cefb889", + "serial_buffer/src/lib.rs": "0f6e0f315c6e35f49edf4cfe44e8996aa0ffcf93278d0482de48f473b0ea34b4", + "src/bin/ch-remote.rs": "bc39453ffbedfc9c4f112edebb2362fb5d4e8cfa8471fd9fe1f18ca786075935", + "src/bin/cube-cpuid.rs": "c50d735d1495f7e89d2e5f9e0a17515295c8992fc6c11b96c3fcdebf1d4c353d", + "src/common.rs": "6d4155d4bcd2ed567419805c25eb58d59638a8b2dbea3fe3ebf53b838f9079f3", + "src/lib.rs": "84917b9210aad626026960c06e31178b6a72a83c751e3114c8b9eec554439d05", + "src/main.rs": "0a693183e92d3bd003b65fba126c9582cec1cd9a9f1ea96e9e3660121cb8feae", + "src/vmm_config.rs": "cd14f5ba280cd2e56adf44160eabd1e6260e2f7680546b6ba013c7131e87c447", + "test_data/cloud-init/ubuntu/meta-data": "f00d5d802e1901e8bc9e8b26783471cac2ccce15bd217ab7331c192d478f132a", + "test_data/cloud-init/ubuntu/network-config": "95b8bbcefb9a4444298db9ca1869e7a830b3346b514f84ecf7917585a71f8dc4", + "test_data/cloud-init/ubuntu/user-data": "fe612b7b5d086b3c78e897d845adbde2d7d0ca00c23f5805309d41b015b0e9bc", + "test_infra/Cargo.toml": "d410c0cab258000c7f0ef300ffba5a697c287aef6a81e262b87e452fd078ad7e", + "test_infra/src/lib.rs": "a659d2899a989b38e01b1357f82fab8311fab7c041436c5d989bbe75aca7638b", + "tests/integration.rs": "c78ce22f3e857249568c21c216f13016f30a653de84b8e39220623b1910738db", + "tpm/Cargo.toml": "4b539e0aac1eeaa4837df32b6db71899ee65543432919cee3bee042dbe648316", + "tpm/src/emulator.rs": "6b3edd84e9598e01ac71fa52aa9a3f40cec87759e7d50e6d8b0b164a9497a596", + "tpm/src/lib.rs": "49e894840702fdd55681489f2e7db32287820d7b17b59302a5aa30ae2b90bb17", + "tpm/src/socket.rs": "b7d5a9221fa3706076881a8f718237d314f442b000e9b1508bf1efac0713ab31", + "tracer/Cargo.toml": "b08898347d1105f6b36837f2a486f3219795162bc0772fd98065f97cf6365152", + "tracer/src/lib.rs": "f7aba347c72f115461dc00841216324c910025bf7b7a5704556cf31174037433", + "tracer/src/tracer.rs": "3e8c3b79e676541bcc8e35c973fd5f70f753dcea99adcef1c2087f606c0760a4", + "tracer/src/tracer_noop.rs": "f9e13a8761106a07578ebbf2952b732bb7ab2c114f3c9878b4d9a592189ba379", + "vfio_user/Cargo.toml": "0eb4cfce48236f9942959bbbf77932fe26295bd69bb3a88ca986c6a2ba46eb70", + "vfio_user/src/lib.rs": "3556146d69a3667d5b3eb3f7150ad9263c8952e777271ee3a9fb21168b885ede", + "vhdx/Cargo.toml": "9fa538a00c79e42c16f84b77d7f1aee223961f6f0878cc84a83add0e29633d9b", + "vhdx/src/lib.rs": "c6fd5cd18d19269b9c2edb1fd9f7eea941cfb33cb5a74d045739a46003e736d1", + "vhdx/src/vhdx.rs": "88557435f502131ac6b4b3e88d7deea8b5b51018babfab92f18a361c9600dc57", + "vhdx/src/vhdx_bat.rs": "6b85f57d3dd8852268103e96093e8b9dc80b74420b254adcb23b337a21d2ff52", + "vhdx/src/vhdx_header.rs": "9e3af2542ad621f8a838354af9649283e3b829fd057b80757f91f2805999832e", + "vhdx/src/vhdx_io.rs": "89704209a3d2c038c2751e1b89767460355a83d86a16a24bb4de980c969b699b", + "vhdx/src/vhdx_metadata.rs": "cccd702f81c68a1d59b09f23fedc85493318d39af804e3974ad05100c4a6c4d6", + "vhost_user_block/Cargo.toml": "5693e5b3cd8aa6854f3584b30dfdab62df080bcde6117e9885c3c1ced585a9a6", + "vhost_user_block/src/lib.rs": "39eee4eb927b9509267a45e3837c40c19002650232667f1020a0b10e38b96410", + "vhost_user_block/src/main.rs": "4b0ca6aadfd13ece8a9669a2fc57cd45a8ec205efdca622c332a5338d7698278", + "vhost_user_net/Cargo.toml": "ac772a0a4f629aacf29e2e2554b6d8b6b56f5f5e33fd43bf5f5e29a2f892ec51", + "vhost_user_net/src/lib.rs": "9c9c7a6a1810e2fe9c96acef43813c5c7c4ce512296667d42f5fc8678129b5e2", + "vhost_user_net/src/main.rs": "4b32d76efe49e2522ccf7c6131d6309c3ced927847413179942f541c3f0beda2", + "virtio-devices/Cargo.toml": "b37b8bc84ba4734445ce08c48cb0f14f7a0a33acc8ae0aa9f2ec8c09b61e50ce", + "virtio-devices/src/balloon.rs": "c89e31087e6ba907e21f6841e2da8dcda9b5b57b787d34910f352630fcd020e9", + "virtio-devices/src/block.rs": "0db3b7119c7442bcfb1f9e22fd9b1128b527bbf17f1e3259ad81095479256454", + "virtio-devices/src/console.rs": "f5f2d8ee859a5b3209d6168ceced62850cfd6fb71cd0b1beca5515d10b0a41db", + "virtio-devices/src/device.rs": "b1d50b744779b298312d7d90b374ae1c65564204a4e960b569cdaf00c56a2bb6", + "virtio-devices/src/epoll_helper.rs": "32fec21f56dec19c0f0c436a2c3ad62513f05c1a49380b1e19f30291058e6c5e", + "virtio-devices/src/fs.rs": "13fc45f33cebcc111907379705de26f40cce194502d970293d5d107f7786915d", + "virtio-devices/src/iommu.rs": "2d4e0f5e27b4a3e101852bbade2208fe112c5c344639fdd9377cbc165354e801", + "virtio-devices/src/lib.rs": "459ca47b8795463174e1ad037bc576fff23a83932d7d66c4eaaab75f77882c78", + "virtio-devices/src/mem.rs": "88e099e49731ba3f309ac664fec053c15888abde9d158d17a1cd84f23b0281d9", + "virtio-devices/src/net.rs": "920e6934dbf33fa6e2d4821203d3f09e0a88ceec410aa8361948cae19d8fe62d", + "virtio-devices/src/pmem.rs": "05f73a21ad56452c59a26b14d48ca1c9c61a30378c940508a3d3fc78a0140573", + "virtio-devices/src/rng.rs": "d8aa8a6c2db6303e0b689e6c4ed79657f74f444b190713d5138d8d90e04f504e", + "virtio-devices/src/seccomp_filters.rs": "14da7db06887afdcdccb9815323e0e9e6336a24b47ede1f25f49cf4f87804355", + "virtio-devices/src/thread_helper.rs": "8db9d92315fc10aa30565841528e80dc09d805be9ba910f22aa4a4294e7f8e0a", + "virtio-devices/src/transport/mod.rs": "73b5f8c76a178da32ad676d98914e4f8bd02239c980ac4bfef15a09ba6bc3210", + "virtio-devices/src/transport/pci_common_config.rs": "af717be0e1caa518525ce74775b92fb86d0c5241c4df850002816989681207cd", + "virtio-devices/src/transport/pci_device.rs": "27a09666b838b64d99ce8d0873f1586173c0878612b20c34300b6be0081f635f", + "virtio-devices/src/vdpa.rs": "014ef6a130d90d73ce04b68f5f88c2703ea9f2abaf72e47071feb38489ef40ed", + "virtio-devices/src/vhost_user/blk.rs": "1bc41c8b1b5f3484195702ae417ce53a08b97056d9c347be0d86cfcaa315552b", + "virtio-devices/src/vhost_user/fs.rs": "9b885737c04f810dd5694823ac75121e3e413fdbccd01e6f6e61597340986387", + "virtio-devices/src/vhost_user/mod.rs": "18a60173adf12c111cda01d1a3911d6d8bb1218c36e0712d0f18e463593dca32", + "virtio-devices/src/vhost_user/net.rs": "70e09d05f68aca8f6160e5454d0d7bba2489f7179ff6bbf826915295b39db642", + "virtio-devices/src/vhost_user/vu_common_ctrl.rs": "57d359800c8d37c823107485b9b190b776fe10812ccf0c861c29c7beae3821e6", + "virtio-devices/src/vsock/csm/connection.rs": "0cb6ddd274906d3af441712d754ac5e273b3021f5c169850f5f98b5b2f6ca57e", + "virtio-devices/src/vsock/csm/mod.rs": "2f9e55acb3c06bdd17c787b605126181ad17c4a0376f31150ab7ad23490725cf", + "virtio-devices/src/vsock/csm/txbuf.rs": "0549e5de0c1ad1454f07f3c7882c7611830ffaaf5a0906bfa26adce64332d5f4", + "virtio-devices/src/vsock/device.rs": "b726cc311318ee07260974441779ce3222afbbc2eb603e26b6661957c1b8951c", + "virtio-devices/src/vsock/mod.rs": "9d7b5b846c42507bed027fb1ce111a9fc58ebabb8ac2a4c8d57feb86a0973f3d", + "virtio-devices/src/vsock/packet.rs": "137e7694fb621f2eb08d03fa9a674834634a10e1ae9a06463eb8464b65c80949", + "virtio-devices/src/vsock/unix/mod.rs": "9d200e1f9fe1eef24ec9587dd34cb62b1fdd622b0dbbbf3188ea9dd678fdbe4a", + "virtio-devices/src/vsock/unix/muxer.rs": "63ca99971d6f336eb3ed0415f5bda5775c83bfb9d85907c51abd3c90d0e69667", + "virtio-devices/src/vsock/unix/muxer_killq.rs": "1ece0d706dc4d593d83bb7c5478f86e65558a2df9d560302d7e465ff2210a275", + "virtio-devices/src/vsock/unix/muxer_rxq.rs": "ffd21b7ec6dc7640a8032e43c98ca5ded8b413f9315932bd92a19869e4ce7bfa", + "virtio-devices/src/vsock/unix/stream.rs": "eb8d71da25b142ba79fbb50950307848dde4454c218fcd6ef34aeef6901ef2e8", + "virtio-devices/src/watchdog.rs": "3cfb94b80e28acda80e937a92d706e756c9fa22d3194cf82b377a5a1f0d6660e", + "virtiofsd/.gitignore": "4f0944189d5358b284a34108aa30a63556e6f5b60ad36482b740159b5399a662", + "virtiofsd/.gitlab-ci.yml": "5ec1cc9b38ec5ecfae2629e06de825864df01be2eb15562fbeac014c0464dc5e", + "virtiofsd/50-virtiofsd.json": "2160818c240a7daab46edd994649b6a2225585e638b9126e07c593b58a843b1d", + "virtiofsd/CONTRIBUTING.md": "61287dab2e7aaa6a689000d4c5f50131401500b2804830977ca7af7a0610ab8a", + "virtiofsd/Cargo.toml": "26c1ce6073b5218340b479ded4cb48b26aead0d93f836109b0d09bd1758773ac", + "virtiofsd/LICENSE-APACHE": "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", + "virtiofsd/LICENSE-BSD-3-Clause": "a6d3ebd1c2f37d4fd83d0676621f695fc0cc2d8c6e646cdbb831b46e0650c208", + "virtiofsd/README.md": "25696275a330adc72b58a7a70b944baa07b2a0be25af7b268f736ee635e5515f", + "virtiofsd/doc/xattr-mapping.md": "4752641f8c70b7b1ac3d1ca139efee86e073e827964c9d68a2827e6327048726", + "virtiofsd/docs/preserialization-storeonly-design.md": "59b81dff76528350baf0b7fc28dab5f27f7b089adef6bf742d71b5f816fa02b1", + "virtiofsd/rustfmt.toml": "b3ff5fef058f4ef40b66b6df90d29bef1d02d27766293cf49e4ca599d795c254", + "virtiofsd/src/descriptor_utils.rs": "3600b096f242bcaf577dbac4679df7f95d7071059456434a705907bdc2593152", + "virtiofsd/src/file_traits.rs": "b89bf282d23829fbea5048d5d03b03edfc8a11a124b05da4609a0edbdca1fd8f", + "virtiofsd/src/filesystem.rs": "9663f9038c6bb1f29457072e08eb56b1554919ce554b341dbc92e06b6a64c104", + "virtiofsd/src/fs_cache_req_handler.rs": "d514c513afcbf38e0c5dd245a673ed3522a551d96ba6a9b1a26f7dc49720b19a", + "virtiofsd/src/fuse.rs": "0c8880cc4b060da16fd93fed5c5ef1de45be7ab134913adcf45a5b4aa4099c10", + "virtiofsd/src/idmap.rs": "464e35983bbfa2f41022fcfa50f5389e53a88400f20f4651326585941e6f80ca", + "virtiofsd/src/lib.rs": "92fe2dbf0b424891a5e7aac192b6247c33c287d3ce9a1e48195425d2943b07c8", + "virtiofsd/src/limits.rs": "dc8d11d39acb742136e87b1340ab374ddd1a90ddbf7fcb0840b4916fb6999584", + "virtiofsd/src/macros.rs": "ac61b3d6afcf65604dc28fa19a01000fee266f2ef1fb55972e82240daddf5fd6", + "virtiofsd/src/main.rs": "2ee0e40893008ed5e183c850c39957948e65d521d76cdaf3b3b3ed872fa419fe", + "virtiofsd/src/oslib.rs": "56cd2044aee6a0891eaf2a5303ab78e368a5768b1baf093abc5674c3bb20b7f5", + "virtiofsd/src/passthrough/credentials.rs": "72008e9f627a2b6c5b2f45dbb1ffea6f4cd6a4e4b6f78637b35391765b07889b", + "virtiofsd/src/passthrough/device_state/deserialization.rs": "e9a753fa0f75b7142724f36183c5fbe5dec1ef688ad54399d7a46ed89df1081e", + "virtiofsd/src/passthrough/device_state/mod.rs": "e8b5e3eeece9c0501c9ac13d97abf5e7a96525656a71a81527871b43037444bc", + "virtiofsd/src/passthrough/device_state/preserialization.rs": "f09e472a200d93444d8bcf72150b208688ebfd179f77e0d17780093e5572e371", + "virtiofsd/src/passthrough/device_state/serialization.rs": "c1a6b51a42d822b84e7f4e3b8d4d56fdc3937c207029741be8fac811fcc642c4", + "virtiofsd/src/passthrough/device_state/serialized.rs": "45f72c4d4c928a18a9e1a4c65b48dd0bda0bbf7f4b230e088ecd66ed47d14636", + "virtiofsd/src/passthrough/file_handle.rs": "69e05899616ea1896c809236cebd91c13be518351d4b5e5474446cf553b5913f", + "virtiofsd/src/passthrough/inode_store.rs": "de9de3c97365b8b141cc86f0c3aba6532da96cf811a1b3e736bbf5ff2c7ccad1", + "virtiofsd/src/passthrough/mod.rs": "8029a12b01f4e8b43a7f8ed01228d2ebee803c2a13b8f023a8832c6b456b7ea3", + "virtiofsd/src/passthrough/mount_fd.rs": "deb2052c13a51b71260fd48df04a6ed9471769d8b1a81c987738f7ebb86a6a3f", + "virtiofsd/src/passthrough/read_only.rs": "8bb757ef42af3437bdee8846b1846ff4d8ecc9efbb44f57638869d47a67bd544", + "virtiofsd/src/passthrough/stat/file_status.rs": "58a6a619ee30f90cb4c8433fd108f7c6178e1fb9471798a182acc834dc3d253d", + "virtiofsd/src/passthrough/stat.rs": "a220d599d66724d1cfd93dcb61e09535b47e685eb2cf6a7d668a87e9b301c6db", + "virtiofsd/src/passthrough/util.rs": "8b23bfe8987c2b733522071b5b499f7b9356778f4143b7a8fc081be2bea891d5", + "virtiofsd/src/passthrough/xattrmap.rs": "cd323255acbe403b14f6c90d2802eef11320de3af35045dfe70a03487688d326", + "virtiofsd/src/read_dir.rs": "005921b3baa01751b8299557f0a9674b37532cfe951fb471a1a42cfba42ea4e5", + "virtiofsd/src/sandbox.rs": "573da676f7b56b3beedc491852448660033503ed7c87bb6d9a97594eccdf08d6", + "virtiofsd/src/seccomp.rs": "9f221a7072401d7c6fc1a9038dada39ff699b70fc3159ad58cec6c14438ffed5", + "virtiofsd/src/server.rs": "615a0d6874b459c94027f8c0972ca21378317dc08f5f430cd5b97dea9c077ed1", + "virtiofsd/src/util.rs": "dcb2a63ed2ccfba4cad568891c46c4a3bb0670a02de456e3ebf46518380ccf69", + "vm-allocator/Cargo.toml": "2bf09271ba1a0dffbecea3cdb5f5205383d4bf6e182dd696da849b76e85cea5c", + "vm-allocator/src/address.rs": "51f968edfcf3fd7054013e0116d1de2080130081f86cfc56bbe0f84f01bf3230", + "vm-allocator/src/gsi.rs": "83a7d72eec4049281e6c124909690a22df0283aa8ef93a878c21461febd4f52c", + "vm-allocator/src/lib.rs": "56eb1afcd1a83c769119afdb59de99d2ca0057e6b6303f75cd2e7ca4c61e2404", + "vm-allocator/src/system.rs": "906f80d1208e93f04fe6f7b89822393ed2f7c3916eb94aa08bd219c8a7b2a6aa", + "vm-device/Cargo.toml": "f4e9a72f07ffe238922d0d0cd3f0ae58e9ebfa753aa292083e647b3b21f05424", + "vm-device/src/bus.rs": "3a467656a8d5629f67fc0f6e888bb9528392f442ddd971995c2fb559250ea3a3", + "vm-device/src/dma_mapping/mod.rs": "144e0d60a26f15ae9894fd9bb509b889fdd0e0076941b76f719dfe7d9d2705cb", + "vm-device/src/dma_mapping/vfio.rs": "9a0ad14eda4fd81b16cd19dd0df363090c1a109383d662fa2b4b028facd15079", + "vm-device/src/interrupt/mod.rs": "eccc9c7f9e5352ef0e65e540107c4f91cdc7cf59ca8b913590b6922286cc164d", + "vm-device/src/lib.rs": "954b1937cc792452f313ccfe1c00537758b18ed208266a98c019c6de06c6a9e3", + "vm-migration/Cargo.toml": "e94f6cceff2ab847f45442e0a3a24323b872d0605185824153c10cc4a5e4f1a7", + "vm-migration/src/lib.rs": "077729099d381f1e996b3b7b30e7ec9a14272c0c4829645f02fc71c66c52b5c3", + "vm-migration/src/protocol.rs": "a28051755cfbd24ddef1874ddc37c14d4b114f1511bf1507a31e172f21eff692", + "vm-virtio/Cargo.toml": "aa1b8a9a34133cfb8c74dff8d9fc93d947b35c7985363e5d82855ea8881e21ef", + "vm-virtio/src/checked_descriptor.rs": "a5758dac5193c4711d90490170d2cdd3637fbc97be5fc24e12f4f8bb8f631824", + "vm-virtio/src/lib.rs": "892008acea32a94b0f6b594e956b2506f6f88bea82287cb05bd310aa681dc29e", + "vm-virtio/src/queue.rs": "f4e222941c4ae3b51699bb40aa3c22085f168784499aace7619e12821b3df6d6", + "vmm/Cargo.toml": "d0fcde708024e49cba3e79ff890e14ddf6f8acc25a72e476455527c88d0276aa", + "vmm/src/acpi.rs": "cbaf11cb33cfe0cabc488abc11df222e35ea7942a2f87cab65da4e5dffd19b6f", + "vmm/src/api/http/http_endpoint.rs": "aeb4aa0920658cfd9d862ebb99bed00be9d59e54f042b9f8db7f26a48ed16480", + "vmm/src/api/http/mod.rs": "06faf3c959c4a5deacf1028c6fd7ab3aa875eac0904eefdc52b1ff94f291c540", + "vmm/src/api/mod.rs": "0b83a0681c6b53aaf438d0e6ec08c8d1f07bd2586ef750018da5f0abbbf16a9e", + "vmm/src/api/openapi/cloud-hypervisor.yaml": "902aa9c3b68b5d68bcdc2c625b88a3b3c7d916d9bb26784e0344a66e2f7b4543", + "vmm/src/api/service.rs": "efd46ab94bd22fd395486981bdb7d61e728a97dc4c00a460179df28cf3f1612f", + "vmm/src/clone3.rs": "341b38e660b1f8a364b6486ea1ca99c953a25aec69a8cdf4b553a89bf6be6f41", + "vmm/src/config.rs": "2767a13f948e21b43577210286dbc4bec8505810f7e2104c4187bd3a6e0906f6", + "vmm/src/coredump.rs": "7a834e7c66bcbdb10146f0076818c54cbeb47bd2d7ad88884ee18d74b5486c8a", + "vmm/src/cpu.rs": "2d1266cd1cdc3daf63be1afd84cb019f6d33b5c7373b9fec54ddc70bf5a72e11", + "vmm/src/device_manager.rs": "6dcbb3e1e9fb0c8f375934fe38306451af783ef142c720d02b77aafe7e734b8f", + "vmm/src/device_tree.rs": "5ad4799b2e634e66d589793c63f6c58f77f6ddb09c77774ac90015fa3d746481", + "vmm/src/gdb.rs": "2d907ea42d8c1d452cd5dbd8acca4193f28f6894d9e1cbb89867700573aceefb", + "vmm/src/interrupt.rs": "4d8229f1dca674a0aedb13020a9385974ea82e35377c55b3d9362cfad3f00832", + "vmm/src/lib.rs": "067e35b9d0fef026b481f6c19eadaac1e6fb58ad740595e163703f5bbec73201", + "vmm/src/memory_manager.rs": "d0b0de21040f8bab5bfb4f00a160ca12f978b373f24b92e8f7ada43560d34c54", + "vmm/src/migration.rs": "5d96b217b10dd25091bf9a385d9438c25c0905bd81c53aab561690a773ab3053", + "vmm/src/pagemap_anon.rs": "52da114b546647b60e23c3b169cfa808f4fefda2455dfd903ad62169d9198bee", + "vmm/src/pagemap_anon_bench.rs": "63d35fb2ea56e5248db5d58c841e1a5babd7a4d05f69c8421411e4c5c30c8942", + "vmm/src/pci_segment.rs": "6b688f1b5197fed853d3ea9ca190d12dfa35f7a1473e7047e93db646dc933350", + "vmm/src/seccomp_filters.rs": "dac02d8b8b200e48f209e367bed3cbc2f1d278aaece8e2ce092b2e967de0634f", + "vmm/src/serial_manager.rs": "4616d2f4e6ade6acc524df7c04259f59c123e3775869c3201d05a69c08e76e35", + "vmm/src/sigwinch_listener.rs": "0620229b4e612f09d30572f2a8ce3d4b374607dfbefec7fa3b1be3747be23fac", + "vmm/src/soft_dirty.rs": "8c64dc5ab36217731dd502dec398be880e92c829fc0878c387d37cc35e1d112e", + "vmm/src/vm.rs": "27109d0574690bf1fa8659eedbeec2ea70915ce5933f9926070cfe425af8ff49", + "vmm/src/vm_config.rs": "bedf3b8f2409362921220a81fc55fd0eba817d5d9588b3ad5331ee6485d064f3", + "workflows/1check.sh": "46749842292358682721f6baeabdd55b0fe7b666a2110d3d1971db108678d0e5", + "workflows/2upload.sh": "83b6a36ad2167b5c47b0b37d43ccfb866089aff93348a815d579e8ab0371f2dc" + } +} diff --git a/vendor/cube-hypervisor/acpi_tables/Cargo.toml b/vendor/cube-hypervisor/acpi_tables/Cargo.toml new file mode 100644 index 0000000..d96d6b8 --- /dev/null +++ b/vendor/cube-hypervisor/acpi_tables/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "acpi_tables" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[dependencies] +vm-memory = { workspace = true } +zerocopy = "0.6.1" diff --git a/vendor/cube-hypervisor/acpi_tables/src/aml.rs b/vendor/cube-hypervisor/acpi_tables/src/aml.rs new file mode 100644 index 0000000..53f977b --- /dev/null +++ b/vendor/cube-hypervisor/acpi_tables/src/aml.rs @@ -0,0 +1,1894 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use std::marker::PhantomData; + +pub trait Aml { + fn append_aml_bytes(&self, _v: &mut Vec) { + unimplemented!() + } + + fn to_aml_bytes(&self) -> Vec { + let mut v = Vec::new(); + self.append_aml_bytes(&mut v); + v + } +} + +pub const ZERO: Zero = Zero {}; +pub struct Zero {} + +impl Aml for Zero { + fn append_aml_bytes(&self, v: &mut Vec) { + v.push(0u8) + } +} + +pub const ONE: One = One {}; +pub struct One {} + +impl Aml for One { + fn append_aml_bytes(&self, v: &mut Vec) { + v.push(1u8) + } +} + +pub const ONES: Ones = Ones {}; +pub struct Ones {} + +impl Aml for Ones { + fn append_aml_bytes(&self, v: &mut Vec) { + v.push(0xffu8) + } +} + +pub struct Path { + root: bool, + name_parts: Vec<[u8; 4]>, +} + +impl Aml for Path { + fn append_aml_bytes(&self, bytes: &mut Vec) { + if self.root { + bytes.push(b'\\'); + } + + match self.name_parts.len() { + 0 => panic!("Name cannot be empty"), + 1 => {} + 2 => { + bytes.push(0x2e); /* DualNamePrefix */ + } + n => { + bytes.push(0x2f); /* MultiNamePrefix */ + bytes.push(n as u8); + } + }; + + for part in self.name_parts.clone().iter_mut() { + bytes.extend_from_slice(part.as_ref()); + } + } +} + +impl Path { + pub fn new(name: &str) -> Self { + let root = name.starts_with('\\'); + let offset = root as usize; + let mut name_parts = Vec::new(); + for part in name[offset..].split('.') { + assert_eq!(part.len(), 4); + let mut name_part = [0u8; 4]; + name_part.copy_from_slice(part.as_bytes()); + name_parts.push(name_part); + } + + Path { root, name_parts } + } +} + +impl From<&str> for Path { + fn from(s: &str) -> Self { + Path::new(s) + } +} + +pub type Byte = u8; + +impl Aml for Byte { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x0a); /* BytePrefix */ + bytes.push(*self); + } +} + +pub type Word = u16; + +impl Aml for Word { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x0b); /* WordPrefix */ + bytes.extend_from_slice(&self.to_le_bytes()) + } +} + +pub type DWord = u32; + +impl Aml for DWord { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x0c); /* DWordPrefix */ + bytes.extend_from_slice(&self.to_le_bytes()) + } +} + +pub type QWord = u64; + +impl Aml for QWord { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x0e); /* QWordPrefix */ + bytes.extend_from_slice(&self.to_le_bytes()) + } +} + +pub struct Name { + bytes: Vec, +} + +impl Aml for Name { + fn append_aml_bytes(&self, bytes: &mut Vec) { + // TODO: Refactor this to make more efficient but there are + // lifetime/ownership challenges. + bytes.extend_from_slice(&self.bytes.clone()) + } +} + +impl Name { + pub fn new(path: Path, inner: &dyn Aml) -> Self { + let mut bytes = vec![0x08]; /* NameOp */ + path.append_aml_bytes(&mut bytes); + inner.append_aml_bytes(&mut bytes); + Name { bytes } + } +} + +pub struct Package<'a> { + children: Vec<&'a dyn Aml>, +} + +impl<'a> Aml for Package<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut tmp = vec![self.children.len() as u8]; + for child in &self.children { + child.append_aml_bytes(&mut tmp); + } + + let pkg_length = create_pkg_length(&tmp, true); + + bytes.push(0x12); /* PackageOp */ + bytes.extend_from_slice(&pkg_length); + bytes.extend_from_slice(&tmp); + } +} + +impl<'a> Package<'a> { + pub fn new(children: Vec<&'a dyn Aml>) -> Self { + Package { children } + } +} + +/* + +From the ACPI spec for PkgLength: + +"The high 2 bits of the first byte reveal how many follow bytes are in the PkgLength. If the +PkgLength has only one byte, bit 0 through 5 are used to encode the package length (in other +words, values 0-63). If the package length value is more than 63, more than one byte must be +used for the encoding in which case bit 4 and 5 of the PkgLeadByte are reserved and must be zero. +If the multiple bytes encoding is used, bits 0-3 of the PkgLeadByte become the least significant 4 +bits of the resulting package length value. The next ByteData will become the next least +significant 8 bits of the resulting value and so on, up to 3 ByteData bytes. Thus, the maximum +package length is 2**28." + +*/ + +/* Also used for NamedField but in that case the length is not included in itself */ +fn create_pkg_length(data: &[u8], include_self: bool) -> Vec { + let mut result = Vec::new(); + + /* PkgLength is inclusive and includes the length bytes */ + let length_length = if data.len() < (2usize.pow(6) - 1) { + 1 + } else if data.len() < (2usize.pow(12) - 2) { + 2 + } else if data.len() < (2usize.pow(20) - 3) { + 3 + } else { + 4 + }; + + let length = data.len() + if include_self { length_length } else { 0 }; + + match length_length { + 1 => result.push(length as u8), + 2 => { + result.push((1u8 << 6) | (length & 0xf) as u8); + result.push((length >> 4) as u8) + } + 3 => { + result.push((2u8 << 6) | (length & 0xf) as u8); + result.push((length >> 4) as u8); + result.push((length >> 12) as u8); + } + _ => { + result.push((3u8 << 6) | (length & 0xf) as u8); + result.push((length >> 4) as u8); + result.push((length >> 12) as u8); + result.push((length >> 20) as u8); + } + } + + result +} + +pub struct EisaName { + value: DWord, +} + +impl EisaName { + pub fn new(name: &str) -> Self { + assert_eq!(name.len(), 7); + + let data = name.as_bytes(); + + let value: u32 = (u32::from(data[0] - 0x40) << 26 + | u32::from(data[1] - 0x40) << 21 + | u32::from(data[2] - 0x40) << 16 + | name.chars().nth(3).unwrap().to_digit(16).unwrap() << 12 + | name.chars().nth(4).unwrap().to_digit(16).unwrap() << 8 + | name.chars().nth(5).unwrap().to_digit(16).unwrap() << 4 + | name.chars().nth(6).unwrap().to_digit(16).unwrap()) + .swap_bytes(); + + EisaName { value } + } +} + +impl Aml for EisaName { + fn append_aml_bytes(&self, bytes: &mut Vec) { + self.value.append_aml_bytes(bytes) + } +} + +pub type Usize = usize; + +impl Aml for Usize { + fn append_aml_bytes(&self, bytes: &mut Vec) { + if *self <= u8::max_value().into() { + (*self as u8).append_aml_bytes(bytes) + } else if *self <= u16::max_value().into() { + (*self as u16).append_aml_bytes(bytes) + } else if *self <= u32::max_value() as usize { + (*self as u32).append_aml_bytes(bytes) + } else { + (*self as u64).append_aml_bytes(bytes) + } + } +} + +fn append_aml_string(v: &str, bytes: &mut Vec) { + bytes.push(0x0D); /* String Op */ + bytes.extend_from_slice(v.as_bytes()); + bytes.push(0x0); /* NullChar */ +} + +pub type AmlStr = &'static str; + +impl Aml for AmlStr { + fn append_aml_bytes(&self, bytes: &mut Vec) { + append_aml_string(self, bytes) + } +} + +pub type AmlString = String; + +impl Aml for AmlString { + fn append_aml_bytes(&self, bytes: &mut Vec) { + append_aml_string(self, bytes) + } +} + +pub struct ResourceTemplate<'a> { + children: Vec<&'a dyn Aml>, +} + +impl<'a> Aml for ResourceTemplate<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut tmp = Vec::new(); + // Add buffer data + for child in &self.children { + child.append_aml_bytes(&mut tmp); + } + + // Mark with end and mark checksum as as always valid + tmp.push(0x79); /* EndTag */ + tmp.push(0); /* zero checksum byte */ + + // Buffer length is an encoded integer including buffer data + // and EndTag and checksum byte + let mut buffer_length = tmp.len().to_aml_bytes(); + buffer_length.reverse(); + for byte in buffer_length { + tmp.insert(0, byte); + } + + // PkgLength is everything else + let pkg_length = create_pkg_length(&tmp, true); + + bytes.push(0x11); /* BufferOp */ + bytes.extend_from_slice(&pkg_length); + bytes.extend_from_slice(&tmp); + } +} + +impl<'a> ResourceTemplate<'a> { + pub fn new(children: Vec<&'a dyn Aml>) -> Self { + ResourceTemplate { children } + } +} + +pub struct Memory32Fixed { + read_write: bool, /* true for read & write, false for read only */ + base: u32, + length: u32, +} + +impl Memory32Fixed { + pub fn new(read_write: bool, base: u32, length: u32) -> Self { + Memory32Fixed { + read_write, + base, + length, + } + } +} + +impl Aml for Memory32Fixed { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x86); /* Memory32Fixed */ + bytes.extend_from_slice(&9u16.to_le_bytes()); + // 9 bytes of payload + bytes.push(self.read_write as u8); + bytes.extend_from_slice(&self.base.to_le_bytes()); + bytes.extend_from_slice(&self.length.to_le_bytes()); + } +} + +#[derive(Copy, Clone)] +enum AddressSpaceType { + Memory, + Io, + BusNumber, +} + +#[derive(Copy, Clone)] +pub enum AddressSpaceCachable { + NotCacheable, + Cacheable, + WriteCombining, + PreFetchable, +} + +pub struct AddressSpace { + r#type: AddressSpaceType, + min: T, + max: T, + type_flags: u8, +} + +impl AddressSpace { + pub fn new_memory(cacheable: AddressSpaceCachable, read_write: bool, min: T, max: T) -> Self { + AddressSpace { + r#type: AddressSpaceType::Memory, + min, + max, + type_flags: (cacheable as u8) << 1 | read_write as u8, + } + } + + pub fn new_io(min: T, max: T) -> Self { + AddressSpace { + r#type: AddressSpaceType::Io, + min, + max, + type_flags: 3, /* EntireRange */ + } + } + + pub fn new_bus_number(min: T, max: T) -> Self { + AddressSpace { + r#type: AddressSpaceType::BusNumber, + min, + max, + type_flags: 0, + } + } + + fn push_header(&self, bytes: &mut Vec, descriptor: u8, length: usize) { + bytes.push(descriptor); /* Word Address Space Descriptor */ + bytes.extend_from_slice(&(length as u16).to_le_bytes()); + bytes.push(self.r#type as u8); /* type */ + let generic_flags = 1 << 2 /* Min Fixed */ | 1 << 3; /* Max Fixed */ + bytes.push(generic_flags); + bytes.push(self.type_flags); + } +} + +impl Aml for AddressSpace { + fn append_aml_bytes(&self, bytes: &mut Vec) { + self.push_header( + bytes, + 0x88, /* Word Address Space Descriptor */ + 3 + 5 * std::mem::size_of::(), /* 3 bytes of header + 5 u16 fields */ + ); + + bytes.extend_from_slice(&0u16.to_le_bytes()); /* Granularity */ + bytes.extend_from_slice(&self.min.to_le_bytes()); /* Min */ + bytes.extend_from_slice(&self.max.to_le_bytes()); /* Max */ + bytes.extend_from_slice(&0u16.to_le_bytes()); /* Translation */ + let len = self.max - self.min + 1; + bytes.extend_from_slice(&len.to_le_bytes()); /* Length */ + } +} + +impl Aml for AddressSpace { + fn append_aml_bytes(&self, bytes: &mut Vec) { + self.push_header( + bytes, + 0x87, /* DWord Address Space Descriptor */ + 3 + 5 * std::mem::size_of::(), /* 3 bytes of header + 5 u32 fields */ + ); + + bytes.extend_from_slice(&0u32.to_le_bytes()); /* Granularity */ + bytes.extend_from_slice(&self.min.to_le_bytes()); /* Min */ + bytes.extend_from_slice(&self.max.to_le_bytes()); /* Max */ + bytes.extend_from_slice(&0u32.to_le_bytes()); /* Translation */ + let len = self.max - self.min + 1; + bytes.extend_from_slice(&len.to_le_bytes()); /* Length */ + } +} + +impl Aml for AddressSpace { + fn append_aml_bytes(&self, bytes: &mut Vec) { + self.push_header( + bytes, + 0x8A, /* QWord Address Space Descriptor */ + 3 + 5 * std::mem::size_of::(), /* 3 bytes of header + 5 u64 fields */ + ); + + bytes.extend_from_slice(&0u64.to_le_bytes()); /* Granularity */ + bytes.extend_from_slice(&self.min.to_le_bytes()); /* Min */ + bytes.extend_from_slice(&self.max.to_le_bytes()); /* Max */ + bytes.extend_from_slice(&0u64.to_le_bytes()); /* Translation */ + let len = self.max - self.min + 1; + bytes.extend_from_slice(&len.to_le_bytes()); /* Length */ + } +} + +pub struct Io { + min: u16, + max: u16, + alignment: u8, + length: u8, +} + +impl Io { + pub fn new(min: u16, max: u16, alignment: u8, length: u8) -> Self { + Io { + min, + max, + alignment, + length, + } + } +} + +impl Aml for Io { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x47); /* Io Port Descriptor */ + bytes.push(1); /* IODecode16 */ + bytes.extend_from_slice(&self.min.to_le_bytes()); + bytes.extend_from_slice(&self.max.to_le_bytes()); + bytes.push(self.alignment); + bytes.push(self.length); + } +} + +pub struct Interrupt { + consumer: bool, + edge_triggered: bool, + active_low: bool, + shared: bool, + number: u32, +} + +impl Interrupt { + pub fn new( + consumer: bool, + edge_triggered: bool, + active_low: bool, + shared: bool, + number: u32, + ) -> Self { + Interrupt { + consumer, + edge_triggered, + active_low, + shared, + number, + } + } +} + +impl Aml for Interrupt { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x89); /* Extended IRQ Descriptor */ + bytes.extend_from_slice(&6u16.to_le_bytes()); + let flags = (self.shared as u8) << 3 + | (self.active_low as u8) << 2 + | (self.edge_triggered as u8) << 1 + | self.consumer as u8; + bytes.push(flags); + bytes.push(1u8); /* count */ + bytes.extend_from_slice(&self.number.to_le_bytes()); + } +} + +pub struct Device<'a> { + path: Path, + children: Vec<&'a dyn Aml>, +} + +impl<'a> Aml for Device<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut tmp = Vec::new(); + self.path.append_aml_bytes(&mut tmp); + + for child in &self.children { + child.append_aml_bytes(&mut tmp); + } + + let pkg_length = create_pkg_length(&tmp, true); + + bytes.push(0x5b); /* ExtOpPrefix */ + bytes.push(0x82); /* DeviceOp */ + bytes.extend_from_slice(&pkg_length); + bytes.extend_from_slice(&tmp); + } +} + +impl<'a> Device<'a> { + pub fn new(path: Path, children: Vec<&'a dyn Aml>) -> Self { + Device { path, children } + } +} + +pub struct Scope<'a> { + path: Path, + children: Vec<&'a dyn Aml>, +} + +impl<'a> Aml for Scope<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut tmp = Vec::new(); + self.path.append_aml_bytes(&mut tmp); + for child in &self.children { + child.append_aml_bytes(&mut tmp); + } + + let pkg_length = create_pkg_length(&tmp, true); + + bytes.push(0x10); /* ScopeOp */ + bytes.extend_from_slice(&pkg_length); + bytes.extend_from_slice(&tmp) + } +} + +impl<'a> Scope<'a> { + pub fn new(path: Path, children: Vec<&'a dyn Aml>) -> Self { + Scope { path, children } + } +} + +pub struct Method<'a> { + path: Path, + children: Vec<&'a dyn Aml>, + args: u8, + serialized: bool, +} + +impl<'a> Method<'a> { + pub fn new(path: Path, args: u8, serialized: bool, children: Vec<&'a dyn Aml>) -> Self { + Method { + path, + children, + args, + serialized, + } + } +} + +impl<'a> Aml for Method<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut tmp = Vec::new(); + self.path.append_aml_bytes(&mut tmp); + let flags: u8 = (self.args & 0x7) | (self.serialized as u8) << 3; + tmp.push(flags); + for child in &self.children { + child.append_aml_bytes(&mut tmp); + } + + let pkg_length = create_pkg_length(&tmp, true); + + bytes.push(0x14); /* MethodOp */ + bytes.extend_from_slice(&pkg_length); + bytes.extend_from_slice(&tmp) + } +} + +pub struct Return<'a> { + value: &'a dyn Aml, +} + +impl<'a> Return<'a> { + pub fn new(value: &'a dyn Aml) -> Self { + Return { value } + } +} + +impl<'a> Aml for Return<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0xa4); /* ReturnOp */ + self.value.append_aml_bytes(bytes); + } +} + +#[derive(Clone, Copy)] +pub enum FieldAccessType { + Any, + Byte, + Word, + DWord, + QWord, + Buffer, +} + +#[derive(Clone, Copy)] +pub enum FieldUpdateRule { + Preserve = 0, + WriteAsOnes = 1, + WriteAsZeroes = 2, +} + +pub enum FieldEntry { + Named([u8; 4], usize), + Reserved(usize), +} + +pub struct Field { + path: Path, + + fields: Vec, + access_type: FieldAccessType, + update_rule: FieldUpdateRule, +} + +impl Field { + pub fn new( + path: Path, + access_type: FieldAccessType, + update_rule: FieldUpdateRule, + fields: Vec, + ) -> Self { + Field { + path, + fields, + access_type, + update_rule, + } + } +} + +impl Aml for Field { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut tmp = Vec::new(); + self.path.append_aml_bytes(&mut tmp); + + let flags: u8 = self.access_type as u8 | (self.update_rule as u8) << 5; + tmp.push(flags); + + for field in self.fields.iter() { + match field { + FieldEntry::Named(name, length) => { + tmp.extend_from_slice(name); + tmp.extend_from_slice(&create_pkg_length(&vec![0; *length], false)); + } + FieldEntry::Reserved(length) => { + tmp.push(0x0); + tmp.extend_from_slice(&create_pkg_length(&vec![0; *length], false)); + } + } + } + + let pkg_length = create_pkg_length(&tmp, true); + + bytes.push(0x5b); /* ExtOpPrefix */ + bytes.push(0x81); /* FieldOp */ + bytes.extend_from_slice(&pkg_length); + bytes.extend_from_slice(&tmp) + } +} + +#[derive(Clone, Copy)] +pub enum OpRegionSpace { + SystemMemory, + SystemIo, + PConfig, + EmbeddedControl, + Smbus, + SystemCmos, + PciBarTarget, + Ipmi, + GeneralPurposeIo, + GenericSerialBus, +} + +pub struct OpRegion { + path: Path, + space: OpRegionSpace, + offset: usize, + length: usize, +} + +impl OpRegion { + pub fn new(path: Path, space: OpRegionSpace, offset: usize, length: usize) -> Self { + OpRegion { + path, + space, + offset, + length, + } + } +} + +impl Aml for OpRegion { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x5b); /* ExtOpPrefix */ + bytes.push(0x80); /* OpRegionOp */ + self.path.append_aml_bytes(bytes); + bytes.push(self.space as u8); + self.offset.append_aml_bytes(bytes); /* RegionOffset */ + self.length.append_aml_bytes(bytes); /* RegionLen */ + } +} + +pub struct If<'a> { + predicate: &'a dyn Aml, + if_children: Vec<&'a dyn Aml>, +} + +impl<'a> If<'a> { + pub fn new(predicate: &'a dyn Aml, if_children: Vec<&'a dyn Aml>) -> Self { + If { + predicate, + if_children, + } + } +} + +impl<'a> Aml for If<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut tmp = Vec::new(); + self.predicate.append_aml_bytes(&mut tmp); + for child in self.if_children.iter() { + child.append_aml_bytes(&mut tmp); + } + + let pkg_length = create_pkg_length(&tmp, true); + + bytes.push(0xa0); /* IfOp */ + bytes.extend_from_slice(&pkg_length); + bytes.extend_from_slice(&tmp); + } +} + +pub struct Equal<'a> { + left: &'a dyn Aml, + right: &'a dyn Aml, +} + +impl<'a> Equal<'a> { + pub fn new(left: &'a dyn Aml, right: &'a dyn Aml) -> Self { + Equal { left, right } + } +} + +impl<'a> Aml for Equal<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x93); /* LEqualOp */ + self.left.append_aml_bytes(bytes); + self.right.append_aml_bytes(bytes); + } +} + +pub struct LessThan<'a> { + left: &'a dyn Aml, + right: &'a dyn Aml, +} + +impl<'a> LessThan<'a> { + pub fn new(left: &'a dyn Aml, right: &'a dyn Aml) -> Self { + LessThan { left, right } + } +} + +impl<'a> Aml for LessThan<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x95); /* LLessOp */ + self.left.append_aml_bytes(bytes); + self.right.append_aml_bytes(bytes); + } +} + +pub struct Arg(pub u8); + +impl Aml for Arg { + fn append_aml_bytes(&self, bytes: &mut Vec) { + assert!(self.0 <= 6); + bytes.push(0x68 + self.0); /* Arg0Op */ + } +} + +pub struct Local(pub u8); + +impl Aml for Local { + fn append_aml_bytes(&self, bytes: &mut Vec) { + assert!(self.0 <= 7); + bytes.push(0x60 + self.0); /* Local0Op */ + } +} + +pub struct Store<'a> { + name: &'a dyn Aml, + value: &'a dyn Aml, +} + +impl<'a> Store<'a> { + pub fn new(name: &'a dyn Aml, value: &'a dyn Aml) -> Self { + Store { name, value } + } +} + +impl<'a> Aml for Store<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x70); /* StoreOp */ + self.value.append_aml_bytes(bytes); + self.name.append_aml_bytes(bytes); + } +} + +pub struct Mutex { + path: Path, + sync_level: u8, +} + +impl Mutex { + pub fn new(path: Path, sync_level: u8) -> Self { + Self { path, sync_level } + } +} + +impl Aml for Mutex { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x5b); /* ExtOpPrefix */ + bytes.push(0x01); /* MutexOp */ + self.path.append_aml_bytes(bytes); + bytes.push(self.sync_level); + } +} + +pub struct Acquire { + mutex: Path, + timeout: u16, +} + +impl Acquire { + pub fn new(mutex: Path, timeout: u16) -> Self { + Acquire { mutex, timeout } + } +} + +impl Aml for Acquire { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x5b); /* ExtOpPrefix */ + bytes.push(0x23); /* AcquireOp */ + self.mutex.append_aml_bytes(bytes); + bytes.extend_from_slice(&self.timeout.to_le_bytes()); + } +} + +pub struct Release { + mutex: Path, +} + +impl Release { + pub fn new(mutex: Path) -> Self { + Release { mutex } + } +} + +impl Aml for Release { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x5b); /* ExtOpPrefix */ + bytes.push(0x27); /* ReleaseOp */ + self.mutex.append_aml_bytes(bytes); + } +} + +pub struct Notify<'a> { + object: &'a dyn Aml, + value: &'a dyn Aml, +} + +impl<'a> Notify<'a> { + pub fn new(object: &'a dyn Aml, value: &'a dyn Aml) -> Self { + Notify { object, value } + } +} + +impl<'a> Aml for Notify<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x86); /* NotifyOp */ + self.object.append_aml_bytes(bytes); + self.value.append_aml_bytes(bytes); + } +} + +pub struct While<'a> { + predicate: &'a dyn Aml, + while_children: Vec<&'a dyn Aml>, +} + +impl<'a> While<'a> { + pub fn new(predicate: &'a dyn Aml, while_children: Vec<&'a dyn Aml>) -> Self { + While { + predicate, + while_children, + } + } +} + +impl<'a> Aml for While<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut tmp = Vec::new(); + self.predicate.append_aml_bytes(&mut tmp); + for child in self.while_children.iter() { + child.append_aml_bytes(&mut tmp) + } + + let pkg_length = create_pkg_length(&tmp, true); + + bytes.push(0xa2); /* WhileOp */ + bytes.extend_from_slice(&pkg_length); + bytes.extend_from_slice(&tmp); + } +} + +macro_rules! binary_op { + ($name:ident, $opcode:expr) => { + pub struct $name<'a> { + a: &'a dyn Aml, + b: &'a dyn Aml, + target: &'a dyn Aml, + } + + impl<'a> $name<'a> { + pub fn new(target: &'a dyn Aml, a: &'a dyn Aml, b: &'a dyn Aml) -> Self { + $name { a, b, target } + } + } + + impl<'a> Aml for $name<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push($opcode); /* Op for the binary operator */ + self.a.append_aml_bytes(bytes); + self.b.append_aml_bytes(bytes); + self.target.append_aml_bytes(bytes); + } + } + }; +} + +/* binary operators: TermArg TermArg Target */ +binary_op!(Add, 0x72); +binary_op!(Concat, 0x73); +binary_op!(Subtract, 0x74); +binary_op!(Multiply, 0x77); +binary_op!(ShiftLeft, 0x79); +binary_op!(ShiftRight, 0x7A); +binary_op!(And, 0x7B); +binary_op!(Nand, 0x7C); +binary_op!(Or, 0x7D); +binary_op!(Nor, 0x7E); +binary_op!(Xor, 0x7F); +binary_op!(ConateRes, 0x84); +binary_op!(Mod, 0x85); +binary_op!(Index, 0x88); +binary_op!(ToString, 0x9C); + +pub struct MethodCall<'a> { + name: Path, + args: Vec<&'a dyn Aml>, +} + +impl<'a> MethodCall<'a> { + pub fn new(name: Path, args: Vec<&'a dyn Aml>) -> Self { + MethodCall { name, args } + } +} + +impl<'a> Aml for MethodCall<'a> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + self.name.append_aml_bytes(bytes); + for arg in self.args.iter() { + arg.append_aml_bytes(bytes); + } + } +} + +pub struct Buffer { + data: Vec, +} + +impl Buffer { + pub fn new(data: Vec) -> Self { + Buffer { data } + } +} + +impl Aml for Buffer { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut tmp = Vec::new(); + self.data.len().append_aml_bytes(&mut tmp); + tmp.extend_from_slice(&self.data); + + let pkg_length = create_pkg_length(&tmp, true); + + bytes.push(0x11); /* BufferOp */ + bytes.extend_from_slice(&pkg_length); + bytes.extend_from_slice(&tmp); + } +} + +pub struct CreateField<'a, T> { + buffer: &'a dyn Aml, + offset: &'a dyn Aml, + field: Path, + phantom: PhantomData<&'a T>, +} + +impl<'a, T> CreateField<'a, T> { + pub fn new(buffer: &'a dyn Aml, offset: &'a dyn Aml, field: Path) -> Self { + CreateField:: { + buffer, + offset, + field, + phantom: PhantomData, + } + } +} + +impl<'a> Aml for CreateField<'a, u64> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x8f); /* CreateQWordFieldOp */ + self.buffer.append_aml_bytes(bytes); + self.offset.append_aml_bytes(bytes); + self.field.append_aml_bytes(bytes); + } +} + +impl<'a> Aml for CreateField<'a, u32> { + fn append_aml_bytes(&self, bytes: &mut Vec) { + bytes.push(0x8a); /* CreateDWordFieldOp */ + self.buffer.append_aml_bytes(bytes); + self.offset.append_aml_bytes(bytes); + self.field.append_aml_bytes(bytes); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_device() { + /* + Device (_SB.COM1) + { + Name (_HID, EisaId ("PNP0501") /* 16550A-compatible COM Serial Port */) // _HID: Hardware ID + Name (_CRS, ResourceTemplate () // _CRS: Current Resource Settings + { + Interrupt (ResourceConsumer, Edge, ActiveHigh, Exclusive, ,, ) + { + 0x00000004, + } + IO (Decode16, + 0x03F8, // Range Minimum + 0x03F8, // Range Maximum + 0x00, // Alignment + 0x08, // Length + ) + } + } + */ + let com1_device = [ + 0x5B, 0x82, 0x30, 0x2E, 0x5F, 0x53, 0x42, 0x5F, 0x43, 0x4F, 0x4D, 0x31, 0x08, 0x5F, + 0x48, 0x49, 0x44, 0x0C, 0x41, 0xD0, 0x05, 0x01, 0x08, 0x5F, 0x43, 0x52, 0x53, 0x11, + 0x16, 0x0A, 0x13, 0x89, 0x06, 0x00, 0x03, 0x01, 0x04, 0x00, 0x00, 0x00, 0x47, 0x01, + 0xF8, 0x03, 0xF8, 0x03, 0x00, 0x08, 0x79, 0x00, + ]; + assert_eq!( + Device::new( + "_SB_.COM1".into(), + vec![ + &Name::new("_HID".into(), &EisaName::new("PNP0501")), + &Name::new( + "_CRS".into(), + &ResourceTemplate::new(vec![ + &Interrupt::new(true, true, false, false, 4), + &Io::new(0x3f8, 0x3f8, 0, 0x8) + ]) + ) + ] + ) + .to_aml_bytes(), + &com1_device[..] + ); + } + + #[test] + fn test_scope() { + /* + Scope (_SB.MBRD) + { + Name (_CRS, ResourceTemplate () // _CRS: Current Resource Settings + { + Memory32Fixed (ReadWrite, + 0xE8000000, // Address Base + 0x10000000, // Address Length + ) + }) + } + */ + + let mbrd_scope = [ + 0x10, 0x21, 0x2E, 0x5F, 0x53, 0x42, 0x5F, 0x4D, 0x42, 0x52, 0x44, 0x08, 0x5F, 0x43, + 0x52, 0x53, 0x11, 0x11, 0x0A, 0x0E, 0x86, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00, 0xE8, + 0x00, 0x00, 0x00, 0x10, 0x79, 0x00, + ]; + + assert_eq!( + Scope::new( + "_SB_.MBRD".into(), + vec![&Name::new( + "_CRS".into(), + &ResourceTemplate::new(vec![&Memory32Fixed::new( + true, + 0xE800_0000, + 0x1000_0000 + )]) + )] + ) + .to_aml_bytes(), + &mbrd_scope[..] + ); + } + + #[test] + fn test_resource_template() { + /* + Name (_CRS, ResourceTemplate () // _CRS: Current Resource Settings + { + Memory32Fixed (ReadWrite, + 0xE8000000, // Address Base + 0x10000000, // Address Length + ) + }) + */ + let crs_memory_32_fixed = [ + 0x08, 0x5F, 0x43, 0x52, 0x53, 0x11, 0x11, 0x0A, 0x0E, 0x86, 0x09, 0x00, 0x01, 0x00, + 0x00, 0x00, 0xE8, 0x00, 0x00, 0x00, 0x10, 0x79, 0x00, + ]; + + assert_eq!( + Name::new( + "_CRS".into(), + &ResourceTemplate::new(vec![&Memory32Fixed::new(true, 0xE800_0000, 0x1000_0000)]) + ) + .to_aml_bytes(), + crs_memory_32_fixed + ); + + /* + Name (_CRS, ResourceTemplate () // _CRS: Current Resource Settings + { + WordBusNumber (ResourceProducer, MinFixed, MaxFixed, PosDecode, + 0x0000, // Granularity + 0x0000, // Range Minimum + 0x00FF, // Range Maximum + 0x0000, // Translation Offset + 0x0100, // Length + ,, ) + WordIO (ResourceProducer, MinFixed, MaxFixed, PosDecode, EntireRange, + 0x0000, // Granularity + 0x0000, // Range Minimum + 0x0CF7, // Range Maximum + 0x0000, // Translation Offset + 0x0CF8, // Length + ,, , TypeStatic, DenseTranslation) + WordIO (ResourceProducer, MinFixed, MaxFixed, PosDecode, EntireRange, + 0x0000, // Granularity + 0x0D00, // Range Minimum + 0xFFFF, // Range Maximum + 0x0000, // Translation Offset + 0xF300, // Length + ,, , TypeStatic, DenseTranslation) + DWordMemory (ResourceProducer, PosDecode, MinFixed, MaxFixed, Cacheable, ReadWrite, + 0x00000000, // Granularity + 0x000A0000, // Range Minimum + 0x000BFFFF, // Range Maximum + 0x00000000, // Translation Offset + 0x00020000, // Length + ,, , AddressRangeMemory, TypeStatic) + DWordMemory (ResourceProducer, PosDecode, MinFixed, MaxFixed, NonCacheable, ReadWrite, + 0x00000000, // Granularity + 0xC0000000, // Range Minimum + 0xFEBFFFFF, // Range Maximum + 0x00000000, // Translation Offset + 0x3EC00000, // Length + ,, , AddressRangeMemory, TypeStatic) + QWordMemory (ResourceProducer, PosDecode, MinFixed, MaxFixed, Cacheable, ReadWrite, + 0x0000000000000000, // Granularity + 0x0000000800000000, // Range Minimum + 0x0000000FFFFFFFFF, // Range Maximum + 0x0000000000000000, // Translation Offset + 0x0000000800000000, // Length + ,, , AddressRangeMemory, TypeStatic) + }) + */ + + // WordBusNumber from above + let crs_word_bus_number = [ + 0x08, 0x5F, 0x43, 0x52, 0x53, 0x11, 0x15, 0x0A, 0x12, 0x88, 0x0D, 0x00, 0x02, 0x0C, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x01, 0x79, 0x00, + ]; + + assert_eq!( + Name::new( + "_CRS".into(), + &ResourceTemplate::new(vec![&AddressSpace::new_bus_number(0x0u16, 0xffu16),]) + ) + .to_aml_bytes(), + &crs_word_bus_number + ); + + // WordIO blocks (x 2) from above + let crs_word_io = [ + 0x08, 0x5F, 0x43, 0x52, 0x53, 0x11, 0x25, 0x0A, 0x22, 0x88, 0x0D, 0x00, 0x01, 0x0C, + 0x03, 0x00, 0x00, 0x00, 0x00, 0xF7, 0x0C, 0x00, 0x00, 0xF8, 0x0C, 0x88, 0x0D, 0x00, + 0x01, 0x0C, 0x03, 0x00, 0x00, 0x00, 0x0D, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xF3, 0x79, + 0x00, + ]; + + assert_eq!( + Name::new( + "_CRS".into(), + &ResourceTemplate::new(vec![ + &AddressSpace::new_io(0x0u16, 0xcf7u16), + &AddressSpace::new_io(0xd00u16, 0xffffu16), + ]) + ) + .to_aml_bytes(), + &crs_word_io[..] + ); + + // DWordMemory blocks (x 2) from above + let crs_dword_memory = [ + 0x08, 0x5F, 0x43, 0x52, 0x53, 0x11, 0x39, 0x0A, 0x36, 0x87, 0x17, 0x00, 0x00, 0x0C, + 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0xFF, 0xFF, 0x0B, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x87, 0x17, 0x00, 0x00, 0x0C, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0xFF, 0xBF, 0xFE, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xC0, 0x3E, 0x79, 0x00, + ]; + + assert_eq!( + Name::new( + "_CRS".into(), + &ResourceTemplate::new(vec![ + &AddressSpace::new_memory( + AddressSpaceCachable::Cacheable, + true, + 0xa_0000u32, + 0xb_ffffu32 + ), + &AddressSpace::new_memory( + AddressSpaceCachable::NotCacheable, + true, + 0xc000_0000u32, + 0xfebf_ffffu32 + ), + ]) + ) + .to_aml_bytes(), + &crs_dword_memory[..] + ); + + // QWordMemory from above + let crs_qword_memory = [ + 0x08, 0x5F, 0x43, 0x52, 0x53, 0x11, 0x33, 0x0A, 0x30, 0x8A, 0x2B, 0x00, 0x00, 0x0C, + 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, + 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x79, + 0x00, + ]; + + assert_eq!( + Name::new( + "_CRS".into(), + &ResourceTemplate::new(vec![&AddressSpace::new_memory( + AddressSpaceCachable::Cacheable, + true, + 0x8_0000_0000u64, + 0xf_ffff_ffffu64 + )]) + ) + .to_aml_bytes(), + &crs_qword_memory[..] + ); + + /* + Name (_CRS, ResourceTemplate () // _CRS: Current Resource Settings + { + Interrupt (ResourceConsumer, Edge, ActiveHigh, Exclusive, ,, ) + { + 0x00000004, + } + IO (Decode16, + 0x03F8, // Range Minimum + 0x03F8, // Range Maximum + 0x00, // Alignment + 0x08, // Length + ) + }) + + */ + let interrupt_io_data = [ + 0x08, 0x5F, 0x43, 0x52, 0x53, 0x11, 0x16, 0x0A, 0x13, 0x89, 0x06, 0x00, 0x03, 0x01, + 0x04, 0x00, 0x00, 0x00, 0x47, 0x01, 0xF8, 0x03, 0xF8, 0x03, 0x00, 0x08, 0x79, 0x00, + ]; + + assert_eq!( + Name::new( + "_CRS".into(), + &ResourceTemplate::new(vec![ + &Interrupt::new(true, true, false, false, 4), + &Io::new(0x3f8, 0x3f8, 0, 0x8) + ]) + ) + .to_aml_bytes(), + &interrupt_io_data[..] + ); + } + + #[test] + fn test_pkg_length() { + assert_eq!(create_pkg_length(&[0u8; 62], true), vec![63]); + assert_eq!( + create_pkg_length(&[0u8; 64], true), + vec![1 << 6 | (66 & 0xf), 66 >> 4] + ); + assert_eq!( + create_pkg_length(&[0u8; 4096], true), + vec![ + 2 << 6 | (4099 & 0xf) as u8, + (4099 >> 4) as u8, + (4099 >> 12) as u8 + ] + ); + } + + #[test] + fn test_package() { + /* + Name (_S5, Package (0x01) // _S5_: S5 System State + { + 0x05 + }) + */ + let s5_sleep_data = [0x08, 0x5F, 0x53, 0x35, 0x5F, 0x12, 0x04, 0x01, 0x0A, 0x05]; + + let s5 = Name::new("_S5_".into(), &Package::new(vec![&5u8])); + + assert_eq!(s5_sleep_data.to_vec(), s5.to_aml_bytes()); + } + + #[test] + fn test_eisa_name() { + assert_eq!( + Name::new("_HID".into(), &EisaName::new("PNP0501")).to_aml_bytes(), + [0x08, 0x5F, 0x48, 0x49, 0x44, 0x0C, 0x41, 0xD0, 0x05, 0x01], + ) + } + #[test] + fn test_name_path() { + assert_eq!( + (&"_SB_".into() as &Path).to_aml_bytes(), + [0x5Fu8, 0x53, 0x42, 0x5F] + ); + assert_eq!( + (&"\\_SB_".into() as &Path).to_aml_bytes(), + [0x5C, 0x5F, 0x53, 0x42, 0x5F] + ); + assert_eq!( + (&"_SB_.COM1".into() as &Path).to_aml_bytes(), + [0x2E, 0x5F, 0x53, 0x42, 0x5F, 0x43, 0x4F, 0x4D, 0x31] + ); + assert_eq!( + (&"_SB_.PCI0._HID".into() as &Path).to_aml_bytes(), + [0x2F, 0x03, 0x5F, 0x53, 0x42, 0x5F, 0x50, 0x43, 0x49, 0x30, 0x5F, 0x48, 0x49, 0x44] + ); + } + + #[test] + fn test_numbers() { + assert_eq!(128u8.to_aml_bytes(), [0x0a, 0x80]); + assert_eq!(1024u16.to_aml_bytes(), [0x0b, 0x0, 0x04]); + assert_eq!((16u32 << 20).to_aml_bytes(), [0x0c, 0x00, 0x00, 0x0, 0x01]); + assert_eq!( + 0xdeca_fbad_deca_fbadu64.to_aml_bytes(), + [0x0e, 0xad, 0xfb, 0xca, 0xde, 0xad, 0xfb, 0xca, 0xde] + ); + } + + #[test] + fn test_name() { + assert_eq!( + Name::new("_SB_.PCI0._UID".into(), &0x1234u16).to_aml_bytes(), + [ + 0x08, /* NameOp */ + 0x2F, /* MultiNamePrefix */ + 0x03, /* 3 name parts */ + 0x5F, 0x53, 0x42, 0x5F, /* _SB_ */ + 0x50, 0x43, 0x49, 0x30, /* PCI0 */ + 0x5F, 0x55, 0x49, 0x44, /* _UID */ + 0x0b, /* WordPrefix */ + 0x34, 0x12 + ] + ); + } + + #[test] + fn test_string() { + assert_eq!( + (&"ACPI" as &dyn Aml).to_aml_bytes(), + [0x0d, b'A', b'C', b'P', b'I', 0] + ); + assert_eq!( + "ACPI".to_owned().to_aml_bytes(), + [0x0d, b'A', b'C', b'P', b'I', 0] + ); + } + + #[test] + fn test_method() { + assert_eq!( + Method::new("_STA".into(), 0, false, vec![&Return::new(&0xfu8)]).to_aml_bytes(), + [0x14, 0x09, 0x5F, 0x53, 0x54, 0x41, 0x00, 0xA4, 0x0A, 0x0F] + ); + } + + #[test] + fn test_field() { + /* + Field (PRST, ByteAcc, NoLock, WriteAsZeros) + { + Offset (0x04), + CPEN, 1, + CINS, 1, + CRMV, 1, + CEJ0, 1, + Offset (0x05), + CCMD, 8 + } + + */ + + let field_data = [ + 0x5Bu8, 0x81, 0x23, 0x50, 0x52, 0x53, 0x54, 0x41, 0x00, 0x20, 0x43, 0x50, 0x45, 0x4E, + 0x01, 0x43, 0x49, 0x4E, 0x53, 0x01, 0x43, 0x52, 0x4D, 0x56, 0x01, 0x43, 0x45, 0x4A, + 0x30, 0x01, 0x00, 0x04, 0x43, 0x43, 0x4D, 0x44, 0x08, + ]; + + assert_eq!( + Field::new( + "PRST".into(), + FieldAccessType::Byte, + FieldUpdateRule::WriteAsZeroes, + vec![ + FieldEntry::Reserved(32), + FieldEntry::Named(*b"CPEN", 1), + FieldEntry::Named(*b"CINS", 1), + FieldEntry::Named(*b"CRMV", 1), + FieldEntry::Named(*b"CEJ0", 1), + FieldEntry::Reserved(4), + FieldEntry::Named(*b"CCMD", 8) + ] + ) + .to_aml_bytes(), + &field_data[..] + ); + + /* + Field (PRST, DWordAcc, NoLock, Preserve) + { + CSEL, 32, + Offset (0x08), + CDAT, 32 + } + */ + + let field_data = [ + 0x5Bu8, 0x81, 0x12, 0x50, 0x52, 0x53, 0x54, 0x03, 0x43, 0x53, 0x45, 0x4C, 0x20, 0x00, + 0x20, 0x43, 0x44, 0x41, 0x54, 0x20, + ]; + + assert_eq!( + Field::new( + "PRST".into(), + FieldAccessType::DWord, + FieldUpdateRule::Preserve, + vec![ + FieldEntry::Named(*b"CSEL", 32), + FieldEntry::Reserved(32), + FieldEntry::Named(*b"CDAT", 32) + ] + ) + .to_aml_bytes(), + &field_data[..] + ); + } + + #[test] + fn test_op_region() { + /* + OperationRegion (PRST, SystemIo, 0x0CD8, 0x0C) + */ + let op_region_data = [ + 0x5Bu8, 0x80, 0x50, 0x52, 0x53, 0x54, 0x01, 0x0B, 0xD8, 0x0C, 0x0A, 0x0C, + ]; + + assert_eq!( + OpRegion::new("PRST".into(), OpRegionSpace::SystemIo, 0xcd8, 0xc).to_aml_bytes(), + &op_region_data[..] + ); + } + + #[test] + fn test_arg_if() { + /* + Method(TEST, 1, NotSerialized) { + If (Arg0 == Zero) { + Return(One) + } + Return(Zero) + } + */ + let arg_if_data = [ + 0x14, 0x0F, 0x54, 0x45, 0x53, 0x54, 0x01, 0xA0, 0x06, 0x93, 0x68, 0x00, 0xA4, 0x01, + 0xA4, 0x00, + ]; + + assert_eq!( + Method::new( + "TEST".into(), + 1, + false, + vec![ + &If::new(&Equal::new(&Arg(0), &ZERO), vec![&Return::new(&ONE)]), + &Return::new(&ZERO) + ] + ) + .to_aml_bytes(), + &arg_if_data + ); + } + + #[test] + fn test_local_if() { + /* + Method(TEST, 0, NotSerialized) { + Local0 = One + If (Local0 == Zero) { + Return(One) + } + Return(Zero) + } + */ + let local_if_data = [ + 0x14, 0x12, 0x54, 0x45, 0x53, 0x54, 0x00, 0x70, 0x01, 0x60, 0xA0, 0x06, 0x93, 0x60, + 0x00, 0xA4, 0x01, 0xA4, 0x00, + ]; + assert_eq!( + Method::new( + "TEST".into(), + 0, + false, + vec![ + &Store::new(&Local(0), &ONE), + &If::new(&Equal::new(&Local(0), &ZERO), vec![&Return::new(&ONE)]), + &Return::new(&ZERO) + ] + ) + .to_aml_bytes(), + &local_if_data + ); + } + + #[test] + fn test_mutex() { + /* + Device (_SB_.MHPC) + { + Name (_HID, EisaId("PNP0A06") /* Generic Container Device */) // _HID: Hardware ID + Mutex (MLCK, 0x00) + Method (TEST, 0, NotSerialized) + { + Acquire (MLCK, 0xFFFF) + Local0 = One + Release (MLCK) + } + } + */ + + let mutex_data = [ + 0x5B, 0x82, 0x33, 0x2E, 0x5F, 0x53, 0x42, 0x5F, 0x4D, 0x48, 0x50, 0x43, 0x08, 0x5F, + 0x48, 0x49, 0x44, 0x0C, 0x41, 0xD0, 0x0A, 0x06, 0x5B, 0x01, 0x4D, 0x4C, 0x43, 0x4B, + 0x00, 0x14, 0x17, 0x54, 0x45, 0x53, 0x54, 0x00, 0x5B, 0x23, 0x4D, 0x4C, 0x43, 0x4B, + 0xFF, 0xFF, 0x70, 0x01, 0x60, 0x5B, 0x27, 0x4D, 0x4C, 0x43, 0x4B, + ]; + + let mutex = Mutex::new("MLCK".into(), 0); + assert_eq!( + Device::new( + "_SB_.MHPC".into(), + vec![ + &Name::new("_HID".into(), &EisaName::new("PNP0A06")), + &mutex, + &Method::new( + "TEST".into(), + 0, + false, + vec![ + &Acquire::new("MLCK".into(), 0xffff), + &Store::new(&Local(0), &ONE), + &Release::new("MLCK".into()) + ] + ) + ] + ) + .to_aml_bytes(), + &mutex_data[..] + ); + } + + #[test] + fn test_notify() { + /* + Device (_SB.MHPC) + { + Name (_HID, EisaId ("PNP0A06") /* Generic Container Device */) // _HID: Hardware ID + Method (TEST, 0, NotSerialized) + { + Notify (MHPC, One) // Device Check + } + } + */ + let notify_data = [ + 0x5B, 0x82, 0x21, 0x2E, 0x5F, 0x53, 0x42, 0x5F, 0x4D, 0x48, 0x50, 0x43, 0x08, 0x5F, + 0x48, 0x49, 0x44, 0x0C, 0x41, 0xD0, 0x0A, 0x06, 0x14, 0x0C, 0x54, 0x45, 0x53, 0x54, + 0x00, 0x86, 0x4D, 0x48, 0x50, 0x43, 0x01, + ]; + + assert_eq!( + Device::new( + "_SB_.MHPC".into(), + vec![ + &Name::new("_HID".into(), &EisaName::new("PNP0A06")), + &Method::new( + "TEST".into(), + 0, + false, + vec![&Notify::new(&Path::new("MHPC"), &ONE),] + ) + ] + ) + .to_aml_bytes(), + ¬ify_data[..] + ); + } + + #[test] + fn test_while() { + /* + Device (_SB.MHPC) + { + Name (_HID, EisaId ("PNP0A06") /* Generic Container Device */) // _HID: Hardware ID + Method (TEST, 0, NotSerialized) + { + Local0 = Zero + While ((Local0 < 0x04)) + { + Local0 += One + } + } + } + */ + + let while_data = [ + 0x5B, 0x82, 0x28, 0x2E, 0x5F, 0x53, 0x42, 0x5F, 0x4D, 0x48, 0x50, 0x43, 0x08, 0x5F, + 0x48, 0x49, 0x44, 0x0C, 0x41, 0xD0, 0x0A, 0x06, 0x14, 0x13, 0x54, 0x45, 0x53, 0x54, + 0x00, 0x70, 0x00, 0x60, 0xA2, 0x09, 0x95, 0x60, 0x0A, 0x04, 0x72, 0x60, 0x01, 0x60, + ]; + + assert_eq!( + Device::new( + "_SB_.MHPC".into(), + vec![ + &Name::new("_HID".into(), &EisaName::new("PNP0A06")), + &Method::new( + "TEST".into(), + 0, + false, + vec![ + &Store::new(&Local(0), &ZERO), + &While::new( + &LessThan::new(&Local(0), &4usize), + vec![&Add::new(&Local(0), &Local(0), &ONE)] + ) + ] + ) + ] + ) + .to_aml_bytes(), + &while_data[..] + ) + } + + #[test] + fn test_method_call() { + /* + Method (TST1, 1, NotSerialized) + { + TST2 (One, One) + } + + Method (TST2, 2, NotSerialized) + { + TST1 (One) + } + */ + let test_data = [ + 0x14, 0x0C, 0x54, 0x53, 0x54, 0x31, 0x01, 0x54, 0x53, 0x54, 0x32, 0x01, 0x01, 0x14, + 0x0B, 0x54, 0x53, 0x54, 0x32, 0x02, 0x54, 0x53, 0x54, 0x31, 0x01, + ]; + + let mut methods = Vec::new(); + methods.extend_from_slice( + &Method::new( + "TST1".into(), + 1, + false, + vec![&MethodCall::new("TST2".into(), vec![&ONE, &ONE])], + ) + .to_aml_bytes(), + ); + methods.extend_from_slice( + &Method::new( + "TST2".into(), + 2, + false, + vec![&MethodCall::new("TST1".into(), vec![&ONE])], + ) + .to_aml_bytes(), + ); + assert_eq!(&methods[..], &test_data[..]) + } + + #[test] + fn test_buffer() { + /* + Name (_MAT, Buffer (0x08) // _MAT: Multiple APIC Table Entry + { + 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 /* ........ */ + }) + */ + let buffer_data = [ + 0x08, 0x5F, 0x4D, 0x41, 0x54, 0x11, 0x0B, 0x0A, 0x08, 0x00, 0x08, 0x00, 0x00, 0x01, + 0x00, 0x00, 0x00, + ]; + + assert_eq!( + Name::new( + "_MAT".into(), + &Buffer::new(vec![0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]) + ) + .to_aml_bytes(), + &buffer_data[..] + ) + } + + #[test] + fn test_create_field() { + /* + Method (MCRS, 0, Serialized) + { + Name (MR64, ResourceTemplate () + { + QWordMemory (ResourceProducer, PosDecode, MinFixed, MaxFixed, Cacheable, ReadWrite, + 0x0000000000000000, // Granularity + 0x0000000000000000, // Range Minimum + 0xFFFFFFFFFFFFFFFE, // Range Maximum + 0x0000000000000000, // Translation Offset + 0xFFFFFFFFFFFFFFFF, // Length + ,, _Y00, AddressRangeMemory, TypeStatic) + }) + CreateQWordField (MR64, \_SB.MHPC.MCRS._Y00._MIN, MIN) // _MIN: Minimum Base Address + CreateQWordField (MR64, \_SB.MHPC.MCRS._Y00._MAX, MAX) // _MAX: Maximum Base Address + CreateQWordField (MR64, \_SB.MHPC.MCRS._Y00._LEN, LEN) // _LEN: Length + } + */ + let data = [ + 0x14, 0x41, 0x06, 0x4D, 0x43, 0x52, 0x53, 0x08, 0x08, 0x4D, 0x52, 0x36, 0x34, 0x11, + 0x33, 0x0A, 0x30, 0x8A, 0x2B, 0x00, 0x00, 0x0C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x79, 0x00, 0x8F, 0x4D, 0x52, 0x36, 0x34, + 0x0A, 0x0E, 0x4D, 0x49, 0x4E, 0x5F, 0x8F, 0x4D, 0x52, 0x36, 0x34, 0x0A, 0x16, 0x4D, + 0x41, 0x58, 0x5F, 0x8F, 0x4D, 0x52, 0x36, 0x34, 0x0A, 0x26, 0x4C, 0x45, 0x4E, 0x5F, + ]; + + assert_eq!( + Method::new( + "MCRS".into(), + 0, + true, + vec![ + &Name::new( + "MR64".into(), + &ResourceTemplate::new(vec![&AddressSpace::new_memory( + AddressSpaceCachable::Cacheable, + true, + 0x0000_0000_0000_0000u64, + 0xFFFF_FFFF_FFFF_FFFEu64 + )]) + ), + &CreateField::::new(&Path::new("MR64"), &14usize, "MIN_".into()), + &CreateField::::new(&Path::new("MR64"), &22usize, "MAX_".into()), + &CreateField::::new(&Path::new("MR64"), &38usize, "LEN_".into()), + ] + ) + .to_aml_bytes(), + &data[..] + ); + } +} diff --git a/vendor/cube-hypervisor/acpi_tables/src/lib.rs b/vendor/cube-hypervisor/acpi_tables/src/lib.rs new file mode 100644 index 0000000..1ca517c --- /dev/null +++ b/vendor/cube-hypervisor/acpi_tables/src/lib.rs @@ -0,0 +1,12 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +pub mod aml; +pub mod rsdp; +pub mod sdt; + +fn generate_checksum(data: &[u8]) -> u8 { + (255 - data.iter().fold(0u8, |acc, x| acc.wrapping_add(*x))).wrapping_add(1) +} diff --git a/vendor/cube-hypervisor/acpi_tables/src/rsdp.rs b/vendor/cube-hypervisor/acpi_tables/src/rsdp.rs new file mode 100644 index 0000000..b4b84ac --- /dev/null +++ b/vendor/cube-hypervisor/acpi_tables/src/rsdp.rs @@ -0,0 +1,68 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use vm_memory::ByteValued; + +#[repr(packed)] +#[derive(Clone, Copy, Default)] +pub struct Rsdp { + pub signature: [u8; 8], + pub checksum: u8, + pub oem_id: [u8; 6], + pub revision: u8, + _rsdt_addr: u32, + pub length: u32, + pub xsdt_addr: u64, + pub extended_checksum: u8, + _reserved: [u8; 3], +} + +// SAFETY: Rsdp only contains a series of integers +unsafe impl ByteValued for Rsdp {} + +impl Rsdp { + pub fn new(oem_id: [u8; 6], xsdt_addr: u64) -> Self { + let mut rsdp = Rsdp { + signature: *b"RSD PTR ", + checksum: 0, + oem_id, + revision: 2, + _rsdt_addr: 0, + length: std::mem::size_of::() as u32, + xsdt_addr, + extended_checksum: 0, + _reserved: [0; 3], + }; + + rsdp.checksum = super::generate_checksum(&rsdp.as_slice()[0..19]); + rsdp.extended_checksum = super::generate_checksum(rsdp.as_slice()); + rsdp + } + + pub fn len() -> usize { + std::mem::size_of::() + } +} + +#[cfg(test)] +mod tests { + use super::Rsdp; + use vm_memory::bytes::ByteValued; + + #[test] + fn test_rsdp() { + let rsdp = Rsdp::new(*b"CHYPER", 0xdead_beef); + let sum = rsdp + .as_slice() + .iter() + .fold(0u8, |acc, x| acc.wrapping_add(*x)); + assert_eq!(sum, 0); + let sum: u8 = rsdp + .as_slice() + .iter() + .fold(0u8, |acc, x| acc.wrapping_add(*x)); + assert_eq!(sum, 0); + } +} diff --git a/vendor/cube-hypervisor/acpi_tables/src/sdt.rs b/vendor/cube-hypervisor/acpi_tables/src/sdt.rs new file mode 100644 index 0000000..7c2ff1c --- /dev/null +++ b/vendor/cube-hypervisor/acpi_tables/src/sdt.rs @@ -0,0 +1,152 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use zerocopy::AsBytes; + +#[repr(packed)] +#[derive(Clone, Copy, AsBytes)] +pub struct GenericAddress { + pub address_space_id: u8, + pub register_bit_width: u8, + pub register_bit_offset: u8, + pub access_size: u8, + pub address: u64, +} + +impl GenericAddress { + pub fn io_port_address(address: u16) -> Self { + GenericAddress { + address_space_id: 1, + register_bit_width: 8 * std::mem::size_of::() as u8, + register_bit_offset: 0, + access_size: std::mem::size_of::() as u8, + address: u64::from(address), + } + } + pub fn mmio_address(address: u64) -> Self { + GenericAddress { + address_space_id: 0, + register_bit_width: 8 * std::mem::size_of::() as u8, + register_bit_offset: 0, + access_size: std::mem::size_of::() as u8, + address, + } + } +} + +pub struct Sdt { + data: Vec, +} + +#[allow(clippy::len_without_is_empty)] +impl Sdt { + pub fn new( + signature: [u8; 4], + length: u32, + revision: u8, + oem_id: [u8; 6], + oem_table: [u8; 8], + oem_revision: u32, + ) -> Self { + assert!(length >= 36); + + let mut data = Vec::with_capacity(length as usize); + data.extend_from_slice(&signature); + data.extend_from_slice(&length.to_le_bytes()); + data.push(revision); + data.push(0); // checksum + data.extend_from_slice(&oem_id); + data.extend_from_slice(&oem_table); + data.extend_from_slice(&oem_revision.to_le_bytes()); + data.extend_from_slice(b"CLDH"); + data.extend_from_slice(&0u32.to_le_bytes()); + assert_eq!(data.len(), 36); + + data.resize(length as usize, 0); + let mut sdt = Sdt { data }; + + sdt.update_checksum(); + sdt + } + + pub fn update_checksum(&mut self) { + self.data[9] = 0; + let checksum = super::generate_checksum(self.data.as_slice()); + self.data[9] = checksum + } + + pub fn as_slice(&self) -> &[u8] { + self.data.as_slice() + } + + pub fn append(&mut self, value: T) { + let orig_length = self.data.len(); + let new_length = orig_length + std::mem::size_of::(); + self.data.resize(new_length, 0); + self.write_u32(4, new_length as u32); + self.write(orig_length, value); + } + + pub fn append_slice(&mut self, data: &[u8]) { + let orig_length = self.data.len(); + let new_length = orig_length + data.len(); + self.write_u32(4, new_length as u32); + self.data.extend_from_slice(data); + self.update_checksum(); + } + + /// Write a slice of data at a specific offset + pub fn write_bytes(&mut self, offset: usize, data: &[u8]) { + assert!(offset + data.len() <= self.data.len()); + self.data.as_mut_slice()[offset..offset + data.len()].copy_from_slice(data); + self.update_checksum(); + } + + /// Write a value at the given offset + pub fn write(&mut self, offset: usize, value: T) { + self.write_bytes(offset, value.as_bytes()) + } + + pub fn write_u8(&mut self, offset: usize, val: u8) { + self.write(offset, val); + } + + pub fn write_u16(&mut self, offset: usize, val: u16) { + self.write(offset, val); + } + + pub fn write_u32(&mut self, offset: usize, val: u32) { + self.write(offset, val); + } + + pub fn write_u64(&mut self, offset: usize, val: u64) { + self.write(offset, val); + } + + pub fn len(&self) -> usize { + self.data.len() + } +} + +#[cfg(test)] +mod tests { + use super::Sdt; + + #[test] + fn test_sdt() { + let mut sdt = Sdt::new(*b"TEST", 40, 1, *b"CLOUDH", *b"TESTTEST", 1); + let sum: u8 = sdt + .as_slice() + .iter() + .fold(0u8, |acc, x| acc.wrapping_add(*x)); + assert_eq!(sum, 0); + sdt.write_u32(36, 0x12345678); + let sum: u8 = sdt + .as_slice() + .iter() + .fold(0u8, |acc, x| acc.wrapping_add(*x)); + assert_eq!(sum, 0); + } +} diff --git a/vendor/cube-hypervisor/api_client/Cargo.toml b/vendor/cube-hypervisor/api_client/Cargo.toml new file mode 100644 index 0000000..e2fba52 --- /dev/null +++ b/vendor/cube-hypervisor/api_client/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "api_client" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[dependencies] +vmm-sys-util = "0.12.1" diff --git a/vendor/cube-hypervisor/api_client/src/lib.rs b/vendor/cube-hypervisor/api_client/src/lib.rs new file mode 100644 index 0000000..b2bd4d9 --- /dev/null +++ b/vendor/cube-hypervisor/api_client/src/lib.rs @@ -0,0 +1,250 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use std::fmt; +use std::io::{Read, Write}; +use std::os::unix::io::RawFd; +use vmm_sys_util::sock_ctrl_msg::ScmSocket; + +#[derive(Debug)] +pub enum Error { + Socket(std::io::Error), + SocketSendFds(vmm_sys_util::errno::Error), + StatusCodeParsing(std::num::ParseIntError), + MissingProtocol, + ContentLengthParsing(std::num::ParseIntError), + ServerResponse(StatusCode, Option), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use Error::*; + match self { + Socket(e) => write!(f, "Error writing to or reading from HTTP socket: {}", e), + SocketSendFds(e) => write!(f, "Error writing to or reading from HTTP socket: {}", e), + StatusCodeParsing(e) => write!(f, "Error parsing HTTP status code: {}", e), + MissingProtocol => write!(f, "HTTP output is missing protocol statement"), + ContentLengthParsing(e) => write!(f, "Error parsing HTTP Content-Length field: {}", e), + ServerResponse(s, o) => { + if let Some(o) = o { + write!(f, "Server responded with an error: {:?}: {}", s, o) + } else { + write!(f, "Server responded with an error: {:?}", s) + } + } + } + } +} + +#[derive(Clone, Copy, Debug)] +pub enum StatusCode { + Continue, + Ok, + NoContent, + BadRequest, + NotFound, + InternalServerError, + NotImplemented, + Unknown, +} + +impl StatusCode { + fn from_raw(code: usize) -> StatusCode { + match code { + 100 => StatusCode::Continue, + 200 => StatusCode::Ok, + 204 => StatusCode::NoContent, + 400 => StatusCode::BadRequest, + 404 => StatusCode::NotFound, + 500 => StatusCode::InternalServerError, + 501 => StatusCode::NotImplemented, + _ => StatusCode::Unknown, + } + } + + fn parse(code: &str) -> Result { + Ok(StatusCode::from_raw( + code.trim().parse().map_err(Error::StatusCodeParsing)?, + )) + } + + fn is_server_error(self) -> bool { + !matches!( + self, + StatusCode::Ok | StatusCode::Continue | StatusCode::NoContent + ) + } +} + +fn get_header<'a>(res: &'a str, header: &'a str) -> Option<&'a str> { + let header_str = format!("{}: ", header); + res.find(&header_str) + .map(|o| &res[o + header_str.len()..o + res[o..].find('\r').unwrap()]) +} + +fn get_status_code(res: &str) -> Result { + if let Some(o) = res.find("HTTP/1.1") { + Ok(StatusCode::parse( + &res[o + "HTTP/1.1 ".len()..res[o..].find('\r').unwrap()], + )?) + } else { + Err(Error::MissingProtocol) + } +} + +fn parse_http_response(socket: &mut dyn Read) -> Result, Error> { + let mut res = String::new(); + let mut body_offset = None; + let mut content_length: Option = None; + loop { + let mut bytes = vec![0; 256]; + let count = socket.read(&mut bytes).map_err(Error::Socket)?; + // If the return value is 0, the peer has performed an orderly shutdown. + if count == 0 { + break; + } + res.push_str(std::str::from_utf8(&bytes[0..count]).unwrap()); + + // End of headers + if let Some(o) = res.find("\r\n\r\n") { + body_offset = Some(o + "\r\n\r\n".len()); + + // With all headers available we can see if there is any body + content_length = if let Some(length) = get_header(&res, "Content-Length") { + Some(length.trim().parse().map_err(Error::ContentLengthParsing)?) + } else { + None + }; + + if content_length.is_none() { + break; + } + } + + if let Some(body_offset) = body_offset { + if let Some(content_length) = content_length { + if res.len() >= content_length + body_offset { + break; + } + } + } + } + let body_string = content_length.and(body_offset.map(|o| String::from(&res[o..]))); + let status_code = get_status_code(&res)?; + + if status_code.is_server_error() { + Err(Error::ServerResponse(status_code, body_string)) + } else { + Ok(body_string) + } +} + +/// Make an API request using the fully qualified command name. +/// For example, full_command could be "vm.create" or "vmm.ping". +pub fn simple_api_full_command_with_fds_and_response( + socket: &mut T, + method: &str, + full_command: &str, + request_body: Option<&str>, + request_fds: Vec, +) -> Result, Error> { + socket + .send_with_fds( + &[format!( + "{} /api/v1/{} HTTP/1.1\r\nHost: localhost\r\nAccept: */*\r\n", + method, full_command + ) + .as_bytes()], + &request_fds, + ) + .map_err(Error::SocketSendFds)?; + + if let Some(request_body) = request_body { + socket + .write_all(format!("Content-Length: {}\r\n", request_body.len()).as_bytes()) + .map_err(Error::Socket)?; + } + + socket.write_all(b"\r\n").map_err(Error::Socket)?; + + if let Some(request_body) = request_body { + socket + .write_all(request_body.as_bytes()) + .map_err(Error::Socket)?; + } + + socket.flush().map_err(Error::Socket)?; + + parse_http_response(socket) +} + +pub fn simple_api_full_command_with_fds( + socket: &mut T, + method: &str, + full_command: &str, + request_body: Option<&str>, + request_fds: Vec, +) -> Result<(), Error> { + let response = simple_api_full_command_with_fds_and_response( + socket, + method, + full_command, + request_body, + request_fds, + )?; + + if response.is_some() { + println!("{}", response.unwrap()); + } + + Ok(()) +} + +pub fn simple_api_full_command( + socket: &mut T, + method: &str, + full_command: &str, + request_body: Option<&str>, +) -> Result<(), Error> { + simple_api_full_command_with_fds(socket, method, full_command, request_body, Vec::new()) +} + +pub fn simple_api_full_command_and_response( + socket: &mut T, + method: &str, + full_command: &str, + request_body: Option<&str>, +) -> Result, Error> { + simple_api_full_command_with_fds_and_response( + socket, + method, + full_command, + request_body, + Vec::new(), + ) +} + +pub fn simple_api_command_with_fds( + socket: &mut T, + method: &str, + c: &str, + request_body: Option<&str>, + request_fds: Vec, +) -> Result<(), Error> { + // Create the full VM command. For VMM commands, use + // simple_api_full_command(). + let full_command = format!("vm.{}", c); + + simple_api_full_command_with_fds(socket, method, &full_command, request_body, request_fds) +} + +pub fn simple_api_command( + socket: &mut T, + method: &str, + c: &str, + request_body: Option<&str>, +) -> Result<(), Error> { + simple_api_command_with_fds(socket, method, c, request_body, Vec::new()) +} diff --git a/vendor/cube-hypervisor/arch/Cargo.toml b/vendor/cube-hypervisor/arch/Cargo.toml new file mode 100644 index 0000000..f1107c4 --- /dev/null +++ b/vendor/cube-hypervisor/arch/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "arch" +version = "0.1.0" +authors = ["The Chromium OS Authors"] +edition = "2021" + +[features] +default = [] +tdx = [] + +[dependencies] +anyhow = "1.0.86" +byteorder = "1.4.3" +hypervisor = { path = "../hypervisor" } +libc = "0.2.137" +linux-loader = { workspace = true, features = ["elf", "bzimage", "pe"] } +log = "0.4.17" +logging = { path = "../logging" } +serde = { workspace = true, features = ["rc", "derive"] } +thiserror = "1.0.37" +uuid = "1.2.1" +vm-memory = { workspace = true, features = ["backend-mmap", "backend-bitmap"] } +vm-migration = { path = "../vm-migration" } +vmm-sys-util = { version = "0.12.1", features = ["with-serde"] } +serde_json = "1.0.87" + +[target.'cfg(target_arch = "aarch64")'.dependencies] +fdt_parser = { version = "0.1.4", package = "fdt" } +vm-fdt = { workspace = true } diff --git a/vendor/cube-hypervisor/arch/src/aarch64/fdt.rs b/vendor/cube-hypervisor/arch/src/aarch64/fdt.rs new file mode 100644 index 0000000..4bc9c57 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/aarch64/fdt.rs @@ -0,0 +1,815 @@ +// Copyright 2020 Arm Limited (or its affiliates). All rights reserved. +// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the THIRD-PARTY file. + +use crate::{NumaNodes, PciSpaceInfo}; +use byteorder::{BigEndian, ByteOrder}; +use hypervisor::arch::aarch64::gic::Vgic; +use std::cmp; +use std::collections::HashMap; +use std::ffi::CStr; +use std::fmt::Debug; +use std::result; +use std::str; +use std::sync::{Arc, Mutex}; + +use super::super::DeviceType; +use super::super::GuestMemoryMmap; +use super::super::InitramfsConfig; +use super::layout::{ + IRQ_BASE, MEM_32BIT_DEVICES_SIZE, MEM_32BIT_DEVICES_START, MEM_PCI_IO_SIZE, MEM_PCI_IO_START, + PCI_HIGH_BASE, PCI_MMIO_CONFIG_SIZE_PER_SEGMENT, +}; +use vm_fdt::{FdtWriter, FdtWriterResult}; +use vm_memory::{Address, Bytes, GuestMemory, GuestMemoryError, GuestMemoryRegion}; + +// This is a value for uniquely identifying the FDT node declaring the interrupt controller. +const GIC_PHANDLE: u32 = 1; +// This is a value for uniquely identifying the FDT node declaring the MSI controller. +const MSI_PHANDLE: u32 = 2; +// This is a value for uniquely identifying the FDT node containing the clock definition. +const CLOCK_PHANDLE: u32 = 3; +// This is a value for uniquely identifying the FDT node containing the gpio controller. +const GPIO_PHANDLE: u32 = 4; +// This is a value for virtio-iommu. Now only one virtio-iommu device is supported. +const VIRTIO_IOMMU_PHANDLE: u32 = 5; +// NOTE: Keep FIRST_VCPU_PHANDLE the last PHANDLE defined. +// This is a value for uniquely identifying the FDT node containing the first vCPU. +// The last number of vCPU phandle depends on the number of vCPUs. +const FIRST_VCPU_PHANDLE: u32 = 6; + +// Read the documentation specified when appending the root node to the FDT. +const ADDRESS_CELLS: u32 = 0x2; +const SIZE_CELLS: u32 = 0x2; + +// As per kvm tool and +// https://www.kernel.org/doc/Documentation/devicetree/bindings/interrupt-controller/arm%2Cgic.txt +// Look for "The 1st cell..." +const GIC_FDT_IRQ_TYPE_SPI: u32 = 0; +const GIC_FDT_IRQ_TYPE_PPI: u32 = 1; + +// From https://elixir.bootlin.com/linux/v4.9.62/source/include/dt-bindings/interrupt-controller/irq.h#L17 +const IRQ_TYPE_EDGE_RISING: u32 = 1; +const IRQ_TYPE_LEVEL_HI: u32 = 4; + +// PMU PPI interrupt number +pub const AARCH64_PMU_IRQ: u32 = 7; + +// Keys and Buttons +// System Power Down +const KEY_POWER: u32 = 116; + +/// Trait for devices to be added to the Flattened Device Tree. +pub trait DeviceInfoForFdt { + /// Returns the address where this device will be loaded. + fn addr(&self) -> u64; + /// Returns the associated interrupt for this device. + fn irq(&self) -> u32; + /// Returns the amount of memory that needs to be reserved for this device. + fn length(&self) -> u64; +} + +/// Errors thrown while configuring the Flattened Device Tree for aarch64. +#[derive(Debug)] +pub enum Error { + /// Failure in writing FDT in memory. + WriteFdtToMemory(GuestMemoryError), +} +type Result = result::Result; + +/// Creates the flattened device tree for this aarch64 VM. +#[allow(clippy::too_many_arguments)] +pub fn create_fdt( + guest_mem: &GuestMemoryMmap, + cmdline: &str, + vcpu_mpidr: Vec, + vcpu_topology: Option<(u8, u8, u8)>, + device_info: &HashMap<(DeviceType, String), T, S>, + gic_device: &Arc>, + initrd: &Option, + pci_space_info: &[PciSpaceInfo], + numa_nodes: &NumaNodes, + virtio_iommu_bdf: Option, + pmu_supported: bool, +) -> FdtWriterResult> { + // Allocate stuff necessary for the holding the blob. + let mut fdt = FdtWriter::new().unwrap(); + + // For an explanation why these nodes were introduced in the blob take a look at + // https://github.com/torvalds/linux/blob/master/Documentation/devicetree/booting-without-of.txt#L845 + // Look for "Required nodes and properties". + + // Header or the root node as per above mentioned documentation. + let root_node = fdt.begin_node("")?; + fdt.property_string("compatible", "linux,dummy-virt")?; + // For info on #address-cells and size-cells read "Note about cells and address representation" + // from the above mentioned txt file. + fdt.property_u32("#address-cells", ADDRESS_CELLS)?; + fdt.property_u32("#size-cells", SIZE_CELLS)?; + // This is not mandatory but we use it to point the root node to the node + // containing description of the interrupt controller for this VM. + fdt.property_u32("interrupt-parent", GIC_PHANDLE)?; + create_cpu_nodes(&mut fdt, &vcpu_mpidr, vcpu_topology, numa_nodes)?; + create_memory_node(&mut fdt, guest_mem, numa_nodes)?; + create_chosen_node(&mut fdt, cmdline, initrd)?; + create_gic_node(&mut fdt, gic_device)?; + create_timer_node(&mut fdt)?; + if pmu_supported { + create_pmu_node(&mut fdt)?; + } + create_clock_node(&mut fdt)?; + create_psci_node(&mut fdt)?; + create_devices_node(&mut fdt, device_info)?; + create_pci_nodes(&mut fdt, pci_space_info, virtio_iommu_bdf)?; + if numa_nodes.len() > 1 { + create_distance_map_node(&mut fdt, numa_nodes)?; + } + + // End Header node. + fdt.end_node(root_node)?; + + let fdt_final = fdt.finish()?; + + Ok(fdt_final) +} + +pub fn write_fdt_to_memory(fdt_final: Vec, guest_mem: &GuestMemoryMmap) -> Result<()> { + // Write FDT to memory. + guest_mem + .write_slice(fdt_final.as_slice(), super::layout::FDT_START) + .map_err(Error::WriteFdtToMemory)?; + Ok(()) +} + +// Following are the auxiliary function for creating the different nodes that we append to our FDT. +fn create_cpu_nodes( + fdt: &mut FdtWriter, + vcpu_mpidr: &[u64], + vcpu_topology: Option<(u8, u8, u8)>, + numa_nodes: &NumaNodes, +) -> FdtWriterResult<()> { + // See https://github.com/torvalds/linux/blob/master/Documentation/devicetree/bindings/arm/cpus.yaml. + let cpus_node = fdt.begin_node("cpus")?; + fdt.property_u32("#address-cells", 0x1)?; + fdt.property_u32("#size-cells", 0x0)?; + + let num_cpus = vcpu_mpidr.len(); + + for (cpu_id, mpidr) in vcpu_mpidr.iter().enumerate().take(num_cpus) { + let cpu_name = format!("cpu@{:x}", cpu_id); + let cpu_node = fdt.begin_node(&cpu_name)?; + fdt.property_string("device_type", "cpu")?; + fdt.property_string("compatible", "arm,arm-v8")?; + if num_cpus > 1 { + // This is required on armv8 64-bit. See aforementioned documentation. + fdt.property_string("enable-method", "psci")?; + } + // Set the field to first 24 bits of the MPIDR - Multiprocessor Affinity Register. + // See http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0488c/BABHBJCI.html. + fdt.property_u32("reg", (mpidr & 0x7FFFFF) as u32)?; + fdt.property_u32("phandle", cpu_id as u32 + FIRST_VCPU_PHANDLE)?; + + // Add `numa-node-id` property if there is any numa config. + if numa_nodes.len() > 1 { + for numa_node_idx in 0..numa_nodes.len() { + let numa_node = numa_nodes.get(&(numa_node_idx as u32)); + if numa_node.unwrap().cpus.contains(&(cpu_id as u8)) { + fdt.property_u32("numa-node-id", numa_node_idx as u32)?; + } + } + } + + fdt.end_node(cpu_node)?; + } + + if let Some(topology) = vcpu_topology { + let (threads_per_core, cores_per_package, packages) = topology; + let cpu_map_node = fdt.begin_node("cpu-map")?; + + // Create device tree nodes with regard of above mapping. + for cluster_idx in 0..packages { + let cluster_name = format!("cluster{:x}", cluster_idx); + let cluster_node = fdt.begin_node(&cluster_name)?; + + for core_idx in 0..cores_per_package { + let core_name = format!("core{:x}", core_idx); + let core_node = fdt.begin_node(&core_name)?; + + for thread_idx in 0..threads_per_core { + let thread_name = format!("thread{:x}", thread_idx); + let thread_node = fdt.begin_node(&thread_name)?; + let cpu_idx = threads_per_core * cores_per_package * cluster_idx + + threads_per_core * core_idx + + thread_idx; + fdt.property_u32("cpu", cpu_idx as u32 + FIRST_VCPU_PHANDLE)?; + fdt.end_node(thread_node)?; + } + + fdt.end_node(core_node)?; + } + fdt.end_node(cluster_node)?; + } + fdt.end_node(cpu_map_node)?; + } else { + debug!("Boot using device tree, CPU topology is not (correctly) specified"); + } + + fdt.end_node(cpus_node)?; + + Ok(()) +} + +fn create_memory_node( + fdt: &mut FdtWriter, + guest_mem: &GuestMemoryMmap, + numa_nodes: &NumaNodes, +) -> FdtWriterResult<()> { + // See https://github.com/torvalds/linux/blob/58ae0b51506802713aa0e9956d1853ba4c722c98/Documentation/devicetree/bindings/numa.txt + // for NUMA setting in memory node. + if numa_nodes.len() > 1 { + for numa_node_idx in 0..numa_nodes.len() { + let numa_node = numa_nodes.get(&(numa_node_idx as u32)); + let mut mem_reg_prop: Vec = Vec::new(); + let mut node_memory_addr: u64 = 0; + // Each memory zone of numa will have its own memory node, but + // different numa nodes should not share same memory zones. + for memory_region in numa_node.unwrap().memory_regions.iter() { + let memory_region_start_addr: u64 = memory_region.start_addr().raw_value(); + let memory_region_size: u64 = memory_region.size() as u64; + mem_reg_prop.push(memory_region_start_addr); + mem_reg_prop.push(memory_region_size); + // Set the node address the first non-zero regison address + if node_memory_addr == 0 { + node_memory_addr = memory_region_start_addr; + } + } + let memory_node_name = format!("memory@{:x}", node_memory_addr); + let memory_node = fdt.begin_node(&memory_node_name)?; + fdt.property_string("device_type", "memory")?; + fdt.property_array_u64("reg", &mem_reg_prop)?; + fdt.property_u32("numa-node-id", numa_node_idx as u32)?; + fdt.end_node(memory_node)?; + } + } else { + let last_addr = guest_mem.last_addr().raw_value(); + if last_addr < super::layout::MEM_32BIT_RESERVED_START.raw_value() { + // Case 1: all RAM is under the hole + let mem_size = last_addr - super::layout::RAM_START.raw_value() + 1; + let mem_reg_prop = [super::layout::RAM_START.raw_value() as u64, mem_size as u64]; + let memory_node = fdt.begin_node("memory")?; + fdt.property_string("device_type", "memory")?; + fdt.property_array_u64("reg", &mem_reg_prop)?; + fdt.end_node(memory_node)?; + } else { + // Case 2: RAM is split by the hole + // Region 1: RAM before the hole + let mem_size = super::layout::MEM_32BIT_RESERVED_START.raw_value() + - super::layout::RAM_START.raw_value(); + let mem_reg_prop = [super::layout::RAM_START.raw_value() as u64, mem_size as u64]; + let memory_node_name = format!("memory@{:x}", super::layout::RAM_START.raw_value()); + let memory_node = fdt.begin_node(&memory_node_name)?; + fdt.property_string("device_type", "memory")?; + fdt.property_array_u64("reg", &mem_reg_prop)?; + fdt.end_node(memory_node)?; + + // Region 2: RAM after the hole + let mem_size = last_addr - super::layout::RAM_64BIT_START.raw_value() + 1; + let mem_reg_prop = [ + super::layout::RAM_64BIT_START.raw_value() as u64, + mem_size as u64, + ]; + let memory_node_name = + format!("memory@{:x}", super::layout::RAM_64BIT_START.raw_value()); + let memory_node = fdt.begin_node(&memory_node_name)?; + fdt.property_string("device_type", "memory")?; + fdt.property_array_u64("reg", &mem_reg_prop)?; + fdt.end_node(memory_node)?; + } + } + + Ok(()) +} + +fn create_chosen_node( + fdt: &mut FdtWriter, + cmdline: &str, + initrd: &Option, +) -> FdtWriterResult<()> { + let chosen_node = fdt.begin_node("chosen")?; + fdt.property_string("bootargs", cmdline)?; + + if let Some(initrd_config) = initrd { + let initrd_start = initrd_config.address.raw_value() as u64; + let initrd_end = initrd_config.address.raw_value() + initrd_config.size as u64; + fdt.property_u64("linux,initrd-start", initrd_start)?; + fdt.property_u64("linux,initrd-end", initrd_end)?; + } + + fdt.end_node(chosen_node)?; + + Ok(()) +} + +fn create_gic_node(fdt: &mut FdtWriter, gic_device: &Arc>) -> FdtWriterResult<()> { + let gic_reg_prop = gic_device.lock().unwrap().device_properties(); + + let intc_node = fdt.begin_node("intc")?; + + fdt.property_string("compatible", gic_device.lock().unwrap().fdt_compatibility())?; + fdt.property_null("interrupt-controller")?; + // "interrupt-cells" field specifies the number of cells needed to encode an + // interrupt source. The type shall be a and the value shall be 3 if no PPI affinity description + // is required. + fdt.property_u32("#interrupt-cells", 3)?; + fdt.property_array_u64("reg", &gic_reg_prop)?; + fdt.property_u32("phandle", GIC_PHANDLE)?; + fdt.property_u32("#address-cells", 2)?; + fdt.property_u32("#size-cells", 2)?; + fdt.property_null("ranges")?; + + let gic_intr_prop = [ + GIC_FDT_IRQ_TYPE_PPI, + gic_device.lock().unwrap().fdt_maint_irq(), + IRQ_TYPE_LEVEL_HI, + ]; + fdt.property_array_u32("interrupts", &gic_intr_prop)?; + + if gic_device.lock().unwrap().msi_compatible() { + let msic_node = fdt.begin_node("msic")?; + fdt.property_string("compatible", gic_device.lock().unwrap().msi_compatibility())?; + fdt.property_null("msi-controller")?; + fdt.property_u32("phandle", MSI_PHANDLE)?; + let msi_reg_prop = gic_device.lock().unwrap().msi_properties(); + fdt.property_array_u64("reg", &msi_reg_prop)?; + fdt.end_node(msic_node)?; + } + + fdt.end_node(intc_node)?; + + Ok(()) +} + +fn create_clock_node(fdt: &mut FdtWriter) -> FdtWriterResult<()> { + // The Advanced Peripheral Bus (APB) is part of the Advanced Microcontroller Bus Architecture + // (AMBA) protocol family. It defines a low-cost interface that is optimized for minimal power + // consumption and reduced interface complexity. + // PCLK is the clock source and this node defines exactly the clock for the APB. + let clock_node = fdt.begin_node("apb-pclk")?; + fdt.property_string("compatible", "fixed-clock")?; + fdt.property_u32("#clock-cells", 0x0)?; + fdt.property_u32("clock-frequency", 24000000)?; + fdt.property_string("clock-output-names", "clk24mhz")?; + fdt.property_u32("phandle", CLOCK_PHANDLE)?; + fdt.end_node(clock_node)?; + + Ok(()) +} + +fn create_timer_node(fdt: &mut FdtWriter) -> FdtWriterResult<()> { + // See + // https://github.com/torvalds/linux/blob/master/Documentation/devicetree/bindings/interrupt-controller/arch_timer.txt + // These are fixed interrupt numbers for the timer device. + let irqs = [13, 14, 11, 10]; + let compatible = "arm,armv8-timer"; + + let mut timer_reg_cells: Vec = Vec::new(); + for &irq in irqs.iter() { + timer_reg_cells.push(GIC_FDT_IRQ_TYPE_PPI); + timer_reg_cells.push(irq); + timer_reg_cells.push(IRQ_TYPE_LEVEL_HI); + } + + let timer_node = fdt.begin_node("timer")?; + fdt.property_string("compatible", compatible)?; + fdt.property_null("always-on")?; + fdt.property_array_u32("interrupts", &timer_reg_cells)?; + fdt.end_node(timer_node)?; + + Ok(()) +} + +fn create_psci_node(fdt: &mut FdtWriter) -> FdtWriterResult<()> { + let compatible = "arm,psci-0.2"; + let psci_node = fdt.begin_node("psci")?; + fdt.property_string("compatible", compatible)?; + // Two methods available: hvc and smc. + // As per documentation, PSCI calls between a guest and hypervisor may use the HVC conduit instead of SMC. + // So, since we are using kvm, we need to use hvc. + fdt.property_string("method", "hvc")?; + fdt.end_node(psci_node)?; + + Ok(()) +} + +fn create_virtio_node( + fdt: &mut FdtWriter, + dev_info: &T, +) -> FdtWriterResult<()> { + let device_reg_prop = [dev_info.addr(), dev_info.length()]; + let irq = [GIC_FDT_IRQ_TYPE_SPI, dev_info.irq(), IRQ_TYPE_EDGE_RISING]; + + let virtio_node = fdt.begin_node(&format!("virtio_mmio@{:x}", dev_info.addr()))?; + fdt.property_string("compatible", "virtio,mmio")?; + fdt.property_array_u64("reg", &device_reg_prop)?; + fdt.property_array_u32("interrupts", &irq)?; + fdt.property_u32("interrupt-parent", GIC_PHANDLE)?; + fdt.end_node(virtio_node)?; + + Ok(()) +} + +fn create_serial_node( + fdt: &mut FdtWriter, + dev_info: &T, +) -> FdtWriterResult<()> { + let compatible = b"arm,pl011\0arm,primecell\0"; + let serial_reg_prop = [dev_info.addr(), dev_info.length()]; + let irq = [ + GIC_FDT_IRQ_TYPE_SPI, + dev_info.irq() - IRQ_BASE, + IRQ_TYPE_EDGE_RISING, + ]; + + let serial_node = fdt.begin_node(&format!("pl011@{:x}", dev_info.addr()))?; + fdt.property("compatible", compatible)?; + fdt.property_array_u64("reg", &serial_reg_prop)?; + fdt.property_u32("clocks", CLOCK_PHANDLE)?; + fdt.property_string("clock-names", "apb_pclk")?; + fdt.property_array_u32("interrupts", &irq)?; + fdt.end_node(serial_node)?; + + Ok(()) +} + +fn create_rtc_node( + fdt: &mut FdtWriter, + dev_info: &T, +) -> FdtWriterResult<()> { + let compatible = b"arm,pl031\0arm,primecell\0"; + let rtc_reg_prop = [dev_info.addr(), dev_info.length()]; + let irq = [ + GIC_FDT_IRQ_TYPE_SPI, + dev_info.irq() - IRQ_BASE, + IRQ_TYPE_LEVEL_HI, + ]; + + let rtc_node = fdt.begin_node(&format!("rtc@{:x}", dev_info.addr()))?; + fdt.property("compatible", compatible)?; + fdt.property_array_u64("reg", &rtc_reg_prop)?; + fdt.property_array_u32("interrupts", &irq)?; + fdt.property_u32("clocks", CLOCK_PHANDLE)?; + fdt.property_string("clock-names", "apb_pclk")?; + fdt.end_node(rtc_node)?; + + Ok(()) +} + +fn create_gpio_node( + fdt: &mut FdtWriter, + dev_info: &T, +) -> FdtWriterResult<()> { + // PL061 GPIO controller node + let compatible = b"arm,pl061\0arm,primecell\0"; + let gpio_reg_prop = [dev_info.addr(), dev_info.length()]; + let irq = [ + GIC_FDT_IRQ_TYPE_SPI, + dev_info.irq() - IRQ_BASE, + IRQ_TYPE_EDGE_RISING, + ]; + + let gpio_node = fdt.begin_node(&format!("pl061@{:x}", dev_info.addr()))?; + fdt.property("compatible", compatible)?; + fdt.property_array_u64("reg", &gpio_reg_prop)?; + fdt.property_array_u32("interrupts", &irq)?; + fdt.property_null("gpio-controller")?; + fdt.property_u32("#gpio-cells", 2)?; + fdt.property_u32("clocks", CLOCK_PHANDLE)?; + fdt.property_string("clock-names", "apb_pclk")?; + fdt.property_u32("phandle", GPIO_PHANDLE)?; + fdt.end_node(gpio_node)?; + + // gpio-keys node + let gpio_keys_node = fdt.begin_node("gpio-keys")?; + fdt.property_string("compatible", "gpio-keys")?; + fdt.property_u32("#size-cells", 0)?; + fdt.property_u32("#address-cells", 1)?; + let gpio_keys_poweroff_node = fdt.begin_node("button@1")?; + fdt.property_string("label", "GPIO Key Poweroff")?; + fdt.property_u32("linux,code", KEY_POWER)?; + let gpios = [GPIO_PHANDLE, 3, 0]; + fdt.property_array_u32("gpios", &gpios)?; + fdt.end_node(gpio_keys_poweroff_node)?; + fdt.end_node(gpio_keys_node)?; + + Ok(()) +} + +fn create_devices_node( + fdt: &mut FdtWriter, + dev_info: &HashMap<(DeviceType, String), T, S>, +) -> FdtWriterResult<()> { + // Create one temp Vec to store all virtio devices + let mut ordered_virtio_device: Vec<&T> = Vec::new(); + + for ((device_type, _device_id), info) in dev_info { + match device_type { + DeviceType::Gpio => create_gpio_node(fdt, info)?, + DeviceType::Rtc => create_rtc_node(fdt, info)?, + DeviceType::Serial => create_serial_node(fdt, info)?, + DeviceType::Virtio(_) => { + ordered_virtio_device.push(info); + } + } + } + + // Sort out virtio devices by address from low to high and insert them into fdt table. + ordered_virtio_device.sort_by_key(|&a| a.addr()); + // Current address allocation strategy in cloud-hypervisor is: the first created device + // will be allocated to higher address. Here we reverse the vector to make sure that + // the older created device will appear in front of the newer created device in FDT. + ordered_virtio_device.reverse(); + for ordered_device_info in ordered_virtio_device.drain(..) { + create_virtio_node(fdt, ordered_device_info)?; + } + + Ok(()) +} + +fn create_pmu_node(fdt: &mut FdtWriter) -> FdtWriterResult<()> { + let compatible = "arm,armv8-pmuv3"; + let irq = [GIC_FDT_IRQ_TYPE_PPI, AARCH64_PMU_IRQ, IRQ_TYPE_LEVEL_HI]; + + let pmu_node = fdt.begin_node("pmu")?; + fdt.property_string("compatible", compatible)?; + fdt.property_array_u32("interrupts", &irq)?; + fdt.end_node(pmu_node)?; + Ok(()) +} + +fn create_pci_nodes( + fdt: &mut FdtWriter, + pci_device_info: &[PciSpaceInfo], + virtio_iommu_bdf: Option, +) -> FdtWriterResult<()> { + // Add node for PCIe controller. + // See Documentation/devicetree/bindings/pci/host-generic-pci.txt in the kernel + // and https://elinux.org/Device_Tree_Usage. + // In multiple PCI segments setup, each PCI segment needs a PCI node. + for pci_device_info_elem in pci_device_info.iter() { + // EDK2 requires the PCIe high space above 4G address. + // The actual space in CLH follows the RAM. If the RAM space is small, the PCIe high space + // could fall bellow 4G. + // Here we cut off PCI device space below 8G in FDT to workaround the EDK2 check. + // But the address written in ACPI is not impacted. + let (pci_device_base_64bit, pci_device_size_64bit) = + if pci_device_info_elem.pci_device_space_start < PCI_HIGH_BASE.raw_value() { + ( + PCI_HIGH_BASE.raw_value(), + pci_device_info_elem.pci_device_space_size + - (PCI_HIGH_BASE.raw_value() - pci_device_info_elem.pci_device_space_start), + ) + } else { + ( + pci_device_info_elem.pci_device_space_start, + pci_device_info_elem.pci_device_space_size, + ) + }; + // There is no specific requirement of the 32bit MMIO range, and + // therefore at least we can make these ranges 4K aligned. + let pci_device_size_32bit: u64 = + MEM_32BIT_DEVICES_SIZE / ((1 << 12) * pci_device_info.len() as u64) * (1 << 12); + let pci_device_base_32bit: u64 = MEM_32BIT_DEVICES_START.0 + + pci_device_size_32bit * pci_device_info_elem.pci_segment_id as u64; + + let ranges = [ + // io addresses. Since AArch64 will not use IO address, + // we can set the same IO address range for every segment. + 0x1000000, + 0_u32, + 0_u32, + (MEM_PCI_IO_START.0 >> 32) as u32, + MEM_PCI_IO_START.0 as u32, + (MEM_PCI_IO_SIZE >> 32) as u32, + MEM_PCI_IO_SIZE as u32, + // mmio addresses + 0x2000000, // (ss = 10: 32-bit memory space) + (pci_device_base_32bit >> 32) as u32, // PCI address + pci_device_base_32bit as u32, + (pci_device_base_32bit >> 32) as u32, // CPU address + pci_device_base_32bit as u32, + (pci_device_size_32bit >> 32) as u32, // size + pci_device_size_32bit as u32, + // device addresses + 0x3000000, // (ss = 11: 64-bit memory space) + (pci_device_base_64bit >> 32) as u32, // PCI address + pci_device_base_64bit as u32, + (pci_device_base_64bit >> 32) as u32, // CPU address + pci_device_base_64bit as u32, + (pci_device_size_64bit >> 32) as u32, // size + pci_device_size_64bit as u32, + ]; + let bus_range = [0, 0]; // Only bus 0 + let reg = [ + pci_device_info_elem.mmio_config_address, + PCI_MMIO_CONFIG_SIZE_PER_SEGMENT, + ]; + // See kernel document Documentation/devicetree/bindings/pci/pci-msi.txt + let msi_map = [ + // rid-base: A single cell describing the first RID matched by the entry. + 0x0, + // msi-controller: A single phandle to an MSI controller. + MSI_PHANDLE, + // msi-base: An msi-specifier describing the msi-specifier produced for the + // first RID matched by the entry. + (pci_device_info_elem.pci_segment_id as u32) << 8, + // length: A single cell describing how many consecutive RIDs are matched + // following the rid-base. + 0x100, + ]; + + let pci_node_name = format!("pci@{:x}", pci_device_info_elem.mmio_config_address); + let pci_node = fdt.begin_node(&pci_node_name)?; + + fdt.property_string("compatible", "pci-host-ecam-generic")?; + fdt.property_string("device_type", "pci")?; + fdt.property_array_u32("ranges", &ranges)?; + fdt.property_array_u32("bus-range", &bus_range)?; + fdt.property_u32( + "linux,pci-domain", + pci_device_info_elem.pci_segment_id as u32, + )?; + fdt.property_u32("#address-cells", 3)?; + fdt.property_u32("#size-cells", 2)?; + fdt.property_array_u64("reg", ®)?; + fdt.property_u32("#interrupt-cells", 1)?; + fdt.property_null("interrupt-map")?; + fdt.property_null("interrupt-map-mask")?; + fdt.property_null("dma-coherent")?; + fdt.property_array_u32("msi-map", &msi_map)?; + fdt.property_u32("msi-parent", MSI_PHANDLE)?; + + if pci_device_info_elem.pci_segment_id == 0 { + if let Some(virtio_iommu_bdf) = virtio_iommu_bdf { + // See kernel document Documentation/devicetree/bindings/pci/pci-iommu.txt + // for 'iommu-map' attribute setting. + let iommu_map = [ + 0_u32, + VIRTIO_IOMMU_PHANDLE, + 0_u32, + virtio_iommu_bdf, + virtio_iommu_bdf + 1, + VIRTIO_IOMMU_PHANDLE, + virtio_iommu_bdf + 1, + 0xffff - virtio_iommu_bdf, + ]; + fdt.property_array_u32("iommu-map", &iommu_map)?; + + // See kernel document Documentation/devicetree/bindings/virtio/iommu.txt + // for virtio-iommu node settings. + let virtio_iommu_node_name = format!("virtio_iommu@{:x}", virtio_iommu_bdf); + let virtio_iommu_node = fdt.begin_node(&virtio_iommu_node_name)?; + fdt.property_u32("#iommu-cells", 1)?; + fdt.property_string("compatible", "virtio,pci-iommu")?; + + // 'reg' is a five-cell address encoded as + // (phys.hi phys.mid phys.lo size.hi size.lo). phys.hi should contain the + // device's BDF as 0b00000000 bbbbbbbb dddddfff 00000000. The other cells + // should be zero. + let reg = [virtio_iommu_bdf << 8, 0_u32, 0_u32, 0_u32, 0_u32]; + fdt.property_array_u32("reg", ®)?; + fdt.property_u32("phandle", VIRTIO_IOMMU_PHANDLE)?; + + fdt.end_node(virtio_iommu_node)?; + } + } + + fdt.end_node(pci_node)?; + } + + Ok(()) +} + +fn create_distance_map_node(fdt: &mut FdtWriter, numa_nodes: &NumaNodes) -> FdtWriterResult<()> { + let distance_map_node = fdt.begin_node("distance-map")?; + fdt.property_string("compatible", "numa-distance-map-v1")?; + // Construct the distance matrix. + // 1. We use the word entry to describe a distance from a node to + // its destination, e.g. 0 -> 1 = 20 is described as <0 1 20>. + // 2. Each entry represents distance from first node to second node. + // The distances are equal in either direction. + // 3. The distance from a node to self (local distance) is represented + // with value 10 and all internode distance should be represented with + // a value greater than 10. + // 4. distance-matrix should have entries in lexicographical ascending + // order of nodes. + let mut distance_matrix = Vec::new(); + for numa_node_idx in 0..numa_nodes.len() { + let numa_node = numa_nodes.get(&(numa_node_idx as u32)); + for dest_numa_node in 0..numa_node.unwrap().distances.len() + 1 { + if numa_node_idx == dest_numa_node { + distance_matrix.push(numa_node_idx as u32); + distance_matrix.push(dest_numa_node as u32); + distance_matrix.push(10_u32); + continue; + } + + distance_matrix.push(numa_node_idx as u32); + distance_matrix.push(dest_numa_node as u32); + distance_matrix.push( + *numa_node + .unwrap() + .distances + .get(&(dest_numa_node as u32)) + .unwrap() as u32, + ); + } + } + fdt.property_array_u32("distance-matrix", distance_matrix.as_ref())?; + fdt.end_node(distance_map_node)?; + + Ok(()) +} + +// Parse the DTB binary and print for debugging +pub fn print_fdt(dtb: &[u8]) { + match fdt_parser::Fdt::new(dtb) { + Ok(fdt) => { + if let Some(root) = fdt.find_node("/") { + debug!("Printing the FDT:"); + print_node(root, 0); + } else { + debug!("Failed to find root node in FDT for debugging."); + } + } + Err(_) => debug!("Failed to parse FDT for debugging."), + } +} + +fn print_node(node: fdt_parser::node::FdtNode<'_, '_>, n_spaces: usize) { + debug!("{:indent$}{}/", "", node.name, indent = n_spaces); + for property in node.properties() { + let name = property.name; + + // If the property is 'compatible', its value requires special handling. + // The u8 array could contain multiple null-terminated strings. + // We copy the original array and simply replace all 'null' characters with spaces. + let value = if name == "compatible" { + let mut compatible = vec![0u8; 256]; + let handled_value = property + .value + .iter() + .map(|&c| if c == 0 { b' ' } else { c }) + .collect::>(); + let len = cmp::min(255, handled_value.len()); + compatible[..len].copy_from_slice(&handled_value[..len]); + compatible[..(len + 1)].to_vec() + } else { + property.value.to_vec() + }; + let value = &value; + + // Now the value can be either: + // - A null-terminated C string, or + // - Binary data + // We follow a very simple logic to present the value: + // - At first, try to convert it to CStr and print, + // - If failed, print it as u32 array. + let value_result = match CStr::from_bytes_with_nul(value) { + Ok(value_cstr) => match value_cstr.to_str() { + Ok(value_str) => Some(value_str), + Err(_e) => None, + }, + Err(_e) => None, + }; + + if let Some(value_str) = value_result { + debug!( + "{:indent$}{} : {:#?}", + "", + name, + value_str, + indent = (n_spaces + 2) + ); + } else { + let mut array = Vec::with_capacity(256); + array.resize(value.len() / 4, 0u32); + BigEndian::read_u32_into(value, &mut array); + debug!( + "{:indent$}{} : {:X?}", + "", + name, + array, + indent = (n_spaces + 2) + ); + }; + } + + // Print children nodes if there is any + for child in node.children() { + print_node(child, n_spaces + 2); + } +} diff --git a/vendor/cube-hypervisor/arch/src/aarch64/layout.rs b/vendor/cube-hypervisor/arch/src/aarch64/layout.rs new file mode 100644 index 0000000..1b834a5 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/aarch64/layout.rs @@ -0,0 +1,140 @@ +// Copyright 2020 Arm Limited (or its affiliates). All rights reserved. +// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// +// Memory layout of AArch64 guest: +// +// Physical +---------------------------------------------------------------+ +// address | | +// end | | +// ~ ~ ~ ~ +// | | +// | Highmem PCI MMIO space | +// | | +// RAM end +---------------------------------------------------------------+ +// (dynamic, | | +// including | | +// hotplug ~ ~ ~ ~ +// memory) | | +// | DRAM | +// | | +// | | +// 4GB +---------------------------------------------------------------+ +// | 32-bit devices hole | +// 4GB-64M +---------------------------------------------------------------+ +// | | +// | | +// | DRAM | +// | | +// | | +// 1GB +---------------------------------------------------------------+ +// | | +// | PCI MMCONFIG space | +// | | +// 768 M +---------------------------------------------------------------+ +// | | +// | | +// | PCI MMIO space | +// | | +// 256 M +---------------------------------------------------------------| +// | | +// | Legacy devices space | +// | | +// 144 M +---------------------------------------------------------------| +// | | +// | Reserved (now GIC is here) | +// | | +// 4 M +---------------------------------------------------------------+ +// | UEFI flash | +// 0GB +---------------------------------------------------------------+ +// +// + +use vm_memory::GuestAddress; + +/// 0x0 ~ 0x40_0000 (4 MiB) is reserved to UEFI +/// UEFI binary size is required less than 3 MiB, reserving 4 MiB is enough. +pub const UEFI_START: GuestAddress = GuestAddress(0); +pub const UEFI_SIZE: u64 = 0x040_0000; + +/// Below this address will reside the GIC, above this address will reside the MMIO devices. +const MAPPED_IO_START: GuestAddress = GuestAddress(0x0900_0000); + +/// See kernel file arch/arm64/include/uapi/asm/kvm.h for the GIC related definitions. +/// 0x08ff_0000 ~ 0x0900_0000 is reserved for GICv3 Distributor +pub const GIC_V3_DIST_SIZE: u64 = 0x01_0000; +pub const GIC_V3_DIST_START: GuestAddress = GuestAddress(MAPPED_IO_START.0 - GIC_V3_DIST_SIZE); +/// Below 0x08ff_0000 is reserved for GICv3 Redistributor. +/// The size defined here is for each vcpu. +/// The total size is 'number_of_vcpu * GIC_V3_REDIST_SIZE' +pub const GIC_V3_REDIST_SIZE: u64 = 0x02_0000; +/// Below Redistributor area is GICv3 ITS +pub const GIC_V3_ITS_SIZE: u64 = 0x02_0000; + +/// Space 0x0900_0000 ~ 0x0905_0000 is reserved for legacy devices. +pub const LEGACY_SERIAL_MAPPED_IO_START: GuestAddress = MAPPED_IO_START; +pub const LEGACY_RTC_MAPPED_IO_START: GuestAddress = GuestAddress(0x0901_0000); +pub const LEGACY_GPIO_MAPPED_IO_START: GuestAddress = GuestAddress(0x0902_0000); +pub const LEGACY_SYS_CTRL_MAPPED_IO_START: GuestAddress = GuestAddress(0x0903_0000); + +/// Space 0x0905_0000 ~ 0x0906_0000 is reserved for pcie io address +pub const MEM_PCI_IO_START: GuestAddress = GuestAddress(0x0905_0000); +pub const MEM_PCI_IO_SIZE: u64 = 0x10000; + +/// Starting from 0x1000_0000 (256MiB) to 0x3000_0000 (768MiB) is used for PCIE MMIO +pub const MEM_32BIT_DEVICES_START: GuestAddress = GuestAddress(0x1000_0000); +pub const MEM_32BIT_DEVICES_SIZE: u64 = 0x2000_0000; + +/// PCI MMCONFIG space (start: after the device space at 1 GiB, length: 256MiB) +pub const PCI_MMCONFIG_START: GuestAddress = GuestAddress(0x3000_0000); +pub const PCI_MMCONFIG_SIZE: u64 = 256 << 20; +// One bus with potentially 256 devices (32 slots x 8 functions). +pub const PCI_MMIO_CONFIG_SIZE_PER_SEGMENT: u64 = 4096 * 256; + +/// Start of RAM. +pub const RAM_START: GuestAddress = GuestAddress(0x4000_0000); + +/// 32-bit reserved area: 64MiB before 4GiB +pub const MEM_32BIT_RESERVED_START: GuestAddress = GuestAddress(0xfc00_0000); +pub const MEM_32BIT_RESERVED_SIZE: u64 = 0x0400_0000; + +/// TPM Address Range +/// This Address range is specific to CRB Interface +pub const TPM_START: GuestAddress = GuestAddress(0xfed4_0000); +pub const TPM_SIZE: u64 = 0x1000; + +/// Start of 64-bit RAM. +pub const RAM_64BIT_START: GuestAddress = GuestAddress(0x1_0000_0000); + +/// Kernel command line maximum size. +/// As per `arch/arm64/include/uapi/asm/setup.h`. +pub const CMDLINE_MAX_SIZE: usize = 2048; + +/// FDT is at the beginning of RAM. +/// Maximum size of the device tree blob as specified in https://www.kernel.org/doc/Documentation/arm64/booting.txt. +pub const FDT_START: GuestAddress = RAM_START; +pub const FDT_MAX_SIZE: u64 = 0x20_0000; + +/// Put ACPI table above dtb +pub const ACPI_START: GuestAddress = GuestAddress(RAM_START.0 + FDT_MAX_SIZE); +pub const ACPI_MAX_SIZE: u64 = 0x20_0000; +pub const RSDP_POINTER: GuestAddress = ACPI_START; + +/// Kernel start after FDT and ACPI +pub const KERNEL_START: GuestAddress = GuestAddress(ACPI_START.0 + ACPI_MAX_SIZE); + +/// Pci high memory base +pub const PCI_HIGH_BASE: GuestAddress = GuestAddress(0x2_0000_0000); + +// As per virt/kvm/arm/vgic/vgic-kvm-device.c we need +// the number of interrupts our GIC will support to be: +// * bigger than 32 +// * less than 1023 and +// * a multiple of 32. +// We are setting up our interrupt controller to support a maximum of 256 interrupts. +/// First usable interrupt on aarch64 +pub const IRQ_BASE: u32 = 32; + +/// Number of supported interrupts +pub const IRQ_NUM: u32 = 256; diff --git a/vendor/cube-hypervisor/arch/src/aarch64/mod.rs b/vendor/cube-hypervisor/arch/src/aarch64/mod.rs new file mode 100644 index 0000000..f160f98 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/aarch64/mod.rs @@ -0,0 +1,240 @@ +// Copyright 2020 Arm Limited (or its affiliates). All rights reserved. +// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +/// Module for the flattened device tree. +pub mod fdt; +/// Layout for this aarch64 system. +pub mod layout; +/// Module for system registers definition +pub mod regs; +/// Module for loading UEFI binary. +pub mod uefi; + +pub use self::fdt::DeviceInfoForFdt; +use crate::{DeviceType, GuestMemoryMmap, NumaNodes, PciSpaceInfo, RegionType}; +use hypervisor::arch::aarch64::gic::Vgic; +use log::{log_enabled, Level}; +use std::collections::HashMap; +use std::convert::TryInto; +use std::fmt::Debug; +use std::sync::{Arc, Mutex}; +use vm_memory::{Address, GuestAddress, GuestMemory, GuestUsize}; + +/// Errors thrown while configuring aarch64 system. +#[derive(Debug)] +pub enum Error { + /// Failed to create a FDT. + SetupFdt, + + /// Failed to write FDT to memory. + WriteFdtToMemory(fdt::Error), + + /// Failed to create a GIC. + SetupGic, + + /// Failed to compute the initramfs address. + InitramfsAddress, + + /// Error configuring the general purpose registers + RegsConfiguration(hypervisor::HypervisorCpuError), + + /// Error configuring the MPIDR register + VcpuRegMpidr(hypervisor::HypervisorCpuError), + + /// Error initializing PMU for vcpu + VcpuInitPmu, +} + +impl From for super::Error { + fn from(e: Error) -> super::Error { + super::Error::PlatformSpecific(e) + } +} + +#[derive(Debug, Copy, Clone)] +/// Specifies the entry point address where the guest must start +/// executing code. +pub struct EntryPoint { + /// Address in guest memory where the guest must start execution + pub entry_addr: GuestAddress, +} + +/// Configure the specified VCPU, and return its MPIDR. +pub fn configure_vcpu( + vcpu: &Arc, + id: u8, + kernel_entry_point: Option, +) -> super::Result { + if let Some(kernel_entry_point) = kernel_entry_point { + vcpu.setup_regs( + id, + kernel_entry_point.entry_addr.raw_value(), + super::layout::FDT_START.raw_value(), + ) + .map_err(Error::RegsConfiguration)?; + } + + let mpidr = vcpu + .get_sys_reg(regs::MPIDR_EL1) + .map_err(Error::VcpuRegMpidr)?; + Ok(mpidr) +} + +pub fn arch_memory_regions(size: GuestUsize) -> Vec<(GuestAddress, usize, RegionType)> { + let mut regions = vec![ + // 0 MiB ~ 256 MiB: UEFI, GIC and legacy devices + ( + GuestAddress(0), + layout::MEM_32BIT_DEVICES_START.0 as usize, + RegionType::Reserved, + ), + // 256 MiB ~ 768 MiB: MMIO space + ( + layout::MEM_32BIT_DEVICES_START, + layout::MEM_32BIT_DEVICES_SIZE as usize, + RegionType::SubRegion, + ), + // 768 MiB ~ 1 GiB: reserved. The leading 256M for PCIe MMCONFIG space + ( + layout::PCI_MMCONFIG_START, + layout::PCI_MMCONFIG_SIZE as usize, + RegionType::Reserved, + ), + ]; + + let ram_32bit_space_size = + layout::MEM_32BIT_RESERVED_START.unchecked_offset_from(layout::RAM_START); + + // RAM space + // Case1: guest memory fits before the gap + if size as u64 <= ram_32bit_space_size { + regions.push((layout::RAM_START, size as usize, RegionType::Ram)); + // Case2: guest memory extends beyond the gap + } else { + // Push memory before the gap + regions.push(( + layout::RAM_START, + ram_32bit_space_size as usize, + RegionType::Ram, + )); + // Other memory is placed after 4GiB + regions.push(( + layout::RAM_64BIT_START, + (size - ram_32bit_space_size) as usize, + RegionType::Ram, + )); + } + + // Add the 32-bit reserved memory hole as a reserved region + regions.push(( + layout::MEM_32BIT_RESERVED_START, + layout::MEM_32BIT_RESERVED_SIZE as usize, + RegionType::Reserved, + )); + + regions +} + +/// Configures the system and should be called once per vm before starting vcpu threads. +#[allow(clippy::too_many_arguments)] +pub fn configure_system( + guest_mem: &GuestMemoryMmap, + cmdline: &str, + vcpu_mpidr: Vec, + vcpu_topology: Option<(u8, u8, u8)>, + device_info: &HashMap<(DeviceType, String), T, S>, + initrd: &Option, + pci_space_info: &[PciSpaceInfo], + virtio_iommu_bdf: Option, + gic_device: &Arc>, + numa_nodes: &NumaNodes, + pmu_supported: bool, +) -> super::Result<()> { + let fdt_final = fdt::create_fdt( + guest_mem, + cmdline, + vcpu_mpidr, + vcpu_topology, + device_info, + gic_device, + initrd, + pci_space_info, + numa_nodes, + virtio_iommu_bdf, + pmu_supported, + ) + .map_err(|_| Error::SetupFdt)?; + + if log_enabled!(Level::Debug) { + fdt::print_fdt(&fdt_final); + } + + fdt::write_fdt_to_memory(fdt_final, guest_mem).map_err(Error::WriteFdtToMemory)?; + + Ok(()) +} + +/// Returns the memory address where the initramfs could be loaded. +pub fn initramfs_load_addr( + guest_mem: &GuestMemoryMmap, + initramfs_size: usize, +) -> super::Result { + let round_to_pagesize = |size| (size + (super::PAGE_SIZE - 1)) & !(super::PAGE_SIZE - 1); + match guest_mem + .last_addr() + .checked_sub(round_to_pagesize(initramfs_size) as u64 - 1) + { + Some(offset) => { + if guest_mem.address_in_range(offset) { + Ok(offset.raw_value()) + } else { + Err(super::Error::PlatformSpecific(Error::InitramfsAddress)) + } + } + None => Err(super::Error::PlatformSpecific(Error::InitramfsAddress)), + } +} + +pub fn get_host_cpu_phys_bits() -> u8 { + // A dummy hypervisor created only for querying the host IPA size and will + // be freed after the query. + let hv = hypervisor::new().unwrap(); + let host_cpu_phys_bits = hv.get_host_ipa_limit().try_into().unwrap(); + if host_cpu_phys_bits == 0 { + // Host kernel does not support `get_host_ipa_limit`, + // we return the default value 40 here. + 40 + } else { + host_cpu_phys_bits + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_arch_memory_regions_dram_2gb() { + let regions = arch_memory_regions((1usize << 31) as u64); //2GB + assert_eq!(5, regions.len()); + assert_eq!(layout::RAM_START, regions[3].0); + assert_eq!((1usize << 31), regions[3].1); + assert_eq!(RegionType::Ram, regions[3].2); + assert_eq!(RegionType::Reserved, regions[4].2); + } + + #[test] + fn test_arch_memory_regions_dram_4gb() { + let regions = arch_memory_regions((1usize << 32) as u64); //4GB + let ram_32bit_space_size = + layout::MEM_32BIT_RESERVED_START.unchecked_offset_from(layout::RAM_START) as usize; + assert_eq!(6, regions.len()); + assert_eq!(layout::RAM_START, regions[3].0); + assert_eq!(ram_32bit_space_size as usize, regions[3].1); + assert_eq!(RegionType::Ram, regions[3].2); + assert_eq!(RegionType::Reserved, regions[5].2); + assert_eq!(RegionType::Ram, regions[4].2); + assert_eq!(((1usize << 32) - ram_32bit_space_size), regions[4].1); + } +} diff --git a/vendor/cube-hypervisor/arch/src/aarch64/regs.rs b/vendor/cube-hypervisor/arch/src/aarch64/regs.rs new file mode 100644 index 0000000..b76a780 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/aarch64/regs.rs @@ -0,0 +1,44 @@ +// Copyright 2022 Arm Limited (or its affiliates). All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +/// +/// AArch64 system register encoding: +/// See https://developer.arm.com/documentation/ddi0487 (chapter D12) +/// +/// 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0 +/// +----------+---+-----+-----+-----+-----+-----+----+ +/// |1101010100| L | op0 | op1 | CRn | CRm | op2 | Rt | +/// +----------+---+-----+-----+-----+-----+-----+----+ +/// +/// Notes: +/// - L and Rt are reserved as implementation defined fields, ignored. +/// +const SYSREG_HEAD: u32 = 0b1101010100u32 << 22; +const SYSREG_OP0_SHIFT: u32 = 19; +const SYSREG_OP0_MASK: u32 = 0b11u32 << 19; +const SYSREG_OP1_SHIFT: u32 = 16; +const SYSREG_OP1_MASK: u32 = 0b111u32 << 16; +const SYSREG_CRN_SHIFT: u32 = 12; +const SYSREG_CRN_MASK: u32 = 0b1111u32 << 12; +const SYSREG_CRM_SHIFT: u32 = 8; +const SYSREG_CRM_MASK: u32 = 0b1111u32 << 8; +const SYSREG_OP2_SHIFT: u32 = 5; +const SYSREG_OP2_MASK: u32 = 0b111u32 << 5; + +/// Define the ID of system registers +#[macro_export] +macro_rules! arm64_sys_reg { + ($name: tt, $op0: tt, $op1: tt, $crn: tt, $crm: tt, $op2: tt) => { + pub const $name: u32 = SYSREG_HEAD + | ((($op0 as u32) << SYSREG_OP0_SHIFT) & SYSREG_OP0_MASK as u32) + | ((($op1 as u32) << SYSREG_OP1_SHIFT) & SYSREG_OP1_MASK as u32) + | ((($crn as u32) << SYSREG_CRN_SHIFT) & SYSREG_CRN_MASK as u32) + | ((($crm as u32) << SYSREG_CRM_SHIFT) & SYSREG_CRM_MASK as u32) + | ((($op2 as u32) << SYSREG_OP2_SHIFT) & SYSREG_OP2_MASK as u32); + }; +} + +arm64_sys_reg!(MPIDR_EL1, 3, 0, 0, 0, 5); +arm64_sys_reg!(ID_AA64MMFR0_EL1, 3, 0, 0, 7, 0); +arm64_sys_reg!(TTBR1_EL1, 3, 0, 2, 0, 1); +arm64_sys_reg!(TCR_EL1, 3, 0, 2, 0, 2); diff --git a/vendor/cube-hypervisor/arch/src/aarch64/uefi.rs b/vendor/cube-hypervisor/arch/src/aarch64/uefi.rs new file mode 100644 index 0000000..789f1cf --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/aarch64/uefi.rs @@ -0,0 +1,44 @@ +// Copyright 2020 Arm Limited (or its affiliates). All rights reserved. + +use std::io::{Read, Seek, SeekFrom}; +use std::os::fd::AsFd; +use std::result; +use vm_memory::{GuestAddress, GuestMemory}; + +/// Errors thrown while loading UEFI binary +#[derive(Debug)] +pub enum Error { + /// Unable to seek to UEFI image start. + SeekUefiStart, + /// Unable to seek to UEFI image end. + SeekUefiEnd, + /// UEFI image too big. + UefiTooBig, + /// Unable to read UEFI image + ReadUefiImage, +} +type Result = result::Result; + +pub fn load_uefi( + guest_mem: &M, + guest_addr: GuestAddress, + uefi_image: &mut F, +) -> Result<()> +where + F: Read + Seek + AsFd, +{ + let uefi_size = uefi_image + .seek(SeekFrom::End(0)) + .map_err(|_| Error::SeekUefiEnd)? as usize; + + // edk2 image on virtual platform is smaller than 3M + if uefi_size > 0x300000 { + return Err(Error::UefiTooBig); + } + uefi_image + .seek(SeekFrom::Start(0)) + .map_err(|_| Error::SeekUefiStart)?; + guest_mem + .read_exact_volatile_from(guest_addr, &mut uefi_image.as_fd(), uefi_size) + .map_err(|_| Error::ReadUefiImage) +} diff --git a/vendor/cube-hypervisor/arch/src/lib.rs b/vendor/cube-hypervisor/arch/src/lib.rs new file mode 100644 index 0000000..de7ebed --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/lib.rs @@ -0,0 +1,177 @@ +// Copyright 2020 Arm Limited (or its affiliates). All rights reserved. +// Copyright © 2020, Oracle and/or its affiliates. +// +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! Implements platform specific functionality. +//! Supported platforms: x86_64, aarch64. + +#[macro_use] +extern crate log; + +#[cfg(target_arch = "x86_64")] +use crate::x86_64::SgxEpcSection; +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; +use std::fmt; +use std::result; +use std::sync::Arc; +use thiserror::Error; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; +type GuestRegionMmap = vm_memory::GuestRegionMmap; + +/// Type for returning error code. +#[derive(Debug, Error)] +pub enum Error { + #[cfg(target_arch = "x86_64")] + #[error("Platform specific error (x86_64): {0:?}")] + PlatformSpecific(x86_64::Error), + #[cfg(target_arch = "aarch64")] + #[error("Platform specific error (aarch64): {0:?}")] + PlatformSpecific(aarch64::Error), + #[error("The memory map table extends past the end of guest memory")] + MemmapTablePastRamEnd, + #[error("Error writing memory map table to guest memory")] + MemmapTableSetup, + #[error("The hvm_start_info structure extends past the end of guest memory")] + StartInfoPastRamEnd, + #[error("Error writing hvm_start_info to guest memory")] + StartInfoSetup, + #[error("Failed to compute initramfs address")] + InitramfsAddress, + #[error("Error writing module entry to guest memory: {0}")] + ModlistSetup(#[source] vm_memory::GuestMemoryError), + #[error("RSDP extends past the end of guest memory")] + RsdpPastRamEnd, +} + +/// Type for returning public functions outcome. +pub type Result = result::Result; + +/// Type for memory region types. +#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] +pub enum RegionType { + /// RAM type + Ram, + + /// SubRegion memory region. + /// A SubRegion is a memory region sub-region, allowing for a region + /// to be split into sub regions managed separately. + /// For example, the x86 32-bit memory hole is a SubRegion. + SubRegion, + + /// Reserved type. + /// A Reserved memory region is one that should not be used for memory + /// allocation. This type can be used to prevent the VMM from allocating + /// memory ranges in a specific address range. + Reserved, +} + +/// Module for aarch64 related functionality. +#[cfg(target_arch = "aarch64")] +pub mod aarch64; + +#[cfg(target_arch = "aarch64")] +pub use aarch64::{ + arch_memory_regions, configure_system, configure_vcpu, fdt::DeviceInfoForFdt, + get_host_cpu_phys_bits, initramfs_load_addr, layout, layout::CMDLINE_MAX_SIZE, + layout::IRQ_BASE, uefi, EntryPoint, +}; + +#[cfg(target_arch = "x86_64")] +pub mod x86_64; + +#[cfg(target_arch = "x86_64")] +pub use x86_64::{ + arch_memory_regions, configure_system, configure_vcpu, generate_common_cpuid, + get_host_cpu_phys_bits, initramfs_load_addr, layout, layout::CMDLINE_MAX_SIZE, + layout::CMDLINE_START, regs, CpuidFeatureEntry, EntryPoint, +}; + +/// Safe wrapper for `sysconf(_SC_PAGESIZE)`. +#[cfg(target_arch = "x86_64")] +#[inline(always)] +fn pagesize() -> usize { + // Trivially safe + unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize } +} + +#[derive(Clone, Default)] +pub struct NumaNode { + pub memory_regions: Vec>, + pub hotplug_regions: Vec>, + pub cpus: Vec, + pub distances: BTreeMap, + pub memory_zones: Vec, + #[cfg(target_arch = "x86_64")] + pub sgx_epc_sections: Vec, +} + +pub type NumaNodes = BTreeMap; + +/// Type for passing information about the initramfs in the guest memory. +pub struct InitramfsConfig { + /// Load address of initramfs in guest memory + pub address: vm_memory::GuestAddress, + /// Size of initramfs in guest memory + pub size: usize, +} + +/// Types of devices that can get attached to this platform. +#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy)] +pub enum DeviceType { + /// Device Type: Virtio. + Virtio(u32), + /// Device Type: Serial. + #[cfg(target_arch = "aarch64")] + Serial, + /// Device Type: RTC. + #[cfg(target_arch = "aarch64")] + Rtc, + /// Device Type: GPIO. + #[cfg(target_arch = "aarch64")] + Gpio, +} + +/// Default (smallest) memory page size for the supported architectures. +pub const PAGE_SIZE: usize = 4096; + +impl fmt::Display for DeviceType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +/// Structure to describe MMIO device information +#[derive(Clone, Debug)] +#[cfg(target_arch = "aarch64")] +pub struct MmioDeviceInfo { + pub addr: u64, + pub len: u64, + pub irq: u32, +} + +/// Structure to describe PCI space information +#[derive(Clone, Debug)] +#[cfg(target_arch = "aarch64")] +pub struct PciSpaceInfo { + pub pci_segment_id: u16, + pub mmio_config_address: u64, + pub pci_device_space_start: u64, + pub pci_device_space_size: u64, +} + +#[cfg(target_arch = "aarch64")] +impl DeviceInfoForFdt for MmioDeviceInfo { + fn addr(&self) -> u64 { + self.addr + } + fn irq(&self) -> u32 { + self.irq + } + fn length(&self) -> u64 { + self.len + } +} diff --git a/vendor/cube-hypervisor/arch/src/x86_64/cpuid_filter.rs b/vendor/cube-hypervisor/arch/src/x86_64/cpuid_filter.rs new file mode 100644 index 0000000..927dce5 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/x86_64/cpuid_filter.rs @@ -0,0 +1,599 @@ +// Copyright © 2023 Tencent Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::x86_64::CpuidReg; +use hypervisor::arch::x86::{CpuIdCustomEntry, CpuIdEntry}; + +/// Intel brand string. +pub const VENDOR_ID_INTEL: &[u8; 12] = b"GenuineIntel"; + +/// AMD brand string. +pub const VENDOR_ID_AMD: &[u8; 12] = b"AuthenticAMD"; + +pub fn apply_compatible_template(cpuids: &mut [CpuIdEntry]) { + if let Some(vendor) = get_vendor_id_from_host() { + if *VENDOR_ID_INTEL == vendor { + debug!("Got Intel machine"); + apply_template(cpuids, &t2cl()); + } else if *VENDOR_ID_AMD == vendor { + debug!("Got AMD machine"); + apply_template(cpuids, &t2a()); + } else { + warn!("Unsupported vendor"); + } + } +} + +/// Extracts the CPU vendor id from leaf 0x0. +/// +/// # Errors +/// +/// When CPUID leaf 0 is not supported. +pub fn get_vendor_id_from_host() -> Option<[u8; 12]> { + // SAFETY: call cpuid with valid leaves + unsafe { + let entry = std::arch::x86_64::__cpuid(0); + // The ordering of the vendor string is ebx,edx,ecx this is not a mistake. + Some(std::mem::transmute::<[u32; 3], [u8; 12]>([ + entry.ebx, entry.edx, entry.ecx, + ])) + } +} + +#[derive(Debug, Default, serde::Deserialize, serde::Serialize)] +pub struct CustomCpuId { + pub cpuids: Vec, +} + +/// Target register to be modified by a bitmap. +#[derive(Debug, Clone)] +pub struct CpuidRegisterModifier { + /// CPUID register to be modified by the bitmap. + pub register: CpuidReg, + /// Bit mapping to be applied as a modifier to the + /// register's value at the address provided. + pub bitmap: RegisterValueFilter, +} + +/// Composite type that holistically provides +/// the location of a specific register being used +/// in the context of a CPUID tree. +#[derive(Debug, Default, Clone)] +pub struct CpuidLeafModifier { + /// Leaf value. + pub leaf: u32, + /// Sub-Leaf value. + pub subleaf: u32, + /// KVM feature flags for this leaf-subleaf. + pub flags: u32, + /// All registers to be modified under the sub-leaf. + pub modifiers: Vec, +} + +/// Bit-mapped value to adjust targeted bits of a register. +#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)] +pub struct RegisterValueFilter { + /// Filter to be used when writing the value bits. + pub filter: u32, + /// Value to be applied. + pub value: u32, +} + +impl RegisterValueFilter { + /// Applies filter to the value + #[inline] + pub fn apply(&self, value: u32) -> u32 { + (value & !self.filter) | self.value + } +} + +/// Wrapper type to containing x86_64 CPU config modifiers. +#[derive(Debug, Default, Clone)] +pub struct CustomCpuTemplate { + /// Modifiers for CPUID configuration. + pub cpuid_modifiers: Vec, +} + +pub fn apply_template(cpuids: &mut [CpuIdEntry], template: &CustomCpuTemplate) { + // Apply CPUID modifiers + for mod_leaf in template.cpuid_modifiers.iter() { + let function = mod_leaf.leaf; + let index = mod_leaf.subleaf; + + for entry in &mut *cpuids { + if entry.function == function && entry.index == index { + entry.flags = mod_leaf.flags; + + for mod_reg in &mod_leaf.modifiers { + match mod_reg.register { + CpuidReg::EAX => { + entry.eax = mod_reg.bitmap.apply(entry.eax); + } + CpuidReg::EBX => { + entry.ebx = mod_reg.bitmap.apply(entry.ebx); + } + CpuidReg::ECX => { + entry.ecx = mod_reg.bitmap.apply(entry.ecx); + } + CpuidReg::EDX => { + entry.edx = mod_reg.bitmap.apply(entry.edx); + } + } + } + } + } + } +} + +/// T2CL template +/// +/// Mask CPUID to make exposed CPU features as close as possbile to Intel Cascade Lake and provide +/// instruction set feature partity with AMD Milan using T2A template. +/// +/// References: +/// - Intel SDM: https://cdrdv2.intel.com/v1/dl/getContent/671200 +/// - AMD APM: https://www.amd.com/system/files/TechDocs/40332.pdf +/// - CPUID Enumeration and Architectural MSRs: https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/cpuid-enumeration-and-architectural-msrs.html +#[allow(clippy::unusual_byte_groupings)] +pub fn t2cl() -> CustomCpuTemplate { + CustomCpuTemplate { + cpuid_modifiers: vec![ + CpuidLeafModifier { + leaf: 0x1, + subleaf: 0x0, + flags: 0, + modifiers: vec![ + // EAX: Version Information + // - Bits 03-00: Stepping ID (Intel SDM) / Stepping (AMD APM) + // - Bits 07-04: Model (Intel SDM) / BaseModel (AMD APM) + // - Bits 11-08: Family (Intel SDM) / BaseFamily (AMD APM) + // - Bits 13-12: Processor Type (Intel SDM) / Reserved (AMD APM) + // - Bits 19-16: Extended Model ID (Intel SDM) / ExtModel (AMD APM) + // - Bits 27-20: Extended Family ID (Intel SDM) / ExtFamily (AMD APM) + CpuidRegisterModifier { + register: CpuidReg::EAX, + bitmap: RegisterValueFilter { + filter: 0b0000_11111111_1111_00_11_1111_1111_1111, + value: 0b0000_00000000_0011_00_00_0110_1111_0010, + }, + }, + // ECX: Feature Information + // - Bit 02: DTES64 (Intel SDM) / Reserved (AMD APM) + // - Bit 03: MONITOR (Intel SDM) / MONITOR (AMD APM) + // - Bit 04: DS-CPL (Intel SDM) / Reserved (AMD APM) + // - Bit 05: VMX (Intel SDM) / Reserved (AMD APM) + // - Bit 06: SMX (Intel SDM) / Reserved (AMD APM) + // - Bit 07: EIST (Intel SDM) / Reserved (AMD APM) + // - Bit 08: TM2 (Intel SDM) / Reserved (AMD APM) + // - Bit 10: CNXT-ID (Intel SDM) / Reserved (AMD APM) + // - Bit 11: SDBG (Intel SDM) / Reserved (AMD APM) + // - Bit 14: xTPR Update Control (Intel SDM) / Reserved (AMD APM) + // - Bit 15: PDCM (Intel SDM) / Reserved (AMD APM) + // - Bit 18: DCA (Intel SDM) / Reserevd (AMD APM) + CpuidRegisterModifier { + register: CpuidReg::ECX, + bitmap: RegisterValueFilter { + filter: 0b0000_0000_0000_0100_1100_1101_1111_1100, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + // EDX: Feature Information + // - Bit 07: MCE (Intel SDM) / MCE (AMD APM) + // - Bit 12: MTRR (Intel SDM) / MTRR (AMD APM) + // - Bit 18: PSN (Intel SDM) / Reserved (AMD APM) + // - Bit 21: DS (Intel SDM) / Reserved (AMD APM)PC + // - Bit 22: ACPI (Intel SDM) / Reserved (AMD APM) + // - Bit 27: SS (Intel SDM) / Reserved (AMD APM) + // - Bit 29: TM (Intel SDM) / Reserved (AMD APM) + // - Bit 30: IA64 (deprecated) / Reserved (AMD APM) https://www.intel.com/content/dam/www/public/us/en/documents/manuals/itanium-architecture-vol-4-manual.pdf + // - Bit 31: PBE (Intel SDM) / Reserved (AMD APM) + CpuidRegisterModifier { + register: CpuidReg::EDX, + bitmap: RegisterValueFilter { + filter: 0b1110_1000_0110_0100_0001_0000_1000_0000, + value: 0b0000_0000_0000_0000_0001_0000_1000_0000, + }, + }, + ], + }, + CpuidLeafModifier { + leaf: 0x7, + subleaf: 0x0, + flags: 1, + modifiers: vec![ + // EBX: + // - Bit 02: SGX (Intel SDM) / Reserved (AMD APM) + // - Bit 04: HLE (Intel SDM) / Reserved (AMD APM) + // - Bit 09: Enhanced REP MOVSB/STOSB (Intel SDM) / Reserved (AMD APM) + // - Bit 11: RTM (Intel SDM) / Reserved (AMD APM) + // - Bit 12: RDT-M (Intel SDM) / PQM (AMD APM) + // - Bit 14: MPX (Intel SDM) / Reserved (AMD APM) + // - Bit 15: RDT-A (Intel SDM) / PQE (AMD APM) + // - Bit 16: AVX512F (Intel SDM) / Reserved (AMD APM) + // - Bit 17: AVX512DQ (Intel SDM) / Reserved (AMD APM) + // - Bit 18: RDSEED (Intel SDM) / RDSEED (AMD APM) + // - Bit 19: ADX (Intel SDM) / ADX (AMD APM) + // - Bit 21: AVX512_IFMA (Intel SDM) / Reserved (AMD APM) + // - Bit 22: Reserved (Intel SDM) / RDPID (AMD APM) + // On kernel codebase and Intel SDM, RDPID is enumerated at CPUID.07h:ECX.RDPID[bit 22]. + // https://elixir.bootlin.com/linux/v6.3.8/source/arch/x86/include/asm/cpufeatures.h#L389 + // - Bit 23: CLFLUSHOPT (Intel SDM) / CLFLUSHOPT (AMD APM) + // - Bit 24: CLWB (Intel SDM) / CLWB (AMD APM) + // - Bit 25: Intel Processor Trace (Intel SDM) / Reserved (AMD APM) + // - Bit 26: AVX512PF (Intel SDM) / Reserved (AMD APM) + // - Bit 27: AVX512ER (Intel SDM) / Reserved (AMD APM) + // - Bit 28: AVX512CD (Intel SDM) / Reserved (AMD APM) + // - Bit 29: SHA (Intel SDM) / SHA (AMD APM) + // - Bit 30: AVX512BW (Intel SDM) / Reserved (AMD APM) + // - Bit 31: AVX512VL (Intel SDM) / Reserved (AMD APM) + CpuidRegisterModifier { + register: CpuidReg::EBX, + bitmap: RegisterValueFilter { + filter: 0b1111_1111_1110_1111_1101_1010_0001_0100, + value: 0b0000_0000_0000_0000_0000_0010_0000_0000, + }, + }, + // ECX: + // - Bit 01: AVX512_VBMI (Intel SDM) / Reserved (AMD APM) + // - Bit 02: UMIP (Intel SDM) / UMIP (AMD APM) + // - Bit 03: PKU (Intel SDM) / PKU (AMD APM) + // - Bit 04: OSPKE (Intel SDM) / OSPKE (AMD APM) + // - Bit 06: AVX512_VBMI2 (Intel SDM) / Reserved (AMD APM) + // - Bit 08: GFNI (Intel SDM) / Reserved (AMD APM) + // - Bit 09: VAES (Intel SDM) / VAES (AMD APM) + // - Bit 10: VPCLMULQDQ (Intel SDM) / VPCLMULQDQ (AMD APM) + // - Bit 11: AVX512_VNNI (Intel SDM) / Reserved (AMD APM) + // - Bit 12: AVX512_BITALG (Intel SDM) / Reserved (AMD APM) + // - Bit 14: AVX512_VPOPCNTDQ (Intel SDM) / Reserved (AMD APM) + // - Bit 16: LA57 (Intel SDM) / LA57 (AMD APM) + // - Bit 22: RDPID and IA32_TSC_AUX (Intel SDM) / Reserved (AMD APM) + // - Bit 30: SGX_LC (Intel SDM) / Reserved (AMD APM) + CpuidRegisterModifier { + register: CpuidReg::ECX, + bitmap: RegisterValueFilter { + filter: 0b0100_0000_0100_0001_0101_1111_0101_1110, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + // EDX: + // - Bit 02: AVX512_4VNNIW (Intel SDM) / Reserved (AMD APM) + // - Bit 03: AVX512_4FMAPS (Intel SDM) / Reserved (AMD APM) + // - Bit 04: Fast Short REP MOV (Intel SDM) / Reserved (AMD APM) + // - Bit 08: AVX512_VP2INTERSECT (Intel SDM) / Reserved (AMD APM) + CpuidRegisterModifier { + register: CpuidReg::EDX, + bitmap: RegisterValueFilter { + filter: 0b0000_0000_0000_0000_0000_0001_0001_1100, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + ], + }, + CpuidLeafModifier { + leaf: 0xd, + subleaf: 0x0, + flags: 1, + modifiers: vec![ + // EAX: + // - Bits 04-03: MPX state (Intel SDM) / Reserved (AMD APM) + // - Bits 07-05: AVX-512 state (Intel SDM) / Reserved (AMD APM) + // - Bit 09: PKRU state (Intel SDM) / MPK (AMD APM) + CpuidRegisterModifier { + register: CpuidReg::EAX, + bitmap: RegisterValueFilter { + filter: 0b0000_0000_0000_0000_0000_00_1_0_111_11_000, + value: 0b0000_0000_0000_0000_0000_00_0_0_000_00_000, + }, + }, + ], + }, + CpuidLeafModifier { + leaf: 0xd, + subleaf: 0x1, + flags: 1, + modifiers: vec![ + // EAX: + // - Bit 01: Supports XSAVEC and the compacted form of XRSTOR (Intel SDM) / + // XSAVEC (AMD APM) + // - Bit 02: Supports XGETBV (Intel SDM) / XGETBV (AMD APM) + // - Bit 03: Supports XSAVES/XRSTORS and IA32_XSS (Intel SDM) / XSAVES (AMD + // APM) + CpuidRegisterModifier { + register: CpuidReg::EAX, + bitmap: RegisterValueFilter { + filter: 0b0000_0000_0000_0000_0000_0000_0000_1110, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + ], + }, + CpuidLeafModifier { + leaf: 0x80000001, + subleaf: 0x0, + flags: 0, + modifiers: vec![ + // ECX: + // - Bit 06: Reserved (Intel SDM) / SSE4A (AMD APM) + // - Bit 07: Reserved (Intel SDM) / MisAlignSse (AMD APM) + // - Bit 08: PREFETCHW (Intel SDM) / 3DNowPrefetch (AMD APM) + // - Bit 29: MONITORX and MWAITX (Intel SDM) / MONITORX (AMD APM) + CpuidRegisterModifier { + register: CpuidReg::ECX, + bitmap: RegisterValueFilter { + filter: 0b0010_0000_0000_0000_0000_0001_1100_0000, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + // EDX: + // - Bit 22: Reserved (Intel SDM) / MmxExt (AMD APM) + // - Bit 23: Reserved (Intel SDM) / MMX (AMD APM) + // - Bit 24: Reserved (Intel SDM) / FSXR (AMD APM) + // - Bit 25: Reserved (Intel SDM) / FFXSR (AMD APM) + // - Bit 26: 1-GByte pages (Intel SDM) / Page1GB (AMD APM) + CpuidRegisterModifier { + register: CpuidReg::EDX, + bitmap: RegisterValueFilter { + filter: 0b0000_0111_1100_0000_0000_0000_0000_0000, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + ], + }, + CpuidLeafModifier { + leaf: 0x80000008, + subleaf: 0x0, + flags: 0, + modifiers: vec![ + // EBX: + // - Bit 09: WBNOINVD (Intel SDM) / WBNOINVD (AMD APM) + CpuidRegisterModifier { + register: CpuidReg::EBX, + bitmap: RegisterValueFilter { + filter: 0b0000_0000_0000_0000_0000_0010_0000_0000, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + ], + }, + ], + } +} + +/// T2A template +/// +/// Provide instruction set feature partity with Intel Cascade Lake or later using T2CL template. +/// +/// References: +/// - Intel SDM: https://cdrdv2.intel.com/v1/dl/getContent/671200 +/// - AMD APM: https://www.amd.com/system/files/TechDocs/40332.pdf +/// - CPUID Enumeration and Architectural MSRs: https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/cpuid-enumeration-and-architectural-msrs.html +#[allow(clippy::unusual_byte_groupings)] +pub fn t2a() -> CustomCpuTemplate { + CustomCpuTemplate { + cpuid_modifiers: vec![ + CpuidLeafModifier { + leaf: 0x1, + subleaf: 0x0, + flags: 0, + modifiers: vec![ + // EAX: Version Information + // - Bits 03-00: Stepping (AMD APM) / Stepping ID (Intel SDM) + // - Bits 07-04: BaseModel (AMD APM) / Model (Intel SDM) + // - Bits 11-08: BaseFamily (AMD APM) / Family (Intel SDM) + // - Bits 13-12: Reserved (AMD APM) / Processor Type (Intel SDM) + // - Bits 19-16: ExtModel (AMD APM) / Extended Model ID (Intel SDM) + // - Bits 27-20: ExtFamily (AMD APM) / Extended Family ID (Intel SDM) + CpuidRegisterModifier { + register: CpuidReg::EAX, + bitmap: RegisterValueFilter { + filter: 0b0000_11111111_1111_00_11_1111_1111_1111, + value: 0b0000_00000000_0011_00_00_0110_1111_0010, + }, + }, + // ECX: Feature Information + // - Bit 02: Reserved (AMD APM) / DTES64 (Intel SDM) + // - Bit 03: MONITOR (AMD APM) / MONITOR (Intel SDM) + // - Bit 04: Reserved (AMD APM) / DS-CPL (Intel SDM) + // - Bit 05: Reserved (AMD APM) / VMX (Intel SDM) + // - Bit 06: Reserved (AMD APM) / SMX (Intel SDM) + // - Bit 07: Reserved (AMD APM) / EIST (Intel SDM) + // - Bit 08: Reserved (AMD APM) / TM2 (Intel SDM) + // - Bit 10: Reserved (AMD APM) / CNXT-ID (Intel SDM) + // - Bit 11: Reserved (AMD APM) / SDBG (Intel SDM) + // - Bit 14: Reserved (AMD APM) / xTPR Update Control (Intel SDM) + // - Bit 15: Reserved (AMD APM) / PDCM (Intel SDM) + // - Bit 18: Reserevd (AMD APM) / DCA (Intel SDM) + CpuidRegisterModifier { + register: CpuidReg::ECX, + bitmap: RegisterValueFilter { + filter: 0b0000_0000_0000_0100_1100_1101_1111_1100, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + // EDX: Feature Information + // - Bit 07: MCE (AMD APM) / MCE (Intel SDM) + // - Bit 12: MTRR (AMD APM) / MTRR (Intel SDM) + // - Bit 18: Reserved (AMD APM) / PSN (Intel SDM) + // - Bit 21: Reserved (AMD APM) / DS (Intel SDM) + // - Bit 22: Reserved (AMD APM) / ACPI (Intel SDM) + // - Bit 27: Reserved (AMD APM) / SS (Intel SDM) + // - Bit 29: Reserved (AMD APM) / TM (Intel SDM) + // - Bit 30: Reserved (AMD APM) / IA-64 (deprecated) https://www.intel.com/content/dam/www/public/us/en/documents/manuals/itanium-architecture-vol-4-manual.pdf + // - Bit 31: Reserved (AMD APM) / PBE (Intel SDM) + CpuidRegisterModifier { + register: CpuidReg::EDX, + bitmap: RegisterValueFilter { + filter: 0b1110_1000_0110_0100_0001_0000_1000_0000, + value: 0b0000_0000_0000_0000_0001_0000_1000_0000, + }, + }, + ], + }, + CpuidLeafModifier { + leaf: 0x7, + subleaf: 0x0, + flags: 1, + modifiers: vec![ + // EBX: + // - Bit 02: Reserved (AMD APM) / SGX (Intel SDM) + // - Bit 04: Reserved (AMD APM) / HLE (Intel SDM) + // - Bit 09: Reserved (AMD APM) / Enhanced REP MOVSB/STOSB (Intel SDM) + // - Bit 11: Reserved (AMD APM) / RTM (Intel SDM) + // - Bit 12: PQM (AMD APM) / RDT-M (Intel SDM) + // - Bit 14: Reserved (AMD APM) / MPX (Intel SDM) + // - Bit 15: PQE (AMD APM) / RDT-A (Intel SDM) + // - Bit 16: Reserved (AMD APM) / AVX512F (Intel SDM) + // - Bit 17: Reserved (AMD APM) / AVX512DQ (Intel SDM) + // - Bit 18: RDSEED (AMD APM) / RDSEED (Intel SDM) + // - Bit 19: ADX (AMD APM) / ADX (Intel SDM) + // - Bit 21: Reserved (AMD APM) / AVX512_IFMA (Intel SDM) + // - Bit 22: RDPID (AMD APM) / Reserved (Intel SDM) + // On kernel codebase and Intel SDM, RDPID is enumerated at CPUID.07h:ECX.RDPID[bit 22]. + // https://elixir.bootlin.com/linux/v6.3.8/source/arch/x86/include/asm/cpufeatures.h#L389 + // - Bit 23: CLFLUSHOPT (AMD APM) / CLFLUSHOPT (Intel SDM) + // - Bit 24: CLWB (AMD APM) / CLWB (Intel SDM) + // - Bit 25: Reserved (AMD APM) / Intel Processor Trace (Intel SDM) + // - Bit 26: Reserved (AMD APM) / AVX512PF (Intel SDM) + // - Bit 27: Reserved (AMD APM) / AVX512ER (Intel SDM) + // - Bit 28: Reserved (AMD APM) / AVX512CD (Intel SDM) + // - Bit 29: SHA (AMD APM) / SHA (Intel SDM) + // - Bit 30: Reserved (AMD APM) / AVX512BW (Intel SDM) + // - Bit 31: Reserved (AMD APM) / AVX512VL (Intel SDM) + CpuidRegisterModifier { + register: CpuidReg::EBX, + bitmap: RegisterValueFilter { + filter: 0b1111_1111_1110_1111_1101_1010_0001_0100, + value: 0b0000_0000_0000_0000_0000_0010_0000_0000, + }, + }, + // ECX: + // - Bit 01: Reserved (AMD APM) / AVX512_VBMI (Intel SDM) + // - Bit 02: UMIP (AMD APM) / UMIP (Intel SDM) + // - Bit 03: PKU (AMD APM) / PKU (Intel SDM) + // - Bit 04: OSPKE (AMD APM) / OSPKE (Intel SDM) + // - Bit 06: Reserved (AMD APM) / AVX512_VBMI2 (Intel SDM) + // - Bit 08: Reserved (AMD APM) / GFNI (Intel SDM) + // - Bit 09: VAES (AMD APM) / VAES (Intel SDM) + // - Bit 10: VPCLMULQDQ (AMD APM) / VPCLMULQDQ (Intel SDM) + // - Bit 11: Reserved (AMD APM) / AVX512_VNNI (Intel SDM) + // - Bit 12: Reserved (AMD APM) / AVX512_BITALG (Intel SDM) + // - Bit 14: Reserved (AMD APM) / AVX512_VPOPCNTDQ (Intel SDM) + // - Bit 16: LA57 (AMD APM) / LA57 (Intel SDM) + // - Bit 22: Reserved (AMD APM) / RDPID and IA32_TSC_AUX (Intel SDM) + // - Bit 30: Reserved (AMD APM) / SGX_LC (Intel SDM) + CpuidRegisterModifier { + register: CpuidReg::ECX, + bitmap: RegisterValueFilter { + filter: 0b0100_0000_0100_0001_0101_1111_0101_1110, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + // EDX: + // - Bit 02: Reserved (AMD APM) / AVX512_4VNNIW (Intel SDM) + // - Bit 03: Reserved (AMD APM) / AVX512_4FMAPS (Intel SDM) + // - Bit 04: Reserved (AMD APM) / Fast Short REP MOV (Intel SDM) + // - Bit 08: Reserved (AMD APM) / AVX512_VP2INTERSECT (Intel SDM) + CpuidRegisterModifier { + register: CpuidReg::EDX, + bitmap: RegisterValueFilter { + filter: 0b0000_0000_0000_0000_0000_0001_0001_1100, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + ], + }, + CpuidLeafModifier { + leaf: 0xd, + subleaf: 0x0, + flags: 1, + modifiers: vec![ + // EAX: + // - Bits 04-03: Reserved (AMD APM) / MPX state (Intel SDM) + // - Bits 07-05: Reserved (AMD APM) / AVX-512 state (Intel SDM) + // - Bit 09: MPK (AMD APM) / PKRU state (Intel SDM) + CpuidRegisterModifier { + register: CpuidReg::EAX, + bitmap: RegisterValueFilter { + filter: 0b0000_0000_0000_0000_0000_00_1_0_111_11_000, + value: 0b0000_0000_0000_0000_0000_00_0_0_000_00_000, + }, + }, + ], + }, + CpuidLeafModifier { + leaf: 0xd, + subleaf: 0x1, + flags: 1, + modifiers: vec![ + // EAX: + // - Bit 01: XSAVEC (AMD APM) / Supports XSAVEC and the compacted form of + // XRSTOR (Intel SDM) + // - Bit 02: XGETBV (AMD APM) / Supports XGETBV (Intel SDM) + // - Bit 03: XSAVES (AMD APM) / Supports XSAVES/XRSTORS and IA32_XSS (Intel + // SDM) + CpuidRegisterModifier { + register: CpuidReg::EAX, + bitmap: RegisterValueFilter { + filter: 0b0000_0000_0000_0000_0000_0000_0000_1110, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + ], + }, + CpuidLeafModifier { + leaf: 0x80000001, + subleaf: 0x0, + flags: 0, + modifiers: vec![ + // ECX: + // - Bit 02: SVM (AMD APM) / Reserved (Intel SDM) + // - Bit 06: SSE4A (AMD APM) / Reserved (Intel SDM) + // - Bit 07: MisAlignSse (AMD APM) / Reserved (Intel SDM) + // - Bit 08: 3DNowPrefetch (AMD APM) / PREFETCHW (Intel SDM) + // - Bit 29: MONITORX (AMD APM) / MONITORX and MWAITX (Intel SDM) + CpuidRegisterModifier { + register: CpuidReg::ECX, + bitmap: RegisterValueFilter { + filter: 0b0010_0000_0000_0000_0000_0001_1100_0100, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + // EDX: + // - Bit 22: MmxExt (AMD APM) / Reserved (Intel SDM) + // - Bit 25: FFXSR (AMD APM) / Reserved (Intel SDM) + // - Bit 26: Page1GB (AMD APM) / 1-GByte pages (Intel SDM) + CpuidRegisterModifier { + register: CpuidReg::EDX, + bitmap: RegisterValueFilter { + filter: 0b0000_0110_0100_0000_0000_0000_0000_0000, + value: 0b0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + ], + }, + CpuidLeafModifier { + leaf: 0x80000008, + subleaf: 0x0, + flags: 0, + modifiers: vec![ + // EBX: + // - Bit 00: CLZERO (AMD APM) / Reserved (Intel SDM) + // - Bit 02: RstrFpErrPtrs (AMD APM) / Reserved (Intel SDM) + // - Bit 09: WBNOINVD (AMD APM) / WBNOINVD (Intel SDM) + // - Bit 18: IbrsPreferred (ADM APM) / Reserved (Intel SDm) + // - Bit 19: IbrsSameMode (AMD APM) / Reserved (Intel SDM) + // - Bit 20: EferLmsleUnsupported (AMD APM) / Reserved (Intel SDM) + CpuidRegisterModifier { + register: CpuidReg::EBX, + bitmap: RegisterValueFilter { + filter: 0b0000_0000_0001_1100_0000_0010_0000_0101, + value: 0b0000_0000_0001_1100_0000_0000_0000_0100, + }, + }, + ], + }, + ], + } +} diff --git a/vendor/cube-hypervisor/arch/src/x86_64/interrupts.rs b/vendor/cube-hypervisor/arch/src/x86_64/interrupts.rs new file mode 100644 index 0000000..1ca322e --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/x86_64/interrupts.rs @@ -0,0 +1,39 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use std::result; +use std::sync::Arc; + +pub type Result = result::Result; + +// Defines poached from apicdef.h kernel header. +pub const APIC_LVT0: usize = 0x350; +pub const APIC_LVT1: usize = 0x360; +pub const APIC_MODE_NMI: u32 = 0x4; +pub const APIC_MODE_EXTINT: u32 = 0x7; + +pub fn set_apic_delivery_mode(reg: u32, mode: u32) -> u32 { + ((reg) & !0x700) | ((mode) << 8) +} + +/// Configures LAPICs. LAPIC0 is set for external interrupts, LAPIC1 is set for NMI. +/// +/// # Arguments +/// * `vcpu` - The VCPU object to configure. +pub fn set_lint(vcpu: &Arc) -> Result<()> { + let mut klapic = vcpu.get_lapic()?; + + let lvt_lint0 = klapic.get_klapic_reg(APIC_LVT0); + klapic.set_klapic_reg( + APIC_LVT0, + set_apic_delivery_mode(lvt_lint0, APIC_MODE_EXTINT), + ); + let lvt_lint1 = klapic.get_klapic_reg(APIC_LVT1); + klapic.set_klapic_reg(APIC_LVT1, set_apic_delivery_mode(lvt_lint1, APIC_MODE_NMI)); + + vcpu.set_lapic(&klapic) +} diff --git a/vendor/cube-hypervisor/arch/src/x86_64/layout.rs b/vendor/cube-hypervisor/arch/src/x86_64/layout.rs new file mode 100644 index 0000000..d8acb83 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/x86_64/layout.rs @@ -0,0 +1,125 @@ +// Copyright © 2020, Oracle and/or its affiliates. +// +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use vm_memory::GuestAddress; + +/* + +Memory layout documentation and constants +~~~~~~ ~~~~~~ ~~~~~~~~~~~~~ ~~~ ~~~~~~~~~ + +Constants are in order and grouped by range. Take care to update all references +when making changes and keep them in order. + +*/ + +// ** Low RAM (start: 0, length: 640KiB) ** +pub const LOW_RAM_START: GuestAddress = GuestAddress(0x0); + +// == Fixed addresses within the "Low RAM" range: == + +// Location of EBDA address +pub const EBDA_POINTER: GuestAddress = GuestAddress(0x40e); + +// Initial GDT/IDT needed to boot kernel +pub const BOOT_GDT_START: GuestAddress = GuestAddress(0x500); +pub const BOOT_IDT_START: GuestAddress = GuestAddress(0x520); + +/// Address for the hvm_start_info struct used in PVH boot +pub const PVH_INFO_START: GuestAddress = GuestAddress(0x6000); + +/// Starting address of array of modules of hvm_modlist_entry type. +/// Used to enable initrd support using the PVH boot ABI. +pub const MODLIST_START: GuestAddress = GuestAddress(0x6040); + +/// Address of memory map table used in PVH boot. Can overlap +/// with the zero page address since they are mutually exclusive. +pub const MEMMAP_START: GuestAddress = GuestAddress(0x7000); + +/// The 'zero page', a.k.a linux kernel bootparams. +pub const ZERO_PAGE_START: GuestAddress = GuestAddress(0x7000); + +/// Initial stack for the boot CPU. +pub const BOOT_STACK_START: GuestAddress = GuestAddress(0x8000); +pub const BOOT_STACK_POINTER: GuestAddress = GuestAddress(0x8ff0); + +// Initial pagetables. +pub const PML5_START: GuestAddress = GuestAddress(0x9000); +pub const PML4_START: GuestAddress = GuestAddress(0xa000); +pub const PDPTE_START: GuestAddress = GuestAddress(0xb000); +pub const PDE_START: GuestAddress = GuestAddress(0xc000); + +/// Kernel command line start address. +pub const CMDLINE_START: GuestAddress = GuestAddress(0x20000); +/// Kernel command line start address maximum size. +pub const CMDLINE_MAX_SIZE: usize = 0x10000; + +// MPTABLE, describing VCPUS. +pub const MPTABLE_START: GuestAddress = GuestAddress(0x9fc00); + +// == End of "Low RAM" range. == + +// ** EBDA reserved area (start: 640KiB, length: 384KiB) ** +pub const EBDA_START: GuestAddress = GuestAddress(0xa0000); + +// == Fixed constants within the "EBDA" range == + +// ACPI RSDP table +pub const RSDP_POINTER: GuestAddress = EBDA_START; + +pub const SMBIOS_START: u64 = 0xf0000; // First possible location per the spec. + +// == End of "EBDA" range == + +// ** High RAM (start: 1MiB, length: 3071MiB) ** +pub const HIGH_RAM_START: GuestAddress = GuestAddress(0x100000); + +// == No fixed addresses in the "High RAM" range == + +// ** 32-bit reserved area (start: 3GiB, length: 896MiB) ** +pub const MEM_32BIT_RESERVED_START: GuestAddress = GuestAddress(0xc000_0000); +pub const MEM_32BIT_RESERVED_SIZE: u64 = PCI_MMCONFIG_SIZE + MEM_32BIT_DEVICES_SIZE; + +// == Fixed constants within the "32-bit reserved" range == + +// Sub range: 32-bit PCI devices (start: 3GiB, length: 640Mib) +pub const MEM_32BIT_DEVICES_START: GuestAddress = MEM_32BIT_RESERVED_START; +pub const MEM_32BIT_DEVICES_SIZE: u64 = 640 << 20; + +// PCI MMCONFIG space (start: after the device space, length: 256MiB) +pub const PCI_MMCONFIG_START: GuestAddress = + GuestAddress(MEM_32BIT_DEVICES_START.0 + MEM_32BIT_DEVICES_SIZE); +pub const PCI_MMCONFIG_SIZE: u64 = 256 << 20; +// One bus with potentially 256 devices (32 slots x 8 functions). +pub const PCI_MMIO_CONFIG_SIZE_PER_SEGMENT: u64 = 4096 * 256; + +// TSS is 3 pages after the PCI MMCONFIG space +pub const KVM_TSS_START: GuestAddress = GuestAddress(PCI_MMCONFIG_START.0 + PCI_MMCONFIG_SIZE); +pub const KVM_TSS_SIZE: u64 = (3 * 4) << 10; + +// Identity map is a one page region after the TSS +pub const KVM_IDENTITY_MAP_START: GuestAddress = GuestAddress(KVM_TSS_START.0 + KVM_TSS_SIZE); +pub const KVM_IDENTITY_MAP_SIZE: u64 = 4 << 10; + +/// TPM Address Range +/// This Address range is specific to CRB Interface +pub const TPM_START: GuestAddress = GuestAddress(0xfed4_0000); +pub const TPM_SIZE: u64 = 0x1000; + +// IOAPIC +pub const IOAPIC_START: GuestAddress = GuestAddress(0xfec0_0000); +pub const IOAPIC_SIZE: u64 = 0x20; + +// APIC +pub const APIC_START: GuestAddress = GuestAddress(0xfee0_0000); + +// == End of "32-bit reserved" range. == + +// ** 64-bit RAM start (start: 4GiB, length: varies) ** +pub const RAM_64BIT_START: GuestAddress = GuestAddress(0x1_0000_0000); diff --git a/vendor/cube-hypervisor/arch/src/x86_64/mod.rs b/vendor/cube-hypervisor/arch/src/x86_64/mod.rs new file mode 100644 index 0000000..07cf169 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/x86_64/mod.rs @@ -0,0 +1,1416 @@ +// Copyright © 2020, Oracle and/or its affiliates. +// +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +use std::sync::Arc; +mod cpuid_filter; +pub mod interrupts; +pub mod layout; +mod mpspec; +mod mptable; +pub mod regs; +use crate::GuestMemoryMmap; +use crate::InitramfsConfig; +use crate::RegionType; +use hypervisor::arch::x86::{CpuIdCustomEntry, CpuIdEntry, CPUID_FLAG_VALID_INDEX}; +use hypervisor::HypervisorError; +use linux_loader::loader::elf::start_info::{ + hvm_memmap_table_entry, hvm_modlist_entry, hvm_start_info, +}; +use std::collections::BTreeMap; +use std::mem; +use vm_memory::{ + Address, Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic, + GuestMemoryRegion, GuestUsize, +}; +mod smbios; +use std::arch::x86_64; +#[cfg(feature = "tdx")] +pub mod tdx; + +// CPUID feature bits +const TSC_DEADLINE_TIMER_ECX_BIT: u8 = 24; // tsc deadline timer ecx bit. +const HYPERVISOR_ECX_BIT: u8 = 31; // Hypervisor ecx bit. +const MTRR_EDX_BIT: u8 = 12; // Hypervisor ecx bit. + +const CUBE_CPUID_COMPAT_FILE: &str = + "/usr/local/services/cubetoolbox/cube-snapshot/cpuid_compatible.json"; + +// KVM feature bits +const KVM_FEATURE_ASYNC_PF_INT_BIT: u8 = 14; +#[cfg(feature = "tdx")] +const KVM_FEATURE_CLOCKSOURCE_BIT: u8 = 0; +#[cfg(feature = "tdx")] +const KVM_FEATURE_CLOCKSOURCE2_BIT: u8 = 3; +#[cfg(feature = "tdx")] +const KVM_FEATURE_CLOCKSOURCE_STABLE_BIT: u8 = 24; +#[cfg(feature = "tdx")] +const KVM_FEATURE_ASYNC_PF_BIT: u8 = 4; +#[cfg(feature = "tdx")] +const KVM_FEATURE_ASYNC_PF_VMEXIT_BIT: u8 = 10; +#[cfg(feature = "tdx")] +const KVM_FEATURE_STEAL_TIME_BIT: u8 = 5; + +#[derive(Debug, Copy, Clone)] +/// Specifies the entry point address where the guest must start +/// executing code, as well as which of the supported boot protocols +/// is to be used to configure the guest initial state. +pub struct EntryPoint { + /// Address in guest memory where the guest must start execution + pub entry_addr: Option, +} + +const E820_RAM: u32 = 1; +const E820_RESERVED: u32 = 2; + +#[derive(Clone)] +pub struct SgxEpcSection { + start: GuestAddress, + size: GuestUsize, +} + +impl SgxEpcSection { + pub fn new(start: GuestAddress, size: GuestUsize) -> Self { + SgxEpcSection { start, size } + } + pub fn start(&self) -> GuestAddress { + self.start + } + pub fn size(&self) -> GuestUsize { + self.size + } +} + +#[derive(Clone)] +pub struct SgxEpcRegion { + start: GuestAddress, + size: GuestUsize, + epc_sections: BTreeMap, +} + +impl SgxEpcRegion { + pub fn new(start: GuestAddress, size: GuestUsize) -> Self { + SgxEpcRegion { + start, + size, + epc_sections: BTreeMap::new(), + } + } + pub fn start(&self) -> GuestAddress { + self.start + } + pub fn size(&self) -> GuestUsize { + self.size + } + pub fn epc_sections(&self) -> &BTreeMap { + &self.epc_sections + } + pub fn insert(&mut self, id: String, epc_section: SgxEpcSection) { + self.epc_sections.insert(id, epc_section); + } +} + +#[derive(Debug)] +pub enum Error { + /// Error writing MP table to memory. + MpTableSetup(mptable::Error), + + /// Error configuring the general purpose registers + RegsConfiguration(regs::Error), + + /// Error configuring the special registers + SregsConfiguration(regs::Error), + + /// Error configuring the floating point related registers + FpuConfiguration(regs::Error), + + /// Error configuring the MSR registers + MsrsConfiguration(regs::Error), + + /// Failed to set supported CPUs. + SetSupportedCpusFailed(anyhow::Error), + + /// Cannot set the local interruption due to bad configuration. + LocalIntConfiguration(anyhow::Error), + + /// Error setting up SMBIOS table + SmbiosSetup(smbios::Error), + + /// Could not find any SGX EPC section + NoSgxEpcSection, + + /// Missing SGX CPU feature + MissingSgxFeature, + + /// Missing SGX_LC CPU feature + MissingSgxLaunchControlFeature, + + /// Error getting supported CPUID through the hypervisor (kvm/mshv) API + CpuidGetSupported(HypervisorError), + + /// Error populating CPUID with KVM HyperV emulation details + CpuidKvmHyperV(vmm_sys_util::fam::Error), + + /// Error populating CPUID with CPU identification + CpuidIdentification(vmm_sys_util::fam::Error), + + /// Error checking CPUID compatibility + CpuidCheckCompatibility, + + // Error writing EBDA address + EbdaSetup(vm_memory::GuestMemoryError), + + /// Error retrieving TDX capabilities through the hypervisor (kvm/mshv) API + #[cfg(feature = "tdx")] + TdxCapabilities(HypervisorError), +} + +impl From for super::Error { + fn from(e: Error) -> super::Error { + super::Error::PlatformSpecific(e) + } +} + +#[allow(clippy::upper_case_acronyms)] +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum CpuidReg { + EAX, + EBX, + ECX, + EDX, +} + +pub struct CpuidPatch { + pub function: u32, + pub index: u32, + pub flags_bit: Option, + pub eax_bit: Option, + pub ebx_bit: Option, + pub ecx_bit: Option, + pub edx_bit: Option, +} + +impl CpuidPatch { + pub fn set_cpuid_reg( + cpuid: &mut Vec, + function: u32, + index: Option, + reg: CpuidReg, + value: u32, + ) { + let mut entry_found = false; + for entry in cpuid.iter_mut() { + if entry.function == function && (index.is_none() || index.unwrap() == entry.index) { + entry_found = true; + match reg { + CpuidReg::EAX => { + entry.eax = value; + } + CpuidReg::EBX => { + entry.ebx = value; + } + CpuidReg::ECX => { + entry.ecx = value; + } + CpuidReg::EDX => { + entry.edx = value; + } + } + } + } + + if entry_found { + return; + } + + // Entry not found, so let's add it. + if let Some(index) = index { + let mut entry = CpuIdEntry { + function, + index, + flags: CPUID_FLAG_VALID_INDEX, + ..Default::default() + }; + match reg { + CpuidReg::EAX => { + entry.eax = value; + } + CpuidReg::EBX => { + entry.ebx = value; + } + CpuidReg::ECX => { + entry.ecx = value; + } + CpuidReg::EDX => { + entry.edx = value; + } + } + + cpuid.push(entry); + } + } + + pub fn patch_cpuid(cpuid: &mut [CpuIdEntry], patches: Vec) { + for entry in cpuid { + for patch in patches.iter() { + if entry.function == patch.function && entry.index == patch.index { + if let Some(flags_bit) = patch.flags_bit { + entry.flags |= 1 << flags_bit; + } + if let Some(eax_bit) = patch.eax_bit { + entry.eax |= 1 << eax_bit; + } + if let Some(ebx_bit) = patch.ebx_bit { + entry.ebx |= 1 << ebx_bit; + } + if let Some(ecx_bit) = patch.ecx_bit { + entry.ecx |= 1 << ecx_bit; + } + if let Some(edx_bit) = patch.edx_bit { + entry.edx |= 1 << edx_bit; + } + } + } + } + } + + pub fn intersect_cpuid(cpuid: &mut [CpuIdEntry], patches: Vec) { + for entry in cpuid { + for patch in patches.iter() { + if entry.function == patch.function && entry.index == patch.index { + if CpuidFeatureEntry::entry_need_intersect( + entry.function, + entry.index, + CpuidReg::EAX, + ) { + entry.eax &= patch.eax; + } + if CpuidFeatureEntry::entry_need_intersect( + entry.function, + entry.index, + CpuidReg::EBX, + ) { + entry.ebx &= patch.ebx; + } + if CpuidFeatureEntry::entry_need_intersect( + entry.function, + entry.index, + CpuidReg::ECX, + ) { + entry.ecx &= patch.ecx; + } + if CpuidFeatureEntry::entry_need_intersect( + entry.function, + entry.index, + CpuidReg::EDX, + ) { + entry.edx &= patch.edx; + } + } + } + } + } + + pub fn is_feature_enabled( + cpuid: &[CpuIdEntry], + function: u32, + index: u32, + reg: CpuidReg, + feature_bit: usize, + ) -> bool { + let mask = 1 << feature_bit; + + for entry in cpuid { + if entry.function == function && entry.index == index { + let reg_val = match reg { + CpuidReg::EAX => entry.eax, + CpuidReg::EBX => entry.ebx, + CpuidReg::ECX => entry.ecx, + CpuidReg::EDX => entry.edx, + }; + + return (reg_val & mask) == mask; + } + } + + false + } +} + +#[derive(Debug, PartialEq)] +enum CpuidCompatibleCheck { + BitwiseSubset, // bitwise subset + Equal, // equal in value + NumNotGreater, // smaller or equal as a number +} + +pub struct CpuidFeatureEntry { + function: u32, + index: u32, + feature_reg: CpuidReg, + compatible_check: CpuidCompatibleCheck, +} + +impl CpuidFeatureEntry { + fn checked_feature_entry_list() -> Vec { + vec![ + // The following list includes all hardware features bits from + // the CPUID Wiki Page: https://en.wikipedia.org/wiki/CPUID + // Leaf 0x1, ECX/EDX, feature bits + CpuidFeatureEntry { + function: 1, + index: 0, + feature_reg: CpuidReg::ECX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + CpuidFeatureEntry { + function: 1, + index: 0, + feature_reg: CpuidReg::EDX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + // Leaf 0x7, EAX/EBX/ECX/EDX, extended features + CpuidFeatureEntry { + function: 7, + index: 0, + feature_reg: CpuidReg::EAX, + compatible_check: CpuidCompatibleCheck::NumNotGreater, + }, + CpuidFeatureEntry { + function: 7, + index: 0, + feature_reg: CpuidReg::EBX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + CpuidFeatureEntry { + function: 7, + index: 0, + feature_reg: CpuidReg::ECX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + CpuidFeatureEntry { + function: 7, + index: 0, + feature_reg: CpuidReg::EDX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + // Leaf 0x7 subleaf 0x1, EAX, extended features + CpuidFeatureEntry { + function: 7, + index: 1, + feature_reg: CpuidReg::EAX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + // Leaf 0x8000_0001, ECX/EDX, CPUID features bits + CpuidFeatureEntry { + function: 0x8000_0001, + index: 0, + feature_reg: CpuidReg::ECX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + CpuidFeatureEntry { + function: 0x8000_0001, + index: 0, + feature_reg: CpuidReg::EDX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + // KVM CPUID bits: https://www.kernel.org/doc/html/latest/virt/kvm/cpuid.html + // Leaf 0x4000_0000, EAX/EBX/ECX/EDX, KVM CPUID SIGNATURE + CpuidFeatureEntry { + function: 0x4000_0000, + index: 0, + feature_reg: CpuidReg::EAX, + compatible_check: CpuidCompatibleCheck::NumNotGreater, + }, + CpuidFeatureEntry { + function: 0x4000_0000, + index: 0, + feature_reg: CpuidReg::EBX, + compatible_check: CpuidCompatibleCheck::Equal, + }, + CpuidFeatureEntry { + function: 0x4000_0000, + index: 0, + feature_reg: CpuidReg::ECX, + compatible_check: CpuidCompatibleCheck::Equal, + }, + CpuidFeatureEntry { + function: 0x4000_0000, + index: 0, + feature_reg: CpuidReg::EDX, + compatible_check: CpuidCompatibleCheck::Equal, + }, + // Leaf 0x4000_0001, EAX/EBX/ECX/EDX, KVM CPUID features + CpuidFeatureEntry { + function: 0x4000_0001, + index: 0, + feature_reg: CpuidReg::EAX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + CpuidFeatureEntry { + function: 0x4000_0001, + index: 0, + feature_reg: CpuidReg::EBX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + CpuidFeatureEntry { + function: 0x4000_0001, + index: 0, + feature_reg: CpuidReg::ECX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + CpuidFeatureEntry { + function: 0x4000_0001, + index: 0, + feature_reg: CpuidReg::EDX, + compatible_check: CpuidCompatibleCheck::BitwiseSubset, + }, + ] + } + + fn get_features_from_cpuid( + cpuid: &[CpuIdEntry], + feature_entry_list: &[CpuidFeatureEntry], + ) -> Vec { + let mut features = vec![0; feature_entry_list.len()]; + for (i, feature_entry) in feature_entry_list.iter().enumerate() { + for cpuid_entry in cpuid { + if cpuid_entry.function == feature_entry.function + && cpuid_entry.index == feature_entry.index + { + match feature_entry.feature_reg { + CpuidReg::EAX => { + features[i] = cpuid_entry.eax; + } + CpuidReg::EBX => { + features[i] = cpuid_entry.ebx; + } + CpuidReg::ECX => { + features[i] = cpuid_entry.ecx; + } + CpuidReg::EDX => { + features[i] = cpuid_entry.edx; + } + } + + break; + } + } + } + + features + } + + // The function returns `Error` (a.k.a. "incompatible"), when the CPUID features from `src_vm_cpuid` + // is not a subset of those of the `dest_vm_cpuid`. + pub fn check_cpuid_compatibility( + src_vm_cpuid: &[CpuIdEntry], + dest_vm_cpuid: &[CpuIdEntry], + ) -> Result<(), Error> { + let feature_entry_list = &Self::checked_feature_entry_list(); + let src_vm_features = Self::get_features_from_cpuid(src_vm_cpuid, feature_entry_list); + let dest_vm_features = Self::get_features_from_cpuid(dest_vm_cpuid, feature_entry_list); + + // Loop on feature bit and check if the 'source vm' feature is a subset + // of those of the 'destination vm' feature + let mut compatible = true; + for (i, (src_vm_feature, dest_vm_feature)) in src_vm_features + .iter() + .zip(dest_vm_features.iter()) + .enumerate() + { + let entry = &feature_entry_list[i]; + let entry_compatible = match entry.compatible_check { + CpuidCompatibleCheck::BitwiseSubset => { + let different_feature_bits = src_vm_feature ^ dest_vm_feature; + let src_vm_feature_bits_only = different_feature_bits & src_vm_feature; + src_vm_feature_bits_only == 0 + } + CpuidCompatibleCheck::Equal => src_vm_feature == dest_vm_feature, + CpuidCompatibleCheck::NumNotGreater => src_vm_feature <= dest_vm_feature, + }; + if !entry_compatible { + error!( + "Detected incompatible CPUID entry: leaf={:#02x} (subleaf={:#02x}), register='{:?}', \ + compatilbe_check='{:?}', source VM feature='{:#04x}', destination VM feature'{:#04x}'.", + entry.function, entry.index, entry.feature_reg, + entry.compatible_check, src_vm_feature, dest_vm_feature + ); + + compatible = false; + } + } + + if compatible { + info!("No CPU incompatibility detected."); + Ok(()) + } else { + Err(Error::CpuidCheckCompatibility) + } + } + + pub fn entry_need_intersect(function: u32, index: u32, reg: CpuidReg) -> bool { + let feature_entry_list = &Self::checked_feature_entry_list(); + for entry in feature_entry_list { + if function == entry.function + && index == entry.index + && reg == entry.feature_reg + && CpuidCompatibleCheck::BitwiseSubset == entry.compatible_check + { + debug!("intersect cpuid function 0x{:x} reg {:?}", function, reg); + return true; + } + } + false + } +} + +// Get a static CPU template stored as a JSON file. +pub fn get_json_template() -> Vec { + if std::fs::metadata(CUBE_CPUID_COMPAT_FILE).is_ok() { + debug!("compatilbe cpuid file {:?}", CUBE_CPUID_COMPAT_FILE); + serde_json::from_str(&std::fs::read_to_string(CUBE_CPUID_COMPAT_FILE).unwrap()).unwrap() + } else { + debug!("not found cpuid compatible json file"); + Vec::new() + } +} + +pub fn generate_compatible_cpuid() -> cpuid_filter::CustomCpuId { + cpuid_filter::CustomCpuId { + cpuids: get_json_template(), + } +} + +const XSAVE_HDR_SIZE: u32 = 64; +const XSAVE_HDR_OFFSET: u32 = 512; + +// from kernel arch/x86/include/asm/fpu/types.h +const XFEATURE_FP: u32 = 0; +const XFEATURE_SSE: u32 = 1; +const XFEATURE_MASK_FP: u32 = 1 << XFEATURE_FP; +const XFEATURE_MASK_SSE: u32 = 1 << XFEATURE_SSE; +const XFEATURE_MASK_FPSSE: u32 = XFEATURE_MASK_FP | XFEATURE_MASK_SSE; +const XFEATURE_MASK_EXTEND: u32 = !(XFEATURE_MASK_FPSSE | (1 << 31)); + +pub fn xstate_required_size(xstate: u32) -> u32 { + let mut feature_bit = 0; + let mut ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET; + let mut xstate_bv = xstate & XFEATURE_MASK_EXTEND; + + debug!( + "mask extend 0x{:x} bv 0x{:x} xstate 0x{:x}", + XFEATURE_MASK_EXTEND, xstate_bv, xstate + ); + + while xstate_bv != 0 { + if xstate_bv & 0x1 != 0 { + // SAFETY: call cpuid with valid leaves + let leaf = unsafe { std::arch::x86_64::__cpuid_count(0xd, feature_bit) }; + ret = std::cmp::max(ret, leaf.eax + leaf.ebx); + debug!("xstate 0x{:x} ret 0x{:x} leaf {:x?}", xstate_bv, ret, leaf); + } + + xstate_bv >>= 1; + feature_bit += 1; + } + + ret +} + +#[allow(clippy::too_many_arguments)] +pub fn generate_common_cpuid( + hypervisor: Arc, + topology: Option<(u8, u8, u8)>, + sgx_epc_sections: Option>, + phys_bits: u8, + kvm_hyperv: bool, + #[cfg(feature = "tdx")] tdx_enabled: bool, + vendor_compatible: bool, + arch_compatible: bool, +) -> super::Result> { + let cpuid_patches = vec![ + // Patch tsc deadline timer bit + CpuidPatch { + function: 1, + index: 0, + flags_bit: None, + eax_bit: None, + ebx_bit: None, + ecx_bit: Some(TSC_DEADLINE_TIMER_ECX_BIT), + edx_bit: None, + }, + // Patch hypervisor bit + CpuidPatch { + function: 1, + index: 0, + flags_bit: None, + eax_bit: None, + ebx_bit: None, + ecx_bit: Some(HYPERVISOR_ECX_BIT), + edx_bit: None, + }, + // Enable MTRR feature + CpuidPatch { + function: 1, + index: 0, + flags_bit: None, + eax_bit: None, + ebx_bit: None, + ecx_bit: None, + edx_bit: Some(MTRR_EDX_BIT), + }, + ]; + + // Supported CPUID + let mut cpuid = hypervisor.get_cpuid().map_err(Error::CpuidGetSupported)?; + debug!("cpuid from hypervisor {:x?}", cpuid); + + if vendor_compatible { + let cpuid_compatible = generate_compatible_cpuid(); + debug!("cpuid from vendor compatible {:x?}", cpuid_compatible); + + if !cpuid_compatible.cpuids.is_empty() { + CpuidPatch::intersect_cpuid(&mut cpuid, cpuid_compatible.cpuids); + debug!("cpuid vendor compatible {:x?}", cpuid); + } + } + + CpuidPatch::patch_cpuid(&mut cpuid, cpuid_patches); + + if let Some(t) = topology { + update_cpuid_topology(&mut cpuid, t.0, t.1, t.2); + } + + if let Some(sgx_epc_sections) = sgx_epc_sections { + update_cpuid_sgx(&mut cpuid, sgx_epc_sections)?; + } + + #[cfg(feature = "tdx")] + let tdx_capabilities = if tdx_enabled { + let caps = hypervisor + .tdx_capabilities() + .map_err(Error::TdxCapabilities)?; + info!("TDX capabilities {:#?}", caps); + Some(caps) + } else { + None + }; + + // Update some existing CPUID + for entry in cpuid.as_mut_slice().iter_mut() { + match entry.function { + 0xd => + { + #[cfg(feature = "tdx")] + if let Some(caps) = &tdx_capabilities { + let xcr0_mask: u64 = 0x82ff; + let xss_mask: u64 = !xcr0_mask; + if entry.index == 0 { + entry.eax &= (caps.xfam_fixed0 as u32) & (xcr0_mask as u32); + entry.eax |= (caps.xfam_fixed1 as u32) & (xcr0_mask as u32); + entry.edx &= ((caps.xfam_fixed0 & xcr0_mask) >> 32) as u32; + entry.edx |= ((caps.xfam_fixed1 & xcr0_mask) >> 32) as u32; + } else if entry.index == 1 { + entry.ecx &= (caps.xfam_fixed0 as u32) & (xss_mask as u32); + entry.ecx |= (caps.xfam_fixed1 as u32) & (xss_mask as u32); + entry.edx &= ((caps.xfam_fixed0 & xss_mask) >> 32) as u32; + entry.edx |= ((caps.xfam_fixed1 & xss_mask) >> 32) as u32; + } + } + } + // Set CPU physical bits + 0x8000_0008 => { + entry.eax = (entry.eax & 0xffff_ff00) | (phys_bits as u32 & 0xff); + } + // Disable KVM_FEATURE_ASYNC_PF_INT + // This is required until we find out why the asynchronous page + // fault is generating unexpected behavior when using interrupt + // mechanism. + // TODO: Re-enable KVM_FEATURE_ASYNC_PF_INT (#2277) + 0x4000_0001 => { + entry.eax &= !(1 << KVM_FEATURE_ASYNC_PF_INT_BIT); + + // These features are not supported by TDX + #[cfg(feature = "tdx")] + if tdx_enabled { + entry.eax &= !(1 << KVM_FEATURE_CLOCKSOURCE_BIT + | 1 << KVM_FEATURE_CLOCKSOURCE2_BIT + | 1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT + | 1 << KVM_FEATURE_ASYNC_PF_BIT + | 1 << KVM_FEATURE_ASYNC_PF_VMEXIT_BIT + | 1 << KVM_FEATURE_STEAL_TIME_BIT) + } + } + _ => {} + } + } + + // Copy CPU identification string + for i in 0x8000_0002..=0x8000_0004 { + cpuid.retain(|c| c.function != i); + let leaf = unsafe { std::arch::x86_64::__cpuid(i) }; + cpuid.push(CpuIdEntry { + function: i, + eax: leaf.eax, + ebx: leaf.ebx, + ecx: leaf.ecx, + edx: leaf.edx, + ..Default::default() + }); + } + + if kvm_hyperv { + // Remove conflicting entries + cpuid.retain(|c| c.function != 0x4000_0000); + cpuid.retain(|c| c.function != 0x4000_0001); + // See "Hypervisor Top Level Functional Specification" for details + // Compliance with "Hv#1" requires leaves up to 0x4000_000a + cpuid.push(CpuIdEntry { + function: 0x40000000, + eax: 0x4000000a, // Maximum cpuid leaf + ebx: 0x756e694c, // "Linu" + ecx: 0x564b2078, // "x KV" + edx: 0x7648204d, // "M Hv" + ..Default::default() + }); + cpuid.push(CpuIdEntry { + function: 0x40000001, + eax: 0x31237648, // "Hv#1" + ..Default::default() + }); + cpuid.push(CpuIdEntry { + function: 0x40000002, + eax: 0x3839, // "Build number" + ebx: 0xa0000, // "Version" + ..Default::default() + }); + cpuid.push(CpuIdEntry { + function: 0x4000_0003, + eax: 1 << 1 // AccessPartitionReferenceCounter + | 1 << 2 // AccessSynicRegs + | 1 << 3 // AccessSyntheticTimerRegs + | 1 << 9, // AccessPartitionReferenceTsc + edx: 1 << 3, // CPU dynamic partitioning + ..Default::default() + }); + cpuid.push(CpuIdEntry { + function: 0x4000_0004, + eax: 1 << 5, // Recommend relaxed timing + ..Default::default() + }); + for i in 0x4000_0005..=0x4000_000a { + cpuid.push(CpuIdEntry { + function: i, + ..Default::default() + }); + } + } + + if arch_compatible { + cpuid_filter::apply_compatible_template(&mut cpuid); + debug!("cpuid arch compatible {:x?}", cpuid); + + for entry in cpuid.as_mut_slice().iter_mut() { + if 0xd == entry.function && entry.index == 0 { + entry.ecx = xstate_required_size(entry.eax); + debug!("ecx {:x}", entry.ecx); + } + } + } + + Ok(cpuid) +} + +pub fn configure_vcpu( + vcpu: &Arc, + id: u8, + kernel_entry_point: Option, + vm_memory: &GuestMemoryAtomic, + cpuid: Vec, + kvm_hyperv: bool, +) -> super::Result<()> { + // Per vCPU CPUID changes; common are handled via generate_common_cpuid() + let mut cpuid = cpuid; + CpuidPatch::set_cpuid_reg(&mut cpuid, 0xb, None, CpuidReg::EDX, u32::from(id)); + CpuidPatch::set_cpuid_reg(&mut cpuid, 0x1f, None, CpuidReg::EDX, u32::from(id)); + + // Set ApicId in cpuid for each vcpu + // SAFETY: get host cpuid when eax=1 + let mut cpu_ebx = unsafe { core::arch::x86_64::__cpuid(1) }.ebx; + cpu_ebx &= 0xffffff; + cpu_ebx |= (id as u32) << 24; + CpuidPatch::set_cpuid_reg(&mut cpuid, 0x1, None, CpuidReg::EBX, cpu_ebx); + + vcpu.set_cpuid2(&cpuid) + .map_err(|e| Error::SetSupportedCpusFailed(e.into()))?; + + if kvm_hyperv { + vcpu.enable_hyperv_synic().unwrap(); + } + + regs::setup_msrs(vcpu).map_err(Error::MsrsConfiguration)?; + if let Some(kernel_entry_point) = kernel_entry_point { + if let Some(entry_addr) = kernel_entry_point.entry_addr { + // Safe to unwrap because this method is called after the VM is configured + regs::setup_regs(vcpu, entry_addr.raw_value()).map_err(Error::RegsConfiguration)?; + regs::setup_fpu(vcpu).map_err(Error::FpuConfiguration)?; + regs::setup_sregs(&vm_memory.memory(), vcpu).map_err(Error::SregsConfiguration)?; + } + } + interrupts::set_lint(vcpu).map_err(|e| Error::LocalIntConfiguration(e.into()))?; + Ok(()) +} + +/// Returns a Vec of the valid memory addresses. +/// These should be used to configure the GuestMemory structure for the platform. +/// For x86_64 all addresses are valid from the start of the kernel except a +/// carve out at the end of 32bit address space. +pub fn arch_memory_regions(size: GuestUsize) -> Vec<(GuestAddress, usize, RegionType)> { + let reserved_memory_gap_start = layout::MEM_32BIT_RESERVED_START + .checked_add(layout::MEM_32BIT_DEVICES_SIZE) + .expect("32-bit reserved region is too large"); + + let requested_memory_size = GuestAddress(size); + let mut regions = Vec::new(); + + // case1: guest memory fits before the gap + if size <= layout::MEM_32BIT_RESERVED_START.raw_value() { + regions.push((GuestAddress(0), size as usize, RegionType::Ram)); + // case2: guest memory extends beyond the gap + } else { + // push memory before the gap + regions.push(( + GuestAddress(0), + layout::MEM_32BIT_RESERVED_START.raw_value() as usize, + RegionType::Ram, + )); + regions.push(( + layout::RAM_64BIT_START, + requested_memory_size.unchecked_offset_from(layout::MEM_32BIT_RESERVED_START) as usize, + RegionType::Ram, + )); + } + + // Add the 32-bit device memory hole as a sub region. + regions.push(( + layout::MEM_32BIT_RESERVED_START, + layout::MEM_32BIT_DEVICES_SIZE as usize, + RegionType::SubRegion, + )); + + // Add the 32-bit reserved memory hole as a sub region. + regions.push(( + reserved_memory_gap_start, + (layout::MEM_32BIT_RESERVED_SIZE - layout::MEM_32BIT_DEVICES_SIZE) as usize, + RegionType::Reserved, + )); + + regions +} + +/// Configures the system and should be called once per vm before starting vcpu threads. +/// +/// # Arguments +/// +/// * `guest_mem` - The memory to be used by the guest. +/// * `cmdline_addr` - Address in `guest_mem` where the kernel command line was loaded. +/// * `cmdline_size` - Size of the kernel command line in bytes including the null terminator. +/// * `num_cpus` - Number of virtual CPUs the guest will have. +#[allow(clippy::too_many_arguments)] +pub fn configure_system( + guest_mem: &GuestMemoryMmap, + cmdline_addr: GuestAddress, + initramfs: &Option, + _num_cpus: u8, + rsdp_addr: Option, + sgx_epc_region: Option, + serial_number: Option<&str>, + uuid: Option<&str>, + oem_strings: Option<&[&str]>, +) -> super::Result<()> { + // Write EBDA address to location where ACPICA expects to find it + guest_mem + .write_obj((layout::EBDA_START.0 >> 4) as u16, layout::EBDA_POINTER) + .map_err(Error::EbdaSetup)?; + + let size = smbios::setup_smbios(guest_mem, serial_number, uuid, oem_strings) + .map_err(Error::SmbiosSetup)?; + + // Place the MP table after the SMIOS table aligned to 16 bytes + let offset = GuestAddress(layout::SMBIOS_START).unchecked_add(size); + let offset = GuestAddress((offset.0 + 16) & !0xf); + mptable::setup_mptable(offset, guest_mem, _num_cpus).map_err(Error::MpTableSetup)?; + + // Check that the RAM is not smaller than the RSDP start address + if let Some(rsdp_addr) = rsdp_addr { + if rsdp_addr.0 > guest_mem.last_addr().0 { + return Err(super::Error::RsdpPastRamEnd); + } + } + + configure_pvh( + guest_mem, + cmdline_addr, + initramfs, + rsdp_addr, + sgx_epc_region, + ) +} + +fn configure_pvh( + guest_mem: &GuestMemoryMmap, + cmdline_addr: GuestAddress, + initramfs: &Option, + rsdp_addr: Option, + sgx_epc_region: Option, +) -> super::Result<()> { + const XEN_HVM_START_MAGIC_VALUE: u32 = 0x336ec578; + + let mut start_info = hvm_start_info { + magic: XEN_HVM_START_MAGIC_VALUE, + version: 1, // pvh has version 1 + nr_modules: 0, + cmdline_paddr: cmdline_addr.raw_value(), + memmap_paddr: layout::MEMMAP_START.raw_value(), + ..Default::default() + }; + + if let Some(rsdp_addr) = rsdp_addr { + start_info.rsdp_paddr = rsdp_addr.0; + } + + if let Some(initramfs_config) = initramfs { + // The initramfs has been written to guest memory already, here we just need to + // create the module structure that describes it. + let ramdisk_mod = hvm_modlist_entry { + paddr: initramfs_config.address.raw_value(), + size: initramfs_config.size as u64, + ..Default::default() + }; + + start_info.nr_modules += 1; + start_info.modlist_paddr = layout::MODLIST_START.raw_value(); + + // Write the modlist struct to guest memory. + guest_mem + .write_obj(ramdisk_mod, layout::MODLIST_START) + .map_err(super::Error::ModlistSetup)?; + } + + // Vector to hold the memory maps which needs to be written to guest memory + // at MEMMAP_START after all of the mappings are recorded. + let mut memmap: Vec = Vec::new(); + + // Create the memory map entries. + add_memmap_entry(&mut memmap, 0, layout::EBDA_START.raw_value(), E820_RAM); + + let mem_end = guest_mem.last_addr(); + + if mem_end < layout::MEM_32BIT_RESERVED_START { + add_memmap_entry( + &mut memmap, + layout::HIGH_RAM_START.raw_value(), + mem_end.unchecked_offset_from(layout::HIGH_RAM_START) + 1, + E820_RAM, + ); + } else { + add_memmap_entry( + &mut memmap, + layout::HIGH_RAM_START.raw_value(), + layout::MEM_32BIT_RESERVED_START.unchecked_offset_from(layout::HIGH_RAM_START), + E820_RAM, + ); + if mem_end > layout::RAM_64BIT_START { + add_memmap_entry( + &mut memmap, + layout::RAM_64BIT_START.raw_value(), + mem_end.unchecked_offset_from(layout::RAM_64BIT_START) + 1, + E820_RAM, + ); + } + } + + add_memmap_entry( + &mut memmap, + layout::PCI_MMCONFIG_START.0, + layout::PCI_MMCONFIG_SIZE, + E820_RESERVED, + ); + + if let Some(sgx_epc_region) = sgx_epc_region { + add_memmap_entry( + &mut memmap, + sgx_epc_region.start().raw_value(), + sgx_epc_region.size(), + E820_RESERVED, + ); + } + + start_info.memmap_entries = memmap.len() as u32; + + // Copy the vector with the memmap table to the MEMMAP_START address + // which is already saved in the memmap_paddr field of hvm_start_info struct. + let mut memmap_start_addr = layout::MEMMAP_START; + + guest_mem + .checked_offset( + memmap_start_addr, + mem::size_of::() * start_info.memmap_entries as usize, + ) + .ok_or(super::Error::MemmapTablePastRamEnd)?; + + // For every entry in the memmap vector, write it to guest memory. + for memmap_entry in memmap { + guest_mem + .write_obj(memmap_entry, memmap_start_addr) + .map_err(|_| super::Error::MemmapTableSetup)?; + memmap_start_addr = + memmap_start_addr.unchecked_add(mem::size_of::() as u64); + } + + // The hvm_start_info struct itself must be stored at PVH_START_INFO + // address, and %rbx will be initialized to contain PVH_INFO_START prior to + // starting the guest, as required by the PVH ABI. + let start_info_addr = layout::PVH_INFO_START; + + guest_mem + .checked_offset(start_info_addr, mem::size_of::()) + .ok_or(super::Error::StartInfoPastRamEnd)?; + + // Write the start_info struct to guest memory. + guest_mem + .write_obj(start_info, start_info_addr) + .map_err(|_| super::Error::StartInfoSetup)?; + + Ok(()) +} + +fn add_memmap_entry(memmap: &mut Vec, addr: u64, size: u64, mem_type: u32) { + // Add the table entry to the vector + memmap.push(hvm_memmap_table_entry { + addr, + size, + type_: mem_type, + reserved: 0, + }); +} + +/// Returns the memory address where the initramfs could be loaded. +pub fn initramfs_load_addr( + guest_mem: &GuestMemoryMmap, + initramfs_size: usize, +) -> super::Result { + let first_region = guest_mem + .find_region(GuestAddress::new(0)) + .ok_or(super::Error::InitramfsAddress)?; + // It's safe to cast to usize because the size of a region can't be greater than usize. + let lowmem_size = first_region.len() as usize; + + if lowmem_size < initramfs_size { + return Err(super::Error::InitramfsAddress); + } + + let aligned_addr: u64 = ((lowmem_size - initramfs_size) & !(crate::pagesize() - 1)) as u64; + Ok(aligned_addr) +} + +pub fn get_host_cpu_phys_bits() -> u8 { + unsafe { + let leaf = x86_64::__cpuid(0x8000_0000); + + // Detect and handle AMD SME (Secure Memory Encryption) properly. + // Some physical address bits may become reserved when the feature is enabled. + // See AMD64 Architecture Programmer's Manual Volume 2, Section 7.10.1 + let reduced = if leaf.eax >= 0x8000_001f + && leaf.ebx == 0x6874_7541 // Vendor ID: AuthenticAMD + && leaf.ecx == 0x444d_4163 + && leaf.edx == 0x6974_6e65 + && x86_64::__cpuid(0x8000_001f).eax & 0x1 != 0 + { + (x86_64::__cpuid(0x8000_001f).ebx >> 6) & 0x3f + } else { + 0 + }; + + if leaf.eax >= 0x8000_0008 { + let leaf = x86_64::__cpuid(0x8000_0008); + ((leaf.eax & 0xff) - reduced) as u8 + } else { + 36 + } + } +} + +fn update_cpuid_topology( + cpuid: &mut Vec, + threads_per_core: u8, + cores_per_die: u8, + dies_per_package: u8, +) { + let thread_width = 8 - (threads_per_core - 1).leading_zeros(); + let core_width = (8 - (cores_per_die - 1).leading_zeros()) + thread_width; + let die_width = (8 - (dies_per_package - 1).leading_zeros()) + core_width; + + // CPU Topology leaf 0xb + CpuidPatch::set_cpuid_reg(cpuid, 0xb, Some(0), CpuidReg::EAX, thread_width); + CpuidPatch::set_cpuid_reg( + cpuid, + 0xb, + Some(0), + CpuidReg::EBX, + u32::from(threads_per_core), + ); + CpuidPatch::set_cpuid_reg(cpuid, 0xb, Some(0), CpuidReg::ECX, 1 << 8); + + CpuidPatch::set_cpuid_reg(cpuid, 0xb, Some(1), CpuidReg::EAX, die_width); + CpuidPatch::set_cpuid_reg( + cpuid, + 0xb, + Some(1), + CpuidReg::EBX, + u32::from(dies_per_package * cores_per_die * threads_per_core), + ); + CpuidPatch::set_cpuid_reg(cpuid, 0xb, Some(1), CpuidReg::ECX, 2 << 8); + + // CPU Topology leaf 0x1f + CpuidPatch::set_cpuid_reg(cpuid, 0x1f, Some(0), CpuidReg::EAX, thread_width); + CpuidPatch::set_cpuid_reg( + cpuid, + 0x1f, + Some(0), + CpuidReg::EBX, + u32::from(threads_per_core), + ); + CpuidPatch::set_cpuid_reg(cpuid, 0x1f, Some(0), CpuidReg::ECX, 1 << 8); + + CpuidPatch::set_cpuid_reg(cpuid, 0x1f, Some(1), CpuidReg::EAX, core_width); + CpuidPatch::set_cpuid_reg( + cpuid, + 0x1f, + Some(1), + CpuidReg::EBX, + u32::from(cores_per_die * threads_per_core), + ); + CpuidPatch::set_cpuid_reg(cpuid, 0x1f, Some(1), CpuidReg::ECX, 2 << 8); + + CpuidPatch::set_cpuid_reg(cpuid, 0x1f, Some(2), CpuidReg::EAX, die_width); + CpuidPatch::set_cpuid_reg( + cpuid, + 0x1f, + Some(2), + CpuidReg::EBX, + u32::from(dies_per_package * cores_per_die * threads_per_core), + ); + CpuidPatch::set_cpuid_reg(cpuid, 0x1f, Some(2), CpuidReg::ECX, 5 << 8); +} + +// The goal is to update the CPUID sub-leaves to reflect the number of EPC +// sections exposed to the guest. +fn update_cpuid_sgx( + cpuid: &mut Vec, + epc_sections: Vec, +) -> Result<(), Error> { + // Something's wrong if there's no EPC section. + if epc_sections.is_empty() { + return Err(Error::NoSgxEpcSection); + } + // We can't go further if the hypervisor does not support SGX feature. + if !CpuidPatch::is_feature_enabled(cpuid, 0x7, 0, CpuidReg::EBX, 2) { + return Err(Error::MissingSgxFeature); + } + // We can't go further if the hypervisor does not support SGX_LC feature. + if !CpuidPatch::is_feature_enabled(cpuid, 0x7, 0, CpuidReg::ECX, 30) { + return Err(Error::MissingSgxLaunchControlFeature); + } + + // Get host CPUID for leaf 0x12, subleaf 0x2. This is to retrieve EPC + // properties such as confidentiality and integrity. + let leaf = unsafe { std::arch::x86_64::__cpuid_count(0x12, 0x2) }; + + for (i, epc_section) in epc_sections.iter().enumerate() { + let subleaf_idx = i + 2; + let start = epc_section.start().raw_value(); + let size = epc_section.size(); + let eax = (start & 0xffff_f000) as u32 | 0x1; + let ebx = (start >> 32) as u32; + let ecx = (size & 0xffff_f000) as u32 | (leaf.ecx & 0xf); + let edx = (size >> 32) as u32; + // CPU Topology leaf 0x12 + CpuidPatch::set_cpuid_reg(cpuid, 0x12, Some(subleaf_idx as u32), CpuidReg::EAX, eax); + CpuidPatch::set_cpuid_reg(cpuid, 0x12, Some(subleaf_idx as u32), CpuidReg::EBX, ebx); + CpuidPatch::set_cpuid_reg(cpuid, 0x12, Some(subleaf_idx as u32), CpuidReg::ECX, ecx); + CpuidPatch::set_cpuid_reg(cpuid, 0x12, Some(subleaf_idx as u32), CpuidReg::EDX, edx); + } + + // Add one NULL entry to terminate the dynamic list + let subleaf_idx = epc_sections.len() + 2; + // CPU Topology leaf 0x12 + CpuidPatch::set_cpuid_reg(cpuid, 0x12, Some(subleaf_idx as u32), CpuidReg::EAX, 0); + CpuidPatch::set_cpuid_reg(cpuid, 0x12, Some(subleaf_idx as u32), CpuidReg::EBX, 0); + CpuidPatch::set_cpuid_reg(cpuid, 0x12, Some(subleaf_idx as u32), CpuidReg::ECX, 0); + CpuidPatch::set_cpuid_reg(cpuid, 0x12, Some(subleaf_idx as u32), CpuidReg::EDX, 0); + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn regions_lt_4gb() { + let regions = arch_memory_regions(1 << 29); + assert_eq!(3, regions.len()); + assert_eq!(GuestAddress(0), regions[0].0); + assert_eq!(1usize << 29, regions[0].1); + } + + #[test] + fn regions_gt_4gb() { + let regions = arch_memory_regions((1 << 32) + 0x8000); + assert_eq!(4, regions.len()); + assert_eq!(GuestAddress(0), regions[0].0); + assert_eq!(GuestAddress(1 << 32), regions[1].0); + } + + #[test] + fn test_system_configuration() { + let no_vcpus = 4; + let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 0x10000)]).unwrap(); + let config_err = configure_system( + &gm, + GuestAddress(0), + &None, + 1, + Some(layout::RSDP_POINTER), + None, + None, + None, + None, + ); + assert!(config_err.is_err()); + + // Now assigning some memory that falls before the 32bit memory hole. + let mem_size = 128 << 20; + let arch_mem_regions = arch_memory_regions(mem_size); + let ram_regions: Vec<(GuestAddress, usize)> = arch_mem_regions + .iter() + .filter(|r| r.2 == RegionType::Ram) + .map(|r| (r.0, r.1)) + .collect(); + let gm = GuestMemoryMmap::from_ranges(&ram_regions).unwrap(); + + configure_system( + &gm, + GuestAddress(0), + &None, + no_vcpus, + None, + None, + None, + None, + None, + ) + .unwrap(); + + // Now assigning some memory that is equal to the start of the 32bit memory hole. + let mem_size = 3328 << 20; + let arch_mem_regions = arch_memory_regions(mem_size); + let ram_regions: Vec<(GuestAddress, usize)> = arch_mem_regions + .iter() + .filter(|r| r.2 == RegionType::Ram) + .map(|r| (r.0, r.1)) + .collect(); + let gm = GuestMemoryMmap::from_ranges(&ram_regions).unwrap(); + configure_system( + &gm, + GuestAddress(0), + &None, + no_vcpus, + None, + None, + None, + None, + None, + ) + .unwrap(); + + configure_system( + &gm, + GuestAddress(0), + &None, + no_vcpus, + None, + None, + None, + None, + None, + ) + .unwrap(); + + // Now assigning some memory that falls after the 32bit memory hole. + let mem_size = 3330 << 20; + let arch_mem_regions = arch_memory_regions(mem_size); + let ram_regions: Vec<(GuestAddress, usize)> = arch_mem_regions + .iter() + .filter(|r| r.2 == RegionType::Ram) + .map(|r| (r.0, r.1)) + .collect(); + let gm = GuestMemoryMmap::from_ranges(&ram_regions).unwrap(); + configure_system( + &gm, + GuestAddress(0), + &None, + no_vcpus, + None, + None, + None, + None, + None, + ) + .unwrap(); + + configure_system( + &gm, + GuestAddress(0), + &None, + no_vcpus, + None, + None, + None, + None, + None, + ) + .unwrap(); + } + + #[test] + fn test_add_memmap_entry() { + let mut memmap: Vec = Vec::new(); + + let expected_memmap = vec![ + hvm_memmap_table_entry { + addr: 0x0, + size: 0x1000, + type_: E820_RAM, + ..Default::default() + }, + hvm_memmap_table_entry { + addr: 0x10000, + size: 0xa000, + type_: E820_RESERVED, + ..Default::default() + }, + ]; + + add_memmap_entry(&mut memmap, 0, 0x1000, E820_RAM); + add_memmap_entry(&mut memmap, 0x10000, 0xa000, E820_RESERVED); + + assert_eq!(format!("{:?}", memmap), format!("{:?}", expected_memmap)); + } +} diff --git a/vendor/cube-hypervisor/arch/src/x86_64/mpspec.rs b/vendor/cube-hypervisor/arch/src/x86_64/mpspec.rs new file mode 100644 index 0000000..8006a3f --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/x86_64/mpspec.rs @@ -0,0 +1,112 @@ +// Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +pub const MP_PROCESSOR: ::std::os::raw::c_uint = 0; +pub const MP_BUS: ::std::os::raw::c_uint = 1; +pub const MP_IOAPIC: ::std::os::raw::c_uint = 2; +pub const MP_INTSRC: ::std::os::raw::c_uint = 3; +pub const MP_LINTSRC: ::std::os::raw::c_uint = 4; +pub const CPU_ENABLED: ::std::os::raw::c_uint = 1; +pub const CPU_BOOTPROCESSOR: ::std::os::raw::c_uint = 2; +pub const MPC_APIC_USABLE: ::std::os::raw::c_uint = 1; +pub const MP_IRQDIR_DEFAULT: ::std::os::raw::c_uint = 0; + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct mpf_intel { + pub signature: [::std::os::raw::c_char; 4usize], + pub physptr: ::std::os::raw::c_uint, + pub length: ::std::os::raw::c_uchar, + pub specification: ::std::os::raw::c_uchar, + pub checksum: ::std::os::raw::c_uchar, + pub feature1: ::std::os::raw::c_uchar, + pub feature2: ::std::os::raw::c_uchar, + pub feature3: ::std::os::raw::c_uchar, + pub feature4: ::std::os::raw::c_uchar, + pub feature5: ::std::os::raw::c_uchar, +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct mpc_table { + pub signature: [::std::os::raw::c_char; 4usize], + pub length: ::std::os::raw::c_ushort, + pub spec: ::std::os::raw::c_char, + pub checksum: ::std::os::raw::c_char, + pub oem: [::std::os::raw::c_char; 8usize], + pub productid: [::std::os::raw::c_char; 12usize], + pub oemptr: ::std::os::raw::c_uint, + pub oemsize: ::std::os::raw::c_ushort, + pub oemcount: ::std::os::raw::c_ushort, + pub lapic: ::std::os::raw::c_uint, + pub reserved: ::std::os::raw::c_uint, +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct mpc_cpu { + pub type_: ::std::os::raw::c_uchar, + pub apicid: ::std::os::raw::c_uchar, + pub apicver: ::std::os::raw::c_uchar, + pub cpuflag: ::std::os::raw::c_uchar, + pub cpufeature: ::std::os::raw::c_uint, + pub featureflag: ::std::os::raw::c_uint, + pub reserved: [::std::os::raw::c_uint; 2usize], +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct mpc_bus { + pub type_: ::std::os::raw::c_uchar, + pub busid: ::std::os::raw::c_uchar, + pub bustype: [::std::os::raw::c_uchar; 6usize], +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct mpc_ioapic { + pub type_: ::std::os::raw::c_uchar, + pub apicid: ::std::os::raw::c_uchar, + pub apicver: ::std::os::raw::c_uchar, + pub flags: ::std::os::raw::c_uchar, + pub apicaddr: ::std::os::raw::c_uint, +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct mpc_intsrc { + pub type_: ::std::os::raw::c_uchar, + pub irqtype: ::std::os::raw::c_uchar, + pub irqflag: ::std::os::raw::c_ushort, + pub srcbus: ::std::os::raw::c_uchar, + pub srcbusirq: ::std::os::raw::c_uchar, + pub dstapic: ::std::os::raw::c_uchar, + pub dstirq: ::std::os::raw::c_uchar, +} + +pub const MP_IRQ_SOURCE_TYPES_MP_INT: ::std::os::raw::c_uint = 0; +pub const MP_IRQ_SOURCE_TYPES_MP_NMI: ::std::os::raw::c_uint = 1; +pub const MP_IRQ_SOURCE_TYPES_MP_EXT_INT: ::std::os::raw::c_uint = 3; + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct mpc_lintsrc { + pub type_: ::std::os::raw::c_uchar, + pub irqtype: ::std::os::raw::c_uchar, + pub irqflag: ::std::os::raw::c_ushort, + pub srcbusid: ::std::os::raw::c_uchar, + pub srcbusirq: ::std::os::raw::c_uchar, + pub destapic: ::std::os::raw::c_uchar, + pub destapiclint: ::std::os::raw::c_uchar, +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct mpc_oemtable { + pub signature: [::std::os::raw::c_char; 4usize], + pub length: ::std::os::raw::c_ushort, + pub rev: ::std::os::raw::c_char, + pub checksum: ::std::os::raw::c_char, + pub mpc: [::std::os::raw::c_char; 8usize], +} diff --git a/vendor/cube-hypervisor/arch/src/x86_64/mptable.rs b/vendor/cube-hypervisor/arch/src/x86_64/mptable.rs new file mode 100644 index 0000000..3e255c1 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/x86_64/mptable.rs @@ -0,0 +1,414 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use crate::layout::{APIC_START, HIGH_RAM_START, IOAPIC_START}; +use crate::x86_64::mpspec; +use crate::GuestMemoryMmap; +use libc::c_char; +use std::mem; +use std::result; +use std::slice; +use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError}; + +// This is a workaround to the Rust enforcement specifying that any implementation of a foreign +// trait (in this case `ByteValued`) where: +// * the type that is implementing the trait is foreign or +// * all of the parameters being passed to the trait (if there are any) are also foreign +// is prohibited. +#[derive(Copy, Clone, Default)] +struct MpcBusWrapper(mpspec::mpc_bus); +#[derive(Copy, Clone, Default)] +struct MpcCpuWrapper(mpspec::mpc_cpu); +#[derive(Copy, Clone, Default)] +struct MpcIntsrcWrapper(mpspec::mpc_intsrc); +#[derive(Copy, Clone, Default)] +struct MpcIoapicWrapper(mpspec::mpc_ioapic); +#[derive(Copy, Clone, Default)] +struct MpcTableWrapper(mpspec::mpc_table); +#[derive(Copy, Clone, Default)] +struct MpcLintsrcWrapper(mpspec::mpc_lintsrc); +#[derive(Copy, Clone, Default)] +struct MpfIntelWrapper(mpspec::mpf_intel); + +// SAFETY: These `mpspec` wrapper types are only data, reading them from data is a safe initialization. +unsafe impl ByteValued for MpcBusWrapper {} +unsafe impl ByteValued for MpcCpuWrapper {} +unsafe impl ByteValued for MpcIntsrcWrapper {} +unsafe impl ByteValued for MpcIoapicWrapper {} +unsafe impl ByteValued for MpcTableWrapper {} +unsafe impl ByteValued for MpcLintsrcWrapper {} +unsafe impl ByteValued for MpfIntelWrapper {} + +#[derive(Debug)] +pub enum Error { + /// There was too little guest memory to store the entire MP table. + NotEnoughMemory, + /// The MP table has too little address space to be stored. + AddressOverflow, + /// Failure while zeroing out the memory for the MP table. + Clear(GuestMemoryError), + /// Number of CPUs exceeds the maximum supported CPUs + TooManyCpus, + /// Failure to write the MP floating pointer. + WriteMpfIntel(GuestMemoryError), + /// Failure to write MP CPU entry. + WriteMpcCpu(GuestMemoryError), + /// Failure to write MP ioapic entry. + WriteMpcIoapic(GuestMemoryError), + /// Failure to write MP bus entry. + WriteMpcBus(GuestMemoryError), + /// Failure to write MP interrupt source entry. + WriteMpcIntsrc(GuestMemoryError), + /// Failure to write MP local interrupt source entry. + WriteMpcLintsrc(GuestMemoryError), + /// Failure to write MP table header. + WriteMpcTable(GuestMemoryError), +} + +pub type Result = result::Result; + +// With APIC/xAPIC, there are only 255 APIC IDs available. And IOAPIC occupies +// one APIC ID, so only 254 CPUs at maximum may be supported. Actually it's +// a large number for FC usecases. +pub const MAX_SUPPORTED_CPUS: u32 = 254; + +// Convenience macro for making arrays of diverse character types. +macro_rules! char_array { + ($t:ty; $( $c:expr ),*) => ( [ $( $c as $t ),* ] ) +} + +// Most of these variables are sourced from the Intel MP Spec 1.4. +const SMP_MAGIC_IDENT: [c_char; 4] = char_array!(c_char; '_', 'M', 'P', '_'); +const MPC_SIGNATURE: [c_char; 4] = char_array!(c_char; 'P', 'C', 'M', 'P'); +const MPC_SPEC: i8 = 4; +const MPC_OEM: [c_char; 8] = char_array!(c_char; 'F', 'C', ' ', ' ', ' ', ' ', ' ', ' '); +const MPC_PRODUCT_ID: [c_char; 12] = ['0' as c_char; 12]; +const BUS_TYPE_ISA: [u8; 6] = char_array!(u8; 'I', 'S', 'A', ' ', ' ', ' '); +const APIC_VERSION: u8 = 0x14; +const CPU_STEPPING: u32 = 0x600; +const CPU_FEATURE_APIC: u32 = 0x200; +const CPU_FEATURE_FPU: u32 = 0x001; + +fn compute_checksum(v: &T) -> u8 { + // Safe because we are only reading the bytes within the size of the `T` reference `v`. + let v_slice = unsafe { slice::from_raw_parts(v as *const T as *const u8, mem::size_of::()) }; + let mut checksum: u8 = 0; + for i in v_slice.iter() { + checksum = checksum.wrapping_add(*i); + } + checksum +} + +fn mpf_intel_compute_checksum(v: &mpspec::mpf_intel) -> u8 { + let checksum = compute_checksum(v).wrapping_sub(v.checksum); + (!checksum).wrapping_add(1) +} + +fn compute_mp_size(num_cpus: u8) -> usize { + mem::size_of::() + + mem::size_of::() + + mem::size_of::() * (num_cpus as usize) + + mem::size_of::() + + mem::size_of::() + + mem::size_of::() * 16 + + mem::size_of::() * 2 +} + +/// Performs setup of the MP table for the given `num_cpus`. +pub fn setup_mptable(offset: GuestAddress, mem: &GuestMemoryMmap, num_cpus: u8) -> Result<()> { + if num_cpus as u32 > MAX_SUPPORTED_CPUS { + return Err(Error::TooManyCpus); + } + + // Used to keep track of the next base pointer into the MP table. + let mut base_mp = offset; + + let mp_size = compute_mp_size(num_cpus); + + if offset.unchecked_add(mp_size as u64) >= HIGH_RAM_START { + warn!("Skipping mptable creation due to insufficient space"); + return Ok(()); + } + + let mut checksum: u8 = 0; + let ioapicid: u8 = num_cpus + 1; + + // The checked_add here ensures the all of the following base_mp.unchecked_add's will be without + // overflow. + if let Some(end_mp) = base_mp.checked_add((mp_size - 1) as u64) { + if !mem.address_in_range(end_mp) { + return Err(Error::NotEnoughMemory); + } + } else { + return Err(Error::AddressOverflow); + } + + mem.read_exact_volatile_from(base_mp, &mut vec![0; mp_size].as_slice(), mp_size) + .map_err(Error::Clear)?; + + { + let mut mpf_intel = MpfIntelWrapper(mpspec::mpf_intel::default()); + let size = mem::size_of::() as u64; + mpf_intel.0.signature = SMP_MAGIC_IDENT; + mpf_intel.0.length = 1; + mpf_intel.0.specification = 4; + mpf_intel.0.physptr = (base_mp.raw_value() + size) as u32; + mpf_intel.0.checksum = mpf_intel_compute_checksum(&mpf_intel.0); + mem.write_obj(mpf_intel, base_mp) + .map_err(Error::WriteMpfIntel)?; + base_mp = base_mp.unchecked_add(size); + } + + // We set the location of the mpc_table here but we can't fill it out until we have the length + // of the entire table later. + let table_base = base_mp; + base_mp = base_mp.unchecked_add(mem::size_of::() as u64); + + { + let size = mem::size_of::(); + for cpu_id in 0..num_cpus { + let mut mpc_cpu = MpcCpuWrapper(mpspec::mpc_cpu::default()); + mpc_cpu.0.type_ = mpspec::MP_PROCESSOR as u8; + mpc_cpu.0.apicid = cpu_id; + mpc_cpu.0.apicver = APIC_VERSION; + mpc_cpu.0.cpuflag = mpspec::CPU_ENABLED as u8 + | if cpu_id == 0 { + mpspec::CPU_BOOTPROCESSOR as u8 + } else { + 0 + }; + mpc_cpu.0.cpufeature = CPU_STEPPING; + mpc_cpu.0.featureflag = CPU_FEATURE_APIC | CPU_FEATURE_FPU; + mem.write_obj(mpc_cpu, base_mp) + .map_err(Error::WriteMpcCpu)?; + base_mp = base_mp.unchecked_add(size as u64); + checksum = checksum.wrapping_add(compute_checksum(&mpc_cpu.0)); + } + } + { + let size = mem::size_of::(); + let mut mpc_bus = MpcBusWrapper(mpspec::mpc_bus::default()); + mpc_bus.0.type_ = mpspec::MP_BUS as u8; + mpc_bus.0.busid = 0; + mpc_bus.0.bustype = BUS_TYPE_ISA; + mem.write_obj(mpc_bus, base_mp) + .map_err(Error::WriteMpcBus)?; + base_mp = base_mp.unchecked_add(size as u64); + checksum = checksum.wrapping_add(compute_checksum(&mpc_bus.0)); + } + { + let size = mem::size_of::(); + let mut mpc_ioapic = MpcIoapicWrapper(mpspec::mpc_ioapic::default()); + mpc_ioapic.0.type_ = mpspec::MP_IOAPIC as u8; + mpc_ioapic.0.apicid = ioapicid; + mpc_ioapic.0.apicver = APIC_VERSION; + mpc_ioapic.0.flags = mpspec::MPC_APIC_USABLE as u8; + mpc_ioapic.0.apicaddr = IOAPIC_START.0 as u32; + mem.write_obj(mpc_ioapic, base_mp) + .map_err(Error::WriteMpcIoapic)?; + base_mp = base_mp.unchecked_add(size as u64); + checksum = checksum.wrapping_add(compute_checksum(&mpc_ioapic.0)); + } + // Per kvm_setup_default_irq_routing() in kernel + for i in 0..16 { + let size = mem::size_of::(); + let mut mpc_intsrc = MpcIntsrcWrapper(mpspec::mpc_intsrc::default()); + mpc_intsrc.0.type_ = mpspec::MP_INTSRC as u8; + mpc_intsrc.0.irqtype = mpspec::MP_IRQ_SOURCE_TYPES_MP_INT as u8; + mpc_intsrc.0.irqflag = mpspec::MP_IRQDIR_DEFAULT as u16; + mpc_intsrc.0.srcbus = 0; + mpc_intsrc.0.srcbusirq = i; + mpc_intsrc.0.dstapic = ioapicid; + mpc_intsrc.0.dstirq = i; + mem.write_obj(mpc_intsrc, base_mp) + .map_err(Error::WriteMpcIntsrc)?; + base_mp = base_mp.unchecked_add(size as u64); + checksum = checksum.wrapping_add(compute_checksum(&mpc_intsrc.0)); + } + { + let size = mem::size_of::(); + let mut mpc_lintsrc = MpcLintsrcWrapper(mpspec::mpc_lintsrc::default()); + mpc_lintsrc.0.type_ = mpspec::MP_LINTSRC as u8; + mpc_lintsrc.0.irqtype = mpspec::MP_IRQ_SOURCE_TYPES_MP_EXT_INT as u8; + mpc_lintsrc.0.irqflag = mpspec::MP_IRQDIR_DEFAULT as u16; + mpc_lintsrc.0.srcbusid = 0; + mpc_lintsrc.0.srcbusirq = 0; + mpc_lintsrc.0.destapic = 0; + mpc_lintsrc.0.destapiclint = 0; + mem.write_obj(mpc_lintsrc, base_mp) + .map_err(Error::WriteMpcLintsrc)?; + base_mp = base_mp.unchecked_add(size as u64); + checksum = checksum.wrapping_add(compute_checksum(&mpc_lintsrc.0)); + } + { + let size = mem::size_of::(); + let mut mpc_lintsrc = MpcLintsrcWrapper(mpspec::mpc_lintsrc::default()); + mpc_lintsrc.0.type_ = mpspec::MP_LINTSRC as u8; + mpc_lintsrc.0.irqtype = mpspec::MP_IRQ_SOURCE_TYPES_MP_NMI as u8; + mpc_lintsrc.0.irqflag = mpspec::MP_IRQDIR_DEFAULT as u16; + mpc_lintsrc.0.srcbusid = 0; + mpc_lintsrc.0.srcbusirq = 0; + mpc_lintsrc.0.destapic = 0xFF; /* to all local APICs */ + mpc_lintsrc.0.destapiclint = 1; + mem.write_obj(mpc_lintsrc, base_mp) + .map_err(Error::WriteMpcLintsrc)?; + base_mp = base_mp.unchecked_add(size as u64); + checksum = checksum.wrapping_add(compute_checksum(&mpc_lintsrc.0)); + } + + // At this point we know the size of the mp_table. + let table_end = base_mp; + + { + let mut mpc_table = MpcTableWrapper(mpspec::mpc_table::default()); + mpc_table.0.signature = MPC_SIGNATURE; + mpc_table.0.length = table_end.unchecked_offset_from(table_base) as u16; + mpc_table.0.spec = MPC_SPEC; + mpc_table.0.oem = MPC_OEM; + mpc_table.0.productid = MPC_PRODUCT_ID; + mpc_table.0.lapic = APIC_START.0 as u32; + checksum = checksum.wrapping_add(compute_checksum(&mpc_table.0)); + mpc_table.0.checksum = (!checksum).wrapping_add(1) as i8; + mem.write_obj(mpc_table, table_base) + .map_err(Error::WriteMpcTable)?; + } + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::layout::MPTABLE_START; + use vm_memory::bitmap::BitmapSlice; + use vm_memory::{GuestAddress, GuestUsize, VolatileMemoryError, VolatileSlice, WriteVolatile}; + + fn table_entry_size(type_: u8) -> usize { + match type_ as u32 { + mpspec::MP_PROCESSOR => mem::size_of::(), + mpspec::MP_BUS => mem::size_of::(), + mpspec::MP_IOAPIC => mem::size_of::(), + mpspec::MP_INTSRC => mem::size_of::(), + mpspec::MP_LINTSRC => mem::size_of::(), + _ => panic!("unrecognized mpc table entry type: {}", type_), + } + } + + #[test] + fn bounds_check() { + let num_cpus = 4; + let mem = + GuestMemoryMmap::from_ranges(&[(MPTABLE_START, compute_mp_size(num_cpus))]).unwrap(); + + setup_mptable(MPTABLE_START, &mem, num_cpus).unwrap(); + } + + #[test] + fn bounds_check_fails() { + let num_cpus = 4; + let mem = GuestMemoryMmap::from_ranges(&[(MPTABLE_START, compute_mp_size(num_cpus) - 1)]) + .unwrap(); + + assert!(setup_mptable(MPTABLE_START, &mem, num_cpus).is_err()); + } + + #[test] + fn mpf_intel_checksum() { + let num_cpus = 1; + let mem = + GuestMemoryMmap::from_ranges(&[(MPTABLE_START, compute_mp_size(num_cpus))]).unwrap(); + + setup_mptable(MPTABLE_START, &mem, num_cpus).unwrap(); + + let mpf_intel: MpfIntelWrapper = mem.read_obj(MPTABLE_START).unwrap(); + + assert_eq!( + mpf_intel_compute_checksum(&mpf_intel.0), + mpf_intel.0.checksum + ); + } + + #[test] + fn mpc_table_checksum() { + let num_cpus = 4; + let mem = + GuestMemoryMmap::from_ranges(&[(MPTABLE_START, compute_mp_size(num_cpus))]).unwrap(); + + setup_mptable(MPTABLE_START, &mem, num_cpus).unwrap(); + + let mpf_intel: MpfIntelWrapper = mem.read_obj(MPTABLE_START).unwrap(); + let mpc_offset = GuestAddress(mpf_intel.0.physptr as GuestUsize); + let mpc_table: MpcTableWrapper = mem.read_obj(mpc_offset).unwrap(); + + struct Sum(u8); + impl WriteVolatile for Sum { + fn write_volatile( + &mut self, + buf: &VolatileSlice, + ) -> result::Result { + let mut tmp = vec![0u8; buf.len()]; + tmp.write_all_volatile(buf)?; + + for v in tmp.iter() { + self.0 = self.0.wrapping_add(*v); + } + Ok(buf.len()) + } + } + + let mut sum = Sum(0); + mem.write_volatile_to(mpc_offset, &mut sum, mpc_table.0.length as usize) + .unwrap(); + assert_eq!(sum.0, 0); + } + + #[test] + fn cpu_entry_count() { + let mem = GuestMemoryMmap::from_ranges(&[( + MPTABLE_START, + compute_mp_size(MAX_SUPPORTED_CPUS as u8), + )]) + .unwrap(); + + for i in 0..MAX_SUPPORTED_CPUS as u8 { + setup_mptable(MPTABLE_START, &mem, i).unwrap(); + + let mpf_intel: MpfIntelWrapper = mem.read_obj(MPTABLE_START).unwrap(); + let mpc_offset = GuestAddress(mpf_intel.0.physptr as GuestUsize); + let mpc_table: MpcTableWrapper = mem.read_obj(mpc_offset).unwrap(); + let mpc_end = mpc_offset + .checked_add(mpc_table.0.length as GuestUsize) + .unwrap(); + + let mut entry_offset = mpc_offset + .checked_add(mem::size_of::() as GuestUsize) + .unwrap(); + let mut cpu_count = 0; + while entry_offset < mpc_end { + let entry_type: u8 = mem.read_obj(entry_offset).unwrap(); + entry_offset = entry_offset + .checked_add(table_entry_size(entry_type) as GuestUsize) + .unwrap(); + assert!(entry_offset <= mpc_end); + if entry_type as u32 == mpspec::MP_PROCESSOR { + cpu_count += 1; + } + } + assert_eq!(cpu_count, i); + } + } + + #[test] + fn cpu_entry_count_max() { + let cpus = MAX_SUPPORTED_CPUS + 1; + let mem = + GuestMemoryMmap::from_ranges(&[(MPTABLE_START, compute_mp_size(cpus as u8))]).unwrap(); + + let result = setup_mptable(MPTABLE_START, &mem, cpus as u8); + assert!(result.is_err()); + } +} diff --git a/vendor/cube-hypervisor/arch/src/x86_64/regs.rs b/vendor/cube-hypervisor/arch/src/x86_64/regs.rs new file mode 100644 index 0000000..7627775 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/x86_64/regs.rs @@ -0,0 +1,212 @@ +// Copyright © 2020, Oracle and/or its affiliates. +// +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +use crate::layout::{BOOT_GDT_START, BOOT_IDT_START, PVH_INFO_START}; +use crate::GuestMemoryMmap; +use hypervisor::arch::x86::gdt::{gdt_entry, segment_from_gdt}; +use hypervisor::arch::x86::regs::CR0_PE; +use hypervisor::arch::x86::{FpuState, SpecialRegisters, StandardRegisters}; +use std::sync::Arc; +use std::{mem, result}; +use vm_memory::{Address, Bytes, GuestMemory, GuestMemoryError}; + +#[derive(Debug)] +pub enum Error { + /// Failed to get SREGs for this CPU. + GetStatusRegisters(hypervisor::HypervisorCpuError), + /// Failed to set base registers for this CPU. + SetBaseRegisters(hypervisor::HypervisorCpuError), + /// Failed to configure the FPU. + SetFpuRegisters(hypervisor::HypervisorCpuError), + /// Setting up MSRs failed. + SetModelSpecificRegisters(hypervisor::HypervisorCpuError), + /// Failed to set SREGs for this CPU. + SetStatusRegisters(hypervisor::HypervisorCpuError), + /// Checking the GDT address failed. + CheckGdtAddr, + /// Writing the GDT to RAM failed. + WriteGdt(GuestMemoryError), + /// Writing the IDT to RAM failed. + WriteIdt(GuestMemoryError), + /// Writing PDPTE to RAM failed. + WritePdpteAddress(GuestMemoryError), + /// Writing PDE to RAM failed. + WritePdeAddress(GuestMemoryError), + /// Writing PML4 to RAM failed. + WritePml4Address(GuestMemoryError), + /// Writing PML5 to RAM failed. + WritePml5Address(GuestMemoryError), +} + +pub type Result = result::Result; + +/// Configure Floating-Point Unit (FPU) registers for a given CPU. +/// +/// # Arguments +/// +/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd. +pub fn setup_fpu(vcpu: &Arc) -> Result<()> { + let fpu: FpuState = FpuState { + fcw: 0x37f, + mxcsr: 0x1f80, + ..Default::default() + }; + + vcpu.set_fpu(&fpu).map_err(Error::SetFpuRegisters) +} + +/// Configure Model Specific Registers (MSRs) for a given CPU. +/// +/// # Arguments +/// +/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd. +pub fn setup_msrs(vcpu: &Arc) -> Result<()> { + vcpu.set_msrs(&vcpu.boot_msr_entries()) + .map_err(Error::SetModelSpecificRegisters)?; + + Ok(()) +} + +/// Configure base registers for a given CPU. +/// +/// # Arguments +/// +/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd. +/// * `boot_ip` - Starting instruction pointer. +pub fn setup_regs(vcpu: &Arc, boot_ip: u64) -> Result<()> { + let regs = StandardRegisters { + rflags: 0x0000000000000002u64, + rbx: PVH_INFO_START.raw_value(), + rip: boot_ip, + ..Default::default() + }; + vcpu.set_regs(®s).map_err(Error::SetBaseRegisters) +} + +/// Configures the segment registers and system page tables for a given CPU. +/// +/// # Arguments +/// +/// * `mem` - The memory that will be passed to the guest. +/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd. +pub fn setup_sregs(mem: &GuestMemoryMmap, vcpu: &Arc) -> Result<()> { + let mut sregs: SpecialRegisters = vcpu.get_sregs().map_err(Error::GetStatusRegisters)?; + configure_segments_and_sregs(mem, &mut sregs)?; + vcpu.set_sregs(&sregs).map_err(Error::SetStatusRegisters) +} + +const BOOT_GDT_MAX: usize = 4; + +fn write_gdt_table(table: &[u64], guest_mem: &GuestMemoryMmap) -> Result<()> { + let boot_gdt_addr = BOOT_GDT_START; + for (index, entry) in table.iter().enumerate() { + let addr = guest_mem + .checked_offset(boot_gdt_addr, index * mem::size_of::()) + .ok_or(Error::CheckGdtAddr)?; + guest_mem.write_obj(*entry, addr).map_err(Error::WriteGdt)?; + } + Ok(()) +} + +fn write_idt_value(val: u64, guest_mem: &GuestMemoryMmap) -> Result<()> { + let boot_idt_addr = BOOT_IDT_START; + guest_mem + .write_obj(val, boot_idt_addr) + .map_err(Error::WriteIdt) +} + +pub fn configure_segments_and_sregs( + mem: &GuestMemoryMmap, + sregs: &mut SpecialRegisters, +) -> Result<()> { + let gdt_table: [u64; BOOT_GDT_MAX] = { + // Configure GDT entries as specified by PVH boot protocol + [ + gdt_entry(0, 0, 0), // NULL + gdt_entry(0xc09b, 0, 0xffffffff), // CODE + gdt_entry(0xc093, 0, 0xffffffff), // DATA + gdt_entry(0x008b, 0, 0x67), // TSS + ] + }; + + let code_seg = segment_from_gdt(gdt_table[1], 1); + let data_seg = segment_from_gdt(gdt_table[2], 2); + let tss_seg = segment_from_gdt(gdt_table[3], 3); + + // Write segments + write_gdt_table(&gdt_table[..], mem)?; + sregs.gdt.base = BOOT_GDT_START.raw_value(); + sregs.gdt.limit = mem::size_of_val(&gdt_table) as u16 - 1; + + write_idt_value(0, mem)?; + sregs.idt.base = BOOT_IDT_START.raw_value(); + sregs.idt.limit = mem::size_of::() as u16 - 1; + + sregs.cs = code_seg; + sregs.ds = data_seg; + sregs.es = data_seg; + sregs.fs = data_seg; + sregs.gs = data_seg; + sregs.ss = data_seg; + sregs.tr = tss_seg; + + sregs.cr0 = CR0_PE; + sregs.cr4 = 0; + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::GuestMemoryMmap; + use vm_memory::GuestAddress; + + fn create_guest_mem() -> GuestMemoryMmap { + GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 0x10000)]).unwrap() + } + + fn read_u64(gm: &GuestMemoryMmap, offset: GuestAddress) -> u64 { + gm.read_obj(offset).unwrap() + } + + #[test] + fn segments_and_sregs() { + let mut sregs: SpecialRegisters = Default::default(); + let gm = create_guest_mem(); + configure_segments_and_sregs(&gm, &mut sregs).unwrap(); + assert_eq!(0x0, read_u64(&gm, BOOT_GDT_START)); + assert_eq!( + 0xcf9b000000ffff, + read_u64(&gm, BOOT_GDT_START.unchecked_add(8)) + ); + assert_eq!( + 0xcf93000000ffff, + read_u64(&gm, BOOT_GDT_START.unchecked_add(16)) + ); + assert_eq!( + 0x8b0000000067, + read_u64(&gm, BOOT_GDT_START.unchecked_add(24)) + ); + assert_eq!(0x0, read_u64(&gm, BOOT_IDT_START)); + + assert_eq!(0, sregs.cs.base); + assert_eq!(0xffffffff, sregs.ds.limit); + assert_eq!(0x10, sregs.es.selector); + assert_eq!(1, sregs.fs.present); + assert_eq!(1, sregs.gs.g); + assert_eq!(0, sregs.ss.avl); + assert_eq!(0, sregs.tr.base); + assert_eq!(0, sregs.tr.g); + assert_eq!(0x67, sregs.tr.limit); + assert_eq!(0xb, sregs.tr.type_); + assert_eq!(0, sregs.tr.avl); + assert_eq!(CR0_PE, sregs.cr0); + assert_eq!(0, sregs.cr4); + } +} diff --git a/vendor/cube-hypervisor/arch/src/x86_64/smbios.rs b/vendor/cube-hypervisor/arch/src/x86_64/smbios.rs new file mode 100644 index 0000000..3cf8158 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/x86_64/smbios.rs @@ -0,0 +1,321 @@ +// Copyright © 2020 Intel Corporation +// +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use crate::layout::SMBIOS_START; +use crate::GuestMemoryMmap; +use std::fmt::{self, Display}; +use std::mem; +use std::result; +use std::slice; +use uuid::Uuid; +use vm_memory::ByteValued; +use vm_memory::{Address, Bytes, GuestAddress}; + +#[derive(Debug)] +pub enum Error { + /// There was too little guest memory to store the entire SMBIOS table. + NotEnoughMemory, + /// The SMBIOS table has too little address space to be stored. + AddressOverflow, + /// Failure while zeroing out the memory for the SMBIOS table. + Clear, + /// Failure to write SMBIOS entrypoint structure + WriteSmbiosEp, + /// Failure to write additional data to memory + WriteData, + /// Failure to parse uuid, uuid format may be error + ParseUuid(uuid::Error), +} + +impl std::error::Error for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + + let description = match self { + NotEnoughMemory => { + "There was too little guest memory to store the SMBIOS table".to_string() + } + AddressOverflow => { + "The SMBIOS table has too little address space to be stored".to_string() + } + Clear => "Failure while zeroing out the memory for the SMBIOS table".to_string(), + WriteSmbiosEp => "Failure to write SMBIOS entrypoint structure".to_string(), + WriteData => "Failure to write additional data to memory".to_string(), + ParseUuid(e) => format!("Failure to parse uuid: {}", e), + }; + + write!(f, "SMBIOS error: {}", description) + } +} + +pub type Result = result::Result; + +// Constants sourced from SMBIOS Spec 3.2.0. +const SM3_MAGIC_IDENT: &[u8; 5usize] = b"_SM3_"; +const BIOS_INFORMATION: u8 = 0; +const SYSTEM_INFORMATION: u8 = 1; +const OEM_STRINGS: u8 = 11; +const END_OF_TABLE: u8 = 127; +const PCI_SUPPORTED: u64 = 1 << 7; +const IS_VIRTUAL_MACHINE: u8 = 1 << 4; + +fn compute_checksum(v: &T) -> u8 { + // Safe because we are only reading the bytes within the size of the `T` reference `v`. + let v_slice = unsafe { slice::from_raw_parts(v as *const T as *const u8, mem::size_of::()) }; + let mut checksum: u8 = 0; + for i in v_slice.iter() { + checksum = checksum.wrapping_add(*i); + } + (!checksum).wrapping_add(1) +} + +#[repr(C)] +#[repr(packed)] +#[derive(Default, Copy, Clone)] +struct Smbios30Entrypoint { + signature: [u8; 5usize], + checksum: u8, + length: u8, + majorver: u8, + minorver: u8, + docrev: u8, + revision: u8, + reserved: u8, + max_size: u32, + physptr: u64, +} + +#[repr(C)] +#[repr(packed)] +#[derive(Default, Copy, Clone)] +struct SmbiosBiosInfo { + r#type: u8, + length: u8, + handle: u16, + vendor: u8, + version: u8, + start_addr: u16, + release_date: u8, + rom_size: u8, + characteristics: u64, + characteristics_ext1: u8, + characteristics_ext2: u8, +} + +#[repr(C)] +#[repr(packed)] +#[derive(Default, Copy, Clone)] +struct SmbiosSysInfo { + r#type: u8, + length: u8, + handle: u16, + manufacturer: u8, + product_name: u8, + version: u8, + serial_number: u8, + uuid: [u8; 16usize], + wake_up_type: u8, + sku: u8, + family: u8, +} + +#[repr(C)] +#[repr(packed)] +#[derive(Default, Copy, Clone)] +struct SmbiosOemStrings { + r#type: u8, + length: u8, + handle: u16, + count: u8, +} + +#[repr(C)] +#[repr(packed)] +#[derive(Default, Copy, Clone)] +struct SmbiosEndOfTable { + r#type: u8, + length: u8, + handle: u16, +} + +// SAFETY: These data structures only contain a series of integers +unsafe impl ByteValued for Smbios30Entrypoint {} +unsafe impl ByteValued for SmbiosBiosInfo {} +unsafe impl ByteValued for SmbiosSysInfo {} +unsafe impl ByteValued for SmbiosOemStrings {} +unsafe impl ByteValued for SmbiosEndOfTable {} + +fn write_and_incr( + mem: &GuestMemoryMmap, + val: T, + mut curptr: GuestAddress, +) -> Result { + mem.write_obj(val, curptr).map_err(|_| Error::WriteData)?; + curptr = curptr + .checked_add(mem::size_of::() as u64) + .ok_or(Error::NotEnoughMemory)?; + Ok(curptr) +} + +fn write_string( + mem: &GuestMemoryMmap, + val: &str, + mut curptr: GuestAddress, +) -> Result { + for c in val.as_bytes().iter() { + curptr = write_and_incr(mem, *c, curptr)?; + } + curptr = write_and_incr(mem, 0u8, curptr)?; + Ok(curptr) +} + +pub fn setup_smbios( + mem: &GuestMemoryMmap, + serial_number: Option<&str>, + uuid: Option<&str>, + oem_strings: Option<&[&str]>, +) -> Result { + let physptr = GuestAddress(SMBIOS_START) + .checked_add(mem::size_of::() as u64) + .ok_or(Error::NotEnoughMemory)?; + let mut curptr = physptr; + let mut handle = 0; + + { + handle += 1; + let smbios_biosinfo = SmbiosBiosInfo { + r#type: BIOS_INFORMATION, + length: mem::size_of::() as u8, + handle, + vendor: 1, // First string written in this section + version: 2, // Second string written in this section + characteristics: PCI_SUPPORTED, + characteristics_ext2: IS_VIRTUAL_MACHINE, + ..Default::default() + }; + curptr = write_and_incr(mem, smbios_biosinfo, curptr)?; + curptr = write_string(mem, "cube-hypervisor", curptr)?; + curptr = write_string(mem, "0", curptr)?; + curptr = write_and_incr(mem, 0u8, curptr)?; + } + + { + handle += 1; + + let uuid_number = uuid + .map(Uuid::parse_str) + .transpose() + .map_err(Error::ParseUuid)? + .unwrap_or(Uuid::nil()); + let smbios_sysinfo = SmbiosSysInfo { + r#type: SYSTEM_INFORMATION, + length: mem::size_of::() as u8, + handle, + manufacturer: 1, // First string written in this section + product_name: 2, // Second string written in this section + serial_number: serial_number.map(|_| 3).unwrap_or_default(), // 3rd string + uuid: uuid_number.to_bytes_le(), // set uuid + ..Default::default() + }; + curptr = write_and_incr(mem, smbios_sysinfo, curptr)?; + curptr = write_string(mem, "Cube Hypervisor", curptr)?; + curptr = write_string(mem, "cube-hypervisor", curptr)?; + if let Some(serial_number) = serial_number { + curptr = write_string(mem, serial_number, curptr)?; + } + curptr = write_and_incr(mem, 0u8, curptr)?; + } + + if let Some(oem_strings) = oem_strings { + handle += 1; + + let smbios_oemstrings = SmbiosOemStrings { + r#type: OEM_STRINGS, + length: mem::size_of::() as u8, + handle, + count: oem_strings.len() as u8, + }; + + curptr = write_and_incr(mem, smbios_oemstrings, curptr)?; + + for s in oem_strings { + curptr = write_string(mem, s, curptr)?; + } + + curptr = write_and_incr(mem, 0u8, curptr)?; + } + + { + handle += 1; + let smbios_end = SmbiosEndOfTable { + r#type: END_OF_TABLE, + length: mem::size_of::() as u8, + handle, + }; + curptr = write_and_incr(mem, smbios_end, curptr)?; + curptr = write_and_incr(mem, 0u8, curptr)?; + curptr = write_and_incr(mem, 0u8, curptr)?; + } + + { + let mut smbios_ep = Smbios30Entrypoint { + signature: *SM3_MAGIC_IDENT, + length: mem::size_of::() as u8, + // SMBIOS rev 3.2.0 + majorver: 0x03, + minorver: 0x02, + docrev: 0x00, + revision: 0x01, // SMBIOS 3.0 + max_size: curptr.unchecked_offset_from(physptr) as u32, + physptr: physptr.0, + ..Default::default() + }; + smbios_ep.checksum = compute_checksum(&smbios_ep); + mem.write_obj(smbios_ep, GuestAddress(SMBIOS_START)) + .map_err(|_| Error::WriteSmbiosEp)?; + } + + Ok(curptr.unchecked_offset_from(physptr) + std::mem::size_of::() as u64) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn struct_size() { + assert_eq!( + mem::size_of::(), + 0x18usize, + concat!("Size of: ", stringify!(Smbios30Entrypoint)) + ); + assert_eq!( + mem::size_of::(), + 0x14usize, + concat!("Size of: ", stringify!(SmbiosBiosInfo)) + ); + assert_eq!( + mem::size_of::(), + 0x1busize, + concat!("Size of: ", stringify!(SmbiosSysInfo)) + ); + } + + #[test] + fn entrypoint_checksum() { + let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(SMBIOS_START), 4096)]).unwrap(); + + setup_smbios(&mem, None, None, None).unwrap(); + + let smbios_ep: Smbios30Entrypoint = mem.read_obj(GuestAddress(SMBIOS_START)).unwrap(); + + assert_eq!(compute_checksum(&smbios_ep), 0); + } +} diff --git a/vendor/cube-hypervisor/arch/src/x86_64/tdx/mod.rs b/vendor/cube-hypervisor/arch/src/x86_64/tdx/mod.rs new file mode 100644 index 0000000..bc5daf9 --- /dev/null +++ b/vendor/cube-hypervisor/arch/src/x86_64/tdx/mod.rs @@ -0,0 +1,453 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +use crate::GuestMemoryMmap; +use std::fs::File; +use std::io::{Read, Seek, SeekFrom}; +use thiserror::Error; +use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemoryError}; + +#[derive(Error, Debug)] +pub enum TdvfError { + #[error("Failed read TDVF descriptor: {0}")] + ReadDescriptor(#[source] std::io::Error), + #[error("Failed read TDVF descriptor offset: {0}")] + ReadDescriptorOffset(#[source] std::io::Error), + #[error("Invalid descriptor signature")] + InvalidDescriptorSignature, + #[error("Invalid descriptor size")] + InvalidDescriptorSize, + #[error("Invalid descriptor version")] + InvalidDescriptorVersion, + #[error("Failed to write HOB details to guest memory: {0}")] + GuestMemoryWriteHob(#[source] GuestMemoryError), +} + +// TDVF_DESCRIPTOR +#[repr(packed)] +#[derive(Default)] +pub struct TdvfDescriptor { + signature: [u8; 4], + length: u32, + version: u32, + num_sections: u32, // NumberOfSectionEntry +} + +// TDVF_SECTION +#[repr(packed)] +#[derive(Clone, Copy, Default, Debug)] +pub struct TdvfSection { + pub data_offset: u32, + pub data_size: u32, // RawDataSize + pub address: u64, // MemoryAddress + pub size: u64, // MemoryDataSize + pub r#type: TdvfSectionType, + pub attributes: u32, +} + +#[repr(u32)] +#[derive(Clone, Copy, Debug)] +pub enum TdvfSectionType { + Bfv, + Cfv, + TdHob, + TempMem, + PermMem, + Payload, + PayloadParam, + Reserved = 0xffffffff, +} + +impl Default for TdvfSectionType { + fn default() -> Self { + TdvfSectionType::Reserved + } +} + +pub fn parse_tdvf_sections(file: &mut File) -> Result, TdvfError> { + // The 32-bit offset to the TDVF metadata is located 32 bytes from + // the end of the file. + // See "TDVF Metadata Pointer" in "TDX Virtual Firmware Design Guide + file.seek(SeekFrom::End(-0x20)) + .map_err(TdvfError::ReadDescriptorOffset)?; + + let mut descriptor_offset: [u8; 4] = [0; 4]; + file.read_exact(&mut descriptor_offset) + .map_err(TdvfError::ReadDescriptorOffset)?; + let descriptor_offset = u32::from_le_bytes(descriptor_offset) as u64; + + file.seek(SeekFrom::Start(descriptor_offset)) + .map_err(TdvfError::ReadDescriptor)?; + + let mut descriptor: TdvfDescriptor = Default::default(); + // Safe as we read exactly the size of the descriptor header + file.read_exact(unsafe { + std::slice::from_raw_parts_mut( + &mut descriptor as *mut _ as *mut u8, + std::mem::size_of::(), + ) + }) + .map_err(TdvfError::ReadDescriptor)?; + + if &descriptor.signature != b"TDVF" { + return Err(TdvfError::InvalidDescriptorSignature); + } + + if descriptor.length as usize + != std::mem::size_of::() + + std::mem::size_of::() * descriptor.num_sections as usize + { + return Err(TdvfError::InvalidDescriptorSize); + } + + if descriptor.version != 1 { + return Err(TdvfError::InvalidDescriptorVersion); + } + + let mut sections = Vec::new(); + sections.resize_with(descriptor.num_sections as usize, TdvfSection::default); + + // Safe as we read exactly the advertised sections + file.read_exact(unsafe { + std::slice::from_raw_parts_mut( + sections.as_mut_ptr() as *mut u8, + descriptor.num_sections as usize * std::mem::size_of::(), + ) + }) + .map_err(TdvfError::ReadDescriptor)?; + + Ok(sections) +} + +#[repr(u16)] +#[derive(Copy, Clone, Debug)] +enum HobType { + Handoff = 0x1, + ResourceDescriptor = 0x3, + GuidExtension = 0x4, + Unused = 0xfffe, + EndOfHobList = 0xffff, +} + +impl Default for HobType { + fn default() -> Self { + HobType::Unused + } +} + +#[repr(C)] +#[derive(Copy, Clone, Default, Debug)] +struct HobHeader { + r#type: HobType, + length: u16, + reserved: u32, +} + +#[repr(C)] +#[derive(Copy, Clone, Default, Debug)] +struct HobHandoffInfoTable { + header: HobHeader, + version: u32, + boot_mode: u32, + efi_memory_top: u64, + efi_memory_bottom: u64, + efi_free_memory_top: u64, + efi_free_memory_bottom: u64, + efi_end_of_hob_list: u64, +} + +#[repr(C)] +#[derive(Copy, Clone, Default, Debug)] +struct EfiGuid { + data1: u32, + data2: u16, + data3: u16, + data4: [u8; 8], +} + +#[repr(C)] +#[derive(Copy, Clone, Default, Debug)] +struct HobResourceDescriptor { + header: HobHeader, + owner: EfiGuid, + resource_type: u32, + resource_attribute: u32, + physical_start: u64, + resource_length: u64, +} + +#[repr(C)] +#[derive(Copy, Clone, Default, Debug)] +struct HobGuidType { + header: HobHeader, + name: EfiGuid, +} + +#[repr(u32)] +#[derive(Clone, Copy, Debug)] +pub enum PayloadImageType { + ExecutablePayload, + BzImage, + RawVmLinux, +} + +impl Default for PayloadImageType { + fn default() -> Self { + PayloadImageType::ExecutablePayload + } +} + +#[repr(C)] +#[derive(Copy, Clone, Default, Debug)] +pub struct PayloadInfo { + pub image_type: PayloadImageType, + pub entry_point: u64, +} + +#[repr(C)] +#[derive(Copy, Clone, Default, Debug)] +struct TdPayload { + guid_type: HobGuidType, + payload_info: PayloadInfo, +} + +// SAFETY: These data structures only contain a series of integers +unsafe impl ByteValued for HobHeader {} +unsafe impl ByteValued for HobHandoffInfoTable {} +unsafe impl ByteValued for HobResourceDescriptor {} +unsafe impl ByteValued for HobGuidType {} +unsafe impl ByteValued for PayloadInfo {} +unsafe impl ByteValued for TdPayload {} + +pub struct TdHob { + start_offset: u64, + current_offset: u64, +} + +fn align_hob(v: u64) -> u64 { + (v + 7) / 8 * 8 +} + +impl TdHob { + fn update_offset(&mut self) { + self.current_offset = align_hob(self.current_offset + std::mem::size_of::() as u64) + } + + pub fn start(offset: u64) -> TdHob { + // Leave a gap to place the HandoffTable at the start as it can only be filled in later + let mut hob = TdHob { + start_offset: offset, + current_offset: offset, + }; + hob.update_offset::(); + hob + } + + pub fn finish(&mut self, mem: &GuestMemoryMmap) -> Result<(), TdvfError> { + // Write end + let end = HobHeader { + r#type: HobType::EndOfHobList, + length: std::mem::size_of::() as u16, + reserved: 0, + }; + info!("Writing HOB end {:x} {:x?}", self.current_offset, end); + mem.write_obj(end, GuestAddress(self.current_offset)) + .map_err(TdvfError::GuestMemoryWriteHob)?; + self.update_offset::(); + + // Write handoff, delayed as it needs end of HOB list + let efi_end_of_hob_list = self.current_offset; + let handoff = HobHandoffInfoTable { + header: HobHeader { + r#type: HobType::Handoff, + length: std::mem::size_of::() as u16, + reserved: 0, + }, + version: 0x9, + boot_mode: 0, + efi_memory_top: 0, + efi_memory_bottom: 0, + efi_free_memory_top: 0, + efi_free_memory_bottom: 0, + efi_end_of_hob_list, + }; + info!("Writing HOB start {:x} {:x?}", self.start_offset, handoff); + mem.write_obj(handoff, GuestAddress(self.start_offset)) + .map_err(TdvfError::GuestMemoryWriteHob) + } + + pub fn add_resource( + &mut self, + mem: &GuestMemoryMmap, + physical_start: u64, + resource_length: u64, + resource_type: u32, + resource_attribute: u32, + ) -> Result<(), TdvfError> { + let resource_descriptor = HobResourceDescriptor { + header: HobHeader { + r#type: HobType::ResourceDescriptor, + length: std::mem::size_of::() as u16, + reserved: 0, + }, + owner: EfiGuid::default(), + resource_type, + resource_attribute, + physical_start, + resource_length, + }; + info!( + "Writing HOB resource {:x} {:x?}", + self.current_offset, resource_descriptor + ); + mem.write_obj(resource_descriptor, GuestAddress(self.current_offset)) + .map_err(TdvfError::GuestMemoryWriteHob)?; + self.update_offset::(); + Ok(()) + } + + pub fn add_memory_resource( + &mut self, + mem: &GuestMemoryMmap, + physical_start: u64, + resource_length: u64, + ram: bool, + ) -> Result<(), TdvfError> { + self.add_resource( + mem, + physical_start, + resource_length, + if ram { + 0 /* EFI_RESOURCE_SYSTEM_MEMORY */ + } else { + 0x5 /*EFI_RESOURCE_MEMORY_RESERVED */ + }, + /* TODO: + * QEMU currently fills it in like this: + * EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED + * which differs from the spec (due to TDVF implementation issue?) + */ + 0x7, + ) + } + + pub fn add_mmio_resource( + &mut self, + mem: &GuestMemoryMmap, + physical_start: u64, + resource_length: u64, + ) -> Result<(), TdvfError> { + self.add_resource( + mem, + physical_start, + resource_length, + 0x1, /* EFI_RESOURCE_MEMORY_MAPPED_IO */ + /* + * EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE + */ + 0x403, + ) + } + + pub fn add_acpi_table( + &mut self, + mem: &GuestMemoryMmap, + table_content: &[u8], + ) -> Result<(), TdvfError> { + // We already know the HobGuidType size is 8 bytes multiple, but we + // need the total size to be 8 bytes multiple. That is why the ACPI + // table size must be 8 bytes multiple as well. + let length = std::mem::size_of::() as u16 + + align_hob(table_content.len() as u64) as u16; + let hob_guid_type = HobGuidType { + header: HobHeader { + r#type: HobType::GuidExtension, + length, + reserved: 0, + }, + // ACPI_TABLE_HOB_GUID + // 0x6a0c5870, 0xd4ed, 0x44f4, {0xa1, 0x35, 0xdd, 0x23, 0x8b, 0x6f, 0xc, 0x8d } + name: EfiGuid { + data1: 0x6a0c_5870, + data2: 0xd4ed, + data3: 0x44f4, + data4: [0xa1, 0x35, 0xdd, 0x23, 0x8b, 0x6f, 0xc, 0x8d], + }, + }; + info!( + "Writing HOB ACPI table {:x} {:x?} {:x?}", + self.current_offset, hob_guid_type, table_content + ); + mem.write_obj(hob_guid_type, GuestAddress(self.current_offset)) + .map_err(TdvfError::GuestMemoryWriteHob)?; + let current_offset = self.current_offset + std::mem::size_of::() as u64; + + // In case the table is quite large, let's make sure we can handle + // retrying until everything has been correctly copied. + let mut offset: usize = 0; + loop { + let bytes_written = mem + .write( + &table_content[offset..], + GuestAddress(current_offset + offset as u64), + ) + .map_err(TdvfError::GuestMemoryWriteHob)?; + offset += bytes_written; + if offset >= table_content.len() { + break; + } + } + self.current_offset += length as u64; + + Ok(()) + } + + pub fn add_payload( + &mut self, + mem: &GuestMemoryMmap, + payload_info: PayloadInfo, + ) -> Result<(), TdvfError> { + let payload = TdPayload { + guid_type: HobGuidType { + header: HobHeader { + r#type: HobType::GuidExtension, + length: std::mem::size_of::() as u16, + reserved: 0, + }, + // HOB_PAYLOAD_INFO_GUID + // 0xb96fa412, 0x461f, 0x4be3, {0x8c, 0xd, 0xad, 0x80, 0x5a, 0x49, 0x7a, 0xc0 + name: EfiGuid { + data1: 0xb96f_a412, + data2: 0x461f, + data3: 0x4be3, + data4: [0x8c, 0xd, 0xad, 0x80, 0x5a, 0x49, 0x7a, 0xc0], + }, + }, + payload_info, + }; + info!( + "Writing HOB TD_PAYLOAD {:x} {:x?}", + self.current_offset, payload + ); + mem.write_obj(payload, GuestAddress(self.current_offset)) + .map_err(TdvfError::GuestMemoryWriteHob)?; + self.update_offset::(); + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[ignore] + fn test_parse_tdvf_sections() { + let mut f = std::fs::File::open("tdvf.fd").unwrap(); + let sections = parse_tdvf_sections(&mut f).unwrap(); + for section in sections { + eprintln!("{:x?}", section) + } + } +} diff --git a/vendor/cube-hypervisor/block_util/Cargo.toml b/vendor/cube-hypervisor/block_util/Cargo.toml new file mode 100644 index 0000000..f924257 --- /dev/null +++ b/vendor/cube-hypervisor/block_util/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "block_util" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[features] +default = [] + +[dependencies] +io-uring = "0.5.8" +libc = "0.2.137" +log = "0.4.17" +qcow = { path = "../qcow" } +serde = { workspace = true, features = ["derive"] } +thiserror = "1.0.37" +vhdx = { path = "../vhdx" } +virtio-bindings = { workspace = true, features = ["virtio-v5_0_0"] } +virtio-queue = { workspace = true } +vm-memory = { workspace = true, features = ["backend-mmap", "backend-atomic", "backend-bitmap"] } +vm-virtio = { path = "../vm-virtio" } +vmm-sys-util = "0.12.1" + diff --git a/vendor/cube-hypervisor/block_util/src/async_io.rs b/vendor/cube-hypervisor/block_util/src/async_io.rs new file mode 100644 index 0000000..2661f1f --- /dev/null +++ b/vendor/cube-hypervisor/block_util/src/async_io.rs @@ -0,0 +1,146 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use libc::{ioctl, S_IFBLK, S_IFMT}; +use std::convert::TryInto; +use std::fs::File; +use std::os::unix::io::AsRawFd; +use thiserror::Error; +use vmm_sys_util::eventfd::EventFd; +use vmm_sys_util::{ioctl_io_nr, ioctl_ioc_nr}; + +#[derive(Error, Debug)] +pub enum DiskFileError { + /// Failed getting disk file size. + #[error("Failed getting disk file size: {0}")] + Size(#[source] std::io::Error), + /// Failed creating a new AsyncIo. + #[error("Failed creating a new AsyncIo: {0}")] + NewAsyncIo(#[source] std::io::Error), +} + +#[derive(Debug)] +pub struct DiskTopology { + pub logical_block_size: u64, + pub physical_block_size: u64, + pub minimum_io_size: u64, + pub optimal_io_size: u64, +} + +impl Default for DiskTopology { + fn default() -> Self { + Self { + logical_block_size: 512, + physical_block_size: 512, + minimum_io_size: 512, + optimal_io_size: 0, + } + } +} + +ioctl_io_nr!(BLKSSZGET, 0x12, 104); +ioctl_io_nr!(BLKPBSZGET, 0x12, 123); +ioctl_io_nr!(BLKIOMIN, 0x12, 120); +ioctl_io_nr!(BLKIOOPT, 0x12, 121); + +enum BlockSize { + LogicalBlock, + PhysicalBlock, + MinimumIo, + OptimalIo, +} + +impl DiskTopology { + fn is_block_device(f: &mut File) -> std::io::Result { + let mut stat = std::mem::MaybeUninit::::uninit(); + let ret = unsafe { libc::fstat(f.as_raw_fd(), stat.as_mut_ptr()) }; + if ret != 0 { + return Err(std::io::Error::last_os_error()); + } + + let is_block = unsafe { (*stat.as_ptr()).st_mode & S_IFMT == S_IFBLK }; + Ok(is_block) + } + + // libc::ioctl() takes different types on different architectures + #[allow(clippy::useless_conversion)] + fn query_block_size(f: &mut File, block_size_type: BlockSize) -> std::io::Result { + let mut block_size = 0; + let ret = unsafe { + ioctl( + f.as_raw_fd(), + match block_size_type { + BlockSize::LogicalBlock => BLKSSZGET(), + BlockSize::PhysicalBlock => BLKPBSZGET(), + BlockSize::MinimumIo => BLKIOMIN(), + BlockSize::OptimalIo => BLKIOOPT(), + } + .try_into() + .unwrap(), + &mut block_size, + ) + }; + if ret != 0 { + return Err(std::io::Error::last_os_error()); + }; + + Ok(block_size) + } + + pub fn probe(f: &mut File) -> std::io::Result { + if !Self::is_block_device(f)? { + return Ok(DiskTopology::default()); + } + + Ok(DiskTopology { + logical_block_size: Self::query_block_size(f, BlockSize::LogicalBlock)?, + physical_block_size: Self::query_block_size(f, BlockSize::PhysicalBlock)?, + minimum_io_size: Self::query_block_size(f, BlockSize::MinimumIo)?, + optimal_io_size: Self::query_block_size(f, BlockSize::OptimalIo)?, + }) + } +} + +pub type DiskFileResult = std::result::Result; + +pub trait DiskFile: Send { + fn size(&mut self) -> DiskFileResult; + fn new_async_io(&self, ring_depth: u32) -> DiskFileResult>; + fn topology(&mut self) -> DiskTopology { + DiskTopology::default() + } +} + +#[derive(Error, Debug)] +pub enum AsyncIoError { + /// Failed vectored reading from file. + #[error("Failed vectored reading from file: {0}")] + ReadVectored(#[source] std::io::Error), + /// Failed vectored writing to file. + #[error("Failed vectored writing to file: {0}")] + WriteVectored(#[source] std::io::Error), + /// Failed synchronizing file. + #[error("Failed synchronizing file: {0}")] + Fsync(#[source] std::io::Error), +} + +pub type AsyncIoResult = std::result::Result; + +pub trait AsyncIo: Send { + fn notifier(&self) -> &EventFd; + fn read_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()>; + fn write_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()>; + fn fsync(&mut self, user_data: Option) -> AsyncIoResult<()>; + fn complete(&mut self) -> Vec<(u64, i32)>; +} diff --git a/vendor/cube-hypervisor/block_util/src/fixed_vhd_async.rs b/vendor/cube-hypervisor/block_util/src/fixed_vhd_async.rs new file mode 100644 index 0000000..16aba4f --- /dev/null +++ b/vendor/cube-hypervisor/block_util/src/fixed_vhd_async.rs @@ -0,0 +1,110 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use crate::async_io::{ + AsyncIo, AsyncIoError, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult, +}; +use crate::raw_async::RawFileAsync; +use crate::vhd::VhdFooter; +use std::fs::File; +use std::os::unix::io::{AsRawFd, RawFd}; +use vmm_sys_util::eventfd::EventFd; + +pub struct FixedVhdDiskAsync { + file: File, + size: u64, +} + +impl FixedVhdDiskAsync { + pub fn new(mut file: File) -> std::io::Result { + let footer = VhdFooter::new(&mut file)?; + + Ok(FixedVhdDiskAsync { + file, + size: footer.current_size(), + }) + } +} + +impl DiskFile for FixedVhdDiskAsync { + fn size(&mut self) -> DiskFileResult { + Ok(self.size) + } + + fn new_async_io(&self, ring_depth: u32) -> DiskFileResult> { + Ok(Box::new( + FixedVhdAsync::new(self.file.as_raw_fd(), ring_depth, self.size) + .map_err(DiskFileError::NewAsyncIo)?, + ) as Box) + } +} + +pub struct FixedVhdAsync { + raw_file_async: RawFileAsync, + size: u64, +} + +impl FixedVhdAsync { + pub fn new(fd: RawFd, ring_depth: u32, size: u64) -> std::io::Result { + let raw_file_async = RawFileAsync::new(fd, ring_depth)?; + + Ok(FixedVhdAsync { + raw_file_async, + size, + }) + } +} + +impl AsyncIo for FixedVhdAsync { + fn notifier(&self) -> &EventFd { + self.raw_file_async.notifier() + } + + fn read_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + if offset as u64 >= self.size { + return Err(AsyncIoError::ReadVectored(std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!( + "Invalid offset {}, can't be larger than file size {}", + offset, self.size + ), + ))); + } + + self.raw_file_async.read_vectored(offset, iovecs, user_data) + } + + fn write_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + if offset as u64 >= self.size { + return Err(AsyncIoError::WriteVectored(std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!( + "Invalid offset {}, can't be larger than file size {}", + offset, self.size + ), + ))); + } + + self.raw_file_async + .write_vectored(offset, iovecs, user_data) + } + + fn fsync(&mut self, user_data: Option) -> AsyncIoResult<()> { + self.raw_file_async.fsync(user_data) + } + + fn complete(&mut self) -> Vec<(u64, i32)> { + self.raw_file_async.complete() + } +} diff --git a/vendor/cube-hypervisor/block_util/src/fixed_vhd_sync.rs b/vendor/cube-hypervisor/block_util/src/fixed_vhd_sync.rs new file mode 100644 index 0000000..f4293ea --- /dev/null +++ b/vendor/cube-hypervisor/block_util/src/fixed_vhd_sync.rs @@ -0,0 +1,107 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use crate::async_io::{ + AsyncIo, AsyncIoError, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult, +}; +use crate::raw_sync::RawFileSync; +use crate::vhd::VhdFooter; +use std::fs::File; +use std::os::unix::io::{AsRawFd, RawFd}; +use vmm_sys_util::eventfd::EventFd; + +pub struct FixedVhdDiskSync { + file: File, + size: u64, +} + +impl FixedVhdDiskSync { + pub fn new(mut file: File) -> std::io::Result { + let footer = VhdFooter::new(&mut file)?; + + Ok(FixedVhdDiskSync { + file, + size: footer.current_size(), + }) + } +} + +impl DiskFile for FixedVhdDiskSync { + fn size(&mut self) -> DiskFileResult { + Ok(self.size) + } + + fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult> { + Ok(Box::new( + FixedVhdSync::new(self.file.as_raw_fd(), self.size) + .map_err(DiskFileError::NewAsyncIo)?, + ) as Box) + } +} + +pub struct FixedVhdSync { + raw_file_sync: RawFileSync, + size: u64, +} + +impl FixedVhdSync { + pub fn new(fd: RawFd, size: u64) -> std::io::Result { + Ok(FixedVhdSync { + raw_file_sync: RawFileSync::new(fd), + size, + }) + } +} + +impl AsyncIo for FixedVhdSync { + fn notifier(&self) -> &EventFd { + self.raw_file_sync.notifier() + } + + fn read_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + if offset as u64 >= self.size { + return Err(AsyncIoError::ReadVectored(std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!( + "Invalid offset {}, can't be larger than file size {}", + offset, self.size + ), + ))); + } + + self.raw_file_sync.read_vectored(offset, iovecs, user_data) + } + + fn write_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + if offset as u64 >= self.size { + return Err(AsyncIoError::WriteVectored(std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!( + "Invalid offset {}, can't be larger than file size {}", + offset, self.size + ), + ))); + } + + self.raw_file_sync.write_vectored(offset, iovecs, user_data) + } + + fn fsync(&mut self, user_data: Option) -> AsyncIoResult<()> { + self.raw_file_sync.fsync(user_data) + } + + fn complete(&mut self) -> Vec<(u64, i32)> { + self.raw_file_sync.complete() + } +} diff --git a/vendor/cube-hypervisor/block_util/src/lib.rs b/vendor/cube-hypervisor/block_util/src/lib.rs new file mode 100644 index 0000000..6f55aad --- /dev/null +++ b/vendor/cube-hypervisor/block_util/src/lib.rs @@ -0,0 +1,1004 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +#[macro_use] +extern crate log; + +pub mod async_io; +pub mod fixed_vhd_async; +pub mod fixed_vhd_sync; +pub mod qcow_sync; +pub mod raw_async; +pub mod raw_sync; +pub mod vhd; +pub mod vhdx_sync; + +use crate::async_io::{AsyncIo, AsyncIoError, AsyncIoResult}; +use io_uring::{opcode, IoUring, Probe}; +use serde::{Deserialize, Serialize}; +use std::alloc::{alloc_zeroed, dealloc, Layout}; +use std::cmp; +use std::convert::TryInto; +use std::fs::File; +use std::io::{self, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write}; +use std::os::linux::fs::MetadataExt; +use std::path::Path; +use std::result; +use std::sync::Arc; +use std::sync::MutexGuard; +use thiserror::Error; +use virtio_bindings::bindings::virtio_blk::*; +use virtio_queue::DescriptorChain; +use vm_memory::{ + bitmap::Bitmap, ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError, + GuestMemoryLoadGuard, +}; +use vm_virtio::{AccessPlatform, Translatable}; +use vmm_sys_util::eventfd::EventFd; + +const SECTOR_SHIFT: u8 = 9; +pub const SECTOR_SIZE: u64 = 0x01 << SECTOR_SHIFT; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Guest gave us bad memory addresses")] + GuestMemory(GuestMemoryError), + #[error("Guest gave us offsets that would have overflowed a usize")] + CheckedOffset(GuestAddress, usize), + #[error("Guest gave us a write only descriptor that protocol says to read from")] + UnexpectedWriteOnlyDescriptor, + #[error("Guest gave us a read only descriptor that protocol says to write to")] + UnexpectedReadOnlyDescriptor, + #[error("Guest gave us too few descriptors in a descriptor chain")] + DescriptorChainTooShort, + #[error("Guest gave us a descriptor that was too short to use")] + DescriptorLengthTooSmall, + #[error("Getting a block's metadata fails for any reason")] + GetFileMetadata, + #[error("The requested operation would cause a seek beyond disk end")] + InvalidOffset, + #[error("The requested operation does not support multiple descriptors")] + TooManyDescriptors, +} + +fn build_device_id(disk_path: &Path) -> result::Result { + let blk_metadata = match disk_path.metadata() { + Err(_) => return Err(Error::GetFileMetadata), + Ok(m) => m, + }; + // This is how kvmtool does it. + let device_id = format!( + "{}{}{}", + blk_metadata.st_dev(), + blk_metadata.st_rdev(), + blk_metadata.st_ino() + ); + Ok(device_id) +} + +pub fn build_disk_image_id(disk_path: &Path) -> Vec { + let mut default_disk_image_id = vec![0; VIRTIO_BLK_ID_BYTES as usize]; + match build_device_id(disk_path) { + Err(_) => { + warn!("Could not generate device id. We'll use a default."); + } + Ok(m) => { + // The kernel only knows to read a maximum of VIRTIO_BLK_ID_BYTES. + // This will also zero out any leftover bytes. + let disk_id = m.as_bytes(); + let bytes_to_copy = cmp::min(disk_id.len(), VIRTIO_BLK_ID_BYTES as usize); + default_disk_image_id[..bytes_to_copy].clone_from_slice(&disk_id[..bytes_to_copy]) + } + } + default_disk_image_id +} + +#[derive(Error, Debug)] +pub enum ExecuteError { + #[error("Bad request: {0}")] + BadRequest(Error), + #[error("Falied to flush: {0}")] + Flush(io::Error), + #[error("Failed to read: {0}")] + Read(GuestMemoryError), + #[error("Failed to read_exact: {0}")] + ReadExact(io::Error), + #[error("Failed to seek: {0}")] + Seek(io::Error), + #[error("Failed to write: {0}")] + Write(GuestMemoryError), + #[error("Failed to write_all: {0}")] + WriteAll(io::Error), + #[error("Unsupported request: {0}")] + Unsupported(u32), + #[error("Failed to submit io uring: {0}")] + SubmitIoUring(io::Error), + #[error("Failed to get guest address: {0}")] + GetHostAddress(GuestMemoryError), + #[error("Failed to async read: {0}")] + AsyncRead(AsyncIoError), + #[error("Failed to async write: {0}")] + AsyncWrite(AsyncIoError), + #[error("failed to async flush: {0}")] + AsyncFlush(AsyncIoError), + #[error("Failed allocating a temporary buffer: {0}")] + TemporaryBufferAllocation(io::Error), +} + +impl ExecuteError { + pub fn status(&self) -> u32 { + match *self { + ExecuteError::BadRequest(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::Flush(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::Read(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::ReadExact(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::Seek(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::Write(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::WriteAll(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::Unsupported(_) => VIRTIO_BLK_S_UNSUPP, + ExecuteError::SubmitIoUring(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::GetHostAddress(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::AsyncRead(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::AsyncWrite(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::AsyncFlush(_) => VIRTIO_BLK_S_IOERR, + ExecuteError::TemporaryBufferAllocation(_) => VIRTIO_BLK_S_IOERR, + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RequestType { + In, + Out, + Flush, + GetDeviceId, + Unsupported(u32), +} + +pub fn request_type( + mem: &vm_memory::GuestMemoryMmap, + desc_addr: GuestAddress, +) -> result::Result { + let type_ = mem.read_obj(desc_addr).map_err(Error::GuestMemory)?; + match type_ { + VIRTIO_BLK_T_IN => Ok(RequestType::In), + VIRTIO_BLK_T_OUT => Ok(RequestType::Out), + VIRTIO_BLK_T_FLUSH => Ok(RequestType::Flush), + VIRTIO_BLK_T_GET_ID => Ok(RequestType::GetDeviceId), + t => Ok(RequestType::Unsupported(t)), + } +} + +fn sector( + mem: &vm_memory::GuestMemoryMmap, + desc_addr: GuestAddress, +) -> result::Result { + const SECTOR_OFFSET: usize = 8; + let addr = match mem.checked_offset(desc_addr, SECTOR_OFFSET) { + Some(v) => v, + None => return Err(Error::CheckedOffset(desc_addr, SECTOR_OFFSET)), + }; + + mem.read_obj(addr).map_err(Error::GuestMemory) +} + +#[derive(Debug)] +pub struct AlignedOperation { + origin_ptr: u64, + aligned_ptr: u64, + size: usize, + layout: Layout, +} + +#[derive(Debug)] +pub struct Request { + pub request_type: RequestType, + pub sector: u64, + pub data_descriptors: Vec<(GuestAddress, u32)>, + pub status_addr: GuestAddress, + pub writeback: bool, + pub aligned_operations: Vec, +} + +impl Request { + pub fn parse( + desc_chain: &mut DescriptorChain>>, + access_platform: Option<&Arc>, + ) -> result::Result { + let hdr_desc = desc_chain + .next() + .ok_or(Error::DescriptorChainTooShort) + .map_err(|e| { + error!("Missing head descriptor"); + e + })?; + + // The head contains the request type which MUST be readable. + if hdr_desc.is_write_only() { + return Err(Error::UnexpectedWriteOnlyDescriptor); + } + + let hdr_desc_addr = hdr_desc + .addr() + .translate_gva(access_platform, hdr_desc.len() as usize); + + let mut req = Request { + request_type: request_type(desc_chain.memory(), hdr_desc_addr)?, + sector: sector(desc_chain.memory(), hdr_desc_addr)?, + data_descriptors: Vec::new(), + status_addr: GuestAddress(0), + writeback: true, + aligned_operations: Vec::new(), + }; + + let status_desc; + let mut desc = desc_chain + .next() + .ok_or(Error::DescriptorChainTooShort) + .map_err(|e| { + error!("Only head descriptor present: request = {:?}", req); + e + })?; + + if !desc.has_next() { + status_desc = desc; + // Only flush requests are allowed to skip the data descriptor. + if req.request_type != RequestType::Flush { + error!("Need a data descriptor: request = {:?}", req); + return Err(Error::DescriptorChainTooShort); + } + } else { + while desc.has_next() { + if desc.is_write_only() && req.request_type == RequestType::Out { + return Err(Error::UnexpectedWriteOnlyDescriptor); + } + if !desc.is_write_only() && req.request_type == RequestType::In { + return Err(Error::UnexpectedReadOnlyDescriptor); + } + if !desc.is_write_only() && req.request_type == RequestType::GetDeviceId { + return Err(Error::UnexpectedReadOnlyDescriptor); + } + + req.data_descriptors.push(( + desc.addr() + .translate_gva(access_platform, desc.len() as usize), + desc.len(), + )); + desc = desc_chain + .next() + .ok_or(Error::DescriptorChainTooShort) + .map_err(|e| { + error!("DescriptorChain corrupted: request = {:?}", req); + e + })?; + } + status_desc = desc; + } + + // The status MUST always be writable. + if !status_desc.is_write_only() { + return Err(Error::UnexpectedReadOnlyDescriptor); + } + + if status_desc.len() < 1 { + return Err(Error::DescriptorLengthTooSmall); + } + + req.status_addr = status_desc + .addr() + .translate_gva(access_platform, status_desc.len() as usize); + + Ok(req) + } + + pub fn execute( + &self, + disk: &mut T, + disk_nsectors: u64, + mem: &vm_memory::GuestMemoryMmap, + disk_id: &[u8], + ) -> result::Result { + disk.seek(SeekFrom::Start(self.sector << SECTOR_SHIFT)) + .map_err(ExecuteError::Seek)?; + let mut len = 0; + for (data_addr, data_len) in &self.data_descriptors { + let mut top: u64 = u64::from(*data_len) / SECTOR_SIZE; + if u64::from(*data_len) % SECTOR_SIZE != 0 { + top += 1; + } + top = top + .checked_add(self.sector) + .ok_or(ExecuteError::BadRequest(Error::InvalidOffset))?; + if top > disk_nsectors { + return Err(ExecuteError::BadRequest(Error::InvalidOffset)); + } + + match self.request_type { + RequestType::In => { + let mut buf = vec![0u8; *data_len as usize]; + disk.read_exact(&mut buf).map_err(ExecuteError::ReadExact)?; + mem.read_exact_volatile_from( + *data_addr, + &mut buf.as_slice(), + *data_len as usize, + ) + .map_err(ExecuteError::Read)?; + len += data_len; + } + RequestType::Out => { + let mut buf: Vec = Vec::new(); + mem.write_volatile_to(*data_addr, &mut buf, *data_len as usize) + .map_err(ExecuteError::Write)?; + disk.write_all(&buf).map_err(ExecuteError::WriteAll)?; + if !self.writeback { + disk.flush().map_err(ExecuteError::Flush)?; + } + } + RequestType::Flush => disk.flush().map_err(ExecuteError::Flush)?, + RequestType::GetDeviceId => { + if (*data_len as usize) < disk_id.len() { + return Err(ExecuteError::BadRequest(Error::InvalidOffset)); + } + mem.write_slice(disk_id, *data_addr) + .map_err(ExecuteError::Write)?; + } + RequestType::Unsupported(t) => return Err(ExecuteError::Unsupported(t)), + }; + } + Ok(len) + } + + pub fn execute_async( + &mut self, + mem: &vm_memory::GuestMemoryMmap, + disk_nsectors: u64, + disk_image: &mut dyn AsyncIo, + disk_id: &[u8], + user_data: u64, + ) -> result::Result { + let sector = self.sector; + let request_type = self.request_type; + let offset = (sector << SECTOR_SHIFT) as libc::off_t; + + let mut iovecs = Vec::new(); + for (data_addr, data_len) in &self.data_descriptors { + if *data_len == 0 { + continue; + } + let mut top: u64 = u64::from(*data_len) / SECTOR_SIZE; + if u64::from(*data_len) % SECTOR_SIZE != 0 { + top += 1; + } + top = top + .checked_add(sector) + .ok_or(ExecuteError::BadRequest(Error::InvalidOffset))?; + if top > disk_nsectors { + return Err(ExecuteError::BadRequest(Error::InvalidOffset)); + } + + let origin_ptr = mem + .get_slice(*data_addr, *data_len as usize) + .map_err(ExecuteError::GetHostAddress)? + .ptr_guard(); + + // Verify the buffer alignment. + // In case it's not properly aligned, an intermediate buffer is + // created with the correct alignment, and a copy from/to the + // origin buffer is performed, depending on the type of operation. + let iov_base = if (origin_ptr.as_ptr() as u64) % SECTOR_SIZE != 0 { + let layout = + Layout::from_size_align(*data_len as usize, SECTOR_SIZE as usize).unwrap(); + // Safe because layout has non-zero size + let aligned_ptr = unsafe { alloc_zeroed(layout) }; + if aligned_ptr.is_null() { + return Err(ExecuteError::TemporaryBufferAllocation( + io::Error::last_os_error(), + )); + } + + // We need to perform the copy beforehand in case we're writing + // data out. + if request_type == RequestType::Out { + // Safe because destination buffer has been allocated with + // the proper size. + unsafe { std::ptr::copy(origin_ptr.as_ptr(), aligned_ptr, *data_len as usize) }; + } + + // Store both origin and aligned pointers for complete_async() + // to process them. + self.aligned_operations.push(AlignedOperation { + origin_ptr: origin_ptr.as_ptr() as u64, + aligned_ptr: aligned_ptr as u64, + size: *data_len as usize, + layout, + }); + + aligned_ptr as *mut libc::c_void + } else { + origin_ptr.as_ptr() as *mut libc::c_void + }; + + let iovec = libc::iovec { + iov_base, + iov_len: *data_len as libc::size_t, + }; + iovecs.push(iovec); + } + + // Queue operations expected to be submitted. + match request_type { + RequestType::In => { + for (data_addr, data_len) in &self.data_descriptors { + mem.get_slice(*data_addr, *data_len as usize) + .map_err(ExecuteError::GetHostAddress)? + .bitmap() + .mark_dirty(0, *data_len as usize); + } + disk_image + .read_vectored(offset, iovecs, user_data) + .map_err(ExecuteError::AsyncRead)?; + } + RequestType::Out => { + disk_image + .write_vectored(offset, iovecs, user_data) + .map_err(ExecuteError::AsyncWrite)?; + } + RequestType::Flush => { + disk_image + .fsync(Some(user_data)) + .map_err(ExecuteError::AsyncFlush)?; + } + RequestType::GetDeviceId => { + let (data_addr, data_len) = if self.data_descriptors.len() == 1 { + (self.data_descriptors[0].0, self.data_descriptors[0].1) + } else { + return Err(ExecuteError::BadRequest(Error::TooManyDescriptors)); + }; + if (data_len as usize) < disk_id.len() { + return Err(ExecuteError::BadRequest(Error::InvalidOffset)); + } + mem.write_slice(disk_id, data_addr) + .map_err(ExecuteError::Write)?; + return Ok(false); + } + RequestType::Unsupported(t) => return Err(ExecuteError::Unsupported(t)), + } + + Ok(true) + } + + pub fn complete_async(&mut self) -> result::Result<(), Error> { + for aligned_operation in self.aligned_operations.drain(..) { + // We need to perform the copy after the data has been read inside + // the aligned buffer in case we're reading data in. + if self.request_type == RequestType::In { + // Safe because origin buffer has been allocated with the + // proper size. + unsafe { + std::ptr::copy( + aligned_operation.aligned_ptr as *const u8, + aligned_operation.origin_ptr as *mut u8, + aligned_operation.size, + ) + }; + } + + // Free the temporary aligned buffer. + // Safe because aligned_ptr was allocated by alloc_zeroed with the same + // layout + unsafe { + dealloc( + aligned_operation.aligned_ptr as *mut u8, + aligned_operation.layout, + ) + }; + } + + Ok(()) + } + + pub fn set_writeback(&mut self, writeback: bool) { + self.writeback = writeback + } +} + +#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] +#[repr(C, packed)] +pub struct VirtioBlockConfig { + pub capacity: u64, + pub size_max: u32, + pub seg_max: u32, + pub geometry: VirtioBlockGeometry, + pub blk_size: u32, + pub physical_block_exp: u8, + pub alignment_offset: u8, + pub min_io_size: u16, + pub opt_io_size: u32, + pub writeback: u8, + pub unused: u8, + pub num_queues: u16, + pub max_discard_sectors: u32, + pub max_discard_seg: u32, + pub discard_sector_alignment: u32, + pub max_write_zeroes_sectors: u32, + pub max_write_zeroes_seg: u32, + pub write_zeroes_may_unmap: u8, + pub unused1: [u8; 3], +} +#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] +#[repr(C, packed)] +pub struct VirtioBlockGeometry { + pub cylinders: u16, + pub heads: u8, + pub sectors: u8, +} + +// SAFETY: these data structures only contain a series of integers +unsafe impl ByteValued for VirtioBlockConfig {} +unsafe impl ByteValued for VirtioBlockGeometry {} + +/// Check if io_uring for block device can be used on the current system, as +/// it correctly supports the expected io_uring features. +pub fn block_io_uring_is_supported() -> bool { + let error_msg = "io_uring not supported:"; + + // Check we can create an io_uring instance, which effectively verifies + // that io_uring_setup() syscall is supported. + let io_uring = match IoUring::new(1) { + Ok(io_uring) => io_uring, + Err(e) => { + info!("{} failed to create io_uring instance: {}", error_msg, e); + return false; + } + }; + + let submitter = io_uring.submitter(); + + let mut probe = Probe::new(); + + // Check we can register a probe to validate supported operations. + match submitter.register_probe(&mut probe) { + Ok(_) => {} + Err(e) => { + info!("{} failed to register a probe: {}", error_msg, e); + return false; + } + } + + // Check IORING_OP_FSYNC is supported + if !probe.is_supported(opcode::Fsync::CODE) { + info!("{} IORING_OP_FSYNC operation not supported", error_msg); + return false; + } + + // Check IORING_OP_READV is supported + if !probe.is_supported(opcode::Readv::CODE) { + info!("{} IORING_OP_READV operation not supported", error_msg); + return false; + } + + // Check IORING_OP_WRITEV is supported + if !probe.is_supported(opcode::Writev::CODE) { + info!("{} IORING_OP_WRITEV operation not supported", error_msg); + return false; + } + + true +} + +pub trait AsyncAdaptor +where + F: Read + Write + Seek, +{ + fn read_vectored_sync( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + eventfd: &EventFd, + completion_list: &mut Vec<(u64, i32)>, + ) -> AsyncIoResult<()> { + // Convert libc::iovec into IoSliceMut + let mut slices = Vec::new(); + for iovec in iovecs.iter() { + slices.push(IoSliceMut::new(unsafe { std::mem::transmute(*iovec) })); + } + + let result = { + let mut file = self.file(); + + // Move the cursor to the right offset + file.seek(SeekFrom::Start(offset as u64)) + .map_err(AsyncIoError::ReadVectored)?; + + let mut r = 0; + 'buffers: for b in slices.iter_mut() { + let mut read = 0; + while read < b.len() { + let count = loop { + match file.read(&mut b[read..]) { + Err(err) if err.kind() == io::ErrorKind::Interrupted => continue, + result => break result.map_err(AsyncIoError::ReadVectored)?, + } + }; + if count == 0 { + break 'buffers; + } + read += count; + r += count; + } + } + r + }; + + let result = i32::try_from(result).map_err(|_| { + AsyncIoError::ReadVectored(io::Error::new( + io::ErrorKind::InvalidData, + "byte count exceeds completion range", + )) + })?; + completion_list.push((user_data, result)); + eventfd.write(1).unwrap(); + + Ok(()) + } + + fn write_vectored_sync( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + eventfd: &EventFd, + completion_list: &mut Vec<(u64, i32)>, + ) -> AsyncIoResult<()> { + // Convert libc::iovec into IoSlice + let mut slices = Vec::new(); + for iovec in iovecs.iter() { + slices.push(IoSlice::new(unsafe { std::mem::transmute(*iovec) })); + } + + let result = { + let mut file = self.file(); + + // Move the cursor to the right offset + file.seek(SeekFrom::Start(offset as u64)) + .map_err(AsyncIoError::WriteVectored)?; + + let mut r = 0; + for b in slices.iter() { + let mut written = 0; + while written < b.len() { + let count = loop { + match file.write(&b[written..]) { + Err(err) if err.kind() == io::ErrorKind::Interrupted => continue, + result => break result.map_err(AsyncIoError::WriteVectored)?, + } + }; + if count == 0 { + return Err(AsyncIoError::WriteVectored(io::Error::new( + io::ErrorKind::WriteZero, + "failed to write whole buffer", + ))); + } + written += count; + r += count; + } + } + r + }; + + let result = i32::try_from(result).map_err(|_| { + AsyncIoError::WriteVectored(io::Error::new( + io::ErrorKind::InvalidData, + "byte count exceeds completion range", + )) + })?; + completion_list.push((user_data, result)); + eventfd.write(1).unwrap(); + + Ok(()) + } + + fn fsync_sync( + &mut self, + user_data: Option, + eventfd: &EventFd, + completion_list: &mut Vec<(u64, i32)>, + ) -> AsyncIoResult<()> { + let result: i32 = { + let mut file = self.file(); + + // Flush + file.flush().map_err(AsyncIoError::Fsync)?; + + 0 + }; + + if let Some(user_data) = user_data { + completion_list.push((user_data, result)); + eventfd.write(1).unwrap(); + } + + Ok(()) + } + + fn file(&mut self) -> MutexGuard; +} + +pub enum ImageType { + FixedVhd, + Qcow2, + Raw, + Vhdx, +} + +#[cfg(test)] +mod tests { + use super::AsyncAdaptor; + use std::io::{self, Cursor, Read, Seek, SeekFrom, Write}; + use std::sync::{Mutex, MutexGuard}; + use vmm_sys_util::eventfd::EventFd; + + struct ShortIo { + inner: Cursor>, + max_read: usize, + max_write: usize, + interrupted_reads: usize, + interrupted_writes: usize, + } + + impl ShortIo { + fn new(data: Vec, max_read: usize, max_write: usize) -> Self { + Self { + inner: Cursor::new(data), + max_read, + max_write, + interrupted_reads: 0, + interrupted_writes: 0, + } + } + + fn with_interrupts(mut self, reads: usize, writes: usize) -> Self { + self.interrupted_reads = reads; + self.interrupted_writes = writes; + self + } + } + + impl Read for ShortIo { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + if self.interrupted_reads > 0 { + self.interrupted_reads -= 1; + return Err(io::Error::from(io::ErrorKind::Interrupted)); + } + let len = buf.len().min(self.max_read); + self.inner.read(&mut buf[..len]) + } + } + + impl Write for ShortIo { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.interrupted_writes > 0 { + self.interrupted_writes -= 1; + return Err(io::Error::from(io::ErrorKind::Interrupted)); + } + let len = buf.len().min(self.max_write); + self.inner.write(&buf[..len]) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } + } + + impl Seek for ShortIo { + fn seek(&mut self, pos: SeekFrom) -> io::Result { + self.inner.seek(pos) + } + } + + struct TestAdaptor { + file: Mutex, + } + + impl AsyncAdaptor for TestAdaptor { + fn file(&mut self) -> MutexGuard<'_, ShortIo> { + self.file.lock().unwrap() + } + } + + fn mutable_iovec(buf: &mut [u8]) -> libc::iovec { + libc::iovec { + iov_base: buf.as_mut_ptr().cast(), + iov_len: buf.len(), + } + } + + fn iovec(buf: &[u8]) -> libc::iovec { + libc::iovec { + iov_base: buf.as_ptr().cast_mut().cast(), + iov_len: buf.len(), + } + } + + #[test] + fn test_async_adaptor_read_vectored_sync_handles_partial_reads() { + let mut adaptor = TestAdaptor { + file: Mutex::new(ShortIo::new(b"0123456789".to_vec(), 2, usize::MAX)), + }; + let mut first = [0; 3]; + let mut second = [0; 4]; + let mut third = [0; 2]; + let iovecs = vec![ + mutable_iovec(&mut first), + mutable_iovec(&mut second), + mutable_iovec(&mut third), + ]; + let eventfd = EventFd::new(libc::EFD_NONBLOCK).unwrap(); + let mut completions = Vec::new(); + + adaptor + .read_vectored_sync(1, iovecs, 42, &eventfd, &mut completions) + .unwrap(); + + assert_eq!(&first, b"123"); + assert_eq!(&second, b"4567"); + assert_eq!(&third, b"89"); + assert_eq!(completions, vec![(42, 9)]); + assert_eq!(eventfd.read().unwrap(), 1); + } + + #[test] + fn test_async_adaptor_read_vectored_sync_stops_at_eof() { + let mut adaptor = TestAdaptor { + file: Mutex::new(ShortIo::new(b"abcde".to_vec(), 2, usize::MAX)), + }; + let mut first = [b'_'; 3]; + let mut second = [b'_'; 4]; + let mut third = [b'_'; 2]; + let iovecs = vec![ + mutable_iovec(&mut first), + mutable_iovec(&mut second), + mutable_iovec(&mut third), + ]; + let eventfd = EventFd::new(libc::EFD_NONBLOCK).unwrap(); + let mut completions = Vec::new(); + + adaptor + .read_vectored_sync(0, iovecs, 43, &eventfd, &mut completions) + .unwrap(); + + assert_eq!(&first, b"abc"); + assert_eq!(&second, b"de__"); + assert_eq!(&third, b"__"); + assert_eq!(completions, vec![(43, 5)]); + assert_eq!(eventfd.read().unwrap(), 1); + } + + #[test] + fn test_async_adaptor_read_vectored_sync_retries_interrupted() { + let mut adaptor = TestAdaptor { + file: Mutex::new(ShortIo::new(b"abcdef".to_vec(), 2, usize::MAX).with_interrupts(1, 0)), + }; + let mut buf = [0; 6]; + let eventfd = EventFd::new(libc::EFD_NONBLOCK).unwrap(); + let mut completions = Vec::new(); + + adaptor + .read_vectored_sync( + 0, + vec![mutable_iovec(&mut buf)], + 44, + &eventfd, + &mut completions, + ) + .unwrap(); + + assert_eq!(&buf, b"abcdef"); + assert_eq!(completions, vec![(44, 6)]); + assert_eq!(eventfd.read().unwrap(), 1); + } + + #[test] + fn test_async_adaptor_write_vectored_sync_handles_partial_writes() { + let mut adaptor = TestAdaptor { + file: Mutex::new(ShortIo::new(vec![b'_'; 12], usize::MAX, 2)), + }; + let first = b"abc"; + let second = b"defg"; + let third = b"hi"; + let iovecs = vec![iovec(first), iovec(second), iovec(third)]; + let eventfd = EventFd::new(libc::EFD_NONBLOCK).unwrap(); + let mut completions = Vec::new(); + + adaptor + .write_vectored_sync(1, iovecs, 44, &eventfd, &mut completions) + .unwrap(); + + assert_eq!( + adaptor.file.lock().unwrap().inner.get_ref(), + b"_abcdefghi__" + ); + assert_eq!(completions, vec![(44, 9)]); + assert_eq!(eventfd.read().unwrap(), 1); + } + + #[test] + fn test_async_adaptor_write_vectored_sync_retries_interrupted() { + let mut adaptor = TestAdaptor { + file: Mutex::new(ShortIo::new(vec![b'_'; 8], usize::MAX, 2).with_interrupts(0, 1)), + }; + let eventfd = EventFd::new(libc::EFD_NONBLOCK).unwrap(); + let mut completions = Vec::new(); + + adaptor + .write_vectored_sync(1, vec![iovec(b"abcdef")], 45, &eventfd, &mut completions) + .unwrap(); + + assert_eq!(adaptor.file.lock().unwrap().inner.get_ref(), b"_abcdef_"); + assert_eq!(completions, vec![(45, 6)]); + assert_eq!(eventfd.read().unwrap(), 1); + } + + #[test] + fn test_async_adaptor_write_vectored_sync_rejects_write_zero() { + let mut adaptor = TestAdaptor { + file: Mutex::new(ShortIo::new(Vec::new(), usize::MAX, 0)), + }; + let iovecs = vec![iovec(b"abc")]; + let eventfd = EventFd::new(libc::EFD_NONBLOCK).unwrap(); + let mut completions = Vec::new(); + + let err = adaptor + .write_vectored_sync(0, iovecs, 45, &eventfd, &mut completions) + .unwrap_err(); + + match err { + crate::async_io::AsyncIoError::WriteVectored(err) => { + assert_eq!(err.kind(), io::ErrorKind::WriteZero) + } + other => panic!("unexpected error: {other:?}"), + } + assert!(completions.is_empty()); + assert_eq!( + eventfd.read().unwrap_err().kind(), + io::ErrorKind::WouldBlock + ); + } +} + +const QCOW_MAGIC: u32 = 0x5146_49fb; +const VHDX_SIGN: u64 = 0x656C_6966_7864_6876; + +/// Determine image type through file parsing. +pub fn detect_image_type(f: &mut File) -> std::io::Result { + // We must create a buffer aligned on 512 bytes with a size being a + // multiple of 512 bytes as the file might be opened with O_DIRECT flag. + #[repr(align(512))] + struct Sector { + data: [u8; 512], + } + let mut s = Sector { data: [0; 512] }; + + f.read_exact(&mut s.data)?; + + // Check 4 first bytes to get the header value and determine the image type + let image_type = if u32::from_be_bytes(s.data[0..4].try_into().unwrap()) == QCOW_MAGIC { + ImageType::Qcow2 + } else if vhd::is_fixed_vhd(f)? { + ImageType::FixedVhd + } else if u64::from_le_bytes(s.data[0..8].try_into().unwrap()) == VHDX_SIGN { + ImageType::Vhdx + } else { + ImageType::Raw + }; + + Ok(image_type) +} diff --git a/vendor/cube-hypervisor/block_util/src/qcow_sync.rs b/vendor/cube-hypervisor/block_util/src/qcow_sync.rs new file mode 100644 index 0000000..990c4ec --- /dev/null +++ b/vendor/cube-hypervisor/block_util/src/qcow_sync.rs @@ -0,0 +1,103 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use crate::async_io::{AsyncIo, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult}; +use crate::AsyncAdaptor; +use qcow::{QcowFile, RawFile, Result as QcowResult}; +use std::fs::File; +use std::io::{Seek, SeekFrom}; +use std::sync::{Arc, Mutex, MutexGuard}; +use vmm_sys_util::eventfd::EventFd; + +pub struct QcowDiskSync { + qcow_file: Arc>, +} + +impl QcowDiskSync { + pub fn new(file: File, direct_io: bool) -> QcowResult { + Ok(QcowDiskSync { + qcow_file: Arc::new(Mutex::new(QcowFile::from(RawFile::new(file, direct_io))?)), + }) + } +} + +impl DiskFile for QcowDiskSync { + fn size(&mut self) -> DiskFileResult { + let mut file = self.qcow_file.lock().unwrap(); + + file.seek(SeekFrom::End(0)).map_err(DiskFileError::Size) + } + + fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult> { + Ok(Box::new(QcowSync::new(self.qcow_file.clone())) as Box) + } +} + +pub struct QcowSync { + qcow_file: Arc>, + eventfd: EventFd, + completion_list: Vec<(u64, i32)>, +} + +impl QcowSync { + pub fn new(qcow_file: Arc>) -> Self { + QcowSync { + qcow_file, + eventfd: EventFd::new(libc::EFD_NONBLOCK) + .expect("Failed creating EventFd for QcowSync"), + completion_list: Vec::new(), + } + } +} + +impl AsyncAdaptor for Arc> { + fn file(&mut self) -> MutexGuard { + self.lock().unwrap() + } +} + +impl AsyncIo for QcowSync { + fn notifier(&self) -> &EventFd { + &self.eventfd + } + + fn read_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + self.qcow_file.read_vectored_sync( + offset, + iovecs, + user_data, + &self.eventfd, + &mut self.completion_list, + ) + } + + fn write_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + self.qcow_file.write_vectored_sync( + offset, + iovecs, + user_data, + &self.eventfd, + &mut self.completion_list, + ) + } + + fn fsync(&mut self, user_data: Option) -> AsyncIoResult<()> { + self.qcow_file + .fsync_sync(user_data, &self.eventfd, &mut self.completion_list) + } + + fn complete(&mut self) -> Vec<(u64, i32)> { + self.completion_list.drain(..).collect() + } +} diff --git a/vendor/cube-hypervisor/block_util/src/raw_async.rs b/vendor/cube-hypervisor/block_util/src/raw_async.rs new file mode 100644 index 0000000..aab48ed --- /dev/null +++ b/vendor/cube-hypervisor/block_util/src/raw_async.rs @@ -0,0 +1,167 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use crate::async_io::{ + AsyncIo, AsyncIoError, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult, DiskTopology, +}; +use io_uring::{opcode, squeue, types, IoUring}; +use std::fs::File; +use std::io::{Seek, SeekFrom}; +use std::os::unix::io::{AsRawFd, RawFd}; +use vmm_sys_util::eventfd::EventFd; + +pub struct RawFileDisk { + file: File, +} + +impl RawFileDisk { + pub fn new(file: File) -> Self { + RawFileDisk { file } + } +} + +impl DiskFile for RawFileDisk { + fn size(&mut self) -> DiskFileResult { + self.file + .seek(SeekFrom::End(0)) + .map_err(DiskFileError::Size) + } + + fn new_async_io(&self, ring_depth: u32) -> DiskFileResult> { + Ok(Box::new( + RawFileAsync::new(self.file.as_raw_fd(), ring_depth) + .map_err(DiskFileError::NewAsyncIo)?, + ) as Box) + } + + fn topology(&mut self) -> DiskTopology { + if let Ok(topology) = DiskTopology::probe(&mut self.file) { + topology + } else { + warn!("Unable to get device topology. Using default topology"); + DiskTopology::default() + } + } +} + +pub struct RawFileAsync { + fd: RawFd, + io_uring: IoUring, + eventfd: EventFd, +} + +impl RawFileAsync { + pub fn new(fd: RawFd, ring_depth: u32) -> std::io::Result { + let io_uring = IoUring::new(ring_depth)?; + let eventfd = EventFd::new(libc::EFD_NONBLOCK)?; + + // Register the io_uring eventfd that will notify when something in + // the completion queue is ready. + io_uring.submitter().register_eventfd(eventfd.as_raw_fd())?; + + Ok(RawFileAsync { + fd, + io_uring, + eventfd, + }) + } +} + +impl AsyncIo for RawFileAsync { + fn notifier(&self) -> &EventFd { + &self.eventfd + } + + fn read_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + let (submitter, mut sq, _) = self.io_uring.split(); + + // Safe because we know the file descriptor is valid and we + // relied on vm-memory to provide the buffer address. + let _ = unsafe { + sq.push( + &opcode::Readv::new(types::Fd(self.fd), iovecs.as_ptr(), iovecs.len() as u32) + .offset(offset) + .build() + .flags(squeue::Flags::ASYNC) + .user_data(user_data), + ) + }; + + // Update the submission queue and submit new operations to the + // io_uring instance. + sq.sync(); + submitter.submit().map_err(AsyncIoError::ReadVectored)?; + + Ok(()) + } + + fn write_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + let (submitter, mut sq, _) = self.io_uring.split(); + + // Safe because we know the file descriptor is valid and we + // relied on vm-memory to provide the buffer address. + let _ = unsafe { + sq.push( + &opcode::Writev::new(types::Fd(self.fd), iovecs.as_ptr(), iovecs.len() as u32) + .offset(offset) + .build() + .flags(squeue::Flags::ASYNC) + .user_data(user_data), + ) + }; + + // Update the submission queue and submit new operations to the + // io_uring instance. + sq.sync(); + submitter.submit().map_err(AsyncIoError::WriteVectored)?; + + Ok(()) + } + + fn fsync(&mut self, user_data: Option) -> AsyncIoResult<()> { + if let Some(user_data) = user_data { + let (submitter, mut sq, _) = self.io_uring.split(); + + // Safe because we know the file descriptor is valid. + let _ = unsafe { + sq.push( + &opcode::Fsync::new(types::Fd(self.fd)) + .build() + .flags(squeue::Flags::ASYNC) + .user_data(user_data), + ) + }; + + // Update the submission queue and submit new operations to the + // io_uring instance. + sq.sync(); + submitter.submit().map_err(AsyncIoError::Fsync)?; + } else { + unsafe { libc::fsync(self.fd) }; + } + + Ok(()) + } + + fn complete(&mut self) -> Vec<(u64, i32)> { + let mut completion_list = Vec::new(); + + let cq = self.io_uring.completion(); + for cq_entry in cq { + completion_list.push((cq_entry.user_data(), cq_entry.result())); + } + + completion_list + } +} diff --git a/vendor/cube-hypervisor/block_util/src/raw_sync.rs b/vendor/cube-hypervisor/block_util/src/raw_sync.rs new file mode 100644 index 0000000..4e9e844 --- /dev/null +++ b/vendor/cube-hypervisor/block_util/src/raw_sync.rs @@ -0,0 +1,352 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use crate::async_io::{ + AsyncIo, AsyncIoError, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult, DiskTopology, +}; +use std::fs::File; +use std::io::{self, Seek, SeekFrom}; +use std::os::unix::io::{AsRawFd, RawFd}; +use vmm_sys_util::eventfd::EventFd; + +fn run_vectored_io( + mut iovecs: Vec, + mut offset: libc::off_t, + write: bool, + mut operation: F, +) -> io::Result +where + F: FnMut(&[libc::iovec], libc::off_t) -> io::Result, +{ + let mut first = 0; + let mut total = 0usize; + + loop { + while first < iovecs.len() && iovecs[first].iov_len == 0 { + first += 1; + } + if first == iovecs.len() { + return Ok(total); + } + + let count = match operation(&iovecs[first..], offset) { + Err(err) if err.kind() == io::ErrorKind::Interrupted => continue, + result => result?, + }; + if count == 0 { + if write { + return Err(io::Error::new( + io::ErrorKind::WriteZero, + "failed to write whole buffer", + )); + } + return Ok(total); + } + + let remaining = iovecs[first..] + .iter() + .try_fold(0usize, |remaining, iovec| { + remaining.checked_add(iovec.iov_len) + }) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "byte count overflow"))?; + if count > remaining { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "vectored I/O returned more bytes than requested", + )); + } + + total = total + .checked_add(count) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "byte count overflow"))?; + let count_offset = libc::off_t::try_from(count) + .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "offset overflow"))?; + offset = offset + .checked_add(count_offset) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "offset overflow"))?; + + let mut completed = count; + while completed > 0 { + if completed >= iovecs[first].iov_len { + completed -= iovecs[first].iov_len; + first += 1; + } else { + iovecs[first].iov_base = + unsafe { iovecs[first].iov_base.cast::().add(completed).cast() }; + iovecs[first].iov_len -= completed; + completed = 0; + } + } + } +} + +pub struct RawFileDiskSync { + file: File, +} + +impl RawFileDiskSync { + pub fn new(file: File) -> Self { + RawFileDiskSync { file } + } +} + +impl DiskFile for RawFileDiskSync { + fn size(&mut self) -> DiskFileResult { + self.file + .seek(SeekFrom::End(0)) + .map_err(DiskFileError::Size) + } + + fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult> { + Ok(Box::new(RawFileSync::new(self.file.as_raw_fd())) as Box) + } + + fn topology(&mut self) -> DiskTopology { + if let Ok(topology) = DiskTopology::probe(&mut self.file) { + topology + } else { + warn!("Unable to get device topology. Using default topology"); + DiskTopology::default() + } + } +} + +pub struct RawFileSync { + fd: RawFd, + eventfd: EventFd, + completion_list: Vec<(u64, i32)>, +} + +impl RawFileSync { + pub fn new(fd: RawFd) -> Self { + RawFileSync { + fd, + eventfd: EventFd::new(libc::EFD_NONBLOCK).expect("Failed creating EventFd for RawFile"), + completion_list: Vec::new(), + } + } +} + +impl AsyncIo for RawFileSync { + fn notifier(&self) -> &EventFd { + &self.eventfd + } + + fn read_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + let result = run_vectored_io(iovecs, offset, false, |iovecs, offset| { + let result = unsafe { + libc::preadv( + self.fd as libc::c_int, + iovecs.as_ptr(), + iovecs.len() as libc::c_int, + offset, + ) + }; + if result < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(result as usize) + } + }) + .map_err(AsyncIoError::ReadVectored)?; + let result = i32::try_from(result).map_err(|_| { + AsyncIoError::ReadVectored(io::Error::new( + io::ErrorKind::InvalidData, + "byte count exceeds completion range", + )) + })?; + + self.completion_list.push((user_data, result)); + self.eventfd.write(1).unwrap(); + + Ok(()) + } + + fn write_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + let result = run_vectored_io(iovecs, offset, true, |iovecs, offset| { + let result = unsafe { + libc::pwritev( + self.fd as libc::c_int, + iovecs.as_ptr(), + iovecs.len() as libc::c_int, + offset, + ) + }; + if result < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(result as usize) + } + }) + .map_err(AsyncIoError::WriteVectored)?; + let result = i32::try_from(result).map_err(|_| { + AsyncIoError::WriteVectored(io::Error::new( + io::ErrorKind::InvalidData, + "byte count exceeds completion range", + )) + })?; + + self.completion_list.push((user_data, result)); + self.eventfd.write(1).unwrap(); + + Ok(()) + } + + fn fsync(&mut self, user_data: Option) -> AsyncIoResult<()> { + let result = unsafe { libc::fsync(self.fd as libc::c_int) }; + if result < 0 { + return Err(AsyncIoError::Fsync(std::io::Error::last_os_error())); + } + + if let Some(user_data) = user_data { + self.completion_list.push((user_data, result)); + self.eventfd.write(1).unwrap(); + } + + Ok(()) + } + + fn complete(&mut self) -> Vec<(u64, i32)> { + self.completion_list.drain(..).collect() + } +} + +#[cfg(test)] +mod tests { + use super::run_vectored_io; + use std::io; + + fn iovec(buf: &mut [u8]) -> libc::iovec { + libc::iovec { + iov_base: buf.as_mut_ptr().cast(), + iov_len: buf.len(), + } + } + + #[test] + fn test_run_vectored_io_advances_partial_operations() { + let mut first = [0; 3]; + let mut second = [0; 4]; + let mut third = [0; 2]; + let first_ptr = first.as_mut_ptr() as usize; + let second_ptr = second.as_mut_ptr() as usize; + let mut calls = Vec::new(); + let mut results = [2, 3, 4].into_iter(); + + let result = run_vectored_io( + vec![iovec(&mut first), iovec(&mut second), iovec(&mut third)], + 7, + false, + |iovecs, offset| { + calls.push(( + offset, + iovecs + .iter() + .map(|iov| (iov.iov_base as usize, iov.iov_len)) + .collect::>(), + )); + Ok(results.next().unwrap()) + }, + ) + .unwrap(); + + assert_eq!(result, 9); + assert_eq!( + calls[0], + ( + 7, + vec![ + (first_ptr, 3), + (second_ptr, 4), + (third.as_mut_ptr() as usize, 2) + ] + ) + ); + assert_eq!( + calls[1], + ( + 9, + vec![ + (first_ptr + 2, 1), + (second_ptr, 4), + (third.as_mut_ptr() as usize, 2) + ] + ) + ); + assert_eq!( + calls[2], + ( + 12, + vec![(second_ptr + 2, 2), (third.as_mut_ptr() as usize, 2)] + ) + ); + } + + #[test] + fn test_run_vectored_io_stops_read_at_eof() { + let mut buf = [0; 4]; + let mut results = [2, 0].into_iter(); + + let result = run_vectored_io(vec![iovec(&mut buf)], 0, false, |_, _| { + Ok(results.next().unwrap()) + }) + .unwrap(); + + assert_eq!(result, 2); + } + + #[test] + fn test_run_vectored_io_rejects_write_zero() { + let mut buf = [0; 4]; + + let err = run_vectored_io(vec![iovec(&mut buf)], 0, true, |_, _| Ok(0)).unwrap_err(); + + assert_eq!(err.kind(), io::ErrorKind::WriteZero); + } + + #[test] + fn test_run_vectored_io_retries_interrupted_operation() { + let mut buf = [0; 4]; + let base = buf.as_mut_ptr() as usize; + let mut calls = Vec::new(); + + let result = run_vectored_io(vec![iovec(&mut buf)], 5, false, |iovecs, offset| { + calls.push((offset, iovecs[0].iov_base as usize, iovecs[0].iov_len)); + if calls.len() == 1 { + Err(io::Error::from(io::ErrorKind::Interrupted)) + } else { + Ok(4) + } + }) + .unwrap(); + + assert_eq!(result, 4); + assert_eq!(calls, vec![(5, base, 4), (5, base, 4)]); + } + + #[test] + fn test_run_vectored_io_ignores_empty_iovecs() { + let mut empty = []; + let mut calls = 0; + + let result = run_vectored_io(vec![iovec(&mut empty)], 0, true, |_, _| { + calls += 1; + Ok(0) + }) + .unwrap(); + + assert_eq!(result, 0); + assert_eq!(calls, 0); + } +} diff --git a/vendor/cube-hypervisor/block_util/src/vhd.rs b/vendor/cube-hypervisor/block_util/src/vhd.rs new file mode 100644 index 0000000..b47ce90 --- /dev/null +++ b/vendor/cube-hypervisor/block_util/src/vhd.rs @@ -0,0 +1,221 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use std::convert::TryInto; +use std::fs::File; +use std::io::{Read, Seek, SeekFrom}; + +#[derive(Clone, Copy)] +pub struct VhdFooter { + cookie: u64, + features: u32, + file_format_version: u32, + data_offset: u64, + time_stamp: u32, + creator_application: u32, + creator_version: u32, + creator_host_os: u32, + original_size: u64, + current_size: u64, + disk_geometry: u32, + disk_type: u32, + checksum: u32, + unique_id: u128, + saved_state: u8, +} + +impl VhdFooter { + pub fn new(file: &mut File) -> std::io::Result { + // We must create a buffer aligned on 512 bytes with a size being a + // multiple of 512 bytes as the file might be opened with O_DIRECT flag. + #[repr(align(512))] + struct Sector { + data: [u8; 512], + } + let mut s = Sector { data: [0; 512] }; + + // Place the cursor 512 bytes before the end of the file, as this is + // where the footer starts. + file.seek(SeekFrom::End(-512))?; + + // Fill in the VhdFooter structure + file.read_exact(&mut s.data)?; + + Ok(VhdFooter { + cookie: u64::from_be_bytes(s.data[0..8].try_into().unwrap()), + features: u32::from_be_bytes(s.data[8..12].try_into().unwrap()), + file_format_version: u32::from_be_bytes(s.data[12..16].try_into().unwrap()), + data_offset: u64::from_be_bytes(s.data[16..24].try_into().unwrap()), + time_stamp: u32::from_be_bytes(s.data[24..28].try_into().unwrap()), + creator_application: u32::from_be_bytes(s.data[28..32].try_into().unwrap()), + creator_version: u32::from_be_bytes(s.data[32..36].try_into().unwrap()), + creator_host_os: u32::from_be_bytes(s.data[36..40].try_into().unwrap()), + original_size: u64::from_be_bytes(s.data[40..48].try_into().unwrap()), + current_size: u64::from_be_bytes(s.data[48..56].try_into().unwrap()), + disk_geometry: u32::from_be_bytes(s.data[56..60].try_into().unwrap()), + disk_type: u32::from_be_bytes(s.data[60..64].try_into().unwrap()), + checksum: u32::from_be_bytes(s.data[64..68].try_into().unwrap()), + unique_id: u128::from_be_bytes(s.data[68..84].try_into().unwrap()), + saved_state: u8::from_be_bytes(s.data[84..85].try_into().unwrap()), + }) + } + + pub fn cookie(&self) -> u64 { + self.cookie + } + pub fn features(&self) -> u32 { + self.features + } + pub fn file_format_version(&self) -> u32 { + self.file_format_version + } + pub fn data_offset(&self) -> u64 { + self.data_offset + } + pub fn time_stamp(&self) -> u32 { + self.time_stamp + } + pub fn creator_application(&self) -> u32 { + self.creator_application + } + pub fn creator_version(&self) -> u32 { + self.creator_version + } + pub fn creator_host_os(&self) -> u32 { + self.creator_host_os + } + pub fn original_size(&self) -> u64 { + self.original_size + } + pub fn current_size(&self) -> u64 { + self.current_size + } + pub fn disk_geometry(&self) -> u32 { + self.disk_geometry + } + pub fn disk_type(&self) -> u32 { + self.disk_type + } + pub fn checksum(&self) -> u32 { + self.checksum + } + pub fn unique_id(&self) -> u128 { + self.unique_id + } + pub fn saved_state(&self) -> u8 { + self.saved_state + } +} + +/// Determine image type through file parsing. +pub fn is_fixed_vhd(f: &mut File) -> std::io::Result { + let footer = VhdFooter::new(f)?; + + // "conectix" => 0x636f6e6563746978 + Ok(footer.cookie() == 0x636f6e6563746978 + && footer.file_format_version() == 0x0001_0000 + && footer.data_offset() == 0xffff_ffff_ffff_ffff + && footer.disk_type() == 0x2) +} + +#[cfg(test)] +mod tests { + use super::{is_fixed_vhd, VhdFooter}; + use std::fs::File; + use std::io::{Seek, SeekFrom, Write}; + use vmm_sys_util::tempfile::TempFile; + + fn valid_fixed_vhd_footer() -> Vec { + vec![ + 0x63, 0x6f, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x78, // cookie + 0x00, 0x00, 0x00, 0x02, // features + 0x00, 0x01, 0x00, 0x00, // file format version + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // data offset + 0x27, 0xa6, 0xa6, 0x5d, // time stamp + 0x71, 0x65, 0x6d, 0x75, // creator application + 0x00, 0x05, 0x00, 0x03, // creator version + 0x57, 0x69, 0x32, 0x6b, // creator host os + 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // original size + 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // current size + 0x11, 0xe0, 0x10, 0x3f, // disk geometry + 0x00, 0x00, 0x00, 0x02, // disk type + 0x00, 0x00, 0x00, 0x00, // checksum + 0x98, 0x7b, 0xb1, 0xcd, 0x84, 0x14, 0x41, 0xfc, 0xa4, 0xab, 0xd0, 0x69, 0x45, 0x2b, + 0xf2, 0x23, // unique id + 0x00, // saved state + ] + } + + fn valid_dynamic_vhd_footer() -> Vec { + vec![ + 0x63, 0x6f, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x78, // cookie + 0x00, 0x00, 0x00, 0x02, // features + 0x00, 0x01, 0x00, 0x00, // file format version + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // data offset + 0x27, 0xa6, 0xa6, 0x5d, // time stamp + 0x71, 0x65, 0x6d, 0x75, // creator application + 0x00, 0x05, 0x00, 0x03, // creator version + 0x57, 0x69, 0x32, 0x6b, // creator host os + 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // original size + 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // current size + 0x11, 0xe0, 0x10, 0x3f, // disk geometry + 0x00, 0x00, 0x00, 0x03, // disk type + 0x00, 0x00, 0x00, 0x00, // checksum + 0x98, 0x7b, 0xb1, 0xcd, 0x84, 0x14, 0x41, 0xfc, 0xa4, 0xab, 0xd0, 0x69, 0x45, 0x2b, + 0xf2, 0x23, // unique id + 0x00, // saved state + ] + } + + fn with_file(footer: &[u8], mut testfn: F) + where + F: FnMut(File), + { + let mut disk_file: File = TempFile::new().unwrap().into_file(); + disk_file.set_len(0x1000_0200).unwrap(); + disk_file.seek(SeekFrom::Start(0x1000_0000)).unwrap(); + disk_file.write_all(footer).unwrap(); + + testfn(disk_file); // File closed when the function exits. + } + + #[test] + fn test_check_vhd_footer() { + with_file(&valid_fixed_vhd_footer(), |mut file: File| { + let vhd_footer = VhdFooter::new(&mut file).expect("Failed to create VHD footer"); + assert_eq!(vhd_footer.cookie(), 0x636f_6e65_6374_6978); + assert_eq!(vhd_footer.features(), 0x0000_0002); + assert_eq!(vhd_footer.file_format_version(), 0x0001_0000); + assert_eq!(vhd_footer.data_offset(), 0xffff_ffff_ffff_ffff); + assert_eq!(vhd_footer.time_stamp(), 0x27a6_a65d); + assert_eq!(vhd_footer.creator_application(), 0x7165_6d75); + assert_eq!(vhd_footer.creator_version(), 0x0005_0003); + assert_eq!(vhd_footer.creator_host_os(), 0x5769_326b); + assert_eq!(vhd_footer.original_size(), 0x0000_0000_1000_0000); + assert_eq!(vhd_footer.current_size(), 0x0000_0000_1000_0000); + assert_eq!(vhd_footer.disk_geometry(), 0x11e0_103f); + assert_eq!(vhd_footer.disk_type(), 0x0000_0002); + assert_eq!(vhd_footer.checksum(), 0x0000_0000); + assert_eq!( + vhd_footer.unique_id(), + 0x987b_b1cd_8414_41fc_a4ab_d069_452b_f223 + ); + assert_eq!(vhd_footer.saved_state(), 0x00); + }); + } + + #[test] + fn test_is_fixed_vhd() { + with_file(&valid_fixed_vhd_footer(), |mut file: File| { + assert!(is_fixed_vhd(&mut file).unwrap()); + }); + } + + #[test] + fn test_is_not_fixed_vhd() { + with_file(&valid_dynamic_vhd_footer(), |mut file: File| { + assert!(!(is_fixed_vhd(&mut file).unwrap())); + }); + } +} diff --git a/vendor/cube-hypervisor/block_util/src/vhdx_sync.rs b/vendor/cube-hypervisor/block_util/src/vhdx_sync.rs new file mode 100644 index 0000000..48b0e3a --- /dev/null +++ b/vendor/cube-hypervisor/block_util/src/vhdx_sync.rs @@ -0,0 +1,102 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use crate::async_io::{AsyncIo, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult}; +use crate::AsyncAdaptor; +use std::fs::File; +use std::sync::{Arc, Mutex, MutexGuard}; +use vhdx::vhdx::{Result as VhdxResult, Vhdx}; +use vmm_sys_util::eventfd::EventFd; + +pub struct VhdxDiskSync { + vhdx_file: Arc>, +} + +impl VhdxDiskSync { + pub fn new(f: File) -> VhdxResult { + Ok(VhdxDiskSync { + vhdx_file: Arc::new(Mutex::new(Vhdx::new(f)?)), + }) + } +} + +impl DiskFile for VhdxDiskSync { + fn size(&mut self) -> DiskFileResult { + Ok(self.vhdx_file.lock().unwrap().virtual_disk_size()) + } + + fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult> { + Ok( + Box::new(VhdxSync::new(self.vhdx_file.clone()).map_err(DiskFileError::NewAsyncIo)?) + as Box, + ) + } +} + +pub struct VhdxSync { + vhdx_file: Arc>, + eventfd: EventFd, + completion_list: Vec<(u64, i32)>, +} + +impl VhdxSync { + pub fn new(vhdx_file: Arc>) -> std::io::Result { + Ok(VhdxSync { + vhdx_file, + eventfd: EventFd::new(libc::EFD_NONBLOCK)?, + completion_list: Vec::new(), + }) + } +} + +impl AsyncAdaptor for Arc> { + fn file(&mut self) -> MutexGuard { + self.lock().unwrap() + } +} + +impl AsyncIo for VhdxSync { + fn notifier(&self) -> &EventFd { + &self.eventfd + } + + fn read_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + self.vhdx_file.read_vectored_sync( + offset, + iovecs, + user_data, + &self.eventfd, + &mut self.completion_list, + ) + } + + fn write_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()> { + self.vhdx_file.write_vectored_sync( + offset, + iovecs, + user_data, + &self.eventfd, + &mut self.completion_list, + ) + } + + fn fsync(&mut self, user_data: Option) -> AsyncIoResult<()> { + self.vhdx_file + .fsync_sync(user_data, &self.eventfd, &mut self.completion_list) + } + + fn complete(&mut self) -> Vec<(u64, i32)> { + self.completion_list.drain(..).collect() + } +} diff --git a/vendor/cube-hypervisor/build.rs b/vendor/cube-hypervisor/build.rs new file mode 100644 index 0000000..f74e84f --- /dev/null +++ b/vendor/cube-hypervisor/build.rs @@ -0,0 +1,47 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#[macro_use(crate_version)] +extern crate clap; + +use std::process::Command; + +fn main() { + // Priority: CUBE_VERSION env > git describe > crate_version > fallback + let version = std::env::var("CUBE_VERSION") + .ok() + .or_else(|| git_output(&["describe", "--dirty"])) + .unwrap_or_else(default_version); + + let build_time = std::env::var("CUBE_BUILD_TIME").unwrap_or_else(|_| "unknown".to_string()); + let commit = std::env::var("CUBE_COMMIT") + .ok() + .or_else(|| git_output(&["rev-parse", "HEAD"]).filter(|s| !s.is_empty())) + .unwrap_or_else(|| "unknown".to_string()); + + // Embed metadata into BUILT_VERSION so clap's --version includes it. + let built_version = format!("{} ({}) built at {}", version, commit, build_time); + + println!("cargo:rustc-env=BUILT_VERSION={}", built_version); + println!("cargo:rustc-env=SNAPSHOT_VERSION=1.0.0"); + println!("cargo:rerun-if-env-changed=CUBE_VERSION"); + println!("cargo:rerun-if-env-changed=CUBE_COMMIT"); + println!("cargo:rerun-if-env-changed=CUBE_BUILD_TIME"); +} + +fn git_output(args: &[&str]) -> Option { + let output = Command::new("git").args(args).output().ok()?; + if !output.status.success() { + return None; + } + + String::from_utf8(output.stdout) + .ok() + .map(|stdout| stdout.trim().to_string()) +} + +fn default_version() -> String { + "v".to_owned() + crate_version!() +} diff --git a/vendor/cube-hypervisor/devices/Cargo.toml b/vendor/cube-hypervisor/devices/Cargo.toml new file mode 100644 index 0000000..5538e49 --- /dev/null +++ b/vendor/cube-hypervisor/devices/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "devices" +version = "0.1.0" +authors = ["The Chromium OS Authors"] +edition = "2021" + +[dependencies] +acpi_tables = { path = "../acpi_tables" } +anyhow = "1.0.86" +arch = { path = "../arch" } +bitflags = "2.6.0" +byteorder = "1.4.3" +event_monitor = { path = "../event_monitor" } +event_notifier = { path = "../event_notifier" } +hypervisor = { path = "../hypervisor" } +libc = "0.2.137" +log = "0.4.17" +logging = { path = "../logging" } +pci = { path = "../pci" } +serde = { workspace = true, features = ["derive"] } +phf = { version = "0.11", features = ["macros"] } +thiserror = "1.0.30" +tpm = { path = "../tpm" } +vm-allocator = { path = "../vm-allocator" } +vm-device = { path = "../vm-device" } +vm-memory = { workspace = true } +vm-migration = { path = "../vm-migration" } +vmm-sys-util = "0.12.1" + +[target.'cfg(target_arch = "aarch64")'.dependencies] +arch = { path = "../arch" } + +[features] +default = [] +lib_support = [] diff --git a/vendor/cube-hypervisor/devices/src/acpi.rs b/vendor/cube-hypervisor/devices/src/acpi.rs new file mode 100644 index 0000000..2f29c08 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/acpi.rs @@ -0,0 +1,231 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use super::AcpiNotificationFlags; +use acpi_tables::{aml, aml::Aml}; +use std::sync::{Arc, Barrier}; +use std::time::Instant; +use vm_device::interrupt::InterruptSourceGroup; +use vm_device::BusDevice; +use vm_memory::GuestAddress; +use vmm_sys_util::eventfd::EventFd; + +pub const GED_DEVICE_ACPI_SIZE: usize = 0x1; + +/// A device for handling ACPI shutdown and reboot +pub struct AcpiShutdownDevice { + exit_evt: EventFd, + reset_evt: EventFd, +} + +impl AcpiShutdownDevice { + /// Constructs a device that will signal the given event when the guest requests it. + pub fn new(exit_evt: EventFd, reset_evt: EventFd) -> AcpiShutdownDevice { + AcpiShutdownDevice { + exit_evt, + reset_evt, + } + } +} + +// Same I/O port used for shutdown and reboot +impl BusDevice for AcpiShutdownDevice { + // Spec has all fields as zero + fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) { + data.fill(0) + } + + fn write(&mut self, _base: u64, _offset: u64, data: &[u8]) -> Option> { + if data[0] == 1 { + warn!("Shutdown because of ACPI Reboot signalled"); + if let Err(e) = self.reset_evt.write(1) { + error!("Error triggering ACPI reset event: {}", e); + } + } + // The ACPI DSDT table specifies the S5 sleep state (shutdown) as value 5 + const S5_SLEEP_VALUE: u8 = 5; + const SLEEP_STATUS_EN_BIT: u8 = 5; + const SLEEP_VALUE_BIT: u8 = 2; + if data[0] == (S5_SLEEP_VALUE << SLEEP_VALUE_BIT) | (1 << SLEEP_STATUS_EN_BIT) { + info!("ACPI Shutdown signalled and set exit_evt"); + + if let Err(e) = self.exit_evt.write(1) { + error!("Error triggering ACPI shutdown event: {}", e); + } + } + None + } +} + +/// A device for handling ACPI GED event generation +pub struct AcpiGedDevice { + interrupt: Arc, + notification_type: AcpiNotificationFlags, + ged_irq: u32, + address: GuestAddress, +} + +impl AcpiGedDevice { + pub fn new( + interrupt: Arc, + ged_irq: u32, + address: GuestAddress, + ) -> AcpiGedDevice { + AcpiGedDevice { + interrupt, + notification_type: AcpiNotificationFlags::NO_DEVICES_CHANGED, + ged_irq, + address, + } + } + + pub fn notify( + &mut self, + notification_type: AcpiNotificationFlags, + ) -> Result<(), std::io::Error> { + self.notification_type |= notification_type; + self.interrupt.trigger(0) + } + + pub fn irq(&self) -> u32 { + self.ged_irq + } +} + +// I/O port reports what type of notification was made +impl BusDevice for AcpiGedDevice { + // Spec has all fields as zero + fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) { + data[0] = self.notification_type.bits(); + self.notification_type = AcpiNotificationFlags::NO_DEVICES_CHANGED; + } +} + +impl Aml for AcpiGedDevice { + fn append_aml_bytes(&self, bytes: &mut Vec) { + aml::Device::new( + "_SB_.GEC_".into(), + vec![ + &aml::Name::new("_HID".into(), &aml::EisaName::new("PNP0A06")), + &aml::Name::new("_UID".into(), &"Generic Event Controller"), + &aml::Name::new( + "_CRS".into(), + &aml::ResourceTemplate::new(vec![&aml::AddressSpace::new_memory( + aml::AddressSpaceCachable::NotCacheable, + true, + self.address.0, + self.address.0 + GED_DEVICE_ACPI_SIZE as u64 - 1, + )]), + ), + &aml::OpRegion::new( + "GDST".into(), + aml::OpRegionSpace::SystemMemory, + self.address.0 as usize, + GED_DEVICE_ACPI_SIZE, + ), + &aml::Field::new( + "GDST".into(), + aml::FieldAccessType::Byte, + aml::FieldUpdateRule::WriteAsZeroes, + vec![aml::FieldEntry::Named(*b"GDAT", 8)], + ), + &aml::Method::new( + "ESCN".into(), + 0, + true, + vec![ + &aml::Store::new(&aml::Local(0), &aml::Path::new("GDAT")), + &aml::And::new(&aml::Local(1), &aml::Local(0), &aml::ONE), + &aml::If::new( + &aml::Equal::new(&aml::Local(1), &aml::ONE), + vec![&aml::MethodCall::new("\\_SB_.CPUS.CSCN".into(), vec![])], + ), + &aml::And::new(&aml::Local(1), &aml::Local(0), &2usize), + &aml::If::new( + &aml::Equal::new(&aml::Local(1), &2usize), + vec![&aml::MethodCall::new("\\_SB_.MHPC.MSCN".into(), vec![])], + ), + &aml::And::new(&aml::Local(1), &aml::Local(0), &4usize), + &aml::If::new( + &aml::Equal::new(&aml::Local(1), &4usize), + vec![&aml::MethodCall::new("\\_SB_.PHPR.PSCN".into(), vec![])], + ), + &aml::And::new(&aml::Local(1), &aml::Local(0), &8usize), + &aml::If::new( + &aml::Equal::new(&aml::Local(1), &8usize), + vec![&aml::Notify::new( + &aml::Path::new("\\_SB_.PWRB"), + &0x80usize, + )], + ), + ], + ), + ], + ) + .append_aml_bytes(bytes); + aml::Device::new( + "_SB_.GED_".into(), + vec![ + &aml::Name::new("_HID".into(), &"ACPI0013"), + &aml::Name::new("_UID".into(), &aml::ZERO), + &aml::Name::new( + "_CRS".into(), + &aml::ResourceTemplate::new(vec![&aml::Interrupt::new( + true, + true, + false, + false, + self.ged_irq, + )]), + ), + &aml::Method::new( + "_EVT".into(), + 1, + true, + vec![&aml::MethodCall::new("\\_SB_.GEC_.ESCN".into(), vec![])], + ), + ], + ) + .append_aml_bytes(bytes) + } +} + +pub struct AcpiPmTimerDevice { + start: Instant, +} + +impl AcpiPmTimerDevice { + pub fn new() -> Self { + Self { + start: Instant::now(), + } + } +} + +impl Default for AcpiPmTimerDevice { + fn default() -> Self { + Self::new() + } +} + +impl BusDevice for AcpiPmTimerDevice { + fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) { + if data.len() != std::mem::size_of::() { + warn!("Invalid sized read of PM timer: {}", data.len()); + return; + } + let now = Instant::now(); + let since = now.duration_since(self.start); + let nanos = since.as_nanos(); + + const PM_TIMER_FREQUENCY_HZ: u128 = 3_579_545; + const NANOS_PER_SECOND: u128 = 1_000_000_000; + + let counter = (nanos * PM_TIMER_FREQUENCY_HZ) / NANOS_PER_SECOND; + let counter: u32 = (counter & 0xffff_ffff) as u32; + + data.copy_from_slice(&counter.to_le_bytes()); + } +} diff --git a/vendor/cube-hypervisor/devices/src/gic.rs b/vendor/cube-hypervisor/devices/src/gic.rs new file mode 100644 index 0000000..e04d6a0 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/gic.rs @@ -0,0 +1,169 @@ +// Copyright 2020, ARM Limited. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use super::interrupt_controller::{Error, InterruptController}; +extern crate arch; +use anyhow::anyhow; +use arch::layout; +use hypervisor::{ + arch::aarch64::gic::{Vgic, VgicConfig}, + CpuState, +}; +use std::result; +use std::sync::{Arc, Mutex}; +use vm_device::interrupt::{ + InterruptIndex, InterruptManager, InterruptSourceConfig, InterruptSourceGroup, + LegacyIrqSourceConfig, MsiIrqGroupConfig, +}; +use vm_memory::address::Address; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vmm_sys_util::eventfd::EventFd; + +type Result = result::Result; + +// Reserve 32 IRQs for legacy devices. +pub const IRQ_LEGACY_BASE: usize = layout::IRQ_BASE as usize; +pub const IRQ_LEGACY_COUNT: usize = 32; + +// Gic (Generic Interupt Controller) struct provides all the functionality of a +// GIC device. It wraps a hypervisor-emulated GIC device (Vgic) provided by the +// `hypervisor` crate. +// Gic struct also implements InterruptController to provide interrupt delivery +// service. +pub struct Gic { + interrupt_source_group: Arc, + // The hypervisor agnostic virtual GIC + vgic: Option>>, +} + +impl Gic { + pub fn new( + _vcpu_count: u8, + interrupt_manager: Arc>, + ) -> Result { + let interrupt_source_group = interrupt_manager + .create_group(MsiIrqGroupConfig { + base: IRQ_LEGACY_BASE as InterruptIndex, + count: IRQ_LEGACY_COUNT as InterruptIndex, + }) + .map_err(Error::CreateInterruptSourceGroup)?; + + Ok(Gic { + interrupt_source_group, + vgic: None, + }) + } + + /// Default config implied by arch::layout + pub fn create_default_config(vcpu_count: u64) -> VgicConfig { + let redists_size = layout::GIC_V3_REDIST_SIZE * vcpu_count; + let redists_addr = layout::GIC_V3_DIST_START.raw_value() - redists_size; + VgicConfig { + vcpu_count, + dist_addr: layout::GIC_V3_DIST_START.raw_value(), + dist_size: layout::GIC_V3_DIST_SIZE, + redists_addr, + redists_size, + msi_addr: redists_addr - layout::GIC_V3_ITS_SIZE, + msi_size: layout::GIC_V3_ITS_SIZE, + nr_irqs: layout::IRQ_NUM, + } + } + + pub fn create_vgic( + &mut self, + vm: &Arc, + config: VgicConfig, + ) -> Result>> { + let vgic = vm.create_vgic(config).map_err(Error::CreateGic)?; + self.vgic = Some(vgic.clone()); + Ok(vgic.clone()) + } + + pub fn set_gicr_typers(&mut self, vcpu_states: &[CpuState]) { + let vgic = self.vgic.as_ref().unwrap().clone(); + vgic.lock().unwrap().set_gicr_typers(vcpu_states); + } +} + +impl InterruptController for Gic { + fn enable(&self) -> Result<()> { + // Set irqfd for legacy interrupts + self.interrupt_source_group + .enable() + .map_err(Error::EnableInterrupt)?; + + // Set irq_routing for legacy interrupts. + // irqchip: Hardcode to 0 as we support only 1 GIC + // pin: Use irq number as pin + for i in IRQ_LEGACY_BASE..(IRQ_LEGACY_BASE + IRQ_LEGACY_COUNT) { + let config = LegacyIrqSourceConfig { + irqchip: 0, + pin: (i - IRQ_LEGACY_BASE) as u32, + }; + self.interrupt_source_group + .update( + i as InterruptIndex, + InterruptSourceConfig::LegacyIrq(config), + false, + ) + .map_err(Error::EnableInterrupt)?; + } + Ok(()) + } + + // This should be called anytime an interrupt needs to be injected into the + // running guest. + fn service_irq(&mut self, irq: usize) -> Result<()> { + self.interrupt_source_group + .trigger(irq as InterruptIndex) + .map_err(Error::TriggerInterrupt)?; + + Ok(()) + } + + fn notifier(&self, irq: usize) -> Option { + self.interrupt_source_group.notifier(irq as InterruptIndex) + } +} + +pub const GIC_V3_ITS_SNAPSHOT_ID: &str = "gic-v3-its"; +impl Snapshottable for Gic { + fn id(&self) -> String { + GIC_V3_ITS_SNAPSHOT_ID.to_string() + } + + fn snapshot(&mut self) -> std::result::Result { + let vgic = self.vgic.as_ref().unwrap().clone(); + let state = vgic.lock().unwrap().state().unwrap(); + Snapshot::new_from_state(&self.id(), &state) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + let vgic = self.vgic.as_ref().unwrap().clone(); + vgic.lock() + .unwrap() + .set_state(&snapshot.to_state(&self.id())?) + .map_err(|e| { + MigratableError::Restore(anyhow!("Could not restore GICv3ITS state {:?}", e)) + })?; + Ok(()) + } +} + +impl Pausable for Gic { + fn pause(&mut self) -> std::result::Result<(), MigratableError> { + // Flush tables to guest RAM + let vgic = self.vgic.as_ref().unwrap().clone(); + vgic.lock().unwrap().save_data_tables().map_err(|e| { + MigratableError::Pause(anyhow!( + "Could not save GICv3ITS GIC pending tables {:?}", + e + )) + })?; + Ok(()) + } +} +impl Transportable for Gic {} +impl Migratable for Gic {} diff --git a/vendor/cube-hypervisor/devices/src/interrupt_controller.rs b/vendor/cube-hypervisor/devices/src/interrupt_controller.rs new file mode 100644 index 0000000..6cca81d --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/interrupt_controller.rs @@ -0,0 +1,63 @@ +// Copyright 2020, ARM Limited. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use std::io; +use std::result; +use vmm_sys_util::eventfd::EventFd; + +#[derive(Debug)] +pub enum Error { + /// Invalid trigger mode. + InvalidTriggerMode, + /// Invalid delivery mode. + InvalidDeliveryMode, + /// Failed creating the interrupt source group. + CreateInterruptSourceGroup(io::Error), + /// Failed triggering the interrupt. + TriggerInterrupt(io::Error), + /// Failed masking the interrupt. + MaskInterrupt(io::Error), + /// Failed unmasking the interrupt. + UnmaskInterrupt(io::Error), + /// Failed updating the interrupt. + UpdateInterrupt(io::Error), + /// Failed enabling the interrupt. + EnableInterrupt(io::Error), + /// Failed creating GIC device. + CreateGic(hypervisor::HypervisorVmError), +} + +type Result = result::Result; + +pub struct MsiMessage { + // Message Address Register + // 31-20: Base address. Fixed value (0x0FEE) + // 19-12: Destination ID + // 11-4: Reserved + // 3: Redirection Hint indication + // 2: Destination Mode + // 1-0: Reserved + pub addr: u32, + // Message Data Register + // 32-16: Reserved + // 15: Trigger Mode. 0 = Edge, 1 = Level + // 14: Level. 0 = Deassert, 1 = Assert + // 13-11: Reserved + // 10-8: Delivery Mode + // 7-0: Vector + pub data: u32, +} + +// Introduce trait InterruptController to uniform the interrupt +// service provided for devices. +// Device manager uses this trait without caring whether it is a +// IOAPIC (X86) or GIC (Arm). +pub trait InterruptController: Send { + fn service_irq(&mut self, irq: usize) -> Result<()>; + #[cfg(target_arch = "aarch64")] + fn enable(&self) -> Result<()>; + #[cfg(target_arch = "x86_64")] + fn end_of_interrupt(&mut self, vec: u8); + fn notifier(&self, irq: usize) -> Option; +} diff --git a/vendor/cube-hypervisor/devices/src/ioapic.rs b/vendor/cube-hypervisor/devices/src/ioapic.rs new file mode 100644 index 0000000..e5f5a7e --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/ioapic.rs @@ -0,0 +1,435 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause +// +// Implementation of an intel 82093AA Input/Output Advanced Programmable Interrupt Controller +// See https://pdos.csail.mit.edu/6.828/2016/readings/ia32/ioapic.pdf for a specification. + +use super::interrupt_controller::{Error, InterruptController}; +use anyhow::anyhow; +use byteorder::{ByteOrder, LittleEndian}; +use serde::{Deserialize, Serialize}; +use std::result; +use std::sync::{Arc, Barrier}; + +use vm_device::interrupt::{ + InterruptIndex, InterruptManager, InterruptSourceConfig, InterruptSourceGroup, + MsiIrqGroupConfig, MsiIrqSourceConfig, +}; +use vm_device::BusDevice; +use vm_memory::GuestAddress; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vmm_sys_util::eventfd::EventFd; + +type Result = result::Result; + +// I/O REDIRECTION TABLE REGISTER +// +// There are 24 I/O Redirection Table entry registers. Each register is a +// dedicated entry for each interrupt input signal. Each register is 64 bits +// split between two 32 bits registers as follow: +// +// 63-56: Destination Field - R/W +// 55-17: Reserved +// 16: Interrupt Mask - R/W +// 15: Trigger Mode - R/W +// 14: Remote IRR - RO +// 13: Interrupt Input Pin Polarity - R/W +// 12: Delivery Status - RO +// 11: Destination Mode - R/W +// 10-8: Delivery Mode - R/W +// 7-0: Interrupt Vector - R/W +pub type RedirectionTableEntry = u64; + +fn vector(entry: RedirectionTableEntry) -> u8 { + (entry & 0xffu64) as u8 +} +fn delivery_mode(entry: RedirectionTableEntry) -> u8 { + ((entry >> 8) & 0x7u64) as u8 +} +fn destination_mode(entry: RedirectionTableEntry) -> u8 { + ((entry >> 11) & 0x1u64) as u8 +} +fn remote_irr(entry: RedirectionTableEntry) -> u8 { + ((entry >> 14) & 0x1u64) as u8 +} +fn trigger_mode(entry: RedirectionTableEntry) -> u8 { + ((entry >> 15) & 0x1u64) as u8 +} +fn interrupt_mask(entry: RedirectionTableEntry) -> u8 { + ((entry >> 16) & 0x1u64) as u8 +} +fn destination_field(entry: RedirectionTableEntry) -> u8 { + // When the destination mode is physical, the destination field should only + // be defined through bits 56-59, as defined in the IOAPIC specification. + // But from the APIC specification, the APIC ID is always defined on 8 bits + // no matter which destination mode is selected. That's why we always + // retrieve the destination field based on bits 56-63. + ((entry >> 56) & 0xffu64) as u8 +} +fn set_delivery_status(entry: &mut RedirectionTableEntry, val: u8) { + // Clear bit 12 + *entry &= 0xffff_ffff_ffff_efff; + // Set it with the expected value + *entry |= u64::from(val & 0x1) << 12; +} +fn set_remote_irr(entry: &mut RedirectionTableEntry, val: u8) { + // Clear bit 14 + *entry &= 0xffff_ffff_ffff_bfff; + // Set it with the expected value + *entry |= u64::from(val & 0x1) << 14; +} + +pub const NUM_IOAPIC_PINS: usize = 24; +const IOAPIC_VERSION_ID: u32 = 0x0017_0011; + +// Constants for IOAPIC direct register offset +const IOAPIC_REG_ID: u8 = 0x00; +const IOAPIC_REG_VERSION: u8 = 0x01; +const IOAPIC_REG_ARBITRATION_ID: u8 = 0x02; + +// Register offsets +const IOREGSEL_OFF: u8 = 0x0; +const IOWIN_OFF: u8 = 0x10; +const IOWIN_SCALE: u8 = 0x2; +const REG_MAX_OFFSET: u8 = IOWIN_OFF + (NUM_IOAPIC_PINS as u8 * 2) - 1; + +#[repr(u8)] +enum TriggerMode { + Edge = 0, + Level = 1, +} + +#[repr(u8)] +enum DeliveryMode { + Fixed = 0b000, + Lowest = 0b001, + Smi = 0b010, // System management interrupt + RemoteRead = 0b011, // This is no longer supported by intel. + Nmi = 0b100, // Non maskable interrupt + Init = 0b101, + Startup = 0b110, + External = 0b111, +} + +/// Given an offset that was read from/written to, return a tuple of the relevant IRQ and whether +/// the offset refers to the high bits of that register. +fn decode_irq_from_selector(selector: u8) -> (usize, bool) { + ( + ((selector - IOWIN_OFF) / IOWIN_SCALE) as usize, + selector & 1 != 0, + ) +} + +pub struct Ioapic { + id: String, + id_reg: u32, + reg_sel: u32, + reg_entries: [RedirectionTableEntry; NUM_IOAPIC_PINS], + used_entries: [bool; NUM_IOAPIC_PINS], + apic_address: GuestAddress, + interrupt_source_group: Arc, +} + +#[derive(Serialize, Deserialize)] +pub struct IoapicState { + id_reg: u32, + reg_sel: u32, + reg_entries: [RedirectionTableEntry; NUM_IOAPIC_PINS], + used_entries: [bool; NUM_IOAPIC_PINS], + apic_address: u64, +} + +impl BusDevice for Ioapic { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + if data.len() != std::mem::size_of::() { + warn!("Invalid read size on IOAPIC: {}", data.len()); + return; + } + + debug!("IOAPIC_R @ offset 0x{:x}", offset); + + let value: u32 = match offset as u8 { + IOREGSEL_OFF => self.reg_sel, + IOWIN_OFF => self.ioapic_read(), + _ => { + error!("IOAPIC: failed reading at offset {}", offset); + return; + } + }; + + LittleEndian::write_u32(data, value); + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + if data.len() != std::mem::size_of::() { + warn!("Invalid write size on IOAPIC: {}", data.len()); + return None; + } + + debug!("IOAPIC_W @ offset 0x{:x}", offset); + + let value = LittleEndian::read_u32(data); + + match offset as u8 { + IOREGSEL_OFF => self.reg_sel = value, + IOWIN_OFF => self.ioapic_write(value), + _ => { + error!("IOAPIC: failed writing at offset {}", offset); + } + } + None + } +} + +impl Ioapic { + pub fn new( + id: String, + apic_address: GuestAddress, + interrupt_manager: Arc>, + ) -> Result { + let interrupt_source_group = interrupt_manager + .create_group(MsiIrqGroupConfig { + base: 0, + count: NUM_IOAPIC_PINS as InterruptIndex, + }) + .map_err(Error::CreateInterruptSourceGroup)?; + + // The IOAPIC is created with entries already masked. The guest will be + // in charge of unmasking them if/when necessary. + Ok(Ioapic { + id, + id_reg: 0, + reg_sel: 0, + reg_entries: [0x10000; NUM_IOAPIC_PINS], + used_entries: [false; NUM_IOAPIC_PINS], + apic_address, + interrupt_source_group, + }) + } + + fn ioapic_write(&mut self, val: u32) { + debug!("IOAPIC_W reg 0x{:x}, val 0x{:x}", self.reg_sel, val); + + match self.reg_sel as u8 { + IOAPIC_REG_VERSION => { + if val == 0 { + // Windows writes zero here (see #1791) + } else { + error!( + "IOAPIC: invalid write to version register (0x{:x}): 0x{:x}", + self.reg_sel, val + ); + } + } + IOAPIC_REG_ID => self.id_reg = (val >> 24) & 0xf, + IOWIN_OFF..=REG_MAX_OFFSET => { + let (index, is_high_bits) = decode_irq_from_selector(self.reg_sel as u8); + if index > NUM_IOAPIC_PINS { + warn!("IOAPIC index out of range: {}", index); + return; + } + if is_high_bits { + self.reg_entries[index] &= 0xffff_ffff; + self.reg_entries[index] |= u64::from(val) << 32; + } else { + // Ensure not to override read-only bits: + // - Delivery Status (bit 12) + // - Remote IRR (bit 14) + self.reg_entries[index] &= 0xffff_ffff_0000_5000; + self.reg_entries[index] |= u64::from(val) & 0xffff_afff; + } + // The entry must be updated through the interrupt source + // group. + if let Err(e) = self.update_entry(index) { + error!("Failed updating IOAPIC entry: {:?}", e); + } + // Store the information this IRQ is now being used. + self.used_entries[index] = true; + } + _ => error!( + "IOAPIC: invalid write to register offset 0x{:x}", + self.reg_sel + ), + } + } + + fn ioapic_read(&self) -> u32 { + debug!("IOAPIC_R reg 0x{:x}", self.reg_sel); + + match self.reg_sel as u8 { + IOAPIC_REG_VERSION => IOAPIC_VERSION_ID, + IOAPIC_REG_ID | IOAPIC_REG_ARBITRATION_ID => (self.id_reg & 0xf) << 24, + IOWIN_OFF..=REG_MAX_OFFSET => { + let (index, is_high_bits) = decode_irq_from_selector(self.reg_sel as u8); + if index > NUM_IOAPIC_PINS { + warn!("IOAPIC index out of range: {}", index); + return 0; + } + if is_high_bits { + (self.reg_entries[index] >> 32) as u32 + } else { + (self.reg_entries[index] & 0xffff_ffff) as u32 + } + } + _ => { + error!( + "IOAPIC: invalid read from register offset 0x{:x}", + self.reg_sel + ); + 0 + } + } + } + + fn state(&self) -> IoapicState { + IoapicState { + id_reg: self.id_reg, + reg_sel: self.reg_sel, + reg_entries: self.reg_entries, + used_entries: self.used_entries, + apic_address: self.apic_address.0, + } + } + + fn set_state(&mut self, state: &IoapicState) -> Result<()> { + self.id_reg = state.id_reg; + self.reg_sel = state.reg_sel; + self.reg_entries = state.reg_entries; + self.used_entries = state.used_entries; + self.apic_address = GuestAddress(state.apic_address); + for (irq, entry) in self.used_entries.iter().enumerate() { + if *entry { + self.update_entry(irq)?; + } + } + + Ok(()) + } + + fn update_entry(&self, irq: usize) -> Result<()> { + let entry = self.reg_entries[irq]; + + // Validate Destination Mode value, and retrieve Destination ID + let destination_mode = destination_mode(entry); + let destination_id = destination_field(entry); + + // When this bit is set, the message is directed to the processor with + // the lowest interrupt priority among processors that can receive the + // interrupt. + let redirection_hint: u8 = 1; + + // Generate MSI message address + let low_addr: u32 = self.apic_address.0 as u32 + | u32::from(destination_id) << 12 + | u32::from(redirection_hint) << 3 + | u32::from(destination_mode) << 2; + + // Validate Trigger Mode value + let trigger_mode = trigger_mode(entry); + match trigger_mode { + x if (x == TriggerMode::Edge as u8) || (x == TriggerMode::Level as u8) => {} + _ => return Err(Error::InvalidTriggerMode), + } + + // Validate Delivery Mode value + let delivery_mode = delivery_mode(entry); + match delivery_mode { + x if (x == DeliveryMode::Fixed as u8) + || (x == DeliveryMode::Lowest as u8) + || (x == DeliveryMode::Smi as u8) + || (x == DeliveryMode::RemoteRead as u8) + || (x == DeliveryMode::Nmi as u8) + || (x == DeliveryMode::Init as u8) + || (x == DeliveryMode::Startup as u8) + || (x == DeliveryMode::External as u8) => {} + _ => return Err(Error::InvalidDeliveryMode), + } + + // Generate MSI message data + let data: u32 = u32::from(trigger_mode) << 15 + | u32::from(remote_irr(entry)) << 14 + | u32::from(delivery_mode) << 8 + | u32::from(vector(entry)); + + let config = MsiIrqSourceConfig { + high_addr: 0x0, + low_addr, + data, + devid: 0, + }; + + self.interrupt_source_group + .update( + irq as InterruptIndex, + InterruptSourceConfig::MsiIrq(config), + interrupt_mask(entry) == 1, + ) + .map_err(Error::UpdateInterrupt)?; + + Ok(()) + } +} + +impl InterruptController for Ioapic { + // The ioapic must be informed about EOIs in order to deassert interrupts + // already sent. + fn end_of_interrupt(&mut self, vec: u8) { + for i in 0..NUM_IOAPIC_PINS { + let entry = &mut self.reg_entries[i]; + // Clear Remote IRR bit + if vector(*entry) == vec && trigger_mode(*entry) == 1 { + set_remote_irr(entry, 0); + } + } + } + + // This should be called anytime an interrupt needs to be injected into the + // running guest. + fn service_irq(&mut self, irq: usize) -> Result<()> { + let entry = &mut self.reg_entries[irq]; + + self.interrupt_source_group + .trigger(irq as InterruptIndex) + .map_err(Error::TriggerInterrupt)?; + debug!("Interrupt successfully delivered"); + + // If trigger mode is level sensitive, set the Remote IRR bit. + // It will be cleared when the EOI is received. + if trigger_mode(*entry) == 1 { + set_remote_irr(entry, 1); + } + // Clear the Delivery Status bit + set_delivery_status(entry, 0); + + Ok(()) + } + + fn notifier(&self, irq: usize) -> Option { + self.interrupt_source_group.notifier(irq as InterruptIndex) + } +} + +impl Snapshottable for Ioapic { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + self.set_state(&snapshot.to_state(&self.id)?).map_err(|e| { + MigratableError::Restore(anyhow!("Could not restore state for {}: {:?}", self.id, e)) + }) + } +} + +impl Pausable for Ioapic {} +impl Transportable for Ioapic {} +impl Migratable for Ioapic {} diff --git a/vendor/cube-hypervisor/devices/src/ivshmem.rs b/vendor/cube-hypervisor/devices/src/ivshmem.rs new file mode 100644 index 0000000..4414328 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/ivshmem.rs @@ -0,0 +1,378 @@ +// Copyright © 2024 Tencent Corporation. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +use byteorder::{ByteOrder, LittleEndian}; +use pci::{ + BarReprogrammingParams, PciBarConfiguration, PciBarPrefetchable, PciBarRegionType, + PciClassCode, PciConfiguration, PciDevice, PciDeviceError, PciHeaderType, PciSubclass, +}; +use serde::{Deserialize, Serialize}; +use std::any::Any; +use std::result; +use std::sync::atomic::{AtomicU32, Ordering}; +use std::sync::{Arc, Barrier, Mutex}; +use vm_allocator::{AddressAllocator, SystemAllocator}; +use vm_device::{BusDevice, Resource}; +use vm_memory::bitmap::AtomicBitmap; +use vm_memory::{Address, GuestAddress}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; + +const IVSHMEM_BAR0_IDX: usize = 0; +const IVSHMEM_BAR1_IDX: usize = 1; +const IVSHMEM_BAR2_IDX: usize = 2; + +const IVSHMEM_VENDOR_ID: u16 = 0x1af4; +const IVSHMEM_DEVICE_ID: u16 = 0x1110; + +const IVSHMEM_REG_BAR_SIZE: u64 = 0x100; + +/// +/// ```text +/// Offset Size Access On reset Function +/// 0 4 read/write 0 Interrupt Mask +/// bit 0: peer interrupt (rev 0) +/// reserved (rev 1) +/// bit 1..31: reserved +/// 4 4 read/write 0 Interrupt Status +/// bit 0: peer interrupt (rev 0) +/// reserved (rev 1) +/// bit 1..31: reserved +/// 8 4 read-only 0 or ID IVPosition +/// 12 4 write-only N/A Doorbell +/// bit 0..15: vector +/// bit 16..31: peer ID +/// 16 240 none N/A reserved +/// ``` +/// + +const IVSHMEM_REG_INTERRUPT_MASK: u64 = 0; +const IVSHMEM_REG_INTERRUPT_STATUS: u64 = 4; +const IVSHMEM_REG_IV_POSITION: u64 = 8; +const IVSHMEM_REG_DOORBELL: u64 = 12; + +type GuestRegionMmap = vm_memory::GuestRegionMmap; + +#[allow(dead_code)] +#[derive(Copy, Clone)] +pub enum IvshmemSubclass { + Other = 0x00, +} + +impl PciSubclass for IvshmemSubclass { + fn get_register_value(&self) -> u8 { + *self as u8 + } +} + +pub struct IvshmemDevice { + id: String, + + // ivshmem device registers + interrupt_mask: u32, + interrupt_status: Arc, + iv_position: u32, + doorbell: u32, + + // PCI configuration registers. + configuration: PciConfiguration, + bar_regions: Vec, + + region: Option>, + region_size: u64, +} + +#[derive(Serialize, Deserialize, Default, Clone)] +pub struct IvshmemDeviceState { + interrupt_mask: u32, + interrupt_status: u32, + iv_position: u32, + doorbell: u32, +} + +impl IvshmemDevice { + pub fn new(id: String, state: Option, region_size: u64) -> Self { + let configuration = PciConfiguration::new( + IVSHMEM_VENDOR_ID, + IVSHMEM_DEVICE_ID, + 0x1, + PciClassCode::MemoryController, + &IvshmemSubclass::Other, + None, + PciHeaderType::Device, + 0, + 0, + None, + ); + + if let Some(s) = state { + IvshmemDevice { + id, + configuration, + bar_regions: vec![], + interrupt_mask: s.interrupt_mask, + interrupt_status: Arc::new(AtomicU32::new(s.interrupt_status)), + iv_position: s.iv_position, + doorbell: s.doorbell, + region_size, + region: None, + } + } else { + IvshmemDevice { + id, + configuration, + bar_regions: vec![], + interrupt_mask: 0, + interrupt_status: Arc::new(AtomicU32::new(0)), + iv_position: 0, + doorbell: 0, + region_size, + region: None, + } + } + } + + pub fn config_bar_addr(&self) -> u64 { + self.configuration.get_bar_addr(0) + } + + pub fn data_bar_addr(&self) -> u64 { + self.configuration.get_bar_addr(2) + } + + pub fn assign_region(&mut self, region: Arc) { + self.region = Some(region); + } + + fn state(&self) -> IvshmemDeviceState { + IvshmemDeviceState { + interrupt_mask: self.interrupt_mask, + interrupt_status: self.interrupt_status.load(Ordering::SeqCst), + iv_position: self.iv_position, + doorbell: self.doorbell, + } + } + + fn set_state(&mut self, state: &IvshmemDeviceState) { + self.interrupt_mask = state.interrupt_mask; + self.interrupt_status = Arc::new(AtomicU32::new(state.interrupt_status)); + self.iv_position = state.iv_position; + self.doorbell = state.doorbell; + } +} + +impl BusDevice for IvshmemDevice { + fn read(&mut self, base: u64, offset: u64, data: &mut [u8]) { + self.read_bar(base, offset, data) + } + + fn write(&mut self, base: u64, offset: u64, data: &[u8]) -> Option> { + self.write_bar(base, offset, data) + } +} + +impl PciDevice for IvshmemDevice { + fn allocate_bars( + &mut self, + allocator: &Arc>, + _mmio_allocator: &mut AddressAllocator, + resources: Option>, + ) -> std::result::Result, PciDeviceError> { + let mut bars = Vec::new(); + let mut bar0_addr = None; + let mut bar2_addr = None; + + if let Some(resources) = resources { + for resource in resources { + match resource { + Resource::PciBar { index, base, .. } => { + match index { + IVSHMEM_BAR0_IDX => { + bar0_addr = Some(GuestAddress(base)); + } + IVSHMEM_BAR1_IDX => {} + IVSHMEM_BAR2_IDX => { + bar2_addr = Some(GuestAddress(base)); + } + _ => { + error!("Unexpected pci bar index {index}"); + } + }; + } + _ => { + error!("Unexpected resource {resource:?}"); + } + } + } + if bar0_addr.is_none() || bar2_addr.is_none() { + return Err(PciDeviceError::MissingResource); + } + } + + // BAR0 holds device registers (256 Byte MMIO) + let bar0_addr = allocator + .lock() + .unwrap() + .allocate_mmio_hole_addresses(bar0_addr, IVSHMEM_REG_BAR_SIZE, None) + .ok_or(PciDeviceError::IoAllocationFailed(IVSHMEM_REG_BAR_SIZE))?; + + let bar0 = PciBarConfiguration::default() + .set_index(IVSHMEM_BAR0_IDX) + .set_address(bar0_addr.raw_value()) + .set_size(IVSHMEM_REG_BAR_SIZE) + .set_region_type(PciBarRegionType::Memory32BitRegion) + .set_prefetchable(PciBarPrefetchable::NotPrefetchable); + + debug!("ivshmem bar0 address 0x{:x}", bar0_addr.0); + self.configuration + .add_pci_bar(&bar0) + .map_err(|e| PciDeviceError::IoRegistrationFailed(bar0_addr.raw_value(), e))?; + + // BAR1 holds MSI-X table and PBA (only ivshmem-doorbell). + + // BAR2 maps the shared memory object + let bar2_size = self.region_size; + let bar2_addr = allocator + .lock() + .unwrap() + .allocate_mmio_hole_addresses(bar2_addr, bar2_size, None) + .ok_or(PciDeviceError::IoAllocationFailed(bar2_size))?; + + let bar2 = PciBarConfiguration::default() + .set_index(IVSHMEM_BAR2_IDX) + .set_address(bar2_addr.raw_value()) + .set_size(bar2_size) + .set_region_type(PciBarRegionType::Memory64BitRegion) + .set_prefetchable(PciBarPrefetchable::Prefetchable); + + debug!("ivshmem bar2 address 0x{:x}", bar2_addr.0); + self.configuration + .add_pci_bar(&bar2) + .map_err(|e| PciDeviceError::IoRegistrationFailed(bar2_addr.raw_value(), e))?; + + bars.push(bar0); + bars.push(bar2); + self.bar_regions = bars.clone(); + + Ok(bars) + } + + fn free_bars( + &mut self, + allocator: &mut SystemAllocator, + _mmio_allocator: &mut AddressAllocator, + ) -> std::result::Result<(), PciDeviceError> { + for bar in self.bar_regions.drain(..) { + allocator.free_mmio_hole_addresses(GuestAddress(bar.addr()), bar.size()); + } + + Ok(()) + } + + fn write_config_register( + &mut self, + reg_idx: usize, + offset: u64, + data: &[u8], + ) -> Option> { + self.configuration + .write_config_register(reg_idx, offset, data); + None + } + + fn read_config_register(&mut self, reg_idx: usize) -> u32 { + self.configuration.read_reg(reg_idx) + } + + fn detect_bar_reprogramming( + &mut self, + reg_idx: usize, + data: &[u8], + ) -> Option { + self.configuration.detect_bar_reprogramming(reg_idx, data) + } + + fn read_bar(&mut self, base: u64, offset: u64, data: &mut [u8]) { + debug!("read base {base:x} offset {offset}"); + + let mut bar_idx = 0; + for (idx, bar) in self.bar_regions.iter().enumerate() { + if bar.addr() == base { + bar_idx = idx; + } + } + match bar_idx { + // bar 0 + 0 => { + let v = match offset { + IVSHMEM_REG_INTERRUPT_MASK => self.interrupt_mask, + IVSHMEM_REG_INTERRUPT_STATUS => self.interrupt_status.load(Ordering::SeqCst), + IVSHMEM_REG_IV_POSITION => self.iv_position, + IVSHMEM_REG_DOORBELL => self.doorbell, + _ => { + warn!("Unknown offset: {offset}"); + 0u32 + } + }; + LittleEndian::write_u32(data, v); + } + // bar 2 + 1 => warn!("unexpect read ivshmem memory idx: {offset}"), + _ => { + warn!("invalid bar_idx: {bar_idx}"); + } + }; + } + + fn write_bar(&mut self, base: u64, offset: u64, _data: &[u8]) -> Option> { + debug!("write base {base:x} offset {offset}"); + warn!("unexpect write ivshmem memory idx: {offset}"); + None + } + + fn move_bar(&mut self, old_base: u64, new_base: u64) -> result::Result<(), std::io::Error> { + for bar in self.bar_regions.iter_mut() { + if bar.addr() == old_base { + *bar = bar.set_address(new_base); + } + } + + Ok(()) + } + + fn as_any(&mut self) -> &mut dyn Any { + self + } + + fn id(&self) -> Option { + Some(self.id.clone()) + } +} + +impl Pausable for IvshmemDevice {} + +impl Snapshottable for IvshmemDevice { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> Result { + let mut snapshot = Snapshot::new_from_state(&self.id, &self.state())?; + + snapshot.add_snapshot(self.configuration.snapshot()?); + + Ok(snapshot) + } + + fn restore(&mut self, snapshot: Snapshot) -> Result<(), MigratableError> { + self.set_state(&snapshot.to_state(&self.id)?); + if let Some(pci_config_snapshot) = snapshot.snapshots.get(&self.configuration.id()) { + self.configuration.restore(*pci_config_snapshot.clone())?; + } + Ok(()) + } +} + +impl Transportable for IvshmemDevice {} +impl Migratable for IvshmemDevice {} diff --git a/vendor/cube-hypervisor/devices/src/legacy/cmos.rs b/vendor/cube-hypervisor/devices/src/legacy/cmos.rs new file mode 100644 index 0000000..6a08fd5 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/legacy/cmos.rs @@ -0,0 +1,153 @@ +// Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use libc::{clock_gettime, gmtime_r, timespec, tm, CLOCK_REALTIME}; +use std::cmp::min; +use std::mem; +use std::sync::{Arc, Barrier}; +use vm_device::BusDevice; +use vmm_sys_util::eventfd::EventFd; + +// https://github.com/rust-lang/libc/issues/1848 +#[cfg_attr(target_env = "musl", allow(deprecated))] +use libc::time_t; + +const INDEX_MASK: u8 = 0x7f; +const INDEX_OFFSET: u64 = 0x0; +const DATA_OFFSET: u64 = 0x1; +const DATA_LEN: usize = 128; + +/// A CMOS/RTC device commonly seen on x86 I/O port 0x70/0x71. +pub struct Cmos { + index: u8, + data: [u8; DATA_LEN], + reset_evt: EventFd, +} + +impl Cmos { + /// Constructs a CMOS/RTC device with initial data. + /// `mem_below_4g` is the size of memory in bytes below the 32-bit gap. + /// `mem_above_4g` is the size of memory in bytes above the 32-bit gap. + pub fn new(mem_below_4g: u64, mem_above_4g: u64, reset_evt: EventFd) -> Cmos { + let mut data = [0u8; DATA_LEN]; + + // Extended memory from 16 MB to 4 GB in units of 64 KB + let ext_mem = min( + 0xFFFF, + mem_below_4g.saturating_sub(16 * 1024 * 1024) / (64 * 1024), + ); + data[0x34] = ext_mem as u8; + data[0x35] = (ext_mem >> 8) as u8; + + // High memory (> 4GB) in units of 64 KB + let high_mem = min(0x00FF_FFFF, mem_above_4g / (64 * 1024)); + data[0x5b] = high_mem as u8; + data[0x5c] = (high_mem >> 8) as u8; + data[0x5d] = (high_mem >> 16) as u8; + + Cmos { + index: 0, + data, + reset_evt, + } + } +} + +impl BusDevice for Cmos { + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + if data.len() != 1 { + warn!("Invalid write size on CMOS device: {}", data.len()); + return None; + } + + match offset { + INDEX_OFFSET => self.index = data[0], + DATA_OFFSET => { + if self.index == 0x8f && data[0] == 0 { + error!("Shutdown because of CMOS reset"); + self.reset_evt.write(1).unwrap(); + } else { + self.data[(self.index & INDEX_MASK) as usize] = data[0] + } + } + o => warn!("bad write offset on CMOS device: {}", o), + }; + None + } + + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + fn to_bcd(v: u8) -> u8 { + assert!(v < 100); + ((v / 10) << 4) | (v % 10) + } + + if data.len() != 1 { + warn!("Invalid read size on CMOS device: {}", data.len()); + return; + } + + data[0] = match offset { + INDEX_OFFSET => self.index, + DATA_OFFSET => { + let seconds; + let minutes; + let hours; + let week_day; + let day; + let month; + let year; + // The clock_gettime and gmtime_r calls are safe as long as the structs they are + // given are large enough, and neither of them fail. It is safe to zero initialize + // the tm and timespec struct because it contains only plain data. + let update_in_progress = unsafe { + let mut timespec: timespec = mem::zeroed(); + clock_gettime(CLOCK_REALTIME, &mut timespec as *mut _); + + // https://github.com/rust-lang/libc/issues/1848 + #[cfg_attr(target_env = "musl", allow(deprecated))] + let now: time_t = timespec.tv_sec; + let mut tm: tm = mem::zeroed(); + gmtime_r(&now, &mut tm as *mut _); + + // The following lines of code are safe but depend on tm being in scope. + seconds = tm.tm_sec; + minutes = tm.tm_min; + hours = tm.tm_hour; + week_day = tm.tm_wday + 1; + day = tm.tm_mday; + month = tm.tm_mon + 1; + year = tm.tm_year; + + // Update in Progress bit held for last 224us of each second + const NANOSECONDS_PER_SECOND: i64 = 1_000_000_000; + const UIP_HOLD_LENGTH: i64 = 8 * NANOSECONDS_PER_SECOND / 32768; + timespec.tv_nsec >= (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH) + }; + match self.index { + 0x00 => to_bcd(seconds as u8), + 0x02 => to_bcd(minutes as u8), + 0x04 => to_bcd(hours as u8), + 0x06 => to_bcd(week_day as u8), + 0x07 => to_bcd(day as u8), + 0x08 => to_bcd(month as u8), + 0x09 => to_bcd((year % 100) as u8), + // Bit 5 for 32kHz clock. Bit 7 for Update in Progress + 0x0a => 1 << 5 | (update_in_progress as u8) << 7, + // Bit 0-6 are reserved and must be 0. + // Bit 7 must be 1 (CMOS has power) + 0x0d => 1 << 7, + 0x32 => to_bcd(((year + 1900) / 100) as u8), + _ => { + // self.index is always guaranteed to be in range via INDEX_MASK. + self.data[(self.index & INDEX_MASK) as usize] + } + } + } + o => { + warn!("bad read offset on CMOS device: {}", o); + 0 + } + } + } +} diff --git a/vendor/cube-hypervisor/devices/src/legacy/debug_port.rs b/vendor/cube-hypervisor/devices/src/legacy/debug_port.rs new file mode 100644 index 0000000..2568e9c --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/legacy/debug_port.rs @@ -0,0 +1,86 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use std::fmt; +use std::time::Instant; +use vm_device::BusDevice; + +/// Debug I/O port, see: +/// https://www.intel.com/content/www/us/en/support/articles/000005500/boards-and-kits.html +/// +/// Since we're not a physical platform, we can freely assign code ranges for +/// debugging specific parts of our virtual platform. +pub enum DebugIoPortRange { + Firmware, + Bootloader, + Kernel, + Userspace, + Custom, +} + +#[cfg(target_arch = "x86_64")] +const DEBUG_IOPORT_PREFIX: &str = "Debug I/O port"; + +#[cfg(target_arch = "x86_64")] +impl DebugIoPortRange { + fn from_u8(value: u8) -> DebugIoPortRange { + match value { + 0x00..=0x1f => DebugIoPortRange::Firmware, + 0x20..=0x3f => DebugIoPortRange::Bootloader, + 0x40..=0x5f => DebugIoPortRange::Kernel, + 0x60..=0x7f => DebugIoPortRange::Userspace, + _ => DebugIoPortRange::Custom, + } + } +} + +#[cfg(target_arch = "x86_64")] +impl fmt::Display for DebugIoPortRange { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + DebugIoPortRange::Firmware => write!(f, "{}: Firmware", DEBUG_IOPORT_PREFIX), + DebugIoPortRange::Bootloader => write!(f, "{}: Bootloader", DEBUG_IOPORT_PREFIX), + DebugIoPortRange::Kernel => write!(f, "{}: Kernel", DEBUG_IOPORT_PREFIX), + DebugIoPortRange::Userspace => write!(f, "{}: Userspace", DEBUG_IOPORT_PREFIX), + DebugIoPortRange::Custom => write!(f, "{}: Custom", DEBUG_IOPORT_PREFIX), + } + } +} + +pub struct DebugPort { + timestamp: Instant, +} + +impl DebugPort { + pub fn new(timestamp: Instant) -> Self { + Self { timestamp } + } +} + +impl BusDevice for DebugPort { + fn read(&mut self, _base: u64, _offset: u64, _data: &mut [u8]) { + error!("Invalid read to debug port") + } + + fn write( + &mut self, + _base: u64, + _offset: u64, + data: &[u8], + ) -> Option> { + let elapsed = self.timestamp.elapsed(); + + let code = data[0]; + warn!( + "[{} code 0x{:x}] {}.{:>06} seconds", + DebugIoPortRange::from_u8(code), + code, + elapsed.as_secs(), + elapsed.as_micros() + ); + + None + } +} diff --git a/vendor/cube-hypervisor/devices/src/legacy/fwdebug.rs b/vendor/cube-hypervisor/devices/src/legacy/fwdebug.rs new file mode 100644 index 0000000..e28ee73 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/legacy/fwdebug.rs @@ -0,0 +1,43 @@ +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause +// + +use std::sync::{Arc, Barrier}; +use vm_device::BusDevice; + +/// Provides firmware debug output via I/O port controls +#[derive(Default)] +pub struct FwDebugDevice {} + +impl FwDebugDevice { + pub fn new() -> Self { + Self {} + } +} + +/// FwDebugDevice sits on the I/O bus as 0x402 and receives ASCII characters +impl BusDevice for FwDebugDevice { + /// Upon read return the magic value to indicate that there is a debug port + fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) { + if data.len() == 1 { + data[0] = 0xe9 + } else { + error!("Invalid read size on debug port: {}", data.len()) + } + } + + fn write(&mut self, _base: u64, _offset: u64, data: &[u8]) -> Option> { + if data.len() == 1 { + print!("{}", data[0] as char); + } else { + error!("Invalid write size on debug port: {}", data.len()) + } + + None + } +} diff --git a/vendor/cube-hypervisor/devices/src/legacy/gpio_pl061.rs b/vendor/cube-hypervisor/devices/src/legacy/gpio_pl061.rs new file mode 100644 index 0000000..238c820 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/legacy/gpio_pl061.rs @@ -0,0 +1,456 @@ +// Copyright 2021 Arm Limited (or its affiliates). All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 + +//! ARM PrimeCell General Purpose Input/Output(PL061) +//! +//! This module implements an ARM PrimeCell General Purpose Input/Output(PL061) to support gracefully poweroff microvm from external. +//! + +use crate::{read_le_u32, write_le_u32}; +use serde::{Deserialize, Serialize}; +use std::result; +use std::sync::{Arc, Barrier}; +use std::{fmt, io}; +use vm_device::interrupt::InterruptSourceGroup; +use vm_device::BusDevice; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; + +const OFS_DATA: u64 = 0x400; // Data Register +const GPIODIR: u64 = 0x400; // Direction Register +const GPIOIS: u64 = 0x404; // Interrupt Sense Register +const GPIOIBE: u64 = 0x408; // Interrupt Both Edges Register +const GPIOIEV: u64 = 0x40c; // Interrupt Event Register +const GPIOIE: u64 = 0x410; // Interrupt Mask Register +const GPIORIE: u64 = 0x414; // Raw Interrupt Status Register +const GPIOMIS: u64 = 0x418; // Masked Interrupt Status Register +const GPIOIC: u64 = 0x41c; // Interrupt Clear Register +const GPIOAFSEL: u64 = 0x420; // Mode Control Select Register + // From 0x424 to 0xFDC => reserved space. + // From 0xFE0 to 0xFFC => Peripheral and PrimeCell Identification Registers which are Read Only registers. + // Thses registers can conceptually be treated as a 32-bit register, and PartNumber[11:0] is used to identify the peripheral. + // We are putting the expected values (look at 'Reset value' column from above mentioned document) in an array. +const GPIO_ID: [u8; 8] = [0x61, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1]; +// ID Margins +const GPIO_ID_LOW: u64 = 0xfe0; +const GPIO_ID_HIGH: u64 = 0x1000; + +const N_GPIOS: u32 = 8; + +#[derive(Debug)] +pub enum Error { + BadWriteOffset(u64), + GpioInterruptDisabled, + GpioInterruptFailure(io::Error), + GpioTriggerKeyFailure(u32), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Error::BadWriteOffset(offset) => write!(f, "Bad Write Offset: {}", offset), + Error::GpioInterruptDisabled => write!(f, "GPIO interrupt disabled by guest driver.",), + Error::GpioInterruptFailure(ref e) => { + write!(f, "Could not trigger GPIO interrupt: {}.", e) + } + Error::GpioTriggerKeyFailure(key) => { + write!(f, "Invalid GPIO Input key triggerd: {}.", key) + } + } + } +} + +type Result = result::Result; + +/// A GPIO device following the PL061 specification. +pub struct Gpio { + id: String, + // Data Register + data: u32, + old_in_data: u32, + // Direction Register + dir: u32, + // Interrupt Sense Register + isense: u32, + // Interrupt Both Edges Register + ibe: u32, + // Interrupt Event Register + iev: u32, + // Interrupt Mask Register + im: u32, + // Raw Interrupt Status Register + istate: u32, + // Mode Control Select Register + afsel: u32, + // GPIO irq_field + interrupt: Arc, +} + +#[derive(Serialize, Deserialize)] +pub struct GpioState { + data: u32, + old_in_data: u32, + dir: u32, + isense: u32, + ibe: u32, + iev: u32, + im: u32, + istate: u32, + afsel: u32, +} + +impl Gpio { + /// Constructs an PL061 GPIO device. + pub fn new(id: String, interrupt: Arc) -> Self { + Self { + id, + data: 0, + old_in_data: 0, + dir: 0, + isense: 0, + ibe: 0, + iev: 0, + im: 0, + istate: 0, + afsel: 0, + interrupt, + } + } + + fn state(&self) -> GpioState { + GpioState { + data: self.data, + old_in_data: self.old_in_data, + dir: self.dir, + isense: self.isense, + ibe: self.ibe, + iev: self.iev, + im: self.im, + istate: self.istate, + afsel: self.afsel, + } + } + + fn set_state(&mut self, state: &GpioState) { + self.data = state.data; + self.old_in_data = state.old_in_data; + self.dir = state.dir; + self.isense = state.isense; + self.ibe = state.ibe; + self.iev = state.iev; + self.im = state.im; + self.istate = state.istate; + self.afsel = state.afsel; + } + + fn pl061_internal_update(&mut self) { + // FIXME: + // Missing Output Interrupt Emulation. + + // Input Edging Interrupt Emulation. + let changed = ((self.old_in_data ^ self.data) & !self.dir) as u32; + if changed > 0 { + self.old_in_data = self.data; + for i in 0..N_GPIOS { + let mask = (1 << i) as u32; + if (changed & mask) > 0 { + // Bits set high in GPIOIS(Interrupt sense register) configure the corresponding + // pins to detect levels, otherwise, detect edges. + if (self.isense & mask) == 0 { + if (self.ibe & mask) > 0 { + // Bits set high in GPIOIBE(Interrupt both-edges register) configure the corresponding + // pins to detect both falling and rising edges. + // Clearing a bit configures the pin to be controlled by GPIOIEV. + self.istate |= mask; + } else { + // Bits set to high in GPIOIEV(Interrupt event register) configure the + // corresponding pin to detect rising edges, otherwise, detect falling edges. + self.istate |= !(self.data ^ self.iev) & mask; + } + } + } + } + } + + // Input Level Interrupt Emulation. + self.istate |= !(self.data ^ self.iev) & self.isense; + } + + fn handle_write(&mut self, offset: u64, val: u32) -> Result<()> { + if offset < OFS_DATA { + // In order to write to data register, the corresponding bits in the mask, resulting + // from the offsite[9:2], must be HIGH. otherwise the bit values remain unchanged. + let mask = (offset >> 2) as u32 & self.dir; + self.data = (self.data & !mask) | (val & mask); + } else { + match offset { + GPIODIR => { + /* Direction Register */ + self.dir = val & 0xff; + } + GPIOIS => { + /* Interrupt Sense Register */ + self.isense = val & 0xff; + } + GPIOIBE => { + /* Interrupt Both Edges Register */ + self.ibe = val & 0xff; + } + GPIOIEV => { + /* Interrupt Event Register */ + self.iev = val & 0xff; + } + GPIOIE => { + /* Interrupt Mask Register */ + self.im = val & 0xff; + } + GPIOIC => { + /* Interrupt Clear Register */ + self.istate &= !val; + } + GPIOAFSEL => { + /* Mode Control Select Register */ + self.afsel = val & 0xff; + } + o => { + return Err(Error::BadWriteOffset(o)); + } + } + } + Ok(()) + } + + pub fn trigger_key(&mut self, key: u32) -> Result<()> { + let mask = (1 << key) as u32; + if (!self.dir & mask) > 0 { + // emulate key event + // By default, Input Pin is configured to detect both rising and falling edges. + // So reverse the input pin data to generate a pulse. + self.data |= !(self.data & mask) & mask; + self.pl061_internal_update(); + + match self.trigger_gpio_interrupt() { + Ok(_) | Err(Error::GpioInterruptDisabled) => return Ok(()), + Err(e) => return Err(e), + } + } + + Err(Error::GpioTriggerKeyFailure(key)) + } + + fn trigger_gpio_interrupt(&self) -> Result<()> { + // Bits set to high in GPIOIE(Interrupt mask register) allow the corresponding pins to + // trigger their individual interrupts and then the combined GPIOINTR line. + if (self.istate & self.im) == 0 { + warn!("Failed to trigger GPIO input interrupt (disabled by guest OS)"); + return Err(Error::GpioInterruptDisabled); + } + self.interrupt + .trigger(0) + .map_err(Error::GpioInterruptFailure)?; + Ok(()) + } +} + +impl BusDevice for Gpio { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + let value; + let mut read_ok = true; + + if (GPIO_ID_LOW..GPIO_ID_HIGH).contains(&offset) { + let index = ((offset - GPIO_ID_LOW) >> 2) as usize; + value = u32::from(GPIO_ID[index]); + } else if offset < OFS_DATA { + value = self.data & ((offset >> 2) as u32) + } else { + value = match offset { + GPIODIR => self.dir, + GPIOIS => self.isense, + GPIOIBE => self.ibe, + GPIOIEV => self.iev, + GPIOIE => self.im, + GPIORIE => self.istate, + GPIOMIS => self.istate & self.im, + GPIOAFSEL => self.afsel, + _ => { + read_ok = false; + 0 + } + }; + } + + if read_ok && data.len() <= 4 { + write_le_u32(data, value); + } else { + warn!( + "Invalid GPIO PL061 read: offset {}, data length {}", + offset, + data.len() + ); + } + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + if data.len() <= 4 { + let value = read_le_u32(data); + if let Err(e) = self.handle_write(offset, value) { + warn!("Failed to write to GPIO PL061 device: {}", e); + } + } else { + warn!( + "Invalid GPIO PL061 write: offset {}, data length {}", + offset, + data.len() + ); + } + + None + } +} + +impl Snapshottable for Gpio { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + self.set_state(&snapshot.to_state(&self.id)?); + Ok(()) + } +} + +impl Pausable for Gpio {} +impl Transportable for Gpio {} +impl Migratable for Gpio {} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{read_le_u32, write_le_u32}; + use std::sync::Arc; + use vm_device::interrupt::{InterruptIndex, InterruptSourceConfig}; + use vmm_sys_util::eventfd::EventFd; + + const GPIO_NAME: &str = "gpio"; + const LEGACY_GPIO_MAPPED_IO_START: u64 = 0x0902_0000; + + struct TestInterrupt { + event_fd: EventFd, + } + + impl InterruptSourceGroup for TestInterrupt { + fn trigger(&self, _index: InterruptIndex) -> result::Result<(), std::io::Error> { + self.event_fd.write(1) + } + + fn update( + &self, + _index: InterruptIndex, + _config: InterruptSourceConfig, + _masked: bool, + ) -> result::Result<(), std::io::Error> { + Ok(()) + } + + fn notifier(&self, _index: InterruptIndex) -> Option { + Some(self.event_fd.try_clone().unwrap()) + } + } + + impl TestInterrupt { + fn new(event_fd: EventFd) -> Self { + TestInterrupt { event_fd } + } + } + + #[test] + fn test_gpio_read_write_and_event() { + let intr_evt = EventFd::new(libc::EFD_NONBLOCK).unwrap(); + let mut gpio = Gpio::new( + String::from(GPIO_NAME), + Arc::new(TestInterrupt::new(intr_evt.try_clone().unwrap())), + ); + let mut data = [0; 4]; + + // Read and write to the GPIODIR register. + // Set pin 0 output pin. + write_le_u32(&mut data, 1); + gpio.write(LEGACY_GPIO_MAPPED_IO_START, GPIODIR, &data); + gpio.read(LEGACY_GPIO_MAPPED_IO_START, GPIODIR, &mut data); + let v = read_le_u32(&data); + assert_eq!(v, 1); + + // Read and write to the GPIODATA register. + write_le_u32(&mut data, 1); + // Set pin 0 high. + let offset = 0x00000004_u64; + gpio.write(LEGACY_GPIO_MAPPED_IO_START, offset, &data); + gpio.read(LEGACY_GPIO_MAPPED_IO_START, offset, &mut data); + let v = read_le_u32(&data); + assert_eq!(v, 1); + + // Read and write to the GPIOIS register. + // Configure pin 0 detecting level interrupt. + write_le_u32(&mut data, 1); + gpio.write(LEGACY_GPIO_MAPPED_IO_START, GPIOIS, &data); + gpio.read(LEGACY_GPIO_MAPPED_IO_START, GPIOIS, &mut data); + let v = read_le_u32(&data); + assert_eq!(v, 1); + + // Read and write to the GPIOIBE register. + // Configure pin 1 detecting both falling and rising edges. + write_le_u32(&mut data, 2); + gpio.write(LEGACY_GPIO_MAPPED_IO_START, GPIOIBE, &data); + gpio.read(LEGACY_GPIO_MAPPED_IO_START, GPIOIBE, &mut data); + let v = read_le_u32(&data); + assert_eq!(v, 2); + + // Read and write to the GPIOIEV register. + // Configure pin 2 detecting both falling and rising edges. + write_le_u32(&mut data, 4); + gpio.write(LEGACY_GPIO_MAPPED_IO_START, GPIOIEV, &data); + gpio.read(LEGACY_GPIO_MAPPED_IO_START, GPIOIEV, &mut data); + let v = read_le_u32(&data); + assert_eq!(v, 4); + + // Read and write to the GPIOIE register. + // Configure pin 0...2 capable of triggering their individual interrupts + // and then the combined GPIOINTR line. + write_le_u32(&mut data, 7); + gpio.write(LEGACY_GPIO_MAPPED_IO_START, GPIOIE, &data); + gpio.read(LEGACY_GPIO_MAPPED_IO_START, GPIOIE, &mut data); + let v = read_le_u32(&data); + assert_eq!(v, 7); + + let mask = 0x00000002_u32; + // emulate an rising pulse in pin 1. + gpio.data |= !(gpio.data & mask) & mask; + gpio.pl061_internal_update(); + // The interrupt line on pin 1 should be on. + // Read the GPIOMIS register. + gpio.read(LEGACY_GPIO_MAPPED_IO_START, GPIOMIS, &mut data); + let v = read_le_u32(&data); + assert_eq!(v, 2); + + // Read and Write to the GPIOIC register. + // clear interrupt in pin 1. + write_le_u32(&mut data, 2); + gpio.write(LEGACY_GPIO_MAPPED_IO_START, GPIOIC, &data); + gpio.read(LEGACY_GPIO_MAPPED_IO_START, GPIOIC, &mut data); + let v = read_le_u32(&data); + assert_eq!(v, 2); + + // Attempts to write beyond the writable space. + write_le_u32(&mut data, 0); + gpio.write(LEGACY_GPIO_MAPPED_IO_START, GPIO_ID_LOW, &data); + + let mut data = [0; 4]; + gpio.read(LEGACY_GPIO_MAPPED_IO_START, GPIO_ID_LOW, &mut data); + let index = GPIO_ID_LOW + 3; + assert_eq!(data[0], GPIO_ID[((index - GPIO_ID_LOW) >> 2) as usize]); + } +} diff --git a/vendor/cube-hypervisor/devices/src/legacy/i8042.rs b/vendor/cube-hypervisor/devices/src/legacy/i8042.rs new file mode 100644 index 0000000..76d2e9c --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/legacy/i8042.rs @@ -0,0 +1,45 @@ +// Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use std::sync::{Arc, Barrier}; +use vm_device::BusDevice; +use vmm_sys_util::eventfd::EventFd; + +/// A i8042 PS/2 controller that emulates just enough to shutdown the machine. +pub struct I8042Device { + reset_evt: EventFd, +} + +impl I8042Device { + /// Constructs a i8042 device that will signal the given event when the guest requests it. + pub fn new(reset_evt: EventFd) -> I8042Device { + I8042Device { reset_evt } + } +} + +// i8042 device is located at I/O port 0x61. We partially implement two 8-bit +// registers: port 0x61 (I8042_PORT_B_REG, offset 0 from base of 0x61), and +// port 0x64 (I8042_COMMAND_REG, offset 3 from base of 0x61). +impl BusDevice for I8042Device { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + if data.len() == 1 && offset == 3 { + data[0] = 0x0; + } else if data.len() == 1 && offset == 0 { + // Like kvmtool, we return bit 5 set in I8042_PORT_B_REG to + // avoid hang in pit_calibrate_tsc() in Linux kernel. + data[0] = 0x20; + } + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + if data.len() == 1 && data[0] == 0xfe && offset == 3 { + warn!("Shutdown because of i8042 reset signalled"); + if let Err(e) = self.reset_evt.write(1) { + error!("Error triggering i8042 reset event: {}", e); + } + } + + None + } +} diff --git a/vendor/cube-hypervisor/devices/src/legacy/mod.rs b/vendor/cube-hypervisor/devices/src/legacy/mod.rs new file mode 100644 index 0000000..7ac0737 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/legacy/mod.rs @@ -0,0 +1,39 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +mod cmos; +#[cfg(target_arch = "x86_64")] +mod debug_port; +#[cfg(target_arch = "x86_64")] +mod fwdebug; +#[cfg(target_arch = "aarch64")] +mod gpio_pl061; +mod i8042; +#[cfg(target_arch = "aarch64")] +mod rtc_pl031; +mod serial; +mod sys_ctrl; +#[cfg(target_arch = "aarch64")] +mod uart_pl011; + +pub use self::cmos::Cmos; +#[cfg(target_arch = "x86_64")] +pub use self::debug_port::DebugPort; +#[cfg(target_arch = "x86_64")] +pub use self::fwdebug::FwDebugDevice; +pub use self::i8042::I8042Device; +pub use self::serial::Serial; +pub use self::sys_ctrl::SysCtrl; + +#[cfg(target_arch = "aarch64")] +pub use self::gpio_pl061::Error as GpioDeviceError; +#[cfg(target_arch = "aarch64")] +pub use self::gpio_pl061::Gpio; +#[cfg(target_arch = "aarch64")] +pub use self::rtc_pl031::Rtc; +#[cfg(target_arch = "aarch64")] +pub use self::uart_pl011::Pl011; diff --git a/vendor/cube-hypervisor/devices/src/legacy/rtc_pl031.rs b/vendor/cube-hypervisor/devices/src/legacy/rtc_pl031.rs new file mode 100644 index 0000000..b07b009 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/legacy/rtc_pl031.rs @@ -0,0 +1,584 @@ +// Copyright 2020 Arm Limited (or its affiliates). All rights reserved. +// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! ARM PL031 Real Time Clock +//! +//! This module implements a PL031 Real Time Clock (RTC) that provides to provides long time base counter. +//! This is achieved by generating an interrupt signal after counting for a programmed number of cycles of +//! a real-time clock input. +//! +use crate::{read_le_u32, write_le_u32}; +use std::fmt; +use std::sync::{Arc, Barrier}; +use std::time::Instant; +use std::{io, result}; +use vm_device::interrupt::InterruptSourceGroup; +use vm_device::BusDevice; + +// As you can see in https://static.docs.arm.com/ddi0224/c/real_time_clock_pl031_r1p3_technical_reference_manual_DDI0224C.pdf +// at section 3.2 Summary of RTC registers, the total size occupied by this device is 0x000 -> 0xFFC + 4 = 0x1000. +// From 0x0 to 0x1C we have following registers: +const RTCDR: u64 = 0x0; // Data Register. +const RTCMR: u64 = 0x4; // Match Register. +const RTCLR: u64 = 0x8; // Load Register. +const RTCCR: u64 = 0xc; // Control Register. +const RTCIMSC: u64 = 0x10; // Interrupt Mask Set or Clear Register. +const RTCRIS: u64 = 0x14; // Raw Interrupt Status. +const RTCMIS: u64 = 0x18; // Masked Interrupt Status. +const RTCICR: u64 = 0x1c; // Interrupt Clear Register. + // From 0x020 to 0xFDC => reserved space. + // From 0xFE0 to 0x1000 => Peripheral and PrimeCell Identification Registers which are Read Only registers. + // AMBA standard devices have CIDs (Cell IDs) and PIDs (Peripheral IDs). The linux kernel will look for these in order to assert the identity + // of these devices (i.e look at the `amba_device_try_add` function). + // We are putting the expected values (look at 'Reset value' column from above mentioned document) in an array. +const PL031_ID: [u8; 8] = [0x31, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1]; +// We are only interested in the margins. +const AMBA_ID_LOW: u64 = 0xFE0; +const AMBA_ID_HIGH: u64 = 0x1000; +/// Constant to convert seconds to nanoseconds. +pub const NANOS_PER_SECOND: u64 = 1_000_000_000; + +#[derive(Debug)] +pub enum Error { + BadWriteOffset(u64), + InterruptFailure(io::Error), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Error::BadWriteOffset(offset) => write!(f, "Bad Write Offset: {}", offset), + Error::InterruptFailure(e) => write!(f, "Failed to trigger interrupt: {}", e), + } + } +} + +type Result = result::Result; + +/// Wrapper over `libc::clockid_t` to specify Linux Kernel clock source. +pub enum ClockType { + /// Equivalent to `libc::CLOCK_MONOTONIC`. + Monotonic, + /// Equivalent to `libc::CLOCK_REALTIME`. + #[allow(dead_code)] + Real, + /// Equivalent to `libc::CLOCK_PROCESS_CPUTIME_ID`. + ProcessCpu, + /// Equivalent to `libc::CLOCK_THREAD_CPUTIME_ID`. + #[allow(dead_code)] + ThreadCpu, +} + +impl From for libc::clockid_t { + fn from(ct: ClockType) -> libc::clockid_t { + match ct { + ClockType::Monotonic => libc::CLOCK_MONOTONIC, + ClockType::Real => libc::CLOCK_REALTIME, + ClockType::ProcessCpu => libc::CLOCK_PROCESS_CPUTIME_ID, + ClockType::ThreadCpu => libc::CLOCK_THREAD_CPUTIME_ID, + } + } +} + +/// Structure representing the date in local time with nanosecond precision. +pub struct LocalTime { + /// Seconds in current minute. + sec: i32, + /// Minutes in current hour. + min: i32, + /// Hours in current day, 24H format. + hour: i32, + /// Days in current month. + mday: i32, + /// Months in current year. + mon: i32, + /// Years passed since 1900 BC. + year: i32, + /// Nanoseconds in current second. + nsec: i64, +} + +impl LocalTime { + /// Returns the [LocalTime](struct.LocalTime.html) structure for the calling moment. + #[allow(dead_code)] + pub fn now() -> LocalTime { + let mut timespec = libc::timespec { + tv_sec: 0, + tv_nsec: 0, + }; + let mut tm: libc::tm = libc::tm { + tm_sec: 0, + tm_min: 0, + tm_hour: 0, + tm_mday: 0, + tm_mon: 0, + tm_year: 0, + tm_wday: 0, + tm_yday: 0, + tm_isdst: 0, + tm_gmtoff: 0, + tm_zone: std::ptr::null(), + }; + + // Safe because the parameters are valid. + unsafe { + libc::clock_gettime(libc::CLOCK_REALTIME, &mut timespec); + libc::localtime_r(×pec.tv_sec, &mut tm); + } + + LocalTime { + sec: tm.tm_sec, + min: tm.tm_min, + hour: tm.tm_hour, + mday: tm.tm_mday, + mon: tm.tm_mon, + year: tm.tm_year, + nsec: timespec.tv_nsec, + } + } +} + +impl fmt::Display for LocalTime { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}-{:02}-{:02}T{:02}:{:02}:{:02}.{:09}", + self.year + 1900, + self.mon + 1, + self.mday, + self.hour, + self.min, + self.sec, + self.nsec + ) + } +} + +/// Holds a micro-second resolution timestamp with both the real time and cpu time. +#[derive(Clone)] +pub struct TimestampUs { + /// Real time in microseconds. + pub time_us: u64, + /// Cpu time in microseconds. + pub cputime_us: u64, +} + +impl Default for TimestampUs { + fn default() -> TimestampUs { + TimestampUs { + time_us: get_time(ClockType::Monotonic) / 1000, + cputime_us: get_time(ClockType::ProcessCpu) / 1000, + } + } +} + +/// Returns a timestamp in nanoseconds from a monotonic clock. +/// +/// Uses `_rdstc` on `x86_64` and [`get_time`](fn.get_time.html) on other architectures. +#[allow(dead_code)] +pub fn timestamp_cycles() -> u64 { + #[cfg(target_arch = "x86_64")] + // Safe because there's nothing that can go wrong with this call. + unsafe { + std::arch::x86_64::_rdtsc() as u64 + } + #[cfg(not(target_arch = "x86_64"))] + { + get_time(ClockType::Monotonic) + } +} + +/// Returns a timestamp in nanoseconds based on the provided clock type. +/// +/// # Arguments +/// +/// * `clock_type` - Identifier of the Linux Kernel clock on which to act. +pub fn get_time(clock_type: ClockType) -> u64 { + let mut time_struct = libc::timespec { + tv_sec: 0, + tv_nsec: 0, + }; + // Safe because the parameters are valid. + unsafe { libc::clock_gettime(clock_type.into(), &mut time_struct) }; + seconds_to_nanoseconds(time_struct.tv_sec).unwrap() as u64 + (time_struct.tv_nsec as u64) +} + +/// Converts a timestamp in seconds to an equivalent one in nanoseconds. +/// Returns `None` if the conversion overflows. +/// +/// # Arguments +/// +/// * `value` - Timestamp in seconds. +pub fn seconds_to_nanoseconds(value: i64) -> Option { + value.checked_mul(NANOS_PER_SECOND as i64) +} + +/// A RTC device following the PL031 specification.. +pub struct Rtc { + previous_now: Instant, + tick_offset: i64, + // This is used for implementing the RTC alarm. However, in Firecracker we do not need it. + match_value: u32, + // Writes to this register load an update value into the RTC. + load: u32, + imsc: u32, + ris: u32, + interrupt: Arc, +} + +impl Rtc { + /// Constructs an AMBA PL031 RTC device. + pub fn new(interrupt: Arc) -> Self { + Self { + // This is used only for duration measuring purposes. + previous_now: Instant::now(), + tick_offset: get_time(ClockType::Real) as i64, + match_value: 0, + load: 0, + imsc: 0, + ris: 0, + interrupt, + } + } + + fn trigger_interrupt(&mut self) -> Result<()> { + self.interrupt.trigger(0).map_err(Error::InterruptFailure)?; + Ok(()) + } + + fn get_time(&self) -> u32 { + let ts = (self.tick_offset as i128) + + (Instant::now().duration_since(self.previous_now).as_nanos() as i128); + (ts / NANOS_PER_SECOND as i128) as u32 + } + + fn handle_write(&mut self, offset: u64, val: u32) -> Result<()> { + match offset { + RTCMR => { + // The MR register is used for implementing the RTC alarm. A real time clock alarm is + // a feature that can be used to allow a computer to 'wake up' after shut down to execute + // tasks every day or on a certain day. It can sometimes be found in the 'Power Management' + // section of a motherboard's BIOS setup. This is functionality that extends beyond + // Firecracker intended use. However, we increment a metric just in case. + self.match_value = val; + } + RTCLR => { + self.load = val; + self.previous_now = Instant::now(); + // If the unwrap fails, then the internal value of the clock has been corrupted and + // we want to terminate the execution of the process. + self.tick_offset = seconds_to_nanoseconds(i64::from(val)).unwrap(); + } + RTCIMSC => { + self.imsc = val & 1; + self.trigger_interrupt()?; + } + RTCICR => { + // As per above mentioned doc, the interrupt is cleared by writing any data value to + // the Interrupt Clear Register. + self.ris = 0; + self.trigger_interrupt()?; + } + RTCCR => (), // ignore attempts to turn off the timer. + o => { + return Err(Error::BadWriteOffset(o)); + } + } + Ok(()) + } +} + +impl BusDevice for Rtc { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + let mut read_ok = true; + + let v = if (AMBA_ID_LOW..AMBA_ID_HIGH).contains(&offset) { + let index = ((offset - AMBA_ID_LOW) >> 2) as usize; + u32::from(PL031_ID[index]) + } else { + match offset { + RTCDR => self.get_time(), + RTCMR => { + // Even though we are not implementing RTC alarm we return the last value + self.match_value + } + RTCLR => self.load, + RTCCR => 1, // RTC is always enabled. + RTCIMSC => self.imsc, + RTCRIS => self.ris, + RTCMIS => self.ris & self.imsc, + _ => { + read_ok = false; + 0 + } + } + }; + if read_ok && data.len() <= 4 { + write_le_u32(data, v); + } else { + warn!( + "Invalid RTC PL031 read: offset {}, data length {}", + offset, + data.len() + ); + } + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + if data.len() <= 4 { + let v = read_le_u32(data); + if let Err(e) = self.handle_write(offset, v) { + warn!("Failed to write to RTC PL031 device: {}", e); + } + } else { + warn!( + "Invalid RTC PL031 write: offset {}, data length {}", + offset, + data.len() + ); + } + + None + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + read_be_u16, read_be_u32, read_le_i32, read_le_u16, read_le_u32, read_le_u64, write_be_u16, + write_be_u32, write_le_i32, write_le_u16, write_le_u32, write_le_u64, + }; + use std::sync::Arc; + use vm_device::interrupt::{InterruptIndex, InterruptSourceConfig}; + use vmm_sys_util::eventfd::EventFd; + + const LEGACY_RTC_MAPPED_IO_START: u64 = 0x0901_0000; + + #[test] + fn test_get_time() { + for _ in 0..1000 { + assert!(get_time(ClockType::Monotonic) <= get_time(ClockType::Monotonic)); + } + + for _ in 0..1000 { + assert!(get_time(ClockType::ProcessCpu) <= get_time(ClockType::ProcessCpu)); + } + + for _ in 0..1000 { + assert!(get_time(ClockType::ThreadCpu) <= get_time(ClockType::ThreadCpu)); + } + + assert_ne!(get_time(ClockType::Real), 0); + } + + #[test] + fn test_local_time_display() { + let local_time = LocalTime { + sec: 30, + min: 15, + hour: 10, + mday: 4, + mon: 6, + year: 119, + nsec: 123_456_789, + }; + assert_eq!( + String::from("2019-07-04T10:15:30.123456789"), + local_time.to_string() + ); + + let local_time = LocalTime { + sec: 5, + min: 5, + hour: 5, + mday: 23, + mon: 7, + year: 44, + nsec: 123, + }; + assert_eq!( + String::from("1944-08-23T05:05:05.000000123"), + local_time.to_string() + ); + + let local_time = LocalTime::now(); + assert!(local_time.mon >= 0 && local_time.mon <= 11); + } + + #[test] + fn test_seconds_to_nanoseconds() { + assert_eq!( + seconds_to_nanoseconds(100).unwrap() as u64, + 100 * NANOS_PER_SECOND + ); + + assert!(seconds_to_nanoseconds(9_223_372_037).is_none()); + } + + struct TestInterrupt { + event_fd: EventFd, + } + + impl InterruptSourceGroup for TestInterrupt { + fn trigger(&self, _index: InterruptIndex) -> result::Result<(), std::io::Error> { + self.event_fd.write(1) + } + + fn update( + &self, + _index: InterruptIndex, + _config: InterruptSourceConfig, + _masked: bool, + ) -> result::Result<(), std::io::Error> { + Ok(()) + } + + fn notifier(&self, _index: InterruptIndex) -> Option { + Some(self.event_fd.try_clone().unwrap()) + } + } + + impl TestInterrupt { + fn new(event_fd: EventFd) -> Self { + TestInterrupt { event_fd } + } + } + + #[test] + fn test_rtc_read_write_and_event() { + let intr_evt = EventFd::new(libc::EFD_NONBLOCK).unwrap(); + + let mut rtc = Rtc::new(Arc::new(TestInterrupt::new(intr_evt.try_clone().unwrap()))); + let mut data = [0; 4]; + + // Read and write to the MR register. + write_le_u32(&mut data, 123); + rtc.write(LEGACY_RTC_MAPPED_IO_START, RTCMR, &data); + rtc.read(LEGACY_RTC_MAPPED_IO_START, RTCMR, &mut data); + let v = read_le_u32(&data); + assert_eq!(v, 123); + + // Read and write to the LR register. + let v = get_time(ClockType::Real); + write_le_u32(&mut data, (v / NANOS_PER_SECOND) as u32); + let previous_now_before = rtc.previous_now; + rtc.write(LEGACY_RTC_MAPPED_IO_START, RTCLR, &data); + + assert!(rtc.previous_now > previous_now_before); + + rtc.read(LEGACY_RTC_MAPPED_IO_START, RTCLR, &mut data); + let v_read = read_le_u32(&data); + assert_eq!((v / NANOS_PER_SECOND) as u32, v_read); + + // Read and write to IMSC register. + // Test with non zero value. + let non_zero = 1; + write_le_u32(&mut data, non_zero); + rtc.write(LEGACY_RTC_MAPPED_IO_START, RTCIMSC, &data); + // The interrupt line should be on. + assert!(rtc.interrupt.notifier(0).unwrap().read().unwrap() == 1); + rtc.read(LEGACY_RTC_MAPPED_IO_START, RTCIMSC, &mut data); + let v = read_le_u32(&data); + assert_eq!(non_zero & 1, v); + + // Now test with 0. + write_le_u32(&mut data, 0); + rtc.write(LEGACY_RTC_MAPPED_IO_START, RTCIMSC, &data); + rtc.read(LEGACY_RTC_MAPPED_IO_START, RTCIMSC, &mut data); + let v = read_le_u32(&data); + assert_eq!(0, v); + + // Read and write to the ICR register. + write_le_u32(&mut data, 1); + rtc.write(LEGACY_RTC_MAPPED_IO_START, RTCICR, &data); + // The interrupt line should be on. + assert!(rtc.interrupt.notifier(0).unwrap().read().unwrap() > 1); + let v_before = read_le_u32(&data); + + rtc.read(LEGACY_RTC_MAPPED_IO_START, RTCICR, &mut data); + let v = read_le_u32(&data); + // ICR is a write only register. Data received should stay equal to data sent. + assert_eq!(v, v_before); + + // Attempts to turn off the RTC should not go through. + write_le_u32(&mut data, 0); + rtc.write(LEGACY_RTC_MAPPED_IO_START, RTCCR, &data); + rtc.read(LEGACY_RTC_MAPPED_IO_START, RTCCR, &mut data); + let v = read_le_u32(&data); + assert_eq!(v, 1); + + // Attempts to write beyond the writable space. Using here the space used to read + // the CID and PID from. + write_le_u32(&mut data, 0); + rtc.write(LEGACY_RTC_MAPPED_IO_START, AMBA_ID_LOW, &data); + // However, reading from the AMBA_ID_LOW should succeed upon read. + + let mut data = [0; 4]; + rtc.read(LEGACY_RTC_MAPPED_IO_START, AMBA_ID_LOW, &mut data); + let index = AMBA_ID_LOW + 3; + assert_eq!(data[0], PL031_ID[((index - AMBA_ID_LOW) >> 2) as usize]); + } + + macro_rules! byte_order_test_read_write { + ($test_name: ident, $write_fn_name: ident, $read_fn_name: ident, $is_be: expr, $data_type: ty) => { + #[test] + fn $test_name() { + #[allow(overflowing_literals)] + let test_cases = [ + ( + 0x0123_4567_89AB_CDEF as u64, + [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef], + ), + ( + 0x0000_0000_0000_0000 as u64, + [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], + ), + ( + 0x1923_2345_ABF3_CCD4 as u64, + [0x19, 0x23, 0x23, 0x45, 0xAB, 0xF3, 0xCC, 0xD4], + ), + ( + 0x0FF0_0FF0_0FF0_0FF0 as u64, + [0x0F, 0xF0, 0x0F, 0xF0, 0x0F, 0xF0, 0x0F, 0xF0], + ), + ( + 0xFFFF_FFFF_FFFF_FFFF as u64, + [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], + ), + ( + 0x89AB_12D4_C2D2_09BB as u64, + [0x89, 0xAB, 0x12, 0xD4, 0xC2, 0xD2, 0x09, 0xBB], + ), + ]; + + let type_size = std::mem::size_of::<$data_type>(); + for (test_val, v_arr) in &test_cases { + let v = *test_val as $data_type; + let cmp_iter: Box> = if $is_be { + Box::new(v_arr[(8 - type_size)..].iter()) + } else { + Box::new(v_arr.iter().rev()) + }; + // test write + let mut write_arr = vec![Default::default(); type_size]; + $write_fn_name(&mut write_arr, v); + for (cmp, cur) in cmp_iter.zip(write_arr.iter()) { + assert_eq!(*cmp, *cur as u8) + } + // test read + let read_val = $read_fn_name(&write_arr); + assert_eq!(v, read_val); + } + } + }; + } + + byte_order_test_read_write!(test_le_u16, write_le_u16, read_le_u16, false, u16); + byte_order_test_read_write!(test_le_u32, write_le_u32, read_le_u32, false, u32); + byte_order_test_read_write!(test_le_u64, write_le_u64, read_le_u64, false, u64); + byte_order_test_read_write!(test_le_i32, write_le_i32, read_le_i32, false, i32); + byte_order_test_read_write!(test_be_u16, write_be_u16, read_be_u16, true, u16); + byte_order_test_read_write!(test_be_u32, write_be_u32, read_be_u32, true, u32); +} diff --git a/vendor/cube-hypervisor/devices/src/legacy/serial.rs b/vendor/cube-hypervisor/devices/src/legacy/serial.rs new file mode 100644 index 0000000..787db4e --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/legacy/serial.rs @@ -0,0 +1,516 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use serde::{Deserialize, Serialize}; +use std::collections::VecDeque; +use std::sync::{Arc, Barrier}; +use std::{io, result}; +use vm_device::interrupt::InterruptSourceGroup; +use vm_device::BusDevice; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vmm_sys_util::errno::Result; + +const LOOP_SIZE: usize = 0x40; + +const DATA: u8 = 0; +const IER: u8 = 1; +const IIR: u8 = 2; +const LCR: u8 = 3; +const MCR: u8 = 4; +const LSR: u8 = 5; +const MSR: u8 = 6; +const SCR: u8 = 7; + +const DLAB_LOW: u8 = 0; +const DLAB_HIGH: u8 = 1; + +const IER_RECV_BIT: u8 = 0x1; +const IER_THR_BIT: u8 = 0x2; +const IER_FIFO_BITS: u8 = 0x0f; + +const IIR_FIFO_BITS: u8 = 0xc0; +const IIR_NONE_BIT: u8 = 0x1; +const IIR_THR_BIT: u8 = 0x2; +const IIR_RECV_BIT: u8 = 0x4; + +const LCR_DLAB_BIT: u8 = 0x80; + +const LSR_DATA_BIT: u8 = 0x1; +const LSR_EMPTY_BIT: u8 = 0x20; +const LSR_IDLE_BIT: u8 = 0x40; + +const MCR_LOOP_BIT: u8 = 0x10; + +const DEFAULT_INTERRUPT_IDENTIFICATION: u8 = IIR_NONE_BIT; // no pending interrupt +const DEFAULT_LINE_STATUS: u8 = LSR_EMPTY_BIT | LSR_IDLE_BIT; // THR empty and line is idle +const DEFAULT_LINE_CONTROL: u8 = 0x3; // 8-bits per character +const DEFAULT_MODEM_CONTROL: u8 = 0x8; // Auxiliary output 2 +const DEFAULT_MODEM_STATUS: u8 = 0x20 | 0x10 | 0x80; // data ready, clear to send, carrier detect +const DEFAULT_BAUD_DIVISOR: u16 = 12; // 9600 bps + +/// Emulates serial COM ports commonly seen on x86 I/O ports 0x3f8/0x2f8/0x3e8/0x2e8. +/// +/// This can optionally write the guest's output to a Write trait object. To send input to the +/// guest, use `queue_input_bytes`. +pub struct Serial { + id: String, + interrupt_enable: u8, + interrupt_identification: u8, + interrupt: Arc, + line_control: u8, + line_status: u8, + modem_control: u8, + modem_status: u8, + scratch: u8, + baud_divisor: u16, + in_buffer: VecDeque, + out: Option>, +} + +#[derive(Serialize, Deserialize)] +pub struct SerialState { + interrupt_enable: u8, + interrupt_identification: u8, + line_control: u8, + line_status: u8, + modem_control: u8, + modem_status: u8, + scratch: u8, + baud_divisor: u16, + in_buffer: Vec, +} + +impl Serial { + pub fn new( + id: String, + interrupt: Arc, + out: Option>, + ) -> Serial { + Serial { + id, + interrupt_enable: 0, + interrupt_identification: DEFAULT_INTERRUPT_IDENTIFICATION, + interrupt, + line_control: DEFAULT_LINE_CONTROL, + line_status: DEFAULT_LINE_STATUS, + modem_control: DEFAULT_MODEM_CONTROL, + modem_status: DEFAULT_MODEM_STATUS, + scratch: 0, + baud_divisor: DEFAULT_BAUD_DIVISOR, + in_buffer: VecDeque::new(), + out, + } + } + + /// Constructs a Serial port ready for output. + pub fn new_out( + id: String, + interrupt: Arc, + out: Box, + ) -> Serial { + Self::new(id, interrupt, Some(out)) + } + + /// Constructs a Serial port with no connected output. + pub fn new_sink(id: String, interrupt: Arc) -> Serial { + Self::new(id, interrupt, None) + } + + pub fn set_out(&mut self, out: Box) { + self.out = Some(out); + } + + /// Queues raw bytes for the guest to read and signals the interrupt if the line status would + /// change. + pub fn queue_input_bytes(&mut self, c: &[u8]) -> Result<()> { + if !self.is_loop() { + self.in_buffer.extend(c); + self.recv_data()?; + } + Ok(()) + } + + pub fn flush_output(&mut self) -> result::Result<(), io::Error> { + if let Some(out) = self.out.as_mut() { + out.flush()?; + } + Ok(()) + } + + fn is_dlab_set(&self) -> bool { + (self.line_control & LCR_DLAB_BIT) != 0 + } + + fn is_recv_intr_enabled(&self) -> bool { + (self.interrupt_enable & IER_RECV_BIT) != 0 + } + + fn is_thr_intr_enabled(&self) -> bool { + (self.interrupt_enable & IER_THR_BIT) != 0 + } + + fn is_loop(&self) -> bool { + (self.modem_control & MCR_LOOP_BIT) != 0 + } + + fn add_intr_bit(&mut self, bit: u8) { + self.interrupt_identification &= !IIR_NONE_BIT; + self.interrupt_identification |= bit; + } + + fn del_intr_bit(&mut self, bit: u8) { + self.interrupt_identification &= !bit; + if self.interrupt_identification == 0x0 { + self.interrupt_identification = IIR_NONE_BIT; + } + } + + fn thr_empty(&mut self) -> Result<()> { + if self.is_thr_intr_enabled() { + self.add_intr_bit(IIR_THR_BIT); + self.trigger_interrupt()? + } + Ok(()) + } + + fn recv_data(&mut self) -> Result<()> { + if self.is_recv_intr_enabled() { + self.add_intr_bit(IIR_RECV_BIT); + self.trigger_interrupt()? + } + self.line_status |= LSR_DATA_BIT; + Ok(()) + } + + fn trigger_interrupt(&mut self) -> result::Result<(), io::Error> { + self.interrupt.trigger(0) + } + + fn iir_reset(&mut self) { + self.interrupt_identification = DEFAULT_INTERRUPT_IDENTIFICATION; + } + + fn handle_write(&mut self, offset: u8, v: u8) -> Result<()> { + match offset { + DLAB_LOW if self.is_dlab_set() => { + self.baud_divisor = (self.baud_divisor & 0xff00) | u16::from(v) + } + DLAB_HIGH if self.is_dlab_set() => { + self.baud_divisor = (self.baud_divisor & 0x00ff) | ((u16::from(v)) << 8) + } + DATA => { + if self.is_loop() { + if self.in_buffer.len() < LOOP_SIZE { + self.in_buffer.push_back(v); + self.recv_data()?; + } + } else { + if let Some(out) = self.out.as_mut() { + out.write_all(&[v])?; + out.flush()?; + } + self.thr_empty()?; + } + } + IER => self.interrupt_enable = v & IER_FIFO_BITS, + LCR => self.line_control = v, + MCR => self.modem_control = v, + SCR => self.scratch = v, + _ => {} + } + Ok(()) + } + + fn state(&self) -> SerialState { + SerialState { + interrupt_enable: self.interrupt_enable, + interrupt_identification: self.interrupt_identification, + line_control: self.line_control, + line_status: self.line_status, + modem_control: self.modem_control, + modem_status: self.modem_status, + scratch: self.scratch, + baud_divisor: self.baud_divisor, + in_buffer: self.in_buffer.clone().into(), + } + } + + fn set_state(&mut self, state: &SerialState) { + self.interrupt_enable = state.interrupt_enable; + self.interrupt_identification = state.interrupt_identification; + self.line_control = state.line_control; + self.line_status = state.line_status; + self.modem_control = state.modem_control; + self.modem_status = state.modem_status; + self.scratch = state.scratch; + self.baud_divisor = state.baud_divisor; + self.in_buffer = state.in_buffer.clone().into(); + } +} + +impl BusDevice for Serial { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + if data.len() != 1 { + return; + } + + data[0] = match offset as u8 { + DLAB_LOW if self.is_dlab_set() => self.baud_divisor as u8, + DLAB_HIGH if self.is_dlab_set() => (self.baud_divisor >> 8) as u8, + DATA => { + self.del_intr_bit(IIR_RECV_BIT); + if self.in_buffer.len() <= 1 { + self.line_status &= !LSR_DATA_BIT; + } + self.in_buffer.pop_front().unwrap_or_default() + } + IER => self.interrupt_enable, + IIR => { + let v = self.interrupt_identification | IIR_FIFO_BITS; + self.iir_reset(); + v + } + LCR => self.line_control, + MCR => self.modem_control, + LSR => self.line_status, + MSR => self.modem_status, + SCR => self.scratch, + _ => 0, + }; + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + if data.len() != 1 { + return None; + } + + self.handle_write(offset as u8, data[0]).ok(); + + None + } +} + +impl Snapshottable for Serial { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + self.set_state(&snapshot.to_state(&self.id)?); + Ok(()) + } +} + +impl Pausable for Serial {} +impl Transportable for Serial {} +impl Migratable for Serial {} + +#[cfg(test)] +mod tests { + use super::*; + use std::io; + use std::sync::{Arc, Mutex}; + use vm_device::interrupt::{InterruptIndex, InterruptSourceConfig}; + use vmm_sys_util::eventfd::EventFd; + + const SERIAL_NAME: &str = "serial"; + + struct TestInterrupt { + event_fd: EventFd, + } + + impl InterruptSourceGroup for TestInterrupt { + fn trigger(&self, _index: InterruptIndex) -> result::Result<(), std::io::Error> { + self.event_fd.write(1) + } + fn update( + &self, + _index: InterruptIndex, + _config: InterruptSourceConfig, + _masked: bool, + ) -> result::Result<(), std::io::Error> { + Ok(()) + } + fn notifier(&self, _index: InterruptIndex) -> Option { + Some(self.event_fd.try_clone().unwrap()) + } + } + + impl TestInterrupt { + fn new(event_fd: EventFd) -> Self { + TestInterrupt { event_fd } + } + } + + #[derive(Clone)] + struct SharedBuffer { + buf: Arc>>, + } + + impl SharedBuffer { + fn new() -> SharedBuffer { + SharedBuffer { + buf: Arc::new(Mutex::new(Vec::new())), + } + } + } + + impl io::Write for SharedBuffer { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.buf.lock().unwrap().write(buf) + } + fn flush(&mut self) -> io::Result<()> { + self.buf.lock().unwrap().flush() + } + } + + #[test] + fn serial_output() { + let intr_evt = EventFd::new(0).unwrap(); + let serial_out = SharedBuffer::new(); + let mut serial = Serial::new_out( + String::from(SERIAL_NAME), + Arc::new(TestInterrupt::new(intr_evt.try_clone().unwrap())), + Box::new(serial_out.clone()), + ); + + serial.write(0, DATA as u64, &[b'x', b'y']); + serial.write(0, DATA as u64, &[b'a']); + serial.write(0, DATA as u64, &[b'b']); + serial.write(0, DATA as u64, &[b'c']); + assert_eq!( + serial_out.buf.lock().unwrap().as_slice(), + &[b'a', b'b', b'c'] + ); + } + + #[test] + fn serial_input() { + let intr_evt = EventFd::new(0).unwrap(); + let serial_out = SharedBuffer::new(); + let mut serial = Serial::new_out( + String::from(SERIAL_NAME), + Arc::new(TestInterrupt::new(intr_evt.try_clone().unwrap())), + Box::new(serial_out), + ); + + // write 1 to the interrupt event fd, so that read doesn't block in case the event fd + // counter doesn't change (for 0 it blocks) + assert!(intr_evt.write(1).is_ok()); + serial.write(0, IER as u64, &[IER_RECV_BIT]); + serial.queue_input_bytes(&[b'a', b'b', b'c']).unwrap(); + + assert_eq!(intr_evt.read().unwrap(), 2); + + // check if reading in a 2-length array doesn't have side effects + let mut data = [0u8, 0u8]; + serial.read(0, DATA as u64, &mut data[..]); + assert_eq!(data, [0u8, 0u8]); + + let mut data = [0u8]; + serial.read(0, LSR as u64, &mut data[..]); + assert_ne!(data[0] & LSR_DATA_BIT, 0); + serial.read(0, DATA as u64, &mut data[..]); + assert_eq!(data[0], b'a'); + serial.read(0, DATA as u64, &mut data[..]); + assert_eq!(data[0], b'b'); + serial.read(0, DATA as u64, &mut data[..]); + assert_eq!(data[0], b'c'); + + // check if reading from the largest u8 offset returns 0 + serial.read(0, 0xff, &mut data[..]); + assert_eq!(data[0], 0); + } + + #[test] + fn serial_thr() { + let intr_evt = EventFd::new(0).unwrap(); + let mut serial = Serial::new_sink( + String::from(SERIAL_NAME), + Arc::new(TestInterrupt::new(intr_evt.try_clone().unwrap())), + ); + + // write 1 to the interrupt event fd, so that read doesn't block in case the event fd + // counter doesn't change (for 0 it blocks) + assert!(intr_evt.write(1).is_ok()); + serial.write(0, IER as u64, &[IER_THR_BIT]); + serial.write(0, DATA as u64, &[b'a']); + + assert_eq!(intr_evt.read().unwrap(), 2); + let mut data = [0u8]; + serial.read(0, IER as u64, &mut data[..]); + assert_eq!(data[0] & IER_FIFO_BITS, IER_THR_BIT); + serial.read(0, IIR as u64, &mut data[..]); + assert_ne!(data[0] & IIR_THR_BIT, 0); + } + + #[test] + fn serial_dlab() { + let intr_evt = EventFd::new(0).unwrap(); + let mut serial = Serial::new_sink( + String::from(SERIAL_NAME), + Arc::new(TestInterrupt::new(intr_evt.try_clone().unwrap())), + ); + + serial.write(0, LCR as u64, &[LCR_DLAB_BIT]); + serial.write(0, DLAB_LOW as u64, &[0x12]); + serial.write(0, DLAB_HIGH as u64, &[0x34]); + + let mut data = [0u8]; + serial.read(0, LCR as u64, &mut data[..]); + assert_eq!(data[0], LCR_DLAB_BIT); + serial.read(0, DLAB_LOW as u64, &mut data[..]); + assert_eq!(data[0], 0x12); + serial.read(0, DLAB_HIGH as u64, &mut data[..]); + assert_eq!(data[0], 0x34); + } + + #[test] + fn serial_modem() { + let intr_evt = EventFd::new(0).unwrap(); + let mut serial = Serial::new_sink( + String::from(SERIAL_NAME), + Arc::new(TestInterrupt::new(intr_evt.try_clone().unwrap())), + ); + + serial.write(0, MCR as u64, &[MCR_LOOP_BIT]); + serial.write(0, DATA as u64, &[b'a']); + serial.write(0, DATA as u64, &[b'b']); + serial.write(0, DATA as u64, &[b'c']); + + let mut data = [0u8]; + serial.read(0, MSR as u64, &mut data[..]); + assert_eq!(data[0], DEFAULT_MODEM_STATUS); + serial.read(0, MCR as u64, &mut data[..]); + assert_eq!(data[0], MCR_LOOP_BIT); + serial.read(0, DATA as u64, &mut data[..]); + assert_eq!(data[0], b'a'); + serial.read(0, DATA as u64, &mut data[..]); + assert_eq!(data[0], b'b'); + serial.read(0, DATA as u64, &mut data[..]); + assert_eq!(data[0], b'c'); + } + + #[test] + fn serial_scratch() { + let intr_evt = EventFd::new(0).unwrap(); + let mut serial = Serial::new_sink( + String::from(SERIAL_NAME), + Arc::new(TestInterrupt::new(intr_evt.try_clone().unwrap())), + ); + + serial.write(0, SCR as u64, &[0x12]); + + let mut data = [0u8]; + serial.read(0, SCR as u64, &mut data[..]); + assert_eq!(data[0], 0x12); + } +} diff --git a/vendor/cube-hypervisor/devices/src/legacy/sys_ctrl.rs b/vendor/cube-hypervisor/devices/src/legacy/sys_ctrl.rs new file mode 100644 index 0000000..98623f7 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/legacy/sys_ctrl.rs @@ -0,0 +1,123 @@ +// Copyright © 2023 Tencent Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use event_notifier::{event_notify, NotifyEvent}; +use serde::{Deserialize, Serialize}; +use vm_device::BusDevice; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; + +/// Debug I/O port, see: +/// https://bochs.sourceforge.io/techspec/PORTS.LST +/// https://wiki.osdev.org/I/O_Ports +/// +/// Here we use 0x0680 as system control port address. + +const SYS_START: u8 = 1 << 0; +const SYS_RESTORE: u8 = 1 << 1; +const SYS_PANIC: u8 = 1 << 2; +const SYS_VSOCK_SERVER: u8 = 1 << 3; +const SYS_VALID: u8 = SYS_START | SYS_RESTORE; + +fn sys_start(sys_state: u8) -> bool { + (sys_state & SYS_START) == SYS_START +} + +#[derive(Serialize, Deserialize)] +pub struct SysCtrlState { + state: u8, +} + +pub struct SysCtrl { + id: String, + sys_state: u8, + sys_started: std::sync::Once, +} + +impl SysCtrl { + pub fn new(id: String, state: Option) -> Self { + let mut sys_state = 0; + if let Some(state) = state { + sys_state = state.state; + } + + Self { + id, + sys_state, + sys_started: std::sync::Once::new(), + } + } + + pub fn sys_started(&self) -> bool { + sys_start(self.sys_state) + } + + fn state(&self) -> SysCtrlState { + SysCtrlState { + state: self.sys_state, + } + } +} + +impl BusDevice for SysCtrl { + fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) { + self.sys_started + .call_once(|| info!("read system control state {}", self.sys_state)); + data[0] = self.sys_state & SYS_VALID; + } + + fn write( + &mut self, + _base: u64, + _offset: u64, + data: &[u8], + ) -> Option> { + let code = data[0]; + + if code == self.sys_state { + return None; + } + + debug!( + "write system control from {:x} into {:x}", + self.sys_state, code + ); + if sys_start(code) && !sys_start(self.sys_state) { + self.sys_state |= SYS_START; + event_notify!(NotifyEvent::SysStart); + } + + if (code & SYS_PANIC) == SYS_PANIC { + warn!("Guest paniced and coredump"); + } + if (code & SYS_VSOCK_SERVER) == SYS_VSOCK_SERVER { + info!("vsock server ready"); + event_notify!(NotifyEvent::VsockServerReady); + } + + None + } +} + +impl Pausable for SysCtrl {} + +impl Snapshottable for SysCtrl { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + let snapshot = Snapshot::new_from_state(&self.id, &self.state())?; + + Ok(snapshot) + } + + fn restore(&mut self, _: Snapshot) -> std::result::Result<(), MigratableError> { + self.sys_state |= SYS_RESTORE; + Ok(()) + } +} + +impl Transportable for SysCtrl {} +impl Migratable for SysCtrl {} diff --git a/vendor/cube-hypervisor/devices/src/legacy/uart_pl011.rs b/vendor/cube-hypervisor/devices/src/legacy/uart_pl011.rs new file mode 100644 index 0000000..06102fb --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/legacy/uart_pl011.rs @@ -0,0 +1,534 @@ +// Copyright 2021 Arm Limited (or its affiliates). All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! ARM PrimeCell UART(PL011) +//! +//! This module implements an ARM PrimeCell UART(PL011). +//! + +use crate::{read_le_u32, write_le_u32}; +use serde::{Deserialize, Serialize}; +use std::collections::VecDeque; +use std::fmt; +use std::sync::{Arc, Barrier}; +use std::time::Instant; +use std::{io, result}; +use vm_device::interrupt::InterruptSourceGroup; +use vm_device::BusDevice; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; + +/* Registers */ +const UARTDR: u64 = 0; +const UARTRSR_UARTECR: u64 = 1; +const UARTFR: u64 = 6; +const UARTILPR: u64 = 8; +const UARTIBRD: u64 = 9; +const UARTFBRD: u64 = 10; +const UARTLCR_H: u64 = 11; +const UARTCR: u64 = 12; +const UARTIFLS: u64 = 13; +const UARTIMSC: u64 = 14; +const UARTRIS: u64 = 15; +const UARTMIS: u64 = 16; +const UARTICR: u64 = 17; +const UARTDMACR: u64 = 18; +const UARTDEBUG: u64 = 0x3c0; + +const PL011_INT_TX: u32 = 0x20; +const PL011_INT_RX: u32 = 0x10; + +const PL011_FLAG_RXFF: u32 = 0x40; +const PL011_FLAG_RXFE: u32 = 0x10; + +const PL011_ID: [u8; 8] = [0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1]; +// We are only interested in the margins. +const AMBA_ID_LOW: u64 = 0x3f8; +const AMBA_ID_HIGH: u64 = 0x401; + +#[derive(Debug)] +pub enum Error { + BadWriteOffset(u64), + DmaNotImplemented, + InterruptFailure(io::Error), + WriteAllFailure(io::Error), + FlushFailure(io::Error), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Error::BadWriteOffset(offset) => write!(f, "pl011_write: Bad Write Offset: {}", offset), + Error::DmaNotImplemented => write!(f, "pl011: DMA not implemented."), + Error::InterruptFailure(e) => write!(f, "Failed to trigger interrupt: {}", e), + Error::WriteAllFailure(e) => write!(f, "Failed to write: {}", e), + Error::FlushFailure(e) => write!(f, "Failed to flush: {}", e), + } + } +} + +type Result = result::Result; + +/// A PL011 device following the PL011 specification. +pub struct Pl011 { + id: String, + flags: u32, + lcr: u32, + rsr: u32, + cr: u32, + dmacr: u32, + debug: u32, + int_enabled: u32, + int_level: u32, + read_fifo: VecDeque, + ilpr: u32, + ibrd: u32, + fbrd: u32, + ifl: u32, + read_count: u32, + read_trigger: u32, + irq: Arc, + out: Option>, + timestamp: std::time::Instant, +} + +#[derive(Serialize, Deserialize)] +pub struct Pl011State { + flags: u32, + lcr: u32, + rsr: u32, + cr: u32, + dmacr: u32, + debug: u32, + int_enabled: u32, + int_level: u32, + read_fifo: Vec, + ilpr: u32, + ibrd: u32, + fbrd: u32, + ifl: u32, + read_count: u32, + read_trigger: u32, +} + +impl Pl011 { + /// Constructs an AMBA PL011 UART device. + pub fn new( + id: String, + irq: Arc, + out: Option>, + timestamp: Instant, + ) -> Self { + Self { + id, + flags: 0x90u32, + lcr: 0u32, + rsr: 0u32, + cr: 0x300u32, + dmacr: 0u32, + debug: 0u32, + int_enabled: 0u32, + int_level: 0u32, + read_fifo: VecDeque::new(), + ilpr: 0u32, + ibrd: 0u32, + fbrd: 0u32, + ifl: 0x12u32, + read_count: 0u32, + read_trigger: 1u32, + irq, + out, + timestamp, + } + } + + pub fn set_out(&mut self, out: Box) { + self.out = Some(out); + } + + fn state(&self) -> Pl011State { + Pl011State { + flags: self.flags, + lcr: self.lcr, + rsr: self.rsr, + cr: self.cr, + dmacr: self.dmacr, + debug: self.debug, + int_enabled: self.int_enabled, + int_level: self.int_level, + read_fifo: self.read_fifo.clone().into(), + ilpr: self.ilpr, + ibrd: self.ibrd, + fbrd: self.fbrd, + ifl: self.ifl, + read_count: self.read_count, + read_trigger: self.read_trigger, + } + } + + fn set_state(&mut self, state: &Pl011State) { + self.flags = state.flags; + self.lcr = state.lcr; + self.rsr = state.rsr; + self.cr = state.cr; + self.dmacr = state.dmacr; + self.debug = state.debug; + self.int_enabled = state.int_enabled; + self.int_level = state.int_level; + self.read_fifo = state.read_fifo.clone().into(); + self.ilpr = state.ilpr; + self.ibrd = state.ibrd; + self.fbrd = state.fbrd; + self.ifl = state.ifl; + self.read_count = state.read_count; + self.read_trigger = state.read_trigger; + } + + /// Queues raw bytes for the guest to read and signals the interrupt + pub fn queue_input_bytes(&mut self, c: &[u8]) -> vmm_sys_util::errno::Result<()> { + self.read_fifo.extend(c); + self.read_count += c.len() as u32; + self.flags &= !PL011_FLAG_RXFE; + + if ((self.lcr & 0x10) == 0) || (self.read_count == 16) { + self.flags |= PL011_FLAG_RXFF; + } + + if self.read_count >= self.read_trigger { + self.int_level |= PL011_INT_RX; + self.trigger_interrupt()?; + } + + Ok(()) + } + + pub fn flush_output(&mut self) -> result::Result<(), io::Error> { + if let Some(out) = self.out.as_mut() { + out.flush()?; + } + Ok(()) + } + + fn pl011_get_baudrate(&self) -> u32 { + if self.fbrd == 0 { + return 0; + } + + let clk = 24_000_000; // We set the APB_PLCK to 24M in device tree + (clk / ((self.ibrd << 6) + self.fbrd)) << 2 + } + + fn pl011_trace_baudrate_change(&self) { + debug!( + "=== New baudrate: {:#?} (clk: {:#?}Hz, ibrd: {:#?}, fbrd: {:#?}) ===", + self.pl011_get_baudrate(), + 24_000_000, // We set the APB_PLCK to 24M in device tree + self.ibrd, + self.fbrd + ); + } + + fn pl011_set_read_trigger(&mut self) { + self.read_trigger = 1; + } + + fn handle_write(&mut self, offset: u64, val: u32) -> Result<()> { + match offset >> 2 { + UARTDR => { + self.int_level |= PL011_INT_TX; + if let Some(out) = self.out.as_mut() { + out.write_all(&[val.to_le_bytes()[0]]) + .map_err(Error::WriteAllFailure)?; + out.flush().map_err(Error::FlushFailure)?; + } + } + UARTRSR_UARTECR => { + self.rsr = 0; + } + UARTFR => { /* Writes to Flag register are ignored.*/ } + UARTILPR => { + self.ilpr = val; + } + UARTIBRD => { + self.ibrd = val; + self.pl011_trace_baudrate_change(); + } + UARTFBRD => { + self.fbrd = val; + self.pl011_trace_baudrate_change(); + } + UARTLCR_H => { + /* Reset the FIFO state on FIFO enable or disable */ + if ((self.lcr ^ val) & 0x10) != 0 { + self.read_count = 0; + } + self.lcr = val; + self.pl011_set_read_trigger(); + } + UARTCR => { + self.cr = val; + } + UARTIFLS => { + self.ifl = val; + self.pl011_set_read_trigger(); + } + UARTIMSC => { + self.int_enabled = val; + self.trigger_interrupt().map_err(Error::InterruptFailure)?; + } + UARTICR => { + self.int_level &= !val; + self.trigger_interrupt().map_err(Error::InterruptFailure)?; + } + UARTDMACR => { + self.dmacr = val; + if (val & 3) != 0 { + return Err(Error::DmaNotImplemented); + } + } + UARTDEBUG => { + self.debug = val; + self.handle_debug(); + } + off => { + debug!("PL011: Bad write offset, offset: {}", off); + return Err(Error::BadWriteOffset(off)); + } + } + Ok(()) + } + + fn handle_debug(&self) { + let elapsed = self.timestamp.elapsed(); + + match self.debug { + 0x00..=0x1f => warn!( + "[Debug I/O port: Firmware code: 0x{:x}] {}.{:>06} seconds", + self.debug, + elapsed.as_secs(), + elapsed.as_micros() + ), + 0x20..=0x3f => warn!( + "[Debug I/O port: Bootloader code: 0x{:x}] {}.{:>06} seconds", + self.debug, + elapsed.as_secs(), + elapsed.as_micros() + ), + 0x40..=0x5f => warn!( + "[Debug I/O port: Kernel code: 0x{:x}] {}.{:>06} seconds", + self.debug, + elapsed.as_secs(), + elapsed.as_micros() + ), + 0x60..=0x7f => warn!( + "[Debug I/O port: Userspace code: 0x{:x}] {}.{:>06} seconds", + self.debug, + elapsed.as_secs(), + elapsed.as_micros() + ), + _ => {} + } + } + + fn trigger_interrupt(&mut self) -> result::Result<(), io::Error> { + self.irq.trigger(0) + } +} + +impl BusDevice for Pl011 { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + let mut read_ok = true; + let v = if (AMBA_ID_LOW..AMBA_ID_HIGH).contains(&(offset >> 2)) { + let index = ((offset - 0xfe0) >> 2) as usize; + u32::from(PL011_ID[index]) + } else { + match offset >> 2 { + UARTDR => { + self.flags &= !PL011_FLAG_RXFF; + let c: u32 = self.read_fifo.pop_front().unwrap_or_default().into(); + if self.read_count > 0 { + self.read_count -= 1; + } + if self.read_count == 0 { + self.flags |= PL011_FLAG_RXFE; + } + if self.read_count == (self.read_trigger - 1) { + self.int_level &= !PL011_INT_RX; + } + self.rsr = c >> 8; + c + } + UARTRSR_UARTECR => self.rsr, + UARTFR => self.flags, + UARTILPR => self.ilpr, + UARTIBRD => self.ibrd, + UARTFBRD => self.fbrd, + UARTLCR_H => self.lcr, + UARTCR => self.cr, + UARTIFLS => self.ifl, + UARTIMSC => self.int_enabled, + UARTRIS => self.int_level, + UARTMIS => self.int_level & self.int_enabled, + UARTDMACR => self.dmacr, + UARTDEBUG => self.debug, + _ => { + read_ok = false; + 0 + } + } + }; + + if read_ok && data.len() <= 4 { + write_le_u32(data, v); + } else { + warn!( + "Invalid PL011 read: offset {}, data length {}", + offset, + data.len() + ); + } + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + if data.len() <= 4 { + let v = read_le_u32(data); + if let Err(e) = self.handle_write(offset, v) { + warn!("Failed to write to PL011 device: {}", e); + } + } else { + warn!( + "Invalid PL011 write: offset {}, data length {}", + offset, + data.len() + ); + } + + None + } +} + +impl Snapshottable for Pl011 { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + self.set_state(&snapshot.to_state(&self.id)?); + Ok(()) + } +} + +impl Pausable for Pl011 {} +impl Transportable for Pl011 {} +impl Migratable for Pl011 {} + +#[cfg(test)] +mod tests { + use super::*; + use std::io; + use std::sync::{Arc, Mutex}; + use vm_device::interrupt::{InterruptIndex, InterruptSourceConfig}; + use vmm_sys_util::eventfd::EventFd; + + const SERIAL_NAME: &str = "serial"; + + struct TestInterrupt { + event_fd: EventFd, + } + + impl InterruptSourceGroup for TestInterrupt { + fn trigger(&self, _index: InterruptIndex) -> result::Result<(), std::io::Error> { + self.event_fd.write(1) + } + fn update( + &self, + _index: InterruptIndex, + _config: InterruptSourceConfig, + _masked: bool, + ) -> result::Result<(), std::io::Error> { + Ok(()) + } + fn notifier(&self, _index: InterruptIndex) -> Option { + Some(self.event_fd.try_clone().unwrap()) + } + } + + impl TestInterrupt { + fn new(event_fd: EventFd) -> Self { + TestInterrupt { event_fd } + } + } + + #[derive(Clone)] + struct SharedBuffer { + buf: Arc>>, + } + + impl SharedBuffer { + fn new() -> SharedBuffer { + SharedBuffer { + buf: Arc::new(Mutex::new(Vec::new())), + } + } + } + + impl io::Write for SharedBuffer { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.buf.lock().unwrap().write(buf) + } + fn flush(&mut self) -> io::Result<()> { + self.buf.lock().unwrap().flush() + } + } + + #[test] + fn pl011_output() { + let intr_evt = EventFd::new(0).unwrap(); + let pl011_out = SharedBuffer::new(); + let mut pl011 = Pl011::new( + String::from(SERIAL_NAME), + Arc::new(TestInterrupt::new(intr_evt.try_clone().unwrap())), + Some(Box::new(pl011_out.clone())), + Instant::now(), + ); + + pl011.write(0, UARTDR as u64, &[b'x', b'y']); + pl011.write(0, UARTDR as u64, &[b'a']); + pl011.write(0, UARTDR as u64, &[b'b']); + pl011.write(0, UARTDR as u64, &[b'c']); + assert_eq!( + pl011_out.buf.lock().unwrap().as_slice(), + &[b'x', b'a', b'b', b'c'] + ); + } + + #[test] + fn pl011_input() { + let intr_evt = EventFd::new(0).unwrap(); + let pl011_out = SharedBuffer::new(); + let mut pl011 = Pl011::new( + String::from(SERIAL_NAME), + Arc::new(TestInterrupt::new(intr_evt.try_clone().unwrap())), + Some(Box::new(pl011_out)), + Instant::now(), + ); + + // write 1 to the interrupt event fd, so that read doesn't block in case the event fd + // counter doesn't change (for 0 it blocks) + assert!(intr_evt.write(1).is_ok()); + pl011.queue_input_bytes(&[b'a', b'b', b'c']).unwrap(); + + assert_eq!(intr_evt.read().unwrap(), 2); + + let mut data = [0u8]; + pl011.read(0, UARTDR as u64, &mut data); + assert_eq!(data[0], b'a'); + pl011.read(0, UARTDR as u64, &mut data); + assert_eq!(data[0], b'b'); + pl011.read(0, UARTDR as u64, &mut data); + assert_eq!(data[0], b'c'); + } +} diff --git a/vendor/cube-hypervisor/devices/src/lib.rs b/vendor/cube-hypervisor/devices/src/lib.rs new file mode 100644 index 0000000..7d6b640 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/lib.rs @@ -0,0 +1,98 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +//! Emulates virtual and hardware devices. + +#[macro_use] +extern crate bitflags; +#[macro_use] +extern crate log; + +pub mod acpi; +#[cfg(target_arch = "aarch64")] +pub mod gic; +pub mod interrupt_controller; +#[cfg(target_arch = "x86_64")] +pub mod ioapic; +pub mod ivshmem; +pub mod legacy; +pub mod pvpanic; +pub mod tpm; + +pub use self::acpi::{AcpiGedDevice, AcpiPmTimerDevice, AcpiShutdownDevice}; +pub use self::ivshmem::IvshmemDevice; +pub use self::pvpanic::{PvPanicDevice, PVPANIC_DEVICE_MMIO_SIZE}; + +bitflags! { + pub struct AcpiNotificationFlags: u8 { + const NO_DEVICES_CHANGED = 0; + const CPU_DEVICES_CHANGED = 0b1; + const MEMORY_DEVICES_CHANGED = 0b10; + const PCI_DEVICES_CHANGED = 0b100; + const POWER_BUTTON_CHANGED = 0b1000; + } +} + +#[allow(unused_macros)] +#[cfg(target_arch = "aarch64")] +macro_rules! generate_read_fn { + ($fn_name: ident, $data_type: ty, $byte_type: ty, $type_size: expr, $endian_type: ident) => { + #[allow(dead_code)] + pub fn $fn_name(input: &[$byte_type]) -> $data_type { + assert!($type_size == std::mem::size_of::<$data_type>()); + let mut array = [0u8; $type_size]; + for (byte, read) in array.iter_mut().zip(input.iter().cloned()) { + *byte = read as u8; + } + <$data_type>::$endian_type(array) + } + }; +} + +#[allow(unused_macros)] +#[cfg(target_arch = "aarch64")] +macro_rules! generate_write_fn { + ($fn_name: ident, $data_type: ty, $byte_type: ty, $endian_type: ident) => { + #[allow(dead_code)] + pub fn $fn_name(buf: &mut [$byte_type], n: $data_type) { + for (byte, read) in buf + .iter_mut() + .zip(<$data_type>::$endian_type(n).iter().cloned()) + { + *byte = read as $byte_type; + } + } + }; +} + +#[cfg(target_arch = "aarch64")] +generate_read_fn!(read_le_u16, u16, u8, 2, from_le_bytes); +#[cfg(target_arch = "aarch64")] +generate_read_fn!(read_le_u32, u32, u8, 4, from_le_bytes); +#[cfg(target_arch = "aarch64")] +generate_read_fn!(read_le_u64, u64, u8, 8, from_le_bytes); +#[cfg(target_arch = "aarch64")] +generate_read_fn!(read_le_i32, i32, i8, 4, from_le_bytes); + +#[cfg(target_arch = "aarch64")] +generate_read_fn!(read_be_u16, u16, u8, 2, from_be_bytes); +#[cfg(target_arch = "aarch64")] +generate_read_fn!(read_be_u32, u32, u8, 4, from_be_bytes); + +#[cfg(target_arch = "aarch64")] +generate_write_fn!(write_le_u16, u16, u8, to_le_bytes); +#[cfg(target_arch = "aarch64")] +generate_write_fn!(write_le_u32, u32, u8, to_le_bytes); +#[cfg(target_arch = "aarch64")] +generate_write_fn!(write_le_u64, u64, u8, to_le_bytes); +#[cfg(target_arch = "aarch64")] +generate_write_fn!(write_le_i32, i32, i8, to_le_bytes); + +#[cfg(target_arch = "aarch64")] +generate_write_fn!(write_be_u16, u16, u8, to_be_bytes); +#[cfg(target_arch = "aarch64")] +generate_write_fn!(write_be_u32, u32, u8, to_be_bytes); diff --git a/vendor/cube-hypervisor/devices/src/pvpanic.rs b/vendor/cube-hypervisor/devices/src/pvpanic.rs new file mode 100644 index 0000000..39bf61b --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/pvpanic.rs @@ -0,0 +1,255 @@ +// Copyright © 2023 Tencent Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use event_monitor::event; +use pci::{ + BarReprogrammingParams, PciBarConfiguration, PciBarPrefetchable, PciBarRegionType, + PciClassCode, PciConfiguration, PciDevice, PciDeviceError, PciHeaderType, PciSubclass, +}; +use serde::{Deserialize, Serialize}; +use std::any::Any; +use std::result; +use std::sync::{Arc, Barrier, Mutex}; +use thiserror::Error; +use vm_allocator::{AddressAllocator, SystemAllocator}; +use vm_device::{BusDevice, Resource}; +use vm_memory::{Address, GuestAddress}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; + +const PVPANIC_VENDOR_ID: u16 = 0x1b36; +const PVPANIC_DEVICE_ID: u16 = 0x0011; + +pub const PVPANIC_DEVICE_MMIO_SIZE: u64 = 0x2; + +const PVPANIC_PANICKED: u8 = 1 << 0; +const PVPANIC_CRASH_LOADED: u8 = 1 << 1; + +#[derive(Debug, Error)] +pub enum PvPanicError { + #[error("Failed creating PvPanicDevice: {0}")] + CreatePvPanicDevice(#[source] anyhow::Error), + #[error("Failed to retrieve PciConfigurationState: {0}")] + RetrievePciConfigurationState(#[source] anyhow::Error), +} + +#[allow(dead_code)] +#[derive(Copy, Clone)] +pub enum PvPanicSubclass { + Other = 0x80, +} + +impl PciSubclass for PvPanicSubclass { + fn get_register_value(&self) -> u8 { + *self as u8 + } +} + +/// A device for handling guest panic event +pub struct PvPanicDevice { + id: String, + events: u8, + + // PCI configuration registers. + configuration: PciConfiguration, + bar_regions: Vec, +} + +#[derive(Serialize, Deserialize)] +pub struct PvPanicDeviceState { + events: u8, +} + +impl PvPanicDevice { + pub fn new(id: String, state: Option) -> Result { + let mut configuration = PciConfiguration::new( + PVPANIC_VENDOR_ID, + PVPANIC_DEVICE_ID, + 0x1, // modern pci devices + PciClassCode::BaseSystemPeripheral, + &PvPanicSubclass::Other, + None, + PciHeaderType::Device, + 0, + 0, + None, + ); + + let command: [u8; 2] = [0x03, 0x01]; + configuration.write_config_register(1, 0, &command); + + let events = if let Some(state) = state { + state.events + } else { + PVPANIC_PANICKED | PVPANIC_CRASH_LOADED + }; + + let pvpanic_device = PvPanicDevice { + id, + events, + configuration, + bar_regions: vec![], + }; + + Ok(pvpanic_device) + } + + pub fn event_to_string(&self, event: u8) -> String { + if event == PVPANIC_PANICKED { + "panic".to_string() + } else if event == PVPANIC_CRASH_LOADED { + "crash_loaded".to_string() + } else { + "unknown_event".to_string() + } + } + + fn state(&self) -> PvPanicDeviceState { + PvPanicDeviceState { + events: self.events, + } + } + + pub fn config_bar_addr(&self) -> u64 { + self.configuration.get_bar_addr(0) + } +} + +impl BusDevice for PvPanicDevice { + fn read(&mut self, base: u64, offset: u64, data: &mut [u8]) { + self.read_bar(base, offset, data) + } + + fn write(&mut self, _base: u64, _offset: u64, data: &[u8]) -> Option> { + let event = self.event_to_string(data[0]); + warn!("pvpanic got guest event {}", event); + event!("guest", "panic", "event", &event); + None + } +} + +impl PciDevice for PvPanicDevice { + fn write_config_register( + &mut self, + reg_idx: usize, + offset: u64, + data: &[u8], + ) -> Option> { + self.configuration + .write_config_register(reg_idx, offset, data); + None + } + + fn read_config_register(&mut self, reg_idx: usize) -> u32 { + self.configuration.read_reg(reg_idx) + } + + fn detect_bar_reprogramming( + &mut self, + reg_idx: usize, + data: &[u8], + ) -> Option { + self.configuration.detect_bar_reprogramming(reg_idx, data) + } + + fn allocate_bars( + &mut self, + allocator: &Arc>, + _mmio_allocator: &mut AddressAllocator, + resources: Option>, + ) -> std::result::Result, PciDeviceError> { + let mut bars = Vec::new(); + let region_type = PciBarRegionType::Memory32BitRegion; + let bar_id = 0; + let region_size = PVPANIC_DEVICE_MMIO_SIZE; + let restoring = resources.is_some(); + let bar_addr = allocator + .lock() + .unwrap() + .allocate_mmio_hole_addresses(None, region_size, None) + .ok_or(PciDeviceError::IoAllocationFailed(region_size))?; + + let bar = PciBarConfiguration::default() + .set_index(bar_id as usize) + .set_address(bar_addr.raw_value()) + .set_size(region_size) + .set_region_type(region_type) + .set_prefetchable(PciBarPrefetchable::NotPrefetchable); + + debug!("pvpanic bar address 0x{:x}", bar_addr.0); + if !restoring { + self.configuration + .add_pci_bar(&bar) + .map_err(|e| PciDeviceError::IoRegistrationFailed(bar_addr.raw_value(), e))?; + } + + bars.push(bar); + self.bar_regions = bars.clone(); + + Ok(bars) + } + + fn free_bars( + &mut self, + allocator: &mut SystemAllocator, + _mmio_allocator: &mut AddressAllocator, + ) -> std::result::Result<(), PciDeviceError> { + for bar in self.bar_regions.drain(..) { + allocator.free_mmio_hole_addresses(GuestAddress(bar.addr()), bar.size()); + } + + Ok(()) + } + + fn move_bar(&mut self, old_base: u64, new_base: u64) -> result::Result<(), std::io::Error> { + for bar in self.bar_regions.iter_mut() { + if bar.addr() == old_base { + *bar = bar.set_address(new_base); + } + } + + Ok(()) + } + + fn read_bar(&mut self, _base: u64, _offset: u64, data: &mut [u8]) { + data[0] = self.events; + } + + fn as_any(&mut self) -> &mut dyn Any { + self + } + + fn id(&self) -> Option { + Some(self.id.clone()) + } +} + +impl Pausable for PvPanicDevice {} + +impl Snapshottable for PvPanicDevice { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + let mut snapshot = Snapshot::new_from_state(&self.id, &self.state())?; + + // Snapshot PciConfiguration + snapshot.add_snapshot(self.configuration.snapshot()?); + + Ok(snapshot) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + // Restore PciConfiguration + if let Some(pci_config_snapshot) = snapshot.snapshots.get(&self.configuration.id()) { + self.configuration.restore(*pci_config_snapshot.clone())?; + } + + Ok(()) + } +} + +impl Transportable for PvPanicDevice {} +impl Migratable for PvPanicDevice {} diff --git a/vendor/cube-hypervisor/devices/src/tpm.rs b/vendor/cube-hypervisor/devices/src/tpm.rs new file mode 100644 index 0000000..e387003 --- /dev/null +++ b/vendor/cube-hypervisor/devices/src/tpm.rs @@ -0,0 +1,470 @@ +// Copyright © 2022, Microsoft Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use anyhow::anyhow; +#[cfg(target_arch = "aarch64")] +use arch::aarch64::layout::{TPM_SIZE, TPM_START}; +#[cfg(target_arch = "x86_64")] +use arch::x86_64::layout::{TPM_SIZE, TPM_START}; +use phf::phf_map; +use std::cmp; +use std::sync::{Arc, Barrier}; +use thiserror::Error; +use tpm::emulator::{BackendCmd, Emulator}; +use tpm::TPM_CRB_BUFFER_MAX; +use tpm::TPM_SUCCESS; +use vm_device::BusDevice; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Emulator doesn't implement min required capabilities: {0}")] + CheckCaps(#[source] anyhow::Error), + #[error("Failed to initialize tpm: {0}")] + Init(#[source] anyhow::Error), + #[error("Failed to deliver tpm Command: {0}")] + DeliverRequest(#[source] anyhow::Error), +} +type Result = anyhow::Result; + +/* crb 32-bit registers */ +const CRB_LOC_STATE: u32 = 0x0; +//Register Fields +// Field => (start, length) +// start: lowest bit in the bit field numbered from 0 +// length: length of the bit field +const CRB_LOC_STATE_FIELDS: phf::Map<&str, [u32; 2]> = phf_map! { + "tpmEstablished" => [0, 1], + "locAssigned" => [1,1], + "activeLocality"=> [2, 3], + "reserved" => [5, 2], + "tpmRegValidSts" => [7, 1] +}; +const CRB_LOC_CTRL: u32 = 0x08; +const CRB_LOC_CTRL_REQUEST_ACCESS: u32 = 1 << 0; +const CRB_LOC_CTRL_RELINQUISH: u32 = 1 << 1; +const CRB_LOC_CTRL_RESET_ESTABLISHMENT_BIT: u32 = 1 << 3; +const CRB_LOC_STS: u32 = 0x0C; +const CRB_LOC_STS_FIELDS: phf::Map<&str, [u32; 2]> = phf_map! { + "Granted" => [0, 1], + "beenSeized" => [1,1] +}; +const CRB_INTF_ID: u32 = 0x30; +const CRB_INTF_ID_FIELDS: phf::Map<&str, [u32; 2]> = phf_map! { + "InterfaceType" => [0, 4], + "InterfaceVersion" => [4, 4], + "CapLocality" => [8, 1], + "CapCRBIdleBypass" => [9, 1], + "Reserved1" => [10, 1], + "CapDataXferSizeSupport" => [11, 2], + "CapFIFO" => [13, 1], + "CapCRB" => [14, 1], + "CapIFRes" => [15, 2], + "InterfaceSelector" => [17, 2], + "IntfSelLock" => [19, 1], + "Reserved2" => [20, 4], + "RID" => [24, 8] +}; +const CRB_INTF_ID2: u32 = 0x34; +const CRB_INTF_ID2_FIELDS: phf::Map<&str, [u32; 2]> = phf_map! { + "VID" => [0, 16], + "DID" => [16, 16] +}; +const CRB_CTRL_REQ: u32 = 0x40; +const CRB_CTRL_REQ_CMD_READY: u32 = 1 << 0; +const CRB_CTRL_REQ_GO_IDLE: u32 = 1 << 1; +const CRB_CTRL_STS: u32 = 0x44; +const CRB_CTRL_STS_FIELDS: phf::Map<&str, [u32; 2]> = phf_map! { + "tpmSts" => [0, 1], + "tpmIdle" => [1, 1] +}; +const CRB_CTRL_CANCEL: u32 = 0x48; +const CRB_CANCEL_INVOKE: u32 = 1 << 0; +const CRB_CTRL_START: u32 = 0x4C; +const CRB_START_INVOKE: u32 = 1 << 0; +const CRB_CTRL_CMD_LADDR: u32 = 0x5C; +const CRB_CTRL_CMD_HADDR: u32 = 0x60; +const CRB_CTRL_RSP_SIZE: u32 = 0x64; +const CRB_CTRL_RSP_ADDR: u32 = 0x68; +const CRB_DATA_BUFFER: u32 = 0x80; + +const TPM_CRB_NO_LOCALITY: u32 = 0xff; + +const TPM_CRB_ADDR_BASE: u32 = TPM_START.0 as u32; +const TPM_CRB_ADDR_SIZE: usize = TPM_SIZE as usize; + +const TPM_CRB_R_MAX: u32 = CRB_DATA_BUFFER; + +// CRB Protocol details +const CRB_INTF_TYPE_CRB_ACTIVE: u32 = 0b1; +const CRB_INTF_VERSION_CRB: u32 = 0b1; +const CRB_INTF_CAP_LOCALITY_0_ONLY: u32 = 0b0; +const CRB_INTF_CAP_IDLE_FAST: u32 = 0b0; +const CRB_INTF_CAP_XFER_SIZE_64: u32 = 0b11; +const CRB_INTF_CAP_FIFO_NOT_SUPPORTED: u32 = 0b0; +const CRB_INTF_CAP_CRB_SUPPORTED: u32 = 0b1; +const CRB_INTF_IF_SELECTOR_CRB: u32 = 0b1; +const PCI_VENDOR_ID_IBM: u32 = 0x1014; +const CRB_CTRL_CMD_SIZE_REG: u32 = 0x58; +const CRB_CTRL_CMD_SIZE: usize = TPM_CRB_ADDR_SIZE - CRB_DATA_BUFFER as usize; + +fn get_fields_map(reg: u32) -> phf::Map<&'static str, [u32; 2]> { + match reg { + CRB_LOC_STATE => CRB_LOC_STATE_FIELDS, + CRB_LOC_STS => CRB_LOC_STS_FIELDS, + CRB_INTF_ID => CRB_INTF_ID_FIELDS, + CRB_INTF_ID2 => CRB_INTF_ID2_FIELDS, + CRB_CTRL_STS => CRB_CTRL_STS_FIELDS, + _ => { + panic!( + "Fields in '{:?}' register were accessed which are Invalid", + reg + ); + } + } +} + +/// Set a particular field in a Register +fn set_reg_field(regs: &mut [u32; TPM_CRB_R_MAX as usize], reg: u32, field: &str, value: u32) { + let reg_fields = get_fields_map(reg); + if reg_fields.contains_key(field) { + let start = reg_fields.get(field).unwrap()[0]; + let len = reg_fields.get(field).unwrap()[1]; + let mask = (!(0_u32) >> (32 - len)) << start; + regs[reg as usize] = (regs[reg as usize] & !mask) | ((value << start) & mask); + } else { + error!( + "Failed to tpm Register. {:?} is not a valid field in Reg {:#X}", + field, reg + ) + } +} + +/// Get the value of a particular field in a Register +fn get_reg_field(regs: &[u32; TPM_CRB_R_MAX as usize], reg: u32, field: &str) -> u32 { + let reg_fields = get_fields_map(reg); + if reg_fields.contains_key(field) { + let start = reg_fields.get(field).unwrap()[0]; + let len = reg_fields.get(field).unwrap()[1]; + let mask = (!(0_u32) >> (32 - len)) << start; + (regs[reg as usize] & mask) >> start + } else { + // TODO: Sensible return value if fields do not exist + 0x0 + } +} + +fn locality_from_addr(addr: u32) -> u8 { + (addr >> 12) as u8 +} + +pub struct Tpm { + emulator: Emulator, + cmd: Option, + regs: [u32; TPM_CRB_R_MAX as usize], + backend_buff_size: usize, + data_buff: [u8; TPM_CRB_BUFFER_MAX], + data_buff_len: usize, +} + +impl Tpm { + pub fn new(path: String) -> Result { + let emulator = Emulator::new(path) + .map_err(|e| Error::Init(anyhow!("Failed while initializing tpm Emulator: {:?}", e)))?; + let mut tpm = Tpm { + emulator, + cmd: None, + regs: [0; TPM_CRB_R_MAX as usize], + backend_buff_size: TPM_CRB_BUFFER_MAX, + data_buff: [0; TPM_CRB_BUFFER_MAX], + data_buff_len: 0, + }; + tpm.reset()?; + Ok(tpm) + } + + fn get_active_locality(&mut self) -> u32 { + if get_reg_field(&self.regs, CRB_LOC_STATE, "locAssigned") == 0 { + return TPM_CRB_NO_LOCALITY; + } + get_reg_field(&self.regs, CRB_LOC_STATE, "activeLocality") + } + + fn request_completed(&mut self, result: isize) { + self.regs[CRB_CTRL_START as usize] = !CRB_START_INVOKE; + if result != 0 { + set_reg_field(&mut self.regs, CRB_CTRL_STS, "tpmSts", 1); + } + } + + fn reset(&mut self) -> Result<()> { + let cur_buff_size = self.emulator.get_buffer_size().unwrap(); + self.regs = [0; TPM_CRB_R_MAX as usize]; + set_reg_field(&mut self.regs, CRB_LOC_STATE, "tpmRegValidSts", 1); + set_reg_field(&mut self.regs, CRB_CTRL_STS, "tpmIdle", 1); + set_reg_field( + &mut self.regs, + CRB_INTF_ID, + "InterfaceType", + CRB_INTF_TYPE_CRB_ACTIVE, + ); + set_reg_field( + &mut self.regs, + CRB_INTF_ID, + "InterfaceVersion", + CRB_INTF_VERSION_CRB, + ); + set_reg_field( + &mut self.regs, + CRB_INTF_ID, + "CapLocality", + CRB_INTF_CAP_LOCALITY_0_ONLY, + ); + set_reg_field( + &mut self.regs, + CRB_INTF_ID, + "CapCRBIdleBypass", + CRB_INTF_CAP_IDLE_FAST, + ); + set_reg_field( + &mut self.regs, + CRB_INTF_ID, + "CapDataXferSizeSupport", + CRB_INTF_CAP_XFER_SIZE_64, + ); + set_reg_field( + &mut self.regs, + CRB_INTF_ID, + "CapFIFO", + CRB_INTF_CAP_FIFO_NOT_SUPPORTED, + ); + set_reg_field( + &mut self.regs, + CRB_INTF_ID, + "CapCRB", + CRB_INTF_CAP_CRB_SUPPORTED, + ); + set_reg_field( + &mut self.regs, + CRB_INTF_ID, + "InterfaceSelector", + CRB_INTF_IF_SELECTOR_CRB, + ); + set_reg_field(&mut self.regs, CRB_INTF_ID, "RID", 0b0000); + set_reg_field(&mut self.regs, CRB_INTF_ID2, "VID", PCI_VENDOR_ID_IBM); + + self.regs[CRB_CTRL_CMD_SIZE_REG as usize] = CRB_CTRL_CMD_SIZE as u32; + self.regs[CRB_CTRL_CMD_LADDR as usize] = TPM_CRB_ADDR_BASE + CRB_DATA_BUFFER; + self.regs[CRB_CTRL_RSP_SIZE as usize] = CRB_CTRL_CMD_SIZE as u32; + self.regs[CRB_CTRL_RSP_ADDR as usize] = TPM_CRB_ADDR_BASE + CRB_DATA_BUFFER; + + self.backend_buff_size = cmp::min(cur_buff_size, TPM_CRB_BUFFER_MAX); + + if let Err(e) = self.emulator.startup_tpm(self.backend_buff_size) { + return Err(Error::Init(anyhow!( + "Failed while running Startup TPM. Error: {:?}", + e + ))); + } + Ok(()) + } +} + +//impl BusDevice for TPM +impl BusDevice for Tpm { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + let mut offset: u32 = offset as u32; + let read_len: usize = data.len(); + + if offset >= CRB_DATA_BUFFER + && (offset + read_len as u32) < (CRB_DATA_BUFFER + self.data_buff.len() as u32) + { + // Read from Data Buffer + let start: usize = (offset as usize) - (CRB_DATA_BUFFER as usize); + let end: usize = start + read_len; + data[..].clone_from_slice(&self.data_buff[start..end]); + } else { + offset &= 0xff; + let mut val = self.regs[offset as usize]; + + if offset == CRB_LOC_STATE && !self.emulator.get_established_flag() { + val |= 0x1; + } + + if data.len() <= 4 { + data.clone_from_slice(val.to_ne_bytes()[0..read_len].as_ref()); + } else { + error!( + "Invalid tpm read: offset {:#X}, data length {:?}", + offset, + data.len() + ); + } + } + debug!( + "MMIO Read: offset {:#X} len {:?} val = {:02X?} ", + offset, + data.len(), + data + ); + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + debug!( + "MMIO Write: offset {:#X} len {:?} input data {:02X?}", + offset, + data.len(), + data + ); + let mut offset: u32 = offset as u32; + if offset < CRB_DATA_BUFFER { + offset &= 0xff; + } + let locality = locality_from_addr(offset) as u32; + let write_len = data.len(); + + if offset >= CRB_DATA_BUFFER + && (offset + write_len as u32) < (CRB_DATA_BUFFER + self.data_buff.len() as u32) + { + let start: usize = (offset as usize) - (CRB_DATA_BUFFER as usize); + if start == 0 { + // If filling data_buff at index 0, reset length to 0 + self.data_buff_len = 0; + self.data_buff.fill(0); + } + let end: usize = start + data.len(); + self.data_buff[start..end].clone_from_slice(data); + self.data_buff_len += data.len(); + } else { + // Ctrl Commands that take more than 4 bytes as input are not yet supported + // CTRL_RSP_ADDR usually gets 8 byte write request. Last 4 bytes are zeros. + if write_len > 4 && offset != CRB_CTRL_RSP_ADDR { + error!( + "Invalid tpm write: offset {:#X}, data length {}", + offset, + data.len() + ); + return None; + } + + let mut input: [u8; 4] = [0; 4]; + input.copy_from_slice(&data[0..4]); + let v = u32::from_le_bytes(input); + + match offset { + CRB_CTRL_CMD_SIZE_REG => { + self.regs[CRB_CTRL_CMD_SIZE_REG as usize] = v; + } + CRB_CTRL_CMD_LADDR => { + self.regs[CRB_CTRL_CMD_LADDR as usize] = v; + } + CRB_CTRL_CMD_HADDR => { + self.regs[CRB_CTRL_CMD_HADDR as usize] = v; + } + CRB_CTRL_RSP_SIZE => { + self.regs[CRB_CTRL_RSP_SIZE as usize] = v; + } + CRB_CTRL_RSP_ADDR => { + self.regs[CRB_CTRL_RSP_ADDR as usize] = v; + } + CRB_CTRL_REQ => match v { + CRB_CTRL_REQ_CMD_READY => { + set_reg_field(&mut self.regs, CRB_CTRL_STS, "tpmIdle", 0); + } + CRB_CTRL_REQ_GO_IDLE => { + set_reg_field(&mut self.regs, CRB_CTRL_STS, "tpmIdle", 1); + } + _ => { + error!("Invalid value passed to CRTL_REQ register"); + return None; + } + }, + CRB_CTRL_CANCEL => { + if v == CRB_CANCEL_INVOKE + && (self.regs[CRB_CTRL_START as usize] & CRB_START_INVOKE != 0) + { + if let Err(e) = self.emulator.cancel_cmd() { + error!("Failed to run cancel command. Error: {:?}", e); + } + } + } + CRB_CTRL_START => { + if v == CRB_START_INVOKE + && ((self.regs[CRB_CTRL_START as usize] & CRB_START_INVOKE) == 0) + && self.get_active_locality() == locality + { + self.regs[CRB_CTRL_START as usize] |= CRB_START_INVOKE; + + self.cmd = Some(BackendCmd { + locality: locality as u8, + input: self.data_buff[0..self.data_buff_len].to_vec(), + input_len: cmp::min(self.data_buff_len, TPM_CRB_BUFFER_MAX), + output: self.data_buff.to_vec(), + output_len: TPM_CRB_BUFFER_MAX, + selftest_done: false, + }); + + let mut cmd = self.cmd.as_ref().unwrap().clone(); + let output = self.emulator.deliver_request(&mut cmd).map_err(|e| { + Error::DeliverRequest(anyhow!( + "Failed to deliver tpm request. Error :{:?}", + e + )) + }); + //TODO: drop the copy here + self.data_buff.fill(0); + self.data_buff.clone_from_slice(output.unwrap().as_slice()); + + self.request_completed(TPM_SUCCESS as isize); + } + } + CRB_LOC_CTRL => { + warn!( + "CRB_LOC_CTRL locality to write = {:?} val = {:?}", + locality, v + ); + match v { + CRB_LOC_CTRL_RESET_ESTABLISHMENT_BIT => {} + CRB_LOC_CTRL_RELINQUISH => { + set_reg_field(&mut self.regs, CRB_LOC_STATE, "locAssigned", 0); + set_reg_field(&mut self.regs, CRB_LOC_STS, "Granted", 0); + } + CRB_LOC_CTRL_REQUEST_ACCESS => { + set_reg_field(&mut self.regs, CRB_LOC_STS, "Granted", 1); + set_reg_field(&mut self.regs, CRB_LOC_STS, "beenSeized", 0); + set_reg_field(&mut self.regs, CRB_LOC_STATE, "locAssigned", 1); + } + _ => { + error!("Invalid value to write in CRB_LOC_CTRL {:#X} ", v); + } + } + } + _ => { + error!( + "Invalid tpm write: offset {:#X}, data length {:?}", + offset, + data.len() + ); + } + } + } + None + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_set_get_reg_field() { + let mut regs: [u32; TPM_CRB_R_MAX as usize] = [0; TPM_CRB_R_MAX as usize]; + set_reg_field(&mut regs, CRB_INTF_ID, "RID", 0xAC); + assert_eq!( + get_reg_field(®s, CRB_INTF_ID, "RID"), + 0xAC, + concat!("Test: ", stringify!(set_get_reg_field)) + ); + } +} diff --git a/vendor/cube-hypervisor/docs/COPYING.docs b/vendor/cube-hypervisor/docs/COPYING.docs new file mode 100644 index 0000000..4728f5b --- /dev/null +++ b/vendor/cube-hypervisor/docs/COPYING.docs @@ -0,0 +1,399 @@ +The documentation in this directory is covered by the following license: + +Attribution 4.0 International + +======================================================================= + +Creative Commons Corporation ("Creative Commons") is not a law firm and +does not provide legal services or legal advice. Distribution of +Creative Commons public licenses does not create a lawyer-client or +other relationship. Creative Commons makes its licenses and related +information available on an "as-is" basis. Creative Commons gives no +warranties regarding its licenses, any material licensed under their +terms and conditions, or any related information. Creative Commons +disclaims all liability for damages resulting from their use to the +fullest extent possible. + +Using Creative Commons Public Licenses + +Creative Commons public licenses provide a standard set of terms and +conditions that creators and other rights holders may use to share +original works of authorship and other material subject to copyright +and certain other rights specified in the public license below. The +following considerations are for informational purposes only, are not +exhaustive, and do not form part of our licenses. + + Considerations for licensors: Our public licenses are + intended for use by those authorized to give the public + permission to use material in ways otherwise restricted by + copyright and certain other rights. Our licenses are + irrevocable. Licensors should read and understand the terms + and conditions of the license they choose before applying it. + Licensors should also secure all rights necessary before + applying our licenses so that the public can reuse the + material as expected. Licensors should clearly mark any + material not subject to the license. This includes other CC- + licensed material, or material used under an exception or + limitation to copyright. More considerations for licensors: + wiki.creativecommons.org/Considerations_for_licensors + + Considerations for the public: By using one of our public + licenses, a licensor grants the public permission to use the + licensed material under specified terms and conditions. If + the licensor's permission is not necessary for any reason--for + example, because of any applicable exception or limitation to + copyright--then that use is not regulated by the license. Our + licenses grant only permissions under copyright and certain + other rights that a licensor has authority to grant. Use of + the licensed material may still be restricted for other + reasons, including because others have copyright or other + rights in the material. A licensor may make special requests, + such as asking that all changes be marked or described. + Although not required by our licenses, you are encouraged to + respect those requests where reasonable. More considerations + for the public: + wiki.creativecommons.org/Considerations_for_licensees + +======================================================================= + +Creative Commons Attribution 4.0 International Public License + +By exercising the Licensed Rights (defined below), You accept and agree +to be bound by the terms and conditions of this Creative Commons +Attribution 4.0 International Public License ("Public License"). To the +extent this Public License may be interpreted as a contract, You are +granted the Licensed Rights in consideration of Your acceptance of +these terms and conditions, and the Licensor grants You such rights in +consideration of benefits the Licensor receives from making the +Licensed Material available under these terms and conditions. + + +Section 1 -- Definitions. + + a. Adapted Material means material subject to Copyright and Similar + Rights that is derived from or based upon the Licensed Material + and in which the Licensed Material is translated, altered, + arranged, transformed, or otherwise modified in a manner requiring + permission under the Copyright and Similar Rights held by the + Licensor. For purposes of this Public License, where the Licensed + Material is a musical work, performance, or sound recording, + Adapted Material is always produced where the Licensed Material is + synched in timed relation with a moving image. + + b. Adapter's License means the license You apply to Your Copyright + and Similar Rights in Your contributions to Adapted Material in + accordance with the terms and conditions of this Public License. + + c. Copyright and Similar Rights means copyright and/or similar rights + closely related to copyright including, without limitation, + performance, broadcast, sound recording, and Sui Generis Database + Rights, without regard to how the rights are labeled or + categorized. For purposes of this Public License, the rights + specified in Section 2(b)(1)-(2) are not Copyright and Similar + Rights. + + d. Effective Technological Measures means those measures that, in the + absence of proper authority, may not be circumvented under laws + fulfilling obligations under Article 11 of the WIPO Copyright + Treaty adopted on December 20, 1996, and/or similar international + agreements. + + e. Exceptions and Limitations means fair use, fair dealing, and/or + any other exception or limitation to Copyright and Similar Rights + that applies to Your use of the Licensed Material. + + f. Licensed Material means the artistic or literary work, database, + or other material to which the Licensor applied this Public + License. + + g. Licensed Rights means the rights granted to You subject to the + terms and conditions of this Public License, which are limited to + all Copyright and Similar Rights that apply to Your use of the + Licensed Material and that the Licensor has authority to license. + + h. Licensor means the individual(s) or entity(ies) granting rights + under this Public License. + + i. Share means to provide material to the public by any means or + process that requires permission under the Licensed Rights, such + as reproduction, public display, public performance, distribution, + dissemination, communication, or importation, and to make material + available to the public including in ways that members of the + public may access the material from a place and at a time + individually chosen by them. + + j. Sui Generis Database Rights means rights other than copyright + resulting from Directive 96/9/EC of the European Parliament and of + the Council of 11 March 1996 on the legal protection of databases, + as amended and/or succeeded, as well as other essentially + equivalent rights anywhere in the world. + + k. You means the individual or entity exercising the Licensed Rights + under this Public License. Your has a corresponding meaning. + + +Section 2 -- Scope. + + a. License grant. + + 1. Subject to the terms and conditions of this Public License, + the Licensor hereby grants You a worldwide, royalty-free, + non-sublicensable, non-exclusive, irrevocable license to + exercise the Licensed Rights in the Licensed Material to: + + a. reproduce and Share the Licensed Material, in whole or + in part; and + + b. produce, reproduce, and Share Adapted Material. + + 2. Exceptions and Limitations. For the avoidance of doubt, where + Exceptions and Limitations apply to Your use, this Public + License does not apply, and You do not need to comply with + its terms and conditions. + + 3. Term. The term of this Public License is specified in Section + 6(a). + + 4. Media and formats; technical modifications allowed. The + Licensor authorizes You to exercise the Licensed Rights in + all media and formats whether now known or hereafter created, + and to make technical modifications necessary to do so. The + Licensor waives and/or agrees not to assert any right or + authority to forbid You from making technical modifications + necessary to exercise the Licensed Rights, including + technical modifications necessary to circumvent Effective + Technological Measures. For purposes of this Public License, + simply making modifications authorized by this Section 2(a) + (4) never produces Adapted Material. + + 5. Downstream recipients. + + a. Offer from the Licensor -- Licensed Material. Every + recipient of the Licensed Material automatically + receives an offer from the Licensor to exercise the + Licensed Rights under the terms and conditions of this + Public License. + + b. No downstream restrictions. You may not offer or impose + any additional or different terms or conditions on, or + apply any Effective Technological Measures to, the + Licensed Material if doing so restricts exercise of the + Licensed Rights by any recipient of the Licensed + Material. + + 6. No endorsement. Nothing in this Public License constitutes or + may be construed as permission to assert or imply that You + are, or that Your use of the Licensed Material is, connected + with, or sponsored, endorsed, or granted official status by, + the Licensor or others designated to receive attribution as + provided in Section 3(a)(1)(A)(i). + + b. Other rights. + + 1. Moral rights, such as the right of integrity, are not + licensed under this Public License, nor are publicity, + privacy, and/or other similar personality rights; however, to + the extent possible, the Licensor waives and/or agrees not to + assert any such rights held by the Licensor to the limited + extent necessary to allow You to exercise the Licensed + Rights, but not otherwise. + + 2. Patent and trademark rights are not licensed under this + Public License. + + 3. To the extent possible, the Licensor waives any right to + collect royalties from You for the exercise of the Licensed + Rights, whether directly or through a collecting society + under any voluntary or waivable statutory or compulsory + licensing scheme. In all other cases the Licensor expressly + reserves any right to collect such royalties. + + +Section 3 -- License Conditions. + +Your exercise of the Licensed Rights is expressly made subject to the +following conditions. + + a. Attribution. + + 1. If You Share the Licensed Material (including in modified + form), You must: + + a. retain the following if it is supplied by the Licensor + with the Licensed Material: + + i. identification of the creator(s) of the Licensed + Material and any others designated to receive + attribution, in any reasonable manner requested by + the Licensor (including by pseudonym if + designated); + + ii. a copyright notice; + + iii. a notice that refers to this Public License; + + iv. a notice that refers to the disclaimer of + warranties; + + v. a URI or hyperlink to the Licensed Material to the + extent reasonably practicable; + + b. indicate if You modified the Licensed Material and + retain an indication of any previous modifications; and + + c. indicate the Licensed Material is licensed under this + Public License, and include the text of, or the URI or + hyperlink to, this Public License. + + 2. You may satisfy the conditions in Section 3(a)(1) in any + reasonable manner based on the medium, means, and context in + which You Share the Licensed Material. For example, it may be + reasonable to satisfy the conditions by providing a URI or + hyperlink to a resource that includes the required + information. + + 3. If requested by the Licensor, You must remove any of the + information required by Section 3(a)(1)(A) to the extent + reasonably practicable. + + 4. If You Share Adapted Material You produce, the Adapter's + License You apply must not prevent recipients of the Adapted + Material from complying with this Public License. + + +Section 4 -- Sui Generis Database Rights. + +Where the Licensed Rights include Sui Generis Database Rights that +apply to Your use of the Licensed Material: + + a. for the avoidance of doubt, Section 2(a)(1) grants You the right + to extract, reuse, reproduce, and Share all or a substantial + portion of the contents of the database; + + b. if You include all or a substantial portion of the database + contents in a database in which You have Sui Generis Database + Rights, then the database in which You have Sui Generis Database + Rights (but not its individual contents) is Adapted Material; and + + c. You must comply with the conditions in Section 3(a) if You Share + all or a substantial portion of the contents of the database. + +For the avoidance of doubt, this Section 4 supplements and does not +replace Your obligations under this Public License where the Licensed +Rights include other Copyright and Similar Rights. + + +Section 5 -- Disclaimer of Warranties and Limitation of Liability. + + a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE + EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS + AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF + ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, + IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, + WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR + PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, + ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT + KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT + ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. + + b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE + TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, + NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, + INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, + COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR + USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN + ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR + DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR + IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. + + c. The disclaimer of warranties and limitation of liability provided + above shall be interpreted in a manner that, to the extent + possible, most closely approximates an absolute disclaimer and + waiver of all liability. + + +Section 6 -- Term and Termination. + + a. This Public License applies for the term of the Copyright and + Similar Rights licensed here. However, if You fail to comply with + this Public License, then Your rights under this Public License + terminate automatically. + + b. Where Your right to use the Licensed Material has terminated under + Section 6(a), it reinstates: + + 1. automatically as of the date the violation is cured, provided + it is cured within 30 days of Your discovery of the + violation; or + + 2. upon express reinstatement by the Licensor. + + For the avoidance of doubt, this Section 6(b) does not affect any + right the Licensor may have to seek remedies for Your violations + of this Public License. + + c. For the avoidance of doubt, the Licensor may also offer the + Licensed Material under separate terms or conditions or stop + distributing the Licensed Material at any time; however, doing so + will not terminate this Public License. + + d. Sections 1, 5, 6, 7, and 8 survive termination of this Public + License. + + +Section 7 -- Other Terms and Conditions. + + a. The Licensor shall not be bound by any additional or different + terms or conditions communicated by You unless expressly agreed. + + b. Any arrangements, understandings, or agreements regarding the + Licensed Material not stated herein are separate from and + independent of the terms and conditions of this Public License. + + +Section 8 -- Interpretation. + + a. For the avoidance of doubt, this Public License does not, and + shall not be interpreted to, reduce, limit, restrict, or impose + conditions on any use of the Licensed Material that could lawfully + be made without permission under this Public License. + + b. To the extent possible, if any provision of this Public License is + deemed unenforceable, it shall be automatically reformed to the + minimum extent necessary to make it enforceable. If the provision + cannot be reformed, it shall be severed from this Public License + without affecting the enforceability of the remaining terms and + conditions. + + c. No term or condition of this Public License will be waived and no + failure to comply consented to unless expressly agreed to by the + Licensor. + + d. Nothing in this Public License constitutes or may be interpreted + as a limitation upon, or waiver of, any privileges and immunities + that apply to the Licensor or You, including from the legal + processes of any jurisdiction or authority. + + +======================================================================= + +Creative Commons is not a party to its public +licenses. Notwithstanding, Creative Commons may elect to apply one of +its public licenses to material it publishes and in those instances +will be considered the “Licensor.” The text of the Creative Commons +public licenses is dedicated to the public domain under the CC0 Public +Domain Dedication. Except for the limited purpose of indicating that +material is shared under a Creative Commons public license or as +otherwise permitted by the Creative Commons policies published at +creativecommons.org/policies, Creative Commons does not authorize the +use of the trademark "Creative Commons" or any other trademark or logo +of Creative Commons without its prior written consent including, +without limitation, in connection with any unauthorized modifications +to any of its public licenses or any other arrangements, +understandings, or agreements concerning use of licensed material. For +the avoidance of doubt, this paragraph does not form part of the +public licenses. + +Creative Commons may be contacted at creativecommons.org. + + diff --git a/vendor/cube-hypervisor/docs/api.md b/vendor/cube-hypervisor/docs/api.md new file mode 100644 index 0000000..42569a0 --- /dev/null +++ b/vendor/cube-hypervisor/docs/api.md @@ -0,0 +1,383 @@ +- [Cloud Hypervisor API](#cloud-hypervisor-api) + - [External API](#external-api) + - [REST API](#rest-api) + - [Location and availability](#location-and-availability) + - [Endpoints](#endpoints) + - [Virtual Machine Manager (VMM) Actions](#virtual-machine-manager-vmm-actions) + - [Virtual Machine (VM) Actions](#virtual-machine-vm-actions) + - [REST API Examples](#rest-api-examples) + - [Create a Virtual Machine](#create-a-virtual-machine) + - [Boot a Virtual Machine](#boot-a-virtual-machine) + - [Dump a Virtual Machine Information](#dump-a-virtual-machine-information) + - [Reboot a Virtual Machine](#reboot-a-virtual-machine) + - [Shut a Virtual Machine Down](#shut-a-virtual-machine-down) + - [Command Line Interface](#command-line-interface) + - [REST API and CLI Architectural Relationship](#rest-api-and-cli-architectural-relationship) + - [Internal API](#internal-api) + - [Goals and Design](#goals-and-design) + - [End to End Example](#end-to-end-example) + +# Cloud Hypervisor API + +The Cloud Hypervisor API is made of 2 distinct interfaces: + +1. **The external API**. This is the user facing API. Users and operators can + control and manage Cloud Hypervisor through either a REST API or a Command + Line Interface (CLI). +1. **The internal API**, based on [rust's Multi-Producer, Single-Consumer (MPSC)](https://doc.rust-lang.org/std/sync/mpsc/) + module. This API is used internally by the Cloud Hypervisor threads to + communicate between each others. + +The goal of this document is to describe the Cloud Hypervisor API as a whole, +and to outline how the internal and external APIs are architecturally related. + +## External API + +### REST API + +The Cloud Hypervisor [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) +API triggers VM and VMM specific actions, and as such it is designed as a +collection of RPC-style, static methods. + +The API is [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md) +compliant. Please consult the [Cloud Hypervisor API](https://raw.githubusercontent.com/cloud-hypervisor/cloud-hypervisor/master/vmm/src/api/openapi/cloud-hypervisor.yaml) +document for more details about the API payloads and responses. + +### Location and availability + +The REST API is available as soon as the Cloud Hypervisor binary is started, +through a local UNIX socket. +By default, it is located at `/run/user/{user ID}/cloud-hypervisor.{Cloud Hypervisor PID}`. +For example, if you launched Cloud Hypervisor as user ID 1000 and its PID is +123456, the Cloud Hypervisor REST API will be available at `/run/user/1000/cloud-hypervisor.123456`. + +The REST API default URL can be overridden through the Cloud Hypervisor +option `--api-socket`: + +``` +$ ./target/debug/cloud-hypervisor --api-socket /tmp/cloud-hypervisor.sock +Cloud Hypervisor Guest + API server: /tmp/cloud-hypervisor.sock + vCPUs: 1 + Memory: 512 MB + Kernel: None + Kernel cmdline: + Disk(s): None +``` + +### Endpoints + +The Cloud Hypervisor API exposes the following actions through its endpoints: + +#### Virtual Machine Manager (VMM) Actions + +| Action | Endpoint | Request Body | Response Body | Prerequisites | +| ----------------------------------- | --------------- | ------------ | -------------------------- | ------------------ | +| Check for the REST API availability | `/vmm.ping` | N/A | `/schemas/VmmPingResponse` | N/A | +| Shut the VMM down | `/vmm.shutdown` | N/A | N/A | The VMM is running | + +#### Virtual Machine (VM) Actions + +| Action | Endpoint | Request Body | Response Body | Prerequisites | +| ---------------------------------- | --------------------- | --------------------------- | ------------------------ | -------------------------------- | +| Create the VM | `/vm.create` | `/schemas/VmConfig` | N/A | The VM is not created yet | +| Delete the VM | `/vm.delete` | N/A | N/A | N/A | +| Boot the VM | `/vm.boot` | N/A | N/A | The VM is created but not booted | +| Shut the VM down | `/vm.shutdown` | N/A | N/A | The VM is booted | +| Reboot the VM | `/vm.reboot` | N/A | N/A | The VM is booted | +| Trigger power button of the VM | `/vm.power-button` | N/A | N/A | The VM is booted | +| Pause the VM | `/vm.pause` | N/A | N/A | The VM is booted | +| Resume the VM | `/vm.resume` | N/A | N/A | The VM is paused | +| Task a snapshot of the VM | `/vm.snapshot` | `/schemas/VmSnapshotConfig` | N/A | The VM is paused | +| Perform a coredump of the VM | `/vm.coredump` | `/schemas/VmCoredumpData` | N/A | The VM is paused | +| Restore the VM from a snapshot | `/vm.restore` | `/schemas/RestoreConfig` | N/A | The VM is created but not booted | +| Add/remove CPUs to/from the VM | `/vm.resize` | `/schemas/VmResize` | N/A | The VM is booted | +| Add/remove memory from the VM | `/vm.resize` | `/schemas/VmResize` | N/A | The VM is booted | +| Add/remove memory from a zone | `/vm.resize-zone` | `/schemas/VmResizeZone` | N/A | The VM is booted | +| Dump the VM information | `/vm.info` | N/A | `/schemas/VmInfo` | The VM is created | +| Add VFIO PCI device to the VM | `/vm.add-device` | `/schemas/VmAddDevice` | `/schemas/PciDeviceInfo` | The VM is booted | +| Add disk device to the VM | `/vm.add-disk` | `/schemas/DiskConfig` | `/schemas/PciDeviceInfo` | The VM is booted | +| Add fs device to the VM | `/vm.add-fs` | `/schemas/FsConfig` | `/schemas/PciDeviceInfo` | The VM is booted | +| Add pmem device to the VM | `/vm.add-pmem` | `/schemas/PmemConfig` | `/schemas/PciDeviceInfo` | The VM is booted | +| Add network device to the VM | `/vm.add-net` | `/schemas/NetConfig` | `/schemas/PciDeviceInfo` | The VM is booted | +| Add userspace PCI device to the VM | `/vm.add-user-device` | `/schemas/VmAddUserDevice` | `/schemas/PciDeviceInfo` | The VM is booted | +| Add vdpa device to the VM | `/vm.add-vdpa` | `/schemas/VdpaConfig` | `/schemas/PciDeviceInfo` | The VM is booted | +| Add vsock device to the VM | `/vm.add-vsock` | `/schemas/VsockConfig` | `/schemas/PciDeviceInfo` | The VM is booted | +| Remove device from the VM | `/vm.remove-device` | `/schemas/VmRemoveDevice` | N/A | The VM is booted | +| Dump the VM counters | `/vm.counters` | N/A | `/schemas/VmCounters` | The VM is booted | + +### REST API Examples + +For the following set of examples, we assume Cloud Hypervisor is started with +the REST API available at `/tmp/cloud-hypervisor.sock`: + +``` +$ ./target/debug/cloud-hypervisor --api-socket /tmp/cloud-hypervisor.sock +Cloud Hypervisor Guest + API server: /tmp/cloud-hypervisor.sock + vCPUs: 1 + Memory: 512 MB + Kernel: None + Kernel cmdline: + Disk(s): None +``` + +#### Create a Virtual Machine + +We want to create a virtual machine with the following characteristics: + +* 4 vCPUs +* 1 GB of RAM +* 1 virtio based networking interface +* Direct kernel boot from a custom 5.6.0-rc4 Linux kernel located at + `/opt/clh/kernel/vmlinux-virtio-fs-virtio-iommu` +* Using a Ubuntu image as its root filesystem, located at + `/opt/clh/images/focal-server-cloudimg-amd64.raw` + +```shell +#!/bin/bash + +curl --unix-socket /tmp/cloud-hypervisor.sock -i \ + -X PUT 'http://localhost/api/v1/vm.create' \ + -H 'Accept: application/json' \ + -H 'Content-Type: application/json' \ + -d '{ + "cpus":{"boot_vcpus": 4, "max_vcpus": 4}, + "payload":{"kernel":"/opt/clh/kernel/vmlinux-virtio-fs-virtio-iommu", "cmdline":"console=ttyS0 console=hvc0 root=/dev/vda1 rw"}, + "disks":[{"path":"/opt/clh/images/focal-server-cloudimg-amd64.raw"}], + "rng":{"src":"/dev/urandom"}, + "net":[{"ip":"192.168.10.10", "mask":"255.255.255.0", "mac":"12:34:56:78:90:01"}] + }' +``` + +#### Boot a Virtual Machine + +Once the VM is created, we can boot it: + +```shell +#!/bin/bash + +curl --unix-socket /tmp/cloud-hypervisor.sock -i -X PUT 'http://localhost/api/v1/vm.boot' +``` + +#### Dump a Virtual Machine Information + +We can fetch information about any VM, as soon as it's created: + +```shell +#!/bin/bash + +curl --unix-socket /tmp/cloud-hypervisor.sock -i \ + -X GET 'http://localhost/api/v1/vm.info' \ + -H 'Accept: application/json' +``` + +#### Reboot a Virtual Machine + +We can reboot a VM that's already booted: + +```shell +#!/bin/bash + +curl --unix-socket /tmp/cloud-hypervisor.sock -i -X PUT 'http://localhost/api/v1/vm.reboot' +``` + +#### Shut a Virtual Machine Down + +Once booted, we can shut a VM down from the REST API: + +```shell +#!/bin/bash + +curl --unix-socket /tmp/cloud-hypervisor.sock -i -X PUT 'http://localhost/api/v1/vm.shutdown' +``` + +### Command Line Interface + +The Cloud Hypervisor Command Line Interface (CLI) can only be used for launching +the Cloud Hypervisor binary, i.e. it can not be used for controlling the VMM or +the launched VM once they're up and running. + +If you want to inspect the VMM, or control the VM after launching Cloud +Hypervisor from the CLI, you must use the [REST API](#rest-api). + +From the CLI, one can either: + +1. Create and boot a complete virtual machine by using the CLI options to build + the VM config. Run `cloud-hypervisor --help` for a complete list of CLI + options. As soon as the `cloud-hypervisor` binary is launched, the + [REST API](#rest-api) is available for controlling and managing the VM. +1. Start the [REST API](#rest-api) server only, by not passing any VM + configuration options. The VM can then be asynchronously created and booted + by sending HTTP commands to the [REST API](#rest-api). Check the + [REST API examples](#rest-api-examples) section for more details. + +### REST API and CLI Architectural Relationship + +The REST API and the CLI both rely on a common, [internal API](#internal-api). + +The CLI options are parsed by the +[clap crate](https://docs.rs/clap/2.33.0/clap/) and then translated into +[internal API](#internal-api) commands. + +The REST API is processed by an HTTP thread using the +[Firecracker's `micro_http`](https://github.com/firecracker-microvm/firecracker/tree/master/src/micro_http) +crate. As with the CLI, the HTTP requests eventually get translated into +[internal API](#internal-api) commands. + +As a summary, the REST API and the CLI are essentially frontends for the +[internal API](#internal-api): + +``` + +------------------+ + REST API | | + +--------->+ micro_http +--------+ + | | | | + | +------------------+ | + | | +------------------------+ + | | | | ++------------+ | | | | +| | | | | +--------------+ | +| User +---------+ +------> | Internal API | | +| | | | | +--------------+ | ++------------+ | | | | + | | | | + | | +------------------------+ + | +----------+ | VMM + | CLI | | | + +----------->+ clap +--------------+ + | | + +----------+ + + +``` + +## Internal API + +The Cloud Hypervisor internal API, as its name suggests, is used internally +by the different Cloud Hypervisor threads (VMM, HTTP, control loop, etc) to +send commands and responses to each others. + +It is based on [rust's Multi-Producer, Single-Consumer (MPSC)](https://doc.rust-lang.org/std/sync/mpsc/), +and the single consumer (a.k.a. the API receiver) is the Cloud Hypervisor +control loop. + +API producers are the HTTP thread handling the [REST API](#rest-api) and the +main thread that initially parses the [CLI](#command-line-interface). + +### Goals and Design + +The internal API is designed for controlling, managing and inspecting a Cloud +Hypervisor VMM and its guest. It is a backend for handling external, user +visible requests through either the [REST API](#rest-api) or the +[CLI](#command-line-interface) interfaces. + +The API follows a command-response scheme that closely maps the [REST API](#rest-api). +Any command must be replied to with a response. + +Commands are [MPSC](https://doc.rust-lang.org/std/sync/mpsc/) based messages and +are received and processed by the VMM control loop. + +In order for the VMM control loop to respond to any internal API command, it +must be able to send a response back to the MPSC sender. For that purpose, all +internal API command payload carry the [Sender](https://doc.rust-lang.org/std/sync/mpsc/struct.Sender.html) +end of an [MPSC](https://doc.rust-lang.org/std/sync/mpsc/) channel. + +The sender of any internal API command is therefore responsible for: + +1. Creating an [MPSC](https://doc.rust-lang.org/std/sync/mpsc/) response + channel. +1. Passing the [Sender](https://doc.rust-lang.org/std/sync/mpsc/struct.Sender.html) + end of the response channel as part of the internal API command payload. +1. Waiting for the internal API command's response on the [Receiver](https://doc.rust-lang.org/std/sync/mpsc/struct.Receiver.html) + end of the response channel. + +## End to End Example + +In order to further understand how the external and internal Cloud Hypervisor +APIs work together, let's look at a complete VM creation flow, from the +[REST API](#rest-api) call, to the reply the external user will receive: + +1. A user or operator sends an HTTP request to the Cloud Hypervisor + [REST API](#rest-api) in order to creates a virtual machine: + ``` + shell + #!/bin/bash + + curl --unix-socket /tmp/cloud-hypervisor.sock -i \ + -X PUT 'http://localhost/api/v1/vm.create' \ + -H 'Accept: application/json' \ + -H 'Content-Type: application/json' \ + -d '{ + "cpus":{"boot_vcpus": 4, "max_vcpus": 4}, + "payload":{"kernel":"/opt/clh/kernel/vmlinux-virtio-fs-virtio-iommu", "cmdline":"console=ttyS0 console=hvc0 root=/dev/vda1 rw"}, + "disks":[{"path":"/opt/clh/images/focal-server-cloudimg-amd64.raw"}], + "rng":{"src":"/dev/urandom"}, + "net":[{"ip":"192.168.10.10", "mask":"255.255.255.0", "mac":"12:34:56:78:90:01"}] + }' + ``` +1. The Cloud Hypervisor HTTP thread processes the request and de-serializes the + HTTP request JSON body into an internal `VmConfig` structure. +1. The Cloud Hypervisor HTTP thread creates an + [MPSC](https://doc.rust-lang.org/std/sync/mpsc/) channel for the internal API + server to send its response back. +1. The Cloud Hypervisor HTTP thread prepares an internal API command for creating a + virtual machine. The command's payload is made of the de-serialized + `VmConfig` structure and the response channel: + ```Rust + VmCreate(Arc>, Sender) + ``` +1. The Cloud Hypervisor HTTP thread sends the internal API command, and waits + for the response: + ```Rust + // Send the VM creation request. + api_sender + .send(ApiRequest::VmCreate(config, response_sender)) + .map_err(ApiError::RequestSend)?; + api_evt.write(1).map_err(ApiError::EventFdWrite)?; + + response_receiver.recv().map_err(ApiError::ResponseRecv)??; + ``` +1. The Cloud Hypervisor control loop receives the command, as it listens on the + internal API [MPSC](https://doc.rust-lang.org/std/sync/mpsc/) channel: + ```Rust + // Read from the API receiver channel + let api_request = api_receiver.recv().map_err(Error::ApiRequestRecv)?; + ``` +1. The Cloud Hypervisor control loop matches the received internal API against + the `VmCreate` payload, and extracts both the `VmConfig` structure and the + [Sender](https://doc.rust-lang.org/std/sync/mpsc/struct.Sender.html) from the + command payload. It stores the `VmConfig` structure and replies back to the + sender ((The HTTP thread): + ```Rust + match api_request { + ApiRequest::VmCreate(config, sender) => { + // We only store the passed VM config. + // The VM will be created when being asked to boot it. + let response = if self.vm_config.is_none() { + self.vm_config = Some(config); + Ok(ApiResponsePayload::Empty) + } else { + Err(ApiError::VmAlreadyCreated) + }; + + sender.send(response).map_err(Error::ApiResponseSend)?; + } + ``` +1. The Cloud Hypervisor HTTP thread receives the internal API command response + as the return value from its `VmCreate` HTTP handler. Depending on the + control loop internal API response, it generates the appropriate HTTP + response: + ```Rust + // Call vm_create() + match vm_create(api_notifier, api_sender, Arc::new(Mutex::new(vm_config))) + .map_err(HttpError::VmCreate) + { + Ok(_) => Response::new(Version::Http11, StatusCode::NoContent), + Err(e) => error_response(e, StatusCode::InternalServerError), + } + ``` +1. The Cloud Hypervisor HTTP thread sends the formed HTTP response back to the + user. This is abstracted by the + [micro_http](https://github.com/firecracker-microvm/firecracker/tree/master/src/micro_http) + crate. diff --git a/vendor/cube-hypervisor/docs/arm64.md b/vendor/cube-hypervisor/docs/arm64.md new file mode 100644 index 0000000..72aa64f --- /dev/null +++ b/vendor/cube-hypervisor/docs/arm64.md @@ -0,0 +1,152 @@ +# How to build and test Cloud Hypervisor on AArch64 + +This document introduces how to build and test Cloud Hypervisor on AArch64. +Currently, Cloud Hypervisor supports 2 methods of booting on AArch64: UEFI +booting and direct-kernel booting. The document covers both methods. + +All the steps are based on Ubuntu. We use the Ubuntu cloud image for guest VM +disk. + +## Hardware requirements + +- AArch64 servers (recommended) or development boards equipped with the GICv3 +interrupt controller. + +- On development boards that have constrained RAM resources, if the creation of +a VM consumes a large portion of the free memory on the host, it may be required +to enable swap. For example, this was required on a board with 3 GB of RAM +booting a 2 GB VM at a point in time when 2.8 GB were free. Without enabling +swap the `cloud-hypervisor` process was terminated by the OOM killer. In this +situation memory was allocated for the virtual machine using memfd while the +page cache was filled, leading to a situation where the kernel could not even +drop caches. Making a small section of swap available (observably, 1 to 15 MB), +this situation can be resolved and the resulting memory footprint of +`cloud-hypervisor` is as expected. + +## Getting started + +We create a folder to build and run Cloud Hypervisor at `$HOME/cloud-hypervisor` + +```shell +$ export CLOUDH=$HOME/cloud-hypervisor +$ mkdir $CLOUDH +``` + +## Prerequisites + +You need to install some prerequisite packages to build and test Cloud Hypervisor. + +### Tools + +```bash +# Install rust tool chain +$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh +# Install the tools used for building guest kernel, EDK2 and converting guest disk +$ sudo apt-get update +$ sudo apt-get install git build-essential m4 bison flex uuid-dev qemu-utils +``` + +### Building Cloud Hypervisor + +```bash +$ pushd $CLOUDH +$ git clone https://github.com/cloud-hypervisor/cloud-hypervisor.git +$ cd cloud-hypervisor +$ cargo build +$ popd +``` + +### Disk image + +Download the Ubuntu cloud image and convert the image type. + +```bash +$ pushd $CLOUDH +$ wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-arm64.img +$ qemu-img convert -p -f qcow2 -O raw focal-server-cloudimg-arm64.img focal-server-cloudimg-arm64.raw +$ popd +``` + +## UEFI booting + +This part introduces how to build EDK2 firmware and boot Cloud Hypervisor with it. + +### Building EDK2 + +```bash +$ pushd $CLOUDH + +# Clone source code repos +$ git clone --depth 1 https://github.com/tianocore/edk2.git -b master +$ cd edk2 +$ git submodule update --init +$ cd .. +$ git clone --depth 1 https://github.com/tianocore/edk2-platforms.git -b master +$ git clone --depth 1 https://github.com/acpica/acpica.git -b master + +# Build tools +$ export PACKAGES_PATH="$PWD/edk2:$PWD/edk2-platforms" +$ export IASL_PREFIX="$PWD/acpica/generate/unix/bin/" +$ make -C acpica +$ cd edk2/ +$ . edksetup.sh +$ cd .. +$ make -C edk2/BaseTools + +# Build EDK2 +$ build -a AARCH64 -t GCC5 -p ArmVirtPkg/ArmVirtCloudHv.dsc -b RELEASE + +$ popd +``` + +If the build goes well, the EDK2 binary is available at +`edk2/Build/ArmVirtCloudHv-AARCH64/RELEASE_GCC5/FV/CLOUDHV_EFI.fd`. + +### Booting the guest VM + +```bash +$ pushd $CLOUDH +$ sudo RUST_BACKTRACE=1 $CLOUDH/cloud-hypervisor/target/debug/cloud-hypervisor \ + --api-socket /tmp/cloud-hypervisor.sock \ + --kernel $CLOUDH/edk2/Build/ArmVirtCloudHv-AARCH64/RELEASE_GCC5/FV/CLOUDHV_EFI.fd \ + --disk path=$CLOUDH/focal-server-cloudimg-arm64.raw \ + --cpus boot=4 \ + --memory size=4096M \ + --net tap=,mac=12:34:56:78:90:01,ip=192.168.1.1,mask=255.255.255.0 \ + --serial tty \ + --console off +$ popd +``` + +## Direct-kernel booting + +Alternativelly, you can build your own kernel for guest VM. This way, UEFI is +not involved and ACPI cannot be enabled. + +### Building kernel + +```bash +$ pushd $CLOUDH +$ git clone --depth 1 "https://github.com/cloud-hypervisor/linux.git" -b ch-5.12 +$ cd linux +$ cp $CLOUDH/cloud-hypervisor/resources/linux-config-aarch64 .config +$ make -j `nproc` +$ popd +``` + +### Booting the guest VM + +```bash +$ pushd $CLOUDH +$ sudo $CLOUDH/cloud-hypervisor/target/debug/cloud-hypervisor \ + --api-socket /tmp/cloud-hypervisor.sock \ + --kernel $CLOUDH/linux/arch/arm64/boot/Image \ + --disk path=focal-server-cloudimg-arm64.raw \ + --cmdline "keep_bootcon console=ttyAMA0 reboot=k panic=1 root=/dev/vda1 rw" \ + --cpus boot=4 \ + --memory size=4096M \ + --net tap=,mac=12:34:56:78:90:01,ip=192.168.1.1,mask=255.255.255.0 \ + --serial tty \ + --console off +$ popd +``` diff --git a/vendor/cube-hypervisor/docs/balloon.md b/vendor/cube-hypervisor/docs/balloon.md new file mode 100644 index 0000000..802be38 --- /dev/null +++ b/vendor/cube-hypervisor/docs/balloon.md @@ -0,0 +1,76 @@ +# Balloon + +Cloud Hypervisor implements a balloon device based on the VIRTIO specification. +Its main purpose is to provide the host a way to reclaim memory by controlling +the amount of memory visible to the guest. But it also provides some interesting +features related to guest memory management. + +## Parameters + +`BalloonConfig` (known as `--balloon` from the CLI perspective) contains the +list of parameters available for the balloon device. + +```rust +struct BalloonConfig { + pub size: u64, + pub deflate_on_oom: bool, + pub free_page_reporting: bool, +} +``` + +``` +--balloon Balloon parameters "size=,deflate_on_oom=on|off,free_page_reporting=on|off" +``` + +### `size` + +Size of the balloon device. It is subtracted from the VM's total size. For +instance, if creating a VM with 4GiB of RAM, along with a balloon of 1GiB, the +guest will be able to use 3GiB of accessible memory. The guest sees all the RAM +and unless it is balloon enlightened is entitled to all of it. + +This parameter is mandatory. + +Value is an unsigned integer of 64 bits corresponding to the balloon size in +bytes. + +_Example_ + +``` +--balloon size=1G +``` + +### `deflate_on_oom` + +Allow the guest to deflate the balloon if running Out Of Memory (OOM). Assuming +the balloon size is greater than 0, this means the guest is allowed to reduce +the balloon size all the way down to 0 if this can help recover from the OOM +event. + +This parameter is optional. + +Value is a boolean set to `off` by default. + +_Example_ + +``` +--ballloon size=2G,deflate_on_oom=on +``` + +### `free_page_reporting` + +Allow the guest to report lists of free pages. This feature doesn't require the +balloon to be of any specific size as it doesn't impact the balloon size. The +guest can let the VMM know about pages that are free after they have been used. +Based on this information, the VMM can advise the host that it doesn't need +these pages anymore. + +This parameter is optional. + +Value is a boolean set to `off` by default. + +_Example_ + +``` +--ballloon size=0,free_page_reporting=on +``` diff --git a/vendor/cube-hypervisor/docs/building.md b/vendor/cube-hypervisor/docs/building.md new file mode 100644 index 0000000..419b0f7 --- /dev/null +++ b/vendor/cube-hypervisor/docs/building.md @@ -0,0 +1,82 @@ +- [Building Cloud Hypervisor](#building-cloud-hypervisor) + - [Preparation](#preparation) + - [Install prerequisites](#install-prerequisites) + - [Clone and build](#clone-and-build) + - [Containerized builds and tests](#containerized-builds-and-tests) + +# Building Cloud Hypervisor + +We recommend users use the pre-built binaries that are mentioned in the README.md file in the root of the repository. Building from source is only necessary if you wish to make modifications. + +## Preparation + +We create a folder to build and run `cloud-hypervisor` at `$HOME/cloud-hypervisor` + +```shell +$ export CLOUDH=$HOME/cloud-hypervisor +$ mkdir $CLOUDH +``` + +## Install prerequisites + +You need to install some prerequisite packages in order to build and test Cloud +Hypervisor. Here, all the steps are based on Ubuntu, for other Linux +distributions please replace the package manager and package name. + +```shell +# Install build-essential, git, and qemu-utils +$ sudo apt install git build-essential qemu-utils +# Install rust tool chain +$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh +# If you want to build statically linked binary please add musl target +$ rustup target add x86_64-unknown-linux-musl +``` + +## Clone and build + +First you need to clone and build the Cloud Hypervisor repository: + +```shell +$ pushd $CLOUDH +$ git clone https://github.com/cloud-hypervisor/cloud-hypervisor.git +$ cd cloud-hypervisor +$ cargo build --release + +# We need to give the cloud-hypervisor binary the NET_ADMIN capabilities for it to set TAP interfaces up on the host. +$ sudo setcap cap_net_admin+ep ./target/release/cloud-hypervisor + +# If you want to build statically linked binary +$ cargo build --release --target=x86_64-unknown-linux-musl --all +$ popd +``` + +This will build a `cloud-hypervisor` binary under +`$CLOUDH/cloud-hypervisor/target/release/cloud-hypervisor`. + +### Containerized builds and tests + +If you want to build and test Cloud Hypervisor without having to install all the +required dependencies (The rust toolchain, cargo tools, etc), you can also use +Cloud Hypervisor's development script: `dev_cli.sh`. Please note that upon its +first invocation, this script will pull a fairly large container image. + +For example, to build the Cloud Hypervisor release binary: + +```shell +$ pushd $CLOUDH +$ cd cloud-hypervisor +$ ./scripts/dev_cli.sh build --release +``` + +With `dev_cli.sh`, one can also run the Cloud Hypervisor CI locally. This can be +very convenient for debugging CI errors without having to fully rely on the +Cloud Hypervisor CI infrastructure. + +For example, to run the Cloud Hypervisor unit tests: + +```shell +$ ./scripts/dev_cli.sh tests --unit +``` + +Run the `./scripts/dev_cli.sh --help` command to view all the supported +development script commands and their related options. diff --git a/vendor/cube-hypervisor/docs/cpu.md b/vendor/cube-hypervisor/docs/cpu.md new file mode 100644 index 0000000..5dd7973 --- /dev/null +++ b/vendor/cube-hypervisor/docs/cpu.md @@ -0,0 +1,211 @@ +# CPU + +Cloud Hypervisor has many options when it comes to the creation of virtual +CPUs. This document aims to explain what Cloud Hypervisor is capable of and +how it can be used to meet the needs of very different use cases. + +## Options + +`CpusConfig` or what is known as `--cpus` from the CLI perspective is the way +to set vCPUs options for Cloud Hypervisor. + +```rust +struct CpusConfig { + boot_vcpus: u8, + max_vcpus: u8, + topology: Option, + kvm_hyperv: bool, + max_phys_bits: u8, + affinity: Option>, + features: CpuFeatures, +} +``` + +``` +--cpus boot=,max=,topology=:::,kvm_hyperv=on|off,max_phys_bits=,affinity=,features= +``` + +### `boot` + +Number of vCPUs present at boot time. + +This option allows to define a specific number of vCPUs to be present at the +time the VM is started. This option is mandatory when using the `--cpus` +parameter. If `--cpus` is not specified, this option takes the default value +of `1`, starting the VM with a single vCPU. + +Value is an unsigned integer of 8 bits. + +_Example_ + +``` +--cpus boot=2 +``` + +### `max` + +Maximum number of vCPUs. + +This option defines the maximum number of vCPUs that can be assigned to the VM. +In particular, this option is used when looking for CPU hotplug as it lets the +provide an indication about how many vCPUs might be needed later during the +runtime of the VM. +For instance, if booting the VM with 2 vCPUs and a maximum of 6 vCPUs, it means +up to 4 vCPUs can be added later at runtime by resizing the VM. + +The value must be greater than or equal to the number of boot vCPUs. +The value is an unsigned integer of 8 bits. + +By default this option takes the value of `boot`, meaning vCPU hotplug is not +expected and can't be performed. + +_Example_ + +``` +--cpus max=3 +``` + +### `topology` + +Topology of the guest platform. + +This option gives the user a way to describe the exact topology that should be +exposed to the guest. It can be useful to describe to the guest the same +topology found on the host as it allows for proper usage of the resources and +is a way to achieve better performances. + +The topology is described through the following structure: + +```rust +struct CpuTopology { + threads_per_core: u8, + cores_per_die: u8, + dies_per_package: u8, + packages: u8, +} +``` + +or the following syntax through the CLI: + +``` +topology=::: +``` + +By default the topology will be `1:1:1:1`. + +_Example_ + +``` +--cpus boot=2,topology=1:1:2:1 +``` + +### `kvm_hyperv` + +Enable KVM Hyper-V emulation. + +When turned on, this option relies on KVM to emulate the synthetic interrupt +controller (SynIC) along with synthetic timers expected by a Windows guest. +A Windows guest usually runs on top of Microsoft Hyper-V, therefore expects +these synthetic devices to be present. That's why KVM provides a way to emulate +them and avoids failures running a Windows guest with Cloud Hypervisor. + +By default this option is turned off. + +_Example_ + +``` +--cpus kvm_hyperv=on +``` + +### `max_phys_bits` + +Maximum size for guest's addressable space. + +This option defines the maximum number of physical bits for all vCPUs, which +sets a limit for the size of the guest's addressable space. This is mainly +useful for debug purpose. + +The value is an unsigned integer of 8 bits. + +_Example_ + +``` +--cpus max_phys_bits=40 +``` + +### `affinity` + +Affinity of each vCPU. + +This option gives the user a way to provide the host CPU set associated with +each vCPU. It is useful for achieving CPU pinning, ensuring multiple VMs won't +affect the performance of each other. It might also be used in the context of +NUMA as it is way of making sure the VM can run on a specific host NUMA node. +In general, this option is used to increase the performances of a VM depending +on the host platform and the type of workload running in the guest. + +The affinity is described through the following structure: + +```rust +struct CpuAffinity { + vcpu: u8, + host_cpus: Vec, +} +``` + +or the following syntax through the CLI: + +``` +affinity=[@[, ], @[, ]] +``` + +The outer brackets define the list of vCPUs. And for each vCPU, the inner +brackets attached to `@` define the list of host CPUs the vCPU is allowed to +run onto. + +Multiple values can be provided to define each list. Each value is an unsigned +integer of 8 bits. + +For instance, if one needs to run vCPU 0 on host CPUs from 0 to 4, the syntax +using `-` will help define a contiguous range with `affinity=0@[0-4]`. The +same example could also be described with `affinity=0@[0,1,2,3,4]`. + +A combination of both `-` and `,` separators is useful when one might need to +describe a list containing host CPUs from 0 to 99 and the host CPU 255, as it +could simply be described with `affinity=0@[0-99,255]`. + +As soon as one tries to describe a list of values, `[` and `]` must be used to +demarcate the list. + +By default each vCPU runs on the entire host CPU set. + +_Example_ + +``` +--cpus boot=3,affinity=[0@[2,3],1@[0,1]] +``` + +In this example, assuming the host has 4 CPUs, vCPU 0 will run exclusively on +host CPUs 2 and 3, while vCPU 1 will run exclusively on host CPUs 0 and 1. +Because nothing is defined for vCPU 2, it can run on any of the 4 host CPUs. + +### `features` + +Set of CPU features to enable. + +This option allows the user to enable a set of CPU features that are disabled +by default otherwise. + +The currently available feature set is: `amx`. + +The `amx` feature will enable the x86 extension adding hardware units for +matrix operations (int and float dot products). The goal of the extension is to +provide performance enhancements for these common operations. + +_Example_ + +``` +--cpus features=amx +``` + +In this example the amx CPU feature will be enabled for the VMM. diff --git a/vendor/cube-hypervisor/docs/custom-image.md b/vendor/cube-hypervisor/docs/custom-image.md new file mode 100644 index 0000000..2970dd0 --- /dev/null +++ b/vendor/cube-hypervisor/docs/custom-image.md @@ -0,0 +1,160 @@ +# How to create a custom Ubuntu image + +In the context of adding more utilities to the Ubuntu cloud image being used +for integration testing, this quick guide details how to achieve the proper +modification of an official Ubuntu cloud image. + +## Create the image + +Let's go through the steps on how to extend an official Ubuntu image. These +steps can be applied to other distributions (with a few changes regarding +package management). + +### Get latest Ubuntu cloud image + +```bash +wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img +``` + +### Check the file format is QCOW2 + +```bash +file focal-server-cloudimg-amd64.img +focal-server-cloudimg-amd64.img: QEMU QCOW2 Image (v2), 2361393152 bytes +``` + +### Convert QCOW2 into RAW + +```bash +qemu-img convert -p -f qcow2 -O raw focal-server-cloudimg-amd64.img focal-server-cloudimg-amd64.raw +``` + +### Identify the Linux partition + +The goal is to mount the image rootfs so that it can be modified as needed. +That's why we need to identify where the Linux filesystem partition is located +in the image. + +```bash +sudo fdisk -l focal-server-cloudimg-amd64.raw +Disk focal-server-cloudimg-amd64.raw: 2.2 GiB, 2361393152 bytes, 4612096 sectors +Units: sectors of 1 * 512 = 512 bytes +Sector size (logical/physical): 512 bytes / 512 bytes +I/O size (minimum/optimal): 512 bytes / 512 bytes +Disklabel type: gpt +Disk identifier: A1171ABA-2BEA-4218-A467-1B2B607E5953 + +Device Start End Sectors Size Type +focal-server-cloudimg-amd64.raw1 227328 4612062 4384735 2.1G Linux filesystem +focal-server-cloudimg-amd64.raw14 2048 10239 8192 4M BIOS boot +focal-server-cloudimg-amd64.raw15 10240 227327 217088 106M EFI System + +Partition table entries are not in disk order. +``` + +### Mount the Linux partition + +```bash +mkdir -p /mnt +sudo mount -o loop,offset=$((227328 * 512)) focal-server-cloudimg-amd64.raw /mnt +``` + +### Set up DNS + +The next step describes changing the root directory to the rootfs contained by +the cloud image. For DNS to work in the root directory, you will need to first bind-mount +the host `/etc/resolv.conf` onto the mounted linux partition of the cloud image. + +```bash +sudo mount -o bind /etc/resolv.conf /mnt/etc/resolv.conf +``` + +### Change root directory + +Changing the root directory will allow us to install new packages to the rootfs +contained by the cloud image. + +```bash +sudo chroot /mnt +mount -t proc proc /proc +mount -t devpts devpts /dev/pts +``` + +### Install needed packages + +In the context Cloud Hypervisor's integration tests, we need several utilities. +Here is the way to install them for a Ubuntu image. This step is specific to +Ubuntu distributions. + +```bash +apt update +apt install fio iperf iperf3 socat stress +``` + +### Remove counterproductive packages + +* snapd: + +This prevents snapd from trying to mount squashfs filesystem when the kernel +might not support it. This might be the case when the image is used with direct +kernel boot. This step is specific to Ubuntu distributions. + +* pollinate: + +Remove this package which can fail and lead to the SSH daemon failing to start. +See #2113 for details. + +```bash +apt remove --purge snapd pollinate +``` + + +### Cleanup the image + +Leave no trace in the image before unmounting its content. + +```bash +umount /dev/pts +umount /proc +history -c +exit +umount /mnt/etc/resolv.conf +umount /mnt +``` + +### Rename the image + +Renaming is important to identify this is a modified image. + +```bash +mv focal-server-cloudimg-amd64.raw focal-server-cloudimg-amd64-custom-$(date "+%Y%m%d")-0.raw +``` + +The `-0` is the revision and is only necessary to change if multiple images are +updated on the same day. + +### Create QCOW2 from RAW + +Last step is to create the QCOW2 image back from the modified image. + +```bash +qemu-img convert -p -f raw -O qcow2 focal-server-cloudimg-amd64-custom-$(date "+%Y%m%d")-0.raw focal-server-cloudimg-amd64-custom-$(date "+%Y%m%d")-0.qcow2 +``` + +## Switch CI to use the new image + +### Upload to Azure storage + +The next step is to update both images (QCOW2 and RAW) stored as part of the +Azure storage account, replacing them with the newly created ones. This will +make these new images available from the integration tests. This is usually +achieved through the web interface. + +### Update integration tests + +Last step is about updating the integration tests to work with this new image. +The key point is to identify where the Linux filesystem partition is located, +as we might need to update the direct kernel boot command line, replacing +`/dev/vda1` with the appropriate partition number. + +Update all references to the previous image name to the new one. diff --git a/vendor/cube-hypervisor/docs/debug-port.md b/vendor/cube-hypervisor/docs/debug-port.md new file mode 100644 index 0000000..6bafd1c --- /dev/null +++ b/vendor/cube-hypervisor/docs/debug-port.md @@ -0,0 +1,61 @@ +# `cloud-hypervisor` debug IO port + +`cloud-hypervisor` uses the [`0x80`](https://www.intel.com/content/www/us/en/support/articles/000005500/boards-and-kits.html) +I/O port to trace user defined guest events. + +Whenever the guest write one byte between `0x0` and `0xF` on this particular +I/O port, `cloud-hypervisor` will log and timestamp that event at the `debug` +log level. + +It is up to the guest stack to decide when and what to write to the 0x80 port +in order to signal the host about specific events and have `cloud-hypervisor` +log it. + +`cloud-hypervisor` defines several debug port code ranges that should be used +for debugging specific components of the guest software stack. When logging a +write of one of those codes to the debug port, `cloud-hypervisor` adds a +pre-defined string to the logs. + +| Code Range | Component | Log string | +| ---------------- | ----------- | ------------ | +| `0x00` to `0x1f` | Firmware | `Firmware` | +| `0x20` to `0x3f` | Bootloader | `Bootloader` | +| `0x40` to `0x5f` | Kernel | `Kernel` | +| `0x60` to `0x7f` | Userspace | `Userspace` | +| `0x80` to `0xff` | Custom | `Custom` | + +One typical use case is guest boot time measurement and tracing. By writing +different values to the debug I/O port at different boot process steps, the +guest will have `cloud-hypervisor` generate timestamped logs of all those steps. +That provides a basic but convenient way of measuring not only the overall guest +boot time but all intermediate steps as well. + +## Logging + +Assuming parts of the guest software stack have been instrumented to use the +`cloud-hypervisor` debug I/O port, we may want to gather the related logs. + +To do so we need to start `cloud-hypervisor` with the right debug level +(`-vvv`). It is also recommended to have it log into a dedicated file in order +to easily grep for the tracing logs (e.g. +`--log-file /tmp/cloud-hypervisor.log`): + +``` +./target/debug/cloud-hypervisor \ + --kernel ~/rust-hypervisor-firmware/target/target/release/hypervisor-fw \ + --disk path=~/hypervisor/images/focal-server-cloudimg-amd64.raw \ + --cpus 4 \ + --memory size=1024M \ + --rng \ + --log-file /tmp/ch-fw.log \ + -vvv +``` + +After booting the guest, we then have to grep for the debug I/O port traces in +the log file: + +```Shell +$ grep "Debug I/O port" /tmp/ch-fw.log +cloud-hypervisor: 19.762449ms: DEBUG:vmm/src/vm.rs:510 -- [Debug I/O port: Firmware code 0x0] 0.019004 seconds +cloud-hypervisor: 403.499628ms: DEBUG:vmm/src/vm.rs:510 -- [Debug I/O port: Firmware code 0x1] 0.402744 seconds +``` diff --git a/vendor/cube-hypervisor/docs/device_model.md b/vendor/cube-hypervisor/docs/device_model.md new file mode 100644 index 0000000..0233ad0 --- /dev/null +++ b/vendor/cube-hypervisor/docs/device_model.md @@ -0,0 +1,217 @@ +# Device Model + +This document describes the device model supported by `cloud-hypervisor`. + +## Summary + +| Device | Build configurable | Enabled by default | Runtime configurable | +| :----: | :----: | :----: | :----: | +| Serial port | :x: | :x: | :heavy_check_mark: | +| RTC/CMOS | :heavy_check_mark: | :heavy_check_mark: | :x: | +| I/O APIC | :x: | :x: | :heavy_check_mark: | +| i8042 shutdown/reboot | :x: | :x: | :x: | +| ACPI shutdown/reboot | :x: | :heavy_check_mark: | :x: | +| virtio-blk | :x: | :x: | :heavy_check_mark: | +| virtio-console | :x: | :x: | :heavy_check_mark: | +| virtio-iommu | :x: | :x: | :heavy_check_mark: | +| virtio-net | :x: | :x: | :heavy_check_mark: | +| virtio-pmem | :x: | :x: | :heavy_check_mark: | +| virtio-rng | :x: | :x: | :heavy_check_mark: | +| virtio-vsock | :x: | :x: | :heavy_check_mark: | +| vhost-user-blk | :x: | :x: | :heavy_check_mark: | +| vhost-user-fs | :x: | :x: | :heavy_check_mark: | +| vhost-user-net | :x: | :x: | :heavy_check_mark: | +| VFIO | :heavy_check_mark: | :x: | :heavy_check_mark: | + +## Legacy devices + +### Serial port + +Simple emulation of a serial port by reading and writing to specific port I/O +addresses. The serial port can be very useful to gather early logs from the +operating system booted inside the VM. + +For x86_64, The default serial port is from an emulated 16550A device. It can +be used as the default console for Linux when booting with the option +`console=ttyS0`. For AArch64, the default serial port is from an emulated +PL011 UART device. The related command line for AArch64 is `console=ttyAMA0`. + +This device is always built-in, and it is disabled by default. It can be +enabled with the `--serial` option, as long as its parameter is not `off`. + +### RTC/CMOS + +For environments such as Windows or EFI which cannot rely on KVM clock, the +emulation of this legacy device makes the platform usable. + +This device is built-in by default, but it can be compiled out with Rust +features. When compiled in, it is always enabled, and cannot be disabled +from the command line. + +For AArch64 machines, an ARM PrimeCell Real Time Clock(PL031) is implemented. +This device is built-in by default for the AArch64 platform, and it is always +enabled, and cannot be disabled from the command line. + +### I/O APIC + +`cloud-hypervisor` supports a so-called split IRQ chip implementation by +implementing support for the [IOAPIC](https://wiki.osdev.org/IOAPIC). +By moving part of the IRQ chip implementation from kernel space to user space, +the IRQ chip emulation does not always run in a fully privileged mode. + +The device is always built-in, and it is enabled depending on the presence of +the serial port. If the serial port is disabled, and because no other device +would require pin based interrupts (INTx), the I/O APIC is disabled. + +### i8042 + +Simplified PS/2 port since it supports only one key to trigger a reboot or +shutdown, depending on the ACPI support. + +This device is always built-in, but it is disabled by default. Because ACPI is +enabled by default, the handling of reboot/shutdown goes through the dedicated +ACPI device. In case ACPI is disabled, this device is enabled to bring to the +VM some reboot/shutdown support. + +### ARM PrimeCell General Purpose Input/Output (PL061) + +Simplified ARM PrimeCell GPIO (PL061) implementation. Only supports key 3 to +trigger a graceful shutdown of the AArch64 guest. + +### ACPI device + +This is a dedicated device for handling ACPI shutdown and reboot when ACPI is +enabled. + +This device is always built-in, and it is enabled by default since the ACPI +feature is enabled by default. + +## Virtio devices + +For all virtio devices listed below, only `virtio-pci` transport layer is +supported. Cloud Hypervisor supports multiple PCI segments, and users can +append `,pci_segment=` to the device flag in the Cloud +Hypervisor command line to assign devices to a specific PCI segment. + +### virtio-block + +The `virtio-blk` device exposes a block device to the guest. This device is +usually used to boot the operating system running in the VM. + +This device is always built-in, and it is enabled based on the presence of the +flag `--disk`. + +### virtio-console + +`cloud-hypervisor` exposes a `virtio-console` device to the guest. Although +using this device as a guest console can potentially cut some early boot +messages, it can reduce the guest boot time and provides a complete console +implementation. + +This device is always built-in, and it is enabled by default to provide a guest +console. It can be disabled, switching back to the legacy serial port by +selecting `--serial tty --console off` from the command line. + +### virtio-iommu + +As we want to improve our nested guests support, we added support for exposing +a [paravirtualized IOMMU](iommu.md) device through virtio. This allows for a +safer nested virtio and directly assigned devices support. + +This device is always built-in, and it is enabled based on the presence of the +parameter `iommu=on` in any of the virtio or VFIO devices. If at least one of +these devices needs to be connected to the paravirtualized IOMMU, the +`virtio-iommu` device will be created. + +### virtio-net + +The `virtio-net` device provides network connectivity for the guest, as it +creates a network interface connected to a TAP interface automatically created +by the `cloud-hypervisor` on the host. + +This device is always built-in, and it is enabled based on the presence of the +flag `--net`. + +### virtio-pmem + +The `virtio-pmem` implementation emulates a virtual persistent memory device +that `cloud-hypervisor` can e.g. boot from. Booting from a `virtio-pmem` device +allows to bypass the guest page cache and improve the guest memory footprint. + +This device is always built-in, and it is enabled based on the presence of the +flag `--pmem`. + +### virtio-rng + +A VM does not generate entropy like a real machine would, which is an issue +when workloads running in the guest need random numbers to be generated. The +`virtio-rng` device provides entropy to the guest by relying on the generator +that can be found on the host. By default, the chosen source of entropy is +`/dev/urandom`. + +This device is always built-in, and it is always enabled. The `--rng` flag can +be used to change the source of entropy. + +### virtio-vsock + +In order to more efficiently and securely communicate between host and guest, +we added a hybrid implementation of the [VSOCK](http://man7.org/linux/man-pages/man7/vsock.7.html) +socket address family over virtio. +Credits go to the [Firecracker](https://github.com/firecracker-microvm/firecracker/blob/master/docs/vsock.md) +project as our implementation is a copy of theirs. + +This device is always built-in, and it is enabled based on the presence of the +flag `--vsock`. + +## Vhost-user devices + +Vhost-user devices are virtio backends running outside of the VMM, as its own +separate process. They are usually used to bring more flexibility and increased +isolation. + +### vhost-user-blk + +As part of the general effort to offload paravirtualized I/O to external +processes, we added support for vhost-user-blk backends. This enables +`cloud-hypervisor` users to plug a `vhost-user` based block device (e.g. SPDK) +into the VMM as their virtio block backend. + +This device is always built-in, and it is enabled when `vhost_user=true` and +`socket` are provided to the `--disk` parameter. + +### vhost-user-fs + +`cloud-hypervisor` supports the [virtio-fs](https://virtio-fs.gitlab.io/) +shared file system, allowing for an efficient and reliable way of sharing +a filesystem between the host and the cloud-hypervisor guest. + +See our [filesystem sharing](fs.md) documentation for more details on how to +use virtio-fs with cloud-hypervisor. + +This device is always built-in, and it is enabled based on the presence of the +flag `--fs`. + +### vhost-user-net + +As part of the general effort to offload paravirtualized I/O to external +processes, we added support for [vhost-user-net](https://access.redhat.com/solutions/3394851) +backends. This enables `cloud-hypervisor` users to plug a `vhost-user` based +networking device (e.g. DPDK) into the VMM as their virtio network backend. + +This device is always built-in, and it is enabled when `vhost_user=true` and +`socket` are provided to the `--net` parameter. + +## VFIO + +VFIO (Virtual Function I/O) is a kernel framework that exposes direct device +access to userspace. `cloud-hypervisor` uses VFIO to directly assign host +physical devices into its guest. + +See our [VFIO documentation](vfio.md) for more details on how to directly +assign host devices to `cloud-hypervisor` guests. + +Because VFIO implies `vfio-pci` in the `cloud-hypervisor` context, the VFIO +support is built-in when the `pci` feature is selected. And because the `pci` +feature is built-in by default, VFIO support is also built-in by default. +When VFIO support is built-in, a physical device can be passed through, using +the flag `--device` in order to enable the VFIO code. diff --git a/vendor/cube-hypervisor/docs/fs.md b/vendor/cube-hypervisor/docs/fs.md new file mode 100644 index 0000000..80f0f5f --- /dev/null +++ b/vendor/cube-hypervisor/docs/fs.md @@ -0,0 +1,98 @@ +# How to use virtio-fs + +In the context of virtualization, it is always convenient to be able to share a +directory from the host with the guest. + +__virtio-fs__, also known as __vhost-user-fs__ is a virtual device defined by +the VIRTIO specification which allows any VMM to perform filesystem sharing. + +## Pre-requisites + +### The daemon + +This virtual device relies on the _vhost-user_ protocol, which assumes the +backend (device emulation) is handled by a dedicated process running on the +host. This daemon is called __virtiofsd__ and needs to be present on the host. + +_Build virtiofsd_ +```bash +git clone https://gitlab.com/virtio-fs/virtiofsd +pushd virtiofsd +cargo build --release +sudo setcap cap_sys_admin+epi target/release/virtiofsd +``` + +_Create shared directory_ +```bash +mkdir /tmp/shared_dir +``` +_Run virtiofsd_ +```bash +./virtiofsd \ + --log-level debug \ + --socket-path=/tmp/virtiofs \ + --shared-dir=/tmp/shared_dir \ + --cache=never \ + --thread-pool-size=$N +``` + +The `cache=never` option is the default when using `virtiofsd` with +Cloud Hypervisor. This prevents from using the host page cache, reducing the +overall footprint on host memory. This increases the maximum density of virtual +machines that can be launched on a single host. + +The `cache=always` option will allow the host page cache to be used, which can +result in better performance for the guest's workload at the cost of increasing +the footprint on host memory. + +The `thread-pool-size` option controls how many IO threads are spawned. For +very fast storage like NVMe spawning enough worker threads is critical to +getting an acceptable performance compared to native. + +### Kernel support + +Modern Linux kernels (at least v5.10) have support for virtio-fs. Use of older +kernels, with additional patches, are not supported. + +## How to share directories with cloud-hypervisor + +### Start the VM + +Once the daemon is running, the option `--fs` from Cloud Hypervisor needs +to be used. + +Both direct kernel boot and EFI firmware can be used to boot a VM with +virtio-fs, given that the cloud image contains a recent enough kernel. + +Correct functioning of `--fs` requires `--memory shared=on` to facilitate +interprocess memory sharing. + +Assuming you have `focal-server-cloudimg-amd64.raw` and `vmlinux` on your +system, here is the Cloud Hypervisor command you need to run: +```bash +./cloud-hypervisor \ + --cpus boot=1 \ + --memory size=1G,shared=on \ + --disk path=focal-server-cloudimg-amd64.raw \ + --kernel vmlinux \ + --cmdline "console=hvc0 root=/dev/vda1 rw" \ + --fs tag=myfs,socket=/tmp/virtiofs,num_queues=1,queue_size=512 +``` + +### Mount the shared directory + +The last step is to mount the shared directory inside the guest, using the +`virtiofs` filesystem type. + +```bash +mkdir mount_dir +mount -t virtiofs myfs mount_dir/ +``` + +The `tag` needs to be consistent with what has been provided through the +Cloud Hypervisor command line, which happens to be `myfs` in this example. + +## DAX feature + +Given the DAX feature is not stable yet from a daemon standpoint, it is not +available in Cloud Hypervisor. diff --git a/vendor/cube-hypervisor/docs/fuzzing.md b/vendor/cube-hypervisor/docs/fuzzing.md new file mode 100644 index 0000000..d6dd968 --- /dev/null +++ b/vendor/cube-hypervisor/docs/fuzzing.md @@ -0,0 +1,35 @@ +# Fuzzing in Cloud Hypervisor + +Cloud Hypervisor uses [cargo-fuzz](https://github.com/rust-fuzz/cargo-fuzz) for fuzzing individual components. + +The fuzzers are are in the `fuzz/fuzz_targets` directory + +## Preparation + +Switch to nightly: + +```` +rustup override set nightly +```` + +Install `cargo fuzz`: + +``` +cargo install cargo-fuzz +``` + +## Running the fuzzers + +e.g. To run the `block` fuzzer using all available CPUs: + +``` +cargo fuzz run block -j `nproc` +``` + +## Adding a new fuzzer + +``` +cargo fuzz add +``` + +Inspiration for fuzzers can be found in [crosvm](https://chromium.googlesource.com/chromiumos/platform/crosvm/+/refs/heads/master/fuzz/) diff --git a/vendor/cube-hypervisor/docs/gdb.md b/vendor/cube-hypervisor/docs/gdb.md new file mode 100644 index 0000000..10b75c9 --- /dev/null +++ b/vendor/cube-hypervisor/docs/gdb.md @@ -0,0 +1,47 @@ +# GDB Support + +This feature allows remote guest debugging using GDB. Note that this feature is only supported on x86_64/KVM. + +To enable debugging with GDB, build with the `guest_debug` feature enabled: + +```bash +cargo build --features guest_debug +``` + +To use the `--gdb` option, specify the Unix Domain Socket with `--path` that Cloud Hypervisor will use to communicate with the host's GDB: + +```bash +./cloud-hypervisor \ + --kernel hypervisor-fw \ + --disk path=bionic-server-cloudimg-amd64.raw \ + --cpus boot=1 \ + --memory size=1024M \ + --net "tap=,mac=,ip=,mask=" \ + --console off \ + --serial tty \ + --gdb path=/tmp/ch-gdb-sock +``` + +Cloud Hypervisor will listen for GDB on the host side before starting the guest. +On the host side, connect to the GDB remote server as follows: + +```bash +gdb -q +(gdb) target remote /tmp/ch-gdb-sock +Remote debugging using /tmp/ch-gdb-sock +warning: No executable has been specified, and target does not support +determining executable automatically. Try using the "file" command. +0x000000000011217e in ?? () +``` + +You can set up to four hardware breakpoints using the x86 debug register: + +```bash +(gdb) hb *0x1121b7 +Hardware assisted breakpoint 1 at 0x1121b7 +(gdb) c +Continuing. + +Breakpoint 1, 0x00000000001121b7 in ?? () +(gdb) +``` diff --git a/vendor/cube-hypervisor/docs/hotplug.md b/vendor/cube-hypervisor/docs/hotplug.md new file mode 100644 index 0000000..63906b0 --- /dev/null +++ b/vendor/cube-hypervisor/docs/hotplug.md @@ -0,0 +1,250 @@ +# Cloud Hypervisor Hot Plug + +Currently Cloud Hypervisor supports hot plugging of CPUs devices (x86 only), PCI devices and memory resizing. + +## Kernel support + +For hotplug on Cloud Hypervisor ACPI GED support is needed. This can either be achieved by turning on `CONFIG_ACPI_REDUCED_HARDWARE_ONLY` +or by using this kernel patch (available in 5.5-rc1 and later): https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/patch/drivers/acpi/Makefile?id=ac36d37e943635fc072e9d4f47e40a48fbcdb3f0 + +## CPU Hot Plug + +Extra vCPUs can be added and removed from a running `cloud-hypervisor` instance. This is controlled by two mechanisms: + +1. Specifying a number of maximum potential vCPUs that is greater than the number of default (boot) vCPUs. +2. Making a HTTP API request to the VMM to ask for the additional vCPUs to be added. + +To use CPU hotplug start the VM with the number of max vCPUs greater than the number of boot vCPUs, e.g. + +```shell +$ pushd $CLOUDH +$ sudo setcap cap_net_admin+ep ./cloud-hypervisor/target/release/cloud-hypervisor +$ ./cloud-hypervisor/target/release/cloud-hypervisor \ + --kernel custom-vmlinux.bin \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \ + --disk path=focal-server-cloudimg-amd64.raw \ + --cpus boot=4,max=8 \ + --memory size=1024M \ + --net "tap=,mac=,ip=,mask=" \ + --rng \ + --api-socket=/tmp/ch-socket +$ popd +``` + +Notice the addition of `--api-socket=/tmp/ch-socket` and a `max` parameter on `--cpus boot=4,max=8`. + +To ask the VMM to add additional vCPUs then use the resize API: + +```shell +./ch-remote --api-socket=/tmp/ch-socket resize --cpus 8 +``` + +The extra vCPU threads will be created and advertised to the running kernel. The kernel does not bring up the CPUs immediately and instead the user must "online" them from inside the VM: + +```shell +root@ch-guest ~ # lscpu | grep list: +On-line CPU(s) list: 0-3 +Off-line CPU(s) list: 4-7 +root@ch-guest ~ # echo 1 | tee /sys/devices/system/cpu/cpu[4,5,6,7]/online +1 +root@ch-guest ~ # lscpu | grep list: +On-line CPU(s) list: 0-7 +``` + +After a reboot the added CPUs will remain. + +Removing CPUs works similarly by reducing the number in the "desired_vcpus" field of the reisze API. The CPUs will be automatically offlined inside the guest so there is no need to run any commands inside the guest: + +```shell +./ch-remote --api-socket=/tmp/ch-socket resize --cpus 2 +``` + +As per adding CPUs to the guest, after a reboot the VM will be running with the reduced number of vCPUs. + +## Memory Hot Plug + +### ACPI method + +Extra memory can be added from a running `cloud-hypervisor` instance. This is controlled by two mechanisms: + +1. Allocating some of the guest physical address space for hotplug memory. +2. Making a HTTP API request to the VMM to ask for a new amount of RAM to be assigned to the VM. In the case of expanding the memory for the VM the new memory will be hotplugged into the running VM, if reducing the size of the memory then change will take effect after the next reboot. + +To use memory hotplug start the VM specifying some size RAM in the `hotplug_size` parameter to the memory configuration. Not all the memory specified in this parameter will be available to hotplug as there are spacing and alignment requirements so it is recommended to make it larger than the hotplug RAM needed. + +Because the ACPI method is the default, there is no need to add the extra option `hotplug_method=acpi`. + +```shell +$ pushd $CLOUDH +$ sudo setcap cap_net_admin+ep ./cloud-hypervisor/target/release/cloud-hypervisor +$ ./cloud-hypervisor/target/release/cloud-hypervisor \ + --kernel custom-vmlinux.bin \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \ + --disk path=focal-server-cloudimg-amd64.raw \ + --cpus boot=4,max=8 \ + --memory size=1024M,hotplug_size=8192M \ + --net "tap=,mac=,ip=,mask=" \ + --rng \ + --api-socket=/tmp/ch-socket +$ popd +``` + +Before issuing the API request it is necessary to run the following command inside the VM to make it automatically online the added memory: + +```shell +root@ch-guest ~ # echo online | sudo tee /sys/devices/system/memory/auto_online_blocks +``` + +To ask the VMM to expand the RAM for the VM: + +```shell +./ch-remote --api-socket=/tmp/ch-socket resize --memory 3G +``` + +The new memory is now available to use inside the VM: + +```shell +free -h + total used free shared buff/cache available +Mem: 3.0Gi 71Mi 2.8Gi 0.0Ki 47Mi 2.8Gi +Swap: 32Mi 0B 32Mi +``` + +Due to guest OS limitations is is necessary to ensure that amount of memory added (between currently assigned RAM and that which is desired) is a multiple of 128MiB. + +The same API can also be used to reduce the desired RAM for a VM but the change will not be applied until the VM is rebooted. + +Memory and CPU resizing can be combined together into the same HTTP API request. + +### virtio-mem method + +Extra memory can be added and removed from a running Cloud Hypervisor instance. This is controlled by two mechanisms: + +1. Allocating some of the guest physical address space for hotplug memory. +2. Making a HTTP API request to the VMM to ask for a new amount of RAM to be assigned to the VM. + +To use memory hotplug start the VM specifying some size RAM in the `hotplug_size` parameter along with `hotplug_method=virtio-mem` to the memory configuration. + +```shell +$ pushd $CLOUDH +$ sudo setcap cap_net_admin+ep ./cloud-hypervisor/target/release/cloud-hypervisor +$ ./cloud-hypervisor/target/release/cloud-hypervisor \ + --kernel custom-vmlinux.bin \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \ + --disk path=focal-server-cloudimg-amd64.raw \ + --memory size=1024M,hotplug_size=8192M,hotplug_method=virtio-mem \ + --net "tap=,mac=,ip=,mask=" \ + --api-socket=/tmp/ch-socket +$ popd +``` + +To ask the VMM to expand the RAM for the VM (request is in bytes): + +```shell +./ch-remote --api-socket=/tmp/ch-socket resize --memory 3G +``` + +The new memory is now available to use inside the VM: + +```shell +free -h + total used free shared buff/cache available +Mem: 3.0Gi 71Mi 2.8Gi 0.0Ki 47Mi 2.8Gi +Swap: 32Mi 0B 32Mi +``` + +The same API can also be used to reduce the desired RAM for a VM. It is important to note that reducing RAM size might only partially work, as the guest might be using some of it. + +## PCI Device Hot Plug + +Extra PCI devices can be added and removed from a running `cloud-hypervisor` instance. This is controlled by making a HTTP API request to the VMM to ask for the additional device to be added, or for the existing device to be removed. + +Note: On AArch64 platform, PCI device hotplug can only be achieved using ACPI. Please refer to the [documentation](arm64.md#uefi-booting) for more information. + +To use PCI device hotplug start the VM with the HTTP server. + +```shell +$ sudo setcap cap_net_admin+ep ./cloud-hypervisor/target/release/cloud-hypervisor +$ ./cloud-hypervisor/target/release/cloud-hypervisor \ + --kernel custom-vmlinux.bin \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \ + --disk path=focal-server-cloudimg-amd64.raw \ + --cpus boot=4 \ + --memory size=1024M \ + --net "tap=,mac=,ip=,mask=" \ + --api-socket=/tmp/ch-socket +``` + +Notice the addition of `--api-socket=/tmp/ch-socket`. + +### Add VFIO Device + +To ask the VMM to add additional VFIO device then use the `add-device` API. + +```shell +./ch-remote --api-socket=/tmp/ch-socket add-device path=/sys/bus/pci/devices/0000:01:00.0/ +``` + +### Add Disk Device + +To ask the VMM to add additional disk device then use the `add-disk` API. + +```shell +./ch-remote --api-socket=/tmp/ch-socket add-disk path=/foo/bar/cloud.img +``` + +### Add Fs Device + +To ask the VMM to add additional fs device then use the `add-fs` API. + +```shell +./ch-remote --api-socket=/tmp/ch-socket add-fs tag=myfs,socket=/foo/bar/virtiofs.sock +``` + +### Add Net Device + +To ask the VMM to add additional network device then use the `add-net` API. + +```shell +./ch-remote --api-socket=/tmp/ch-socket add-net tap=chtap0 +``` + +### Add Pmem Device + +To ask the VMM to add additional PMEM device then use the `add-pmem` API. + +```shell +./ch-remote --api-socket=/tmp/ch-socket add-pmem file=/foo/bar.cloud.img +``` + +### Add Vsock Device + +To ask the VMM to add additional vsock device then use the `add-vsock` API. + +```shell +./ch-remote --api-socket=/tmp/ch-socket add-vsock cid=3,socket=/foo/bar/vsock.sock +``` + +### Common Across All PCI Devices + +The extra PCI device will be created and advertised to the running kernel. The new device can be found by checking the list of PCI devices. + +```shell +root@ch-guest ~ # lspci +00:00.0 Host bridge: Intel Corporation Device 0d57 +00:01.0 Unassigned class [ffff]: Red Hat, Inc. Virtio console (rev 01) +00:02.0 Mass storage controller: Red Hat, Inc. Virtio block device (rev 01) +00:03.0 Unassigned class [ffff]: Red Hat, Inc. Virtio RNG (rev 01) +``` + +After a reboot the added PCI device will remain. + +### Remove PCI device + +Removing a PCI device works the same way for all kind of PCI devices. The unique identifier related to the device must be provided. This identifier can be provided by the user when adding the new device, or by default Cloud Hypervisor will assign one. + +```shell +./ch-remote --api-socket=/tmp/ch-socket remove-device _disk0 +``` + +As per adding a PCI device to the guest, after a reboot the VM will be running without the removed PCI device. diff --git a/vendor/cube-hypervisor/docs/intel_sgx.md b/vendor/cube-hypervisor/docs/intel_sgx.md new file mode 100644 index 0000000..69a3925 --- /dev/null +++ b/vendor/cube-hypervisor/docs/intel_sgx.md @@ -0,0 +1,55 @@ +# Intel SGX + +Intel® Software Guard Extensions (Intel® SGX) is an Intel technology designed +to increase the security of application code and data. Cloud Hypervisor supports +SGX virtualization through KVM. Because SGX is built on hardware features that +cannot be emulated in software, virtualizing SGX requires support in KVM and in +the host kernel. The required Linux and KVM changes can be found in the +[KVM SGX Tree](https://github.com/intel/kvm-sgx). + +Utilizing SGX in the guest requires a kernel/OS with SGX support, e.g. a kernel +built using the [SGX Linux Development Tree](https://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-sgx.git) +or the [KVM SGX Tree](https://github.com/intel/kvm-sgx). Running KVM SGX as the +guest kernel allows nested virtualization of SGX. + +For more information about SGX, please refer to the [SGX Homepage](https://software.intel.com/sgx). + +For more information about SGX SDK and how to test SGX, please refer to the +following [instructions](https://github.com/intel/linux-sgx). + +## Cloud Hypervisor support + +Assuming the host exposes `/dev/sgx_vepc`, we can pass SGX enclaves through +the guest. + +In order to use SGX enclaves within a Cloud Hypervisor VM, we must define one +or several Enclave Page Cache (EPC) sections. Here is an example of a VM being +created with 2 EPC sections, the first one being 64MiB with pre-allocated +memory, the second one being 32MiB with no pre-allocated memory. + +```bash +./cloud-hypervisor \ + --cpus boot=1 \ + --memory size=1G \ + --disk path=focal-server-cloudimg-amd64.raw \ + --kernel vmlinux \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \ + --sgx-epc id=epc0,size=64M,prefault=on id=epc1,size=32M,prefault=off +``` + +Once booted, and assuming your guest kernel contains the patches from the +[KVM SGX Tree](https://github.com/intel/kvm-sgx), you can validate SGX devices +have been correctly created under `/dev/sgx`: + +```bash +ls /dev/sgx* +/dev/sgx_enclave /dev/sgx_provision /dev/sgx_vepc +``` + +From this point, it is possible to run any SGX application from the guest, as +it will access `/dev/sgx_enclave` device to create dedicated SGX enclaves. + +Note: There is only one contiguous SGX EPC region, which contains all SGX EPC +sections. This region is exposed through ACPI and marked as reserved through +the e820 table. It is treated as yet another device, which means it should +appear at the end of the guest address space. diff --git a/vendor/cube-hypervisor/docs/intel_tdx.md b/vendor/cube-hypervisor/docs/intel_tdx.md new file mode 100644 index 0000000..1052d89 --- /dev/null +++ b/vendor/cube-hypervisor/docs/intel_tdx.md @@ -0,0 +1,123 @@ +# Intel TDX + +Intel® Trust Domain Extensions (Intel® TDX) is an Intel technology designed to +isolate virtual machines from the VMM, hypervisor and any other software on the +host platform. + +For more information about TDX technical aspects, design and specification +please refer to the +[TDX Homepage](https://www.intel.com/content/www/us/en/developer/articles/technical/intel-trust-domain-extensions.html). + +The required Linux changes for the host side can be found in the +[KVM TDX tree](https://github.com/intel/tdx/tree/kvm) while the changes for +the guest side can be found in the [Guest TDX tree](https://github.com/intel/tdx/tree/guest). + +The TDVF firmware can be found in the +[EDK2 staging project](https://github.com/tianocore/edk2-staging/tree/TDVF). + +The TDShim firmware can be found in the +[Confidential Containers project](https://github.com/confidential-containers/td-shim). + +## Cloud Hypervisor support + +First, you must be running on a machine with TDX enabled in hardware, and +with the host OS compiled from the [KVM TDX tree](https://github.com/intel/tdx/tree/kvm). + +Cloud Hypervisor can run TDX VM (Trust Domain) by loading a TD firmware, +which will then load the guest kernel from the image. The image must be custom +as it must include a kernel built from the [Guest TDX tree](https://github.com/intel/tdx/tree/guest). + +### TDVF + +The firmware can be built as follows: + +```bash +git clone https://github.com/tianocore/edk2-staging.git +cd edk2-staging +git checkout origin/TDVF +git submodule update --init --recursive +make -C BaseTools +source ./edksetup.sh +build -p OvmfPkg/OvmfCh.dsc -a X64 -t GCC5 -b RELEASE +``` + +If debug logs are needed, here is the alternative command: + +```bash +build -p OvmfPkg/OvmfCh.dsc -a X64 -t GCC5 -D DEBUG_ON_SERIAL_PORT=TRUE +``` + +On the Cloud Hypervisor side, all you need is to build the project with the +`tdx` feature enabled: + +```bash +cargo build --features tdx +``` + +And run a TDX VM by providing the firmware previously built, along with the +guest image containing the TDX enlightened kernel. The latest image +`td-guest-rhel8.5.raw` contains `console=hvc0` on the kernel boot parameters, +meaning it will be printing guest kernel logs to the `virtio-console` device. + +```bash +./cloud-hypervisor \ + --platform tdx=on + --firmware edk2-staging/Build/OvmfCh/RELEASE_GCC5/FV/OVMF.fd \ + --cpus boot=1 \ + --memory size=1G \ + --disk path=tdx_guest_img +``` + +And here is the alternative command when looking for debug logs from the +firmware: + +```bash +./cloud-hypervisor \ + --platform tdx=on + --firmware edk2-staging/Build/OvmfCh/DEBUG_GCC5/FV/OVMF.fd \ + --cpus boot=1 \ + --memory size=1G \ + --disk path=tdx_guest_img \ + --serial file=/tmp/ch_serial \ + --console tty +``` + +### TDShim + +This is a lightweight version of the TDVF, written in Rust and designed for +direct kernel boot, which is useful for containers use cases. + +You can find the instructions for building the firmware directly from the +project [documentation](https://github.com/confidential-containers/td-shim/tree/staging#how-to-build). + +And run a TDX VM by providing the firmware previously built, along with a guest +kernel built from the [Guest TDX tree](https://github.com/intel/tdx/tree/guest). +The appropriate kernel boot options must be provided through the `--cmdline` +option as well. + +```bash +./cloud-hypervisor \ + --platform tdx=on + --firmware tdshim \ + --kernel bzImage \ + --cmdline "root=/dev/vda3 console=hvc0 rw" + --cpus boot=1 \ + --memory size=1G \ + --disk path=tdx_guest_img +``` + +### Guest kernel limitations + +#### Serial ports disabled + +The latest guest kernel that can be found in the latest image +`td-guest-rhel8.5.raw` disabled the support for serial ports. This means adding +`console=ttyS0` will have no effect and will not print any log from the guest. + +#### PCI hotplug through ACPI + +Unless you run the guest kernel with the parameter `tdx_disable_filter`, ACPI +devices responsible for handling PCI hotplug (PCI hotplug controller, PCI +Express Bus and Generic Event Device) will not be allowed, therefore the +corresponding drivers will not be loaded and the PCI hotplug feature will not +be supported. diff --git a/vendor/cube-hypervisor/docs/io_throttling.md b/vendor/cube-hypervisor/docs/io_throttling.md new file mode 100644 index 0000000..5992939 --- /dev/null +++ b/vendor/cube-hypervisor/docs/io_throttling.md @@ -0,0 +1,44 @@ +# I/O Throttling + +Cloud Hypervisor now supports I/O throttling on virtio-block and virtio-net +devices. This support is based on the [`rate-limiter` module](https://github.com/firecracker-microvm/firecracker/tree/master/src/rate_limiter) +from Firecracker. This document explains the user interface of this +feature, and highlights some internal implementations that can help users +better understand the expected behavior of I/O throttling in practice. + +Cloud Hypervisor allows to limit both the I/O bandwidth (e.g. bytes/s) +and I/O operations (ops/s) independently. For virtio-net devices, while +sharing the same "rate limit" from user inputs (on both bandwidth and +operations), the RX and TX queues are throttled independently. +To limit the I/O bandwidth, Cloud Hypervisor +provides three user options, i.e., `bw_size` (bytes), `bw_one_time_burst` +(bytes), and `bw_refill_time` (ms). Both `bw_size` and `bw_refill_time` +are required, while `bw_one_time_burst` is optional. +Internally, these options define a TokenBucket with a maximum capacity +(`bw_size` bytes), an initial burst size (`bw_one_time_burst`) and an +interval for refilling purposes (`bw_refill_time`). The "refill-rate" is +`bw_size` bytes per `bw_refill_time` ms, and it is the constant rate at +which the tokens replenish. The refill process only starts happening +after the initial burst budget is consumed. Consumption from the token +bucket is unbounded in speed which allows for bursts bound in size by +the amount of tokens available. Once the token bucket is empty, +consumption speed is bound by the "refill-rate". Similarly, Cloud +Hypervisor provides another three options for limiting I/O operations, +i.e., `ops_size` (I/O operations), `ops_one_time_burst` (I/O operations), +and `ops_refill_time` (ms). + +One caveat in the I/O throttling is that every-time the bucket gets +empty, it will stop I/O operations for a fixed amount of time +(`cool_down_time`). The `cool_down_time` now is fixed at `100 ms`, it +can have big implications to the actual rate limit (which can be a lot +different the expected "refill-rate" derived from user inputs). For +example, to have a 1000 IOPS limit on a virtio-blk device, users should +be able to provide either of the following two options: +`ops_size=1000,ops_refill_time=1000` or +`ops_size=10,ops_refill_time=10`. However, the actual IOPS limits are +likely to be ~1000 IOPS and ~100 IOPS respectively. The reason is the +actual rate limit users get can be as low as +`ops_size/(ops_refill_time+cool_down_time)`. As a result, it is +generally advisable to keep `bw/ops_refill_time` larger than `100 ms` +(`cool_down_time`) to make sure the actual rate limit is close to users' +expectation ("refill-rate"). diff --git a/vendor/cube-hypervisor/docs/iommu.md b/vendor/cube-hypervisor/docs/iommu.md new file mode 100644 index 0000000..62c04ad --- /dev/null +++ b/vendor/cube-hypervisor/docs/iommu.md @@ -0,0 +1,268 @@ +# Virtual IOMMU + +## Rationales + +Having the possibility to expose a virtual IOMMU to the guest can be +interesting to support specific use cases. That being said, it is always +important to keep in mind a virtual IOMMU can impact the performance of the +attached devices, which is the reason why one should be careful when enabling +this feature. + +### Protect nested virtual machines + +The first reason why one might want to expose a virtual IOMMU to the guest is +to increase the security regarding the memory accesses performed by the virtual +devices (VIRTIO devices), on behalf of the guest drivers. + +With a virtual IOMMU, the VMM stands between the guest driver and its device +counterpart, validating and translating every address before to try accessing +the guest memory. This is standard interposition that is performed here by the +VMM. + +The increased security does not apply for a simple case where we have one VM +per VMM. Because the guest cannot be trusted, as we always consider it could +be malicious and gain unauthorized privileges inside the VM, preventing some +devices from accessing the entire guest memory is pointless. + +But let's take the interesting case of nested virtualization, and let's assume +we have a VMM running a first layer VM. This L1 guest is fully trusted as the +user intends to run multiple VMs from this L1. We can end up with multiple L2 +VMs running on a single L1 VM. In this particular case, and without exposing a +virtual IOMMU to the L1 guest, it would be possible for any L2 guest to use the +device implementation from the host VMM to access the entire guest L1 memory. +The virtual IOMMU prevents from this kind of trouble as it will validate the +addresses the device is authorized to access. + +### Achieve VFIO nested + +Another reason for having a virtual IOMMU is to allow passing physical devices +from the host through multiple layers of virtualization. Let's take as example +a system with a physical IOMMU running a VM with a virtual IOMMU. The +implementation of the virtual IOMMU is responsible for updating the physical +DMA Remapping table (DMAR) everytime the DMA mapping changes. This must happen +through the VFIO framework on the host as this is the only userspace interface +to interact with a physical IOMMU. + +Relying on this update mechanism, it is possible to attach physical devices to +the virtual IOMMU, which allows these devices to be passed from L1 to another +layer of virtualization. + +## Why virtio-iommu? + +The Cloud Hypervisor project decided to implement the brand new virtio-iommu +device in order to provide a virtual IOMMU to its users. The reason being the +simplicity brought by the paravirtualization solution. By having one side +handled from the guest itself, it removes the complexity of trapping memory +page accesses and shadowing them. This is why the project will not try to +implement a full emulation of a physical IOMMU. + +## Pre-requisites + +### Kernel + +Since virtio-iommu has landed partially into the version 5.3 of the Linux +kernel, a special branch is needed to get things working with Cloud Hypervisor. +By partially, we are talking about x86 specifically, as it is already fully +functional for ARM architectures. + +## Usage + +In order to expose a virtual IOMMU to the guest, it is required to create a +virtio-iommu device and expose it through the ACPI IORT table. This can be +simply achieved by attaching at least one device to the virtual IOMMU. + +The way to expose to the guest a specific device as sitting behind this IOMMU +is to explicitly tag it from the command line with the option `iommu=on`. + +Not all devices support this extra option, and the default value will always +be `off` since we want to avoid the performance impact for most users who don't +need this. + +Refer to the command line `--help` to find out which device support to be +attached to the virtual IOMMU. + +Below is a simple example exposing the `virtio-blk` device as attached to the +virtual IOMMU: + +```bash +./cloud-hypervisor \ + --cpus boot=1 \ + --memory size=512M \ + --disk path=focal-server-cloudimg-amd64.raw,iommu=on \ + --kernel custom-vmlinux \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \ +``` + +From a guest perspective, it is easy to verify if the device is protected by +the virtual IOMMU. Check the directories listed under +`/sys/kernel/iommu_groups`: + +```bash +ls /sys/kernel/iommu_groups +0 +``` + +In this case, only one IOMMU group should be created. Under this group, it is +possible to find out the b/d/f of the device(s) part of this group. + +```bash +ls /sys/kernel/iommu_groups/0/devices/ +0000:00:03.0 +``` + +And you can validate the device is the one we expect running `lspci`: + +```bash +lspci +00:00.0 Host bridge: Intel Corporation Device 0d57 +00:01.0 Unassigned class [ffff]: Red Hat, Inc. Device 1057 +00:02.0 Unassigned class [ffff]: Red Hat, Inc. Virtio console +00:03.0 Mass storage controller: Red Hat, Inc. Virtio block device +00:04.0 Unassigned class [ffff]: Red Hat, Inc. Virtio RNG +``` + +### Work with FDT on AArch64 + +On AArch64 architecture, the virtual IOMMU can still be used even if ACPI is not +enabled. But the effect is different with what the aforementioned test showed. + +When ACPI is disabled, virtual IOMMU is supported through Flattened Device Tree +(FDT). In this case, the guest kernel can not tell which device should be +IOMMU-attached and which should not. No matter how many devices you attached to +the virtual IOMMU by setting `iommu=on` option, all the devices on the PCI bus +will be attached to the virtual IOMMU (except the IOMMU itself). Each of the +devices will be added into a IOMMU group. + +As a result, the directory content of `/sys/kernel/iommu_groups` would be: + +```bash +ls /sys/kernel/iommu_groups/0/devices/ +0000:00:02.0 +ls /sys/kernel/iommu_groups/1/devices/ +0000:00:03.0 +ls /sys/kernel/iommu_groups/2/devices/ +0000:00:04.0 +``` + +## Faster mappings + +By default, the guest memory is mapped with 4k pages and no huge pages, which +causes the virtual IOMMU device to be asked for 4k mappings only. This +configuration slows down the setup of the physical IOMMU as an important number +of requests need to be issued in order to create large mappings. + +One use case is even more impacted by the slowdown, the nested VFIO case. When +passing a device through a L2 guest, the VFIO driver running in L1 will update +the DMAR entries for the specific device. Because VFIO pins the entire guest +memory, this means the entire mapping of the L2 guest need to be stored into +multiple 4k mappings. Obviously, the bigger the L2 guest RAM is, the longer the +update of the mappings will last. There is an additional problem happening in +this case, if the L2 guest RAM is quite large, it will require a large number +of mappings, which might exceed the VFIO limit set on the host. The default +value is 65536, which can simply be reached with a 256MiB sized RAM. + +The way to solve both problems, the slowdown and the limit being exceeded, is +to reduce the amount of requests to describe those same large mappings. This +can be achieved by using 2MiB pages, known as huge pages. By seeing the guest +RAM as larger pages, and because the virtual IOMMU device supports it, the +guest will require less mappings, which will prevent the limit from being +exceeded, but also will take less time to process them on the host. That's +how using huge pages as much as possible can speed up VM boot time. + +### Basic usage + +Let's look at an example of how to run a guest with huge pages. + +First, make sure your system has enough pages to cover the entire guest RAM: +```bash +# This example creates 4096 hugepages +echo 4096 > /proc/sys/vm/nr_hugepages +``` + +Next step is simply to create the VM. Two things are important, first we want +the VM RAM to be mapped on huge pages by backing it with `/dev/hugepages`. And +second thing, we need to create some huge pages in the guest itself so they can +be consumed. + +```bash +./cloud-hypervisor \ + --cpus boot=1 \ + --memory size=8G,hugepages=on \ + --disk path=focal-server-cloudimg-amd64.raw \ + --kernel custom-vmlinux \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw hugepagesz=2M hugepages=2048" \ + --net tap=,mac=,iommu=on +``` + +### Nested usage + +Let's now look at the specific example of nested virtualization. In order to +reach optimized performances, the L2 guest also need to be mapped based on +huge pages. Here is how to achieve this, assuming the physical device you are +passing through is `0000:00:01.0`. + +```bash +./cloud-hypervisor \ + --cpus boot=1 \ + --memory size=8G,hugepages=on \ + --disk path=focal-server-cloudimg-amd64.raw \ + --kernel custom-vmlinux \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw kvm-intel.nested=1 vfio_iommu_type1.allow_unsafe_interrupts rw hugepagesz=2M hugepages=2048" \ + --device path=/sys/bus/pci/devices/0000:00:01.0,iommu=on +``` + +Once the L1 VM is running, unbind the device from the default driver in the +guest, and bind it to VFIO (it should appear as `0000:00:04.0`). + +```bash +echo 0000:00:04.0 > /sys/bus/pci/devices/0000\:00\:04.0/driver/unbind +echo 8086 1502 > /sys/bus/pci/drivers/vfio-pci/new_id +echo 0000:00:04.0 > /sys/bus/pci/drivers/vfio-pci/bind +``` + +Last thing is to start the L2 guest with the huge pages memory backend. + +```bash +./cloud-hypervisor \ + --cpus boot=1 \ + --memory size=4G,hugepages=on \ + --disk path=focal-server-cloudimg-amd64.raw \ + --kernel custom-vmlinux \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \ + --device path=/sys/bus/pci/devices/0000:00:04.0 +``` + +### Dedicated IOMMU PCI segments + +To facilitate hotplug of devices that require being behind an IOMMU it is +possible to mark entire PCI segments as behind the IOMMU. + +This is accomplished through `--platform +num_pci_segments=,iommu_segments=` or +via the equivalents in `PlatformConfig` for the API. + +e.g. + +```bash +./cloud-hypervisor \ + --api-socket=/tmp/api \ + --cpus boot=1 \ + --memory size=4G,hugepages=on \ + --disk path=focal-server-cloudimg-amd64.raw \ + --kernel custom-vmlinux \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \ + --platform num_pci_segments=2,iommu_segments=1 +``` + +This adds a second PCI segment to the platform behind the IOMMU. A VFIO device +requiring the IOMMU then may be hotplugged: + +e.g. + +```bash +./ch-remote --api-socket=/tmp/api add-device path=/sys/bus/pci/devices/0000:00:04.0,iommu=on,pci_segment=1 +``` + +Devices that cannot be placed behind an IOMMU (e.g. lacking an `iommu=` option) +cannot be placed on the IOMMU segments. + diff --git a/vendor/cube-hypervisor/docs/live_migration.md b/vendor/cube-hypervisor/docs/live_migration.md new file mode 100644 index 0000000..347cbb7 --- /dev/null +++ b/vendor/cube-hypervisor/docs/live_migration.md @@ -0,0 +1,139 @@ +# Live Migration + +This document gives two examples of how to use the live migration +support in Cloud Hypervisor: + +1. local migration - migrating between two VMs running on the same + machine; +1. nested-vm migration - migrating between two nested VMs whose host VMs + are running on the same machine. + +## Local Migration (Suitable for Live Upgrade of VMM) +Launch the source VM (on the host machine): +```bash +$ target/release/cloud-hypervisor + --kernel ~/workloads/vmlinux \ + --disk path=~/workloads/focal.raw \ + --cpus boot=1 --memory size=1G,shared=on \ + --cmdline "root=/dev/vda1 console=ttyS0" \ + --serial tty --console off --api-socket=/tmp/api1 +``` + +Launch the destination VM from the same directory (on the host machine): +```bash +$ target/release/cloud-hypervisor --api-socket=/tmp/api2 +``` + +Get ready for receiving migration for the destination VM (on the host machine): +```bash +$ target/release/ch-remote --api-socket=/tmp/api2 receive-migration unix:/tmp/sock +``` + +Start to send migration for the source VM (on the host machine): +```bash +$ target/release/ch-remote --api-socket=/tmp/api1 send-migration --local unix:/tmp/sock +``` + +When the above commands completed, the source VM should be successfully +migrated to the destination VM. Now the destination VM is running while +the source VM is terminated gracefully. + +## Nested-VM Migration + +Launch VM 1 (on the host machine) with an extra virtio-blk device for +exposing a guest image for the nested source VM: +> Note: the example below also attached an additional virtio-blk device +> with a dummy image for testing purpose (which is optional). +```bash +$ head -c 1M < /dev/urandom > tmp.img # create a dummy image for testing +$ sudo /target/release/cloud-hypervisor \ + --serial tty --console off \ + --cpus boot=1 --memory size=512M \ + --kernel vmlinux \ + --cmdline "root=/dev/vda1 console=ttyS0" \ + --disk path=focal-1.raw path=focal-nested.raw path=tmp.img\ + --net ip=192.168.101.1 +``` + +Launch VM 2 (on the host machine) with an extra virtio-blk device for +exposing the same guest image for the nested destination VM: +```bash +$ sudo /target/release/cloud-hypervisor \ + --serial tty --console off \ + --cpus boot=1 --memory size=512M \ + --kernel vmlinux \ + --cmdline "root=/dev/vda1 console=ttyS0" \ + --disk path=focal-2.raw path=focal-nested.raw path=tmp.img\ + --net ip=192.168.102.1 +``` + +Launch the nested source VM (inside the guest OS of the VM 1) : +```bash +vm-1:~$ sudo ./cloud-hypervisor \ + --serial tty --console off \ + --memory size=128M \ + --kernel vmlinux \ + --cmdline "console=ttyS0 root=/dev/vda1" \ + --disk path=/dev/vdb path=/dev/vdc \ + --api-socket=/tmp/api1 \ + --net ip=192.168.100.1 +vm-1:~$ # setup the guest network if needed +vm-1:~$ sudo ip addr add 192.168.101.2/24 dev ens4 +vm-1:~$ sudo ip link set up dev ens4 +vm-1:~$ sudo ip r add default via 192.168.101.1 +``` +Optional: Run the guest workload below (on the guest OS of the nested source VM), +which performs intensive virtio-blk operations. Now the console of the nested +source VM should repeatedly print `"equal"`, and our goal is migrating +this VM and the running workload without interruption. +```bash +#/bin/bash + +# On the guest OS of the nested source VM + +input="/dev/vdb" +result=$(md5sum $input) +tmp=$(md5sum $input) + +while [[ "$result" == "$tmp" ]] +do + echo "equal" + tmp=$(md5sum $input) +done + +echo "not equal" +echo "result = $result" +echo "tmp = $tmp" +``` + +Launch the nested destination VM (inside the guest OS of the VM 2): +```bash +vm-2:~$ sudo ./cloud-hypervisor --api-socket=/tmp/api2 +vm-2:~$ # setup the guest network with the following commands if needed +vm-2:~$ sudo ip addr add 192.168.102.2/24 dev ens4 +vm-2:~$ sudo ip link set up dev ens4 +vm-2:~$ sudo ip r add default via 192.168.102.1 +vm-2:~$ ping 192.168.101.2 # This should succeed +``` +> Note: If the above ping failed, please check the iptables rule on the +> host machine, e.g. whether the policy for the `FORWARD` chain is set +> to `DROP` (which is the default setting configured by Docker). + +Get ready for receiving migration for the nested destination VM (inside +the guest OS of the VM 2): +```bash +vm-2:~$ sudo ./ch-remote --api-socket=/tmp/api2 receive-migration unix:/tmp/sock2 +vm-2:~$ sudo socat TCP-LISTEN:6000,reuseaddr UNIX-CLIENT:/tmp/sock2 +``` + +Start to send migration for the nested source VM (inside the guest OS of +the VM 1): +```bash +vm-1:~$ sudo socat UNIX-LISTEN:/tmp/sock1,reuseaddr TCP:192.168.102.2:6000 +vm-1:~$ sudo ./ch-remote --api-socket=/tmp/api1 send-migration unix:/tmp/sock1 +``` + +When the above commands completed, the source VM should be successfully +migrated to the destination VM without interrupting our testing guest +workload. Now the destination VM is running the testing guest workload +while the source VM is terminated gracefully. diff --git a/vendor/cube-hypervisor/docs/logging.md b/vendor/cube-hypervisor/docs/logging.md new file mode 100644 index 0000000..f39f6b8 --- /dev/null +++ b/vendor/cube-hypervisor/docs/logging.md @@ -0,0 +1,43 @@ +# Logging + +The target audience of this document is both: + +* Developers who want to understand what log level to use and when, +* Users who want to debug issues with running their workloads in Cloud Hypervisor + +## Control + +The number of `-v` parameters passed to the `cloud-hypervisor` binary will determine the log level. Currenly the default is log messages up to `WARN:` (`warn!`) are included by default. The `--log-file` allows the log to be sent to a location other than `stderr`. + +## Levels + +### `error!()` + +For immediate, unrecoverable errors where it does not make sense for the execution to continue as the behaviour of the VM is considerablely impacted. + +Cloud Hypervisor should exit shortly after reporting this error (with a non-zero exit code). Generally this should be used during initial construction of the VM state before the virtual CPUs have begun running code. + +A typical situation where this might occur is when the user is using command line options that conflict with each other or is trying to use a file that is not present on the filesystem. + +Users should react to this error by checking their initial VM configuration. + +### `warn!()` + +A serious problem has occurred but the execution of the VM can continue although some functionality might be impacted. + +A typical example of where this level of message should be generated is during an API call request that cannot be fulfilled. + +The user should investigate the meaning of this warning and take steps to ensure the correct functionality. + + +### `info!()` + +Use `-v` to enable. + +This level is for the benefit of developers. It should be used for sporadic and infrequent messages. The same message should not "spam" the logs. The VM should be usable when this level of debugging is enabled and trying to use `stdin/stdout` and the logs are going to `stderr`. + +### `debug!()` + +Use `-vv` to enable. + +For the most verbose of logging messages. It is acceptable to "spam" the log with repeated invocations of the same message. This level of logging would be combined with `--log-file`. diff --git a/vendor/cube-hypervisor/docs/macvtap-bridge.md b/vendor/cube-hypervisor/docs/macvtap-bridge.md new file mode 100644 index 0000000..75173ee --- /dev/null +++ b/vendor/cube-hypervisor/docs/macvtap-bridge.md @@ -0,0 +1,32 @@ +# Using MACVTAP to Bridge onto Host Network + +Cloud Hypervisor supports using a MACVTAP device which is derived from a MACVLAN. Full details of configuring MACVLAN or MACVTAP is out of scope of this document. However the example below indicates how to bridge the guest directly onto the network the host is on. Due to the lack of hairpin mode it not usually possible to reach the guest directly from the host. + +```bash +# The MAC address must be attached to the macvtap and be used inside the guest +mac="c2:67:4f:53:29:cb" +# Host network adapter to bridge the guest onto +host_net="eno1" + +# Create the macvtap0 as a new virtual MAC associated with the host network +sudo ip link add link "$host_net" name macvtap0 type macvtap +sudo ip link set macvtap0 address "$mac" up +sudo ip link show macvtap0 + +# A new character device is created for this interface +tapindex=$(< /sys/class/net/macvtap0/ifindex) +tapdevice="/dev/tap$tapindex" + +# Ensure that we can access this device +sudo chown "$UID.$UID" "$tapdevice" + +# Use --net fd=3 to point to fd 3 which the shell has opened to point to the /dev/tapN device +target/debug/cloud-hypervisor \ + --kernel ~/src/linux/vmlinux \ + --disk path=~/workloads/focal.raw \ + --cpus boot=1 --memory size=512M \ + --cmdline "root=/dev/vda1 console=hvc0" \ + --net fd=3,mac=$mac 3<>$"$tapdevice" +``` + +As the guest is now connected to the same L2 network as the host you can obtain an IP address based on your host network (potentially including via DHCP) diff --git a/vendor/cube-hypervisor/docs/memory.md b/vendor/cube-hypervisor/docs/memory.md new file mode 100644 index 0000000..dcae83a --- /dev/null +++ b/vendor/cube-hypervisor/docs/memory.md @@ -0,0 +1,581 @@ +# Memory + +Cloud Hypervisor has many ways to expose memory to the guest VM. This document +aims to explain what Cloud Hypervisor is capable of and how it can be used to +meet the needs of very different use cases. + +## Basic Parameters + +`MemoryConfig` or what is known as `--memory` from the CLI perspective is the +easiest way to get started with Cloud Hypervisor. + +```rust +struct MemoryConfig { + size: u64, + mergeable: bool, + hotplug_method: HotplugMethod, + hotplug_size: Option, + hotplugged_size: Option, + shared: bool, + hugepages: bool, + hugepage_size: Option, + prefault: bool, + thp: bool + zones: Option>, +} +``` + +``` +--memory Memory parameters "size=,mergeable=on|off,shared=on|off,hugepages=on|off,hugepage_size=,hotplug_method=acpi|virtio-mem,hotplug_size=,hotplugged_size=,prefault=on|off,thp=on|off" [default: size=512M,thp=on] +``` + +### `size` + +Size of the RAM in the guest VM. + +This option is mandatory when using the `--memory` parameter. + +Value is an unsigned integer of 64 bits. + +_Example_ + +``` +--memory size=1G +``` + +### `mergeable` + +Specifies if the pages from the guest RAM must be marked as _mergeable_. In +case this option is `true` or `on`, the pages will be marked with `madvise(2)` +to let the host kernel know which pages are eligible for being merged by the +KSM daemon. + +This option can be used when trying to reach a higher density of VMs running +on a single host, as it will reduce the amount of memory consumed by each VM. + +By default this option is turned off. + +_Example_ + +``` +--memory size=1G,mergeable=on +``` + +### `hotplug_method` + +Selects the way of adding and/or removing memory to/from a booted VM. + +Possible values are `acpi` and `virtio-mem`. Default value is `acpi`. + +_Example_ + +``` +--memory size=1G,hotplug_method=acpi +``` + +### `hotplug_size` + +Amount of memory that can be dynamically added to the VM. + +Value is an unsigned integer of 64 bits. A value of 0 is invalid. + +_Example_ + +``` +--memory size=1G,hotplug_size=1G +``` + +### `hotplugged_size` + +Amount of memory that will be dynamically added to the VM at boot. This option +allows for starting a VM with a certain amount of memory that can be reduced +during runtime. + +This is only valid when the `hotplug_method` is `virtio-mem` as it does not +make sense for the `acpi` use case. When using ACPI, the memory can't be +resized after it has been extended. + +This option is only valid when `hotplug_size` is specified, and its value can't +exceed the value of `hotplug_size`. + +Value is an unsigned integer of 64 bits. A value of 0 is invalid. + +_Example_ + +``` +--memory size=1G,hotplug_method=virtio-mem,hotplug_size=1G,hotplugged_size=512M +``` + +### `shared` + +Specifies if the memory must be `mmap(2)` with `MAP_SHARED` flag. + +By sharing a memory mapping, one can share the guest RAM with other processes +running on the host. One can use this option when running vhost-user devices +as part of the VM device model, as they will be driven by standalone daemons +needing access to the guest RAM content. + +By default this option is turned off, which results in performing `mmap(2)` +with `MAP_PRIVATE` flag. + +If `hugepages=on` then the value of this field is ignored as huge pages always +requires `MAP_SHARED`. + +_Example_ + +``` +--memory size=1G,shared=on +``` + +### `hugepages` and `hugepage_size` + +Specifies if the memory must be created and `mmap(2)` with `MAP_HUGETLB` and size +flags. This performs a memory mapping relying on the specified huge page size. +If no huge page size is supplied the system's default huge page size is used. + +By using hugepages, one can improve the overall performance of the VM, assuming +the guest will allocate hugepages as well. Another interesting use case is VFIO +as it speeds up the VM's boot time since the amount of IOMMU mappings are +reduced. + +The user is responsible for ensuring there are sufficient huge pages of the +specified size for the VMM to use. Failure to do so may result in strange VMM +behaviour, e.g. error with `ReadKernelImage` is common. If there is a strange +error with `hugepages` enabled, just disable it or check whether there are enough +huge pages. + +If `hugepages=on` then the value of `shared` is ignored as huge pages always +requires `MAP_SHARED`. + +By default this option is turned off. + +_Example_ + +``` +--memory size=1G,hugepages=on,hugepage_size=2M +``` + +### `prefault` + +Specifies if the memory must be `mmap(2)` with `MAP_POPULATE` flag. + +By triggering prefault, one can allocate all required physical memory and create +its page tables while calling `mmap`. With physical memory allocated, the number +of page faults will decrease during running, and performance will also improve. + +Note that boot of VM will be slower with `prefault` enabled because of allocating +physical memory and creating page tables in advance, and physical memory of the +specified size will be consumed quickly. + +This option only takes effect at boot of VM. There is also a `prefault` option in +restore and its choice will overwrite `prefault` in memory. + +By default this option is turned off. + +_Example_ + +``` +--memory size=1G,prefault=on +``` + +### `thp` + +Specifies if private anonymous memory for the guest (i.e. `shared=off` and no +backing file) should be labelled `MADV_HUGEPAGE` with `madvise(2)` indicating +to the kernel that this memory may be backed with huge pages transparently. + +The use of transparent huge pages can improve the performance of the guest as +there will fewer virtualisation related page faults. Unlike using +`hugepages=on` a specific number of huge pages do not need to be allocated by +the kernel. + +By default this option is turned on. + +_Example_ + +``` +--memory size=1G,thp=on +``` + +## Advanced Parameters + +`MemoryZoneConfig` or what is known as `--memory-zone` from the CLI perspective +is a power user parameter. It allows for a full description of the guest RAM, +describing how every memory region is backed and exposed to the guest. + +```rust +struct MemoryZoneConfig { + id: String, + size: u64, + file: Option, + shared: bool, + hugepages: bool, + hugepage_size: Option, + host_numa_node: Option, + hotplug_size: Option, + hotplugged_size: Option, + prefault: bool, +} +``` + +``` +--memory-zone User defined memory zone parameters "size=,file=,shared=on|off,hugepages=on|off,hugepage_size=,host_numa_node=,id=,hotplug_size=,hotplugged_size=,prefault=on|off" +``` + +This parameter expects one or more occurences, allowing for a list of memory +zones to be defined. It must be used with `--memory size=0`, clearly indicating +that the memory will be described through advanced parameters. + +Each zone is given a list of options which we detail through the following +sections. + +### `id` + +Memory zone identifier. This identifier must be unique, otherwise an error will +be returned. + +This option is useful when referring to a memory zone previously created. In +particular, the `--numa` parameter can associate a memory zone to a specific +NUMA node based on the memory zone identifier. + +This option is mandatory when using the `--memory-zone` parameter. + +Value is a string. + +_Example_ + +``` +--memory size=0 +--memory-zone id=mem0,size=1G +``` + +### `size` + +Size of the memory zone. + +This option is mandatory when using the `--memory-zone` parameter. + +Value is an unsigned integer of 64 bits. + +_Example_ + +``` +--memory size=0 +--memory-zone id=mem0,size=1G +``` + +### `file` + +Path to the file backing the memory zone. This can be either a file or a +directory. In case of a file, it will be opened and used as the backing file +for the `mmap(2)` operation. In case of a directory, a temporary file with no +hard link on the filesystem will be created. This file will be used as the +backing file for the `mmap(2)` operation. + +This option can be particularly useful when trying to back a part of the guest +RAM with a well known file. In the context of the snapshot/restore feature, and +if the provided path is a file, the snapshot operation will not perform any +copy of the guest RAM content for this specific memory zone since the user has +access to it and it would duplicate data already stored on the current +filesystem. + +Value is a string. + +_Example_ + +``` +--memory size=0 +--memory-zone id=mem0,size=1G,file=/foo/bar +``` + +### `shared` + +Specifies if the memory zone must be `mmap(2)` with `MAP_SHARED` flag. + +By sharing a memory zone mapping, one can share part of the guest RAM with +other processes running on the host. One can use this option when running +vhost-user devices as part of the VM device model, as they will be driven +by standalone daemons needing access to the guest RAM content. + +If `hugepages=on` then the value of this field is ignored as huge pages always +requires `MAP_SHARED`. + +By default this option is turned off, which result in performing `mmap(2)` +with `MAP_PRIVATE` flag. + +_Example_ + +``` +--memory size=0 +--memory-zone id=mem0,size=1G,shared=on +``` + +### `hugepages` and `hugepage_size` + +Specifies if the memory must be created and `mmap(2)` with `MAP_HUGETLB` and size +flags. This performs a memory mapping relying on the specified huge page size. +If no huge page size is supplied the system's default huge page size is used. + +By using hugepages, one can improve the overall performance of the VM, assuming +the guest will allocate hugepages as well. Another interesting use case is VFIO +as it speeds up the VM's boot time since the amount of IOMMU mappings are +reduced. + +The user is responsible for ensuring there are sufficient huge pages of the +specified size for the VMM to use. Failure to do so may result in strange VMM +behaviour, e.g. error with `ReadKernelImage` is common. If there is a strange +error with `hugepages` enabled, just disable it or check whether there are enough +huge pages. + +If `hugepages=on` then the value of `shared` is ignored as huge pages always +requires `MAP_SHARED`. + +By default this option is turned off. + +_Example_ + +``` +--memory size=0 +--memory-zone id=mem0,size=1G,hugepages=on,hugepage_size=2M +``` + +### `host_numa_node` + +Node identifier of a node present on the host. This option will let the user +pick a specific NUMA node from which the memory must be allocated. After the +memory zone is `mmap(2)`, the NUMA policy for this memory mapping will be +applied through `mbind(2)`, relying on the provided node identifier. If the +node does not exist on the host, the call to `mbind(2)` will fail. + +This option is useful when trying to back a VM memory with a specific type of +memory from the host. Assuming a host has two types of memory, with one slower +than the other, each related to a distinct NUMA node, one could create a VM +with slower memory accesses by backing the entire guest RAM from the furthest +NUMA node on the host. + +This option also gives the opportunity to create a VM with non uniform memory +accesses as one could define a first memory zone backed by fast memory, and a +second memory zone backed by slow memory. + +Value is an unsigned integer of 32 bits. + +_Example_ + +``` +--memory size=0 +--memory-zone id=mem0,size=1G,host_numa_node=0 +``` + +### `hotplug_size` + +Amount of memory that can be dynamically added to the memory zone. Since +`virtio-mem` is the only way of resizing a memory zone, one must specify +the `hotplug_method=virtio-mem` to the `--memory` parameter. + +Value is an unsigned integer of 64 bits. A value of 0 is invalid. + +_Example_ + +``` +--memory size=0,hotplug_method=virtio-mem +--memory-zone id=mem0,size=1G,hotplug_size=1G +``` + +### `hotplugged_size` + +Amount of memory that will be dynamically added to a memory zone at VM's boot. +This option allows for starting a VM with a certain amount of memory that can +be reduced during runtime. + +This is only valid when the `hotplug_method` is `virtio-mem` as it does not +make sense for the `acpi` use case. When using ACPI, the memory can't be +resized after it has been extended. + +This option is only valid when `hotplug_size` is specified, and its value can't +exceed the value of `hotplug_size`. + +Value is an unsigned integer of 64 bits. A value of 0 is invalid. + +_Example_ + +``` +--memory size=0,hotplug_method=virtio-mem +--memory-zone id=mem0,size=1G,hotplug_size=1G,hotplugged_size=512M +``` + +### `prefault` + +Specifies if the memory must be `mmap(2)` with `MAP_POPULATE` flag. + +By triggering prefault, one can allocate all required physical memory and create +its page tables while calling `mmap`. With physical memory allocated, the number +of page faults will decrease during running, and performance will also improve. + +Note that boot of VM will be slower with `prefault` enabled because of allocating +physical memory and creating page tables in advance, and physical memory of the +specified size will be consumed quickly. + +This option only takes effect at boot of VM. There is also a `prefault` option in +restore and its choice will overwrite `prefault` in memory. + +By default this option is turned off. + +_Example_ + +``` +--memory size=0 +--memory-zone id=mem0,size=1G,prefault=on +``` + +## NUMA settings + +`NumaConfig` or what is known as `--numa` from the CLI perspective has been +introduced to define a guest NUMA topology. It allows for a fine description +about the CPUs and memory ranges associated with each NUMA node. Additionally +it allows for specifying the distance between each NUMA node. + +```rust +struct NumaConfig { + guest_numa_id: u32, + cpus: Option>, + distances: Option>, + memory_zones: Option>, + sgx_epc_sections: Option>, +} +``` + +``` +--numa Settings related to a given NUMA node "guest_numa_id=,cpus=,distances=,memory_zones=,sgx_epc_sections=" +``` + +### `guest_numa_id` + +Node identifier of a guest NUMA node. This identifier must be unique, otherwise +an error will be returned. + +This option is mandatory when using the `--numa` parameter. + +Value is an unsigned integer of 32 bits. + +_Example_ + +``` +--numa guest_numa_id=0 +``` + +### `cpus` + +List of virtual CPUs attached to the guest NUMA node identified by the +`guest_numa_id` option. This allows for describing a list of CPUs which +must be seen by the guest as belonging to the NUMA node `guest_numa_id`. + +One can use this option for a fine grained description of the NUMA topology +regarding the CPUs associated with it, which might help the guest run more +efficiently. + +Multiple values can be provided to define the list. Each value is an unsigned +integer of 8 bits. + +For instance, if one needs to attach all CPUs from 0 to 4 to a specific node, +the syntax using `-` will help define a contiguous range with `cpus=0-4`. The +same example could also be described with `cpus=[0,1,2,3,4]`. + +A combination of both `-` and `,` separators is useful when one might need to +describe a list containing all CPUs from 0 to 99 and the CPU 255, as it could +simply be described with `cpus=[0-99,255]`. + +As soon as one tries to describe a list of values, `[` and `]` must be used to +demarcate the list. + +_Example_ + +``` +--cpus boot=8 +--numa guest_numa_id=0,cpus=[1-3,7] guest_numa_id=1,cpus=[0,4-6] +``` + +### `distances` + +List of distances between the current NUMA node referred by `guest_numa_id` +and the destination NUMA nodes listed along with distances. This option let +the user choose the distances between guest NUMA nodes. This is important to +provide an accurate description of the way non uniform memory accesses will +perform in the guest. + +One or more tuple of two values must be provided through this option. The first +value is an unsigned integer of 32 bits as it represents the destination NUMA +node. The second value is an unsigned integer of 8 bits as it represents the +distance between the current NUMA node and the destination NUMA node. The two +values are separated by `@` (`value1@value2`), meaning the destination NUMA +node `value1` is located at a distance of `value2`. Each tuple is separated +from the others with `,` separator. + +As soon as one tries to describe a list of values, `[` and `]` must be used to +demarcate the list. + +For instance, if one wants to define 3 NUMA nodes, with each node located at +different distances, it can be described with the following example. + +_Example_ + +``` +--numa guest_numa_id=0,distances=[1@15,2@25] guest_numa_id=1,distances=[0@15,2@20] guest_numa_id=2,distances=[0@25,1@20] +``` + +### `memory_zones` + +List of memory zones attached to the guest NUMA node identified by the +`guest_numa_id` option. This allows for describing a list of memory ranges +which must be seen by the guest as belonging to the NUMA node `guest_numa_id`. + +This option can be very useful and powerful when combined with `host_numa_node` +option from `--memory-zone` parameter as it allows for creating a VM with non +uniform memory accesses, and let the guest know about it. It allows for +exposing memory zones through different NUMA nodes, which can help the guest +workload run more efficiently. + +Multiple values can be provided to define the list. Each value is a string +referring to an existing memory zone identifier. Values are separated from +each other with the `,` separator. + +As soon as one tries to describe a list of values, `[` and `]` must be used to +demarcate the list. + +Note that a memory zone must belong to a single NUMA node. The following +configuration is incorrect, therefore not allowed: +`--numa guest_numa_id=0,memory_zones=mem0 guest_numa_id=1,memory_zones=mem0` + +_Example_ + +``` +--memory size=0 +--memory-zone id=mem0,size=1G id=mem1,size=1G id=mem2,size=1G +--numa guest_numa_id=0,memory_zones=[mem0,mem2] guest_numa_id=1,memory_zones=mem1 +``` + +### `sgx_epc_sections` + +List of SGX EPC sections attached to the guest NUMA node identified by the +`guest_numa_id` option. This allows for describing a list of SGX EPC sections +which must be seen by the guest as belonging to the NUMA node `guest_numa_id`. + +Multiple values can be provided to define the list. Each value is a string +referring to an existing SGX EPC section identifier. Values are separated from +each other with the `,` separator. + +As soon as one tries to describe a list of values, `[` and `]` must be used to +demarcate the list. + +_Example_ + +``` +--sgx-epc id=epc0,size=32M id=epc1,size=64M id=epc2,size=32M +--numa guest_numa_id=0,sgx_epc_sections=epc1 guest_numa_id=1,sgx_epc_sections=[epc0,epc2] +``` + +### PCI bus + +Cloud Hypervisor supports only one PCI bus, which is why it has been tied to +the NUMA node 0 by default. It is the user responsibility to organize the NUMA +nodes correctly so that vCPUs and guest RAM which should be located on the same +NUMA node as the PCI bus end up on the NUMA node 0. diff --git a/vendor/cube-hypervisor/docs/performance_metrics.md b/vendor/cube-hypervisor/docs/performance_metrics.md new file mode 100644 index 0000000..fdd1fef --- /dev/null +++ b/vendor/cube-hypervisor/docs/performance_metrics.md @@ -0,0 +1,29 @@ +# Performance Metrics + +Cloud Hypervisor provides a [performance metrics](https://github.com/cloud-hypervisor/cloud-hypervisor/tree/main/performance-metrics) +binary for users to generate metrics data from their own +environment. This document describes how to generate metrics data +quickly by using Cloud Hypervisor's development script, +e.g. `dev_cli.sh`. The only prerequisite is [Docker installation](https://docs.docker.com/engine/install/). +Please note that upon its first invocation, this script will pull a +fairly large container image. + +To generate metrics data for all available performance tests (including +boot time, block I/O throughput, and network throughput & latency) and +output the result into a json file: + +``` +$ ./scripts/dev_cli.sh tests --metrics -- -- --report-file /tmp/metrics.json +``` + +To get a list of available performance tests: + +``` +$ ./scripts/dev_cli.sh tests --metrics -- -- --list-tests +``` + +To generate metrics data for selected performance tests, e.g. boot time only: + +``` +$ ./scripts/dev_cli.sh tests --metrics -- -- --report-file /tmp/metrics.json --test-filter boot_time +``` diff --git a/vendor/cube-hypervisor/docs/profiling.md b/vendor/cube-hypervisor/docs/profiling.md new file mode 100644 index 0000000..93c113e --- /dev/null +++ b/vendor/cube-hypervisor/docs/profiling.md @@ -0,0 +1,64 @@ +# Profiling + +`perf` can be used to profile the `cloud-hypervisor` binary but it is necessary to make some modifications to the build in order to produce a binary that gives useful results. + +## Building a suitable binary + +Modify the `Cargo.toml` file to add `debug = 1` to the `[profile.release]` block. It should look like this: + +``` +[profile.release] +lto = true +debug = 1 +``` + +This adds the symbol information to the release binary but does not otherwise affect the performance. + +The binary must also be built with frame pointers included so that the call graph can be captured by the profiler. + +``` +$ cargo clean && RUSTFLAGS='-C force-frame-pointers=y' cargo build --release +``` + +## Profiling + +`perf` may then be used in the usual manner: + +e.g. + +``` +$ perf record -g target/release/cloud-hypervisor \ + --kernel ~/src/linux/vmlinux \ + --pmem file=~/workloads/focal.raw \ + --cpus boot=1 --memory size=1G \ + --cmdline "root=/dev/pmem0p1 console=ttyS0" \ + --serial tty --console off \ + --api-socket=/tmp/api1 +``` + +For analysing the samples: + +``` +$ perf report -g +``` + +If profiling with a network device attached either the TAP device must be already created and configured or the profiling must be done as root so that the TAP device can be created. + +## Userspace only profiling with LBR + +The use of LBR (Last Branch Record; available since Haswell) offers lower +overhead if only userspace profiling is required. This lower overhead can allow +a higher frequency of sampling. This also removes the requirement to compile +with custom `RUSTFLAGS` however debug symbols should still be included: + +e.g. + +``` +$ perf record --call-graph lbr --all-user --user-callchains -g target/release/cloud-hypervisor \ + --kernel ~/src/linux/vmlinux \ + --pmem file=~/workloads/focal.raw \ + --cpus boot=1 --memory size=1G \ + --cmdline "root=/dev/pmem0p1 console=ttyS0" \ + --serial tty --console off \ + --api-socket=/tmp/api1 +``` diff --git a/vendor/cube-hypervisor/docs/releases.md b/vendor/cube-hypervisor/docs/releases.md new file mode 100755 index 0000000..11b6946 --- /dev/null +++ b/vendor/cube-hypervisor/docs/releases.md @@ -0,0 +1,103 @@ +# Release Documentation + +## Abstract + +This document provides guidance to users, downstream maintainers and +any other consumers of the Cloud Hypervisor project, this document +describes the release process, release cadence, stability expectations and +related topics. + +## Basic Terms + +### Stability + +For Cloud Hypervisor the following areas are subject to stability guarantees: + +- [REST API](api.md#rest-api) +- [Command line options](api.md#command-line-interface) +- [Device Model](device_model.md) +- Device tree, device list, ACPI, Hyper-V enlightenments and any other + features exposed to guest +- KVM compatibility +- Rust edition compatibility + +This list is incomplete but this document serves as a best effort guide to stability +across releases. + +### Experimental features + +Experimental features are under active development and no guarantees are made about their stability. + +List of experimental features: + +- TDX +- vfio-user +- vDPA + +### Security + +Security fixes should be included in a new point release. + +For security issues an advisory will be published via the GitHub security advisory process along with the release. Watching the project on GitHub will notify you of those issues. + +## Releases + +### Versioning + +The versioning scheme uses `MAJOR.POINT` pattern: + +- `MAJOR` can introduce incompatible changes along with support for new features. Changes to the [API](api.md#rest-api), + [CLI options](api.md#command-line-interface) and [device model](device_model.md) + require a notice at least 2 releases in advance for the actual change to take + place. +- `POINT` contains bug fixes and/or security fixes. + +### Major Release Cadence + +Cloud Hypervisor is under active development. A new major release is issued approximately +every 6 weeks. Point releases are issued on demand, when important bug fixes are in +the queue. A major release would receive bug fixes for the next two cycles (~12 weeks) +and then be considered EOL. + +``` ++ - Active release support +E - EOL + + 2021 2022 2023 + | | | | | | | | | +18.0 | | | ++++++++E +19.0 | | | |++++++++E +20.0 | | | | ++++++++E +21.0 | | | | | ++++++++E +22.0 | | | | | +++++++++E +23.0 | | | | | | +++++++++E + +``` + +### Major Release Stability Considerations + +Snapshot/restore support is not compatible across `MAJOR` versions. +Live migration support is not compatible across `MAJOR` versions. + +### LTS Release Cadence + +A regular release is promoted to LTS every 12 months. An LTS release is supported for 18 months. This gives a 6 months window for users to move to the new LTS. + +``` ++ - Active release support +E - EOL + + 2022 2023 2024 2025 2026 + | | | | | | | | | | | | | | | | | +23.0 | |+++++++++++++++++++++++++++++E +43.0 | | | | | |+++++++++++++++++++++++++++++E +63.0 | | | | | | | | | |+++++++++++++++++++++++++++++E + +``` + +### LTS Stablity Considerations + +An LTS release is just a `MAJOR` release for which point releases are made for +longer following the same rules for what can be backported to a `POINT` release. +The focus lays on critical and security bug fixes which are pulled at the +maintainer's discretion. diff --git a/vendor/cube-hypervisor/docs/seccomp.md b/vendor/cube-hypervisor/docs/seccomp.md new file mode 100644 index 0000000..4668b35 --- /dev/null +++ b/vendor/cube-hypervisor/docs/seccomp.md @@ -0,0 +1,68 @@ +# Seccomp filtering + +As a means to harden Cloud Hypervisor's security, the project leverages seccomp +filtering. + +## What is seccomp filtering + +A seccomp filter is a way for a process to tell the kernel which system calls +are authorized. +In case this process calls into a prohibited system call, the kernel will kill +the process right away. + +## How does it apply to Cloud Hypervisor + +Cloud Hypervisor is a multi threaded application. It spawns dedicated threads +for virtual CPUs, virtio devices and HTTP server, along with the main thread +representing the VMM. + +Each of these threads has a limited scope of what it is expected to perform, +which is why different filters are applied to each of them. + +By default, Cloud Hypervisor enables seccomp filtering as the project believes +that security should not be an option. + +For development and debugging purposes, one might want to disable this feature +or log the faulty system call. + +### Disabling seccomp filters + +Append `--seccomp false` to Cloud Hypervisor's command line to prevent seccomp +filtering from being applied. + +### Logging prohibited system calls + +In the context of debug, one alternative to disabling seccomp filtering is to +log faulty system calls that would have caused the application to be killed by +the kernel. + +Append `--seccomp log` to Cloud Hypervisor's command line to enable faulty +system calls to be logged. + +The kernel running on the host machine must have the `audit` parameter enabled. +If this is not the case, update kernel boot options by appending `audit=1`. + +Unauthorized system calls will be logged to the journal similarly to the +following example + +``` +type=SECCOMP msg=audit(1423263412.694:7878): auid=1000 uid=1000 gid=1000 ses=3 subj=unconfined_u:unconfined_r:cloud_hypervisor:s0-s0:c0.c1023 pid=1193 comm="cloud-hypervisor" exe="/usr/bin/cloud-hypervisor" sig=0 arch=c000003e syscall=47 compat=0 ip=0x7f4f63982604 code=0x50000 +``` + +Provided `ausyscall` has been installed on the host, the system call can be +identified with + +``` +$ ausyscall 47 +recvmsg +``` + +### Further debug with `strace` + +One more way of debugging seccomp related issues is to use the `strace` tool as +it will log every system call issued by the process. It is important to use +`-f` option in order to trace each and every thread belonging to the process. + +``` +strace --decode-pids=comm -f ./cloud-hypervisor ... +``` diff --git a/vendor/cube-hypervisor/docs/snapshot_restore.md b/vendor/cube-hypervisor/docs/snapshot_restore.md new file mode 100644 index 0000000..c455280 --- /dev/null +++ b/vendor/cube-hypervisor/docs/snapshot_restore.md @@ -0,0 +1,124 @@ +# Snapshot and Restore + +The goal for the snapshot/restore feature is to provide the user with the +ability to take a snapshot of a previously paused virtual machine. This +snapshot can be used as the base for creating new identical virtual machines, +without the need to boot them from scratch. The restore codepath takes the +snapshot and creates the exact same virtual machine, restoring the previously +saved states. The new virtual machine is restored in a paused state, as it was +before the snapshot was performed. + +## Snapshot a Cloud Hypervisor VM + +First thing, we must run a Cloud Hypervisor VM: + +```bash +./cloud-hypervisor \ + --api-socket /tmp/cloud-hypervisor.sock \ + --cpus boot=4 \ + --memory size=4G \ + --kernel vmlinux \ + --cmdline "root=/dev/vda1 console=hvc0 rw" \ + --disk path=focal-server-cloudimg-amd64.raw +``` + +At any point in time when the VM is running, one might choose to pause it: + +```bash +./ch-remote --api-socket=/tmp/cloud-hypervisor.sock pause +``` + +Once paused, the VM can be safely snapshot into the specified directory and +using the following command: + +```bash +./ch-remote --api-socket=/tmp/cloud-hypervisor.sock snapshot file:///home/foo/snapshot +``` + +Given the directory was present on the system, the snapshot will succeed and +it should contain the following files: + +```bash +ll /home/foo/snapshot/ +total 4194536 +drwxrwxr-x 2 foo bar 4096 Jul 22 11:50 ./ +drwxr-xr-x 47 foo bar 4096 Jul 22 11:47 ../ +-rw------- 1 foo bar 1084 Jul 22 11:19 config.json +-rw------- 1 foo bar 4294967296 Jul 22 11:19 memory-ranges +-rw------- 1 foo bar 217853 Jul 22 11:19 state.json +``` + +`config.json` contains the virtual machine configuration. It is used to create +a similar virtual machine with the correct amount of CPUs, RAM, and other +expected devices. It is stored in a human readable format so that it could be +modified between the snapshot and restore phases to achieve some very special +use cases. But for most cases, manually modifying the configuration should not +be needed. + +`memory-ranges` stores the content of the guest RAM. + +`state.json` contains the virtual machine state. It is used to restore each +component in the state it was left before the snapshot occurred. + +## Restore a Cloud Hypervisor VM + +Given that one has access to an existing snapshot in `/home/foo/snapshot`, +it is possible to create a new VM based on this snapshot with the following +command: + +```bash +./cloud-hypervisor \ + --api-socket /tmp/cloud-hypervisor.sock \ + --restore source_url=file:///home/foo/snapshot +``` + +Or using two different commands from two terminals: + +```bash +# First terminal +./cloud-hypervisor --api-socket /tmp/cloud-hypervisor.sock + +# Second terminal +./ch-remote --api-socket=/tmp/cloud-hypervisor.sock restore source_url=file:///home/foo/snapshot +``` + +Remember the VM is restored in a `paused` state, which was the VM's state when +it was snapshot. For this reason, one must explicitly `resume` the VM before to +start using it. + +```bash +./ch-remote --api-socket=/tmp/cloud-hypervisor.sock resume +``` + +At this point, the VM is fully restored and is identical to the VM which was +snapshot earlier. + +## Limitations + +VFIO devices and Intel SGX are out of scope. + +## Snapshot types + +The `--snapshot-type` flag of the `snapshot` and `pause2snapshot` ch-remote +subcommands selects how guest RAM is captured. The other snapshot artifacts +(`config.json`, `state.json`) are unaffected. + +| Type | What gets saved to `memory-ranges` | When to use | +|------|------------------------------------|-------------| +| `full` | Every byte of guest RAM. | First snapshot after VM start, or any time you need a self-contained image. | +| `incremental` | Only **CoW anonymous** pages (pages the guest has written to since the original `MAP_PRIVATE` mmap was set up). Detected from `/proc/self/pagemap` bit 61 (`PM_FILE`). | Cheap "second" snapshot when a base file already exists. The set of saved pages monotonically grows over the VM's lifetime — every page ever touched is in the delta. | +| `soft-dirty` | Only the pages written **since the previous `soft-dirty` snapshot** (a true delta). Detected via `/proc/self/clear_refs` + pagemap bit 55. | Repeated incremental snapshots on a long-running VM, where most pages are not touched between cycles. | + +### Soft-dirty details + +- The first `soft-dirty` snapshot taken after VM start writes a full base + image and arms the kernel tracker. +- Each subsequent `soft-dirty` snapshot writes only the pages whose PTE has + been marked soft-dirty since the previous call, then re-arms the tracker. +- Requires the host kernel to be built with `CONFIG_MEM_SOFT_DIRTY=y`. On + kernels without this support, `soft-dirty` requests are silently downgraded + to the `incremental` (pagemap_anon) path and a debug log is emitted. +- A runtime failure of the `clear_refs` write (e.g. seccomp blocking it) + also degrades subsequent snapshots to `incremental` for the rest of the + VM's lifetime, with a warning log. + diff --git a/vendor/cube-hypervisor/docs/tpm.md b/vendor/cube-hypervisor/docs/tpm.md new file mode 100644 index 0000000..aadd07d --- /dev/null +++ b/vendor/cube-hypervisor/docs/tpm.md @@ -0,0 +1,122 @@ +# TPM +Tpm in Cloud-Hypervisor is emulated using `swtpm` as the backend. [swtpm](https://github.com/stefanberger/swtpm) is the link to swtpm project. + +Current implementation only supports TPM `2.0` version. At the moment only +`CRB Interface` is implemented. This interface is described in +[TCG PC Client Platform TPM Profile Specification for TPM 2.0, Revision 01.05 v4](https://trustedcomputinggroup.org/wp-content/uploads/PC-Client-Specific-Platform-TPM-Profile-for-TPM-2p0-v1p05p_r14_pub.pdf). + + +## Usage +`--tpm`, an optional argument, can be passed to enable tpm device. +This argument takes an UNIX domain Socket as a `socket` value. + +_Example_ + +An Example invocation with `--tpm` argument: + +``` + ./cloud-hypervisor/target/release/cloud-hypervisor \ + --kernel ./hypervisor-fw \ + --disk path=focal-server-cloudimg-amd64.raw \ + --cpus boot=4 \ + --memory size=1024M \ + --net "tap=,mac=,ip=,mask=" \ + --tpm socket="/var/run/swtpm.socket" +``` + +## swtpm +Before invoking cloud-hypervisor with `--tpm` argument, a `swtpm` +process should be started to listen at the input socket. Below is an +example invocation of swtpm process. + +``` +swtpm socket --tmpstate dir=/var/run/swtpm \ + --ctrl type=unixio,path="/var/run/swtpm.socket" \ + --flags startup-clear \ + --tpm2 +``` + +## Guest +After starting a guest with the above commands, ensure below listed modules are +loaded in the guest: + +``` +# lsmod | grep tpm +tpm_crb 20480 0 +tpm 81920 1 tpm_crb +``` + +Below is the IO Memory map configured in the guest: + +``` +# cat /proc/iomem | grep MSFT +fed40000-fed40fff : MSFT0101:00 + fed40000-fed40fff : MSFT0101:00 +``` +Below are the devices created in the guest: + +``` +# ls /dev/tpm* +/dev/tpm0 /dev/tpmrm0 +``` + + +## Testing + +Inside the guest install `tpm2-tools` package. This package provides some +commands to run against TPM that supports 2.0 version. + +_Examples_ +``` +// Run Self Test +# tpm2_selftest -f +# echo $? +0 + + +# echo "hello" > input.txt +// this command generates hash of the input file using all the algos supported by TPM + +# tpm2_pcrevent input.txt +sha1: f572d396fae9206628714fb2ce00f72e94f2258f +sha256: 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 +sha384: 1d0f284efe3edea4b9ca3bd514fa134b17eae361ccc7a1eefeff801b9bd6604e01f21f6bf249ef030599f0c +218f2ba8c +sha512: e7c22b994c59d9cf2b48e549b1e24666636045930d3da7c1acb299d1c3b7f931f94aae41edda2c2b207a36e +10f8bcb8d45223e54878f5b316e7ce3b6bc019629 + +// verify one of the hashes +# sha256sum input.txt +5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 input.txt +``` + +### Bundled Functional Test + +Build time dependencies for `tpm2-tss` are captured in [INSTALL](https://github.com/tpm2-software/tpm2-tss/blob/master/INSTALL.md). + +``` +# git clone https://github.com/tpm2-software/tpm2-tss.git +# cd tpm2-tss +# ./configure --enable-integration --with-devicetests="mandatory,optional" --with-device=/dev/tpm0 +# sudo make check-device +. +. +. +. +============================================================================ +Testsuite summary for tpm2-tss 3.2.0-74-ge03617d9 +============================================================================ +# TOTAL: 154 +# PASS: 88 +# SKIP: 7 +# XFAIL: 0 +# FAIL: 59 +# XPASS: 0 +# ERROR: 0 +============================================================================ +See ./test-suite.log +Please report to https://github.com/tpm2-software/tpm2-tss/issues +============================================================================ +``` +The same set of failures are noticed while running these tests on `Qemu` with +its TPM implementation. diff --git a/vendor/cube-hypervisor/docs/tracing.md b/vendor/cube-hypervisor/docs/tracing.md new file mode 100644 index 0000000..735abde --- /dev/null +++ b/vendor/cube-hypervisor/docs/tracing.md @@ -0,0 +1,42 @@ +# Tracing + +Cloud Hypervisor has a basic tracing infrastucture, particularly focussed on +the tracing of the initial VM setup. + +## Usage + +To enabling tracing, build with "tracing" feature. When compiled without the +feature the tracing is compiled out. + +```bash +cargo build --features "tracing" +``` + +And then run Cloud Hypervisor as you wish, the trace will be written to the current directory as `cloud-hypervisor-.trace`. This is JSON file which you can inspect yourself. + +Alternatively you can use the provided script in +`scripts/ch-trace-visualiser.py` to generate an SVG: + +For example: + +```bash +scripts/ch-trace-visualiser.py cloud-hypervisor-39466.trace output.svg +``` + +## Tracing in the codebase + +There are existing tracepoints in the code base; extra ones can be added for +more detailed tracing. + +The `tracer::trace_scoped!()` macro is used to add the current existing scope +appears as a block in the trace. Other than providing a useful name for the +event nothing else is required from the developer. + +The `tracer::start()` and `tracer::end()` functions are already in place for +generating traces of the boot. These can be relocated for focus tracing on a +narrow part of the code base. + +A `tracer::trace_point!()` macro is also provided for an instantaneous trace +point however this is not in use in the code base currently nor is handled by +the visualisation script due to the difficulty in representation in the SVG. + diff --git a/vendor/cube-hypervisor/docs/uefi.md b/vendor/cube-hypervisor/docs/uefi.md new file mode 100644 index 0000000..3695d12 --- /dev/null +++ b/vendor/cube-hypervisor/docs/uefi.md @@ -0,0 +1,39 @@ +# UEFI Boot + +Cloud Hypervisor supports UEFI boot through the utilization of the EDK II based UEFI firmware. + +## Building UEFI Firmware + +To avoid any unnecessary issues, it is recommended to use Ubuntu 18.04 and its default toolset. Any other compatible Linux distribution is otherwise suitable, however it is suggested to use a temporary Docker container with Ubuntu 18.04 for a quick build on an existing Linux machine. + +The commands below will compile an OVMF firmware suitable for Cloud Hypervisor. + +```shell +sudo apt-get update +sudo apt-get install uuid-dev nasm iasl build-essential python3-distutils git + +git clone https://github.com/cloud-hypervisor/edk2 -b ch +cd edk2 +. edksetup.sh +git submodule update --init + +echo "ACTIVE_PLATFORM=OvmfPkg/CloudHv/CloudHvX64.dsc" >> Conf/target.txt +echo "TARGET_ARCH=X64" >> Conf/target.txt +echo "TOOL_CHAIN_TAG=GCC5" >> Conf/target.txt + +make -C ./BaseTools +build +``` + +After the successful build, the resulting firmware binaries are available under `Build/CloudHvX64/DEBUG_GCC5/FV` underneath the edk2 checkout. + +## Using OVMF Binaries + +Any UEFI capable image can be booted using the Cloud Hypervisor specific firmware. Windows guests under Cloud Hypervisor only support UEFI boot, therefore OVMF is mandatory there. + +To make Cloud Hypervisor use UEFI boot, pass the `CLOUDHV.fd` file path as an argument to the `--kernel` option. The firmware file will be opened in read only mode. + +# Links + +- [OVMF wiki](https://github.com/tianocore/tianocore.github.io/wiki/OVMF) +- [Cloud Hypervisor specific tree](https://github.com/cloud-hypervisor/edk2/tree/ch) diff --git a/vendor/cube-hypervisor/docs/vdpa.md b/vendor/cube-hypervisor/docs/vdpa.md new file mode 100644 index 0000000..2ba9126 --- /dev/null +++ b/vendor/cube-hypervisor/docs/vdpa.md @@ -0,0 +1,154 @@ +# Virtio Data Path Acceleration + +vDPA aims at achieving bare-metal performance for devices passed into a virtual +machine. It is an alternative to VFIO, as it provides a simpler solution for +achieving migration. + +It is a kernel framework introduced recently to handle devices complying with +the VIRTIO specification on their data-path, while the control path is vendor +specific. In practice, virtqueues are accessed directly through DMA mechanism +between the hardware and the guest. The control path is accessed through the +vDPA framework, being exposed through the vhost interface as a vhost-vdpa +device. + +Because DMA accesses between device and guest are going through virtqueues, +migration can be achieved without requiring device's driver to implement any +specific migration support. In case of VFIO, each vendor is expected to provide +an implementation of the VFIO migration framework, complicating things as it +must be done for each and every device's driver. + +The official [website](https://vdpa-dev.gitlab.io/) contains some extensive +documentation on the topic. + +## Usage + +`VdpaConfig` (known as `--vdpa` from the CLI perspective) contains the list of +parameters available for the vDPA device. + +```rust +struct VdpaConfig { + path: PathBuf, + num_queues: usize, + id: Option, + pci_segment: u16, +} +``` + +``` +--vdpa vDPA device "path=,num_queues=,iommu=on|off,id=,pci_segment=" +``` + +### `path` + +Path of the vDPA device. Usually `/dev/vhost-vdpa-X`. + +This parameter is mandatory. + +Value is a string. + +_Example_ + +``` +--vdpa path=/dev/vhost-vdpa-0 +``` + +### `num_queues` + +Number of virtqueues supported by the vDPA device. + +This parameter is optional. + +Value is an unsigned integer set to `1` by default. + +_Example_ + +``` +--vdpa path=/dev/vhost-vdpa-0,num_queues=2 +``` + +### `id` + +Identifier of the vDPA device. + +This parameter is optional. If provided, it must be unique across the entire +virtual machine. + +Value is a string. + +_Example_ + +``` +--vdpa path=/dev/vhost-vdpa-0,id=vdpa0 +``` + +### `pci_segment` + +PCI segment number to which the vDPA device should be attached to. + +This parameter is optional. + +Value is an unsigned integer of 16 bits set to `0` by default. + +_Example_ + +``` +--vdpa path=/dev/vhost-vdpa-0,pci_segment=1 +``` + +## Example with vDPA block simulator + +The vDPA framework provides a simulator with both `virtio-block` and +`virtio-net` implementations. This is very useful for testing vDPA when we +don't have access to the specific hardware. + +Given the host kernel has the appropriate modules available, let's load them +all: + +``` +sudo modprobe vdpa +sudo modprobe vhost_vdpa +sudo modprobe vdpa_sim +sudo modprobe vdpa_sim_blk +``` + +Given you have the `iproute2/vdpa` tool installed, let's now create the +`virtio-block` vDPA device: + +```sh +sudo vdpa dev add name vdpa-blk1 mgmtdev vdpasim_blk +sudo chown $USER:$USER /dev/vhost-vdpa-0 +sudo chmod 660 /dev/vhost-vdpa-0 +``` + +Increase the maximum locked memory to ensure setting up IOMMU mappings will +succeed: + +```sh +ulimit -l unlimited +``` + +Start Cloud Hypervisor: + +```sh +cloud-hypervisor \ + --cpus boot=1 \ + --memory size=1G,hugepages=on \ + --disk path=focal-server-cloudimg-amd64.raw \ + --kernel vmlinux \ + --cmdline "root=/dev/vda1 console=hvc0" \ + --vdpa path=/dev/vhost-vdpa-0,num_queues=1 +``` + +The `virtio-block` device backed by the vDPA simulator can be found as +`/dev/vdb` in the guest: + +``` +cloud@cloud:~$ lsblk +NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT +nullb0 252:0 0 250G 0 disk +vda 254:0 0 2.2G 0 disk +├─vda1 254:1 0 2.1G 0 part / +├─vda14 254:14 0 4M 0 part +└─vda15 254:15 0 106M 0 part /boot/efi +vdb 254:16 0 128M 0 disk +``` diff --git a/vendor/cube-hypervisor/docs/vfio-user.md b/vendor/cube-hypervisor/docs/vfio-user.md new file mode 100644 index 0000000..b21d376 --- /dev/null +++ b/vendor/cube-hypervisor/docs/vfio-user.md @@ -0,0 +1,79 @@ +# Cloud Hypervisor VFIO-user HOWTO + +VFIO-user is an *experimental* protocol for allowing devices to be implemented in another process and communicate over a socket; ie.e VFIO-user is to VFIO as virtio is to vhost-user. + +The protocol is documented here: https://github.com/nutanix/libvfio-user/blob/master/docs/vfio-user.rst + +The Cloud Hypervisor support for such devices is *experimental*. Not all Cloud Hypervisor functionality is supported in particular: virtio-mem and iommu are not supported. + +## Usage + +The `--user-device socket=` parameter is used to create a vfio-user device when creating the VM specifying the socket to connect to. The device can also be hotplugged with `ch-remote add-user-device socket=`. + +## Example (GPIO device) + +There is a simple GPIO device included in the libvfio-user repository: https://github.com/nutanix/libvfio-user#gpio + +Run the example from the libvfio-user repository: + +```sh +rm /tmp/vfio-user.sock +./build/dbg/samples/gpio-pci-idio-16 -v /tmp/vfio-user.sock & +``` + +Start Cloud Hypervisor: + +```sh +target/debug/cloud-hypervisor \ + --memory size=1G,shared=on \ + --disk path=~/images/focal-server-cloudimg-amd64.raw \ + --kernel ~/src/linux/vmlinux \ + --cmdline "root=/dev/vda1 console=hvc0" \ + --user-device socket=/tmp/vfio-user.sock +``` + +Inside the VM you can test the device with: + +```sh +cat /sys/class/gpio/gpiochip480/base > /sys/class/gpio/export +for ((i=0;i<12;i++)); do cat /sys/class/gpio/OUT0/value; done +``` + +## Example (NVMe device) + +Use SPDK: https://github.com/spdk/spdk + +Compile with `./configure --with-vfio-user` + +Create an NVMe controller listening on a vfio-user socket with a simple AIO block device in spdk. +More details of configuring SPDK bdev can be viewed in [SPDK bdev](https://spdk.io/doc/bdev.html). +More details of setting SPDK NVMe-oF target can be viewed in [SDPK NVMe-oF tgt](https://spdk.io/doc/nvmf.html). + +```sh +sudo scripts/setup.sh +rm ~/images/test-disk.raw +truncate ~/images/test-disk.raw -s 128M +mkfs.ext4 ~/images/test-disk.raw +sudo killall ./build/bin/nvmf_tgt +sudo ./build/bin/nvmf_tgt -i 0 -e 0xFFFF -m 0x1 & +sleep 2 +sudo ./scripts/rpc.py nvmf_create_transport -t VFIOUSER +sudo rm -rf /tmp/nvme-vfio-user +sudo mkdir -p /tmp/nvme-vfio-user +sudo ./scripts/rpc.py bdev_aio_create ~/images/test-disk.raw test 512 +sudo ./scripts/rpc.py nvmf_create_subsystem nqn.2019-07.io.spdk:cnode -a -s test +sudo ./scripts/rpc.py nvmf_subsystem_add_ns nqn.2019-07.io.spdk:cnode test +sudo ./scripts/rpc.py nvmf_subsystem_add_listener nqn.2019-07.io.spdk:cnode -t VFIOUSER -a /tmp/nvme-vfio-user -s 0 +sudo chown $USER.$USER -R /tmp/nvme-vfio-user +``` + +Start Cloud Hypervisor: + +```sh +target/debug/cloud-hypervisor \ + --memory size=1G,shared=on \ + --disk path=~/images/focal-server-cloudimg-amd64.raw \ + --kernel ~/src/linux/vmlinux \ + --cmdline "root=/dev/vda1 console=hvc0" \ + --user-device socket=/tmp/nvme-vfio-user/cntrl +``` diff --git a/vendor/cube-hypervisor/docs/vfio.md b/vendor/cube-hypervisor/docs/vfio.md new file mode 100644 index 0000000..17e633a --- /dev/null +++ b/vendor/cube-hypervisor/docs/vfio.md @@ -0,0 +1,125 @@ +# Cloud Hypervisor VFIO HOWTO + +VFIO (Virtual Function I/O) is a kernel framework that exposes direct device +access to userspace. `cloud-hypervisor`, as many VMMs do, uses the VFIO +framework to directly assign host physical devices to the guest workloads. + +## Direct Device Assignment with Cloud Hypervisor + +To assign a device to a `cloud-hypervisor` guest, the device needs to be managed +by the VFIO kernel drivers. However, by default, a host device will be bound to +its native driver, which is not the VFIO one. + +As a consequence, a device must be unbound from its native driver before passing +it to `cloud-hypervisor` for assigning it to a guest. + +### Example + +In this example we're going to assign a PCI memory card (SD, MMC, etc) reader +from the host in a cloud hypervisor guest. + +`cloud-hypervisor` only supports assigning PCI devices to its guests. `lspci` +helps with identifying PCI devices on the host: + +``` +$ lspci +[...] +01:00.0 Unassigned class [ff00]: Realtek Semiconductor Co., Ltd. RTS525A PCI Express Card Reader (rev 01) +[...] +``` + +Here we see that our device is on bus 1, slot 0 and function 0 (`01:00.0`). + +Now that we have identified the device, we must unbind it from its native driver +(`rtsx_pci`) and bind it to the VFIO driver instead (`vfio_pci`). + +First we add VFIO support to the host: + +``` +# modprobe -r vfio_pci +# modprobe -r vfio_iommu_type1 +# modprobe vfio_iommu_type1 allow_unsafe_interrupts +# modprobe vfio_pci +``` + +In case the VFIO drivers are built-in, enable unsafe interrupts with: + +``` +# echo 1 > /sys/module/vfio_iommu_type1/parameters/allow_unsafe_interrupts +``` + +Then we unbind it from its native driver: + +``` +# echo 0000:01:00.0 > /sys/bus/pci/devices/0000\:01\:00.0/driver/unbind +``` + +And finally we bind it to the VFIO driver. To do that we first need to get the +device's VID (Vendor ID) and PID (Product ID): + +``` +$ lspci -n -s 01:00.0 +01:00.0 ff00: 10ec:525a (rev 01) + +# echo 10ec 525a > /sys/bus/pci/drivers/vfio-pci/new_id +``` + +If you have more than one device with the same `vendorID`/`deviceID`, starting +with the second device, the binding is performed as follows: + +``` +# echo 0000:02:00.0 > /sys/bus/pci/drivers/vfio-pci/bind +``` + +Now the device is managed by the VFIO framework. + +The final step is to give that device to `cloud-hypervisor` to assign it to the +guest. This is done by using the `--device` command line option. This option +takes the device's sysfs path as an argument. In our example it is +`/sys/bus/pci/devices/0000:01:00.0/`: + +``` +./target/debug/cloud-hypervisor \ + --kernel ~/vmlinux \ + --disk path=~/focal-server-cloudimg-amd64.raw \ + --console off \ + --serial tty \ + --cmdline "console=ttyS0 root=/dev/vda1 rw" \ + --cpus 4 \ + --memory size=512M \ + --device path=/sys/bus/pci/devices/0000:01:00.0/ +``` + +The guest kernel will then detect the card reader on its PCI bus and provided +that support for this device is enabled, it will probe and enable it for the +guest to use. + +In case you want to pass multiple devices, here is the correct syntax: + +``` +--device path=/sys/bus/pci/devices/0000:01:00.0/ path=/sys/bus/pci/devices/0000:02:00.0/ +``` + +### Multiple devices in the same IOMMU group + +There are cases where multiple devices can be found under the same IOMMU group. +This happens often with graphics card embedding an audio controller. + +``` +$ lspci +[...] +01:00.0 VGA compatible controller: NVIDIA Corporation GK208B [GeForce GT 710] (rev a1) +01:00.1 Audio device: NVIDIA Corporation GK208 HDMI/DP Audio Controller (rev a1) +[...] +``` + +This is usually exposed as follows through `sysfs`: + +``` +$ ls /sys/kernel/iommu_groups/22/devices/ +0000:01:00.0 0000:01:00.1 +``` + +This means these two devices are under the same IOMMU group 22. In such case, +it is important to bind both devices to VFIO and pass them both through the +VM, otherwise this could cause some functional and security issues. \ No newline at end of file diff --git a/vendor/cube-hypervisor/docs/vhost-user-blk-testing.md b/vendor/cube-hypervisor/docs/vhost-user-blk-testing.md new file mode 100644 index 0000000..6bd3677 --- /dev/null +++ b/vendor/cube-hypervisor/docs/vhost-user-blk-testing.md @@ -0,0 +1,122 @@ +# How to test vhost-user-blk with SPDK + +The purpose of this document is to illustrate how to test vhost-user-blk in Cloud Hypervisor with SPDK as the backend. + +## Framework + +It's a simple test to validate the block read/write between VM and block backend. +``` + +----+----------+ +-------------+-----------+ + | | | | | | + | |vhost-user|----------| vhost-user | dpdk | + | |blk device| | port 1 | | + | | | | | | + | +----------+ +-------------+-----------+ + | | | | + | vm | | spdk | + | | | | + +--+----------------------------------------------------+--+ + | | hugepages | | + | +----------------------------------------------------+ | + | | + | host | + | | + +----------------------------------------------------------+ +``` +## Prerequisites + +Prior to running the test, the following steps need to be performed. +- Enable hugepages +- Install SPDK + +Here are some good references for detailing them. +- spdk + * https://spdk.io/doc/ + +## Test environment + +The below test environment is based on Ubuntu release (20.04.1 LTS), as for other system, please check related documents. +This test runs with multiple queue (MQ) support enabled, using 4 queues defined for both SPDK and the virtual machine. +Here are the detailed instructions. + +### The hugepages settings on the host +Allocate enough persistent huge pages (e.g. 2GiB) on the host: + +```bash +# Assume the default hugepage size is 2MiB +echo 1024 | sudo tee /proc/sys/vm/nr_hugepages +# Confirm the value of "Hugepagesize" and "HugePages_Total" +cat /proc/meminfo | grep -i huge +``` + +### Download the SPDK code +```bash +git clone https://github.com/spdk/spdk +cd spdk +git submodule update --init +``` + +### Create the build dep + +```bash +./scripts/pkgdep.sh +``` + +### Build SPDK +```bash +./configure +make +``` + +### Set the SPDK environment + +Run SPDK vhost target with 2 CPU cores, i.e., core 0 and 1. +```bash +sudo HUGEMEM=2048 scripts/setup.sh +sudo ./build/bin/vhost -S /var/tmp -s 1024 -m 0x3 & +``` + +### Create 512M block device + +Create a 512M (first parameter) block device with 512 bytes (second parameter) block size. +```bash +sudo scripts/rpc.py bdev_malloc_create 512 512 -b Malloc0 +sudo scripts/rpc.py vhost_create_blk_controller --cpumask 0x1 vhost.1 Malloc0 +``` + +### Launch the VM + +VMs run in client mode. They connect to the socket created by the `dpdkvhostuser` in the SPDK backend. +```bash +# From the test terminal. We need to create one vhost-user-blk device for the --disk. +./cloud-hypervisor \ + --cpus boot=4 \ + --memory size=1024M,hugepages=on,shared=true \ + --kernel linux/arch/x86/boot/compressed/vmlinux.bin \ + --cmdline "console=ttyS0 root=/dev/vda1 rw iommu=off" \ + --disk path=images/focal-server-cloudimg-amd64.raw vhost_user=true,socket=/var/tmp/vhost.1,num_queues=4,queue_size=128 \ + --console off \ + --serial tty \ + --rng +``` + +```bash +# How to test the vhost-user-blk device with SPDK backend +login in guest + +# Use lsblk command to find out vhost-user-blk device +lsblk +NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT +vda 252:0 0 2.2G 0 disk +├─vda1 252:1 0 2.1G 0 part / +├─vda14 252:14 0 4M 0 part +└─vda15 252:15 0 106M 0 part /boot/efi +vdb 253:16 0 512M 0 disk + +The vhost-user-blk device is /dev/vdb + +# How to do simple read/write test +dd if=/dev/vdb of=/dev/null bs=2M iflag=direct +dd of=/dev/vdb if=/dev/zero bs=2M oflag=direct count=256 + +If you want to do fio test, please install fio binary into guest. The detailed info is not listed here. diff --git a/vendor/cube-hypervisor/docs/vhost-user-net-testing.md b/vendor/cube-hypervisor/docs/vhost-user-net-testing.md new file mode 100644 index 0000000..58e912c --- /dev/null +++ b/vendor/cube-hypervisor/docs/vhost-user-net-testing.md @@ -0,0 +1,145 @@ +# How to test Vhost-user net with OpenVSwitch/DPDK + +The purpose of this document is to illustrate how to test vhost-user-net +in cloud-hypervisor with OVS/DPDK as the backend. This document was +tested with Open vSwitch v2.13.1, DPDK v19.11.3, and Cloud Hypervisor +v15.0 on Ubuntu 20.04.1 (host kernel v5.4.0). + +## Framework + +It's a simple test to validate the communication between two virtual machine, connecting them to vhost-user ports respectively provided by `OVS/DPDK`. +``` + +----+----------+ +-------------+-----------+-------------+ +----------+----+ + | | | | | | | | | | + | |vhost-user|----------| vhost-user | ovs | vhost-user |----------|vhost-user| | + | |net device| | port 1 | | port 2 | |net device| | + | | | | | | | | | | + | +----------+ +-------------+-----------+-------------+ +----------+ | + | | | | | | + |vm1 | | dpdk | | vm2 | + | | | | | | + +--+---------------------------------------------------------------------------------------------+--+ + | | hugepages | | + | +---------------------------------------------------------------------------------------------+ | + | | + | host | + | | + +---------------------------------------------------------------------------------------------------+ +``` +## Prerequisites + +Prior to running the test, the following steps need to be performed. +- Enable hugepages +- Install DPDK +- Install OVS + +Here is a good reference for setting up OVS with DPDK from scratch: +https://docs.openvswitch.org/en/latest/intro/install/dpdk/. + +On Ubuntu systems (18.04 or newer), the OpenVswitch-DPDK package can be +easily installed with: +```bash +sudo apt-get update +sudo apt-get install openvswitch-switch-dpdk +sudo update-alternatives --set ovs-vswitchd /usr/lib/openvswitch-switch-dpdk/ovs-vswitchd-dpdk +``` +## Test +The test runs with multiple queue (MQ) support enabled, using 2 pairs of +TX/RX queues defined for both OVS and the virtual machine. Here are the +detailed instructions. + +_Setup OVS_ + +Here is an example how to configure a basic OpenVswitch using DPDK: +```bash +# load the ovs kernel module +modprobe openvswitch +sudo service openvswitch-switch start +ovs-vsctl init +ovs-vsctl set Open_vSwitch . other_config:dpdk-init=true +# run on core 0-3 only +ovs-vsctl set Open_vSwitch . other_config:dpdk-lcore-mask=0xf +# allocate 2G huge pages (to NUMA 0 only) +ovs-vsctl set Open_vSwitch . other_config:dpdk-socket-mem=1024 +# run PMD (Pull Mode Driver) threads on core 0-3 only +ovs-vsctl set Open_vSwitch . other_config:pmd-cpu-mask=0xf +sudo service openvswitch-switch restart +# double check the configurations +ovs-vsctl list Open_vSwitch +``` + +Here is an example how to create a bridge and add two DPDK ports to it +(for later use via Cloud Hypervisor): +```bash +# create a bridge +ovs-vsctl add-br ovsbr0 -- set bridge ovsbr0 datapath_type=netdev +# create two DPDK ports and add them to the bridge +ovs-vsctl add-port ovsbr0 vhost-user1 -- set Interface vhost-user1 type=dpdkvhostuser +ovs-vsctl add-port ovsbr0 vhost-user2 -- set Interface vhost-user2 type=dpdkvhostuser +# set the number of rx queues +ovs-vsctl set Interface vhost-user1 options:n_rxq=2 +ovs-vsctl set Interface vhost-user2 options:n_rxq=2 +``` + +_Launch the VMs_ + +VMs run in client mode. They connect to the socket created by the `dpdkvhostuser` backend. +```bash +# From one terminal. We need to give the cloud-hypervisor binary the NET_ADMIN capabilities for it to set TAP interfaces up on the host. +./cloud-hypervisor \ + --cpus boot=2 \ + --memory size=512M,hugepages=on,shared=true \ + --kernel vmlinux \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \ + --disk path=focal-server-cloudimg-amd64.raw \ + --net mac=52:54:00:02:d9:01,vhost_user=true,socket=/var/run/openvswitch/vhost-user1,num_queues=4 + +# From another terminal. We need to give the cloud-hypervisor binary the NET_ADMIN capabilities for it to set TAP interfaces up on the host. +./cloud-hypervisor \ + --cpus boot=2 \ + --memory size=512M,hugepages=on,shared=true \ + --kernel vmlinux \ + --cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \ + --disk path=focal-server-cloudimg-amd64.raw \ + --net mac=52:54:20:11:C5:02,vhost_user=true,socket=/var/run/openvswitch/vhost-user2,num_queues=4 +``` + +_Setup VM1_ +```bash +# From inside the guest +sudo ip addr add 172.100.0.1/24 dev ens2 +sudo ip link set up dev ens2 +``` + +_Setup VM2_ +```bash +# From inside the guest +sudo ip addr add 172.100.0.2/24 dev ens2 +sudo ip link set up dev ens2 +``` + +_Ping VM1 from VM2_ +```bash +# From inside the guest +sudo ping 172.100.0.1 +``` + +_Ping VM2 from VM1_ +```bash +# From inside the guest +sudo ping 172.100.0.2 +``` + +__Result:__ At this point, VM1 and VM2 can ping each other successfully. We can now run `iperf3` test. + +_Run VM1 as server_ +```bash +# From inside the guest +iperf3 -s -p 4444 +``` + +_Run VM2 as client_ +```bash +# From inside the guest +iperf3 -c 172.100.0.1 -t 30 -p 4444 & +``` diff --git a/vendor/cube-hypervisor/docs/virtiofs-root.md b/vendor/cube-hypervisor/docs/virtiofs-root.md new file mode 100644 index 0000000..d3a23f1 --- /dev/null +++ b/vendor/cube-hypervisor/docs/virtiofs-root.md @@ -0,0 +1,92 @@ +# HOWTO VirtioFS rootfs + +A quick guide for using virtiofs as a cloud-hypervisor guest's rootfs (i.e. +with no root block device). This document is a quick getting started guide. +There are many more steps to take to make this a production ready, secure +setup. + +## Prerequisites + +1. virtiofsd from the qemu project + * We are using the Qemu version for now + * There is a Rust version being worked on that may be a better option in the future + * Part of the qemu-system-common package on Ubuntu + * Part of the qemu-common package on Fedora +2. cloud-hypervisor - the newer the better, but I tested with 0.12 +3. a rootfs - This howto uses an alpine rootfs available here: + * https://dl-cdn.alpinelinux.org/alpine/v3.13/releases/x86_64/alpine-minirootfs-3.13.2-x86_64.tar.gz + * Others should work + +## To create the VM rootfs + +```bash +mkdir rootfs/ +cd rootfs +# this needs sudo to be able to set root permissions on fs components +sudo tar -xf /path/to/alpine-minirootfs-3.13.1-x86_64.tar.gz +# this will get created when the VM actually boots by the dhcp client +# but we need it in the chroot to download packages +sudo cp /etc/resolv.conf etc/ +# the alpine mini rootfs is meant for docker containers, we need a few extra +# things for a working rootfs +sudo chroot $PWD apk add openrc busybox-initscripts +# we are using the paravirt console in cloud-hypervisor, so enable it in init +# append it after the other console since it doesn't work just appending it +sudo sed -i '/vt100/a \n# paravirt console\nhvc0::respawn:/sbin/getty -L hvc0 115200 vt100' etc/inittab +# set no password for root user... you obviously don't want to do this for +# any sort of production setup +sudo sed -i 's/root:!::0:::::/root:::0:::::/' etc/shadow +# set up init scripts +for i in acpid crond + sudo ln -sf /etc/init.d/$i etc/runlevels/default/$i +end +for i in bootmisc hostname hwclock loadkmap modules networking swap sysctl syslog urandom + sudo ln -sf /etc/init.d/$i etc/runlevels/boot/$i +end + +for i in killprocs mount-ro savecache + sudo ln -sf /etc/init.d/$i etc/runlevels/shutdown/$i +end + +for i in devfs dmesg hwdrivers mdev + sudo ln -sf /etc/init.d/$i etc/runlevels/sysinit/$i +end +# setup network config +echo 'auto lo +iface lo inet loopback + +auto eth0 +iface eth0 inet dhcp +' | sudo tee etc/network/interfaces + +``` + +## To run the VM + +```bash +# starting in the directory above rootfs +sudo virtiofsd --socket-path=$PWD/virtiofs-rootfs.sock --shared-dir=$PWD/rootfs --cache=never & +sudo cloud-hypervisor \ + --cpus boot=1,max=1 \ + --kernel vmlinux \ + --fs tag=/dev/root,socket=$PWD/virtiofs-rootfs.sock \ + --memory size=2G,shared=on \ + --cmdline "console=hvc0 rootfstype=virtiofs root=/dev/root ro debug" \ + --api-socket $PWD/ch.sock \ + --rng \ + --net ... +``` + +Note: an important part of the above is the `tag=/dev/root` and +`root=/dev/root` parts. For whatever reason, it would only work with that as +the tag. + +Note: another important bit is that the memory is shared. This is required for +virtiofs + +## Message from the author + +If you find any issues or have suggestions, feel free to reach out to @iggy on +the cloud-hypervisor slack. Also if this works for you, I'd like to know as +well. It would also be nice to get steps for preparing other distribution root +filesystems. \ No newline at end of file diff --git a/vendor/cube-hypervisor/docs/vsock.md b/vendor/cube-hypervisor/docs/vsock.md new file mode 100644 index 0000000..ba8e0d6 --- /dev/null +++ b/vendor/cube-hypervisor/docs/vsock.md @@ -0,0 +1,84 @@ +# VSOCK support + +VSOCK provides a way for guest and host to communicate through a socket. VSOCK sockets support both stream and datagram types. + +The `virtio-vsock` is based on the [Firecracker](https://github.com/firecracker-microvm/firecracker/blob/main/docs/vsock.md) implementation, where additional details can be found. + +## What is a CID? + +CID is a 32-bit context identifier describing the source or destination. In combination with the port, the complete addressing can be achieved to describe multiple listeners running on the same machine. + +The table below depicts the well known CID values: + +| CID | Description | +|-----|-------------| +| -1 | Random CID | +| 0 | Hypervisor | +| 1 | Loopback | +| 2 | Host | + +## Prerequisites + +### Kernel Requirements + +Host kernel: CONFIG_VHOST_VSOCK + +Guest kernel: CONFIG_VIRTIO_VSOCKETS + +### Nested VM support + +Linux __v5.5__ or newer is required for the L1 VM. + +### Loopback support + +Linux __v5.6__ or newer is required. + +## Establishing VSOCK Connection + +VSOCK device becomes available with `--vsock` option passed by the VM start. Cloud Hypervisor can be invoked for instance as below: + +```bash +cloud-hypervisor \ + --cpus boot=1 \ + --memory size=4G \ + --firmware CLOUDHV.fd \ + --disk path=jammy-server-cloudimg.raw \ + --vsock cid=3,socket=/tmp/ch.vsock +``` + +The examples use __socat__ `>=1.7.4` to illustrate the VSOCK functionality. However, there are other tools supporting VSOCK, like [ncat](https://stefano-garzarella.github.io/posts/2019-11-08-kvmforum-2019-vsock/). + +### Connecting from Host to Guest + +The host starts to listen on the defined port: + +`$ socat - VSOCK-LISTEN:1234` + +Once the host is listening, the guest can send data: + +`echo -e "CONNECT 1234\\nHello from host!" | socat - UNIX-CONNECT:/tmp/ch.vsock + +Note the string `CONNECT ` prepended to the actual data. It is possible for the guest to start listening on different ports, thus the specific command is needed to instruct VSOCK to which listener the host wants to connect. It needs to be sent once per connection. Once the connection established, data transfers can take place directly. + +### Connecting from Guest to Host + +This first requires a listening UNIX socket on the host side. The UNIX socket path has to be constructed by using the socket path used at the VM launch time with appended `_` and the port number to be used on the guest side. As in the example above, if we'd intended to connect from the guest to the port `1234`, the Unix socket path on the host side would be `/tmp/ch.vsock_1234`. + +Also note that the CID used on the guest side is the well known CID value `2`. + +Listening on the host side: + +`$ socat - UNIX-LISTEN:/tmp/ch.vsock_1234` + +From the guest: + +`$ echo -e "Hello from guest!" | socat - VSOCK-CONNECT:2:1234` + +## Links + +- [virtio-vsock in QEMU, Firecracker and Linux: Status, Performance and Challenges](https://kvmforum2019.sched.com/event/TmwK) +- [Leveraging virtio-vsock in the cloud and containers](https://archive.fosdem.org/2021/schedule/event/vai_virtio_vsock/) +- [VSOCK man page](https://manpages.ubuntu.com/manpages/focal/man7/vsock.7.html) +- [https://stefano-garzarella.github.io/posts/2020-02-20-vsock-nested-vms-loopback/](https://stefano-garzarella.github.io/posts/2020-02-20-vsock-nested-vms-loopback/) +- [https://github.com/firecracker-microvm/firecracker/blob/main/docs/vsock.md](https://github.com/firecracker-microvm/firecracker/blob/main/docs/vsock.md) + diff --git a/vendor/cube-hypervisor/docs/windows.md b/vendor/cube-hypervisor/docs/windows.md new file mode 100644 index 0000000..26bcc82 --- /dev/null +++ b/vendor/cube-hypervisor/docs/windows.md @@ -0,0 +1,347 @@ +# Windows Support + +Starting with the release version [0.10.0](https://github.com/cloud-hypervisor/cloud-hypervisor/releases/tag/v0.10.0), Cloud Hypervisor supports Windows guests. + +__Requirements__ + +- Host with KVM enabled +- [UEFI](uefi.md) capable Windows guest image with Virtio drivers integrated + +Any modern Windows Server version is compatible. Cloud Hypervisor has been successfully tested with Windows Server 2019 and Windows Server Core 2004. + +At the current stage, only UEFI capable Windows images are supported. This implies the presence of the OVMF firmware during the Windows installation and in any subsequent usage. BIOS boot is not supported. + +The subsequent sections will tell, in detail, how to prepare an appropriate Windows image. + +## Image Preparation + +### Installation using the stock Windows ISO + +__Prerequisites__ + +- QEMU, version >=5.0.0 is recommended. +- Windows installation ISO. Obtained through MSDN, Visual Studio subscription, evaluation center, etc. +- [VirtIO driver ISO](https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/) +- Suitable firmware for Cloud Hypervisor (`CLOUDHV.fd`) and for QEMU (`OVMF.fd`) +- With the suggested image size of 30G, there should be enough free disk space to hold the installation ISO and any other necessary files + +This step currently requires QEMU to install Windows onto the guest. QEMU is only used at the preparation stage, the resulting image is then fully functional with Cloud Hypervisor. + +Preparing several command parts as these will be used in the follow up sections as well. + +```shell +IMG_FILE=windows-disk.raw +WIN_ISO_FILE=en_windows_server_version_2004_updated_may_2020_x64_dvd_1e7f1cfa.iso +VIRTIO_ISO_FILE=virtio-win-0.1.185.iso +OVMF_DIR=./FV +``` + +Create an empty image file, `raw` is supported. +```shell +qemu-img create -f raw $IMG_FILE 30G +``` + +Begin the Windows installation process under QEMU +```shell +qemu-system-x86_64 \ + -machine q35,accel=kvm \ + -cpu host \ + -m 4G \ + -bios ./$OVMF_DIR/OVMF_CODE.fd \ + -cdrom ./$WIN_ISO_FILE \ + -drive file=./$VIRTIO_ISO_FILE,index=0,media=cdrom + -drive if=none,id=root,file=./$IMG_FILE \ + -device virtio-blk-pci,drive=root,disable-legacy=on \ + -device virtio-net-pci,netdev=mynet0,disable-legacy=on \ + -netdev user,id=mynet0 \ + -vga std +``` + +Before the installation can proceed, point the Windows installation program to the VirtIO disk and install the necessary storage controller drivers. After that, the attached hard drive will become visible and the actual installation can commence. + +After the installation has completed, proceed further to the configuration section. QEMU will be needed at least once more to enable the Windows Special Administration Console (SAC) and to possibly install extra device drivers. + +## Image Usage + +The basic command to boot a Windows image. The configuration section should be checked before executing it for the first time. + +```shell +cloud-hypervisor \ + --kernel ./$OVMF_DIR/CLOUDHV.fd \ + --disk path=./$IMG_FILE \ + --cpus boot=1,kvm_hyperv=on \ + --memory size=4G \ + --serial tty \ + --console off \ + --net tap= +``` + +It is necessary to always: + +- Carry the OVMF firmware in the `--kernel` option +- Add `kvm_hyperv=on` to the `--cpus` option + +In cases where the host processor supports address space > 39 bits, it might be necessary to limit the address space. It can be done by appending the option `max_phys_bits=X` to the `--cpus` parameter, where `X` is the number of bits to be supported. Windows was tested to support at least 39-bit address space. + +To daemonize the Cloud Hypervisor process, `nohup` can be used. Some STDIO redirections might need to be done. In a simple case it is sufficient to just redirect all the output to `/dev/null`. + +## Image Configuration + +### Device Drivers + +After the Windows installation has finished under QEMU, there might be still devices with no drivers installed. This might happen for example, when a device was not used during the installation. In particular it is important to ensure that the VirtIO network device is setup correctly because further steps for the configuration and the usage require network in most case. + +Boot once more under QEMU and use the [Device Manager](https://support.microsoft.com/en-in/help/4028443/windows-10-update-drivers), to ensure all the device drivers, and especially the network card, are installed correctly. Also, as Cloud Hypervisor can introduce new devices, it is advisable to repeat the procedure while booted under Cloud Hypervisor, when the RDP access to the image is functional. + +### Windows Special Administration Console (SAC) enablement + +SAC provides a text based console access to the Windows guest. As Cloud Hypervisor doesn't implement a VGA adaptor, SAC is an important instrument for the Windows guest management. + +Boot the Windows image under QEMU and execute the below commands to permanently enable SAC + +```cmd +bcdedit /emssettings emsport:1 emsbaudrate:115200 +bcdedit /ems on +bcdedit /bootems on +``` + +Once SAC is enabled, the image can be booted under Cloud Hypervisor. The SAC prompt will show up + +
+Computer is booting, SAC started and initialized.                               
+                                                                                
+Use the "ch -?" command for information about using channels.                   
+Use the "?" command for general help.                                           
+                                                                                
+                                                                                
+SAC>
+
+ +To open a console on the guest, the command sequence below can be used +
+SAC>cmd
+The Command Prompt session was successfully launched.
+SAC>
+EVENT:   A new channel has been created.  Use "ch -?" for channel help.
+Channel: Cmd0001
+SAC>ch -si 1
+
+ +See also the [links](#Links) section for a more extended SAC documentation. + +## Network + +This section illustrates the Windows specific aspects of the VM network configuration. + +### Basic Networking + +As the simplest option, using `--net tap=` in the Cloud Hypervisor command line will create a `vmtapX` device on the host with the default IPv4 address `192.168.249.1`. After SAC becomes available, the guest configuration can be set with + +
+SAC>i 10 192.168.249.2 255.255.255.0 192.168.249.1
+
+ +Where `10` is the device index as shown by the `i` command. + +### Guest Internet Connectivity + +Additional steps are necessary to provide the guest with internet access. + +- On the guest, add the DNS server either by using `netsh` or by opening `Network and Connectivity Center` and editing the adapter properties. +- On the host, configure the traffic forwarding. Replace the `NET_DEV` with the name of your network device. +```shell +NET_DEV=wlp3s0 +sysctl -w net.ipv4.ip_forward=1 +iptables -t nat -A POSTROUTING -o $NET_DEV -j MASQUERADE +``` + +### Remote Desktop Protocol (RDP) enablement + +#### Using QEMU + - Execute `SystemPropertiesRemote` + - In the properties window, choose "Allow remote connections to this computer" + - Click "Select Users" and add some user to the allow list +#### Using powershell +```powershell +Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\" -Name "fDenyTSConnections" -Value 0 +Enable-NetFirewallRule -DisplayGroup "Remote Desktop" +Add-LocalGroupMember -Group "Remote Desktop Users" -Member someuser +``` + +Administrators can always RDP, non administrator users have to be explicitly enabled. + +Once the configuration is set, RDP clients can connect to `192.168.249.2`. + +### SSH + +#### Enable using powershell + +```powershell +Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 +Start-Service sshd +Set-Service -Name sshd -StartupType ‘Automatic’ +``` + +This allows for SSH login from a remote machine, for example through the `administrator` user: `ssh administrator@192.168.249.2`. For a more detailed OpenSSH guide, please follow the MSDN article from the [links](#links) section. + +## Hotplug capability + +CPU hotplug is supported. The VM operating system needs to support hotplug and be appropriately licensed. SKU limitations like constraints on the number of cores are to be taken into consideration. Note, that Windows doesn't support CPU hot-remove. When `ch-remote` is invoked to reduce the number of CPUs, the result will be visible after the OS reboot within the same hypervisor instance. + +RAM hotplug is supported. Note, that while the `pnpmem.sys` driver in use supports RAM hot-remove, the RAM being unplugged has to be not in use and have no reserved pages. In most cases it means, hot-remove won't work. Same as with the CPU hot-remove, when `ch-remote` is invoked to reduce the RAM size, the result will be visible after the OS reboot. + +Network device hotplug and hot-remove are supported. + +Disk hotplug and hot-remove are supported. After the device has been hotplugged, it will need to be onlined from within the guest. Among other tools, powershell applets `Get-Disk` and `Set-Disk` can be used for the disk configuration and activation. + +## Debugging + +The Windows guest debugging process relies heavily on QEMU and [socat](http://www.dest-unreach.org/socat/). The procedure requires two Windows VMs: + +- A debugger VM running under QEMU. +- A debuggee, a Windows VM that has been created in the previous steps, running under Cloud Hypervisor or QEMU. + +The connection between both guests happens over TCP, whereby on the guest side it is automatically translated to a COM port. Because the VMs are connected through TCP, the debugging infrastructure can be distributed over the network. The serial port, while slowly transferring data, is common enough to support a wide range of cases and tools. + +In this excercise, [WinDbg](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/) is used. Any other debugger of choice with the ability to use serial connection can be used instead. + +### Debugger and Debuggee + +#### WinDbg VM + +For simplicity, the debugger VM is supposed to be only running under QEMU. It will require VGA and doesn't necessarily depend on UEFI. As an OS, it can carry any supported Windows OS where the debugger of choice can be installed. The simplest way is to follow the image preparation instructions from the previous chapter, but avoid using the OVMF firmware. It is also not required to use VirtIO drivers, whereby it might be useful in some cases. Though, while creating the image file for the debugger VM, be sure to choose a sufficient disk size that counts in the need to save the corresponding debug symbols and sources. + +To create the debugger Windows VM, the following command can be used: + +```shell +qemu-system-x86_64 \ + -machine q35,accel=kvm \ + -cpu host \ + -smp 1 \ + -m 4G \ + -cdrom ./$WIN_ISO_FILE \ + -drive file=./$VIRTIO_ISO_FILE,index=0,media=cdrom + -drive if=none,id=root,file=./windbg-disk.raw \ + -device virtio-blk-pci,drive=root,disable-legacy=on \ + -device virtio-net-pci,netdev=mynet0,disable-legacy=on \ + -netdev user,id=mynet0,net=192.168.178.0/24,host=192.168.178.1,dhcpstart=192.168.178.64,hostname=windbg-host \ + -vga std +``` + +A non server Windows OS like Windows 10 can be used to carry the debugging tools in the debugger VM. + +#### Debuggee VM + +The debuggee VM is the one that we've learned to configure and run in the first section. There might be various reasons to debug. For example, there could be an issue in the Windows guest with an emulated device or an included driver. Or, we might want to develop a custom feature like a kernel driver to be available in the guest. + +Note, that there are several ways to debug Windows, not all of them need to be enabled at the same time. For example, if developing a kernel module, the only useful options would be to configure for the serial debugging and enable the kernel debug. In that case, any crash or misbehavior in the boot loader or kernel would be ignored. The commands below must be run as administrator on the debuggee guest VM. + +##### Turn On Serial Debugging + +This will configure the debugging to be enabled and instruct to use the serial port for it. + +```cmd +bcdedit /dbgsettings serial debugport:1 baudrate:115200 +``` + +##### Turn On Kernel Debuging + +```cmd +bcdedit /debug on +``` + +##### Turn On Boot Loader Debug + +```cmd +bcdedit /bootdebug on +``` + +##### Turn on boot manager debug + +```cmd +bcdedit /set {bootmgr} bootdebug on +``` + +##### Disable Recovery Screen On Boot Failure + +There could be a situation, where a crash is debugged. In such cases, the guest could be left in an inconsistent state. The default Windows behavior would be to boot into the recovery screen, however in some cases it might be not desired. To make Windows ignore failures and always proceed to booting the OS, use the command below: + +```cmd +bcdedit /set {default} bootstatuspolicy ignoreallfailures +``` + +### Debugging Process + +#### Invoke the WinDbg VM + +```shell +qemu-system-x86_64 \ + -machine q35,accel=kvm \ + -cpu host \ + -smp 1 \ + -m 4G \ + -drive if=none,id=root,file=./windbg-disk.raw \ + -device virtio-blk-pci,drive=root,disable-legacy=on \ + -serial tcp::4445,server,nowait \ + -device virtio-net-pci,netdev=mynet0,disable-legacy=on \ + -netdev user,id=mynet0,net=192.168.178.0/24,host=192.168.178.1,dhcpstart=192.168.178.64,hostname=windbg-host \ + -vga std +``` + +Note, this VM has the networking enabled. It is needed, because symbols and sources might need to be fetched from a network location. + +Also, notice the `-serial` parameter - that's what does the magic on exposing the serial port to the guest while connecting the debugger VM with a client VM through the network. SAC/EMS needs to be disabled in the debugger VM, as otherwise the COM device might be blocked. + +Hereafter, WinDbg can be started using a command below: + +```cmd +set _NT_DEBUG_PORT=com1 +set _NT_DEBUG_BAUD_RATE=115200 + +windbg -v -d -k +``` + +Once started, WinDbg will wait for an incoming connection which is going to be initialized by the debuggee VM started in the next section. + +#### Invoke the Debuggee VM + +##### Under QEMU + +Essentially it would be the command like depicted in the guest preparation sections, with a few modifications: +```shell +qemu-system-x86_64 \ + -machine q35,accel=kvm \ + -cpu host \ + -m 4G \ + -bios ./$OVMF_DIR/OVMF_CODE.fd \ + -cdrom ./$WIN_ISO_FILE \ + -drive file=./$VIRTIO_ISO_FILE,index=0,media=cdrom + -drive if=none,id=root,file=./$IMG_FILE \ + -device virtio-blk-pci,drive=root,disable-legacy=on \ + -device virtio-net-pci,netdev=mynet0,disable-legacy=on \ + -netdev user,id=mynet0 \ + -serial tcp:127.0.0.1:4445 \ + -vga std +``` + +It is to see, that `-serial` parameter is used here, to establish the connection with the debugger VM. + +To disable HPET, attach `--no-hpet`. To enable hypervisor reference timer, use `-cpu host,hv-time`. These and other options can be used to achieve better [Hyper-V compatibility](https://archive.fosdem.org/2019/schedule/event/vai_enlightening_kvm/attachments/slides/2860/export/events/attachments/vai_enlightening_kvm/slides/2860/vkuznets_fosdem2019_enlightening_kvm.pdf). + +##### Cloud Hypervisor + +The `socat` tool is used to establish the QEMU compatible behavior. Here as well, the Cloud Hypervisor command used to run the Windows guest is to be used. Put the command into a shell script: + +`socat SYSTEM:"./ch-script",openpty,raw,echo=0 TCP:localhost:4445` + +The reason to pack the command into the shell script is that the command might contain a comma. When using SYSTEM, the shell command can't contain `,` or `!!`. + +## Links + +- [Fedora VirtIO guide for Windows](https://docs.fedoraproject.org/en-US/quick-docs/creating-windows-virtual-machines-using-virtio-drivers/) +- [VirtIO driver binaries](https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/) +- [VirtIO driver sources](https://github.com/virtio-win/kvm-guest-drivers-windows) +- [Emergency Management Services](https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2003/cc787940(v=ws.10)) +- [OpenSSH server/client configuration](https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse) +- [Windows guest debugging under KVM](https://www.linux-kvm.org/page/WindowsGuestDrivers/GuestDebugging) +- ["ENLIGHTENING" KVM](https://archive.fosdem.org/2019/schedule/event/vai_enlightening_kvm/attachments/slides/2860/export/events/attachments/vai_enlightening_kvm/slides/2860/vkuznets_fosdem2019_enlightening_kvm.pdf) diff --git a/vendor/cube-hypervisor/event_monitor/Cargo.toml b/vendor/cube-hypervisor/event_monitor/Cargo.toml new file mode 100644 index 0000000..8466c5d --- /dev/null +++ b/vendor/cube-hypervisor/event_monitor/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "event_monitor" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[dependencies] +libc = "0.2.137" +serde = { workspace = true, features = ["rc", "derive"] } +serde_json = "1.0.87" diff --git a/vendor/cube-hypervisor/event_monitor/src/lib.rs b/vendor/cube-hypervisor/event_monitor/src/lib.rs new file mode 100644 index 0000000..e67991a --- /dev/null +++ b/vendor/cube-hypervisor/event_monitor/src/lib.rs @@ -0,0 +1,82 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use serde::Serialize; +use std::borrow::Cow; +use std::collections::HashMap; +use std::fs::File; +use std::io::Write; +use std::os::unix::io::AsRawFd; +use std::sync::Mutex; +use std::time::{Duration, Instant}; + +static mut MONITOR: Option> = None; + +/// This function must only be called once from the main process before any threads +/// are created to avoid race conditions +pub fn set_monitor(file: File) -> Result<(), std::io::Error> { + assert!(unsafe { MONITOR.is_none() }); + let fd = file.as_raw_fd(); + let ret = unsafe { + let mut flags = libc::fcntl(fd, libc::F_GETFL); + flags |= libc::O_NONBLOCK; + libc::fcntl(fd, libc::F_SETFL, flags) + }; + if ret < 0 { + return Err(std::io::Error::last_os_error()); + } + // SAFETY: MONITOR is None. Nobody else can hold a reference to it. + unsafe { + MONITOR = Some(Mutex::new((file, Instant::now()))); + }; + Ok(()) +} + +#[derive(Serialize)] +struct Event<'a> { + timestamp: Duration, + source: &'a str, + event: &'a str, + properties: Option<&'a HashMap, Cow<'a, str>>>, +} + +pub fn event_log(source: &str, event: &str, properties: Option<&HashMap, Cow>>) { + // SAFETY: MONITOR is always in a valid state (None or Some). + if let Some(mutex) = unsafe { MONITOR.as_ref() } { + let mut guard = mutex.lock().unwrap(); + let e = Event { + timestamp: guard.1.elapsed(), + source, + event, + properties, + }; + serde_json::to_writer_pretty(&guard.0, &e).ok(); + + guard.0.write_all(b"\n\n").ok(); + } +} + +/* + Through the use of Cow<'a, str> it is possible to use String as well as + &str as the parameters: + e.g. + event!("cpu_manager", "create_vcpu", "id", cpu_id.to_string()); +*/ +#[macro_export] +macro_rules! event { + ($source:expr, $event:expr) => { + $crate::event_log($source, $event, None) + }; + ($source:expr, $event:expr, $($key:expr, $value:expr),*) => { + { + let mut properties = ::std::collections::HashMap::new(); + $( + properties.insert($key.into(), $value.into()); + )+ + $crate::event_log($source, $event, Some(&properties)) + } + }; + +} diff --git a/vendor/cube-hypervisor/event_notifier/Cargo.toml b/vendor/cube-hypervisor/event_notifier/Cargo.toml new file mode 100644 index 0000000..2013d69 --- /dev/null +++ b/vendor/cube-hypervisor/event_notifier/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "event_notifier" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +lazy_static = "1.2" +log = "0.4.22" \ No newline at end of file diff --git a/vendor/cube-hypervisor/event_notifier/src/lib.rs b/vendor/cube-hypervisor/event_notifier/src/lib.rs new file mode 100644 index 0000000..9a5692a --- /dev/null +++ b/vendor/cube-hypervisor/event_notifier/src/lib.rs @@ -0,0 +1,82 @@ +use std::sync::mpsc::Sender; +use std::sync::Mutex; + +use lazy_static::lazy_static; +use log::warn; + +lazy_static! { + static ref NOTIFIER: Mutex = Mutex::new(EventNotifier::new()); +} + +/// Supported Notify Event +/// +/// Note VmShutdown event should enable feature `lib_support` for lib. +/// +#[derive(Debug, PartialEq, Eq)] +pub enum NotifyEvent { + VmShutdown, + VsockServerReady, + RestoreReady, + SysStart, + MigrationComplete, + MigrationFail, +} + +#[derive(Default)] +pub struct EventNotifier { + notifier: Option>, +} + +impl EventNotifier { + pub fn new() -> Self { + Self { notifier: None } + } + pub fn init(&mut self, notifier: Sender) { + self.notifier = Some(notifier); + } + pub fn send(&mut self, event: NotifyEvent) { + if let Some(sender) = self.notifier.as_ref() { + if let Err(e) = sender.send(event) { + warn!("event notifier send err: {e}, remove sender."); + self.notifier.take(); + } + } + } +} + +pub fn setup_notifier(notifier: Sender) { + let mut guard = NOTIFIER.lock().unwrap(); + guard.init(notifier); +} + +pub fn send_event(event: NotifyEvent) { + let mut guard = NOTIFIER.lock().unwrap(); + guard.send(event); +} + +#[macro_export] +macro_rules! event_notify { + ($event:expr) => { + $crate::send_event($event) + }; +} + +#[cfg(test)] +mod tests { + use std::sync::mpsc::channel; + use std::thread; + + use crate::{setup_notifier, NotifyEvent}; + + #[test] + fn test_event_notify() { + let (sender, receiver) = channel(); + let handle = thread::spawn(move || { + setup_notifier(sender); + event_notify!(NotifyEvent::VmShutdown); + }); + let evt = receiver.recv().unwrap(); + assert_eq!(evt, NotifyEvent::VmShutdown); + handle.join().unwrap(); + } +} diff --git a/vendor/cube-hypervisor/fix_lock.py b/vendor/cube-hypervisor/fix_lock.py new file mode 100644 index 0000000..c5c3b8d --- /dev/null +++ b/vendor/cube-hypervisor/fix_lock.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +"""Resolve Cargo.lock conflicts by keeping incoming (theirs) version for each block.""" + +with open('Cargo.lock', 'r') as f: + lines = f.read().split('\n') + +result = [] +i = 0 +while i < len(lines): + if lines[i].startswith('<<<<<<< '): + i += 1 + # Skip HEAD lines + while i < len(lines) and not lines[i].startswith('======='): + i += 1 + i += 1 # skip ======= + # Keep incoming lines + while i < len(lines) and not lines[i].startswith('>>>>>>>'): + result.append(lines[i]) + i += 1 + i += 1 # skip >>>>>>> marker + else: + result.append(lines[i]) + i += 1 + +with open('Cargo.lock', 'w') as f: + f.write('\n'.join(result)) + +print('Cargo.lock conflicts resolved') diff --git a/vendor/cube-hypervisor/fuzz/.gitignore b/vendor/cube-hypervisor/fuzz/.gitignore new file mode 100644 index 0000000..572e03b --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/.gitignore @@ -0,0 +1,4 @@ + +target +corpus +artifacts diff --git a/vendor/cube-hypervisor/fuzz/Cargo.toml b/vendor/cube-hypervisor/fuzz/Cargo.toml new file mode 100644 index 0000000..508e33a --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/Cargo.toml @@ -0,0 +1,113 @@ +[package] +name = "cloud-hypervisor-fuzz" +version = "0.0.0" +authors = ["Automatically generated"] +publish = false +edition = "2021" + +[package.metadata] +cargo-fuzz = true + +[dependencies] +block_util = { path = "../block_util" } +devices = { path = "../devices" } +epoll = "4.3.1" +libc = "0.2.135" +libfuzzer-sys = "0.4.5" +micro_http = { git = "https://github.com/firecracker-microvm/micro-http", branch = "main" } +once_cell = "1.16.0" +qcow = { path = "../qcow" } +seccompiler = { workspace = true } +vhdx = { path = "../vhdx" } +virtio-devices = { path = "../virtio-devices" } +virtio-queue = "0.11.0" +vmm = { path = "../vmm" } +vmm-sys-util = "0.12.1" +vm-memory = "0.14.1" +vm-device = { path = "../vm-device" } +vm-virtio = { path = "../vm-virtio" } + +[dependencies.cloud-hypervisor] +path = ".." + +# Prevent this from interfering with workspaces +[workspace] +members = ["."] + +[[bin]] +name = "balloon" +path = "fuzz_targets/balloon.rs" +test = false +doc = false + +[[bin]] +name = "block" +path = "fuzz_targets/block.rs" +test = false +doc = false + +[[bin]] +name = "cmos" +path = "fuzz_targets/cmos.rs" +test = false +doc = false + +[[bin]] +name = "console" +path = "fuzz_targets/console.rs" +test = false +doc = false + +[[bin]] +name = "http_api" +path = "fuzz_targets/http_api.rs" +test = false +doc = false + +[[bin]] +name = "iommu" +path = "fuzz_targets/iommu.rs" +test = false +doc = false + +[[bin]] +name = "mem" +path = "fuzz_targets/mem.rs" +test = false +doc = false + +[[bin]] +name = "pmem" +path = "fuzz_targets/pmem.rs" +test = false +doc = false + +[[bin]] +name = "qcow" +path = "fuzz_targets/qcow.rs" +test = false +doc = false + +[[bin]] +name = "rng" +path = "fuzz_targets/rng.rs" +test = false +doc = false + +[[bin]] +name = "serial" +path = "fuzz_targets/serial.rs" +test = false +doc = false + +[[bin]] +name = "vhdx" +path = "fuzz_targets/vhdx.rs" +test = false +doc = false + +[[bin]] +name = "watchdog" +path = "fuzz_targets/watchdog.rs" +test = false +doc = false diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/balloon.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/balloon.rs new file mode 100644 index 0000000..67be31a --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/balloon.rs @@ -0,0 +1,153 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use seccompiler::SeccompAction; +use std::os::unix::io::{AsRawFd, FromRawFd}; +use std::sync::Arc; +use virtio_devices::{VirtioDevice, VirtioInterrupt, VirtioInterruptType}; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic}; +use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +const QUEUE_DATA_SIZE: usize = 4; +const MEM_SIZE: usize = 512 * 1024; +const BALLOON_SIZE: u64 = 512 * 1024; +// Number of queues +const QUEUE_NUM: usize = 3; +// Max entries in the queue. +const QUEUE_SIZE: u16 = 64; +// Descriptor table alignment +const DESC_TABLE_ALIGN_SIZE: u64 = 16; +// Avalable ring alignment +const AVAIL_RING_ALIGN_SIZE: u64 = 2; +// Used ring alignment +const USED_RING_ALIGN_SIZE: u64 = 4; +// Descriptor table size +const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; +// Available ring size +const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; +// Used ring size +const USED_RING_SIZE: u64 = 6_u64 + 8 * QUEUE_SIZE as u64; + +fuzz_target!(|bytes| { + if bytes.len() < QUEUE_DATA_SIZE * QUEUE_NUM + || bytes.len() > (QUEUE_DATA_SIZE * QUEUE_NUM + MEM_SIZE) + { + return; + } + + let mut balloon = virtio_devices::Balloon::new( + "fuzzer_balloon".to_owned(), + BALLOON_SIZE, + true, + true, + SeccompAction::Allow, + EventFd::new(EFD_NONBLOCK).unwrap(), + None, + ) + .unwrap(); + + let queue_data = &bytes[..QUEUE_DATA_SIZE * QUEUE_NUM]; + let mem_bytes = &bytes[QUEUE_DATA_SIZE * QUEUE_NUM..]; + + // Setup the guest memory with the input bytes + let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE)]).unwrap(); + if mem.write_slice(mem_bytes, GuestAddress(0 as u64)).is_err() { + return; + } + let guest_memory = GuestMemoryAtomic::new(mem); + + // Setup the virt queues with the input bytes + let mut queues = setup_virt_queues( + &[ + &queue_data[..QUEUE_DATA_SIZE].try_into().unwrap(), + &queue_data[QUEUE_DATA_SIZE..QUEUE_DATA_SIZE * 2] + .try_into() + .unwrap(), + &queue_data[QUEUE_DATA_SIZE * 2..QUEUE_DATA_SIZE * 3] + .try_into() + .unwrap(), + ], + 0, + ); + + let inflate_q = queues.remove(0); + let inflate_evt = EventFd::new(0).unwrap(); + let inflate_queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(inflate_evt.as_raw_fd())) }; + let deflate_q = queues.remove(0); + let deflate_evt = EventFd::new(0).unwrap(); + let deflate_queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(deflate_evt.as_raw_fd())) }; + let reporting_q = queues.remove(0); + let reporting_evt = EventFd::new(0).unwrap(); + let reporting_queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(reporting_evt.as_raw_fd())) }; + + // Kick the 'queue' events before activate the balloon device + inflate_queue_evt.write(1).unwrap(); + deflate_queue_evt.write(1).unwrap(); + reporting_queue_evt.write(1).unwrap(); + + balloon + .activate( + guest_memory, + Arc::new(NoopVirtioInterrupt {}), + vec![ + (0, inflate_q, inflate_evt), + (1, deflate_q, deflate_evt), + (2, reporting_q, reporting_evt), + ], + ) + .ok(); + + // Wait for the events to finish and balloon device worker thread to return + balloon.wait_for_epoll_threads(); +}); + +pub struct NoopVirtioInterrupt {} + +impl VirtioInterrupt for NoopVirtioInterrupt { + fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { + Ok(()) + } +} + +macro_rules! align { + ($n:expr, $align:expr) => {{ + (($n + $align - 1) / $align) * $align + }}; +} + +fn setup_virt_queues(bytes: &[&[u8; QUEUE_DATA_SIZE]], base_addr: u64) -> Vec { + let mut queues = Vec::new(); + let mut base_addr = base_addr; + for b in bytes { + let mut q = Queue::new(QUEUE_SIZE).unwrap(); + + let desc_table_addr = align!(base_addr, DESC_TABLE_ALIGN_SIZE); + let avail_ring_addr = align!(desc_table_addr + DESC_TABLE_SIZE, AVAIL_RING_ALIGN_SIZE); + let used_ring_addr = align!(avail_ring_addr + AVAIL_RING_SIZE, USED_RING_ALIGN_SIZE); + q.try_set_desc_table_address(GuestAddress(desc_table_addr)) + .unwrap(); + q.try_set_avail_ring_address(GuestAddress(avail_ring_addr)) + .unwrap(); + q.try_set_used_ring_address(GuestAddress(used_ring_addr)) + .unwrap(); + + q.set_next_avail(b[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small + q.set_next_used(b[1] as u16); + q.set_event_idx(b[2] % 2 != 0); + q.set_size(b[3] as u16 % QUEUE_SIZE); + + q.set_ready(true); + queues.push(q); + + base_addr = used_ring_addr + USED_RING_SIZE; + } + + queues +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/block.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/block.rs new file mode 100644 index 0000000..99f85d2 --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/block.rs @@ -0,0 +1,129 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +#![no_main] + +use block_util::{async_io::DiskFile, raw_sync::RawFileDiskSync}; +use libfuzzer_sys::fuzz_target; +use seccompiler::SeccompAction; +use std::ffi; +use std::fs::File; +use std::io; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::path::PathBuf; +use std::sync::Arc; +use virtio_devices::{Block, VirtioDevice, VirtioInterrupt, VirtioInterruptType}; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic}; +use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +const QUEUE_DATA_SIZE: usize = 4; +const MEM_SIZE: usize = 256 * 1024 * 1024; +// Max entries in the queue. +const QUEUE_SIZE: u16 = 256; +// Guest physical address for descriptor table. +const DESC_TABLE_ADDR: u64 = 0; +const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; +// Guest physical address for available ring +const AVAIL_RING_ADDR: u64 = DESC_TABLE_ADDR + DESC_TABLE_SIZE; +const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; +// Guest physical address for used ring (requires to 4-bytes aligned) +const USED_RING_ADDR: u64 = (AVAIL_RING_ADDR + AVAIL_RING_SIZE + 3) & !3_u64; + +fuzz_target!(|bytes| { + if bytes.len() < QUEUE_DATA_SIZE || bytes.len() > (QUEUE_DATA_SIZE + MEM_SIZE) { + return; + } + + let queue_data = &bytes[..QUEUE_DATA_SIZE]; + let mem_bytes = &bytes[QUEUE_DATA_SIZE..]; + + // Create a virtio-block device backed by a synchronous raw file + let shm = memfd_create(&ffi::CString::new("fuzz").unwrap(), 0).unwrap(); + let disk_file: File = unsafe { File::from_raw_fd(shm) }; + let qcow_disk = Box::new(RawFileDiskSync::new(disk_file)) as Box; + let mut block = Block::new( + "tmp".to_owned(), + qcow_disk, + PathBuf::from(""), + false, + false, + 2, + 256, + SeccompAction::Allow, + None, + EventFd::new(EFD_NONBLOCK).unwrap(), + None, + ) + .unwrap(); + + // Setup the virt queue with the input bytes + let q = setup_virt_queue(queue_data.try_into().unwrap()); + + // Setup the guest memory with the input bytes + let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE)]).unwrap(); + if mem.write_slice(mem_bytes, GuestAddress(0 as u64)).is_err() { + return; + } + let guest_memory = GuestMemoryAtomic::new(mem); + + let evt = EventFd::new(0).unwrap(); + let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(evt.as_raw_fd())) }; + + // Kick the 'queue' event before activate the block device + queue_evt.write(1).unwrap(); + + block + .activate( + guest_memory, + Arc::new(NoopVirtioInterrupt {}), + vec![(0, q, evt)], + ) + .ok(); + + // Wait for the events to finish and block device worker thread to return + block.wait_for_epoll_threads(); +}); + +fn memfd_create(name: &ffi::CStr, flags: u32) -> Result { + let res = unsafe { libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags) }; + + if res < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(res as RawFd) + } +} + +pub struct NoopVirtioInterrupt {} + +impl VirtioInterrupt for NoopVirtioInterrupt { + fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { + Ok(()) + } +} + +fn setup_virt_queue(bytes: &[u8; QUEUE_DATA_SIZE]) -> Queue { + let mut q = Queue::new(QUEUE_SIZE).unwrap(); + q.set_next_avail(bytes[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small + q.set_next_used(bytes[1] as u16); + q.set_event_idx(bytes[2] % 2 != 0); + q.set_size(bytes[3] as u16 % QUEUE_SIZE); + + q.try_set_desc_table_address(GuestAddress(DESC_TABLE_ADDR)) + .unwrap(); + q.try_set_avail_ring_address(GuestAddress(AVAIL_RING_ADDR)) + .unwrap(); + q.try_set_used_ring_address(GuestAddress(USED_RING_ADDR)) + .unwrap(); + q.set_ready(true); + + q +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/cmos.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/cmos.rs new file mode 100644 index 0000000..5afcfa2 --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/cmos.rs @@ -0,0 +1,48 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] +use devices::legacy::Cmos; +use libc::EFD_NONBLOCK; +use libfuzzer_sys::fuzz_target; +use vm_device::BusDevice; +use vmm_sys_util::eventfd::EventFd; + +fuzz_target!(|bytes| { + // Need at least 16 bytes for the test + if bytes.len() < 16 { + return; + } + + let mut below_4g = [0u8; 8]; + let mut above_4g = [0u8; 8]; + + below_4g.copy_from_slice(&bytes[0..8]); + above_4g.copy_from_slice(&bytes[8..16]); + + let mut cmos = Cmos::new( + u64::from_le_bytes(below_4g), + u64::from_le_bytes(above_4g), + EventFd::new(EFD_NONBLOCK).unwrap(), + ); + + let mut i = 16; + while i < bytes.len() { + let read = bytes.get(i).unwrap_or(&0) % 2 == 0; + i += 1; + + if read { + let offset = (bytes.get(i).unwrap_or(&0) % 2) as u64; + i += 1; + let mut out_bytes = vec![0]; + cmos.read(0, offset, &mut out_bytes); + } else { + let offset = (bytes.get(i).unwrap_or(&0) % 2) as u64; + i += 1; + let data = vec![*bytes.get(i).unwrap_or(&0)]; + i += 1; + cmos.write(0, offset, &data); + } + } +}); diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/console.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/console.rs new file mode 100644 index 0000000..02ee371 --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/console.rs @@ -0,0 +1,194 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use seccompiler::SeccompAction; +use std::fs::File; +use std::io::Write; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::sync::Arc; +use virtio_devices::{VirtioDevice, VirtioInterrupt, VirtioInterruptType}; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic}; +use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +macro_rules! align { + ($n:expr, $align:expr) => {{ + (($n + $align - 1) / $align) * $align + }}; +} + +const CONSOLE_INPUT_SIZE: usize = 128; +const QUEUE_DATA_SIZE: usize = 4; +const MEM_SIZE: usize = 32 * 1024 * 1024; +// Guest memory gap +const GUEST_MEM_GAP: u64 = 1 * 1024 * 1024; +// Guest physical address for the first virt queue +const BASE_VIRT_QUEUE_ADDR: u64 = MEM_SIZE as u64 + GUEST_MEM_GAP; +// Number of queues +const QUEUE_NUM: usize = 2; +// Max entries in the queue. +const QUEUE_SIZE: u16 = 256; +// Descriptor table alignment +const DESC_TABLE_ALIGN_SIZE: u64 = 16; +// Used ring alignment +const USED_RING_ALIGN_SIZE: u64 = 4; +// Descriptor table size +const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; +// Available ring size +const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; +// Padding size before used ring +const PADDING_SIZE: u64 = align!(AVAIL_RING_SIZE, USED_RING_ALIGN_SIZE) - AVAIL_RING_SIZE; +// Used ring size +const USED_RING_SIZE: u64 = 6_u64 + 8 * QUEUE_SIZE as u64; +// Virtio-queue size in bytes +const QUEUE_BYTES_SIZE: usize = align!( + DESC_TABLE_SIZE + AVAIL_RING_SIZE + PADDING_SIZE + USED_RING_SIZE, + DESC_TABLE_ALIGN_SIZE +) as usize; + +fuzz_target!(|bytes| { + if bytes.len() < (QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE) * QUEUE_NUM + CONSOLE_INPUT_SIZE + || bytes.len() + > (QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE) * QUEUE_NUM + CONSOLE_INPUT_SIZE + MEM_SIZE + { + return; + } + + let (pipe_rx, mut pipe_tx) = create_pipe().unwrap(); + let output = unsafe { + File::from_raw_fd( + memfd_create(&std::ffi::CString::new("fuzz_console_output").unwrap()).unwrap(), + ) + }; + let endpoint = virtio_devices::Endpoint::FilePair(output, pipe_rx); + + let (mut console, _) = virtio_devices::Console::new( + "fuzzer_console".to_owned(), + endpoint, + None, // resize_pipe + false, // iommu + SeccompAction::Allow, + EventFd::new(EFD_NONBLOCK).unwrap(), + None, + ) + .unwrap(); + + let console_input_bytes = &bytes[..CONSOLE_INPUT_SIZE]; + let queue_data = &bytes[CONSOLE_INPUT_SIZE..CONSOLE_INPUT_SIZE + QUEUE_DATA_SIZE * QUEUE_NUM]; + let queue_bytes = &bytes[CONSOLE_INPUT_SIZE + QUEUE_DATA_SIZE * QUEUE_NUM + ..CONSOLE_INPUT_SIZE + (QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE) * QUEUE_NUM]; + let mem_bytes = &bytes[CONSOLE_INPUT_SIZE + (QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE) * QUEUE_NUM..]; + + // Setup the virt queues with the input bytes + let mut queues = setup_virt_queues( + &[ + &queue_data[..QUEUE_DATA_SIZE].try_into().unwrap(), + &queue_data[QUEUE_DATA_SIZE..QUEUE_DATA_SIZE * 2] + .try_into() + .unwrap(), + ], + BASE_VIRT_QUEUE_ADDR, + ); + + // Setup the guest memory with the input bytes + let mem = GuestMemoryMmap::from_ranges(&[ + (GuestAddress(0), MEM_SIZE), + (GuestAddress(BASE_VIRT_QUEUE_ADDR), queue_bytes.len()), + ]) + .unwrap(); + if mem + .write_slice(queue_bytes, GuestAddress(BASE_VIRT_QUEUE_ADDR)) + .is_err() + { + return; + } + if mem.write_slice(mem_bytes, GuestAddress(0 as u64)).is_err() { + return; + } + let guest_memory = GuestMemoryAtomic::new(mem); + + let input_queue = queues.remove(0); + let input_evt = EventFd::new(0).unwrap(); + let input_queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(input_evt.as_raw_fd())) }; + let output_queue = queues.remove(0); + let output_evt = EventFd::new(0).unwrap(); + let output_queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(output_evt.as_raw_fd())) }; + + // Kick the 'queue' events and endpoint event before activate the console device + input_queue_evt.write(1).unwrap(); + output_queue_evt.write(1).unwrap(); + pipe_tx.write_all(console_input_bytes).unwrap(); // To use fuzzed data; + + console + .activate( + guest_memory, + Arc::new(NoopVirtioInterrupt {}), + vec![(0, input_queue, input_evt), (1, output_queue, output_evt)], + ) + .unwrap(); + + // Wait for the events to finish and console device worker thread to return + console.wait_for_epoll_threads(); +}); + +pub struct NoopVirtioInterrupt {} + +impl VirtioInterrupt for NoopVirtioInterrupt { + fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { + Ok(()) + } +} + +fn setup_virt_queues(bytes: &[&[u8; QUEUE_DATA_SIZE]], base_addr: u64) -> Vec { + let mut queues = Vec::new(); + for (i, b) in bytes.iter().enumerate() { + let mut q = Queue::new(QUEUE_SIZE).unwrap(); + + let desc_table_addr = base_addr + (QUEUE_BYTES_SIZE * i) as u64; + let avail_ring_addr = desc_table_addr + DESC_TABLE_SIZE; + let used_ring_addr = avail_ring_addr + PADDING_SIZE + AVAIL_RING_SIZE; + q.try_set_desc_table_address(GuestAddress(desc_table_addr)) + .unwrap(); + q.try_set_avail_ring_address(GuestAddress(avail_ring_addr)) + .unwrap(); + q.try_set_used_ring_address(GuestAddress(used_ring_addr)) + .unwrap(); + + q.set_next_avail(b[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small + q.set_next_used(b[1] as u16); + q.set_event_idx(b[2] % 2 != 0); + q.set_size(b[3] as u16 % QUEUE_SIZE); + + q.set_ready(true); + queues.push(q); + } + + queues +} + +fn memfd_create(name: &std::ffi::CStr) -> Result { + let res = unsafe { libc::syscall(libc::SYS_memfd_create, name.as_ptr(), 0) }; + + if res < 0 { + Err(std::io::Error::last_os_error()) + } else { + Ok(res as RawFd) + } +} + +fn create_pipe() -> Result<(File, File), std::io::Error> { + let mut pipe = [-1; 2]; + if unsafe { libc::pipe2(pipe.as_mut_ptr(), libc::O_CLOEXEC) } == -1 { + return Err(std::io::Error::last_os_error()); + } + let rx = unsafe { File::from_raw_fd(pipe[0]) }; + let tx = unsafe { File::from_raw_fd(pipe[1]) }; + + Ok((rx, tx)) +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/http_api.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/http_api.rs new file mode 100644 index 0000000..caa5ec3 --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/http_api.rs @@ -0,0 +1,194 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] +use libfuzzer_sys::fuzz_target; +use micro_http::Request; +use once_cell::sync::Lazy; +use std::os::unix::io::AsRawFd; +use std::sync::mpsc::{channel, Receiver}; +use std::thread; +use vmm::api::{http::*, ApiRequest, ApiResponsePayload}; +use vmm::{EpollContext, EpollDispatch}; +use vmm_sys_util::eventfd::EventFd; + +// Need to be ordered for test case reproducibility +static ROUTES: Lazy>> = + Lazy::new(|| HTTP_ROUTES.routes.values().collect()); + +fuzz_target!(|bytes| { + if bytes.len() < 2 { + return; + } + + let route = ROUTES[bytes[0] as usize % ROUTES.len()]; + if let Some(request) = generate_request(&bytes[1..]) { + let exit_evt = EventFd::new(libc::EFD_NONBLOCK).unwrap(); + let api_evt = EventFd::new(libc::EFD_NONBLOCK).unwrap(); + let (api_sender, api_receiver) = channel(); + + let http_receiver_thread = { + let exit_evt = exit_evt.try_clone().unwrap(); + let api_evt = api_evt.try_clone().unwrap(); + thread::Builder::new() + .name("http_receiver".to_string()) + .spawn(move || { + http_receiver_stub(exit_evt, api_evt, api_receiver); + }) + .unwrap() + }; + + route.handle_request(&request, api_evt, api_sender); + exit_evt.write(1).ok(); + http_receiver_thread.join().unwrap(); + }; +}); + +fn generate_request(bytes: &[u8]) -> Option { + let req_method = match bytes[0] % 5 { + 0 => "GET", + 1 => "PUT", + 2 => "PATCH", + 3 => "POST", + _ => "INVALID", + }; + let request_line = format!("{} http://localhost/home HTTP/1.1\r\n", req_method); + + let req_body = &bytes[1..]; + let request = if req_body.len() > 0 { + [ + format!("{}Content-Length: {}\r\n", request_line, req_body.len()).as_bytes(), + req_body, + ] + .concat() + } else { + format!("{}\r\n", request_line).as_bytes().to_vec() + }; + + Request::try_from(&request, None).ok() +} + +fn http_receiver_stub(exit_evt: EventFd, api_evt: EventFd, api_receiver: Receiver) { + let mut epoll = EpollContext::new().unwrap(); + epoll.add_event(&exit_evt, EpollDispatch::Exit).unwrap(); + epoll.add_event(&api_evt, EpollDispatch::Api).unwrap(); + + let epoll_fd = epoll.as_raw_fd(); + let mut events = vec![epoll::Event::new(epoll::Events::empty(), 0); 2]; + let num_events; + loop { + num_events = match epoll::wait(epoll_fd, -1, &mut events[..]) { + Ok(num_events) => num_events, + Err(e) => match e.raw_os_error() { + Some(libc::EAGAIN) | Some(libc::EINTR) => continue, + _ => panic!("Unexpected epoll::wait error!"), + }, + }; + + break; + } + + for event in events.iter().take(num_events) { + let dispatch_event: EpollDispatch = event.data.into(); + match dispatch_event { + EpollDispatch::Exit => { + break; + } + EpollDispatch::Api => { + for _ in 0..api_evt.read().unwrap() { + let api_request = api_receiver.recv().unwrap(); + match api_request { + ApiRequest::VmCreate(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmDelete(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmBoot(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmShutdown(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmReboot(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmInfo(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmmPing(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmPause(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmResume(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmSnapshot(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmRestore(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmmShutdown(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmResize(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmResizeZone(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmAddDevice(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmAddUserDevice(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmRemoveDevice(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmAddDisk(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmAddFs(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmAddPmem(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmAddNet(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmAddVdpa(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmAddVsock(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmCounters(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmReceiveMigration(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmSendMigration(_, sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmPowerButton(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + ApiRequest::VmWaitStart(sender) => { + sender.send(Ok(ApiResponsePayload::Empty)).unwrap(); + } + } + } + } + _ => { + panic!("Unexpected Epoll event"); + } + } + } +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/iommu.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/iommu.rs new file mode 100644 index 0000000..d3a054e --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/iommu.rs @@ -0,0 +1,145 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use seccompiler::SeccompAction; +use std::os::unix::io::{AsRawFd, FromRawFd}; +use std::sync::Arc; +use virtio_devices::{VirtioDevice, VirtioInterrupt, VirtioInterruptType}; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic}; +use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +macro_rules! align { + ($n:expr, $align:expr) => {{ + (($n + $align - 1) / $align) * $align + }}; +} + +const QUEUE_DATA_SIZE: usize = 4; +const MEM_SIZE: usize = 32 * 1024 * 1024; +// Reuse what's being done from DeviceManager::get_msi_iova_space() +const IOVA_SPACE_SIZE: usize = (0xfeef_ffff - 0xfee0_0000) + 1; + +// Max entries in the queue. +const QUEUE_SIZE: u16 = 256; +// Descriptor table alignment +const DESC_TABLE_ALIGN_SIZE: u64 = 16; +// Avalable ring alignment +const AVAIL_RING_ALIGN_SIZE: u64 = 2; +// Used ring alignment +const USED_RING_ALIGN_SIZE: u64 = 4; +// Descriptor table size +const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; +// Available ring size +const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; +// Used ring size +const USED_RING_SIZE: u64 = 6_u64 + 8 * QUEUE_SIZE as u64; + +// Guest memory gap +const GUEST_MEM_GAP: u64 = 1 * 1024 * 1024; +// Guest physical address for descriptor table. +const DESC_TABLE_ADDR: u64 = align!(MEM_SIZE as u64 + GUEST_MEM_GAP, DESC_TABLE_ALIGN_SIZE); +// Guest physical address for available ring +const AVAIL_RING_ADDR: u64 = align!(DESC_TABLE_ADDR + DESC_TABLE_SIZE, AVAIL_RING_ALIGN_SIZE); +// Guest physical address for used ring +const USED_RING_ADDR: u64 = align!(AVAIL_RING_ADDR + AVAIL_RING_SIZE, USED_RING_ALIGN_SIZE); +// Virtio-queue size in bytes +const QUEUE_BYTES_SIZE: usize = (USED_RING_ADDR + USED_RING_SIZE - DESC_TABLE_ADDR) as usize; + +fuzz_target!(|bytes| { + if bytes.len() < (QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE) + || bytes.len() > (QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE + MEM_SIZE) + { + return; + } + + let (mut iommu, _) = virtio_devices::Iommu::new( + "fuzzer_iommu".to_owned(), + SeccompAction::Allow, + EventFd::new(EFD_NONBLOCK).unwrap(), + ((MEM_SIZE - IOVA_SPACE_SIZE) as u64, (MEM_SIZE - 1) as u64), + None, + ) + .unwrap(); + + let queue_data = &bytes[..QUEUE_DATA_SIZE]; + let queue_bytes = &bytes[QUEUE_DATA_SIZE..QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE]; + let mem_bytes = &bytes[QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE..]; + + // Setup the request queue with the input bytes + let request_queue = setup_virt_queue(queue_data.try_into().unwrap()); + // Given the "event queue" events are not handled from the current + // implementation of virtio-iommu, we simply setup the 'event_queue' + // with exactly the same content as the 'request_queue'. + let _event_queue = setup_virt_queue(queue_data.try_into().unwrap()); + + // Setup the guest memory with the input bytes + let mem = GuestMemoryMmap::from_ranges(&[ + (GuestAddress(0), MEM_SIZE), + (GuestAddress(DESC_TABLE_ADDR), QUEUE_BYTES_SIZE), + ]) + .unwrap(); + if mem + .write_slice(queue_bytes, GuestAddress(DESC_TABLE_ADDR)) + .is_err() + { + return; + } + if mem.write_slice(mem_bytes, GuestAddress(0 as u64)).is_err() { + return; + } + let guest_memory = GuestMemoryAtomic::new(mem); + + let request_evt = EventFd::new(0).unwrap(); + let request_queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(request_evt.as_raw_fd())) }; + let _event_evt = EventFd::new(0).unwrap(); + + // Kick the 'queue' event before activate the vIOMMU device + request_queue_evt.write(1).unwrap(); + + iommu + .activate( + guest_memory, + Arc::new(NoopVirtioInterrupt {}), + vec![ + (0, request_queue, request_evt), + (0, _event_queue, _event_evt), + ], + ) + .ok(); + + // Wait for the events to finish and vIOMMU device worker thread to return + iommu.wait_for_epoll_threads(); +}); + +pub struct NoopVirtioInterrupt {} + +impl VirtioInterrupt for NoopVirtioInterrupt { + fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { + Ok(()) + } +} + +fn setup_virt_queue(bytes: &[u8; QUEUE_DATA_SIZE]) -> Queue { + let mut q = Queue::new(QUEUE_SIZE).unwrap(); + q.set_next_avail(bytes[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small + q.set_next_used(bytes[1] as u16); + q.set_event_idx(bytes[2] % 2 != 0); + q.set_size(bytes[3] as u16 % QUEUE_SIZE); + + q.try_set_desc_table_address(GuestAddress(DESC_TABLE_ADDR)) + .unwrap(); + q.try_set_avail_ring_address(GuestAddress(AVAIL_RING_ADDR)) + .unwrap(); + q.try_set_used_ring_address(GuestAddress(USED_RING_ADDR)) + .unwrap(); + q.set_ready(true); + + q +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/mem.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/mem.rs new file mode 100644 index 0000000..ad91e1e --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/mem.rs @@ -0,0 +1,179 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use seccompiler::SeccompAction; +use std::os::unix::io::{AsRawFd, FromRawFd}; +use std::sync::{Arc, Mutex}; +use virtio_devices::{BlocksState, Mem, VirtioDevice, VirtioInterrupt, VirtioInterruptType}; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic}; +use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; +type GuestRegionMmap = vm_memory::GuestRegionMmap; + +macro_rules! align { + ($n:expr, $align:expr) => {{ + (($n + $align - 1) / $align) * $align + }}; +} + +const VIRTIO_MEM_DATA_SIZE: usize = 1; +const QUEUE_DATA_SIZE: usize = 4; +// The size of the guest memory for the virtio-mem region +const MEM_SIZE: usize = 128 * 1024 * 1024; +// The start address of the virtio-mem region in the guest memory +const VIRTIO_MEM_REGION_ADDRESS: u64 = 0; + +// Max entries in the queue. +const QUEUE_SIZE: u16 = 64; +// Descriptor table alignment +const DESC_TABLE_ALIGN_SIZE: u64 = 16; +// Avalable ring alignment +const AVAIL_RING_ALIGN_SIZE: u64 = 2; +// Used ring alignment +const USED_RING_ALIGN_SIZE: u64 = 4; +// Descriptor table size +const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; +// Available ring size +const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; +// Used ring size +const USED_RING_SIZE: u64 = 6_u64 + 8 * QUEUE_SIZE as u64; + +// Guest memory gap +const GUEST_MEM_GAP: u64 = 1 * 1024 * 1024; +// Guest physical address for descriptor table. +const DESC_TABLE_ADDR: u64 = align!(MEM_SIZE as u64 + GUEST_MEM_GAP, DESC_TABLE_ALIGN_SIZE); +// Guest physical address for available ring +const AVAIL_RING_ADDR: u64 = align!(DESC_TABLE_ADDR + DESC_TABLE_SIZE, AVAIL_RING_ALIGN_SIZE); +// Guest physical address for used ring +const USED_RING_ADDR: u64 = align!(AVAIL_RING_ADDR + AVAIL_RING_SIZE, USED_RING_ALIGN_SIZE); +// Virtio-queue size in bytes +const QUEUE_BYTES_SIZE: usize = (USED_RING_ADDR + USED_RING_SIZE - DESC_TABLE_ADDR) as usize; + +fuzz_target!(|bytes| { + if bytes.len() < VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE + || bytes.len() > (VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE + MEM_SIZE) + { + return; + } + + let virtio_mem_data = &bytes[..VIRTIO_MEM_DATA_SIZE]; + let queue_data = &bytes[VIRTIO_MEM_DATA_SIZE..VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE]; + let queue_bytes = &bytes[VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE + ..VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE]; + let mem_bytes = &bytes[VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE..]; + + // Create a virtio-mem device based on the input bytes; + let (mut virtio_mem, virtio_mem_region) = + create_dummy_virtio_mem(virtio_mem_data.try_into().unwrap()); + + // Setup the virt queue with the input bytes + let q = setup_virt_queue(queue_data.try_into().unwrap()); + + // Setup the guest memory with the input bytes + let mem = GuestMemoryMmap::from_ranges(&[ + (GuestAddress(DESC_TABLE_ADDR), QUEUE_BYTES_SIZE), // guest region for the virt queue + ]) + .unwrap(); + if mem + .write_slice(queue_bytes, GuestAddress(DESC_TABLE_ADDR)) + .is_err() + { + return; + } + // Add the memory region for the virtio-mem device + let mem = mem.insert_region(virtio_mem_region).unwrap(); + if mem + .write_slice(mem_bytes, GuestAddress(VIRTIO_MEM_REGION_ADDRESS)) + .is_err() + { + return; + } + let guest_memory = GuestMemoryAtomic::new(mem); + + let evt = EventFd::new(0).unwrap(); + let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(evt.as_raw_fd())) }; + + // Kick the 'queue' event before activate the virtio-mem device + queue_evt.write(1).unwrap(); + + virtio_mem + .activate( + guest_memory, + Arc::new(NoopVirtioInterrupt {}), + vec![(0, q, evt)], + ) + .ok(); + + // Wait for the events to finish and virtio-mem device worker thread to return + virtio_mem.wait_for_epoll_threads(); +}); + +pub struct NoopVirtioInterrupt {} + +impl VirtioInterrupt for NoopVirtioInterrupt { + fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { + Ok(()) + } +} + +// Create a dummy virtio-mem device for fuzzing purpose only +fn create_dummy_virtio_mem(bytes: &[u8; VIRTIO_MEM_DATA_SIZE]) -> (Mem, Arc) { + let numa_id = if bytes[0] % 2 != 0 { Some(0) } else { None }; + + let region = vmm::memory_manager::MemoryManager::create_ram_region( + &None, + 0, + GuestAddress(VIRTIO_MEM_REGION_ADDRESS), + MEM_SIZE, + false, + false, + false, + None, + numa_id, + None, + false, + ) + .unwrap(); + + let blocks_state = Arc::new(Mutex::new(BlocksState::new(region.size() as u64))); + + ( + Mem::new( + "fuzzer_mem".to_owned(), + ®ion, + SeccompAction::Allow, + numa_id.map(|i| i as u16), + 0, + false, + EventFd::new(EFD_NONBLOCK).unwrap(), + blocks_state.clone(), + None, + ) + .unwrap(), + region, + ) +} + +fn setup_virt_queue(bytes: &[u8; QUEUE_DATA_SIZE]) -> Queue { + let mut q = Queue::new(QUEUE_SIZE).unwrap(); + q.set_next_avail(bytes[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small + q.set_next_used(bytes[1] as u16); + q.set_event_idx(bytes[2] % 2 != 0); + q.set_size(bytes[3] as u16 % QUEUE_SIZE); + + q.try_set_desc_table_address(GuestAddress(DESC_TABLE_ADDR)) + .unwrap(); + q.try_set_avail_ring_address(GuestAddress(AVAIL_RING_ADDR)) + .unwrap(); + q.try_set_used_ring_address(GuestAddress(USED_RING_ADDR)) + .unwrap(); + q.set_ready(true); + + q +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/pmem.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/pmem.rs new file mode 100644 index 0000000..00762fe --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/pmem.rs @@ -0,0 +1,151 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] + +use libc::{MAP_NORESERVE, MAP_PRIVATE, PROT_READ, PROT_WRITE}; +use libfuzzer_sys::fuzz_target; +use seccompiler::SeccompAction; +use std::ffi; +use std::fs::File; +use std::io; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::sync::Arc; +use virtio_devices::{Pmem, UserspaceMapping, VirtioDevice, VirtioInterrupt, VirtioInterruptType}; +use virtio_queue::{Queue, QueueT}; +use vm_memory::guest_memory::FileOffset; +use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic, MmapRegion}; +use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +const QUEUE_DATA_SIZE: usize = 4; +const MEM_SIZE: usize = 256 * 1024 * 1024; +const PMEM_FILE_SIZE: usize = 128 * 1024 * 1024; +// Max entries in the queue. +const QUEUE_SIZE: u16 = 256; +// Guest physical address for descriptor table. +const DESC_TABLE_ADDR: u64 = 0; +const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; +// Guest physical address for available ring +const AVAIL_RING_ADDR: u64 = DESC_TABLE_ADDR + DESC_TABLE_SIZE; +const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; +// Guest physical address for used ring (requires to 4-bytes aligned) +const USED_RING_ADDR: u64 = (AVAIL_RING_ADDR + AVAIL_RING_SIZE + 3) & !3_u64; + +fuzz_target!(|bytes| { + if bytes.len() < QUEUE_DATA_SIZE || bytes.len() > (QUEUE_DATA_SIZE + MEM_SIZE) { + return; + } + + let mut pmem = create_dummy_pmem(); + let queue_data = &bytes[..QUEUE_DATA_SIZE]; + let mem_bytes = &bytes[QUEUE_DATA_SIZE..]; + + // Setup the virt queue with the input bytes + let q = setup_virt_queue(queue_data.try_into().unwrap()); + + // Setup the guest memory with the input bytes + let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE)]).unwrap(); + if mem.write_slice(mem_bytes, GuestAddress(0 as u64)).is_err() { + return; + } + let guest_memory = GuestMemoryAtomic::new(mem); + + let evt = EventFd::new(0).unwrap(); + let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(evt.as_raw_fd())) }; + + // Kick the 'queue' event before activate the pmem device + queue_evt.write(1).unwrap(); + + pmem.activate( + guest_memory, + Arc::new(NoopVirtioInterrupt {}), + vec![(0, q, evt)], + ) + .ok(); + + // Wait for the events to finish and pmem device worker thread to return + pmem.wait_for_epoll_threads(); +}); + +fn memfd_create_with_size(name: &ffi::CStr, flags: u32, size: usize) -> Result { + let fd = unsafe { libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags) }; + if fd < 0 { + return Err(io::Error::last_os_error()); + } + + let res = unsafe { libc::syscall(libc::SYS_ftruncate, fd, size) }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + + Ok(fd as RawFd) +} + +pub struct NoopVirtioInterrupt {} + +impl VirtioInterrupt for NoopVirtioInterrupt { + fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { + Ok(()) + } +} + +// Create a dummy virtio-pmem device for fuzzing purpose only +fn create_dummy_pmem() -> Pmem { + let shm = + memfd_create_with_size(&ffi::CString::new("fuzz").unwrap(), 0, PMEM_FILE_SIZE).unwrap(); + let file: File = unsafe { File::from_raw_fd(shm) }; + + // The fuzzer is focusing on the virtio-pmem code that processes guest inputs (e.g. virt queues), + // so dummy mappings (both mmap and user mapping) does the job and is faster (using smaller amount of memory). + let dummy_mapping_size = 1; + let cloned_file = file.try_clone().unwrap(); + let dummy_mmap_region = MmapRegion::build( + Some(FileOffset::new(cloned_file, 0)), + dummy_mapping_size, + PROT_READ | PROT_WRITE, + MAP_NORESERVE | MAP_PRIVATE, + ) + .unwrap(); + let guest_addr = GuestAddress(0); + let dummy_user_mapping = UserspaceMapping { + host_addr: dummy_mmap_region.as_ptr() as u64, + mem_slot: 0, + addr: guest_addr, + len: dummy_mapping_size as u64, + mergeable: false, + }; + + Pmem::new( + "tmp".to_owned(), + file, + guest_addr, + dummy_user_mapping, + dummy_mmap_region, + false, + SeccompAction::Allow, + EventFd::new(EFD_NONBLOCK).unwrap(), + None, + ) + .unwrap() +} + +fn setup_virt_queue(bytes: &[u8; QUEUE_DATA_SIZE]) -> Queue { + let mut q = Queue::new(QUEUE_SIZE).unwrap(); + q.set_next_avail(bytes[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small + q.set_next_used(bytes[1] as u16); + q.set_event_idx(bytes[2] % 2 != 0); + q.set_size(bytes[3] as u16 % QUEUE_SIZE); + + q.try_set_desc_table_address(GuestAddress(DESC_TABLE_ADDR)) + .unwrap(); + q.try_set_avail_ring_address(GuestAddress(AVAIL_RING_ADDR)) + .unwrap(); + q.try_set_used_ring_address(GuestAddress(USED_RING_ADDR)) + .unwrap(); + q.set_ready(true); + + q +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/qcow.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/qcow.rs new file mode 100644 index 0000000..5768a59 --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/qcow.rs @@ -0,0 +1,49 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +#![no_main] +use libfuzzer_sys::fuzz_target; +use qcow::{QcowFile, RawFile}; +use std::ffi; +use std::fs::File; +use std::io::{self, Cursor, Read, Seek, SeekFrom, Write}; +use std::mem::size_of; +use std::os::unix::io::{FromRawFd, RawFd}; + +// Take the first 64 bits of data as an address and the next 64 bits as data to +// store there. The rest of the data is used as a qcow image. +fuzz_target!(|bytes| { + if bytes.len() < 16 { + // Need an address and data, each are 8 bytes. + return; + } + let mut disk_image = Cursor::new(bytes); + let addr = read_u64(&mut disk_image); + let value = read_u64(&mut disk_image); + let shm = memfd_create(&ffi::CString::new("fuzz").unwrap(), 0).unwrap(); + let mut disk_file: File = unsafe { File::from_raw_fd(shm) }; + disk_file.write_all(&bytes[16..]).unwrap(); + disk_file.seek(SeekFrom::Start(0)).unwrap(); + if let Ok(mut qcow) = QcowFile::from(RawFile::new(disk_file, false)) { + if qcow.seek(SeekFrom::Start(addr)).is_ok() { + let _ = qcow.write_all(&value.to_le_bytes()); + } + } +}); + +fn read_u64(readable: &mut T) -> u64 { + let mut buf = [0u8; size_of::()]; + readable.read_exact(&mut buf[..]).unwrap(); + u64::from_le_bytes(buf) +} + +fn memfd_create(name: &ffi::CStr, flags: u32) -> Result { + let res = unsafe { libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags) }; + + if res < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(res as RawFd) + } +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/rng.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/rng.rs new file mode 100644 index 0000000..f237937 --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/rng.rs @@ -0,0 +1,135 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use seccompiler::SeccompAction; +use std::os::unix::io::{AsRawFd, FromRawFd}; +use std::sync::Arc; +use virtio_devices::{VirtioDevice, VirtioInterrupt, VirtioInterruptType}; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic}; +use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +macro_rules! align { + ($n:expr, $align:expr) => {{ + (($n + $align - 1) / $align) * $align + }}; +} + +const QUEUE_DATA_SIZE: usize = 4; +const MEM_SIZE: usize = 1 * 1024 * 1024; + +// Max entries in the queue. +const QUEUE_SIZE: u16 = 256; +// Descriptor table alignment +const DESC_TABLE_ALIGN_SIZE: u64 = 16; +// Avalable ring alignment +const AVAIL_RING_ALIGN_SIZE: u64 = 2; +// Used ring alignment +const USED_RING_ALIGN_SIZE: u64 = 4; +// Descriptor table size +const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; +// Available ring size +const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; +// Used ring size +const USED_RING_SIZE: u64 = 6_u64 + 8 * QUEUE_SIZE as u64; + +// Guest memory gap +const GUEST_MEM_GAP: u64 = 1 * 1024 * 1024; +// Guest physical address for descriptor table. +const DESC_TABLE_ADDR: u64 = align!(MEM_SIZE as u64 + GUEST_MEM_GAP, DESC_TABLE_ALIGN_SIZE); +// Guest physical address for available ring +const AVAIL_RING_ADDR: u64 = align!(DESC_TABLE_ADDR + DESC_TABLE_SIZE, AVAIL_RING_ALIGN_SIZE); +// Guest physical address for used ring +const USED_RING_ADDR: u64 = align!(AVAIL_RING_ADDR + AVAIL_RING_SIZE, USED_RING_ALIGN_SIZE); +// Virtio-queue size in bytes +const QUEUE_BYTES_SIZE: usize = (USED_RING_ADDR + USED_RING_SIZE - DESC_TABLE_ADDR) as usize; + +fuzz_target!(|bytes| { + if bytes.len() < (QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE) + || bytes.len() > (QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE + MEM_SIZE) + { + return; + } + + let mut rng = virtio_devices::Rng::new( + "fuzzer_rng".to_owned(), + "/dev/urandom", + false, + SeccompAction::Allow, + EventFd::new(EFD_NONBLOCK).unwrap(), + None, + ) + .unwrap(); + + let queue_data = &bytes[..QUEUE_DATA_SIZE]; + let queue_bytes = &bytes[QUEUE_DATA_SIZE..QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE]; + let mem_bytes = &bytes[QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE..]; + + // Setup the virt queue with the input bytes + let q = setup_virt_queue(queue_data.try_into().unwrap()); + + // Setup the guest memory with the input bytes + let mem = GuestMemoryMmap::from_ranges(&[ + (GuestAddress(0), MEM_SIZE), + (GuestAddress(DESC_TABLE_ADDR), QUEUE_BYTES_SIZE), + ]) + .unwrap(); + if mem + .write_slice(queue_bytes, GuestAddress(DESC_TABLE_ADDR)) + .is_err() + { + return; + } + if mem.write_slice(mem_bytes, GuestAddress(0 as u64)).is_err() { + return; + } + let guest_memory = GuestMemoryAtomic::new(mem); + + let evt = EventFd::new(0).unwrap(); + let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(evt.as_raw_fd())) }; + + // Kick the 'queue' event before activate the rng device + queue_evt.write(1).unwrap(); + + rng.activate( + guest_memory, + Arc::new(NoopVirtioInterrupt {}), + vec![(0, q, evt)], + ) + .ok(); + + // Wait for the events to finish and rng device worker thread to return + rng.wait_for_epoll_threads(); +}); + +pub struct NoopVirtioInterrupt {} + +impl VirtioInterrupt for NoopVirtioInterrupt { + fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { + Ok(()) + } +} + +fn setup_virt_queue(bytes: &[u8; QUEUE_DATA_SIZE]) -> Queue { + let mut q = Queue::new(QUEUE_SIZE).unwrap(); + q.set_next_avail(bytes[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small + q.set_next_used(bytes[1] as u16); + q.set_event_idx(bytes[2] % 2 != 0); + q.set_size(bytes[3] as u16 % QUEUE_SIZE); + + q.try_set_desc_table_address(GuestAddress(DESC_TABLE_ADDR)) + .unwrap(); + q.try_set_avail_ring_address(GuestAddress(AVAIL_RING_ADDR)) + .unwrap(); + q.try_set_used_ring_address(GuestAddress(USED_RING_ADDR)) + .unwrap(); + q.set_ready(true); + + q +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/serial.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/serial.rs new file mode 100644 index 0000000..1648c26 --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/serial.rs @@ -0,0 +1,73 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] +use devices::legacy::Serial; +use libc::EFD_NONBLOCK; +use libfuzzer_sys::fuzz_target; +use std::sync::Arc; +use vm_device::interrupt::{InterruptIndex, InterruptSourceConfig, InterruptSourceGroup}; +use vm_device::BusDevice; +use vmm_sys_util::eventfd::EventFd; + +fuzz_target!(|bytes| { + let mut serial = Serial::new_sink( + "serial".into(), + Arc::new(TestInterrupt::new(EventFd::new(EFD_NONBLOCK).unwrap())), + ); + + let mut i = 0; + while i < bytes.len() { + let choice = bytes.get(i).unwrap_or(&0) % 3; + i += 1; + + match choice { + 0 => { + let offset = (bytes.get(i).unwrap_or(&0) % 8) as u64; + i += 1; + let mut out_bytes = vec![0]; + serial.read(0, offset, &mut out_bytes); + } + 1 => { + let offset = (bytes.get(i).unwrap_or(&0) % 8) as u64; + i += 1; + let data = vec![*bytes.get(i).unwrap_or(&0)]; + i += 1; + serial.write(0, offset, &data); + } + _ => { + let data = vec![*bytes.get(i).unwrap_or(&0)]; + i += 1; + serial.queue_input_bytes(&data).ok(); + } + } + } +}); + +struct TestInterrupt { + event_fd: EventFd, +} + +impl InterruptSourceGroup for TestInterrupt { + fn trigger(&self, _index: InterruptIndex) -> Result<(), std::io::Error> { + self.event_fd.write(1) + } + fn update( + &self, + _index: InterruptIndex, + _config: InterruptSourceConfig, + _masked: bool, + ) -> Result<(), std::io::Error> { + Ok(()) + } + fn notifier(&self, _index: InterruptIndex) -> Option { + Some(self.event_fd.try_clone().unwrap()) + } +} + +impl TestInterrupt { + fn new(event_fd: EventFd) -> Self { + TestInterrupt { event_fd } + } +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/vhdx.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/vhdx.rs new file mode 100644 index 0000000..c3d372e --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/vhdx.rs @@ -0,0 +1,53 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] +use libfuzzer_sys::fuzz_target; +use std::ffi; +use std::fs::File; +use std::io::{self, Read, Seek, SeekFrom, Write}; +use std::os::unix::io::{FromRawFd, RawFd}; +use vhdx::vhdx::Vhdx; + +// Populate the corpus directory with a test file: +// truncate -s 16M /tmp/source +// qemu-img convert -O vhdx /tmp/source fuzz/corpus/vhdx/test.vhdx +// Run with: +// cargo fuzz run vhdx -j 32 -- -max_len=16777216 +fuzz_target!(|bytes| { + let shm = memfd_create(&ffi::CString::new("fuzz").unwrap(), 0).unwrap(); + let mut disk_file: File = unsafe { File::from_raw_fd(shm) }; + disk_file.write_all(&bytes[..]).unwrap(); + disk_file.seek(SeekFrom::Start(0)).unwrap(); + + if let Ok(mut vhdx) = Vhdx::new(disk_file) { + if vhdx.seek(SeekFrom::Start(0)).is_ok() { + let mut offset = 0; + while offset < bytes.len() { + let mut data = vec![0; 8192]; + vhdx.read_exact(&mut data).ok(); + offset += data.len(); + } + } + + if vhdx.seek(SeekFrom::Start(0)).is_ok() { + let mut offset = 0; + while offset < bytes.len() { + let data = vec![0; 8192]; + vhdx.write_all(&data).ok(); + offset += data.len(); + } + } + } +}); + +fn memfd_create(name: &ffi::CStr, flags: u32) -> Result { + let res = unsafe { libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags) }; + + if res < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(res as RawFd) + } +} diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/watchdog.rs b/vendor/cube-hypervisor/fuzz/fuzz_targets/watchdog.rs new file mode 100644 index 0000000..1ce70b8 --- /dev/null +++ b/vendor/cube-hypervisor/fuzz/fuzz_targets/watchdog.rs @@ -0,0 +1,100 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use seccompiler::SeccompAction; +use std::os::unix::io::{AsRawFd, FromRawFd}; +use std::sync::Arc; +use virtio_devices::{VirtioDevice, VirtioInterrupt, VirtioInterruptType}; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic}; +use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +const QUEUE_DATA_SIZE: usize = 4; +const MEM_SIZE: usize = 256 * 1024 * 1024; +// Max entries in the queue. +const QUEUE_SIZE: u16 = 256; +// Guest physical address for descriptor table. +const DESC_TABLE_ADDR: u64 = 0; +const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; +// Guest physical address for available ring +const AVAIL_RING_ADDR: u64 = DESC_TABLE_ADDR + DESC_TABLE_SIZE; +const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; +// Guest physical address for used ring (requires to 4-bytes aligned) +const USED_RING_ADDR: u64 = (AVAIL_RING_ADDR + AVAIL_RING_SIZE + 3) & !3_u64; + +fuzz_target!(|bytes| { + if bytes.len() < QUEUE_DATA_SIZE || bytes.len() > (QUEUE_DATA_SIZE + MEM_SIZE) { + return; + } + + let mut watchdog = virtio_devices::Watchdog::new( + "fuzzer_watchdog".to_owned(), + EventFd::new(EFD_NONBLOCK).unwrap(), + SeccompAction::Allow, + EventFd::new(EFD_NONBLOCK).unwrap(), + None, + ) + .unwrap(); + + let queue_data = &bytes[..QUEUE_DATA_SIZE]; + let mem_bytes = &bytes[QUEUE_DATA_SIZE..]; + + // Setup the virt queue with the input bytes + let q = setup_virt_queue(queue_data.try_into().unwrap()); + + // Setup the guest memory with the input bytes + let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE)]).unwrap(); + if mem.write_slice(mem_bytes, GuestAddress(0 as u64)).is_err() { + return; + } + let guest_memory = GuestMemoryAtomic::new(mem); + + let evt = EventFd::new(0).unwrap(); + let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(evt.as_raw_fd())) }; + + // Kick the 'queue' event before activate the watchdog device + queue_evt.write(1).unwrap(); + + watchdog + .activate( + guest_memory, + Arc::new(NoopVirtioInterrupt {}), + vec![(0, q, evt)], + ) + .ok(); + + // Wait for the events to finish and watchdog device worker thread to return + watchdog.wait_for_epoll_threads(); +}); + +pub struct NoopVirtioInterrupt {} + +impl VirtioInterrupt for NoopVirtioInterrupt { + fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { + Ok(()) + } +} + +fn setup_virt_queue(bytes: &[u8; QUEUE_DATA_SIZE]) -> Queue { + let mut q = Queue::new(QUEUE_SIZE).unwrap(); + q.set_next_avail(bytes[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small + q.set_next_used(bytes[1] as u16); + q.set_event_idx(bytes[2] % 2 != 0); + q.set_size(bytes[3] as u16 % QUEUE_SIZE); + + q.try_set_desc_table_address(GuestAddress(DESC_TABLE_ADDR)) + .unwrap(); + q.try_set_avail_ring_address(GuestAddress(AVAIL_RING_ADDR)) + .unwrap(); + q.try_set_used_ring_address(GuestAddress(USED_RING_ADDR)) + .unwrap(); + q.set_ready(true); + + q +} diff --git a/vendor/cube-hypervisor/hypervisor/Cargo.toml b/vendor/cube-hypervisor/hypervisor/Cargo.toml new file mode 100644 index 0000000..e995f0c --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "hypervisor" +version = "0.1.0" +authors = ["Microsoft Authors"] +edition = "2021" +license = "Apache-2.0 OR BSD-3-Clause" + +[features] +kvm = ["kvm-ioctls", "kvm-bindings"] +mshv = ["mshv-ioctls", "mshv-bindings"] +tdx = [] + +[dependencies] +anyhow = "1.0.86" +byteorder = "1.4.3" +thiserror = "1.0.37" +libc = "0.2.137" +log = "0.4.17" +kvm-bindings = { version = "0.8.1", features = ["serde"], optional = true } +kvm-ioctls = { version = "0.17.0", optional = true } +mshv-bindings = { git = "https://github.com/rust-vmm/mshv", tag = "v0.2.0", features = ["with-serde", "fam-wrappers"], optional = true } +mshv-ioctls = { git = "https://github.com/rust-vmm/mshv", tag = "v0.2.0", optional = true } +serde = { version = "1.0.147", features = ["rc", "derive"] } +serde_with = { version = "2.0.1", default-features = false, features = ["macros"] } +vfio-ioctls = { git = "https://github.com/rust-vmm/vfio", rev = "64171f3", default-features = false } +vm-memory = { version = "0.14.1", features = ["backend-mmap", "backend-atomic"] } +vmm-sys-util = { version = "0.12.1", features = ["with-serde"] } + +[target.'cfg(target_arch = "x86_64")'.dependencies.iced-x86] +version = "1.17.0" +default-features = false +features = ["std", "decoder", "op_code_info", "instr_info", "fast_fmt"] + +[dev-dependencies] +env_logger = "0.10.2" diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/aarch64/gic.rs b/vendor/cube-hypervisor/hypervisor/src/arch/aarch64/gic.rs new file mode 100644 index 0000000..9dc8e69 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/aarch64/gic.rs @@ -0,0 +1,68 @@ +// Copyright 2022 Arm Limited (or its affiliates). All rights reserved. + +use crate::{CpuState, GicState, HypervisorDeviceError, HypervisorVmError}; +use std::any::Any; +use std::result; + +/// Errors thrown while setting up the VGIC. +#[derive(Debug)] +pub enum Error { + /// Error while calling KVM ioctl for setting up the global interrupt controller. + CreateGic(HypervisorVmError), + /// Error while setting device attributes for the GIC. + SetDeviceAttribute(HypervisorDeviceError), + /// Error while getting device attributes for the GIC. + GetDeviceAttribute(HypervisorDeviceError), +} +pub type Result = result::Result; + +#[derive(Debug)] +pub struct VgicConfig { + pub vcpu_count: u64, + pub dist_addr: u64, + pub dist_size: u64, + pub redists_addr: u64, + pub redists_size: u64, + pub msi_addr: u64, + pub msi_size: u64, + pub nr_irqs: u32, +} + +/// Hypervisor agnostic interface for a virtualized GIC +pub trait Vgic: Send + Sync { + /// Returns the fdt compatibility property of the device + fn fdt_compatibility(&self) -> &str; + + /// Returns the maint_irq fdt property of the device + fn fdt_maint_irq(&self) -> u32; + + /// Returns an array with GIC device properties + fn device_properties(&self) -> [u64; 4]; + + /// Returns the number of vCPUs this GIC handles + fn vcpu_count(&self) -> u64; + + /// Returns whether the GIC device is MSI compatible or not + fn msi_compatible(&self) -> bool; + + /// Returns the MSI compatibility property of the device + fn msi_compatibility(&self) -> &str; + + /// Returns the MSI reg property of the device + fn msi_properties(&self) -> [u64; 2]; + + /// Get the values of GICR_TYPER for each vCPU. + fn set_gicr_typers(&mut self, vcpu_states: &[CpuState]); + + /// Downcast the trait object to its concrete type. + fn as_any_concrete_mut(&mut self) -> &mut dyn Any; + + /// Save the state of GICv3ITS. + fn state(&self) -> Result; + + /// Restore the state of GICv3ITS. + fn set_state(&mut self, state: &GicState) -> Result<()>; + + /// Saves GIC internal data tables into RAM. + fn save_data_tables(&self) -> Result<()>; +} diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/aarch64/mod.rs b/vendor/cube-hypervisor/hypervisor/src/arch/aarch64/mod.rs new file mode 100644 index 0000000..68bf881 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/aarch64/mod.rs @@ -0,0 +1,3 @@ +// Copyright 2022 Arm Limited (or its affiliates). All rights reserved. + +pub mod gic; diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/emulator/mod.rs b/vendor/cube-hypervisor/hypervisor/src/arch/emulator/mod.rs new file mode 100644 index 0000000..507918c --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/emulator/mod.rs @@ -0,0 +1,157 @@ +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use core::fmt::Debug; +use std::fmt::{self, Display}; +use thiserror::Error; + +#[derive(Clone, Copy, Error, Debug)] +pub struct Exception { + vector: T, + ip: u64, + error: Option, + payload: Option, +} + +impl Display for Exception { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "Exception {:?} at IP {:#x}{}{}", + self.vector, + self.ip, + self.error + .map(|e| format!(": error {:x}", e)) + .unwrap_or_else(|| "".to_owned()), + self.payload + .map(|payload| format!(": payload {:x}", payload)) + .unwrap_or_else(|| "".to_owned()) + ) + } +} + +#[derive(Error, Debug)] +pub enum PlatformError { + #[error("Invalid address: {0}")] + InvalidAddress(#[source] anyhow::Error), + + #[error("Invalid register: {0}")] + InvalidRegister(#[source] anyhow::Error), + + #[error("Invalid state: {0}")] + InvalidState(#[source] anyhow::Error), + + #[error("Memory read failure: {0}")] + MemoryReadFailure(#[source] anyhow::Error), + + #[error("Memory write failure: {0}")] + MemoryWriteFailure(#[source] anyhow::Error), + + #[error("Get CPU state failure: {0}")] + GetCpuStateFailure(#[source] anyhow::Error), + + #[error("Set CPU state failure: {0}")] + SetCpuStateFailure(#[source] anyhow::Error), + + #[error("Translate virtual address: {0}")] + TranslateVirtualAddress(#[source] anyhow::Error), + + #[error("Unsupported CPU Mode: {0}")] + UnsupportedCpuMode(#[source] anyhow::Error), + + #[error("Invalid instruction operand: {0}")] + InvalidOperand(#[source] anyhow::Error), +} + +#[derive(Error, Debug)] +pub enum EmulationError { + #[error("Unsupported instruction: {0}")] + UnsupportedInstruction(#[source] anyhow::Error), + + #[error("Unsupported memory size: {0}")] + UnsupportedMemorySize(#[source] anyhow::Error), + + #[error("Invalid operand: {0}")] + InvalidOperand(#[source] anyhow::Error), + + #[error("Wrong number of operands: {0}")] + WrongNumberOperands(#[source] anyhow::Error), + + #[error("Instruction Exception: {0}")] + InstructionException(Exception), + + #[error("Instruction fetching error: {0}")] + InstructionFetchingError(#[source] anyhow::Error), + + #[error("Platform emulation error: {0}")] + PlatformEmulationError(PlatformError), + + #[error(transparent)] + EmulationError(#[from] anyhow::Error), +} + +/// The PlatformEmulator trait emulates a guest platform. +/// It's mostly a guest resources (memory and CPU state) getter and setter. +/// +/// A CpuState is an architecture specific type, representing a CPU state. +/// The emulator and its instruction handlers modify a given CPU state and +/// eventually ask the platform to commit it back through `set_cpu_state`. +pub trait PlatformEmulator { + type CpuState: Clone; + + /// Read guest memory into a u8 slice. + /// + /// # Arguments + /// + /// * `gva` - Guest virtual address to read from. + /// * `data` - Data slice to read into. + /// + fn read_memory(&self, gva: u64, data: &mut [u8]) -> Result<(), PlatformError>; + + /// Write a u8 slice into guest memory. + /// + /// # Arguments + /// + /// * `gva` - Guest virtual address to write into. + /// * `data` - Data slice to be written. + /// + fn write_memory(&mut self, gva: u64, data: &[u8]) -> Result<(), PlatformError>; + + /// Get a CPU state from the guest. + /// + /// # Arguments + /// + /// * `cpu_id` - Logical CPU ID. + /// + fn cpu_state(&self, cpu_id: usize) -> Result; + + /// Set a guest CPU state. + /// + /// # Arguments + /// + /// * `cpu_id` - Logical CPU ID. + /// * `state` - State to set the CPU into. + /// + fn set_cpu_state(&self, cpu_id: usize, state: Self::CpuState) -> Result<(), PlatformError>; + + /// Translate a guest virtual address into a physical one + /// + /// # Arguments + /// + /// * `gva` - Guest virtual address to translate. + /// + fn gva_to_gpa(&self, gva: u64) -> Result; + + /// Fetch instruction bytes from memory. + /// + /// # Arguments + /// + /// * `ip` - Instruction pointer virtual address to start fetching instructions from. + /// + fn fetch(&self, ip: u64, instruction_bytes: &mut [u8]) -> Result<(), PlatformError>; +} + +pub type EmulationResult = std::result::Result>; diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/mod.rs b/vendor/cube-hypervisor/hypervisor/src/arch/mod.rs new file mode 100644 index 0000000..e68225d --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/mod.rs @@ -0,0 +1,23 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2022 Arm Limited (or its affiliates). All rights reserved. +// + +pub mod emulator; + +#[cfg(target_arch = "x86_64")] +#[macro_use] +pub mod x86; + +#[cfg(target_arch = "aarch64")] +pub mod aarch64; diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/cmp.rs b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/cmp.rs new file mode 100644 index 0000000..0cfd143 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/cmp.rs @@ -0,0 +1,331 @@ +// +// Copyright © 2020 Microsoft +// +// SPDX-License-Identifier: Apache-2.0 +// + +#![allow(non_camel_case_types, clippy::upper_case_acronyms)] + +// +// CMP-Compare Two Operands +// + +use crate::arch::emulator::{EmulationError, PlatformEmulator}; +use crate::arch::x86::emulator::instructions::*; +use crate::arch::x86::regs::*; +use crate::arch::x86::Exception; + +// CMP affects OF, SF, ZF, AF, PF and CF +const FLAGS_MASK: u64 = CF | PF | AF | ZF | SF | OF; + +// TODO: Switch to inline asm when that's stable. Executing CMP (or any arthimetic instructions) +// natively and extracting RFLAGS will be much faster and make the code simpler. +fn calc_rflags_cpazso(op0: u64, op1: u64, op_size: usize) -> u64 { + let op_bits = op_size * 8; + let msb_shift = op_bits - 1; + // CMP is the same as SUB. + let result = op0.wrapping_sub(op1); + + // Carry-out vector for SUB. + let cout = (!op0 & op1) | ((!op0 ^ op1) & result); + + let cf = ((cout >> msb_shift) & 0x1) << CF_SHIFT; + + // PF only needs the least significant byte. XOR its higher 4 bits with its lower 4 bits then + // use the value directly. + let pf = ((0x9669 >> ((result ^ (result >> 4)) & 0xf)) & 0x1) << PF_SHIFT; + + // AF cares about the lowest 4 bits (nibble). msb_shift is 3 in this case. + let af = ((cout >> 3) & 0x1) << AF_SHIFT; + + let zf = u64::from(result & (!0u64 >> (63 - msb_shift)) == 0) << ZF_SHIFT; + + let sf = ((result >> msb_shift) & 0x1) << SF_SHIFT; + + // Overflow happens when two operands have the same sign but the result has a different sign. + let of = ((((op0 ^ op1) & (op0 ^ result)) >> msb_shift) & 0x1) << OF_SHIFT; + + cf | pf | af | zf | sf | of +} + +macro_rules! cmp_rm_r { + ($bound:ty) => { + fn emulate( + &self, + insn: &Instruction, + state: &mut T, + platform: &mut dyn PlatformEmulator, + ) -> Result<(), EmulationError> { + let op0_value = get_op(&insn, 0, std::mem::size_of::<$bound>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + let op1_value = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + + let cpazso = calc_rflags_cpazso(op0_value, op1_value, std::mem::size_of::<$bound>()); + + state.set_flags((state.flags() & !FLAGS_MASK) | cpazso); + + Ok(()) + } + }; +} + +macro_rules! cmp_r_rm { + ($bound:ty) => { + fn emulate( + &self, + insn: &Instruction, + state: &mut T, + platform: &mut dyn PlatformEmulator, + ) -> Result<(), EmulationError> { + let op0_value = get_op(&insn, 0, std::mem::size_of::<$bound>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + let op1_value = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + + let cpazso = calc_rflags_cpazso(op0_value, op1_value, std::mem::size_of::<$bound>()); + + state.set_flags((state.flags() & !FLAGS_MASK) | cpazso); + + Ok(()) + } + }; +} + +macro_rules! cmp_rm_imm { + ($imm:ty, $bound:ty) => { + fn emulate( + &self, + insn: &Instruction, + state: &mut T, + platform: &mut dyn PlatformEmulator, + ) -> Result<(), EmulationError> { + let op0_value = get_op(&insn, 0, std::mem::size_of::<$bound>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + let op1_value = get_op(&insn, 1, std::mem::size_of::<$imm>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + + let cpazso = calc_rflags_cpazso(op0_value, op1_value, std::mem::size_of::<$bound>()); + + state.set_flags((state.flags() & !FLAGS_MASK) | cpazso); + + Ok(()) + } + }; +} + +pub struct Cmp_rm64_r64; +impl InstructionHandler for Cmp_rm64_r64 { + cmp_rm_r!(u64); +} + +pub struct Cmp_rm32_r32; +impl InstructionHandler for Cmp_rm32_r32 { + cmp_rm_r!(u32); +} + +pub struct Cmp_rm16_r16; +impl InstructionHandler for Cmp_rm16_r16 { + cmp_rm_r!(u16); +} + +pub struct Cmp_rm8_r8; +impl InstructionHandler for Cmp_rm8_r8 { + cmp_rm_r!(u8); +} + +pub struct Cmp_r64_rm64; +impl InstructionHandler for Cmp_r64_rm64 { + cmp_r_rm!(u64); +} + +pub struct Cmp_r32_rm32; +impl InstructionHandler for Cmp_r32_rm32 { + cmp_r_rm!(u32); +} + +pub struct Cmp_r16_rm16; +impl InstructionHandler for Cmp_r16_rm16 { + cmp_r_rm!(u16); +} + +pub struct Cmp_r8_rm8; +impl InstructionHandler for Cmp_r8_rm8 { + cmp_r_rm!(u8); +} + +pub struct Cmp_AL_imm8; +impl InstructionHandler for Cmp_AL_imm8 { + cmp_rm_imm!(u8, u8); +} + +pub struct Cmp_AX_imm16; +impl InstructionHandler for Cmp_AX_imm16 { + cmp_rm_imm!(u16, u16); +} + +pub struct Cmp_EAX_imm32; +impl InstructionHandler for Cmp_EAX_imm32 { + cmp_rm_imm!(u32, u32); +} + +pub struct Cmp_RAX_imm32; +impl InstructionHandler for Cmp_RAX_imm32 { + cmp_rm_imm!(u32, u64); +} + +pub struct Cmp_rm8_imm8; +impl InstructionHandler for Cmp_rm8_imm8 { + cmp_rm_imm!(u8, u8); +} + +pub struct Cmp_rm16_imm16; +impl InstructionHandler for Cmp_rm16_imm16 { + cmp_rm_imm!(u16, u16); +} + +pub struct Cmp_rm32_imm32; +impl InstructionHandler for Cmp_rm32_imm32 { + cmp_rm_imm!(u32, u32); +} + +pub struct Cmp_rm64_imm32; +impl InstructionHandler for Cmp_rm64_imm32 { + cmp_rm_imm!(u32, u64); +} + +pub struct Cmp_rm16_imm8; +impl InstructionHandler for Cmp_rm16_imm8 { + cmp_rm_imm!(u8, u16); +} + +pub struct Cmp_rm32_imm8; +impl InstructionHandler for Cmp_rm32_imm8 { + cmp_rm_imm!(u8, u32); +} + +pub struct Cmp_rm64_imm8; +impl InstructionHandler for Cmp_rm64_imm8 { + cmp_rm_imm!(u8, u64); +} + +#[cfg(test)] +mod tests { + #![allow(unused_mut)] + + use super::*; + use crate::arch::x86::emulator::mock_vmm::*; + + #[test] + // cmp ah,al + fn test_cmp_rm8_r8_1() { + let rax: u64 = 0x0; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x38, 0xc4]; // cmp ah,al + let mut vmm = MockVmm::new(ip, vec![(Register::RAX, rax)], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let rflags: u64 = vmm.cpu_state(cpu_id).unwrap().flags() & FLAGS_MASK; + assert_eq!(0b1000100, rflags); + } + + #[test] + // cmp eax,100 + fn test_cmp_rm32_imm8_1() { + let rax: u64 = 0xabcdef; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x83, 0xf8, 0x64]; // cmp eax,100 + let mut vmm = MockVmm::new(ip, vec![(Register::RAX, rax)], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let rflags: u64 = vmm.cpu_state(cpu_id).unwrap().flags() & FLAGS_MASK; + assert_eq!(0b100, rflags); + } + + #[test] + // cmp eax,-1 + fn test_cmp_rm32_imm8_2() { + let rax: u64 = 0xabcdef; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x83, 0xf8, 0xff]; // cmp eax,-1 + let mut vmm = MockVmm::new(ip, vec![(Register::RAX, rax)], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let rflags: u64 = vmm.cpu_state(cpu_id).unwrap().flags() & FLAGS_MASK; + assert_eq!(0b101, rflags); + } + + #[test] + // cmp rax,rbx + fn test_cmp_rm64_r64() { + let rax: u64 = 0xabcdef; + let rbx: u64 = 0x1234; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x48, 0x39, 0xd8, 0x00, 0xc3]; // cmp rax,rbx + two bytes garbage + let mut vmm = MockVmm::new(ip, vec![(Register::RAX, rax), (Register::RBX, rbx)], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let rflags: u64 = vmm.cpu_state(cpu_id).unwrap().flags() & FLAGS_MASK; + assert_eq!(0b100, rflags); + } + + #[test] + fn test_cmp_64() { + let data = [ + (0xabcdef, 0x1234, 0b100), + (0x0, 0x101, 0b1001_0101), + (0x0, 0x8000_0000_0000_0000, 0b1000_1000_0101), + (0x1234abcd, 0x1234abcd, 0b100_0100), + (0x1234abcd, 0xdeadbeef, 0b1001_0101), + (0xffff_ffff_ffff_ffff, 0xdeadbeef, 0b1000_0000), + (0xffff_ffff_ffff_ffff, 0x0, 0b1000_0100), + ]; + + for d in data.iter() { + let rax = d.0; + let rbx = d.1; + let insn = [0x48, 0x39, 0xd8]; // cmp rax,rbx + let mut vmm = MockVmm::new( + 0x1000, + vec![(Register::RAX, rax), (Register::RBX, rbx)], + None, + ); + assert!(vmm.emulate_first_insn(0, &insn).is_ok()); + + let rflags: u64 = vmm.cpu_state(0).unwrap().flags() & FLAGS_MASK; + assert_eq!(d.2, rflags); + } + } + + #[test] + fn test_cmp_32() { + let data = [ + (0xabcdef, 0x1234, 0b100), + (0x0, 0x101, 0b1001_0101), + (0x0, 0x8000_0000_0000_0000, 0b100_0100), // Same as cmp 0,0 due to truncation + (0x1234abcd, 0x1234abcd, 0b100_0100), + (0x1234abcd, 0xdeadbeef, 0b1_0101), + (0xffff_ffff_ffff_ffff, 0xdeadbeef, 0b0), // Same as cmp 0xffffffff,0xdeadbeef + (0xffff_ffff, 0x0, 0b1000_0100), + ]; + + for d in data.iter() { + let rax = d.0; + let rbx = d.1; + let insn = [0x39, 0xd8]; // cmp eax,ebx + let mut vmm = MockVmm::new( + 0x1000, + vec![(Register::RAX, rax), (Register::RBX, rbx)], + None, + ); + assert!(vmm.emulate_first_insn(0, &insn).is_ok()); + + let rflags: u64 = vmm.cpu_state(0).unwrap().flags() & FLAGS_MASK; + assert_eq!(d.2, rflags); + } + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mod.rs b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mod.rs new file mode 100644 index 0000000..3fac495 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mod.rs @@ -0,0 +1,153 @@ +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::arch::emulator::{EmulationError, PlatformEmulator, PlatformError}; +use crate::arch::x86::emulator::CpuStateManager; +use crate::arch::x86::Exception; +use iced_x86::*; + +pub mod cmp; +pub mod mov; +pub mod movs; +pub mod or; + +fn get_op( + insn: &Instruction, + op_index: u32, + op_size: usize, + state: &mut T, + platform: &mut dyn PlatformEmulator, +) -> Result { + if insn.op_count() < op_index + 1 { + return Err(PlatformError::InvalidOperand(anyhow!( + "Invalid operand {:?}", + op_index + ))); + } + + if !matches!(op_size, 1 | 2 | 4 | 8) { + return Err(PlatformError::InvalidOperand(anyhow!( + "Invalid operand size {:?}", + op_size + ))); + } + + let value = match insn + .try_op_kind(op_index) + .map_err(|e| PlatformError::InvalidOperand(e.into()))? + { + OpKind::Register => state.read_reg( + insn.try_op_register(op_index) + .map_err(|e| PlatformError::InvalidOperand(e.into()))?, + )?, + OpKind::Memory => { + let addr = memory_operand_address(insn, state, false)?; + let mut memory: [u8; 8] = [0; 8]; + platform.read_memory(addr, &mut memory[0..op_size])?; + ::from_le_bytes(memory) + } + OpKind::Immediate8 => insn.immediate8() as u64, + OpKind::Immediate8to16 => insn.immediate8to16() as u64, + OpKind::Immediate8to32 => insn.immediate8to32() as u64, + OpKind::Immediate8to64 => insn.immediate8to64() as u64, + OpKind::Immediate16 => insn.immediate16() as u64, + OpKind::Immediate32 => insn.immediate32() as u64, + OpKind::Immediate32to64 => insn.immediate32to64() as u64, + OpKind::Immediate64 => insn.immediate64(), + k => return Err(PlatformError::InvalidOperand(anyhow!("{:?}", k))), + }; + + Ok(value) +} + +fn set_op( + insn: &Instruction, + op_index: u32, + op_size: usize, + state: &mut T, + platform: &mut dyn PlatformEmulator, + value: u64, +) -> Result<(), PlatformError> { + if insn.op_count() < op_index + 1 { + return Err(PlatformError::InvalidOperand(anyhow!( + "Invalid operand {:?}", + op_index + ))); + } + + if !matches!(op_size, 1 | 2 | 4 | 8) { + return Err(PlatformError::InvalidOperand(anyhow!( + "Invalid operand size {:?}", + op_size + ))); + } + + match insn + .try_op_kind(op_index) + .map_err(|e| PlatformError::InvalidOperand(e.into()))? + { + OpKind::Register => state.write_reg( + insn.try_op_register(op_index) + .map_err(|e| PlatformError::InvalidOperand(e.into()))?, + value, + )?, + OpKind::Memory => { + let addr = memory_operand_address(insn, state, true)?; + platform.write_memory(addr, &value.to_le_bytes()[..op_size])?; + } + k => return Err(PlatformError::InvalidOperand(anyhow!("{:?}", k))), + }; + + Ok(()) +} + +// Returns the linear a.k.a. virtual address for a memory operand. +fn memory_operand_address( + insn: &Instruction, + state: &T, + write: bool, +) -> Result { + let mut address: u64 = 0; + + if insn.memory_base() != iced_x86::Register::None { + let base: u64 = state.read_reg(insn.memory_base())?; + address += base; + } + + if insn.memory_index() != iced_x86::Register::None { + let mut index: u64 = state.read_reg(insn.memory_index())?; + index *= insn.memory_index_scale() as u64; + + address += index; + } + + address += insn.memory_displacement64(); + + // Translate to a linear address. + state.linearize(insn.memory_segment(), address, write) +} + +pub trait InstructionHandler { + fn emulate( + &self, + insn: &Instruction, + state: &mut T, + platform: &mut dyn PlatformEmulator, + ) -> Result<(), EmulationError>; +} + +macro_rules! insn_format { + ($insn:ident) => {{ + let mut output = String::new(); + let mut formatter = FastFormatter::new(); + formatter + .options_mut() + .set_space_after_operand_separator(true); + formatter.format(&$insn, &mut output); + + output + }}; +} diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mov.rs b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mov.rs new file mode 100644 index 0000000..7ea9086 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mov.rs @@ -0,0 +1,620 @@ +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#![allow(non_camel_case_types)] + +// +// MOV-Move +// SDM Volume 1, Chapter 4.3 +// Copies the second operand (source operand) to the first operand (destination operand). +// + +use crate::arch::emulator::{EmulationError, PlatformEmulator}; +use crate::arch::x86::emulator::instructions::*; +use crate::arch::x86::Exception; + +macro_rules! mov_rm_r { + ($bound:ty) => { + fn emulate( + &self, + insn: &Instruction, + state: &mut T, + platform: &mut dyn PlatformEmulator, + ) -> Result<(), EmulationError> { + let src_reg_value = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + + set_op( + &insn, + 0, + std::mem::size_of::<$bound>(), + state, + platform, + src_reg_value, + ) + .map_err(EmulationError::PlatformEmulationError)?; + + Ok(()) + } + }; +} + +macro_rules! mov_rm_imm { + ($bound:ty) => { + fn emulate( + &self, + insn: &Instruction, + state: &mut T, + platform: &mut dyn PlatformEmulator, + ) -> Result<(), EmulationError> { + let imm = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + + set_op( + &insn, + 0, + std::mem::size_of::<$bound>(), + state, + platform, + imm, + ) + .map_err(EmulationError::PlatformEmulationError)?; + + Ok(()) + } + }; +} + +macro_rules! movzx { + ($dest_op_size:ty, $src_op_size:ty) => { + fn emulate( + &self, + insn: &Instruction, + state: &mut T, + platform: &mut dyn PlatformEmulator, + ) -> Result<(), EmulationError> { + let src_value = get_op( + &insn, + 1, + std::mem::size_of::<$src_op_size>(), + state, + platform, + ) + .map_err(EmulationError::PlatformEmulationError)?; + + set_op( + &insn, + 0, + std::mem::size_of::<$dest_op_size>(), + state, + platform, + src_value, + ) + .map_err(EmulationError::PlatformEmulationError)?; + + Ok(()) + } + }; +} + +// MOV r/rm is a special case of MOVZX, where both operands have the same size. +macro_rules! mov_r_rm { + ($op_size:ty) => { + movzx!($op_size, $op_size); + }; +} + +macro_rules! mov_r_imm { + ($bound:ty) => { + fn emulate( + &self, + insn: &Instruction, + state: &mut T, + platform: &mut dyn PlatformEmulator, + ) -> Result<(), EmulationError> { + let imm = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + + set_op( + &insn, + 0, + std::mem::size_of::<$bound>(), + state, + platform, + imm, + ) + .map_err(EmulationError::PlatformEmulationError)?; + + Ok(()) + } + }; +} + +pub struct Mov_r8_rm8; +impl InstructionHandler for Mov_r8_rm8 { + mov_r_rm!(u8); +} + +pub struct Mov_r8_imm8; +impl InstructionHandler for Mov_r8_imm8 { + mov_r_imm!(u8); +} + +pub struct Mov_r16_rm16; +impl InstructionHandler for Mov_r16_rm16 { + mov_r_rm!(u16); +} + +pub struct Mov_r16_imm16; +impl InstructionHandler for Mov_r16_imm16 { + mov_r_imm!(u16); +} + +pub struct Mov_r32_rm32; +impl InstructionHandler for Mov_r32_rm32 { + mov_r_rm!(u32); +} + +pub struct Mov_r32_imm32; +impl InstructionHandler for Mov_r32_imm32 { + mov_r_imm!(u32); +} + +pub struct Mov_r64_rm64; +impl InstructionHandler for Mov_r64_rm64 { + mov_r_rm!(u64); +} + +pub struct Mov_r64_imm64; +impl InstructionHandler for Mov_r64_imm64 { + mov_r_imm!(u64); +} + +pub struct Mov_rm8_imm8; +impl InstructionHandler for Mov_rm8_imm8 { + mov_rm_imm!(u8); +} + +pub struct Mov_rm8_r8; +impl InstructionHandler for Mov_rm8_r8 { + mov_rm_r!(u8); +} + +pub struct Mov_rm16_imm16; +impl InstructionHandler for Mov_rm16_imm16 { + mov_rm_imm!(u16); +} + +pub struct Mov_rm16_r16; +impl InstructionHandler for Mov_rm16_r16 { + mov_rm_r!(u16); +} + +pub struct Mov_rm32_imm32; +impl InstructionHandler for Mov_rm32_imm32 { + mov_rm_imm!(u32); +} + +pub struct Mov_rm32_r32; +impl InstructionHandler for Mov_rm32_r32 { + mov_rm_r!(u32); +} + +pub struct Mov_rm64_imm32; +impl InstructionHandler for Mov_rm64_imm32 { + mov_rm_imm!(u32); +} + +pub struct Mov_rm64_r64; +impl InstructionHandler for Mov_rm64_r64 { + mov_rm_r!(u64); +} + +// MOVZX +pub struct Movzx_r16_rm8; +impl InstructionHandler for Movzx_r16_rm8 { + movzx!(u16, u8); +} + +pub struct Movzx_r32_rm8; +impl InstructionHandler for Movzx_r32_rm8 { + movzx!(u32, u8); +} + +pub struct Movzx_r64_rm8; +impl InstructionHandler for Movzx_r64_rm8 { + movzx!(u64, u8); +} + +pub struct Movzx_r32_rm16; +impl InstructionHandler for Movzx_r32_rm16 { + movzx!(u32, u16); +} + +pub struct Movzx_r64_rm16; +impl InstructionHandler for Movzx_r64_rm16 { + movzx!(u64, u16); +} + +#[cfg(test)] +mod tests { + #![allow(unused_mut)] + use super::*; + use crate::arch::x86::emulator::mock_vmm::*; + + #[test] + // mov rax,rbx + fn test_mov_r64_r64() { + let rbx: u64 = 0x8899aabbccddeeff; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x48, 0x89, 0xd8]; + let mut vmm = MockVmm::new(ip, vec![(Register::RBX, rbx)], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let rax: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::RAX) + .unwrap(); + assert_eq!(rax, rbx); + } + + #[test] + // mov rax,0x1122334411223344 + fn test_mov_r64_imm64() { + let imm64: u64 = 0x1122334411223344; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x48, 0xb8, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11]; + let mut vmm = MockVmm::new(ip, vec![], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let rax: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::RAX) + .unwrap(); + assert_eq!(rax, imm64); + } + + #[test] + // mov rax, [rax+rax] + fn test_mov_r64_m64() { + let target_rax: u64 = 0x1234567812345678; + let mut rax: u64 = 0x100; + let ip: u64 = 0x1000; + let cpu_id = 0; + let memory: [u8; 8] = target_rax.to_le_bytes(); + let insn = [0x48, 0x8b, 0x04, 0x00]; + let mut vmm = MockVmm::new(ip, vec![(Register::RAX, rax)], Some((rax + rax, &memory))); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + rax = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::RAX) + .unwrap(); + assert_eq!(rax, target_rax); + } + + #[test] + // mov al,0x11 + fn test_mov_r8_imm8() { + let imm8: u8 = 0x11; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0xb0, 0x11]; + let mut vmm = MockVmm::new(ip, vec![], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let al = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::AL) + .unwrap(); + assert_eq!(al as u8, imm8); + } + + #[test] + // mov eax,0x11 + fn test_mov_r32_imm8() { + let imm8: u8 = 0x11; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0xb8, 0x11, 0x00, 0x00, 0x00]; + let mut vmm = MockVmm::new(ip, vec![], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let eax = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::EAX) + .unwrap(); + assert_eq!(eax as u8, imm8); + } + + #[test] + // mov rax,0x11223344 + fn test_mov_r64_imm32() { + let imm32: u32 = 0x11223344; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x48, 0xc7, 0xc0, 0x44, 0x33, 0x22, 0x11]; + let mut vmm = MockVmm::new(ip, vec![], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let rax: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::RAX) + .unwrap(); + assert_eq!(rax, imm32 as u64); + } + + #[test] + // mov byte ptr [rax],dh + fn test_mov_m8_r8() { + let rax: u64 = 0x100; + let dh: u8 = 0x99; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x88, 0x30]; + let mut vmm = MockVmm::new( + ip, + vec![(Register::RAX, rax), (Register::DH, dh.into())], + None, + ); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let mut memory: [u8; 1] = [0; 1]; + vmm.read_memory(rax, &mut memory).unwrap(); + + assert_eq!(u8::from_le_bytes(memory), dh); + } + + #[test] + // mov dword ptr [rax],esi + fn test_mov_m32_r32() { + let rax: u64 = 0x100; + let esi: u32 = 0x8899; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x89, 0x30]; + let mut vmm = MockVmm::new( + ip, + vec![(Register::RAX, rax), (Register::ESI, esi.into())], + None, + ); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let mut memory: [u8; 4] = [0; 4]; + vmm.read_memory(rax, &mut memory).unwrap(); + + assert_eq!(u32::from_le_bytes(memory), esi); + } + + #[test] + // mov dword ptr [rax+0x00000001],edi + fn test_mov_m32imm32_r32() { + let rax: u64 = 0x100; + let displacement: u64 = 0x1; + let edi: u32 = 0x8899; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x89, 0x3c, 0x05, 0x01, 0x00, 0x00, 0x00]; + let mut vmm = MockVmm::new( + ip, + vec![(Register::RAX, rax), (Register::EDI, edi.into())], + None, + ); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let mut memory: [u8; 4] = [0; 4]; + vmm.read_memory(rax + displacement, &mut memory).unwrap(); + + assert_eq!(u32::from_le_bytes(memory), edi); + } + + #[test] + // mov eax,dword ptr [rax+10h] + fn test_mov_r32_m32imm32() { + let rax: u64 = 0x100; + let displacement: u64 = 0x10; + let eax: u32 = 0xaabbccdd; + let memory: [u8; 4] = eax.to_le_bytes(); + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x8b, 0x40, 0x10]; + let mut vmm = MockVmm::new( + ip, + vec![(Register::RAX, rax)], + Some((rax + displacement, &memory)), + ); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let new_eax = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::EAX) + .unwrap(); + assert_eq!(new_eax, eax as u64); + } + + #[test] + // mov al,byte ptr [rax+10h] + fn test_mov_r8_m32imm32() { + let rax: u64 = 0x100; + let displacement: u64 = 0x10; + let al: u8 = 0xaa; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x8a, 0x40, 0x10]; + let memory: [u8; 1] = al.to_le_bytes(); + let mut vmm = MockVmm::new( + ip, + vec![(Register::RAX, rax)], + Some((rax + displacement, &memory)), + ); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let new_al = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::AL) + .unwrap(); + assert_eq!(new_al, al as u64); + } + + #[test] + // mov rax, 0x100 + // mov rbx, qword ptr [rax+10h] + fn test_mov_r64_imm64_and_r64_m64() { + let target_rax: u64 = 0x1234567812345678; + let rax: u64 = 0x100; + let displacement: u64 = 0x10; + let ip: u64 = 0x1000; + let cpu_id = 0; + let memory: [u8; 8] = target_rax.to_le_bytes(); + let insn = [ + 0x48, 0xc7, 0xc0, 0x00, 0x01, 0x00, 0x00, // mov rax, 0x100 + 0x48, 0x8b, 0x58, 0x10, // mov rbx, qword ptr [rax+10h] + ]; + let mut vmm = MockVmm::new(ip, vec![], Some((rax + displacement, &memory))); + assert!(vmm.emulate_insn(cpu_id, &insn, Some(2)).is_ok()); + + let rbx: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::RBX) + .unwrap(); + assert_eq!(rbx, target_rax); + } + + #[test] + // mov rax, 0x100 + // mov rbx, qword ptr [rax+10h] + fn test_mov_r64_imm64_and_r64_m64_first_insn() { + let target_rax: u64 = 0x1234567812345678; + let rax: u64 = 0x100; + let displacement: u64 = 0x10; + let ip: u64 = 0x1000; + let cpu_id = 0; + let memory: [u8; 8] = target_rax.to_le_bytes(); + let insn = [ + 0x48, 0xc7, 0xc0, 0x00, 0x01, 0x00, 0x00, // mov rax, 0x100 + 0x48, 0x8b, 0x58, 0x10, // mov rbx, qword ptr [rax+10h] + ]; + + let mut vmm = MockVmm::new(ip, vec![], Some((rax + displacement, &memory))); + // Only run the first instruction. + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + assert_eq!(ip + 7, vmm.cpu_state(cpu_id).unwrap().ip()); + + let new_rax: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::RAX) + .unwrap(); + assert_eq!(rax, new_rax); + } + + #[test] + // mov rax, 0x100 + // mov rbx, qword ptr [rax+10h] + // mov rax, 0x200 + fn test_mov_r64_imm64_and_r64_m64_two_insns() { + let target_rax: u64 = 0x1234567812345678; + let rax: u64 = 0x100; + let displacement: u64 = 0x10; + let ip: u64 = 0x1000; + let cpu_id = 0; + let memory: [u8; 8] = target_rax.to_le_bytes(); + let insn = [ + 0x48, 0xc7, 0xc0, 0x00, 0x01, 0x00, 0x00, // mov rax, 0x100 + 0x48, 0x8b, 0x58, 0x10, // mov rbx, qword ptr [rax+10h] + 0x48, 0xc7, 0xc0, 0x00, 0x02, 0x00, 0x00, // mov rax, 0x200 + ]; + + let mut vmm = MockVmm::new(ip, vec![], Some((rax + displacement, &memory))); + // Run the 2 first instructions. + assert!(vmm.emulate_insn(cpu_id, &insn, Some(2)).is_ok()); + + assert_eq!(ip + 7 + 4, vmm.cpu_state(cpu_id).unwrap().ip()); + + let rbx: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::RBX) + .unwrap(); + assert_eq!(rbx, target_rax); + + // Check that rax is still at 0x100 + let new_rax: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::RAX) + .unwrap(); + assert_eq!(rax, new_rax); + } + + #[test] + // movzx eax, bl + fn test_movzx_r32_r8l() { + let bx: u16 = 0x8899; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x0f, 0xb6, 0xc3]; + let mut vmm = MockVmm::new(ip, vec![(Register::BX, bx as u64)], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let eax: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::EAX) + .unwrap(); + assert_eq!(eax, (bx & 0xff) as u64); + } + + #[test] + // movzx eax, bh + fn test_movzx_r32_r8h() { + let bx: u16 = 0x8899; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x0f, 0xb6, 0xc7]; + let mut vmm = MockVmm::new(ip, vec![(Register::BX, bx as u64)], None); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let eax: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::EAX) + .unwrap(); + assert_eq!(eax, (bx >> 8) as u64); + } + + #[test] + // movzx eax, byte ptr [rbx] + fn test_movzx_r32_m8() { + let rbx: u64 = 0x100; + let value: u8 = 0xaa; + let ip: u64 = 0x1000; + let cpu_id = 0; + let insn = [0x0f, 0xb7, 0x03]; + let memory: [u8; 1] = value.to_le_bytes(); + let mut vmm = MockVmm::new(ip, vec![(Register::RBX, rbx)], Some((rbx, &memory))); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let eax: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::EAX) + .unwrap(); + assert_eq!(eax, value as u64); + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/movs.rs b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/movs.rs new file mode 100644 index 0000000..23fd049 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/movs.rs @@ -0,0 +1,145 @@ +// +// Copyright © 2021 Microsoft +// +// SPDX-License-Identifier: Apache-2.0 +// + +#![allow(non_camel_case_types)] + +// +// MOVS - Move Data from String to String +// + +use crate::arch::emulator::{EmulationError, PlatformEmulator}; +use crate::arch::x86::emulator::instructions::*; +use crate::arch::x86::regs::DF; +use crate::arch::x86::Exception; + +pub struct Movsd_m32_m32; +impl InstructionHandler for Movsd_m32_m32 { + fn emulate( + &self, + insn: &Instruction, + state: &mut T, + platform: &mut dyn PlatformEmulator, + ) -> Result<(), EmulationError> { + let mut count: u64 = if insn.has_rep_prefix() { + state + .read_reg(Register::ECX) + .map_err(|e| EmulationError::InvalidOperand(anyhow!(e)))? + } else { + 1 + }; + + let mut rsi = state + .read_reg(Register::RSI) + .map_err(|e| EmulationError::InvalidOperand(anyhow!(e)))?; + let mut rdi = state + .read_reg(Register::RDI) + .map_err(|e| EmulationError::InvalidOperand(anyhow!(e)))?; + + let df = (state.flags() & DF) != 0; + let len = std::mem::size_of::(); + + while count > 0 { + let mut memory: [u8; 4] = [0; 4]; + + let src = state + .linearize(Register::DS, rsi, false) + .map_err(|e| EmulationError::InvalidOperand(anyhow!(e)))?; + let dst = state + .linearize(Register::ES, rdi, true) + .map_err(|e| EmulationError::InvalidOperand(anyhow!(e)))?; + + platform + .read_memory(src, &mut memory[0..len]) + .map_err(EmulationError::PlatformEmulationError)?; + platform + .write_memory(dst, &memory[0..len]) + .map_err(EmulationError::PlatformEmulationError)?; + + if df { + rsi = rsi.wrapping_sub(len as u64); + rdi = rdi.wrapping_sub(len as u64); + } else { + rsi = rsi.wrapping_add(len as u64); + rdi = rdi.wrapping_add(len as u64); + } + count -= 1; + } + + state + .write_reg(Register::RSI, rsi) + .map_err(|e| EmulationError::InvalidOperand(anyhow!(e)))?; + state + .write_reg(Register::RDI, rdi) + .map_err(|e| EmulationError::InvalidOperand(anyhow!(e)))?; + if insn.has_rep_prefix() { + state + .write_reg(Register::ECX, 0) + .map_err(|e| EmulationError::InvalidOperand(anyhow!(e)))?; + } + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + #![allow(unused_mut)] + use super::*; + use crate::arch::x86::emulator::mock_vmm::*; + + #[test] + fn test_rep_movsd_m32_m32() { + let ip: u64 = 0x1000; + let memory: [u8; 24] = [ + 0x78, 0x56, 0x34, 0x12, // 0x12345678 + 0xdd, 0xcc, 0xbb, 0xaa, // 0xaabbccdd + 0xa5, 0x5a, 0xa5, 0x5a, // 0x5aa55aa5 + 0x00, 0x00, 0x00, 0x00, // 0x00000000 + 0x00, 0x00, 0x00, 0x00, // 0x00000000 + 0x00, 0x00, 0x00, 0x00, // 0x00000000 + ]; + let insn = [0xf3, 0xa5]; // rep movsd + let regs = vec![(Register::ECX, 3), (Register::ESI, 0), (Register::EDI, 0xc)]; + let mut data = [0u8; 4]; + + let mut vmm = MockVmm::new(ip, regs, Some((0, &memory))); + + assert!(vmm.emulate_first_insn(0, &insn).is_ok()); + + vmm.read_memory(0xc, &mut data).unwrap(); + assert_eq!(0x12345678, ::from_le_bytes(data)); + vmm.read_memory(0xc + 4, &mut data).unwrap(); + assert_eq!(0xaabbccdd, ::from_le_bytes(data)); + vmm.read_memory(0xc + 8, &mut data).unwrap(); + assert_eq!(0x5aa55aa5, ::from_le_bytes(data)); + // The rest should be default value 0 from MockVmm + vmm.read_memory(0xc + 12, &mut data).unwrap(); + assert_eq!(0x0, ::from_le_bytes(data)); + } + + #[test] + fn test_movsd_m32_m32() { + let ip: u64 = 0x1000; + let memory: [u8; 4] = [ + 0x78, 0x56, 0x34, 0x12, // 0x12345678 + ]; + let insn = [0xa5]; // movsd + let regs = vec![(Register::ESI, 0), (Register::EDI, 0x8)]; + let mut data = [0u8; 4]; + + let mut vmm = MockVmm::new(ip, regs, Some((0, &memory))); + + assert!(vmm.emulate_first_insn(0, &insn).is_ok()); + + vmm.read_memory(0x8, &mut data).unwrap(); + assert_eq!(0x12345678, ::from_le_bytes(data)); + // The rest should be default value 0 from MockVmm + vmm.read_memory(0x4, &mut data).unwrap(); + assert_eq!(0x0, ::from_le_bytes(data)); + vmm.read_memory(0x8 + 8, &mut data).unwrap(); + assert_eq!(0x0, ::from_le_bytes(data)); + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/or.rs b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/or.rs new file mode 100644 index 0000000..bdabc2d --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/or.rs @@ -0,0 +1,83 @@ +// +// Copyright © 2021 Microsoft +// +// SPDX-License-Identifier: Apache-2.0 +// + +#![allow(non_camel_case_types)] + +// +// OR - Logical inclusive OR +// + +use crate::arch::emulator::{EmulationError, PlatformEmulator}; +use crate::arch::x86::emulator::instructions::*; +use crate::arch::x86::Exception; + +macro_rules! or_rm_r { + ($bound:ty) => { + fn emulate( + &self, + insn: &Instruction, + state: &mut T, + platform: &mut dyn PlatformEmulator, + ) -> Result<(), EmulationError> { + let src_reg_value = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + + let dst_value = get_op(&insn, 0, std::mem::size_of::<$bound>(), state, platform) + .map_err(EmulationError::PlatformEmulationError)?; + + let result = src_reg_value | dst_value; + + set_op( + &insn, + 0, + std::mem::size_of::<$bound>(), + state, + platform, + result, + ) + .map_err(EmulationError::PlatformEmulationError)?; + + Ok(()) + } + }; +} + +pub struct Or_rm8_r8; +impl InstructionHandler for Or_rm8_r8 { + or_rm_r!(u8); +} + +#[cfg(test)] +mod tests { + #![allow(unused_mut)] + use super::*; + + use crate::arch::x86::emulator::mock_vmm::*; + + #[test] + // or byte ptr [rax+1h], sil + fn test_or_rm8_r8() { + let rax = 0; + let insn = [0x40, 0x08, 0x70, 0x1]; + let cpu_id = 0; + let ip: u64 = 0x1000; + let sil = 0xaa; + let memory = [0x0, 0x55]; + + let mut vmm = MockVmm::new( + ip, + vec![(Register::SIL, sil), (Register::RAX, rax)], + Some((0, &memory)), + ); + + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_ok()); + + let mut out: [u8; 1] = [0; 1]; + + vmm.read_memory(rax + 1, &mut out).unwrap(); + assert_eq!(u8::from_le_bytes(out), 0xff); + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/mod.rs b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/mod.rs new file mode 100644 index 0000000..c0d2860 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/mod.rs @@ -0,0 +1,860 @@ +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::arch::emulator::{EmulationError, EmulationResult, PlatformEmulator, PlatformError}; +use crate::arch::x86::emulator::instructions::*; +use crate::arch::x86::regs::{CR0_PE, EFER_LMA}; +use crate::arch::x86::{ + segment_type_expand_down, segment_type_ro, Exception, SegmentRegister, SpecialRegisters, + StandardRegisters, +}; +use anyhow::Context; +use iced_x86::*; + +#[macro_use] +mod instructions; + +/// x86 CPU modes +#[derive(Debug, PartialEq, Eq)] +pub enum CpuMode { + /// Real mode + Real, + + /// Virtual 8086 mode + Virtual8086, + + /// 16-bit protected mode + Protected16, + + /// 32-bit protected mode + Protected, + + /// 64-bit mode, a.k.a. long mode + Long, +} + +/// CpuStateManager manages an x86 CPU state. +/// +/// Instruction emulation handlers get a mutable reference to +/// a `CpuStateManager` implementation, representing the current state of the +/// CPU they have to emulate an instruction stream against. Usually those +/// handlers will modify the CPU state by modifying `CpuState` and it is up to +/// the handler caller to commit those changes back by invoking a +/// `PlatformEmulator` implementation `set_state()` method. +/// +pub trait CpuStateManager: Clone { + /// Reads a CPU register. + /// + /// # Arguments + /// + /// * `reg` - A general purpose, control or debug register. + fn read_reg(&self, reg: Register) -> Result; + + /// Write to a CPU register. + /// + /// # Arguments + /// + /// * `reg` - A general purpose, control or debug register. + /// * `val` - The value to load. + fn write_reg(&mut self, reg: Register, val: u64) -> Result<(), PlatformError>; + + /// Reads a segment register. + /// + /// # Arguments + /// + /// * `reg` - A segment register. + fn read_segment(&self, reg: Register) -> Result; + + /// Write to a segment register. + /// + /// # Arguments + /// + /// * `reg` - A segment register. + /// * `segment_reg` - The segment register value to load. + fn write_segment( + &mut self, + reg: Register, + segment_reg: SegmentRegister, + ) -> Result<(), PlatformError>; + + /// Get the CPU instruction pointer. + fn ip(&self) -> u64; + + /// Set the CPU instruction pointer. + /// + /// # Arguments + /// + /// * `ip` - The CPU instruction pointer. + fn set_ip(&mut self, ip: u64); + + /// Get the CPU Extended Feature Enable Register. + fn efer(&self) -> u64; + + /// Set the CPU Extended Feature Enable Register. + /// + /// # Arguments + /// + /// * `efer` - The CPU EFER value. + fn set_efer(&mut self, efer: u64); + + /// Get the CPU flags. + fn flags(&self) -> u64; + + /// Set the CPU flags. + /// + /// # Arguments + /// + /// * `flags` - The CPU flags + fn set_flags(&mut self, flags: u64); + + /// Get the CPU mode. + fn mode(&self) -> Result; + + /// Translate a logical (segmented) address into a linear (virtual) one. + /// + /// # Arguments + /// + /// * `segment` - Which segment to use for linearization + /// * `logical_addr` - The logical address to be translated + fn linearize( + &self, + segment: Register, + logical_addr: u64, + write: bool, + ) -> Result { + let segment_register = self.read_segment(segment)?; + let mode = self.mode()?; + + match mode { + CpuMode::Long => { + // TODO Check that we got a canonical address. + Ok(logical_addr + .checked_add(segment_register.base) + .ok_or_else(|| { + PlatformError::InvalidAddress(anyhow!( + "Logical address {:#x} can not be linearized with segment {:#x?}", + logical_addr, + segment_register + )) + })?) + } + + CpuMode::Protected | CpuMode::Real => { + let segment_type = segment_register.segment_type(); + + // Must not write to a read-only segment. + if segment_type_ro(segment_type) && write { + return Err(PlatformError::InvalidAddress(anyhow!( + "Can not write to a read-only segment" + ))); + } + + let logical_addr = logical_addr & 0xffff_ffffu64; + let mut segment_limit: u32 = if segment_register.granularity() != 0 { + (segment_register.limit << 12) | 0xfff + } else { + segment_register.limit + }; + + // Expand-down segment + if segment_type_expand_down(segment_type) { + if logical_addr >= segment_limit.into() { + return Err(PlatformError::InvalidAddress(anyhow!( + "{:#x} is off limits {:#x} (expand down)", + logical_addr, + segment_limit + ))); + } + + if segment_register.db() != 0 { + segment_limit = 0xffffffff + } else { + segment_limit = 0xffff + } + } + + if logical_addr > segment_limit.into() { + return Err(PlatformError::InvalidAddress(anyhow!( + "{:#x} is off limits {:#x}", + logical_addr, + segment_limit + ))); + } + + Ok(logical_addr + segment_register.base) + } + + _ => Err(PlatformError::UnsupportedCpuMode(anyhow!("{:?}", mode))), + } + } +} + +const REGISTER_MASK_64: u64 = 0xffff_ffff_ffff_ffffu64; +const REGISTER_MASK_32: u64 = 0xffff_ffffu64; +const REGISTER_MASK_16: u64 = 0xffffu64; +const REGISTER_MASK_8: u64 = 0xffu64; + +macro_rules! set_reg { + ($reg:expr, $mask:expr, $value:expr) => { + $reg = ($reg & $mask) | $value + }; +} + +#[derive(Clone, Default, Debug)] +/// A minimal, emulated CPU state. +/// +/// Hypervisors needing x86 emulation can choose to either use their own +/// CPU state structures and implement the CpuStateManager interface for it, +/// or use `EmulatorCpuState`. The latter implies creating a new state +/// `EmulatorCpuState` instance for each platform `cpu_state()` call, which +/// might be less efficient. +pub struct EmulatorCpuState { + pub regs: StandardRegisters, + pub sregs: SpecialRegisters, +} + +impl CpuStateManager for EmulatorCpuState { + fn read_reg(&self, reg: Register) -> Result { + let mut reg_value: u64 = match reg { + Register::RAX | Register::EAX | Register::AX | Register::AL | Register::AH => { + self.regs.rax + } + Register::RBX | Register::EBX | Register::BX | Register::BL | Register::BH => { + self.regs.rbx + } + Register::RCX | Register::ECX | Register::CX | Register::CL | Register::CH => { + self.regs.rcx + } + Register::RDX | Register::EDX | Register::DX | Register::DL | Register::DH => { + self.regs.rdx + } + Register::RSP | Register::ESP | Register::SP => self.regs.rsp, + Register::RBP | Register::EBP | Register::BP => self.regs.rbp, + Register::RSI | Register::ESI | Register::SI | Register::SIL => self.regs.rsi, + Register::RDI | Register::EDI | Register::DI | Register::DIL => self.regs.rdi, + Register::R8 | Register::R8D | Register::R8W | Register::R8L => self.regs.r8, + Register::R9 | Register::R9D | Register::R9W | Register::R9L => self.regs.r9, + Register::R10 | Register::R10D | Register::R10W | Register::R10L => self.regs.r10, + Register::R11 | Register::R11D | Register::R11W | Register::R11L => self.regs.r11, + Register::R12 | Register::R12D | Register::R12W | Register::R12L => self.regs.r12, + Register::R13 | Register::R13D | Register::R13W | Register::R13L => self.regs.r13, + Register::R14 | Register::R14D | Register::R14W | Register::R14L => self.regs.r14, + Register::R15 | Register::R15D | Register::R15W | Register::R15L => self.regs.r15, + Register::CR0 => self.sregs.cr0, + Register::CR2 => self.sregs.cr2, + Register::CR3 => self.sregs.cr3, + Register::CR4 => self.sregs.cr4, + Register::CR8 => self.sregs.cr8, + + r => { + return Err(PlatformError::InvalidRegister(anyhow!( + "read_reg invalid GPR {:?}", + r + ))) + } + }; + + reg_value = if reg.is_gpr64() || reg.is_cr() { + reg_value + } else if reg.is_gpr32() { + reg_value & REGISTER_MASK_32 + } else if reg.is_gpr16() { + reg_value & REGISTER_MASK_16 + } else if reg.is_gpr8() { + if reg == Register::AH + || reg == Register::BH + || reg == Register::CH + || reg == Register::DH + { + (reg_value >> 8) & REGISTER_MASK_8 + } else { + reg_value & REGISTER_MASK_8 + } + } else { + return Err(PlatformError::InvalidRegister(anyhow!( + "read_reg invalid GPR {:?}", + reg + ))); + }; + + debug!("Register read: {:#x} from {:?}", reg_value, reg); + + Ok(reg_value) + } + + fn write_reg(&mut self, reg: Register, val: u64) -> Result<(), PlatformError> { + debug!("Register write: {:#x} to {:?}", val, reg); + + // SDM Vol 1 - 3.4.1.1 + // + // 8-bit and 16-bit operands generate an 8-bit or 16-bit result. + // The upper 56 bits or 48 bits (respectively) of the destination + // general-purpose register are not modified by the operation. + let (reg_value, mask): (u64, u64) = if reg.is_gpr64() || reg.is_cr() { + (val, !REGISTER_MASK_64) + } else if reg.is_gpr32() { + (val & REGISTER_MASK_32, !REGISTER_MASK_64) + } else if reg.is_gpr16() { + (val & REGISTER_MASK_16, !REGISTER_MASK_16) + } else if reg.is_gpr8() { + if reg == Register::AH + || reg == Register::BH + || reg == Register::CH + || reg == Register::DH + { + ((val & REGISTER_MASK_8) << 8, !(REGISTER_MASK_8 << 8)) + } else { + (val & REGISTER_MASK_8, !REGISTER_MASK_8) + } + } else { + return Err(PlatformError::InvalidRegister(anyhow!( + "write_reg invalid register {:?}", + reg + ))); + }; + + match reg { + Register::RAX | Register::EAX | Register::AX | Register::AL | Register::AH => { + set_reg!(self.regs.rax, mask, reg_value); + } + Register::RBX | Register::EBX | Register::BX | Register::BL | Register::BH => { + set_reg!(self.regs.rbx, mask, reg_value); + } + Register::RCX | Register::ECX | Register::CX | Register::CL | Register::CH => { + set_reg!(self.regs.rcx, mask, reg_value); + } + Register::RDX | Register::EDX | Register::DX | Register::DL | Register::DH => { + set_reg!(self.regs.rdx, mask, reg_value); + } + Register::RSP | Register::ESP | Register::SP => { + set_reg!(self.regs.rsp, mask, reg_value) + } + Register::RBP | Register::EBP | Register::BP => { + set_reg!(self.regs.rbp, mask, reg_value) + } + Register::RSI | Register::ESI | Register::SI | Register::SIL => { + set_reg!(self.regs.rsi, mask, reg_value) + } + Register::RDI | Register::EDI | Register::DI | Register::DIL => { + set_reg!(self.regs.rdi, mask, reg_value) + } + Register::R8 | Register::R8D | Register::R8W | Register::R8L => { + set_reg!(self.regs.r8, mask, reg_value) + } + Register::R9 | Register::R9D | Register::R9W | Register::R9L => { + set_reg!(self.regs.r9, mask, reg_value) + } + Register::R10 | Register::R10D | Register::R10W | Register::R10L => { + set_reg!(self.regs.r10, mask, reg_value) + } + Register::R11 | Register::R11D | Register::R11W | Register::R11L => { + set_reg!(self.regs.r11, mask, reg_value) + } + Register::R12 | Register::R12D | Register::R12W | Register::R12L => { + set_reg!(self.regs.r12, mask, reg_value) + } + Register::R13 | Register::R13D | Register::R13W | Register::R13L => { + set_reg!(self.regs.r13, mask, reg_value) + } + Register::R14 | Register::R14D | Register::R14W | Register::R14L => { + set_reg!(self.regs.r14, mask, reg_value) + } + Register::R15 | Register::R15D | Register::R15W | Register::R15L => { + set_reg!(self.regs.r15, mask, reg_value) + } + Register::CR0 => set_reg!(self.sregs.cr0, mask, reg_value), + Register::CR2 => set_reg!(self.sregs.cr2, mask, reg_value), + Register::CR3 => set_reg!(self.sregs.cr3, mask, reg_value), + Register::CR4 => set_reg!(self.sregs.cr4, mask, reg_value), + Register::CR8 => set_reg!(self.sregs.cr8, mask, reg_value), + _ => { + return Err(PlatformError::InvalidRegister(anyhow!( + "write_reg invalid register {:?}", + reg + ))) + } + } + + Ok(()) + } + + fn read_segment(&self, reg: Register) -> Result { + if !reg.is_segment_register() { + return Err(PlatformError::InvalidRegister(anyhow!( + "read_segment {:?} is not a segment register", + reg + ))); + } + + match reg { + Register::CS => Ok(self.sregs.cs), + Register::DS => Ok(self.sregs.ds), + Register::ES => Ok(self.sregs.es), + Register::FS => Ok(self.sregs.fs), + Register::GS => Ok(self.sregs.gs), + Register::SS => Ok(self.sregs.ss), + r => Err(PlatformError::InvalidRegister(anyhow!( + "read_segment invalid register {:?}", + r + ))), + } + } + + fn write_segment( + &mut self, + reg: Register, + segment_register: SegmentRegister, + ) -> Result<(), PlatformError> { + if !reg.is_segment_register() { + return Err(PlatformError::InvalidRegister(anyhow!("{:?}", reg))); + } + + match reg { + Register::CS => self.sregs.cs = segment_register, + Register::DS => self.sregs.ds = segment_register, + Register::ES => self.sregs.es = segment_register, + Register::FS => self.sregs.fs = segment_register, + Register::GS => self.sregs.gs = segment_register, + Register::SS => self.sregs.ss = segment_register, + r => return Err(PlatformError::InvalidRegister(anyhow!("{:?}", r))), + } + + Ok(()) + } + + fn ip(&self) -> u64 { + self.regs.rip + } + + fn set_ip(&mut self, ip: u64) { + self.regs.rip = ip; + } + + fn efer(&self) -> u64 { + self.sregs.efer + } + + fn set_efer(&mut self, efer: u64) { + self.sregs.efer = efer + } + + fn flags(&self) -> u64 { + self.regs.rflags + } + + fn set_flags(&mut self, flags: u64) { + self.regs.rflags = flags; + } + + fn mode(&self) -> Result { + let efer = self.efer(); + let cr0 = self.read_reg(Register::CR0)?; + let mut mode = CpuMode::Real; + + if (cr0 & CR0_PE) == CR0_PE { + mode = CpuMode::Protected; + } + + if (efer & EFER_LMA) == EFER_LMA { + if mode != CpuMode::Protected { + return Err(PlatformError::InvalidState(anyhow!( + "Protection must be enabled in long mode" + ))); + } + + mode = CpuMode::Long; + } + + Ok(mode) + } +} + +pub struct Emulator<'a, T: CpuStateManager> { + platform: &'a mut dyn PlatformEmulator, +} + +// Reduce repetition, see its invocation in get_handler(). +macro_rules! gen_handler_match { + ($value: ident, $( ($module:ident, $code:ident) ),* ) => { + match $value { + $( + Code::$code => Some(Box::new($module::$code)), + )* + _ => None, + } + }; +} + +impl<'a, T: CpuStateManager> Emulator<'a, T> { + pub fn new(platform: &mut dyn PlatformEmulator) -> Emulator { + Emulator { platform } + } + + fn get_handler(code: Code) -> Option>> { + let handler: Option>> = gen_handler_match!( + code, + // CMP + (cmp, Cmp_rm32_r32), + (cmp, Cmp_rm8_r8), + (cmp, Cmp_rm32_imm8), + (cmp, Cmp_rm64_r64), + // MOV + (mov, Mov_r8_rm8), + (mov, Mov_r8_imm8), + (mov, Mov_r16_imm16), + (mov, Mov_r16_rm16), + (mov, Mov_r32_imm32), + (mov, Mov_r32_rm32), + (mov, Mov_r64_imm64), + (mov, Mov_r64_rm64), + (mov, Mov_rm8_imm8), + (mov, Mov_rm8_r8), + (mov, Mov_rm16_imm16), + (mov, Mov_rm16_r16), + (mov, Mov_rm32_imm32), + (mov, Mov_rm32_r32), + (mov, Mov_rm64_imm32), + (mov, Mov_rm64_r64), + // MOVZX + (mov, Movzx_r16_rm8), + (mov, Movzx_r32_rm8), + (mov, Movzx_r64_rm8), + (mov, Movzx_r32_rm16), + (mov, Movzx_r64_rm16), + // MOVS + (movs, Movsd_m32_m32), + // OR + (or, Or_rm8_r8) + ); + + handler + } + + fn emulate_insn_stream( + &mut self, + cpu_id: usize, + insn_stream: &[u8], + num_insn: Option, + ) -> EmulationResult { + let mut state = self + .platform + .cpu_state(cpu_id) + .map_err(EmulationError::PlatformEmulationError)?; + let mut decoder = Decoder::new(64, insn_stream, DecoderOptions::NONE); + let mut insn = Instruction::default(); + let mut num_insn_emulated: usize = 0; + let mut fetched_insn_stream: [u8; 16] = [0; 16]; + let mut last_decoded_ip: u64 = state.ip(); + let mut stop_emulation: bool = false; + + decoder.set_ip(state.ip()); + + while decoder.can_decode() && !stop_emulation { + decoder.decode_out(&mut insn); + + if decoder.last_error() == DecoderError::NoMoreBytes { + // The decoder is missing some bytes to decode the current + // instruction, for example because the instruction stream + // crosses a page boundary. + // We fetch 16 more bytes from the instruction segment, + // decode and emulate the failing instruction and terminate + // the emulation loop. + debug!( + "Fetching {} bytes from {:#x}", + fetched_insn_stream.len(), + last_decoded_ip + ); + + // fetched_insn_stream is 16 bytes long, enough to contain + // any complete x86 instruction. + self.platform + .fetch(last_decoded_ip, &mut fetched_insn_stream) + .map_err(EmulationError::PlatformEmulationError)?; + + debug!("Fetched {:x?}", fetched_insn_stream); + + // Once we have the new stream, we must create a new decoder + // and emulate one last instruction from the last decoded IP. + decoder = Decoder::new(64, &fetched_insn_stream, DecoderOptions::NONE); + decoder.decode_out(&mut insn); + if decoder.last_error() != DecoderError::None { + return Err(EmulationError::InstructionFetchingError(anyhow!( + "{:#x?}", + insn_format!(insn) + ))); + } + + stop_emulation = true; + } + + // Emulate the decoded instruction + Emulator::get_handler(insn.code()) + .ok_or_else(|| { + EmulationError::UnsupportedInstruction(anyhow!( + "{:#x?} {:?} {:?}", + insn_format!(insn), + insn.mnemonic(), + insn.code() + )) + })? + .emulate(&insn, &mut state, self.platform) + .context(anyhow!("Failed to emulate {:#x?}", insn_format!(insn)))?; + + last_decoded_ip = decoder.ip(); + num_insn_emulated += 1; + + if let Some(num_insn) = num_insn { + if num_insn_emulated >= num_insn { + // Exit the decoding loop, do not decode the next instruction. + stop_emulation = true; + } + } + } + + state.set_ip(decoder.ip()); + Ok(state) + } + + /// Emulate all instructions from the instructions stream. + pub fn emulate(&mut self, cpu_id: usize, insn_stream: &[u8]) -> EmulationResult { + self.emulate_insn_stream(cpu_id, insn_stream, None) + } + + /// Only emulate the first instruction from the stream. + /// + /// This is useful for cases where we get readahead instruction stream + /// but implicitly must only emulate the first instruction, and then return + /// to the guest. + pub fn emulate_first_insn( + &mut self, + cpu_id: usize, + insn_stream: &[u8], + ) -> EmulationResult { + self.emulate_insn_stream(cpu_id, insn_stream, Some(1)) + } +} + +#[cfg(test)] +mod mock_vmm { + #![allow(unused_mut)] + + use super::*; + use crate::arch::emulator::{EmulationError, PlatformEmulator}; + use crate::arch::x86::emulator::{Emulator, EmulatorCpuState as CpuState}; + use crate::arch::x86::gdt::{gdt_entry, segment_from_gdt}; + use crate::arch::x86::Exception; + use std::sync::{Arc, Mutex}; + + #[derive(Debug, Clone)] + pub struct MockVmm { + memory: Vec, + state: Arc>, + } + + pub type MockResult = Result<(), EmulationError>; + + impl MockVmm { + pub fn new(ip: u64, regs: Vec<(Register, u64)>, memory: Option<(u64, &[u8])>) -> MockVmm { + let _ = env_logger::try_init(); + let cs_reg = segment_from_gdt(gdt_entry(0xc09b, 0, 0xffffffff), 1); + let ds_reg = segment_from_gdt(gdt_entry(0xc093, 0, 0xffffffff), 2); + let es_reg = segment_from_gdt(gdt_entry(0xc093, 0, 0xffffffff), 3); + let mut initial_state = CpuState::default(); + initial_state.set_ip(ip); + initial_state.write_segment(Register::CS, cs_reg).unwrap(); + initial_state.write_segment(Register::DS, ds_reg).unwrap(); + initial_state.write_segment(Register::ES, es_reg).unwrap(); + for (reg, value) in regs { + initial_state.write_reg(reg, value).unwrap(); + } + + let mut vmm = MockVmm { + memory: vec![0; 8192], + state: Arc::new(Mutex::new(initial_state)), + }; + + if let Some(mem) = memory { + vmm.write_memory(mem.0, mem.1).unwrap(); + } + + vmm + } + + pub fn emulate_insn( + &mut self, + cpu_id: usize, + insn: &[u8], + num_insn: Option, + ) -> MockResult { + let ip = self.cpu_state(cpu_id).unwrap().ip(); + let mut emulator = Emulator::new(self); + + let new_state = emulator.emulate_insn_stream(cpu_id, insn, num_insn)?; + if num_insn.is_none() { + assert_eq!(ip + insn.len() as u64, new_state.ip()); + } + + self.set_cpu_state(cpu_id, new_state).unwrap(); + + Ok(()) + } + + pub fn emulate_first_insn(&mut self, cpu_id: usize, insn: &[u8]) -> MockResult { + self.emulate_insn(cpu_id, insn, Some(1)) + } + } + + impl PlatformEmulator for MockVmm { + type CpuState = CpuState; + + fn read_memory(&self, gva: u64, data: &mut [u8]) -> Result<(), PlatformError> { + debug!( + "Memory read {} bytes from [{:#x} -> {:#x}]", + data.len(), + gva, + gva + data.len() as u64 - 1 + ); + data.copy_from_slice(&self.memory[gva as usize..gva as usize + data.len()]); + Ok(()) + } + + fn write_memory(&mut self, gva: u64, data: &[u8]) -> Result<(), PlatformError> { + debug!( + "Memory write {} bytes at [{:#x} -> {:#x}]", + data.len(), + gva, + gva + data.len() as u64 - 1 + ); + self.memory[gva as usize..gva as usize + data.len()].copy_from_slice(data); + + Ok(()) + } + + fn cpu_state(&self, _cpu_id: usize) -> Result { + Ok(self.state.lock().unwrap().clone()) + } + + fn set_cpu_state( + &self, + _cpu_id: usize, + state: Self::CpuState, + ) -> Result<(), PlatformError> { + *self.state.lock().unwrap() = state; + Ok(()) + } + + fn gva_to_gpa(&self, gva: u64) -> Result { + Ok(gva) + } + + fn fetch(&self, ip: u64, instruction_bytes: &mut [u8]) -> Result<(), PlatformError> { + let rip = self + .state + .lock() + .unwrap() + .linearize(Register::CS, ip, false)?; + self.read_memory(rip, instruction_bytes) + } + } +} + +#[cfg(test)] +mod tests { + #![allow(unused_mut)] + use super::*; + use crate::arch::x86::emulator::mock_vmm::*; + + #[test] + // Emulate truncated instruction stream, which should cause a fetch. + // + // mov rax, 0x1000 + // Test with a first instruction truncated. + fn test_fetch_first_instruction() { + let ip: u64 = 0x1000; + let cpu_id = 0; + let memory = [ + // Code at IP + 0x48, 0xc7, 0xc0, 0x00, 0x10, 0x00, 0x00, // mov rax, 0x1000 + 0x48, 0x8b, 0x58, 0x10, // mov rbx, qword ptr [rax+10h] + // Padding + 0x00, 0x00, 0x00, 0x00, 0x00, // Padding is all zeroes + // Data at IP + 0x10 (0x1234567812345678 in LE) + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, + ]; + let insn = [ + // First instruction is truncated + 0x48, 0xc7, 0xc0, 0x00, // mov rax, 0x1000 -- Missing bytes: 0x00, 0x10, 0x00, 0x00, + ]; + + let mut vmm = MockVmm::new(ip, vec![], Some((ip, &memory))); + assert!(vmm.emulate_insn(cpu_id, &insn, Some(2)).is_ok()); + + let rax: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::RAX) + .unwrap(); + assert_eq!(rax, ip); + } + + #[test] + // Emulate truncated instruction stream, which should cause a fetch. + // + // mov rax, 0x1000 + // mov rbx, qword ptr [rax+10h] + // Test with a 2nd instruction truncated. + fn test_fetch_second_instruction() { + let target_rax: u64 = 0x1234567812345678; + let ip: u64 = 0x1000; + let cpu_id = 0; + let memory = [ + // Code at IP + 0x48, 0xc7, 0xc0, 0x00, 0x10, 0x00, 0x00, // mov rax, 0x1000 + 0x48, 0x8b, 0x58, 0x10, // mov rbx, qword ptr [rax+10h] + // Padding + 0x00, 0x00, 0x00, 0x00, 0x00, // Padding is all zeroes + // Data at IP + 0x10 (0x1234567812345678 in LE) + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, + ]; + let insn = [ + 0x48, 0xc7, 0xc0, 0x00, 0x10, 0x00, 0x00, // mov rax, 0x1000 + 0x48, 0x8b, // Truncated mov rbx, qword ptr [rax+10h] -- missing [0x58, 0x10] + ]; + + let mut vmm = MockVmm::new(ip, vec![], Some((ip, &memory))); + assert!(vmm.emulate_insn(cpu_id, &insn, Some(2)).is_ok()); + + let rbx: u64 = vmm + .cpu_state(cpu_id) + .unwrap() + .read_reg(Register::RBX) + .unwrap(); + assert_eq!(rbx, target_rax); + } + + #[test] + // Emulate truncated instruction stream, which should cause a fetch. + // + // mov rax, 0x1000 + // Test with a first instruction truncated and a bad fetched instruction. + // Verify that the instruction emulation returns an error. + fn test_fetch_bad_insn() { + let ip: u64 = 0x1000; + let cpu_id = 0; + let memory = [ + // Code at IP + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, + ]; + let insn = [ + // First instruction is truncated + 0x48, 0xc7, 0xc0, 0x00, // mov rax, 0x1000 -- Missing bytes: 0x00, 0x10, 0x00, 0x00, + ]; + + let mut vmm = MockVmm::new(ip, vec![], Some((ip, &memory))); + assert!(vmm.emulate_first_insn(cpu_id, &insn).is_err()); + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/gdt.rs b/vendor/cube-hypervisor/hypervisor/src/arch/x86/gdt.rs new file mode 100644 index 0000000..88eeb35 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/x86/gdt.rs @@ -0,0 +1,141 @@ +// Copyright © 2020, Oracle and/or its affiliates. +// +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +// For GDT details see arch/x86/include/asm/segment.h +use crate::arch::x86::SegmentRegister; + +/// Constructor for a conventional segment GDT (or LDT) entry. Derived from the kernel's segment.h. +pub fn gdt_entry(flags: u16, base: u32, limit: u32) -> u64 { + (((base as u64) & 0xff000000u64) << (56 - 24)) + | (((flags as u64) & 0x0000f0ffu64) << 40) + | (((limit as u64) & 0x000f0000u64) << (48 - 16)) + | (((base as u64) & 0x00ffffffu64) << 16) + | ((limit as u64) & 0x0000ffffu64) +} + +fn get_base(entry: u64) -> u64 { + (((entry) & 0xFF00000000000000) >> 32) + | (((entry) & 0x000000FF00000000) >> 16) + | (((entry) & 0x00000000FFFF0000) >> 16) +} + +// Extract the segment limit from the GDT segment descriptor. +// +// In a segment descriptor, the limit field is 20 bits, so it can directly describe +// a range from 0 to 0xFFFFF (1MByte). When G flag is set (4-KByte page granularity) it +// scales the value in the limit field by a factor of 2^12 (4Kbytes), making the effective +// limit range from 0xFFF (4 KBytes) to 0xFFFF_FFFF (4 GBytes). +// +// However, the limit field in the VMCS definition is a 32 bit field, and the limit value is not +// automatically scaled using the G flag. This means that for a desired range of 4GB for a +// given segment, its limit must be specified as 0xFFFF_FFFF. Therefore the method of obtaining +// the limit from the GDT entry is not sufficient, since it only provides 20 bits when 32 bits +// are necessary. Fortunately, we can check if the G flag is set when extracting the limit since +// the full GDT entry is passed as an argument, and perform the scaling of the limit value to +// return the full 32 bit value. +// +// The scaling mentioned above is required when using PVH boot, since the guest boots in protected +// (32-bit) mode and must be able to access the entire 32-bit address space. It does not cause issues +// for the case of direct boot to 64-bit (long) mode, since in 64-bit mode the processor does not +// perform runtime limit checking on code or data segments. +fn get_limit(entry: u64) -> u32 { + let limit: u32 = + ((((entry) & 0x000F000000000000) >> 32) | ((entry) & 0x000000000000FFFF)) as u32; + + // Perform manual limit scaling if G flag is set + match get_g(entry) { + 0 => limit, + _ => (limit << 12) | 0xFFF, // G flag is either 0 or 1 + } +} + +fn get_g(entry: u64) -> u8 { + ((entry & 0x0080000000000000) >> 55) as u8 +} + +fn get_db(entry: u64) -> u8 { + ((entry & 0x0040000000000000) >> 54) as u8 +} + +fn get_l(entry: u64) -> u8 { + ((entry & 0x0020000000000000) >> 53) as u8 +} + +fn get_avl(entry: u64) -> u8 { + ((entry & 0x0010000000000000) >> 52) as u8 +} + +fn get_p(entry: u64) -> u8 { + ((entry & 0x0000800000000000) >> 47) as u8 +} + +fn get_dpl(entry: u64) -> u8 { + ((entry & 0x0000600000000000) >> 45) as u8 +} + +fn get_s(entry: u64) -> u8 { + ((entry & 0x0000100000000000) >> 44) as u8 +} + +fn get_type(entry: u64) -> u8 { + ((entry & 0x00000F0000000000) >> 40) as u8 +} + +/// Automatically build the struct for SET_SREGS from the kernel bit fields. +/// +/// # Arguments +/// +/// * `entry` - The gdt entry. +/// * `table_index` - Index of the entry in the gdt table. +pub fn segment_from_gdt(entry: u64, table_index: u8) -> SegmentRegister { + SegmentRegister { + base: get_base(entry), + limit: get_limit(entry), + selector: (table_index * 8) as u16, + type_: get_type(entry), + present: get_p(entry), + dpl: get_dpl(entry), + db: get_db(entry), + s: get_s(entry), + l: get_l(entry), + g: get_g(entry), + avl: get_avl(entry), + unusable: match get_p(entry) { + 0 => 1, + _ => 0, + }, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn field_parse() { + let gdt = gdt_entry(0xA09B, 0x100000, 0xfffff); + let seg = segment_from_gdt(gdt, 0); + // 0xA09B + // 'A' + assert_eq!(0x1, seg.g); + assert_eq!(0x0, seg.db); + assert_eq!(0x1, seg.l); + assert_eq!(0x0, seg.avl); + // '9' + assert_eq!(0x1, seg.present); + assert_eq!(0x0, seg.dpl); + assert_eq!(0x1, seg.s); + // 'B' + assert_eq!(0xB, seg.type_); + // base and limit + assert_eq!(0x100000, seg.base); + assert_eq!(0xffffffff, seg.limit); + assert_eq!(0x0, seg.unusable); + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/mod.rs b/vendor/cube-hypervisor/hypervisor/src/arch/x86/mod.rs new file mode 100644 index 0000000..c5e5899 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/x86/mod.rs @@ -0,0 +1,403 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// + +pub mod emulator; +pub mod gdt; +#[allow(non_camel_case_types)] +#[allow(non_snake_case)] +#[allow(non_upper_case_globals)] +pub mod msr_index; + +use serde::de::Error as SerdeError; +use serde::{Deserialize, Deserializer, Serializer}; + +// MTRR constants +pub const MTRR_ENABLE: u64 = 0x800; // IA32_MTRR_DEF_TYPE MSR: E (MTRRs enabled) flag, bit 11 +pub const MTRR_MEM_TYPE_WB: u64 = 0x6; + +// IOAPIC pins +pub const NUM_IOAPIC_PINS: usize = 24; + +// X86 Exceptions +#[allow(clippy::upper_case_acronyms)] +#[derive(Clone, Debug)] +pub enum Exception { + DE = 0, // Divide Error + DB = 1, // Debug Exception + BP = 3, // Breakpoint + OF = 4, // Overflow + BR = 5, // BOUND Range Exceeded + UD = 6, // Invalid/Undefined Opcode + NM = 7, // No Math Coprocessor + DF = 8, // Double Fault + TS = 10, // Invalid TSS + NP = 11, // Segment Not Present + SS = 12, // Stack Segment Fault + GP = 13, // General Protection + PF = 14, // Page Fault + MF = 16, // Math Fault + AC = 17, // Alignment Check + MC = 18, // Machine Check + XM = 19, // SIMD Floating-Point Exception + VE = 20, // Virtualization Exception + CP = 21, // Control Protection Exception +} + +pub mod regs; + +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))] +pub struct SegmentRegister { + pub base: u64, + pub limit: u32, + pub selector: u16, + pub type_: u8, + pub present: u8, + pub dpl: u8, + pub db: u8, + pub s: u8, + pub l: u8, + pub g: u8, + pub avl: u8, + pub unusable: u8, +} + +impl SegmentRegister { + pub fn segment_type(&self) -> u8 { + self.type_ + } + pub fn set_segment_type(&mut self, val: u8) { + self.type_ = val; + } + + pub fn dpl(&self) -> u8 { + self.dpl + } + + pub fn set_dpl(&mut self, val: u8) { + self.dpl = val; + } + + pub fn present(&self) -> u8 { + self.present + } + + pub fn set_present(&mut self, val: u8) { + self.present = val; + } + + pub fn long(&self) -> u8 { + self.l + } + + pub fn set_long(&mut self, val: u8) { + self.l = val; + } + + pub fn avl(&self) -> u8 { + self.avl + } + + pub fn set_avl(&mut self, val: u8) { + self.avl = val; + } + + pub fn desc_type(&self) -> u8 { + self.s + } + + pub fn set_desc_type(&mut self, val: u8) { + self.s = val; + } + + pub fn granularity(&self) -> u8 { + self.g + } + + pub fn set_granularity(&mut self, val: u8) { + self.g = val; + } + + pub fn db(&self) -> u8 { + self.db + } + + pub fn set_db(&mut self, val: u8) { + self.db = val; + } +} + +// Code segment +pub const CODE_SEGMENT_TYPE: u8 = 0x8; + +// Read/Write or Read/Exec segment +pub const RWRX_SEGMENT_TYPE: u8 = 0x2; + +// Expand down segment +pub const EXPAND_DOWN_SEGMENT_TYPE: u8 = 0x4; + +pub fn segment_type_code(t: u8) -> bool { + t & CODE_SEGMENT_TYPE != 0 +} + +pub fn segment_type_ro(t: u8) -> bool { + t & !RWRX_SEGMENT_TYPE == 0 +} + +pub fn segment_type_expand_down(t: u8) -> bool { + !segment_type_code(t) && (t & EXPAND_DOWN_SEGMENT_TYPE != 0) +} +#[macro_export] +macro_rules! msr { + ($msr:expr) => { + MsrEntry { + index: $msr, + data: 0x0, + } + }; +} +#[macro_export] +macro_rules! msr_data { + ($msr:expr, $data:expr) => { + MsrEntry { + index: $msr, + data: $data, + } + }; +} + +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))] +pub struct StandardRegisters { + pub rax: u64, + pub rbx: u64, + pub rcx: u64, + pub rdx: u64, + pub rsi: u64, + pub rdi: u64, + pub rsp: u64, + pub rbp: u64, + pub r8: u64, + pub r9: u64, + pub r10: u64, + pub r11: u64, + pub r12: u64, + pub r13: u64, + pub r14: u64, + pub r15: u64, + pub rip: u64, + pub rflags: u64, +} + +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))] +pub struct DescriptorTable { + pub base: u64, + pub limit: u16, +} + +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))] +pub struct SpecialRegisters { + pub cs: SegmentRegister, + pub ds: SegmentRegister, + pub es: SegmentRegister, + pub fs: SegmentRegister, + pub gs: SegmentRegister, + pub ss: SegmentRegister, + pub tr: SegmentRegister, + pub ldt: SegmentRegister, + pub gdt: DescriptorTable, + pub idt: DescriptorTable, + pub cr0: u64, + pub cr2: u64, + pub cr3: u64, + pub cr4: u64, + pub cr8: u64, + pub efer: u64, + pub apic_base: u64, + pub interrupt_bitmap: [u64; 4usize], +} + +/// Serializes number to hex +pub fn serialize_to_hex_str(number: &N, serializer: S) -> Result +where + S: Serializer, + N: std::fmt::LowerHex, +{ + serializer.serialize_str(format!("{:#x}", number).as_str()) +} + +macro_rules! deserialize_from_str { + ($name:ident, $type:tt) => { + /// Deserializes number from string. + /// Number can be in binary, hex or dec formats. + pub fn $name<'de, D>(deserializer: D) -> Result<$type, D::Error> + where + D: Deserializer<'de>, + { + let number_str = String::deserialize(deserializer)?; + let deserialized_number = if let Some(s) = number_str.strip_prefix("0b") { + $type::from_str_radix(s, 2) + } else if let Some(s) = number_str.strip_prefix("0x") { + $type::from_str_radix(s, 16) + } else { + return Err(D::Error::custom(format!( + "No supported number system prefix found in value [{}]. Make sure to prefix \ + the number with '0x' for hexadecimal numbers or '0b' for binary numbers.", + number_str, + ))); + } + .map_err(|err| { + D::Error::custom(format!( + "Failed to parse string [{}] as a number for CPU template - {:?}", + number_str, err + )) + })?; + Ok(deserialized_number) + } + }; +} + +deserialize_from_str!(deserialize_from_str_u32, u32); +deserialize_from_str!(deserialize_from_str_u64, u64); + +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)] +pub struct CpuIdEntry { + pub function: u32, + pub index: u32, + pub flags: u32, + pub eax: u32, + pub ebx: u32, + pub ecx: u32, + pub edx: u32, +} + +// To keep compatibility of old snaps, use a new structure. +// Old snaps member are u32, and we use string here, in order +// to get them from json file. +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)] +pub struct CpuIdCustomEntry { + #[serde( + deserialize_with = "deserialize_from_str_u32", + serialize_with = "serialize_to_hex_str" + )] + pub function: u32, + pub index: u32, + pub flags: u32, + pub eax: u32, + pub ebx: u32, + pub ecx: u32, + pub edx: u32, +} + +impl From for CpuIdEntry { + fn from(s: CpuIdCustomEntry) -> Self { + Self { + function: s.function, + index: s.index, + flags: s.flags, + eax: s.eax, + ebx: s.ebx, + ecx: s.ecx, + edx: s.edx, + } + } +} + +pub const CPUID_FLAG_VALID_INDEX: u32 = 1; + +#[derive(Default, Debug, Clone, serde::Deserialize, serde::Serialize)] +pub struct FpuState { + pub fpr: [[u8; 16usize]; 8usize], + pub fcw: u16, + pub fsw: u16, + pub ftwx: u8, + pub last_opcode: u16, + pub last_ip: u64, + pub last_dp: u64, + pub xmm: [[u8; 16usize]; 16usize], + pub mxcsr: u32, +} + +#[serde_with::serde_as] +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct LapicState { + #[serde_as(as = "[_; 1024usize]")] + pub(crate) regs: [::std::os::raw::c_char; 1024usize], +} + +impl Default for LapicState { + fn default() -> Self { + // SAFETY: this is plain old data structure + unsafe { ::std::mem::zeroed() } + } +} + +impl LapicState { + pub fn get_klapic_reg(&self, reg_offset: usize) -> u32 { + use byteorder::{LittleEndian, ReadBytesExt}; + use std::io::Cursor; + use std::mem; + + let sliceu8 = unsafe { + // This array is only accessed as parts of a u32 word, so interpret it as a u8 array. + // Cursors are only readable on arrays of u8, not i8(c_char). + mem::transmute::<&[i8], &[u8]>(&self.regs[reg_offset..]) + }; + + let mut reader = Cursor::new(sliceu8); + // Following call can't fail if the offsets defined above are correct. + reader + .read_u32::() + .expect("Failed to read klapic register") + } + + pub fn set_klapic_reg(&mut self, reg_offset: usize, value: u32) { + use byteorder::{LittleEndian, WriteBytesExt}; + use std::io::Cursor; + use std::mem; + + let sliceu8 = unsafe { + // This array is only accessed as parts of a u32 word, so interpret it as a u8 array. + // Cursors are only readable on arrays of u8, not i8(c_char). + mem::transmute::<&mut [i8], &mut [u8]>(&mut self.regs[reg_offset..]) + }; + + let mut writer = Cursor::new(sliceu8); + // Following call can't fail if the offsets defined above are correct. + writer + .write_u32::(value) + .expect("Failed to write klapic register") + } +} + +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)] +pub struct MsrEntry { + pub index: u32, + pub data: u64, +} + +#[serde_with::serde_as] +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct XsaveState { + #[serde_as(as = "[_; 1024usize]")] + pub region: [u32; 1024usize], +} + +impl Default for XsaveState { + fn default() -> Self { + // SAFETY: this is plain old data structure + unsafe { ::std::mem::zeroed() } + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/msr_index.rs b/vendor/cube-hypervisor/hypervisor/src/arch/x86/msr_index.rs new file mode 100644 index 0000000..81a7b06 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/x86/msr_index.rs @@ -0,0 +1,543 @@ +// Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +/* + * automatically generated by rust-bindgen + * From upstream linux msr-index.h at commit: + * 806276b7f07a39a1cc3f38bb1ef5c573d4594a38 + */ + +pub const MSR_EFER: ::std::os::raw::c_uint = 0xc0000080; +pub const MSR_STAR: ::std::os::raw::c_uint = 0xc0000081; +pub const MSR_LSTAR: ::std::os::raw::c_uint = 0xc0000082; +pub const MSR_CSTAR: ::std::os::raw::c_uint = 0xc0000083; +pub const MSR_SYSCALL_MASK: ::std::os::raw::c_uint = 0xc0000084; +pub const MSR_FS_BASE: ::std::os::raw::c_uint = 0xc0000100; +pub const MSR_GS_BASE: ::std::os::raw::c_uint = 0xc0000101; +pub const MSR_KERNEL_GS_BASE: ::std::os::raw::c_uint = 0xc0000102; +pub const MSR_TSC_AUX: ::std::os::raw::c_uint = 0xc0000103; +pub const _EFER_SCE: ::std::os::raw::c_uint = 0x00000000; +pub const _EFER_LME: ::std::os::raw::c_uint = 0x00000008; +pub const _EFER_LMA: ::std::os::raw::c_uint = 0x0000000a; +pub const _EFER_NX: ::std::os::raw::c_uint = 0x0000000b; +pub const _EFER_SVME: ::std::os::raw::c_uint = 0x0000000c; +pub const _EFER_LMSLE: ::std::os::raw::c_uint = 0x0000000d; +pub const _EFER_FFXSR: ::std::os::raw::c_uint = 0x0000000e; +pub const EFER_SCE: ::std::os::raw::c_uint = 0x00000001; +pub const EFER_LME: ::std::os::raw::c_uint = 0x00000100; +pub const EFER_LMA: ::std::os::raw::c_uint = 0x00000400; +pub const EFER_NX: ::std::os::raw::c_uint = 0x00000800; +pub const EFER_SVME: ::std::os::raw::c_uint = 0x00001000; +pub const EFER_LMSLE: ::std::os::raw::c_uint = 0x00002000; +pub const EFER_FFXSR: ::std::os::raw::c_uint = 0x00004000; +pub const MSR_PPIN_CTL: ::std::os::raw::c_uint = 0x0000004e; +pub const MSR_PPIN: ::std::os::raw::c_uint = 0x0000004f; +pub const MSR_IA32_PERFCTR0: ::std::os::raw::c_uint = 0x000000c1; +pub const MSR_IA32_PERFCTR1: ::std::os::raw::c_uint = 0x000000c2; +pub const MSR_FSB_FREQ: ::std::os::raw::c_uint = 0x000000cd; +pub const MSR_PLATFORM_INFO: ::std::os::raw::c_uint = 0x000000ce; +pub const MSR_PKG_CST_CONFIG_CONTROL: ::std::os::raw::c_uint = 0x000000e2; +pub const NHM_C3_AUTO_DEMOTE: ::std::os::raw::c_uint = 0x02000000; +pub const NHM_C1_AUTO_DEMOTE: ::std::os::raw::c_uint = 0x04000000; +pub const ATM_LNC_C6_AUTO_DEMOTE: ::std::os::raw::c_uint = 0x02000000; +pub const SNB_C1_AUTO_UNDEMOTE: ::std::os::raw::c_uint = 0x08000000; +pub const SNB_C3_AUTO_UNDEMOTE: ::std::os::raw::c_uint = 0x10000000; +pub const MSR_MTRRcap: ::std::os::raw::c_uint = 0x000000fe; +pub const MSR_IA32_BBL_CR_CTL: ::std::os::raw::c_uint = 0x00000119; +pub const MSR_IA32_BBL_CR_CTL3: ::std::os::raw::c_uint = 0x0000011e; +pub const MSR_IA32_SYSENTER_CS: ::std::os::raw::c_uint = 0x00000174; +pub const MSR_IA32_SYSENTER_ESP: ::std::os::raw::c_uint = 0x00000175; +pub const MSR_IA32_SYSENTER_EIP: ::std::os::raw::c_uint = 0x00000176; +pub const MSR_IA32_MCG_CAP: ::std::os::raw::c_uint = 0x00000179; +pub const MSR_IA32_MCG_STATUS: ::std::os::raw::c_uint = 0x0000017a; +pub const MSR_IA32_MCG_CTL: ::std::os::raw::c_uint = 0x0000017b; +pub const MSR_IA32_MCG_EXT_CTL: ::std::os::raw::c_uint = 0x000004d0; +pub const MSR_OFFCORE_RSP_0: ::std::os::raw::c_uint = 0x000001a6; +pub const MSR_OFFCORE_RSP_1: ::std::os::raw::c_uint = 0x000001a7; +pub const MSR_TURBO_RATIO_LIMIT: ::std::os::raw::c_uint = 0x000001ad; +pub const MSR_TURBO_RATIO_LIMIT1: ::std::os::raw::c_uint = 0x000001ae; +pub const MSR_TURBO_RATIO_LIMIT2: ::std::os::raw::c_uint = 0x000001af; +pub const MSR_LBR_SELECT: ::std::os::raw::c_uint = 0x000001c8; +pub const MSR_LBR_TOS: ::std::os::raw::c_uint = 0x000001c9; +pub const MSR_LBR_NHM_FROM: ::std::os::raw::c_uint = 0x00000680; +pub const MSR_LBR_NHM_TO: ::std::os::raw::c_uint = 0x000006c0; +pub const MSR_LBR_CORE_FROM: ::std::os::raw::c_uint = 0x00000040; +pub const MSR_LBR_CORE_TO: ::std::os::raw::c_uint = 0x00000060; +pub const MSR_LBR_INFO_0: ::std::os::raw::c_uint = 0x00000dc0; +pub const LBR_INFO_CYCLES: ::std::os::raw::c_uint = 0x0000ffff; +pub const MSR_IA32_PEBS_ENABLE: ::std::os::raw::c_uint = 0x000003f1; +pub const MSR_IA32_DS_AREA: ::std::os::raw::c_uint = 0x00000600; +pub const MSR_IA32_PERF_CAPABILITIES: ::std::os::raw::c_uint = 0x00000345; +pub const MSR_PEBS_LD_LAT_THRESHOLD: ::std::os::raw::c_uint = 0x000003f6; +pub const MSR_IA32_RTIT_CTL: ::std::os::raw::c_uint = 0x00000570; +pub const MSR_IA32_RTIT_STATUS: ::std::os::raw::c_uint = 0x00000571; +pub const MSR_IA32_RTIT_ADDR0_A: ::std::os::raw::c_uint = 0x00000580; +pub const MSR_IA32_RTIT_ADDR0_B: ::std::os::raw::c_uint = 0x00000581; +pub const MSR_IA32_RTIT_ADDR1_A: ::std::os::raw::c_uint = 0x00000582; +pub const MSR_IA32_RTIT_ADDR1_B: ::std::os::raw::c_uint = 0x00000583; +pub const MSR_IA32_RTIT_ADDR2_A: ::std::os::raw::c_uint = 0x00000584; +pub const MSR_IA32_RTIT_ADDR2_B: ::std::os::raw::c_uint = 0x00000585; +pub const MSR_IA32_RTIT_ADDR3_A: ::std::os::raw::c_uint = 0x00000586; +pub const MSR_IA32_RTIT_ADDR3_B: ::std::os::raw::c_uint = 0x00000587; +pub const MSR_IA32_RTIT_CR3_MATCH: ::std::os::raw::c_uint = 0x00000572; +pub const MSR_IA32_RTIT_OUTPUT_BASE: ::std::os::raw::c_uint = 0x00000560; +pub const MSR_IA32_RTIT_OUTPUT_MASK: ::std::os::raw::c_uint = 0x00000561; +pub const MSR_MTRRfix64K_00000: ::std::os::raw::c_uint = 0x00000250; +pub const MSR_MTRRfix16K_80000: ::std::os::raw::c_uint = 0x00000258; +pub const MSR_MTRRfix16K_A0000: ::std::os::raw::c_uint = 0x00000259; +pub const MSR_MTRRfix4K_C0000: ::std::os::raw::c_uint = 0x00000268; +pub const MSR_MTRRfix4K_C8000: ::std::os::raw::c_uint = 0x00000269; +pub const MSR_MTRRfix4K_D0000: ::std::os::raw::c_uint = 0x0000026a; +pub const MSR_MTRRfix4K_D8000: ::std::os::raw::c_uint = 0x0000026b; +pub const MSR_MTRRfix4K_E0000: ::std::os::raw::c_uint = 0x0000026c; +pub const MSR_MTRRfix4K_E8000: ::std::os::raw::c_uint = 0x0000026d; +pub const MSR_MTRRfix4K_F0000: ::std::os::raw::c_uint = 0x0000026e; +pub const MSR_MTRRfix4K_F8000: ::std::os::raw::c_uint = 0x0000026f; +pub const MSR_MTRRdefType: ::std::os::raw::c_uint = 0x000002ff; +pub const MSR_IA32_CR_PAT: ::std::os::raw::c_uint = 0x00000277; +pub const MSR_IA32_DEBUGCTLMSR: ::std::os::raw::c_uint = 0x000001d9; +pub const MSR_IA32_LASTBRANCHFROMIP: ::std::os::raw::c_uint = 0x000001db; +pub const MSR_IA32_LASTBRANCHTOIP: ::std::os::raw::c_uint = 0x000001dc; +pub const MSR_IA32_LASTINTFROMIP: ::std::os::raw::c_uint = 0x000001dd; +pub const MSR_IA32_LASTINTTOIP: ::std::os::raw::c_uint = 0x000001de; +pub const DEBUGCTLMSR_LBR: ::std::os::raw::c_uint = 0x00000001; +pub const DEBUGCTLMSR_BTF: ::std::os::raw::c_uint = 0x00000002; +pub const DEBUGCTLMSR_TR: ::std::os::raw::c_uint = 0x00000040; +pub const DEBUGCTLMSR_BTS: ::std::os::raw::c_uint = 0x00000080; +pub const DEBUGCTLMSR_BTINT: ::std::os::raw::c_uint = 0x00000100; +pub const DEBUGCTLMSR_BTS_OFF_OS: ::std::os::raw::c_uint = 0x00000200; +pub const DEBUGCTLMSR_BTS_OFF_USR: ::std::os::raw::c_uint = 0x00000400; +pub const DEBUGCTLMSR_FREEZE_LBRS_ON_PMI: ::std::os::raw::c_uint = 0x00000800; +pub const MSR_PEBS_FRONTEND: ::std::os::raw::c_uint = 0x000003f7; +pub const MSR_IA32_POWER_CTL: ::std::os::raw::c_uint = 0x000001fc; +pub const MSR_IA32_MC0_CTL: ::std::os::raw::c_uint = 0x00000400; +pub const MSR_IA32_MC0_STATUS: ::std::os::raw::c_uint = 0x00000401; +pub const MSR_IA32_MC0_ADDR: ::std::os::raw::c_uint = 0x00000402; +pub const MSR_IA32_MC0_MISC: ::std::os::raw::c_uint = 0x00000403; +pub const MSR_PKG_C3_RESIDENCY: ::std::os::raw::c_uint = 0x000003f8; +pub const MSR_PKG_C6_RESIDENCY: ::std::os::raw::c_uint = 0x000003f9; +pub const MSR_ATOM_PKG_C6_RESIDENCY: ::std::os::raw::c_uint = 0x000003fa; +pub const MSR_PKG_C7_RESIDENCY: ::std::os::raw::c_uint = 0x000003fa; +pub const MSR_CORE_C3_RESIDENCY: ::std::os::raw::c_uint = 0x000003fc; +pub const MSR_CORE_C6_RESIDENCY: ::std::os::raw::c_uint = 0x000003fd; +pub const MSR_CORE_C7_RESIDENCY: ::std::os::raw::c_uint = 0x000003fe; +pub const MSR_KNL_CORE_C6_RESIDENCY: ::std::os::raw::c_uint = 0x000003ff; +pub const MSR_PKG_C2_RESIDENCY: ::std::os::raw::c_uint = 0x0000060d; +pub const MSR_PKG_C8_RESIDENCY: ::std::os::raw::c_uint = 0x00000630; +pub const MSR_PKG_C9_RESIDENCY: ::std::os::raw::c_uint = 0x00000631; +pub const MSR_PKG_C10_RESIDENCY: ::std::os::raw::c_uint = 0x00000632; +pub const MSR_PKGC3_IRTL: ::std::os::raw::c_uint = 0x0000060a; +pub const MSR_PKGC6_IRTL: ::std::os::raw::c_uint = 0x0000060b; +pub const MSR_PKGC7_IRTL: ::std::os::raw::c_uint = 0x0000060c; +pub const MSR_PKGC8_IRTL: ::std::os::raw::c_uint = 0x00000633; +pub const MSR_PKGC9_IRTL: ::std::os::raw::c_uint = 0x00000634; +pub const MSR_PKGC10_IRTL: ::std::os::raw::c_uint = 0x00000635; +pub const MSR_RAPL_POWER_UNIT: ::std::os::raw::c_uint = 0x00000606; +pub const MSR_PKG_POWER_LIMIT: ::std::os::raw::c_uint = 0x00000610; +pub const MSR_PKG_ENERGY_STATUS: ::std::os::raw::c_uint = 0x00000611; +pub const MSR_PKG_PERF_STATUS: ::std::os::raw::c_uint = 0x00000613; +pub const MSR_PKG_POWER_INFO: ::std::os::raw::c_uint = 0x00000614; +pub const MSR_DRAM_POWER_LIMIT: ::std::os::raw::c_uint = 0x00000618; +pub const MSR_DRAM_ENERGY_STATUS: ::std::os::raw::c_uint = 0x00000619; +pub const MSR_DRAM_PERF_STATUS: ::std::os::raw::c_uint = 0x0000061b; +pub const MSR_DRAM_POWER_INFO: ::std::os::raw::c_uint = 0x0000061c; +pub const MSR_PP0_POWER_LIMIT: ::std::os::raw::c_uint = 0x00000638; +pub const MSR_PP0_ENERGY_STATUS: ::std::os::raw::c_uint = 0x00000639; +pub const MSR_PP0_POLICY: ::std::os::raw::c_uint = 0x0000063a; +pub const MSR_PP0_PERF_STATUS: ::std::os::raw::c_uint = 0x0000063b; +pub const MSR_PP1_POWER_LIMIT: ::std::os::raw::c_uint = 0x00000640; +pub const MSR_PP1_ENERGY_STATUS: ::std::os::raw::c_uint = 0x00000641; +pub const MSR_PP1_POLICY: ::std::os::raw::c_uint = 0x00000642; +pub const MSR_CONFIG_TDP_NOMINAL: ::std::os::raw::c_uint = 0x00000648; +pub const MSR_CONFIG_TDP_LEVEL_1: ::std::os::raw::c_uint = 0x00000649; +pub const MSR_CONFIG_TDP_LEVEL_2: ::std::os::raw::c_uint = 0x0000064a; +pub const MSR_CONFIG_TDP_CONTROL: ::std::os::raw::c_uint = 0x0000064b; +pub const MSR_TURBO_ACTIVATION_RATIO: ::std::os::raw::c_uint = 0x0000064c; +pub const MSR_PLATFORM_ENERGY_STATUS: ::std::os::raw::c_uint = 0x0000064d; +pub const MSR_PKG_WEIGHTED_CORE_C0_RES: ::std::os::raw::c_uint = 0x00000658; +pub const MSR_PKG_ANY_CORE_C0_RES: ::std::os::raw::c_uint = 0x00000659; +pub const MSR_PKG_ANY_GFXE_C0_RES: ::std::os::raw::c_uint = 0x0000065a; +pub const MSR_PKG_BOTH_CORE_GFXE_C0_RES: ::std::os::raw::c_uint = 0x0000065b; +pub const MSR_CORE_C1_RES: ::std::os::raw::c_uint = 0x00000660; +pub const MSR_MODULE_C6_RES_MS: ::std::os::raw::c_uint = 0x00000664; +pub const MSR_CC6_DEMOTION_POLICY_CONFIG: ::std::os::raw::c_uint = 0x00000668; +pub const MSR_MC6_DEMOTION_POLICY_CONFIG: ::std::os::raw::c_uint = 0x00000669; +pub const MSR_ATOM_CORE_RATIOS: ::std::os::raw::c_uint = 0x0000066a; +pub const MSR_ATOM_CORE_VIDS: ::std::os::raw::c_uint = 0x0000066b; +pub const MSR_ATOM_CORE_TURBO_RATIOS: ::std::os::raw::c_uint = 0x0000066c; +pub const MSR_ATOM_CORE_TURBO_VIDS: ::std::os::raw::c_uint = 0x0000066d; +pub const MSR_CORE_PERF_LIMIT_REASONS: ::std::os::raw::c_uint = 0x00000690; +pub const MSR_GFX_PERF_LIMIT_REASONS: ::std::os::raw::c_uint = 0x000006b0; +pub const MSR_RING_PERF_LIMIT_REASONS: ::std::os::raw::c_uint = 0x000006b1; +pub const MSR_PPERF: ::std::os::raw::c_uint = 0x0000064e; +pub const MSR_PERF_LIMIT_REASONS: ::std::os::raw::c_uint = 0x0000064f; +pub const MSR_PM_ENABLE: ::std::os::raw::c_uint = 0x00000770; +pub const MSR_HWP_CAPABILITIES: ::std::os::raw::c_uint = 0x00000771; +pub const MSR_HWP_REQUEST_PKG: ::std::os::raw::c_uint = 0x00000772; +pub const MSR_HWP_INTERRUPT: ::std::os::raw::c_uint = 0x00000773; +pub const MSR_HWP_REQUEST: ::std::os::raw::c_uint = 0x00000774; +pub const MSR_HWP_STATUS: ::std::os::raw::c_uint = 0x00000777; +pub const HWP_BASE_BIT: ::std::os::raw::c_uint = 0x00000080; +pub const HWP_NOTIFICATIONS_BIT: ::std::os::raw::c_uint = 0x00000100; +pub const HWP_ACTIVITY_WINDOW_BIT: ::std::os::raw::c_uint = 0x00000200; +pub const HWP_ENERGY_PERF_PREFERENCE_BIT: ::std::os::raw::c_uint = 0x00000400; +pub const HWP_PACKAGE_LEVEL_REQUEST_BIT: ::std::os::raw::c_uint = 0x00000800; +pub const MSR_AMD64_MC0_MASK: ::std::os::raw::c_uint = 0xc0010044; +pub const MSR_IA32_MC0_CTL2: ::std::os::raw::c_uint = 0x00000280; +pub const MSR_P6_PERFCTR0: ::std::os::raw::c_uint = 0x000000c1; +pub const MSR_P6_PERFCTR1: ::std::os::raw::c_uint = 0x000000c2; +pub const MSR_P6_EVNTSEL0: ::std::os::raw::c_uint = 0x00000186; +pub const MSR_P6_EVNTSEL1: ::std::os::raw::c_uint = 0x00000187; +pub const MSR_KNC_PERFCTR0: ::std::os::raw::c_uint = 0x00000020; +pub const MSR_KNC_PERFCTR1: ::std::os::raw::c_uint = 0x00000021; +pub const MSR_KNC_EVNTSEL0: ::std::os::raw::c_uint = 0x00000028; +pub const MSR_KNC_EVNTSEL1: ::std::os::raw::c_uint = 0x00000029; +pub const MSR_IA32_PMC0: ::std::os::raw::c_uint = 0x000004c1; +pub const MSR_AMD64_PATCH_LEVEL: ::std::os::raw::c_uint = 0x0000008b; +pub const MSR_AMD64_TSC_RATIO: ::std::os::raw::c_uint = 0xc0000104; +pub const MSR_AMD64_NB_CFG: ::std::os::raw::c_uint = 0xc001001f; +pub const MSR_AMD64_PATCH_LOADER: ::std::os::raw::c_uint = 0xc0010020; +pub const MSR_AMD64_OSVW_ID_LENGTH: ::std::os::raw::c_uint = 0xc0010140; +pub const MSR_AMD64_OSVW_STATUS: ::std::os::raw::c_uint = 0xc0010141; +pub const MSR_AMD64_LS_CFG: ::std::os::raw::c_uint = 0xc0011020; +pub const MSR_AMD64_DC_CFG: ::std::os::raw::c_uint = 0xc0011022; +pub const MSR_AMD64_BU_CFG2: ::std::os::raw::c_uint = 0xc001102a; +pub const MSR_AMD64_IBSFETCHCTL: ::std::os::raw::c_uint = 0xc0011030; +pub const MSR_AMD64_IBSFETCHLINAD: ::std::os::raw::c_uint = 0xc0011031; +pub const MSR_AMD64_IBSFETCHPHYSAD: ::std::os::raw::c_uint = 0xc0011032; +pub const MSR_AMD64_IBSFETCH_REG_COUNT: ::std::os::raw::c_uint = 0x00000003; +pub const MSR_AMD64_IBSFETCH_REG_MASK: ::std::os::raw::c_uint = 0x00000007; +pub const MSR_AMD64_IBSOPCTL: ::std::os::raw::c_uint = 0xc0011033; +pub const MSR_AMD64_IBSOPRIP: ::std::os::raw::c_uint = 0xc0011034; +pub const MSR_AMD64_IBSOPDATA: ::std::os::raw::c_uint = 0xc0011035; +pub const MSR_AMD64_IBSOPDATA2: ::std::os::raw::c_uint = 0xc0011036; +pub const MSR_AMD64_IBSOPDATA3: ::std::os::raw::c_uint = 0xc0011037; +pub const MSR_AMD64_IBSDCLINAD: ::std::os::raw::c_uint = 0xc0011038; +pub const MSR_AMD64_IBSDCPHYSAD: ::std::os::raw::c_uint = 0xc0011039; +pub const MSR_AMD64_IBSOP_REG_COUNT: ::std::os::raw::c_uint = 0x00000007; +pub const MSR_AMD64_IBSOP_REG_MASK: ::std::os::raw::c_uint = 0x0000007f; +pub const MSR_AMD64_IBSCTL: ::std::os::raw::c_uint = 0xc001103a; +pub const MSR_AMD64_IBSBRTARGET: ::std::os::raw::c_uint = 0xc001103b; +pub const MSR_AMD64_IBSOPDATA4: ::std::os::raw::c_uint = 0xc001103d; +pub const MSR_AMD64_IBS_REG_COUNT_MAX: ::std::os::raw::c_uint = 0x00000008; +pub const MSR_F17H_IRPERF: ::std::os::raw::c_uint = 0xc00000e9; +pub const MSR_F16H_L2I_PERF_CTL: ::std::os::raw::c_uint = 0xc0010230; +pub const MSR_F16H_L2I_PERF_CTR: ::std::os::raw::c_uint = 0xc0010231; +pub const MSR_F16H_DR1_ADDR_MASK: ::std::os::raw::c_uint = 0xc0011019; +pub const MSR_F16H_DR2_ADDR_MASK: ::std::os::raw::c_uint = 0xc001101a; +pub const MSR_F16H_DR3_ADDR_MASK: ::std::os::raw::c_uint = 0xc001101b; +pub const MSR_F16H_DR0_ADDR_MASK: ::std::os::raw::c_uint = 0xc0011027; +pub const MSR_F15H_PERF_CTL: ::std::os::raw::c_uint = 0xc0010200; +pub const MSR_F15H_PERF_CTR: ::std::os::raw::c_uint = 0xc0010201; +pub const MSR_F15H_NB_PERF_CTL: ::std::os::raw::c_uint = 0xc0010240; +pub const MSR_F15H_NB_PERF_CTR: ::std::os::raw::c_uint = 0xc0010241; +pub const MSR_F15H_PTSC: ::std::os::raw::c_uint = 0xc0010280; +pub const MSR_F15H_IC_CFG: ::std::os::raw::c_uint = 0xc0011021; +pub const MSR_FAM10H_MMIO_CONF_BASE: ::std::os::raw::c_uint = 0xc0010058; +pub const FAM10H_MMIO_CONF_ENABLE: ::std::os::raw::c_uint = 0x00000001; +pub const FAM10H_MMIO_CONF_BUSRANGE_MASK: ::std::os::raw::c_uint = 0x0000000f; +pub const FAM10H_MMIO_CONF_BUSRANGE_SHIFT: ::std::os::raw::c_uint = 0x00000002; +pub const FAM10H_MMIO_CONF_BASE_MASK: ::std::os::raw::c_uint = 0x0fffffff; +pub const FAM10H_MMIO_CONF_BASE_SHIFT: ::std::os::raw::c_uint = 0x00000014; +pub const MSR_FAM10H_NODE_ID: ::std::os::raw::c_uint = 0xc001100c; +pub const MSR_K8_TOP_MEM1: ::std::os::raw::c_uint = 0xc001001a; +pub const MSR_K8_TOP_MEM2: ::std::os::raw::c_uint = 0xc001001d; +pub const MSR_K8_SYSCFG: ::std::os::raw::c_uint = 0xc0010010; +pub const MSR_K8_INT_PENDING_MSG: ::std::os::raw::c_uint = 0xc0010055; +pub const K8_INTP_C1E_ACTIVE_MASK: ::std::os::raw::c_uint = 0x18000000; +pub const MSR_K8_TSEG_ADDR: ::std::os::raw::c_uint = 0xc0010112; +pub const MSR_K8_TSEG_MASK: ::std::os::raw::c_uint = 0xc0010113; +pub const K8_MTRRFIXRANGE_DRAM_ENABLE: ::std::os::raw::c_uint = 0x00040000; +pub const K8_MTRRFIXRANGE_DRAM_MODIFY: ::std::os::raw::c_uint = 0x00080000; +pub const K8_MTRR_RDMEM_WRMEM_MASK: ::std::os::raw::c_uint = 0x18181818; +pub const MSR_K7_EVNTSEL0: ::std::os::raw::c_uint = 0xc0010000; +pub const MSR_K7_PERFCTR0: ::std::os::raw::c_uint = 0xc0010004; +pub const MSR_K7_EVNTSEL1: ::std::os::raw::c_uint = 0xc0010001; +pub const MSR_K7_PERFCTR1: ::std::os::raw::c_uint = 0xc0010005; +pub const MSR_K7_EVNTSEL2: ::std::os::raw::c_uint = 0xc0010002; +pub const MSR_K7_PERFCTR2: ::std::os::raw::c_uint = 0xc0010006; +pub const MSR_K7_EVNTSEL3: ::std::os::raw::c_uint = 0xc0010003; +pub const MSR_K7_PERFCTR3: ::std::os::raw::c_uint = 0xc0010007; +pub const MSR_K7_CLK_CTL: ::std::os::raw::c_uint = 0xc001001b; +pub const MSR_K7_HWCR: ::std::os::raw::c_uint = 0xc0010015; +pub const MSR_K7_FID_VID_CTL: ::std::os::raw::c_uint = 0xc0010041; +pub const MSR_K7_FID_VID_STATUS: ::std::os::raw::c_uint = 0xc0010042; +pub const MSR_K6_WHCR: ::std::os::raw::c_uint = 0xc0000082; +pub const MSR_K6_UWCCR: ::std::os::raw::c_uint = 0xc0000085; +pub const MSR_K6_EPMR: ::std::os::raw::c_uint = 0xc0000086; +pub const MSR_K6_PSOR: ::std::os::raw::c_uint = 0xc0000087; +pub const MSR_K6_PFIR: ::std::os::raw::c_uint = 0xc0000088; +pub const MSR_IDT_FCR1: ::std::os::raw::c_uint = 0x00000107; +pub const MSR_IDT_FCR2: ::std::os::raw::c_uint = 0x00000108; +pub const MSR_IDT_FCR3: ::std::os::raw::c_uint = 0x00000109; +pub const MSR_IDT_FCR4: ::std::os::raw::c_uint = 0x0000010a; +pub const MSR_IDT_MCR0: ::std::os::raw::c_uint = 0x00000110; +pub const MSR_IDT_MCR1: ::std::os::raw::c_uint = 0x00000111; +pub const MSR_IDT_MCR2: ::std::os::raw::c_uint = 0x00000112; +pub const MSR_IDT_MCR3: ::std::os::raw::c_uint = 0x00000113; +pub const MSR_IDT_MCR4: ::std::os::raw::c_uint = 0x00000114; +pub const MSR_IDT_MCR5: ::std::os::raw::c_uint = 0x00000115; +pub const MSR_IDT_MCR6: ::std::os::raw::c_uint = 0x00000116; +pub const MSR_IDT_MCR7: ::std::os::raw::c_uint = 0x00000117; +pub const MSR_IDT_MCR_CTRL: ::std::os::raw::c_uint = 0x00000120; +pub const MSR_VIA_FCR: ::std::os::raw::c_uint = 0x00001107; +pub const MSR_VIA_LONGHAUL: ::std::os::raw::c_uint = 0x0000110a; +pub const MSR_VIA_RNG: ::std::os::raw::c_uint = 0x0000110b; +pub const MSR_VIA_BCR2: ::std::os::raw::c_uint = 0x00001147; +pub const MSR_TMTA_LONGRUN_CTRL: ::std::os::raw::c_uint = 0x80868010; +pub const MSR_TMTA_LONGRUN_FLAGS: ::std::os::raw::c_uint = 0x80868011; +pub const MSR_TMTA_LRTI_READOUT: ::std::os::raw::c_uint = 0x80868018; +pub const MSR_TMTA_LRTI_VOLT_MHZ: ::std::os::raw::c_uint = 0x8086801a; +pub const MSR_IA32_P5_MC_ADDR: ::std::os::raw::c_uint = 0x00000000; +pub const MSR_IA32_P5_MC_TYPE: ::std::os::raw::c_uint = 0x00000001; +pub const MSR_IA32_TSC: ::std::os::raw::c_uint = 0x00000010; +pub const MSR_IA32_PLATFORM_ID: ::std::os::raw::c_uint = 0x00000017; +pub const MSR_IA32_EBL_CR_POWERON: ::std::os::raw::c_uint = 0x0000002a; +pub const MSR_EBC_FREQUENCY_ID: ::std::os::raw::c_uint = 0x0000002c; +pub const MSR_SMI_COUNT: ::std::os::raw::c_uint = 0x00000034; +pub const MSR_IA32_FEATURE_CONTROL: ::std::os::raw::c_uint = 0x0000003a; +pub const MSR_IA32_TSC_ADJUST: ::std::os::raw::c_uint = 0x0000003b; +pub const MSR_IA32_BNDCFGS: ::std::os::raw::c_uint = 0x00000d90; +pub const MSR_IA32_XSS: ::std::os::raw::c_uint = 0x00000da0; +pub const FEATURE_CONTROL_LOCKED: ::std::os::raw::c_uint = 0x00000001; +pub const FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX: ::std::os::raw::c_uint = 0x00000002; +pub const FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX: ::std::os::raw::c_uint = 0x00000004; +pub const FEATURE_CONTROL_LMCE: ::std::os::raw::c_uint = 0x00100000; +pub const MSR_IA32_APICBASE: ::std::os::raw::c_uint = 0x0000001b; +pub const MSR_IA32_APICBASE_BSP: ::std::os::raw::c_uint = 0x00000100; +pub const MSR_IA32_APICBASE_ENABLE: ::std::os::raw::c_uint = 0x00000800; +pub const MSR_IA32_APICBASE_BASE: ::std::os::raw::c_uint = 0xfffff000; +pub const MSR_IA32_TSCDEADLINE: ::std::os::raw::c_uint = 0x000006e0; +pub const MSR_IA32_UCODE_WRITE: ::std::os::raw::c_uint = 0x00000079; +pub const MSR_IA32_UCODE_REV: ::std::os::raw::c_uint = 0x0000008b; +pub const MSR_IA32_SMM_MONITOR_CTL: ::std::os::raw::c_uint = 0x0000009b; +pub const MSR_IA32_SMBASE: ::std::os::raw::c_uint = 0x0000009e; +pub const MSR_IA32_PERF_STATUS: ::std::os::raw::c_uint = 0x00000198; +pub const MSR_IA32_PERF_CTL: ::std::os::raw::c_uint = 0x00000199; +pub const INTEL_PERF_CTL_MASK: ::std::os::raw::c_uint = 0x0000ffff; +pub const MSR_AMD_PSTATE_DEF_BASE: ::std::os::raw::c_uint = 0xc0010064; +pub const MSR_AMD_PERF_STATUS: ::std::os::raw::c_uint = 0xc0010063; +pub const MSR_AMD_PERF_CTL: ::std::os::raw::c_uint = 0xc0010062; +pub const MSR_IA32_MPERF: ::std::os::raw::c_uint = 0x000000e7; +pub const MSR_IA32_APERF: ::std::os::raw::c_uint = 0x000000e8; +pub const MSR_IA32_THERM_CONTROL: ::std::os::raw::c_uint = 0x0000019a; +pub const MSR_IA32_THERM_INTERRUPT: ::std::os::raw::c_uint = 0x0000019b; +pub const THERM_INT_HIGH_ENABLE: ::std::os::raw::c_uint = 0x00000001; +pub const THERM_INT_LOW_ENABLE: ::std::os::raw::c_uint = 0x00000002; +pub const THERM_INT_PLN_ENABLE: ::std::os::raw::c_uint = 0x01000000; +pub const MSR_IA32_THERM_STATUS: ::std::os::raw::c_uint = 0x0000019c; +pub const THERM_STATUS_PROCHOT: ::std::os::raw::c_uint = 0x00000001; +pub const THERM_STATUS_POWER_LIMIT: ::std::os::raw::c_uint = 0x00000400; +pub const MSR_THERM2_CTL: ::std::os::raw::c_uint = 0x0000019d; +pub const MSR_THERM2_CTL_TM_SELECT: ::std::os::raw::c_uint = 0x00010000; +pub const MSR_IA32_MISC_ENABLE: ::std::os::raw::c_uint = 0x000001a0; +pub const MSR_IA32_TEMPERATURE_TARGET: ::std::os::raw::c_uint = 0x000001a2; +pub const MSR_MISC_FEATURE_CONTROL: ::std::os::raw::c_uint = 0x000001a4; +pub const MSR_MISC_PWR_MGMT: ::std::os::raw::c_uint = 0x000001aa; +pub const MSR_IA32_ENERGY_PERF_BIAS: ::std::os::raw::c_uint = 0x000001b0; +pub const ENERGY_PERF_BIAS_PERFORMANCE: ::std::os::raw::c_uint = 0x00000000; +pub const ENERGY_PERF_BIAS_NORMAL: ::std::os::raw::c_uint = 0x00000006; +pub const ENERGY_PERF_BIAS_POWERSAVE: ::std::os::raw::c_uint = 0x0000000f; +pub const MSR_IA32_PACKAGE_THERM_STATUS: ::std::os::raw::c_uint = 0x000001b1; +pub const PACKAGE_THERM_STATUS_PROCHOT: ::std::os::raw::c_uint = 0x00000001; +pub const PACKAGE_THERM_STATUS_POWER_LIMIT: ::std::os::raw::c_uint = 0x00000400; +pub const MSR_IA32_PACKAGE_THERM_INTERRUPT: ::std::os::raw::c_uint = 0x000001b2; +pub const PACKAGE_THERM_INT_HIGH_ENABLE: ::std::os::raw::c_uint = 0x00000001; +pub const PACKAGE_THERM_INT_LOW_ENABLE: ::std::os::raw::c_uint = 0x00000002; +pub const PACKAGE_THERM_INT_PLN_ENABLE: ::std::os::raw::c_uint = 0x01000000; +pub const THERM_INT_THRESHOLD0_ENABLE: ::std::os::raw::c_uint = 0x00008000; +pub const THERM_SHIFT_THRESHOLD0: ::std::os::raw::c_uint = 0x00000008; +pub const THERM_MASK_THRESHOLD0: ::std::os::raw::c_uint = 0x00007f00; +pub const THERM_INT_THRESHOLD1_ENABLE: ::std::os::raw::c_uint = 0x00800000; +pub const THERM_SHIFT_THRESHOLD1: ::std::os::raw::c_uint = 0x00000010; +pub const THERM_MASK_THRESHOLD1: ::std::os::raw::c_uint = 0x007f0000; +pub const THERM_STATUS_THRESHOLD0: ::std::os::raw::c_uint = 0x00000040; +pub const THERM_LOG_THRESHOLD0: ::std::os::raw::c_uint = 0x00000080; +pub const THERM_STATUS_THRESHOLD1: ::std::os::raw::c_uint = 0x00000100; +pub const THERM_LOG_THRESHOLD1: ::std::os::raw::c_uint = 0x00000200; +pub const MSR_IA32_MISC_ENABLE_FAST_STRING_BIT: ::std::os::raw::c_uint = 0x00000000; +pub const MSR_IA32_MISC_ENABLE_FAST_STRING: ::std::os::raw::c_uint = 0x00000001; +pub const MSR_IA32_MISC_ENABLE_TCC_BIT: ::std::os::raw::c_uint = 0x00000001; +pub const MSR_IA32_MISC_ENABLE_TCC: ::std::os::raw::c_uint = 0x00000002; +pub const MSR_IA32_MISC_ENABLE_EMON_BIT: ::std::os::raw::c_uint = 0x00000007; +pub const MSR_IA32_MISC_ENABLE_EMON: ::std::os::raw::c_uint = 0x00000080; +pub const MSR_IA32_MISC_ENABLE_BTS_UNAVAIL_BIT: ::std::os::raw::c_uint = 0x0000000b; +pub const MSR_IA32_MISC_ENABLE_BTS_UNAVAIL: ::std::os::raw::c_uint = 0x00000800; +pub const MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL_BIT: ::std::os::raw::c_uint = 0x0000000c; +pub const MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL: ::std::os::raw::c_uint = 0x00001000; +pub const MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP_BIT: ::std::os::raw::c_uint = 0x00000010; +pub const MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP: ::std::os::raw::c_uint = 0x00010000; +pub const MSR_IA32_MISC_ENABLE_MWAIT_BIT: ::std::os::raw::c_uint = 0x00000012; +pub const MSR_IA32_MISC_ENABLE_MWAIT: ::std::os::raw::c_uint = 0x00040000; +pub const MSR_IA32_MISC_ENABLE_LIMIT_CPUID_BIT: ::std::os::raw::c_uint = 0x00000016; +pub const MSR_IA32_MISC_ENABLE_LIMIT_CPUID: ::std::os::raw::c_uint = 0x00400000; +pub const MSR_IA32_MISC_ENABLE_XTPR_DISABLE_BIT: ::std::os::raw::c_uint = 0x00000017; +pub const MSR_IA32_MISC_ENABLE_XTPR_DISABLE: ::std::os::raw::c_uint = 0x00800000; +pub const MSR_IA32_MISC_ENABLE_XD_DISABLE_BIT: ::std::os::raw::c_uint = 0x00000022; +pub const MSR_IA32_MISC_ENABLE_XD_DISABLE: ::std::os::raw::c_ulonglong = 0x400000000; +pub const MSR_IA32_MISC_ENABLE_X87_COMPAT_BIT: ::std::os::raw::c_uint = 0x00000002; +pub const MSR_IA32_MISC_ENABLE_X87_COMPAT: ::std::os::raw::c_uint = 0x00000004; +pub const MSR_IA32_MISC_ENABLE_TM1_BIT: ::std::os::raw::c_uint = 0x00000003; +pub const MSR_IA32_MISC_ENABLE_TM1: ::std::os::raw::c_uint = 0x00000008; +pub const MSR_IA32_MISC_ENABLE_SPLIT_LOCK_DISABLE_BIT: ::std::os::raw::c_uint = 0x00000004; +pub const MSR_IA32_MISC_ENABLE_SPLIT_LOCK_DISABLE: ::std::os::raw::c_uint = 0x00000010; +pub const MSR_IA32_MISC_ENABLE_L3CACHE_DISABLE_BIT: ::std::os::raw::c_uint = 0x00000006; +pub const MSR_IA32_MISC_ENABLE_L3CACHE_DISABLE: ::std::os::raw::c_uint = 0x00000040; +pub const MSR_IA32_MISC_ENABLE_SUPPRESS_LOCK_BIT: ::std::os::raw::c_uint = 0x00000008; +pub const MSR_IA32_MISC_ENABLE_SUPPRESS_LOCK: ::std::os::raw::c_uint = 0x00000100; +pub const MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE_BIT: ::std::os::raw::c_uint = 0x00000009; +pub const MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE: ::std::os::raw::c_uint = 0x00000200; +pub const MSR_IA32_MISC_ENABLE_FERR_BIT: ::std::os::raw::c_uint = 0x0000000a; +pub const MSR_IA32_MISC_ENABLE_FERR: ::std::os::raw::c_uint = 0x00000400; +pub const MSR_IA32_MISC_ENABLE_FERR_MULTIPLEX_BIT: ::std::os::raw::c_uint = 0x0000000a; +pub const MSR_IA32_MISC_ENABLE_FERR_MULTIPLEX: ::std::os::raw::c_uint = 0x00000400; +pub const MSR_IA32_MISC_ENABLE_TM2_BIT: ::std::os::raw::c_uint = 0x0000000d; +pub const MSR_IA32_MISC_ENABLE_TM2: ::std::os::raw::c_uint = 0x00002000; +pub const MSR_IA32_MISC_ENABLE_ADJ_PREF_DISABLE_BIT: ::std::os::raw::c_uint = 0x00000013; +pub const MSR_IA32_MISC_ENABLE_ADJ_PREF_DISABLE: ::std::os::raw::c_uint = 0x00080000; +pub const MSR_IA32_MISC_ENABLE_SPEEDSTEP_LOCK_BIT: ::std::os::raw::c_uint = 0x00000014; +pub const MSR_IA32_MISC_ENABLE_SPEEDSTEP_LOCK: ::std::os::raw::c_uint = 0x00100000; +pub const MSR_IA32_MISC_ENABLE_L1D_CONTEXT_BIT: ::std::os::raw::c_uint = 0x00000018; +pub const MSR_IA32_MISC_ENABLE_L1D_CONTEXT: ::std::os::raw::c_uint = 0x01000000; +pub const MSR_IA32_MISC_ENABLE_DCU_PREF_DISABLE_BIT: ::std::os::raw::c_uint = 0x00000025; +pub const MSR_IA32_MISC_ENABLE_DCU_PREF_DISABLE: ::std::os::raw::c_ulonglong = 0x2000000000; +pub const MSR_IA32_MISC_ENABLE_TURBO_DISABLE_BIT: ::std::os::raw::c_uint = 0x00000026; +pub const MSR_IA32_MISC_ENABLE_TURBO_DISABLE: ::std::os::raw::c_ulonglong = 0x4000000000; +pub const MSR_IA32_MISC_ENABLE_IP_PREF_DISABLE_BIT: ::std::os::raw::c_uint = 0x00000027; +pub const MSR_IA32_MISC_ENABLE_IP_PREF_DISABLE: ::std::os::raw::c_ulonglong = 0x8000000000; +pub const MSR_MISC_FEATURE_ENABLES: ::std::os::raw::c_uint = 0x00000140; +pub const MSR_MISC_FEATURE_ENABLES_RING3MWAIT_BIT: ::std::os::raw::c_uint = 0x00000001; +pub const MSR_IA32_TSC_DEADLINE: ::std::os::raw::c_uint = 0x000006e0; +pub const MSR_IA32_MCG_EAX: ::std::os::raw::c_uint = 0x00000180; +pub const MSR_IA32_MCG_EBX: ::std::os::raw::c_uint = 0x00000181; +pub const MSR_IA32_MCG_ECX: ::std::os::raw::c_uint = 0x00000182; +pub const MSR_IA32_MCG_EDX: ::std::os::raw::c_uint = 0x00000183; +pub const MSR_IA32_MCG_ESI: ::std::os::raw::c_uint = 0x00000184; +pub const MSR_IA32_MCG_EDI: ::std::os::raw::c_uint = 0x00000185; +pub const MSR_IA32_MCG_EBP: ::std::os::raw::c_uint = 0x00000186; +pub const MSR_IA32_MCG_ESP: ::std::os::raw::c_uint = 0x00000187; +pub const MSR_IA32_MCG_EFLAGS: ::std::os::raw::c_uint = 0x00000188; +pub const MSR_IA32_MCG_EIP: ::std::os::raw::c_uint = 0x00000189; +pub const MSR_IA32_MCG_RESERVED: ::std::os::raw::c_uint = 0x0000018a; +pub const MSR_P4_BPU_PERFCTR0: ::std::os::raw::c_uint = 0x00000300; +pub const MSR_P4_BPU_PERFCTR1: ::std::os::raw::c_uint = 0x00000301; +pub const MSR_P4_BPU_PERFCTR2: ::std::os::raw::c_uint = 0x00000302; +pub const MSR_P4_BPU_PERFCTR3: ::std::os::raw::c_uint = 0x00000303; +pub const MSR_P4_MS_PERFCTR0: ::std::os::raw::c_uint = 0x00000304; +pub const MSR_P4_MS_PERFCTR1: ::std::os::raw::c_uint = 0x00000305; +pub const MSR_P4_MS_PERFCTR2: ::std::os::raw::c_uint = 0x00000306; +pub const MSR_P4_MS_PERFCTR3: ::std::os::raw::c_uint = 0x00000307; +pub const MSR_P4_FLAME_PERFCTR0: ::std::os::raw::c_uint = 0x00000308; +pub const MSR_P4_FLAME_PERFCTR1: ::std::os::raw::c_uint = 0x00000309; +pub const MSR_P4_FLAME_PERFCTR2: ::std::os::raw::c_uint = 0x0000030a; +pub const MSR_P4_FLAME_PERFCTR3: ::std::os::raw::c_uint = 0x0000030b; +pub const MSR_P4_IQ_PERFCTR0: ::std::os::raw::c_uint = 0x0000030c; +pub const MSR_P4_IQ_PERFCTR1: ::std::os::raw::c_uint = 0x0000030d; +pub const MSR_P4_IQ_PERFCTR2: ::std::os::raw::c_uint = 0x0000030e; +pub const MSR_P4_IQ_PERFCTR3: ::std::os::raw::c_uint = 0x0000030f; +pub const MSR_P4_IQ_PERFCTR4: ::std::os::raw::c_uint = 0x00000310; +pub const MSR_P4_IQ_PERFCTR5: ::std::os::raw::c_uint = 0x00000311; +pub const MSR_P4_BPU_CCCR0: ::std::os::raw::c_uint = 0x00000360; +pub const MSR_P4_BPU_CCCR1: ::std::os::raw::c_uint = 0x00000361; +pub const MSR_P4_BPU_CCCR2: ::std::os::raw::c_uint = 0x00000362; +pub const MSR_P4_BPU_CCCR3: ::std::os::raw::c_uint = 0x00000363; +pub const MSR_P4_MS_CCCR0: ::std::os::raw::c_uint = 0x00000364; +pub const MSR_P4_MS_CCCR1: ::std::os::raw::c_uint = 0x00000365; +pub const MSR_P4_MS_CCCR2: ::std::os::raw::c_uint = 0x00000366; +pub const MSR_P4_MS_CCCR3: ::std::os::raw::c_uint = 0x00000367; +pub const MSR_P4_FLAME_CCCR0: ::std::os::raw::c_uint = 0x00000368; +pub const MSR_P4_FLAME_CCCR1: ::std::os::raw::c_uint = 0x00000369; +pub const MSR_P4_FLAME_CCCR2: ::std::os::raw::c_uint = 0x0000036a; +pub const MSR_P4_FLAME_CCCR3: ::std::os::raw::c_uint = 0x0000036b; +pub const MSR_P4_IQ_CCCR0: ::std::os::raw::c_uint = 0x0000036c; +pub const MSR_P4_IQ_CCCR1: ::std::os::raw::c_uint = 0x0000036d; +pub const MSR_P4_IQ_CCCR2: ::std::os::raw::c_uint = 0x0000036e; +pub const MSR_P4_IQ_CCCR3: ::std::os::raw::c_uint = 0x0000036f; +pub const MSR_P4_IQ_CCCR4: ::std::os::raw::c_uint = 0x00000370; +pub const MSR_P4_IQ_CCCR5: ::std::os::raw::c_uint = 0x00000371; +pub const MSR_P4_ALF_ESCR0: ::std::os::raw::c_uint = 0x000003ca; +pub const MSR_P4_ALF_ESCR1: ::std::os::raw::c_uint = 0x000003cb; +pub const MSR_P4_BPU_ESCR0: ::std::os::raw::c_uint = 0x000003b2; +pub const MSR_P4_BPU_ESCR1: ::std::os::raw::c_uint = 0x000003b3; +pub const MSR_P4_BSU_ESCR0: ::std::os::raw::c_uint = 0x000003a0; +pub const MSR_P4_BSU_ESCR1: ::std::os::raw::c_uint = 0x000003a1; +pub const MSR_P4_CRU_ESCR0: ::std::os::raw::c_uint = 0x000003b8; +pub const MSR_P4_CRU_ESCR1: ::std::os::raw::c_uint = 0x000003b9; +pub const MSR_P4_CRU_ESCR2: ::std::os::raw::c_uint = 0x000003cc; +pub const MSR_P4_CRU_ESCR3: ::std::os::raw::c_uint = 0x000003cd; +pub const MSR_P4_CRU_ESCR4: ::std::os::raw::c_uint = 0x000003e0; +pub const MSR_P4_CRU_ESCR5: ::std::os::raw::c_uint = 0x000003e1; +pub const MSR_P4_DAC_ESCR0: ::std::os::raw::c_uint = 0x000003a8; +pub const MSR_P4_DAC_ESCR1: ::std::os::raw::c_uint = 0x000003a9; +pub const MSR_P4_FIRM_ESCR0: ::std::os::raw::c_uint = 0x000003a4; +pub const MSR_P4_FIRM_ESCR1: ::std::os::raw::c_uint = 0x000003a5; +pub const MSR_P4_FLAME_ESCR0: ::std::os::raw::c_uint = 0x000003a6; +pub const MSR_P4_FLAME_ESCR1: ::std::os::raw::c_uint = 0x000003a7; +pub const MSR_P4_FSB_ESCR0: ::std::os::raw::c_uint = 0x000003a2; +pub const MSR_P4_FSB_ESCR1: ::std::os::raw::c_uint = 0x000003a3; +pub const MSR_P4_IQ_ESCR0: ::std::os::raw::c_uint = 0x000003ba; +pub const MSR_P4_IQ_ESCR1: ::std::os::raw::c_uint = 0x000003bb; +pub const MSR_P4_IS_ESCR0: ::std::os::raw::c_uint = 0x000003b4; +pub const MSR_P4_IS_ESCR1: ::std::os::raw::c_uint = 0x000003b5; +pub const MSR_P4_ITLB_ESCR0: ::std::os::raw::c_uint = 0x000003b6; +pub const MSR_P4_ITLB_ESCR1: ::std::os::raw::c_uint = 0x000003b7; +pub const MSR_P4_IX_ESCR0: ::std::os::raw::c_uint = 0x000003c8; +pub const MSR_P4_IX_ESCR1: ::std::os::raw::c_uint = 0x000003c9; +pub const MSR_P4_MOB_ESCR0: ::std::os::raw::c_uint = 0x000003aa; +pub const MSR_P4_MOB_ESCR1: ::std::os::raw::c_uint = 0x000003ab; +pub const MSR_P4_MS_ESCR0: ::std::os::raw::c_uint = 0x000003c0; +pub const MSR_P4_MS_ESCR1: ::std::os::raw::c_uint = 0x000003c1; +pub const MSR_P4_PMH_ESCR0: ::std::os::raw::c_uint = 0x000003ac; +pub const MSR_P4_PMH_ESCR1: ::std::os::raw::c_uint = 0x000003ad; +pub const MSR_P4_RAT_ESCR0: ::std::os::raw::c_uint = 0x000003bc; +pub const MSR_P4_RAT_ESCR1: ::std::os::raw::c_uint = 0x000003bd; +pub const MSR_P4_SAAT_ESCR0: ::std::os::raw::c_uint = 0x000003ae; +pub const MSR_P4_SAAT_ESCR1: ::std::os::raw::c_uint = 0x000003af; +pub const MSR_P4_SSU_ESCR0: ::std::os::raw::c_uint = 0x000003be; +pub const MSR_P4_SSU_ESCR1: ::std::os::raw::c_uint = 0x000003bf; +pub const MSR_P4_TBPU_ESCR0: ::std::os::raw::c_uint = 0x000003c2; +pub const MSR_P4_TBPU_ESCR1: ::std::os::raw::c_uint = 0x000003c3; +pub const MSR_P4_TC_ESCR0: ::std::os::raw::c_uint = 0x000003c4; +pub const MSR_P4_TC_ESCR1: ::std::os::raw::c_uint = 0x000003c5; +pub const MSR_P4_U2L_ESCR0: ::std::os::raw::c_uint = 0x000003b0; +pub const MSR_P4_U2L_ESCR1: ::std::os::raw::c_uint = 0x000003b1; +pub const MSR_P4_PEBS_MATRIX_VERT: ::std::os::raw::c_uint = 0x000003f2; +pub const MSR_CORE_PERF_FIXED_CTR0: ::std::os::raw::c_uint = 0x00000309; +pub const MSR_CORE_PERF_FIXED_CTR1: ::std::os::raw::c_uint = 0x0000030a; +pub const MSR_CORE_PERF_FIXED_CTR2: ::std::os::raw::c_uint = 0x0000030b; +pub const MSR_CORE_PERF_FIXED_CTR_CTRL: ::std::os::raw::c_uint = 0x0000038d; +pub const MSR_CORE_PERF_GLOBAL_STATUS: ::std::os::raw::c_uint = 0x0000038e; +pub const MSR_CORE_PERF_GLOBAL_CTRL: ::std::os::raw::c_uint = 0x0000038f; +pub const MSR_CORE_PERF_GLOBAL_OVF_CTRL: ::std::os::raw::c_uint = 0x00000390; +pub const MSR_GEODE_BUSCONT_CONF0: ::std::os::raw::c_uint = 0x00001900; +pub const MSR_IA32_VMX_BASIC: ::std::os::raw::c_uint = 0x00000480; +pub const MSR_IA32_VMX_PINBASED_CTLS: ::std::os::raw::c_uint = 0x00000481; +pub const MSR_IA32_VMX_PROCBASED_CTLS: ::std::os::raw::c_uint = 0x00000482; +pub const MSR_IA32_VMX_EXIT_CTLS: ::std::os::raw::c_uint = 0x00000483; +pub const MSR_IA32_VMX_ENTRY_CTLS: ::std::os::raw::c_uint = 0x00000484; +pub const MSR_IA32_VMX_MISC: ::std::os::raw::c_uint = 0x00000485; +pub const MSR_IA32_VMX_CR0_FIXED0: ::std::os::raw::c_uint = 0x00000486; +pub const MSR_IA32_VMX_CR0_FIXED1: ::std::os::raw::c_uint = 0x00000487; +pub const MSR_IA32_VMX_CR4_FIXED0: ::std::os::raw::c_uint = 0x00000488; +pub const MSR_IA32_VMX_CR4_FIXED1: ::std::os::raw::c_uint = 0x00000489; +pub const MSR_IA32_VMX_VMCS_ENUM: ::std::os::raw::c_uint = 0x0000048a; +pub const MSR_IA32_VMX_PROCBASED_CTLS2: ::std::os::raw::c_uint = 0x0000048b; +pub const MSR_IA32_VMX_EPT_VPID_CAP: ::std::os::raw::c_uint = 0x0000048c; +pub const MSR_IA32_VMX_TRUE_PINBASED_CTLS: ::std::os::raw::c_uint = 0x0000048d; +pub const MSR_IA32_VMX_TRUE_PROCBASED_CTLS: ::std::os::raw::c_uint = 0x0000048e; +pub const MSR_IA32_VMX_TRUE_EXIT_CTLS: ::std::os::raw::c_uint = 0x0000048f; +pub const MSR_IA32_VMX_TRUE_ENTRY_CTLS: ::std::os::raw::c_uint = 0x00000490; +pub const MSR_IA32_VMX_VMFUNC: ::std::os::raw::c_uint = 0x00000491; +pub const VMX_BASIC_VMCS_SIZE_SHIFT: ::std::os::raw::c_uint = 0x00000020; +pub const VMX_BASIC_TRUE_CTLS: ::std::os::raw::c_ulonglong = 0x80000000000000; +pub const VMX_BASIC_64: ::std::os::raw::c_ulonglong = 0x1000000000000; +pub const VMX_BASIC_MEM_TYPE_SHIFT: ::std::os::raw::c_uint = 0x00000032; +pub const VMX_BASIC_MEM_TYPE_MASK: ::std::os::raw::c_ulonglong = 0x3c000000000000; +pub const VMX_BASIC_MEM_TYPE_WB: ::std::os::raw::c_uint = 0x00000006; +pub const VMX_BASIC_INOUT: ::std::os::raw::c_ulonglong = 0x40000000000000; +pub const MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS: ::std::os::raw::c_uint = 0x20000000; +pub const MSR_IA32_VMX_MISC_PREEMPTION_TIMER_SCALE: ::std::os::raw::c_uint = 0x0000001f; +pub const MSR_VM_CR: ::std::os::raw::c_uint = 0xc0010114; +pub const MSR_VM_IGNNE: ::std::os::raw::c_uint = 0xc0010115; +pub const MSR_VM_HSAVE_PA: ::std::os::raw::c_uint = 0xc0010117; diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/regs.rs b/vendor/cube-hypervisor/hypervisor/src/arch/x86/regs.rs new file mode 100644 index 0000000..cb39cec --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/arch/x86/regs.rs @@ -0,0 +1,34 @@ +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +// EFER (technically not a register) bits +pub const EFER_LMA: u64 = 0x400; +pub const EFER_LME: u64 = 0x100; + +// CR0 bits +pub const CR0_PE: u64 = 0x1; +pub const CR0_PG: u64 = 0x80000000; + +// CR4 bits +pub const CR4_PAE: u64 = 0x20; +pub const CR4_LA57: u64 = 0x1000; + +// RFlags bits +pub const CF_SHIFT: usize = 0; +pub const PF_SHIFT: usize = 2; +pub const AF_SHIFT: usize = 4; +pub const ZF_SHIFT: usize = 6; +pub const SF_SHIFT: usize = 7; +pub const DF_SHIFT: usize = 10; +pub const OF_SHIFT: usize = 11; + +pub const CF: u64 = 1 << CF_SHIFT; +pub const PF: u64 = 1 << PF_SHIFT; +pub const AF: u64 = 1 << AF_SHIFT; +pub const ZF: u64 = 1 << ZF_SHIFT; +pub const SF: u64 = 1 << SF_SHIFT; +pub const DF: u64 = 1 << DF_SHIFT; +pub const OF: u64 = 1 << OF_SHIFT; diff --git a/vendor/cube-hypervisor/hypervisor/src/cpu.rs b/vendor/cube-hypervisor/hypervisor/src/cpu.rs new file mode 100644 index 0000000..dbf32a4 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/cpu.rs @@ -0,0 +1,429 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// + +#[cfg(target_arch = "aarch64")] +use crate::aarch64::{RegList, StandardRegisters, VcpuInit}; +#[cfg(target_arch = "x86_64")] +use crate::arch::x86::{ + CpuIdEntry, FpuState, LapicState, MsrEntry, SpecialRegisters, StandardRegisters, +}; +#[cfg(feature = "tdx")] +use crate::kvm::{TdxExitDetails, TdxExitStatus}; +use crate::CpuState; +use crate::MpState; +use thiserror::Error; +use vm_memory::GuestAddress; + +#[derive(Error, Debug)] +/// +/// Enum for CPU error +pub enum HypervisorCpuError { + /// + /// Setting standard registers error + /// + #[error("Failed to set standard register: {0}")] + SetStandardRegs(#[source] anyhow::Error), + /// + /// Setting standard registers error + /// + #[error("Failed to get standard registers: {0}")] + GetStandardRegs(#[source] anyhow::Error), + /// + /// Setting special register error + /// + #[error("Failed to set special registers: {0}")] + SetSpecialRegs(#[source] anyhow::Error), + /// + /// Getting standard register error + /// + #[error("Failed to get special registers: {0}")] + GetSpecialRegs(#[source] anyhow::Error), + /// + /// Setting floating point registers error + /// + #[error("Failed to set floating point registers: {0}")] + SetFloatingPointRegs(#[source] anyhow::Error), + /// + /// Getting floating point register error + /// + #[error("Failed to get floating points registers: {0}")] + GetFloatingPointRegs(#[source] anyhow::Error), + /// + /// Setting Cpuid error + /// + #[error("Failed to set Cpuid: {0}")] + SetCpuid(#[source] anyhow::Error), + /// + /// Getting Cpuid error + /// + #[error("Failed to get Cpuid: {0}")] + GetCpuid(#[source] anyhow::Error), + /// + /// Setting lapic state error + /// + #[error("Failed to set Lapic state: {0}")] + SetLapicState(#[source] anyhow::Error), + /// + /// Getting Lapic state error + /// + #[error("Failed to get Lapic state: {0}")] + GetlapicState(#[source] anyhow::Error), + /// + /// Setting MSR entries error + /// + #[error("Failed to set Msr entries: {0}")] + SetMsrEntries(#[source] anyhow::Error), + /// + /// Getting Msr entries error + /// + #[error("Failed to get Msr entries: {0}")] + GetMsrEntries(#[source] anyhow::Error), + /// + /// Setting MSR entries error + /// + #[error("Failed to set MP state: {0}")] + SetMpState(#[source] anyhow::Error), + /// + /// Getting Msr entries error + /// + #[error("Failed to get MP state: {0}")] + GetMpState(#[source] anyhow::Error), + /// + /// Setting Saved Processor Extended States error + /// + #[error("Failed to set Saved Processor Extended States: {0}")] + SetXsaveState(#[source] anyhow::Error), + /// + /// Getting Saved Processor Extended States error + /// + #[error("Failed to get Saved Processor Extended States: {0}")] + GetXsaveState(#[source] anyhow::Error), + /// + /// Setting Extended Control Registers error + /// + #[error("Failed to set Extended Control Registers: {0}")] + SetXcsr(#[source] anyhow::Error), + /// + /// Getting Extended Control Registers error + /// + #[error("Failed to get Extended Control Registers: {0}")] + GetXcsr(#[source] anyhow::Error), + /// + /// Running Vcpu error + /// + #[error("Failed to run vcpu: {0}")] + RunVcpu(#[source] anyhow::Error), + /// + /// Getting Vcpu events error + /// + #[error("Failed to get Vcpu events: {0}")] + GetVcpuEvents(#[source] anyhow::Error), + /// + /// Setting Vcpu events error + /// + #[error("Failed to set Vcpu events: {0}")] + SetVcpuEvents(#[source] anyhow::Error), + /// + /// Vcpu Init error + /// + #[error("Failed to init vcpu: {0}")] + VcpuInit(#[source] anyhow::Error), + /// + /// Setting one reg error + /// + #[error("Failed to init vcpu: {0}")] + SetRegister(#[source] anyhow::Error), + /// + /// Getting one reg error + /// + #[error("Failed to init vcpu: {0}")] + GetRegister(#[source] anyhow::Error), + /// + /// Getting guest clock paused error + /// + #[error("Failed to notify guest its clock was paused: {0}")] + NotifyGuestClockPaused(#[source] anyhow::Error), + /// + /// Setting debug register error + /// + #[error("Failed to set debug registers: {0}")] + SetDebugRegs(#[source] anyhow::Error), + /// + /// Getting debug register error + /// + #[error("Failed to get debug registers: {0}")] + GetDebugRegs(#[source] anyhow::Error), + /// + /// Setting misc register error + /// + #[error("Failed to set misc registers: {0}")] + SetMiscRegs(#[source] anyhow::Error), + /// + /// Getting misc register error + /// + #[error("Failed to get misc registers: {0}")] + GetMiscRegs(#[source] anyhow::Error), + /// + /// Write to Guest Mem + /// + #[error("Failed to write to Guest Mem at: {0}")] + GuestMemWrite(#[source] anyhow::Error), + /// Enabling HyperV SynIC error + /// + #[error("Failed to enable HyperV SynIC")] + EnableHyperVSyncIc(#[source] anyhow::Error), + /// + /// Getting AArch64 core register error + /// + #[error("Failed to get core register: {0}")] + GetCoreRegister(#[source] anyhow::Error), + /// + /// Setting AArch64 core register error + /// + #[error("Failed to set core register: {0}")] + SetCoreRegister(#[source] anyhow::Error), + /// + /// Getting AArch64 registers list error + /// + #[error("Failed to retrieve list of registers: {0}")] + GetRegList(#[source] anyhow::Error), + /// + /// Getting AArch64 system register error + /// + #[error("Failed to get system register: {0}")] + GetSysRegister(#[source] anyhow::Error), + /// + /// Setting AArch64 system register error + /// + #[error("Failed to set system register: {0}")] + SetSysRegister(#[source] anyhow::Error), + /// + /// GVA translation error + /// + #[error("Failed to translate GVA: {0}")] + TranslateVirtualAddress(#[source] anyhow::Error), + /// + /// Set cpu attribute error + /// + #[error("Failed to set vcpu attribute: {0}")] + SetVcpuAttribute(#[source] anyhow::Error), + /// + /// Check if cpu has a certain attribute error + /// + #[error("Failed to check if vcpu has attribute: {0}")] + HasVcpuAttribute(#[source] anyhow::Error), + /// + /// Failed to initialize TDX on CPU + /// + #[cfg(feature = "tdx")] + #[error("Failed to initialize TDX: {0}")] + InitializeTdx(#[source] std::io::Error), + /// + /// Unknown TDX VM call + /// + #[cfg(feature = "tdx")] + #[error("Unknown TDX VM call")] + UnknownTdxVmCall, + #[cfg(target_arch = "aarch64")] + /// + /// Failed to intialize PMU + /// + #[error("Failed to initialize PMU")] + InitializePmu, +} + +#[derive(Debug)] +pub enum VmExit { + #[cfg(target_arch = "x86_64")] + IoapicEoi(u8 /* vector */), + Ignore, + Reset, + Shutdown, + Hyperv, + #[cfg(feature = "tdx")] + Tdx, + #[cfg(feature = "kvm")] + Debug, +} + +/// +/// Result type for returning from a function +/// +pub type Result = anyhow::Result; +/// +/// Trait to represent a generic Vcpu +/// +pub trait Vcpu: Send + Sync { + /// + /// Returns the vCPU general purpose registers. + /// + fn get_regs(&self) -> Result; + /// + /// Sets the vCPU general purpose registers. + /// + fn set_regs(&self, regs: &StandardRegisters) -> Result<()>; + #[cfg(target_arch = "x86_64")] + /// + /// Returns the vCPU special registers. + /// + fn get_sregs(&self) -> Result; + #[cfg(target_arch = "x86_64")] + /// + /// Sets the vCPU special registers + /// + fn set_sregs(&self, sregs: &SpecialRegisters) -> Result<()>; + #[cfg(target_arch = "x86_64")] + /// + /// Returns the floating point state (FPU) from the vCPU. + /// + fn get_fpu(&self) -> Result; + #[cfg(target_arch = "x86_64")] + /// + /// Set the floating point state (FPU) of a vCPU + /// + fn set_fpu(&self, fpu: &FpuState) -> Result<()>; + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call to setup the CPUID registers. + /// + fn set_cpuid2(&self, cpuid: &[CpuIdEntry]) -> Result<()>; + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call to enable HyperV SynIC + /// + fn enable_hyperv_synic(&self) -> Result<()>; + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call to retrieve the CPUID registers. + /// + fn get_cpuid2(&self, num_entries: usize) -> Result>; + #[cfg(target_arch = "x86_64")] + /// + /// Returns the state of the LAPIC (Local Advanced Programmable Interrupt Controller). + /// + fn get_lapic(&self) -> Result; + #[cfg(target_arch = "x86_64")] + /// + /// Sets the state of the LAPIC (Local Advanced Programmable Interrupt Controller). + /// + fn set_lapic(&self, lapic: &LapicState) -> Result<()>; + #[cfg(target_arch = "x86_64")] + /// + /// Returns the model-specific registers (MSR) for this vCPU. + /// + fn get_msrs(&self, msrs: &mut Vec) -> Result; + #[cfg(target_arch = "x86_64")] + /// + /// Setup the model-specific registers (MSR) for this vCPU. + /// + fn set_msrs(&self, msrs: &[MsrEntry]) -> Result; + /// + /// Returns the vcpu's current "multiprocessing state". + /// + fn get_mp_state(&self) -> Result; + /// + /// Sets the vcpu's current "multiprocessing state". + /// + fn set_mp_state(&self, mp_state: MpState) -> Result<()>; + #[cfg(target_arch = "x86_64")] + /// + /// Let the guest know that it has been paused, which prevents from + /// potential soft lockups when being resumed. + /// + fn notify_guest_clock_paused(&self) -> Result<()> { + Ok(()) + } + /// + /// Sets debug registers to set hardware breakpoints and/or enable single step. + /// + fn set_guest_debug(&self, _addrs: &[GuestAddress], _singlestep: bool) -> Result<()> { + Err(HypervisorCpuError::SetDebugRegs(anyhow!("unimplemented"))) + } + /// + /// Sets the type of CPU to be exposed to the guest and optional features. + /// + #[cfg(target_arch = "aarch64")] + fn vcpu_init(&self, kvi: &VcpuInit) -> Result<()>; + /// + /// Gets a list of the guest registers that are supported for the + /// KVM_GET_ONE_REG/KVM_SET_ONE_REG calls. + /// + #[cfg(target_arch = "aarch64")] + fn get_reg_list(&self, reg_list: &mut RegList) -> Result<()>; + /// + /// Gets the value of a system register + /// + #[cfg(target_arch = "aarch64")] + fn get_sys_reg(&self, sys_reg: u32) -> Result; + /// + /// Configure core registers for a given CPU. + /// + #[cfg(target_arch = "aarch64")] + fn setup_regs(&self, cpu_id: u8, boot_ip: u64, fdt_start: u64) -> Result<()>; + /// + /// Check if the CPU supports PMU + /// + #[cfg(target_arch = "aarch64")] + fn has_pmu_support(&self) -> bool; + /// + /// Initialize PMU + /// + #[cfg(target_arch = "aarch64")] + fn init_pmu(&self, irq: u32) -> Result<()>; + /// + /// Retrieve the vCPU state. + /// This function is necessary to snapshot the VM + /// + fn state(&self) -> Result; + /// + /// Set the vCPU state. + /// This function is required when restoring the VM + /// + fn set_state(&self, state: &CpuState) -> Result<()>; + /// + /// Triggers the running of the current virtual CPU returning an exit reason. + /// + fn run(&self) -> std::result::Result; + #[cfg(target_arch = "x86_64")] + /// + /// Translate guest virtual address to guest physical address + /// + fn translate_gva(&self, gva: u64, flags: u64) -> Result<(u64, u32)>; + /// + /// Initialize TDX support on the vCPU + /// + #[cfg(feature = "tdx")] + fn tdx_init(&self, _hob_address: u64) -> Result<()> { + unimplemented!() + } + /// + /// Set the "immediate_exit" state + /// + fn set_immediate_exit(&self, _exit: bool) {} + #[cfg(feature = "tdx")] + /// + /// Returns the details about TDX exit reason + /// + fn get_tdx_exit_details(&mut self) -> Result { + unimplemented!() + } + #[cfg(feature = "tdx")] + /// + /// Set the status code for TDX exit + /// + fn set_tdx_status(&mut self, _status: TdxExitStatus) { + unimplemented!() + } + #[cfg(target_arch = "x86_64")] + /// + /// Return the list of initial MSR entries for a VCPU + /// + fn boot_msr_entries(&self) -> Vec; +} diff --git a/vendor/cube-hypervisor/hypervisor/src/device.rs b/vendor/cube-hypervisor/hypervisor/src/device.rs new file mode 100644 index 0000000..561e110 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/device.rs @@ -0,0 +1,28 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// Copyright 2020, ARM Limited +// + +use thiserror::Error; + +#[derive(Error, Debug)] +/// +/// Enum for device error +pub enum HypervisorDeviceError { + /// + /// Set device attribute error + /// + #[error("Failed to set device attribute: {0}")] + SetDeviceAttribute(#[source] anyhow::Error), + /// + /// Get device attribute error + /// + #[error("Failed to get device attribute: {0}")] + GetDeviceAttribute(#[source] anyhow::Error), +} diff --git a/vendor/cube-hypervisor/hypervisor/src/hypervisor.rs b/vendor/cube-hypervisor/hypervisor/src/hypervisor.rs new file mode 100644 index 0000000..08daee4 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/hypervisor.rs @@ -0,0 +1,136 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// +#[cfg(target_arch = "x86_64")] +use crate::arch::x86::CpuIdEntry; +#[cfg(feature = "tdx")] +use crate::kvm::TdxCapabilities; +use crate::vm::Vm; +use crate::HypervisorType; +use std::sync::Arc; +use thiserror::Error; + +#[derive(Error, Debug)] +/// +/// +pub enum HypervisorError { + /// + /// Hypervisor availability check error + /// + #[error("Failed to check availability of the hypervisor: {0}")] + HypervisorAvailableCheck(#[source] anyhow::Error), + /// + /// hypervisor creation error + /// + #[error("Failed to create the hypervisor: {0}")] + HypervisorCreate(#[source] anyhow::Error), + /// + /// Vm creation failure + /// + #[error("Failed to create Vm: {0}")] + VmCreate(#[source] anyhow::Error), + /// + /// Vm setup failure + /// + #[error("Failed to setup Vm: {0}")] + VmSetup(#[source] anyhow::Error), + /// + /// API version error + /// + #[error("Failed to get API Version: {0}")] + GetApiVersion(#[source] anyhow::Error), + /// + /// CpuId error + /// + #[error("Failed to get cpuid: {0}")] + GetCpuId(#[source] anyhow::Error), + /// + /// Failed to retrieve list of MSRs. + /// + #[error("Failed to get the list of supported MSRs: {0}")] + GetMsrList(#[source] anyhow::Error), + /// + /// API version is not compatible + /// + #[error("Incompatible API version")] + IncompatibleApiVersion, + /// + /// Checking extensions failed + /// + #[error("Checking extensions:{0}")] + CheckExtensions(#[source] anyhow::Error), + /// + /// Failed to retrieve TDX capabilities + /// + #[error("Failed to retrieve TDX capabilities:{0}")] + TdxCapabilities(#[source] anyhow::Error), + /// + /// Failed to set partition property + /// + #[error("Failed to set partition property:{0}")] + SetPartitionProperty(#[source] anyhow::Error), +} + +/// +/// Result type for returning from a function +/// +pub type Result = std::result::Result; + +/// +/// Trait to represent a Hypervisor +/// +/// This crate provides a hypervisor-agnostic interfaces +/// +pub trait Hypervisor: Send + Sync { + /// + /// Returns the type of the hypervisor + /// + fn hypervisor_type(&self) -> HypervisorType; + /// + /// Create a Vm using the underlying hypervisor + /// Return a hypervisor-agnostic Vm trait object + /// + fn create_vm(&self) -> Result>; + /// + /// Create a Vm of a specific type using the underlying hypervisor + /// Return a hypervisor-agnostic Vm trait object + /// + fn create_vm_with_type(&self, _vm_type: u64) -> Result> { + unreachable!() + } + #[cfg(target_arch = "x86_64")] + /// + /// Get the supported CpuID + /// + fn get_cpuid(&self) -> Result>; + /// + /// Check particular extensions if any + /// + fn check_required_extensions(&self) -> Result<()> { + Ok(()) + } + #[cfg(target_arch = "aarch64")] + /// + /// Retrieve AArch64 host maximum IPA size supported by KVM + /// + fn get_host_ipa_limit(&self) -> i32; + /// + /// Retrieve TDX capabilities + /// + #[cfg(feature = "tdx")] + fn tdx_capabilities(&self) -> Result { + unimplemented!() + } + /// + /// Get the number of supported hardware breakpoints + /// + fn get_guest_debug_hw_bps(&self) -> usize { + unimplemented!() + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/dist_regs.rs b/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/dist_regs.rs new file mode 100644 index 0000000..dba7061 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/dist_regs.rs @@ -0,0 +1,192 @@ +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use crate::arch::aarch64::gic::{Error, Result}; +use crate::device::HypervisorDeviceError; +use crate::kvm::kvm_bindings::{ + kvm_device_attr, KVM_DEV_ARM_VGIC_GRP_DIST_REGS, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, +}; +use kvm_ioctls::DeviceFd; + +/* + Distributor registers as detailed at page 456 from + https://static.docs.arm.com/ihi0069/c/IHI0069C_gic_architecture_specification.pdf. + Address offsets are relative to the Distributor base address defined +by the system memory map. Unless otherwise stated in the register description, +all GIC registers are 32-bits wide. + */ +const GICD_CTLR: u32 = 0x0; +const GICD_STATUSR: u32 = 0x0010; +const GICD_IGROUPR: u32 = 0x0080; +const GICD_ISENABLER: u32 = 0x0100; +const GICD_ICENABLER: u32 = 0x0180; +const GICD_ISPENDR: u32 = 0x0200; +const GICD_ICPENDR: u32 = 0x0280; +const GICD_ISACTIVER: u32 = 0x0300; +const GICD_ICACTIVER: u32 = 0x0380; +const GICD_IPRIORITYR: u32 = 0x0400; +const GICD_ICFGR: u32 = 0x0C00; +const GICD_IROUTER: u32 = 0x6000; + +/// This is how we represent the registers of the vgic's distributor. +/// Some of the distributor register )(i.e GICD_STATUSR) are simple +/// registers (i.e they are associated to a 32 bit value). +/// However, there are other registers that have variable lengths since +/// they dedicate some of the 32 bits to some specific interrupt. So, their length +/// depends on the number of interrupts (i.e the ones that are represented as GICD_REG) +/// in the documentation mentioned above. +struct DistReg { + /// Offset from distributor address. + base: u32, + /// Bits per interrupt. + /// Relevant for registers that DO share IRQs. + bpi: u8, + /// Length of the register. + /// Relevant for registers that DO NOT share IRQs. + length: u16, +} + +// All or at least the registers we are interested in are 32 bit, so +// we use a constant for size(u32). +const REG_SIZE: u8 = 4; + +// Creates a vgic distributor register. +macro_rules! VGIC_DIST_REG { + ($base:expr, $bpi:expr, $length:expr) => { + DistReg { + base: $base, + bpi: $bpi, + length: $length, + } + }; +} + +// List with relevant distributor registers that we will be restoring. +// Order is taken from qemu. +static VGIC_DIST_REGS: &[DistReg] = &[ + VGIC_DIST_REG!(GICD_STATUSR, 0, 4), + VGIC_DIST_REG!(GICD_ICENABLER, 1, 0), + VGIC_DIST_REG!(GICD_ISENABLER, 1, 0), + VGIC_DIST_REG!(GICD_IGROUPR, 1, 0), + VGIC_DIST_REG!(GICD_IROUTER, 64, 0), + VGIC_DIST_REG!(GICD_ICFGR, 2, 0), + VGIC_DIST_REG!(GICD_ICPENDR, 1, 0), + VGIC_DIST_REG!(GICD_ISPENDR, 1, 0), + VGIC_DIST_REG!(GICD_ICACTIVER, 1, 0), + VGIC_DIST_REG!(GICD_ISACTIVER, 1, 0), + VGIC_DIST_REG!(GICD_IPRIORITYR, 8, 0), +]; + +/// Read a distributor register. +/// +/// The destination buffer must be a local `mut` whose address is handed to +/// KVM_GET_DEVICE_ATTR; going through a `&u32` would let LLVM treat the kernel +/// write-back as a readonly-noalias violation and fold the result to 0. +fn dist_attr_get(gic: &DeviceFd, offset: u32) -> Result { + let mut val: u32 = 0; + let mut gic_dist_attr = kvm_device_attr { + group: KVM_DEV_ARM_VGIC_GRP_DIST_REGS, + attr: offset as u64, + addr: &mut val as *mut u32 as u64, + flags: 0, + }; + gic.get_device_attr(&mut gic_dist_attr).map_err(|e| { + Error::GetDeviceAttribute(HypervisorDeviceError::GetDeviceAttribute(e.into())) + })?; + Ok(val) +} + +/// Write a distributor register. +fn dist_attr_set(gic: &DeviceFd, offset: u32, val: u32) -> Result<()> { + let gic_dist_attr = kvm_device_attr { + group: KVM_DEV_ARM_VGIC_GRP_DIST_REGS, + attr: offset as u64, + addr: &val as *const u32 as u64, + flags: 0, + }; + gic.set_device_attr(&gic_dist_attr) + .map_err(|e| Error::SetDeviceAttribute(HypervisorDeviceError::SetDeviceAttribute(e.into()))) +} + +/// Get the distributor control register. +pub fn read_ctlr(gic: &DeviceFd) -> Result { + dist_attr_get(gic, GICD_CTLR) +} + +/// Set the distributor control register. +pub fn write_ctlr(gic: &DeviceFd, val: u32) -> Result<()> { + dist_attr_set(gic, GICD_CTLR, val) +} + +fn get_interrupts_num(gic: &DeviceFd) -> Result { + let mut num_irq = 0; + + let mut nr_irqs_attr = kvm_device_attr { + group: KVM_DEV_ARM_VGIC_GRP_NR_IRQS, + attr: 0, + addr: &mut num_irq as *mut u32 as u64, + flags: 0, + }; + gic.get_device_attr(&mut nr_irqs_attr).map_err(|e| { + Error::GetDeviceAttribute(HypervisorDeviceError::GetDeviceAttribute(e.into())) + })?; + Ok(num_irq) +} + +fn compute_reg_len(gic: &DeviceFd, reg: &DistReg, base: u32) -> Result { + // FIXME: + // Redefine some GIC constants to avoid the dependency on `layout` crate. + // This is temporary solution, will be fixed in future refactoring. + const LAYOUT_IRQ_BASE: u32 = 32; + + let mut end = base; + let num_irq = get_interrupts_num(gic)?; + if reg.length > 0 { + // This is the single type register (i.e one that is not DIST_X) and for which + // the bpi is 0. + // Look in the kernel for REGISTER_DESC_WITH_LENGTH. + end = base + reg.length as u32; + } + if reg.bpi > 0 { + // This is the type of register that takes into account the number of interrupts + // that the model has. It is also the type of register where + // a register relates to multiple interrupts. + end = base + (reg.bpi as u32 * (num_irq - LAYOUT_IRQ_BASE) / 8); + if reg.bpi as u32 * (num_irq - LAYOUT_IRQ_BASE) % 8 > 0 { + end += REG_SIZE as u32; + } + } + Ok(end) +} + +/// Set distributor registers of the GIC. +pub fn set_dist_regs(gic: &DeviceFd, state: &[u32]) -> Result<()> { + let mut idx = 0; + + for dreg in VGIC_DIST_REGS { + let mut base = dreg.base + REG_SIZE as u32 * dreg.bpi as u32; + let end = compute_reg_len(gic, dreg, base)?; + + while base < end { + dist_attr_set(gic, base, state[idx])?; + idx += 1; + base += REG_SIZE as u32; + } + } + Ok(()) +} +/// Get distributor registers of the GIC. +pub fn get_dist_regs(gic: &DeviceFd) -> Result> { + let mut state = Vec::new(); + + for dreg in VGIC_DIST_REGS { + let mut base = dreg.base + REG_SIZE as u32 * dreg.bpi as u32; + let end = compute_reg_len(gic, dreg, base)?; + + while base < end { + state.push(dist_attr_get(gic, base)?); + base += REG_SIZE as u32; + } + } + Ok(state) +} diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/icc_regs.rs b/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/icc_regs.rs new file mode 100644 index 0000000..ecc5626 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/icc_regs.rs @@ -0,0 +1,212 @@ +// Copyright 2022 Arm Limited (or its affiliates). All rights reserved. +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use crate::arch::aarch64::gic::{Error, Result}; +use crate::device::HypervisorDeviceError; +use crate::kvm::kvm_bindings::{ + kvm_device_attr, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS, KVM_REG_ARM64_SYSREG_CRM_MASK, + KVM_REG_ARM64_SYSREG_CRM_SHIFT, KVM_REG_ARM64_SYSREG_CRN_MASK, KVM_REG_ARM64_SYSREG_CRN_SHIFT, + KVM_REG_ARM64_SYSREG_OP0_MASK, KVM_REG_ARM64_SYSREG_OP0_SHIFT, KVM_REG_ARM64_SYSREG_OP1_MASK, + KVM_REG_ARM64_SYSREG_OP1_SHIFT, KVM_REG_ARM64_SYSREG_OP2_MASK, KVM_REG_ARM64_SYSREG_OP2_SHIFT, +}; +use kvm_ioctls::DeviceFd; + +const KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT: u32 = 32; +const KVM_DEV_ARM_VGIC_V3_MPIDR_MASK: u64 = 0xffffffff << KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT as u64; + +const ICC_CTLR_EL1_PRIBITS_SHIFT: u32 = 8; +const ICC_CTLR_EL1_PRIBITS_MASK: u32 = 7 << ICC_CTLR_EL1_PRIBITS_SHIFT; + +macro_rules! arm64_vgic_sys_reg { + ($name: tt, $op0: tt, $op1: tt, $crn: tt, $crm: tt, $op2: expr) => { + const $name: u64 = ((($op0 as u64) << KVM_REG_ARM64_SYSREG_OP0_SHIFT) + & KVM_REG_ARM64_SYSREG_OP0_MASK as u64) + | ((($op1 as u64) << KVM_REG_ARM64_SYSREG_OP1_SHIFT) + & KVM_REG_ARM64_SYSREG_OP1_MASK as u64) + | ((($crn as u64) << KVM_REG_ARM64_SYSREG_CRN_SHIFT) + & KVM_REG_ARM64_SYSREG_CRN_MASK as u64) + | ((($crm as u64) << KVM_REG_ARM64_SYSREG_CRM_SHIFT) + & KVM_REG_ARM64_SYSREG_CRM_MASK as u64) + | ((($op2 as u64) << KVM_REG_ARM64_SYSREG_OP2_SHIFT) + & KVM_REG_ARM64_SYSREG_OP2_MASK as u64); + }; +} + +macro_rules! SYS_ICC_AP0Rn_EL1 { + ($name: tt, $n: tt) => { + arm64_vgic_sys_reg!($name, 3, 0, 12, 8, (4 | $n)); + }; +} + +macro_rules! SYS_ICC_AP1Rn_EL1 { + ($name: tt, $n: tt) => { + arm64_vgic_sys_reg!($name, 3, 0, 12, 9, $n); + }; +} + +arm64_vgic_sys_reg!(SYS_ICC_SRE_EL1, 3, 0, 12, 12, 5); +arm64_vgic_sys_reg!(SYS_ICC_CTLR_EL1, 3, 0, 12, 12, 4); +arm64_vgic_sys_reg!(SYS_ICC_IGRPEN0_EL1, 3, 0, 12, 12, 6); +arm64_vgic_sys_reg!(SYS_ICC_IGRPEN1_EL1, 3, 0, 12, 12, 7); +arm64_vgic_sys_reg!(SYS_ICC_PMR_EL1, 3, 0, 4, 6, 0); +arm64_vgic_sys_reg!(SYS_ICC_BPR0_EL1, 3, 0, 12, 8, 3); +arm64_vgic_sys_reg!(SYS_ICC_BPR1_EL1, 3, 0, 12, 12, 3); +SYS_ICC_AP0Rn_EL1!(SYS_ICC_AP0R0_EL1, 0); +SYS_ICC_AP0Rn_EL1!(SYS_ICC_AP0R1_EL1, 1); +SYS_ICC_AP0Rn_EL1!(SYS_ICC_AP0R2_EL1, 2); +SYS_ICC_AP0Rn_EL1!(SYS_ICC_AP0R3_EL1, 3); +SYS_ICC_AP1Rn_EL1!(SYS_ICC_AP1R0_EL1, 0); +SYS_ICC_AP1Rn_EL1!(SYS_ICC_AP1R1_EL1, 1); +SYS_ICC_AP1Rn_EL1!(SYS_ICC_AP1R2_EL1, 2); +SYS_ICC_AP1Rn_EL1!(SYS_ICC_AP1R3_EL1, 3); + +static VGIC_ICC_REGS: &[u64] = &[ + SYS_ICC_SRE_EL1, + SYS_ICC_CTLR_EL1, + SYS_ICC_IGRPEN0_EL1, + SYS_ICC_IGRPEN1_EL1, + SYS_ICC_PMR_EL1, + SYS_ICC_BPR0_EL1, + SYS_ICC_BPR1_EL1, + SYS_ICC_AP0R0_EL1, + SYS_ICC_AP0R1_EL1, + SYS_ICC_AP0R2_EL1, + SYS_ICC_AP0R3_EL1, + SYS_ICC_AP1R0_EL1, + SYS_ICC_AP1R1_EL1, + SYS_ICC_AP1R2_EL1, + SYS_ICC_AP1R3_EL1, +]; + +/// Read an ICC register. +/// +/// `KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS` attributes go through the kvm_one_reg +/// ABI, which accesses the user buffer as a `__u64` (see `kvm_sys_reg_get_user` +/// in the kernel), so the ioctl must be backed by a full `u64` even though the +/// register is 32-bit — a `u32` buffer would make KVM write 8 bytes into 4 +/// bytes of stack. The value is converted (and validated) at the `Vec` +/// serialization boundary. The buffer must also be a local `mut` whose address +/// is handed to KVM_GET_DEVICE_ATTR: going through a shared reference would +/// let LLVM treat the kernel write-back as a readonly-noalias violation and +/// fold the result to 0 (observed with rustc 1.96, release + lto). +fn icc_attr_get(gic: &DeviceFd, offset: u64, typer: u64) -> Result { + let mut val: u64 = 0; + let mut gic_icc_attr = kvm_device_attr { + group: KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS, + attr: ((typer & KVM_DEV_ARM_VGIC_V3_MPIDR_MASK) | offset), // this needs the mpidr + addr: &mut val as *mut u64 as u64, + flags: 0, + }; + gic.get_device_attr(&mut gic_icc_attr).map_err(|e| { + Error::GetDeviceAttribute(HypervisorDeviceError::GetDeviceAttribute(e.into())) + })?; + u32::try_from(val).map_err(|_| { + Error::GetDeviceAttribute(HypervisorDeviceError::GetDeviceAttribute(anyhow::anyhow!( + "ICC register {:#x} read-back exceeds 32 bits: {:#x}", + offset, + val + ))) + }) +} + +/// Write an ICC register. +/// +/// Same `__u64` buffer requirement as [`icc_attr_get`]. +fn icc_attr_set(gic: &DeviceFd, offset: u64, typer: u64, val: u32) -> Result<()> { + let val: u64 = u64::from(val); + let gic_icc_attr = kvm_device_attr { + group: KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS, + attr: ((typer & KVM_DEV_ARM_VGIC_V3_MPIDR_MASK) | offset), // this needs the mpidr + addr: &val as *const u64 as u64, + flags: 0, + }; + gic.set_device_attr(&gic_icc_attr) + .map_err(|e| Error::SetDeviceAttribute(HypervisorDeviceError::SetDeviceAttribute(e.into()))) +} + +/// Get ICC registers. +pub fn get_icc_regs(gic: &DeviceFd, gicr_typer: &[u64]) -> Result> { + let mut state: Vec = Vec::new(); + // We need this for the ICC_APR_EL1 registers. + let mut num_priority_bits = 0; + + for ix in gicr_typer { + let i = *ix; + for icc_offset in VGIC_ICC_REGS { + if *icc_offset == SYS_ICC_CTLR_EL1 { + // calculate priority bits by reading the ctrl_el1 register. + let val = icc_attr_get(gic, *icc_offset, i)?; + // The priority bits are found in the ICC_CTLR_EL1 register (bits from 10:8). + // See page 194 from https://static.docs.arm.com/ihi0069/c/IHI0069C_gic_ + // architecture_specification.pdf. + // Citation: + // "Priority bits. Read-only and writes are ignored. The number of priority bits + // implemented, minus one." + num_priority_bits = + ((val & ICC_CTLR_EL1_PRIBITS_MASK) >> ICC_CTLR_EL1_PRIBITS_SHIFT) + 1; + state.push(val); + } + // As per ARMv8 documentation: https://static.docs.arm.com/ihi0069/c/IHI0069C_ + // gic_architecture_specification.pdf + // page 178, + // ICC_AP0R1_EL1 is only implemented in implementations that support 6 or more bits of + // priority. + // ICC_AP0R2_EL1 and ICC_AP0R3_EL1 are only implemented in implementations that support + // 7 bits of priority. + else if *icc_offset == SYS_ICC_AP0R1_EL1 || *icc_offset == SYS_ICC_AP1R1_EL1 { + if num_priority_bits >= 6 { + state.push(icc_attr_get(gic, *icc_offset, i)?); + } + } else if *icc_offset == SYS_ICC_AP0R2_EL1 + || *icc_offset == SYS_ICC_AP0R3_EL1 + || *icc_offset == SYS_ICC_AP1R2_EL1 + || *icc_offset == SYS_ICC_AP1R3_EL1 + { + if num_priority_bits == 7 { + state.push(icc_attr_get(gic, *icc_offset, i)?); + } + } else { + state.push(icc_attr_get(gic, *icc_offset, i)?); + } + } + } + Ok(state) +} + +/// Set ICC registers. +pub fn set_icc_regs(gic: &DeviceFd, gicr_typer: &[u64], state: &[u32]) -> Result<()> { + let mut num_priority_bits = 0; + let mut idx = 0; + for ix in gicr_typer { + let i = *ix; + for icc_offset in VGIC_ICC_REGS { + if *icc_offset == SYS_ICC_CTLR_EL1 { + let ctrl_el1 = state[idx]; + num_priority_bits = + ((ctrl_el1 & ICC_CTLR_EL1_PRIBITS_MASK) >> ICC_CTLR_EL1_PRIBITS_SHIFT) + 1; + } + if *icc_offset == SYS_ICC_AP0R1_EL1 || *icc_offset == SYS_ICC_AP1R1_EL1 { + if num_priority_bits >= 6 { + icc_attr_set(gic, *icc_offset, i, state[idx])?; + idx += 1; + } + continue; + } + if *icc_offset == SYS_ICC_AP0R2_EL1 + || *icc_offset == SYS_ICC_AP0R3_EL1 + || *icc_offset == SYS_ICC_AP1R2_EL1 + || *icc_offset == SYS_ICC_AP1R3_EL1 + { + if num_priority_bits == 7 { + icc_attr_set(gic, *icc_offset, i, state[idx])?; + idx += 1; + } + continue; + } + icc_attr_set(gic, *icc_offset, i, state[idx])?; + idx += 1; + } + } + Ok(()) +} diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/mod.rs b/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/mod.rs new file mode 100644 index 0000000..87bb694 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/mod.rs @@ -0,0 +1,603 @@ +// Copyright 2022 Arm Limited (or its affiliates). All rights reserved. + +mod dist_regs; +mod icc_regs; +mod redist_regs; + +use crate::arch::aarch64::gic::{Error, Result, Vgic, VgicConfig}; +use crate::device::HypervisorDeviceError; +use crate::kvm::{kvm_bindings, KvmVm}; +use crate::{CpuState, Vm}; +use dist_regs::{get_dist_regs, read_ctlr, set_dist_regs, write_ctlr}; +use icc_regs::{get_icc_regs, set_icc_regs}; +use kvm_ioctls::DeviceFd; +use redist_regs::{construct_gicr_typers, get_redist_regs, set_redist_regs}; +use serde::{Deserialize, Serialize}; +use std::any::Any; +use std::convert::TryInto; + +const GITS_CTLR: u32 = 0x0000; +const GITS_IIDR: u32 = 0x0004; +const GITS_CBASER: u32 = 0x0080; +const GITS_CWRITER: u32 = 0x0088; +const GITS_CREADR: u32 = 0x0090; +const GITS_BASER: u32 = 0x0100; + +/// Read an ITS device attribute. +/// +/// The destination buffer must be a local `mut` whose address is handed to +/// KVM_GET_DEVICE_ATTR; going through a `&u64` would let LLVM treat the kernel +/// write-back as a readonly-noalias violation and fold the result to 0. +pub fn gicv3_its_attr_get(its_device: &DeviceFd, group: u32, attr: u32) -> Result { + let mut val: u64 = 0; + let mut gicv3_its_attr = kvm_bindings::kvm_device_attr { + group, + attr: attr as u64, + addr: &mut val as *mut u64 as u64, + flags: 0, + }; + its_device + .get_device_attr(&mut gicv3_its_attr) + .map_err(|e| { + Error::GetDeviceAttribute(HypervisorDeviceError::GetDeviceAttribute(e.into())) + })?; + Ok(val) +} + +/// Write an ITS device attribute. +pub fn gicv3_its_attr_set(its_device: &DeviceFd, group: u32, attr: u32, val: u64) -> Result<()> { + let gicv3_its_attr = kvm_bindings::kvm_device_attr { + group, + attr: attr as u64, + addr: &val as *const u64 as u64, + flags: 0, + }; + its_device + .set_device_attr(&gicv3_its_attr) + .map_err(|e| Error::SetDeviceAttribute(HypervisorDeviceError::SetDeviceAttribute(e.into()))) +} + +/// Function that saves/restores ITS tables into guest RAM. +/// +/// The tables get flushed to guest RAM whenever the VM gets stopped. +pub fn gicv3_its_tables_access(its_device: &DeviceFd, save: bool) -> Result<()> { + let attr = if save { + u64::from(kvm_bindings::KVM_DEV_ARM_ITS_SAVE_TABLES) + } else { + u64::from(kvm_bindings::KVM_DEV_ARM_ITS_RESTORE_TABLES) + }; + + let init_gic_attr = kvm_bindings::kvm_device_attr { + group: kvm_bindings::KVM_DEV_ARM_VGIC_GRP_CTRL, + attr, + addr: 0, + flags: 0, + }; + its_device + .set_device_attr(&init_gic_attr) + .map_err(|e| Error::SetDeviceAttribute(HypervisorDeviceError::SetDeviceAttribute(e.into()))) +} + +pub struct KvmGicV3Its { + /// The KVM device for the GicV3 + device: DeviceFd, + + /// The KVM device for the Its device + its_device: Option, + + /// Vector holding values of GICR_TYPER for each vCPU + gicr_typers: Vec, + + /// GIC distributor address + dist_addr: u64, + + /// GIC distributor size + dist_size: u64, + + /// GIC distributors address + redists_addr: u64, + + /// GIC distributors size + redists_size: u64, + + /// GIC MSI address + msi_addr: u64, + + /// GIC MSI size + msi_size: u64, + + /// Number of CPUs handled by the device + vcpu_count: u64, +} + +#[derive(Clone, Default, Serialize, Deserialize)] +pub struct Gicv3ItsState { + dist: Vec, + rdist: Vec, + icc: Vec, + // special register that enables interrupts and affinity routing + gicd_ctlr: u32, + its_ctlr: u64, + its_iidr: u64, + its_cbaser: u64, + its_cwriter: u64, + its_creadr: u64, + its_baser: [u64; 8], +} + +impl KvmGicV3Its { + /// Device trees specific constants + pub const ARCH_GIC_V3_MAINT_IRQ: u32 = 9; + + /// Returns the GIC version of the device + fn version() -> u32 { + kvm_bindings::kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V3 + } + + /// Setup the device-specific attributes + fn init_device_attributes(&mut self, vm: &KvmVm, nr_irqs: u32) -> Result<()> { + // GicV3 part attributes + /* Setting up the distributor attribute. */ + Self::set_device_attribute( + &self.device, + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR, + u64::from(kvm_bindings::KVM_VGIC_V3_ADDR_TYPE_DIST), + &self.dist_addr as *const u64 as u64, + 0, + )?; + + /* Setting up the redistributors' attribute. */ + Self::set_device_attribute( + &self.device, + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR, + u64::from(kvm_bindings::KVM_VGIC_V3_ADDR_TYPE_REDIST), + &self.redists_addr as *const u64 as u64, + 0, + )?; + + // ITS part attributes + let mut its_device = kvm_bindings::kvm_create_device { + type_: kvm_bindings::kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_ITS, + fd: 0, + flags: 0, + }; + + let its_fd = vm + .create_device(&mut its_device) + .map_err(Error::CreateGic)?; + + // We know vm is KvmVm + let its_fd = its_fd.to_kvm().unwrap(); + + Self::set_device_attribute( + &its_fd, + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR, + u64::from(kvm_bindings::KVM_VGIC_ITS_ADDR_TYPE), + &self.msi_addr as *const u64 as u64, + 0, + )?; + + Self::set_device_attribute( + &its_fd, + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_CTRL, + u64::from(kvm_bindings::KVM_DEV_ARM_VGIC_CTRL_INIT), + 0, + 0, + )?; + + self.its_device = Some(its_fd); + + /* We need to tell the kernel how many irqs to support with this vgic. + * See the `layout` module for details. + */ + let nr_irqs_ptr = &nr_irqs as *const u32; + Self::set_device_attribute( + &self.device, + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_NR_IRQS, + 0, + nr_irqs_ptr as u64, + 0, + )?; + + /* Finalize the GIC. + * See https://code.woboq.org/linux/linux/virt/kvm/arm/vgic/vgic-kvm-device.c.html#211. + */ + Self::set_device_attribute( + &self.device, + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_CTRL, + u64::from(kvm_bindings::KVM_DEV_ARM_VGIC_CTRL_INIT), + 0, + 0, + ) + } + + /// Create a KVM Vgic device + fn create_device(vm: &KvmVm) -> Result { + let mut gic_device = kvm_bindings::kvm_create_device { + type_: Self::version(), + fd: 0, + flags: 0, + }; + + let device_fd = vm + .create_device(&mut gic_device) + .map_err(Error::CreateGic)?; + + // We know for sure this is a KVM fd + Ok(device_fd.to_kvm().unwrap()) + } + + /// Set a GIC device attribute + fn set_device_attribute( + device: &DeviceFd, + group: u32, + attr: u64, + addr: u64, + flags: u32, + ) -> Result<()> { + let attr = kvm_bindings::kvm_device_attr { + flags, + group, + attr, + addr, + }; + device.set_device_attr(&attr).map_err(|e| { + Error::SetDeviceAttribute(HypervisorDeviceError::SetDeviceAttribute(e.into())) + }) + } + + /// Method to initialize the GIC device + #[allow(clippy::new_ret_no_self)] + pub fn new(vm: &dyn Vm, config: VgicConfig) -> Result { + // This is inside KVM module + let vm = vm.as_any().downcast_ref::().expect("Wrong VM type?"); + + let vgic = Self::create_device(vm)?; + + let mut gic_device = KvmGicV3Its { + device: vgic, + its_device: None, + gicr_typers: vec![0; config.vcpu_count.try_into().unwrap()], + dist_addr: config.dist_addr, + dist_size: config.dist_size, + redists_addr: config.redists_addr, + redists_size: config.redists_size, + msi_addr: config.msi_addr, + msi_size: config.msi_size, + vcpu_count: config.vcpu_count, + }; + + gic_device.init_device_attributes(vm, config.nr_irqs)?; + + Ok(gic_device) + } +} + +impl Vgic for KvmGicV3Its { + fn fdt_compatibility(&self) -> &str { + "arm,gic-v3" + } + + fn msi_compatible(&self) -> bool { + true + } + + fn msi_compatibility(&self) -> &str { + "arm,gic-v3-its" + } + + fn fdt_maint_irq(&self) -> u32 { + KvmGicV3Its::ARCH_GIC_V3_MAINT_IRQ + } + + fn vcpu_count(&self) -> u64 { + self.vcpu_count + } + + fn device_properties(&self) -> [u64; 4] { + [ + self.dist_addr, + self.dist_size, + self.redists_addr, + self.redists_size, + ] + } + + fn msi_properties(&self) -> [u64; 2] { + [self.msi_addr, self.msi_size] + } + + fn set_gicr_typers(&mut self, vcpu_states: &[CpuState]) { + let gicr_typers = construct_gicr_typers(vcpu_states); + self.gicr_typers = gicr_typers; + } + + fn as_any_concrete_mut(&mut self) -> &mut dyn Any { + self + } + + /// Save the state of GICv3ITS. + fn state(&self) -> Result { + let gicr_typers = self.gicr_typers.clone(); + + let gicd_ctlr = read_ctlr(&self.device)?; + + let dist_state = get_dist_regs(&self.device)?; + + let rdist_state = get_redist_regs(&self.device, &gicr_typers)?; + + let icc_state = get_icc_regs(&self.device, &gicr_typers)?; + + let mut its_baser_state: [u64; 8] = [0; 8]; + for i in 0..8 { + its_baser_state[i as usize] = gicv3_its_attr_get( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_BASER + i * 8, + )?; + } + + let its_ctlr_state = gicv3_its_attr_get( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_CTLR, + )?; + + let its_cbaser_state = gicv3_its_attr_get( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_CBASER, + )?; + + let its_creadr_state = gicv3_its_attr_get( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_CREADR, + )?; + + let its_cwriter_state = gicv3_its_attr_get( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_CWRITER, + )?; + + let its_iidr_state = gicv3_its_attr_get( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_IIDR, + )?; + + Ok(Gicv3ItsState { + dist: dist_state, + rdist: rdist_state, + icc: icc_state, + gicd_ctlr, + its_ctlr: its_ctlr_state, + its_iidr: its_iidr_state, + its_cbaser: its_cbaser_state, + its_cwriter: its_cwriter_state, + its_creadr: its_creadr_state, + its_baser: its_baser_state, + }) + } + + /// Restore the state of GICv3ITS. + fn set_state(&mut self, state: &Gicv3ItsState) -> Result<()> { + let gicr_typers = self.gicr_typers.clone(); + + write_ctlr(&self.device, state.gicd_ctlr)?; + + set_dist_regs(&self.device, &state.dist)?; + + set_redist_regs(&self.device, &gicr_typers, &state.rdist)?; + + set_icc_regs(&self.device, &gicr_typers, &state.icc)?; + + //Restore GICv3ITS registers + gicv3_its_attr_set( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_IIDR, + state.its_iidr, + )?; + + gicv3_its_attr_set( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_CBASER, + state.its_cbaser, + )?; + + gicv3_its_attr_set( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_CREADR, + state.its_creadr, + )?; + + gicv3_its_attr_set( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_CWRITER, + state.its_cwriter, + )?; + + for i in 0..8 { + gicv3_its_attr_set( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_BASER + i * 8, + state.its_baser[i as usize], + )?; + } + + // Restore ITS tables + gicv3_its_tables_access(self.its_device.as_ref().unwrap(), false)?; + + gicv3_its_attr_set( + self.its_device.as_ref().unwrap(), + kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ITS_REGS, + GITS_CTLR, + state.its_ctlr, + ) + } + + /// Saves GIC internal data tables into RAM, including: + /// - RDIST pending tables + /// - ITS tables into guest RAM. + fn save_data_tables(&self) -> Result<()> { + // Flash RDIST pending tables + let init_gic_attr = kvm_bindings::kvm_device_attr { + group: kvm_bindings::KVM_DEV_ARM_VGIC_GRP_CTRL, + attr: u64::from(kvm_bindings::KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES), + addr: 0, + flags: 0, + }; + self.device.set_device_attr(&init_gic_attr).map_err(|e| { + Error::SetDeviceAttribute(HypervisorDeviceError::SetDeviceAttribute(e.into())) + })?; + // Flush ITS tables to guest RAM. + gicv3_its_tables_access(self.its_device.as_ref().unwrap(), true) + } +} + +#[cfg(test)] +mod tests { + use crate::aarch64::gic::{ + get_dist_regs, get_icc_regs, get_redist_regs, set_dist_regs, set_icc_regs, set_redist_regs, + }; + use crate::arch::aarch64::gic::VgicConfig; + use crate::kvm::KvmGicV3Its; + + fn create_test_vgic_config() -> VgicConfig { + VgicConfig { + vcpu_count: 1, + dist_addr: 0x0900_0000 - 0x01_0000, + dist_size: 0x01_0000, + // dist_addr - redists_size + redists_addr: 0x0900_0000 - 0x01_0000 - 0x02_0000, + redists_size: 0x02_0000, + // redists_addr - msi_size + msi_addr: 0x0900_0000 - 0x01_0000 - 0x02_0000 - 0x02_0000, + msi_size: 0x02_0000, + nr_irqs: 256, + } + } + + #[test] + fn test_create_gic() { + let hv = crate::new().unwrap(); + let vm = hv.create_vm().unwrap(); + + assert!(KvmGicV3Its::new(&*vm, create_test_vgic_config()).is_ok()); + } + + #[test] + fn test_get_set_dist_regs() { + let hv = crate::new().unwrap(); + let vm = hv.create_vm().unwrap(); + let _ = vm.create_vcpu(0, None).unwrap(); + let gic = KvmGicV3Its::new(&*vm, create_test_vgic_config()).expect("Cannot create gic"); + + let res = get_dist_regs(&gic.device); + assert!(res.is_ok()); + let state = res.unwrap(); + assert_eq!(state.len(), 568); + + let res = set_dist_regs(&gic.device, &state); + assert!(res.is_ok()); + } + + #[test] + fn test_get_set_redist_regs() { + let hv = crate::new().unwrap(); + let vm = hv.create_vm().unwrap(); + let _ = vm.create_vcpu(0, None).unwrap(); + let gic = KvmGicV3Its::new(&*vm, create_test_vgic_config()).expect("Cannot create gic"); + + let gicr_typer = vec![123]; + let res = get_redist_regs(&gic.device, &gicr_typer); + assert!(res.is_ok()); + let state = res.unwrap(); + println!("{}", state.len()); + assert!(state.len() == 24); + + assert!(set_redist_regs(&gic.device, &gicr_typer, &state).is_ok()); + } + + #[test] + fn test_get_set_icc_regs() { + let hv = crate::new().unwrap(); + let vm = hv.create_vm().unwrap(); + let _ = vm.create_vcpu(0, None).unwrap(); + let gic = KvmGicV3Its::new(&*vm, create_test_vgic_config()).expect("Cannot create gic"); + + let gicr_typer = vec![123]; + let res = get_icc_regs(&gic.device, &gicr_typer); + assert!(res.is_ok()); + let state = res.unwrap(); + println!("{}", state.len()); + assert!(state.len() == 9); + + assert!(set_icc_regs(&gic.device, &gicr_typer, &state).is_ok()); + } + + // Regression test for the vgic state-save miscompile: rustc 1.96+ + // (release, lto) folded the KVM_GET_DEVICE_ATTR read-backs to zero, + // producing an all-zero GIC snapshot that breaks pause->resume + // (GICv3ITS SetDeviceAttribute EINVAL). Needs /dev/kvm; run in + // release to exercise the optimized build. + #[test] + fn test_gic_state_save_nonzero_roundtrip() { + use crate::arch::aarch64::gic::Vgic; + + let hv = crate::new().unwrap(); + let vm = hv.create_vm().unwrap(); + vm.create_vcpu(0, None).unwrap(); + let mut vgic = KvmGicV3Its::new(&*vm, create_test_vgic_config()).unwrap(); + + // Every bank must read back non-zero (all-zero == miscompiled). + let dist = get_dist_regs(&vgic.device).unwrap(); + let rdist = get_redist_regs(&vgic.device, &[123]).unwrap(); + let icc = get_icc_regs(&vgic.device, &[123]).unwrap(); + let its = vgic.state().unwrap(); + assert!(dist.iter().any(|&v| v != 0), "dist all zero — miscompiled?"); + assert!( + rdist.iter().any(|&v| v != 0), + "rdist all zero — miscompiled?" + ); + assert!(icc.iter().any(|&v| v != 0), "icc all zero — miscompiled?"); + assert_ne!(its.its_iidr, 0, "GITS_IIDR zero — miscompiled?"); + + // Exact-value round-trip on the writable ICC registers (the bank + // that broke pause->resume): IGRPEN0/1, PMR, BPR0/1. PMR is masked + // to the 5 implemented priority bits for kernel portability. + let mut icc_want = icc.clone(); + icc_want[2..7].copy_from_slice(&[1, 1, 0xA8, 3, 3]); + set_icc_regs(&vgic.device, &[123], &icc_want).unwrap(); + assert_eq!( + &get_icc_regs(&vgic.device, &[123]).unwrap()[2..7], + &[1u32, 1, 0xA8, 3, 3] + ); + + // Aggregate round-trip through the restore path (set_state). + vgic.set_state(&its).unwrap(); + assert_ne!( + vgic.state().unwrap().its_iidr, + 0, + "GITS_IIDR zero after set_state" + ); + } + + #[test] + fn test_save_data_tables() { + let hv = crate::new().unwrap(); + let vm = hv.create_vm().unwrap(); + let _ = vm.create_vcpu(0, None).unwrap(); + let gic = vm + .create_vgic(create_test_vgic_config()) + .expect("Cannot create gic"); + + assert!(gic.lock().unwrap().save_data_tables().is_ok()); + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/redist_regs.rs b/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/redist_regs.rs new file mode 100644 index 0000000..6febbd1 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/redist_regs.rs @@ -0,0 +1,226 @@ +// Copyright 2022 Arm Limited (or its affiliates). All rights reserved. +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use crate::arch::aarch64::gic::{Error, Result}; +use crate::device::HypervisorDeviceError; +use crate::kvm::kvm_bindings::{ + kvm_device_attr, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS, KVM_REG_ARM64, KVM_REG_ARM64_SYSREG, + KVM_REG_ARM64_SYSREG_OP0_MASK, KVM_REG_ARM64_SYSREG_OP0_SHIFT, KVM_REG_ARM64_SYSREG_OP2_MASK, + KVM_REG_ARM64_SYSREG_OP2_SHIFT, KVM_REG_SIZE_U64, +}; +use crate::kvm::Register; +use crate::kvm::VcpuKvmState; +use crate::CpuState; +use kvm_ioctls::DeviceFd; + +// Relevant redistributor registers that we want to save/restore. +const GICR_CTLR: u32 = 0x0000; +const GICR_STATUSR: u32 = 0x0010; +const GICR_WAKER: u32 = 0x0014; +const GICR_PROPBASER: u32 = 0x0070; +const GICR_PENDBASER: u32 = 0x0078; + +/* SGI and PPI Redistributor registers, offsets from RD_base */ +/* + * Redistributor frame offsets from RD_base which is actually SZ_ + */ +const GICR_SGI_OFFSET: u32 = 0x0001_0000; +const GICR_IGROUPR0: u32 = GICR_SGI_OFFSET + 0x0080; +const GICR_ICENABLER0: u32 = GICR_SGI_OFFSET + 0x0180; +const GICR_ISENABLER0: u32 = GICR_SGI_OFFSET + 0x0100; +const GICR_ISPENDR0: u32 = GICR_SGI_OFFSET + 0x0200; +const GICR_ICPENDR0: u32 = GICR_SGI_OFFSET + 0x0280; +const GICR_ISACTIVER0: u32 = GICR_SGI_OFFSET + 0x0300; +const GICR_ICACTIVER0: u32 = GICR_SGI_OFFSET + 0x0380; +const GICR_IPRIORITYR0: u32 = GICR_SGI_OFFSET + 0x0400; +const GICR_ICFGR0: u32 = GICR_SGI_OFFSET + 0x0C00; + +const KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT: u32 = 32; +const KVM_DEV_ARM_VGIC_V3_MPIDR_MASK: u64 = 0xffffffff << KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT as u64; + +const KVM_ARM64_SYSREG_MPIDR_EL1: u64 = KVM_REG_ARM64 as u64 + | KVM_REG_SIZE_U64 as u64 + | KVM_REG_ARM64_SYSREG as u64 + | (((3_u64) << KVM_REG_ARM64_SYSREG_OP0_SHIFT) & KVM_REG_ARM64_SYSREG_OP0_MASK as u64) + | (((5_u64) << KVM_REG_ARM64_SYSREG_OP2_SHIFT) & KVM_REG_ARM64_SYSREG_OP2_MASK as u64); + +/// This is how we represent the registers of a distributor. +/// It is relrvant their offset from the base address of the +/// distributor. +/// Each register has a different number +/// of bits_per_irq and is therefore variable length. +/// First 32 interrupts (0-32) are private to each CPU (SGIs and PPIs). +/// and so we save the first irq to identify between the type of the interrupt +/// that the respective register deals with. +struct RdistReg { + /// Offset from distributor address. + base: u32, + /// Length of the register. + length: u8, +} + +// All or at least the registers we are interested in are 32 bit, so +// we use a constant for size(u32). +const REG_SIZE: u8 = 4; + +// Creates a vgic redistributor register. +macro_rules! VGIC_RDIST_REG { + ($base:expr, $len:expr) => { + RdistReg { + base: $base, + length: $len, + } + }; +} + +// List with relevant distributor registers that we will be restoring. +static VGIC_RDIST_REGS: &[RdistReg] = &[ + VGIC_RDIST_REG!(GICR_STATUSR, 4), + VGIC_RDIST_REG!(GICR_WAKER, 4), + VGIC_RDIST_REG!(GICR_PROPBASER, 8), + VGIC_RDIST_REG!(GICR_PENDBASER, 8), + VGIC_RDIST_REG!(GICR_CTLR, 4), +]; + +// List with relevant distributor registers that we will be restoring. +static VGIC_SGI_REGS: &[RdistReg] = &[ + VGIC_RDIST_REG!(GICR_IGROUPR0, 4), + VGIC_RDIST_REG!(GICR_ICENABLER0, 4), + VGIC_RDIST_REG!(GICR_ISENABLER0, 4), + VGIC_RDIST_REG!(GICR_ICFGR0, 8), + VGIC_RDIST_REG!(GICR_ICPENDR0, 4), + VGIC_RDIST_REG!(GICR_ISPENDR0, 4), + VGIC_RDIST_REG!(GICR_ICACTIVER0, 4), + VGIC_RDIST_REG!(GICR_ISACTIVER0, 4), + VGIC_RDIST_REG!(GICR_IPRIORITYR0, 32), +]; + +/// Read a redistributor register. +/// +/// The destination buffer must be a local `mut` whose address is handed to +/// KVM_GET_DEVICE_ATTR; going through a `&u32` would let LLVM treat the kernel +/// write-back as a readonly-noalias violation and fold the result to 0. +fn redist_attr_get(gic: &DeviceFd, offset: u32, typer: u64) -> Result { + let mut val: u32 = 0; + let mut gic_redist_attr = kvm_device_attr { + group: KVM_DEV_ARM_VGIC_GRP_REDIST_REGS, + attr: (typer & KVM_DEV_ARM_VGIC_V3_MPIDR_MASK) | (offset as u64), // this needs the mpidr + addr: &mut val as *mut u32 as u64, + flags: 0, + }; + gic.get_device_attr(&mut gic_redist_attr).map_err(|e| { + Error::GetDeviceAttribute(HypervisorDeviceError::GetDeviceAttribute(e.into())) + })?; + Ok(val) +} + +/// Write a redistributor register. +fn redist_attr_set(gic: &DeviceFd, offset: u32, typer: u64, val: u32) -> Result<()> { + let gic_redist_attr = kvm_device_attr { + group: KVM_DEV_ARM_VGIC_GRP_REDIST_REGS, + attr: (typer & KVM_DEV_ARM_VGIC_V3_MPIDR_MASK) | (offset as u64), // this needs the mpidr + addr: &val as *const u32 as u64, + flags: 0, + }; + gic.set_device_attr(&gic_redist_attr) + .map_err(|e| Error::SetDeviceAttribute(HypervisorDeviceError::SetDeviceAttribute(e.into()))) +} + +fn access_redists_aux( + gic: &DeviceFd, + gicr_typer: &[u64], + state: &mut Vec, + reg_list: &[RdistReg], + idx: &mut usize, + set: bool, +) -> Result<()> { + for i in gicr_typer { + for rdreg in reg_list { + let mut base = rdreg.base; + let end = base + rdreg.length as u32; + + while base < end { + if set { + redist_attr_set(gic, base, *i, state[*idx])?; + *idx += 1; + } else { + state.push(redist_attr_get(gic, base, *i)?); + } + base += REG_SIZE as u32; + } + } + } + Ok(()) +} + +/// Get redistributor registers. +pub fn get_redist_regs(gic: &DeviceFd, gicr_typer: &[u64]) -> Result> { + let mut state = Vec::new(); + let mut idx: usize = 0; + access_redists_aux( + gic, + gicr_typer, + &mut state, + VGIC_RDIST_REGS, + &mut idx, + false, + )?; + + access_redists_aux(gic, gicr_typer, &mut state, VGIC_SGI_REGS, &mut idx, false)?; + Ok(state) +} + +/// Set redistributor registers. +pub fn set_redist_regs(gic: &DeviceFd, gicr_typer: &[u64], state: &[u32]) -> Result<()> { + let mut idx: usize = 0; + let mut mut_state = state.to_owned(); + access_redists_aux( + gic, + gicr_typer, + &mut mut_state, + VGIC_RDIST_REGS, + &mut idx, + true, + )?; + access_redists_aux( + gic, + gicr_typer, + &mut mut_state, + VGIC_SGI_REGS, + &mut idx, + true, + ) +} + +pub fn construct_gicr_typers(vcpu_states: &[CpuState]) -> Vec { + /* Pre-construct the GICR_TYPER: + * For our implementation: + * Top 32 bits are the affinity value of the associated CPU + * CommonLPIAff == 01 (redistributors with same Aff3 share LPI table) + * Processor_Number == CPU index starting from 0 + * DPGS == 0 (GICR_CTLR.DPG* not supported) + * Last == 1 if this is the last redistributor in a series of + * contiguous redistributor pages + * DirectLPI == 0 (direct injection of LPIs not supported) + * VLPIS == 0 (virtual LPIs not supported) + * PLPIS == 0 (physical LPIs not supported) + */ + let mut gicr_typers: Vec = Vec::new(); + for (index, state) in vcpu_states.iter().enumerate() { + let state: VcpuKvmState = state.clone().into(); + let last = (index == vcpu_states.len() - 1) as u64; + // state.sys_regs is a big collection of system registers, including MIPDR_EL1 + let mpidr: Vec = state + .sys_regs + .into_iter() + .filter(|reg| reg.id == KVM_ARM64_SYSREG_MPIDR_EL1) + .collect(); + //calculate affinity + let mut cpu_affid = mpidr[0].addr & 1095233437695; + cpu_affid = ((cpu_affid & 0xFF00000000) >> 8) | (cpu_affid & 0xFFFFFF); + gicr_typers.push((cpu_affid << 32) | (1 << 24) | (index as u64) << 8 | (last << 4)); + } + + gicr_typers +} diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/mod.rs b/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/mod.rs new file mode 100644 index 0000000..7712508 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/mod.rs @@ -0,0 +1,122 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// + +pub mod gic; + +use crate::kvm::{KvmError, KvmResult}; +use kvm_bindings::{ + kvm_mp_state, kvm_one_reg, kvm_regs, KVM_REG_ARM_COPROC_MASK, KVM_REG_ARM_CORE, + KVM_REG_SIZE_MASK, KVM_REG_SIZE_U32, KVM_REG_SIZE_U64, +}; +pub use kvm_bindings::{ + kvm_one_reg as Register, kvm_regs as StandardRegisters, kvm_vcpu_init as VcpuInit, RegList, +}; +use serde::{Deserialize, Serialize}; +pub use {kvm_ioctls::Cap, kvm_ioctls::Kvm}; + +// This macro gets the offset of a structure (i.e `str`) member (i.e `field`) without having +// an instance of that structure. +#[macro_export] +macro_rules! offset__of { + ($str:ty, $($field:ident)+) => ({ + let tmp: std::mem::MaybeUninit<$str> = std::mem::MaybeUninit::uninit(); + let base = tmp.as_ptr(); + + // Avoid warnings when nesting `unsafe` blocks. + #[allow(unused_unsafe)] + // SAFETY: The pointer is valid and aligned, just not initialised. Using `addr_of` ensures + // that we don't actually read from `base` (which would be UB) nor create an intermediate + // reference. + let member = unsafe { core::ptr::addr_of!((*base).$($field)*) } as *const u8; + + // Avoid warnings when nesting `unsafe` blocks. + #[allow(unused_unsafe)] + // SAFETY: The two pointers are within the same allocated object `tmp`. All requirements + // from offset_from are upheld. + unsafe { member.offset_from(base as *const u8) as usize } + }); +} + +// Following are macros that help with getting the ID of a aarch64 core register. +// The core register are represented by the user_pt_regs structure. Look for it in +// arch/arm64/include/uapi/asm/ptrace.h. + +// Get the ID of a core register +#[macro_export] +macro_rules! arm64_core_reg_id { + ($size: tt, $offset: tt) => { + // The core registers of an arm64 machine are represented + // in kernel by the `kvm_regs` structure. This structure is a + // mix of 32, 64 and 128 bit fields: + // struct kvm_regs { + // struct user_pt_regs regs; + // + // __u64 sp_el1; + // __u64 elr_el1; + // + // __u64 spsr[KVM_NR_SPSR]; + // + // struct user_fpsimd_state fp_regs; + // }; + // struct user_pt_regs { + // __u64 regs[31]; + // __u64 sp; + // __u64 pc; + // __u64 pstate; + // }; + // The id of a core register can be obtained like this: + // offset = id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE). Thus, + // id = KVM_REG_ARM64 | KVM_REG_SIZE_U64/KVM_REG_SIZE_U32/KVM_REG_SIZE_U128 | KVM_REG_ARM_CORE | offset + KVM_REG_ARM64 as u64 + | u64::from(KVM_REG_ARM_CORE) + | $size + | (($offset / mem::size_of::()) as u64) + }; +} + +/// Specifies whether a particular register is a system register or not. +/// The kernel splits the registers on aarch64 in core registers and system registers. +/// So, below we get the system registers by checking that they are not core registers. +/// +/// # Arguments +/// +/// * `regid` - The index of the register we are checking. +pub fn is_system_register(regid: u64) -> bool { + if (regid & KVM_REG_ARM_COPROC_MASK as u64) == KVM_REG_ARM_CORE as u64 { + return false; + } + + let size = regid & KVM_REG_SIZE_MASK; + + assert!( + !(size != KVM_REG_SIZE_U32 && size != KVM_REG_SIZE_U64), + "Unexpected register size for system register {}", + size + ); + + true +} + +pub fn check_required_kvm_extensions(kvm: &Kvm) -> KvmResult<()> { + if !kvm.check_extension(Cap::SignalMsi) { + return Err(KvmError::CapabilityMissing(Cap::SignalMsi)); + } + if !kvm.check_extension(Cap::OneReg) { + return Err(KvmError::CapabilityMissing(Cap::OneReg)); + } + Ok(()) +} + +#[derive(Clone, Default, Serialize, Deserialize)] +pub struct VcpuKvmState { + pub mp_state: kvm_mp_state, + pub core_regs: kvm_regs, + pub sys_regs: Vec, +} diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/mod.rs b/vendor/cube-hypervisor/hypervisor/src/kvm/mod.rs new file mode 100644 index 0000000..d6c2b8b --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/kvm/mod.rs @@ -0,0 +1,2403 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// + +#[cfg(target_arch = "aarch64")] +use crate::aarch64::gic::KvmGicV3Its; +#[cfg(target_arch = "aarch64")] +pub use crate::aarch64::{ + check_required_kvm_extensions, gic::Gicv3ItsState as GicState, is_system_register, VcpuInit, + VcpuKvmState, +}; +#[cfg(target_arch = "aarch64")] +use crate::arch::aarch64::gic::{Vgic, VgicConfig}; +use crate::cpu; +use crate::hypervisor; +use crate::vec_with_array_field; +use crate::vm::{self, InterruptSourceConfig, VmOps}; +use crate::HypervisorType; +#[cfg(target_arch = "aarch64")] +use crate::{arm64_core_reg_id, offset__of}; +use kvm_ioctls::{NoDatamatch, VcpuFd, VmFd}; +use std::any::Any; +use std::collections::HashMap; +#[cfg(target_arch = "aarch64")] +use std::convert::TryInto; +#[cfg(target_arch = "x86_64")] +use std::fs::File; +#[cfg(target_arch = "x86_64")] +use std::os::unix::io::AsRawFd; +#[cfg(feature = "tdx")] +use std::os::unix::io::RawFd; +use std::result; +#[cfg(target_arch = "x86_64")] +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Mutex; +use std::sync::{Arc, RwLock}; +use vmm_sys_util::eventfd::EventFd; +// x86_64 dependencies +#[cfg(target_arch = "x86_64")] +pub mod x86_64; +#[cfg(target_arch = "x86_64")] +use crate::arch::x86::{ + CpuIdEntry, FpuState, LapicState, MsrEntry, SpecialRegisters, StandardRegisters, XsaveState, + NUM_IOAPIC_PINS, +}; +#[cfg(target_arch = "x86_64")] +use crate::ClockData; +use crate::{ + CpuState, IoEventAddress, IrqRoutingEntry, MpState, UserMemoryRegion, + USER_MEMORY_REGION_LOG_DIRTY, USER_MEMORY_REGION_READ, USER_MEMORY_REGION_WRITE, +}; +#[cfg(target_arch = "aarch64")] +use aarch64::{RegList, Register, StandardRegisters}; +#[cfg(target_arch = "x86_64")] +use kvm_bindings::{ + kvm_enable_cap, kvm_msr_entry, MsrList, KVM_CAP_HYPERV_SYNIC, KVM_CAP_SPLIT_IRQCHIP, + KVM_GUESTDBG_USE_HW_BP, +}; +#[cfg(target_arch = "x86_64")] +use x86_64::check_required_kvm_extensions; +#[cfg(target_arch = "x86_64")] +pub use x86_64::{CpuId, ExtendedControlRegisters, MsrEntries, VcpuKvmState}; +// aarch64 dependencies +#[cfg(target_arch = "aarch64")] +pub mod aarch64; +pub use kvm_bindings; +#[cfg(feature = "tdx")] +pub use kvm_bindings::kvm_run__bindgen_ty_1; +pub use kvm_bindings::{ + kvm_clock_data, kvm_create_device, kvm_device_type_KVM_DEV_TYPE_VFIO, kvm_guest_debug, + kvm_irq_routing, kvm_irq_routing_entry, kvm_irqfd, kvm_mp_state, kvm_userspace_memory_region, + KVMIO, KVM_GUESTDBG_ENABLE, KVM_GUESTDBG_SINGLESTEP, KVM_IRQ_ROUTING_IRQCHIP, + KVM_IRQ_ROUTING_MSI, KVM_MEM_LOG_DIRTY_PAGES, KVM_MEM_READONLY, KVM_MSI_VALID_DEVID, +}; +#[cfg(target_arch = "aarch64")] +use kvm_bindings::{ + kvm_regs, user_fpsimd_state, user_pt_regs, KVM_GUESTDBG_USE_HW, KVM_NR_SPSR, KVM_REG_ARM64, + KVM_REG_ARM64_SYSREG, KVM_REG_ARM64_SYSREG_CRM_MASK, KVM_REG_ARM64_SYSREG_CRN_MASK, + KVM_REG_ARM64_SYSREG_OP0_MASK, KVM_REG_ARM64_SYSREG_OP1_MASK, KVM_REG_ARM64_SYSREG_OP2_MASK, + KVM_REG_ARM_CORE, KVM_REG_SIZE_U128, KVM_REG_SIZE_U32, KVM_REG_SIZE_U64, +}; +pub use kvm_ioctls; +pub use kvm_ioctls::{Cap, Kvm}; +#[cfg(target_arch = "aarch64")] +use std::mem; +use thiserror::Error; +use vfio_ioctls::VfioDeviceFd; +#[cfg(feature = "tdx")] +use vmm_sys_util::{ioctl::ioctl_with_val, ioctl_ioc_nr, ioctl_iowr_nr}; +/// +/// Export generically-named wrappers of kvm-bindings for Unix-based platforms +/// +pub use { + kvm_bindings::kvm_create_device as CreateDevice, kvm_bindings::kvm_device_attr as DeviceAttr, + kvm_bindings::kvm_run, kvm_bindings::kvm_vcpu_events as VcpuEvents, kvm_ioctls::VcpuExit, +}; + +#[cfg(target_arch = "x86_64")] +const KVM_CAP_SGX_ATTRIBUTE: u32 = 196; + +#[cfg(feature = "tdx")] +const KVM_EXIT_TDX: u32 = 35; +#[cfg(feature = "tdx")] +const TDG_VP_VMCALL_GET_QUOTE: u64 = 0x10002; +#[cfg(feature = "tdx")] +const TDG_VP_VMCALL_SETUP_EVENT_NOTIFY_INTERRUPT: u64 = 0x10004; +#[cfg(feature = "tdx")] +const TDG_VP_VMCALL_SUCCESS: u64 = 0; +#[cfg(feature = "tdx")] +const TDG_VP_VMCALL_INVALID_OPERAND: u64 = 0x8000000000000000; + +#[cfg(feature = "tdx")] +ioctl_iowr_nr!(KVM_MEMORY_ENCRYPT_OP, KVMIO, 0xba, std::os::raw::c_ulong); + +#[cfg(feature = "tdx")] +#[repr(u32)] +enum TdxCommand { + Capabilities = 0, + InitVm, + InitVcpu, + InitMemRegion, + Finalize, +} + +#[cfg(feature = "tdx")] +pub enum TdxExitDetails { + GetQuote, + SetupEventNotifyInterrupt, +} + +#[cfg(feature = "tdx")] +pub enum TdxExitStatus { + Success, + InvalidOperand, +} + +#[cfg(feature = "tdx")] +const TDX_MAX_NR_CPUID_CONFIGS: usize = 6; + +#[cfg(feature = "tdx")] +#[repr(C)] +#[derive(Debug, Default)] +pub struct TdxCpuidConfig { + pub leaf: u32, + pub sub_leaf: u32, + pub eax: u32, + pub ebx: u32, + pub ecx: u32, + pub edx: u32, +} + +#[cfg(feature = "tdx")] +#[repr(C)] +#[derive(Debug, Default)] +pub struct TdxCapabilities { + pub attrs_fixed0: u64, + pub attrs_fixed1: u64, + pub xfam_fixed0: u64, + pub xfam_fixed1: u64, + pub nr_cpuid_configs: u32, + pub padding: u32, + pub cpuid_configs: [TdxCpuidConfig; TDX_MAX_NR_CPUID_CONFIGS], +} + +#[cfg(feature = "tdx")] +#[derive(Copy, Clone)] +pub struct KvmTdxExit { + pub type_: u32, + pub pad: u32, + pub u: KvmTdxExitU, +} + +#[cfg(feature = "tdx")] +#[repr(C)] +#[derive(Copy, Clone)] +pub union KvmTdxExitU { + pub vmcall: KvmTdxExitVmcall, +} + +#[cfg(feature = "tdx")] +#[repr(C)] +#[derive(Debug, Default, Copy, Clone, PartialEq)] +pub struct KvmTdxExitVmcall { + pub type_: u64, + pub subfunction: u64, + pub reg_mask: u64, + pub in_r12: u64, + pub in_r13: u64, + pub in_r14: u64, + pub in_r15: u64, + pub in_rbx: u64, + pub in_rdi: u64, + pub in_rsi: u64, + pub in_r8: u64, + pub in_r9: u64, + pub in_rdx: u64, + pub status_code: u64, + pub out_r11: u64, + pub out_r12: u64, + pub out_r13: u64, + pub out_r14: u64, + pub out_r15: u64, + pub out_rbx: u64, + pub out_rdi: u64, + pub out_rsi: u64, + pub out_r8: u64, + pub out_r9: u64, + pub out_rdx: u64, +} + +impl From for UserMemoryRegion { + fn from(region: kvm_userspace_memory_region) -> Self { + let mut flags = USER_MEMORY_REGION_READ; + if region.flags & KVM_MEM_READONLY == 0 { + flags |= USER_MEMORY_REGION_WRITE; + } + if region.flags & KVM_MEM_LOG_DIRTY_PAGES != 0 { + flags |= USER_MEMORY_REGION_LOG_DIRTY; + } + + UserMemoryRegion { + slot: region.slot, + guest_phys_addr: region.guest_phys_addr, + memory_size: region.memory_size, + userspace_addr: region.userspace_addr, + flags, + } + } +} + +impl From for kvm_userspace_memory_region { + fn from(region: UserMemoryRegion) -> Self { + assert!( + region.flags & USER_MEMORY_REGION_READ != 0, + "KVM mapped memory is always readable" + ); + + let mut flags = 0; + if region.flags & USER_MEMORY_REGION_WRITE == 0 { + flags |= KVM_MEM_READONLY; + } + if region.flags & USER_MEMORY_REGION_LOG_DIRTY != 0 { + flags |= KVM_MEM_LOG_DIRTY_PAGES; + } + + kvm_userspace_memory_region { + slot: region.slot, + guest_phys_addr: region.guest_phys_addr, + memory_size: region.memory_size, + userspace_addr: region.userspace_addr, + flags, + } + } +} + +impl From for MpState { + fn from(s: kvm_mp_state) -> Self { + MpState::Kvm(s) + } +} + +impl From for kvm_mp_state { + fn from(ms: MpState) -> Self { + match ms { + MpState::Kvm(s) => s, + /* Needed in case other hypervisors are enabled */ + #[allow(unreachable_patterns)] + _ => panic!("CpuState is not valid"), + } + } +} + +impl From for IoEventAddress { + fn from(a: kvm_ioctls::IoEventAddress) -> Self { + match a { + kvm_ioctls::IoEventAddress::Pio(x) => Self::Pio(x), + kvm_ioctls::IoEventAddress::Mmio(x) => Self::Mmio(x), + } + } +} + +impl From for kvm_ioctls::IoEventAddress { + fn from(a: IoEventAddress) -> Self { + match a { + IoEventAddress::Pio(x) => Self::Pio(x), + IoEventAddress::Mmio(x) => Self::Mmio(x), + } + } +} + +impl From for CpuState { + fn from(s: VcpuKvmState) -> Self { + CpuState::Kvm(s) + } +} + +impl From for VcpuKvmState { + fn from(s: CpuState) -> Self { + match s { + CpuState::Kvm(s) => s, + /* Needed in case other hypervisors are enabled */ + #[allow(unreachable_patterns)] + _ => panic!("CpuState is not valid"), + } + } +} + +#[cfg(target_arch = "x86_64")] +impl From for ClockData { + fn from(d: kvm_clock_data) -> Self { + ClockData::Kvm(d) + } +} + +#[cfg(target_arch = "x86_64")] +impl From for kvm_clock_data { + fn from(ms: ClockData) -> Self { + match ms { + ClockData::Kvm(s) => s, + /* Needed in case other hypervisors are enabled */ + #[allow(unreachable_patterns)] + _ => panic!("CpuState is not valid"), + } + } +} + +impl From for IrqRoutingEntry { + fn from(s: kvm_irq_routing_entry) -> Self { + IrqRoutingEntry::Kvm(s) + } +} + +impl From for kvm_irq_routing_entry { + fn from(e: IrqRoutingEntry) -> Self { + match e { + IrqRoutingEntry::Kvm(e) => e, + /* Needed in case other hypervisors are enabled */ + #[allow(unreachable_patterns)] + _ => panic!("IrqRoutingEntry is not valid"), + } + } +} + +struct KvmDirtyLogSlot { + slot: u32, + guest_phys_addr: u64, + memory_size: u64, + userspace_addr: u64, +} + +/// Wrapper over KVM VM ioctls. +pub struct KvmVm { + fd: Arc, + #[cfg(target_arch = "x86_64")] + msrs: Vec, + dirty_log_slots: Arc>>, +} + +impl KvmVm { + /// + /// Creates an emulated device in the kernel. + /// + /// See the documentation for `KVM_CREATE_DEVICE`. + fn create_device(&self, device: &mut CreateDevice) -> vm::Result { + let device_fd = self + .fd + .create_device(device) + .map_err(|e| vm::HypervisorVmError::CreateDevice(e.into()))?; + Ok(VfioDeviceFd::new_from_kvm(device_fd)) + } + /// Checks if a particular `Cap` is available. + fn check_extension(&self, c: Cap) -> bool { + self.fd.check_extension(c) + } +} + +/// +/// Implementation of Vm trait for KVM +/// Example: +/// #[cfg(feature = "kvm")] +/// extern crate hypervisor +/// let kvm = hypervisor::kvm::KvmHypervisor::new().unwrap(); +/// let hypervisor: Arc = Arc::new(kvm); +/// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); +/// vm.set/get().unwrap() +/// +impl vm::Vm for KvmVm { + #[cfg(target_arch = "x86_64")] + /// + /// Sets the address of the one-page region in the VM's address space. + /// + fn set_identity_map_address(&self, address: u64) -> vm::Result<()> { + self.fd + .set_identity_map_address(address) + .map_err(|e| vm::HypervisorVmError::SetIdentityMapAddress(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// Sets the address of the three-page region in the VM's address space. + /// + fn set_tss_address(&self, offset: usize) -> vm::Result<()> { + self.fd + .set_tss_address(offset) + .map_err(|e| vm::HypervisorVmError::SetTssAddress(e.into())) + } + /// + /// Creates an in-kernel interrupt controller. + /// + fn create_irq_chip(&self) -> vm::Result<()> { + self.fd + .create_irq_chip() + .map_err(|e| vm::HypervisorVmError::CreateIrq(e.into())) + } + /// + /// Registers an event that will, when signaled, trigger the `gsi` IRQ. + /// + fn register_irqfd(&self, fd: &EventFd, gsi: u32) -> vm::Result<()> { + self.fd + .register_irqfd(fd, gsi) + .map_err(|e| vm::HypervisorVmError::RegisterIrqFd(e.into())) + } + /// + /// Unregisters an event that will, when signaled, trigger the `gsi` IRQ. + /// + fn unregister_irqfd(&self, fd: &EventFd, gsi: u32) -> vm::Result<()> { + self.fd + .unregister_irqfd(fd, gsi) + .map_err(|e| vm::HypervisorVmError::UnregisterIrqFd(e.into())) + } + /// + /// Creates a VcpuFd object from a vcpu RawFd. + /// + fn create_vcpu( + &self, + id: u8, + vm_ops: Option>, + ) -> vm::Result> { + let fd = self + .fd + .create_vcpu(id as u64) + .map_err(|e| vm::HypervisorVmError::CreateVcpu(e.into()))?; + let vcpu = KvmVcpu { + fd: Arc::new(Mutex::new(fd)), + #[cfg(target_arch = "x86_64")] + msrs: self.msrs.clone(), + vm_ops, + #[cfg(target_arch = "x86_64")] + hyperv_synic: AtomicBool::new(false), + }; + Ok(Arc::new(vcpu)) + } + #[cfg(target_arch = "aarch64")] + /// + /// Creates a virtual GIC device. + /// + fn create_vgic(&self, config: VgicConfig) -> vm::Result>> { + let gic_device = KvmGicV3Its::new(self, config) + .map_err(|e| vm::HypervisorVmError::CreateVgic(anyhow!("Vgic error {:?}", e)))?; + Ok(Arc::new(Mutex::new(gic_device))) + } + /// + /// Registers an event to be signaled whenever a certain address is written to. + /// + fn register_ioevent( + &self, + fd: &EventFd, + addr: &IoEventAddress, + datamatch: Option, + ) -> vm::Result<()> { + let addr = &kvm_ioctls::IoEventAddress::from(*addr); + if let Some(dm) = datamatch { + match dm { + vm::DataMatch::DataMatch32(kvm_dm32) => self + .fd + .register_ioevent(fd, addr, kvm_dm32) + .map_err(|e| vm::HypervisorVmError::RegisterIoEvent(e.into())), + vm::DataMatch::DataMatch64(kvm_dm64) => self + .fd + .register_ioevent(fd, addr, kvm_dm64) + .map_err(|e| vm::HypervisorVmError::RegisterIoEvent(e.into())), + } + } else { + self.fd + .register_ioevent(fd, addr, NoDatamatch) + .map_err(|e| vm::HypervisorVmError::RegisterIoEvent(e.into())) + } + } + /// + /// Unregisters an event from a certain address it has been previously registered to. + /// + fn unregister_ioevent(&self, fd: &EventFd, addr: &IoEventAddress) -> vm::Result<()> { + let addr = &kvm_ioctls::IoEventAddress::from(*addr); + self.fd + .unregister_ioevent(fd, addr, NoDatamatch) + .map_err(|e| vm::HypervisorVmError::UnregisterIoEvent(e.into())) + } + + /// + /// Constructs a routing entry + /// + fn make_routing_entry(&self, gsi: u32, config: &InterruptSourceConfig) -> IrqRoutingEntry { + match &config { + InterruptSourceConfig::MsiIrq(cfg) => { + let mut kvm_route = kvm_irq_routing_entry { + gsi, + type_: KVM_IRQ_ROUTING_MSI, + ..Default::default() + }; + + kvm_route.u.msi.address_lo = cfg.low_addr; + kvm_route.u.msi.address_hi = cfg.high_addr; + kvm_route.u.msi.data = cfg.data; + + if self.check_extension(crate::kvm::Cap::MsiDevid) { + // On AArch64, there is limitation on the range of the 'devid', + // it can not be greater than 65536 (the max of u16). + // + // BDF can not be used directly, because 'segment' is in high + // 16 bits. The layout of the u32 BDF is: + // |---- 16 bits ----|-- 8 bits --|-- 5 bits --|-- 3 bits --| + // | segment | bus | device | function | + // + // Now that we support 1 bus only in a segment, we can build a + // 'devid' by replacing the 'bus' bits with the low 8 bits of + // 'segment' data. + // This way we can resolve the range checking problem and give + // different `devid` to all the devices. Limitation is that at + // most 256 segments can be supported. + // + let modified_devid = (cfg.devid & 0x00ff_0000) >> 8 | cfg.devid & 0xff; + + kvm_route.flags = KVM_MSI_VALID_DEVID; + kvm_route.u.msi.__bindgen_anon_1.devid = modified_devid; + } + kvm_route.into() + } + InterruptSourceConfig::LegacyIrq(cfg) => { + let mut kvm_route = kvm_irq_routing_entry { + gsi, + type_: KVM_IRQ_ROUTING_IRQCHIP, + ..Default::default() + }; + kvm_route.u.irqchip.irqchip = cfg.irqchip; + kvm_route.u.irqchip.pin = cfg.pin; + + kvm_route.into() + } + } + } + + /// + /// Sets the GSI routing table entries, overwriting any previously set + /// entries, as per the `KVM_SET_GSI_ROUTING` ioctl. + /// + fn set_gsi_routing(&self, entries: &[IrqRoutingEntry]) -> vm::Result<()> { + let mut irq_routing = + vec_with_array_field::(entries.len()); + irq_routing[0].nr = entries.len() as u32; + irq_routing[0].flags = 0; + let entries: Vec = entries + .iter() + .map(|entry| match entry { + IrqRoutingEntry::Kvm(e) => *e, + #[allow(unreachable_patterns)] + _ => panic!("IrqRoutingEntry type is wrong"), + }) + .collect(); + + // SAFETY: irq_routing initialized with entries.len() and now it is being turned into + // entries_slice with entries.len() again. It is guaranteed to be large enough to hold + // everything from entries. + unsafe { + let entries_slice: &mut [kvm_irq_routing_entry] = + irq_routing[0].entries.as_mut_slice(entries.len()); + entries_slice.copy_from_slice(&entries); + } + + self.fd + .set_gsi_routing(&irq_routing[0]) + .map_err(|e| vm::HypervisorVmError::SetGsiRouting(e.into())) + } + /// + /// Creates a memory region structure that can be used with {create/remove}_user_memory_region + /// + fn make_user_memory_region( + &self, + slot: u32, + guest_phys_addr: u64, + memory_size: u64, + userspace_addr: u64, + readonly: bool, + log_dirty_pages: bool, + ) -> UserMemoryRegion { + kvm_userspace_memory_region { + slot, + guest_phys_addr, + memory_size, + userspace_addr, + flags: if readonly { KVM_MEM_READONLY } else { 0 } + | if log_dirty_pages { + KVM_MEM_LOG_DIRTY_PAGES + } else { + 0 + }, + } + .into() + } + /// + /// Creates a guest physical memory region. + /// + fn create_user_memory_region(&self, user_memory_region: UserMemoryRegion) -> vm::Result<()> { + let mut region: kvm_userspace_memory_region = user_memory_region.into(); + + if (region.flags & KVM_MEM_LOG_DIRTY_PAGES) != 0 { + if (region.flags & KVM_MEM_READONLY) != 0 { + return Err(vm::HypervisorVmError::CreateUserMemory(anyhow!( + "Error creating regions with both 'dirty-pages-log' and 'read-only'." + ))); + } + + // Keep track of the regions that need dirty pages log + self.dirty_log_slots.write().unwrap().insert( + region.slot, + KvmDirtyLogSlot { + slot: region.slot, + guest_phys_addr: region.guest_phys_addr, + memory_size: region.memory_size, + userspace_addr: region.userspace_addr, + }, + ); + + // Always create guest physical memory region without `KVM_MEM_LOG_DIRTY_PAGES`. + // For regions that need this flag, dirty pages log will be turned on in `start_dirty_log`. + region.flags = 0; + } + + // SAFETY: Safe because guest regions are guaranteed not to overlap. + unsafe { + self.fd + .set_user_memory_region(region) + .map_err(|e| vm::HypervisorVmError::CreateUserMemory(e.into())) + } + } + /// + /// Removes a guest physical memory region. + /// + fn remove_user_memory_region(&self, user_memory_region: UserMemoryRegion) -> vm::Result<()> { + let mut region: kvm_userspace_memory_region = user_memory_region.into(); + + // Remove the corresponding entry from "self.dirty_log_slots" if needed + self.dirty_log_slots.write().unwrap().remove(®ion.slot); + + // Setting the size to 0 means "remove" + region.memory_size = 0; + // SAFETY: Safe because guest regions are guaranteed not to overlap. + unsafe { + self.fd + .set_user_memory_region(region) + .map_err(|e| vm::HypervisorVmError::RemoveUserMemory(e.into())) + } + } + /// + /// Returns the preferred CPU target type which can be emulated by KVM on underlying host. + /// + #[cfg(target_arch = "aarch64")] + fn get_preferred_target(&self, kvi: &mut VcpuInit) -> vm::Result<()> { + self.fd + .get_preferred_target(kvi) + .map_err(|e| vm::HypervisorVmError::GetPreferredTarget(e.into())) + } + #[cfg(target_arch = "x86_64")] + fn enable_split_irq(&self) -> vm::Result<()> { + // Create split irqchip + // Only the local APIC is emulated in kernel, both PICs and IOAPIC + // are not. + let mut cap = kvm_enable_cap { + cap: KVM_CAP_SPLIT_IRQCHIP, + ..Default::default() + }; + cap.args[0] = NUM_IOAPIC_PINS as u64; + self.fd + .enable_cap(&cap) + .map_err(|e| vm::HypervisorVmError::EnableSplitIrq(e.into()))?; + Ok(()) + } + #[cfg(target_arch = "x86_64")] + fn enable_sgx_attribute(&self, file: File) -> vm::Result<()> { + let mut cap = kvm_enable_cap { + cap: KVM_CAP_SGX_ATTRIBUTE, + ..Default::default() + }; + cap.args[0] = file.as_raw_fd() as u64; + self.fd + .enable_cap(&cap) + .map_err(|e| vm::HypervisorVmError::EnableSgxAttribute(e.into()))?; + Ok(()) + } + /// Retrieve guest clock. + #[cfg(target_arch = "x86_64")] + fn get_clock(&self) -> vm::Result { + Ok(self + .fd + .get_clock() + .map_err(|e| vm::HypervisorVmError::GetClock(e.into()))? + .into()) + } + /// Set guest clock. + #[cfg(target_arch = "x86_64")] + fn set_clock(&self, data: &ClockData) -> vm::Result<()> { + let data = (*data).into(); + self.fd + .set_clock(&data) + .map_err(|e| vm::HypervisorVmError::SetClock(e.into())) + } + /// Create a device that is used for passthrough + fn create_passthrough_device(&self) -> vm::Result { + let mut vfio_dev = kvm_create_device { + type_: kvm_device_type_KVM_DEV_TYPE_VFIO, + fd: 0, + flags: 0, + }; + + self.create_device(&mut vfio_dev) + .map_err(|e| vm::HypervisorVmError::CreatePassthroughDevice(e.into())) + } + /// + /// Start logging dirty pages + /// + fn start_dirty_log(&self) -> vm::Result<()> { + let dirty_log_slots = self.dirty_log_slots.read().unwrap(); + for (_, s) in dirty_log_slots.iter() { + let region = kvm_userspace_memory_region { + slot: s.slot, + guest_phys_addr: s.guest_phys_addr, + memory_size: s.memory_size, + userspace_addr: s.userspace_addr, + flags: KVM_MEM_LOG_DIRTY_PAGES, + }; + // SAFETY: Safe because guest regions are guaranteed not to overlap. + unsafe { + self.fd + .set_user_memory_region(region) + .map_err(|e| vm::HypervisorVmError::StartDirtyLog(e.into()))?; + } + } + + Ok(()) + } + + /// + /// Stop logging dirty pages + /// + fn stop_dirty_log(&self) -> vm::Result<()> { + let dirty_log_slots = self.dirty_log_slots.read().unwrap(); + for (_, s) in dirty_log_slots.iter() { + let region = kvm_userspace_memory_region { + slot: s.slot, + guest_phys_addr: s.guest_phys_addr, + memory_size: s.memory_size, + userspace_addr: s.userspace_addr, + flags: 0, + }; + // SAFETY: Safe because guest regions are guaranteed not to overlap. + unsafe { + self.fd + .set_user_memory_region(region) + .map_err(|e| vm::HypervisorVmError::StartDirtyLog(e.into()))?; + } + } + + Ok(()) + } + + /// + /// Get dirty pages bitmap (one bit per page) + /// + fn get_dirty_log(&self, slot: u32, _base_gpa: u64, memory_size: u64) -> vm::Result> { + self.fd + .get_dirty_log(slot, memory_size as usize) + .map_err(|e| vm::HypervisorVmError::GetDirtyLog(e.into())) + } + + /// + /// Initialize TDX for this VM + /// + #[cfg(feature = "tdx")] + fn tdx_init(&self, cpuid: &[CpuIdEntry], max_vcpus: u32) -> vm::Result<()> { + use std::io::{Error, ErrorKind}; + let cpuid: Vec = + cpuid.iter().map(|e| (*e).into()).collect(); + let kvm_cpuid = kvm_bindings::CpuId::from_entries(&cpuid).map_err(|_| { + vm::HypervisorVmError::InitializeTdx(Error::new( + ErrorKind::Other, + "failed to allocate CpuId", + )) + })?; + + #[repr(C)] + struct TdxInitVm { + max_vcpus: u32, + tsc_khz: u32, + attributes: u64, + cpuid: u64, + mrconfigid: [u64; 6], + mrowner: [u64; 6], + mrownerconfig: [u64; 6], + reserved: [u64; 43], + } + let data = TdxInitVm { + max_vcpus, + tsc_khz: 0, + attributes: 0, + cpuid: kvm_cpuid.as_fam_struct_ptr() as u64, + mrconfigid: [0; 6], + mrowner: [0; 6], + mrownerconfig: [0; 6], + reserved: [0; 43], + }; + + tdx_command( + &self.fd.as_raw_fd(), + TdxCommand::InitVm, + 0, + &data as *const _ as u64, + ) + .map_err(vm::HypervisorVmError::InitializeTdx) + } + + /// + /// Finalize the TDX setup for this VM + /// + #[cfg(feature = "tdx")] + fn tdx_finalize(&self) -> vm::Result<()> { + tdx_command(&self.fd.as_raw_fd(), TdxCommand::Finalize, 0, 0) + .map_err(vm::HypervisorVmError::FinalizeTdx) + } + + /// + /// Initialize memory regions for the TDX VM + /// + #[cfg(feature = "tdx")] + fn tdx_init_memory_region( + &self, + host_address: u64, + guest_address: u64, + size: u64, + measure: bool, + ) -> vm::Result<()> { + #[repr(C)] + struct TdxInitMemRegion { + host_address: u64, + guest_address: u64, + pages: u64, + } + let data = TdxInitMemRegion { + host_address, + guest_address, + pages: size / 4096, + }; + + tdx_command( + &self.fd.as_raw_fd(), + TdxCommand::InitMemRegion, + u32::from(measure), + &data as *const _ as u64, + ) + .map_err(vm::HypervisorVmError::InitMemRegionTdx) + } + /// Downcast to the underlying KvmVm type + fn as_any(&self) -> &dyn Any { + self + } +} + +#[cfg(feature = "tdx")] +fn tdx_command( + fd: &RawFd, + command: TdxCommand, + metadata: u32, + data: u64, +) -> std::result::Result<(), std::io::Error> { + #[repr(C)] + struct TdxIoctlCmd { + command: TdxCommand, + metadata: u32, + data: u64, + } + let cmd = TdxIoctlCmd { + command, + metadata, + data, + }; + // SAFETY: FFI call. All input parameters are valid. + let ret = unsafe { + ioctl_with_val( + fd, + KVM_MEMORY_ENCRYPT_OP(), + &cmd as *const TdxIoctlCmd as std::os::raw::c_ulong, + ) + }; + + if ret < 0 { + return Err(std::io::Error::last_os_error()); + } + Ok(()) +} + +/// Wrapper over KVM system ioctls. +pub struct KvmHypervisor { + kvm: Kvm, + hypervisor_type: HypervisorType, +} + +impl KvmHypervisor { + #[cfg(target_arch = "x86_64")] + /// + /// Retrieve the list of MSRs supported by the hypervisor. + /// + fn get_msr_list(&self) -> hypervisor::Result { + self.kvm + .get_msr_index_list() + .map_err(|e| hypervisor::HypervisorError::GetMsrList(e.into())) + } +} + +/// Enum for KVM related error +#[derive(Debug, Error)] +pub enum KvmError { + #[error("Capability missing: {0:?}")] + CapabilityMissing(Cap), +} +pub type KvmResult = result::Result; + +#[cfg(target_arch = "x86_64")] +const MSR_PVM_VCPU_STRUCT: u32 = 0x4b56_4df1; + +impl KvmHypervisor { + /// Create a hypervisor based on Kvm + #[allow(clippy::new_ret_no_self)] + pub fn new() -> hypervisor::Result> { + let kvm_obj = Kvm::new().map_err(|e| hypervisor::HypervisorError::VmCreate(e.into()))?; + let api_version = kvm_obj.get_api_version(); + + if api_version != kvm_bindings::KVM_API_VERSION as i32 { + return Err(hypervisor::HypervisorError::IncompatibleApiVersion); + } + + #[cfg_attr(not(target_arch = "x86_64"), allow(unused_mut))] + let mut hypervisor_type = HypervisorType::Kvm; + + #[cfg(target_arch = "x86_64")] + if kvm_obj + .get_msr_index_list() + .map(|msrs| msrs.as_slice().contains(&MSR_PVM_VCPU_STRUCT)) + .unwrap_or(false) + { + hypervisor_type = HypervisorType::KvmPvm; + } + + Ok(Arc::new(KvmHypervisor { + kvm: kvm_obj, + hypervisor_type, + })) + } + /// Check if the hypervisor is available + pub fn is_available() -> hypervisor::Result { + match std::fs::metadata("/dev/kvm") { + Ok(_) => Ok(true), + Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(false), + Err(err) => Err(hypervisor::HypervisorError::HypervisorAvailableCheck( + err.into(), + )), + } + } +} +/// Implementation of Hypervisor trait for KVM +/// Example: +/// #[cfg(feature = "kvm")] +/// extern crate hypervisor +/// let kvm = hypervisor::kvm::KvmHypervisor::new().unwrap(); +/// let hypervisor: Arc = Arc::new(kvm); +/// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); +/// +impl hypervisor::Hypervisor for KvmHypervisor { + /// + /// Returns the type of the hypervisor + /// + fn hypervisor_type(&self) -> HypervisorType { + self.hypervisor_type + } + + /// Create a KVM vm object of a specific VM type and return the object as Vm trait object + /// Example + /// # extern crate hypervisor; + /// # use hypervisor::KvmHypervisor; + /// use hypervisor::KvmVm; + /// let hypervisor = KvmHypervisor::new().unwrap(); + /// let vm = hypervisor.create_vm_with_type(KvmVmType::LegacyVm).unwrap() + /// + fn create_vm_with_type(&self, vm_type: u64) -> hypervisor::Result> { + let fd: VmFd; + loop { + match self.kvm.create_vm_with_type(vm_type) { + Ok(res) => fd = res, + Err(e) => { + if e.errno() == libc::EINTR { + // If the error returned is EINTR, which means the + // ioctl has been interrupted, we have to retry as + // this can't be considered as a regular error. + continue; + } else { + return Err(hypervisor::HypervisorError::VmCreate(e.into())); + } + } + } + break; + } + + let vm_fd = Arc::new(fd); + + #[cfg(target_arch = "x86_64")] + { + let msr_list = self.get_msr_list()?; + let num_msrs = msr_list.as_fam_struct_ref().nmsrs as usize; + let mut msrs: Vec = vec![ + MsrEntry { + ..Default::default() + }; + num_msrs + ]; + let indices = msr_list.as_slice(); + for (pos, index) in indices.iter().enumerate() { + msrs[pos].index = *index; + } + + Ok(Arc::new(KvmVm { + fd: vm_fd, + msrs, + dirty_log_slots: Arc::new(RwLock::new(HashMap::new())), + })) + } + + #[cfg(target_arch = "aarch64")] + { + Ok(Arc::new(KvmVm { + fd: vm_fd, + dirty_log_slots: Arc::new(RwLock::new(HashMap::new())), + })) + } + } + + /// Create a KVM vm object and return the object as Vm trait object + /// Example + /// # extern crate hypervisor; + /// # use hypervisor::KvmHypervisor; + /// use hypervisor::KvmVm; + /// let hypervisor = KvmHypervisor::new().unwrap(); + /// let vm = hypervisor.create_vm().unwrap() + /// + fn create_vm(&self) -> hypervisor::Result> { + #[allow(unused_mut)] + let mut vm_type: u64 = 0; // Create with default platform type + + // When KVM supports Cap::ArmVmIPASize, it is better to get the IPA + // size from the host and use that when creating the VM, which may + // avoid unnecessary VM creation failures. + #[cfg(target_arch = "aarch64")] + if self.kvm.check_extension(Cap::ArmVmIPASize) { + vm_type = self.kvm.get_host_ipa_limit().try_into().unwrap(); + } + + self.create_vm_with_type(vm_type) + } + + fn check_required_extensions(&self) -> hypervisor::Result<()> { + check_required_kvm_extensions(&self.kvm) + .map_err(|e| hypervisor::HypervisorError::CheckExtensions(e.into())) + } + + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call to get the system supported CPUID values. + /// + fn get_cpuid(&self) -> hypervisor::Result> { + let kvm_cpuid = self + .kvm + .get_supported_cpuid(kvm_bindings::KVM_MAX_CPUID_ENTRIES) + .map_err(|e| hypervisor::HypervisorError::GetCpuId(e.into()))?; + + let v = kvm_cpuid.as_slice().iter().map(|e| (*e).into()).collect(); + + Ok(v) + } + + #[cfg(target_arch = "aarch64")] + /// + /// Retrieve AArch64 host maximum IPA size supported by KVM. + /// + fn get_host_ipa_limit(&self) -> i32 { + self.kvm.get_host_ipa_limit() + } + + /// + /// Retrieve TDX capabilities + /// + #[cfg(feature = "tdx")] + fn tdx_capabilities(&self) -> hypervisor::Result { + let data = TdxCapabilities { + nr_cpuid_configs: TDX_MAX_NR_CPUID_CONFIGS as u32, + ..Default::default() + }; + + tdx_command( + &self.kvm.as_raw_fd(), + TdxCommand::Capabilities, + 0, + &data as *const _ as u64, + ) + .map_err(|e| hypervisor::HypervisorError::TdxCapabilities(e.into()))?; + + Ok(data) + } + + /// + /// Get the number of supported hardware breakpoints + /// + fn get_guest_debug_hw_bps(&self) -> usize { + #[cfg(target_arch = "x86_64")] + { + 4 + } + #[cfg(target_arch = "aarch64")] + { + self.kvm.get_guest_debug_hw_bps() as usize + } + } +} +/// Vcpu struct for KVM +pub struct KvmVcpu { + fd: Arc>, + #[cfg(target_arch = "x86_64")] + msrs: Vec, + vm_ops: Option>, + #[cfg(target_arch = "x86_64")] + hyperv_synic: AtomicBool, +} +/// Implementation of Vcpu trait for KVM +/// Example: +/// #[cfg(feature = "kvm")] +/// extern crate hypervisor +/// let kvm = hypervisor::kvm::KvmHypervisor::new().unwrap(); +/// let hypervisor: Arc = Arc::new(kvm); +/// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); +/// let vcpu = vm.create_vcpu(0, None).unwrap(); +/// vcpu.get/set().unwrap() +/// +impl cpu::Vcpu for KvmVcpu { + #[cfg(target_arch = "x86_64")] + /// + /// Returns the vCPU general purpose registers. + /// + fn get_regs(&self) -> cpu::Result { + Ok(self + .fd + .lock() + .unwrap() + .get_regs() + .map_err(|e| cpu::HypervisorCpuError::GetStandardRegs(e.into()))? + .into()) + } + /// + /// Returns the vCPU general purpose registers. + /// The `KVM_GET_REGS` ioctl is not available on AArch64, `KVM_GET_ONE_REG` + /// is used to get registers one by one. + /// + #[cfg(target_arch = "aarch64")] + fn get_regs(&self) -> cpu::Result { + let mut state: StandardRegisters = kvm_regs::default(); + let mut off = offset__of!(user_pt_regs, regs); + // There are 31 user_pt_regs: + // https://elixir.free-electrons.com/linux/v4.14.174/source/arch/arm64/include/uapi/asm/ptrace.h#L72 + // These actually are the general-purpose registers of the Armv8-a + // architecture (i.e x0-x30 if used as a 64bit register or w0-30 when used as a 32bit register). + for i in 0..31 { + let mut bytes = [0_u8; 8]; + self.fd + .lock() + .unwrap() + .get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U64, off), &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetCoreRegister(e.into()))?; + state.regs.regs[i] = u64::from_le_bytes(bytes); + off += std::mem::size_of::(); + } + + // We are now entering the "Other register" section of the ARMv8-a architecture. + // First one, stack pointer. + let off = offset__of!(user_pt_regs, sp); + let mut bytes = [0_u8; 8]; + self.fd + .lock() + .unwrap() + .get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U64, off), &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetCoreRegister(e.into()))?; + state.regs.sp = u64::from_le_bytes(bytes); + + // Second one, the program counter. + let off = offset__of!(user_pt_regs, pc); + let mut bytes = [0_u8; 8]; + self.fd + .lock() + .unwrap() + .get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U64, off), &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetCoreRegister(e.into()))?; + state.regs.pc = u64::from_le_bytes(bytes); + + // Next is the processor state. + let off = offset__of!(user_pt_regs, pstate); + let mut bytes = [0_u8; 8]; + self.fd + .lock() + .unwrap() + .get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U64, off), &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetCoreRegister(e.into()))?; + state.regs.pstate = u64::from_le_bytes(bytes); + + // The stack pointer associated with EL1 + let off = offset__of!(kvm_regs, sp_el1); + let mut bytes = [0_u8; 8]; + self.fd + .lock() + .unwrap() + .get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U64, off), &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetCoreRegister(e.into()))?; + state.sp_el1 = u64::from_le_bytes(bytes); + + // Exception Link Register for EL1, when taking an exception to EL1, this register + // holds the address to which to return afterwards. + let off = offset__of!(kvm_regs, elr_el1); + let mut bytes = [0_u8; 8]; + self.fd + .lock() + .unwrap() + .get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U64, off), &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetCoreRegister(e.into()))?; + state.elr_el1 = u64::from_le_bytes(bytes); + + // Saved Program Status Registers, there are 5 of them used in the kernel. + let mut off = offset__of!(kvm_regs, spsr); + for i in 0..KVM_NR_SPSR as usize { + let mut bytes = [0_u8; 8]; + self.fd + .lock() + .unwrap() + .get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U64, off), &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetCoreRegister(e.into()))?; + state.spsr[i] = u64::from_le_bytes(bytes); + off += std::mem::size_of::(); + } + + // Now moving on to floating point registers which are stored in the user_fpsimd_state in the kernel: + // https://elixir.free-electrons.com/linux/v4.9.62/source/arch/arm64/include/uapi/asm/kvm.h#L53 + let mut off = offset__of!(kvm_regs, fp_regs) + offset__of!(user_fpsimd_state, vregs); + for i in 0..32 { + let mut bytes = [0_u8; 16]; + self.fd + .lock() + .unwrap() + .get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U128, off), &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetCoreRegister(e.into()))?; + state.fp_regs.vregs[i] = u128::from_le_bytes(bytes); + off += mem::size_of::(); + } + + // Floating-point Status Register + let off = offset__of!(kvm_regs, fp_regs) + offset__of!(user_fpsimd_state, fpsr); + let mut bytes = [0_u8; 4]; + self.fd + .lock() + .unwrap() + .get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U32, off), &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetCoreRegister(e.into()))?; + state.fp_regs.fpsr = u32::from_le_bytes(bytes); + + // Floating-point Control Register + let off = offset__of!(kvm_regs, fp_regs) + offset__of!(user_fpsimd_state, fpcr); + let mut bytes = [0_u8; 4]; + self.fd + .lock() + .unwrap() + .get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U32, off), &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetCoreRegister(e.into()))?; + state.fp_regs.fpcr = u32::from_le_bytes(bytes); + Ok(state) + } + #[cfg(target_arch = "x86_64")] + /// + /// Sets the vCPU general purpose registers using the `KVM_SET_REGS` ioctl. + /// + fn set_regs(&self, regs: &StandardRegisters) -> cpu::Result<()> { + let regs = (*regs).into(); + self.fd + .lock() + .unwrap() + .set_regs(®s) + .map_err(|e| cpu::HypervisorCpuError::SetStandardRegs(e.into())) + } + + /// + /// Sets the vCPU general purpose registers. + /// The `KVM_SET_REGS` ioctl is not available on AArch64, `KVM_SET_ONE_REG` + /// is used to set registers one by one. + /// + #[cfg(target_arch = "aarch64")] + fn set_regs(&self, state: &StandardRegisters) -> cpu::Result<()> { + // The function follows the exact identical order from `state`. Look there + // for some additional info on registers. + let mut off = offset__of!(user_pt_regs, regs); + for i in 0..31 { + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U64, off), + &state.regs.regs[i].to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + off += std::mem::size_of::(); + } + + let off = offset__of!(user_pt_regs, sp); + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U64, off), + &state.regs.sp.to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + + let off = offset__of!(user_pt_regs, pc); + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U64, off), + &state.regs.pc.to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + + let off = offset__of!(user_pt_regs, pstate); + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U64, off), + &state.regs.pstate.to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + + let off = offset__of!(kvm_regs, sp_el1); + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U64, off), + &state.sp_el1.to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + + let off = offset__of!(kvm_regs, elr_el1); + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U64, off), + &state.elr_el1.to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + + let mut off = offset__of!(kvm_regs, spsr); + for i in 0..KVM_NR_SPSR as usize { + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U64, off), + &state.spsr[i].to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + off += std::mem::size_of::(); + } + + let mut off = offset__of!(kvm_regs, fp_regs) + offset__of!(user_fpsimd_state, vregs); + for i in 0..32 { + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U128, off), + &state.fp_regs.vregs[i].to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + off += mem::size_of::(); + } + + let off = offset__of!(kvm_regs, fp_regs) + offset__of!(user_fpsimd_state, fpsr); + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U32, off), + &state.fp_regs.fpsr.to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + + let off = offset__of!(kvm_regs, fp_regs) + offset__of!(user_fpsimd_state, fpcr); + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U32, off), + &state.fp_regs.fpcr.to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + Ok(()) + } + + #[cfg(target_arch = "x86_64")] + /// + /// Returns the vCPU special registers. + /// + fn get_sregs(&self) -> cpu::Result { + Ok(self + .fd + .lock() + .unwrap() + .get_sregs() + .map_err(|e| cpu::HypervisorCpuError::GetSpecialRegs(e.into()))? + .into()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Sets the vCPU special registers using the `KVM_SET_SREGS` ioctl. + /// + fn set_sregs(&self, sregs: &SpecialRegisters) -> cpu::Result<()> { + let sregs = (*sregs).into(); + self.fd + .lock() + .unwrap() + .set_sregs(&sregs) + .map_err(|e| cpu::HypervisorCpuError::SetSpecialRegs(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// Returns the floating point state (FPU) from the vCPU. + /// + fn get_fpu(&self) -> cpu::Result { + Ok(self + .fd + .lock() + .unwrap() + .get_fpu() + .map_err(|e| cpu::HypervisorCpuError::GetFloatingPointRegs(e.into()))? + .into()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Set the floating point state (FPU) of a vCPU using the `KVM_SET_FPU` ioct. + /// + fn set_fpu(&self, fpu: &FpuState) -> cpu::Result<()> { + let fpu: kvm_bindings::kvm_fpu = (*fpu).clone().into(); + self.fd + .lock() + .unwrap() + .set_fpu(&fpu) + .map_err(|e| cpu::HypervisorCpuError::SetFloatingPointRegs(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call to setup the CPUID registers. + /// + fn set_cpuid2(&self, cpuid: &[CpuIdEntry]) -> cpu::Result<()> { + let cpuid: Vec = + cpuid.iter().map(|e| (*e).into()).collect(); + let kvm_cpuid = ::from_entries(&cpuid) + .map_err(|_| cpu::HypervisorCpuError::SetCpuid(anyhow!("failed to create CpuId")))?; + + self.fd + .lock() + .unwrap() + .set_cpuid2(&kvm_cpuid) + .map_err(|e| cpu::HypervisorCpuError::SetCpuid(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call to enable HyperV SynIC + /// + fn enable_hyperv_synic(&self) -> cpu::Result<()> { + // Update the information about Hyper-V SynIC being enabled and + // emulated as it will influence later which MSRs should be saved. + self.hyperv_synic.store(true, Ordering::Release); + + let cap = kvm_enable_cap { + cap: KVM_CAP_HYPERV_SYNIC, + ..Default::default() + }; + self.fd + .lock() + .unwrap() + .enable_cap(&cap) + .map_err(|e| cpu::HypervisorCpuError::EnableHyperVSyncIc(e.into())) + } + /// + /// X86 specific call to retrieve the CPUID registers. + /// + #[cfg(target_arch = "x86_64")] + fn get_cpuid2(&self, num_entries: usize) -> cpu::Result> { + let kvm_cpuid = self + .fd + .lock() + .unwrap() + .get_cpuid2(num_entries) + .map_err(|e| cpu::HypervisorCpuError::GetCpuid(e.into()))?; + + let v = kvm_cpuid.as_slice().iter().map(|e| (*e).into()).collect(); + + Ok(v) + } + #[cfg(target_arch = "x86_64")] + /// + /// Returns the state of the LAPIC (Local Advanced Programmable Interrupt Controller). + /// + fn get_lapic(&self) -> cpu::Result { + Ok(self + .fd + .lock() + .unwrap() + .get_lapic() + .map_err(|e| cpu::HypervisorCpuError::GetlapicState(e.into()))? + .into()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Sets the state of the LAPIC (Local Advanced Programmable Interrupt Controller). + /// + fn set_lapic(&self, klapic: &LapicState) -> cpu::Result<()> { + let klapic: kvm_bindings::kvm_lapic_state = (*klapic).clone().into(); + self.fd + .lock() + .unwrap() + .set_lapic(&klapic) + .map_err(|e| cpu::HypervisorCpuError::SetLapicState(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// Returns the model-specific registers (MSR) for this vCPU. + /// + fn get_msrs(&self, msrs: &mut Vec) -> cpu::Result { + let kvm_msrs: Vec = msrs.iter().map(|e| (*e).into()).collect(); + let mut kvm_msrs = MsrEntries::from_entries(&kvm_msrs).unwrap(); + let succ = self + .fd + .lock() + .unwrap() + .get_msrs(&mut kvm_msrs) + .map_err(|e| cpu::HypervisorCpuError::GetMsrEntries(e.into()))?; + + msrs[..succ].copy_from_slice( + &kvm_msrs.as_slice()[..succ] + .iter() + .map(|e| (*e).into()) + .collect::>(), + ); + + Ok(succ) + } + #[cfg(target_arch = "x86_64")] + /// + /// Setup the model-specific registers (MSR) for this vCPU. + /// Returns the number of MSR entries actually written. + /// + fn set_msrs(&self, msrs: &[MsrEntry]) -> cpu::Result { + let kvm_msrs: Vec = msrs.iter().map(|e| (*e).into()).collect(); + let kvm_msrs = MsrEntries::from_entries(&kvm_msrs).unwrap(); + self.fd + .lock() + .unwrap() + .set_msrs(&kvm_msrs) + .map_err(|e| cpu::HypervisorCpuError::SetMsrEntries(e.into())) + } + /// + /// Returns the vcpu's current "multiprocessing state". + /// + fn get_mp_state(&self) -> cpu::Result { + Ok(self + .fd + .lock() + .unwrap() + .get_mp_state() + .map_err(|e| cpu::HypervisorCpuError::GetMpState(e.into()))? + .into()) + } + /// + /// Sets the vcpu's current "multiprocessing state". + /// + fn set_mp_state(&self, mp_state: MpState) -> cpu::Result<()> { + self.fd + .lock() + .unwrap() + .set_mp_state(mp_state.into()) + .map_err(|e| cpu::HypervisorCpuError::SetMpState(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// Translates guest virtual address to guest physical address using the `KVM_TRANSLATE` ioctl. + /// + fn translate_gva(&self, gva: u64, _flags: u64) -> cpu::Result<(u64, u32)> { + let tr = self + .fd + .lock() + .unwrap() + .translate_gva(gva) + .map_err(|e| cpu::HypervisorCpuError::TranslateVirtualAddress(e.into()))?; + // tr.valid is set if the GVA is mapped to valid GPA. + match tr.valid { + 0 => Err(cpu::HypervisorCpuError::TranslateVirtualAddress(anyhow!( + "Invalid GVA: {:#x}", + gva + ))), + _ => Ok((tr.physical_address, 0)), + } + } + /// + /// Triggers the running of the current virtual CPU returning an exit reason. + /// + fn run(&self) -> std::result::Result { + match self.fd.lock().unwrap().run() { + Ok(run) => match run { + #[cfg(target_arch = "x86_64")] + VcpuExit::IoIn(addr, data) => { + if let Some(vm_ops) = &self.vm_ops { + return vm_ops + .pio_read(addr.into(), data) + .map(|_| cpu::VmExit::Ignore) + .map_err(|e| cpu::HypervisorCpuError::RunVcpu(e.into())); + } + + Ok(cpu::VmExit::Ignore) + } + #[cfg(target_arch = "x86_64")] + VcpuExit::IoOut(addr, data) => { + if let Some(vm_ops) = &self.vm_ops { + return vm_ops + .pio_write(addr.into(), data) + .map(|_| cpu::VmExit::Ignore) + .map_err(|e| cpu::HypervisorCpuError::RunVcpu(e.into())); + } + + Ok(cpu::VmExit::Ignore) + } + #[cfg(target_arch = "x86_64")] + VcpuExit::IoapicEoi(vector) => Ok(cpu::VmExit::IoapicEoi(vector)), + #[cfg(target_arch = "x86_64")] + VcpuExit::Shutdown | VcpuExit::Hlt => Ok(cpu::VmExit::Reset), + + #[cfg(target_arch = "aarch64")] + VcpuExit::SystemEvent(event_type, flags) => { + use kvm_bindings::{KVM_SYSTEM_EVENT_RESET, KVM_SYSTEM_EVENT_SHUTDOWN}; + // On Aarch64, when the VM is shutdown, run() returns + // VcpuExit::SystemEvent with reason KVM_SYSTEM_EVENT_SHUTDOWN + if event_type == KVM_SYSTEM_EVENT_RESET { + Ok(cpu::VmExit::Reset) + } else if event_type == KVM_SYSTEM_EVENT_SHUTDOWN { + Ok(cpu::VmExit::Shutdown) + } else { + Err(cpu::HypervisorCpuError::RunVcpu(anyhow!( + "Unexpected system event with type 0x{:x}, flags 0x{:x?}", + event_type, + flags + ))) + } + } + + VcpuExit::MmioRead(addr, data) => { + if let Some(vm_ops) = &self.vm_ops { + return vm_ops + .mmio_read(addr, data) + .map(|_| cpu::VmExit::Ignore) + .map_err(|e| cpu::HypervisorCpuError::RunVcpu(e.into())); + } + + Ok(cpu::VmExit::Ignore) + } + VcpuExit::MmioWrite(addr, data) => { + if let Some(vm_ops) = &self.vm_ops { + return vm_ops + .mmio_write(addr, data) + .map(|_| cpu::VmExit::Ignore) + .map_err(|e| cpu::HypervisorCpuError::RunVcpu(e.into())); + } + + Ok(cpu::VmExit::Ignore) + } + VcpuExit::Hyperv => Ok(cpu::VmExit::Hyperv), + #[cfg(feature = "tdx")] + VcpuExit::Unsupported(KVM_EXIT_TDX) => Ok(cpu::VmExit::Tdx), + VcpuExit::Debug(_) => Ok(cpu::VmExit::Debug), + + r => Err(cpu::HypervisorCpuError::RunVcpu(anyhow!( + "Unexpected exit reason on vcpu run: {:?}", + r + ))), + }, + + Err(ref e) => match e.errno() { + libc::EAGAIN | libc::EINTR => Ok(cpu::VmExit::Ignore), + _ => Err(cpu::HypervisorCpuError::RunVcpu(anyhow!( + "VCPU error {:?}", + e + ))), + }, + } + } + #[cfg(target_arch = "x86_64")] + /// + /// Let the guest know that it has been paused, which prevents from + /// potential soft lockups when being resumed. + /// + fn notify_guest_clock_paused(&self) -> cpu::Result<()> { + if let Err(e) = self.fd.lock().unwrap().kvmclock_ctrl() { + // Linux kernel returns -EINVAL if the PV clock isn't yet initialised + // which could be because we're still in firmware or the guest doesn't + // use KVM clock. + if e.errno() != libc::EINVAL { + return Err(cpu::HypervisorCpuError::NotifyGuestClockPaused(e.into())); + } + } + + Ok(()) + } + /// + /// Sets debug registers to set hardware breakpoints and/or enable single step. + /// + fn set_guest_debug( + &self, + addrs: &[vm_memory::GuestAddress], + singlestep: bool, + ) -> cpu::Result<()> { + let mut dbg = kvm_guest_debug { + #[cfg(target_arch = "x86_64")] + control: KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP, + #[cfg(target_arch = "aarch64")] + control: KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW, + ..Default::default() + }; + if singlestep { + dbg.control |= KVM_GUESTDBG_SINGLESTEP; + } + + // Set the debug registers. + // Here we assume that the number of addresses do not exceed what + // `Hypervisor::get_guest_debug_hw_bps()` specifies. + #[cfg(target_arch = "x86_64")] + { + // Set bits 9 and 10. + // bit 9: GE (global exact breakpoint enable) flag. + // bit 10: always 1. + dbg.arch.debugreg[7] = 0x0600; + + for (i, addr) in addrs.iter().enumerate() { + dbg.arch.debugreg[i] = addr.0; + // Set global breakpoint enable flag + dbg.arch.debugreg[7] |= 2 << (i * 2); + } + } + #[cfg(target_arch = "aarch64")] + { + for (i, addr) in addrs.iter().enumerate() { + // DBGBCR_EL1 (Debug Breakpoint Control Registers, D13.3.2): + // bit 0: 1 (Enabled) + // bit 1~2: 0b11 (PMC = EL1/EL0) + // bit 5~8: 0b1111 (BAS = AArch64) + // others: 0 + dbg.arch.dbg_bcr[i] = 0b1u64 | 0b110u64 | 0b1_1110_0000u64; + // DBGBVR_EL1 (Debug Breakpoint Value Registers, D13.3.3): + // bit 2~52: VA[2:52] + dbg.arch.dbg_bvr[i] = (!0u64 >> 11) & addr.0; + } + } + self.fd + .lock() + .unwrap() + .set_guest_debug(&dbg) + .map_err(|e| cpu::HypervisorCpuError::SetDebugRegs(e.into())) + } + #[cfg(target_arch = "aarch64")] + fn vcpu_init(&self, kvi: &VcpuInit) -> cpu::Result<()> { + self.fd + .lock() + .unwrap() + .vcpu_init(kvi) + .map_err(|e| cpu::HypervisorCpuError::VcpuInit(e.into())) + } + /// + /// Gets a list of the guest registers that are supported for the + /// KVM_GET_ONE_REG/KVM_SET_ONE_REG calls. + /// + #[cfg(target_arch = "aarch64")] + fn get_reg_list(&self, reg_list: &mut RegList) -> cpu::Result<()> { + self.fd + .lock() + .unwrap() + .get_reg_list(reg_list) + .map_err(|e| cpu::HypervisorCpuError::GetRegList(e.into())) + } + /// + /// Gets the value of a system register + /// + #[cfg(target_arch = "aarch64")] + fn get_sys_reg(&self, sys_reg: u32) -> cpu::Result { + // + // Arm Architecture Reference Manual defines the encoding of + // AArch64 system registers, see + // https://developer.arm.com/documentation/ddi0487 (chapter D12). + // While KVM defines another ID for each AArch64 system register, + // which is used in calling `KVM_G/SET_ONE_REG` to access a system + // register of a guest. + // A mapping exists between the Arm standard encoding and the KVM ID. + // This function takes the standard u32 ID as input parameter, converts + // it to the corresponding KVM ID, and call `KVM_GET_ONE_REG` API to + // get the value of the system parameter. + // + let id: u64 = KVM_REG_ARM64 as u64 + | KVM_REG_SIZE_U64 as u64 + | KVM_REG_ARM64_SYSREG as u64 + | ((((sys_reg) >> 5) + & (KVM_REG_ARM64_SYSREG_OP0_MASK + | KVM_REG_ARM64_SYSREG_OP1_MASK + | KVM_REG_ARM64_SYSREG_CRN_MASK + | KVM_REG_ARM64_SYSREG_CRM_MASK + | KVM_REG_ARM64_SYSREG_OP2_MASK)) as u64); + let mut bytes = [0_u8; 8]; + self.fd + .lock() + .unwrap() + .get_one_reg(id, &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetSysRegister(e.into()))?; + Ok(u64::from_le_bytes(bytes)) + } + /// + /// Configure core registers for a given CPU. + /// + #[cfg(target_arch = "aarch64")] + fn setup_regs(&self, cpu_id: u8, boot_ip: u64, fdt_start: u64) -> cpu::Result<()> { + #[allow(non_upper_case_globals)] + // PSR (Processor State Register) bits. + // Taken from arch/arm64/include/uapi/asm/ptrace.h. + const PSR_MODE_EL1h: u64 = 0x0000_0005; + const PSR_F_BIT: u64 = 0x0000_0040; + const PSR_I_BIT: u64 = 0x0000_0080; + const PSR_A_BIT: u64 = 0x0000_0100; + const PSR_D_BIT: u64 = 0x0000_0200; + // Taken from arch/arm64/kvm/inject_fault.c. + const PSTATE_FAULT_BITS_64: u64 = + PSR_MODE_EL1h | PSR_A_BIT | PSR_F_BIT | PSR_I_BIT | PSR_D_BIT; + + let kreg_off = offset__of!(kvm_regs, regs); + + // Get the register index of the PSTATE (Processor State) register. + let pstate = offset__of!(user_pt_regs, pstate) + kreg_off; + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U64, pstate), + &PSTATE_FAULT_BITS_64.to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + + // Other vCPUs are powered off initially awaiting PSCI wakeup. + if cpu_id == 0 { + // Setting the PC (Processor Counter) to the current program address (kernel address). + let pc = offset__of!(user_pt_regs, pc) + kreg_off; + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U64, pc), + &boot_ip.to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + + // Last mandatory thing to set -> the address pointing to the FDT (also called DTB). + // "The device tree blob (dtb) must be placed on an 8-byte boundary and must + // not exceed 2 megabytes in size." -> https://www.kernel.org/doc/Documentation/arm64/booting.txt. + // We are choosing to place it the end of DRAM. See `get_fdt_addr`. + let regs0 = offset__of!(user_pt_regs, regs) + kreg_off; + self.fd + .lock() + .unwrap() + .set_one_reg( + arm64_core_reg_id!(KVM_REG_SIZE_U64, regs0), + &fdt_start.to_le_bytes(), + ) + .map_err(|e| cpu::HypervisorCpuError::SetCoreRegister(e.into()))?; + } + Ok(()) + } + + #[cfg(target_arch = "x86_64")] + /// + /// Get the current CPU state + /// + /// Ordering requirements: + /// + /// KVM_GET_MP_STATE calls kvm_apic_accept_events(), which might modify + /// vCPU/LAPIC state. As such, it must be done before most everything + /// else, otherwise we cannot restore everything and expect it to work. + /// + /// KVM_GET_VCPU_EVENTS/KVM_SET_VCPU_EVENTS is unsafe if other vCPUs are + /// still running. + /// + /// KVM_GET_LAPIC may change state of LAPIC before returning it. + /// + /// GET_VCPU_EVENTS should probably be last to save. The code looks as + /// it might as well be affected by internal state modifications of the + /// GET ioctls. + /// + /// SREGS saves/restores a pending interrupt, similar to what + /// VCPU_EVENTS also does. + /// + /// GET_MSRS requires a pre-populated data structure to do something + /// meaningful. For SET_MSRS it will then contain good data. + /// + /// # Example + /// + /// ```rust + /// # extern crate hypervisor; + /// # use hypervisor::KvmHypervisor; + /// # use std::sync::Arc; + /// let kvm = hypervisor::kvm::KvmHypervisor::new().unwrap(); + /// let hv: Arc = Arc::new(kvm); + /// let vm = hv.create_vm().expect("new VM fd creation failed"); + /// vm.enable_split_irq().unwrap(); + /// let vcpu = vm.create_vcpu(0, None).unwrap(); + /// let state = vcpu.state().unwrap(); + /// ``` + fn state(&self) -> cpu::Result { + let cpuid = self.get_cpuid2(kvm_bindings::KVM_MAX_CPUID_ENTRIES)?; + let mp_state = self.get_mp_state()?.into(); + let regs = self.get_regs()?; + let sregs = self.get_sregs()?; + let xsave = self.get_xsave()?; + let xcrs = self.get_xcrs()?; + let lapic_state = self.get_lapic()?; + let fpu = self.get_fpu()?; + + // Try to get all MSRs based on the list previously retrieved from KVM. + // If the number of MSRs obtained from GET_MSRS is different from the + // expected amount, we fallback onto a slower method by getting MSRs + // by chunks. This is the only way to make sure we try to get as many + // MSRs as possible, even if some MSRs are not supported. + let mut msr_entries = self.msrs.clone(); + + // Save extra MSRs if the Hyper-V synthetic interrupt controller is + // emulated. + if self.hyperv_synic.load(Ordering::Acquire) { + let hyperv_synic_msrs = vec![ + 0x40000020, 0x40000021, 0x40000080, 0x40000081, 0x40000082, 0x40000083, 0x40000084, + 0x40000090, 0x40000091, 0x40000092, 0x40000093, 0x40000094, 0x40000095, 0x40000096, + 0x40000097, 0x40000098, 0x40000099, 0x4000009a, 0x4000009b, 0x4000009c, 0x4000009d, + 0x4000009e, 0x4000009f, 0x400000b0, 0x400000b1, 0x400000b2, 0x400000b3, 0x400000b4, + 0x400000b5, 0x400000b6, 0x400000b7, + ]; + for index in hyperv_synic_msrs { + let msr = kvm_msr_entry { + index, + ..Default::default() + }; + msr_entries.push(msr.into()); + } + } + + let expected_num_msrs = msr_entries.len(); + let num_msrs = self.get_msrs(&mut msr_entries)?; + let msrs = if num_msrs != expected_num_msrs { + let mut faulty_msr_index = num_msrs; + let mut msr_entries_tmp = msr_entries[..faulty_msr_index].to_vec(); + + loop { + warn!( + "Detected faulty MSR 0x{:x} while getting MSRs", + msr_entries[faulty_msr_index].index + ); + + // Skip the first bad MSR + let start_pos = faulty_msr_index + 1; + + let mut sub_msr_entries = msr_entries[start_pos..].to_vec(); + let num_msrs = self.get_msrs(&mut sub_msr_entries)?; + + msr_entries_tmp.extend(&sub_msr_entries[..num_msrs]); + + if num_msrs == sub_msr_entries.len() { + break; + } + + faulty_msr_index = start_pos + num_msrs; + } + + msr_entries_tmp + } else { + msr_entries + }; + + let vcpu_events = self.get_vcpu_events()?; + + Ok(VcpuKvmState { + cpuid, + msrs, + vcpu_events, + regs: regs.into(), + sregs: sregs.into(), + fpu, + lapic_state, + xsave: xsave.into(), + xcrs, + mp_state, + } + .into()) + } + /// + /// Get the current AArch64 CPU state + /// + #[cfg(target_arch = "aarch64")] + fn state(&self) -> cpu::Result { + let mut state = VcpuKvmState { + mp_state: self.get_mp_state()?.into(), + ..Default::default() + }; + // Get core registers + state.core_regs = self.get_regs()?; + + // Get systerm register + // Call KVM_GET_REG_LIST to get all registers available to the guest. + // For ArmV8 there are around 500 registers. + let mut sys_regs: Vec = Vec::new(); + let mut reg_list = RegList::new(500).unwrap(); + self.fd + .lock() + .unwrap() + .get_reg_list(&mut reg_list) + .map_err(|e| cpu::HypervisorCpuError::GetRegList(e.into()))?; + + // At this point reg_list should contain: core registers and system + // registers. + // The register list contains the number of registers and their ids. We + // will be needing to call KVM_GET_ONE_REG on each id in order to save + // all of them. We carve out from the list the core registers which are + // represented in the kernel by kvm_regs structure and for which we can + // calculate the id based on the offset in the structure. + reg_list.retain(|regid| is_system_register(*regid)); + + // Now, for the rest of the registers left in the previously fetched + // register list, we are simply calling KVM_GET_ONE_REG. + let indices = reg_list.as_slice(); + for index in indices.iter() { + let mut bytes = [0_u8; 8]; + self.fd + .lock() + .unwrap() + .get_one_reg(*index, &mut bytes) + .map_err(|e| cpu::HypervisorCpuError::GetSysRegister(e.into()))?; + sys_regs.push(kvm_bindings::kvm_one_reg { + id: *index, + addr: u64::from_le_bytes(bytes), + }); + } + + state.sys_regs = sys_regs; + + Ok(state.into()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Restore the previously saved CPU state + /// + /// Ordering requirements: + /// + /// KVM_GET_VCPU_EVENTS/KVM_SET_VCPU_EVENTS is unsafe if other vCPUs are + /// still running. + /// + /// Some SET ioctls (like set_mp_state) depend on kvm_vcpu_is_bsp(), so + /// if we ever change the BSP, we have to do that before restoring anything. + /// The same seems to be true for CPUID stuff. + /// + /// SREGS saves/restores a pending interrupt, similar to what + /// VCPU_EVENTS also does. + /// + /// SET_REGS clears pending exceptions unconditionally, thus, it must be + /// done before SET_VCPU_EVENTS, which restores it. + /// + /// SET_LAPIC must come after SET_SREGS, because the latter restores + /// the apic base msr. + /// + /// SET_LAPIC must come before SET_MSRS, because the TSC deadline MSR + /// only restores successfully, when the LAPIC is correctly configured. + /// + /// Arguments: CpuState + /// # Example + /// + /// ```rust + /// # extern crate hypervisor; + /// # use hypervisor::KvmHypervisor; + /// # use std::sync::Arc; + /// let kvm = hypervisor::kvm::KvmHypervisor::new().unwrap(); + /// let hv: Arc = Arc::new(kvm); + /// let vm = hv.create_vm().expect("new VM fd creation failed"); + /// vm.enable_split_irq().unwrap(); + /// let vcpu = vm.create_vcpu(0, None).unwrap(); + /// let state = vcpu.state().unwrap(); + /// vcpu.set_state(&state).unwrap(); + /// ``` + fn set_state(&self, state: &CpuState) -> cpu::Result<()> { + let state: VcpuKvmState = state.clone().into(); + self.set_cpuid2(&state.cpuid)?; + self.set_mp_state(state.mp_state.into())?; + self.set_regs(&state.regs.into())?; + self.set_sregs(&state.sregs.into())?; + self.set_xsave(&state.xsave.into())?; + self.set_xcrs(&state.xcrs)?; + self.set_lapic(&state.lapic_state)?; + self.set_fpu(&state.fpu)?; + + // Try to set all MSRs previously stored. + // If the number of MSRs set from SET_MSRS is different from the + // expected amount, we fallback onto a slower method by setting MSRs + // by chunks. This is the only way to make sure we try to set as many + // MSRs as possible, even if some MSRs are not supported. + let expected_num_msrs = state.msrs.len(); + let num_msrs = self.set_msrs(&state.msrs)?; + if num_msrs != expected_num_msrs { + let mut faulty_msr_index = num_msrs; + + loop { + warn!( + "Detected faulty MSR 0x{:x} while setting MSRs", + state.msrs[faulty_msr_index].index + ); + + // Skip the first bad MSR + let start_pos = faulty_msr_index + 1; + + let sub_msr_entries = state.msrs[start_pos..].to_vec(); + + let num_msrs = self.set_msrs(&sub_msr_entries)?; + + if num_msrs == sub_msr_entries.len() { + break; + } + + faulty_msr_index = start_pos + num_msrs; + } + } + + self.set_vcpu_events(&state.vcpu_events)?; + + Ok(()) + } + /// + /// Restore the previously saved AArch64 CPU state + /// + #[cfg(target_arch = "aarch64")] + fn set_state(&self, state: &CpuState) -> cpu::Result<()> { + let state: VcpuKvmState = state.clone().into(); + // Set core registers + self.set_regs(&state.core_regs)?; + // Set system registers + for reg in &state.sys_regs { + self.fd + .lock() + .unwrap() + .set_one_reg(reg.id, ®.addr.to_le_bytes()) + .map_err(|e| cpu::HypervisorCpuError::SetSysRegister(e.into()))?; + } + + self.set_mp_state(state.mp_state.into())?; + + Ok(()) + } + + /// + /// Initialize TDX for this CPU + /// + #[cfg(feature = "tdx")] + fn tdx_init(&self, hob_address: u64) -> cpu::Result<()> { + tdx_command( + &self.fd.lock().unwrap().as_raw_fd(), + TdxCommand::InitVcpu, + 0, + hob_address, + ) + .map_err(cpu::HypervisorCpuError::InitializeTdx) + } + + /// + /// Set the "immediate_exit" state + /// + fn set_immediate_exit(&self, exit: bool) { + self.fd.lock().unwrap().set_kvm_immediate_exit(exit.into()); + } + + /// + /// Returns the details about TDX exit reason + /// + #[cfg(feature = "tdx")] + fn get_tdx_exit_details(&mut self) -> cpu::Result { + let mut fd = self.fd.as_ref().lock().unwrap(); + let kvm_run = fd.get_kvm_run(); + // SAFETY: accessing a union field in a valid structure + let tdx_vmcall = unsafe { + &mut (*((&mut kvm_run.__bindgen_anon_1) as *mut kvm_run__bindgen_ty_1 + as *mut KvmTdxExit)) + .u + .vmcall + }; + + tdx_vmcall.status_code = TDG_VP_VMCALL_INVALID_OPERAND; + + if tdx_vmcall.type_ != 0 { + return Err(cpu::HypervisorCpuError::UnknownTdxVmCall); + } + + match tdx_vmcall.subfunction { + TDG_VP_VMCALL_GET_QUOTE => Ok(TdxExitDetails::GetQuote), + TDG_VP_VMCALL_SETUP_EVENT_NOTIFY_INTERRUPT => { + Ok(TdxExitDetails::SetupEventNotifyInterrupt) + } + _ => Err(cpu::HypervisorCpuError::UnknownTdxVmCall), + } + } + + /// + /// Set the status code for TDX exit + /// + #[cfg(feature = "tdx")] + fn set_tdx_status(&mut self, status: TdxExitStatus) { + let mut fd = self.fd.as_ref().lock().unwrap(); + let kvm_run = fd.get_kvm_run(); + // SAFETY: accessing a union field in a valid structure + let tdx_vmcall = unsafe { + &mut (*((&mut kvm_run.__bindgen_anon_1) as *mut kvm_run__bindgen_ty_1 + as *mut KvmTdxExit)) + .u + .vmcall + }; + + tdx_vmcall.status_code = match status { + TdxExitStatus::Success => TDG_VP_VMCALL_SUCCESS, + TdxExitStatus::InvalidOperand => TDG_VP_VMCALL_INVALID_OPERAND, + }; + } + #[cfg(target_arch = "x86_64")] + /// + /// Return the list of initial MSR entries for a VCPU + /// + fn boot_msr_entries(&self) -> Vec { + use crate::arch::x86::{msr_index, MTRR_ENABLE, MTRR_MEM_TYPE_WB}; + + [ + msr!(msr_index::MSR_IA32_SYSENTER_CS), + msr!(msr_index::MSR_IA32_SYSENTER_ESP), + msr!(msr_index::MSR_IA32_SYSENTER_EIP), + msr!(msr_index::MSR_STAR), + msr!(msr_index::MSR_CSTAR), + msr!(msr_index::MSR_LSTAR), + msr!(msr_index::MSR_KERNEL_GS_BASE), + msr!(msr_index::MSR_SYSCALL_MASK), + msr!(msr_index::MSR_IA32_TSC), + msr_data!( + msr_index::MSR_IA32_MISC_ENABLE, + msr_index::MSR_IA32_MISC_ENABLE_FAST_STRING as u64 + ), + msr_data!(msr_index::MSR_MTRRdefType, MTRR_ENABLE | MTRR_MEM_TYPE_WB), + ] + .to_vec() + } + #[cfg(target_arch = "aarch64")] + fn has_pmu_support(&self) -> bool { + let cpu_attr = kvm_bindings::kvm_device_attr { + group: kvm_bindings::KVM_ARM_VCPU_PMU_V3_CTRL, + attr: u64::from(kvm_bindings::KVM_ARM_VCPU_PMU_V3_INIT), + addr: 0x0, + flags: 0, + }; + self.fd.lock().unwrap().has_device_attr(&cpu_attr).is_ok() + } + #[cfg(target_arch = "aarch64")] + fn init_pmu(&self, irq: u32) -> cpu::Result<()> { + let cpu_attr = kvm_bindings::kvm_device_attr { + group: kvm_bindings::KVM_ARM_VCPU_PMU_V3_CTRL, + attr: u64::from(kvm_bindings::KVM_ARM_VCPU_PMU_V3_INIT), + addr: 0x0, + flags: 0, + }; + let cpu_attr_irq = kvm_bindings::kvm_device_attr { + group: kvm_bindings::KVM_ARM_VCPU_PMU_V3_CTRL, + attr: u64::from(kvm_bindings::KVM_ARM_VCPU_PMU_V3_IRQ), + addr: &irq as *const u32 as u64, + flags: 0, + }; + self.fd + .lock() + .unwrap() + .set_device_attr(&cpu_attr_irq) + .map_err(|_| cpu::HypervisorCpuError::InitializePmu)?; + self.fd + .lock() + .unwrap() + .set_device_attr(&cpu_attr) + .map_err(|_| cpu::HypervisorCpuError::InitializePmu) + } +} + +impl KvmVcpu { + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call that returns the vcpu's current "xsave struct". + /// + fn get_xsave(&self) -> cpu::Result { + Ok(self + .fd + .lock() + .unwrap() + .get_xsave() + .map_err(|e| cpu::HypervisorCpuError::GetXsaveState(e.into()))? + .into()) + } + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call that sets the vcpu's current "xsave struct". + /// + fn set_xsave(&self, xsave: &XsaveState) -> cpu::Result<()> { + let xsave: kvm_bindings::kvm_xsave = (*xsave).clone().into(); + self.fd + .lock() + .unwrap() + .set_xsave(&xsave) + .map_err(|e| cpu::HypervisorCpuError::SetXsaveState(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call that returns the vcpu's current "xcrs". + /// + fn get_xcrs(&self) -> cpu::Result { + self.fd + .lock() + .unwrap() + .get_xcrs() + .map_err(|e| cpu::HypervisorCpuError::GetXcsr(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call that sets the vcpu's current "xcrs". + /// + fn set_xcrs(&self, xcrs: &ExtendedControlRegisters) -> cpu::Result<()> { + self.fd + .lock() + .unwrap() + .set_xcrs(xcrs) + .map_err(|e| cpu::HypervisorCpuError::SetXcsr(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// Returns currently pending exceptions, interrupts, and NMIs as well as related + /// states of the vcpu. + /// + fn get_vcpu_events(&self) -> cpu::Result { + self.fd + .lock() + .unwrap() + .get_vcpu_events() + .map_err(|e| cpu::HypervisorCpuError::GetVcpuEvents(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// Sets pending exceptions, interrupts, and NMIs as well as related states + /// of the vcpu. + /// + fn set_vcpu_events(&self, events: &VcpuEvents) -> cpu::Result<()> { + self.fd + .lock() + .unwrap() + .set_vcpu_events(events) + .map_err(|e| cpu::HypervisorCpuError::SetVcpuEvents(e.into())) + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/x86_64/mod.rs b/vendor/cube-hypervisor/hypervisor/src/kvm/x86_64/mod.rs new file mode 100644 index 0000000..91a4d22 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/kvm/x86_64/mod.rs @@ -0,0 +1,343 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// + +use crate::arch::x86::{ + CpuIdEntry, DescriptorTable, FpuState, LapicState, MsrEntry, SegmentRegister, SpecialRegisters, + StandardRegisters, XsaveState, CPUID_FLAG_VALID_INDEX, +}; +use crate::kvm::{Cap, Kvm, KvmError, KvmResult}; +use serde::{Deserialize, Serialize}; + +/// +/// Export generically-named wrappers of kvm-bindings for Unix-based platforms +/// +pub use { + kvm_bindings::kvm_cpuid_entry2, kvm_bindings::kvm_dtable, kvm_bindings::kvm_fpu, + kvm_bindings::kvm_lapic_state, kvm_bindings::kvm_mp_state as MpState, + kvm_bindings::kvm_msr_entry, kvm_bindings::kvm_regs, kvm_bindings::kvm_segment, + kvm_bindings::kvm_sregs, kvm_bindings::kvm_vcpu_events as VcpuEvents, + kvm_bindings::kvm_xcrs as ExtendedControlRegisters, kvm_bindings::kvm_xsave as Xsave, + kvm_bindings::CpuId, kvm_bindings::MsrList, kvm_bindings::Msrs as MsrEntries, + kvm_bindings::KVM_CPUID_FLAG_SIGNIFCANT_INDEX, +}; + +/// +/// Check KVM extension for Linux +/// +pub fn check_required_kvm_extensions(kvm: &Kvm) -> KvmResult<()> { + if !kvm.check_extension(Cap::SignalMsi) { + return Err(KvmError::CapabilityMissing(Cap::SignalMsi)); + } + if !kvm.check_extension(Cap::TscDeadlineTimer) { + return Err(KvmError::CapabilityMissing(Cap::TscDeadlineTimer)); + } + if !kvm.check_extension(Cap::SplitIrqchip) { + return Err(KvmError::CapabilityMissing(Cap::SplitIrqchip)); + } + if !kvm.check_extension(Cap::SetIdentityMapAddr) { + return Err(KvmError::CapabilityMissing(Cap::SetIdentityMapAddr)); + } + if !kvm.check_extension(Cap::SetTssAddr) { + return Err(KvmError::CapabilityMissing(Cap::SetTssAddr)); + } + if !kvm.check_extension(Cap::ImmediateExit) { + return Err(KvmError::CapabilityMissing(Cap::ImmediateExit)); + } + Ok(()) +} +#[derive(Clone, Serialize, Deserialize)] +pub struct VcpuKvmState { + pub cpuid: Vec, + pub msrs: Vec, + pub vcpu_events: VcpuEvents, + pub regs: kvm_regs, + pub sregs: kvm_sregs, + pub fpu: FpuState, + pub lapic_state: LapicState, + pub xsave: XsaveState, + pub xcrs: ExtendedControlRegisters, + pub mp_state: MpState, +} + +impl From for kvm_regs { + fn from(regs: StandardRegisters) -> Self { + Self { + rax: regs.rax, + rbx: regs.rbx, + rcx: regs.rcx, + rdx: regs.rdx, + rsi: regs.rsi, + rdi: regs.rdi, + rsp: regs.rsp, + rbp: regs.rbp, + r8: regs.r8, + r9: regs.r9, + r10: regs.r10, + r11: regs.r11, + r12: regs.r12, + r13: regs.r13, + r14: regs.r14, + r15: regs.r15, + rip: regs.rip, + rflags: regs.rflags, + } + } +} + +impl From for StandardRegisters { + fn from(regs: kvm_regs) -> Self { + Self { + rax: regs.rax, + rbx: regs.rbx, + rcx: regs.rcx, + rdx: regs.rdx, + rsi: regs.rsi, + rdi: regs.rdi, + rsp: regs.rsp, + rbp: regs.rbp, + r8: regs.r8, + r9: regs.r9, + r10: regs.r10, + r11: regs.r11, + r12: regs.r12, + r13: regs.r13, + r14: regs.r14, + r15: regs.r15, + rip: regs.rip, + rflags: regs.rflags, + } + } +} + +impl From for kvm_segment { + fn from(s: SegmentRegister) -> Self { + Self { + base: s.base, + limit: s.limit, + selector: s.selector, + type_: s.type_, + present: s.present, + dpl: s.dpl, + db: s.db, + s: s.s, + l: s.l, + g: s.g, + avl: s.avl, + unusable: s.unusable, + ..Default::default() + } + } +} + +impl From for SegmentRegister { + fn from(s: kvm_segment) -> Self { + Self { + base: s.base, + limit: s.limit, + selector: s.selector, + type_: s.type_, + present: s.present, + dpl: s.dpl, + db: s.db, + s: s.s, + l: s.l, + g: s.g, + avl: s.avl, + unusable: s.unusable, + } + } +} + +impl From for kvm_dtable { + fn from(dt: DescriptorTable) -> Self { + Self { + base: dt.base, + limit: dt.limit, + ..Default::default() + } + } +} + +impl From for DescriptorTable { + fn from(dt: kvm_dtable) -> Self { + Self { + base: dt.base, + limit: dt.limit, + } + } +} + +impl From for kvm_sregs { + fn from(s: SpecialRegisters) -> Self { + Self { + cs: s.cs.into(), + ds: s.ds.into(), + es: s.es.into(), + fs: s.fs.into(), + gs: s.gs.into(), + ss: s.ss.into(), + tr: s.tr.into(), + ldt: s.ldt.into(), + gdt: s.gdt.into(), + idt: s.idt.into(), + cr0: s.cr0, + cr2: s.cr2, + cr3: s.cr3, + cr4: s.cr4, + cr8: s.cr8, + efer: s.efer, + apic_base: s.apic_base, + interrupt_bitmap: s.interrupt_bitmap, + } + } +} + +impl From for SpecialRegisters { + fn from(s: kvm_sregs) -> Self { + Self { + cs: s.cs.into(), + ds: s.ds.into(), + es: s.es.into(), + fs: s.fs.into(), + gs: s.gs.into(), + ss: s.ss.into(), + tr: s.tr.into(), + ldt: s.ldt.into(), + gdt: s.gdt.into(), + idt: s.idt.into(), + cr0: s.cr0, + cr2: s.cr2, + cr3: s.cr3, + cr4: s.cr4, + cr8: s.cr8, + efer: s.efer, + apic_base: s.apic_base, + interrupt_bitmap: s.interrupt_bitmap, + } + } +} + +impl From for kvm_cpuid_entry2 { + fn from(e: CpuIdEntry) -> Self { + let flags = if e.flags & CPUID_FLAG_VALID_INDEX != 0 { + KVM_CPUID_FLAG_SIGNIFCANT_INDEX + } else { + 0 + }; + Self { + function: e.function, + index: e.index, + flags, + eax: e.eax, + ebx: e.ebx, + ecx: e.ecx, + edx: e.edx, + ..Default::default() + } + } +} + +impl From for CpuIdEntry { + fn from(e: kvm_cpuid_entry2) -> Self { + let flags = if e.flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX != 0 { + CPUID_FLAG_VALID_INDEX + } else { + 0 + }; + Self { + function: e.function, + index: e.index, + flags, + eax: e.eax, + ebx: e.ebx, + ecx: e.ecx, + edx: e.edx, + } + } +} + +impl From for FpuState { + fn from(s: kvm_fpu) -> Self { + Self { + fpr: s.fpr, + fcw: s.fcw, + fsw: s.fsw, + ftwx: s.ftwx, + last_opcode: s.last_opcode, + last_ip: s.last_ip, + last_dp: s.last_dp, + xmm: s.xmm, + mxcsr: s.mxcsr, + } + } +} + +impl From for kvm_fpu { + fn from(s: FpuState) -> Self { + Self { + fpr: s.fpr, + fcw: s.fcw, + fsw: s.fsw, + ftwx: s.ftwx, + last_opcode: s.last_opcode, + last_ip: s.last_ip, + last_dp: s.last_dp, + xmm: s.xmm, + mxcsr: s.mxcsr, + ..Default::default() + } + } +} + +impl From for kvm_lapic_state { + fn from(s: LapicState) -> Self { + Self { regs: s.regs } + } +} + +impl From for LapicState { + fn from(s: kvm_lapic_state) -> Self { + Self { regs: s.regs } + } +} + +impl From for MsrEntry { + fn from(e: kvm_msr_entry) -> Self { + Self { + index: e.index, + data: e.data, + } + } +} + +impl From for kvm_msr_entry { + fn from(e: MsrEntry) -> Self { + Self { + index: e.index, + data: e.data, + ..Default::default() + } + } +} + +impl From for XsaveState { + fn from(s: Xsave) -> Self { + Self { region: s.region } + } +} + +impl From for Xsave { + fn from(s: XsaveState) -> Self { + Self { + region: s.region, + extra: Default::default(), + } + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/lib.rs b/vendor/cube-hypervisor/hypervisor/src/lib.rs new file mode 100644 index 0000000..b9c8e11 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/lib.rs @@ -0,0 +1,187 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// + +//! A generic abstraction around hypervisor functionality +//! +//! This crate offers a trait abstraction for underlying hypervisors +//! +//! # Platform support +//! +//! - x86_64 +//! - arm64 +//! + +#![allow(clippy::significant_drop_in_scrutinee)] + +#[macro_use] +extern crate anyhow; +#[cfg(target_arch = "x86_64")] +#[macro_use] +extern crate log; + +/// Architecture specific definitions +#[macro_use] +pub mod arch; + +#[cfg(feature = "kvm")] +/// KVM implementation module +pub mod kvm; + +/// Microsoft Hypervisor implementation module +#[cfg(all(feature = "mshv", target_arch = "x86_64"))] +pub mod mshv; + +/// Hypevisor related module +mod hypervisor; + +/// Vm related module +mod vm; + +/// CPU related module +mod cpu; + +/// Device related module +mod device; + +pub use cpu::{HypervisorCpuError, Vcpu, VmExit}; +pub use device::HypervisorDeviceError; +pub use hypervisor::{Hypervisor, HypervisorError}; +#[cfg(all(feature = "kvm", target_arch = "aarch64"))] +pub use kvm::{aarch64, GicState}; +use std::sync::Arc; +pub use vm::{ + DataMatch, HypervisorVmError, InterruptSourceConfig, LegacyIrqSourceConfig, MsiIrqSourceConfig, + Vm, VmOps, +}; + +#[derive(Debug, Copy, Clone)] +pub enum HypervisorType { + Kvm, + Mshv, + KvmPvm, +} + +pub fn new() -> std::result::Result, HypervisorError> { + #[cfg(feature = "kvm")] + if kvm::KvmHypervisor::is_available()? { + return kvm::KvmHypervisor::new(); + } + + #[cfg(feature = "mshv")] + if mshv::MshvHypervisor::is_available()? { + return mshv::MshvHypervisor::new(); + } + + Err(HypervisorError::HypervisorCreate(anyhow!( + "no supported hypervisor" + ))) +} + +// Returns a `Vec` with a size in bytes at least as large as `size_in_bytes`. +fn vec_with_size_in_bytes(size_in_bytes: usize) -> Vec { + let rounded_size = (size_in_bytes + size_of::() - 1) / size_of::(); + let mut v = Vec::with_capacity(rounded_size); + v.resize_with(rounded_size, T::default); + v +} + +// The kvm API has many structs that resemble the following `Foo` structure: +// +// ``` +// #[repr(C)] +// struct Foo { +// some_data: u32 +// entries: __IncompleteArrayField<__u32>, +// } +// ``` +// +// In order to allocate such a structure, `size_of::()` would be too small because it would not +// include any space for `entries`. To make the allocation large enough while still being aligned +// for `Foo`, a `Vec` is created. Only the first element of `Vec` would actually be used +// as a `Foo`. The remaining memory in the `Vec` is for `entries`, which must be contiguous +// with `Foo`. This function is used to make the `Vec` with enough space for `count` entries. +use std::mem::size_of; +pub fn vec_with_array_field(count: usize) -> Vec { + let element_space = count * size_of::(); + let vec_size_bytes = size_of::() + element_space; + vec_with_size_in_bytes(vec_size_bytes) +} + +/// +/// User memory region structure +/// +#[derive(Debug, Default, Eq, PartialEq)] +pub struct UserMemoryRegion { + pub slot: u32, + pub guest_phys_addr: u64, + pub memory_size: u64, + pub userspace_addr: u64, + pub flags: u32, +} + +/// +/// Flags for user memory region +/// +pub const USER_MEMORY_REGION_READ: u32 = 1; +pub const USER_MEMORY_REGION_WRITE: u32 = 1 << 1; +pub const USER_MEMORY_REGION_EXECUTE: u32 = 1 << 2; +pub const USER_MEMORY_REGION_LOG_DIRTY: u32 = 1 << 3; + +#[derive(Debug)] +pub enum MpState { + #[cfg(feature = "kvm")] + Kvm(kvm_bindings::kvm_mp_state), + #[cfg(all(feature = "mshv", target_arch = "x86_64"))] + Mshv, /* MSHV does not supprt MpState yet */ +} + +#[derive(Debug, Clone, Copy)] +pub enum IoEventAddress { + Pio(u64), + Mmio(u64), +} + +#[derive(Clone, serde::Serialize, serde::Deserialize)] +#[allow(clippy::large_enum_variant)] +pub enum CpuState { + #[cfg(feature = "kvm")] + Kvm(kvm::VcpuKvmState), + #[cfg(all(feature = "mshv", target_arch = "x86_64"))] + Mshv(mshv::VcpuMshvState), +} + +#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)] +#[cfg(target_arch = "x86_64")] +pub enum ClockData { + #[cfg(feature = "kvm")] + Kvm(kvm_bindings::kvm_clock_data), + #[cfg(feature = "mshv")] + Mshv, /* MSHV does not supprt ClockData yet */ +} + +#[cfg(target_arch = "x86_64")] +impl ClockData { + pub fn reset_flags(&mut self) { + match self { + #[cfg(feature = "kvm")] + ClockData::Kvm(s) => s.flags = 0, + #[allow(unreachable_patterns)] + _ => {} + } + } +} + +#[derive(Copy, Clone)] +pub enum IrqRoutingEntry { + #[cfg(feature = "kvm")] + Kvm(kvm_bindings::kvm_irq_routing_entry), + #[cfg(feature = "mshv")] + Mshv(mshv_bindings::mshv_msi_routing_entry), +} diff --git a/vendor/cube-hypervisor/hypervisor/src/mshv/mod.rs b/vendor/cube-hypervisor/hypervisor/src/mshv/mod.rs new file mode 100644 index 0000000..4beb8ff --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/mshv/mod.rs @@ -0,0 +1,1208 @@ +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// + +use crate::arch::emulator::{PlatformEmulator, PlatformError}; + +#[cfg(target_arch = "x86_64")] +use crate::arch::x86::emulator::{Emulator, EmulatorCpuState}; +use crate::cpu; +use crate::cpu::Vcpu; +use crate::hypervisor; +use crate::vec_with_array_field; +use crate::vm::{self, InterruptSourceConfig, VmOps}; +use crate::HypervisorType; +use mshv_bindings::*; +use mshv_ioctls::{set_registers_64, Mshv, NoDatamatch, VcpuFd, VmFd}; +use std::any::Any; +use std::collections::HashMap; +use std::sync::{Arc, RwLock}; +use vfio_ioctls::VfioDeviceFd; +use vm::DataMatch; +// x86_64 dependencies +#[cfg(target_arch = "x86_64")] +pub mod x86_64; +use crate::{ + ClockData, CpuState, IoEventAddress, IrqRoutingEntry, MpState, UserMemoryRegion, + USER_MEMORY_REGION_EXECUTE, USER_MEMORY_REGION_READ, USER_MEMORY_REGION_WRITE, +}; +use vmm_sys_util::eventfd::EventFd; +#[cfg(target_arch = "x86_64")] +pub use x86_64::VcpuMshvState; +#[cfg(target_arch = "x86_64")] +pub use x86_64::*; + +#[cfg(target_arch = "x86_64")] +use std::fs::File; +use std::os::unix::io::AsRawFd; + +#[cfg(target_arch = "x86_64")] +use crate::arch::x86::{ + CpuIdEntry, FpuState, LapicState, MsrEntry, SpecialRegisters, StandardRegisters, +}; + +const DIRTY_BITMAP_CLEAR_DIRTY: u64 = 0x4; +const DIRTY_BITMAP_SET_DIRTY: u64 = 0x8; + +/// +/// Export generically-named wrappers of mshv-bindings for Unix-based platforms +/// +pub use { + mshv_bindings::mshv_create_device as CreateDevice, + mshv_bindings::mshv_device_attr as DeviceAttr, mshv_ioctls::DeviceFd, +}; + +pub const PAGE_SHIFT: usize = 12; + +impl From for UserMemoryRegion { + fn from(region: mshv_user_mem_region) -> Self { + let mut flags: u32 = 0; + if region.flags & HV_MAP_GPA_READABLE != 0 { + flags |= USER_MEMORY_REGION_READ; + } + if region.flags & HV_MAP_GPA_WRITABLE != 0 { + flags |= USER_MEMORY_REGION_WRITE; + } + if region.flags & HV_MAP_GPA_EXECUTABLE != 0 { + flags |= USER_MEMORY_REGION_EXECUTE; + } + + UserMemoryRegion { + guest_phys_addr: (region.guest_pfn << PAGE_SHIFT as u64) + + (region.userspace_addr & ((1 << PAGE_SHIFT) - 1)), + memory_size: region.size, + userspace_addr: region.userspace_addr, + flags, + ..Default::default() + } + } +} + +impl From for mshv_user_mem_region { + fn from(region: UserMemoryRegion) -> Self { + let mut flags: u32 = 0; + if region.flags & USER_MEMORY_REGION_READ != 0 { + flags |= HV_MAP_GPA_READABLE; + } + if region.flags & USER_MEMORY_REGION_WRITE != 0 { + flags |= HV_MAP_GPA_WRITABLE; + } + if region.flags & USER_MEMORY_REGION_EXECUTE != 0 { + flags |= HV_MAP_GPA_EXECUTABLE; + } + + mshv_user_mem_region { + guest_pfn: region.guest_phys_addr >> PAGE_SHIFT, + size: region.memory_size, + userspace_addr: region.userspace_addr, + flags, + } + } +} + +impl From for IoEventAddress { + fn from(a: mshv_ioctls::IoEventAddress) -> Self { + match a { + mshv_ioctls::IoEventAddress::Pio(x) => Self::Pio(x), + mshv_ioctls::IoEventAddress::Mmio(x) => Self::Mmio(x), + } + } +} + +impl From for mshv_ioctls::IoEventAddress { + fn from(a: IoEventAddress) -> Self { + match a { + IoEventAddress::Pio(x) => Self::Pio(x), + IoEventAddress::Mmio(x) => Self::Mmio(x), + } + } +} + +impl From for CpuState { + fn from(s: VcpuMshvState) -> Self { + CpuState::Mshv(s) + } +} + +impl From for VcpuMshvState { + fn from(s: CpuState) -> Self { + match s { + CpuState::Mshv(s) => s, + /* Needed in case other hypervisors are enabled */ + #[allow(unreachable_patterns)] + _ => panic!("CpuState is not valid"), + } + } +} + +impl From for IrqRoutingEntry { + fn from(s: mshv_msi_routing_entry) -> Self { + IrqRoutingEntry::Mshv(s) + } +} + +impl From for mshv_msi_routing_entry { + fn from(e: IrqRoutingEntry) -> Self { + match e { + IrqRoutingEntry::Mshv(e) => e, + /* Needed in case other hypervisors are enabled */ + #[allow(unreachable_patterns)] + _ => panic!("IrqRoutingEntry is not valid"), + } + } +} + +struct MshvDirtyLogSlot { + guest_pfn: u64, + memory_size: u64, +} + +/// Wrapper over mshv system ioctls. +pub struct MshvHypervisor { + mshv: Mshv, +} + +impl MshvHypervisor { + #[cfg(target_arch = "x86_64")] + /// + /// Retrieve the list of MSRs supported by MSHV. + /// + fn get_msr_list(&self) -> hypervisor::Result { + self.mshv + .get_msr_index_list() + .map_err(|e| hypervisor::HypervisorError::GetMsrList(e.into())) + } +} + +impl MshvHypervisor { + /// Create a hypervisor based on Mshv + #[allow(clippy::new_ret_no_self)] + pub fn new() -> hypervisor::Result> { + let mshv_obj = + Mshv::new().map_err(|e| hypervisor::HypervisorError::HypervisorCreate(e.into()))?; + Ok(Arc::new(MshvHypervisor { mshv: mshv_obj })) + } + /// Check if the hypervisor is available + pub fn is_available() -> hypervisor::Result { + match std::fs::metadata("/dev/mshv") { + Ok(_) => Ok(true), + Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(false), + Err(err) => Err(hypervisor::HypervisorError::HypervisorAvailableCheck( + err.into(), + )), + } + } +} +/// Implementation of Hypervisor trait for Mshv +/// Example: +/// #[cfg(feature = "mshv")] +/// extern crate hypervisor +/// let mshv = hypervisor::mshv::MshvHypervisor::new().unwrap(); +/// let hypervisor: Arc = Arc::new(mshv); +/// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); +/// +impl hypervisor::Hypervisor for MshvHypervisor { + /// + /// Returns the type of the hypervisor + /// + fn hypervisor_type(&self) -> HypervisorType { + HypervisorType::Mshv + } + /// Create a mshv vm object and return the object as Vm trait object + /// Example + /// # extern crate hypervisor; + /// # use hypervisor::MshvHypervisor; + /// use hypervisor::MshvVm; + /// let hypervisor = MshvHypervisor::new().unwrap(); + /// let vm = hypervisor.create_vm().unwrap() + /// + fn create_vm(&self) -> hypervisor::Result> { + let fd: VmFd; + loop { + match self.mshv.create_vm() { + Ok(res) => fd = res, + Err(e) => { + if e.errno() == libc::EINTR { + // If the error returned is EINTR, which means the + // ioctl has been interrupted, we have to retry as + // this can't be considered as a regular error. + continue; + } else { + return Err(hypervisor::HypervisorError::VmCreate(e.into())); + } + } + } + break; + } + + // Default Microsoft Hypervisor behavior for unimplemented MSR is to + // send a fault to the guest if it tries to access it. It is possible + // to override this behavior with a more suitable option i.e., ignore + // writes from the guest and return zero in attempt to read unimplemented + // MSR. + fd.set_partition_property( + hv_partition_property_code_HV_PARTITION_PROPERTY_UNIMPLEMENTED_MSR_ACTION, + hv_unimplemented_msr_action_HV_UNIMPLEMENTED_MSR_ACTION_IGNORE_WRITE_READ_ZERO as u64, + ) + .map_err(|e| hypervisor::HypervisorError::SetPartitionProperty(e.into()))?; + + let msr_list = self.get_msr_list()?; + let num_msrs = msr_list.as_fam_struct_ref().nmsrs as usize; + let mut msrs: Vec = vec![ + MsrEntry { + ..Default::default() + }; + num_msrs + ]; + let indices = msr_list.as_slice(); + for (pos, index) in indices.iter().enumerate() { + msrs[pos].index = *index; + } + let vm_fd = Arc::new(fd); + + Ok(Arc::new(MshvVm { + fd: vm_fd, + msrs, + dirty_log_slots: Arc::new(RwLock::new(HashMap::new())), + })) + } + /// + /// Get the supported CpuID + /// + fn get_cpuid(&self) -> hypervisor::Result> { + Ok(Vec::new()) + } +} + +/// Vcpu struct for Microsoft Hypervisor +pub struct MshvVcpu { + fd: VcpuFd, + vp_index: u8, + cpuid: Vec, + msrs: Vec, + vm_ops: Option>, +} + +/// Implementation of Vcpu trait for Microsoft Hypervisor +/// Example: +/// #[cfg(feature = "mshv")] +/// extern crate hypervisor +/// let mshv = hypervisor::mshv::MshvHypervisor::new().unwrap(); +/// let hypervisor: Arc = Arc::new(mshv); +/// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); +/// let vcpu = vm.create_vcpu(0).unwrap(); +/// vcpu.get/set().unwrap() +/// +impl cpu::Vcpu for MshvVcpu { + #[cfg(target_arch = "x86_64")] + /// + /// Returns the vCPU general purpose registers. + /// + fn get_regs(&self) -> cpu::Result { + Ok(self + .fd + .get_regs() + .map_err(|e| cpu::HypervisorCpuError::GetStandardRegs(e.into()))? + .into()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Sets the vCPU general purpose registers. + /// + fn set_regs(&self, regs: &StandardRegisters) -> cpu::Result<()> { + let regs = (*regs).into(); + self.fd + .set_regs(®s) + .map_err(|e| cpu::HypervisorCpuError::SetStandardRegs(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// Returns the vCPU special registers. + /// + fn get_sregs(&self) -> cpu::Result { + Ok(self + .fd + .get_sregs() + .map_err(|e| cpu::HypervisorCpuError::GetSpecialRegs(e.into()))? + .into()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Sets the vCPU special registers. + /// + fn set_sregs(&self, sregs: &SpecialRegisters) -> cpu::Result<()> { + let sregs = (*sregs).into(); + self.fd + .set_sregs(&sregs) + .map_err(|e| cpu::HypervisorCpuError::SetSpecialRegs(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// Returns the floating point state (FPU) from the vCPU. + /// + fn get_fpu(&self) -> cpu::Result { + Ok(self + .fd + .get_fpu() + .map_err(|e| cpu::HypervisorCpuError::GetFloatingPointRegs(e.into()))? + .into()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Set the floating point state (FPU) of a vCPU. + /// + fn set_fpu(&self, fpu: &FpuState) -> cpu::Result<()> { + let fpu: mshv_bindings::FloatingPointUnit = (*fpu).clone().into(); + self.fd + .set_fpu(&fpu) + .map_err(|e| cpu::HypervisorCpuError::SetFloatingPointRegs(e.into())) + } + + #[cfg(target_arch = "x86_64")] + /// + /// Returns the model-specific registers (MSR) for this vCPU. + /// + fn get_msrs(&self, msrs: &mut Vec) -> cpu::Result { + let mshv_msrs: Vec = msrs.iter().map(|e| (*e).into()).collect(); + let mut mshv_msrs = MsrEntries::from_entries(&mshv_msrs).unwrap(); + let succ = self + .fd + .get_msrs(&mut mshv_msrs) + .map_err(|e| cpu::HypervisorCpuError::GetMsrEntries(e.into()))?; + + msrs[..succ].copy_from_slice( + &mshv_msrs.as_slice()[..succ] + .iter() + .map(|e| (*e).into()) + .collect::>(), + ); + + Ok(succ) + } + #[cfg(target_arch = "x86_64")] + /// + /// Setup the model-specific registers (MSR) for this vCPU. + /// Returns the number of MSR entries actually written. + /// + fn set_msrs(&self, msrs: &[MsrEntry]) -> cpu::Result { + let mshv_msrs: Vec = msrs.iter().map(|e| (*e).into()).collect(); + let mshv_msrs = MsrEntries::from_entries(&mshv_msrs).unwrap(); + self.fd + .set_msrs(&mshv_msrs) + .map_err(|e| cpu::HypervisorCpuError::SetMsrEntries(e.into())) + } + + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call to enable HyperV SynIC + /// + fn enable_hyperv_synic(&self) -> cpu::Result<()> { + /* We always have SynIC enabled on MSHV */ + Ok(()) + } + #[allow(non_upper_case_globals)] + fn run(&self) -> std::result::Result { + let hv_message: hv_message = hv_message::default(); + match self.fd.run(hv_message) { + Ok(x) => match x.header.message_type { + hv_message_type_HVMSG_X64_HALT => { + debug!("HALT"); + Ok(cpu::VmExit::Reset) + } + hv_message_type_HVMSG_UNRECOVERABLE_EXCEPTION => { + warn!("TRIPLE FAULT"); + Ok(cpu::VmExit::Shutdown) + } + hv_message_type_HVMSG_X64_IO_PORT_INTERCEPT => { + let info = x.to_ioport_info().unwrap(); + let access_info = info.access_info; + // SAFETY: access_info is valid, otherwise we won't be here + let len = unsafe { access_info.__bindgen_anon_1.access_size() } as usize; + let is_write = info.header.intercept_access_type == 1; + let port = info.port_number; + let mut data: [u8; 4] = [0; 4]; + let mut ret_rax = info.rax; + + /* + * XXX: Ignore QEMU fw_cfg (0x5xx) and debug console (0x402) ports. + * + * Cloud Hypervisor doesn't support fw_cfg at the moment. It does support 0x402 + * under the "fwdebug" feature flag. But that feature is not enabled by default + * and is considered legacy. + * + * OVMF unconditionally pokes these IO ports with string IO. + * + * Instead of trying to implement string IO support now which does not do much + * now, skip those ports explicitly to avoid panicking. + * + * Proper string IO support can be added once we gain the ability to translate + * guest virtual addresses to guest physical addresses on MSHV. + */ + match port { + 0x402 | 0x510 | 0x511 | 0x514 => { + let insn_len = info.header.instruction_length() as u64; + + /* Advance RIP and update RAX */ + let arr_reg_name_value = [ + ( + hv_register_name_HV_X64_REGISTER_RIP, + info.header.rip + insn_len, + ), + (hv_register_name_HV_X64_REGISTER_RAX, ret_rax), + ]; + set_registers_64!(self.fd, arr_reg_name_value) + .map_err(|e| cpu::HypervisorCpuError::SetRegister(e.into()))?; + return Ok(cpu::VmExit::Ignore); + } + _ => {} + } + + // SAFETY: access_info is valid, otherwise we won't be here + assert!( + (unsafe { access_info.__bindgen_anon_1.string_op() } != 1), + "String IN/OUT not supported" + ); + assert!( + (unsafe { access_info.__bindgen_anon_1.rep_prefix() } != 1), + "Rep IN/OUT not supported" + ); + + if is_write { + let data = (info.rax as u32).to_le_bytes(); + if let Some(vm_ops) = &self.vm_ops { + vm_ops + .pio_write(port.into(), &data[0..len]) + .map_err(|e| cpu::HypervisorCpuError::RunVcpu(e.into()))?; + } + } else { + if let Some(vm_ops) = &self.vm_ops { + vm_ops + .pio_read(port.into(), &mut data[0..len]) + .map_err(|e| cpu::HypervisorCpuError::RunVcpu(e.into()))?; + } + + let v = u32::from_le_bytes(data); + /* Preserve high bits in EAX but clear out high bits in RAX */ + let mask = 0xffffffff >> (32 - len * 8); + let eax = (info.rax as u32 & !mask) | (v & mask); + ret_rax = eax as u64; + } + + let insn_len = info.header.instruction_length() as u64; + + /* Advance RIP and update RAX */ + let arr_reg_name_value = [ + ( + hv_register_name_HV_X64_REGISTER_RIP, + info.header.rip + insn_len, + ), + (hv_register_name_HV_X64_REGISTER_RAX, ret_rax), + ]; + set_registers_64!(self.fd, arr_reg_name_value) + .map_err(|e| cpu::HypervisorCpuError::SetRegister(e.into()))?; + Ok(cpu::VmExit::Ignore) + } + hv_message_type_HVMSG_UNMAPPED_GPA => { + let info = x.to_memory_info().unwrap(); + let insn_len = info.instruction_byte_count as usize; + assert!(insn_len > 0 && insn_len <= 16); + + let mut context = MshvEmulatorContext { + vcpu: self, + map: (info.guest_virtual_address, info.guest_physical_address), + }; + + // Create a new emulator. + let mut emul = Emulator::new(&mut context); + + // Emulate the trapped instruction, and only the first one. + let new_state = emul + .emulate_first_insn(self.vp_index as usize, &info.instruction_bytes) + .map_err(|e| cpu::HypervisorCpuError::RunVcpu(e.into()))?; + + // Set CPU state back. + context + .set_cpu_state(self.vp_index as usize, new_state) + .map_err(|e| cpu::HypervisorCpuError::RunVcpu(e.into()))?; + + Ok(cpu::VmExit::Ignore) + } + hv_message_type_HVMSG_X64_CPUID_INTERCEPT => { + let info = x.to_cpuid_info().unwrap(); + debug!("cpuid eax: {:x}", { info.rax }); + Ok(cpu::VmExit::Ignore) + } + hv_message_type_HVMSG_X64_MSR_INTERCEPT => { + let info = x.to_msr_info().unwrap(); + if info.header.intercept_access_type == 0 { + debug!("msr read: {:x}", { info.msr_number }); + } else { + debug!("msr write: {:x}", { info.msr_number }); + } + Ok(cpu::VmExit::Ignore) + } + hv_message_type_HVMSG_X64_EXCEPTION_INTERCEPT => { + //TODO: Handler for VMCALL here. + let info = x.to_exception_info().unwrap(); + debug!("Exception Info {:?}", { info.exception_vector }); + Ok(cpu::VmExit::Ignore) + } + exit => Err(cpu::HypervisorCpuError::RunVcpu(anyhow!( + "Unhandled VCPU exit {:?}", + exit + ))), + }, + + Err(e) => match e.errno() { + libc::EAGAIN | libc::EINTR => Ok(cpu::VmExit::Ignore), + _ => Err(cpu::HypervisorCpuError::RunVcpu(anyhow!( + "VCPU error {:?}", + e + ))), + }, + } + } + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call to setup the CPUID registers. + /// + fn set_cpuid2(&self, _cpuid: &[CpuIdEntry]) -> cpu::Result<()> { + Ok(()) + } + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call to retrieve the CPUID registers. + /// + fn get_cpuid2(&self, _num_entries: usize) -> cpu::Result> { + Ok(self.cpuid.clone()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Returns the state of the LAPIC (Local Advanced Programmable Interrupt Controller). + /// + fn get_lapic(&self) -> cpu::Result { + Ok(self + .fd + .get_lapic() + .map_err(|e| cpu::HypervisorCpuError::GetlapicState(e.into()))? + .into()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Sets the state of the LAPIC (Local Advanced Programmable Interrupt Controller). + /// + fn set_lapic(&self, lapic: &LapicState) -> cpu::Result<()> { + let lapic: mshv_bindings::LapicState = (*lapic).clone().into(); + self.fd + .set_lapic(&lapic) + .map_err(|e| cpu::HypervisorCpuError::SetLapicState(e.into())) + } + /// + /// Returns the vcpu's current "multiprocessing state". + /// + fn get_mp_state(&self) -> cpu::Result { + Ok(MpState::Mshv) + } + /// + /// Sets the vcpu's current "multiprocessing state". + /// + fn set_mp_state(&self, _mp_state: MpState) -> cpu::Result<()> { + Ok(()) + } + /// + /// Set CPU state + /// + fn set_state(&self, state: &CpuState) -> cpu::Result<()> { + let state: VcpuMshvState = state.clone().into(); + self.set_msrs(&state.msrs)?; + self.set_vcpu_events(&state.vcpu_events)?; + self.set_regs(&state.regs.into())?; + self.set_sregs(&state.sregs.into())?; + self.set_fpu(&state.fpu)?; + self.set_xcrs(&state.xcrs)?; + self.set_lapic(&state.lapic)?; + self.set_xsave(&state.xsave)?; + // These registers are global and needed to be set only for first VCPU + // as Microsoft Hypervisor allows setting this regsier for only one VCPU + if self.vp_index == 0 { + self.fd + .set_misc_regs(&state.misc) + .map_err(|e| cpu::HypervisorCpuError::SetMiscRegs(e.into()))? + } + self.fd + .set_debug_regs(&state.dbg) + .map_err(|e| cpu::HypervisorCpuError::SetDebugRegs(e.into()))?; + Ok(()) + } + /// + /// Get CPU State + /// + fn state(&self) -> cpu::Result { + let regs = self.get_regs()?; + let sregs = self.get_sregs()?; + let xcrs = self.get_xcrs()?; + let fpu = self.get_fpu()?; + let vcpu_events = self.get_vcpu_events()?; + let mut msrs = self.msrs.clone(); + self.get_msrs(&mut msrs)?; + let lapic = self.get_lapic()?; + let xsave = self.get_xsave()?; + let misc = self + .fd + .get_misc_regs() + .map_err(|e| cpu::HypervisorCpuError::GetMiscRegs(e.into()))?; + let dbg = self + .fd + .get_debug_regs() + .map_err(|e| cpu::HypervisorCpuError::GetDebugRegs(e.into()))?; + + Ok(VcpuMshvState { + msrs, + vcpu_events, + regs: regs.into(), + sregs: sregs.into(), + fpu, + xcrs, + lapic, + dbg, + xsave, + misc, + } + .into()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Translate guest virtual address to guest physical address + /// + fn translate_gva(&self, gva: u64, flags: u64) -> cpu::Result<(u64, u32)> { + let r = self + .fd + .translate_gva(gva, flags) + .map_err(|e| cpu::HypervisorCpuError::TranslateVirtualAddress(e.into()))?; + + let gpa = r.0; + // SAFETY: r is valid, otherwise this function will have returned + let result_code = unsafe { r.1.__bindgen_anon_1.result_code }; + + Ok((gpa, result_code)) + } + #[cfg(target_arch = "x86_64")] + /// + /// Return the list of initial MSR entries for a VCPU + /// + fn boot_msr_entries(&self) -> Vec { + use crate::arch::x86::{msr_index, MTRR_ENABLE, MTRR_MEM_TYPE_WB}; + + [ + msr!(msr_index::MSR_IA32_SYSENTER_CS), + msr!(msr_index::MSR_IA32_SYSENTER_ESP), + msr!(msr_index::MSR_IA32_SYSENTER_EIP), + msr!(msr_index::MSR_STAR), + msr!(msr_index::MSR_CSTAR), + msr!(msr_index::MSR_LSTAR), + msr!(msr_index::MSR_KERNEL_GS_BASE), + msr!(msr_index::MSR_SYSCALL_MASK), + msr_data!(msr_index::MSR_MTRRdefType, MTRR_ENABLE | MTRR_MEM_TYPE_WB), + ] + .to_vec() + } +} + +impl MshvVcpu { + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call that returns the vcpu's current "xsave struct". + /// + fn get_xsave(&self) -> cpu::Result { + self.fd + .get_xsave() + .map_err(|e| cpu::HypervisorCpuError::GetXsaveState(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call that sets the vcpu's current "xsave struct". + /// + fn set_xsave(&self, xsave: &Xsave) -> cpu::Result<()> { + self.fd + .set_xsave(xsave) + .map_err(|e| cpu::HypervisorCpuError::SetXsaveState(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call that returns the vcpu's current "xcrs". + /// + fn get_xcrs(&self) -> cpu::Result { + self.fd + .get_xcrs() + .map_err(|e| cpu::HypervisorCpuError::GetXcsr(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// X86 specific call that sets the vcpu's current "xcrs". + /// + fn set_xcrs(&self, xcrs: &ExtendedControlRegisters) -> cpu::Result<()> { + self.fd + .set_xcrs(xcrs) + .map_err(|e| cpu::HypervisorCpuError::SetXcsr(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// Returns currently pending exceptions, interrupts, and NMIs as well as related + /// states of the vcpu. + /// + fn get_vcpu_events(&self) -> cpu::Result { + self.fd + .get_vcpu_events() + .map_err(|e| cpu::HypervisorCpuError::GetVcpuEvents(e.into())) + } + #[cfg(target_arch = "x86_64")] + /// + /// Sets pending exceptions, interrupts, and NMIs as well as related states + /// of the vcpu. + /// + fn set_vcpu_events(&self, events: &VcpuEvents) -> cpu::Result<()> { + self.fd + .set_vcpu_events(events) + .map_err(|e| cpu::HypervisorCpuError::SetVcpuEvents(e.into())) + } +} + +struct MshvEmulatorContext<'a> { + vcpu: &'a MshvVcpu, + map: (u64, u64), // Initial GVA to GPA mapping provided by the hypervisor +} + +impl<'a> MshvEmulatorContext<'a> { + // Do the actual gva -> gpa translation + #[allow(non_upper_case_globals)] + fn translate(&self, gva: u64) -> Result { + if self.map.0 == gva { + return Ok(self.map.1); + } + + // TODO: More fine-grained control for the flags + let flags = HV_TRANSLATE_GVA_VALIDATE_READ | HV_TRANSLATE_GVA_VALIDATE_WRITE; + + let (gpa, result_code) = self + .vcpu + .translate_gva(gva, flags.into()) + .map_err(|e| PlatformError::TranslateVirtualAddress(anyhow!(e)))?; + + match result_code { + hv_translate_gva_result_code_HV_TRANSLATE_GVA_SUCCESS => Ok(gpa), + _ => Err(PlatformError::TranslateVirtualAddress(anyhow!(result_code))), + } + } +} + +/// Platform emulation for Hyper-V +impl<'a> PlatformEmulator for MshvEmulatorContext<'a> { + type CpuState = EmulatorCpuState; + + fn read_memory(&self, gva: u64, data: &mut [u8]) -> Result<(), PlatformError> { + let gpa = self.translate(gva)?; + debug!( + "mshv emulator: memory read {} bytes from [{:#x} -> {:#x}]", + data.len(), + gva, + gpa + ); + + if let Some(vm_ops) = &self.vcpu.vm_ops { + if vm_ops.guest_mem_read(gpa, data).is_err() { + vm_ops + .mmio_read(gpa, data) + .map_err(|e| PlatformError::MemoryReadFailure(e.into()))?; + } + } + + Ok(()) + } + + fn write_memory(&mut self, gva: u64, data: &[u8]) -> Result<(), PlatformError> { + let gpa = self.translate(gva)?; + debug!( + "mshv emulator: memory write {} bytes at [{:#x} -> {:#x}]", + data.len(), + gva, + gpa + ); + + if let Some(vm_ops) = &self.vcpu.vm_ops { + if vm_ops.guest_mem_write(gpa, data).is_err() { + vm_ops + .mmio_write(gpa, data) + .map_err(|e| PlatformError::MemoryWriteFailure(e.into()))?; + } + } + + Ok(()) + } + + fn cpu_state(&self, cpu_id: usize) -> Result { + if cpu_id != self.vcpu.vp_index as usize { + return Err(PlatformError::GetCpuStateFailure(anyhow!( + "CPU id mismatch {:?} {:?}", + cpu_id, + self.vcpu.vp_index + ))); + } + + let regs = self + .vcpu + .get_regs() + .map_err(|e| PlatformError::GetCpuStateFailure(e.into()))?; + let sregs = self + .vcpu + .get_sregs() + .map_err(|e| PlatformError::GetCpuStateFailure(e.into()))?; + + debug!("mshv emulator: Getting new CPU state"); + debug!("mshv emulator: {:#x?}", regs); + + Ok(EmulatorCpuState { regs, sregs }) + } + + fn set_cpu_state(&self, cpu_id: usize, state: Self::CpuState) -> Result<(), PlatformError> { + if cpu_id != self.vcpu.vp_index as usize { + return Err(PlatformError::SetCpuStateFailure(anyhow!( + "CPU id mismatch {:?} {:?}", + cpu_id, + self.vcpu.vp_index + ))); + } + + debug!("mshv emulator: Setting new CPU state"); + debug!("mshv emulator: {:#x?}", state.regs); + + self.vcpu + .set_regs(&state.regs) + .map_err(|e| PlatformError::SetCpuStateFailure(e.into()))?; + self.vcpu + .set_sregs(&state.sregs) + .map_err(|e| PlatformError::SetCpuStateFailure(e.into())) + } + + fn gva_to_gpa(&self, gva: u64) -> Result { + self.translate(gva) + } + + fn fetch(&self, _ip: u64, _instruction_bytes: &mut [u8]) -> Result<(), PlatformError> { + Err(PlatformError::MemoryReadFailure(anyhow!("unimplemented"))) + } +} + +/// Wrapper over Mshv VM ioctls. +pub struct MshvVm { + fd: Arc, + msrs: Vec, + dirty_log_slots: Arc>>, +} + +impl MshvVm { + /// + /// Creates an in-kernel device. + /// + /// See the documentation for `MSHV_CREATE_DEVICE`. + fn create_device(&self, device: &mut CreateDevice) -> vm::Result { + let device_fd = self + .fd + .create_device(device) + .map_err(|e| vm::HypervisorVmError::CreateDevice(e.into()))?; + Ok(VfioDeviceFd::new_from_mshv(device_fd)) + } +} + +/// +/// Implementation of Vm trait for Mshv +/// Example: +/// #[cfg(feature = "mshv")] +/// # extern crate hypervisor; +/// # use hypervisor::MshvHypervisor; +/// let mshv = MshvHypervisor::new().unwrap(); +/// let hypervisor: Arc = Arc::new(mshv); +/// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); +/// vm.set/get().unwrap() +/// +impl vm::Vm for MshvVm { + #[cfg(target_arch = "x86_64")] + /// + /// Sets the address of the one-page region in the VM's address space. + /// + fn set_identity_map_address(&self, _address: u64) -> vm::Result<()> { + Ok(()) + } + #[cfg(target_arch = "x86_64")] + /// + /// Sets the address of the three-page region in the VM's address space. + /// + fn set_tss_address(&self, _offset: usize) -> vm::Result<()> { + Ok(()) + } + /// + /// Creates an in-kernel interrupt controller. + /// + fn create_irq_chip(&self) -> vm::Result<()> { + Ok(()) + } + /// + /// Registers an event that will, when signaled, trigger the `gsi` IRQ. + /// + fn register_irqfd(&self, fd: &EventFd, gsi: u32) -> vm::Result<()> { + debug!("register_irqfd fd {} gsi {}", fd.as_raw_fd(), gsi); + + self.fd + .register_irqfd(fd, gsi) + .map_err(|e| vm::HypervisorVmError::RegisterIrqFd(e.into()))?; + + Ok(()) + } + /// + /// Unregisters an event that will, when signaled, trigger the `gsi` IRQ. + /// + fn unregister_irqfd(&self, fd: &EventFd, gsi: u32) -> vm::Result<()> { + debug!("unregister_irqfd fd {} gsi {}", fd.as_raw_fd(), gsi); + + self.fd + .unregister_irqfd(fd, gsi) + .map_err(|e| vm::HypervisorVmError::UnregisterIrqFd(e.into()))?; + + Ok(()) + } + /// + /// Creates a VcpuFd object from a vcpu RawFd. + /// + fn create_vcpu( + &self, + id: u8, + vm_ops: Option>, + ) -> vm::Result> { + let vcpu_fd = self + .fd + .create_vcpu(id) + .map_err(|e| vm::HypervisorVmError::CreateVcpu(e.into()))?; + let vcpu = MshvVcpu { + fd: vcpu_fd, + vp_index: id, + cpuid: Vec::new(), + msrs: self.msrs.clone(), + vm_ops, + }; + Ok(Arc::new(vcpu)) + } + #[cfg(target_arch = "x86_64")] + fn enable_split_irq(&self) -> vm::Result<()> { + Ok(()) + } + #[cfg(target_arch = "x86_64")] + fn enable_sgx_attribute(&self, _file: File) -> vm::Result<()> { + Ok(()) + } + fn register_ioevent( + &self, + fd: &EventFd, + addr: &IoEventAddress, + datamatch: Option, + ) -> vm::Result<()> { + let addr = &mshv_ioctls::IoEventAddress::from(*addr); + debug!( + "register_ioevent fd {} addr {:x?} datamatch {:?}", + fd.as_raw_fd(), + addr, + datamatch + ); + if let Some(dm) = datamatch { + match dm { + vm::DataMatch::DataMatch32(mshv_dm32) => self + .fd + .register_ioevent(fd, addr, mshv_dm32) + .map_err(|e| vm::HypervisorVmError::RegisterIoEvent(e.into())), + vm::DataMatch::DataMatch64(mshv_dm64) => self + .fd + .register_ioevent(fd, addr, mshv_dm64) + .map_err(|e| vm::HypervisorVmError::RegisterIoEvent(e.into())), + } + } else { + self.fd + .register_ioevent(fd, addr, NoDatamatch) + .map_err(|e| vm::HypervisorVmError::RegisterIoEvent(e.into())) + } + } + /// Unregister an event from a certain address it has been previously registered to. + fn unregister_ioevent(&self, fd: &EventFd, addr: &IoEventAddress) -> vm::Result<()> { + let addr = &mshv_ioctls::IoEventAddress::from(*addr); + debug!("unregister_ioevent fd {} addr {:x?}", fd.as_raw_fd(), addr); + + self.fd + .unregister_ioevent(fd, addr, NoDatamatch) + .map_err(|e| vm::HypervisorVmError::UnregisterIoEvent(e.into())) + } + + /// Creates a guest physical memory region. + fn create_user_memory_region(&self, user_memory_region: UserMemoryRegion) -> vm::Result<()> { + let user_memory_region: mshv_user_mem_region = user_memory_region.into(); + // No matter read only or not we keep track the slots. + // For readonly hypervisor can enable the dirty bits, + // but a VM exit happens before setting the dirty bits + self.dirty_log_slots.write().unwrap().insert( + user_memory_region.guest_pfn, + MshvDirtyLogSlot { + guest_pfn: user_memory_region.guest_pfn, + memory_size: user_memory_region.size, + }, + ); + + self.fd + .map_user_memory(user_memory_region) + .map_err(|e| vm::HypervisorVmError::CreateUserMemory(e.into()))?; + Ok(()) + } + + /// Removes a guest physical memory region. + fn remove_user_memory_region(&self, user_memory_region: UserMemoryRegion) -> vm::Result<()> { + let user_memory_region: mshv_user_mem_region = user_memory_region.into(); + // Remove the corresponding entry from "self.dirty_log_slots" if needed + self.dirty_log_slots + .write() + .unwrap() + .remove(&user_memory_region.guest_pfn); + + self.fd + .unmap_user_memory(user_memory_region) + .map_err(|e| vm::HypervisorVmError::RemoveUserMemory(e.into()))?; + Ok(()) + } + + fn make_user_memory_region( + &self, + _slot: u32, + guest_phys_addr: u64, + memory_size: u64, + userspace_addr: u64, + readonly: bool, + _log_dirty_pages: bool, + ) -> UserMemoryRegion { + let mut flags = HV_MAP_GPA_READABLE | HV_MAP_GPA_EXECUTABLE; + if !readonly { + flags |= HV_MAP_GPA_WRITABLE; + } + + mshv_user_mem_region { + flags, + guest_pfn: guest_phys_addr >> PAGE_SHIFT, + size: memory_size, + userspace_addr, + } + .into() + } + + fn create_passthrough_device(&self) -> vm::Result { + let mut vfio_dev = mshv_create_device { + type_: mshv_device_type_MSHV_DEV_TYPE_VFIO, + fd: 0, + flags: 0, + }; + + self.create_device(&mut vfio_dev) + .map_err(|e| vm::HypervisorVmError::CreatePassthroughDevice(e.into())) + } + + /// + /// Constructs a routing entry + /// + fn make_routing_entry(&self, gsi: u32, config: &InterruptSourceConfig) -> IrqRoutingEntry { + match config { + InterruptSourceConfig::MsiIrq(cfg) => mshv_msi_routing_entry { + gsi, + address_lo: cfg.low_addr, + address_hi: cfg.high_addr, + data: cfg.data, + } + .into(), + _ => { + unreachable!() + } + } + } + + fn set_gsi_routing(&self, entries: &[IrqRoutingEntry]) -> vm::Result<()> { + let mut msi_routing = + vec_with_array_field::(entries.len()); + msi_routing[0].nr = entries.len() as u32; + + let entries: Vec = entries + .iter() + .map(|entry| match entry { + IrqRoutingEntry::Mshv(e) => *e, + #[allow(unreachable_patterns)] + _ => panic!("IrqRoutingEntry type is wrong"), + }) + .collect(); + + // SAFETY: msi_routing initialized with entries.len() and now it is being turned into + // entries_slice with entries.len() again. It is guaranteed to be large enough to hold + // everything from entries. + unsafe { + let entries_slice: &mut [mshv_msi_routing_entry] = + msi_routing[0].entries.as_mut_slice(entries.len()); + entries_slice.copy_from_slice(&entries); + } + + self.fd + .set_msi_routing(&msi_routing[0]) + .map_err(|e| vm::HypervisorVmError::SetGsiRouting(e.into())) + } + /// + /// Start logging dirty pages + /// + fn start_dirty_log(&self) -> vm::Result<()> { + self.fd + .enable_dirty_page_tracking() + .map_err(|e| vm::HypervisorVmError::StartDirtyLog(e.into())) + } + /// + /// Stop logging dirty pages + /// + fn stop_dirty_log(&self) -> vm::Result<()> { + let dirty_log_slots = self.dirty_log_slots.read().unwrap(); + // Before disabling the dirty page tracking we need + // to set the dirty bits in the Hypervisor + // This is a requirement from Microsoft Hypervisor + for (_, s) in dirty_log_slots.iter() { + self.fd + .get_dirty_log(s.guest_pfn, s.memory_size as usize, DIRTY_BITMAP_SET_DIRTY) + .map_err(|e| vm::HypervisorVmError::StartDirtyLog(e.into()))?; + } + self.fd + .disable_dirty_page_tracking() + .map_err(|e| vm::HypervisorVmError::StartDirtyLog(e.into()))?; + Ok(()) + } + /// + /// Get dirty pages bitmap (one bit per page) + /// + fn get_dirty_log(&self, _slot: u32, base_gpa: u64, memory_size: u64) -> vm::Result> { + self.fd + .get_dirty_log( + base_gpa >> PAGE_SHIFT, + memory_size as usize, + DIRTY_BITMAP_CLEAR_DIRTY, + ) + .map_err(|e| vm::HypervisorVmError::GetDirtyLog(e.into())) + } + /// Retrieve guest clock. + #[cfg(target_arch = "x86_64")] + fn get_clock(&self) -> vm::Result { + Ok(ClockData::Mshv) + } + /// Set guest clock. + #[cfg(target_arch = "x86_64")] + fn set_clock(&self, _data: &ClockData) -> vm::Result<()> { + Ok(()) + } + /// Downcast to the underlying MshvVm type + fn as_any(&self) -> &dyn Any { + self + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/mshv/x86_64/mod.rs b/vendor/cube-hypervisor/hypervisor/src/mshv/x86_64/mod.rs new file mode 100644 index 0000000..af4bf39 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/mshv/x86_64/mod.rs @@ -0,0 +1,319 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// +use crate::arch::x86::{ + CpuIdEntry, DescriptorTable, FpuState, LapicState, MsrEntry, SegmentRegister, SpecialRegisters, + StandardRegisters, +}; +use serde::{Deserialize, Serialize}; +use std::fmt; + +/// +/// Export generically-named wrappers of mshv_bindings for Unix-based platforms +/// +pub use { + mshv_bindings::hv_cpuid_entry, mshv_bindings::mshv_user_mem_region as MemoryRegion, + mshv_bindings::msr_entry, mshv_bindings::CpuId, mshv_bindings::DebugRegisters, + mshv_bindings::FloatingPointUnit, mshv_bindings::LapicState as MshvLapicState, + mshv_bindings::MiscRegs as MiscRegisters, mshv_bindings::MsrList, + mshv_bindings::Msrs as MsrEntries, mshv_bindings::Msrs, + mshv_bindings::SegmentRegister as MshvSegmentRegister, + mshv_bindings::SpecialRegisters as MshvSpecialRegisters, + mshv_bindings::StandardRegisters as MshvStandardRegisters, mshv_bindings::SuspendRegisters, + mshv_bindings::TableRegister, mshv_bindings::VcpuEvents, mshv_bindings::XSave as Xsave, + mshv_bindings::Xcrs as ExtendedControlRegisters, +}; + +#[derive(Clone, Serialize, Deserialize)] +pub struct VcpuMshvState { + pub msrs: Vec, + pub vcpu_events: VcpuEvents, + pub regs: MshvStandardRegisters, + pub sregs: MshvSpecialRegisters, + pub fpu: FpuState, + pub xcrs: ExtendedControlRegisters, + pub lapic: LapicState, + pub dbg: DebugRegisters, + pub xsave: Xsave, + pub misc: MiscRegisters, +} + +impl fmt::Display for VcpuMshvState { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let expected_num_msrs = self.msrs.len(); + let mut msr_entries = vec![vec![0; 2]; expected_num_msrs]; + + for (i, entry) in self.msrs.iter().enumerate() { + msr_entries[i][1] = entry.data; + msr_entries[i][0] = entry.index as u64; + } + write!(f, "Number of MSRs: {}: MSRs: {:#010X?}, -- VCPU Events: {:?} -- Standard registers: {:?} Special Registers: {:?} ---- Floating Point Unit: {:?} --- Extended Control Register: {:?} --- Local APIC: {:?} --- DBG: {:?} --- Xsave: {:?}", + msr_entries.len(), + msr_entries, + self.vcpu_events, + self.regs, + self.sregs, + self.fpu, + self.xcrs, + self.lapic, + self.dbg, + self.xsave, + ) + } +} + +impl From for MshvStandardRegisters { + fn from(regs: StandardRegisters) -> Self { + Self { + rax: regs.rax, + rbx: regs.rbx, + rcx: regs.rcx, + rdx: regs.rdx, + rsi: regs.rsi, + rdi: regs.rdi, + rsp: regs.rsp, + rbp: regs.rbp, + r8: regs.r8, + r9: regs.r9, + r10: regs.r10, + r11: regs.r11, + r12: regs.r12, + r13: regs.r13, + r14: regs.r14, + r15: regs.r15, + rip: regs.rip, + rflags: regs.rflags, + } + } +} + +impl From for StandardRegisters { + fn from(regs: MshvStandardRegisters) -> Self { + Self { + rax: regs.rax, + rbx: regs.rbx, + rcx: regs.rcx, + rdx: regs.rdx, + rsi: regs.rsi, + rdi: regs.rdi, + rsp: regs.rsp, + rbp: regs.rbp, + r8: regs.r8, + r9: regs.r9, + r10: regs.r10, + r11: regs.r11, + r12: regs.r12, + r13: regs.r13, + r14: regs.r14, + r15: regs.r15, + rip: regs.rip, + rflags: regs.rflags, + } + } +} + +impl From for MshvSegmentRegister { + fn from(s: SegmentRegister) -> Self { + Self { + base: s.base, + limit: s.limit, + selector: s.selector, + type_: s.type_, + present: s.present, + dpl: s.dpl, + db: s.db, + s: s.s, + l: s.l, + g: s.g, + avl: s.avl, + unusable: s.unusable, + ..Default::default() + } + } +} + +impl From for SegmentRegister { + fn from(s: MshvSegmentRegister) -> Self { + Self { + base: s.base, + limit: s.limit, + selector: s.selector, + type_: s.type_, + present: s.present, + dpl: s.dpl, + db: s.db, + s: s.s, + l: s.l, + g: s.g, + avl: s.avl, + unusable: s.unusable, + } + } +} + +impl From for TableRegister { + fn from(dt: DescriptorTable) -> Self { + Self { + base: dt.base, + limit: dt.limit, + } + } +} + +impl From for DescriptorTable { + fn from(dt: TableRegister) -> Self { + Self { + base: dt.base, + limit: dt.limit, + } + } +} + +impl From for MshvSpecialRegisters { + fn from(s: SpecialRegisters) -> Self { + Self { + cs: s.cs.into(), + ds: s.ds.into(), + es: s.es.into(), + fs: s.fs.into(), + gs: s.gs.into(), + ss: s.ss.into(), + tr: s.tr.into(), + ldt: s.ldt.into(), + gdt: s.gdt.into(), + idt: s.idt.into(), + cr0: s.cr0, + cr2: s.cr2, + cr3: s.cr3, + cr4: s.cr4, + cr8: s.cr8, + efer: s.efer, + apic_base: s.apic_base, + interrupt_bitmap: s.interrupt_bitmap, + } + } +} + +impl From for SpecialRegisters { + fn from(s: MshvSpecialRegisters) -> Self { + Self { + cs: s.cs.into(), + ds: s.ds.into(), + es: s.es.into(), + fs: s.fs.into(), + gs: s.gs.into(), + ss: s.ss.into(), + tr: s.tr.into(), + ldt: s.ldt.into(), + gdt: s.gdt.into(), + idt: s.idt.into(), + cr0: s.cr0, + cr2: s.cr2, + cr3: s.cr3, + cr4: s.cr4, + cr8: s.cr8, + efer: s.efer, + apic_base: s.apic_base, + interrupt_bitmap: s.interrupt_bitmap, + } + } +} + +impl From for hv_cpuid_entry { + fn from(e: CpuIdEntry) -> Self { + Self { + function: e.function, + index: e.index, + flags: e.flags, + eax: e.eax, + ebx: e.ebx, + ecx: e.ecx, + edx: e.edx, + ..Default::default() + } + } +} + +impl From for CpuIdEntry { + fn from(e: hv_cpuid_entry) -> Self { + Self { + function: e.function, + index: e.index, + flags: e.flags, + eax: e.eax, + ebx: e.ebx, + ecx: e.ecx, + edx: e.edx, + } + } +} + +impl From for FpuState { + fn from(s: FloatingPointUnit) -> Self { + Self { + fpr: s.fpr, + fcw: s.fcw, + fsw: s.fsw, + ftwx: s.ftwx, + last_opcode: s.last_opcode, + last_ip: s.last_ip, + last_dp: s.last_dp, + xmm: s.xmm, + mxcsr: s.mxcsr, + } + } +} + +impl From for FloatingPointUnit { + fn from(s: FpuState) -> Self { + Self { + fpr: s.fpr, + fcw: s.fcw, + fsw: s.fsw, + ftwx: s.ftwx, + last_opcode: s.last_opcode, + last_ip: s.last_ip, + last_dp: s.last_dp, + xmm: s.xmm, + mxcsr: s.mxcsr, + ..Default::default() + } + } +} + +impl From for MshvLapicState { + fn from(s: LapicState) -> Self { + Self { regs: s.regs } + } +} + +impl From for LapicState { + fn from(s: MshvLapicState) -> Self { + Self { regs: s.regs } + } +} + +impl From for MsrEntry { + fn from(e: msr_entry) -> Self { + Self { + index: e.index, + data: e.data, + } + } +} + +impl From for msr_entry { + fn from(e: MsrEntry) -> Self { + Self { + index: e.index, + data: e.data, + ..Default::default() + } + } +} diff --git a/vendor/cube-hypervisor/hypervisor/src/vm.rs b/vendor/cube-hypervisor/hypervisor/src/vm.rs new file mode 100644 index 0000000..cb52d54 --- /dev/null +++ b/vendor/cube-hypervisor/hypervisor/src/vm.rs @@ -0,0 +1,361 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// + +#[cfg(target_arch = "aarch64")] +use crate::aarch64::VcpuInit; +#[cfg(target_arch = "aarch64")] +use crate::arch::aarch64::gic::{Vgic, VgicConfig}; +#[cfg(feature = "tdx")] +use crate::arch::x86::CpuIdEntry; +use crate::cpu::Vcpu; +#[cfg(target_arch = "x86_64")] +use crate::ClockData; +use crate::UserMemoryRegion; +use crate::{IoEventAddress, IrqRoutingEntry}; +use std::any::Any; +#[cfg(target_arch = "x86_64")] +use std::fs::File; +use std::sync::Arc; +#[cfg(target_arch = "aarch64")] +use std::sync::Mutex; +use thiserror::Error; +use vmm_sys_util::eventfd::EventFd; + +/// +/// I/O events data matches (32 or 64 bits). +/// +#[derive(Debug)] +pub enum DataMatch { + DataMatch32(u32), + DataMatch64(u64), +} + +impl From for u64 { + fn from(dm: DataMatch) -> u64 { + match dm { + DataMatch::DataMatch32(dm) => dm.into(), + DataMatch::DataMatch64(dm) => dm, + } + } +} + +#[derive(Error, Debug)] +/// +/// Enum for VM error +pub enum HypervisorVmError { + /// + /// Create Vcpu error + /// + #[error("Failed to create Vcpu: {0}")] + CreateVcpu(#[source] anyhow::Error), + /// + /// Identity map address error + /// + #[error("Failed to set identity map address: {0}")] + SetIdentityMapAddress(#[source] anyhow::Error), + /// + /// TSS address error + /// + #[error("Failed to set TSS address: {0}")] + SetTssAddress(#[source] anyhow::Error), + /// + /// Create interrupt controller error + /// + #[error("Failed to create interrupt controller: {0}")] + CreateIrq(#[source] anyhow::Error), + /// + /// Register interrupt event error + /// + #[error("Failed to register interrupt event: {0}")] + RegisterIrqFd(#[source] anyhow::Error), + /// + /// Un register interrupt event error + /// + #[error("Failed to unregister interrupt event: {0}")] + UnregisterIrqFd(#[source] anyhow::Error), + /// + /// Register IO event error + /// + #[error("Failed to register IO event: {0}")] + RegisterIoEvent(#[source] anyhow::Error), + /// + /// Unregister IO event error + /// + #[error("Failed to unregister IO event: {0}")] + UnregisterIoEvent(#[source] anyhow::Error), + /// + /// Set GSI routing error + /// + #[error("Failed to set GSI routing: {0}")] + SetGsiRouting(#[source] anyhow::Error), + /// + /// Create user memory error + /// + #[error("Failed to create user memory: {0}")] + CreateUserMemory(#[source] anyhow::Error), + /// + /// Remove user memory region error + /// + #[error("Failed to remove user memory: {0}")] + RemoveUserMemory(#[source] anyhow::Error), + /// + /// Create device error + /// + #[error("Failed to set GSI routing: {0}")] + CreateDevice(#[source] anyhow::Error), + /// + /// Get preferred target error + /// + #[error("Failed to get preferred target: {0}")] + GetPreferredTarget(#[source] anyhow::Error), + /// + /// Enable split Irq error + /// + #[error("Failed to enable split Irq: {0}")] + EnableSplitIrq(#[source] anyhow::Error), + /// + /// Enable SGX attribute error + /// + #[error("Failed to enable SGX attribute: {0}")] + EnableSgxAttribute(#[source] anyhow::Error), + /// + /// Get clock error + /// + #[error("Failed to get clock: {0}")] + GetClock(#[source] anyhow::Error), + /// + /// Set clock error + /// + #[error("Failed to set clock: {0}")] + SetClock(#[source] anyhow::Error), + /// + /// Create passthrough device + /// + #[error("Failed to create passthrough device: {0}")] + CreatePassthroughDevice(#[source] anyhow::Error), + /// Write to Guest memory + /// + #[error("Failed to write to guest memory: {0}")] + GuestMemWrite(#[source] anyhow::Error), + /// + /// Read Guest memory + /// + #[error("Failed to read guest memory: {0}")] + GuestMemRead(#[source] anyhow::Error), + /// + /// Read from MMIO Bus + /// + #[error("Failed to read from MMIO Bus: {0}")] + MmioBusRead(#[source] anyhow::Error), + /// + /// Write to MMIO Bus + /// + #[error("Failed to write to MMIO Bus: {0}")] + MmioBusWrite(#[source] anyhow::Error), + /// + /// Read from IO Bus + /// + #[error("Failed to read from IO Bus: {0}")] + IoBusRead(#[source] anyhow::Error), + /// + /// Write to IO Bus + /// + #[error("Failed to write to IO Bus: {0}")] + IoBusWrite(#[source] anyhow::Error), + /// + /// Start dirty log error + /// + #[error("Failed to get dirty log: {0}")] + StartDirtyLog(#[source] anyhow::Error), + /// + /// Stop dirty log error + /// + #[error("Failed to get dirty log: {0}")] + StopDirtyLog(#[source] anyhow::Error), + /// + /// Get dirty log error + /// + #[error("Failed to get dirty log: {0}")] + GetDirtyLog(#[source] anyhow::Error), + /// + /// Assert virtual interrupt error + /// + #[error("Failed to assert virtual Interrupt: {0}")] + AsserttVirtualInterrupt(#[source] anyhow::Error), + + #[cfg(feature = "tdx")] + /// + /// Error initializing TDX on the VM + /// + #[error("Failed to initialize TDX: {0}")] + InitializeTdx(#[source] std::io::Error), + #[cfg(feature = "tdx")] + /// + /// Error finalizing the TDX configuration on the VM + /// + #[error("Failed to finalize TDX: {0}")] + FinalizeTdx(#[source] std::io::Error), + #[cfg(feature = "tdx")] + /// + /// Error initializing the TDX memory region + /// + #[error("Failed to initialize memory region TDX: {0}")] + InitMemRegionTdx(#[source] std::io::Error), + /// + /// Create Vgic error + /// + #[error("Failed to create Vgic: {0}")] + CreateVgic(#[source] anyhow::Error), +} +/// +/// Result type for returning from a function +/// +pub type Result = std::result::Result; + +/// Configuration data for legacy interrupts. +/// +/// On x86 platforms, legacy interrupts means those interrupts routed through PICs or IOAPICs. +#[derive(Copy, Clone, Debug)] +pub struct LegacyIrqSourceConfig { + pub irqchip: u32, + pub pin: u32, +} + +/// Configuration data for MSI/MSI-X interrupts. +/// +/// On x86 platforms, these interrupts are vectors delivered directly to the LAPIC. +#[derive(Copy, Clone, Debug, Default)] +pub struct MsiIrqSourceConfig { + /// High address to delivery message signaled interrupt. + pub high_addr: u32, + /// Low address to delivery message signaled interrupt. + pub low_addr: u32, + /// Data to write to delivery message signaled interrupt. + pub data: u32, + /// Unique ID of the device to delivery message signaled interrupt. + pub devid: u32, +} + +/// Configuration data for an interrupt source. +#[derive(Copy, Clone, Debug)] +pub enum InterruptSourceConfig { + /// Configuration data for Legacy interrupts. + LegacyIrq(LegacyIrqSourceConfig), + /// Configuration data for PciMsi, PciMsix and generic MSI interrupts. + MsiIrq(MsiIrqSourceConfig), +} + +/// +/// Trait to represent a Vm +/// +/// This crate provides a hypervisor-agnostic interfaces for Vm +/// +pub trait Vm: Send + Sync + Any { + #[cfg(target_arch = "x86_64")] + /// Sets the address of the one-page region in the VM's address space. + fn set_identity_map_address(&self, address: u64) -> Result<()>; + #[cfg(target_arch = "x86_64")] + /// Sets the address of the three-page region in the VM's address space. + fn set_tss_address(&self, offset: usize) -> Result<()>; + /// Creates an in-kernel interrupt controller. + fn create_irq_chip(&self) -> Result<()>; + /// Registers an event that will, when signaled, trigger the `gsi` IRQ. + fn register_irqfd(&self, fd: &EventFd, gsi: u32) -> Result<()>; + /// Unregister an event that will, when signaled, trigger the `gsi` IRQ. + fn unregister_irqfd(&self, fd: &EventFd, gsi: u32) -> Result<()>; + /// Creates a new KVM vCPU file descriptor and maps the memory corresponding + fn create_vcpu(&self, id: u8, vm_ops: Option>) -> Result>; + #[cfg(target_arch = "aarch64")] + fn create_vgic(&self, config: VgicConfig) -> Result>>; + + /// Registers an event to be signaled whenever a certain address is written to. + fn register_ioevent( + &self, + fd: &EventFd, + addr: &IoEventAddress, + datamatch: Option, + ) -> Result<()>; + /// Unregister an event from a certain address it has been previously registered to. + fn unregister_ioevent(&self, fd: &EventFd, addr: &IoEventAddress) -> Result<()>; + // Construct a routing entry + fn make_routing_entry(&self, gsi: u32, config: &InterruptSourceConfig) -> IrqRoutingEntry; + /// Sets the GSI routing table entries, overwriting any previously set + fn set_gsi_routing(&self, entries: &[IrqRoutingEntry]) -> Result<()>; + /// Creates a memory region structure that can be used with {create/remove}_user_memory_region + fn make_user_memory_region( + &self, + slot: u32, + guest_phys_addr: u64, + memory_size: u64, + userspace_addr: u64, + readonly: bool, + log_dirty_pages: bool, + ) -> UserMemoryRegion; + /// Creates a guest physical memory slot. + fn create_user_memory_region(&self, user_memory_region: UserMemoryRegion) -> Result<()>; + /// Removes a guest physical memory slot. + fn remove_user_memory_region(&self, user_memory_region: UserMemoryRegion) -> Result<()>; + /// Returns the preferred CPU target type which can be emulated by KVM on underlying host. + #[cfg(target_arch = "aarch64")] + fn get_preferred_target(&self, kvi: &mut VcpuInit) -> Result<()>; + /// Enable split Irq capability + #[cfg(target_arch = "x86_64")] + fn enable_split_irq(&self) -> Result<()>; + #[cfg(target_arch = "x86_64")] + fn enable_sgx_attribute(&self, file: File) -> Result<()>; + /// Retrieve guest clock. + #[cfg(target_arch = "x86_64")] + fn get_clock(&self) -> Result; + /// Set guest clock. + #[cfg(target_arch = "x86_64")] + fn set_clock(&self, data: &ClockData) -> Result<()>; + /// Create a device that is used for passthrough + fn create_passthrough_device(&self) -> Result; + /// Start logging dirty pages + fn start_dirty_log(&self) -> Result<()>; + /// Stop logging dirty pages + fn stop_dirty_log(&self) -> Result<()>; + /// Get dirty pages bitmap + fn get_dirty_log(&self, slot: u32, base_gpa: u64, memory_size: u64) -> Result>; + #[cfg(feature = "tdx")] + /// Initalize TDX on this VM + fn tdx_init(&self, _cpuid: &[CpuIdEntry], _max_vcpus: u32) -> Result<()> { + unimplemented!() + } + #[cfg(feature = "tdx")] + /// Finalize the configuration of TDX on this VM + fn tdx_finalize(&self) -> Result<()> { + unimplemented!() + } + #[cfg(feature = "tdx")] + /// Initalize a TDX memory region for this VM + fn tdx_init_memory_region( + &self, + _host_address: u64, + _guest_address: u64, + _size: u64, + _measure: bool, + ) -> Result<()> { + unimplemented!() + } + /// Downcast to the underlying hypervisor VM type + fn as_any(&self) -> &dyn Any; +} + +pub trait VmOps: Send + Sync { + fn guest_mem_write(&self, gpa: u64, buf: &[u8]) -> Result; + fn guest_mem_read(&self, gpa: u64, buf: &mut [u8]) -> Result; + fn mmio_read(&self, gpa: u64, data: &mut [u8]) -> Result<()>; + fn mmio_write(&self, gpa: u64, data: &[u8]) -> Result<()>; + #[cfg(target_arch = "x86_64")] + fn pio_read(&self, port: u64, data: &mut [u8]) -> Result<()>; + #[cfg(target_arch = "x86_64")] + fn pio_write(&self, port: u64, data: &[u8]) -> Result<()>; +} diff --git a/vendor/cube-hypervisor/log_json/Cargo.toml b/vendor/cube-hypervisor/log_json/Cargo.toml new file mode 100644 index 0000000..719887e --- /dev/null +++ b/vendor/cube-hypervisor/log_json/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "log_json" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[dependencies] +libc = "0.2.137" +serde = { workspace = true, features = ["rc", "derive"] } +serde_json = "1.0.87" diff --git a/vendor/cube-hypervisor/log_json/src/lib.rs b/vendor/cube-hypervisor/log_json/src/lib.rs new file mode 100644 index 0000000..8b956c6 --- /dev/null +++ b/vendor/cube-hypervisor/log_json/src/lib.rs @@ -0,0 +1,57 @@ +// Copyright © 2023 Tencent Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use std::io::Write; +use std::sync::Mutex; + +static mut JSON_FILE_NAME: Option = None; +static mut JSON_FILE: Mutex>> = Mutex::new(None); + +/// This function must only be called once from the main process before any threads +/// are created to avoid race conditions +pub fn set_log_file(filename: String) -> Result<(), std::io::Error> { + // SAFETY: there is only one caller of this function, so JSON_FILE_NAME is written to only once + assert!(unsafe { JSON_FILE_NAME.is_none() }); + // SAFETY: JSON_FILE_NAME is None. Nobody else can hold a reference to it. + unsafe { + JSON_FILE_NAME = Some(filename); + }; + Ok(()) +} + +pub fn log_string(content: &str) { + // SAFETY: JSON_FILE_NAME is read only here. + if unsafe { JSON_FILE_NAME.is_none() } { + return; + } + + // SAFETY: JSON_FILE_NAME is valid and read only here. + let mut output = unsafe { JSON_FILE.lock().unwrap() }; + if output.is_none() { + // SAFETY: JSON_FILE_NAME is valid and read only here. + if let Some(filename) = unsafe { JSON_FILE_NAME.as_ref() } { + *output = match std::fs::File::options() + .create(true) + .append(true) + .open(std::path::Path::new(&filename)) + { + Ok(output) => Some(Box::new(output)), + Err(_) => None, + } + } + } + + if output.is_some() { + let t = format!("{}\n", content); + (*(output.as_mut().unwrap())).write_all(t.as_bytes()).ok(); + } +} + +#[macro_export] +macro_rules! log_json { + ($source:expr) => { + $crate::log_string(&serde_json::to_string($source).unwrap()) + }; +} diff --git a/vendor/cube-hypervisor/logging/Cargo.toml b/vendor/cube-hypervisor/logging/Cargo.toml new file mode 100644 index 0000000..3d06454 --- /dev/null +++ b/vendor/cube-hypervisor/logging/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "logging" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2018" + +[dependencies] +lazy_static = "1.2" +serde = { workspace = true, features = ["derive"] } +slog = "2.7.0" +slog-term = "2.9.0" +slog-async = "2.7.0" diff --git a/vendor/cube-hypervisor/logging/src/lib.rs b/vendor/cube-hypervisor/logging/src/lib.rs new file mode 100644 index 0000000..79efee4 --- /dev/null +++ b/vendor/cube-hypervisor/logging/src/lib.rs @@ -0,0 +1,279 @@ +use slog::*; +use slog_term::{Decorator, RecordDecorator}; +use std::cell::RefCell; +use std::io::Write; +use std::sync::Mutex; +use std::{io, result}; + +#[macro_use] +extern crate lazy_static; + +/// Slog tag used to request an explicit log-file reopen. +pub const LOG_CTRL_REOPEN: &str = "LogReopen"; + +fn open_log_file(name: &str) -> io::Result { + std::fs::File::options() + .create(true) + .append(true) + .open(std::path::Path::new(name)) +} + +fn open_replacement_log_file(name: &str) -> io::Result { + std::fs::File::options() + .create(true) + .append(true) + .open(std::path::Path::new(name)) +} + +lazy_static! { + pub static ref START_TM: Mutex = Mutex::new(std::time::Instant::now()); +} + +// {{{ Plain + +pub struct RawPlainDecorator { + name: String, + output: std::fs::File, +} + +pub struct PlainDecorator { + deco: RefCell, +} + +impl PlainDecorator { + /// Create `PlainDecorator` instance + pub fn new(name: String) -> Self { + let filename = name.clone(); + let output = open_log_file(&filename).unwrap(); + PlainDecorator { + deco: RefCell::new(RawPlainDecorator { name, output }), + } + } +} + +impl Decorator for PlainDecorator { + fn with_record(&self, record: &Record, _logger_values: &OwnedKVList, f: F) -> io::Result<()> + where + F: FnOnce(&mut dyn RecordDecorator) -> io::Result<()>, + { + let mut deco = self.deco.borrow_mut(); + if record.tag() == LOG_CTRL_REOPEN { + if let Err(error) = deco.reopen() { + eprintln!( + "cube-vmm: failed to reopen log file {:?}: {}", + deco.name, error + ); + } + return Ok(()); + } + let mut deco = PlainRecordDecorator { deco: &mut *deco }; + f(&mut deco) + } +} + +/// Record decorator used by `PlainDecorator` +pub struct PlainRecordDecorator<'a> { + deco: &'a mut RawPlainDecorator, +} + +impl RawPlainDecorator { + fn reopen(&mut self) -> io::Result<()> { + self.output.flush()?; + let output = open_replacement_log_file(&self.name)?; + self.output = output; + Ok(()) + } +} + +impl<'a> io::Write for PlainRecordDecorator<'a> { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.deco.output.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.deco.output.flush() + } +} + +impl<'a> RecordDecorator for PlainRecordDecorator<'a> { + fn reset(&mut self) -> io::Result<()> { + Ok(()) + } +} + +//// {{{ RawFormat + +pub struct RawFormat +where + D: Decorator, +{ + decorator: D, +} + +/// Streamer builder +pub struct RawFormatBuilder +where + D: Decorator, +{ + decorator: D, +} + +impl RawFormatBuilder +where + D: Decorator, +{ + /// Build the streamer + pub fn build(self) -> RawFormat { + RawFormat { + decorator: self.decorator, + } + } +} + +impl Drain for RawFormat +where + D: Decorator, +{ + type Ok = (); + type Err = io::Error; + + fn log(&self, record: &Record, values: &OwnedKVList) -> result::Result { + self.format_compact(record, values) + } +} + +impl RawFormat +where + D: Decorator, +{ + /// New `RawFormatBuilder` + #[allow(clippy::new_ret_no_self)] + pub fn new(d: D) -> RawFormatBuilder { + RawFormatBuilder { decorator: d } + } + + fn format_compact(&self, record: &Record, values: &OwnedKVList) -> io::Result<()> { + self.decorator.with_record(record, values, |mut decorator| { + decorator.start_msg()?; + let mut count_rd = slog_term::CountingWriter::new(&mut decorator); + write!(count_rd, "{}", record.msg())?; + + decorator.flush()?; + + Ok(()) + }) + } +} + +pub fn create_logger(name: String) -> slog::Logger { + let decorator = PlainDecorator::new(name); + let drain = RawFormat::new(decorator).build().ignore_res(); + let drain = slog_async::Async::new(drain) + .chan_size(8192) + .thread_name("cube-log".to_string()) + .overflow_strategy(slog_async::OverflowStrategy::Drop) + .build() + .ignore_res(); + let logger = slog::Logger::root(drain, o!()); + + logger +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + use std::sync::atomic::{AtomicU64, Ordering}; + use std::time::{SystemTime, UNIX_EPOCH}; + + static NEXT_TEST_ID: AtomicU64 = AtomicU64::new(0); + + fn test_log_path() -> std::path::PathBuf { + let suffix = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("system clock before unix epoch") + .as_nanos(); + let test_id = NEXT_TEST_ID.fetch_add(1, Ordering::Relaxed); + std::env::temp_dir().join(format!( + "cube-vmm-log-reopen-{}-{}-{}", + std::process::id(), + suffix, + test_id + )) + } + + struct TestLogFiles { + active: std::path::PathBuf, + rotated: std::path::PathBuf, + } + + impl TestLogFiles { + fn new() -> Self { + let active = test_log_path(); + let rotated = active.with_extension("log.1"); + Self { active, rotated } + } + } + + impl Drop for TestLogFiles { + fn drop(&mut self) { + let _ = fs::remove_file(&self.active); + let _ = fs::remove_file(&self.rotated); + } + } + + #[test] + fn control_record_reopens_after_rename_based_rotation() { + let files = TestLogFiles::new(); + let drain = RawFormat::new(PlainDecorator::new( + files.active.to_string_lossy().into_owned(), + )) + .build(); + let values = OwnedKVList::from(o!()); + + drain + .log( + &slog::record!(Level::Info, "", &format_args!("before"), b!()), + &values, + ) + .unwrap(); + + fs::rename(&files.active, &files.rotated).unwrap(); + fs::File::create(&files.active).unwrap(); + + drain + .log( + &slog::record!(Level::Info, LOG_CTRL_REOPEN, &format_args!(""), b!()), + &values, + ) + .unwrap(); + drain + .log( + &slog::record!(Level::Info, "", &format_args!("after"), b!()), + &values, + ) + .unwrap(); + + assert_eq!(fs::read_to_string(&files.rotated).unwrap(), "before"); + assert_eq!(fs::read_to_string(&files.active).unwrap(), "after"); + } + + #[test] + fn failed_reopen_keeps_current_descriptor_usable() { + let files = TestLogFiles::new(); + let output = open_log_file(files.active.to_str().expect("utf-8 log path")).unwrap(); + let mut raw = RawPlainDecorator { + name: std::env::temp_dir().to_string_lossy().into_owned(), + output, + }; + + raw.output.write_all(b"before\n").unwrap(); + assert!(raw.reopen().is_err()); + raw.output.write_all(b"after\n").unwrap(); + + assert_eq!( + fs::read_to_string(&files.active).unwrap(), + "before\nafter\n" + ); + } +} diff --git a/vendor/cube-hypervisor/net_gen/Cargo.toml b/vendor/cube-hypervisor/net_gen/Cargo.toml new file mode 100644 index 0000000..cb7baa8 --- /dev/null +++ b/vendor/cube-hypervisor/net_gen/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "net_gen" +version = "0.1.0" +authors = ["The Chromium OS Authors"] +edition = "2021" + +[dependencies] +vmm-sys-util = "0.12.1" diff --git a/vendor/cube-hypervisor/net_gen/src/if_tun.rs b/vendor/cube-hypervisor/net_gen/src/if_tun.rs new file mode 100644 index 0000000..c80a5d6 --- /dev/null +++ b/vendor/cube-hypervisor/net_gen/src/if_tun.rs @@ -0,0 +1,326 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +// bindgen /usr/include/linux/if_tun.h --no-layout-tests + +/* automatically generated by rust-bindgen 0.58.1 */ + +#[repr(C)] +#[derive(Default)] +pub struct __IncompleteArrayField(::std::marker::PhantomData, [T; 0]); +impl __IncompleteArrayField { + #[inline] + pub const fn new() -> Self { + __IncompleteArrayField(::std::marker::PhantomData, []) + } + #[inline] + pub fn as_ptr(&self) -> *const T { + self as *const _ as *const T + } + #[inline] + pub fn as_mut_ptr(&mut self) -> *mut T { + self as *mut _ as *mut T + } + #[inline] + pub unsafe fn as_slice(&self, len: usize) -> &[T] { + ::std::slice::from_raw_parts(self.as_ptr(), len) + } + #[inline] + pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) + } +} +impl ::std::fmt::Debug for __IncompleteArrayField { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + fmt.write_str("__IncompleteArrayField") + } +} +pub const __BITS_PER_LONG: u32 = 64; +pub const __FD_SETSIZE: u32 = 1024; +pub const ETH_ALEN: u32 = 6; +pub const ETH_TLEN: u32 = 2; +pub const ETH_HLEN: u32 = 14; +pub const ETH_ZLEN: u32 = 60; +pub const ETH_DATA_LEN: u32 = 1500; +pub const ETH_FRAME_LEN: u32 = 1514; +pub const ETH_FCS_LEN: u32 = 4; +pub const ETH_MIN_MTU: u32 = 68; +pub const ETH_MAX_MTU: u32 = 65535; +pub const ETH_P_LOOP: u32 = 96; +pub const ETH_P_PUP: u32 = 512; +pub const ETH_P_PUPAT: u32 = 513; +pub const ETH_P_TSN: u32 = 8944; +pub const ETH_P_ERSPAN2: u32 = 8939; +pub const ETH_P_IP: u32 = 2048; +pub const ETH_P_X25: u32 = 2053; +pub const ETH_P_ARP: u32 = 2054; +pub const ETH_P_BPQ: u32 = 2303; +pub const ETH_P_IEEEPUP: u32 = 2560; +pub const ETH_P_IEEEPUPAT: u32 = 2561; +pub const ETH_P_BATMAN: u32 = 17157; +pub const ETH_P_DEC: u32 = 24576; +pub const ETH_P_DNA_DL: u32 = 24577; +pub const ETH_P_DNA_RC: u32 = 24578; +pub const ETH_P_DNA_RT: u32 = 24579; +pub const ETH_P_LAT: u32 = 24580; +pub const ETH_P_DIAG: u32 = 24581; +pub const ETH_P_CUST: u32 = 24582; +pub const ETH_P_SCA: u32 = 24583; +pub const ETH_P_TEB: u32 = 25944; +pub const ETH_P_RARP: u32 = 32821; +pub const ETH_P_ATALK: u32 = 32923; +pub const ETH_P_AARP: u32 = 33011; +pub const ETH_P_8021Q: u32 = 33024; +pub const ETH_P_ERSPAN: u32 = 35006; +pub const ETH_P_IPX: u32 = 33079; +pub const ETH_P_IPV6: u32 = 34525; +pub const ETH_P_PAUSE: u32 = 34824; +pub const ETH_P_SLOW: u32 = 34825; +pub const ETH_P_WCCP: u32 = 34878; +pub const ETH_P_MPLS_UC: u32 = 34887; +pub const ETH_P_MPLS_MC: u32 = 34888; +pub const ETH_P_ATMMPOA: u32 = 34892; +pub const ETH_P_PPP_DISC: u32 = 34915; +pub const ETH_P_PPP_SES: u32 = 34916; +pub const ETH_P_LINK_CTL: u32 = 34924; +pub const ETH_P_ATMFATE: u32 = 34948; +pub const ETH_P_PAE: u32 = 34958; +pub const ETH_P_AOE: u32 = 34978; +pub const ETH_P_8021AD: u32 = 34984; +pub const ETH_P_802_EX1: u32 = 34997; +pub const ETH_P_PREAUTH: u32 = 35015; +pub const ETH_P_TIPC: u32 = 35018; +pub const ETH_P_LLDP: u32 = 35020; +pub const ETH_P_MRP: u32 = 35043; +pub const ETH_P_MACSEC: u32 = 35045; +pub const ETH_P_8021AH: u32 = 35047; +pub const ETH_P_MVRP: u32 = 35061; +pub const ETH_P_1588: u32 = 35063; +pub const ETH_P_NCSI: u32 = 35064; +pub const ETH_P_PRP: u32 = 35067; +pub const ETH_P_FCOE: u32 = 35078; +pub const ETH_P_IBOE: u32 = 35093; +pub const ETH_P_TDLS: u32 = 35085; +pub const ETH_P_FIP: u32 = 35092; +pub const ETH_P_80221: u32 = 35095; +pub const ETH_P_HSR: u32 = 35119; +pub const ETH_P_NSH: u32 = 35151; +pub const ETH_P_LOOPBACK: u32 = 36864; +pub const ETH_P_QINQ1: u32 = 37120; +pub const ETH_P_QINQ2: u32 = 37376; +pub const ETH_P_QINQ3: u32 = 37632; +pub const ETH_P_EDSA: u32 = 56026; +pub const ETH_P_DSA_8021Q: u32 = 56027; +pub const ETH_P_IFE: u32 = 60734; +pub const ETH_P_AF_IUCV: u32 = 64507; +pub const ETH_P_802_3_MIN: u32 = 1536; +pub const ETH_P_802_3: u32 = 1; +pub const ETH_P_AX25: u32 = 2; +pub const ETH_P_ALL: u32 = 3; +pub const ETH_P_802_2: u32 = 4; +pub const ETH_P_SNAP: u32 = 5; +pub const ETH_P_DDCMP: u32 = 6; +pub const ETH_P_WAN_PPP: u32 = 7; +pub const ETH_P_PPP_MP: u32 = 8; +pub const ETH_P_LOCALTALK: u32 = 9; +pub const ETH_P_CAN: u32 = 12; +pub const ETH_P_CANFD: u32 = 13; +pub const ETH_P_PPPTALK: u32 = 16; +pub const ETH_P_TR_802_2: u32 = 17; +pub const ETH_P_MOBITEX: u32 = 21; +pub const ETH_P_CONTROL: u32 = 22; +pub const ETH_P_IRDA: u32 = 23; +pub const ETH_P_ECONET: u32 = 24; +pub const ETH_P_HDLC: u32 = 25; +pub const ETH_P_ARCNET: u32 = 26; +pub const ETH_P_DSA: u32 = 27; +pub const ETH_P_TRAILER: u32 = 28; +pub const ETH_P_PHONET: u32 = 245; +pub const ETH_P_IEEE802154: u32 = 246; +pub const ETH_P_CAIF: u32 = 247; +pub const ETH_P_XDSA: u32 = 248; +pub const ETH_P_MAP: u32 = 249; +pub const __UAPI_DEF_ETHHDR: u32 = 1; +pub const BPF_LD: u32 = 0; +pub const BPF_LDX: u32 = 1; +pub const BPF_ST: u32 = 2; +pub const BPF_STX: u32 = 3; +pub const BPF_ALU: u32 = 4; +pub const BPF_JMP: u32 = 5; +pub const BPF_RET: u32 = 6; +pub const BPF_MISC: u32 = 7; +pub const BPF_W: u32 = 0; +pub const BPF_H: u32 = 8; +pub const BPF_B: u32 = 16; +pub const BPF_IMM: u32 = 0; +pub const BPF_ABS: u32 = 32; +pub const BPF_IND: u32 = 64; +pub const BPF_MEM: u32 = 96; +pub const BPF_LEN: u32 = 128; +pub const BPF_MSH: u32 = 160; +pub const BPF_ADD: u32 = 0; +pub const BPF_SUB: u32 = 16; +pub const BPF_MUL: u32 = 32; +pub const BPF_DIV: u32 = 48; +pub const BPF_OR: u32 = 64; +pub const BPF_AND: u32 = 80; +pub const BPF_LSH: u32 = 96; +pub const BPF_RSH: u32 = 112; +pub const BPF_NEG: u32 = 128; +pub const BPF_MOD: u32 = 144; +pub const BPF_XOR: u32 = 160; +pub const BPF_JA: u32 = 0; +pub const BPF_JEQ: u32 = 16; +pub const BPF_JGT: u32 = 32; +pub const BPF_JGE: u32 = 48; +pub const BPF_JSET: u32 = 64; +pub const BPF_K: u32 = 0; +pub const BPF_X: u32 = 8; +pub const BPF_MAXINSNS: u32 = 4096; +pub const BPF_MAJOR_VERSION: u32 = 1; +pub const BPF_MINOR_VERSION: u32 = 1; +pub const BPF_A: u32 = 16; +pub const BPF_TAX: u32 = 0; +pub const BPF_TXA: u32 = 128; +pub const BPF_MEMWORDS: u32 = 16; +pub const SKF_AD_OFF: i32 = -4096; +pub const SKF_AD_PROTOCOL: u32 = 0; +pub const SKF_AD_PKTTYPE: u32 = 4; +pub const SKF_AD_IFINDEX: u32 = 8; +pub const SKF_AD_NLATTR: u32 = 12; +pub const SKF_AD_NLATTR_NEST: u32 = 16; +pub const SKF_AD_MARK: u32 = 20; +pub const SKF_AD_QUEUE: u32 = 24; +pub const SKF_AD_HATYPE: u32 = 28; +pub const SKF_AD_RXHASH: u32 = 32; +pub const SKF_AD_CPU: u32 = 36; +pub const SKF_AD_ALU_XOR_X: u32 = 40; +pub const SKF_AD_VLAN_TAG: u32 = 44; +pub const SKF_AD_VLAN_TAG_PRESENT: u32 = 48; +pub const SKF_AD_PAY_OFFSET: u32 = 52; +pub const SKF_AD_RANDOM: u32 = 56; +pub const SKF_AD_VLAN_TPID: u32 = 60; +pub const SKF_AD_MAX: u32 = 64; +pub const SKF_NET_OFF: i32 = -1048576; +pub const SKF_LL_OFF: i32 = -2097152; +pub const BPF_NET_OFF: i32 = -1048576; +pub const BPF_LL_OFF: i32 = -2097152; +pub const TUN_READQ_SIZE: u32 = 500; +pub const TUN_TYPE_MASK: u32 = 15; +pub const IFF_TUN: u32 = 1; +pub const IFF_TAP: u32 = 2; +pub const IFF_NAPI: u32 = 16; +pub const IFF_NAPI_FRAGS: u32 = 32; +pub const IFF_NO_PI: u32 = 4096; +pub const IFF_ONE_QUEUE: u32 = 8192; +pub const IFF_VNET_HDR: u32 = 16384; +pub const IFF_TUN_EXCL: u32 = 32768; +pub const IFF_MULTI_QUEUE: u32 = 256; +pub const IFF_ATTACH_QUEUE: u32 = 512; +pub const IFF_DETACH_QUEUE: u32 = 1024; +pub const IFF_PERSIST: u32 = 2048; +pub const IFF_NOFILTER: u32 = 4096; +pub const TUN_TX_TIMESTAMP: u32 = 1; +pub const TUN_F_CSUM: u32 = 1; +pub const TUN_F_TSO4: u32 = 2; +pub const TUN_F_TSO6: u32 = 4; +pub const TUN_F_TSO_ECN: u32 = 8; +pub const TUN_F_UFO: u32 = 16; +pub const TUN_PKT_STRIP: u32 = 1; +pub const TUN_FLT_ALLMULTI: u32 = 1; +pub type __s8 = ::std::os::raw::c_schar; +pub type __u8 = ::std::os::raw::c_uchar; +pub type __s16 = ::std::os::raw::c_short; +pub type __u16 = ::std::os::raw::c_ushort; +pub type __s32 = ::std::os::raw::c_int; +pub type __u32 = ::std::os::raw::c_uint; +pub type __s64 = ::std::os::raw::c_longlong; +pub type __u64 = ::std::os::raw::c_ulonglong; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __kernel_fd_set { + pub fds_bits: [::std::os::raw::c_ulong; 16usize], +} +pub type __kernel_sighandler_t = + ::std::option::Option; +pub type __kernel_key_t = ::std::os::raw::c_int; +pub type __kernel_mqd_t = ::std::os::raw::c_int; +pub type __kernel_old_uid_t = ::std::os::raw::c_ushort; +pub type __kernel_old_gid_t = ::std::os::raw::c_ushort; +pub type __kernel_old_dev_t = ::std::os::raw::c_ulong; +pub type __kernel_long_t = ::std::os::raw::c_long; +pub type __kernel_ulong_t = ::std::os::raw::c_ulong; +pub type __kernel_ino_t = __kernel_ulong_t; +pub type __kernel_mode_t = ::std::os::raw::c_uint; +pub type __kernel_pid_t = ::std::os::raw::c_int; +pub type __kernel_ipc_pid_t = ::std::os::raw::c_int; +pub type __kernel_uid_t = ::std::os::raw::c_uint; +pub type __kernel_gid_t = ::std::os::raw::c_uint; +pub type __kernel_suseconds_t = __kernel_long_t; +pub type __kernel_daddr_t = ::std::os::raw::c_int; +pub type __kernel_uid32_t = ::std::os::raw::c_uint; +pub type __kernel_gid32_t = ::std::os::raw::c_uint; +pub type __kernel_size_t = __kernel_ulong_t; +pub type __kernel_ssize_t = __kernel_long_t; +pub type __kernel_ptrdiff_t = __kernel_long_t; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __kernel_fsid_t { + pub val: [::std::os::raw::c_int; 2usize], +} +pub type __kernel_off_t = __kernel_long_t; +pub type __kernel_loff_t = ::std::os::raw::c_longlong; +pub type __kernel_old_time_t = __kernel_long_t; +pub type __kernel_time_t = __kernel_long_t; +pub type __kernel_time64_t = ::std::os::raw::c_longlong; +pub type __kernel_clock_t = __kernel_long_t; +pub type __kernel_timer_t = ::std::os::raw::c_int; +pub type __kernel_clockid_t = ::std::os::raw::c_int; +pub type __kernel_caddr_t = *mut ::std::os::raw::c_char; +pub type __kernel_uid16_t = ::std::os::raw::c_ushort; +pub type __kernel_gid16_t = ::std::os::raw::c_ushort; +pub type __le16 = __u16; +pub type __be16 = __u16; +pub type __le32 = __u32; +pub type __be32 = __u32; +pub type __le64 = __u64; +pub type __be64 = __u64; +pub type __sum16 = __u16; +pub type __wsum = __u32; +pub type __poll_t = ::std::os::raw::c_uint; +#[repr(C, packed)] +#[derive(Debug, Copy, Clone)] +pub struct ethhdr { + pub h_dest: [::std::os::raw::c_uchar; 6usize], + pub h_source: [::std::os::raw::c_uchar; 6usize], + pub h_proto: __be16, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct sock_filter { + pub code: __u16, + pub jt: __u8, + pub jf: __u8, + pub k: __u32, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct sock_fprog { + pub len: ::std::os::raw::c_ushort, + pub filter: *mut sock_filter, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct tun_pi { + pub flags: __u16, + pub proto: __be16, +} +#[repr(C)] +#[derive(Debug)] +pub struct tun_filter { + pub flags: __u16, + pub count: __u16, + pub addr: __IncompleteArrayField<[__u8; 6usize]>, +} diff --git a/vendor/cube-hypervisor/net_gen/src/iff.rs b/vendor/cube-hypervisor/net_gen/src/iff.rs new file mode 100644 index 0000000..4a9e6a7 --- /dev/null +++ b/vendor/cube-hypervisor/net_gen/src/iff.rs @@ -0,0 +1,1226 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +// bindgen /usr/include/linux/if.h --no-layout-tests + +/* automatically generated by rust-bindgen 0.58.1 */ + +#[repr(C)] +#[derive(Default)] +pub struct __IncompleteArrayField(::std::marker::PhantomData, [T; 0]); +impl __IncompleteArrayField { + #[inline] + pub const fn new() -> Self { + __IncompleteArrayField(::std::marker::PhantomData, []) + } + #[inline] + pub fn as_ptr(&self) -> *const T { + self as *const _ as *const T + } + #[inline] + pub fn as_mut_ptr(&mut self) -> *mut T { + self as *mut _ as *mut T + } + #[inline] + pub unsafe fn as_slice(&self, len: usize) -> &[T] { + ::std::slice::from_raw_parts(self.as_ptr(), len) + } + #[inline] + pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) + } +} +impl ::std::fmt::Debug for __IncompleteArrayField { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + fmt.write_str("__IncompleteArrayField") + } +} +pub const __UAPI_DEF_IF_IFCONF: u32 = 1; +pub const __UAPI_DEF_IF_IFMAP: u32 = 1; +pub const __UAPI_DEF_IF_IFNAMSIZ: u32 = 1; +pub const __UAPI_DEF_IF_IFREQ: u32 = 1; +pub const __UAPI_DEF_IF_NET_DEVICE_FLAGS: u32 = 1; +pub const __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO: u32 = 1; +pub const __UAPI_DEF_IN_ADDR: u32 = 1; +pub const __UAPI_DEF_IN_IPPROTO: u32 = 1; +pub const __UAPI_DEF_IN_PKTINFO: u32 = 1; +pub const __UAPI_DEF_IP_MREQ: u32 = 1; +pub const __UAPI_DEF_SOCKADDR_IN: u32 = 1; +pub const __UAPI_DEF_IN_CLASS: u32 = 1; +pub const __UAPI_DEF_IN6_ADDR: u32 = 1; +pub const __UAPI_DEF_IN6_ADDR_ALT: u32 = 1; +pub const __UAPI_DEF_SOCKADDR_IN6: u32 = 1; +pub const __UAPI_DEF_IPV6_MREQ: u32 = 1; +pub const __UAPI_DEF_IPPROTO_V6: u32 = 1; +pub const __UAPI_DEF_IPV6_OPTIONS: u32 = 1; +pub const __UAPI_DEF_IN6_PKTINFO: u32 = 1; +pub const __UAPI_DEF_IP6_MTUINFO: u32 = 1; +pub const __UAPI_DEF_SOCKADDR_IPX: u32 = 1; +pub const __UAPI_DEF_IPX_ROUTE_DEFINITION: u32 = 1; +pub const __UAPI_DEF_IPX_INTERFACE_DEFINITION: u32 = 1; +pub const __UAPI_DEF_IPX_CONFIG_DATA: u32 = 1; +pub const __UAPI_DEF_IPX_ROUTE_DEF: u32 = 1; +pub const __UAPI_DEF_XATTR: u32 = 1; +pub const __BITS_PER_LONG: u32 = 64; +pub const __FD_SETSIZE: u32 = 1024; +pub const _K_SS_MAXSIZE: u32 = 128; +pub const _SYS_SOCKET_H: u32 = 1; +pub const _FEATURES_H: u32 = 1; +pub const _DEFAULT_SOURCE: u32 = 1; +pub const __GLIBC_USE_ISOC2X: u32 = 0; +pub const __USE_ISOC11: u32 = 1; +pub const __USE_ISOC99: u32 = 1; +pub const __USE_ISOC95: u32 = 1; +pub const __USE_POSIX_IMPLICITLY: u32 = 1; +pub const _POSIX_SOURCE: u32 = 1; +pub const _POSIX_C_SOURCE: u32 = 200809; +pub const __USE_POSIX: u32 = 1; +pub const __USE_POSIX2: u32 = 1; +pub const __USE_POSIX199309: u32 = 1; +pub const __USE_POSIX199506: u32 = 1; +pub const __USE_XOPEN2K: u32 = 1; +pub const __USE_XOPEN2K8: u32 = 1; +pub const _ATFILE_SOURCE: u32 = 1; +pub const __USE_MISC: u32 = 1; +pub const __USE_ATFILE: u32 = 1; +pub const __USE_FORTIFY_LEVEL: u32 = 0; +pub const __GLIBC_USE_DEPRECATED_GETS: u32 = 0; +pub const __GLIBC_USE_DEPRECATED_SCANF: u32 = 0; +pub const _STDC_PREDEF_H: u32 = 1; +pub const __STDC_IEC_559__: u32 = 1; +pub const __STDC_IEC_559_COMPLEX__: u32 = 1; +pub const __STDC_ISO_10646__: u32 = 201706; +pub const __GNU_LIBRARY__: u32 = 6; +pub const __GLIBC__: u32 = 2; +pub const __GLIBC_MINOR__: u32 = 32; +pub const _SYS_CDEFS_H: u32 = 1; +pub const __glibc_c99_flexarr_available: u32 = 1; +pub const __WORDSIZE: u32 = 64; +pub const __WORDSIZE_TIME64_COMPAT32: u32 = 1; +pub const __SYSCALL_WORDSIZE: u32 = 64; +pub const __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI: u32 = 0; +pub const __HAVE_GENERIC_SELECTION: u32 = 1; +pub const __iovec_defined: u32 = 1; +pub const _SYS_TYPES_H: u32 = 1; +pub const _BITS_TYPES_H: u32 = 1; +pub const __TIMESIZE: u32 = 64; +pub const _BITS_TYPESIZES_H: u32 = 1; +pub const __OFF_T_MATCHES_OFF64_T: u32 = 1; +pub const __INO_T_MATCHES_INO64_T: u32 = 1; +pub const __RLIM_T_MATCHES_RLIM64_T: u32 = 1; +pub const __STATFS_MATCHES_STATFS64: u32 = 1; +pub const __KERNEL_OLD_TIMEVAL_MATCHES_TIMEVAL64: u32 = 1; +pub const _BITS_TIME64_H: u32 = 1; +pub const __clock_t_defined: u32 = 1; +pub const __clockid_t_defined: u32 = 1; +pub const __time_t_defined: u32 = 1; +pub const __timer_t_defined: u32 = 1; +pub const _BITS_STDINT_INTN_H: u32 = 1; +pub const __BIT_TYPES_DEFINED__: u32 = 1; +pub const _ENDIAN_H: u32 = 1; +pub const _BITS_ENDIAN_H: u32 = 1; +pub const __LITTLE_ENDIAN: u32 = 1234; +pub const __BIG_ENDIAN: u32 = 4321; +pub const __PDP_ENDIAN: u32 = 3412; +pub const _BITS_ENDIANNESS_H: u32 = 1; +pub const __BYTE_ORDER: u32 = 1234; +pub const __FLOAT_WORD_ORDER: u32 = 1234; +pub const LITTLE_ENDIAN: u32 = 1234; +pub const BIG_ENDIAN: u32 = 4321; +pub const PDP_ENDIAN: u32 = 3412; +pub const BYTE_ORDER: u32 = 1234; +pub const _BITS_BYTESWAP_H: u32 = 1; +pub const _BITS_UINTN_IDENTITY_H: u32 = 1; +pub const _SYS_SELECT_H: u32 = 1; +pub const __sigset_t_defined: u32 = 1; +pub const __timeval_defined: u32 = 1; +pub const _STRUCT_TIMESPEC: u32 = 1; +pub const FD_SETSIZE: u32 = 1024; +pub const _BITS_PTHREADTYPES_COMMON_H: u32 = 1; +pub const _THREAD_SHARED_TYPES_H: u32 = 1; +pub const _BITS_PTHREADTYPES_ARCH_H: u32 = 1; +pub const __SIZEOF_PTHREAD_MUTEX_T: u32 = 40; +pub const __SIZEOF_PTHREAD_ATTR_T: u32 = 56; +pub const __SIZEOF_PTHREAD_RWLOCK_T: u32 = 56; +pub const __SIZEOF_PTHREAD_BARRIER_T: u32 = 32; +pub const __SIZEOF_PTHREAD_MUTEXATTR_T: u32 = 4; +pub const __SIZEOF_PTHREAD_COND_T: u32 = 48; +pub const __SIZEOF_PTHREAD_CONDATTR_T: u32 = 4; +pub const __SIZEOF_PTHREAD_RWLOCKATTR_T: u32 = 8; +pub const __SIZEOF_PTHREAD_BARRIERATTR_T: u32 = 4; +pub const _THREAD_MUTEX_INTERNAL_H: u32 = 1; +pub const __PTHREAD_MUTEX_HAVE_PREV: u32 = 1; +pub const __have_pthread_attr_t: u32 = 1; +pub const PF_UNSPEC: u32 = 0; +pub const PF_LOCAL: u32 = 1; +pub const PF_UNIX: u32 = 1; +pub const PF_FILE: u32 = 1; +pub const PF_INET: u32 = 2; +pub const PF_AX25: u32 = 3; +pub const PF_IPX: u32 = 4; +pub const PF_APPLETALK: u32 = 5; +pub const PF_NETROM: u32 = 6; +pub const PF_BRIDGE: u32 = 7; +pub const PF_ATMPVC: u32 = 8; +pub const PF_X25: u32 = 9; +pub const PF_INET6: u32 = 10; +pub const PF_ROSE: u32 = 11; +pub const PF_DECnet: u32 = 12; +pub const PF_NETBEUI: u32 = 13; +pub const PF_SECURITY: u32 = 14; +pub const PF_KEY: u32 = 15; +pub const PF_NETLINK: u32 = 16; +pub const PF_ROUTE: u32 = 16; +pub const PF_PACKET: u32 = 17; +pub const PF_ASH: u32 = 18; +pub const PF_ECONET: u32 = 19; +pub const PF_ATMSVC: u32 = 20; +pub const PF_RDS: u32 = 21; +pub const PF_SNA: u32 = 22; +pub const PF_IRDA: u32 = 23; +pub const PF_PPPOX: u32 = 24; +pub const PF_WANPIPE: u32 = 25; +pub const PF_LLC: u32 = 26; +pub const PF_IB: u32 = 27; +pub const PF_MPLS: u32 = 28; +pub const PF_CAN: u32 = 29; +pub const PF_TIPC: u32 = 30; +pub const PF_BLUETOOTH: u32 = 31; +pub const PF_IUCV: u32 = 32; +pub const PF_RXRPC: u32 = 33; +pub const PF_ISDN: u32 = 34; +pub const PF_PHONET: u32 = 35; +pub const PF_IEEE802154: u32 = 36; +pub const PF_CAIF: u32 = 37; +pub const PF_ALG: u32 = 38; +pub const PF_NFC: u32 = 39; +pub const PF_VSOCK: u32 = 40; +pub const PF_KCM: u32 = 41; +pub const PF_QIPCRTR: u32 = 42; +pub const PF_SMC: u32 = 43; +pub const PF_XDP: u32 = 44; +pub const PF_MAX: u32 = 45; +pub const AF_UNSPEC: u32 = 0; +pub const AF_LOCAL: u32 = 1; +pub const AF_UNIX: u32 = 1; +pub const AF_FILE: u32 = 1; +pub const AF_INET: u32 = 2; +pub const AF_AX25: u32 = 3; +pub const AF_IPX: u32 = 4; +pub const AF_APPLETALK: u32 = 5; +pub const AF_NETROM: u32 = 6; +pub const AF_BRIDGE: u32 = 7; +pub const AF_ATMPVC: u32 = 8; +pub const AF_X25: u32 = 9; +pub const AF_INET6: u32 = 10; +pub const AF_ROSE: u32 = 11; +pub const AF_DECnet: u32 = 12; +pub const AF_NETBEUI: u32 = 13; +pub const AF_SECURITY: u32 = 14; +pub const AF_KEY: u32 = 15; +pub const AF_NETLINK: u32 = 16; +pub const AF_ROUTE: u32 = 16; +pub const AF_PACKET: u32 = 17; +pub const AF_ASH: u32 = 18; +pub const AF_ECONET: u32 = 19; +pub const AF_ATMSVC: u32 = 20; +pub const AF_RDS: u32 = 21; +pub const AF_SNA: u32 = 22; +pub const AF_IRDA: u32 = 23; +pub const AF_PPPOX: u32 = 24; +pub const AF_WANPIPE: u32 = 25; +pub const AF_LLC: u32 = 26; +pub const AF_IB: u32 = 27; +pub const AF_MPLS: u32 = 28; +pub const AF_CAN: u32 = 29; +pub const AF_TIPC: u32 = 30; +pub const AF_BLUETOOTH: u32 = 31; +pub const AF_IUCV: u32 = 32; +pub const AF_RXRPC: u32 = 33; +pub const AF_ISDN: u32 = 34; +pub const AF_PHONET: u32 = 35; +pub const AF_IEEE802154: u32 = 36; +pub const AF_CAIF: u32 = 37; +pub const AF_ALG: u32 = 38; +pub const AF_NFC: u32 = 39; +pub const AF_VSOCK: u32 = 40; +pub const AF_KCM: u32 = 41; +pub const AF_QIPCRTR: u32 = 42; +pub const AF_SMC: u32 = 43; +pub const AF_XDP: u32 = 44; +pub const AF_MAX: u32 = 45; +pub const SOL_RAW: u32 = 255; +pub const SOL_DECNET: u32 = 261; +pub const SOL_X25: u32 = 262; +pub const SOL_PACKET: u32 = 263; +pub const SOL_ATM: u32 = 264; +pub const SOL_AAL: u32 = 265; +pub const SOL_IRDA: u32 = 266; +pub const SOL_NETBEUI: u32 = 267; +pub const SOL_LLC: u32 = 268; +pub const SOL_DCCP: u32 = 269; +pub const SOL_NETLINK: u32 = 270; +pub const SOL_TIPC: u32 = 271; +pub const SOL_RXRPC: u32 = 272; +pub const SOL_PPPOL2TP: u32 = 273; +pub const SOL_BLUETOOTH: u32 = 274; +pub const SOL_PNPIPE: u32 = 275; +pub const SOL_RDS: u32 = 276; +pub const SOL_IUCV: u32 = 277; +pub const SOL_CAIF: u32 = 278; +pub const SOL_ALG: u32 = 279; +pub const SOL_NFC: u32 = 280; +pub const SOL_KCM: u32 = 281; +pub const SOL_TLS: u32 = 282; +pub const SOL_XDP: u32 = 283; +pub const SOMAXCONN: u32 = 4096; +pub const _BITS_SOCKADDR_H: u32 = 1; +pub const _SS_SIZE: u32 = 128; +pub const FIOSETOWN: u32 = 35073; +pub const SIOCSPGRP: u32 = 35074; +pub const FIOGETOWN: u32 = 35075; +pub const SIOCGPGRP: u32 = 35076; +pub const SIOCATMARK: u32 = 35077; +pub const SIOCGSTAMP_OLD: u32 = 35078; +pub const SIOCGSTAMPNS_OLD: u32 = 35079; +pub const SOL_SOCKET: u32 = 1; +pub const SO_DEBUG: u32 = 1; +pub const SO_REUSEADDR: u32 = 2; +pub const SO_TYPE: u32 = 3; +pub const SO_ERROR: u32 = 4; +pub const SO_DONTROUTE: u32 = 5; +pub const SO_BROADCAST: u32 = 6; +pub const SO_SNDBUF: u32 = 7; +pub const SO_RCVBUF: u32 = 8; +pub const SO_SNDBUFFORCE: u32 = 32; +pub const SO_RCVBUFFORCE: u32 = 33; +pub const SO_KEEPALIVE: u32 = 9; +pub const SO_OOBINLINE: u32 = 10; +pub const SO_NO_CHECK: u32 = 11; +pub const SO_PRIORITY: u32 = 12; +pub const SO_LINGER: u32 = 13; +pub const SO_BSDCOMPAT: u32 = 14; +pub const SO_REUSEPORT: u32 = 15; +pub const SO_PASSCRED: u32 = 16; +pub const SO_PEERCRED: u32 = 17; +pub const SO_RCVLOWAT: u32 = 18; +pub const SO_SNDLOWAT: u32 = 19; +pub const SO_RCVTIMEO_OLD: u32 = 20; +pub const SO_SNDTIMEO_OLD: u32 = 21; +pub const SO_SECURITY_AUTHENTICATION: u32 = 22; +pub const SO_SECURITY_ENCRYPTION_TRANSPORT: u32 = 23; +pub const SO_SECURITY_ENCRYPTION_NETWORK: u32 = 24; +pub const SO_BINDTODEVICE: u32 = 25; +pub const SO_ATTACH_FILTER: u32 = 26; +pub const SO_DETACH_FILTER: u32 = 27; +pub const SO_GET_FILTER: u32 = 26; +pub const SO_PEERNAME: u32 = 28; +pub const SO_ACCEPTCONN: u32 = 30; +pub const SO_PEERSEC: u32 = 31; +pub const SO_PASSSEC: u32 = 34; +pub const SO_MARK: u32 = 36; +pub const SO_PROTOCOL: u32 = 38; +pub const SO_DOMAIN: u32 = 39; +pub const SO_RXQ_OVFL: u32 = 40; +pub const SO_WIFI_STATUS: u32 = 41; +pub const SCM_WIFI_STATUS: u32 = 41; +pub const SO_PEEK_OFF: u32 = 42; +pub const SO_NOFCS: u32 = 43; +pub const SO_LOCK_FILTER: u32 = 44; +pub const SO_SELECT_ERR_QUEUE: u32 = 45; +pub const SO_BUSY_POLL: u32 = 46; +pub const SO_MAX_PACING_RATE: u32 = 47; +pub const SO_BPF_EXTENSIONS: u32 = 48; +pub const SO_INCOMING_CPU: u32 = 49; +pub const SO_ATTACH_BPF: u32 = 50; +pub const SO_DETACH_BPF: u32 = 27; +pub const SO_ATTACH_REUSEPORT_CBPF: u32 = 51; +pub const SO_ATTACH_REUSEPORT_EBPF: u32 = 52; +pub const SO_CNX_ADVICE: u32 = 53; +pub const SCM_TIMESTAMPING_OPT_STATS: u32 = 54; +pub const SO_MEMINFO: u32 = 55; +pub const SO_INCOMING_NAPI_ID: u32 = 56; +pub const SO_COOKIE: u32 = 57; +pub const SCM_TIMESTAMPING_PKTINFO: u32 = 58; +pub const SO_PEERGROUPS: u32 = 59; +pub const SO_ZEROCOPY: u32 = 60; +pub const SO_TXTIME: u32 = 61; +pub const SCM_TXTIME: u32 = 61; +pub const SO_BINDTOIFINDEX: u32 = 62; +pub const SO_TIMESTAMP_OLD: u32 = 29; +pub const SO_TIMESTAMPNS_OLD: u32 = 35; +pub const SO_TIMESTAMPING_OLD: u32 = 37; +pub const SO_TIMESTAMP_NEW: u32 = 63; +pub const SO_TIMESTAMPNS_NEW: u32 = 64; +pub const SO_TIMESTAMPING_NEW: u32 = 65; +pub const SO_RCVTIMEO_NEW: u32 = 66; +pub const SO_SNDTIMEO_NEW: u32 = 67; +pub const SO_DETACH_REUSEPORT_BPF: u32 = 68; +pub const SO_TIMESTAMP: u32 = 29; +pub const SO_TIMESTAMPNS: u32 = 35; +pub const SO_TIMESTAMPING: u32 = 37; +pub const SO_RCVTIMEO: u32 = 20; +pub const SO_SNDTIMEO: u32 = 21; +pub const SCM_TIMESTAMP: u32 = 29; +pub const SCM_TIMESTAMPNS: u32 = 35; +pub const SCM_TIMESTAMPING: u32 = 37; +pub const __osockaddr_defined: u32 = 1; +pub const IFNAMSIZ: u32 = 16; +pub const IFALIASZ: u32 = 256; +pub const ALTIFNAMSIZ: u32 = 128; +pub const GENERIC_HDLC_VERSION: u32 = 4; +pub const CLOCK_DEFAULT: u32 = 0; +pub const CLOCK_EXT: u32 = 1; +pub const CLOCK_INT: u32 = 2; +pub const CLOCK_TXINT: u32 = 3; +pub const CLOCK_TXFROMRX: u32 = 4; +pub const ENCODING_DEFAULT: u32 = 0; +pub const ENCODING_NRZ: u32 = 1; +pub const ENCODING_NRZI: u32 = 2; +pub const ENCODING_FM_MARK: u32 = 3; +pub const ENCODING_FM_SPACE: u32 = 4; +pub const ENCODING_MANCHESTER: u32 = 5; +pub const PARITY_DEFAULT: u32 = 0; +pub const PARITY_NONE: u32 = 1; +pub const PARITY_CRC16_PR0: u32 = 2; +pub const PARITY_CRC16_PR1: u32 = 3; +pub const PARITY_CRC16_PR0_CCITT: u32 = 4; +pub const PARITY_CRC16_PR1_CCITT: u32 = 5; +pub const PARITY_CRC32_PR0_CCITT: u32 = 6; +pub const PARITY_CRC32_PR1_CCITT: u32 = 7; +pub const LMI_DEFAULT: u32 = 0; +pub const LMI_NONE: u32 = 1; +pub const LMI_ANSI: u32 = 2; +pub const LMI_CCITT: u32 = 3; +pub const LMI_CISCO: u32 = 4; +pub const IF_GET_IFACE: u32 = 1; +pub const IF_GET_PROTO: u32 = 2; +pub const IF_IFACE_V35: u32 = 4096; +pub const IF_IFACE_V24: u32 = 4097; +pub const IF_IFACE_X21: u32 = 4098; +pub const IF_IFACE_T1: u32 = 4099; +pub const IF_IFACE_E1: u32 = 4100; +pub const IF_IFACE_SYNC_SERIAL: u32 = 4101; +pub const IF_IFACE_X21D: u32 = 4102; +pub const IF_PROTO_HDLC: u32 = 8192; +pub const IF_PROTO_PPP: u32 = 8193; +pub const IF_PROTO_CISCO: u32 = 8194; +pub const IF_PROTO_FR: u32 = 8195; +pub const IF_PROTO_FR_ADD_PVC: u32 = 8196; +pub const IF_PROTO_FR_DEL_PVC: u32 = 8197; +pub const IF_PROTO_X25: u32 = 8198; +pub const IF_PROTO_HDLC_ETH: u32 = 8199; +pub const IF_PROTO_FR_ADD_ETH_PVC: u32 = 8200; +pub const IF_PROTO_FR_DEL_ETH_PVC: u32 = 8201; +pub const IF_PROTO_FR_PVC: u32 = 8202; +pub const IF_PROTO_FR_ETH_PVC: u32 = 8203; +pub const IF_PROTO_RAW: u32 = 8204; +pub const IFHWADDRLEN: u32 = 6; +pub type __s8 = ::std::os::raw::c_schar; +pub type __u8 = ::std::os::raw::c_uchar; +pub type __s16 = ::std::os::raw::c_short; +pub type __u16 = ::std::os::raw::c_ushort; +pub type __s32 = ::std::os::raw::c_int; +pub type __u32 = ::std::os::raw::c_uint; +pub type __s64 = ::std::os::raw::c_longlong; +pub type __u64 = ::std::os::raw::c_ulonglong; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __kernel_fd_set { + pub fds_bits: [::std::os::raw::c_ulong; 16usize], +} +pub type __kernel_sighandler_t = + ::std::option::Option; +pub type __kernel_key_t = ::std::os::raw::c_int; +pub type __kernel_mqd_t = ::std::os::raw::c_int; +pub type __kernel_old_uid_t = ::std::os::raw::c_ushort; +pub type __kernel_old_gid_t = ::std::os::raw::c_ushort; +pub type __kernel_old_dev_t = ::std::os::raw::c_ulong; +pub type __kernel_long_t = ::std::os::raw::c_long; +pub type __kernel_ulong_t = ::std::os::raw::c_ulong; +pub type __kernel_ino_t = __kernel_ulong_t; +pub type __kernel_mode_t = ::std::os::raw::c_uint; +pub type __kernel_pid_t = ::std::os::raw::c_int; +pub type __kernel_ipc_pid_t = ::std::os::raw::c_int; +pub type __kernel_uid_t = ::std::os::raw::c_uint; +pub type __kernel_gid_t = ::std::os::raw::c_uint; +pub type __kernel_suseconds_t = __kernel_long_t; +pub type __kernel_daddr_t = ::std::os::raw::c_int; +pub type __kernel_uid32_t = ::std::os::raw::c_uint; +pub type __kernel_gid32_t = ::std::os::raw::c_uint; +pub type __kernel_size_t = __kernel_ulong_t; +pub type __kernel_ssize_t = __kernel_long_t; +pub type __kernel_ptrdiff_t = __kernel_long_t; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __kernel_fsid_t { + pub val: [::std::os::raw::c_int; 2usize], +} +pub type __kernel_off_t = __kernel_long_t; +pub type __kernel_loff_t = ::std::os::raw::c_longlong; +pub type __kernel_old_time_t = __kernel_long_t; +pub type __kernel_time_t = __kernel_long_t; +pub type __kernel_time64_t = ::std::os::raw::c_longlong; +pub type __kernel_clock_t = __kernel_long_t; +pub type __kernel_timer_t = ::std::os::raw::c_int; +pub type __kernel_clockid_t = ::std::os::raw::c_int; +pub type __kernel_caddr_t = *mut ::std::os::raw::c_char; +pub type __kernel_uid16_t = ::std::os::raw::c_ushort; +pub type __kernel_gid16_t = ::std::os::raw::c_ushort; +pub type __le16 = __u16; +pub type __be16 = __u16; +pub type __le32 = __u32; +pub type __be32 = __u32; +pub type __le64 = __u64; +pub type __be64 = __u64; +pub type __sum16 = __u16; +pub type __wsum = __u32; +pub type __poll_t = ::std::os::raw::c_uint; +pub type __kernel_sa_family_t = ::std::os::raw::c_ushort; +#[repr(C)] +#[derive(Copy, Clone)] +pub struct __kernel_sockaddr_storage { + pub __bindgen_anon_1: __kernel_sockaddr_storage__bindgen_ty_1, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union __kernel_sockaddr_storage__bindgen_ty_1 { + pub __bindgen_anon_1: __kernel_sockaddr_storage__bindgen_ty_1__bindgen_ty_1, + pub __align: *mut ::std::os::raw::c_void, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct __kernel_sockaddr_storage__bindgen_ty_1__bindgen_ty_1 { + pub ss_family: __kernel_sa_family_t, + pub __data: [::std::os::raw::c_char; 126usize], +} +pub type size_t = ::std::os::raw::c_ulong; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct iovec { + pub iov_base: *mut ::std::os::raw::c_void, + pub iov_len: size_t, +} +pub type __u_char = ::std::os::raw::c_uchar; +pub type __u_short = ::std::os::raw::c_ushort; +pub type __u_int = ::std::os::raw::c_uint; +pub type __u_long = ::std::os::raw::c_ulong; +pub type __int8_t = ::std::os::raw::c_schar; +pub type __uint8_t = ::std::os::raw::c_uchar; +pub type __int16_t = ::std::os::raw::c_short; +pub type __uint16_t = ::std::os::raw::c_ushort; +pub type __int32_t = ::std::os::raw::c_int; +pub type __uint32_t = ::std::os::raw::c_uint; +pub type __int64_t = ::std::os::raw::c_long; +pub type __uint64_t = ::std::os::raw::c_ulong; +pub type __int_least8_t = __int8_t; +pub type __uint_least8_t = __uint8_t; +pub type __int_least16_t = __int16_t; +pub type __uint_least16_t = __uint16_t; +pub type __int_least32_t = __int32_t; +pub type __uint_least32_t = __uint32_t; +pub type __int_least64_t = __int64_t; +pub type __uint_least64_t = __uint64_t; +pub type __quad_t = ::std::os::raw::c_long; +pub type __u_quad_t = ::std::os::raw::c_ulong; +pub type __intmax_t = ::std::os::raw::c_long; +pub type __uintmax_t = ::std::os::raw::c_ulong; +pub type __dev_t = ::std::os::raw::c_ulong; +pub type __uid_t = ::std::os::raw::c_uint; +pub type __gid_t = ::std::os::raw::c_uint; +pub type __ino_t = ::std::os::raw::c_ulong; +pub type __ino64_t = ::std::os::raw::c_ulong; +pub type __mode_t = ::std::os::raw::c_uint; +pub type __nlink_t = ::std::os::raw::c_ulong; +pub type __off_t = ::std::os::raw::c_long; +pub type __off64_t = ::std::os::raw::c_long; +pub type __pid_t = ::std::os::raw::c_int; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __fsid_t { + pub __val: [::std::os::raw::c_int; 2usize], +} +pub type __clock_t = ::std::os::raw::c_long; +pub type __rlim_t = ::std::os::raw::c_ulong; +pub type __rlim64_t = ::std::os::raw::c_ulong; +pub type __id_t = ::std::os::raw::c_uint; +pub type __time_t = ::std::os::raw::c_long; +pub type __useconds_t = ::std::os::raw::c_uint; +pub type __suseconds_t = ::std::os::raw::c_long; +pub type __suseconds64_t = ::std::os::raw::c_long; +pub type __daddr_t = ::std::os::raw::c_int; +pub type __key_t = ::std::os::raw::c_int; +pub type __clockid_t = ::std::os::raw::c_int; +pub type __timer_t = *mut ::std::os::raw::c_void; +pub type __blksize_t = ::std::os::raw::c_long; +pub type __blkcnt_t = ::std::os::raw::c_long; +pub type __blkcnt64_t = ::std::os::raw::c_long; +pub type __fsblkcnt_t = ::std::os::raw::c_ulong; +pub type __fsblkcnt64_t = ::std::os::raw::c_ulong; +pub type __fsfilcnt_t = ::std::os::raw::c_ulong; +pub type __fsfilcnt64_t = ::std::os::raw::c_ulong; +pub type __fsword_t = ::std::os::raw::c_long; +pub type __ssize_t = ::std::os::raw::c_long; +pub type __syscall_slong_t = ::std::os::raw::c_long; +pub type __syscall_ulong_t = ::std::os::raw::c_ulong; +pub type __loff_t = __off64_t; +pub type __caddr_t = *mut ::std::os::raw::c_char; +pub type __intptr_t = ::std::os::raw::c_long; +pub type __socklen_t = ::std::os::raw::c_uint; +pub type __sig_atomic_t = ::std::os::raw::c_int; +pub type u_char = __u_char; +pub type u_short = __u_short; +pub type u_int = __u_int; +pub type u_long = __u_long; +pub type quad_t = __quad_t; +pub type u_quad_t = __u_quad_t; +pub type fsid_t = __fsid_t; +pub type loff_t = __loff_t; +pub type ino_t = __ino_t; +pub type dev_t = __dev_t; +pub type gid_t = __gid_t; +pub type mode_t = __mode_t; +pub type nlink_t = __nlink_t; +pub type uid_t = __uid_t; +pub type off_t = __off_t; +pub type pid_t = __pid_t; +pub type id_t = __id_t; +pub type ssize_t = __ssize_t; +pub type daddr_t = __daddr_t; +pub type caddr_t = __caddr_t; +pub type key_t = __key_t; +pub type clock_t = __clock_t; +pub type clockid_t = __clockid_t; +pub type time_t = __time_t; +pub type timer_t = __timer_t; +pub type ulong = ::std::os::raw::c_ulong; +pub type ushort = ::std::os::raw::c_ushort; +pub type uint = ::std::os::raw::c_uint; +pub type u_int8_t = __uint8_t; +pub type u_int16_t = __uint16_t; +pub type u_int32_t = __uint32_t; +pub type u_int64_t = __uint64_t; +pub type register_t = ::std::os::raw::c_long; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __sigset_t { + pub __val: [::std::os::raw::c_ulong; 16usize], +} +pub type sigset_t = __sigset_t; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct timeval { + pub tv_sec: __time_t, + pub tv_usec: __suseconds_t, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct timespec { + pub tv_sec: __time_t, + pub tv_nsec: __syscall_slong_t, +} +pub type suseconds_t = __suseconds_t; +pub type __fd_mask = ::std::os::raw::c_long; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct fd_set { + pub __fds_bits: [__fd_mask; 16usize], +} +pub type fd_mask = __fd_mask; +extern "C" { + pub fn select( + __nfds: ::std::os::raw::c_int, + __readfds: *mut fd_set, + __writefds: *mut fd_set, + __exceptfds: *mut fd_set, + __timeout: *mut timeval, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn pselect( + __nfds: ::std::os::raw::c_int, + __readfds: *mut fd_set, + __writefds: *mut fd_set, + __exceptfds: *mut fd_set, + __timeout: *const timespec, + __sigmask: *const __sigset_t, + ) -> ::std::os::raw::c_int; +} +pub type blksize_t = __blksize_t; +pub type blkcnt_t = __blkcnt_t; +pub type fsblkcnt_t = __fsblkcnt_t; +pub type fsfilcnt_t = __fsfilcnt_t; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __pthread_internal_list { + pub __prev: *mut __pthread_internal_list, + pub __next: *mut __pthread_internal_list, +} +pub type __pthread_list_t = __pthread_internal_list; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __pthread_internal_slist { + pub __next: *mut __pthread_internal_slist, +} +pub type __pthread_slist_t = __pthread_internal_slist; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __pthread_mutex_s { + pub __lock: ::std::os::raw::c_int, + pub __count: ::std::os::raw::c_uint, + pub __owner: ::std::os::raw::c_int, + pub __nusers: ::std::os::raw::c_uint, + pub __kind: ::std::os::raw::c_int, + pub __spins: ::std::os::raw::c_short, + pub __elision: ::std::os::raw::c_short, + pub __list: __pthread_list_t, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __pthread_rwlock_arch_t { + pub __readers: ::std::os::raw::c_uint, + pub __writers: ::std::os::raw::c_uint, + pub __wrphase_futex: ::std::os::raw::c_uint, + pub __writers_futex: ::std::os::raw::c_uint, + pub __pad3: ::std::os::raw::c_uint, + pub __pad4: ::std::os::raw::c_uint, + pub __cur_writer: ::std::os::raw::c_int, + pub __shared: ::std::os::raw::c_int, + pub __rwelision: ::std::os::raw::c_schar, + pub __pad1: [::std::os::raw::c_uchar; 7usize], + pub __pad2: ::std::os::raw::c_ulong, + pub __flags: ::std::os::raw::c_uint, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct __pthread_cond_s { + pub __bindgen_anon_1: __pthread_cond_s__bindgen_ty_1, + pub __bindgen_anon_2: __pthread_cond_s__bindgen_ty_2, + pub __g_refs: [::std::os::raw::c_uint; 2usize], + pub __g_size: [::std::os::raw::c_uint; 2usize], + pub __g1_orig_size: ::std::os::raw::c_uint, + pub __wrefs: ::std::os::raw::c_uint, + pub __g_signals: [::std::os::raw::c_uint; 2usize], +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union __pthread_cond_s__bindgen_ty_1 { + pub __wseq: ::std::os::raw::c_ulonglong, + pub __wseq32: __pthread_cond_s__bindgen_ty_1__bindgen_ty_1, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __pthread_cond_s__bindgen_ty_1__bindgen_ty_1 { + pub __low: ::std::os::raw::c_uint, + pub __high: ::std::os::raw::c_uint, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union __pthread_cond_s__bindgen_ty_2 { + pub __g1_start: ::std::os::raw::c_ulonglong, + pub __g1_start32: __pthread_cond_s__bindgen_ty_2__bindgen_ty_1, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __pthread_cond_s__bindgen_ty_2__bindgen_ty_1 { + pub __low: ::std::os::raw::c_uint, + pub __high: ::std::os::raw::c_uint, +} +pub type __tss_t = ::std::os::raw::c_uint; +pub type __thrd_t = ::std::os::raw::c_ulong; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __once_flag { + pub __data: ::std::os::raw::c_int, +} +pub type pthread_t = ::std::os::raw::c_ulong; +#[repr(C)] +#[derive(Copy, Clone)] +pub union pthread_mutexattr_t { + pub __size: [::std::os::raw::c_char; 4usize], + pub __align: ::std::os::raw::c_int, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union pthread_condattr_t { + pub __size: [::std::os::raw::c_char; 4usize], + pub __align: ::std::os::raw::c_int, +} +pub type pthread_key_t = ::std::os::raw::c_uint; +pub type pthread_once_t = ::std::os::raw::c_int; +#[repr(C)] +#[derive(Copy, Clone)] +pub union pthread_attr_t { + pub __size: [::std::os::raw::c_char; 56usize], + pub __align: ::std::os::raw::c_long, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union pthread_mutex_t { + pub __data: __pthread_mutex_s, + pub __size: [::std::os::raw::c_char; 40usize], + pub __align: ::std::os::raw::c_long, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union pthread_cond_t { + pub __data: __pthread_cond_s, + pub __size: [::std::os::raw::c_char; 48usize], + pub __align: ::std::os::raw::c_longlong, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union pthread_rwlock_t { + pub __data: __pthread_rwlock_arch_t, + pub __size: [::std::os::raw::c_char; 56usize], + pub __align: ::std::os::raw::c_long, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union pthread_rwlockattr_t { + pub __size: [::std::os::raw::c_char; 8usize], + pub __align: ::std::os::raw::c_long, +} +pub type pthread_spinlock_t = ::std::os::raw::c_int; +#[repr(C)] +#[derive(Copy, Clone)] +pub union pthread_barrier_t { + pub __size: [::std::os::raw::c_char; 32usize], + pub __align: ::std::os::raw::c_long, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union pthread_barrierattr_t { + pub __size: [::std::os::raw::c_char; 4usize], + pub __align: ::std::os::raw::c_int, +} +pub type socklen_t = __socklen_t; +pub const __socket_type_SOCK_STREAM: __socket_type = 1; +pub const __socket_type_SOCK_DGRAM: __socket_type = 2; +pub const __socket_type_SOCK_RAW: __socket_type = 3; +pub const __socket_type_SOCK_RDM: __socket_type = 4; +pub const __socket_type_SOCK_SEQPACKET: __socket_type = 5; +pub const __socket_type_SOCK_DCCP: __socket_type = 6; +pub const __socket_type_SOCK_PACKET: __socket_type = 10; +pub const __socket_type_SOCK_CLOEXEC: __socket_type = 524288; +pub const __socket_type_SOCK_NONBLOCK: __socket_type = 2048; +pub type __socket_type = ::std::os::raw::c_uint; +pub type sa_family_t = ::std::os::raw::c_ushort; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct sockaddr { + pub sa_family: sa_family_t, + pub sa_data: [::std::os::raw::c_uchar; 14usize], +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct sockaddr_storage { + pub ss_family: sa_family_t, + pub __ss_padding: [::std::os::raw::c_char; 118usize], + pub __ss_align: ::std::os::raw::c_ulong, +} +pub const MSG_OOB: ::std::os::raw::c_uint = 1; +pub const MSG_PEEK: ::std::os::raw::c_uint = 2; +pub const MSG_DONTROUTE: ::std::os::raw::c_uint = 4; +pub const MSG_CTRUNC: ::std::os::raw::c_uint = 8; +pub const MSG_PROXY: ::std::os::raw::c_uint = 16; +pub const MSG_TRUNC: ::std::os::raw::c_uint = 32; +pub const MSG_DONTWAIT: ::std::os::raw::c_uint = 64; +pub const MSG_EOR: ::std::os::raw::c_uint = 128; +pub const MSG_WAITALL: ::std::os::raw::c_uint = 256; +pub const MSG_FIN: ::std::os::raw::c_uint = 512; +pub const MSG_SYN: ::std::os::raw::c_uint = 1024; +pub const MSG_CONFIRM: ::std::os::raw::c_uint = 2048; +pub const MSG_RST: ::std::os::raw::c_uint = 4096; +pub const MSG_ERRQUEUE: ::std::os::raw::c_uint = 8192; +pub const MSG_NOSIGNAL: ::std::os::raw::c_uint = 16384; +pub const MSG_MORE: ::std::os::raw::c_uint = 32768; +pub const MSG_WAITFORONE: ::std::os::raw::c_uint = 65536; +pub const MSG_BATCH: ::std::os::raw::c_uint = 262144; +pub const MSG_ZEROCOPY: ::std::os::raw::c_uint = 67108864; +pub const MSG_FASTOPEN: ::std::os::raw::c_uint = 536870912; +pub const MSG_CMSG_CLOEXEC: ::std::os::raw::c_uint = 1073741824; +pub type _bindgen_ty_1 = ::std::os::raw::c_uint; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct msghdr { + pub msg_name: *mut ::std::os::raw::c_void, + pub msg_namelen: socklen_t, + pub msg_iov: *mut iovec, + pub msg_iovlen: size_t, + pub msg_control: *mut ::std::os::raw::c_void, + pub msg_controllen: size_t, + pub msg_flags: ::std::os::raw::c_int, +} +#[repr(C)] +#[derive(Debug)] +pub struct cmsghdr { + pub cmsg_len: size_t, + pub cmsg_level: ::std::os::raw::c_int, + pub cmsg_type: ::std::os::raw::c_int, + pub __cmsg_data: __IncompleteArrayField<::std::os::raw::c_uchar>, +} +extern "C" { + pub fn __cmsg_nxthdr(__mhdr: *mut msghdr, __cmsg: *mut cmsghdr) -> *mut cmsghdr; +} +pub const SCM_RIGHTS: ::std::os::raw::c_uint = 1; +pub type _bindgen_ty_2 = ::std::os::raw::c_uint; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct linger { + pub l_onoff: ::std::os::raw::c_int, + pub l_linger: ::std::os::raw::c_int, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct osockaddr { + pub sa_family: ::std::os::raw::c_ushort, + pub sa_data: [::std::os::raw::c_uchar; 14usize], +} +pub const SHUT_RD: ::std::os::raw::c_uint = 0; +pub const SHUT_WR: ::std::os::raw::c_uint = 1; +pub const SHUT_RDWR: ::std::os::raw::c_uint = 2; +pub type _bindgen_ty_3 = ::std::os::raw::c_uint; +extern "C" { + pub fn socket( + __domain: ::std::os::raw::c_int, + __type: ::std::os::raw::c_int, + __protocol: ::std::os::raw::c_int, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn socketpair( + __domain: ::std::os::raw::c_int, + __type: ::std::os::raw::c_int, + __protocol: ::std::os::raw::c_int, + __fds: *mut ::std::os::raw::c_int, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn bind( + __fd: ::std::os::raw::c_int, + __addr: *const sockaddr, + __len: socklen_t, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn getsockname( + __fd: ::std::os::raw::c_int, + __addr: *mut sockaddr, + __len: *mut socklen_t, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn connect( + __fd: ::std::os::raw::c_int, + __addr: *const sockaddr, + __len: socklen_t, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn getpeername( + __fd: ::std::os::raw::c_int, + __addr: *mut sockaddr, + __len: *mut socklen_t, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn send( + __fd: ::std::os::raw::c_int, + __buf: *const ::std::os::raw::c_void, + __n: size_t, + __flags: ::std::os::raw::c_int, + ) -> ssize_t; +} +extern "C" { + pub fn recv( + __fd: ::std::os::raw::c_int, + __buf: *mut ::std::os::raw::c_void, + __n: size_t, + __flags: ::std::os::raw::c_int, + ) -> ssize_t; +} +extern "C" { + pub fn sendto( + __fd: ::std::os::raw::c_int, + __buf: *const ::std::os::raw::c_void, + __n: size_t, + __flags: ::std::os::raw::c_int, + __addr: *const sockaddr, + __addr_len: socklen_t, + ) -> ssize_t; +} +extern "C" { + pub fn recvfrom( + __fd: ::std::os::raw::c_int, + __buf: *mut ::std::os::raw::c_void, + __n: size_t, + __flags: ::std::os::raw::c_int, + __addr: *mut sockaddr, + __addr_len: *mut socklen_t, + ) -> ssize_t; +} +extern "C" { + pub fn sendmsg( + __fd: ::std::os::raw::c_int, + __message: *const msghdr, + __flags: ::std::os::raw::c_int, + ) -> ssize_t; +} +extern "C" { + pub fn recvmsg( + __fd: ::std::os::raw::c_int, + __message: *mut msghdr, + __flags: ::std::os::raw::c_int, + ) -> ssize_t; +} +extern "C" { + pub fn getsockopt( + __fd: ::std::os::raw::c_int, + __level: ::std::os::raw::c_int, + __optname: ::std::os::raw::c_int, + __optval: *mut ::std::os::raw::c_void, + __optlen: *mut socklen_t, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn setsockopt( + __fd: ::std::os::raw::c_int, + __level: ::std::os::raw::c_int, + __optname: ::std::os::raw::c_int, + __optval: *const ::std::os::raw::c_void, + __optlen: socklen_t, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn listen(__fd: ::std::os::raw::c_int, __n: ::std::os::raw::c_int) + -> ::std::os::raw::c_int; +} +extern "C" { + pub fn accept( + __fd: ::std::os::raw::c_int, + __addr: *mut sockaddr, + __addr_len: *mut socklen_t, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn shutdown( + __fd: ::std::os::raw::c_int, + __how: ::std::os::raw::c_int, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn sockatmark(__fd: ::std::os::raw::c_int) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn isfdtype( + __fd: ::std::os::raw::c_int, + __fdtype: ::std::os::raw::c_int, + ) -> ::std::os::raw::c_int; +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct sync_serial_settings { + pub clock_rate: ::std::os::raw::c_uint, + pub clock_type: ::std::os::raw::c_uint, + pub loopback: ::std::os::raw::c_ushort, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct te1_settings { + pub clock_rate: ::std::os::raw::c_uint, + pub clock_type: ::std::os::raw::c_uint, + pub loopback: ::std::os::raw::c_ushort, + pub slot_map: ::std::os::raw::c_uint, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct raw_hdlc_proto { + pub encoding: ::std::os::raw::c_ushort, + pub parity: ::std::os::raw::c_ushort, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct fr_proto { + pub t391: ::std::os::raw::c_uint, + pub t392: ::std::os::raw::c_uint, + pub n391: ::std::os::raw::c_uint, + pub n392: ::std::os::raw::c_uint, + pub n393: ::std::os::raw::c_uint, + pub lmi: ::std::os::raw::c_ushort, + pub dce: ::std::os::raw::c_ushort, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct fr_proto_pvc { + pub dlci: ::std::os::raw::c_uint, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct fr_proto_pvc_info { + pub dlci: ::std::os::raw::c_uint, + pub master: [::std::os::raw::c_char; 16usize], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct cisco_proto { + pub interval: ::std::os::raw::c_uint, + pub timeout: ::std::os::raw::c_uint, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct x25_hdlc_proto { + pub dce: ::std::os::raw::c_ushort, + pub modulo: ::std::os::raw::c_uint, + pub window: ::std::os::raw::c_uint, + pub t1: ::std::os::raw::c_uint, + pub t2: ::std::os::raw::c_uint, + pub n2: ::std::os::raw::c_uint, +} +pub const net_device_flags_IFF_UP: net_device_flags = 1; +pub const net_device_flags_IFF_BROADCAST: net_device_flags = 2; +pub const net_device_flags_IFF_DEBUG: net_device_flags = 4; +pub const net_device_flags_IFF_LOOPBACK: net_device_flags = 8; +pub const net_device_flags_IFF_POINTOPOINT: net_device_flags = 16; +pub const net_device_flags_IFF_NOTRAILERS: net_device_flags = 32; +pub const net_device_flags_IFF_RUNNING: net_device_flags = 64; +pub const net_device_flags_IFF_NOARP: net_device_flags = 128; +pub const net_device_flags_IFF_PROMISC: net_device_flags = 256; +pub const net_device_flags_IFF_ALLMULTI: net_device_flags = 512; +pub const net_device_flags_IFF_MASTER: net_device_flags = 1024; +pub const net_device_flags_IFF_SLAVE: net_device_flags = 2048; +pub const net_device_flags_IFF_MULTICAST: net_device_flags = 4096; +pub const net_device_flags_IFF_PORTSEL: net_device_flags = 8192; +pub const net_device_flags_IFF_AUTOMEDIA: net_device_flags = 16384; +pub const net_device_flags_IFF_DYNAMIC: net_device_flags = 32768; +pub const net_device_flags_IFF_LOWER_UP: net_device_flags = 65536; +pub const net_device_flags_IFF_DORMANT: net_device_flags = 131072; +pub const net_device_flags_IFF_ECHO: net_device_flags = 262144; +#[doc = " enum net_device_flags - &struct net_device flags"] +#[doc = ""] +#[doc = " These are the &struct net_device flags, they can be set by drivers, the"] +#[doc = " kernel and some can be triggered by userspace. Userspace can query and"] +#[doc = " set these flags using userspace utilities but there is also a sysfs"] +#[doc = " entry available for all dev flags which can be queried and set. These flags"] +#[doc = " are shared for all types of net_devices. The sysfs entries are available"] +#[doc = " via /sys/class/net//flags. Flags which can be toggled through sysfs"] +#[doc = " are annotated below, note that only a few flags can be toggled and some"] +#[doc = " other flags are always preserved from the original net_device flags"] +#[doc = " even if you try to set them via sysfs. Flags which are always preserved"] +#[doc = " are kept under the flag grouping @IFF_VOLATILE. Flags which are __volatile__"] +#[doc = " are annotated below as such."] +#[doc = ""] +#[doc = " You should have a pretty good reason to be extending these flags."] +#[doc = ""] +#[doc = " @IFF_UP: interface is up. Can be toggled through sysfs."] +#[doc = " @IFF_BROADCAST: broadcast address valid. Volatile."] +#[doc = " @IFF_DEBUG: turn on debugging. Can be toggled through sysfs."] +#[doc = " @IFF_LOOPBACK: is a loopback net. Volatile."] +#[doc = " @IFF_POINTOPOINT: interface is has p-p link. Volatile."] +#[doc = " @IFF_NOTRAILERS: avoid use of trailers. Can be toggled through sysfs."] +#[doc = "\tVolatile."] +#[doc = " @IFF_RUNNING: interface RFC2863 OPER_UP. Volatile."] +#[doc = " @IFF_NOARP: no ARP protocol. Can be toggled through sysfs. Volatile."] +#[doc = " @IFF_PROMISC: receive all packets. Can be toggled through sysfs."] +#[doc = " @IFF_ALLMULTI: receive all multicast packets. Can be toggled through"] +#[doc = "\tsysfs."] +#[doc = " @IFF_MASTER: master of a load balancer. Volatile."] +#[doc = " @IFF_SLAVE: slave of a load balancer. Volatile."] +#[doc = " @IFF_MULTICAST: Supports multicast. Can be toggled through sysfs."] +#[doc = " @IFF_PORTSEL: can set media type. Can be toggled through sysfs."] +#[doc = " @IFF_AUTOMEDIA: auto media select active. Can be toggled through sysfs."] +#[doc = " @IFF_DYNAMIC: dialup device with changing addresses. Can be toggled"] +#[doc = "\tthrough sysfs."] +#[doc = " @IFF_LOWER_UP: driver signals L1 up. Volatile."] +#[doc = " @IFF_DORMANT: driver signals dormant. Volatile."] +#[doc = " @IFF_ECHO: echo sent packets. Volatile."] +pub type net_device_flags = ::std::os::raw::c_uint; +pub const IF_OPER_UNKNOWN: ::std::os::raw::c_uint = 0; +pub const IF_OPER_NOTPRESENT: ::std::os::raw::c_uint = 1; +pub const IF_OPER_DOWN: ::std::os::raw::c_uint = 2; +pub const IF_OPER_LOWERLAYERDOWN: ::std::os::raw::c_uint = 3; +pub const IF_OPER_TESTING: ::std::os::raw::c_uint = 4; +pub const IF_OPER_DORMANT: ::std::os::raw::c_uint = 5; +pub const IF_OPER_UP: ::std::os::raw::c_uint = 6; +pub type _bindgen_ty_4 = ::std::os::raw::c_uint; +pub const IF_LINK_MODE_DEFAULT: ::std::os::raw::c_uint = 0; +pub const IF_LINK_MODE_DORMANT: ::std::os::raw::c_uint = 1; +pub const IF_LINK_MODE_TESTING: ::std::os::raw::c_uint = 2; +pub type _bindgen_ty_5 = ::std::os::raw::c_uint; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ifmap { + pub mem_start: ::std::os::raw::c_ulong, + pub mem_end: ::std::os::raw::c_ulong, + pub base_addr: ::std::os::raw::c_ushort, + pub irq: ::std::os::raw::c_uchar, + pub dma: ::std::os::raw::c_uchar, + pub port: ::std::os::raw::c_uchar, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct if_settings { + pub type_: ::std::os::raw::c_uint, + pub size: ::std::os::raw::c_uint, + pub ifs_ifsu: if_settings__bindgen_ty_1, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union if_settings__bindgen_ty_1 { + pub raw_hdlc: *mut raw_hdlc_proto, + pub cisco: *mut cisco_proto, + pub fr: *mut fr_proto, + pub fr_pvc: *mut fr_proto_pvc, + pub fr_pvc_info: *mut fr_proto_pvc_info, + pub x25: *mut x25_hdlc_proto, + pub sync: *mut sync_serial_settings, + pub te1: *mut te1_settings, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct ifreq { + pub ifr_ifrn: ifreq__bindgen_ty_1, + pub ifr_ifru: ifreq__bindgen_ty_2, +} + +impl Default for ifreq { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union ifreq__bindgen_ty_1 { + pub ifrn_name: [::std::os::raw::c_uchar; 16usize], +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union ifreq__bindgen_ty_2 { + pub ifru_addr: sockaddr, + pub ifru_dstaddr: sockaddr, + pub ifru_broadaddr: sockaddr, + pub ifru_netmask: sockaddr, + pub ifru_hwaddr: sockaddr, + pub ifru_flags: ::std::os::raw::c_short, + pub ifru_ivalue: ::std::os::raw::c_int, + pub ifru_mtu: ::std::os::raw::c_int, + pub ifru_map: ifmap, + pub ifru_slave: [::std::os::raw::c_uchar; 16usize], + pub ifru_newname: [::std::os::raw::c_uchar; 16usize], + pub ifru_data: *mut ::std::os::raw::c_void, + pub ifru_settings: if_settings, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct ifconf { + pub ifc_len: ::std::os::raw::c_int, + pub ifc_ifcu: ifconf__bindgen_ty_1, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union ifconf__bindgen_ty_1 { + pub ifcu_buf: *mut ::std::os::raw::c_char, + pub ifcu_req: *mut ifreq, +} diff --git a/vendor/cube-hypervisor/net_gen/src/inn.rs b/vendor/cube-hypervisor/net_gen/src/inn.rs new file mode 100644 index 0000000..f7a4e50 --- /dev/null +++ b/vendor/cube-hypervisor/net_gen/src/inn.rs @@ -0,0 +1,294 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +// bindgen /usr/include/linux/in.h --no-layout-tests + +/* automatically generated by rust-bindgen 0.58.1 */ + +pub const __BITS_PER_LONG: u32 = 64; +pub const __FD_SETSIZE: u32 = 1024; +pub const __UAPI_DEF_IF_IFCONF: u32 = 1; +pub const __UAPI_DEF_IF_IFMAP: u32 = 1; +pub const __UAPI_DEF_IF_IFNAMSIZ: u32 = 1; +pub const __UAPI_DEF_IF_IFREQ: u32 = 1; +pub const __UAPI_DEF_IF_NET_DEVICE_FLAGS: u32 = 1; +pub const __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO: u32 = 1; +pub const __UAPI_DEF_IN_ADDR: u32 = 1; +pub const __UAPI_DEF_IN_IPPROTO: u32 = 1; +pub const __UAPI_DEF_IN_PKTINFO: u32 = 1; +pub const __UAPI_DEF_IP_MREQ: u32 = 1; +pub const __UAPI_DEF_SOCKADDR_IN: u32 = 1; +pub const __UAPI_DEF_IN_CLASS: u32 = 1; +pub const __UAPI_DEF_IN6_ADDR: u32 = 1; +pub const __UAPI_DEF_IN6_ADDR_ALT: u32 = 1; +pub const __UAPI_DEF_SOCKADDR_IN6: u32 = 1; +pub const __UAPI_DEF_IPV6_MREQ: u32 = 1; +pub const __UAPI_DEF_IPPROTO_V6: u32 = 1; +pub const __UAPI_DEF_IPV6_OPTIONS: u32 = 1; +pub const __UAPI_DEF_IN6_PKTINFO: u32 = 1; +pub const __UAPI_DEF_IP6_MTUINFO: u32 = 1; +pub const __UAPI_DEF_SOCKADDR_IPX: u32 = 1; +pub const __UAPI_DEF_IPX_ROUTE_DEFINITION: u32 = 1; +pub const __UAPI_DEF_IPX_INTERFACE_DEFINITION: u32 = 1; +pub const __UAPI_DEF_IPX_CONFIG_DATA: u32 = 1; +pub const __UAPI_DEF_IPX_ROUTE_DEF: u32 = 1; +pub const __UAPI_DEF_XATTR: u32 = 1; +pub const _K_SS_MAXSIZE: u32 = 128; +pub const IP_TOS: u32 = 1; +pub const IP_TTL: u32 = 2; +pub const IP_HDRINCL: u32 = 3; +pub const IP_OPTIONS: u32 = 4; +pub const IP_ROUTER_ALERT: u32 = 5; +pub const IP_RECVOPTS: u32 = 6; +pub const IP_RETOPTS: u32 = 7; +pub const IP_PKTINFO: u32 = 8; +pub const IP_PKTOPTIONS: u32 = 9; +pub const IP_MTU_DISCOVER: u32 = 10; +pub const IP_RECVERR: u32 = 11; +pub const IP_RECVTTL: u32 = 12; +pub const IP_RECVTOS: u32 = 13; +pub const IP_MTU: u32 = 14; +pub const IP_FREEBIND: u32 = 15; +pub const IP_IPSEC_POLICY: u32 = 16; +pub const IP_XFRM_POLICY: u32 = 17; +pub const IP_PASSSEC: u32 = 18; +pub const IP_TRANSPARENT: u32 = 19; +pub const IP_RECVRETOPTS: u32 = 7; +pub const IP_ORIGDSTADDR: u32 = 20; +pub const IP_RECVORIGDSTADDR: u32 = 20; +pub const IP_MINTTL: u32 = 21; +pub const IP_NODEFRAG: u32 = 22; +pub const IP_CHECKSUM: u32 = 23; +pub const IP_BIND_ADDRESS_NO_PORT: u32 = 24; +pub const IP_RECVFRAGSIZE: u32 = 25; +pub const IP_PMTUDISC_DONT: u32 = 0; +pub const IP_PMTUDISC_WANT: u32 = 1; +pub const IP_PMTUDISC_DO: u32 = 2; +pub const IP_PMTUDISC_PROBE: u32 = 3; +pub const IP_PMTUDISC_INTERFACE: u32 = 4; +pub const IP_PMTUDISC_OMIT: u32 = 5; +pub const IP_MULTICAST_IF: u32 = 32; +pub const IP_MULTICAST_TTL: u32 = 33; +pub const IP_MULTICAST_LOOP: u32 = 34; +pub const IP_ADD_MEMBERSHIP: u32 = 35; +pub const IP_DROP_MEMBERSHIP: u32 = 36; +pub const IP_UNBLOCK_SOURCE: u32 = 37; +pub const IP_BLOCK_SOURCE: u32 = 38; +pub const IP_ADD_SOURCE_MEMBERSHIP: u32 = 39; +pub const IP_DROP_SOURCE_MEMBERSHIP: u32 = 40; +pub const IP_MSFILTER: u32 = 41; +pub const MCAST_JOIN_GROUP: u32 = 42; +pub const MCAST_BLOCK_SOURCE: u32 = 43; +pub const MCAST_UNBLOCK_SOURCE: u32 = 44; +pub const MCAST_LEAVE_GROUP: u32 = 45; +pub const MCAST_JOIN_SOURCE_GROUP: u32 = 46; +pub const MCAST_LEAVE_SOURCE_GROUP: u32 = 47; +pub const MCAST_MSFILTER: u32 = 48; +pub const IP_MULTICAST_ALL: u32 = 49; +pub const IP_UNICAST_IF: u32 = 50; +pub const MCAST_EXCLUDE: u32 = 0; +pub const MCAST_INCLUDE: u32 = 1; +pub const IP_DEFAULT_MULTICAST_TTL: u32 = 1; +pub const IP_DEFAULT_MULTICAST_LOOP: u32 = 1; +pub const __SOCK_SIZE__: u32 = 16; +pub const IN_CLASSA_NET: u32 = 4278190080; +pub const IN_CLASSA_NSHIFT: u32 = 24; +pub const IN_CLASSA_HOST: u32 = 16777215; +pub const IN_CLASSA_MAX: u32 = 128; +pub const IN_CLASSB_NET: u32 = 4294901760; +pub const IN_CLASSB_NSHIFT: u32 = 16; +pub const IN_CLASSB_HOST: u32 = 65535; +pub const IN_CLASSB_MAX: u32 = 65536; +pub const IN_CLASSC_NET: u32 = 4294967040; +pub const IN_CLASSC_NSHIFT: u32 = 8; +pub const IN_CLASSC_HOST: u32 = 255; +pub const IN_MULTICAST_NET: u32 = 3758096384; +pub const IN_CLASSE_NET: u32 = 4294967295; +pub const IN_CLASSE_NSHIFT: u32 = 0; +pub const IN_LOOPBACKNET: u32 = 127; +pub const INADDR_LOOPBACK: u32 = 2130706433; +pub const INADDR_UNSPEC_GROUP: u32 = 3758096384; +pub const INADDR_ALLHOSTS_GROUP: u32 = 3758096385; +pub const INADDR_ALLRTRS_GROUP: u32 = 3758096386; +pub const INADDR_ALLSNOOPERS_GROUP: u32 = 3758096490; +pub const INADDR_MAX_LOCAL_GROUP: u32 = 3758096639; +pub const __LITTLE_ENDIAN: u32 = 1234; +pub type __s8 = ::std::os::raw::c_schar; +pub type __u8 = ::std::os::raw::c_uchar; +pub type __s16 = ::std::os::raw::c_short; +pub type __u16 = ::std::os::raw::c_ushort; +pub type __s32 = ::std::os::raw::c_int; +pub type __u32 = ::std::os::raw::c_uint; +pub type __s64 = ::std::os::raw::c_longlong; +pub type __u64 = ::std::os::raw::c_ulonglong; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __kernel_fd_set { + pub fds_bits: [::std::os::raw::c_ulong; 16usize], +} +pub type __kernel_sighandler_t = + ::std::option::Option; +pub type __kernel_key_t = ::std::os::raw::c_int; +pub type __kernel_mqd_t = ::std::os::raw::c_int; +pub type __kernel_old_uid_t = ::std::os::raw::c_ushort; +pub type __kernel_old_gid_t = ::std::os::raw::c_ushort; +pub type __kernel_old_dev_t = ::std::os::raw::c_ulong; +pub type __kernel_long_t = ::std::os::raw::c_long; +pub type __kernel_ulong_t = ::std::os::raw::c_ulong; +pub type __kernel_ino_t = __kernel_ulong_t; +pub type __kernel_mode_t = ::std::os::raw::c_uint; +pub type __kernel_pid_t = ::std::os::raw::c_int; +pub type __kernel_ipc_pid_t = ::std::os::raw::c_int; +pub type __kernel_uid_t = ::std::os::raw::c_uint; +pub type __kernel_gid_t = ::std::os::raw::c_uint; +pub type __kernel_suseconds_t = __kernel_long_t; +pub type __kernel_daddr_t = ::std::os::raw::c_int; +pub type __kernel_uid32_t = ::std::os::raw::c_uint; +pub type __kernel_gid32_t = ::std::os::raw::c_uint; +pub type __kernel_size_t = __kernel_ulong_t; +pub type __kernel_ssize_t = __kernel_long_t; +pub type __kernel_ptrdiff_t = __kernel_long_t; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __kernel_fsid_t { + pub val: [::std::os::raw::c_int; 2usize], +} +pub type __kernel_off_t = __kernel_long_t; +pub type __kernel_loff_t = ::std::os::raw::c_longlong; +pub type __kernel_old_time_t = __kernel_long_t; +pub type __kernel_time_t = __kernel_long_t; +pub type __kernel_time64_t = ::std::os::raw::c_longlong; +pub type __kernel_clock_t = __kernel_long_t; +pub type __kernel_timer_t = ::std::os::raw::c_int; +pub type __kernel_clockid_t = ::std::os::raw::c_int; +pub type __kernel_caddr_t = *mut ::std::os::raw::c_char; +pub type __kernel_uid16_t = ::std::os::raw::c_ushort; +pub type __kernel_gid16_t = ::std::os::raw::c_ushort; +pub type __le16 = __u16; +pub type __be16 = __u16; +pub type __le32 = __u32; +pub type __be32 = __u32; +pub type __le64 = __u64; +pub type __be64 = __u64; +pub type __sum16 = __u16; +pub type __wsum = __u32; +pub type __poll_t = ::std::os::raw::c_uint; +pub type __kernel_sa_family_t = ::std::os::raw::c_ushort; +#[repr(C)] +#[derive(Copy, Clone)] +pub struct __kernel_sockaddr_storage { + pub __bindgen_anon_1: __kernel_sockaddr_storage__bindgen_ty_1, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union __kernel_sockaddr_storage__bindgen_ty_1 { + pub __bindgen_anon_1: __kernel_sockaddr_storage__bindgen_ty_1__bindgen_ty_1, + pub __align: *mut ::std::os::raw::c_void, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct __kernel_sockaddr_storage__bindgen_ty_1__bindgen_ty_1 { + pub ss_family: __kernel_sa_family_t, + pub __data: [::std::os::raw::c_char; 126usize], +} +pub const IPPROTO_IP: ::std::os::raw::c_uint = 0; +pub const IPPROTO_ICMP: ::std::os::raw::c_uint = 1; +pub const IPPROTO_IGMP: ::std::os::raw::c_uint = 2; +pub const IPPROTO_IPIP: ::std::os::raw::c_uint = 4; +pub const IPPROTO_TCP: ::std::os::raw::c_uint = 6; +pub const IPPROTO_EGP: ::std::os::raw::c_uint = 8; +pub const IPPROTO_PUP: ::std::os::raw::c_uint = 12; +pub const IPPROTO_UDP: ::std::os::raw::c_uint = 17; +pub const IPPROTO_IDP: ::std::os::raw::c_uint = 22; +pub const IPPROTO_TP: ::std::os::raw::c_uint = 29; +pub const IPPROTO_DCCP: ::std::os::raw::c_uint = 33; +pub const IPPROTO_IPV6: ::std::os::raw::c_uint = 41; +pub const IPPROTO_RSVP: ::std::os::raw::c_uint = 46; +pub const IPPROTO_GRE: ::std::os::raw::c_uint = 47; +pub const IPPROTO_ESP: ::std::os::raw::c_uint = 50; +pub const IPPROTO_AH: ::std::os::raw::c_uint = 51; +pub const IPPROTO_MTP: ::std::os::raw::c_uint = 92; +pub const IPPROTO_BEETPH: ::std::os::raw::c_uint = 94; +pub const IPPROTO_ENCAP: ::std::os::raw::c_uint = 98; +pub const IPPROTO_PIM: ::std::os::raw::c_uint = 103; +pub const IPPROTO_COMP: ::std::os::raw::c_uint = 108; +pub const IPPROTO_SCTP: ::std::os::raw::c_uint = 132; +pub const IPPROTO_UDPLITE: ::std::os::raw::c_uint = 136; +pub const IPPROTO_MPLS: ::std::os::raw::c_uint = 137; +pub const IPPROTO_ETHERNET: ::std::os::raw::c_uint = 143; +pub const IPPROTO_RAW: ::std::os::raw::c_uint = 255; +pub const IPPROTO_MPTCP: ::std::os::raw::c_uint = 262; +pub const IPPROTO_MAX: ::std::os::raw::c_uint = 263; +pub type _bindgen_ty_1 = ::std::os::raw::c_uint; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct in_addr { + pub s_addr: __be32, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ip_mreq { + pub imr_multiaddr: in_addr, + pub imr_interface: in_addr, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ip_mreqn { + pub imr_multiaddr: in_addr, + pub imr_address: in_addr, + pub imr_ifindex: ::std::os::raw::c_int, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ip_mreq_source { + pub imr_multiaddr: __be32, + pub imr_interface: __be32, + pub imr_sourceaddr: __be32, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ip_msfilter { + pub imsf_multiaddr: __be32, + pub imsf_interface: __be32, + pub imsf_fmode: __u32, + pub imsf_numsrc: __u32, + pub imsf_slist: [__be32; 1usize], +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct group_req { + pub gr_interface: __u32, + pub gr_group: __kernel_sockaddr_storage, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct group_source_req { + pub gsr_interface: __u32, + pub gsr_group: __kernel_sockaddr_storage, + pub gsr_source: __kernel_sockaddr_storage, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct group_filter { + pub gf_interface: __u32, + pub gf_group: __kernel_sockaddr_storage, + pub gf_fmode: __u32, + pub gf_numsrc: __u32, + pub gf_slist: [__kernel_sockaddr_storage; 1usize], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct in_pktinfo { + pub ipi_ifindex: ::std::os::raw::c_int, + pub ipi_spec_dst: in_addr, + pub ipi_addr: in_addr, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct sockaddr_in { + pub sin_family: __kernel_sa_family_t, + pub sin_port: __be16, + pub sin_addr: in_addr, + pub __pad: [::std::os::raw::c_uchar; 8usize], +} diff --git a/vendor/cube-hypervisor/net_gen/src/lib.rs b/vendor/cube-hypervisor/net_gen/src/lib.rs new file mode 100644 index 0000000..7d0be17 --- /dev/null +++ b/vendor/cube-hypervisor/net_gen/src/lib.rs @@ -0,0 +1,63 @@ +// Copyright TUNTAP, 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the THIRD-PARTY file. + +#![allow(clippy::all)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +#[macro_use] +extern crate vmm_sys_util; + +// generated with bindgen /usr/include/linux/if.h --no-unstable-rust +// --constified-enum '*' --with-derive-default -- -D __UAPI_DEF_IF_IFNAMSIZ -D +// __UAPI_DEF_IF_NET_DEVICE_FLAGS -D __UAPI_DEF_IF_IFREQ -D __UAPI_DEF_IF_IFMAP +// Name is "iff" to avoid conflicting with "if" keyword. +// Generated against Linux 4.11 to include fix "uapi: fix linux/if.h userspace +// compilation errors". +// Manual fixup of ifrn_name to be of type c_uchar instead of c_char. +pub mod iff; +// generated with bindgen /usr/include/linux/if_tun.h --no-unstable-rust +// --constified-enum '*' --with-derive-default +pub mod if_tun; +// generated with bindgen /usr/include/linux/in.h --no-unstable-rust +// --constified-enum '*' --with-derive-default +// Name is "inn" to avoid conflicting with "in" keyword. +pub mod inn; +// generated with bindgen /usr/include/linux/sockios.h --no-unstable-rust +// --constified-enum '*' --with-derive-default +pub mod sockios; +pub use if_tun::{ + sock_fprog, IFF_MULTI_QUEUE, IFF_NO_PI, IFF_TAP, IFF_VNET_HDR, TUN_F_CSUM, TUN_F_TSO4, + TUN_F_TSO6, TUN_F_TSO_ECN, TUN_F_UFO, +}; +pub use iff::{ifreq, net_device_flags_IFF_UP, setsockopt, sockaddr, AF_INET}; +pub use inn::sockaddr_in; + +pub const TUNTAP: ::std::os::raw::c_uint = 84; + +ioctl_iow_nr!(TUNSETNOCSUM, TUNTAP, 200, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETDEBUG, TUNTAP, 201, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETIFF, TUNTAP, 202, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETPERSIST, TUNTAP, 203, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETOWNER, TUNTAP, 204, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETLINK, TUNTAP, 205, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETGROUP, TUNTAP, 206, ::std::os::raw::c_int); +ioctl_ior_nr!(TUNGETFEATURES, TUNTAP, 207, ::std::os::raw::c_uint); +ioctl_iow_nr!(TUNSETOFFLOAD, TUNTAP, 208, ::std::os::raw::c_uint); +ioctl_iow_nr!(TUNSETTXFILTER, TUNTAP, 209, ::std::os::raw::c_uint); +ioctl_ior_nr!(TUNGETIFF, TUNTAP, 210, ::std::os::raw::c_uint); +ioctl_ior_nr!(TUNGETSNDBUF, TUNTAP, 211, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETSNDBUF, TUNTAP, 212, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNATTACHFILTER, TUNTAP, 213, sock_fprog); +ioctl_iow_nr!(TUNDETACHFILTER, TUNTAP, 214, sock_fprog); +ioctl_ior_nr!(TUNGETVNETHDRSZ, TUNTAP, 215, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETVNETHDRSZ, TUNTAP, 216, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETQUEUE, TUNTAP, 217, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETIFINDEX, TUNTAP, 218, ::std::os::raw::c_uint); +ioctl_ior_nr!(TUNGETFILTER, TUNTAP, 219, sock_fprog); +ioctl_iow_nr!(TUNSETVNETLE, TUNTAP, 220, ::std::os::raw::c_int); +ioctl_ior_nr!(TUNGETVNETLE, TUNTAP, 221, ::std::os::raw::c_int); +ioctl_iow_nr!(TUNSETVNETBE, TUNTAP, 222, ::std::os::raw::c_int); +ioctl_ior_nr!(TUNGETVNETBE, TUNTAP, 223, ::std::os::raw::c_int); diff --git a/vendor/cube-hypervisor/net_gen/src/sockios.rs b/vendor/cube-hypervisor/net_gen/src/sockios.rs new file mode 100644 index 0000000..ad3dee7 --- /dev/null +++ b/vendor/cube-hypervisor/net_gen/src/sockios.rs @@ -0,0 +1,95 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +// bindgen /usr/include/linux/sockios.h --no-layout-tests + +/* automatically generated by rust-bindgen 0.58.1 */ + +pub const __BITS_PER_LONG: u32 = 64; +pub const FIOSETOWN: u32 = 35073; +pub const SIOCSPGRP: u32 = 35074; +pub const FIOGETOWN: u32 = 35075; +pub const SIOCGPGRP: u32 = 35076; +pub const SIOCATMARK: u32 = 35077; +pub const SIOCGSTAMP_OLD: u32 = 35078; +pub const SIOCGSTAMPNS_OLD: u32 = 35079; +pub const SOCK_IOC_TYPE: u32 = 137; +pub const SIOCGSTAMP: u32 = 35078; +pub const SIOCGSTAMPNS: u32 = 35079; +pub const SIOCADDRT: u32 = 35083; +pub const SIOCDELRT: u32 = 35084; +pub const SIOCRTMSG: u32 = 35085; +pub const SIOCGIFNAME: u32 = 35088; +pub const SIOCSIFLINK: u32 = 35089; +pub const SIOCGIFCONF: u32 = 35090; +pub const SIOCGIFFLAGS: u32 = 35091; +pub const SIOCSIFFLAGS: u32 = 35092; +pub const SIOCGIFADDR: u32 = 35093; +pub const SIOCSIFADDR: u32 = 35094; +pub const SIOCGIFDSTADDR: u32 = 35095; +pub const SIOCSIFDSTADDR: u32 = 35096; +pub const SIOCGIFBRDADDR: u32 = 35097; +pub const SIOCSIFBRDADDR: u32 = 35098; +pub const SIOCGIFNETMASK: u32 = 35099; +pub const SIOCSIFNETMASK: u32 = 35100; +pub const SIOCGIFMETRIC: u32 = 35101; +pub const SIOCSIFMETRIC: u32 = 35102; +pub const SIOCGIFMEM: u32 = 35103; +pub const SIOCSIFMEM: u32 = 35104; +pub const SIOCGIFMTU: u32 = 35105; +pub const SIOCSIFMTU: u32 = 35106; +pub const SIOCSIFNAME: u32 = 35107; +pub const SIOCSIFHWADDR: u32 = 35108; +pub const SIOCGIFENCAP: u32 = 35109; +pub const SIOCSIFENCAP: u32 = 35110; +pub const SIOCGIFHWADDR: u32 = 35111; +pub const SIOCGIFSLAVE: u32 = 35113; +pub const SIOCSIFSLAVE: u32 = 35120; +pub const SIOCADDMULTI: u32 = 35121; +pub const SIOCDELMULTI: u32 = 35122; +pub const SIOCGIFINDEX: u32 = 35123; +pub const SIOGIFINDEX: u32 = 35123; +pub const SIOCSIFPFLAGS: u32 = 35124; +pub const SIOCGIFPFLAGS: u32 = 35125; +pub const SIOCDIFADDR: u32 = 35126; +pub const SIOCSIFHWBROADCAST: u32 = 35127; +pub const SIOCGIFCOUNT: u32 = 35128; +pub const SIOCGIFBR: u32 = 35136; +pub const SIOCSIFBR: u32 = 35137; +pub const SIOCGIFTXQLEN: u32 = 35138; +pub const SIOCSIFTXQLEN: u32 = 35139; +pub const SIOCETHTOOL: u32 = 35142; +pub const SIOCGMIIPHY: u32 = 35143; +pub const SIOCGMIIREG: u32 = 35144; +pub const SIOCSMIIREG: u32 = 35145; +pub const SIOCWANDEV: u32 = 35146; +pub const SIOCOUTQNSD: u32 = 35147; +pub const SIOCGSKNS: u32 = 35148; +pub const SIOCDARP: u32 = 35155; +pub const SIOCGARP: u32 = 35156; +pub const SIOCSARP: u32 = 35157; +pub const SIOCDRARP: u32 = 35168; +pub const SIOCGRARP: u32 = 35169; +pub const SIOCSRARP: u32 = 35170; +pub const SIOCGIFMAP: u32 = 35184; +pub const SIOCSIFMAP: u32 = 35185; +pub const SIOCADDDLCI: u32 = 35200; +pub const SIOCDELDLCI: u32 = 35201; +pub const SIOCGIFVLAN: u32 = 35202; +pub const SIOCSIFVLAN: u32 = 35203; +pub const SIOCBONDENSLAVE: u32 = 35216; +pub const SIOCBONDRELEASE: u32 = 35217; +pub const SIOCBONDSETHWADDR: u32 = 35218; +pub const SIOCBONDSLAVEINFOQUERY: u32 = 35219; +pub const SIOCBONDINFOQUERY: u32 = 35220; +pub const SIOCBONDCHANGEACTIVE: u32 = 35221; +pub const SIOCBRADDBR: u32 = 35232; +pub const SIOCBRDELBR: u32 = 35233; +pub const SIOCBRADDIF: u32 = 35234; +pub const SIOCBRDELIF: u32 = 35235; +pub const SIOCSHWTSTAMP: u32 = 35248; +pub const SIOCGHWTSTAMP: u32 = 35249; +pub const SIOCDEVPRIVATE: u32 = 35312; +pub const SIOCPROTOPRIVATE: u32 = 35296; diff --git a/vendor/cube-hypervisor/net_util/Cargo.toml b/vendor/cube-hypervisor/net_util/Cargo.toml new file mode 100644 index 0000000..5c45168 --- /dev/null +++ b/vendor/cube-hypervisor/net_util/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "net_util" +version = "0.1.0" +authors = ["The Chromium OS Authors"] +edition = "2021" + +[dependencies] +epoll = "4.3.1" +getrandom = "0.2.7" +libc = "0.2.137" +log = "0.4.17" +net_gen = { path = "../net_gen" } +rate_limiter = { path = "../rate_limiter" } +serde = { workspace = true, features = ["derive"] } +thiserror = "1.0.37" +virtio-bindings = "0.1.0" +virtio-queue = { workspace = true } +vm-memory = { workspace = true, features = ["backend-mmap", "backend-atomic", "backend-bitmap"] } +vm-virtio = { path = "../vm-virtio" } +vmm-sys-util = "0.12.1" +serde_json = "1.0.87" + +[dev-dependencies] +once_cell = "1.16.0" +pnet = "0.34.0" +pnet_datalink = "0.34.0" diff --git a/vendor/cube-hypervisor/net_util/src/ctrl_queue.rs b/vendor/cube-hypervisor/net_util/src/ctrl_queue.rs new file mode 100644 index 0000000..76477d5 --- /dev/null +++ b/vendor/cube-hypervisor/net_util/src/ctrl_queue.rs @@ -0,0 +1,177 @@ +// Copyright (c) 2021 Intel Corporation. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use crate::GuestMemoryMmap; +use crate::Tap; +use libc::c_uint; +use std::sync::Arc; +use virtio_bindings::bindings::virtio_net::{ + VIRTIO_NET_CTRL_GUEST_OFFLOADS, VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, VIRTIO_NET_CTRL_MQ, + VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN, + VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, VIRTIO_NET_ERR, VIRTIO_NET_F_GUEST_CSUM, + VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, + VIRTIO_NET_F_GUEST_UFO, VIRTIO_NET_OK, +}; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{ByteValued, Bytes, GuestMemoryError}; +use vm_virtio::{AccessPlatform, Translatable}; + +#[derive(Debug)] +pub enum Error { + /// Read queue failed. + GuestMemory(GuestMemoryError), + /// No control header descriptor + NoControlHeaderDescriptor, + /// Missing the data descriptor in the chain. + NoDataDescriptor, + /// No status descriptor + NoStatusDescriptor, + /// Failed adding used index + QueueAddUsed(virtio_queue::Error), + /// Failed creating an iterator over the queue + QueueIterator(virtio_queue::Error), + /// Failed enabling notification for the queue + QueueEnableNotification(virtio_queue::Error), +} + +type Result = std::result::Result; + +#[repr(C, packed)] +#[derive(Debug, Clone, Copy, Default)] +pub struct ControlHeader { + pub class: u8, + pub cmd: u8, +} + +// SAFETY: ControlHeader only contains a series of integers +unsafe impl ByteValued for ControlHeader {} + +pub struct CtrlQueue { + pub taps: Vec, +} + +impl CtrlQueue { + pub fn new(taps: Vec) -> Self { + CtrlQueue { taps } + } + + pub fn process( + &mut self, + mem: &GuestMemoryMmap, + queue: &mut Queue, + access_platform: Option<&Arc>, + ) -> Result<()> { + while let Some(mut desc_chain) = queue.pop_descriptor_chain(mem) { + let ctrl_desc = desc_chain.next().ok_or(Error::NoControlHeaderDescriptor)?; + + let ctrl_hdr: ControlHeader = desc_chain + .memory() + .read_obj( + ctrl_desc + .addr() + .translate_gva(access_platform, ctrl_desc.len() as usize), + ) + .map_err(Error::GuestMemory)?; + let data_desc = desc_chain.next().ok_or(Error::NoDataDescriptor)?; + + let data_desc_addr = data_desc + .addr() + .translate_gva(access_platform, data_desc.len() as usize); + + let status_desc = desc_chain.next().ok_or(Error::NoStatusDescriptor)?; + + let ok = match u32::from(ctrl_hdr.class) { + VIRTIO_NET_CTRL_MQ => { + let queue_pairs = desc_chain + .memory() + .read_obj::(data_desc_addr) + .map_err(Error::GuestMemory)?; + if u32::from(ctrl_hdr.cmd) != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET { + warn!("Unsupported command: {}", ctrl_hdr.cmd); + false + } else if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN as u16) + || (queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX as u16) + { + warn!("Number of MQ pairs out of range: {}", queue_pairs); + false + } else { + info!("Number of MQ pairs requested: {}", queue_pairs); + true + } + } + VIRTIO_NET_CTRL_GUEST_OFFLOADS => { + let features = desc_chain + .memory() + .read_obj::(data_desc_addr) + .map_err(Error::GuestMemory)?; + if u32::from(ctrl_hdr.cmd) != VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET { + warn!("Unsupported command: {}", ctrl_hdr.cmd); + false + } else { + let mut ok = true; + for tap in self.taps.iter_mut() { + info!("Reprogramming tap offload with features: {}", features); + tap.set_offload(virtio_features_to_tap_offload(features)) + .map_err(|e| { + error!("Error programming tap offload: {:?}", e); + ok = false + }) + .ok(); + } + ok + } + } + _ => { + warn!("Unsupported command {:?}", ctrl_hdr); + false + } + }; + + desc_chain + .memory() + .write_obj( + if ok { VIRTIO_NET_OK } else { VIRTIO_NET_ERR } as u8, + status_desc + .addr() + .translate_gva(access_platform, status_desc.len() as usize), + ) + .map_err(Error::GuestMemory)?; + let len = ctrl_desc.len() + data_desc.len() + status_desc.len(); + + queue + .add_used(desc_chain.memory(), desc_chain.head_index(), len) + .map_err(Error::QueueAddUsed)?; + + if !queue + .enable_notification(mem) + .map_err(Error::QueueEnableNotification)? + { + break; + } + } + + Ok(()) + } +} + +pub fn virtio_features_to_tap_offload(features: u64) -> c_uint { + let mut tap_offloads: c_uint = 0; + if features & (1 << VIRTIO_NET_F_GUEST_CSUM) != 0 { + tap_offloads |= net_gen::TUN_F_CSUM; + } + if features & (1 << VIRTIO_NET_F_GUEST_TSO4) != 0 { + tap_offloads |= net_gen::TUN_F_TSO4; + } + if features & (1 << VIRTIO_NET_F_GUEST_TSO6) != 0 { + tap_offloads |= net_gen::TUN_F_TSO6; + } + if features & (1 << VIRTIO_NET_F_GUEST_ECN) != 0 { + tap_offloads |= net_gen::TUN_F_TSO_ECN; + } + if features & (1 << VIRTIO_NET_F_GUEST_UFO) != 0 { + tap_offloads |= net_gen::TUN_F_UFO; + } + + tap_offloads +} diff --git a/vendor/cube-hypervisor/net_util/src/lib.rs b/vendor/cube-hypervisor/net_util/src/lib.rs new file mode 100644 index 0000000..a68404a --- /dev/null +++ b/vendor/cube-hypervisor/net_util/src/lib.rs @@ -0,0 +1,204 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the THIRD-PARTY file. + +#[macro_use] +extern crate log; + +mod ctrl_queue; +mod mac; +mod open_tap; +mod queue_pair; +mod tap; + +use serde::{Deserialize, Serialize}; +use std::io::Error as IoError; +use std::os::raw::c_uint; +use std::os::unix::io::{FromRawFd, RawFd}; +use std::{io, mem, net}; +use thiserror::Error; +use virtio_bindings::bindings::virtio_net::{ + virtio_net_hdr_v1, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN, + VIRTIO_NET_F_GUEST_CSUM, VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_TSO4, + VIRTIO_NET_F_GUEST_TSO6, VIRTIO_NET_F_GUEST_UFO, VIRTIO_NET_F_MAC, VIRTIO_NET_F_MQ, +}; +use vm_memory::{bitmap::AtomicBitmap, ByteValued}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +pub use ctrl_queue::{CtrlQueue, Error as CtrlQueueError}; +pub use mac::{MacAddr, MAC_ADDR_LEN}; +pub use open_tap::{open_tap, Error as OpenTapError}; +pub use queue_pair::{NetCounters, NetQueuePair, NetQueuePairError, RxVirtio, TxVirtio}; +pub use tap::{Error as TapError, Tap}; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Failed to create a socket: {0}")] + CreateSocket(IoError), +} + +pub type Result = std::result::Result; + +#[repr(C, packed)] +#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] +pub struct VirtioNetConfig { + pub mac: [u8; 6], + pub status: u16, + pub max_virtqueue_pairs: u16, + pub mtu: u16, + pub speed: u32, + pub duplex: u8, +} + +// SAFETY: it only has data and has no implicit padding. +unsafe impl ByteValued for VirtioNetConfig {} + +/// Create a sockaddr_in from an IPv4 address, and expose it as +/// an opaque sockaddr suitable for usage by socket ioctls. +fn create_sockaddr(ip_addr: net::Ipv4Addr) -> net_gen::sockaddr { + // IPv4 addresses big-endian (network order), but Ipv4Addr will give us + // a view of those bytes directly so we can avoid any endian trickiness. + let addr_in = net_gen::sockaddr_in { + sin_family: net_gen::AF_INET as u16, + sin_port: 0, + sin_addr: unsafe { mem::transmute(ip_addr.octets()) }, + __pad: [0; 8usize], + }; + + unsafe { mem::transmute(addr_in) } +} + +fn create_inet_socket() -> Result { + // This is safe since we check the return value. + let sock = unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM, 0) }; + if sock < 0 { + return Err(Error::CreateSocket(IoError::last_os_error())); + } + + // This is safe; nothing else will use or hold onto the raw sock fd. + Ok(unsafe { net::UdpSocket::from_raw_fd(sock) }) +} + +fn create_unix_socket() -> Result { + // This is safe since we check the return value. + let sock = unsafe { libc::socket(libc::AF_UNIX, libc::SOCK_DGRAM, 0) }; + if sock < 0 { + return Err(Error::CreateSocket(IoError::last_os_error())); + } + + // This is safe; nothing else will use or hold onto the raw sock fd. + Ok(unsafe { net::UdpSocket::from_raw_fd(sock) }) +} + +fn vnet_hdr_len() -> usize { + std::mem::size_of::() +} + +pub fn register_listener( + epoll_fd: RawFd, + fd: RawFd, + ev_type: epoll::Events, + data: u64, +) -> std::result::Result<(), io::Error> { + epoll::ctl( + epoll_fd, + epoll::ControlOptions::EPOLL_CTL_ADD, + fd, + epoll::Event::new(ev_type, data), + ) +} + +pub fn unregister_listener( + epoll_fd: RawFd, + fd: RawFd, + ev_type: epoll::Events, + data: u64, +) -> std::result::Result<(), io::Error> { + epoll::ctl( + epoll_fd, + epoll::ControlOptions::EPOLL_CTL_DEL, + fd, + epoll::Event::new(ev_type, data), + ) +} + +pub fn build_net_config_space( + config: &mut VirtioNetConfig, + mac: MacAddr, + num_queues: usize, + mtu: Option, + avail_features: &mut u64, +) { + config.mac.copy_from_slice(mac.get_bytes()); + *avail_features |= 1 << VIRTIO_NET_F_MAC; + + build_net_config_space_with_mq(config, num_queues, mtu, avail_features); +} + +pub fn build_net_config_space_with_mq( + config: &mut VirtioNetConfig, + num_queues: usize, + mtu: Option, + avail_features: &mut u64, +) { + let num_queue_pairs = (num_queues / 2) as u16; + if (num_queue_pairs >= VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN as u16) + && (num_queue_pairs <= VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX as u16) + { + config.max_virtqueue_pairs = num_queue_pairs; + *avail_features |= 1 << VIRTIO_NET_F_MQ; + } + if let Some(mtu) = mtu { + config.mtu = mtu; + } +} + +pub fn virtio_features_to_tap_offload(features: u64) -> c_uint { + let mut tap_offloads: c_uint = 0; + if features & (1 << VIRTIO_NET_F_GUEST_CSUM) != 0 { + tap_offloads |= net_gen::TUN_F_CSUM; + } + if features & (1 << VIRTIO_NET_F_GUEST_TSO4) != 0 { + tap_offloads |= net_gen::TUN_F_TSO4; + } + if features & (1 << VIRTIO_NET_F_GUEST_TSO6) != 0 { + tap_offloads |= net_gen::TUN_F_TSO6; + } + if features & (1 << VIRTIO_NET_F_GUEST_ECN) != 0 { + tap_offloads |= net_gen::TUN_F_TSO_ECN; + } + if features & (1 << VIRTIO_NET_F_GUEST_UFO) != 0 { + tap_offloads |= net_gen::TUN_F_UFO; + } + + tap_offloads +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_create_sockaddr() { + let addr: net::Ipv4Addr = "10.0.0.1".parse().unwrap(); + let sockaddr = create_sockaddr(addr); + + assert_eq!(sockaddr.sa_family, net_gen::AF_INET as u16); + + let data = &sockaddr.sa_data[..]; + + // The first two bytes should represent the port, which is 0. + assert_eq!(data[0], 0); + assert_eq!(data[1], 0); + + // The next four bytes should represent the actual IPv4 address, in network order. + assert_eq!(data[2], 10); + assert_eq!(data[3], 0); + assert_eq!(data[4], 0); + assert_eq!(data[5], 1); + } +} diff --git a/vendor/cube-hypervisor/net_util/src/mac.rs b/vendor/cube-hypervisor/net_util/src/mac.rs new file mode 100644 index 0000000..b0dcfe4 --- /dev/null +++ b/vendor/cube-hypervisor/net_util/src/mac.rs @@ -0,0 +1,195 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the THIRD-PARTY file. + +use std::fmt; +use std::io; +use std::result::Result; +use std::str::FromStr; + +use serde::de::{Deserialize, Deserializer, Error}; +use serde::ser::{Serialize, Serializer}; + +pub const MAC_ADDR_LEN: usize = 6; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct MacAddr { + bytes: [u8; MAC_ADDR_LEN], +} + +impl MacAddr { + pub fn parse_str(s: &S) -> Result + where + S: AsRef + ?Sized, + { + let v: Vec<&str> = s.as_ref().split(':').collect(); + let mut bytes = [0u8; MAC_ADDR_LEN]; + let common_err = Err(io::Error::new( + io::ErrorKind::Other, + format!("parsing of {} into a MAC address failed", s.as_ref()), + )); + + if v.len() != MAC_ADDR_LEN { + return common_err; + } + + for i in 0..MAC_ADDR_LEN { + if v[i].len() != 2 { + return common_err; + } + bytes[i] = u8::from_str_radix(v[i], 16).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("parsing of {} into a MAC address failed: {}", s.as_ref(), e), + ) + })?; + } + + Ok(MacAddr { bytes }) + } + + // Does not check whether src.len() == MAC_ADDR_LEN. + #[inline] + pub fn from_bytes_unchecked(src: &[u8]) -> MacAddr { + // TODO: using something like std::mem::uninitialized could avoid the extra initialization, + // if this ever becomes a performance bottleneck. + let mut bytes = [0u8; MAC_ADDR_LEN]; + bytes[..].copy_from_slice(src); + + MacAddr { bytes } + } + + // An error can only occur if the slice length is different from MAC_ADDR_LEN. + #[inline] + pub fn from_bytes(src: &[u8]) -> Result { + if src.len() != MAC_ADDR_LEN { + return Err(io::Error::new( + io::ErrorKind::Other, + format!("invalid length of slice: {} vs {}", src.len(), MAC_ADDR_LEN), + )); + } + Ok(MacAddr::from_bytes_unchecked(src)) + } + + #[inline] + pub fn get_bytes(&self) -> &[u8] { + &self.bytes + } + + pub fn local_random() -> MacAddr { + // Generate a fully random MAC + let mut random_bytes = [0u8; MAC_ADDR_LEN]; + if let Err(e) = getrandom::getrandom(&mut random_bytes) { + error!( + "Error populating MAC address with random data: {}", + e.to_string() + ); + } + + // Set the first byte to make the OUI a locally administered OUI + random_bytes[0] = 0x2e; + + MacAddr { + bytes: random_bytes, + } + } +} + +impl fmt::Display for MacAddr { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let b = &self.bytes; + write!( + f, + "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", + b[0], b[1], b[2], b[3], b[4], b[5] + ) + } +} + +impl Serialize for MacAddr { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.to_string().serialize(serializer) + } +} + +impl<'de> Deserialize<'de> for MacAddr { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + MacAddr::parse_str(&s) + .map_err(|e| D::Error::custom(format!("The provided MAC address is invalid: {}", e))) + } +} + +pub enum MacAddrParseError { + InvalidValue(String), +} + +impl FromStr for MacAddr { + type Err = MacAddrParseError; + + fn from_str(s: &str) -> std::result::Result { + MacAddr::parse_str(s).map_err(|_| MacAddrParseError::InvalidValue(s.to_owned())) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_mac_addr() { + // too long + assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:aa:aa").is_err()); + + // invalid hex + assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:ax").is_err()); + + // single digit mac address component should be invalid + assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:b").is_err()); + + // components with more than two digits should also be invalid + assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:bbb").is_err()); + + let mac = MacAddr::parse_str("12:34:56:78:9a:BC").unwrap(); + + println!("parsed MAC address: {}", mac); + + let bytes = mac.get_bytes(); + assert_eq!(bytes, [0x12u8, 0x34, 0x56, 0x78, 0x9a, 0xbc]); + } + + #[test] + fn test_from_bytes() { + let src1 = [0x01, 0x02, 0x03, 0x04, 0x05]; + let src2 = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]; + let src3 = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]; + + assert!(MacAddr::from_bytes(&src1[..]).is_err()); + + let x = MacAddr::from_bytes(&src2[..]).unwrap(); + assert_eq!(x.to_string(), String::from("01:02:03:04:05:06")); + + assert!(MacAddr::from_bytes(&src3[..]).is_err()); + } + + #[test] + fn test_mac_addr_serialization_and_deserialization() { + let mac: MacAddr = + serde_json::from_str("\"12:34:56:78:9a:bc\"").expect("MacAddr deserialization failed."); + + let bytes = mac.get_bytes(); + assert_eq!(bytes, [0x12u8, 0x34, 0x56, 0x78, 0x9a, 0xbc]); + + let s = serde_json::to_string(&mac).expect("MacAddr serialization failed."); + assert_eq!(s, "\"12:34:56:78:9a:bc\""); + } +} diff --git a/vendor/cube-hypervisor/net_util/src/open_tap.rs b/vendor/cube-hypervisor/net_util/src/open_tap.rs new file mode 100644 index 0000000..4fdd6df --- /dev/null +++ b/vendor/cube-hypervisor/net_util/src/open_tap.rs @@ -0,0 +1,149 @@ +// Copyright (c) 2020 Intel Corporation. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use super::{vnet_hdr_len, MacAddr, Tap, TapError}; +use std::net::Ipv4Addr; +use std::path::Path; +use std::{fs, io}; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Failed to convert an hexadecimal string into an integer: {0}")] + ConvertHexStringToInt(std::num::ParseIntError), + #[error("Error related to the multiqueue support (no support TAP side)")] + MultiQueueNoTapSupport, + #[error("Error related to the multiqueue support (no support device side)")] + MultiQueueNoDeviceSupport, + #[error("Failed to read the TAP flags from sysfs: {0}")] + ReadSysfsTunFlags(io::Error), + #[error("Open tap device failed: {0}")] + TapOpen(TapError), + #[error("Setting tap IP failed: {0}")] + TapSetIp(TapError), + #[error("Setting tap netmask failed: {0}")] + TapSetNetmask(TapError), + #[error("Setting MAC address failed: {0}")] + TapSetMac(TapError), + #[error("Getting MAC address failed: {0}")] + TapGetMac(TapError), + #[error("Setting vnet header size failed: {0}")] + TapSetVnetHdrSize(TapError), + #[error("Setting MTU failed: {0}")] + TapSetMtu(TapError), + #[error("Enabling tap interface failed: {0}")] + TapEnable(TapError), +} + +type Result = std::result::Result; + +fn check_mq_support(if_name: &Option<&str>, queue_pairs: usize) -> Result<()> { + if let Some(tap_name) = if_name { + let mq = queue_pairs > 1; + let path = format!("/sys/class/net/{}/tun_flags", tap_name); + // interface does not exist, check is not required + if !Path::new(&path).exists() { + return Ok(()); + } + let tun_flags_str = fs::read_to_string(path).map_err(Error::ReadSysfsTunFlags)?; + let tun_flags = u32::from_str_radix(tun_flags_str.trim().trim_start_matches("0x"), 16) + .map_err(Error::ConvertHexStringToInt)?; + if (tun_flags & net_gen::IFF_MULTI_QUEUE != 0) && !mq { + return Err(Error::MultiQueueNoDeviceSupport); + } else if (tun_flags & net_gen::IFF_MULTI_QUEUE == 0) && mq { + return Err(Error::MultiQueueNoTapSupport); + } + } + Ok(()) +} + +/// Create a new virtio network device with the given IP address and +/// netmask. +pub fn open_tap( + if_name: Option<&str>, + ip_addr: Option, + netmask: Option, + host_mac: &mut Option, + mtu: Option, + num_rx_q: usize, + flags: Option, + sandbox_id: Option, +) -> Result> { + let mut taps: Vec = Vec::new(); + let mut ifname: String = String::new(); + let vnet_hdr_size = vnet_hdr_len() as i32; + // Check if the given interface exists before we create it. + let tap_existed = if_name.map_or(false, |n| { + Path::new(&format!("/sys/class/net/{n}")).exists() + }); + + if num_rx_q == 1 { + if let Some(name) = if_name { + if let Some(sandbox_id) = sandbox_id { + match Tap::lookup_from_pool(name, sandbox_id) { + Ok(tap) => { + taps.push(tap); + info!("Fast request tap success"); + return Ok(taps); + } + Err(e) => { + info!("Fast request tap failed {:?}, fall back to slow path", e) + } + } + } + } + } + + // In case the tap interface already exists, check if the number of + // queues is appropriate. The tap might not support multiqueue while + // the number of queues indicates the user expects multiple queues, or + // on the contrary, the tap might support multiqueue while the number + // of queues indicates the user doesn't expect multiple queues. + check_mq_support(&if_name, num_rx_q)?; + + for i in 0..num_rx_q { + let tap: Tap; + if i == 0 { + tap = match if_name { + Some(name) => Tap::open_named(name, num_rx_q, flags).map_err(Error::TapOpen)?, + None => Tap::new(num_rx_q).map_err(Error::TapOpen)?, + }; + // Don't overwrite ip configuration of existing interfaces: + if !tap_existed { + if let Some(ip) = ip_addr { + tap.set_ip_addr(ip).map_err(Error::TapSetIp)?; + } + if let Some(mask) = netmask { + tap.set_netmask(mask).map_err(Error::TapSetNetmask)?; + } + } else { + warn!( + "Tap {} already exists. IP configuration will not be overwritten.", + if_name.unwrap_or_default() + ); + } + if let Some(mac) = host_mac { + tap.set_mac_addr(*mac).map_err(Error::TapSetMac)? + } else { + *host_mac = Some(tap.get_mac_addr().map_err(Error::TapGetMac)?) + } + if let Some(mtu) = mtu { + tap.set_mtu(mtu as i32).map_err(Error::TapSetMtu)?; + } + tap.enable().map_err(Error::TapEnable)?; + + tap.set_vnet_hdr_size(vnet_hdr_size) + .map_err(Error::TapSetVnetHdrSize)?; + + ifname = String::from_utf8(tap.get_if_name()).unwrap(); + } else { + tap = Tap::open_named(ifname.as_str(), num_rx_q, flags).map_err(Error::TapOpen)?; + + tap.set_vnet_hdr_size(vnet_hdr_size) + .map_err(Error::TapSetVnetHdrSize)?; + } + taps.push(tap); + } + Ok(taps) +} diff --git a/vendor/cube-hypervisor/net_util/src/queue_pair.rs b/vendor/cube-hypervisor/net_util/src/queue_pair.rs new file mode 100644 index 0000000..95dd999 --- /dev/null +++ b/vendor/cube-hypervisor/net_util/src/queue_pair.rs @@ -0,0 +1,566 @@ +// Copyright (c) 2020 Intel Corporation. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use super::{register_listener, unregister_listener, vnet_hdr_len, Tap}; +use rate_limiter::{BucketReduction, RateLimiter, TokenType}; +use std::io; +use std::num::Wrapping; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::Arc; +use thiserror::Error; +use virtio_queue::{Queue, QueueOwnedT, QueueT}; +use vm_memory::bitmap::Bitmap; +use vm_memory::{Bytes, GuestMemory}; +use vm_virtio::{AccessPlatform, Translatable}; + +/// Whether an EIO-drop log line should be emitted after the cumulative dropped +/// count moved from `before` to `after`. Log the first drop, then once per +/// 1000-frame boundary crossed, so a persistently down tap cannot flood the +/// log at line rate. The `tx_dropped_frames` counter is the source of truth +/// for monitoring; this log is best-effort. +fn should_log_dropped(before: u64, after: u64) -> bool { + (before == 0 && after > 0) || before / 1000 != after / 1000 +} + +#[derive(Clone)] +pub struct TxVirtio { + pub counter_bytes: Wrapping, + pub counter_frames: Wrapping, + pub limit_bytes: Wrapping, + pub limit_frames: Wrapping, + /// Frames the tap refused with EIO and we dropped instead of killing the + /// VM (see the EIO branch in `process_desc_chain`). + pub dropped_frames: Wrapping, +} + +impl Default for TxVirtio { + fn default() -> Self { + Self::new() + } +} + +impl TxVirtio { + pub fn new() -> Self { + TxVirtio { + counter_bytes: Wrapping(0), + counter_frames: Wrapping(0), + limit_bytes: Wrapping(0), + limit_frames: Wrapping(0), + dropped_frames: Wrapping(0), + } + } + + pub fn process_desc_chain( + &mut self, + mem: &vm_memory::GuestMemoryMmap, + tap: &mut Tap, + queue: &mut Queue, + rate_limiter: &mut Option, + access_platform: Option<&Arc>, + rate_limited: &mut std::sync::Once, + ) -> Result { + let mut retry_write = false; + let mut rate_limit_reached = BucketReduction::Success; + + while let Some(mut desc_chain) = queue.pop_descriptor_chain(mem) { + match rate_limit_reached { + BucketReduction::Failure => { + queue.go_to_previous_position(); + break; + } + BucketReduction::OverConsumption(_r) => { + break; + } + BucketReduction::Success => {} + } + + let mut next_desc = desc_chain.next(); + + let mut iovecs = Vec::new(); + while let Some(desc) = next_desc { + let desc_addr = desc + .addr() + .translate_gva(access_platform, desc.len() as usize); + if !desc.is_write_only() && desc.len() > 0 { + let buf = desc_chain + .memory() + .get_slice(desc_addr, desc.len() as usize) + .map_err(NetQueuePairError::GuestMemory)? + .ptr_guard_mut(); + let iovec = libc::iovec { + iov_base: buf.as_ptr() as *mut libc::c_void, + iov_len: desc.len() as libc::size_t, + }; + iovecs.push(iovec); + } else { + error!( + "Invalid descriptor chain: address = 0x{:x} length = {} write_only = {}", + desc_addr.0, + desc.len(), + desc.is_write_only() + ); + return Err(NetQueuePairError::DescriptorChainInvalid); + } + next_desc = desc_chain.next(); + } + + let len = if !iovecs.is_empty() { + let result = unsafe { + libc::writev( + tap.as_raw_fd() as libc::c_int, + iovecs.as_ptr(), + iovecs.len() as libc::c_int, + ) + }; + + if result < 0 { + let e = std::io::Error::last_os_error(); + + /* EAGAIN */ + if e.kind() == std::io::ErrorKind::WouldBlock { + queue.go_to_previous_position(); + retry_write = true; + break; + } + + if e.raw_os_error() != Some(libc::EIO) { + error!("net: tx: failed writing to tap: {}", e); + return Err(NetQueuePairError::WriteTap(e)); + } + + // EIO: the tap refused the frame (device down / carrier + // not up). Drop it like real hardware would and keep the + // queue alive; see the commit message for the rationale. + self.dropped_frames += Wrapping(1); + 0 + } else { + self.counter_bytes += Wrapping(result as u64 - vnet_hdr_len() as u64); + self.counter_frames += Wrapping(1); + result as u32 + } + } else { + 0 + }; + + // For the sake of simplicity (similar to the RX rate limiting), we always + // let the 'last' descriptor chain go-through even if it was over the rate + // limit, and simply stop processing oncoming `avail_desc` if any. + if let Some(rate_limiter) = rate_limiter { + rate_limit_reached = rate_limiter.consume(1, TokenType::Ops); + if rate_limit_reached != BucketReduction::Success { + self.limit_frames += Wrapping(1); + rate_limited.call_once(|| info!("net tx ops ratelimit fired")); + } else { + rate_limit_reached = rate_limiter.consume(len as u64, TokenType::Bytes); + if rate_limit_reached != BucketReduction::Success { + self.limit_bytes += Wrapping(1); + rate_limited.call_once(|| info!("net tx bw ratelimit fired")); + } + } + } + + queue + .add_used(desc_chain.memory(), desc_chain.head_index(), len) + .map_err(NetQueuePairError::QueueAddUsed)?; + + if !queue + .enable_notification(mem) + .map_err(NetQueuePairError::QueueEnableNotification)? + { + break; + } + } + + Ok(retry_write) + } +} + +#[derive(Clone)] +pub struct RxVirtio { + pub counter_bytes: Wrapping, + pub counter_frames: Wrapping, + pub limit_bytes: Wrapping, + pub limit_frames: Wrapping, +} + +impl Default for RxVirtio { + fn default() -> Self { + Self::new() + } +} + +impl RxVirtio { + pub fn new() -> Self { + RxVirtio { + counter_bytes: Wrapping(0), + counter_frames: Wrapping(0), + limit_bytes: Wrapping(0), + limit_frames: Wrapping(0), + } + } + + pub fn process_desc_chain( + &mut self, + mem: &vm_memory::GuestMemoryMmap, + tap: &mut Tap, + queue: &mut Queue, + rate_limiter: &mut Option, + access_platform: Option<&Arc>, + rate_limited: &mut std::sync::Once, + ) -> Result { + let mut exhausted_descs = true; + let mut rate_limit_reached = BucketReduction::Success; + + while let Some(mut desc_chain) = queue.pop_descriptor_chain(mem) { + match rate_limit_reached { + BucketReduction::Failure => { + exhausted_descs = false; + queue.go_to_previous_position(); + break; + } + BucketReduction::OverConsumption(_r) => { + break; + } + BucketReduction::Success => {} + } + + let desc = desc_chain + .next() + .ok_or(NetQueuePairError::DescriptorChainTooShort)?; + + let num_buffers_addr = desc_chain + .memory() + .checked_offset( + desc.addr() + .translate_gva(access_platform, desc.len() as usize), + 10, + ) + .unwrap(); + let mut next_desc = Some(desc); + + let mut iovecs = Vec::new(); + while let Some(desc) = next_desc { + let desc_addr = desc + .addr() + .translate_gva(access_platform, desc.len() as usize); + if desc.is_write_only() && desc.len() > 0 { + let buf = desc_chain + .memory() + .get_slice(desc_addr, desc.len() as usize) + .map_err(NetQueuePairError::GuestMemory)? + .ptr_guard_mut(); + let iovec = libc::iovec { + iov_base: buf.as_ptr() as *mut libc::c_void, + iov_len: desc.len() as libc::size_t, + }; + iovecs.push(iovec); + } else { + error!( + "Invalid descriptor chain: address = 0x{:x} length = {} write_only = {}", + desc_addr.0, + desc.len(), + desc.is_write_only() + ); + return Err(NetQueuePairError::DescriptorChainInvalid); + } + next_desc = desc_chain.next(); + } + + let len = if !iovecs.is_empty() { + let result = unsafe { + libc::readv( + tap.as_raw_fd() as libc::c_int, + iovecs.as_ptr(), + iovecs.len() as libc::c_int, + ) + }; + if result < 0 { + let e = std::io::Error::last_os_error(); + exhausted_descs = false; + queue.go_to_previous_position(); + + /* EAGAIN */ + if e.kind() == std::io::ErrorKind::WouldBlock { + break; + } + + error!("net: rx: failed reading from tap: {}", e); + return Err(NetQueuePairError::ReadTap(e)); + } + + // Write num_buffers to guest memory. We simply write 1 as we + // never spread the frame over more than one descriptor chain. + desc_chain + .memory() + .write_obj(1u16, num_buffers_addr) + .map_err(NetQueuePairError::GuestMemory)?; + + self.counter_bytes += Wrapping(result as u64 - vnet_hdr_len() as u64); + self.counter_frames += Wrapping(1); + + result as u32 + } else { + 0 + }; + + // For the sake of simplicity (keeping the handling of RX_QUEUE_EVENT and + // RX_TAP_EVENT totally asynchronous), we always let the 'last' descriptor + // chain go-through even if it was over the rate limit, and simply stop + // processing oncoming `avail_desc` if any. + if let Some(rate_limiter) = rate_limiter { + rate_limit_reached = rate_limiter.consume(1, TokenType::Ops); + if rate_limit_reached != BucketReduction::Success { + self.limit_frames += Wrapping(1); + rate_limited.call_once(|| info!("net rx ops ratelimit fired")); + } else { + rate_limit_reached = rate_limiter.consume(len as u64, TokenType::Bytes); + if rate_limit_reached != BucketReduction::Success { + self.limit_bytes += Wrapping(1); + rate_limited.call_once(|| info!("net rx bw ratelimit fired")); + } + } + } + + queue + .add_used(desc_chain.memory(), desc_chain.head_index(), len) + .map_err(NetQueuePairError::QueueAddUsed)?; + + if !queue + .enable_notification(mem) + .map_err(NetQueuePairError::QueueEnableNotification)? + { + break; + } + } + + Ok(exhausted_descs) + } +} + +#[derive(Default, Clone)] +pub struct NetCounters { + pub tx_bytes: Arc, + pub tx_frames: Arc, + pub tx_dropped_frames: Arc, + pub rx_bytes: Arc, + pub rx_frames: Arc, + pub rx_limit_bytes: Arc, + pub rx_limit_frames: Arc, + pub tx_limit_bytes: Arc, + pub tx_limit_frames: Arc, +} + +#[derive(Error, Debug)] +pub enum NetQueuePairError { + #[error("No memory configured")] + NoMemoryConfigured, + #[error("Error registering listener: {0}")] + RegisterListener(io::Error), + #[error("Error unregistering listener: {0}")] + UnregisterListener(io::Error), + #[error("Error writing to the TAP device: {0}")] + WriteTap(io::Error), + #[error("Error reading from the TAP device: {0}")] + ReadTap(io::Error), + #[error("Error related to guest memory: {0}")] + GuestMemory(vm_memory::GuestMemoryError), + #[error("Returned an error while iterating through the queue: {0}")] + QueueIteratorFailed(virtio_queue::Error), + #[error("Descriptor chain is too short")] + DescriptorChainTooShort, + #[error("Descriptor chain does not contain valid descriptors")] + DescriptorChainInvalid, + #[error("Failed to determine if queue needed notification: {0}")] + QueueNeedsNotification(virtio_queue::Error), + #[error("Failed to enable notification on the queue: {0}")] + QueueEnableNotification(virtio_queue::Error), + #[error("Failed to add used index to the queue: {0}")] + QueueAddUsed(virtio_queue::Error), +} + +pub struct NetQueuePair { + pub tap: Tap, + // With epoll each FD must be unique. So in order to filter the + // events we need to get a second FD responding to the original + // device so that we can send EPOLLOUT and EPOLLIN to separate + // events. + pub tap_for_write_epoll: Tap, + pub rx: RxVirtio, + pub tx: TxVirtio, + pub epoll_fd: Option, + pub rx_tap_listening: bool, + pub tx_tap_listening: bool, + pub counters: NetCounters, + pub tap_rx_event_id: u16, + pub tap_tx_event_id: u16, + pub rx_desc_avail: bool, + pub rx_rate_limiter: Option, + pub tx_rate_limiter: Option, + pub access_platform: Option>, + pub tx_rate_limited: std::sync::Once, + pub rx_rate_limited: std::sync::Once, + pub id: String, +} + +impl NetQueuePair { + pub fn process_tx( + &mut self, + mem: &vm_memory::GuestMemoryMmap, + queue: &mut Queue, + ) -> Result { + let tx_tap_retry = self.tx.process_desc_chain( + mem, + &mut self.tap, + queue, + &mut self.tx_rate_limiter, + self.access_platform.as_ref(), + &mut self.tx_rate_limited, + )?; + + // We got told to try again when writing to the tap. Wait for the TAP to be writable + if tx_tap_retry && !self.tx_tap_listening { + register_listener( + self.epoll_fd.unwrap(), + self.tap_for_write_epoll.as_raw_fd(), + epoll::Events::EPOLLOUT, + u64::from(self.tap_tx_event_id), + ) + .map_err(NetQueuePairError::RegisterListener)?; + self.tx_tap_listening = true; + info!("Writing to TAP returned EAGAIN. Listening for TAP to become writable."); + } else if !tx_tap_retry && self.tx_tap_listening { + unregister_listener( + self.epoll_fd.unwrap(), + self.tap_for_write_epoll.as_raw_fd(), + epoll::Events::EPOLLOUT, + u64::from(self.tap_tx_event_id), + ) + .map_err(NetQueuePairError::UnregisterListener)?; + self.tx_tap_listening = false; + info!("Writing to TAP succeeded. No longer listening for TAP to become writable."); + } + + self.counters + .tx_bytes + .fetch_add(self.tx.counter_bytes.0, Ordering::AcqRel); + self.counters + .tx_frames + .fetch_add(self.tx.counter_frames.0, Ordering::AcqRel); + self.counters + .tx_limit_bytes + .fetch_add(self.tx.limit_bytes.0, Ordering::AcqRel); + self.counters + .tx_limit_frames + .fetch_add(self.tx.limit_frames.0, Ordering::AcqRel); + if self.tx.dropped_frames.0 != 0 { + let before = self + .counters + .tx_dropped_frames + .fetch_add(self.tx.dropped_frames.0, Ordering::AcqRel); + let after = before + self.tx.dropped_frames.0; + if should_log_dropped(before, after) { + debug!( + "net: tx: tap refused frame(s) with EIO; dropping instead of failing the VM (total dropped: {})", + after + ); + } + } + self.tx.counter_bytes = Wrapping(0); + self.tx.counter_frames = Wrapping(0); + self.tx.limit_bytes = Wrapping(0); + self.tx.limit_frames = Wrapping(0); + self.tx.dropped_frames = Wrapping(0); + + queue + .needs_notification(mem) + .map_err(NetQueuePairError::QueueNeedsNotification) + } + + pub fn process_rx( + &mut self, + mem: &vm_memory::GuestMemoryMmap, + queue: &mut Queue, + ) -> Result { + self.rx_desc_avail = !self.rx.process_desc_chain( + mem, + &mut self.tap, + queue, + &mut self.rx_rate_limiter, + self.access_platform.as_ref(), + &mut self.rx_rate_limited, + )?; + let rate_limit_reached = self + .rx_rate_limiter + .as_ref() + .map_or(false, |r| r.is_blocked()); + + // Stop listening on the `RX_TAP_EVENT` when: + // 1) there is no available describles, or + // 2) the RX rate limit is reached. + if self.rx_tap_listening && (!self.rx_desc_avail || rate_limit_reached) { + unregister_listener( + self.epoll_fd.unwrap(), + self.tap.as_raw_fd(), + epoll::Events::EPOLLIN, + u64::from(self.tap_rx_event_id), + ) + .map_err(NetQueuePairError::UnregisterListener)?; + self.rx_tap_listening = false; + } + + self.counters + .rx_bytes + .fetch_add(self.rx.counter_bytes.0, Ordering::AcqRel); + self.counters + .rx_frames + .fetch_add(self.rx.counter_frames.0, Ordering::AcqRel); + self.counters + .rx_limit_bytes + .fetch_add(self.rx.limit_bytes.0, Ordering::AcqRel); + self.counters + .rx_limit_frames + .fetch_add(self.rx.limit_frames.0, Ordering::AcqRel); + self.rx.counter_bytes = Wrapping(0); + self.rx.counter_frames = Wrapping(0); + self.rx.limit_bytes = Wrapping(0); + self.rx.limit_frames = Wrapping(0); + + queue + .needs_notification(mem) + .map_err(NetQueuePairError::QueueNeedsNotification) + } +} + +#[cfg(test)] +mod tests { + use super::should_log_dropped; + + #[test] + fn logs_first_drop() { + assert!(should_log_dropped(0, 1)); + assert!(should_log_dropped(0, 5)); + } + + #[test] + fn suppresses_within_the_same_thousand_bucket() { + assert!(!should_log_dropped(1, 2)); + assert!(!should_log_dropped(500, 999)); + assert!(!should_log_dropped(1000, 1500)); + } + + #[test] + fn logs_on_each_thousand_boundary_crossed() { + assert!(should_log_dropped(999, 1000)); + assert!(should_log_dropped(1999, 2001)); + // A single batch spanning several boundaries still logs once. + assert!(should_log_dropped(1500, 4200)); + } + + #[test] + fn no_log_when_nothing_was_dropped() { + assert!(!should_log_dropped(0, 0)); + assert!(!should_log_dropped(42, 42)); + } +} diff --git a/vendor/cube-hypervisor/net_util/src/tap.rs b/vendor/cube-hypervisor/net_util/src/tap.rs new file mode 100644 index 0000000..2240299 --- /dev/null +++ b/vendor/cube-hypervisor/net_util/src/tap.rs @@ -0,0 +1,914 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the THIRD-PARTY file. + +use super::{ + create_inet_socket, create_sockaddr, create_unix_socket, vnet_hdr_len, Error as NetUtilError, + MacAddr, +}; +use crate::mac::MAC_ADDR_LEN; +use serde::{Deserialize, Serialize}; +use std::env; +use std::fs::File; +use std::io::{Error as IoError, Read, Result as IoResult, Write}; +use std::net; +use std::os::raw::*; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::os::unix::net::UnixStream; +use std::time::Duration; +use thiserror::Error; +use vmm_sys_util::ioctl::{ioctl_with_mut_ref, ioctl_with_ref, ioctl_with_val}; +use vmm_sys_util::sock_ctrl_msg::ScmSocket; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Couldn't open /dev/net/tun: {0}")] + OpenTun(IoError), + #[error("Unable to configure tap interface: {0}")] + ConfigureTap(IoError), + #[error("Unable to retrieve features: {0}")] + GetFeatures(IoError), + #[error("Missing multiqueue support in the kernel")] + MultiQueueKernelSupport, + #[error("ioctl failed: {0}")] + IoctlError(IoError), + #[error("Failed to create a socket: {0}")] + NetUtil(NetUtilError), + #[error("Invalid interface name")] + InvalidIfname, + #[error("Error parsing MAC data: {0}")] + MacParsing(IoError), + #[error("Failed to connect to tap pool: {0}")] + OpenTunConnect(IoError), + #[error("Failed to sendmsg to tap pool: {0}")] + OpenTunJson(serde_json::Error), + #[error("Failed to sendmsg to tap pool: {0}")] + OpenTunSendmsg(IoError), + #[error("Failed to recvmsg from tap pool: {0}")] + OpenTunRecvmsg(vmm_sys_util::errno::Error), + #[error("Couldn't got FD from tap pool")] + OpenTunNoFD, + #[error("Couldn't dup FD from tap pool")] + OpenTunDupErr(IoError), + #[error("Couldn't set FD flags ")] + OpenTunSetflErr(IoError), +} + +pub type Result = ::std::result::Result; + +/// Handle for a network tap interface. +/// +/// For now, this simply wraps the file descriptor for the tap device so methods +/// can run ioctls on the interface. The tap interface fd will be closed when +/// Tap goes out of scope, and the kernel will clean up the interface +/// automatically. +#[derive(Debug)] +pub struct Tap { + tap_file: File, + if_name: Vec, + donated: bool, +} + +impl PartialEq for Tap { + fn eq(&self, other: &Tap) -> bool { + self.if_name == other.if_name + } +} + +impl std::clone::Clone for Tap { + fn clone(&self) -> Self { + Tap { + tap_file: self.tap_file.try_clone().unwrap(), + if_name: self.if_name.clone(), + donated: self.donated, + } + } +} + +// Returns a byte vector representing the contents of a null terminated C string which +// contains if_name. +fn build_terminated_if_name(if_name: &str) -> Result> { + // Convert the string slice to bytes, and shadow the variable, + // since we no longer need the &str version. + let if_name = if_name.as_bytes(); + + // TODO: the 16usize limit of the if_name member from struct Tap is pretty arbitrary. + // We leave it as is for now, but this should be refactored at some point. + if if_name.len() > 15 { + return Err(Error::InvalidIfname); + } + + let mut terminated_if_name = vec![b'\0'; if_name.len() + 1]; + terminated_if_name[..if_name.len()].copy_from_slice(if_name); + + Ok(terminated_if_name) +} + +#[allow(non_snake_case)] +#[derive(Default, Deserialize, Serialize)] +struct TapRequest { + #[serde(default)] + name: String, + #[serde(default)] + sandboxId: String, +} + +fn request_file_from_pool( + sock: &mut T, + if_name: &str, + sandbox_id: String, +) -> Result { + let buf = &mut vec![0; 256]; + let request = TapRequest { + name: if_name.to_string(), + sandboxId: sandbox_id, + }; + let start = std::time::Instant::now(); + + // Send message + let body = serde_json::to_vec(&request).map_err(Error::OpenTunJson)?; + sock.write_all(&body).map_err(Error::OpenTunSendmsg)?; + let ts_send = std::time::Instant::now(); + + let mut fds = [0; 1]; + let mut iovecs = [libc::iovec { + iov_base: buf.as_mut_ptr() as *mut libc::c_void, + iov_len: buf.len(), + }]; + + let mut cnt = 0; + let fd_count = loop { + unsafe { + match sock.recv_with_fds(&mut iovecs, &mut fds) { + Ok((_, fd_count)) => break fd_count, + Err(ref err) if err.errno() == libc::EAGAIN => { + std::thread::sleep(Duration::from_millis(1)); + cnt += 1; + continue; + } + Err(e) => { + return Err(Error::OpenTunRecvmsg(e)); + } + } + }; + }; + if fd_count == 0 { + return Err(Error::OpenTunNoFD); + } + let ts_recv = std::time::Instant::now(); + + // Dup FD. + let fd = unsafe { libc::dup(fds[0]) }; + if fd < 0 { + return Err(Error::OpenTunDupErr(IoError::last_os_error())); + } + let ts_dup = std::time::Instant::now(); + + // SAFETY: FFI call to close the received fd from socket + unsafe { libc::close(fds[0]) }; + + // Set nonblock. + let ret = unsafe { + let mut flags = libc::fcntl(fd, libc::F_GETFL); + flags |= libc::O_NONBLOCK; + libc::fcntl(fd, libc::F_SETFL, flags) + }; + if ret < 0 { + unsafe { libc::close(fd) }; + return Err(Error::OpenTunSetflErr(IoError::last_os_error())); + } + let ts_fcntl = std::time::Instant::now(); + + info!( + "Fast request tap recv done, got {} fd after {} try, send {} recv {} dup {} fcntl {}", + fd_count, + cnt, + ts_send.duration_since(start).as_millis(), + ts_recv.duration_since(ts_send).as_millis(), + ts_dup.duration_since(ts_recv).as_millis(), + ts_fcntl.duration_since(ts_dup).as_millis() + ); + + Ok(unsafe { File::from_raw_fd(fd) }) +} + +fn request_from_pool(if_name: &str, sandbox_id: String) -> Result { + let path = match env::var("CUBE_TAP_SERV_PATH") { + Ok(var) => var, + Err(_) => "/data/cubelet/cubetap.sock".to_string(), + }; + + let mut sock = UnixStream::connect(path).map_err(Error::OpenTunConnect)?; + + sock.set_nonblocking(true).map_err(Error::OpenTunSetflErr)?; + sock.set_write_timeout(Some(Duration::from_millis(1))) + .map_err(Error::OpenTunSetflErr)?; + + request_file_from_pool(&mut sock, if_name, sandbox_id) +} + +impl Tap { + // Fast path to open TAP. Open and Initialize Tap device always + // needs some netlink related operation, which will invoke + // rtnl_lock contention. + // Here we introduce a new way to avoid such costs. Create Tap + // pool, open and initialize Tap devices before launch VM. When + // we launch new VM, try to got Tap device FD from the Tap pool, + // then we could directly use the FD. + pub fn lookup_from_pool(if_name: &str, sandbox_id: String) -> Result { + let file = request_from_pool(if_name, sandbox_id)?; + + return Ok(Tap { + tap_file: file, + if_name: if_name.into(), + donated: true, + }); + } + + pub fn open_named(if_name: &str, num_queue_pairs: usize, flags: Option) -> Result { + let terminated_if_name = build_terminated_if_name(if_name)?; + + let fd = unsafe { + // Open calls are safe because we give a constant null-terminated + // string and verify the result. + libc::open( + b"/dev/net/tun\0".as_ptr() as *const c_char, + flags.unwrap_or(libc::O_RDWR | libc::O_NONBLOCK | libc::O_CLOEXEC), + ) + }; + if fd < 0 { + return Err(Error::OpenTun(IoError::last_os_error())); + } + + // We just checked that the fd is valid. + let tuntap = unsafe { File::from_raw_fd(fd) }; + + // Let's validate some features before going any further. + // ioctl is safe since we call it with a valid tap fd and check the return + // value. + let mut features = 0; + let ret = unsafe { ioctl_with_mut_ref(&tuntap, net_gen::TUNGETFEATURES(), &mut features) }; + if ret < 0 { + return Err(Error::GetFeatures(IoError::last_os_error())); + } + + // Check if the user parameters match the kernel support for MQ + if (features & net_gen::IFF_MULTI_QUEUE == 0) && num_queue_pairs > 1 { + return Err(Error::MultiQueueKernelSupport); + } + + // This is pretty messy because of the unions used by ifreq. Since we + // don't call as_mut on the same union field more than once, this block + // is safe. + let mut ifreq: net_gen::ifreq = Default::default(); + unsafe { + let ifrn_name = ifreq.ifr_ifrn.ifrn_name.as_mut(); + let name_slice = &mut ifrn_name[..terminated_if_name.len()]; + name_slice.copy_from_slice(terminated_if_name.as_slice()); + ifreq.ifr_ifru.ifru_flags = + (net_gen::IFF_TAP | net_gen::IFF_NO_PI | net_gen::IFF_VNET_HDR) as c_short; + if num_queue_pairs > 1 { + ifreq.ifr_ifru.ifru_flags |= net_gen::IFF_MULTI_QUEUE as c_short; + } + } + + // ioctl is safe since we call it with a valid tap fd and check the return + // value. + let ret = unsafe { ioctl_with_mut_ref(&tuntap, net_gen::TUNSETIFF(), &mut ifreq) }; + if ret < 0 { + return Err(Error::ConfigureTap(IoError::last_os_error())); + } + + let mut if_name = unsafe { ifreq.ifr_ifrn.ifrn_name }.to_vec(); + if_name.truncate(terminated_if_name.len() - 1); + // Safe since only the name is accessed, and it's cloned out. + Ok(Tap { + tap_file: tuntap, + if_name, + donated: false, + }) + } + + /// Create a new tap interface. + pub fn new(num_queue_pairs: usize) -> Result { + Self::open_named("vmtap%d", num_queue_pairs, None) + } + + pub fn from_tap_fd(fd: RawFd, num_queue_pairs: usize) -> Result { + // Ensure that the file is opened non-blocking, this is particularly + // needed when opened via the shell for macvtap. + let ret = unsafe { + let mut flags = libc::fcntl(fd, libc::F_GETFL); + flags |= libc::O_NONBLOCK; + libc::fcntl(fd, libc::F_SETFL, flags) + }; + if ret < 0 { + return Err(Error::ConfigureTap(IoError::last_os_error())); + } + + let tap_file = unsafe { File::from_raw_fd(fd) }; + let mut ifreq: net_gen::ifreq = Default::default(); + + // Get current config including name + let ret = unsafe { ioctl_with_mut_ref(&tap_file, net_gen::TUNGETIFF(), &mut ifreq) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + // We only access one field of the ifru union, hence this is safe. + let if_name = unsafe { ifreq.ifr_ifrn.ifrn_name }.to_vec(); + + // Try and update flags. Depending on how the tap was created (macvtap + // or via open_named()) this might return -EEXIST so we just ignore that. + unsafe { + ifreq.ifr_ifru.ifru_flags = + (net_gen::IFF_TAP | net_gen::IFF_NO_PI | net_gen::IFF_VNET_HDR) as c_short; + if num_queue_pairs > 1 { + ifreq.ifr_ifru.ifru_flags |= net_gen::IFF_MULTI_QUEUE as c_short; + } + } + let ret = unsafe { ioctl_with_mut_ref(&tap_file, net_gen::TUNSETIFF(), &mut ifreq) }; + if ret < 0 && IoError::last_os_error().raw_os_error().unwrap() != libc::EEXIST { + return Err(Error::ConfigureTap(IoError::last_os_error())); + } + + let tap = Tap { + tap_file, + if_name, + donated: false, + }; + let vnet_hdr_size = vnet_hdr_len() as i32; + tap.set_vnet_hdr_size(vnet_hdr_size)?; + + Ok(tap) + } + + /// Set the host-side IP address for the tap interface. + pub fn set_ip_addr(&self, ip_addr: net::Ipv4Addr) -> Result<()> { + let sock = create_inet_socket().map_err(Error::NetUtil)?; + let addr = create_sockaddr(ip_addr); + + let mut ifreq = self.get_ifreq(); + + ifreq.ifr_ifru.ifru_addr = addr; + + // ioctl is safe. Called with a valid sock fd, and we check the return. + let ret = + unsafe { ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFADDR as c_ulong, &ifreq) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + + Ok(()) + } + + /// Set mac addr for tap interface. + pub fn set_mac_addr(&self, addr: MacAddr) -> Result<()> { + // Checking if the mac address already matches the desired one + // is useful to avoid making the "set ioctl" in the case where + // the VMM is running without the privilege to do that. + // In practice this comes from a reboot after the configuration + // has been update with the kernel generated address. + if self.get_mac_addr()? == addr { + return Ok(()); + } + + let sock = create_unix_socket().map_err(Error::NetUtil)?; + + let mut ifreq = self.get_ifreq(); + + // ioctl is safe. Called with a valid sock fd, and we check the return. + let ret = + unsafe { ioctl_with_ref(&sock, net_gen::sockios::SIOCGIFHWADDR as c_ulong, &ifreq) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + // We only access one field of the ifru union, hence this is safe. + unsafe { + let ifru_hwaddr = &mut ifreq.ifr_ifru.ifru_hwaddr; + for (i, v) in addr.get_bytes().iter().enumerate() { + ifru_hwaddr.sa_data[i] = *v as c_uchar; + } + } + + // ioctl is safe. Called with a valid sock fd, and we check the return. + let ret = + unsafe { ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFHWADDR as c_ulong, &ifreq) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + + Ok(()) + } + + /// Get mac addr for tap interface. + pub fn get_mac_addr(&self) -> Result { + let sock = create_unix_socket().map_err(Error::NetUtil)?; + + let ifreq = self.get_ifreq(); + + // ioctl is safe. Called with a valid sock fd, and we check the return. + let ret = + unsafe { ioctl_with_ref(&sock, net_gen::sockios::SIOCGIFHWADDR as c_ulong, &ifreq) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + + // We only access one field of the ifru union, hence this is safe. + let addr = unsafe { + MacAddr::from_bytes(&ifreq.ifr_ifru.ifru_hwaddr.sa_data[0..MAC_ADDR_LEN]) + .map_err(Error::MacParsing)? + }; + Ok(addr) + } + + /// Set the netmask for the subnet that the tap interface will exist on. + pub fn set_netmask(&self, netmask: net::Ipv4Addr) -> Result<()> { + let sock = create_inet_socket().map_err(Error::NetUtil)?; + let addr = create_sockaddr(netmask); + + let mut ifreq = self.get_ifreq(); + + ifreq.ifr_ifru.ifru_addr = addr; + + // ioctl is safe. Called with a valid sock fd, and we check the return. + let ret = + unsafe { ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFNETMASK as c_ulong, &ifreq) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + + Ok(()) + } + + pub fn mtu(&self) -> Result { + let sock = create_unix_socket().map_err(Error::NetUtil)?; + + let ifreq = self.get_ifreq(); + + // ioctl is safe. Called with a valid sock fd, and we check the return. + let ret = unsafe { ioctl_with_ref(&sock, net_gen::sockios::SIOCGIFMTU as c_ulong, &ifreq) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + + let mtu = unsafe { ifreq.ifr_ifru.ifru_mtu }; + + Ok(mtu) + } + + pub fn set_mtu(&self, mtu: i32) -> Result<()> { + let sock = create_unix_socket().map_err(Error::NetUtil)?; + + let mut ifreq = self.get_ifreq(); + ifreq.ifr_ifru.ifru_mtu = mtu; + + // ioctl is safe. Called with a valid sock fd, and we check the return. + let ret = unsafe { ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFMTU as c_ulong, &ifreq) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + + Ok(()) + } + + /// Set the offload flags for the tap interface. + pub fn set_offload(&self, flags: c_uint) -> Result<()> { + // ioctl is safe. Called with a valid tap fd, and we check the return. + let ret = + unsafe { ioctl_with_val(&self.tap_file, net_gen::TUNSETOFFLOAD(), flags as c_ulong) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + + Ok(()) + } + + /// Enable the tap interface. + pub fn enable(&self) -> Result<()> { + let sock = create_unix_socket().map_err(Error::NetUtil)?; + + let mut ifreq = self.get_ifreq(); + + let ret = + unsafe { ioctl_with_ref(&sock, net_gen::sockios::SIOCGIFFLAGS as c_ulong, &ifreq) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + + // If TAP device is already up don't try and enable it + let ifru_flags = unsafe { ifreq.ifr_ifru.ifru_flags }; + if ifru_flags & net_gen::net_device_flags_IFF_UP as i16 + == net_gen::net_device_flags_IFF_UP as i16 + { + return Ok(()); + } + + ifreq.ifr_ifru.ifru_flags = net_gen::net_device_flags_IFF_UP as i16; + + // ioctl is safe. Called with a valid sock fd, and we check the return. + let ret = + unsafe { ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFFLAGS as c_ulong, &ifreq) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + + Ok(()) + } + + /// Set the size of the vnet hdr. + pub fn set_vnet_hdr_size(&self, size: c_int) -> Result<()> { + // ioctl is safe. Called with a valid tap fd, and we check the return. + let ret = unsafe { ioctl_with_ref(&self.tap_file, net_gen::TUNSETVNETHDRSZ(), &size) }; + if ret < 0 { + return Err(Error::IoctlError(IoError::last_os_error())); + } + + Ok(()) + } + + fn get_ifreq(&self) -> net_gen::ifreq { + let mut ifreq: net_gen::ifreq = Default::default(); + + // This sets the name of the interface, which is the only entry + // in a single-field union. + unsafe { + let ifrn_name = ifreq.ifr_ifrn.ifrn_name.as_mut(); + let name_slice = &mut ifrn_name[..self.if_name.len()]; + name_slice.copy_from_slice(&self.if_name); + } + + ifreq + } + + pub fn get_if_name(&self) -> Vec { + self.if_name.clone() + } + + pub fn is_donated(&self) -> bool { + return self.donated; + } +} + +impl Read for Tap { + fn read(&mut self, buf: &mut [u8]) -> IoResult { + self.tap_file.read(buf) + } +} + +impl Write for Tap { + fn write(&mut self, buf: &[u8]) -> IoResult { + self.tap_file.write(buf) + } + + fn flush(&mut self) -> IoResult<()> { + Ok(()) + } +} + +impl AsRawFd for Tap { + fn as_raw_fd(&self) -> RawFd { + self.tap_file.as_raw_fd() + } +} + +#[cfg(test)] +mod tests { + use std::net::Ipv4Addr; + use std::str; + use std::sync::{mpsc, Mutex}; + use std::thread; + use std::time::Duration; + + use once_cell::sync::Lazy; + + use pnet::packet::ethernet::{EtherTypes, EthernetPacket, MutableEthernetPacket}; + use pnet::packet::ip::IpNextHeaderProtocols; + use pnet::packet::ipv4::{Ipv4Packet, MutableIpv4Packet}; + use pnet::packet::udp::{MutableUdpPacket, UdpPacket}; + use pnet::packet::{MutablePacket, Packet}; + use pnet::util::MacAddr; + use pnet_datalink::Channel::Ethernet; + use pnet_datalink::{DataLinkReceiver, DataLinkSender, NetworkInterface}; + + use super::*; + + static DATA_STRING: &str = "test for tap"; + static SUBNET_MASK: &str = "255.255.255.0"; + + // We needed to have a mutex as a global variable, so we used once_cell for testing. The main + // potential problem, caused by tests being run in parallel by cargo, is creating different + // TAPs and trying to associate the same address, so we hide the IP address &str behind this + // mutex, more as a convention to remember to lock it at the very beginning of each function + // susceptible to this issue. Another variant is to use a different IP address per function, + // but we must remember to pick an unique one each time. + static TAP_IP_LOCK: Lazy> = Lazy::new(|| Mutex::new("192.168.241.1")); + + // Describes the outcomes we are currently interested in when parsing a packet (we use + // an UDP packet for testing). + struct ParsedPkt<'a> { + eth: EthernetPacket<'a>, + ipv4: Option>, + udp: Option>, + } + + impl<'a> ParsedPkt<'a> { + fn new(buf: &'a [u8]) -> Self { + let eth = EthernetPacket::new(buf).unwrap(); + let mut ipv4 = None; + let mut udp = None; + + if eth.get_ethertype() == EtherTypes::Ipv4 { + let ipv4_start = 14; + ipv4 = Some(Ipv4Packet::new(&buf[ipv4_start..]).unwrap()); + + // Hiding the old ipv4 variable for the rest of this block. + let ipv4 = Ipv4Packet::new(eth.payload()).unwrap(); + + if ipv4.get_next_level_protocol() == IpNextHeaderProtocols::Udp { + // The value in header_length indicates the number of 32 bit words + // that make up the header, not the actual length in bytes. + let udp_start = ipv4_start + ipv4.get_header_length() as usize * 4; + udp = Some(UdpPacket::new(&buf[udp_start..]).unwrap()); + } + } + + ParsedPkt { eth, ipv4, udp } + } + + fn print(&self) { + print!( + "{} {} {} ", + self.eth.get_source(), + self.eth.get_destination(), + self.eth.get_ethertype() + ); + if let Some(ref ipv4) = self.ipv4 { + print!( + "{} {} {} ", + ipv4.get_source(), + ipv4.get_destination(), + ipv4.get_next_level_protocol() + ); + } + if let Some(ref udp) = self.udp { + print!( + "{} {} {}", + udp.get_source(), + udp.get_destination(), + str::from_utf8(udp.payload()).unwrap() + ); + } + println!(); + } + } + + fn tap_name_to_string(tap: &Tap) -> String { + let null_pos = tap.if_name.iter().position(|x| *x == 0).unwrap(); + str::from_utf8(&tap.if_name[..null_pos]) + .unwrap() + .to_string() + } + + // Given a buffer of appropriate size, this fills in the relevant fields based on the + // provided information. Payload refers to the UDP payload. + fn pnet_build_packet(buf: &mut [u8], dst_mac: MacAddr, payload: &[u8]) { + let mut eth = MutableEthernetPacket::new(buf).unwrap(); + eth.set_source(MacAddr::new(0x06, 0, 0, 0, 0, 0)); + eth.set_destination(dst_mac); + eth.set_ethertype(EtherTypes::Ipv4); + + let mut ipv4 = MutableIpv4Packet::new(eth.payload_mut()).unwrap(); + ipv4.set_version(4); + ipv4.set_header_length(5); + ipv4.set_total_length(20 + 8 + payload.len() as u16); + ipv4.set_ttl(200); + ipv4.set_next_level_protocol(IpNextHeaderProtocols::Udp); + ipv4.set_source(Ipv4Addr::new(192, 168, 241, 1)); + ipv4.set_destination(Ipv4Addr::new(192, 168, 241, 2)); + + let mut udp = MutableUdpPacket::new(ipv4.payload_mut()).unwrap(); + udp.set_source(1000); + udp.set_destination(1001); + udp.set_length(8 + payload.len() as u16); + udp.set_payload(payload); + } + + // Sends a test packet on the interface named "ifname". + fn pnet_send_packet(ifname: String) { + let payload = DATA_STRING.as_bytes(); + + // eth hdr + ip hdr + udp hdr + payload len + let buf_size = 14 + 20 + 8 + payload.len(); + + let (mac, mut tx, _) = pnet_get_mac_tx_rx(ifname); + + let res = tx.build_and_send(1, buf_size, &mut |buf| { + pnet_build_packet(buf, mac, payload); + }); + // Make sure build_and_send() -> Option> succeeds. + res.unwrap().unwrap(); + } + + // For a given interface name, this returns a tuple that contains the MAC address of the + // interface, an object that can be used to send Ethernet frames, and a receiver of + // Ethernet frames arriving at the specified interface. + fn pnet_get_mac_tx_rx( + ifname: String, + ) -> (MacAddr, Box, Box) { + let interface_name_matches = |iface: &NetworkInterface| iface.name == ifname; + + // Find the network interface with the provided name. + let interfaces = pnet_datalink::interfaces(); + let interface = interfaces.into_iter().find(interface_name_matches).unwrap(); + + if let Ok(Ethernet(tx, rx)) = pnet_datalink::channel(&interface, Default::default()) { + (interface.mac.unwrap(), tx, rx) + } else { + panic!("datalink channel error or unhandled channel type"); + } + } + + #[test] + fn test_tap_create() { + let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); + + let t = Tap::new(1).unwrap(); + println!("created tap: {:?}", t); + } + + #[test] + fn test_tap_from_fd() { + let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); + + let orig_tap = Tap::new(1).unwrap(); + let fd = orig_tap.as_raw_fd(); + let _new_tap = Tap::from_tap_fd(fd, 1).unwrap(); + } + + #[test] + fn test_tap_configure() { + // This should be the first thing to be called inside the function, so everything else + // is torn down by the time the mutex is automatically released. Also, we should + // explicitly bind the MutexGuard to a variable via let, the make sure it lives until + // the end of the function. + let tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); + + let tap = Tap::new(1).unwrap(); + let ip_addr: net::Ipv4Addr = (*tap_ip_guard).parse().unwrap(); + let netmask: net::Ipv4Addr = SUBNET_MASK.parse().unwrap(); + + let ret = tap.set_ip_addr(ip_addr); + assert!(ret.is_ok()); + let ret = tap.set_netmask(netmask); + assert!(ret.is_ok()); + } + + #[test] + fn test_set_options() { + let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); + + // This line will fail to provide an initialized FD if the test is not run as root. + let tap = Tap::new(1).unwrap(); + tap.set_vnet_hdr_size(16).unwrap(); + tap.set_offload(0).unwrap(); + } + + #[test] + fn test_tap_enable() { + let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); + + let tap = Tap::new(1).unwrap(); + let ret = tap.enable(); + assert!(ret.is_ok()); + } + + #[test] + fn test_raw_fd() { + let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); + + let tap = Tap::new(1).unwrap(); + assert_eq!(tap.as_raw_fd(), tap.tap_file.as_raw_fd()); + } + + #[test] + fn test_read() { + let tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); + + let mut tap = Tap::new(1).unwrap(); + tap.set_ip_addr((*tap_ip_guard).parse().unwrap()).unwrap(); + tap.set_netmask(SUBNET_MASK.parse().unwrap()).unwrap(); + tap.enable().unwrap(); + + // Send a packet to the interface. We expect to be able to receive it on the associated fd. + pnet_send_packet(tap_name_to_string(&tap)); + + let mut buf = [0u8; 4096]; + + let mut found_packet_sz = None; + + // In theory, this could actually loop forever if something keeps sending data through the + // tap interface, but it's highly unlikely. + while found_packet_sz.is_none() { + let result = tap.read(&mut buf); + assert!(result.is_ok()); + + let size = result.unwrap(); + + // We skip the first 10 bytes because the IFF_VNET_HDR flag is set when the interface + // is created, and the legacy header is 10 bytes long without a certain flag which + // is not set in Tap::new(). + let eth_bytes = &buf[10..size]; + + let packet = EthernetPacket::new(eth_bytes).unwrap(); + if packet.get_ethertype() != EtherTypes::Ipv4 { + // not an IPv4 packet + continue; + } + + let ipv4_bytes = ð_bytes[14..]; + let packet = Ipv4Packet::new(ipv4_bytes).unwrap(); + + // Our packet should carry an UDP payload, and not contain IP options. + if packet.get_next_level_protocol() != IpNextHeaderProtocols::Udp + && packet.get_header_length() != 5 + { + continue; + } + + let udp_bytes = &ipv4_bytes[20..]; + + let udp_len = UdpPacket::new(udp_bytes).unwrap().get_length() as usize; + + // Skip the header bytes. + let inner_string = str::from_utf8(&udp_bytes[8..udp_len]).unwrap(); + + if inner_string.eq(DATA_STRING) { + found_packet_sz = Some(size); + break; + } + } + + assert!(found_packet_sz.is_some()); + } + + #[test] + fn test_write() { + let tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); + + let mut tap = Tap::new(1).unwrap(); + tap.set_ip_addr((*tap_ip_guard).parse().unwrap()).unwrap(); + tap.set_netmask(SUBNET_MASK.parse().unwrap()).unwrap(); + tap.enable().unwrap(); + + let (mac, _, mut rx) = pnet_get_mac_tx_rx(tap_name_to_string(&tap)); + + let payload = DATA_STRING.as_bytes(); + + // vnet hdr + eth hdr + ip hdr + udp hdr + payload len + let buf_size = 10 + 14 + 20 + 8 + payload.len(); + + let mut buf = vec![0u8; buf_size]; + // leave the vnet hdr as is + pnet_build_packet(&mut buf[10..], mac, payload); + + assert!(tap.write(&buf[..]).is_ok()); + assert!(tap.flush().is_ok()); + + let (channel_tx, channel_rx) = mpsc::channel(); + + // We use a separate thread to wait for the test packet because the API exposed by pnet is + // blocking. This thread will be killed when the main thread exits. + let _handle = thread::spawn(move || loop { + let buf = rx.next().unwrap(); + let p = ParsedPkt::new(buf); + p.print(); + + if let Some(ref udp) = p.udp { + if payload == udp.payload() { + channel_tx.send(true).unwrap(); + break; + } + } + }); + + // We wait for at most SLEEP_MILLIS * SLEEP_ITERS milliseconds for the reception of the + // test packet to be detected. + static SLEEP_MILLIS: u64 = 500; + static SLEEP_ITERS: u32 = 6; + + let mut found_test_packet = false; + + for _ in 0..SLEEP_ITERS { + thread::sleep(Duration::from_millis(SLEEP_MILLIS)); + if let Ok(true) = channel_rx.try_recv() { + found_test_packet = true; + break; + } + } + + assert!(found_test_packet); + } +} diff --git a/vendor/cube-hypervisor/option_parser/Cargo.toml b/vendor/cube-hypervisor/option_parser/Cargo.toml new file mode 100644 index 0000000..99a0964 --- /dev/null +++ b/vendor/cube-hypervisor/option_parser/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "option_parser" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" diff --git a/vendor/cube-hypervisor/option_parser/src/lib.rs b/vendor/cube-hypervisor/option_parser/src/lib.rs new file mode 100644 index 0000000..3da99bb --- /dev/null +++ b/vendor/cube-hypervisor/option_parser/src/lib.rs @@ -0,0 +1,409 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use std::collections::HashMap; +use std::fmt; +use std::num::ParseIntError; +use std::str::FromStr; + +#[derive(Default)] +pub struct OptionParser { + options: HashMap, +} + +struct OptionParserValue { + value: Option, + requires_value: bool, +} + +#[derive(Debug)] +pub enum OptionParserError { + UnknownOption(String), + InvalidSyntax(String), + Conversion(String, String), +} + +impl fmt::Display for OptionParserError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + OptionParserError::UnknownOption(s) => write!(f, "unknown option: {}", s), + OptionParserError::InvalidSyntax(s) => write!(f, "invalid syntax:{}", s), + OptionParserError::Conversion(field, value) => { + write!(f, "unable to convert {} for {}", value, field) + } + } + } +} +type OptionParserResult = std::result::Result; + +fn split_commas(s: &str) -> OptionParserResult> { + let mut list: Vec = Vec::new(); + let mut opened_brackets = 0; + let mut in_quotes = false; + let mut current = String::new(); + + for c in s.trim().chars() { + match c { + '[' => { + opened_brackets += 1; + current.push('['); + } + ']' => { + opened_brackets -= 1; + if opened_brackets < 0 { + return Err(OptionParserError::InvalidSyntax(s.to_owned())); + } + current.push(']'); + } + '"' => in_quotes = !in_quotes, + ',' => { + if opened_brackets > 0 || in_quotes { + current.push(',') + } else { + list.push(current); + current = String::new(); + } + } + c => current.push(c), + } + } + list.push(current); + + if opened_brackets != 0 || in_quotes { + return Err(OptionParserError::InvalidSyntax(s.to_owned())); + } + + Ok(list) +} + +impl OptionParser { + pub fn new() -> Self { + Self { + options: HashMap::new(), + } + } + + pub fn parse(&mut self, input: &str) -> OptionParserResult<()> { + if input.trim().is_empty() { + return Ok(()); + } + + for option in split_commas(input)?.iter() { + let parts: Vec<&str> = option.splitn(2, '=').collect(); + + match self.options.get_mut(parts[0]) { + None => return Err(OptionParserError::UnknownOption(parts[0].to_owned())), + Some(value) => { + if value.requires_value { + if parts.len() != 2 { + return Err(OptionParserError::InvalidSyntax((*option).to_owned())); + } + value.value = Some(parts[1].trim().to_owned()); + } else { + value.value = Some(String::new()); + } + } + } + } + + Ok(()) + } + + pub fn add(&mut self, option: &str) -> &mut Self { + self.options.insert( + option.to_owned(), + OptionParserValue { + value: None, + requires_value: true, + }, + ); + + self + } + + pub fn add_valueless(&mut self, option: &str) -> &mut Self { + self.options.insert( + option.to_owned(), + OptionParserValue { + value: None, + requires_value: false, + }, + ); + + self + } + + pub fn get(&self, option: &str) -> Option { + self.options + .get(option) + .and_then(|v| v.value.clone()) + .and_then(|s| if s.is_empty() { None } else { Some(s) }) + } + + pub fn is_set(&self, option: &str) -> bool { + self.options + .get(option) + .and_then(|v| v.value.as_ref()) + .is_some() + } + + pub fn convert(&self, option: &str) -> OptionParserResult> { + match self.get(option) { + None => Ok(None), + Some(v) => Ok(Some(v.parse().map_err(|_| { + OptionParserError::Conversion(option.to_owned(), v.to_owned()) + })?)), + } + } +} + +pub struct Toggle(pub bool); + +pub enum ToggleParseError { + InvalidValue(String), +} + +impl FromStr for Toggle { + type Err = ToggleParseError; + + fn from_str(s: &str) -> std::result::Result { + match s.to_lowercase().as_str() { + "" => Ok(Toggle(false)), + "on" => Ok(Toggle(true)), + "off" => Ok(Toggle(false)), + "true" => Ok(Toggle(true)), + "false" => Ok(Toggle(false)), + _ => Err(ToggleParseError::InvalidValue(s.to_owned())), + } + } +} + +pub struct ByteSized(pub u64); + +#[derive(Debug)] +pub enum ByteSizedParseError { + InvalidValue(String), +} + +impl FromStr for ByteSized { + type Err = ByteSizedParseError; + + fn from_str(s: &str) -> std::result::Result { + Ok(ByteSized({ + let s = s.trim(); + let shift = if s.ends_with('K') { + 10 + } else if s.ends_with('M') { + 20 + } else if s.ends_with('G') { + 30 + } else { + 0 + }; + + let s = s.trim_end_matches(|c| c == 'K' || c == 'M' || c == 'G'); + s.parse::() + .map_err(|_| ByteSizedParseError::InvalidValue(s.to_owned()))? + << shift + })) + } +} + +pub struct IntegerList(pub Vec); + +pub enum IntegerListParseError { + InvalidValue(String), +} + +impl FromStr for IntegerList { + type Err = IntegerListParseError; + + fn from_str(s: &str) -> std::result::Result { + let mut integer_list = Vec::new(); + let ranges_list: Vec<&str> = s + .trim() + .trim_matches(|c| c == '[' || c == ']') + .split(',') + .collect(); + + for range in ranges_list.iter() { + let items: Vec<&str> = range.split('-').collect(); + + if items.len() > 2 { + return Err(IntegerListParseError::InvalidValue((*range).to_string())); + } + + let start_range = items[0] + .parse::() + .map_err(|_| IntegerListParseError::InvalidValue(items[0].to_owned()))?; + + integer_list.push(start_range); + + if items.len() == 2 { + let end_range = items[1] + .parse::() + .map_err(|_| IntegerListParseError::InvalidValue(items[1].to_owned()))?; + if start_range >= end_range { + return Err(IntegerListParseError::InvalidValue((*range).to_string())); + } + + for i in start_range..end_range { + integer_list.push(i + 1); + } + } + } + + Ok(IntegerList(integer_list)) + } +} + +pub trait TupleValue { + fn parse_value(input: &str) -> Result + where + Self: Sized; +} + +impl TupleValue for u64 { + fn parse_value(input: &str) -> Result { + input.parse::().map_err(TupleError::InvalidInteger) + } +} + +impl TupleValue for Vec { + fn parse_value(input: &str) -> Result { + Ok(IntegerList::from_str(input) + .map_err(TupleError::InvalidIntegerList)? + .0 + .iter() + .map(|v| *v as u8) + .collect()) + } +} + +impl TupleValue for Vec { + fn parse_value(input: &str) -> Result { + Ok(IntegerList::from_str(input) + .map_err(TupleError::InvalidIntegerList)? + .0) + } +} + +pub struct Tuple(pub Vec<(S, T)>); + +pub enum TupleError { + InvalidValue(String), + SplitOutsideBrackets(OptionParserError), + InvalidIntegerList(IntegerListParseError), + InvalidInteger(ParseIntError), +} + +impl FromStr for Tuple { + type Err = TupleError; + + fn from_str(s: &str) -> std::result::Result { + let mut list: Vec<(S, T)> = Vec::new(); + + let body = s + .trim() + .strip_prefix('[') + .and_then(|s| s.strip_suffix(']')) + .ok_or_else(|| TupleError::InvalidValue(s.to_string()))?; + + let tuples_list = split_commas(body).map_err(TupleError::SplitOutsideBrackets)?; + for tuple in tuples_list.iter() { + let items: Vec<&str> = tuple.split('@').collect(); + + if items.len() != 2 { + return Err(TupleError::InvalidValue((*tuple).to_string())); + } + + let item1 = items[0] + .parse::() + .map_err(|_| TupleError::InvalidValue(items[0].to_owned()))?; + let item2 = TupleValue::parse_value(items[1])?; + + list.push((item1, item2)); + } + + Ok(Tuple(list)) + } +} + +#[derive(Default)] +pub struct StringList(pub Vec); + +pub enum StringListParseError { + InvalidValue(String), +} + +impl FromStr for StringList { + type Err = StringListParseError; + + fn from_str(s: &str) -> std::result::Result { + let string_list: Vec = s + .trim() + .trim_matches(|c| c == '[' || c == ']') + .split(',') + .map(|e| e.to_owned()) + .collect(); + + Ok(StringList(string_list)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_option_parser() { + let mut parser = OptionParser::new(); + parser + .add("size") + .add("mergeable") + .add("hotplug_method") + .add("hotplug_size") + .add("topology") + .add("cmdline"); + + assert!(parser.parse("size=128M,hanging_param").is_err()); + assert!(parser.parse("size=128M,too_many_equals=foo=bar").is_err()); + assert!(parser.parse("size=128M,file=/dev/shm").is_err()); + + assert!(parser.parse("size=128M").is_ok()); + assert_eq!(parser.get("size"), Some("128M".to_owned())); + assert!(!parser.is_set("mergeable")); + assert!(parser.is_set("size")); + + assert!(parser.parse("size=128M,mergeable=on").is_ok()); + assert_eq!(parser.get("size"), Some("128M".to_owned())); + assert_eq!(parser.get("mergeable"), Some("on".to_owned())); + + assert!(parser + .parse("size=128M,mergeable=on,topology=[1,2]") + .is_ok()); + assert_eq!(parser.get("size"), Some("128M".to_owned())); + assert_eq!(parser.get("mergeable"), Some("on".to_owned())); + assert_eq!(parser.get("topology"), Some("[1,2]".to_owned())); + + assert!(parser + .parse("size=128M,mergeable=on,topology=[[1,2],[3,4]]") + .is_ok()); + assert_eq!(parser.get("size"), Some("128M".to_owned())); + assert_eq!(parser.get("mergeable"), Some("on".to_owned())); + assert_eq!(parser.get("topology"), Some("[[1,2],[3,4]]".to_owned())); + + assert!(parser.parse("topology=[").is_err()); + assert!(parser.parse("topology=[[[]]]]").is_err()); + + assert!(parser.parse("cmdline=\"console=ttyS0,9600n8\"").is_ok()); + assert_eq!( + parser.get("cmdline"), + Some("console=ttyS0,9600n8".to_owned()) + ); + assert!(parser.parse("cmdline=\"").is_err()); + assert!(parser.parse("cmdline=\"\"\"").is_err()); + } +} diff --git a/vendor/cube-hypervisor/pci/Cargo.toml b/vendor/cube-hypervisor/pci/Cargo.toml new file mode 100644 index 0000000..21b989c --- /dev/null +++ b/vendor/cube-hypervisor/pci/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "pci" +version = "0.1.0" +authors = ["Samuel Ortiz "] +edition = "2021" + +[features] +default = [] +kvm = ["vfio-ioctls/kvm"] +mshv = ["vfio-ioctls/mshv"] + +[dependencies] +anyhow = "1.0.86" +byteorder = "1.4.3" +hypervisor = { path = "../hypervisor" } +vfio-ioctls = { git = "https://github.com/rust-vmm/vfio", rev = "64171f3", default-features = false } +vfio_user = { path = "../vfio_user" } +vmm-sys-util = "0.12.1" +libc = "0.2.137" +log = "0.4.17" +serde = { workspace = true, features = ["derive"] } +thiserror = "1.0.37" +vm-allocator = { path = "../vm-allocator" } +vm-device = { path = "../vm-device" } +vm-memory = { workspace = true, features = ["backend-bitmap"] } +vm-migration = { path = "../vm-migration" } + +[dependencies.vfio-bindings] +version = "0.3.1" +features = ["fam-wrappers"] diff --git a/vendor/cube-hypervisor/pci/src/bus.rs b/vendor/cube-hypervisor/pci/src/bus.rs new file mode 100644 index 0000000..ff033dd --- /dev/null +++ b/vendor/cube-hypervisor/pci/src/bus.rs @@ -0,0 +1,474 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use crate::configuration::{ + PciBarRegionType, PciBridgeSubclass, PciClassCode, PciConfiguration, PciHeaderType, +}; +use crate::device::{DeviceRelocation, Error as PciDeviceError, PciDevice}; +use crate::PciBarConfiguration; +use byteorder::{ByteOrder, LittleEndian}; +use std::any::Any; +use std::collections::HashMap; +use std::ops::DerefMut; +use std::sync::{Arc, Barrier, Mutex}; +use vm_device::{Bus, BusDevice}; + +const VENDOR_ID_INTEL: u16 = 0x8086; +const DEVICE_ID_INTEL_VIRT_PCIE_HOST: u16 = 0x0d57; +const NUM_DEVICE_IDS: usize = 32; + +/// Errors for device manager. +#[derive(Debug)] +pub enum PciRootError { + /// Could not allocate device address space for the device. + AllocateDeviceAddrs(PciDeviceError), + /// Could not allocate an IRQ number. + AllocateIrq, + /// Could not add a device to the port io bus. + PioInsert(vm_device::BusError), + /// Could not add a device to the mmio bus. + MmioInsert(vm_device::BusError), + /// Could not find an available device slot on the PCI bus. + NoPciDeviceSlotAvailable, + /// Invalid PCI device identifier provided. + InvalidPciDeviceSlot(usize), + /// Valid PCI device identifier but already used. + AlreadyInUsePciDeviceSlot(usize), +} +pub type Result = std::result::Result; + +/// Emulates the PCI Root bridge device. +pub struct PciRoot { + /// Configuration space. + config: PciConfiguration, +} + +impl PciRoot { + /// Create an empty PCI root bridge. + pub fn new(config: Option) -> Self { + if let Some(config) = config { + PciRoot { config } + } else { + PciRoot { + config: PciConfiguration::new( + VENDOR_ID_INTEL, + DEVICE_ID_INTEL_VIRT_PCIE_HOST, + 0, + PciClassCode::BridgeDevice, + &PciBridgeSubclass::HostBridge, + None, + PciHeaderType::Device, + 0, + 0, + None, + ), + } + } + } +} + +impl BusDevice for PciRoot {} + +impl PciDevice for PciRoot { + fn write_config_register( + &mut self, + reg_idx: usize, + offset: u64, + data: &[u8], + ) -> Option> { + self.config.write_config_register(reg_idx, offset, data); + None + } + + fn read_config_register(&mut self, reg_idx: usize) -> u32 { + self.config.read_reg(reg_idx) + } + + fn as_any(&mut self) -> &mut dyn Any { + self + } + + fn id(&self) -> Option { + None + } +} + +pub struct PciBus { + /// Devices attached to this bus. + /// Device 0 is host bridge. + devices: HashMap>>, + device_reloc: Arc, + device_ids: Vec, +} + +impl PciBus { + pub fn new(pci_root: PciRoot, device_reloc: Arc) -> Self { + let mut devices: HashMap>> = HashMap::new(); + let mut device_ids: Vec = vec![false; NUM_DEVICE_IDS]; + + devices.insert(0, Arc::new(Mutex::new(pci_root))); + device_ids[0] = true; + + PciBus { + devices, + device_reloc, + device_ids, + } + } + + pub fn register_mapping( + &self, + dev: Arc>, + #[cfg(target_arch = "x86_64")] io_bus: &Bus, + mmio_bus: &Bus, + bars: Vec, + ) -> Result<()> { + for bar in bars { + match bar.region_type() { + PciBarRegionType::IoRegion => { + #[cfg(target_arch = "x86_64")] + io_bus + .insert(dev.clone(), bar.addr(), bar.size()) + .map_err(PciRootError::PioInsert)?; + #[cfg(target_arch = "aarch64")] + error!("I/O region is not supported"); + } + PciBarRegionType::Memory32BitRegion | PciBarRegionType::Memory64BitRegion => { + mmio_bus + .insert(dev.clone(), bar.addr(), bar.size()) + .map_err(PciRootError::MmioInsert)?; + } + } + } + Ok(()) + } + + pub fn add_device(&mut self, device_id: u32, device: Arc>) -> Result<()> { + self.devices.insert(device_id, device); + Ok(()) + } + + pub fn remove_by_device(&mut self, device: &Arc>) -> Result<()> { + self.devices.retain(|_, dev| !Arc::ptr_eq(dev, device)); + Ok(()) + } + + pub fn next_device_id(&mut self) -> Result { + for (idx, device_id) in self.device_ids.iter_mut().enumerate() { + if !(*device_id) { + *device_id = true; + return Ok(idx as u32); + } + } + + Err(PciRootError::NoPciDeviceSlotAvailable) + } + + pub fn get_device_id(&mut self, id: usize) -> Result<()> { + if id < NUM_DEVICE_IDS { + if !self.device_ids[id] { + self.device_ids[id] = true; + Ok(()) + } else { + Err(PciRootError::AlreadyInUsePciDeviceSlot(id)) + } + } else { + Err(PciRootError::InvalidPciDeviceSlot(id)) + } + } + + pub fn put_device_id(&mut self, id: usize) -> Result<()> { + if id < NUM_DEVICE_IDS { + self.device_ids[id] = false; + Ok(()) + } else { + Err(PciRootError::InvalidPciDeviceSlot(id)) + } + } +} + +pub struct PciConfigIo { + /// Config space register. + config_address: u32, + pci_bus: Arc>, +} + +impl PciConfigIo { + pub fn new(pci_bus: Arc>) -> Self { + PciConfigIo { + config_address: 0, + pci_bus, + } + } + + pub fn config_space_read(&self) -> u32 { + let enabled = (self.config_address & 0x8000_0000) != 0; + if !enabled { + return 0xffff_ffff; + } + + let (bus, device, function, register) = + parse_io_config_address(self.config_address & !0x8000_0000); + + // Only support one bus. + if bus != 0 { + return 0xffff_ffff; + } + + // Don't support multi-function devices. + if function > 0 { + return 0xffff_ffff; + } + + self.pci_bus + .as_ref() + .lock() + .unwrap() + .devices + .get(&(device as u32)) + .map_or(0xffff_ffff, |d| { + d.lock().unwrap().read_config_register(register) + }) + } + + pub fn config_space_write(&mut self, offset: u64, data: &[u8]) -> Option> { + if offset as usize + data.len() > 4 { + return None; + } + + let enabled = (self.config_address & 0x8000_0000) != 0; + if !enabled { + return None; + } + + let (bus, device, _function, register) = + parse_io_config_address(self.config_address & !0x8000_0000); + + // Only support one bus. + if bus != 0 { + return None; + } + + let pci_bus = self.pci_bus.as_ref().lock().unwrap(); + if let Some(d) = pci_bus.devices.get(&(device as u32)) { + let mut device = d.lock().unwrap(); + + // Find out if one of the device's BAR is being reprogrammed, and + // reprogram it if needed. + if let Some(params) = device.detect_bar_reprogramming(register, data) { + if let Err(e) = pci_bus.device_reloc.move_bar( + params.old_base, + params.new_base, + params.len, + device.deref_mut(), + params.region_type, + ) { + error!( + "Failed moving device BAR: {}: 0x{:x}->0x{:x}(0x{:x})", + e, params.old_base, params.new_base, params.len + ); + } + } + + // Update the register value + device.write_config_register(register, offset, data) + } else { + None + } + } + + fn set_config_address(&mut self, offset: u64, data: &[u8]) { + if offset as usize + data.len() > 4 { + return; + } + let (mask, value): (u32, u32) = match data.len() { + 1 => ( + 0x0000_00ff << (offset * 8), + u32::from(data[0]) << (offset * 8), + ), + 2 => ( + 0x0000_ffff << (offset * 16), + (u32::from(data[1]) << 8 | u32::from(data[0])) << (offset * 16), + ), + 4 => (0xffff_ffff, LittleEndian::read_u32(data)), + _ => return, + }; + self.config_address = (self.config_address & !mask) | value; + } +} + +impl BusDevice for PciConfigIo { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + // `offset` is relative to 0xcf8 + let value = match offset { + 0..=3 => self.config_address, + 4..=7 => self.config_space_read(), + _ => 0xffff_ffff, + }; + + // Only allow reads to the register boundary. + let start = offset as usize % 4; + let end = start + data.len(); + if end <= 4 { + for i in start..end { + data[i - start] = (value >> (i * 8)) as u8; + } + } else { + for d in data { + *d = 0xff; + } + } + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + // `offset` is relative to 0xcf8 + match offset { + o @ 0..=3 => { + self.set_config_address(o, data); + None + } + o @ 4..=7 => self.config_space_write(o - 4, data), + _ => None, + } + } +} + +/// Emulates PCI memory-mapped configuration access mechanism. +pub struct PciConfigMmio { + pci_bus: Arc>, +} + +impl PciConfigMmio { + pub fn new(pci_bus: Arc>) -> Self { + PciConfigMmio { pci_bus } + } + + fn config_space_read(&self, config_address: u32) -> u32 { + let (bus, device, _function, register) = parse_mmio_config_address(config_address); + + // Only support one bus. + if bus != 0 { + return 0xffff_ffff; + } + + self.pci_bus + .lock() + .unwrap() + .devices + .get(&(device as u32)) + .map_or(0xffff_ffff, |d| { + d.lock().unwrap().read_config_register(register) + }) + } + + fn config_space_write(&mut self, config_address: u32, offset: u64, data: &[u8]) { + if offset as usize + data.len() > 4 { + return; + } + + let (bus, device, _function, register) = parse_mmio_config_address(config_address); + + // Only support one bus. + if bus != 0 { + return; + } + + let pci_bus = self.pci_bus.lock().unwrap(); + if let Some(d) = pci_bus.devices.get(&(device as u32)) { + let mut device = d.lock().unwrap(); + + // Find out if one of the device's BAR is being reprogrammed, and + // reprogram it if needed. + if let Some(params) = device.detect_bar_reprogramming(register, data) { + if let Err(e) = pci_bus.device_reloc.move_bar( + params.old_base, + params.new_base, + params.len, + device.deref_mut(), + params.region_type, + ) { + error!( + "Failed moving device BAR: {}: 0x{:x}->0x{:x}(0x{:x})", + e, params.old_base, params.new_base, params.len + ); + } + } + + // Update the register value + device.write_config_register(register, offset, data); + } + } +} + +impl BusDevice for PciConfigMmio { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + // Only allow reads to the register boundary. + let start = offset as usize % 4; + let end = start + data.len(); + if end > 4 || offset > u64::from(u32::max_value()) { + for d in data { + *d = 0xff; + } + return; + } + + let value = self.config_space_read(offset as u32); + for i in start..end { + data[i - start] = (value >> (i * 8)) as u8; + } + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + if offset > u64::from(u32::max_value()) { + return None; + } + self.config_space_write(offset as u32, offset % 4, data); + + None + } +} + +fn shift_and_mask(value: u32, offset: usize, mask: u32) -> usize { + ((value >> offset) & mask) as usize +} + +// Parse the MMIO address offset to a (bus, device, function, register) tuple. +// See section 7.2.2 PCI Express Enhanced Configuration Access Mechanism (ECAM) +// from the Pci Express Base Specification Revision 5.0 Version 1.0. +fn parse_mmio_config_address(config_address: u32) -> (usize, usize, usize, usize) { + const BUS_NUMBER_OFFSET: usize = 20; + const BUS_NUMBER_MASK: u32 = 0x00ff; + const DEVICE_NUMBER_OFFSET: usize = 15; + const DEVICE_NUMBER_MASK: u32 = 0x1f; + const FUNCTION_NUMBER_OFFSET: usize = 12; + const FUNCTION_NUMBER_MASK: u32 = 0x07; + const REGISTER_NUMBER_OFFSET: usize = 2; + const REGISTER_NUMBER_MASK: u32 = 0x3ff; + + ( + shift_and_mask(config_address, BUS_NUMBER_OFFSET, BUS_NUMBER_MASK), + shift_and_mask(config_address, DEVICE_NUMBER_OFFSET, DEVICE_NUMBER_MASK), + shift_and_mask(config_address, FUNCTION_NUMBER_OFFSET, FUNCTION_NUMBER_MASK), + shift_and_mask(config_address, REGISTER_NUMBER_OFFSET, REGISTER_NUMBER_MASK), + ) +} + +// Parse the CONFIG_ADDRESS register to a (bus, device, function, register) tuple. +fn parse_io_config_address(config_address: u32) -> (usize, usize, usize, usize) { + const BUS_NUMBER_OFFSET: usize = 16; + const BUS_NUMBER_MASK: u32 = 0x00ff; + const DEVICE_NUMBER_OFFSET: usize = 11; + const DEVICE_NUMBER_MASK: u32 = 0x1f; + const FUNCTION_NUMBER_OFFSET: usize = 8; + const FUNCTION_NUMBER_MASK: u32 = 0x07; + const REGISTER_NUMBER_OFFSET: usize = 2; + const REGISTER_NUMBER_MASK: u32 = 0x3f; + + ( + shift_and_mask(config_address, BUS_NUMBER_OFFSET, BUS_NUMBER_MASK), + shift_and_mask(config_address, DEVICE_NUMBER_OFFSET, DEVICE_NUMBER_MASK), + shift_and_mask(config_address, FUNCTION_NUMBER_OFFSET, FUNCTION_NUMBER_MASK), + shift_and_mask(config_address, REGISTER_NUMBER_OFFSET, REGISTER_NUMBER_MASK), + ) +} diff --git a/vendor/cube-hypervisor/pci/src/configuration.rs b/vendor/cube-hypervisor/pci/src/configuration.rs new file mode 100644 index 0000000..7b397e5 --- /dev/null +++ b/vendor/cube-hypervisor/pci/src/configuration.rs @@ -0,0 +1,1244 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use crate::device::BarReprogrammingParams; +use crate::{MsixConfig, PciInterruptPin}; +use byteorder::{ByteOrder, LittleEndian}; +use serde::{Deserialize, Serialize}; +use std::fmt::{self, Display}; +use std::sync::{Arc, Mutex}; +use vm_device::PciBarType; +use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable}; + +// The number of 32bit registers in the config space, 4096 bytes. +const NUM_CONFIGURATION_REGISTERS: usize = 1024; + +const STATUS_REG: usize = 1; +const STATUS_REG_CAPABILITIES_USED_MASK: u32 = 0x0010_0000; +const BAR0_REG: usize = 4; +const ROM_BAR_REG: usize = 12; +const ROM_BAR_IDX: usize = 6; +const BAR_IO_ADDR_MASK: u32 = 0xffff_fffc; +const BAR_MEM_ADDR_MASK: u32 = 0xffff_fff0; +const ROM_BAR_ADDR_MASK: u32 = 0xffff_f800; +const MSI_CAPABILITY_REGISTER_MASK: u32 = 0x0071_0000; +const MSIX_CAPABILITY_REGISTER_MASK: u32 = 0xc000_0000; +const NUM_BAR_REGS: usize = 6; +const CAPABILITY_LIST_HEAD_OFFSET: usize = 0x34; +const FIRST_CAPABILITY_OFFSET: usize = 0x40; +const CAPABILITY_MAX_OFFSET: usize = 192; + +const INTERRUPT_LINE_PIN_REG: usize = 15; + +/// Represents the types of PCI headers allowed in the configuration registers. +#[derive(Copy, Clone)] +pub enum PciHeaderType { + Device, + Bridge, +} + +/// Classes of PCI nodes. +#[allow(dead_code)] +#[derive(Copy, Clone)] +pub enum PciClassCode { + TooOld, + MassStorage, + NetworkController, + DisplayController, + MultimediaController, + MemoryController, + BridgeDevice, + SimpleCommunicationController, + BaseSystemPeripheral, + InputDevice, + DockingStation, + Processor, + SerialBusController, + WirelessController, + IntelligentIoController, + EncryptionController, + DataAcquisitionSignalProcessing, + Other = 0xff, +} + +impl PciClassCode { + pub fn get_register_value(self) -> u8 { + self as u8 + } +} + +/// A PCI subclass. Each class in `PciClassCode` can specify a unique set of subclasses. This trait +/// is implemented by each subclass. It allows use of a trait object to generate configurations. +pub trait PciSubclass { + /// Convert this subclass to the value used in the PCI specification. + fn get_register_value(&self) -> u8; +} + +/// Subclasses of the MultimediaController class. +#[allow(dead_code)] +#[derive(Copy, Clone)] +pub enum PciMultimediaSubclass { + VideoController = 0x00, + AudioController = 0x01, + TelephonyDevice = 0x02, + AudioDevice = 0x03, + Other = 0x80, +} + +impl PciSubclass for PciMultimediaSubclass { + fn get_register_value(&self) -> u8 { + *self as u8 + } +} + +/// Subclasses of the BridgeDevice +#[allow(dead_code)] +#[derive(Copy, Clone)] +pub enum PciBridgeSubclass { + HostBridge = 0x00, + IsaBridge = 0x01, + EisaBridge = 0x02, + McaBridge = 0x03, + PciToPciBridge = 0x04, + PcmciaBridge = 0x05, + NuBusBridge = 0x06, + CardBusBridge = 0x07, + RacEwayBridge = 0x08, + PciToPciSemiTransparentBridge = 0x09, + InfiniBrandToPciHostBridge = 0x0a, + OtherBridgeDevice = 0x80, +} + +impl PciSubclass for PciBridgeSubclass { + fn get_register_value(&self) -> u8 { + *self as u8 + } +} + +/// Subclass of the SerialBus +#[allow(dead_code)] +#[derive(Copy, Clone)] +pub enum PciSerialBusSubClass { + Firewire = 0x00, + Accessbus = 0x01, + Ssa = 0x02, + Usb = 0x03, +} + +impl PciSubclass for PciSerialBusSubClass { + fn get_register_value(&self) -> u8 { + *self as u8 + } +} + +/// Mass Storage Sub Classes +#[allow(dead_code)] +#[derive(Copy, Clone)] +pub enum PciMassStorageSubclass { + ScsiStorage = 0x00, + IdeInterface = 0x01, + FloppyController = 0x02, + IpiController = 0x03, + RaidController = 0x04, + AtaController = 0x05, + SataController = 0x06, + SerialScsiController = 0x07, + NvmController = 0x08, + MassStorage = 0x80, +} + +impl PciSubclass for PciMassStorageSubclass { + fn get_register_value(&self) -> u8 { + *self as u8 + } +} + +/// Network Controller Sub Classes +#[allow(dead_code)] +#[derive(Copy, Clone)] +pub enum PciNetworkControllerSubclass { + EthernetController = 0x00, + TokenRingController = 0x01, + FddiController = 0x02, + AtmController = 0x03, + IsdnController = 0x04, + WorldFipController = 0x05, + PicmgController = 0x06, + InfinibandController = 0x07, + FabricController = 0x08, + NetworkController = 0x80, +} + +impl PciSubclass for PciNetworkControllerSubclass { + fn get_register_value(&self) -> u8 { + *self as u8 + } +} + +/// A PCI class programming interface. Each combination of `PciClassCode` and +/// `PciSubclass` can specify a set of register-level programming interfaces. +/// This trait is implemented by each programming interface. +/// It allows use of a trait object to generate configurations. +pub trait PciProgrammingInterface { + /// Convert this programming interface to the value used in the PCI specification. + fn get_register_value(&self) -> u8; +} + +/// Types of PCI capabilities. +#[derive(PartialEq, Eq, Copy, Clone)] +#[allow(dead_code)] +#[allow(non_camel_case_types)] +#[repr(u8)] +pub enum PciCapabilityId { + ListId = 0, + PowerManagement = 0x01, + AcceleratedGraphicsPort = 0x02, + VitalProductData = 0x03, + SlotIdentification = 0x04, + MessageSignalledInterrupts = 0x05, + CompactPciHotSwap = 0x06, + PciX = 0x07, + HyperTransport = 0x08, + VendorSpecific = 0x09, + Debugport = 0x0A, + CompactPciCentralResourceControl = 0x0B, + PciStandardHotPlugController = 0x0C, + BridgeSubsystemVendorDeviceId = 0x0D, + AgpTargetPciPcibridge = 0x0E, + SecureDevice = 0x0F, + PciExpress = 0x10, + MsiX = 0x11, + SataDataIndexConf = 0x12, + PciAdvancedFeatures = 0x13, + PciEnhancedAllocation = 0x14, +} + +impl From for PciCapabilityId { + fn from(c: u8) -> Self { + match c { + 0 => PciCapabilityId::ListId, + 0x01 => PciCapabilityId::PowerManagement, + 0x02 => PciCapabilityId::AcceleratedGraphicsPort, + 0x03 => PciCapabilityId::VitalProductData, + 0x04 => PciCapabilityId::SlotIdentification, + 0x05 => PciCapabilityId::MessageSignalledInterrupts, + 0x06 => PciCapabilityId::CompactPciHotSwap, + 0x07 => PciCapabilityId::PciX, + 0x08 => PciCapabilityId::HyperTransport, + 0x09 => PciCapabilityId::VendorSpecific, + 0x0A => PciCapabilityId::Debugport, + 0x0B => PciCapabilityId::CompactPciCentralResourceControl, + 0x0C => PciCapabilityId::PciStandardHotPlugController, + 0x0D => PciCapabilityId::BridgeSubsystemVendorDeviceId, + 0x0E => PciCapabilityId::AgpTargetPciPcibridge, + 0x0F => PciCapabilityId::SecureDevice, + 0x10 => PciCapabilityId::PciExpress, + 0x11 => PciCapabilityId::MsiX, + 0x12 => PciCapabilityId::SataDataIndexConf, + 0x13 => PciCapabilityId::PciAdvancedFeatures, + 0x14 => PciCapabilityId::PciEnhancedAllocation, + _ => PciCapabilityId::ListId, + } + } +} + +/// Types of PCI Express capabilities. +#[derive(PartialEq, Eq, Copy, Clone, Debug)] +#[allow(dead_code)] +#[repr(u16)] +pub enum PciExpressCapabilityId { + NullCapability = 0x0000, + AdvancedErrorReporting = 0x0001, + VirtualChannelMultiFunctionVirtualChannelNotPresent = 0x0002, + DeviceSerialNumber = 0x0003, + PowerBudgeting = 0x0004, + RootComplexLinkDeclaration = 0x0005, + RootComplexInternalLinkControl = 0x0006, + RootComplexEventCollectorEndpointAssociation = 0x0007, + MultiFunctionVirtualChannel = 0x0008, + VirtualChannelMultiFunctionVirtualChannelPresent = 0x0009, + RootComplexRegisterBlock = 0x000a, + VendorSpecificExtendedCapability = 0x000b, + ConfigurationAccessCorrelation = 0x000c, + AccessControlServices = 0x000d, + AlternativeRoutingIdentificationIntepretation = 0x000e, + AddressTranslationServices = 0x000f, + SingleRootIoVirtualization = 0x0010, + DeprecatedMultiRootIoVirtualzation = 0x0011, + Multicast = 0x0012, + PageRequestInterface = 0x0013, + ReservedForAmd = 0x0014, + ResizeableBar = 0x0015, + DynamicPowerAllocation = 0x0016, + ThpRequester = 0x0017, + LatencyToleranceReporting = 0x0018, + SecondaryPciExpress = 0x0019, + ProtocolMultiplexing = 0x001a, + ProcessAddressSpaceId = 0x001b, + LnRequestor = 0x001c, + DownstreamPortContainment = 0x001d, + L1PmSubstates = 0x001e, + PrecisionTimeMeasurement = 0x001f, + PciExpressOverMphy = 0x0020, + FRSQueueing = 0x0021, + ReadinessTimeReporting = 0x0022, + DesignatedVendorSpecificExtendedCapability = 0x0023, + VfResizeableBar = 0x0024, + DataLinkFeature = 0x0025, + PhysicalLayerSixteenGts = 0x0026, + LaneMargeningAtTheReceiver = 0x0027, + HierarchyId = 0x0028, + NativePcieEnclosureManagement = 0x0029, + PhysicalLayerThirtyTwoGts = 0x002a, + AlternateProtocol = 0x002b, + SystemFirmwareIntermediary = 0x002c, + ShadowFunctions = 0x002d, + DataObjectExchange = 0x002e, + Reserved = 0x002f, + ExtendedCapabilitiesAbsence = 0xffff, +} + +impl From for PciExpressCapabilityId { + fn from(c: u16) -> Self { + match c { + 0x0000 => PciExpressCapabilityId::NullCapability, + 0x0001 => PciExpressCapabilityId::AdvancedErrorReporting, + 0x0002 => PciExpressCapabilityId::VirtualChannelMultiFunctionVirtualChannelNotPresent, + 0x0003 => PciExpressCapabilityId::DeviceSerialNumber, + 0x0004 => PciExpressCapabilityId::PowerBudgeting, + 0x0005 => PciExpressCapabilityId::RootComplexLinkDeclaration, + 0x0006 => PciExpressCapabilityId::RootComplexInternalLinkControl, + 0x0007 => PciExpressCapabilityId::RootComplexEventCollectorEndpointAssociation, + 0x0008 => PciExpressCapabilityId::MultiFunctionVirtualChannel, + 0x0009 => PciExpressCapabilityId::VirtualChannelMultiFunctionVirtualChannelPresent, + 0x000a => PciExpressCapabilityId::RootComplexRegisterBlock, + 0x000b => PciExpressCapabilityId::VendorSpecificExtendedCapability, + 0x000c => PciExpressCapabilityId::ConfigurationAccessCorrelation, + 0x000d => PciExpressCapabilityId::AccessControlServices, + 0x000e => PciExpressCapabilityId::AlternativeRoutingIdentificationIntepretation, + 0x000f => PciExpressCapabilityId::AddressTranslationServices, + 0x0010 => PciExpressCapabilityId::SingleRootIoVirtualization, + 0x0011 => PciExpressCapabilityId::DeprecatedMultiRootIoVirtualzation, + 0x0012 => PciExpressCapabilityId::Multicast, + 0x0013 => PciExpressCapabilityId::PageRequestInterface, + 0x0014 => PciExpressCapabilityId::ReservedForAmd, + 0x0015 => PciExpressCapabilityId::ResizeableBar, + 0x0016 => PciExpressCapabilityId::DynamicPowerAllocation, + 0x0017 => PciExpressCapabilityId::ThpRequester, + 0x0018 => PciExpressCapabilityId::LatencyToleranceReporting, + 0x0019 => PciExpressCapabilityId::SecondaryPciExpress, + 0x001a => PciExpressCapabilityId::ProtocolMultiplexing, + 0x001b => PciExpressCapabilityId::ProcessAddressSpaceId, + 0x001c => PciExpressCapabilityId::LnRequestor, + 0x001d => PciExpressCapabilityId::DownstreamPortContainment, + 0x001e => PciExpressCapabilityId::L1PmSubstates, + 0x001f => PciExpressCapabilityId::PrecisionTimeMeasurement, + 0x0020 => PciExpressCapabilityId::PciExpressOverMphy, + 0x0021 => PciExpressCapabilityId::FRSQueueing, + 0x0022 => PciExpressCapabilityId::ReadinessTimeReporting, + 0x0023 => PciExpressCapabilityId::DesignatedVendorSpecificExtendedCapability, + 0x0024 => PciExpressCapabilityId::VfResizeableBar, + 0x0025 => PciExpressCapabilityId::DataLinkFeature, + 0x0026 => PciExpressCapabilityId::PhysicalLayerSixteenGts, + 0x0027 => PciExpressCapabilityId::LaneMargeningAtTheReceiver, + 0x0028 => PciExpressCapabilityId::HierarchyId, + 0x0029 => PciExpressCapabilityId::NativePcieEnclosureManagement, + 0x002a => PciExpressCapabilityId::PhysicalLayerThirtyTwoGts, + 0x002b => PciExpressCapabilityId::AlternateProtocol, + 0x002c => PciExpressCapabilityId::SystemFirmwareIntermediary, + 0x002d => PciExpressCapabilityId::ShadowFunctions, + 0x002e => PciExpressCapabilityId::DataObjectExchange, + 0xffff => PciExpressCapabilityId::ExtendedCapabilitiesAbsence, + _ => PciExpressCapabilityId::Reserved, + } + } +} + +/// A PCI capability list. Devices can optionally specify capabilities in their configuration space. +pub trait PciCapability { + fn bytes(&self) -> &[u8]; + fn id(&self) -> PciCapabilityId; +} + +fn encode_32_bits_bar_size(bar_size: u32) -> Option { + if bar_size > 0 { + return Some(!(bar_size - 1)); + } + None +} + +fn decode_32_bits_bar_size(bar_size: u32) -> Option { + if bar_size > 0 { + return Some(!bar_size + 1); + } + None +} + +fn encode_64_bits_bar_size(bar_size: u64) -> Option<(u32, u32)> { + if bar_size > 0 { + let result = !(bar_size - 1); + let result_hi = (result >> 32) as u32; + let result_lo = (result & 0xffff_ffff) as u32; + return Some((result_hi, result_lo)); + } + None +} + +fn decode_64_bits_bar_size(bar_size_hi: u32, bar_size_lo: u32) -> Option { + let bar_size: u64 = ((bar_size_hi as u64) << 32) | (bar_size_lo as u64); + if bar_size > 0 { + return Some(!bar_size + 1); + } + None +} + +#[derive(Default, Clone, Copy, Serialize, Deserialize)] +struct PciBar { + addr: u32, + size: u32, + used: bool, + r#type: Option, +} + +#[derive(Serialize, Deserialize)] +struct PciConfigurationState { + registers: Vec, + writable_bits: Vec, + bars: Vec, + rom_bar_addr: u32, + rom_bar_size: u32, + rom_bar_used: bool, + last_capability: Option<(usize, usize)>, + msix_cap_reg_idx: Option, +} + +/// Contains the configuration space of a PCI node. +/// See the [specification](https://en.wikipedia.org/wiki/PCI_configuration_space). +/// The configuration space is accessed with DWORD reads and writes from the guest. +pub struct PciConfiguration { + registers: [u32; NUM_CONFIGURATION_REGISTERS], + writable_bits: [u32; NUM_CONFIGURATION_REGISTERS], // writable bits for each register. + bars: [PciBar; NUM_BAR_REGS], + rom_bar_addr: u32, + rom_bar_size: u32, + rom_bar_used: bool, + // Contains the byte offset and size of the last capability. + last_capability: Option<(usize, usize)>, + msix_cap_reg_idx: Option, + msix_config: Option>>, +} + +/// See pci_regs.h in kernel +#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] +pub enum PciBarRegionType { + Memory32BitRegion = 0, + IoRegion = 0x01, + Memory64BitRegion = 0x04, +} + +impl From for PciBarRegionType { + fn from(type_: PciBarType) -> Self { + match type_ { + PciBarType::Io => PciBarRegionType::IoRegion, + PciBarType::Mmio32 => PciBarRegionType::Memory32BitRegion, + PciBarType::Mmio64 => PciBarRegionType::Memory64BitRegion, + } + } +} + +#[allow(clippy::from_over_into)] +impl Into for PciBarRegionType { + fn into(self) -> PciBarType { + match self { + PciBarRegionType::IoRegion => PciBarType::Io, + PciBarRegionType::Memory32BitRegion => PciBarType::Mmio32, + PciBarRegionType::Memory64BitRegion => PciBarType::Mmio64, + } + } +} + +#[derive(Copy, Clone)] +pub enum PciBarPrefetchable { + NotPrefetchable = 0, + Prefetchable = 0x08, +} + +#[allow(clippy::from_over_into)] +impl Into for PciBarPrefetchable { + fn into(self) -> bool { + match self { + PciBarPrefetchable::NotPrefetchable => false, + PciBarPrefetchable::Prefetchable => true, + } + } +} + +#[derive(Copy, Clone)] +pub struct PciBarConfiguration { + addr: u64, + size: u64, + idx: usize, + region_type: PciBarRegionType, + prefetchable: PciBarPrefetchable, +} + +#[derive(Debug)] +pub enum Error { + BarAddressInvalid(u64, u64), + BarInUse(usize), + BarInUse64(usize), + BarInvalid(usize), + BarInvalid64(usize), + BarSizeInvalid(u64), + CapabilityEmpty, + CapabilityLengthInvalid(usize), + CapabilitySpaceFull(usize), + Decode32BarSize, + Decode64BarSize, + Encode32BarSize, + Encode64BarSize, + RomBarAddressInvalid(u64, u64), + RomBarInUse(usize), + RomBarInvalid(usize), + RomBarSizeInvalid(u64), +} +pub type Result = std::result::Result; + +impl std::error::Error for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + match self { + BarAddressInvalid(a, s) => write!(f, "address {} size {} too big", a, s), + BarInUse(b) => write!(f, "bar {} already used", b), + BarInUse64(b) => write!(f, "64bit bar {} already used(requires two regs)", b), + BarInvalid(b) => write!(f, "bar {} invalid, max {}", b, NUM_BAR_REGS - 1), + BarInvalid64(b) => write!( + f, + "64bitbar {} invalid, requires two regs, max {}", + b, + NUM_BAR_REGS - 1 + ), + BarSizeInvalid(s) => write!(f, "bar address {} not a power of two", s), + CapabilityEmpty => write!(f, "empty capabilities are invalid"), + CapabilityLengthInvalid(l) => write!(f, "Invalid capability length {}", l), + CapabilitySpaceFull(s) => write!(f, "capability of size {} doesn't fit", s), + Decode32BarSize => write!(f, "failed to decode 32 bits BAR size"), + Decode64BarSize => write!(f, "failed to decode 64 bits BAR size"), + Encode32BarSize => write!(f, "failed to encode 32 bits BAR size"), + Encode64BarSize => write!(f, "failed to encode 64 bits BAR size"), + RomBarAddressInvalid(a, s) => write!(f, "address {} size {} too big", a, s), + RomBarInUse(b) => write!(f, "rom bar {} already used", b), + RomBarInvalid(b) => write!(f, "rom bar {} invalid, max {}", b, NUM_BAR_REGS - 1), + RomBarSizeInvalid(s) => write!(f, "rom bar address {} not a power of two", s), + } + } +} + +impl PciConfiguration { + #[allow(clippy::too_many_arguments)] + pub fn new( + vendor_id: u16, + device_id: u16, + revision_id: u8, + class_code: PciClassCode, + subclass: &dyn PciSubclass, + programming_interface: Option<&dyn PciProgrammingInterface>, + header_type: PciHeaderType, + subsystem_vendor_id: u16, + subsystem_id: u16, + msix_config: Option>>, + ) -> Self { + let mut registers = [0u32; NUM_CONFIGURATION_REGISTERS]; + let mut writable_bits = [0u32; NUM_CONFIGURATION_REGISTERS]; + registers[0] = u32::from(device_id) << 16 | u32::from(vendor_id); + // TODO(dverkamp): Status should be write-1-to-clear + writable_bits[1] = 0x0000_ffff; // Status (r/o), command (r/w) + let pi = if let Some(pi) = programming_interface { + pi.get_register_value() + } else { + 0 + }; + registers[2] = u32::from(class_code.get_register_value()) << 24 + | u32::from(subclass.get_register_value()) << 16 + | u32::from(pi) << 8 + | u32::from(revision_id); + writable_bits[3] = 0x0000_00ff; // Cacheline size (r/w) + match header_type { + PciHeaderType::Device => { + registers[3] = 0x0000_0000; // Header type 0 (device) + writable_bits[15] = 0x0000_00ff; // Interrupt line (r/w) + } + PciHeaderType::Bridge => { + registers[3] = 0x0001_0000; // Header type 1 (bridge) + writable_bits[9] = 0xfff0_fff0; // Memory base and limit + writable_bits[15] = 0xffff_00ff; // Bridge control (r/w), interrupt line (r/w) + } + }; + registers[11] = u32::from(subsystem_id) << 16 | u32::from(subsystem_vendor_id); + + let bars = [PciBar::default(); NUM_BAR_REGS]; + + PciConfiguration { + registers, + writable_bits, + bars, + rom_bar_addr: 0, + rom_bar_size: 0, + rom_bar_used: false, + last_capability: None, + msix_cap_reg_idx: None, + msix_config, + } + } + + fn state(&self) -> PciConfigurationState { + PciConfigurationState { + registers: self.registers.to_vec(), + writable_bits: self.writable_bits.to_vec(), + bars: self.bars.to_vec(), + rom_bar_addr: self.rom_bar_addr, + rom_bar_size: self.rom_bar_size, + rom_bar_used: self.rom_bar_used, + last_capability: self.last_capability, + msix_cap_reg_idx: self.msix_cap_reg_idx, + } + } + + fn set_state(&mut self, state: &PciConfigurationState) { + self.registers.clone_from_slice(state.registers.as_slice()); + self.writable_bits + .clone_from_slice(state.writable_bits.as_slice()); + self.bars.clone_from_slice(state.bars.as_slice()); + self.rom_bar_addr = state.rom_bar_addr; + self.rom_bar_size = state.rom_bar_size; + self.rom_bar_used = state.rom_bar_used; + self.last_capability = state.last_capability; + self.msix_cap_reg_idx = state.msix_cap_reg_idx; + } + + /// Reads a 32bit register from `reg_idx` in the register map. + pub fn read_reg(&self, reg_idx: usize) -> u32 { + *(self.registers.get(reg_idx).unwrap_or(&0xffff_ffff)) + } + + /// Writes a 32bit register to `reg_idx` in the register map. + pub fn write_reg(&mut self, reg_idx: usize, value: u32) { + let mut mask = self.writable_bits[reg_idx]; + + if (BAR0_REG..BAR0_REG + NUM_BAR_REGS).contains(®_idx) { + // Handle very specific case where the BAR is being written with + // all 1's to retrieve the BAR size during next BAR reading. + if value == 0xffff_ffff { + mask &= self.bars[reg_idx - 4].size; + } + } else if reg_idx == ROM_BAR_REG { + // Handle very specific case where the BAR is being written with + // all 1's on bits 31-11 to retrieve the BAR size during next BAR + // reading. + if value & ROM_BAR_ADDR_MASK == ROM_BAR_ADDR_MASK { + mask &= self.rom_bar_size; + } + } + + if let Some(r) = self.registers.get_mut(reg_idx) { + *r = (*r & !self.writable_bits[reg_idx]) | (value & mask); + } else { + warn!("bad PCI register write {}", reg_idx); + } + } + + /// Writes a 16bit word to `offset`. `offset` must be 16bit aligned. + pub fn write_word(&mut self, offset: usize, value: u16) { + let shift = match offset % 4 { + 0 => 0, + 2 => 16, + _ => { + warn!("bad PCI config write offset {}", offset); + return; + } + }; + let reg_idx = offset / 4; + + if let Some(r) = self.registers.get_mut(reg_idx) { + let writable_mask = self.writable_bits[reg_idx]; + let mask = (0xffffu32 << shift) & writable_mask; + let shifted_value = (u32::from(value) << shift) & writable_mask; + *r = *r & !mask | shifted_value; + } else { + warn!("bad PCI config write offset {}", offset); + } + } + + /// Writes a byte to `offset`. + pub fn write_byte(&mut self, offset: usize, value: u8) { + self.write_byte_internal(offset, value, true); + } + + /// Writes a byte to `offset`, optionally enforcing read-only bits. + fn write_byte_internal(&mut self, offset: usize, value: u8, apply_writable_mask: bool) { + let shift = (offset % 4) * 8; + let reg_idx = offset / 4; + + if let Some(r) = self.registers.get_mut(reg_idx) { + let writable_mask = if apply_writable_mask { + self.writable_bits[reg_idx] + } else { + 0xffff_ffff + }; + let mask = (0xffu32 << shift) & writable_mask; + let shifted_value = (u32::from(value) << shift) & writable_mask; + *r = *r & !mask | shifted_value; + } else { + warn!("bad PCI config write offset {}", offset); + } + } + + /// Adds a region specified by `config`. Configures the specified BAR(s) to + /// report this region and size to the guest kernel. Enforces a few constraints + /// (i.e, region size must be power of two, register not already used). + pub fn add_pci_bar(&mut self, config: &PciBarConfiguration) -> Result<()> { + let bar_idx = config.idx; + let reg_idx = BAR0_REG + bar_idx; + + if self.bars[bar_idx].used { + return Err(Error::BarInUse(bar_idx)); + } + + if config.size.count_ones() != 1 { + return Err(Error::BarSizeInvalid(config.size)); + } + + if bar_idx >= NUM_BAR_REGS { + return Err(Error::BarInvalid(bar_idx)); + } + + let end_addr = config + .addr + .checked_add(config.size - 1) + .ok_or(Error::BarAddressInvalid(config.addr, config.size))?; + match config.region_type { + PciBarRegionType::Memory32BitRegion | PciBarRegionType::IoRegion => { + if end_addr > u64::from(u32::max_value()) { + return Err(Error::BarAddressInvalid(config.addr, config.size)); + } + + // Encode the BAR size as expected by the software running in + // the guest. + self.bars[bar_idx].size = + encode_32_bits_bar_size(config.size as u32).ok_or(Error::Encode32BarSize)?; + } + PciBarRegionType::Memory64BitRegion => { + if bar_idx + 1 >= NUM_BAR_REGS { + return Err(Error::BarInvalid64(bar_idx)); + } + + if end_addr > u64::max_value() { + return Err(Error::BarAddressInvalid(config.addr, config.size)); + } + + if self.bars[bar_idx + 1].used { + return Err(Error::BarInUse64(bar_idx)); + } + + // Encode the BAR size as expected by the software running in + // the guest. + let (bar_size_hi, bar_size_lo) = + encode_64_bits_bar_size(config.size).ok_or(Error::Encode64BarSize)?; + + self.registers[reg_idx + 1] = (config.addr >> 32) as u32; + self.writable_bits[reg_idx + 1] = 0xffff_ffff; + self.bars[bar_idx + 1].addr = self.registers[reg_idx + 1]; + self.bars[bar_idx].size = bar_size_lo; + self.bars[bar_idx + 1].size = bar_size_hi; + self.bars[bar_idx + 1].used = true; + } + } + + let (mask, lower_bits) = match config.region_type { + PciBarRegionType::Memory32BitRegion | PciBarRegionType::Memory64BitRegion => ( + BAR_MEM_ADDR_MASK, + config.prefetchable as u32 | config.region_type as u32, + ), + PciBarRegionType::IoRegion => (BAR_IO_ADDR_MASK, config.region_type as u32), + }; + + self.registers[reg_idx] = ((config.addr as u32) & mask) | lower_bits; + self.writable_bits[reg_idx] = mask; + self.bars[bar_idx].addr = self.registers[reg_idx]; + self.bars[bar_idx].used = true; + self.bars[bar_idx].r#type = Some(config.region_type); + + Ok(()) + } + + /// Adds rom expansion BAR. + pub fn add_pci_rom_bar(&mut self, config: &PciBarConfiguration, active: u32) -> Result<()> { + let bar_idx = config.idx; + let reg_idx = ROM_BAR_REG; + + if self.rom_bar_used { + return Err(Error::RomBarInUse(bar_idx)); + } + + if config.size.count_ones() != 1 { + return Err(Error::RomBarSizeInvalid(config.size)); + } + + if bar_idx != ROM_BAR_IDX { + return Err(Error::RomBarInvalid(bar_idx)); + } + + let end_addr = config + .addr + .checked_add(config.size - 1) + .ok_or(Error::RomBarAddressInvalid(config.addr, config.size))?; + + if end_addr > u64::from(u32::max_value()) { + return Err(Error::RomBarAddressInvalid(config.addr, config.size)); + } + + self.registers[reg_idx] = (config.addr as u32) | active; + self.writable_bits[reg_idx] = ROM_BAR_ADDR_MASK; + self.rom_bar_addr = self.registers[reg_idx]; + self.rom_bar_size = + encode_32_bits_bar_size(config.size as u32).ok_or(Error::Encode32BarSize)?; + self.rom_bar_used = true; + + Ok(()) + } + + /// Returns the address of the given BAR region. + pub fn get_bar_addr(&self, bar_num: usize) -> u64 { + let bar_idx = BAR0_REG + bar_num; + + let mut addr = u64::from(self.bars[bar_num].addr & self.writable_bits[bar_idx]); + + if let Some(bar_type) = self.bars[bar_num].r#type { + if bar_type == PciBarRegionType::Memory64BitRegion { + addr |= u64::from(self.bars[bar_num + 1].addr) << 32; + } + } + + addr + } + + /// Configures the IRQ line and pin used by this device. + pub fn set_irq(&mut self, line: u8, pin: PciInterruptPin) { + // `pin` is 1-based in the pci config space. + let pin_idx = (pin as u32) + 1; + self.registers[INTERRUPT_LINE_PIN_REG] = (self.registers[INTERRUPT_LINE_PIN_REG] + & 0xffff_0000) + | (pin_idx << 8) + | u32::from(line); + } + + /// Adds the capability `cap_data` to the list of capabilities. + /// `cap_data` should include the two-byte PCI capability header (type, next), + /// but not populate it. Correct values will be generated automatically based + /// on `cap_data.id()`. + pub fn add_capability(&mut self, cap_data: &dyn PciCapability) -> Result { + let total_len = cap_data.bytes().len(); + // Check that the length is valid. + if cap_data.bytes().is_empty() { + return Err(Error::CapabilityEmpty); + } + let (cap_offset, tail_offset) = match self.last_capability { + Some((offset, len)) => (Self::next_dword(offset, len), offset + 1), + None => (FIRST_CAPABILITY_OFFSET, CAPABILITY_LIST_HEAD_OFFSET), + }; + let end_offset = cap_offset + .checked_add(total_len) + .ok_or(Error::CapabilitySpaceFull(total_len))?; + if end_offset > CAPABILITY_MAX_OFFSET { + return Err(Error::CapabilitySpaceFull(total_len)); + } + self.registers[STATUS_REG] |= STATUS_REG_CAPABILITIES_USED_MASK; + self.write_byte_internal(tail_offset, cap_offset as u8, false); + self.write_byte_internal(cap_offset, cap_data.id() as u8, false); + self.write_byte_internal(cap_offset + 1, 0, false); // Next pointer. + for (i, byte) in cap_data.bytes().iter().enumerate() { + self.write_byte_internal(cap_offset + i + 2, *byte, false); + } + self.last_capability = Some((cap_offset, total_len)); + + match cap_data.id() { + PciCapabilityId::MessageSignalledInterrupts => { + self.writable_bits[cap_offset / 4] = MSI_CAPABILITY_REGISTER_MASK; + } + PciCapabilityId::MsiX => { + self.msix_cap_reg_idx = Some(cap_offset / 4); + self.writable_bits[self.msix_cap_reg_idx.unwrap()] = MSIX_CAPABILITY_REGISTER_MASK; + } + _ => {} + } + + Ok(cap_offset) + } + + // Find the next aligned offset after the one given. + fn next_dword(offset: usize, len: usize) -> usize { + let next = offset + len; + (next + 3) & !3 + } + + pub fn write_config_register(&mut self, reg_idx: usize, offset: u64, data: &[u8]) { + if offset as usize + data.len() > 4 { + return; + } + + // Handle potential write to MSI-X message control register + if let Some(msix_cap_reg_idx) = self.msix_cap_reg_idx { + if let Some(msix_config) = &self.msix_config { + if msix_cap_reg_idx == reg_idx && offset == 2 && data.len() == 2 { + msix_config + .lock() + .unwrap() + .set_msg_ctl(LittleEndian::read_u16(data)); + } else if msix_cap_reg_idx == reg_idx && offset == 0 && data.len() == 4 { + msix_config + .lock() + .unwrap() + .set_msg_ctl((LittleEndian::read_u32(data) >> 16) as u16); + } + } + } + + match data.len() { + 1 => self.write_byte(reg_idx * 4 + offset as usize, data[0]), + 2 => self.write_word( + reg_idx * 4 + offset as usize, + u16::from(data[0]) | u16::from(data[1]) << 8, + ), + 4 => self.write_reg(reg_idx, LittleEndian::read_u32(data)), + _ => (), + } + } + + pub fn read_config_register(&self, reg_idx: usize) -> u32 { + self.read_reg(reg_idx) + } + + pub fn detect_bar_reprogramming( + &mut self, + reg_idx: usize, + data: &[u8], + ) -> Option { + if data.len() != 4 { + return None; + } + + let value = LittleEndian::read_u32(data); + + let mask = self.writable_bits[reg_idx]; + if (BAR0_REG..BAR0_REG + NUM_BAR_REGS).contains(®_idx) { + // Ignore the case where the BAR size is being asked for. + if value == 0xffff_ffff { + return None; + } + + let bar_idx = reg_idx - 4; + // Handle special case where the address being written is + // different from the address initially provided. This is a + // BAR reprogramming case which needs to be properly caught. + if let Some(bar_type) = self.bars[bar_idx].r#type { + // In case of 64 bits memory BAR, we don't do anything until + // the upper BAR is modified, otherwise we would be moving the + // BAR to a wrong location in memory. + if bar_type == PciBarRegionType::Memory64BitRegion { + return None; + } + + // Ignore the case where the value is unchanged. + if (value & mask) == (self.bars[bar_idx].addr & mask) { + return None; + } + + info!( + "Detected BAR reprogramming: (BAR {}) 0x{:x}->0x{:x}", + reg_idx, self.registers[reg_idx], value + ); + let old_base = u64::from(self.bars[bar_idx].addr & mask); + let new_base = u64::from(value & mask); + let len = u64::from( + decode_32_bits_bar_size(self.bars[bar_idx].size) + .ok_or(Error::Decode32BarSize) + .unwrap(), + ); + let region_type = bar_type; + + self.bars[bar_idx].addr = value; + + return Some(BarReprogrammingParams { + old_base, + new_base, + len, + region_type, + }); + } else if (reg_idx > BAR0_REG) + && ((self.registers[reg_idx - 1] & self.writable_bits[reg_idx - 1]) + != (self.bars[bar_idx - 1].addr & self.writable_bits[reg_idx - 1]) + || (value & mask) != (self.bars[bar_idx].addr & mask)) + { + info!( + "Detected BAR reprogramming: (BAR {}) 0x{:x}->0x{:x}", + reg_idx, self.registers[reg_idx], value + ); + let old_base = u64::from(self.bars[bar_idx].addr & mask) << 32 + | u64::from(self.bars[bar_idx - 1].addr & self.writable_bits[reg_idx - 1]); + let new_base = u64::from(value & mask) << 32 + | u64::from(self.registers[reg_idx - 1] & self.writable_bits[reg_idx - 1]); + let len = + decode_64_bits_bar_size(self.bars[bar_idx].size, self.bars[bar_idx - 1].size) + .ok_or(Error::Decode64BarSize) + .unwrap(); + let region_type = PciBarRegionType::Memory64BitRegion; + + self.bars[bar_idx].addr = value; + self.bars[bar_idx - 1].addr = self.registers[reg_idx - 1]; + + return Some(BarReprogrammingParams { + old_base, + new_base, + len, + region_type, + }); + } + } else if reg_idx == ROM_BAR_REG && (value & mask) != (self.rom_bar_addr & mask) { + // Ignore the case where the BAR size is being asked for. + if value & ROM_BAR_ADDR_MASK == ROM_BAR_ADDR_MASK { + return None; + } + + info!( + "Detected ROM BAR reprogramming: (BAR {}) 0x{:x}->0x{:x}", + reg_idx, self.registers[reg_idx], value + ); + let old_base = u64::from(self.rom_bar_addr & mask); + let new_base = u64::from(value & mask); + let len = u64::from( + decode_32_bits_bar_size(self.rom_bar_size) + .ok_or(Error::Decode32BarSize) + .unwrap(), + ); + let region_type = PciBarRegionType::Memory32BitRegion; + + self.rom_bar_addr = value; + + return Some(BarReprogrammingParams { + old_base, + new_base, + len, + region_type, + }); + } + + None + } +} + +impl Pausable for PciConfiguration {} + +impl Snapshottable for PciConfiguration { + fn id(&self) -> String { + String::from("pci_configuration") + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id(), &self.state()) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + self.set_state(&snapshot.to_state(&self.id())?); + Ok(()) + } +} + +impl Default for PciBarConfiguration { + fn default() -> Self { + PciBarConfiguration { + idx: 0, + addr: 0, + size: 0, + region_type: PciBarRegionType::Memory64BitRegion, + prefetchable: PciBarPrefetchable::NotPrefetchable, + } + } +} + +impl PciBarConfiguration { + pub fn new( + idx: usize, + size: u64, + region_type: PciBarRegionType, + prefetchable: PciBarPrefetchable, + ) -> Self { + PciBarConfiguration { + idx, + addr: 0, + size, + region_type, + prefetchable, + } + } + + #[must_use] + pub fn set_index(mut self, idx: usize) -> Self { + self.idx = idx; + self + } + + #[must_use] + pub fn set_address(mut self, addr: u64) -> Self { + self.addr = addr; + self + } + + #[must_use] + pub fn set_size(mut self, size: u64) -> Self { + self.size = size; + self + } + + #[must_use] + pub fn set_region_type(mut self, region_type: PciBarRegionType) -> Self { + self.region_type = region_type; + self + } + + #[must_use] + pub fn set_prefetchable(mut self, prefetchable: PciBarPrefetchable) -> Self { + self.prefetchable = prefetchable; + self + } + + pub fn idx(&self) -> usize { + self.idx + } + + pub fn addr(&self) -> u64 { + self.addr + } + + pub fn size(&self) -> u64 { + self.size + } + + pub fn region_type(&self) -> PciBarRegionType { + self.region_type + } + + pub fn prefetchable(&self) -> PciBarPrefetchable { + self.prefetchable + } +} + +#[cfg(test)] +mod tests { + use vm_memory::ByteValued; + + use super::*; + + #[repr(packed)] + #[derive(Clone, Copy, Default)] + #[allow(dead_code)] + struct TestCap { + len: u8, + foo: u8, + } + + // SAFETY: All members are simple numbers and any value is valid. + unsafe impl ByteValued for TestCap {} + + impl PciCapability for TestCap { + fn bytes(&self) -> &[u8] { + self.as_slice() + } + + fn id(&self) -> PciCapabilityId { + PciCapabilityId::VendorSpecific + } + } + + #[test] + fn add_capability() { + let mut cfg = PciConfiguration::new( + 0x1234, + 0x5678, + 0x1, + PciClassCode::MultimediaController, + &PciMultimediaSubclass::AudioController, + None, + PciHeaderType::Device, + 0xABCD, + 0x2468, + None, + ); + + // Add two capabilities with different contents. + let cap1 = TestCap { len: 4, foo: 0xAA }; + let cap1_offset = cfg.add_capability(&cap1).unwrap(); + assert_eq!(cap1_offset % 4, 0); + + let cap2 = TestCap { + len: 0x04, + foo: 0x55, + }; + let cap2_offset = cfg.add_capability(&cap2).unwrap(); + assert_eq!(cap2_offset % 4, 0); + + // The capability list head should be pointing to cap1. + let cap_ptr = cfg.read_reg(CAPABILITY_LIST_HEAD_OFFSET / 4) & 0xFF; + assert_eq!(cap1_offset, cap_ptr as usize); + + // Verify the contents of the capabilities. + let cap1_data = cfg.read_reg(cap1_offset / 4); + assert_eq!(cap1_data & 0xFF, 0x09); // capability ID + assert_eq!((cap1_data >> 8) & 0xFF, cap2_offset as u32); // next capability pointer + assert_eq!((cap1_data >> 16) & 0xFF, 0x04); // cap1.len + assert_eq!((cap1_data >> 24) & 0xFF, 0xAA); // cap1.foo + + let cap2_data = cfg.read_reg(cap2_offset / 4); + assert_eq!(cap2_data & 0xFF, 0x09); // capability ID + assert_eq!((cap2_data >> 8) & 0xFF, 0x00); // next capability pointer + assert_eq!((cap2_data >> 16) & 0xFF, 0x04); // cap2.len + assert_eq!((cap2_data >> 24) & 0xFF, 0x55); // cap2.foo + } + + #[derive(Copy, Clone)] + enum TestPi { + Test = 0x5a, + } + + impl PciProgrammingInterface for TestPi { + fn get_register_value(&self) -> u8 { + *self as u8 + } + } + + #[test] + fn class_code() { + let cfg = PciConfiguration::new( + 0x1234, + 0x5678, + 0x1, + PciClassCode::MultimediaController, + &PciMultimediaSubclass::AudioController, + Some(&TestPi::Test), + PciHeaderType::Device, + 0xABCD, + 0x2468, + None, + ); + + let class_reg = cfg.read_reg(2); + let class_code = (class_reg >> 24) & 0xFF; + let subclass = (class_reg >> 16) & 0xFF; + let prog_if = (class_reg >> 8) & 0xFF; + assert_eq!(class_code, 0x04); + assert_eq!(subclass, 0x01); + assert_eq!(prog_if, 0x5a); + } +} diff --git a/vendor/cube-hypervisor/pci/src/device.rs b/vendor/cube-hypervisor/pci/src/device.rs new file mode 100644 index 0000000..1a49beb --- /dev/null +++ b/vendor/cube-hypervisor/pci/src/device.rs @@ -0,0 +1,131 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use crate::configuration::{self, PciBarRegionType}; +use crate::PciBarConfiguration; +use std::any::Any; +use std::fmt::{self, Display}; +use std::sync::{Arc, Barrier, Mutex}; +use std::{self, io, result}; +use vm_allocator::{AddressAllocator, SystemAllocator}; +use vm_device::{BusDevice, Resource}; + +#[derive(Debug)] +pub enum Error { + /// Setup of the device capabilities failed. + CapabilitiesSetup(configuration::Error), + /// Allocating space for an IO BAR failed. + IoAllocationFailed(u64), + /// Registering an IO BAR failed. + IoRegistrationFailed(u64, configuration::Error), + /// Expected resource not found. + MissingResource, + /// Invalid resource. + InvalidResource(Resource), +} +pub type Result = std::result::Result; + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + + match self { + CapabilitiesSetup(e) => write!(f, "failed to add capability {}", e), + IoAllocationFailed(size) => { + write!(f, "failed to allocate space for an IO BAR, size={}", size) + } + IoRegistrationFailed(addr, e) => { + write!(f, "failed to register an IO BAR, addr={} err={}", addr, e) + } + MissingResource => write!(f, "failed to find expected resource"), + InvalidResource(r) => write!(f, "invalid resource {:?}", r), + } + } +} + +#[derive(Clone, Copy)] +pub struct BarReprogrammingParams { + pub old_base: u64, + pub new_base: u64, + pub len: u64, + pub region_type: PciBarRegionType, +} + +pub trait PciDevice: BusDevice { + /// Allocates the needed PCI BARs space using the `allocate` function which takes a size and + /// returns an address. Returns a Vec of (GuestAddress, GuestUsize) tuples. + fn allocate_bars( + &mut self, + _allocator: &Arc>, + _mmio_allocator: &mut AddressAllocator, + _resources: Option>, + ) -> Result> { + Ok(Vec::new()) + } + + /// Frees the PCI BARs previously allocated with a call to allocate_bars(). + fn free_bars( + &mut self, + _allocator: &mut SystemAllocator, + _mmio_allocator: &mut AddressAllocator, + ) -> Result<()> { + Ok(()) + } + + /// Sets a register in the configuration space. + /// * `reg_idx` - The index of the config register to modify. + /// * `offset` - Offset in to the register. + fn write_config_register( + &mut self, + reg_idx: usize, + offset: u64, + data: &[u8], + ) -> Option>; + /// Gets a register from the configuration space. + /// * `reg_idx` - The index of the config register to read. + fn read_config_register(&mut self, reg_idx: usize) -> u32; + /// Detects if a BAR is being reprogrammed. + fn detect_bar_reprogramming( + &mut self, + _reg_idx: usize, + _data: &[u8], + ) -> Option { + None + } + /// Reads from a BAR region mapped in to the device. + /// * `addr` - The guest address inside the BAR. + /// * `data` - Filled with the data from `addr`. + fn read_bar(&mut self, _base: u64, _offset: u64, _data: &mut [u8]) {} + /// Writes to a BAR region mapped in to the device. + /// * `addr` - The guest address inside the BAR. + /// * `data` - The data to write. + fn write_bar(&mut self, _base: u64, _offset: u64, _data: &[u8]) -> Option> { + None + } + /// Relocates the BAR to a different address in guest address space. + fn move_bar(&mut self, _old_base: u64, _new_base: u64) -> result::Result<(), io::Error> { + Ok(()) + } + /// Provides a mutable reference to the Any trait. This is useful to let + /// the caller have access to the underlying type behind the trait. + fn as_any(&mut self) -> &mut dyn Any; + + /// Optionally returns a unique identifier. + fn id(&self) -> Option; +} + +/// This trait defines a set of functions which can be triggered whenever a +/// PCI device is modified in any way. +pub trait DeviceRelocation: Send + Sync { + /// The BAR needs to be moved to a different location in the guest address + /// space. This follows a decision from the software running in the guest. + fn move_bar( + &self, + old_base: u64, + new_base: u64, + len: u64, + pci_dev: &mut dyn PciDevice, + region_type: PciBarRegionType, + ) -> result::Result<(), io::Error>; +} diff --git a/vendor/cube-hypervisor/pci/src/lib.rs b/vendor/cube-hypervisor/pci/src/lib.rs new file mode 100644 index 0000000..0758d17 --- /dev/null +++ b/vendor/cube-hypervisor/pci/src/lib.rs @@ -0,0 +1,183 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +//! Implements pci devices and busses. +#[macro_use] +extern crate log; + +mod bus; +mod configuration; +mod device; +mod msi; +mod msix; +mod vfio; +mod vfio_user; + +pub use self::bus::{PciBus, PciConfigIo, PciConfigMmio, PciRoot, PciRootError}; +pub use self::configuration::{ + PciBarConfiguration, PciBarPrefetchable, PciBarRegionType, PciCapability, PciCapabilityId, + PciClassCode, PciConfiguration, PciExpressCapabilityId, PciHeaderType, PciMassStorageSubclass, + PciNetworkControllerSubclass, PciProgrammingInterface, PciSerialBusSubClass, PciSubclass, +}; +pub use self::device::{ + BarReprogrammingParams, DeviceRelocation, Error as PciDeviceError, PciDevice, +}; +pub use self::msi::{msi_num_enabled_vectors, MsiCap, MsiConfig}; +pub use self::msix::{MsixCap, MsixConfig, MsixTableEntry, MSIX_TABLE_ENTRY_SIZE}; +pub use self::vfio::{VfioPciDevice, VfioPciError}; +pub use self::vfio_user::{VfioUserDmaMapping, VfioUserPciDevice, VfioUserPciDeviceError}; +use serde::de::Visitor; +use std::fmt::{self, Display}; +use std::num::ParseIntError; +use std::str::FromStr; + +/// PCI has four interrupt pins A->D. +#[derive(Copy, Clone)] +pub enum PciInterruptPin { + IntA, + IntB, + IntC, + IntD, +} + +impl PciInterruptPin { + pub fn to_mask(self) -> u32 { + self as u32 + } +} + +#[cfg(target_arch = "x86_64")] +pub const PCI_CONFIG_IO_PORT: u64 = 0xcf8; +#[cfg(target_arch = "x86_64")] +pub const PCI_CONFIG_IO_PORT_SIZE: u64 = 0x8; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd)] +pub struct PciBdf(u32); + +struct PciBdfVisitor; + +impl<'de> Visitor<'de> for PciBdfVisitor { + type Value = PciBdf; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("struct PciBdf") + } + + fn visit_str(self, v: &str) -> Result + where + E: serde::de::Error, + { + Ok(v.into()) + } +} + +impl<'de> serde::Deserialize<'de> for PciBdf { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + deserializer.deserialize_str(PciBdfVisitor) + } +} + +impl serde::Serialize for PciBdf { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.collect_str(&self.to_string()) + } +} + +impl PciBdf { + pub fn segment(&self) -> u16 { + ((self.0 >> 16) & 0xffff) as u16 + } + + pub fn bus(&self) -> u8 { + ((self.0 >> 8) & 0xff) as u8 + } + + pub fn device(&self) -> u8 { + ((self.0 >> 3) & 0x1f) as u8 + } + + pub fn function(&self) -> u8 { + (self.0 & 0x7) as u8 + } + + pub fn new(segment: u16, bus: u8, device: u8, function: u8) -> Self { + Self( + (segment as u32) << 16 + | (bus as u32) << 8 + | ((device & 0x1f) as u32) << 3 + | (function & 0x7) as u32, + ) + } +} + +impl From for PciBdf { + fn from(bdf: u32) -> Self { + Self(bdf) + } +} + +impl From for u32 { + fn from(bdf: PciBdf) -> Self { + bdf.0 + } +} + +impl From<&PciBdf> for u32 { + fn from(bdf: &PciBdf) -> Self { + bdf.0 + } +} + +impl From for u16 { + fn from(bdf: PciBdf) -> Self { + (bdf.0 & 0xffff) as u16 + } +} + +impl From<&PciBdf> for u16 { + fn from(bdf: &PciBdf) -> Self { + (bdf.0 & 0xffff) as u16 + } +} + +impl Display for PciBdf { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{:04x}:{:02x}:{:02x}.{:01x}", + self.segment(), + self.bus(), + self.device(), + self.function() + ) + } +} + +impl FromStr for PciBdf { + type Err = ParseIntError; + + fn from_str(s: &str) -> Result { + let items: Vec<&str> = s.split('.').collect(); + assert_eq!(items.len(), 2); + let function = u8::from_str_radix(items[1], 16)?; + let items: Vec<&str> = items[0].split(':').collect(); + assert_eq!(items.len(), 3); + let segment = u16::from_str_radix(items[0], 16)?; + let bus = u8::from_str_radix(items[1], 16)?; + let device = u8::from_str_radix(items[2], 16)?; + Ok(PciBdf::new(segment, bus, device, function)) + } +} + +impl From<&str> for PciBdf { + fn from(bdf: &str) -> Self { + Self::from_str(bdf).unwrap() + } +} diff --git a/vendor/cube-hypervisor/pci/src/msi.rs b/vendor/cube-hypervisor/pci/src/msi.rs new file mode 100644 index 0000000..f383e79 --- /dev/null +++ b/vendor/cube-hypervisor/pci/src/msi.rs @@ -0,0 +1,298 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// + +use anyhow::anyhow; +use byteorder::{ByteOrder, LittleEndian}; +use serde::{Deserialize, Serialize}; +use std::io; +use std::sync::Arc; +use thiserror::Error; +use vm_device::interrupt::{ + InterruptIndex, InterruptSourceConfig, InterruptSourceGroup, MsiIrqSourceConfig, +}; +use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable}; + +// MSI control masks +const MSI_CTL_ENABLE: u16 = 0x1; +const MSI_CTL_MULTI_MSG_ENABLE: u16 = 0x70; +const MSI_CTL_64_BITS: u16 = 0x80; +const MSI_CTL_PER_VECTOR: u16 = 0x100; + +// MSI message offsets +const MSI_MSG_CTL_OFFSET: u64 = 0x2; +const MSI_MSG_ADDR_LO_OFFSET: u64 = 0x4; + +// MSI message masks +const MSI_MSG_ADDR_LO_MASK: u32 = 0xffff_fffc; + +pub fn msi_num_enabled_vectors(msg_ctl: u16) -> usize { + let field = (msg_ctl >> 4) & 0x7; + + if field > 5 { + return 0; + } + + 1 << field +} + +#[derive(Error, Debug)] +enum Error { + #[error("Failed enabling the interrupt route: {0}")] + EnableInterruptRoute(io::Error), + #[error("Failed updating the interrupt route: {0}")] + UpdateInterruptRoute(io::Error), +} + +#[derive(Clone, Copy, Default, Serialize, Deserialize)] +pub struct MsiCap { + // Message Control Register + // 0: MSI enable. + // 3-1; Multiple message capable. + // 6-4: Multiple message enable. + // 7: 64 bits address capable. + // 8: Per-vector masking capable. + // 15-9: Reserved. + pub msg_ctl: u16, + // Message Address (LSB) + // 1-0: Reserved. + // 31-2: Message address. + pub msg_addr_lo: u32, + // Message Upper Address (MSB) + // 31-0: Message address. + pub msg_addr_hi: u32, + // Message Data + // 15-0: Message data. + pub msg_data: u16, + // Mask Bits + // 31-0: Mask bits. + pub mask_bits: u32, + // Pending Bits + // 31-0: Pending bits. + pub pending_bits: u32, +} + +impl MsiCap { + fn addr_64_bits(&self) -> bool { + self.msg_ctl & MSI_CTL_64_BITS == MSI_CTL_64_BITS + } + + fn per_vector_mask(&self) -> bool { + self.msg_ctl & MSI_CTL_PER_VECTOR == MSI_CTL_PER_VECTOR + } + + fn enabled(&self) -> bool { + self.msg_ctl & MSI_CTL_ENABLE == MSI_CTL_ENABLE + } + + fn num_enabled_vectors(&self) -> usize { + msi_num_enabled_vectors(self.msg_ctl) + } + + fn vector_masked(&self, vector: usize) -> bool { + if !self.per_vector_mask() { + return false; + } + + (self.mask_bits >> vector) & 0x1 == 0x1 + } + + fn size(&self) -> u64 { + let mut size: u64 = 0xa; + + if self.addr_64_bits() { + size += 0x4; + } + if self.per_vector_mask() { + size += 0xa; + } + + size + } + + fn update(&mut self, offset: u64, data: &[u8]) { + // Calculate message data offset depending on the address being 32 or + // 64 bits. + // Calculate upper address offset if the address is 64 bits. + // Calculate mask bits offset based on the address being 32 or 64 bits + // and based on the per vector masking being enabled or not. + let (msg_data_offset, addr_hi_offset, mask_bits_offset): (u64, Option, Option) = + if self.addr_64_bits() { + let mask_bits = if self.per_vector_mask() { + Some(0x10) + } else { + None + }; + (0xc, Some(0x8), mask_bits) + } else { + let mask_bits = if self.per_vector_mask() { + Some(0xc) + } else { + None + }; + (0x8, None, mask_bits) + }; + + // Update cache without overriding the read-only bits. + match data.len() { + 2 => { + let value = LittleEndian::read_u16(data); + match offset { + MSI_MSG_CTL_OFFSET => { + self.msg_ctl = (self.msg_ctl & !(MSI_CTL_ENABLE | MSI_CTL_MULTI_MSG_ENABLE)) + | (value & (MSI_CTL_ENABLE | MSI_CTL_MULTI_MSG_ENABLE)) + } + x if x == msg_data_offset => self.msg_data = value, + _ => error!("invalid offset"), + } + } + 4 => { + let value = LittleEndian::read_u32(data); + match offset { + 0x0 => { + self.msg_ctl = (self.msg_ctl & !(MSI_CTL_ENABLE | MSI_CTL_MULTI_MSG_ENABLE)) + | ((value >> 16) as u16 & (MSI_CTL_ENABLE | MSI_CTL_MULTI_MSG_ENABLE)) + } + MSI_MSG_ADDR_LO_OFFSET => self.msg_addr_lo = value & MSI_MSG_ADDR_LO_MASK, + x if x == msg_data_offset => self.msg_data = value as u16, + x if addr_hi_offset.is_some() && x == addr_hi_offset.unwrap() => { + self.msg_addr_hi = value + } + x if mask_bits_offset.is_some() && x == mask_bits_offset.unwrap() => { + self.mask_bits = value + } + _ => error!("invalid offset"), + } + } + _ => error!("invalid data length"), + } + } +} + +#[derive(Serialize, Deserialize)] +struct MsiConfigState { + cap: MsiCap, +} + +pub struct MsiConfig { + pub cap: MsiCap, + interrupt_source_group: Arc, +} + +impl MsiConfig { + pub fn new(msg_ctl: u16, interrupt_source_group: Arc) -> Self { + let cap = MsiCap { + msg_ctl, + ..Default::default() + }; + + MsiConfig { + cap, + interrupt_source_group, + } + } + + fn state(&self) -> MsiConfigState { + MsiConfigState { cap: self.cap } + } + + fn set_state(&mut self, state: &MsiConfigState) -> Result<(), Error> { + self.cap = state.cap; + + if self.enabled() { + for idx in 0..self.num_enabled_vectors() { + let config = MsiIrqSourceConfig { + high_addr: self.cap.msg_addr_hi, + low_addr: self.cap.msg_addr_lo, + data: self.cap.msg_data as u32, + devid: 0, + }; + + self.interrupt_source_group + .update( + idx as InterruptIndex, + InterruptSourceConfig::MsiIrq(config), + self.cap.vector_masked(idx), + ) + .map_err(Error::UpdateInterruptRoute)?; + } + + self.interrupt_source_group + .enable() + .map_err(Error::EnableInterruptRoute)?; + } + + Ok(()) + } + + pub fn enabled(&self) -> bool { + self.cap.enabled() + } + + pub fn size(&self) -> u64 { + self.cap.size() + } + + pub fn num_enabled_vectors(&self) -> usize { + self.cap.num_enabled_vectors() + } + + pub fn update(&mut self, offset: u64, data: &[u8]) { + let old_enabled = self.cap.enabled(); + + self.cap.update(offset, data); + + if self.cap.enabled() { + for idx in 0..self.num_enabled_vectors() { + let config = MsiIrqSourceConfig { + high_addr: self.cap.msg_addr_hi, + low_addr: self.cap.msg_addr_lo, + data: self.cap.msg_data as u32, + devid: 0, + }; + + if let Err(e) = self.interrupt_source_group.update( + idx as InterruptIndex, + InterruptSourceConfig::MsiIrq(config), + self.cap.vector_masked(idx), + ) { + error!("Failed updating vector: {:?}", e); + } + } + + if !old_enabled { + if let Err(e) = self.interrupt_source_group.enable() { + error!("Failed enabling irq_fd: {:?}", e); + } + } + } else if old_enabled { + if let Err(e) = self.interrupt_source_group.disable() { + error!("Failed disabling irq_fd: {:?}", e); + } + } + } +} + +impl Pausable for MsiConfig {} + +impl Snapshottable for MsiConfig { + fn id(&self) -> String { + String::from("msi_config") + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id(), &self.state()) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + self.set_state(&snapshot.to_state(&self.id())?) + .map_err(|e| { + MigratableError::Restore(anyhow!( + "Could not restore state for {}: {:?}", + self.id(), + e + )) + }) + } +} diff --git a/vendor/cube-hypervisor/pci/src/msix.rs b/vendor/cube-hypervisor/pci/src/msix.rs new file mode 100644 index 0000000..909fa67 --- /dev/null +++ b/vendor/cube-hypervisor/pci/src/msix.rs @@ -0,0 +1,579 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// + +use crate::{PciCapability, PciCapabilityId}; +use anyhow::anyhow; +use byteorder::{ByteOrder, LittleEndian}; +use serde::{Deserialize, Serialize}; +use std::io; +use std::result; +use std::sync::Arc; +use vm_device::interrupt::{ + InterruptIndex, InterruptSourceConfig, InterruptSourceGroup, MsiIrqSourceConfig, +}; +use vm_memory::ByteValued; +use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable}; + +const MAX_MSIX_VECTORS_PER_DEVICE: u16 = 2048; +const MSIX_TABLE_ENTRIES_MODULO: u64 = 16; +const MSIX_PBA_ENTRIES_MODULO: u64 = 8; +const BITS_PER_PBA_ENTRY: usize = 64; +const FUNCTION_MASK_BIT: u8 = 14; +const MSIX_ENABLE_BIT: u8 = 15; +const FUNCTION_MASK_MASK: u16 = (1 << FUNCTION_MASK_BIT) as u16; +const MSIX_ENABLE_MASK: u16 = (1 << MSIX_ENABLE_BIT) as u16; +pub const MSIX_TABLE_ENTRY_SIZE: usize = 16; + +#[derive(Debug)] +pub enum Error { + /// Failed enabling the interrupt route. + EnableInterruptRoute(io::Error), + /// Failed updating the interrupt route. + UpdateInterruptRoute(io::Error), +} + +#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] +pub struct MsixTableEntry { + pub msg_addr_lo: u32, + pub msg_addr_hi: u32, + pub msg_data: u32, + pub vector_ctl: u32, +} + +impl MsixTableEntry { + pub fn masked(&self) -> bool { + self.vector_ctl & 0x1 == 0x1 + } +} + +impl Default for MsixTableEntry { + fn default() -> Self { + MsixTableEntry { + msg_addr_lo: 0, + msg_addr_hi: 0, + msg_data: 0, + vector_ctl: 0x1, + } + } +} + +#[derive(Serialize, Deserialize)] +struct MsixConfigState { + table_entries: Vec, + pba_entries: Vec, + masked: bool, + enabled: bool, +} + +pub struct MsixConfig { + pub table_entries: Vec, + pub pba_entries: Vec, + pub devid: u32, + interrupt_source_group: Arc, + masked: bool, + enabled: bool, +} + +impl MsixConfig { + pub fn new( + msix_vectors: u16, + interrupt_source_group: Arc, + devid: u32, + ) -> Self { + assert!(msix_vectors <= MAX_MSIX_VECTORS_PER_DEVICE); + + let mut table_entries: Vec = Vec::new(); + table_entries.resize_with(msix_vectors as usize, Default::default); + let mut pba_entries: Vec = Vec::new(); + let num_pba_entries: usize = ((msix_vectors as usize) / BITS_PER_PBA_ENTRY) + 1; + pba_entries.resize_with(num_pba_entries, Default::default); + + MsixConfig { + table_entries, + pba_entries, + devid, + interrupt_source_group, + masked: true, + enabled: false, + } + } + + fn state(&self) -> MsixConfigState { + MsixConfigState { + table_entries: self.table_entries.clone(), + pba_entries: self.pba_entries.clone(), + masked: self.masked, + enabled: self.enabled, + } + } + + fn set_state(&mut self, state: &MsixConfigState) -> result::Result<(), Error> { + self.table_entries = state.table_entries.clone(); + self.pba_entries = state.pba_entries.clone(); + self.masked = state.masked; + self.enabled = state.enabled; + + if self.enabled && !self.masked { + for (idx, table_entry) in self.table_entries.iter().enumerate() { + if table_entry.masked() { + continue; + } + + let config = MsiIrqSourceConfig { + high_addr: table_entry.msg_addr_hi, + low_addr: table_entry.msg_addr_lo, + data: table_entry.msg_data, + devid: self.devid, + }; + + self.interrupt_source_group + .update( + idx as InterruptIndex, + InterruptSourceConfig::MsiIrq(config), + self.masked, + ) + .map_err(Error::UpdateInterruptRoute)?; + + self.interrupt_source_group + .enable() + .map_err(Error::EnableInterruptRoute)?; + } + } + + Ok(()) + } + + pub fn masked(&self) -> bool { + self.masked + } + + pub fn enabled(&self) -> bool { + self.enabled + } + + pub fn set_msg_ctl(&mut self, reg: u16) { + let old_masked = self.masked; + let old_enabled = self.enabled; + + self.masked = ((reg >> FUNCTION_MASK_BIT) & 1u16) == 1u16; + self.enabled = ((reg >> MSIX_ENABLE_BIT) & 1u16) == 1u16; + + // Update interrupt routing + if old_masked != self.masked || old_enabled != self.enabled { + if self.enabled && !self.masked { + debug!("MSI-X enabled for device 0x{:x}", self.devid); + for (idx, table_entry) in self.table_entries.iter().enumerate() { + let config = MsiIrqSourceConfig { + high_addr: table_entry.msg_addr_hi, + low_addr: table_entry.msg_addr_lo, + data: table_entry.msg_data, + devid: self.devid, + }; + + if let Err(e) = self.interrupt_source_group.update( + idx as InterruptIndex, + InterruptSourceConfig::MsiIrq(config), + table_entry.masked(), + ) { + error!("Failed updating vector: {:?}", e); + } + } + } else if old_enabled || !old_masked { + debug!("MSI-X disabled for device 0x{:x}", self.devid); + if let Err(e) = self.interrupt_source_group.disable() { + error!("Failed disabling irq_fd: {:?}", e); + } + } + } + + // If the Function Mask bit was set, and has just been cleared, it's + // important to go through the entire PBA to check if there was any + // pending MSI-X message to inject, given that the vector is not + // masked. + if old_masked && !self.masked { + for (index, entry) in self.table_entries.clone().iter().enumerate() { + if !entry.masked() && self.get_pba_bit(index as u16) == 1 { + self.inject_msix_and_clear_pba(index); + } + } + } + } + + pub fn read_table(&self, offset: u64, data: &mut [u8]) { + if data.len() != 4 && data.len() != 8 { + error!( + "invalid MSI-X table read size: {} (allowed: 4 or 8)", + data.len() + ); + data.fill(0xff); + return; + } + + let index: usize = (offset / MSIX_TABLE_ENTRIES_MODULO) as usize; + let modulo_offset = offset % MSIX_TABLE_ENTRIES_MODULO; + + if index >= self.table_entries.len() { + debug!("Invalid MSI-X table entry index {index}"); + data.copy_from_slice(&[0xff; 8][..data.len()]); + return; + } + + match data.len() { + 4 => { + let value = match modulo_offset { + 0x0 => self.table_entries[index].msg_addr_lo, + 0x4 => self.table_entries[index].msg_addr_hi, + 0x8 => self.table_entries[index].msg_data, + 0xc => self.table_entries[index].vector_ctl, + _ => { + error!("invalid offset"); + 0 + } + }; + + debug!("MSI_R TABLE offset 0x{:x} data 0x{:x}", offset, value); + LittleEndian::write_u32(data, value); + } + 8 => { + let value = match modulo_offset { + 0x0 => { + (u64::from(self.table_entries[index].msg_addr_hi) << 32) + | u64::from(self.table_entries[index].msg_addr_lo) + } + 0x8 => { + (u64::from(self.table_entries[index].vector_ctl) << 32) + | u64::from(self.table_entries[index].msg_data) + } + _ => { + error!("invalid offset"); + 0 + } + }; + + debug!("MSI_R TABLE offset 0x{:x} data 0x{:x}", offset, value); + LittleEndian::write_u64(data, value); + } + _ => { + error!("invalid data length"); + } + } + } + + pub fn write_table(&mut self, offset: u64, data: &[u8]) { + if data.len() != 4 && data.len() != 8 { + error!( + "invalid MSI-X table write size: {} (allowed: 4 or 8)", + data.len() + ); + return; + } + + let index: usize = (offset / MSIX_TABLE_ENTRIES_MODULO) as usize; + let modulo_offset = offset % MSIX_TABLE_ENTRIES_MODULO; + + if index >= self.table_entries.len() { + debug!("Invalid MSI-X table entry index {index}"); + return; + } + + // Store the value of the entry before modification + let old_entry = self.table_entries[index].clone(); + + match data.len() { + 4 => { + let value = LittleEndian::read_u32(data); + match modulo_offset { + 0x0 => self.table_entries[index].msg_addr_lo = value, + 0x4 => self.table_entries[index].msg_addr_hi = value, + 0x8 => self.table_entries[index].msg_data = value, + 0xc => { + self.table_entries[index].vector_ctl = value; + } + _ => error!("invalid offset"), + }; + + debug!("MSI_W TABLE offset 0x{:x} data 0x{:x}", offset, value); + } + 8 => { + let value = LittleEndian::read_u64(data); + match modulo_offset { + 0x0 => { + self.table_entries[index].msg_addr_lo = (value & 0xffff_ffffu64) as u32; + self.table_entries[index].msg_addr_hi = (value >> 32) as u32; + } + 0x8 => { + self.table_entries[index].msg_data = (value & 0xffff_ffffu64) as u32; + self.table_entries[index].vector_ctl = (value >> 32) as u32; + } + _ => error!("invalid offset"), + }; + + debug!("MSI_W TABLE offset 0x{:x} data 0x{:x}", offset, value); + } + _ => error!("invalid data length"), + }; + + let table_entry = &self.table_entries[index]; + + // Optimisation to avoid excessive updates + if &old_entry == table_entry { + return; + } + + // Update interrupt routes + // Optimisation: only update routes if the entry is not masked; + // this is safe because if the entry is masked (starts masked as per spec) + // in the table then it won't be triggered. (See: #4273) + if self.enabled && !self.masked && !table_entry.masked() { + let config = MsiIrqSourceConfig { + high_addr: table_entry.msg_addr_hi, + low_addr: table_entry.msg_addr_lo, + data: table_entry.msg_data, + devid: self.devid, + }; + + if let Err(e) = self.interrupt_source_group.update( + index as InterruptIndex, + InterruptSourceConfig::MsiIrq(config), + table_entry.masked(), + ) { + error!("Failed updating vector: {:?}", e); + } + } + + // After the MSI-X table entry has been updated, it is necessary to + // check if the vector control masking bit has changed. In case the + // bit has been flipped from 1 to 0, we need to inject a MSI message + // if the corresponding pending bit from the PBA is set. Once the MSI + // has been injected, the pending bit in the PBA needs to be cleared. + // All of this is valid only if MSI-X has not been masked for the whole + // device. + + // Check if bit has been flipped + if !self.masked() + && self.enabled() + && old_entry.masked() + && !table_entry.masked() + && self.get_pba_bit(index as u16) == 1 + { + self.inject_msix_and_clear_pba(index); + } + } + + pub fn read_pba(&mut self, offset: u64, data: &mut [u8]) { + if data.len() != 4 && data.len() != 8 { + error!( + "invalid MSI-X PBA read size: {} (allowed: 4 or 8)", + data.len() + ); + data.fill(0xff); + return; + } + + let index: usize = (offset / MSIX_PBA_ENTRIES_MODULO) as usize; + let modulo_offset = offset % MSIX_PBA_ENTRIES_MODULO; + + if index >= self.pba_entries.len() { + debug!("Invalid MSI-X PBA entry index {index}"); + data.copy_from_slice(&[0xff; 8][..data.len()]); + return; + } + + match data.len() { + 4 => { + let value: u32 = match modulo_offset { + 0x0 => (self.pba_entries[index] & 0xffff_ffffu64) as u32, + 0x4 => (self.pba_entries[index] >> 32) as u32, + _ => { + error!("invalid offset"); + 0 + } + }; + + debug!("MSI_R PBA offset 0x{:x} data 0x{:x}", offset, value); + LittleEndian::write_u32(data, value); + } + 8 => { + let value: u64 = match modulo_offset { + 0x0 => self.pba_entries[index], + _ => { + error!("invalid offset"); + 0 + } + }; + + debug!("MSI_R PBA offset 0x{:x} data 0x{:x}", offset, value); + LittleEndian::write_u64(data, value); + } + _ => { + error!("invalid data length"); + } + } + } + + pub fn write_pba(&mut self, _offset: u64, _data: &[u8]) { + error!("Pending Bit Array is read only"); + } + + pub fn set_pba_bit(&mut self, vector: u16, reset: bool) { + assert!(vector < MAX_MSIX_VECTORS_PER_DEVICE); + + let index: usize = (vector as usize) / BITS_PER_PBA_ENTRY; + let shift: usize = (vector as usize) % BITS_PER_PBA_ENTRY; + let mut mask: u64 = (1 << shift) as u64; + + if reset { + mask = !mask; + self.pba_entries[index] &= mask; + } else { + self.pba_entries[index] |= mask; + } + } + + fn get_pba_bit(&self, vector: u16) -> u8 { + assert!(vector < MAX_MSIX_VECTORS_PER_DEVICE); + + let index: usize = (vector as usize) / BITS_PER_PBA_ENTRY; + let shift: usize = (vector as usize) % BITS_PER_PBA_ENTRY; + + ((self.pba_entries[index] >> shift) & 0x0000_0001u64) as u8 + } + + fn inject_msix_and_clear_pba(&mut self, vector: usize) { + // Inject the MSI message + match self + .interrupt_source_group + .trigger(vector as InterruptIndex) + { + Ok(_) => debug!("MSI-X injected on vector control flip"), + Err(e) => error!("failed to inject MSI-X: {}", e), + } + + // Clear the bit from PBA + self.set_pba_bit(vector as u16, true); + } +} + +impl Pausable for MsixConfig {} + +impl Snapshottable for MsixConfig { + fn id(&self) -> String { + String::from("msix_config") + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id(), &self.state()) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + self.set_state(&snapshot.to_state(&self.id())?) + .map_err(|e| { + MigratableError::Restore(anyhow!( + "Could not restore state for {}: {:?}", + self.id(), + e + )) + }) + } +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Clone, Copy, Default, Serialize, Deserialize)] +pub struct MsixCap { + // Message Control Register + // 10-0: MSI-X Table size + // 13-11: Reserved + // 14: Mask. Mask all MSI-X when set. + // 15: Enable. Enable all MSI-X when set. + pub msg_ctl: u16, + // Table. Contains the offset and the BAR indicator (BIR) + // 2-0: Table BAR indicator (BIR). Can be 0 to 5. + // 31-3: Table offset in the BAR pointed by the BIR. + pub table: u32, + // Pending Bit Array. Contains the offset and the BAR indicator (BIR) + // 2-0: PBA BAR indicator (BIR). Can be 0 to 5. + // 31-3: PBA offset in the BAR pointed by the BIR. + pub pba: u32, +} + +// SAFETY: All members are simple numbers and any value is valid. +unsafe impl ByteValued for MsixCap {} + +impl PciCapability for MsixCap { + fn bytes(&self) -> &[u8] { + self.as_slice() + } + + fn id(&self) -> PciCapabilityId { + PciCapabilityId::MsiX + } +} + +impl MsixCap { + pub fn new( + table_pci_bar: u8, + table_size: u16, + table_off: u32, + pba_pci_bar: u8, + pba_off: u32, + ) -> Self { + assert!(table_size < MAX_MSIX_VECTORS_PER_DEVICE); + + // Set the table size and enable MSI-X. + let msg_ctl: u16 = 0x8000u16 + table_size - 1; + + MsixCap { + msg_ctl, + table: (table_off & 0xffff_fff8u32) | u32::from(table_pci_bar & 0x7u8), + pba: (pba_off & 0xffff_fff8u32) | u32::from(pba_pci_bar & 0x7u8), + } + } + + pub fn set_msg_ctl(&mut self, data: u16) { + self.msg_ctl = (self.msg_ctl & !(FUNCTION_MASK_MASK | MSIX_ENABLE_MASK)) + | (data & (FUNCTION_MASK_MASK | MSIX_ENABLE_MASK)); + } + + pub fn masked(&self) -> bool { + (self.msg_ctl >> FUNCTION_MASK_BIT) & 0x1 == 0x1 + } + + pub fn enabled(&self) -> bool { + (self.msg_ctl >> MSIX_ENABLE_BIT) & 0x1 == 0x1 + } + + pub fn table_offset(&self) -> u32 { + self.table & 0xffff_fff8 + } + + pub fn pba_offset(&self) -> u32 { + self.pba & 0xffff_fff8 + } + + pub fn table_bir(&self) -> u32 { + self.table & 0x7 + } + + pub fn pba_bir(&self) -> u32 { + self.pba & 0x7 + } + + pub fn table_size(&self) -> u16 { + (self.msg_ctl & 0x7ff) + 1 + } + + pub fn table_range(&self) -> (u64, u64) { + // The table takes 16 bytes per entry. + let size = self.table_size() as u64 * 16; + (self.table_offset() as u64, size) + } + + pub fn pba_range(&self) -> (u64, u64) { + // The table takes 1 bit per entry modulo 8 bytes. + let size = ((self.table_size() as u64 / 64) + 1) * 8; + (self.pba_offset() as u64, size) + } +} diff --git a/vendor/cube-hypervisor/pci/src/vfio.rs b/vendor/cube-hypervisor/pci/src/vfio.rs new file mode 100644 index 0000000..a0a9336 --- /dev/null +++ b/vendor/cube-hypervisor/pci/src/vfio.rs @@ -0,0 +1,1720 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// + +use crate::{ + msi_num_enabled_vectors, BarReprogrammingParams, MsiCap, MsiConfig, MsixCap, MsixConfig, + PciBarConfiguration, PciBarPrefetchable, PciBarRegionType, PciBdf, PciCapabilityId, + PciClassCode, PciConfiguration, PciDevice, PciDeviceError, PciExpressCapabilityId, + PciHeaderType, PciSubclass, MSIX_TABLE_ENTRY_SIZE, +}; +use anyhow::anyhow; +use byteorder::{ByteOrder, LittleEndian}; +use hypervisor::HypervisorVmError; +use serde::{Deserialize, Serialize}; +use std::any::Any; +use std::collections::{BTreeMap, HashMap}; +use std::io; +use std::os::unix::io::AsRawFd; +use std::ptr::null_mut; +use std::sync::{Arc, Barrier, Mutex}; +use thiserror::Error; +use vfio_bindings::bindings::vfio::*; +use vfio_ioctls::{ + VfioContainer, VfioDevice, VfioIrq, VfioRegionInfoCap, VfioRegionSparseMmapArea, +}; +use vm_allocator::{AddressAllocator, SystemAllocator}; +use vm_device::interrupt::{ + InterruptIndex, InterruptManager, InterruptSourceGroup, MsiIrqGroupConfig, +}; +use vm_device::{BusDevice, Resource}; +use vm_memory::{Address, GuestAddress, GuestUsize}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vmm_sys_util::eventfd::EventFd; + +#[derive(Debug, Error)] +pub enum VfioPciError { + #[error("Failed to create user memory region: {0}")] + CreateUserMemoryRegion(#[source] HypervisorVmError), + #[error("Failed to DMA map: {0}")] + DmaMap(#[source] vfio_ioctls::VfioError), + #[error("Failed to DMA unmap: {0}")] + DmaUnmap(#[source] vfio_ioctls::VfioError), + #[error("Failed to enable INTx: {0}")] + EnableIntx(#[source] VfioError), + #[error("Failed to enable MSI: {0}")] + EnableMsi(#[source] VfioError), + #[error("Failed to enable MSI-x: {0}")] + EnableMsix(#[source] VfioError), + #[error("Failed to mmap the area")] + MmapArea, + #[error("Failed to notifier's eventfd")] + MissingNotifier, + #[error("Invalid region alignment")] + RegionAlignment, + #[error("Invalid region size")] + RegionSize, +} + +#[derive(Copy, Clone)] +enum PciVfioSubclass { + VfioSubclass = 0xff, +} + +impl PciSubclass for PciVfioSubclass { + fn get_register_value(&self) -> u8 { + *self as u8 + } +} + +enum InterruptUpdateAction { + EnableMsi, + DisableMsi, + EnableMsix, + DisableMsix, +} + +#[derive(Serialize, Deserialize)] +struct IntxState { + enabled: bool, +} + +pub(crate) struct VfioIntx { + interrupt_source_group: Arc, + enabled: bool, +} + +#[derive(Serialize, Deserialize)] +struct MsiState { + cap: MsiCap, + cap_offset: u32, +} + +pub(crate) struct VfioMsi { + pub(crate) cfg: MsiConfig, + cap_offset: u32, + interrupt_source_group: Arc, +} + +impl VfioMsi { + fn update(&mut self, offset: u64, data: &[u8]) -> Option { + let old_enabled = self.cfg.enabled(); + + self.cfg.update(offset, data); + + let new_enabled = self.cfg.enabled(); + + if !old_enabled && new_enabled { + return Some(InterruptUpdateAction::EnableMsi); + } + + if old_enabled && !new_enabled { + return Some(InterruptUpdateAction::DisableMsi); + } + + None + } +} + +#[derive(Serialize, Deserialize)] +struct MsixState { + cap: MsixCap, + cap_offset: u32, + bdf: u32, +} + +pub(crate) struct VfioMsix { + pub(crate) bar: MsixConfig, + cap: MsixCap, + cap_offset: u32, + interrupt_source_group: Arc, +} + +impl VfioMsix { + fn update(&mut self, offset: u64, data: &[u8]) -> Option { + let old_enabled = self.bar.enabled(); + + // Update "Message Control" word + if offset == 2 && data.len() == 2 { + self.bar.set_msg_ctl(LittleEndian::read_u16(data)); + } + + let new_enabled = self.bar.enabled(); + + if !old_enabled && new_enabled { + return Some(InterruptUpdateAction::EnableMsix); + } + + if old_enabled && !new_enabled { + return Some(InterruptUpdateAction::DisableMsix); + } + + None + } + + fn table_accessed(&self, bar_index: u32, offset: u64) -> bool { + let table_offset: u64 = u64::from(self.cap.table_offset()); + let table_size: u64 = u64::from(self.cap.table_size()) * (MSIX_TABLE_ENTRY_SIZE as u64); + let table_bir: u32 = self.cap.table_bir(); + + bar_index == table_bir && offset >= table_offset && offset < table_offset + table_size + } +} + +pub(crate) struct Interrupt { + pub(crate) intx: Option, + pub(crate) msi: Option, + pub(crate) msix: Option, +} + +impl Interrupt { + fn update_msi(&mut self, offset: u64, data: &[u8]) -> Option { + if let Some(ref mut msi) = &mut self.msi { + let action = msi.update(offset, data); + return action; + } + + None + } + + fn update_msix(&mut self, offset: u64, data: &[u8]) -> Option { + if let Some(ref mut msix) = &mut self.msix { + let action = msix.update(offset, data); + return action; + } + + None + } + + fn accessed(&self, offset: u64) -> Option<(PciCapabilityId, u64)> { + if let Some(msi) = &self.msi { + if offset >= u64::from(msi.cap_offset) + && offset < u64::from(msi.cap_offset) + msi.cfg.size() + { + return Some(( + PciCapabilityId::MessageSignalledInterrupts, + u64::from(msi.cap_offset), + )); + } + } + + if let Some(msix) = &self.msix { + if offset == u64::from(msix.cap_offset) { + return Some((PciCapabilityId::MsiX, u64::from(msix.cap_offset))); + } + } + + None + } + + fn msix_table_accessed(&self, bar_index: u32, offset: u64) -> bool { + if let Some(msix) = &self.msix { + return msix.table_accessed(bar_index, offset); + } + + false + } + + fn msix_write_table(&mut self, offset: u64, data: &[u8]) { + if let Some(ref mut msix) = &mut self.msix { + let offset = offset - u64::from(msix.cap.table_offset()); + msix.bar.write_table(offset, data) + } + } + + fn msix_read_table(&self, offset: u64, data: &mut [u8]) { + if let Some(msix) = &self.msix { + let offset = offset - u64::from(msix.cap.table_offset()); + msix.bar.read_table(offset, data) + } + } + + pub(crate) fn intx_in_use(&self) -> bool { + if let Some(intx) = &self.intx { + return intx.enabled; + } + + false + } +} + +#[derive(Copy, Clone)] +pub struct UserMemoryRegion { + pub slot: u32, + pub start: u64, + pub size: u64, + pub host_addr: u64, +} + +#[derive(Clone)] +pub struct MmioRegion { + pub start: GuestAddress, + pub length: GuestUsize, + pub(crate) type_: PciBarRegionType, + pub(crate) index: u32, + pub(crate) user_memory_regions: Vec, +} +#[derive(Debug, Error)] +pub enum VfioError { + #[error("Kernel VFIO error: {0}")] + KernelVfio(#[source] vfio_ioctls::VfioError), + #[error("VFIO user error: {0}")] + VfioUser(#[source] vfio_user::Error), +} + +pub(crate) trait Vfio: Send + Sync { + fn read_config_byte(&self, offset: u32) -> u8 { + let mut data: [u8; 1] = [0]; + self.read_config(offset, &mut data); + data[0] + } + + fn read_config_word(&self, offset: u32) -> u16 { + let mut data: [u8; 2] = [0, 0]; + self.read_config(offset, &mut data); + u16::from_le_bytes(data) + } + + fn read_config_dword(&self, offset: u32) -> u32 { + let mut data: [u8; 4] = [0, 0, 0, 0]; + self.read_config(offset, &mut data); + u32::from_le_bytes(data) + } + + fn write_config_dword(&self, offset: u32, buf: u32) { + let data: [u8; 4] = buf.to_le_bytes(); + self.write_config(offset, &data) + } + + fn read_config(&self, offset: u32, data: &mut [u8]) { + self.region_read(VFIO_PCI_CONFIG_REGION_INDEX, offset.into(), data.as_mut()); + } + + fn write_config(&self, offset: u32, data: &[u8]) { + self.region_write(VFIO_PCI_CONFIG_REGION_INDEX, offset.into(), data) + } + + fn enable_msi(&self, fds: Vec<&EventFd>) -> Result<(), VfioError> { + self.enable_irq(VFIO_PCI_MSI_IRQ_INDEX, fds) + } + + fn disable_msi(&self) -> Result<(), VfioError> { + self.disable_irq(VFIO_PCI_MSI_IRQ_INDEX) + } + + fn enable_msix(&self, fds: Vec<&EventFd>) -> Result<(), VfioError> { + self.enable_irq(VFIO_PCI_MSIX_IRQ_INDEX, fds) + } + + fn disable_msix(&self) -> Result<(), VfioError> { + self.disable_irq(VFIO_PCI_MSIX_IRQ_INDEX) + } + + fn region_read(&self, _index: u32, _offset: u64, _data: &mut [u8]) { + unimplemented!() + } + + fn region_write(&self, _index: u32, _offset: u64, _data: &[u8]) { + unimplemented!() + } + + fn get_irq_info(&self, _irq_index: u32) -> Option { + unimplemented!() + } + + fn enable_irq(&self, _irq_index: u32, _event_fds: Vec<&EventFd>) -> Result<(), VfioError> { + unimplemented!() + } + + fn disable_irq(&self, _irq_index: u32) -> Result<(), VfioError> { + unimplemented!() + } + + fn unmask_irq(&self, _irq_index: u32) -> Result<(), VfioError> { + unimplemented!() + } +} + +struct VfioDeviceWrapper { + device: Arc, +} + +impl VfioDeviceWrapper { + fn new(device: Arc) -> Self { + Self { device } + } +} + +impl Vfio for VfioDeviceWrapper { + fn region_read(&self, index: u32, offset: u64, data: &mut [u8]) { + self.device.region_read(index, data, offset) + } + + fn region_write(&self, index: u32, offset: u64, data: &[u8]) { + self.device.region_write(index, data, offset) + } + + fn get_irq_info(&self, irq_index: u32) -> Option { + self.device.get_irq_info(irq_index).copied() + } + + fn enable_irq(&self, irq_index: u32, event_fds: Vec<&EventFd>) -> Result<(), VfioError> { + self.device + .enable_irq(irq_index, event_fds) + .map_err(VfioError::KernelVfio) + } + + fn disable_irq(&self, irq_index: u32) -> Result<(), VfioError> { + self.device + .disable_irq(irq_index) + .map_err(VfioError::KernelVfio) + } + + fn unmask_irq(&self, irq_index: u32) -> Result<(), VfioError> { + self.device + .unmask_irq(irq_index) + .map_err(VfioError::KernelVfio) + } +} + +#[derive(Serialize, Deserialize)] +struct VfioCommonState { + intx_state: Option, + msi_state: Option, + msix_state: Option, +} + +pub(crate) struct ConfigPatch { + mask: u32, + patch: u32, +} + +pub(crate) struct VfioCommon { + pub(crate) configuration: PciConfiguration, + pub(crate) mmio_regions: Vec, + pub(crate) interrupt: Interrupt, + pub(crate) msi_interrupt_manager: Arc>, + pub(crate) legacy_interrupt_group: Option>, + pub(crate) vfio_wrapper: Arc, + pub(crate) patches: HashMap, +} + +impl VfioCommon { + pub(crate) fn allocate_bars( + &mut self, + allocator: &Arc>, + mmio_allocator: &mut AddressAllocator, + resources: Option>, + ) -> Result, PciDeviceError> { + let mut bars = Vec::new(); + let mut bar_id = VFIO_PCI_BAR0_REGION_INDEX; + + // Going through all regular regions to compute the BAR size. + // We're not saving the BAR address to restore it, because we + // are going to allocate a guest address for each BAR and write + // that new address back. + while bar_id < VFIO_PCI_CONFIG_REGION_INDEX { + let mut region_size: u64 = 0; + let mut region_type = PciBarRegionType::Memory32BitRegion; + let mut prefetchable = PciBarPrefetchable::NotPrefetchable; + let mut flags: u32 = 0; + + let mut restored_bar_addr = None; + if let Some(resources) = &resources { + for resource in resources { + if let Resource::PciBar { + index, + base, + size, + type_, + .. + } = resource + { + if *index == bar_id as usize { + restored_bar_addr = Some(GuestAddress(*base)); + region_size = *size; + region_type = PciBarRegionType::from(*type_); + break; + } + } + } + if restored_bar_addr.is_none() { + bar_id += 1; + continue; + } + } else { + let bar_offset = if bar_id == VFIO_PCI_ROM_REGION_INDEX { + (PCI_ROM_EXP_BAR_INDEX * 4) as u32 + } else { + PCI_CONFIG_BAR_OFFSET + bar_id * 4 + }; + + // First read flags + flags = self.vfio_wrapper.read_config_dword(bar_offset); + + // Is this an IO BAR? + let io_bar = if bar_id != VFIO_PCI_ROM_REGION_INDEX { + matches!(flags & PCI_CONFIG_IO_BAR, PCI_CONFIG_IO_BAR) + } else { + false + }; + + // Is this a 64-bit BAR? + let is_64bit_bar = if bar_id != VFIO_PCI_ROM_REGION_INDEX { + matches!( + flags & PCI_CONFIG_MEMORY_BAR_64BIT, + PCI_CONFIG_MEMORY_BAR_64BIT + ) + } else { + false + }; + + if matches!( + flags & PCI_CONFIG_BAR_PREFETCHABLE, + PCI_CONFIG_BAR_PREFETCHABLE + ) { + prefetchable = PciBarPrefetchable::Prefetchable + }; + + // To get size write all 1s + self.vfio_wrapper + .write_config_dword(bar_offset, 0xffff_ffff); + + // And read back BAR value. The device will write zeros for bits it doesn't care about + let mut lower = self.vfio_wrapper.read_config_dword(bar_offset); + + if io_bar { + // Mask flag bits (lowest 2 for I/O bars) + lower &= !0b11; + + // BAR is not enabled + if lower == 0 { + bar_id += 1; + continue; + } + + // IO BAR + region_type = PciBarRegionType::IoRegion; + + // Invert bits and add 1 to calculate size + region_size = (!lower + 1) as u64; + } else if is_64bit_bar { + // 64 bits Memory BAR + region_type = PciBarRegionType::Memory64BitRegion; + + // Query size of upper BAR of 64-bit BAR + let upper_offset: u32 = PCI_CONFIG_BAR_OFFSET + (bar_id + 1) * 4; + self.vfio_wrapper + .write_config_dword(upper_offset, 0xffff_ffff); + let upper = self.vfio_wrapper.read_config_dword(upper_offset); + + let mut combined_size = u64::from(upper) << 32 | u64::from(lower); + + // Mask out flag bits (lowest 4 for memory bars) + combined_size &= !0b1111; + + // BAR is not enabled + if combined_size == 0 { + bar_id += 1; + continue; + } + + // Invert and add 1 to to find size + region_size = !combined_size + 1; + } else { + region_type = PciBarRegionType::Memory32BitRegion; + + // Mask out flag bits (lowest 4 for memory bars) + lower &= !0b1111; + + if lower == 0 { + bar_id += 1; + continue; + } + + // Invert and add 1 to to find size + region_size = (!lower + 1) as u64; + } + } + + let bar_addr = match region_type { + PciBarRegionType::IoRegion => { + #[cfg(target_arch = "aarch64")] + unimplemented!(); + + // The address needs to be 4 bytes aligned. + #[cfg(not(target_arch = "aarch64"))] + allocator + .lock() + .unwrap() + .allocate_io_addresses(restored_bar_addr, region_size, Some(0x4)) + .ok_or(PciDeviceError::IoAllocationFailed(region_size))? + } + PciBarRegionType::Memory32BitRegion => { + // BAR allocation must be naturally aligned + allocator + .lock() + .unwrap() + .allocate_mmio_hole_addresses( + restored_bar_addr, + region_size, + Some(region_size), + ) + .ok_or(PciDeviceError::IoAllocationFailed(region_size))? + } + PciBarRegionType::Memory64BitRegion => { + // BAR allocation must be naturally aligned + mmio_allocator + .allocate(restored_bar_addr, region_size, Some(region_size)) + .ok_or(PciDeviceError::IoAllocationFailed(region_size))? + } + }; + + // We can now build our BAR configuration block. + let bar = PciBarConfiguration::default() + .set_index(bar_id as usize) + .set_address(bar_addr.raw_value()) + .set_size(region_size) + .set_region_type(region_type) + .set_prefetchable(prefetchable); + + if bar_id == VFIO_PCI_ROM_REGION_INDEX { + self.configuration + .add_pci_rom_bar(&bar, flags & 0x1) + .map_err(|e| PciDeviceError::IoRegistrationFailed(bar_addr.raw_value(), e))?; + } else { + self.configuration + .add_pci_bar(&bar) + .map_err(|e| PciDeviceError::IoRegistrationFailed(bar_addr.raw_value(), e))?; + } + + bars.push(bar); + self.mmio_regions.push(MmioRegion { + start: bar_addr, + length: region_size, + type_: region_type, + index: bar_id, + user_memory_regions: Vec::new(), + }); + + bar_id += 1; + if region_type == PciBarRegionType::Memory64BitRegion { + bar_id += 1; + } + } + + Ok(bars) + } + + pub(crate) fn free_bars( + &mut self, + allocator: &mut SystemAllocator, + mmio_allocator: &mut AddressAllocator, + ) -> Result<(), PciDeviceError> { + for region in self.mmio_regions.iter() { + match region.type_ { + PciBarRegionType::IoRegion => { + #[cfg(target_arch = "x86_64")] + allocator.free_io_addresses(region.start, region.length); + #[cfg(target_arch = "aarch64")] + error!("I/O region is not supported"); + } + PciBarRegionType::Memory32BitRegion => { + allocator.free_mmio_hole_addresses(region.start, region.length); + } + PciBarRegionType::Memory64BitRegion => { + mmio_allocator.free(region.start, region.length); + } + } + } + Ok(()) + } + + pub(crate) fn parse_msix_capabilities(&mut self, cap: u8) -> MsixCap { + let msg_ctl = self.vfio_wrapper.read_config_word((cap + 2).into()); + + let table = self.vfio_wrapper.read_config_dword((cap + 4).into()); + + let pba = self.vfio_wrapper.read_config_dword((cap + 8).into()); + + MsixCap { + msg_ctl, + table, + pba, + } + } + + pub(crate) fn initialize_msix(&mut self, msix_cap: MsixCap, cap_offset: u32, bdf: PciBdf) { + let interrupt_source_group = self + .msi_interrupt_manager + .create_group(MsiIrqGroupConfig { + base: 0, + count: msix_cap.table_size() as InterruptIndex, + }) + .unwrap(); + + let msix_config = MsixConfig::new( + msix_cap.table_size(), + interrupt_source_group.clone(), + bdf.into(), + ); + + self.interrupt.msix = Some(VfioMsix { + bar: msix_config, + cap: msix_cap, + cap_offset, + interrupt_source_group, + }); + } + + pub(crate) fn parse_msi_capabilities(&mut self, cap: u8) -> u16 { + self.vfio_wrapper.read_config_word((cap + 2).into()) + } + + pub(crate) fn initialize_msi(&mut self, msg_ctl: u16, cap_offset: u32) { + let interrupt_source_group = self + .msi_interrupt_manager + .create_group(MsiIrqGroupConfig { + base: 0, + count: msi_num_enabled_vectors(msg_ctl) as InterruptIndex, + }) + .unwrap(); + + let msi_config = MsiConfig::new(msg_ctl, interrupt_source_group.clone()); + + self.interrupt.msi = Some(VfioMsi { + cfg: msi_config, + cap_offset, + interrupt_source_group, + }); + } + + pub(crate) fn parse_capabilities(&mut self, bdf: PciBdf) { + let mut cap_next = self + .vfio_wrapper + .read_config_byte(PCI_CONFIG_CAPABILITY_OFFSET); + + let mut pci_express_cap_found = false; + let mut power_management_cap_found = false; + + while cap_next != 0 { + let cap_id = self.vfio_wrapper.read_config_byte(cap_next.into()); + + match PciCapabilityId::from(cap_id) { + PciCapabilityId::MessageSignalledInterrupts => { + if let Some(irq_info) = self.vfio_wrapper.get_irq_info(VFIO_PCI_MSI_IRQ_INDEX) { + if irq_info.count > 0 { + // Parse capability only if the VFIO device + // supports MSI. + let msg_ctl = self.parse_msi_capabilities(cap_next); + self.initialize_msi(msg_ctl, cap_next as u32); + } + } + } + PciCapabilityId::MsiX => { + if let Some(irq_info) = self.vfio_wrapper.get_irq_info(VFIO_PCI_MSIX_IRQ_INDEX) + { + if irq_info.count > 0 { + // Parse capability only if the VFIO device + // supports MSI-X. + let msix_cap = self.parse_msix_capabilities(cap_next); + self.initialize_msix(msix_cap, cap_next as u32, bdf); + } + } + } + PciCapabilityId::PciExpress => pci_express_cap_found = true, + PciCapabilityId::PowerManagement => power_management_cap_found = true, + _ => {} + }; + + cap_next = self.vfio_wrapper.read_config_byte((cap_next + 1).into()); + } + + if pci_express_cap_found && power_management_cap_found { + self.parse_extended_capabilities(); + } + } + + fn parse_extended_capabilities(&mut self) { + let mut current_offset = PCI_CONFIG_EXTENDED_CAPABILITY_OFFSET; + + loop { + let ext_cap_hdr = self.vfio_wrapper.read_config_dword(current_offset); + + let cap_id: u16 = (ext_cap_hdr & 0xffff) as u16; + let cap_next: u16 = ((ext_cap_hdr >> 20) & 0xfff) as u16; + + match PciExpressCapabilityId::from(cap_id) { + PciExpressCapabilityId::AlternativeRoutingIdentificationIntepretation + | PciExpressCapabilityId::ResizeableBar + | PciExpressCapabilityId::SingleRootIoVirtualization => { + let reg_idx = (current_offset / 4) as usize; + self.patches.insert( + reg_idx, + ConfigPatch { + mask: 0x0000_ffff, + patch: PciExpressCapabilityId::NullCapability as u32, + }, + ); + } + _ => {} + } + + if cap_next == 0 { + break; + } + + current_offset = cap_next.into(); + } + } + + pub(crate) fn enable_intx(&mut self) -> Result<(), VfioPciError> { + if let Some(intx) = &mut self.interrupt.intx { + if !intx.enabled { + if let Some(eventfd) = intx.interrupt_source_group.notifier(0) { + self.vfio_wrapper + .enable_irq(VFIO_PCI_INTX_IRQ_INDEX, vec![&eventfd]) + .map_err(VfioPciError::EnableIntx)?; + + intx.enabled = true; + } else { + return Err(VfioPciError::MissingNotifier); + } + } + } + + Ok(()) + } + + pub(crate) fn disable_intx(&mut self) { + if let Some(intx) = &mut self.interrupt.intx { + if intx.enabled { + if let Err(e) = self.vfio_wrapper.disable_irq(VFIO_PCI_INTX_IRQ_INDEX) { + error!("Could not disable INTx: {}", e); + } else { + intx.enabled = false; + } + } + } + } + + pub(crate) fn enable_msi(&self) -> Result<(), VfioPciError> { + if let Some(msi) = &self.interrupt.msi { + let mut irq_fds: Vec = Vec::new(); + for i in 0..msi.cfg.num_enabled_vectors() { + if let Some(eventfd) = msi.interrupt_source_group.notifier(i as InterruptIndex) { + irq_fds.push(eventfd); + } else { + return Err(VfioPciError::MissingNotifier); + } + } + + self.vfio_wrapper + .enable_msi(irq_fds.iter().collect()) + .map_err(VfioPciError::EnableMsi)?; + } + + Ok(()) + } + + pub(crate) fn disable_msi(&self) { + if let Err(e) = self.vfio_wrapper.disable_msi() { + error!("Could not disable MSI: {}", e); + } + } + + pub(crate) fn enable_msix(&self) -> Result<(), VfioPciError> { + if let Some(msix) = &self.interrupt.msix { + let mut irq_fds: Vec = Vec::new(); + for i in 0..msix.bar.table_entries.len() { + if let Some(eventfd) = msix.interrupt_source_group.notifier(i as InterruptIndex) { + irq_fds.push(eventfd); + } else { + return Err(VfioPciError::MissingNotifier); + } + } + + self.vfio_wrapper + .enable_msix(irq_fds.iter().collect()) + .map_err(VfioPciError::EnableMsix)?; + } + + Ok(()) + } + + pub(crate) fn disable_msix(&self) { + if let Err(e) = self.vfio_wrapper.disable_msix() { + error!("Could not disable MSI-X: {}", e); + } + } + + pub(crate) fn initialize_legacy_interrupt(&mut self) -> Result<(), VfioPciError> { + if let Some(irq_info) = self.vfio_wrapper.get_irq_info(VFIO_PCI_INTX_IRQ_INDEX) { + if irq_info.count == 0 { + // A count of 0 means the INTx IRQ is not supported, therefore + // it shouldn't be initialized. + return Ok(()); + } + } + + if let Some(interrupt_source_group) = self.legacy_interrupt_group.clone() { + self.interrupt.intx = Some(VfioIntx { + interrupt_source_group, + enabled: false, + }); + + self.enable_intx()?; + } + + Ok(()) + } + + pub(crate) fn update_msi_capabilities( + &mut self, + offset: u64, + data: &[u8], + ) -> Result<(), VfioPciError> { + match self.interrupt.update_msi(offset, data) { + Some(InterruptUpdateAction::EnableMsi) => { + // Disable INTx before we can enable MSI + self.disable_intx(); + self.enable_msi()?; + } + Some(InterruptUpdateAction::DisableMsi) => { + // Fallback onto INTx when disabling MSI + self.disable_msi(); + self.enable_intx()?; + } + _ => {} + } + + Ok(()) + } + + pub(crate) fn update_msix_capabilities( + &mut self, + offset: u64, + data: &[u8], + ) -> Result<(), VfioPciError> { + match self.interrupt.update_msix(offset, data) { + Some(InterruptUpdateAction::EnableMsix) => { + // Disable INTx before we can enable MSI-X + self.disable_intx(); + self.enable_msix()?; + } + Some(InterruptUpdateAction::DisableMsix) => { + // Fallback onto INTx when disabling MSI-X + self.disable_msix(); + self.enable_intx()?; + } + _ => {} + } + + Ok(()) + } + + pub(crate) fn find_region(&self, addr: u64) -> Option { + for region in self.mmio_regions.iter() { + if addr >= region.start.raw_value() + && addr < region.start.unchecked_add(region.length).raw_value() + { + return Some(region.clone()); + } + } + None + } + + pub(crate) fn read_bar(&mut self, base: u64, offset: u64, data: &mut [u8]) { + let addr = base + offset; + if let Some(region) = self.find_region(addr) { + let offset = addr - region.start.raw_value(); + + if self.interrupt.msix_table_accessed(region.index, offset) { + self.interrupt.msix_read_table(offset, data); + } else { + self.vfio_wrapper.region_read(region.index, offset, data); + } + } + + // INTx EOI + // The guest reading from the BAR potentially means the interrupt has + // been received and can be acknowledged. + if self.interrupt.intx_in_use() { + if let Err(e) = self.vfio_wrapper.unmask_irq(VFIO_PCI_INTX_IRQ_INDEX) { + error!("Failed unmasking INTx IRQ: {}", e); + } + } + } + + pub(crate) fn write_bar( + &mut self, + base: u64, + offset: u64, + data: &[u8], + ) -> Option> { + let addr = base + offset; + if let Some(region) = self.find_region(addr) { + let offset = addr - region.start.raw_value(); + + // If the MSI-X table is written to, we need to update our cache. + if self.interrupt.msix_table_accessed(region.index, offset) { + self.interrupt.msix_write_table(offset, data); + } else { + self.vfio_wrapper.region_write(region.index, offset, data); + } + } + + // INTx EOI + // The guest writing to the BAR potentially means the interrupt has + // been received and can be acknowledged. + if self.interrupt.intx_in_use() { + if let Err(e) = self.vfio_wrapper.unmask_irq(VFIO_PCI_INTX_IRQ_INDEX) { + error!("Failed unmasking INTx IRQ: {}", e); + } + } + + None + } + + pub(crate) fn write_config_register( + &mut self, + reg_idx: usize, + offset: u64, + data: &[u8], + ) -> Option> { + // When the guest wants to write to a BAR, we trap it into + // our local configuration space. We're not reprogramming + // VFIO device. + if (PCI_CONFIG_BAR0_INDEX..PCI_CONFIG_BAR0_INDEX + BAR_NUMS).contains(®_idx) + || reg_idx == PCI_ROM_EXP_BAR_INDEX + { + // We keep our local cache updated with the BARs. + // We'll read it back from there when the guest is asking + // for BARs (see read_config_register()). + self.configuration + .write_config_register(reg_idx, offset, data); + return None; + } + + let reg = (reg_idx * PCI_CONFIG_REGISTER_SIZE) as u64; + + // If the MSI or MSI-X capabilities are accessed, we need to + // update our local cache accordingly. + // Depending on how the capabilities are modified, this could + // trigger a VFIO MSI or MSI-X toggle. + if let Some((cap_id, cap_base)) = self.interrupt.accessed(reg) { + let cap_offset: u64 = reg - cap_base + offset; + match cap_id { + PciCapabilityId::MessageSignalledInterrupts => { + if let Err(e) = self.update_msi_capabilities(cap_offset, data) { + error!("Could not update MSI capabilities: {}", e); + } + } + PciCapabilityId::MsiX => { + if let Err(e) = self.update_msix_capabilities(cap_offset, data) { + error!("Could not update MSI-X capabilities: {}", e); + } + } + _ => {} + } + } + + // Make sure to write to the device's PCI config space after MSI/MSI-X + // interrupts have been enabled/disabled. In case of MSI, when the + // interrupts are enabled through VFIO (using VFIO_DEVICE_SET_IRQS), + // the MSI Enable bit in the MSI capability structure found in the PCI + // config space is disabled by default. That's why when the guest is + // enabling this bit, we first need to enable the MSI interrupts with + // VFIO through VFIO_DEVICE_SET_IRQS ioctl, and only after we can write + // to the device region to update the MSI Enable bit. + self.vfio_wrapper.write_config((reg + offset) as u32, data); + + None + } + + pub(crate) fn read_config_register(&mut self, reg_idx: usize) -> u32 { + // When reading the BARs, we trap it and return what comes + // from our local configuration space. We want the guest to + // use that and not the VFIO device BARs as it does not map + // with the guest address space. + if (PCI_CONFIG_BAR0_INDEX..PCI_CONFIG_BAR0_INDEX + BAR_NUMS).contains(®_idx) + || reg_idx == PCI_ROM_EXP_BAR_INDEX + { + return self.configuration.read_reg(reg_idx); + } + + // Since we don't support passing multi-functions devices, we should + // mask the multi-function bit, bit 7 of the Header Type byte on the + // register 3. + let mask = if reg_idx == PCI_HEADER_TYPE_REG_INDEX { + 0xff7f_ffff + } else { + 0xffff_ffff + }; + + // The config register read comes from the VFIO device itself. + let mut value = self.vfio_wrapper.read_config_dword((reg_idx * 4) as u32) & mask; + + if let Some(config_patch) = self.patches.get(®_idx) { + value = (value & !config_patch.mask) | config_patch.patch; + } + + value + } + + fn state(&self) -> VfioCommonState { + let intx_state = self.interrupt.intx.as_ref().map(|intx| IntxState { + enabled: intx.enabled, + }); + + let msi_state = self.interrupt.msi.as_ref().map(|msi| MsiState { + cap: msi.cfg.cap, + cap_offset: msi.cap_offset, + }); + + let msix_state = self.interrupt.msix.as_ref().map(|msix| MsixState { + cap: msix.cap, + cap_offset: msix.cap_offset, + bdf: msix.bar.devid, + }); + + VfioCommonState { + intx_state, + msi_state, + msix_state, + } + } + + fn set_state(&mut self, state: &VfioCommonState) -> Result<(), VfioPciError> { + if let (Some(intx), Some(interrupt_source_group)) = + (&state.intx_state, self.legacy_interrupt_group.clone()) + { + self.interrupt.intx = Some(VfioIntx { + interrupt_source_group, + enabled: false, + }); + + if intx.enabled { + self.enable_intx()?; + } + } + + if let Some(msi) = &state.msi_state { + self.initialize_msi(msi.cap.msg_ctl, msi.cap_offset); + } + + if let Some(msix) = &state.msix_state { + self.initialize_msix(msix.cap, msix.cap_offset, msix.bdf.into()); + } + + Ok(()) + } +} + +impl Pausable for VfioCommon {} + +impl Snapshottable for VfioCommon { + fn id(&self) -> String { + String::from("vfio_common") + } + + fn snapshot(&mut self) -> std::result::Result { + let mut vfio_common_snapshot = Snapshot::new_from_state(&self.id(), &self.state())?; + + // Snapshot PciConfiguration + vfio_common_snapshot.add_snapshot(self.configuration.snapshot()?); + + // Snapshot MSI + if let Some(msi) = &mut self.interrupt.msi { + vfio_common_snapshot.add_snapshot(msi.cfg.snapshot()?); + } + + // Snapshot MSI-X + if let Some(msix) = &mut self.interrupt.msix { + vfio_common_snapshot.add_snapshot(msix.bar.snapshot()?); + } + + Ok(vfio_common_snapshot) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + if let Some(vfio_common_section) = snapshot + .snapshot_data + .get(&format!("{}-section", self.id())) + { + // It has to be invoked first as we want Interrupt to be initialized + // correctly before we try to restore MSI and MSI-X configurations. + self.set_state(&vfio_common_section.to_state()?) + .map_err(|e| { + MigratableError::Restore(anyhow!("Could not restore VFIO_COMMON state {:?}", e)) + })?; + + // Restore PciConfiguration + if let Some(pci_config_snapshot) = snapshot.snapshots.get(&self.configuration.id()) { + self.configuration.restore(*pci_config_snapshot.clone())?; + } + + // Restore MSI + if let Some(msi) = &mut self.interrupt.msi { + if let Some(msi_snapshot) = snapshot.snapshots.get(&msi.cfg.id()) { + msi.cfg.restore(*msi_snapshot.clone())?; + } + if msi.cfg.enabled() { + self.enable_msi().unwrap(); + } + } + + // Restore MSI-X + if let Some(msix) = &mut self.interrupt.msix { + if let Some(msix_snapshot) = snapshot.snapshots.get(&msix.bar.id()) { + msix.bar.restore(*msix_snapshot.clone())?; + } + if msix.bar.enabled() { + self.enable_msix().unwrap(); + } + } + + return Ok(()); + } + + Err(MigratableError::Restore(anyhow!( + "Could not find VFIO_COMMON snapshot section" + ))) + } +} + +/// VfioPciDevice represents a VFIO PCI device. +/// This structure implements the BusDevice and PciDevice traits. +/// +/// A VfioPciDevice is bound to a VfioDevice and is also a PCI device. +/// The VMM creates a VfioDevice, then assigns it to a VfioPciDevice, +/// which then gets added to the PCI bus. +pub struct VfioPciDevice { + id: String, + vm: Arc, + device: Arc, + container: Arc, + common: VfioCommon, + iommu_attached: bool, + memory_slot: Arc u32 + Send + Sync>, +} + +impl VfioPciDevice { + /// Constructs a new Vfio Pci device for the given Vfio device + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + vm: &Arc, + device: VfioDevice, + container: Arc, + msi_interrupt_manager: Arc>, + legacy_interrupt_group: Option>, + iommu_attached: bool, + bdf: PciBdf, + restoring: bool, + memory_slot: Arc u32 + Send + Sync>, + ) -> Result { + let device = Arc::new(device); + device.reset(); + + let configuration = PciConfiguration::new( + 0, + 0, + 0, + PciClassCode::Other, + &PciVfioSubclass::VfioSubclass, + None, + PciHeaderType::Device, + 0, + 0, + None, + ); + + let vfio_wrapper = VfioDeviceWrapper::new(Arc::clone(&device)); + + let mut common = VfioCommon { + mmio_regions: Vec::new(), + configuration, + interrupt: Interrupt { + intx: None, + msi: None, + msix: None, + }, + msi_interrupt_manager, + legacy_interrupt_group, + vfio_wrapper: Arc::new(vfio_wrapper) as Arc, + patches: HashMap::new(), + }; + + // No need to parse capabilities from the device if on the restore path. + // The initialization will be performed later when restore() will be + // called. + if !restoring { + common.parse_capabilities(bdf); + common.initialize_legacy_interrupt()?; + } + + let vfio_pci_device = VfioPciDevice { + id, + vm: vm.clone(), + device, + container, + common, + iommu_attached, + memory_slot, + }; + + Ok(vfio_pci_device) + } + + pub fn iommu_attached(&self) -> bool { + self.iommu_attached + } + + fn align_4k(address: u64) -> u64 { + (address + 0xfff) & 0xffff_ffff_ffff_f000 + } + + fn is_4k_aligned(address: u64) -> bool { + (address & 0xfff) == 0 + } + + fn is_4k_multiple(size: u64) -> bool { + (size & 0xfff) == 0 + } + + fn generate_sparse_areas( + caps: &[VfioRegionInfoCap], + region_index: u32, + region_start: u64, + region_size: u64, + vfio_msix: Option<&VfioMsix>, + ) -> Result, VfioPciError> { + for cap in caps { + match cap { + VfioRegionInfoCap::SparseMmap(sparse_mmap) => return Ok(sparse_mmap.areas.clone()), + VfioRegionInfoCap::MsixMappable => { + if !Self::is_4k_aligned(region_start) { + error!( + "Region start address 0x{:x} must be at least aligned on 4KiB", + region_start + ); + return Err(VfioPciError::RegionAlignment); + } + if !Self::is_4k_multiple(region_size) { + error!( + "Region size 0x{:x} must be at least a multiple of 4KiB", + region_size + ); + return Err(VfioPciError::RegionSize); + } + + // In case the region contains the MSI-X vectors table or + // the MSI-X PBA table, we must calculate the subregions + // around them, leading to a list of sparse areas. + // We want to make sure we will still trap MMIO accesses + // to these MSI-X specific ranges. + // + // Using a BtreeMap as the list provided through the iterator is sorted + // by key. This ensures proper split of the whole region. + let mut inter_ranges = BTreeMap::new(); + if let Some(msix) = vfio_msix { + if region_index == msix.cap.table_bir() { + let (offset, size) = msix.cap.table_range(); + inter_ranges.insert(offset, size); + } + if region_index == msix.cap.pba_bir() { + let (offset, size) = msix.cap.pba_range(); + inter_ranges.insert(offset, size); + } + } + + let mut sparse_areas = Vec::new(); + let mut current_offset = 0; + for (range_offset, range_size) in inter_ranges { + if range_offset > current_offset { + sparse_areas.push(VfioRegionSparseMmapArea { + offset: current_offset, + size: range_offset - current_offset, + }); + } + + current_offset = Self::align_4k(range_offset + range_size); + } + + if region_size > current_offset { + sparse_areas.push(VfioRegionSparseMmapArea { + offset: current_offset, + size: region_size - current_offset, + }); + } + + return Ok(sparse_areas); + } + _ => {} + } + } + + // In case no relevant capabilities have been found, create a single + // sparse area corresponding to the entire MMIO region. + Ok(vec![VfioRegionSparseMmapArea { + offset: 0, + size: region_size, + }]) + } + + /// Map MMIO regions into the guest, and avoid VM exits when the guest tries + /// to reach those regions. + /// + /// # Arguments + /// + /// * `vm` - The VM object. It is used to set the VFIO MMIO regions + /// as user memory regions. + /// * `mem_slot` - The closure to return a memory slot. + pub fn map_mmio_regions(&mut self) -> Result<(), VfioPciError> { + let fd = self.device.as_raw_fd(); + + for region in self.common.mmio_regions.iter_mut() { + let region_flags = self.device.get_region_flags(region.index); + if region_flags & VFIO_REGION_INFO_FLAG_MMAP != 0 { + let mut prot = 0; + if region_flags & VFIO_REGION_INFO_FLAG_READ != 0 { + prot |= libc::PROT_READ; + } + if region_flags & VFIO_REGION_INFO_FLAG_WRITE != 0 { + prot |= libc::PROT_WRITE; + } + + // Retrieve the list of capabilities found on the region + let caps = if region_flags & VFIO_REGION_INFO_FLAG_CAPS != 0 { + self.device.get_region_caps(region.index) + } else { + Vec::new() + }; + + // Don't try to mmap the region if it contains MSI-X table or + // MSI-X PBA subregion, and if we couldn't find MSIX_MAPPABLE + // in the list of supported capabilities. + if let Some(msix) = self.common.interrupt.msix.as_ref() { + if (region.index == msix.cap.table_bir() || region.index == msix.cap.pba_bir()) + && !caps.contains(&VfioRegionInfoCap::MsixMappable) + { + continue; + } + } + + let mmap_size = self.device.get_region_size(region.index); + let mmap_offset = self.device.get_region_offset(region.index); + + let sparse_areas = Self::generate_sparse_areas( + &caps, + region.index, + region.start.0, + mmap_size, + self.common.interrupt.msix.as_ref(), + )?; + + for area in sparse_areas.iter() { + let host_addr = unsafe { + libc::mmap( + null_mut(), + area.size as usize, + prot, + libc::MAP_SHARED, + fd, + mmap_offset as libc::off_t + area.offset as libc::off_t, + ) + }; + + if host_addr == libc::MAP_FAILED { + error!( + "Could not mmap sparse area (offset = 0x{:x}, size = 0x{:x}): {}", + area.offset, + area.size, + std::io::Error::last_os_error() + ); + return Err(VfioPciError::MmapArea); + } + + let user_memory_region = UserMemoryRegion { + slot: (self.memory_slot)(), + start: region.start.0 + area.offset, + size: area.size, + host_addr: host_addr as u64, + }; + + region.user_memory_regions.push(user_memory_region); + + let mem_region = self.vm.make_user_memory_region( + user_memory_region.slot, + user_memory_region.start, + user_memory_region.size, + user_memory_region.host_addr, + false, + false, + ); + + self.vm + .create_user_memory_region(mem_region) + .map_err(VfioPciError::CreateUserMemoryRegion)?; + } + } + } + + Ok(()) + } + + pub fn unmap_mmio_regions(&mut self) { + for region in self.common.mmio_regions.iter() { + for user_memory_region in region.user_memory_regions.iter() { + // Remove region + let r = self.vm.make_user_memory_region( + user_memory_region.slot, + user_memory_region.start, + user_memory_region.size, + user_memory_region.host_addr, + false, + false, + ); + + if let Err(e) = self.vm.remove_user_memory_region(r) { + error!("Could not remove the userspace memory region: {}", e); + } + + let ret = unsafe { + libc::munmap( + user_memory_region.host_addr as *mut libc::c_void, + user_memory_region.size as usize, + ) + }; + if ret != 0 { + error!( + "Could not unmap region {}, error:{}", + region.index, + io::Error::last_os_error() + ); + } + } + } + } + + pub fn dma_map(&self, iova: u64, size: u64, user_addr: u64) -> Result<(), VfioPciError> { + if !self.iommu_attached { + self.container + .vfio_dma_map(iova, size, user_addr) + .map_err(VfioPciError::DmaMap)?; + } + + Ok(()) + } + + pub fn dma_unmap(&self, iova: u64, size: u64) -> Result<(), VfioPciError> { + if !self.iommu_attached { + self.container + .vfio_dma_unmap(iova, size) + .map_err(VfioPciError::DmaUnmap)?; + } + + Ok(()) + } + + pub fn mmio_regions(&self) -> Vec { + self.common.mmio_regions.clone() + } +} + +impl Drop for VfioPciDevice { + fn drop(&mut self) { + self.unmap_mmio_regions(); + + if let Some(msix) = &self.common.interrupt.msix { + if msix.bar.enabled() { + self.common.disable_msix(); + } + } + + if let Some(msi) = &self.common.interrupt.msi { + if msi.cfg.enabled() { + self.common.disable_msi() + } + } + + if self.common.interrupt.intx_in_use() { + self.common.disable_intx(); + } + } +} + +impl BusDevice for VfioPciDevice { + fn read(&mut self, base: u64, offset: u64, data: &mut [u8]) { + self.read_bar(base, offset, data) + } + + fn write(&mut self, base: u64, offset: u64, data: &[u8]) -> Option> { + self.write_bar(base, offset, data) + } +} + +// First BAR offset in the PCI config space. +const PCI_CONFIG_BAR_OFFSET: u32 = 0x10; +// Capability register offset in the PCI config space. +const PCI_CONFIG_CAPABILITY_OFFSET: u32 = 0x34; +// Extended capabilities register offset in the PCI config space. +const PCI_CONFIG_EXTENDED_CAPABILITY_OFFSET: u32 = 0x100; +// IO BAR when first BAR bit is 1. +const PCI_CONFIG_IO_BAR: u32 = 0x1; +// 64-bit memory bar flag. +const PCI_CONFIG_MEMORY_BAR_64BIT: u32 = 0x4; +// Prefetchable BAR bit +const PCI_CONFIG_BAR_PREFETCHABLE: u32 = 0x8; +// PCI config register size (4 bytes). +const PCI_CONFIG_REGISTER_SIZE: usize = 4; +// Number of BARs for a PCI device +const BAR_NUMS: usize = 6; +// PCI Header Type register index +const PCI_HEADER_TYPE_REG_INDEX: usize = 3; +// First BAR register index +const PCI_CONFIG_BAR0_INDEX: usize = 4; +// PCI ROM expansion BAR register index +const PCI_ROM_EXP_BAR_INDEX: usize = 12; + +impl PciDevice for VfioPciDevice { + fn allocate_bars( + &mut self, + allocator: &Arc>, + mmio_allocator: &mut AddressAllocator, + resources: Option>, + ) -> Result, PciDeviceError> { + self.common + .allocate_bars(allocator, mmio_allocator, resources) + } + + fn free_bars( + &mut self, + allocator: &mut SystemAllocator, + mmio_allocator: &mut AddressAllocator, + ) -> Result<(), PciDeviceError> { + self.common.free_bars(allocator, mmio_allocator) + } + + fn write_config_register( + &mut self, + reg_idx: usize, + offset: u64, + data: &[u8], + ) -> Option> { + self.common.write_config_register(reg_idx, offset, data) + } + + fn read_config_register(&mut self, reg_idx: usize) -> u32 { + self.common.read_config_register(reg_idx) + } + + fn detect_bar_reprogramming( + &mut self, + reg_idx: usize, + data: &[u8], + ) -> Option { + self.common + .configuration + .detect_bar_reprogramming(reg_idx, data) + } + + fn read_bar(&mut self, base: u64, offset: u64, data: &mut [u8]) { + self.common.read_bar(base, offset, data) + } + + fn write_bar(&mut self, base: u64, offset: u64, data: &[u8]) -> Option> { + self.common.write_bar(base, offset, data) + } + + fn move_bar(&mut self, old_base: u64, new_base: u64) -> Result<(), io::Error> { + for region in self.common.mmio_regions.iter_mut() { + if region.start.raw_value() == old_base { + region.start = GuestAddress(new_base); + + for user_memory_region in region.user_memory_regions.iter_mut() { + // Remove old region + let old_mem_region = self.vm.make_user_memory_region( + user_memory_region.slot, + user_memory_region.start, + user_memory_region.size, + user_memory_region.host_addr, + false, + false, + ); + + self.vm + .remove_user_memory_region(old_mem_region) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + + // Update the user memory region with the correct start address. + if new_base > old_base { + user_memory_region.start += new_base - old_base; + } else { + user_memory_region.start -= old_base - new_base; + } + + // Insert new region + let new_mem_region = self.vm.make_user_memory_region( + user_memory_region.slot, + user_memory_region.start, + user_memory_region.size, + user_memory_region.host_addr, + false, + false, + ); + + self.vm + .create_user_memory_region(new_mem_region) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + } + } + } + + Ok(()) + } + + fn as_any(&mut self) -> &mut dyn Any { + self + } + + fn id(&self) -> Option { + Some(self.id.clone()) + } +} + +impl Pausable for VfioPciDevice {} + +impl Snapshottable for VfioPciDevice { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + let mut vfio_pci_dev_snapshot = Snapshot::new(&self.id); + + // Snapshot VfioCommon + vfio_pci_dev_snapshot.add_snapshot(self.common.snapshot()?); + + Ok(vfio_pci_dev_snapshot) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + // Restore VfioCommon + if let Some(vfio_common_snapshot) = snapshot.snapshots.get(&self.common.id()) { + self.common.restore(*vfio_common_snapshot.clone())?; + self.map_mmio_regions().map_err(|e| { + MigratableError::Restore(anyhow!( + "Could not map MMIO regions for VfioPciDevice on restore {:?}", + e + )) + })?; + } + + Ok(()) + } +} +impl Transportable for VfioPciDevice {} +impl Migratable for VfioPciDevice {} diff --git a/vendor/cube-hypervisor/pci/src/vfio_user.rs b/vendor/cube-hypervisor/pci/src/vfio_user.rs new file mode 100644 index 0000000..95c6541 --- /dev/null +++ b/vendor/cube-hypervisor/pci/src/vfio_user.rs @@ -0,0 +1,643 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::vfio::{Interrupt, UserMemoryRegion, Vfio, VfioCommon, VfioError}; +use crate::{BarReprogrammingParams, PciBarConfiguration, VfioPciError}; +use crate::{ + PciBdf, PciClassCode, PciConfiguration, PciDevice, PciDeviceError, PciHeaderType, PciSubclass, +}; +use anyhow::anyhow; +use hypervisor::HypervisorVmError; +use std::any::Any; +use std::collections::HashMap; +use std::os::unix::prelude::AsRawFd; +use std::ptr::null_mut; +use std::sync::{Arc, Barrier, Mutex}; +use std::u32; +use thiserror::Error; +use vfio_bindings::bindings::vfio::*; +use vfio_ioctls::VfioIrq; +use vfio_user::{Client, Error as VfioUserError}; +use vm_allocator::{AddressAllocator, SystemAllocator}; +use vm_device::dma_mapping::ExternalDmaMapping; +use vm_device::interrupt::{InterruptManager, InterruptSourceGroup, MsiIrqGroupConfig}; +use vm_device::{BusDevice, Resource}; +use vm_memory::bitmap::AtomicBitmap; +use vm_memory::{ + Address, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryRegion, GuestRegionMmap, +}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vmm_sys_util::eventfd::EventFd; + +pub struct VfioUserPciDevice { + id: String, + vm: Arc, + client: Arc>, + common: VfioCommon, + memory_slot: Arc u32 + Send + Sync>, +} + +#[derive(Error, Debug)] +pub enum VfioUserPciDeviceError { + #[error("Client error: {0}")] + Client(#[source] VfioUserError), + #[error("Failed to map VFIO PCI region into guest: {0}")] + MapRegionGuest(#[source] HypervisorVmError), + #[error("Failed to DMA map: {0}")] + DmaMap(#[source] VfioUserError), + #[error("Failed to DMA unmap: {0}")] + DmaUnmap(#[source] VfioUserError), + #[error("Failed to initialize legacy interrupts: {0}")] + InitializeLegacyInterrupts(#[source] VfioPciError), +} + +#[derive(Copy, Clone)] +enum PciVfioUserSubclass { + VfioUserSubclass = 0xff, +} + +impl PciSubclass for PciVfioUserSubclass { + fn get_register_value(&self) -> u8 { + *self as u8 + } +} + +impl VfioUserPciDevice { + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + vm: &Arc, + client: Arc>, + msi_interrupt_manager: Arc>, + legacy_interrupt_group: Option>, + bdf: PciBdf, + restoring: bool, + memory_slot: Arc u32 + Send + Sync>, + ) -> Result { + // This is used for the BAR and capabilities only + let configuration = PciConfiguration::new( + 0, + 0, + 0, + PciClassCode::Other, + &PciVfioUserSubclass::VfioUserSubclass, + None, + PciHeaderType::Device, + 0, + 0, + None, + ); + let resettable = client.lock().unwrap().resettable(); + if resettable { + client + .lock() + .unwrap() + .reset() + .map_err(VfioUserPciDeviceError::Client)?; + } + + let vfio_wrapper = VfioUserClientWrapper { + client: client.clone(), + }; + + let mut common = VfioCommon { + mmio_regions: Vec::new(), + configuration, + interrupt: Interrupt { + intx: None, + msi: None, + msix: None, + }, + msi_interrupt_manager, + legacy_interrupt_group, + vfio_wrapper: Arc::new(vfio_wrapper) as Arc, + patches: HashMap::new(), + }; + + // No need to parse capabilities from the device if on the restore path. + // The initialization will be performed later when restore() will be + // called. + if !restoring { + common.parse_capabilities(bdf); + common + .initialize_legacy_interrupt() + .map_err(VfioUserPciDeviceError::InitializeLegacyInterrupts)?; + } + + Ok(Self { + id, + vm: vm.clone(), + client, + common, + memory_slot, + }) + } + + pub fn map_mmio_regions(&mut self) -> Result<(), VfioUserPciDeviceError> { + for mmio_region in &mut self.common.mmio_regions { + let region_flags = self + .client + .lock() + .unwrap() + .region(mmio_region.index) + .unwrap() + .flags; + let file_offset = self + .client + .lock() + .unwrap() + .region(mmio_region.index) + .unwrap() + .file_offset + .clone(); + + let sparse_areas = self + .client + .lock() + .unwrap() + .region(mmio_region.index) + .unwrap() + .sparse_areas + .clone(); + + if region_flags & VFIO_REGION_INFO_FLAG_MMAP != 0 { + let mut prot = 0; + if region_flags & VFIO_REGION_INFO_FLAG_READ != 0 { + prot |= libc::PROT_READ; + } + if region_flags & VFIO_REGION_INFO_FLAG_WRITE != 0 { + prot |= libc::PROT_WRITE; + } + + let mmaps = if sparse_areas.is_empty() { + vec![vfio_region_sparse_mmap_area { + offset: 0, + size: mmio_region.length, + }] + } else { + sparse_areas + }; + + for s in mmaps.iter() { + let host_addr = unsafe { + libc::mmap( + null_mut(), + s.size as usize, + prot, + libc::MAP_SHARED, + file_offset.as_ref().unwrap().file().as_raw_fd(), + file_offset.as_ref().unwrap().start() as libc::off_t + + s.offset as libc::off_t, + ) + }; + + if host_addr == libc::MAP_FAILED { + error!( + "Could not mmap regions, error:{}", + std::io::Error::last_os_error() + ); + continue; + } + + let user_memory_region = UserMemoryRegion { + slot: (self.memory_slot)(), + start: mmio_region.start.0 + s.offset, + size: s.size, + host_addr: host_addr as u64, + }; + + mmio_region.user_memory_regions.push(user_memory_region); + + let mem_region = self.vm.make_user_memory_region( + user_memory_region.slot, + user_memory_region.start, + user_memory_region.size, + user_memory_region.host_addr, + false, + false, + ); + + self.vm + .create_user_memory_region(mem_region) + .map_err(VfioUserPciDeviceError::MapRegionGuest)?; + } + } + } + + Ok(()) + } + + pub fn unmap_mmio_regions(&mut self) { + for mmio_region in self.common.mmio_regions.iter() { + for user_memory_region in mmio_region.user_memory_regions.iter() { + // Remove region + let r = self.vm.make_user_memory_region( + user_memory_region.slot, + user_memory_region.start, + user_memory_region.size, + user_memory_region.host_addr, + false, + false, + ); + + if let Err(e) = self.vm.remove_user_memory_region(r) { + error!("Could not remove the userspace memory region: {}", e); + } + + // Remove mmaps + let ret = unsafe { + libc::munmap( + user_memory_region.host_addr as *mut libc::c_void, + user_memory_region.size as usize, + ) + }; + if ret != 0 { + error!( + "Could not unmap region {}, error:{}", + mmio_region.index, + std::io::Error::last_os_error() + ); + } + } + } + } + + pub fn dma_map( + &mut self, + region: &GuestRegionMmap, + ) -> Result<(), VfioUserPciDeviceError> { + let (fd, offset) = match region.file_offset() { + Some(_file_offset) => (_file_offset.file().as_raw_fd(), _file_offset.start()), + None => return Ok(()), + }; + + self.client + .lock() + .unwrap() + .dma_map(offset, region.start_addr().raw_value(), region.len(), fd) + .map_err(VfioUserPciDeviceError::DmaMap) + } + + pub fn dma_unmap( + &mut self, + region: &GuestRegionMmap, + ) -> Result<(), VfioUserPciDeviceError> { + self.client + .lock() + .unwrap() + .dma_unmap(region.start_addr().raw_value(), region.len()) + .map_err(VfioUserPciDeviceError::DmaUnmap) + } +} + +impl BusDevice for VfioUserPciDevice { + fn read(&mut self, base: u64, offset: u64, data: &mut [u8]) { + self.read_bar(base, offset, data) + } + + fn write(&mut self, base: u64, offset: u64, data: &[u8]) -> Option> { + self.write_bar(base, offset, data) + } +} + +#[repr(u32)] +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] +#[allow(dead_code)] +enum Regions { + Bar0, + Bar1, + Bar2, + Bar3, + Bar4, + Bar5, + Rom, + Config, + Vga, + Migration, +} + +struct VfioUserClientWrapper { + client: Arc>, +} + +impl Vfio for VfioUserClientWrapper { + fn region_read(&self, index: u32, offset: u64, data: &mut [u8]) { + self.client + .lock() + .unwrap() + .region_read(index, offset, data) + .ok(); + } + + fn region_write(&self, index: u32, offset: u64, data: &[u8]) { + self.client + .lock() + .unwrap() + .region_write(index, offset, data) + .ok(); + } + + fn get_irq_info(&self, irq_index: u32) -> Option { + self.client + .lock() + .unwrap() + .get_irq_info(irq_index) + .ok() + .map(|i| VfioIrq { + index: i.index, + flags: i.flags, + count: i.count, + }) + } + + fn enable_irq(&self, irq_index: u32, event_fds: Vec<&EventFd>) -> Result<(), VfioError> { + info!( + "Enabling IRQ {:x} number of fds = {:?}", + irq_index, + event_fds.len() + ); + let fds: Vec = event_fds.iter().map(|e| e.as_raw_fd()).collect(); + + // Batch into blocks of 16 fds as sendmsg() has a size limit + let mut sent_fds = 0; + let num_fds = event_fds.len() as u32; + while sent_fds < num_fds { + let remaining_fds = num_fds - sent_fds; + let count = if remaining_fds > 16 { + 16 + } else { + remaining_fds + }; + + self.client + .lock() + .unwrap() + .set_irqs( + irq_index, + VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER, + sent_fds, + count, + &fds[sent_fds as usize..(sent_fds + count) as usize], + ) + .map_err(VfioError::VfioUser)?; + + sent_fds += count; + } + + Ok(()) + } + + fn disable_irq(&self, irq_index: u32) -> Result<(), VfioError> { + info!("Disabling IRQ {:x}", irq_index); + self.client + .lock() + .unwrap() + .set_irqs( + irq_index, + VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER, + 0, + 0, + &[], + ) + .map_err(VfioError::VfioUser) + } + + fn unmask_irq(&self, irq_index: u32) -> Result<(), VfioError> { + info!("Unmasking IRQ {:x}", irq_index); + self.client + .lock() + .unwrap() + .set_irqs( + irq_index, + VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK, + 0, + 1, + &[], + ) + .map_err(VfioError::VfioUser) + } +} + +impl PciDevice for VfioUserPciDevice { + fn allocate_bars( + &mut self, + allocator: &Arc>, + mmio_allocator: &mut AddressAllocator, + resources: Option>, + ) -> Result, PciDeviceError> { + self.common + .allocate_bars(allocator, mmio_allocator, resources) + } + + fn free_bars( + &mut self, + allocator: &mut SystemAllocator, + mmio_allocator: &mut AddressAllocator, + ) -> Result<(), PciDeviceError> { + self.common.free_bars(allocator, mmio_allocator) + } + + fn as_any(&mut self) -> &mut dyn Any { + self + } + + fn detect_bar_reprogramming( + &mut self, + reg_idx: usize, + data: &[u8], + ) -> Option { + self.common + .configuration + .detect_bar_reprogramming(reg_idx, data) + } + + fn write_config_register( + &mut self, + reg_idx: usize, + offset: u64, + data: &[u8], + ) -> Option> { + self.common.write_config_register(reg_idx, offset, data) + } + + fn read_config_register(&mut self, reg_idx: usize) -> u32 { + self.common.read_config_register(reg_idx) + } + + fn read_bar(&mut self, base: u64, offset: u64, data: &mut [u8]) { + self.common.read_bar(base, offset, data) + } + + fn write_bar(&mut self, base: u64, offset: u64, data: &[u8]) -> Option> { + self.common.write_bar(base, offset, data) + } + + fn move_bar(&mut self, old_base: u64, new_base: u64) -> Result<(), std::io::Error> { + info!("Moving BAR 0x{:x} -> 0x{:x}", old_base, new_base); + for mmio_region in self.common.mmio_regions.iter_mut() { + if mmio_region.start.raw_value() == old_base { + mmio_region.start = GuestAddress(new_base); + + for user_memory_region in mmio_region.user_memory_regions.iter_mut() { + // Remove old region + let old_region = self.vm.make_user_memory_region( + user_memory_region.slot, + user_memory_region.start, + user_memory_region.size, + user_memory_region.host_addr, + false, + false, + ); + + self.vm + .remove_user_memory_region(old_region) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; + + // Update the user memory region with the correct start address. + if new_base > old_base { + user_memory_region.start += new_base - old_base; + } else { + user_memory_region.start -= old_base - new_base; + } + + // Insert new region + let new_region = self.vm.make_user_memory_region( + user_memory_region.slot, + user_memory_region.start, + user_memory_region.size, + user_memory_region.host_addr, + false, + false, + ); + + self.vm + .create_user_memory_region(new_region) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; + } + info!("Moved bar 0x{:x} -> 0x{:x}", old_base, new_base); + } + } + + Ok(()) + } + + fn id(&self) -> Option { + Some(self.id.clone()) + } +} + +impl Drop for VfioUserPciDevice { + fn drop(&mut self) { + self.unmap_mmio_regions(); + + if let Some(msix) = &self.common.interrupt.msix { + if msix.bar.enabled() { + self.common.disable_msix(); + } + } + + if let Some(msi) = &self.common.interrupt.msi { + if msi.cfg.enabled() { + self.common.disable_msi() + } + } + + if self.common.interrupt.intx_in_use() { + self.common.disable_intx(); + } + + if let Err(e) = self.client.lock().unwrap().shutdown() { + error!("Failed shutting down vfio-user client: {}", e); + } + } +} + +impl Pausable for VfioUserPciDevice {} + +impl Snapshottable for VfioUserPciDevice { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + let mut vfio_pci_dev_snapshot = Snapshot::new(&self.id); + + // Snapshot VfioCommon + vfio_pci_dev_snapshot.add_snapshot(self.common.snapshot()?); + + Ok(vfio_pci_dev_snapshot) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + // Restore VfioCommon + if let Some(vfio_common_snapshot) = snapshot.snapshots.get(&self.common.id()) { + self.common.restore(*vfio_common_snapshot.clone())?; + self.map_mmio_regions().map_err(|e| { + MigratableError::Restore(anyhow!( + "Could not map MMIO regions for VfioUserPciDevice on restore {:?}", + e + )) + })?; + } + + Ok(()) + } +} +impl Transportable for VfioUserPciDevice {} +impl Migratable for VfioUserPciDevice {} + +pub struct VfioUserDmaMapping { + client: Arc>, + memory: Arc, +} + +impl VfioUserDmaMapping { + pub fn new(client: Arc>, memory: Arc) -> Self { + Self { client, memory } + } +} + +impl ExternalDmaMapping for VfioUserDmaMapping { + fn map(&self, iova: u64, gpa: u64, size: u64) -> std::result::Result<(), std::io::Error> { + let mem = self.memory.memory(); + let guest_addr = GuestAddress(gpa); + let region = mem.find_region(guest_addr); + + if let Some(region) = region { + let file_offset = region.file_offset().unwrap(); + let offset = (GuestAddress(gpa).checked_offset_from(region.start_addr())).unwrap() + + file_offset.start(); + + self.client + .lock() + .unwrap() + .dma_map(offset, iova, size, file_offset.file().as_raw_fd()) + .map_err(|e| { + std::io::Error::new( + std::io::ErrorKind::Other, + format!("Error mapping region: {}", e), + ) + }) + } else { + Err(std::io::Error::new( + std::io::ErrorKind::Other, + format!("Region not found for 0x{:x}", gpa), + )) + } + } + + fn unmap(&self, iova: u64, size: u64) -> std::result::Result<(), std::io::Error> { + self.client + .lock() + .unwrap() + .dma_unmap(iova, size) + .map_err(|e| { + std::io::Error::new( + std::io::ErrorKind::Other, + format!("Error unmapping region: {}", e), + ) + }) + } +} diff --git a/vendor/cube-hypervisor/performance-metrics/Cargo.toml b/vendor/cube-hypervisor/performance-metrics/Cargo.toml new file mode 100644 index 0000000..91637f4 --- /dev/null +++ b/vendor/cube-hypervisor/performance-metrics/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "performance-metrics" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" +build = "build.rs" + +[dependencies] +clap = { version = "4.4.7", features = ["wrap_help", "cargo"] } +dirs = "4.0.0" +serde = { workspace = true, features = ["rc", "derive"] } +serde_json = "1.0.87" +test_infra = { path = "../test_infra" } +thiserror = "1.0.37" +wait-timeout = "0.2.0" + +[build-dependencies] +clap = { version = "4.4.7", features = ["cargo"] } diff --git a/vendor/cube-hypervisor/performance-metrics/build.rs b/vendor/cube-hypervisor/performance-metrics/build.rs new file mode 100644 index 0000000..1e94e2b --- /dev/null +++ b/vendor/cube-hypervisor/performance-metrics/build.rs @@ -0,0 +1,26 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#[macro_use(crate_version)] +extern crate clap; + +use std::process::Command; + +fn main() { + let mut git_human_readable = "v".to_owned() + crate_version!(); + if let Ok(git_out) = Command::new("git").args(["describe", "--dirty"]).output() { + if git_out.status.success() { + if let Ok(git_out_str) = String::from_utf8(git_out.stdout) { + git_human_readable = git_out_str; + } + } + } + + // This println!() has a special behavior, as it will set the environment + // variable GIT_HUMAN_READABLE, so that it can be reused from the binary. + // Particularly, this is used from the main.rs to display the exact + // version information. + println!("cargo:rustc-env=GIT_HUMAN_READABLE={}", git_human_readable); +} diff --git a/vendor/cube-hypervisor/performance-metrics/src/main.rs b/vendor/cube-hypervisor/performance-metrics/src/main.rs new file mode 100644 index 0000000..ce15421 --- /dev/null +++ b/vendor/cube-hypervisor/performance-metrics/src/main.rs @@ -0,0 +1,606 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +// Custom harness to run performance tests +extern crate test_infra; +#[macro_use(crate_authors)] +extern crate clap; + +mod performance_tests; + +use clap::{Arg, ArgAction, Command as ClapCommand}; +use performance_tests::*; +use serde::{Deserialize, Serialize}; +use std::{ + env, fmt, + process::Command, + sync::{mpsc::channel, Arc}, + thread, + time::Duration, +}; +use test_infra::FioOps; +use thiserror::Error; + +#[derive(Error, Debug)] +enum Error { + #[error("Error: test timed-out")] + TestTimeout, + #[error("Error: test failed")] + TestFailed, +} + +#[derive(Deserialize, Serialize)] +pub struct PerformanceTestResult { + name: String, + mean: f64, + std_dev: f64, + max: f64, + min: f64, +} + +#[derive(Deserialize, Serialize)] +pub struct MetricsReport { + pub git_human_readable: String, + pub git_revision: String, + pub git_commit_date: String, + pub date: String, + pub results: Vec, +} + +impl Default for MetricsReport { + fn default() -> Self { + let mut git_human_readable = "".to_string(); + if let Ok(git_out) = Command::new("git").args(["describe", "--dirty"]).output() { + if git_out.status.success() { + git_human_readable = String::from_utf8(git_out.stdout) + .unwrap() + .trim() + .to_string(); + } else { + eprintln!( + "Error generating human readable git reference: {}", + String::from_utf8(git_out.stderr).unwrap() + ); + } + } + + let mut git_revision = "".to_string(); + if let Ok(git_out) = Command::new("git").args(["rev-parse", "HEAD"]).output() { + if git_out.status.success() { + git_revision = String::from_utf8(git_out.stdout) + .unwrap() + .trim() + .to_string(); + } else { + eprintln!( + "Error generating git reference: {}", + String::from_utf8(git_out.stderr).unwrap() + ); + } + } + + let mut git_commit_date = "".to_string(); + if let Ok(git_out) = Command::new("git") + .args(["show", "-s", "--format=%cd"]) + .output() + { + if git_out.status.success() { + git_commit_date = String::from_utf8(git_out.stdout) + .unwrap() + .trim() + .to_string(); + } else { + eprintln!( + "Error generating git commit date: {}", + String::from_utf8(git_out.stderr).unwrap() + ); + } + } + + MetricsReport { + git_human_readable, + git_revision, + git_commit_date, + date: date(), + results: Vec::new(), + } + } +} + +#[derive(Default)] +pub struct PerformanceTestOverrides { + test_iterations: Option, +} + +impl fmt::Display for PerformanceTestOverrides { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if let Some(test_iterations) = self.test_iterations { + write!(f, "test_iterations = {}", test_iterations)?; + } + + Ok(()) + } +} + +pub struct PerformanceTestControl { + test_timeout: u32, + test_iterations: u32, + num_queues: Option, + queue_size: Option, + net_rx: Option, + fio_ops: Option, + num_boot_vcpus: Option, +} + +impl fmt::Display for PerformanceTestControl { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut output = format!( + "test_timeout = {}s, test_iterations = {}", + self.test_timeout, self.test_iterations + ); + if let Some(o) = self.num_queues { + output = format!("{}, num_queues = {}", output, o); + } + if let Some(o) = self.queue_size { + output = format!("{}, queue_size = {}", output, o); + } + if let Some(o) = self.net_rx { + output = format!("{}, net_rx = {}", output, o); + } + if let Some(o) = &self.fio_ops { + output = format!("{}, fio_ops = {}", output, o); + } + + write!(f, "{}", output) + } +} + +impl PerformanceTestControl { + const fn default() -> Self { + Self { + test_timeout: 10, + test_iterations: 5, + num_queues: None, + queue_size: None, + net_rx: None, + fio_ops: None, + num_boot_vcpus: Some(1), + } + } +} + +/// A performance test should finish within the a certain time-out and +/// return a performance metrics number (including the average number and +/// standard deviation) +struct PerformanceTest { + pub name: &'static str, + pub func_ptr: fn(&PerformanceTestControl) -> f64, + pub control: PerformanceTestControl, + unit_adjuster: fn(f64) -> f64, +} + +impl PerformanceTest { + pub fn run(&self, overrides: &PerformanceTestOverrides) -> PerformanceTestResult { + let mut metrics = Vec::new(); + for _ in 0..overrides + .test_iterations + .unwrap_or(self.control.test_iterations) + { + metrics.push((self.func_ptr)(&self.control)); + } + + let mean = (self.unit_adjuster)(mean(&metrics).unwrap()); + let std_dev = (self.unit_adjuster)(std_deviation(&metrics).unwrap()); + let max = (self.unit_adjuster)(metrics.clone().into_iter().reduce(f64::max).unwrap()); + let min = (self.unit_adjuster)(metrics.clone().into_iter().reduce(f64::min).unwrap()); + + PerformanceTestResult { + name: self.name.to_string(), + mean, + std_dev, + max, + min, + } + } + + // Calculate the timeout for each test + // Note: To cover the setup/cleanup time, 20s is added for each iteration of the test + pub fn calc_timeout(&self, test_iterations: &Option) -> u64 { + ((self.control.test_timeout + 20) * test_iterations.unwrap_or(self.control.test_iterations)) + as u64 + } +} + +fn mean(data: &[f64]) -> Option { + let count = data.len(); + + if count > 0 { + Some(data.iter().sum::() / count as f64) + } else { + None + } +} + +fn std_deviation(data: &[f64]) -> Option { + let count = data.len(); + + if count > 0 { + let mean = mean(data).unwrap(); + let variance = data + .iter() + .map(|value| { + let diff = mean - *value; + diff * diff + }) + .sum::() + / count as f64; + + Some(variance.sqrt()) + } else { + None + } +} + +mod adjuster { + pub fn identity(v: f64) -> f64 { + v + } + + pub fn s_to_ms(v: f64) -> f64 { + v * 1000.0 + } + + pub fn bps_to_gbps(v: f64) -> f64 { + v / (1_000_000_000_f64) + } + + #[allow(non_snake_case)] + pub fn Bps_to_MiBps(v: f64) -> f64 { + v / (1 << 20) as f64 + } +} + +const TEST_LIST: [PerformanceTest; 17] = [ + PerformanceTest { + name: "boot_time_ms", + func_ptr: performance_boot_time, + control: PerformanceTestControl { + test_timeout: 2, + test_iterations: 10, + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::s_to_ms, + }, + PerformanceTest { + name: "boot_time_pmem_ms", + func_ptr: performance_boot_time_pmem, + control: PerformanceTestControl { + test_timeout: 2, + test_iterations: 10, + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::s_to_ms, + }, + PerformanceTest { + name: "boot_time_16_vcpus_ms", + func_ptr: performance_boot_time, + control: PerformanceTestControl { + test_timeout: 2, + test_iterations: 10, + num_boot_vcpus: Some(16), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::s_to_ms, + }, + PerformanceTest { + name: "boot_time_16_vcpus_pmem_ms", + func_ptr: performance_boot_time_pmem, + control: PerformanceTestControl { + test_timeout: 2, + test_iterations: 10, + num_boot_vcpus: Some(16), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::s_to_ms, + }, + PerformanceTest { + name: "virtio_net_latency_us", + func_ptr: performance_net_latency, + control: PerformanceTestControl { + num_queues: Some(2), + queue_size: Some(256), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::identity, + }, + PerformanceTest { + name: "virtio_net_throughput_single_queue_rx_gbps", + func_ptr: performance_net_throughput, + control: PerformanceTestControl { + num_queues: Some(2), + queue_size: Some(256), + net_rx: Some(true), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::bps_to_gbps, + }, + PerformanceTest { + name: "virtio_net_throughput_single_queue_tx_gbps", + func_ptr: performance_net_throughput, + control: PerformanceTestControl { + num_queues: Some(2), + queue_size: Some(256), + net_rx: Some(false), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::bps_to_gbps, + }, + PerformanceTest { + name: "virtio_net_throughput_multi_queue_rx_gbps", + func_ptr: performance_net_throughput, + control: PerformanceTestControl { + num_queues: Some(4), + queue_size: Some(256), + net_rx: Some(true), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::bps_to_gbps, + }, + PerformanceTest { + name: "virtio_net_throughput_multi_queue_tx_gbps", + func_ptr: performance_net_throughput, + control: PerformanceTestControl { + num_queues: Some(4), + queue_size: Some(256), + net_rx: Some(false), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::bps_to_gbps, + }, + PerformanceTest { + name: "block_read_MiBps", + func_ptr: performance_block_io, + control: PerformanceTestControl { + num_queues: Some(1), + queue_size: Some(128), + fio_ops: Some(FioOps::Read), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::Bps_to_MiBps, + }, + PerformanceTest { + name: "block_write_MiBps", + func_ptr: performance_block_io, + control: PerformanceTestControl { + num_queues: Some(1), + queue_size: Some(128), + fio_ops: Some(FioOps::Write), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::Bps_to_MiBps, + }, + PerformanceTest { + name: "block_random_read_MiBps", + func_ptr: performance_block_io, + control: PerformanceTestControl { + num_queues: Some(1), + queue_size: Some(128), + fio_ops: Some(FioOps::RandomRead), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::Bps_to_MiBps, + }, + PerformanceTest { + name: "block_random_write_MiBps", + func_ptr: performance_block_io, + control: PerformanceTestControl { + num_queues: Some(1), + queue_size: Some(128), + fio_ops: Some(FioOps::RandomWrite), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::Bps_to_MiBps, + }, + PerformanceTest { + name: "block_multi_queue_read_MiBps", + func_ptr: performance_block_io, + control: PerformanceTestControl { + num_queues: Some(2), + queue_size: Some(128), + fio_ops: Some(FioOps::Read), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::Bps_to_MiBps, + }, + PerformanceTest { + name: "block_multi_queue_write_MiBps", + func_ptr: performance_block_io, + control: PerformanceTestControl { + num_queues: Some(2), + queue_size: Some(128), + fio_ops: Some(FioOps::Write), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::Bps_to_MiBps, + }, + PerformanceTest { + name: "block_multi_queue_random_read_MiBps", + func_ptr: performance_block_io, + control: PerformanceTestControl { + num_queues: Some(2), + queue_size: Some(128), + fio_ops: Some(FioOps::RandomRead), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::Bps_to_MiBps, + }, + PerformanceTest { + name: "block_multi_queue_random_write_MiBps", + func_ptr: performance_block_io, + control: PerformanceTestControl { + num_queues: Some(2), + queue_size: Some(128), + fio_ops: Some(FioOps::RandomWrite), + ..PerformanceTestControl::default() + }, + unit_adjuster: adjuster::Bps_to_MiBps, + }, +]; + +fn run_test_with_timeout( + test: &'static PerformanceTest, + overrides: &Arc, +) -> Result { + let (sender, receiver) = channel::>(); + let test_iterations = overrides.test_iterations; + let overrides = overrides.clone(); + thread::spawn(move || { + println!( + "Test '{}' running .. (control: {}, overrides: {})", + test.name, test.control, overrides + ); + + let output = match std::panic::catch_unwind(|| test.run(&overrides)) { + Ok(test_result) => { + println!( + "Test '{}' .. ok: mean = {}, std_dev = {}", + test_result.name, test_result.mean, test_result.std_dev + ); + Ok(test_result) + } + Err(_) => Err(Error::TestFailed), + }; + + let _ = sender.send(output); + }); + + // Todo: Need to cleanup/kill all hanging child processes + let test_timeout = test.calc_timeout(&test_iterations); + receiver + .recv_timeout(Duration::from_secs(test_timeout)) + .map_err(|_| { + eprintln!( + "[Error] Test '{}' time-out after {} seconds", + test.name, test_timeout + ); + Error::TestTimeout + })? +} + +fn date() -> String { + let output = test_infra::exec_host_command_output("date"); + String::from_utf8_lossy(&output.stdout).trim().to_string() +} + +fn main() { + let cmd_arguments = ClapCommand::new("performance-metrics") + .version(env!("GIT_HUMAN_READABLE")) + .author(crate_authors!()) + .about("Generate the performance metrics data for Cloud Hypervisor") + .arg( + Arg::new("test-filter") + .long("test-filter") + .help("Filter metrics tests to run based on provided keywords") + .num_args(1) + .required(false), + ) + .arg( + Arg::new("list-tests") + .long("list-tests") + .help("Print the list of availale metrics tests") + .num_args(0) + .action(ArgAction::SetTrue) + .required(false), + ) + .arg( + Arg::new("report-file") + .long("report-file") + .help("Report file. Standard error is used if not specified") + .num_args(1), + ) + .arg( + Arg::new("iterations") + .long("iterations") + .help("Override number of test iterations") + .num_args(1), + ) + .get_matches(); + + // It seems that the tool (ethr) used for testing the virtio-net latency + // is not stable on AArch64, and therefore the latency test is currently + // skipped on AArch64. + let test_list: Vec<&PerformanceTest> = TEST_LIST + .iter() + .filter(|t| !(cfg!(target_arch = "aarch64") && t.name == "virtio_net_latency_us")) + .collect(); + + if cmd_arguments.get_flag("list-tests") { + for test in test_list.iter() { + println!("\"{}\" ({})", test.name, test.control); + } + + return; + } + + let test_filter = match cmd_arguments.get_many::("test-filter") { + Some(s) => s.collect(), + None => Vec::new(), + }; + + // Run performance tests sequentially and report results (in both readable/json format) + let mut metrics_report: MetricsReport = Default::default(); + + init_tests(); + + let overrides = Arc::new(PerformanceTestOverrides { + test_iterations: cmd_arguments + .get_one::("iterations") + .map(|s| s.parse()) + .transpose() + .unwrap_or_default(), + }); + + for test in test_list.iter() { + if test_filter.is_empty() || test_filter.iter().any(|&s| test.name.contains(s)) { + match run_test_with_timeout(test, &overrides) { + Ok(r) => { + metrics_report.results.push(r); + } + Err(e) => { + eprintln!("Aborting test due to error: '{:?}'", e); + std::process::exit(1); + } + }; + } + } + + cleanup_tests(); + + let mut report_file: Box = + if let Some(file) = cmd_arguments.get_one::("report-file") { + Box::new( + std::fs::File::create(std::path::Path::new(file)) + .map_err(|e| { + eprintln!("Error opening report file: {}: {}", file, e); + std::process::exit(1); + }) + .unwrap(), + ) + } else { + Box::new(std::io::stdout()) + }; + + report_file + .write_all( + serde_json::to_string_pretty(&metrics_report) + .unwrap() + .as_bytes(), + ) + .map_err(|e| { + eprintln!("Error writing report file: {}", e); + std::process::exit(1); + }) + .unwrap(); +} diff --git a/vendor/cube-hypervisor/performance-metrics/src/performance_tests.rs b/vendor/cube-hypervisor/performance-metrics/src/performance_tests.rs new file mode 100644 index 0000000..a79bdf4 --- /dev/null +++ b/vendor/cube-hypervisor/performance-metrics/src/performance_tests.rs @@ -0,0 +1,547 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +// Performance tests + +use crate::{mean, PerformanceTestControl}; +use std::fs; +use std::path::PathBuf; +use std::string::String; +use std::thread; +use std::time::Duration; +use test_infra::Error as InfraError; +use test_infra::*; + +#[cfg(target_arch = "x86_64")] +pub const FOCAL_IMAGE_NAME: &str = "focal-server-cloudimg-amd64-custom-20210609-0.raw"; +#[cfg(target_arch = "aarch64")] +pub const FOCAL_IMAGE_NAME: &str = "focal-server-cloudimg-arm64-custom-20210929-0-update-tool.raw"; + +#[allow(dead_code)] +#[derive(Debug)] +enum Error { + BootTimeParse, + Infra(InfraError), +} + +impl From for Error { + fn from(e: InfraError) -> Self { + Self::Infra(e) + } +} + +const BLK_IO_TEST_IMG: &str = "/var/tmp/ch-blk-io-test.img"; + +pub fn init_tests() { + // The test image can not be created on tmpfs (e.g. /tmp) filesystem, + // as tmpfs does not support O_DIRECT + assert!(exec_host_command_output(&format!( + "dd if=/dev/zero of={} bs=1M count=4096", + BLK_IO_TEST_IMG + )) + .status + .success()); +} + +pub fn cleanup_tests() { + fs::remove_file(BLK_IO_TEST_IMG) + .unwrap_or_else(|_| panic!("Failed to remove file '{}'.", BLK_IO_TEST_IMG)); +} + +// Performance tests are expected to be executed sequentially, so we can +// start VM guests with the same IP while putting them on a different +// private network. The default constructor "Guest::new()" does not work +// well, as we can easily create more than 256 VMs from repeating various +// performance tests dozens times in a single run. +fn performance_test_new_guest(disk_config: Box) -> Guest { + Guest::new_from_ip_range(disk_config, "172.19", 0) +} + +const DIRECT_KERNEL_BOOT_CMDLINE: &str = + "root=/dev/vda1 console=hvc0 rw systemd.journald.forward_to_console=1"; + +// Creates the path for direct kernel boot and return the path. +// For x86_64, this function returns the vmlinux kernel path. +// For AArch64, this function returns the PE kernel path. +fn direct_kernel_boot_path() -> PathBuf { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut kernel_path = workload_path; + #[cfg(target_arch = "x86_64")] + kernel_path.push("vmlinux"); + #[cfg(target_arch = "aarch64")] + kernel_path.push("Image"); + + kernel_path +} + +pub fn performance_net_throughput(control: &PerformanceTestControl) -> f64 { + let test_timeout = control.test_timeout; + let rx = control.net_rx.unwrap(); + + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = performance_test_new_guest(Box::new(focal)); + + let num_queues = control.num_queues.unwrap(); + let queue_size = control.queue_size.unwrap(); + let net_params = format!( + "tap=,mac={},ip={},mask=255.255.255.0,num_queues={},queue_size={}", + guest.network.guest_mac, guest.network.host_ip, num_queues, queue_size, + ); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", &format!("boot={}", num_queues)]) + .args(["--memory", "size=4G"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", net_params.as_str()]) + .capture_output() + .verbosity(VerbosityLevel::Warn) + .set_print_cmd(false) + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + measure_virtio_net_throughput(test_timeout, num_queues / 2, &guest, rx).unwrap() + }); + + let _ = child.kill(); + let output = child.wait_with_output().unwrap(); + + match r { + Ok(r) => r, + Err(e) => { + handle_child_output(Err(e), &output); + panic!("test failed!"); + } + } +} + +pub fn performance_net_latency(control: &PerformanceTestControl) -> f64 { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = performance_test_new_guest(Box::new(focal)); + + let num_queues = control.num_queues.unwrap(); + let queue_size = control.queue_size.unwrap(); + let net_params = format!( + "tap=,mac={},ip={},mask=255.255.255.0,num_queues={},queue_size={}", + guest.network.guest_mac, guest.network.host_ip, num_queues, queue_size, + ); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", &format!("boot={}", num_queues)]) + .args(["--memory", "size=4G"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", net_params.as_str()]) + .capture_output() + .verbosity(VerbosityLevel::Warn) + .set_print_cmd(false) + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // 'ethr' tool will measure the latency multiple times with provided test time + let latency = measure_virtio_net_latency(&guest, control.test_timeout).unwrap(); + mean(&latency).unwrap() + }); + + let _ = child.kill(); + let output = child.wait_with_output().unwrap(); + + match r { + Ok(r) => r, + Err(e) => { + handle_child_output(Err(e), &output); + panic!("test failed!"); + } + } +} + +fn parse_boot_time_output(output: &[u8]) -> Result { + std::panic::catch_unwind(|| { + let l: Vec = String::from_utf8_lossy(output) + .lines() + .into_iter() + .filter(|l| l.contains("Debug I/O port: Kernel code")) + .map(|l| l.to_string()) + .collect(); + + assert_eq!( + l.len(), + 2, + "Expecting two matching lines for 'Debug I/O port: Kernel code'" + ); + + let time_stamp_kernel_start = { + let s = l[0].split("--").collect::>(); + assert_eq!( + s.len(), + 2, + "Expecting '--' for the matching line of 'Debug I/O port' output" + ); + + // Sample output: "[Debug I/O port: Kernel code 0x40] 0.096537 seconds" + assert!( + s[1].contains("0x40"), + "Expecting kernel code '0x40' for 'linux_kernel_start' time stamp output" + ); + let t = s[1].split_whitespace().collect::>(); + assert_eq!( + t.len(), + 8, + "Expecting exact '8' words from the 'Debug I/O port' output" + ); + assert!( + t[7].eq("seconds"), + "Expecting 'seconds' as the the last word of the 'Debug I/O port' output" + ); + + t[6].parse::().unwrap() + }; + + let time_stamp_user_start = { + let s = l[1].split("--").collect::>(); + assert_eq!( + s.len(), + 2, + "Expecting '--' for the matching line of 'Debug I/O port' output" + ); + + // Sample output: "Debug I/O port: Kernel code 0x41] 0.198980 seconds" + assert!( + s[1].contains("0x41"), + "Expecting kernel code '0x41' for 'linux_kernel_start' time stamp output" + ); + let t = s[1].split_whitespace().collect::>(); + assert_eq!( + t.len(), + 8, + "Expecting exact '8' words from the 'Debug I/O port' output" + ); + assert!( + t[7].eq("seconds"), + "Expecting 'seconds' as the the last word of the 'Debug I/O port' output" + ); + + t[6].parse::().unwrap() + }; + + time_stamp_user_start - time_stamp_kernel_start + }) + .map_err(|_| { + eprintln!( + "=============== boot-time output ===============\n\n{}\n\n===========end============\n\n", + String::from_utf8_lossy(output) + ); + Error::BootTimeParse + }) +} + +fn measure_boot_time(cmd: &mut GuestCommand, test_timeout: u32) -> Result { + let mut child = cmd + .capture_output() + .verbosity(VerbosityLevel::Warn) + .set_print_cmd(false) + .spawn() + .unwrap(); + + thread::sleep(Duration::new(test_timeout as u64, 0)); + let _ = child.kill(); + let output = child.wait_with_output().unwrap(); + + parse_boot_time_output(&output.stderr).map_err(|e| { + eprintln!( + "\n\n==== Start child stdout ====\n\n{}\n\n==== End child stdout ====", + String::from_utf8_lossy(&output.stdout) + ); + eprintln!( + "\n\n==== Start child stderr ====\n\n{}\n\n==== End child stderr ====", + String::from_utf8_lossy(&output.stderr) + ); + + e + }) +} + +pub fn performance_boot_time(control: &PerformanceTestControl) -> f64 { + let r = std::panic::catch_unwind(|| { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = performance_test_new_guest(Box::new(focal)); + let mut cmd = GuestCommand::new(&guest); + + let c = cmd + .args([ + "--cpus", + &format!("boot={}", control.num_boot_vcpus.unwrap_or(1)), + ]) + .args(["--memory", "size=1G"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--console", "off"]) + .default_disks(); + + measure_boot_time(c, control.test_timeout).unwrap() + }); + + match r { + Ok(r) => r, + Err(_) => { + panic!("test failed!"); + } + } +} + +pub fn performance_boot_time_pmem(control: &PerformanceTestControl) -> f64 { + let r = std::panic::catch_unwind(|| { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = performance_test_new_guest(Box::new(focal)); + let mut cmd = GuestCommand::new(&guest); + let c = cmd + .args([ + "--cpus", + &format!("boot={}", control.num_boot_vcpus.unwrap_or(1)), + ]) + .args(["--memory", "size=1G,hugepages=on"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", "root=/dev/pmem0p1 console=ttyS0 quiet rw"]) + .args(["--console", "off"]) + .args([ + "--pmem", + format!( + "file={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + ]); + + measure_boot_time(c, control.test_timeout).unwrap() + }); + + match r { + Ok(r) => r, + Err(_) => { + panic!("test failed!"); + } + } +} + +pub fn performance_block_io(control: &PerformanceTestControl) -> f64 { + let test_timeout = control.test_timeout; + let num_queues = control.num_queues.unwrap(); + let fio_ops = control.fio_ops.as_ref().unwrap(); + + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = performance_test_new_guest(Box::new(focal)); + let api_socket = guest + .tmp_dir + .as_path() + .join("cloud-hypervisor.sock") + .to_str() + .unwrap() + .to_string(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", &format!("boot={}", num_queues)]) + .args(["--memory", "size=4G"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + format!("path={}", BLK_IO_TEST_IMG).as_str(), + ]) + .default_net() + .args(["--api-socket", &api_socket]) + .capture_output() + .verbosity(VerbosityLevel::Warn) + .set_print_cmd(false) + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + let fio_command = format!( + "sudo fio --filename=/dev/vdc --name=test --output-format=json \ + --direct=1 --bs=4k --ioengine=io_uring --iodepth=64 \ + --rw={} --runtime={} --numjobs={}", + fio_ops, test_timeout, num_queues + ); + let output = guest + .ssh_command(&fio_command) + .map_err(InfraError::SshCommand) + .unwrap(); + + // Parse fio output + parse_fio_output(&output, fio_ops, num_queues).unwrap() + }); + + let _ = child.kill(); + let output = child.wait_with_output().unwrap(); + + match r { + Ok(r) => r, + Err(e) => { + handle_child_output(Err(e), &output); + panic!("test failed!"); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_iperf3_output() { + let output = r#" +{ + "end": { + "sum_sent": { + "start": 0, + "end": 5.000196, + "seconds": 5.000196, + "bytes": 14973836248, + "bits_per_second": 23957198874.604115, + "retransmits": 0, + "sender": false + } + } +} + "#; + assert_eq!( + parse_iperf3_output(output.as_bytes(), true).unwrap(), + 23957198874.604115 + ); + + let output = r#" +{ + "end": { + "sum_received": { + "start": 0, + "end": 5.000626, + "seconds": 5.000626, + "bytes": 24703557800, + "bits_per_second": 39520744482.79, + "sender": true + } + } +} + "#; + assert_eq!( + parse_iperf3_output(output.as_bytes(), false).unwrap(), + 39520744482.79 + ); + } + + #[test] + fn test_parse_ethr_latency_output() { + let output = r#"{"Time":"2022-02-08T03:52:50Z","Title":"","Type":"INFO","Message":"Using destination: 192.168.249.2, ip: 192.168.249.2, port: 8888"} +{"Time":"2022-02-08T03:52:51Z","Title":"","Type":"INFO","Message":"Running latency test: 1000, 1"} +{"Time":"2022-02-08T03:52:51Z","Title":"","Type":"LatencyResult","RemoteAddr":"192.168.249.2","Protocol":"TCP","Avg":"80.712us","Min":"61.677us","P50":"257.014us","P90":"74.418us","P95":"107.283us","P99":"119.309us","P999":"142.100us","P9999":"216.341us","Max":"216.341us"} +{"Time":"2022-02-08T03:52:52Z","Title":"","Type":"LatencyResult","RemoteAddr":"192.168.249.2","Protocol":"TCP","Avg":"79.826us","Min":"55.129us","P50":"598.996us","P90":"73.849us","P95":"106.552us","P99":"122.152us","P999":"142.459us","P9999":"474.280us","Max":"474.280us"} +{"Time":"2022-02-08T03:52:53Z","Title":"","Type":"LatencyResult","RemoteAddr":"192.168.249.2","Protocol":"TCP","Avg":"78.239us","Min":"56.999us","P50":"396.820us","P90":"69.469us","P95":"115.421us","P99":"119.404us","P999":"130.158us","P9999":"258.686us","Max":"258.686us"}"#; + + let ret = parse_ethr_latency_output(output.as_bytes()).unwrap(); + let reference = vec![80.712_f64, 79.826_f64, 78.239_f64]; + assert_eq!(ret, reference); + } + + #[test] + fn test_parse_boot_time_output() { + let output = r#" +cloud-hypervisor: 161.167103ms: INFO:vmm/src/vm.rs:392 -- [Debug I/O port: Kernel code 0x40] 0.132 seconds +cloud-hypervisor: 613.57361ms: INFO:vmm/src/vm.rs:392 -- [Debug I/O port: Kernel code 0x41] 0.5845 seconds + "#; + + assert_eq!(parse_boot_time_output(output.as_bytes()).unwrap(), 0.4525); + } + + #[test] + fn test_parse_fio_output() { + let output = r#" +{ + "jobs" : [ + { + "read" : { + "io_bytes" : 1965273088, + "io_kbytes" : 1919212, + "bw_bytes" : 392976022, + "bw" : 383765, + "iops" : 95941.411718, + "runtime" : 5001, + "total_ios" : 479803, + "short_ios" : 0, + "drop_ios" : 0 + } + } + ] +} +"#; + + let bps = 1965273088_f64 / (5001_f64 / 1000_f64); + assert_eq!( + parse_fio_output(output, &FioOps::RandomRead, 1).unwrap(), + bps + ); + assert_eq!(parse_fio_output(output, &FioOps::Read, 1).unwrap(), bps); + + let output = r#" +{ + "jobs" : [ + { + "write" : { + "io_bytes" : 1172783104, + "io_kbytes" : 1145296, + "bw_bytes" : 234462835, + "bw" : 228967, + "iops" : 57241.903239, + "runtime" : 5002, + "total_ios" : 286324, + "short_ios" : 0, + "drop_ios" : 0 + } + }, + { + "write" : { + "io_bytes" : 1172234240, + "io_kbytes" : 1144760, + "bw_bytes" : 234353106, + "bw" : 228860, + "iops" : 57215.113954, + "runtime" : 5002, + "total_ios" : 286190, + "short_ios" : 0, + "drop_ios" : 0 + } + } + ] +} +"#; + + let bps = 1172783104_f64 / (5002_f64 / 1000_f64) + 1172234240_f64 / (5002_f64 / 1000_f64); + assert_eq!( + parse_fio_output(output, &FioOps::RandomWrite, 2).unwrap(), + bps + ); + assert_eq!(parse_fio_output(output, &FioOps::Write, 2).unwrap(), bps); + } +} diff --git a/vendor/cube-hypervisor/qcow/Cargo.toml b/vendor/cube-hypervisor/qcow/Cargo.toml new file mode 100644 index 0000000..f589ed6 --- /dev/null +++ b/vendor/cube-hypervisor/qcow/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "qcow" +version = "0.1.0" +authors = ["The Chromium OS Authors"] +edition = "2021" +license = "BSD-3-Clause" + +[lib] +path = "src/qcow.rs" + +[dependencies] +byteorder = "1.4.3" +libc = "0.2.137" +log = "0.4.17" +remain = "0.2.5" +vmm-sys-util = "0.12.1" diff --git a/vendor/cube-hypervisor/qcow/src/qcow.rs b/vendor/cube-hypervisor/qcow/src/qcow.rs new file mode 100644 index 0000000..c22e32d --- /dev/null +++ b/vendor/cube-hypervisor/qcow/src/qcow.rs @@ -0,0 +1,2623 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +#[macro_use] +extern crate log; + +mod qcow_raw_file; +mod raw_file; +mod refcount; +mod vec_cache; + +use crate::qcow_raw_file::QcowRawFile; +use crate::refcount::RefCount; +use crate::vec_cache::{CacheMap, Cacheable, VecCache}; +use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; +use libc::{EINVAL, ENOSPC, ENOTSUP}; +use remain::sorted; +use std::cmp::{max, min}; +use std::fmt::{self, Display}; +use std::io::{self, Read, Seek, SeekFrom, Write}; +use std::mem::size_of; +use vmm_sys_util::{ + file_traits::FileSetLen, file_traits::FileSync, seek_hole::SeekHole, write_zeroes::PunchHole, + write_zeroes::WriteZeroesAt, +}; + +pub use crate::raw_file::RawFile; + +#[sorted] +#[derive(Debug)] +pub enum Error { + BackingFilesNotSupported, + CompressedBlocksNotSupported, + EvictingCache(io::Error), + FileTooBig(u64), + GettingFileSize(io::Error), + GettingRefcount(refcount::Error), + InvalidClusterIndex, + InvalidClusterSize, + InvalidIndex, + InvalidL1TableOffset, + InvalidL1TableSize(u32), + InvalidMagic, + InvalidOffset(u64), + InvalidRefcountTableOffset, + InvalidRefcountTableSize(u64), + NoFreeClusters, + NoRefcountClusters, + NotEnoughSpaceForRefcounts, + OpeningFile(io::Error), + ReadingData(io::Error), + ReadingHeader(io::Error), + ReadingPointers(io::Error), + ReadingRefCountBlock(refcount::Error), + ReadingRefCounts(io::Error), + RebuildingRefCounts(io::Error), + RefcountTableOffEnd, + RefcountTableTooLarge, + SeekingFile(io::Error), + SettingFileSize(io::Error), + SettingRefcountRefcount(io::Error), + SizeTooSmallForNumberOfClusters, + TooManyL1Entries(u64), + TooManyRefcounts(u64), + UnsupportedRefcountOrder, + UnsupportedVersion(u32), + WritingData(io::Error), + WritingHeader(io::Error), +} + +pub type Result = std::result::Result; + +impl Display for Error { + #[remain::check] + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + + #[sorted] + match self { + BackingFilesNotSupported => write!(f, "backing files not supported"), + CompressedBlocksNotSupported => write!(f, "compressed blocks not supported"), + EvictingCache(e) => write!(f, "failed to evict cache: {}", e), + FileTooBig(size) => write!( + f, + "file larger than max of {}: {}", + MAX_QCOW_FILE_SIZE, size + ), + GettingFileSize(e) => write!(f, "failed to get file size: {}", e), + GettingRefcount(e) => write!(f, "failed to get refcount: {}", e), + InvalidClusterIndex => write!(f, "invalid cluster index"), + InvalidClusterSize => write!(f, "invalid cluster size"), + InvalidIndex => write!(f, "invalid index"), + InvalidL1TableOffset => write!(f, "invalid L1 table offset"), + InvalidL1TableSize(size) => write!(f, "invalid L1 table size {}", size), + InvalidMagic => write!(f, "invalid magic"), + InvalidOffset(_) => write!(f, "invalid offset"), + InvalidRefcountTableOffset => write!(f, "invalid refcount table offset"), + InvalidRefcountTableSize(size) => write!(f, "invalid refcount table size: {}", size), + NoFreeClusters => write!(f, "no free clusters"), + NoRefcountClusters => write!(f, "no refcount clusters"), + NotEnoughSpaceForRefcounts => write!(f, "not enough space for refcounts"), + OpeningFile(e) => write!(f, "failed to open file: {}", e), + ReadingData(e) => write!(f, "failed to read data: {}", e), + ReadingHeader(e) => write!(f, "failed to read header: {}", e), + ReadingPointers(e) => write!(f, "failed to read pointers: {}", e), + ReadingRefCountBlock(e) => write!(f, "failed to read ref count block: {}", e), + ReadingRefCounts(e) => write!(f, "failed to read ref counts: {}", e), + RebuildingRefCounts(e) => write!(f, "failed to rebuild ref counts: {}", e), + RefcountTableOffEnd => write!(f, "refcount table offset past file end"), + RefcountTableTooLarge => write!(f, "too many clusters specified for refcount table"), + SeekingFile(e) => write!(f, "failed to seek file: {}", e), + SettingFileSize(e) => write!(f, "failed to set file size: {}", e), + SettingRefcountRefcount(e) => write!(f, "failed to set refcount refcount: {}", e), + SizeTooSmallForNumberOfClusters => write!(f, "size too small for number of clusters"), + TooManyL1Entries(count) => write!(f, "l1 entry table too large: {}", count), + TooManyRefcounts(count) => write!(f, "ref count table too large: {}", count), + UnsupportedRefcountOrder => write!(f, "unsupported refcount order"), + UnsupportedVersion(v) => write!(f, "unsupported version: {}", v), + WritingData(e) => write!(f, "failed to write data: {}", e), + WritingHeader(e) => write!(f, "failed to write header: {}", e), + } + } +} + +pub enum ImageType { + Raw, + Qcow2, +} + +// Maximum data size supported. +const MAX_QCOW_FILE_SIZE: u64 = 0x01 << 44; // 16 TB. + +// QCOW magic constant that starts the header. +const QCOW_MAGIC: u32 = 0x5146_49fb; +// Default to a cluster size of 2^DEFAULT_CLUSTER_BITS +const DEFAULT_CLUSTER_BITS: u32 = 16; +// Limit clusters to reasonable sizes. Choose the same limits as qemu. Making the clusters smaller +// increases the amount of overhead for book keeping. +const MIN_CLUSTER_BITS: u32 = 9; +const MAX_CLUSTER_BITS: u32 = 21; +// The L1 and RefCount table are kept in RAM, only handle files that require less than 35M entries. +// This easily covers 1 TB files. When support for bigger files is needed the assumptions made to +// keep these tables in RAM needs to be thrown out. +const MAX_RAM_POINTER_TABLE_SIZE: u64 = 35_000_000; +// Only support 2 byte refcounts, 2^refcount_order bits. +const DEFAULT_REFCOUNT_ORDER: u32 = 4; + +const V2_BARE_HEADER_SIZE: u32 = 72; +const V3_BARE_HEADER_SIZE: u32 = 104; + +// bits 0-8 and 56-63 are reserved. +const L1_TABLE_OFFSET_MASK: u64 = 0x00ff_ffff_ffff_fe00; +const L2_TABLE_OFFSET_MASK: u64 = 0x00ff_ffff_ffff_fe00; +// Flags +const COMPRESSED_FLAG: u64 = 1 << 62; +const CLUSTER_USED_FLAG: u64 = 1 << 63; +const COMPATIBLE_FEATURES_LAZY_REFCOUNTS: u64 = 1; + +/// Contains the information from the header of a qcow file. +#[derive(Copy, Clone, Debug)] +pub struct QcowHeader { + pub magic: u32, + pub version: u32, + + pub backing_file_offset: u64, + pub backing_file_size: u32, + + pub cluster_bits: u32, + pub size: u64, + pub crypt_method: u32, + + pub l1_size: u32, + pub l1_table_offset: u64, + + pub refcount_table_offset: u64, + pub refcount_table_clusters: u32, + + pub nb_snapshots: u32, + pub snapshots_offset: u64, + + // v3 entries + pub incompatible_features: u64, + pub compatible_features: u64, + pub autoclear_features: u64, + pub refcount_order: u32, + pub header_size: u32, +} + +impl QcowHeader { + /// Creates a QcowHeader from a reference to a file. + pub fn new(f: &mut RawFile) -> Result { + f.seek(SeekFrom::Start(0)).map_err(Error::ReadingHeader)?; + let magic = f.read_u32::().map_err(Error::ReadingHeader)?; + if magic != QCOW_MAGIC { + return Err(Error::InvalidMagic); + } + + // Reads the next u32 from the file. + fn read_u32_from_file(f: &mut RawFile) -> Result { + f.read_u32::().map_err(Error::ReadingHeader) + } + + // Reads the next u64 from the file. + fn read_u64_from_file(f: &mut RawFile) -> Result { + f.read_u64::().map_err(Error::ReadingHeader) + } + + let version = read_u32_from_file(f)?; + + Ok(QcowHeader { + magic, + version, + backing_file_offset: read_u64_from_file(f)?, + backing_file_size: read_u32_from_file(f)?, + cluster_bits: read_u32_from_file(f)?, + size: read_u64_from_file(f)?, + crypt_method: read_u32_from_file(f)?, + l1_size: read_u32_from_file(f)?, + l1_table_offset: read_u64_from_file(f)?, + refcount_table_offset: read_u64_from_file(f)?, + refcount_table_clusters: read_u32_from_file(f)?, + nb_snapshots: read_u32_from_file(f)?, + snapshots_offset: read_u64_from_file(f)?, + incompatible_features: if version == 2 { + 0 + } else { + read_u64_from_file(f)? + }, + compatible_features: if version == 2 { + 0 + } else { + read_u64_from_file(f)? + }, + autoclear_features: if version == 2 { + 0 + } else { + read_u64_from_file(f)? + }, + refcount_order: if version == 2 { + DEFAULT_REFCOUNT_ORDER + } else { + read_u32_from_file(f)? + }, + header_size: if version == 2 { + V2_BARE_HEADER_SIZE + } else { + read_u32_from_file(f)? + }, + }) + } + + /// Create a header for the given `size`. + pub fn create_for_size(version: u32, size: u64) -> QcowHeader { + let cluster_bits: u32 = DEFAULT_CLUSTER_BITS; + let cluster_size: u32 = 0x01 << cluster_bits; + // L2 blocks are always one cluster long. They contain cluster_size/sizeof(u64) addresses. + let entries_per_cluster: u32 = cluster_size / size_of::() as u32; + let num_clusters: u32 = div_round_up_u64(size, u64::from(cluster_size)) as u32; + let num_l2_clusters: u32 = div_round_up_u32(num_clusters, entries_per_cluster); + let l1_clusters: u32 = div_round_up_u32(num_l2_clusters, entries_per_cluster); + let header_clusters = div_round_up_u32(size_of::() as u32, cluster_size); + QcowHeader { + magic: QCOW_MAGIC, + version, + backing_file_offset: 0, + backing_file_size: 0, + cluster_bits: DEFAULT_CLUSTER_BITS, + size, + crypt_method: 0, + l1_size: num_l2_clusters, + l1_table_offset: u64::from(cluster_size), + // The refcount table is after l1 + header. + refcount_table_offset: u64::from(cluster_size * (l1_clusters + 1)), + refcount_table_clusters: { + // Pre-allocate enough clusters for the entire refcount table as it must be + // continuous in the file. Allocate enough space to refcount all clusters, including + // the refcount clusters. + let max_refcount_clusters = max_refcount_clusters( + DEFAULT_REFCOUNT_ORDER, + cluster_size, + num_clusters + l1_clusters + num_l2_clusters + header_clusters, + ) as u32; + // The refcount table needs to store the offset of each refcount cluster. + div_round_up_u32( + max_refcount_clusters * size_of::() as u32, + cluster_size, + ) + }, + nb_snapshots: 0, + snapshots_offset: 0, + incompatible_features: 0, + compatible_features: 0, + autoclear_features: 0, + refcount_order: DEFAULT_REFCOUNT_ORDER, + header_size: if version == 2 { + V2_BARE_HEADER_SIZE + } else { + V3_BARE_HEADER_SIZE + }, + } + } + + /// Write the header to `file`. + pub fn write_to(&self, file: &mut F) -> Result<()> { + // Writes the next u32 to the file. + fn write_u32_to_file(f: &mut F, value: u32) -> Result<()> { + f.write_u32::(value) + .map_err(Error::WritingHeader) + } + + // Writes the next u64 to the file. + fn write_u64_to_file(f: &mut F, value: u64) -> Result<()> { + f.write_u64::(value) + .map_err(Error::WritingHeader) + } + + write_u32_to_file(file, self.magic)?; + write_u32_to_file(file, self.version)?; + write_u64_to_file(file, self.backing_file_offset)?; + write_u32_to_file(file, self.backing_file_size)?; + write_u32_to_file(file, self.cluster_bits)?; + write_u64_to_file(file, self.size)?; + write_u32_to_file(file, self.crypt_method)?; + write_u32_to_file(file, self.l1_size)?; + write_u64_to_file(file, self.l1_table_offset)?; + write_u64_to_file(file, self.refcount_table_offset)?; + write_u32_to_file(file, self.refcount_table_clusters)?; + write_u32_to_file(file, self.nb_snapshots)?; + write_u64_to_file(file, self.snapshots_offset)?; + write_u64_to_file(file, self.incompatible_features)?; + write_u64_to_file(file, self.compatible_features)?; + write_u64_to_file(file, self.autoclear_features)?; + write_u32_to_file(file, self.refcount_order)?; + write_u32_to_file(file, self.header_size)?; + + // Set the file length by seeking and writing a zero to the last byte. This avoids needing + // a `File` instead of anything that implements seek as the `file` argument. + // Zeros out the l1 and refcount table clusters. + let cluster_size = 0x01u64 << self.cluster_bits; + let refcount_blocks_size = u64::from(self.refcount_table_clusters) * cluster_size; + file.seek(SeekFrom::Start( + self.refcount_table_offset + refcount_blocks_size - 2, + )) + .map_err(Error::WritingHeader)?; + file.write(&[0u8]).map_err(Error::WritingHeader)?; + + Ok(()) + } +} + +fn max_refcount_clusters(refcount_order: u32, cluster_size: u32, num_clusters: u32) -> u64 { + // Use u64 as the product of the u32 inputs can overflow. + let refcount_bytes = (0x01 << u64::from(refcount_order)) / 8; + let for_data = div_round_up_u64( + u64::from(num_clusters) * refcount_bytes, + u64::from(cluster_size), + ); + let for_refcounts = div_round_up_u64(for_data * refcount_bytes, u64::from(cluster_size)); + for_data + for_refcounts +} + +/// Represents a qcow2 file. This is a sparse file format maintained by the qemu project. +/// Full documentation of the format can be found in the qemu repository. +/// +/// # Example +/// +/// ``` +/// # use std::io::{Read, Seek, SeekFrom}; +/// # use qcow::{self, QcowFile, RawFile}; +/// # fn test(file: std::fs::File) -> std::io::Result<()> { +/// let mut raw_img = RawFile::new(file, false); +/// let mut q = QcowFile::from(raw_img).expect("Can't open qcow file"); +/// let mut buf = [0u8; 12]; +/// q.seek(SeekFrom::Start(10 as u64))?; +/// q.read(&mut buf[..])?; +/// # Ok(()) +/// # } +/// ``` +#[derive(Clone, Debug)] +pub struct QcowFile { + raw_file: QcowRawFile, + header: QcowHeader, + l1_table: VecCache, + l2_entries: u64, + l2_cache: CacheMap>, + refcounts: RefCount, + current_offset: u64, + unref_clusters: Vec, // List of freshly unreferenced clusters. + // List of unreferenced clusters available to be used. unref clusters become available once the + // removal of references to them have been synced to disk. + avail_clusters: Vec, + //TODO(dgreid) Add support for backing files. - backing_file: Option>>, +} + +impl QcowFile { + /// Creates a QcowFile from `file`. File must be a valid qcow2 image. + pub fn from(mut file: RawFile) -> Result { + let header = QcowHeader::new(&mut file)?; + + // Only v2 and v3 files are supported. + if header.version != 2 && header.version != 3 { + return Err(Error::UnsupportedVersion(header.version)); + } + + // Make sure that the L1 table fits in RAM. + if u64::from(header.l1_size) > MAX_RAM_POINTER_TABLE_SIZE { + return Err(Error::InvalidL1TableSize(header.l1_size)); + } + + let cluster_bits: u32 = header.cluster_bits; + if !(MIN_CLUSTER_BITS..=MAX_CLUSTER_BITS).contains(&cluster_bits) { + return Err(Error::InvalidClusterSize); + } + let cluster_size = 0x01u64 << cluster_bits; + + // Limit the total size of the disk. + if header.size > MAX_QCOW_FILE_SIZE { + return Err(Error::FileTooBig(header.size)); + } + + // No current support for backing files. + if header.backing_file_offset != 0 { + return Err(Error::BackingFilesNotSupported); + } + + // Only support two byte refcounts. + let refcount_bits: u64 = 0x01u64 + .checked_shl(header.refcount_order) + .ok_or(Error::UnsupportedRefcountOrder)?; + if refcount_bits != 16 { + return Err(Error::UnsupportedRefcountOrder); + } + let refcount_bytes = (refcount_bits + 7) / 8; + + // Need at least one refcount cluster + if header.refcount_table_clusters == 0 { + return Err(Error::NoRefcountClusters); + } + offset_is_cluster_boundary(header.backing_file_offset, header.cluster_bits)?; + offset_is_cluster_boundary(header.l1_table_offset, header.cluster_bits)?; + offset_is_cluster_boundary(header.snapshots_offset, header.cluster_bits)?; + // refcount table must be a cluster boundary, and within the file's virtual or actual size. + offset_is_cluster_boundary(header.refcount_table_offset, header.cluster_bits)?; + let file_size = file.metadata().map_err(Error::GettingFileSize)?.len(); + if header.refcount_table_offset > max(file_size, header.size) { + return Err(Error::RefcountTableOffEnd); + } + + // The first cluster should always have a non-zero refcount, so if it is 0, + // this is an old file with broken refcounts, which requires a rebuild. + let mut refcount_rebuild_required = true; + file.seek(SeekFrom::Start(header.refcount_table_offset)) + .map_err(Error::SeekingFile)?; + let first_refblock_addr = file.read_u64::().map_err(Error::ReadingHeader)?; + if first_refblock_addr != 0 { + file.seek(SeekFrom::Start(first_refblock_addr)) + .map_err(Error::SeekingFile)?; + let first_cluster_refcount = + file.read_u16::().map_err(Error::ReadingHeader)?; + if first_cluster_refcount != 0 { + refcount_rebuild_required = false; + } + } + + if (header.compatible_features & COMPATIBLE_FEATURES_LAZY_REFCOUNTS) != 0 { + refcount_rebuild_required = true; + } + + let mut raw_file = + QcowRawFile::from(file, cluster_size).ok_or(Error::InvalidClusterSize)?; + if refcount_rebuild_required { + QcowFile::rebuild_refcounts(&mut raw_file, header)?; + } + + let entries_per_cluster = cluster_size / size_of::() as u64; + let num_clusters = div_round_up_u64(header.size, cluster_size); + let num_l2_clusters = div_round_up_u64(num_clusters, entries_per_cluster); + let l1_clusters = div_round_up_u64(num_l2_clusters, entries_per_cluster); + let header_clusters = div_round_up_u64(size_of::() as u64, cluster_size); + if num_l2_clusters > MAX_RAM_POINTER_TABLE_SIZE { + return Err(Error::TooManyL1Entries(num_l2_clusters)); + } + let l1_table = VecCache::from_vec( + raw_file + .read_pointer_table( + header.l1_table_offset, + num_l2_clusters, + Some(L1_TABLE_OFFSET_MASK), + ) + .map_err(Error::ReadingHeader)?, + ); + + let num_clusters = div_round_up_u64(header.size, cluster_size); + let refcount_clusters = max_refcount_clusters( + header.refcount_order, + cluster_size as u32, + (num_clusters + l1_clusters + num_l2_clusters + header_clusters) as u32, + ); + // Check that the given header doesn't have a suspiciously sized refcount table. + if u64::from(header.refcount_table_clusters) > 2 * refcount_clusters { + return Err(Error::RefcountTableTooLarge); + } + if l1_clusters + refcount_clusters > MAX_RAM_POINTER_TABLE_SIZE { + return Err(Error::TooManyRefcounts(refcount_clusters)); + } + let refcount_block_entries = cluster_size / refcount_bytes; + let refcounts = RefCount::new( + &mut raw_file, + header.refcount_table_offset, + refcount_clusters, + refcount_block_entries, + cluster_size, + ) + .map_err(Error::ReadingRefCounts)?; + + let l2_entries = cluster_size / size_of::() as u64; + + // Check for compressed blocks + for l2_addr_disk in l1_table.get_values() { + if *l2_addr_disk != 0 { + if let Err(e) = Self::read_l2_cluster(&mut raw_file, *l2_addr_disk) { + if let Some(os_error) = e.raw_os_error() { + if os_error == ENOTSUP { + return Err(Error::CompressedBlocksNotSupported); + } + } + } + } + } + + let mut qcow = QcowFile { + raw_file, + header, + l1_table, + l2_entries, + l2_cache: CacheMap::new(100), + refcounts, + current_offset: 0, + unref_clusters: Vec::new(), + avail_clusters: Vec::new(), + }; + + // Check that the L1 and refcount tables fit in a 64bit address space. + qcow.header + .l1_table_offset + .checked_add(qcow.l1_address_offset(qcow.virtual_size())) + .ok_or(Error::InvalidL1TableOffset)?; + qcow.header + .refcount_table_offset + .checked_add(u64::from(qcow.header.refcount_table_clusters) * cluster_size) + .ok_or(Error::InvalidRefcountTableOffset)?; + + qcow.find_avail_clusters()?; + + Ok(qcow) + } + + /// Creates a new QcowFile at the given path. + pub fn new(mut file: RawFile, version: u32, virtual_size: u64) -> Result { + let header = QcowHeader::create_for_size(version, virtual_size); + file.seek(SeekFrom::Start(0)).map_err(Error::SeekingFile)?; + header.write_to(&mut file)?; + + let mut qcow = Self::from(file)?; + + // Set the refcount for each refcount table cluster. + let cluster_size = 0x01u64 << qcow.header.cluster_bits; + let refcount_table_base = qcow.header.refcount_table_offset; + let end_cluster_addr = + refcount_table_base + u64::from(qcow.header.refcount_table_clusters) * cluster_size; + + let mut cluster_addr = 0; + while cluster_addr < end_cluster_addr { + let mut unref_clusters = qcow + .set_cluster_refcount(cluster_addr, 1) + .map_err(Error::SettingRefcountRefcount)?; + qcow.unref_clusters.append(&mut unref_clusters); + cluster_addr += cluster_size; + } + + Ok(qcow) + } + + /// Returns the `QcowHeader` for this file. + pub fn header(&self) -> &QcowHeader { + &self.header + } + + /// Returns the L1 lookup table for this file. This is only useful for debugging. + pub fn l1_table(&self) -> &[u64] { + self.l1_table.get_values() + } + + /// Returns an L2_table of cluster addresses, only used for debugging. + pub fn l2_table(&mut self, l1_index: usize) -> Result> { + let l2_addr_disk = *self.l1_table.get(l1_index).ok_or(Error::InvalidIndex)?; + + if l2_addr_disk == 0 { + // Reading from an unallocated cluster will return zeros. + return Ok(None); + } + + if !self.l2_cache.contains_key(l1_index) { + // Not in the cache. + let table = VecCache::from_vec( + Self::read_l2_cluster(&mut self.raw_file, l2_addr_disk) + .map_err(Error::ReadingPointers)?, + ); + let l1_table = &self.l1_table; + let raw_file = &mut self.raw_file; + self.l2_cache + .insert(l1_index, table, |index, evicted| { + raw_file.write_pointer_table( + l1_table[index], + evicted.get_values(), + CLUSTER_USED_FLAG, + ) + }) + .map_err(Error::EvictingCache)?; + } + + // The index must exist as it was just inserted if it didn't already. + Ok(Some(self.l2_cache.get(l1_index).unwrap().get_values())) + } + + /// Returns the refcount table for this file. This is only useful for debugging. + pub fn ref_table(&self) -> &[u64] { + self.refcounts.ref_table() + } + + /// Returns the `index`th refcount block from the file. + pub fn refcount_block(&mut self, index: usize) -> Result> { + self.refcounts + .refcount_block(&mut self.raw_file, index) + .map_err(Error::ReadingRefCountBlock) + } + + /// Returns the first cluster in the file with a 0 refcount. Used for testing. + pub fn first_zero_refcount(&mut self) -> Result> { + let file_size = self + .raw_file + .file_mut() + .metadata() + .map_err(Error::GettingFileSize)? + .len(); + let cluster_size = 0x01u64 << self.header.cluster_bits; + + let mut cluster_addr = 0; + while cluster_addr < file_size { + let cluster_refcount = self + .refcounts + .get_cluster_refcount(&mut self.raw_file, cluster_addr) + .map_err(Error::GettingRefcount)?; + if cluster_refcount == 0 { + return Ok(Some(cluster_addr)); + } + cluster_addr += cluster_size; + } + Ok(None) + } + + fn find_avail_clusters(&mut self) -> Result<()> { + let cluster_size = self.raw_file.cluster_size(); + + let file_size = self + .raw_file + .file_mut() + .metadata() + .map_err(Error::GettingFileSize)? + .len(); + + for i in (0..file_size).step_by(cluster_size as usize) { + let refcount = self + .refcounts + .get_cluster_refcount(&mut self.raw_file, i) + .map_err(Error::GettingRefcount)?; + if refcount == 0 { + self.avail_clusters.push(i); + } + } + + Ok(()) + } + + /// Rebuild the reference count tables. + fn rebuild_refcounts(raw_file: &mut QcowRawFile, header: QcowHeader) -> Result<()> { + fn add_ref(refcounts: &mut [u16], cluster_size: u64, cluster_address: u64) -> Result<()> { + let idx = (cluster_address / cluster_size) as usize; + if idx >= refcounts.len() { + return Err(Error::InvalidClusterIndex); + } + refcounts[idx] += 1; + Ok(()) + } + + // Add a reference to the first cluster (header plus extensions). + fn set_header_refcount(refcounts: &mut [u16], cluster_size: u64) -> Result<()> { + add_ref(refcounts, cluster_size, 0) + } + + // Add references to the L1 table clusters. + fn set_l1_refcounts( + refcounts: &mut [u16], + header: QcowHeader, + cluster_size: u64, + ) -> Result<()> { + let entries_per_cluster = cluster_size / size_of::() as u64; + let l1_clusters = div_round_up_u64(u64::from(header.l1_size), entries_per_cluster); + let l1_table_offset = header.l1_table_offset; + for i in 0..l1_clusters { + add_ref(refcounts, cluster_size, l1_table_offset + i * cluster_size)?; + } + Ok(()) + } + + // Traverse the L1 and L2 tables to find all reachable data clusters. + fn set_data_refcounts( + refcounts: &mut [u16], + header: QcowHeader, + cluster_size: u64, + raw_file: &mut QcowRawFile, + ) -> Result<()> { + let l1_table = raw_file + .read_pointer_table( + header.l1_table_offset, + u64::from(header.l1_size), + Some(L1_TABLE_OFFSET_MASK), + ) + .map_err(Error::ReadingPointers)?; + for l1_index in 0..header.l1_size as usize { + let l2_addr_disk = *l1_table.get(l1_index).ok_or(Error::InvalidIndex)?; + if l2_addr_disk != 0 { + // Add a reference to the L2 table cluster itself. + add_ref(refcounts, cluster_size, l2_addr_disk)?; + + // Read the L2 table and find all referenced data clusters. + let l2_table = raw_file + .read_pointer_table( + l2_addr_disk, + cluster_size / size_of::() as u64, + Some(L2_TABLE_OFFSET_MASK), + ) + .map_err(Error::ReadingPointers)?; + for data_cluster_addr in l2_table { + if data_cluster_addr != 0 { + add_ref(refcounts, cluster_size, data_cluster_addr)?; + } + } + } + } + + Ok(()) + } + + // Add references to the top-level refcount table clusters. + fn set_refcount_table_refcounts( + refcounts: &mut [u16], + header: QcowHeader, + cluster_size: u64, + ) -> Result<()> { + let refcount_table_offset = header.refcount_table_offset; + for i in 0..u64::from(header.refcount_table_clusters) { + add_ref( + refcounts, + cluster_size, + refcount_table_offset + i * cluster_size, + )?; + } + Ok(()) + } + + // Allocate clusters for refblocks. + // This needs to be done last so that we have the correct refcounts for all other + // clusters. + fn alloc_refblocks( + refcounts: &mut [u16], + cluster_size: u64, + refblock_clusters: u64, + pointers_per_cluster: u64, + ) -> Result> { + let refcount_table_entries = div_round_up_u64(refblock_clusters, pointers_per_cluster); + let mut ref_table = vec![0; refcount_table_entries as usize]; + let mut first_free_cluster: u64 = 0; + for refblock_addr in &mut ref_table { + loop { + if first_free_cluster >= refcounts.len() as u64 { + return Err(Error::NotEnoughSpaceForRefcounts); + } + if refcounts[first_free_cluster as usize] == 0 { + break; + } + first_free_cluster += 1; + } + + *refblock_addr = first_free_cluster * cluster_size; + add_ref(refcounts, cluster_size, *refblock_addr)?; + + first_free_cluster += 1; + } + + Ok(ref_table) + } + + // Write the updated reference count blocks and reftable. + fn write_refblocks( + refcounts: &[u16], + mut header: QcowHeader, + ref_table: &[u64], + raw_file: &mut QcowRawFile, + refcount_block_entries: u64, + ) -> Result<()> { + // Rewrite the header with lazy refcounts enabled while we are rebuilding the tables. + header.compatible_features |= COMPATIBLE_FEATURES_LAZY_REFCOUNTS; + raw_file + .file_mut() + .seek(SeekFrom::Start(0)) + .map_err(Error::SeekingFile)?; + header.write_to(raw_file.file_mut())?; + + for (i, refblock_addr) in ref_table.iter().enumerate() { + // Write a block of refcounts to the location indicated by refblock_addr. + let refblock_start = i * (refcount_block_entries as usize); + let refblock_end = min( + refcounts.len(), + refblock_start + refcount_block_entries as usize, + ); + let refblock = &refcounts[refblock_start..refblock_end]; + raw_file + .write_refcount_block(*refblock_addr, refblock) + .map_err(Error::WritingHeader)?; + + // If this is the last (partial) cluster, pad it out to a full refblock cluster. + if refblock.len() < refcount_block_entries as usize { + let refblock_padding = + vec![0u16; refcount_block_entries as usize - refblock.len()]; + raw_file + .write_refcount_block( + *refblock_addr + refblock.len() as u64 * 2, + &refblock_padding, + ) + .map_err(Error::WritingHeader)?; + } + } + + // Rewrite the top-level refcount table. + raw_file + .write_pointer_table(header.refcount_table_offset, ref_table, 0) + .map_err(Error::WritingHeader)?; + + // Rewrite the header again, now with lazy refcounts disabled. + header.compatible_features &= !COMPATIBLE_FEATURES_LAZY_REFCOUNTS; + raw_file + .file_mut() + .seek(SeekFrom::Start(0)) + .map_err(Error::SeekingFile)?; + header.write_to(raw_file.file_mut())?; + + Ok(()) + } + + let cluster_size = raw_file.cluster_size(); + + let file_size = raw_file + .file_mut() + .metadata() + .map_err(Error::GettingFileSize)? + .len(); + + let refcount_bits = 1u64 << header.refcount_order; + let refcount_bytes = div_round_up_u64(refcount_bits, 8); + let refcount_block_entries = cluster_size / refcount_bytes; + let pointers_per_cluster = cluster_size / size_of::() as u64; + let data_clusters = div_round_up_u64(header.size, cluster_size); + let l2_clusters = div_round_up_u64(data_clusters, pointers_per_cluster); + let l1_clusters = div_round_up_u64(l2_clusters, pointers_per_cluster); + let header_clusters = div_round_up_u64(size_of::() as u64, cluster_size); + let max_clusters = data_clusters + l2_clusters + l1_clusters + header_clusters; + let mut max_valid_cluster_index = max_clusters; + let refblock_clusters = div_round_up_u64(max_valid_cluster_index, refcount_block_entries); + let reftable_clusters = div_round_up_u64(refblock_clusters, pointers_per_cluster); + // Account for refblocks and the ref table size needed to address them. + let refblocks_for_refs = div_round_up_u64( + refblock_clusters + reftable_clusters, + refcount_block_entries, + ); + let reftable_clusters_for_refs = + div_round_up_u64(refblocks_for_refs, refcount_block_entries); + max_valid_cluster_index += refblock_clusters + reftable_clusters; + max_valid_cluster_index += refblocks_for_refs + reftable_clusters_for_refs; + + if max_valid_cluster_index > MAX_RAM_POINTER_TABLE_SIZE { + return Err(Error::InvalidRefcountTableSize(max_valid_cluster_index)); + } + + let max_valid_cluster_offset = max_valid_cluster_index * cluster_size; + if max_valid_cluster_offset < file_size - cluster_size { + return Err(Error::InvalidRefcountTableSize(max_valid_cluster_offset)); + } + + let mut refcounts = vec![0; max_valid_cluster_index as usize]; + + // Find all references clusters and rebuild refcounts. + set_header_refcount(&mut refcounts, cluster_size)?; + set_l1_refcounts(&mut refcounts, header, cluster_size)?; + set_data_refcounts(&mut refcounts, header, cluster_size, raw_file)?; + set_refcount_table_refcounts(&mut refcounts, header, cluster_size)?; + + // Allocate clusters to store the new reference count blocks. + let ref_table = alloc_refblocks( + &mut refcounts, + cluster_size, + refblock_clusters, + pointers_per_cluster, + )?; + + // Write updated reference counts and point the reftable at them. + write_refblocks( + &refcounts, + header, + &ref_table, + raw_file, + refcount_block_entries, + ) + } + + // Limits the range so that it doesn't exceed the virtual size of the file. + fn limit_range_file(&self, address: u64, count: usize) -> usize { + if address.checked_add(count as u64).is_none() || address > self.virtual_size() { + return 0; + } + min(count as u64, self.virtual_size() - address) as usize + } + + // Limits the range so that it doesn't overflow the end of a cluster. + fn limit_range_cluster(&self, address: u64, count: usize) -> usize { + let offset: u64 = self.raw_file.cluster_offset(address); + let limit = self.raw_file.cluster_size() - offset; + min(count as u64, limit) as usize + } + + // Gets the maximum virtual size of this image. + fn virtual_size(&self) -> u64 { + self.header.size + } + + // Gets the offset of `address` in the L1 table. + fn l1_address_offset(&self, address: u64) -> u64 { + let l1_index = self.l1_table_index(address); + l1_index * size_of::() as u64 + } + + // Gets the offset of `address` in the L1 table. + fn l1_table_index(&self, address: u64) -> u64 { + (address / self.raw_file.cluster_size()) / self.l2_entries + } + + // Gets the offset of `address` in the L2 table. + fn l2_table_index(&self, address: u64) -> u64 { + (address / self.raw_file.cluster_size()) % self.l2_entries + } + + // Gets the offset of the given guest address in the host file. If L1, L2, or data clusters have + // yet to be allocated, return None. + fn file_offset_read(&mut self, address: u64) -> std::io::Result> { + if address >= self.virtual_size() { + return Err(std::io::Error::from_raw_os_error(EINVAL)); + } + + let l1_index = self.l1_table_index(address) as usize; + let l2_addr_disk = *self + .l1_table + .get(l1_index) + .ok_or_else(|| std::io::Error::from_raw_os_error(EINVAL))?; + + if l2_addr_disk == 0 { + // Reading from an unallocated cluster will return zeros. + return Ok(None); + } + + let l2_index = self.l2_table_index(address) as usize; + + if !self.l2_cache.contains_key(l1_index) { + // Not in the cache. + let table = + VecCache::from_vec(Self::read_l2_cluster(&mut self.raw_file, l2_addr_disk)?); + + let l1_table = &self.l1_table; + let raw_file = &mut self.raw_file; + self.l2_cache.insert(l1_index, table, |index, evicted| { + raw_file.write_pointer_table( + l1_table[index], + evicted.get_values(), + CLUSTER_USED_FLAG, + ) + })?; + }; + + let cluster_addr = self.l2_cache.get(l1_index).unwrap()[l2_index]; + if cluster_addr == 0 { + return Ok(None); + } + Ok(Some(cluster_addr + self.raw_file.cluster_offset(address))) + } + + // Gets the offset of the given guest address in the host file. If L1, L2, or data clusters need + // to be allocated, they will be. + fn file_offset_write(&mut self, address: u64) -> std::io::Result { + if address >= self.virtual_size() { + return Err(std::io::Error::from_raw_os_error(EINVAL)); + } + + let l1_index = self.l1_table_index(address) as usize; + let l2_addr_disk = *self + .l1_table + .get(l1_index) + .ok_or_else(|| std::io::Error::from_raw_os_error(EINVAL))?; + let l2_index = self.l2_table_index(address) as usize; + + let mut set_refcounts = Vec::new(); + + if !self.l2_cache.contains_key(l1_index) { + // Not in the cache. + let l2_table = if l2_addr_disk == 0 { + // Allocate a new cluster to store the L2 table and update the L1 table to point + // to the new table. + let new_addr: u64 = self.get_new_cluster()?; + // The cluster refcount starts at one meaning it is used but doesn't need COW. + set_refcounts.push((new_addr, 1)); + self.l1_table[l1_index] = new_addr; + VecCache::new(self.l2_entries as usize) + } else { + VecCache::from_vec(Self::read_l2_cluster(&mut self.raw_file, l2_addr_disk)?) + }; + let l1_table = &self.l1_table; + let raw_file = &mut self.raw_file; + self.l2_cache.insert(l1_index, l2_table, |index, evicted| { + raw_file.write_pointer_table( + l1_table[index], + evicted.get_values(), + CLUSTER_USED_FLAG, + ) + })?; + } + + let cluster_addr = match self.l2_cache.get(l1_index).unwrap()[l2_index] { + 0 => { + // Need to allocate a data cluster + let cluster_addr = self.append_data_cluster()?; + self.update_cluster_addr(l1_index, l2_index, cluster_addr, &mut set_refcounts)?; + cluster_addr + } + a => a, + }; + + for (addr, count) in set_refcounts { + let mut newly_unref = self.set_cluster_refcount(addr, count)?; + self.unref_clusters.append(&mut newly_unref); + } + + Ok(cluster_addr + self.raw_file.cluster_offset(address)) + } + + // Updates the l1 and l2 tables to point to the new `cluster_addr`. + fn update_cluster_addr( + &mut self, + l1_index: usize, + l2_index: usize, + cluster_addr: u64, + set_refcounts: &mut Vec<(u64, u16)>, + ) -> io::Result<()> { + if !self.l2_cache.get(l1_index).unwrap().dirty() { + // Free the previously used cluster if one exists. Modified tables are always + // witten to new clusters so the L1 table can be committed to disk after they + // are and L1 never points at an invalid table. + // The index must be valid from when it was inserted. + let addr = self.l1_table[l1_index]; + if addr != 0 { + self.unref_clusters.push(addr); + set_refcounts.push((addr, 0)); + } + + // Allocate a new cluster to store the L2 table and update the L1 table to point + // to the new table. The cluster will be written when the cache is flushed, no + // need to copy the data now. + let new_addr: u64 = self.get_new_cluster()?; + // The cluster refcount starts at one indicating it is used but doesn't need + // COW. + set_refcounts.push((new_addr, 1)); + self.l1_table[l1_index] = new_addr; + } + // 'unwrap' is OK because it was just added. + self.l2_cache.get_mut(l1_index).unwrap()[l2_index] = cluster_addr; + Ok(()) + } + + // Allocate a new cluster and return its offset within the raw file. + fn get_new_cluster(&mut self) -> std::io::Result { + // First use a pre allocated cluster if one is available. + if let Some(free_cluster) = self.avail_clusters.pop() { + self.raw_file.zero_cluster(free_cluster)?; + return Ok(free_cluster); + } + + let max_valid_cluster_offset = self.refcounts.max_valid_cluster_offset(); + if let Some(new_cluster) = self.raw_file.add_cluster_end(max_valid_cluster_offset)? { + Ok(new_cluster) + } else { + error!("No free clusters in get_new_cluster()"); + Err(std::io::Error::from_raw_os_error(ENOSPC)) + } + } + + // Allocate and initialize a new data cluster. Returns the offset of the + // cluster in to the file on success. + fn append_data_cluster(&mut self) -> std::io::Result { + let new_addr: u64 = self.get_new_cluster()?; + // The cluster refcount starts at one indicating it is used but doesn't need COW. + let mut newly_unref = self.set_cluster_refcount(new_addr, 1)?; + self.unref_clusters.append(&mut newly_unref); + Ok(new_addr) + } + + // Returns true if the cluster containing `address` is already allocated. + fn cluster_allocated(&mut self, address: u64) -> std::io::Result { + if address >= self.virtual_size() { + return Err(std::io::Error::from_raw_os_error(EINVAL)); + } + + let l1_index = self.l1_table_index(address) as usize; + let l2_addr_disk = *self + .l1_table + .get(l1_index) + .ok_or_else(|| std::io::Error::from_raw_os_error(EINVAL))?; + let l2_index = self.l2_table_index(address) as usize; + + if l2_addr_disk == 0 { + // The whole L2 table for this address is not allocated yet, + // so the cluster must also be unallocated. + return Ok(false); + } + + if !self.l2_cache.contains_key(l1_index) { + // Not in the cache. + let table = + VecCache::from_vec(Self::read_l2_cluster(&mut self.raw_file, l2_addr_disk)?); + let l1_table = &self.l1_table; + let raw_file = &mut self.raw_file; + self.l2_cache.insert(l1_index, table, |index, evicted| { + raw_file.write_pointer_table( + l1_table[index], + evicted.get_values(), + CLUSTER_USED_FLAG, + ) + })?; + } + + let cluster_addr = self.l2_cache.get(l1_index).unwrap()[l2_index]; + // If cluster_addr != 0, the cluster is allocated. + Ok(cluster_addr != 0) + } + + // Find the first guest address greater than or equal to `address` whose allocation state + // matches `allocated`. + fn find_allocated_cluster( + &mut self, + address: u64, + allocated: bool, + ) -> std::io::Result> { + let size = self.virtual_size(); + if address >= size { + return Ok(None); + } + + // If offset is already within a hole, return it. + if self.cluster_allocated(address)? == allocated { + return Ok(Some(address)); + } + + // Skip to the next cluster boundary. + let cluster_size = self.raw_file.cluster_size(); + let mut cluster_addr = (address / cluster_size + 1) * cluster_size; + + // Search for clusters with the desired allocation state. + while cluster_addr < size { + if self.cluster_allocated(cluster_addr)? == allocated { + return Ok(Some(cluster_addr)); + } + cluster_addr += cluster_size; + } + + Ok(None) + } + + // Deallocate the storage for the cluster starting at `address`. + // Any future reads of this cluster will return all zeroes. + fn deallocate_cluster(&mut self, address: u64) -> std::io::Result<()> { + if address >= self.virtual_size() { + return Err(std::io::Error::from_raw_os_error(EINVAL)); + } + + let l1_index = self.l1_table_index(address) as usize; + let l2_addr_disk = *self + .l1_table + .get(l1_index) + .ok_or_else(|| std::io::Error::from_raw_os_error(EINVAL))?; + let l2_index = self.l2_table_index(address) as usize; + + if l2_addr_disk == 0 { + // The whole L2 table for this address is not allocated yet, + // so the cluster must also be unallocated. + return Ok(()); + } + + if !self.l2_cache.contains_key(l1_index) { + // Not in the cache. + let table = + VecCache::from_vec(Self::read_l2_cluster(&mut self.raw_file, l2_addr_disk)?); + let l1_table = &self.l1_table; + let raw_file = &mut self.raw_file; + self.l2_cache.insert(l1_index, table, |index, evicted| { + raw_file.write_pointer_table( + l1_table[index], + evicted.get_values(), + CLUSTER_USED_FLAG, + ) + })?; + } + + let cluster_addr = self.l2_cache.get(l1_index).unwrap()[l2_index]; + if cluster_addr == 0 { + // This cluster is already unallocated; nothing to do. + return Ok(()); + } + + // Decrement the refcount. + let refcount = self + .refcounts + .get_cluster_refcount(&mut self.raw_file, cluster_addr) + .map_err(|e| { + io::Error::new( + io::ErrorKind::InvalidData, + format!("failed to get cluster refcount: {}", e), + ) + })?; + if refcount == 0 { + return Err(std::io::Error::from_raw_os_error(EINVAL)); + } + + let new_refcount = refcount - 1; + let mut newly_unref = self.set_cluster_refcount(cluster_addr, new_refcount)?; + self.unref_clusters.append(&mut newly_unref); + + // Rewrite the L2 entry to remove the cluster mapping. + // unwrap is safe as we just checked/inserted this entry. + self.l2_cache.get_mut(l1_index).unwrap()[l2_index] = 0; + + if new_refcount == 0 { + let cluster_size = self.raw_file.cluster_size(); + // This cluster is no longer in use; deallocate the storage. + // The underlying FS may not support FALLOC_FL_PUNCH_HOLE, + // so don't treat an error as fatal. Future reads will return zeros anyways. + let _ = self + .raw_file + .file_mut() + .punch_hole(cluster_addr, cluster_size); + self.unref_clusters.push(cluster_addr); + } + Ok(()) + } + + // Deallocate the storage for `length` bytes starting at `address`. + // Any future reads of this range will return all zeroes. + fn deallocate_bytes(&mut self, address: u64, length: usize) -> std::io::Result<()> { + let write_count: usize = self.limit_range_file(address, length); + + let mut nwritten: usize = 0; + while nwritten < write_count { + let curr_addr = address + nwritten as u64; + let count = self.limit_range_cluster(curr_addr, write_count - nwritten); + + if count == self.raw_file.cluster_size() as usize { + // Full cluster - deallocate the storage. + self.deallocate_cluster(curr_addr)?; + } else { + // Partial cluster - zero out the relevant bytes if it was allocated. + // Any space in unallocated clusters can be left alone, since + // unallocated clusters already read back as zeroes. + if let Some(offset) = self.file_offset_read(curr_addr)? { + // Partial cluster - zero it out. + self.raw_file.file_mut().write_zeroes_at(offset, count)?; + } + } + + nwritten += count; + } + Ok(()) + } + + // Reads an L2 cluster from the disk, returning an error if the file can't be read or if any + // cluster is compressed. + fn read_l2_cluster(raw_file: &mut QcowRawFile, cluster_addr: u64) -> std::io::Result> { + let file_values = raw_file.read_pointer_cluster(cluster_addr, None)?; + if file_values.iter().any(|entry| entry & COMPRESSED_FLAG != 0) { + return Err(std::io::Error::from_raw_os_error(ENOTSUP)); + } + Ok(file_values + .iter() + .map(|entry| *entry & L2_TABLE_OFFSET_MASK) + .collect()) + } + + // Set the refcount for a cluster with the given address. + // Returns a list of any refblocks that can be reused, this happens when a refblock is moved, + // the old location can be reused. + fn set_cluster_refcount(&mut self, address: u64, refcount: u16) -> std::io::Result> { + let mut added_clusters = Vec::new(); + let mut unref_clusters = Vec::new(); + let mut refcount_set = false; + let mut new_cluster = None; + + while !refcount_set { + match self.refcounts.set_cluster_refcount( + &mut self.raw_file, + address, + refcount, + new_cluster.take(), + ) { + Ok(None) => { + refcount_set = true; + } + Ok(Some(freed_cluster)) => { + unref_clusters.push(freed_cluster); + refcount_set = true; + } + Err(refcount::Error::EvictingRefCounts(e)) => { + return Err(e); + } + Err(refcount::Error::InvalidIndex) => { + return Err(std::io::Error::from_raw_os_error(EINVAL)); + } + Err(refcount::Error::NeedCluster(addr)) => { + // Read the address and call set_cluster_refcount again. + new_cluster = Some(( + addr, + VecCache::from_vec(self.raw_file.read_refcount_block(addr)?), + )); + } + Err(refcount::Error::NeedNewCluster) => { + // Allocate the cluster and call set_cluster_refcount again. + let addr = self.get_new_cluster()?; + added_clusters.push(addr); + new_cluster = Some(( + addr, + VecCache::new(self.refcounts.refcounts_per_block() as usize), + )); + } + Err(refcount::Error::ReadingRefCounts(e)) => { + return Err(e); + } + } + } + + for addr in added_clusters { + self.set_cluster_refcount(addr, 1)?; + } + Ok(unref_clusters) + } + + fn sync_caches(&mut self) -> std::io::Result<()> { + // Write out all dirty L2 tables. + for (l1_index, l2_table) in self.l2_cache.iter_mut().filter(|(_k, v)| v.dirty()) { + // The index must be valid from when we inserted it. + let addr = self.l1_table[*l1_index]; + if addr != 0 { + self.raw_file.write_pointer_table( + addr, + l2_table.get_values(), + CLUSTER_USED_FLAG, + )?; + } else { + return Err(std::io::Error::from_raw_os_error(EINVAL)); + } + l2_table.mark_clean(); + } + // Write the modified refcount blocks. + self.refcounts.flush_blocks(&mut self.raw_file)?; + // Make sure metadata(file len) and all data clusters are written. + self.raw_file.file_mut().sync_all()?; + + // Push L1 table and refcount table last as all the clusters they point to are now + // guaranteed to be valid. + let mut sync_required = if self.l1_table.dirty() { + self.raw_file.write_pointer_table( + self.header.l1_table_offset, + self.l1_table.get_values(), + 0, + )?; + self.l1_table.mark_clean(); + true + } else { + false + }; + sync_required |= self.refcounts.flush_table(&mut self.raw_file)?; + if sync_required { + self.raw_file.file_mut().sync_data()?; + } + Ok(()) + } +} + +impl Drop for QcowFile { + fn drop(&mut self) { + let _ = self.sync_caches(); + } +} + +impl Read for QcowFile { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + let address: u64 = self.current_offset; + let read_count: usize = self.limit_range_file(address, buf.len()); + + let mut nread: usize = 0; + while nread < read_count { + let curr_addr = address + nread as u64; + let file_offset = self.file_offset_read(curr_addr)?; + let count = self.limit_range_cluster(curr_addr, read_count - nread); + + if let Some(offset) = file_offset { + self.raw_file.file_mut().seek(SeekFrom::Start(offset))?; + self.raw_file + .file_mut() + .read_exact(&mut buf[nread..(nread + count)])?; + } else { + // Previously unwritten region, return zeros + for b in &mut buf[nread..(nread + count)] { + *b = 0; + } + } + + nread += count; + } + self.current_offset += read_count as u64; + Ok(read_count) + } +} + +impl Seek for QcowFile { + fn seek(&mut self, pos: SeekFrom) -> std::io::Result { + let new_offset: Option = match pos { + SeekFrom::Start(off) => Some(off), + SeekFrom::End(off) => { + if off < 0 { + 0i64.checked_sub(off) + .and_then(|increment| self.virtual_size().checked_sub(increment as u64)) + } else { + self.virtual_size().checked_add(off as u64) + } + } + SeekFrom::Current(off) => { + if off < 0 { + 0i64.checked_sub(off) + .and_then(|increment| self.current_offset.checked_sub(increment as u64)) + } else { + self.current_offset.checked_add(off as u64) + } + } + }; + + if let Some(o) = new_offset { + if o <= self.virtual_size() { + self.current_offset = o; + return Ok(o); + } + } + Err(std::io::Error::from_raw_os_error(EINVAL)) + } +} + +impl Write for QcowFile { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + let address: u64 = self.current_offset; + let write_count: usize = self.limit_range_file(address, buf.len()); + + let mut nwritten: usize = 0; + while nwritten < write_count { + let curr_addr = address + nwritten as u64; + let offset = self.file_offset_write(curr_addr)?; + let count = self.limit_range_cluster(curr_addr, write_count - nwritten); + + self.raw_file.file_mut().seek(SeekFrom::Start(offset))?; + let count = self + .raw_file + .file_mut() + .write(&buf[nwritten..(nwritten + count)])?; + + nwritten += count; + } + self.current_offset += write_count as u64; + Ok(write_count) + } + + fn flush(&mut self) -> std::io::Result<()> { + self.sync_caches()?; + self.avail_clusters.append(&mut self.unref_clusters); + Ok(()) + } +} + +impl FileSync for QcowFile { + fn fsync(&mut self) -> std::io::Result<()> { + self.flush() + } +} + +impl FileSetLen for QcowFile { + fn set_len(&self, _len: u64) -> std::io::Result<()> { + Err(std::io::Error::new( + std::io::ErrorKind::Other, + "set_len() not supported for QcowFile", + )) + } +} + +impl PunchHole for QcowFile { + fn punch_hole(&mut self, offset: u64, length: u64) -> std::io::Result<()> { + let mut remaining = length; + let mut offset = offset; + while remaining > 0 { + let chunk_length = min(remaining, std::usize::MAX as u64) as usize; + self.deallocate_bytes(offset, chunk_length)?; + remaining -= chunk_length as u64; + offset += chunk_length as u64; + } + Ok(()) + } +} + +impl WriteZeroesAt for QcowFile { + fn write_zeroes_at(&mut self, offset: u64, length: usize) -> io::Result { + self.punch_hole(offset, length as u64)?; + Ok(length) + } +} + +impl SeekHole for QcowFile { + fn seek_hole(&mut self, offset: u64) -> io::Result> { + match self.find_allocated_cluster(offset, false) { + Err(e) => Err(e), + Ok(None) => { + if offset < self.virtual_size() { + Ok(Some(self.seek(SeekFrom::End(0))?)) + } else { + Ok(None) + } + } + Ok(Some(o)) => { + self.seek(SeekFrom::Start(o))?; + Ok(Some(o)) + } + } + } + + fn seek_data(&mut self, offset: u64) -> io::Result> { + match self.find_allocated_cluster(offset, true) { + Err(e) => Err(e), + Ok(None) => Ok(None), + Ok(Some(o)) => { + self.seek(SeekFrom::Start(o))?; + Ok(Some(o)) + } + } + } +} + +// Returns an Error if the given offset doesn't align to a cluster boundary. +fn offset_is_cluster_boundary(offset: u64, cluster_bits: u32) -> Result<()> { + if offset & ((0x01 << cluster_bits) - 1) != 0 { + return Err(Error::InvalidOffset(offset)); + } + Ok(()) +} + +// Ceiling of the division of `dividend`/`divisor`. +fn div_round_up_u64(dividend: u64, divisor: u64) -> u64 { + dividend / divisor + u64::from(dividend % divisor != 0) +} + +// Ceiling of the division of `dividend`/`divisor`. +fn div_round_up_u32(dividend: u32, divisor: u32) -> u32 { + dividend / divisor + u32::from(dividend % divisor != 0) +} + +fn convert_copy(reader: &mut R, writer: &mut W, offset: u64, size: u64) -> Result<()> +where + R: Read + Seek, + W: Write + Seek, +{ + const CHUNK_SIZE: usize = 65536; + let mut buf = [0; CHUNK_SIZE]; + let mut read_count = 0; + reader + .seek(SeekFrom::Start(offset)) + .map_err(Error::SeekingFile)?; + writer + .seek(SeekFrom::Start(offset)) + .map_err(Error::SeekingFile)?; + loop { + let this_count = min(CHUNK_SIZE as u64, size - read_count) as usize; + let nread = reader + .read(&mut buf[..this_count]) + .map_err(Error::ReadingData)?; + writer.write(&buf[..nread]).map_err(Error::WritingData)?; + read_count += nread as u64; + if nread == 0 || read_count == size { + break; + } + } + + Ok(()) +} + +fn convert_reader_writer(reader: &mut R, writer: &mut W, size: u64) -> Result<()> +where + R: Read + Seek + SeekHole, + W: Write + Seek, +{ + let mut offset = 0; + while offset < size { + // Find the next range of data. + let next_data = match reader.seek_data(offset).map_err(Error::SeekingFile)? { + Some(o) => o, + None => { + // No more data in the file. + break; + } + }; + let next_hole = match reader.seek_hole(next_data).map_err(Error::SeekingFile)? { + Some(o) => o, + None => { + // This should not happen - there should always be at least one hole + // after any data. + return Err(Error::SeekingFile(io::Error::from_raw_os_error(EINVAL))); + } + }; + let count = next_hole - next_data; + convert_copy(reader, writer, next_data, count)?; + offset = next_hole; + } + + Ok(()) +} + +fn convert_reader(reader: &mut R, dst_file: RawFile, dst_type: ImageType) -> Result<()> +where + R: Read + Seek + SeekHole, +{ + let src_size = reader.seek(SeekFrom::End(0)).map_err(Error::SeekingFile)?; + reader + .seek(SeekFrom::Start(0)) + .map_err(Error::SeekingFile)?; + + // Ensure the destination file is empty before writing to it. + dst_file.set_len(0).map_err(Error::SettingFileSize)?; + + match dst_type { + ImageType::Qcow2 => { + let mut dst_writer = QcowFile::new(dst_file, 3, src_size)?; + convert_reader_writer(reader, &mut dst_writer, src_size) + } + ImageType::Raw => { + let mut dst_writer = dst_file; + // Set the length of the destination file to convert it into a sparse file + // of the desired size. + dst_writer + .set_len(src_size) + .map_err(Error::SettingFileSize)?; + convert_reader_writer(reader, &mut dst_writer, src_size) + } + } +} + +/// Copy the contents of a disk image in `src_file` into `dst_file`. +/// The type of `src_file` is automatically detected, and the output file type is +/// determined by `dst_type`. +pub fn convert(mut src_file: RawFile, dst_file: RawFile, dst_type: ImageType) -> Result<()> { + let src_type = detect_image_type(&mut src_file)?; + match src_type { + ImageType::Qcow2 => { + let mut src_reader = QcowFile::from(src_file)?; + convert_reader(&mut src_reader, dst_file, dst_type) + } + ImageType::Raw => { + // src_file is a raw file. + let mut src_reader = src_file; + convert_reader(&mut src_reader, dst_file, dst_type) + } + } +} + +/// Detect the type of an image file by checking for a valid qcow2 header. +pub fn detect_image_type(file: &mut RawFile) -> Result { + let orig_seek = file + .seek(SeekFrom::Current(0)) + .map_err(Error::SeekingFile)?; + file.seek(SeekFrom::Start(0)).map_err(Error::SeekingFile)?; + let magic = file.read_u32::().map_err(Error::ReadingHeader)?; + let image_type = if magic == QCOW_MAGIC { + ImageType::Qcow2 + } else { + ImageType::Raw + }; + file.seek(SeekFrom::Start(orig_seek)) + .map_err(Error::SeekingFile)?; + Ok(image_type) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::{Read, Seek, SeekFrom, Write}; + use vmm_sys_util::tempfile::TempFile; + use vmm_sys_util::write_zeroes::WriteZeroes; + + fn valid_header_v3() -> Vec { + vec![ + 0x51u8, 0x46, 0x49, 0xfb, // magic + 0x00, 0x00, 0x00, 0x03, // version + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // backing file offset + 0x00, 0x00, 0x00, 0x00, // backing file size + 0x00, 0x00, 0x00, 0x10, // cluster_bits + 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, // size + 0x00, 0x00, 0x00, 0x00, // crypt method + 0x00, 0x00, 0x01, 0x00, // L1 size + 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, // L1 table offset + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, // refcount table offset + 0x00, 0x00, 0x00, 0x03, // refcount table clusters + 0x00, 0x00, 0x00, 0x00, // nb snapshots + 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, // snapshots offset + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // incompatible_features + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // compatible_features + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // autoclear_features + 0x00, 0x00, 0x00, 0x04, // refcount_order + 0x00, 0x00, 0x00, 0x68, // header_length + ] + } + + fn valid_header_v2() -> Vec { + vec![ + 0x51u8, 0x46, 0x49, 0xfb, // magic + 0x00, 0x00, 0x00, 0x02, // version + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // backing file offset + 0x00, 0x00, 0x00, 0x00, // backing file size + 0x00, 0x00, 0x00, 0x10, // cluster_bits + 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, // size + 0x00, 0x00, 0x00, 0x00, // crypt method + 0x00, 0x00, 0x01, 0x00, // L1 size + 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, // L1 table offset + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, // refcount table offset + 0x00, 0x00, 0x00, 0x03, // refcount table clusters + 0x00, 0x00, 0x00, 0x00, // nb snapshots + 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, // snapshots offset + ] + } + + // Test case found by clusterfuzz to allocate excessive memory. + fn test_huge_header() -> Vec { + vec![ + 0x51, 0x46, 0x49, 0xfb, // magic + 0x00, 0x00, 0x00, 0x03, // version + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // backing file offset + 0x00, 0x00, 0x00, 0x00, // backing file size + 0x00, 0x00, 0x00, 0x09, // cluster_bits + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, // size + 0x00, 0x00, 0x00, 0x00, // crypt method + 0x00, 0x00, 0x01, 0x00, // L1 size + 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, // L1 table offset + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, // refcount table offset + 0x00, 0x00, 0x00, 0x03, // refcount table clusters + 0x00, 0x00, 0x00, 0x00, // nb snapshots + 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, // snapshots offset + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // incompatible_features + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // compatible_features + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // autoclear_features + 0x00, 0x00, 0x00, 0x04, // refcount_order + 0x00, 0x00, 0x00, 0x68, // header_length + ] + } + + fn with_basic_file(header: &[u8], mut testfn: F) + where + F: FnMut(RawFile), + { + let mut disk_file: RawFile = RawFile::new(TempFile::new().unwrap().into_file(), false); + disk_file.write_all(header).unwrap(); + disk_file.set_len(0x1_0000_0000).unwrap(); + disk_file.seek(SeekFrom::Start(0)).unwrap(); + + testfn(disk_file); // File closed when the function exits. + } + + fn with_default_file(file_size: u64, direct: bool, mut testfn: F) + where + F: FnMut(QcowFile), + { + let tmp: RawFile = RawFile::new(TempFile::new().unwrap().into_file(), direct); + let qcow_file = QcowFile::new(tmp, 3, file_size).unwrap(); + + testfn(qcow_file); // File closed when the function exits. + } + + #[test] + fn default_header_v2() { + let header = QcowHeader::create_for_size(2, 0x10_0000); + let mut disk_file: RawFile = RawFile::new(TempFile::new().unwrap().into_file(), false); + header + .write_to(&mut disk_file) + .expect("Failed to write header to temporary file."); + disk_file.seek(SeekFrom::Start(0)).unwrap(); + QcowFile::from(disk_file).expect("Failed to create Qcow from default Header"); + } + + #[test] + fn default_header_v3() { + let header = QcowHeader::create_for_size(3, 0x10_0000); + let mut disk_file: RawFile = RawFile::new(TempFile::new().unwrap().into_file(), false); + header + .write_to(&mut disk_file) + .expect("Failed to write header to temporary file."); + disk_file.seek(SeekFrom::Start(0)).unwrap(); + QcowFile::from(disk_file).expect("Failed to create Qcow from default Header"); + } + + #[test] + fn header_read() { + with_basic_file(&valid_header_v2(), |mut disk_file: RawFile| { + let header = QcowHeader::new(&mut disk_file).expect("Failed to create Header."); + assert_eq!(header.version, 2); + assert_eq!(header.refcount_order, DEFAULT_REFCOUNT_ORDER); + assert_eq!(header.header_size, V2_BARE_HEADER_SIZE); + }); + with_basic_file(&valid_header_v3(), |mut disk_file: RawFile| { + let header = QcowHeader::new(&mut disk_file).expect("Failed to create Header."); + assert_eq!(header.version, 3); + assert_eq!(header.refcount_order, DEFAULT_REFCOUNT_ORDER); + assert_eq!(header.header_size, V3_BARE_HEADER_SIZE); + }); + } + + #[test] + fn invalid_magic() { + let invalid_header = vec![0x51u8, 0x46, 0x4a, 0xfb]; + with_basic_file(&invalid_header, |mut disk_file: RawFile| { + QcowHeader::new(&mut disk_file).expect_err("Invalid header worked."); + }); + } + + #[test] + fn invalid_refcount_order() { + let mut header = valid_header_v3(); + header[99] = 2; + with_basic_file(&header, |disk_file: RawFile| { + QcowFile::from(disk_file).expect_err("Invalid refcount order worked."); + }); + } + + #[test] + fn invalid_cluster_bits() { + let mut header = valid_header_v3(); + header[23] = 3; + with_basic_file(&header, |disk_file: RawFile| { + QcowFile::from(disk_file).expect_err("Failed to create file."); + }); + } + + #[test] + fn test_header_huge_file() { + let header = test_huge_header(); + with_basic_file(&header, |disk_file: RawFile| { + QcowFile::from(disk_file).expect_err("Failed to create file."); + }); + } + + #[test] + fn test_header_crazy_file_size_rejected() { + let mut header = valid_header_v3(); + header[24..32].copy_from_slice(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1e]); + with_basic_file(&header, |disk_file: RawFile| { + QcowFile::from(disk_file).expect_err("Failed to create file."); + }); + } + + #[test] + fn test_huge_l1_table() { + let mut header = valid_header_v3(); + header[36] = 0x12; + with_basic_file(&header, |disk_file: RawFile| { + QcowFile::from(disk_file).expect_err("Failed to create file."); + }); + } + + #[test] + fn test_header_1_tb_file_min_cluster() { + let mut header = test_huge_header(); + header[24] = 0; + header[26] = 1; + header[31] = 0; + // 1 TB with the min cluster size makes the arrays too big, it should fail. + with_basic_file(&header, |disk_file: RawFile| { + QcowFile::from(disk_file).expect_err("Failed to create file."); + }); + } + + #[test] + fn test_header_1_tb_file() { + let mut header = test_huge_header(); + // reset to 1 TB size. + header[24] = 0; + header[26] = 1; + header[31] = 0; + // set cluster_bits + header[23] = 16; + with_basic_file(&header, |disk_file: RawFile| { + let mut qcow = QcowFile::from(disk_file).expect("Failed to create file."); + qcow.seek(SeekFrom::Start(0x100_0000_0000 - 8)) + .expect("Failed to seek."); + let value = 0x0000_0040_3f00_ffffu64; + qcow.write_all(&value.to_le_bytes()) + .expect("failed to write data"); + }); + } + + #[test] + fn test_header_huge_num_refcounts() { + let mut header = valid_header_v3(); + header[56..60].copy_from_slice(&[0x02, 0x00, 0xe8, 0xff]); + with_basic_file(&header, |disk_file: RawFile| { + QcowFile::from(disk_file).expect_err("Created disk with crazy refcount clusters"); + }); + } + + #[test] + fn test_header_huge_refcount_offset() { + let mut header = valid_header_v3(); + header[48..56].copy_from_slice(&[0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x02, 0x00]); + with_basic_file(&header, |disk_file: RawFile| { + QcowFile::from(disk_file).expect_err("Created disk with crazy refcount offset"); + }); + } + + #[test] + fn write_read_start() { + with_basic_file(&valid_header_v3(), |disk_file: RawFile| { + let mut q = QcowFile::from(disk_file).unwrap(); + q.write_all(b"test first bytes") + .expect("Failed to write test string."); + let mut buf = [0u8; 4]; + q.seek(SeekFrom::Start(0)).expect("Failed to seek."); + q.read_exact(&mut buf).expect("Failed to read."); + assert_eq!(&buf, b"test"); + }); + } + + #[test] + fn offset_write_read() { + with_basic_file(&valid_header_v3(), |disk_file: RawFile| { + let mut q = QcowFile::from(disk_file).unwrap(); + let b = [0x55u8; 0x1000]; + q.seek(SeekFrom::Start(0xfff2000)).expect("Failed to seek."); + q.write_all(&b).expect("Failed to write test string."); + let mut buf = [0u8; 4]; + q.seek(SeekFrom::Start(0xfff2000)).expect("Failed to seek."); + q.read_exact(&mut buf).expect("Failed to read."); + assert_eq!(buf[0], 0x55); + }); + } + + #[test] + fn write_zeroes_read() { + with_basic_file(&valid_header_v3(), |disk_file: RawFile| { + let mut q = QcowFile::from(disk_file).unwrap(); + // Write some test data. + let b = [0x55u8; 0x1000]; + q.seek(SeekFrom::Start(0xfff2000)).expect("Failed to seek."); + q.write_all(&b).expect("Failed to write test string."); + // Overwrite the test data with zeroes. + q.seek(SeekFrom::Start(0xfff2000)).expect("Failed to seek."); + let nwritten = q.write_zeroes(0x200).expect("Failed to write zeroes."); + assert_eq!(nwritten, 0x200); + // Verify that the correct part of the data was zeroed out. + let mut buf = [0u8; 0x1000]; + q.seek(SeekFrom::Start(0xfff2000)).expect("Failed to seek."); + q.read_exact(&mut buf).expect("Failed to read."); + assert_eq!(buf[0], 0); + assert_eq!(buf[0x1FF], 0); + assert_eq!(buf[0x200], 0x55); + assert_eq!(buf[0xFFF], 0x55); + }); + } + + #[test] + fn write_zeroes_full_cluster() { + // Choose a size that is larger than a cluster. + // valid_header uses cluster_bits = 12, which corresponds to a cluster size of 4096. + const CHUNK_SIZE: usize = 4096 * 2 + 512; + with_basic_file(&valid_header_v3(), |disk_file: RawFile| { + let mut q = QcowFile::from(disk_file).unwrap(); + // Write some test data. + let b = [0x55u8; CHUNK_SIZE]; + q.seek(SeekFrom::Start(0)).expect("Failed to seek."); + q.write_all(&b).expect("Failed to write test string."); + // Overwrite the full cluster with zeroes. + q.seek(SeekFrom::Start(0)).expect("Failed to seek."); + let nwritten = q.write_zeroes(CHUNK_SIZE).expect("Failed to write zeroes."); + assert_eq!(nwritten, CHUNK_SIZE); + // Verify that the data was zeroed out. + let mut buf = [0u8; CHUNK_SIZE]; + q.seek(SeekFrom::Start(0)).expect("Failed to seek."); + q.read_exact(&mut buf).expect("Failed to read."); + assert_eq!(buf[0], 0); + assert_eq!(buf[CHUNK_SIZE - 1], 0); + }); + } + + #[test] + fn test_header() { + with_basic_file(&valid_header_v2(), |disk_file: RawFile| { + let q = QcowFile::from(disk_file).unwrap(); + assert_eq!(q.virtual_size(), 0x20_0000_0000); + }); + with_basic_file(&valid_header_v3(), |disk_file: RawFile| { + let q = QcowFile::from(disk_file).unwrap(); + assert_eq!(q.virtual_size(), 0x20_0000_0000); + }); + } + + #[test] + fn read_small_buffer() { + with_basic_file(&valid_header_v3(), |disk_file: RawFile| { + let mut q = QcowFile::from(disk_file).unwrap(); + let mut b = [5u8; 16]; + q.seek(SeekFrom::Start(1000)).expect("Failed to seek."); + q.read_exact(&mut b).expect("Failed to read."); + assert_eq!(0, b[0]); + assert_eq!(0, b[15]); + }); + } + + #[test] + fn replay_ext4() { + with_basic_file(&valid_header_v3(), |disk_file: RawFile| { + let mut q = QcowFile::from(disk_file).unwrap(); + const BUF_SIZE: usize = 0x1000; + let mut b = [0u8; BUF_SIZE]; + + struct Transfer { + pub write: bool, + pub addr: u64, + } + + // Write transactions from mkfs.ext4. + let xfers: Vec = vec![ + Transfer { + write: false, + addr: 0xfff0000, + }, + Transfer { + write: false, + addr: 0xfffe000, + }, + Transfer { + write: false, + addr: 0x0, + }, + Transfer { + write: false, + addr: 0x1000, + }, + Transfer { + write: false, + addr: 0xffff000, + }, + Transfer { + write: false, + addr: 0xffdf000, + }, + Transfer { + write: false, + addr: 0xfff8000, + }, + Transfer { + write: false, + addr: 0xffe0000, + }, + Transfer { + write: false, + addr: 0xffce000, + }, + Transfer { + write: false, + addr: 0xffb6000, + }, + Transfer { + write: false, + addr: 0xffab000, + }, + Transfer { + write: false, + addr: 0xffa4000, + }, + Transfer { + write: false, + addr: 0xff8e000, + }, + Transfer { + write: false, + addr: 0xff86000, + }, + Transfer { + write: false, + addr: 0xff84000, + }, + Transfer { + write: false, + addr: 0xff89000, + }, + Transfer { + write: false, + addr: 0xfe7e000, + }, + Transfer { + write: false, + addr: 0x100000, + }, + Transfer { + write: false, + addr: 0x3000, + }, + Transfer { + write: false, + addr: 0x7000, + }, + Transfer { + write: false, + addr: 0xf000, + }, + Transfer { + write: false, + addr: 0x2000, + }, + Transfer { + write: false, + addr: 0x4000, + }, + Transfer { + write: false, + addr: 0x5000, + }, + Transfer { + write: false, + addr: 0x6000, + }, + Transfer { + write: false, + addr: 0x8000, + }, + Transfer { + write: false, + addr: 0x9000, + }, + Transfer { + write: false, + addr: 0xa000, + }, + Transfer { + write: false, + addr: 0xb000, + }, + Transfer { + write: false, + addr: 0xc000, + }, + Transfer { + write: false, + addr: 0xd000, + }, + Transfer { + write: false, + addr: 0xe000, + }, + Transfer { + write: false, + addr: 0x10000, + }, + Transfer { + write: false, + addr: 0x11000, + }, + Transfer { + write: false, + addr: 0x12000, + }, + Transfer { + write: false, + addr: 0x13000, + }, + Transfer { + write: false, + addr: 0x14000, + }, + Transfer { + write: false, + addr: 0x15000, + }, + Transfer { + write: false, + addr: 0x16000, + }, + Transfer { + write: false, + addr: 0x17000, + }, + Transfer { + write: false, + addr: 0x18000, + }, + Transfer { + write: false, + addr: 0x19000, + }, + Transfer { + write: false, + addr: 0x1a000, + }, + Transfer { + write: false, + addr: 0x1b000, + }, + Transfer { + write: false, + addr: 0x1c000, + }, + Transfer { + write: false, + addr: 0x1d000, + }, + Transfer { + write: false, + addr: 0x1e000, + }, + Transfer { + write: false, + addr: 0x1f000, + }, + Transfer { + write: false, + addr: 0x21000, + }, + Transfer { + write: false, + addr: 0x22000, + }, + Transfer { + write: false, + addr: 0x24000, + }, + Transfer { + write: false, + addr: 0x40000, + }, + Transfer { + write: false, + addr: 0x0, + }, + Transfer { + write: false, + addr: 0x3000, + }, + Transfer { + write: false, + addr: 0x7000, + }, + Transfer { + write: false, + addr: 0x0, + }, + Transfer { + write: false, + addr: 0x1000, + }, + Transfer { + write: false, + addr: 0x2000, + }, + Transfer { + write: false, + addr: 0x3000, + }, + Transfer { + write: false, + addr: 0x0, + }, + Transfer { + write: false, + addr: 0x449000, + }, + Transfer { + write: false, + addr: 0x48000, + }, + Transfer { + write: false, + addr: 0x48000, + }, + Transfer { + write: false, + addr: 0x448000, + }, + Transfer { + write: false, + addr: 0x44a000, + }, + Transfer { + write: false, + addr: 0x48000, + }, + Transfer { + write: false, + addr: 0x48000, + }, + Transfer { + write: true, + addr: 0x0, + }, + Transfer { + write: true, + addr: 0x448000, + }, + Transfer { + write: true, + addr: 0x449000, + }, + Transfer { + write: true, + addr: 0x44a000, + }, + Transfer { + write: true, + addr: 0xfff0000, + }, + Transfer { + write: true, + addr: 0xfff1000, + }, + Transfer { + write: true, + addr: 0xfff2000, + }, + Transfer { + write: true, + addr: 0xfff3000, + }, + Transfer { + write: true, + addr: 0xfff4000, + }, + Transfer { + write: true, + addr: 0xfff5000, + }, + Transfer { + write: true, + addr: 0xfff6000, + }, + Transfer { + write: true, + addr: 0xfff7000, + }, + Transfer { + write: true, + addr: 0xfff8000, + }, + Transfer { + write: true, + addr: 0xfff9000, + }, + Transfer { + write: true, + addr: 0xfffa000, + }, + Transfer { + write: true, + addr: 0xfffb000, + }, + Transfer { + write: true, + addr: 0xfffc000, + }, + Transfer { + write: true, + addr: 0xfffd000, + }, + Transfer { + write: true, + addr: 0xfffe000, + }, + Transfer { + write: true, + addr: 0xffff000, + }, + ]; + + for xfer in &xfers { + q.seek(SeekFrom::Start(xfer.addr)).expect("Failed to seek."); + if xfer.write { + q.write_all(&b).expect("Failed to write."); + } else { + let read_count: usize = q.read(&mut b).expect("Failed to read."); + assert_eq!(read_count, BUF_SIZE); + } + } + }); + } + + #[test] + fn combo_write_read() { + combo_write_read_common(false); + } + + #[test] + fn combo_write_read_direct() { + combo_write_read_common(true); + } + + fn combo_write_read_common(direct: bool) { + with_default_file(1024 * 1024 * 1024 * 256, direct, |mut qcow_file| { + const NUM_BLOCKS: usize = 555; + const BLOCK_SIZE: usize = 0x1_0000; + const OFFSET: usize = 0x1_0000_0020; + let data = [0x55u8; BLOCK_SIZE]; + let mut readback = [0u8; BLOCK_SIZE]; + for i in 0..NUM_BLOCKS { + let seek_offset = OFFSET + i * BLOCK_SIZE; + qcow_file + .seek(SeekFrom::Start(seek_offset as u64)) + .expect("Failed to seek."); + let nwritten = qcow_file.write(&data).expect("Failed to write test data."); + assert_eq!(nwritten, BLOCK_SIZE); + // Read back the data to check it was written correctly. + qcow_file + .seek(SeekFrom::Start(seek_offset as u64)) + .expect("Failed to seek."); + let nread = qcow_file.read(&mut readback).expect("Failed to read."); + assert_eq!(nread, BLOCK_SIZE); + for (orig, read) in data.iter().zip(readback.iter()) { + assert_eq!(orig, read); + } + } + // Check that address 0 is still zeros. + qcow_file.seek(SeekFrom::Start(0)).expect("Failed to seek."); + let nread = qcow_file.read(&mut readback).expect("Failed to read."); + assert_eq!(nread, BLOCK_SIZE); + for read in readback.iter() { + assert_eq!(*read, 0); + } + // Check the data again after the writes have happened. + for i in 0..NUM_BLOCKS { + let seek_offset = OFFSET + i * BLOCK_SIZE; + qcow_file + .seek(SeekFrom::Start(seek_offset as u64)) + .expect("Failed to seek."); + let nread = qcow_file.read(&mut readback).expect("Failed to read."); + assert_eq!(nread, BLOCK_SIZE); + for (orig, read) in data.iter().zip(readback.iter()) { + assert_eq!(orig, read); + } + } + + assert_eq!(qcow_file.first_zero_refcount().unwrap(), None); + }); + } + + fn seek_cur(file: &mut QcowFile) -> u64 { + file.seek(SeekFrom::Current(0)).unwrap() + } + + #[test] + fn seek_data() { + seek_data_common(false); + } + + #[test] + fn seek_data_direct() { + seek_data_common(true); + } + + fn seek_data_common(direct: bool) { + with_default_file(0x30000, direct, |mut file| { + // seek_data at or after the end of the file should return None + assert_eq!(file.seek_data(0x10000).unwrap(), None); + assert_eq!(seek_cur(&mut file), 0); + assert_eq!(file.seek_data(0x10001).unwrap(), None); + assert_eq!(seek_cur(&mut file), 0); + + // Write some data to [0x10000, 0x20000) + let b = [0x55u8; 0x10000]; + file.seek(SeekFrom::Start(0x10000)).unwrap(); + file.write_all(&b).unwrap(); + assert_eq!(file.seek_data(0).unwrap(), Some(0x10000)); + assert_eq!(seek_cur(&mut file), 0x10000); + + // seek_data within data should return the same offset + assert_eq!(file.seek_data(0x10000).unwrap(), Some(0x10000)); + assert_eq!(seek_cur(&mut file), 0x10000); + assert_eq!(file.seek_data(0x10001).unwrap(), Some(0x10001)); + assert_eq!(seek_cur(&mut file), 0x10001); + assert_eq!(file.seek_data(0x1FFFF).unwrap(), Some(0x1FFFF)); + assert_eq!(seek_cur(&mut file), 0x1FFFF); + + assert_eq!(file.seek_data(0).unwrap(), Some(0x10000)); + assert_eq!(seek_cur(&mut file), 0x10000); + assert_eq!(file.seek_data(0x1FFFF).unwrap(), Some(0x1FFFF)); + assert_eq!(seek_cur(&mut file), 0x1FFFF); + assert_eq!(file.seek_data(0x20000).unwrap(), None); + assert_eq!(seek_cur(&mut file), 0x1FFFF); + }); + } + + #[test] + fn seek_hole() { + seek_hole_common(false); + } + + #[test] + fn seek_hole_direct() { + seek_hole_common(true); + } + + fn seek_hole_common(direct: bool) { + with_default_file(0x30000, direct, |mut file| { + // File consisting entirely of a hole + assert_eq!(file.seek_hole(0).unwrap(), Some(0)); + assert_eq!(seek_cur(&mut file), 0); + assert_eq!(file.seek_hole(0xFFFF).unwrap(), Some(0xFFFF)); + assert_eq!(seek_cur(&mut file), 0xFFFF); + + // seek_hole at or after the end of the file should return None + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x30000).unwrap(), None); + assert_eq!(seek_cur(&mut file), 0); + assert_eq!(file.seek_hole(0x30001).unwrap(), None); + assert_eq!(seek_cur(&mut file), 0); + + // Write some data to [0x10000, 0x20000) + let b = [0x55u8; 0x10000]; + file.seek(SeekFrom::Start(0x10000)).unwrap(); + file.write_all(&b).unwrap(); + + // seek_hole within a hole should return the same offset + assert_eq!(file.seek_hole(0).unwrap(), Some(0)); + assert_eq!(seek_cur(&mut file), 0); + assert_eq!(file.seek_hole(0xFFFF).unwrap(), Some(0xFFFF)); + assert_eq!(seek_cur(&mut file), 0xFFFF); + + // seek_hole within data should return the next hole + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x10000).unwrap(), Some(0x20000)); + assert_eq!(seek_cur(&mut file), 0x20000); + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x10001).unwrap(), Some(0x20000)); + assert_eq!(seek_cur(&mut file), 0x20000); + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x1FFFF).unwrap(), Some(0x20000)); + assert_eq!(seek_cur(&mut file), 0x20000); + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0xFFFF).unwrap(), Some(0xFFFF)); + assert_eq!(seek_cur(&mut file), 0xFFFF); + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x10000).unwrap(), Some(0x20000)); + assert_eq!(seek_cur(&mut file), 0x20000); + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x1FFFF).unwrap(), Some(0x20000)); + assert_eq!(seek_cur(&mut file), 0x20000); + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x20000).unwrap(), Some(0x20000)); + assert_eq!(seek_cur(&mut file), 0x20000); + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x20001).unwrap(), Some(0x20001)); + assert_eq!(seek_cur(&mut file), 0x20001); + + // seek_hole at EOF should return None + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x30000).unwrap(), None); + assert_eq!(seek_cur(&mut file), 0); + + // Write some data to [0x20000, 0x30000) + file.seek(SeekFrom::Start(0x20000)).unwrap(); + file.write_all(&b).unwrap(); + + // seek_hole within [0x20000, 0x30000) should now find the hole at EOF + assert_eq!(file.seek_hole(0x20000).unwrap(), Some(0x30000)); + assert_eq!(seek_cur(&mut file), 0x30000); + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x20001).unwrap(), Some(0x30000)); + assert_eq!(seek_cur(&mut file), 0x30000); + file.seek(SeekFrom::Start(0)).unwrap(); + assert_eq!(file.seek_hole(0x30000).unwrap(), None); + assert_eq!(seek_cur(&mut file), 0); + }); + } + + #[test] + fn rebuild_refcounts() { + with_basic_file(&valid_header_v3(), |mut disk_file: RawFile| { + let header = QcowHeader::new(&mut disk_file).expect("Failed to create Header."); + let cluster_size = 65536; + let mut raw_file = + QcowRawFile::from(disk_file, cluster_size).expect("Failed to create QcowRawFile."); + QcowFile::rebuild_refcounts(&mut raw_file, header) + .expect("Failed to rebuild recounts."); + }); + } +} diff --git a/vendor/cube-hypervisor/qcow/src/qcow_raw_file.rs b/vendor/cube-hypervisor/qcow/src/qcow_raw_file.rs new file mode 100644 index 0000000..2c56cef --- /dev/null +++ b/vendor/cube-hypervisor/qcow/src/qcow_raw_file.rs @@ -0,0 +1,149 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use super::RawFile; +use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; +use std::io::{self, BufWriter, Seek, SeekFrom}; +use std::mem::size_of; +use vmm_sys_util::write_zeroes::WriteZeroes; + +/// A qcow file. Allows reading/writing clusters and appending clusters. +#[derive(Debug)] +pub struct QcowRawFile { + file: RawFile, + cluster_size: u64, + cluster_mask: u64, +} + +impl QcowRawFile { + /// Creates a `QcowRawFile` from the given `File`, `None` is returned if `cluster_size` is not + /// a power of two. + pub fn from(file: RawFile, cluster_size: u64) -> Option { + if cluster_size.count_ones() != 1 { + return None; + } + Some(QcowRawFile { + file, + cluster_size, + cluster_mask: cluster_size - 1, + }) + } + + /// Reads `count` 64 bit offsets and returns them as a vector. + /// `mask` optionally ands out some of the bits on the file. + pub fn read_pointer_table( + &mut self, + offset: u64, + count: u64, + mask: Option, + ) -> io::Result> { + let mut table = vec![0; count as usize]; + self.file.seek(SeekFrom::Start(offset))?; + self.file.read_u64_into::(&mut table)?; + if let Some(m) = mask { + for ptr in &mut table { + *ptr &= m; + } + } + Ok(table) + } + + /// Reads a cluster's worth of 64 bit offsets and returns them as a vector. + /// `mask` optionally ands out some of the bits on the file. + pub fn read_pointer_cluster(&mut self, offset: u64, mask: Option) -> io::Result> { + let count = self.cluster_size / size_of::() as u64; + self.read_pointer_table(offset, count, mask) + } + + /// Writes `table` of u64 pointers to `offset` in the file. + /// `non_zero_flags` will be ORed with all non-zero values in `table`. + /// writing. + pub fn write_pointer_table( + &mut self, + offset: u64, + table: &[u64], + non_zero_flags: u64, + ) -> io::Result<()> { + self.file.seek(SeekFrom::Start(offset))?; + let mut buffer = BufWriter::with_capacity(table.len() * size_of::(), &mut self.file); + for addr in table { + let val = if *addr == 0 { + 0 + } else { + *addr | non_zero_flags + }; + buffer.write_u64::(val)?; + } + Ok(()) + } + + /// Read a refcount block from the file and returns a Vec containing the block. + /// Always returns a cluster's worth of data. + pub fn read_refcount_block(&mut self, offset: u64) -> io::Result> { + let count = self.cluster_size / size_of::() as u64; + let mut table = vec![0; count as usize]; + self.file.seek(SeekFrom::Start(offset))?; + self.file.read_u16_into::(&mut table)?; + Ok(table) + } + + /// Writes a refcount block to the file. + pub fn write_refcount_block(&mut self, offset: u64, table: &[u16]) -> io::Result<()> { + self.file.seek(SeekFrom::Start(offset))?; + let mut buffer = BufWriter::with_capacity(table.len() * size_of::(), &mut self.file); + for count in table { + buffer.write_u16::(*count)?; + } + Ok(()) + } + + /// Allocates a new cluster at the end of the current file, return the address. + pub fn add_cluster_end(&mut self, max_valid_cluster_offset: u64) -> io::Result> { + // Determine where the new end of the file should be and set_len, which + // translates to truncate(2). + let file_end: u64 = self.file.seek(SeekFrom::End(0))?; + let new_cluster_address: u64 = (file_end + self.cluster_size - 1) & !self.cluster_mask; + + if new_cluster_address > max_valid_cluster_offset { + return Ok(None); + } + + self.file.set_len(new_cluster_address + self.cluster_size)?; + + Ok(Some(new_cluster_address)) + } + + /// Returns a mutable reference to the underlying file. + pub fn file_mut(&mut self) -> &mut RawFile { + &mut self.file + } + + /// Returns the size of the file's clusters. + pub fn cluster_size(&self) -> u64 { + self.cluster_size + } + + /// Returns the offset of `address` within a cluster. + pub fn cluster_offset(&self, address: u64) -> u64 { + address & self.cluster_mask + } + + /// Zeros out a cluster in the file. + pub fn zero_cluster(&mut self, address: u64) -> io::Result<()> { + let cluster_size = self.cluster_size as usize; + self.file.seek(SeekFrom::Start(address))?; + self.file.write_zeroes(cluster_size)?; + Ok(()) + } +} + +impl Clone for QcowRawFile { + fn clone(&self) -> Self { + QcowRawFile { + file: self.file.try_clone().expect("QcowRawFile cloning failed"), + cluster_size: self.cluster_size, + cluster_mask: self.cluster_mask, + } + } +} diff --git a/vendor/cube-hypervisor/qcow/src/raw_file.rs b/vendor/cube-hypervisor/qcow/src/raw_file.rs new file mode 100644 index 0000000..886f048 --- /dev/null +++ b/vendor/cube-hypervisor/qcow/src/raw_file.rs @@ -0,0 +1,353 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use libc::c_void; +use std::alloc::{alloc_zeroed, dealloc, Layout}; +use std::convert::TryInto; +use std::fs::{File, Metadata}; +use std::io::{self, Read, Seek, SeekFrom, Write}; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::slice; +use vmm_sys_util::{seek_hole::SeekHole, write_zeroes::PunchHole, write_zeroes::WriteZeroesAt}; + +#[derive(Debug)] +pub struct RawFile { + file: File, + alignment: usize, + position: u64, +} + +const BLK_ALIGNMENTS: [usize; 2] = [512, 4096]; + +fn is_valid_alignment(fd: RawFd, alignment: usize) -> bool { + let layout = Layout::from_size_align(alignment, alignment).unwrap(); + // SAFETY: layout has non-zero size + let ptr = unsafe { alloc_zeroed(layout) }; + assert!(!ptr.is_null()); + + // SAFETY: FFI call + let ret = unsafe { + ::libc::pread( + fd, + ptr as *mut c_void, + alignment, + alignment.try_into().unwrap(), + ) + }; + + // SAFETY: ptr was allocated by alloc_zeroed with layout + unsafe { dealloc(ptr, layout) }; + + ret >= 0 +} + +impl RawFile { + pub fn new(file: File, direct_io: bool) -> Self { + // Assume no alignment restrictions if we aren't using O_DIRECT. + let mut alignment = 0; + if direct_io { + for align in &BLK_ALIGNMENTS { + if is_valid_alignment(file.as_raw_fd(), *align) { + alignment = *align; + break; + } + } + } + RawFile { + file, + alignment, + position: 0, + } + } + + fn round_up(&self, offset: u64) -> u64 { + let align: u64 = self.alignment.try_into().unwrap(); + ((offset + align - 1) / align) * align + } + + fn round_down(&self, offset: u64) -> u64 { + let align: u64 = self.alignment.try_into().unwrap(); + (offset / align) * align + } + + fn is_aligned(&self, buf: &[u8]) -> bool { + if self.alignment == 0 { + return true; + } + + let align64: u64 = self.alignment.try_into().unwrap(); + + (self.position % align64 == 0) + && ((buf.as_ptr() as usize) % self.alignment == 0) + && (buf.len() % self.alignment == 0) + } + + pub fn set_len(&self, size: u64) -> std::io::Result<()> { + self.file.set_len(size) + } + + pub fn metadata(&self) -> std::io::Result { + self.file.metadata() + } + + pub fn try_clone(&self) -> std::io::Result { + Ok(RawFile { + file: self.file.try_clone().expect("RawFile cloning failed"), + alignment: self.alignment, + position: self.position, + }) + } + + pub fn sync_all(&self) -> std::io::Result<()> { + self.file.sync_all() + } + + pub fn sync_data(&self) -> std::io::Result<()> { + self.file.sync_data() + } +} + +impl Read for RawFile { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + if self.is_aligned(buf) { + match self.file.read(buf) { + Ok(r) => { + self.position = self.position.checked_add(r.try_into().unwrap()).unwrap(); + Ok(r) + } + Err(e) => Err(e), + } + } else { + let rounded_pos: u64 = self.round_down(self.position); + let file_offset: usize = self + .position + .checked_sub(rounded_pos) + .unwrap() + .try_into() + .unwrap(); + let buf_len: usize = buf.len(); + let rounded_len: usize = self + .round_up( + file_offset + .checked_add(buf_len) + .unwrap() + .try_into() + .unwrap(), + ) + .try_into() + .unwrap(); + + let layout = Layout::from_size_align(rounded_len, self.alignment).unwrap(); + // SAFETY: layout has non-zero size + let tmp_ptr = unsafe { alloc_zeroed(layout) }; + if tmp_ptr.is_null() { + return Err(io::Error::last_os_error()); + } + + // SAFETY: tmp_ptr is valid and at least rounded_len long + let tmp_buf = unsafe { slice::from_raw_parts_mut(tmp_ptr, rounded_len) }; + + // This can eventually replaced with read_at once its interface + // has been stabilized. + // SAFETY: FFI call. All parameters are valid. + let ret = unsafe { + ::libc::pread64( + self.file.as_raw_fd(), + tmp_buf.as_mut_ptr() as *mut c_void, + tmp_buf.len(), + rounded_pos.try_into().unwrap(), + ) + }; + if ret < 0 { + // SAFETY: tmp_ptr was allocated by alloc_zeroed with layout + unsafe { dealloc(tmp_ptr, layout) }; + return Err(io::Error::last_os_error()); + } + + let read: usize = ret.try_into().unwrap(); + if read < file_offset { + // SAFETY: tmp_ptr was allocated by alloc_zeroed with layout + unsafe { dealloc(tmp_ptr, layout) }; + return Ok(0); + } + + let mut to_copy = read - file_offset; + if to_copy > buf_len { + to_copy = buf_len; + } + + buf.copy_from_slice(&tmp_buf[file_offset..(file_offset + buf_len)]); + // SAFETY: tmp_ptr was allocated by alloc_zeroed with layout + unsafe { dealloc(tmp_ptr, layout) }; + + self.seek(SeekFrom::Current(to_copy.try_into().unwrap())) + .unwrap(); + Ok(to_copy) + } + } +} + +impl Write for RawFile { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + if self.is_aligned(buf) { + match self.file.write(buf) { + Ok(r) => { + self.position = self.position.checked_add(r.try_into().unwrap()).unwrap(); + Ok(r) + } + Err(e) => Err(e), + } + } else { + let rounded_pos: u64 = self.round_down(self.position); + let file_offset: usize = self + .position + .checked_sub(rounded_pos) + .unwrap() + .try_into() + .unwrap(); + let buf_len: usize = buf.len(); + let rounded_len: usize = self + .round_up( + file_offset + .checked_add(buf_len) + .unwrap() + .try_into() + .unwrap(), + ) + .try_into() + .unwrap(); + + let layout = Layout::from_size_align(rounded_len, self.alignment).unwrap(); + // SAFETY: layout has non-zero size + let tmp_ptr = unsafe { alloc_zeroed(layout) }; + if tmp_ptr.is_null() { + return Err(io::Error::last_os_error()); + } + + let tmp_buf = unsafe { slice::from_raw_parts_mut(tmp_ptr, rounded_len) }; + + // This can eventually replaced with read_at once its interface + // has been stabilized. + // SAFETY: FFI call + let ret = unsafe { + ::libc::pread64( + self.file.as_raw_fd(), + tmp_buf.as_mut_ptr() as *mut c_void, + tmp_buf.len(), + rounded_pos.try_into().unwrap(), + ) + }; + if ret < 0 { + // SAFETY: tmp_ptr was allocated by alloc_zeroed with layout + unsafe { dealloc(tmp_ptr, layout) }; + return Err(io::Error::last_os_error()); + }; + + tmp_buf[file_offset..(file_offset + buf_len)].copy_from_slice(buf); + + // This can eventually replaced with write_at once its interface + // has been stabilized. + // SAFETY: FFI call + let ret = unsafe { + ::libc::pwrite64( + self.file.as_raw_fd(), + tmp_buf.as_ptr() as *const c_void, + tmp_buf.len(), + rounded_pos.try_into().unwrap(), + ) + }; + + // SAFETY: tmp_ptr was allocated by alloc_zeroed with layout + unsafe { dealloc(tmp_ptr, layout) }; + + if ret < 0 { + return Err(io::Error::last_os_error()); + } + + let written: usize = ret.try_into().unwrap(); + if written < file_offset { + Ok(0) + } else { + let mut to_seek = written - file_offset; + if to_seek > buf_len { + to_seek = buf_len; + } + + self.seek(SeekFrom::Current(to_seek.try_into().unwrap())) + .unwrap(); + Ok(to_seek) + } + } + } + + fn flush(&mut self) -> std::io::Result<()> { + self.file.sync_all() + } +} + +impl Seek for RawFile { + fn seek(&mut self, newpos: SeekFrom) -> std::io::Result { + match self.file.seek(newpos) { + Ok(pos) => { + self.position = pos; + Ok(pos) + } + Err(e) => Err(e), + } + } +} + +impl WriteZeroesAt for RawFile { + fn write_zeroes_at(&mut self, offset: u64, length: usize) -> std::io::Result { + self.file.write_zeroes_at(offset, length) + } +} + +impl PunchHole for RawFile { + fn punch_hole(&mut self, offset: u64, length: u64) -> std::io::Result<()> { + self.file.punch_hole(offset, length) + } +} + +impl SeekHole for RawFile { + fn seek_hole(&mut self, offset: u64) -> std::io::Result> { + match self.file.seek_hole(offset) { + Ok(pos) => { + if let Some(p) = pos { + self.position = p; + } + Ok(pos) + } + Err(e) => Err(e), + } + } + + fn seek_data(&mut self, offset: u64) -> std::io::Result> { + match self.file.seek_data(offset) { + Ok(pos) => { + if let Some(p) = pos { + self.position = p; + } + Ok(pos) + } + Err(e) => Err(e), + } + } +} + +impl Clone for RawFile { + fn clone(&self) -> Self { + RawFile { + file: self.file.try_clone().expect("RawFile cloning failed"), + alignment: self.alignment, + position: self.position, + } + } +} diff --git a/vendor/cube-hypervisor/qcow/src/refcount.rs b/vendor/cube-hypervisor/qcow/src/refcount.rs new file mode 100644 index 0000000..ab3968c --- /dev/null +++ b/vendor/cube-hypervisor/qcow/src/refcount.rs @@ -0,0 +1,252 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use std::fmt::{self, Display}; +use std::io; + +use libc::EINVAL; + +use crate::qcow_raw_file::QcowRawFile; +use crate::vec_cache::{CacheMap, Cacheable, VecCache}; + +#[derive(Debug)] +pub enum Error { + /// `EvictingCache` - Error writing a refblock from the cache to disk. + EvictingRefCounts(io::Error), + /// `InvalidIndex` - Address requested isn't within the range of the disk. + InvalidIndex, + /// `NeedCluster` - Handle this error by reading the cluster and calling the function again. + NeedCluster(u64), + /// `NeedNewCluster` - Handle this error by allocating a cluster and calling the function again. + NeedNewCluster, + /// `ReadingRefCounts` - Error reading the file in to the refcount cache. + ReadingRefCounts(io::Error), +} + +pub type Result = std::result::Result; + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + + match self { + EvictingRefCounts(e) => write!( + f, + "failed to write a refblock from the cache to disk: {}", + e + ), + InvalidIndex => write!(f, "address requested is not within the range of the disk"), + NeedCluster(addr) => write!(f, "cluster with addr={} needs to be read", addr), + NeedNewCluster => write!(f, "new cluster needs to be allocated for refcounts"), + ReadingRefCounts(e) => { + write!(f, "failed to read the file into the refcount cache: {}", e) + } + } + } +} + +/// Represents the refcount entries for an open qcow file. +#[derive(Clone, Debug)] +pub struct RefCount { + ref_table: VecCache, + refcount_table_offset: u64, + refblock_cache: CacheMap>, + refcount_block_entries: u64, // number of refcounts in a cluster. + cluster_size: u64, + max_valid_cluster_offset: u64, +} + +impl RefCount { + /// Creates a `RefCount` from `file`, reading the refcount table from `refcount_table_offset`. + /// `refcount_table_entries` specifies the number of refcount blocks used by this image. + /// `refcount_block_entries` indicates the number of refcounts in each refcount block. + /// Each refcount table entry points to a refcount block. + pub fn new( + raw_file: &mut QcowRawFile, + refcount_table_offset: u64, + refcount_table_entries: u64, + refcount_block_entries: u64, + cluster_size: u64, + ) -> io::Result { + let ref_table = VecCache::from_vec(raw_file.read_pointer_table( + refcount_table_offset, + refcount_table_entries, + None, + )?); + let max_valid_cluster_index = (ref_table.len() as u64) * refcount_block_entries - 1; + let max_valid_cluster_offset = max_valid_cluster_index * cluster_size; + Ok(RefCount { + ref_table, + refcount_table_offset, + refblock_cache: CacheMap::new(50), + refcount_block_entries, + cluster_size, + max_valid_cluster_offset, + }) + } + + /// Returns the number of refcounts per block. + pub fn refcounts_per_block(&self) -> u64 { + self.refcount_block_entries + } + + /// Returns the maximum valid cluster offset in the raw file for this refcount table. + pub fn max_valid_cluster_offset(&self) -> u64 { + self.max_valid_cluster_offset + } + + /// Returns `NeedNewCluster` if a new cluster needs to be allocated for refcounts. If an + /// existing cluster needs to be read, `NeedCluster(addr)` is returned. The Caller should + /// allocate a cluster or read the required one and call this function again with the cluster. + /// On success, an optional address of a dropped cluster is returned. The dropped cluster can + /// be reused for other purposes. + pub fn set_cluster_refcount( + &mut self, + raw_file: &mut QcowRawFile, + cluster_address: u64, + refcount: u16, + mut new_cluster: Option<(u64, VecCache)>, + ) -> Result> { + let (table_index, block_index) = self.get_refcount_index(cluster_address); + + let block_addr_disk = *self.ref_table.get(table_index).ok_or(Error::InvalidIndex)?; + + // Fill the cache if this block isn't yet there. + if !self.refblock_cache.contains_key(table_index) { + // Need a new cluster + if let Some((addr, table)) = new_cluster.take() { + self.ref_table[table_index] = addr; + let ref_table = &self.ref_table; + self.refblock_cache + .insert(table_index, table, |index, evicted| { + raw_file.write_refcount_block(ref_table[index], evicted.get_values()) + }) + .map_err(Error::EvictingRefCounts)?; + } else { + if block_addr_disk == 0 { + return Err(Error::NeedNewCluster); + } + return Err(Error::NeedCluster(block_addr_disk)); + } + } + + // Unwrap is safe here as the entry was filled directly above. + let dropped_cluster = if !self.refblock_cache.get(table_index).unwrap().dirty() { + // Free the previously used block and use a new one. Writing modified counts to new + // blocks keeps the on-disk state consistent even if it's out of date. + if let Some((addr, _)) = new_cluster.take() { + self.ref_table[table_index] = addr; + Some(block_addr_disk) + } else { + return Err(Error::NeedNewCluster); + } + } else { + None + }; + + self.refblock_cache.get_mut(table_index).unwrap()[block_index] = refcount; + Ok(dropped_cluster) + } + + /// Flush the dirty refcount blocks. This must be done before flushing the table that points to + /// the blocks. + pub fn flush_blocks(&mut self, raw_file: &mut QcowRawFile) -> io::Result<()> { + // Write out all dirty L2 tables. + for (table_index, block) in self.refblock_cache.iter_mut().filter(|(_k, v)| v.dirty()) { + let addr = self.ref_table[*table_index]; + if addr != 0 { + raw_file.write_refcount_block(addr, block.get_values())?; + } else { + return Err(std::io::Error::from_raw_os_error(EINVAL)); + } + block.mark_clean(); + } + Ok(()) + } + + /// Flush the refcount table that keeps the address of the refcounts blocks. + /// Returns true if the table changed since the previous `flush_table()` call. + pub fn flush_table(&mut self, raw_file: &mut QcowRawFile) -> io::Result { + if self.ref_table.dirty() { + raw_file.write_pointer_table( + self.refcount_table_offset, + self.ref_table.get_values(), + 0, + )?; + self.ref_table.mark_clean(); + Ok(true) + } else { + Ok(false) + } + } + + /// Gets the refcount for a cluster with the given address. + pub fn get_cluster_refcount( + &mut self, + raw_file: &mut QcowRawFile, + address: u64, + ) -> Result { + let (table_index, block_index) = self.get_refcount_index(address); + let block_addr_disk = *self.ref_table.get(table_index).ok_or(Error::InvalidIndex)?; + if block_addr_disk == 0 { + return Ok(0); + } + if !self.refblock_cache.contains_key(table_index) { + let table = VecCache::from_vec( + raw_file + .read_refcount_block(block_addr_disk) + .map_err(Error::ReadingRefCounts)?, + ); + let ref_table = &self.ref_table; + self.refblock_cache + .insert(table_index, table, |index, evicted| { + raw_file.write_refcount_block(ref_table[index], evicted.get_values()) + }) + .map_err(Error::EvictingRefCounts)?; + } + Ok(self.refblock_cache.get(table_index).unwrap()[block_index]) + } + + /// Returns the refcount table for this file. This is only useful for debugging. + pub fn ref_table(&self) -> &[u64] { + self.ref_table.get_values() + } + + /// Returns the refcounts stored in the given block. + pub fn refcount_block( + &mut self, + raw_file: &mut QcowRawFile, + table_index: usize, + ) -> Result> { + let block_addr_disk = *self.ref_table.get(table_index).ok_or(Error::InvalidIndex)?; + if block_addr_disk == 0 { + return Ok(None); + } + if !self.refblock_cache.contains_key(table_index) { + let table = VecCache::from_vec( + raw_file + .read_refcount_block(block_addr_disk) + .map_err(Error::ReadingRefCounts)?, + ); + // TODO(dgreid) - closure needs to return an error. + let ref_table = &self.ref_table; + self.refblock_cache + .insert(table_index, table, |index, evicted| { + raw_file.write_refcount_block(ref_table[index], evicted.get_values()) + }) + .map_err(Error::EvictingRefCounts)?; + } + // The index must exist as it was just inserted if it didn't already. + Ok(Some( + self.refblock_cache.get(table_index).unwrap().get_values(), + )) + } + + // Gets the address of the refcount block and the index into the block for the given address. + fn get_refcount_index(&self, address: u64) -> (usize, usize) { + let block_index = (address / self.cluster_size) % self.refcount_block_entries; + let refcount_table_index = (address / self.cluster_size) / self.refcount_block_entries; + (refcount_table_index as usize, block_index as usize) + } +} diff --git a/vendor/cube-hypervisor/qcow/src/vec_cache.rs b/vendor/cube-hypervisor/qcow/src/vec_cache.rs new file mode 100644 index 0000000..478f519 --- /dev/null +++ b/vendor/cube-hypervisor/qcow/src/vec_cache.rs @@ -0,0 +1,185 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use std::collections::hash_map::IterMut; +use std::collections::HashMap; +use std::io; +use std::ops::{Index, IndexMut}; +use std::slice::SliceIndex; + +/// Trait that allows for checking if an implementor is dirty. Useful for types that are cached so +/// it can be checked if they need to be committed to disk. +pub trait Cacheable { + /// Used to check if the item needs to be written out or if it can be discarded. + fn dirty(&self) -> bool; +} + +#[derive(Clone, Debug)] +/// Represents a vector that implements the `Cacheable` trait so it can be held in a cache. +pub struct VecCache { + vec: Box<[T]>, + dirty: bool, +} + +impl VecCache { + /// Creates a `VecCache` that can hold `count` elements. + pub fn new(count: usize) -> VecCache { + VecCache { + vec: vec![Default::default(); count].into_boxed_slice(), + dirty: true, + } + } + + /// Creates a `VecCache` from the passed in `vec`. + pub fn from_vec(vec: Vec) -> VecCache { + VecCache { + vec: vec.into_boxed_slice(), + dirty: false, + } + } + + pub fn get(&self, index: I) -> Option<&>::Output> + where + I: SliceIndex<[T]>, + { + self.vec.get(index) + } + + /// Gets a reference to the underlying vector. + pub fn get_values(&self) -> &[T] { + &self.vec + } + + /// Mark this cache element as clean. + pub fn mark_clean(&mut self) { + self.dirty = false; + } + + /// Returns the number of elements in the vector. + pub fn len(&self) -> usize { + self.vec.len() + } +} + +impl Cacheable for VecCache { + fn dirty(&self) -> bool { + self.dirty + } +} + +impl Index for VecCache { + type Output = T; + + fn index(&self, index: usize) -> &T { + self.vec.index(index) + } +} + +impl IndexMut for VecCache { + fn index_mut(&mut self, index: usize) -> &mut T { + self.dirty = true; + self.vec.index_mut(index) + } +} + +#[derive(Clone, Debug)] +pub struct CacheMap { + capacity: usize, + map: HashMap, +} + +impl CacheMap { + pub fn new(capacity: usize) -> Self { + CacheMap { + capacity, + map: HashMap::with_capacity(capacity), + } + } + + pub fn contains_key(&self, key: usize) -> bool { + self.map.contains_key(&key) + } + + pub fn get(&self, index: usize) -> Option<&T> { + self.map.get(&index) + } + + pub fn get_mut(&mut self, index: usize) -> Option<&mut T> { + self.map.get_mut(&index) + } + + pub fn iter_mut(&mut self) -> IterMut { + self.map.iter_mut() + } + + // Check if the refblock cache is full and we need to evict. + pub fn insert(&mut self, index: usize, block: T, write_callback: F) -> io::Result<()> + where + F: FnOnce(usize, T) -> io::Result<()>, + { + if self.map.len() == self.capacity { + // TODO(dgreid) - smarter eviction strategy. + let to_evict = *self.map.iter().next().unwrap().0; + if let Some(evicted) = self.map.remove(&to_evict) { + if evicted.dirty() { + write_callback(to_evict, evicted)?; + } + } + } + self.map.insert(index, block); + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + struct NumCache(()); + impl Cacheable for NumCache { + fn dirty(&self) -> bool { + true + } + } + + #[test] + fn evicts_when_full() { + let mut cache = CacheMap::::new(3); + let mut evicted = None; + cache + .insert(0, NumCache(()), |index, _| { + evicted = Some(index); + Ok(()) + }) + .unwrap(); + assert_eq!(evicted, None); + cache + .insert(1, NumCache(()), |index, _| { + evicted = Some(index); + Ok(()) + }) + .unwrap(); + assert_eq!(evicted, None); + cache + .insert(2, NumCache(()), |index, _| { + evicted = Some(index); + Ok(()) + }) + .unwrap(); + assert_eq!(evicted, None); + cache + .insert(3, NumCache(()), |index, _| { + evicted = Some(index); + Ok(()) + }) + .unwrap(); + assert!(evicted.is_some()); + + // Check that three of the four items inserted are still there and that the most recently + // inserted is one of them. + let num_items = (0..=3).filter(|k| cache.contains_key(*k)).count(); + assert_eq!(num_items, 3); + assert!(cache.contains_key(3)); + } +} diff --git a/vendor/cube-hypervisor/rate_limiter/Cargo.toml b/vendor/cube-hypervisor/rate_limiter/Cargo.toml new file mode 100644 index 0000000..1afd0ea --- /dev/null +++ b/vendor/cube-hypervisor/rate_limiter/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rate_limiter" +version = "0.1.0" +edition = "2021" + +[dependencies] +libc = "0.2.137" +log = "0.4.17" +vmm-sys-util = { workspace = true } diff --git a/vendor/cube-hypervisor/rate_limiter/src/lib.rs b/vendor/cube-hypervisor/rate_limiter/src/lib.rs new file mode 100644 index 0000000..3159cf1 --- /dev/null +++ b/vendor/cube-hypervisor/rate_limiter/src/lib.rs @@ -0,0 +1,918 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +#![deny(missing_docs)] +//! # Rate Limiter +//! +//! Provides a rate limiter written in Rust useful for IO operations that need to +//! be throttled. +//! +//! ## Behavior +//! +//! The rate limiter starts off as 'unblocked' with two token buckets configured +//! with the values passed in the `RateLimiter::new()` constructor. +//! All subsequent accounting is done independently for each token bucket based +//! on the `TokenType` used. If any of the buckets runs out of budget, the limiter +//! goes in the 'blocked' state. At this point an internal timer is set up which +//! will later 'wake up' the user in order to retry sending data. The 'wake up' +//! notification will be dispatched as an event on the FD provided by the `AsRawFD` +//! trait implementation. +//! +//! The contract is that the user shall also call the `event_handler()` method on +//! receipt of such an event. +//! +//! The token buckets are replenished every time a `consume()` is called, before +//! actually trying to consume the requested amount of tokens. The amount of tokens +//! replenished is automatically calculated to respect the `complete_refill_time` +//! configuration parameter provided by the user. The token buckets will never +//! replenish above their respective `size`. +//! +//! Each token bucket can start off with a `one_time_burst` initial extra capacity +//! on top of their `size`. This initial extra credit does not replenish and +//! can be used for an initial burst of data. +//! +//! The granularity for 'wake up' events when the rate limiter is blocked is +//! currently hardcoded to `100 milliseconds`. +//! +//! ## Limitations +//! +//! This rate limiter implementation relies on the *Linux kernel's timerfd* so its +//! usage is limited to Linux systems. +//! +//! Another particularity of this implementation is that it is not self-driving. +//! It is meant to be used in an external event loop and thus implements the `AsRawFd` +//! trait and provides an *event-handler* as part of its API. This *event-handler* +//! needs to be called by the user on every event on the rate limiter's `AsRawFd` FD. +#[macro_use] +extern crate log; + +use std::os::unix::io::{AsRawFd, RawFd}; +use std::time::{Duration, Instant}; +use std::{fmt, io}; +use vmm_sys_util::timerfd::TimerFd; + +#[derive(Debug)] +/// Describes the errors that may occur while handling rate limiter events. +pub enum Error { + /// The event handler was called spuriously. + SpuriousRateLimiterEvent(&'static str), + /// The event handler encounters while TimerFd::wait() + TimerFdWaitError(std::io::Error), +} + +// Interval at which the refill timer will run when limiter is at capacity. +const REFILL_TIMER_INTERVAL_MS: u64 = 100; +const TIMER_REFILL_DUR: Duration = Duration::from_millis(REFILL_TIMER_INTERVAL_MS); + +const NANOSEC_IN_ONE_MILLISEC: u64 = 1_000_000; + +// Euclid's two-thousand-year-old algorithm for finding the greatest common divisor. +fn gcd(x: u64, y: u64) -> u64 { + let mut x = x; + let mut y = y; + while y != 0 { + let t = y; + y = x % y; + x = t; + } + x +} + +/// Enum describing the outcomes of a `reduce()` call on a `TokenBucket`. +#[derive(Clone, Debug, PartialEq)] +pub enum BucketReduction { + /// There are not enough tokens to complete the operation. + Failure, + /// A part of the available tokens have been consumed. + Success, + /// A number of tokens `inner` times larger than the bucket size have been consumed. + OverConsumption(f64), +} + +/// TokenBucket provides a lower level interface to rate limiting with a +/// configurable capacity, refill-rate and initial burst. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct TokenBucket { + // Bucket defining traits. + size: u64, + // Initial burst size (number of free initial tokens, that can be consumed at no cost) + one_time_burst: u64, + // Complete refill time in milliseconds. + refill_time: u64, + + // Internal state descriptors. + budget: u64, + last_update: Instant, + + // Fields used for pre-processing optimizations. + processed_capacity: u64, + processed_refill_time: u64, +} + +impl TokenBucket { + /// Creates a `TokenBucket` wrapped in an `Option`. + /// + /// TokenBucket created is of `size` total capacity and takes `complete_refill_time_ms` + /// milliseconds to go from zero tokens to total capacity. The `one_time_burst` is initial + /// extra credit on top of total capacity, that does not replenish and which can be used + /// for an initial burst of data. + /// + /// If the `size` or the `complete refill time` are zero, then `None` is returned. + pub fn new(size: u64, one_time_burst: u64, complete_refill_time_ms: u64) -> Option { + // If either token bucket capacity or refill time is 0, disable limiting. + if size == 0 || complete_refill_time_ms == 0 { + return None; + } + // Formula for computing current refill amount: + // refill_token_count = (delta_time * size) / (complete_refill_time_ms * 1_000_000) + // In order to avoid overflows, simplify the fractions by computing greatest common divisor. + + let complete_refill_time_ns = complete_refill_time_ms * NANOSEC_IN_ONE_MILLISEC; + // Get the greatest common factor between `size` and `complete_refill_time_ns`. + let common_factor = gcd(size, complete_refill_time_ns); + // The division will be exact since `common_factor` is a factor of `size`. + let processed_capacity: u64 = size / common_factor; + // The division will be exact since `common_factor` is a factor of `complete_refill_time_ns`. + let processed_refill_time: u64 = complete_refill_time_ns / common_factor; + + Some(TokenBucket { + size, + one_time_burst, + refill_time: complete_refill_time_ms, + // Start off full. + budget: size, + // Last updated is now. + last_update: Instant::now(), + processed_capacity, + processed_refill_time, + }) + } + + /// Attempts to consume `tokens` from the bucket and returns whether the action succeeded. + // TODO (Issue #259): handle cases where a single request is larger than the full capacity + // for such cases we need to support partial fulfilment of requests + pub fn reduce(&mut self, mut tokens: u64) -> BucketReduction { + // First things first: consume the one-time-burst budget. + if self.one_time_burst > 0 { + // We still have burst budget for *all* tokens requests. + if self.one_time_burst >= tokens { + self.one_time_burst -= tokens; + self.last_update = Instant::now(); + // No need to continue to the refill process, we still have burst budget to consume from. + return BucketReduction::Success; + } else { + // We still have burst budget for *some* of the tokens requests. + // The tokens left unfulfilled will be consumed from current `self.budget`. + tokens -= self.one_time_burst; + self.one_time_burst = 0; + } + } + + // Compute time passed since last refill/update. + let time_delta = self.last_update.elapsed().as_nanos() as u64; + self.last_update = Instant::now(); + + // At each 'time_delta' nanoseconds the bucket should refill with: + // refill_amount = (time_delta * size) / (complete_refill_time_ms * 1_000_000) + // `processed_capacity` and `processed_refill_time` are the result of simplifying above + // fraction formula with their greatest-common-factor. + self.budget += (time_delta * self.processed_capacity) / self.processed_refill_time; + + if self.budget >= self.size { + self.budget = self.size; + } + + if tokens > self.budget { + // This operation requests a bandwidth higher than the bucket size + if tokens > self.size { + error!( + "Consumed {} tokens from bucket of size {}", + tokens, self.size + ); + // Empty the bucket and report an overconsumption of + // (remaining tokens / size) times larger than the bucket size + tokens -= self.budget; + self.budget = 0; + return BucketReduction::OverConsumption(tokens as f64 / self.size as f64); + } + // If not enough tokens consume() fails, return false. + return BucketReduction::Failure; + } + + self.budget -= tokens; + BucketReduction::Success + } + + /// "Manually" adds tokens to bucket. + pub fn replenish(&mut self, tokens: u64) { + // This means we are still during the burst interval. + // Of course there is a very small chance that the last reduce() also used up burst + // budget which should now be replenished, but for performance and code-complexity + // reasons we're just gonna let that slide since it's practically inconsequential. + if self.one_time_burst > 0 { + self.one_time_burst += tokens; + return; + } + self.budget = std::cmp::min(self.budget + tokens, self.size); + } + + /// Returns the capacity of the token bucket. + pub fn capacity(&self) -> u64 { + self.size + } + + /// Returns the remaining one time burst budget. + pub fn one_time_burst(&self) -> u64 { + self.one_time_burst + } + + /// Returns the time in milliseconds required to to completely fill the bucket. + pub fn refill_time_ms(&self) -> u64 { + self.refill_time + } + + /// Returns the current budget (one time burst allowance notwithstanding). + pub fn budget(&self) -> u64 { + self.budget + } +} + +/// Enum that describes the type of token used. +pub enum TokenType { + /// Token type used for bandwidth limiting. + Bytes, + /// Token type used for operations/second limiting. + Ops, +} + +/// Enum that describes the type of token bucket update. +pub enum BucketUpdate { + /// No Update - same as before. + None, + /// Rate Limiting is disabled on this bucket. + Disabled, + /// Rate Limiting enabled with updated bucket. + Update(TokenBucket), +} + +/// Rate Limiter that works on both bandwidth and ops/s limiting. +/// +/// Bandwidth (bytes/s) and ops/s limiting can be used at the same time or individually. +/// +/// Implementation uses a single timer through TimerFd to refresh either or +/// both token buckets. +/// +/// Its internal buckets are 'passively' replenished as they're being used (as +/// part of `consume()` operations). +/// A timer is enabled and used to 'actively' replenish the token buckets when +/// limiting is in effect and `consume()` operations are disabled. +/// +/// RateLimiters will generate events on the FDs provided by their `AsRawFd` trait +/// implementation. These events are meant to be consumed by the user of this struct. +/// On each such event, the user must call the `event_handler()` method. +pub struct RateLimiter { + bandwidth: Option, + ops: Option, + + timer_fd: TimerFd, + // Internal flag that quickly determines timer state. + timer_active: bool, +} + +impl PartialEq for RateLimiter { + fn eq(&self, other: &RateLimiter) -> bool { + self.bandwidth == other.bandwidth && self.ops == other.ops + } +} + +impl fmt::Debug for RateLimiter { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "RateLimiter {{ bandwidth: {:?}, ops: {:?} }}", + self.bandwidth, self.ops + ) + } +} + +impl RateLimiter { + /// Creates a new Rate Limiter that can limit on both bytes/s and ops/s. + /// + /// # Arguments + /// + /// * `bytes_total_capacity` - the total capacity of the `TokenType::Bytes` token bucket. + /// * `bytes_one_time_burst` - initial extra credit on top of `bytes_total_capacity`, + /// that does not replenish and which can be used for an initial burst of data. + /// * `bytes_complete_refill_time_ms` - number of milliseconds for the `TokenType::Bytes` + /// token bucket to go from zero Bytes to `bytes_total_capacity` Bytes. + /// * `ops_total_capacity` - the total capacity of the `TokenType::Ops` token bucket. + /// * `ops_one_time_burst` - initial extra credit on top of `ops_total_capacity`, + /// that does not replenish and which can be used for an initial burst of data. + /// * `ops_complete_refill_time_ms` - number of milliseconds for the `TokenType::Ops` token + /// bucket to go from zero Ops to `ops_total_capacity` Ops. + /// + /// If either bytes/ops *size* or *refill_time* are **zero**, the limiter + /// is **disabled** for that respective token type. + /// + /// # Errors + /// + /// If the timerfd creation fails, an error is returned. + pub fn new( + bytes_total_capacity: u64, + bytes_one_time_burst: u64, + bytes_complete_refill_time_ms: u64, + ops_total_capacity: u64, + ops_one_time_burst: u64, + ops_complete_refill_time_ms: u64, + ) -> io::Result { + let bytes_token_bucket = TokenBucket::new( + bytes_total_capacity, + bytes_one_time_burst, + bytes_complete_refill_time_ms, + ); + + let ops_token_bucket = TokenBucket::new( + ops_total_capacity, + ops_one_time_burst, + ops_complete_refill_time_ms, + ); + + // We'll need a timer_fd, even if our current config effectively disables rate limiting, + // because `Self::update_buckets()` might re-enable it later, and we might be + // seccomp-blocked from creating the timer_fd at that time. + let timer_fd = TimerFd::new()?; + // Note: vmm_sys_util::TimerFd::new() open the fd w/o O_NONBLOCK. We manually add this flag + // so that `Self::event_handler` won't be blocked with `vmm_sys_util::TimerFd::wait()`. + // SAFETY: FFI calls. + let ret = unsafe { + let fd = timer_fd.as_raw_fd(); + let mut flags = libc::fcntl(fd, libc::F_GETFL); + flags |= libc::O_NONBLOCK; + libc::fcntl(fd, libc::F_SETFL, flags) + }; + if ret < 0 { + return Err(std::io::Error::last_os_error()); + } + + Ok(RateLimiter { + bandwidth: bytes_token_bucket, + ops: ops_token_bucket, + timer_fd, + timer_active: false, + }) + } + + // Arm the timer of the rate limiter with the provided `Duration` (which will fire only once). + fn activate_timer(&mut self, dur: Duration) { + // Panic when failing to arm the timer (same handling in crate TimerFd::set_state()) + self.timer_fd + .reset(dur, None) + .expect("Can't arm the timer (unexpected 'timerfd_settime' failure)."); + self.timer_active = true; + } + + /// Attempts to consume tokens and returns the result. + /// + /// If rate limiting is disabled on provided `token_type`, this function will always succeed. + pub fn consume(&mut self, tokens: u64, token_type: TokenType) -> BucketReduction { + // If the timer is active, we can't consume tokens from any bucket and the function fails. + if self.timer_active { + return BucketReduction::Failure; + } + + // Identify the required token bucket. + let token_bucket = match token_type { + TokenType::Bytes => self.bandwidth.as_mut(), + TokenType::Ops => self.ops.as_mut(), + }; + // Try to consume from the token bucket. + if let Some(bucket) = token_bucket { + let refill_time = bucket.refill_time_ms(); + match bucket.reduce(tokens) { + // When we report budget is over, there will be no further calls here, + // register a timer to replenish the bucket and resume processing; + // make sure there is only one running timer for this limiter. + BucketReduction::Failure => { + if !self.timer_active { + self.activate_timer(TIMER_REFILL_DUR); + } + BucketReduction::Failure + } + // The operation succeeded and further calls can be made. + BucketReduction::Success => BucketReduction::Success, + // The operation succeeded as the tokens have been consumed + // but the timer still needs to be armed. + BucketReduction::OverConsumption(ratio) => { + // The operation "borrowed" a number of tokens `ratio` times + // greater than the size of the bucket, and since it takes + // `refill_time` milliseconds to fill an empty bucket, in + // order to enforce the bandwidth limit we need to prevent + // further calls to the rate limiter for + // `ratio * refill_time` milliseconds. + self.activate_timer(Duration::from_millis((ratio * refill_time as f64) as u64)); + BucketReduction::OverConsumption(ratio) + } + } + } else { + // If bucket is not present rate limiting is disabled on token type, + // consume() will always succeed. + BucketReduction::Success + } + } + + /// Adds tokens of `token_type` to their respective bucket. + /// + /// Can be used to *manually* add tokens to a bucket. Useful for reverting a + /// `consume()` if needed. + pub fn manual_replenish(&mut self, tokens: u64, token_type: TokenType) { + // Identify the required token bucket. + let token_bucket = match token_type { + TokenType::Bytes => self.bandwidth.as_mut(), + TokenType::Ops => self.ops.as_mut(), + }; + // Add tokens to the token bucket. + if let Some(bucket) = token_bucket { + bucket.replenish(tokens); + } + } + + /// Returns whether this rate limiter is blocked. + /// + /// The limiter 'blocks' when a `consume()` operation fails because there was not enough + /// budget for it. + /// An event will be generated on the exported FD when the limiter 'unblocks'. + pub fn is_blocked(&self) -> bool { + self.timer_active + } + + /// This function needs to be called every time there is an event on the + /// FD provided by this object's `AsRawFd` trait implementation. + /// + /// # Errors + /// + /// If the rate limiter is disabled or is not blocked, an error is returned. + pub fn event_handler(&mut self) -> Result<(), Error> { + loop { + // Note: As we manually added the `O_NONBLOCK` flag to the FD, the following + // `timer_fd::wait()` won't block (which is different from its default behavior.) + match self.timer_fd.wait() { + Err(e) => { + let err: std::io::Error = e.into(); + match err.kind() { + std::io::ErrorKind::Interrupted => (), + std::io::ErrorKind::WouldBlock => { + return Err(Error::SpuriousRateLimiterEvent( + "Rate limiter event handler called without a present timer", + )) + } + _ => return Err(Error::TimerFdWaitError(err)), + } + } + _ => { + self.timer_active = false; + return Ok(()); + } + } + } + } + + /// Updates the parameters of the token buckets associated with this RateLimiter. + // TODO: Please note that, right now, the buckets become full after being updated. + pub fn update_buckets(&mut self, bytes: BucketUpdate, ops: BucketUpdate) { + match bytes { + BucketUpdate::Disabled => self.bandwidth = None, + BucketUpdate::Update(tb) => self.bandwidth = Some(tb), + BucketUpdate::None => (), + }; + match ops { + BucketUpdate::Disabled => self.ops = None, + BucketUpdate::Update(tb) => self.ops = Some(tb), + BucketUpdate::None => (), + }; + } + + /// Returns an immutable view of the inner bandwidth token bucket. + pub fn bandwidth(&self) -> Option<&TokenBucket> { + self.bandwidth.as_ref() + } + + /// Returns an immutable view of the inner ops token bucket. + pub fn ops(&self) -> Option<&TokenBucket> { + self.ops.as_ref() + } +} + +impl AsRawFd for RateLimiter { + /// Provides a FD which needs to be monitored for POLLIN events. + /// + /// This object's `event_handler()` method must be called on such events. + /// + /// Will return a negative value if rate limiting is disabled on both + /// token types. + fn as_raw_fd(&self) -> RawFd { + self.timer_fd.as_raw_fd() + } +} + +impl Default for RateLimiter { + /// Default RateLimiter is a no-op limiter with infinite budget. + fn default() -> Self { + // Safe to unwrap since this will not attempt to create timer_fd. + RateLimiter::new(0, 0, 0, 0, 0, 0).expect("Failed to build default RateLimiter") + } +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + use std::thread; + use std::time::Duration; + + impl TokenBucket { + // Resets the token bucket: budget set to max capacity and last-updated set to now. + fn reset(&mut self) { + self.budget = self.size; + self.last_update = Instant::now(); + } + + fn get_last_update(&self) -> &Instant { + &self.last_update + } + + fn get_processed_capacity(&self) -> u64 { + self.processed_capacity + } + + fn get_processed_refill_time(&self) -> u64 { + self.processed_refill_time + } + + // After a restore, we cannot be certain that the last_update field has the same value. + pub fn partial_eq(&self, other: &TokenBucket) -> bool { + (other.capacity() == self.capacity()) + && (other.one_time_burst() == self.one_time_burst()) + && (other.refill_time_ms() == self.refill_time_ms()) + && (other.budget() == self.budget()) + } + } + + impl RateLimiter { + fn get_token_bucket(&self, token_type: TokenType) -> Option<&TokenBucket> { + match token_type { + TokenType::Bytes => self.bandwidth.as_ref(), + TokenType::Ops => self.ops.as_ref(), + } + } + } + + #[test] + fn test_token_bucket_create() { + let before = Instant::now(); + let tb = TokenBucket::new(1000, 0, 1000).unwrap(); + assert_eq!(tb.capacity(), 1000); + assert_eq!(tb.budget(), 1000); + assert!(*tb.get_last_update() >= before); + let after = Instant::now(); + assert!(*tb.get_last_update() <= after); + assert_eq!(tb.get_processed_capacity(), 1); + assert_eq!(tb.get_processed_refill_time(), 1_000_000); + + // Verify invalid bucket configurations result in `None`. + assert!(TokenBucket::new(0, 1234, 1000).is_none()); + assert!(TokenBucket::new(100, 1234, 0).is_none()); + assert!(TokenBucket::new(0, 1234, 0).is_none()); + } + + #[test] + fn test_token_bucket_preprocess() { + let tb = TokenBucket::new(1000, 0, 1000).unwrap(); + assert_eq!(tb.get_processed_capacity(), 1); + assert_eq!(tb.get_processed_refill_time(), NANOSEC_IN_ONE_MILLISEC); + + let thousand = 1000; + let tb = TokenBucket::new(3 * 7 * 11 * 19 * thousand, 0, 7 * 11 * 13 * 17).unwrap(); + assert_eq!(tb.get_processed_capacity(), 3 * 19); + assert_eq!( + tb.get_processed_refill_time(), + 13 * 17 * (NANOSEC_IN_ONE_MILLISEC / thousand) + ); + } + + #[test] + fn test_token_bucket_reduce() { + // token bucket with capacity 1000 and refill time of 1000 milliseconds + // allowing rate of 1 token/ms. + let capacity = 1000; + let refill_ms = 1000; + let mut tb = TokenBucket::new(capacity, 0, refill_ms as u64).unwrap(); + + assert_eq!(tb.reduce(123), BucketReduction::Success); + assert_eq!(tb.budget(), capacity - 123); + + thread::sleep(Duration::from_millis(123)); + assert_eq!(tb.reduce(1), BucketReduction::Success); + assert_eq!(tb.budget(), capacity - 1); + assert_eq!(tb.reduce(100), BucketReduction::Success); + assert_eq!(tb.reduce(capacity), BucketReduction::Failure); + + // token bucket with capacity 1000 and refill time of 1000 milliseconds + let mut tb = TokenBucket::new(1000, 1100, 1000).unwrap(); + // safely assuming the thread can run these 3 commands in less than 500ms + assert_eq!(tb.reduce(1000), BucketReduction::Success); + assert_eq!(tb.one_time_burst(), 100); + assert_eq!(tb.reduce(500), BucketReduction::Success); + assert_eq!(tb.one_time_burst(), 0); + assert_eq!(tb.reduce(500), BucketReduction::Success); + assert_eq!(tb.reduce(500), BucketReduction::Failure); + thread::sleep(Duration::from_millis(500)); + assert_eq!(tb.reduce(500), BucketReduction::Success); + thread::sleep(Duration::from_millis(1000)); + assert_eq!(tb.reduce(2500), BucketReduction::OverConsumption(1.5)); + + let before = Instant::now(); + tb.reset(); + assert_eq!(tb.capacity(), 1000); + assert_eq!(tb.budget(), 1000); + assert!(*tb.get_last_update() >= before); + let after = Instant::now(); + assert!(*tb.get_last_update() <= after); + } + + #[test] + fn test_rate_limiter_default() { + let mut l = RateLimiter::default(); + + // limiter should not be blocked + assert!(!l.is_blocked()); + // limiter should be disabled so consume(whatever) should work + assert_eq!( + l.consume(u64::max_value(), TokenType::Ops), + BucketReduction::Success + ); + assert_eq!( + l.consume(u64::max_value(), TokenType::Bytes), + BucketReduction::Success + ); + // calling the handler without there having been an event should error + assert!(l.event_handler().is_err()); + assert_eq!( + format!("{:?}", l.event_handler().err().unwrap()), + "SpuriousRateLimiterEvent(\ + \"Rate limiter event handler called without a present timer\")" + ); + } + + #[test] + fn test_rate_limiter_new() { + let l = RateLimiter::new(1000, 1001, 1002, 1003, 1004, 1005).unwrap(); + + let bw = l.bandwidth.unwrap(); + assert_eq!(bw.capacity(), 1000); + assert_eq!(bw.one_time_burst(), 1001); + assert_eq!(bw.refill_time_ms(), 1002); + assert_eq!(bw.budget(), 1000); + + let ops = l.ops.unwrap(); + assert_eq!(ops.capacity(), 1003); + assert_eq!(ops.one_time_burst(), 1004); + assert_eq!(ops.refill_time_ms(), 1005); + assert_eq!(ops.budget(), 1003); + } + + #[test] + fn test_rate_limiter_manual_replenish() { + // rate limiter with limit of 1000 bytes/s and 1000 ops/s + let mut l = RateLimiter::new(1000, 0, 1000, 1000, 0, 1000).unwrap(); + + // consume 123 bytes + assert_eq!(l.consume(123, TokenType::Bytes), BucketReduction::Success); + l.manual_replenish(23, TokenType::Bytes); + { + let bytes_tb = l.get_token_bucket(TokenType::Bytes).unwrap(); + assert_eq!(bytes_tb.budget(), 900); + } + // consume 123 ops + assert_eq!(l.consume(123, TokenType::Ops), BucketReduction::Success); + l.manual_replenish(23, TokenType::Ops); + { + let bytes_tb = l.get_token_bucket(TokenType::Ops).unwrap(); + assert_eq!(bytes_tb.budget(), 900); + } + } + + #[test] + fn test_rate_limiter_bandwidth() { + // rate limiter with limit of 1000 bytes/s + let mut l = RateLimiter::new(1000, 0, 1000, 0, 0, 0).unwrap(); + + // limiter should not be blocked + assert!(!l.is_blocked()); + // raw FD for this disabled should be valid + assert!(l.as_raw_fd() > 0); + + // ops/s limiter should be disabled so consume(whatever) should work + assert_eq!( + l.consume(u64::max_value(), TokenType::Ops), + BucketReduction::Success + ); + + // do full 1000 bytes + assert_eq!(l.consume(1000, TokenType::Bytes), BucketReduction::Success); + // try and fail on another 100 + assert_eq!(l.consume(100, TokenType::Bytes), BucketReduction::Failure); + // since consume failed, limiter should be blocked now + assert!(l.is_blocked()); + // wait half the timer period + thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2)); + // limiter should still be blocked + assert!(l.is_blocked()); + // wait the other half of the timer period + thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2)); + // the timer_fd should have an event on it by now + assert!(l.event_handler().is_ok()); + // limiter should now be unblocked + assert!(!l.is_blocked()); + // try and succeed on another 100 bytes this time + assert_eq!(l.consume(100, TokenType::Bytes), BucketReduction::Success); + } + + #[test] + fn test_rate_limiter_ops() { + // rate limiter with limit of 1000 ops/s + let mut l = RateLimiter::new(0, 0, 0, 1000, 0, 1000).unwrap(); + + // limiter should not be blocked + assert!(!l.is_blocked()); + // raw FD for this disabled should be valid + assert!(l.as_raw_fd() > 0); + + // bytes/s limiter should be disabled so consume(whatever) should work + assert_eq!( + l.consume(u64::max_value(), TokenType::Bytes), + BucketReduction::Success + ); + + // do full 1000 ops + assert_eq!(l.consume(1000, TokenType::Ops), BucketReduction::Success); + // try and fail on another 100 + assert_eq!(l.consume(100, TokenType::Ops), BucketReduction::Failure); + // since consume failed, limiter should be blocked now + assert!(l.is_blocked()); + // wait half the timer period + thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2)); + // limiter should still be blocked + assert!(l.is_blocked()); + // wait the other half of the timer period + thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2)); + // the timer_fd should have an event on it by now + assert!(l.event_handler().is_ok()); + // limiter should now be unblocked + assert!(!l.is_blocked()); + // try and succeed on another 100 ops this time + assert_eq!(l.consume(100, TokenType::Ops), BucketReduction::Success); + } + + #[test] + fn test_rate_limiter_full() { + // rate limiter with limit of 1000 bytes/s and 1000 ops/s + let mut l = RateLimiter::new(1000, 0, 1000, 1000, 0, 1000).unwrap(); + + // limiter should not be blocked + assert!(!l.is_blocked()); + // raw FD for this disabled should be valid + assert!(l.as_raw_fd() > 0); + + // do full 1000 bytes + assert_eq!(l.consume(1000, TokenType::Ops), BucketReduction::Success); + // do full 1000 bytes + assert_eq!(l.consume(1000, TokenType::Bytes), BucketReduction::Success); + // try and fail on another 100 ops + assert_eq!(l.consume(100, TokenType::Ops), BucketReduction::Failure); + // try and fail on another 100 bytes + assert_eq!(l.consume(100, TokenType::Bytes), BucketReduction::Failure); + // since consume failed, limiter should be blocked now + assert!(l.is_blocked()); + // wait half the timer period + thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2)); + // limiter should still be blocked + assert!(l.is_blocked()); + // wait the other half of the timer period + thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2)); + // the timer_fd should have an event on it by now + assert!(l.event_handler().is_ok()); + // limiter should now be unblocked + assert!(!l.is_blocked()); + // try and succeed on another 100 ops this time + assert_eq!(l.consume(100, TokenType::Ops), BucketReduction::Success); + // try and succeed on another 100 bytes this time + assert_eq!(l.consume(100, TokenType::Bytes), BucketReduction::Success); + } + + #[test] + fn test_rate_limiter_overconsumption() { + // initialize the rate limiter + let mut l = RateLimiter::new(1000, 0, 1000, 1000, 0, 1000).unwrap(); + // try to consume 2.5x the bucket size + // we are "borrowing" 1.5x the bucket size in tokens since + // the bucket is full + assert_eq!( + l.consume(2500, TokenType::Bytes), + BucketReduction::OverConsumption(1.5_f64) + ); + + // check that even after a whole second passes, the rate limiter + // is still blocked + thread::sleep(Duration::from_millis(1000)); + assert!(l.event_handler().is_err()); + assert!(l.is_blocked()); + + // after 1.5x the replenish time has passed, the rate limiter + // is available again + thread::sleep(Duration::from_millis(500)); + assert!(l.event_handler().is_ok()); + assert!(!l.is_blocked()); + + // reset the rate limiter + let mut l = RateLimiter::new(1000, 0, 1000, 1000, 0, 1000).unwrap(); + // try to consume 1.5x the bucket size + // we are "borrowing" 1.5x the bucket size in tokens since + // the bucket is full, should arm the timer to 0.5x replenish + // time, which is 500 ms + assert_eq!( + l.consume(1500, TokenType::Bytes), + BucketReduction::OverConsumption(0.5_f64) + ); + + // check that after more than the minimum refill time, + // the rate limiter is still blocked + thread::sleep(Duration::from_millis(200)); + assert!(l.event_handler().is_err()); + assert!(l.is_blocked()); + + // try to consume some tokens, which should fail as the timer + // is still active + assert_eq!(l.consume(100, TokenType::Bytes), BucketReduction::Failure); + assert!(l.event_handler().is_err()); + assert!(l.is_blocked()); + + // check that after the minimum refill time, the timer was not + // overwritten and the rate limiter is still blocked from the + // borrowing we performed earlier + thread::sleep(Duration::from_millis(100)); + assert!(l.event_handler().is_err()); + assert!(l.is_blocked()); + assert_eq!(l.consume(100, TokenType::Bytes), BucketReduction::Failure); + + // after waiting out the full duration, rate limiter should be + // availale again + thread::sleep(Duration::from_millis(200)); + assert!(l.event_handler().is_ok()); + assert!(!l.is_blocked()); + assert_eq!(l.consume(100, TokenType::Bytes), BucketReduction::Success); + } + + #[test] + fn test_update_buckets() { + let mut x = RateLimiter::new(1000, 2000, 1000, 10, 20, 1000).unwrap(); + + let initial_bw = x.bandwidth.clone(); + let initial_ops = x.ops.clone(); + + x.update_buckets(BucketUpdate::None, BucketUpdate::None); + assert_eq!(x.bandwidth, initial_bw); + assert_eq!(x.ops, initial_ops); + + let new_bw = TokenBucket::new(123, 0, 57).unwrap(); + let new_ops = TokenBucket::new(321, 12346, 89).unwrap(); + x.update_buckets( + BucketUpdate::Update(new_bw.clone()), + BucketUpdate::Update(new_ops.clone()), + ); + + // We have manually adjust the last_update field, because it changes when update_buckets() + // constructs new buckets (and thus gets a different value for last_update). We do this so + // it makes sense to test the following assertions. + x.bandwidth.as_mut().unwrap().last_update = new_bw.last_update; + x.ops.as_mut().unwrap().last_update = new_ops.last_update; + + assert_eq!(x.bandwidth, Some(new_bw)); + assert_eq!(x.ops, Some(new_ops)); + + x.update_buckets(BucketUpdate::Disabled, BucketUpdate::Disabled); + assert_eq!(x.bandwidth, None); + assert_eq!(x.ops, None); + } + + #[test] + fn test_rate_limiter_debug() { + let l = RateLimiter::new(1, 2, 3, 4, 5, 6).unwrap(); + assert_eq!( + format!("{l:?}"), + format!( + "RateLimiter {{ bandwidth: {:?}, ops: {:?} }}", + l.bandwidth(), + l.ops() + ), + ); + } +} diff --git a/vendor/cube-hypervisor/release-notes.md b/vendor/cube-hypervisor/release-notes.md new file mode 100644 index 0000000..b5b00bd --- /dev/null +++ b/vendor/cube-hypervisor/release-notes.md @@ -0,0 +1,2226 @@ +- [v28.0](#v280) + - [Community Engagement (Reminder)](#community-engagement-reminder) + - [Long Term Support (LTS) Release](#long-term-support-lts-release) + - [Virtualised TPM Support](#virtualised-tpm-support) + - [Transparent Huge Page Support](#transparent-huge-page-support) + - [README Quick Start Improved](#readme-quick-start-improved) + - [Notable Bug Fixes](#notable-bug-fixes) + - [Removals](#removals) + - [Contributors](#contributors) +- [v27.0](#v270) + - [Community Engagement](#community-engagement) + - [Prebuilt Packages](#prebuilt-packages) + - [Network Device MTU Exposed to Guest](#network-device-mtu-exposed-to-guest) + - [Boot Tracing](#boot-tracing) + - [Simplified Build Feature Flags](#simplified-build-feature-flags) + - [Asynchronous Kernel Loading](#asynchronous-kernel-loading) + - [GDB Support for AArch64](#gdb-support-for-aarch64) + - [Notable Bug Fixes](#notable-bug-fixes-1) + - [Deprecations](#deprecations) + - [Contributors](#contributors-1) +- [v26.0](#v260) + - [SMBIOS Improvements via `--platform`](#smbios-improvements-via---platform) + - [Unified Binary MSHV and KVM Support](#unified-binary-mshv-and-kvm-support) + - [Notable Bug Fixes](#notable-bug-fixes-2) + - [Deprecations](#deprecations-1) + - [Removals](#removals-1) + - [Contributors](#contributors-2) +- [v25.0](#v250) + - [`ch-remote` Improvements](#ch-remote-improvements) + - [VM "Coredump" Support](#vm-coredump-support) + - [Notable Bug Fixes](#notable-bug-fixes-3) + - [Removals](#removals-2) + - [Contributors](#contributors-3) +- [v24.0](#v240) + - [Bypass Mode for `virtio-iommu`](#bypass-mode-for-virtio-iommu) + - [Ensure Identifiers Uniqueness](#ensure-identifiers-uniqueness) + - [Sparse Mmap support](#sparse-mmap-support) + - [Expose Platform Serial Number](#expose-platform-serial-number) + - [Notable Bug Fixes](#notable-bug-fixes-4) + - [Notable Improvements](#notable-improvements) + - [Deprecations](#deprecations-2) + - [New on the Website](#new-on-the-website) + - [Contributors](#contributors-4) +- [v23.1](#v231) +- [v23.0](#v230) + - [vDPA Support](#vdpa-support) + - [Updated OS Support list](#updated-os-support-list) + - [`AArch64` Memory Map Improvements](#aarch64-memory-map-improvements) + - [`AMX` Support](#amx-support) + - [Notable Bug Fixes](#notable-bug-fixes-5) + - [Deprecations](#deprecations-3) + - [Contributors](#contributors-5) +- [v22.1](#v221) +- [v22.0](#v220) + - [GDB Debug Stub Support](#gdb-debug-stub-support) + - [`virtio-iommu` Backed Segments](#virtio-iommu-backed-segments) + - [Before Boot Configuration Changes](#before-boot-configuration-changes) + - [`virtio-balloon` Free Page Reporting](#virtio-balloon-free-page-reporting) + - [Support for Direct Kernel Booting with TDX](#support-for-direct-kernel-booting-with-tdx) + - [PMU Support for AArch64](#pmu-support-for-aarch64) + - [Documentation Under CC-BY-4.0 License](#documentation-under-cc-by-40-license) + - [Deprecation of "Classic" `virtiofsd`](#deprecation-of-classic-virtiofsd) + - [Notable Bug Fixes](#notable-bug-fixes-6) + - [Contributors](#contributors-6) +- [v21.0](#v210) + - [Efficient Local Live Migration (for Live Upgrade)](#efficient-local-live-migration-for-live-upgrade) + - [Recommended Kernel is Now 5.15](#recommended-kernel-is-now-515) + - [Notable Bug fixes](#notable-bug-fixes-7) + - [Contributors](#contributors-7) +- [v20.2](#v202) +- [v20.1](#v201) +- [v20.0](#v200) + - [Multiple PCI segments support](#multiple-pci-segments-support) + - [CPU pinning](#cpu-pinning) + - [Improved VFIO support](#improved-vfio-support) + - [Safer code](#safer-code) + - [Extended documentation](#extended-documentation) + - [Notable bug fixes](#notable-bug-fixes-8) + - [Contributors](#contributors-8) +- [v19.0](#v190) + - [Improved PTY handling for serial and `virtio-console`](#improved-pty-handling-for-serial-and-virtio-console) + - [PCI boot time optimisations](#pci-boot-time-optimisations) + - [Improved TDX support](#improved-tdx-support) + - [Live migration enhancements](#live-migration-enhancements) + - [`virtio-mem` support with `vfio-user`](#virtio-mem-support-with-vfio-user) + - [AArch64 for `virtio-iommu`](#aarch64-for-virtio-iommu) + - [Notable bug fixes](#notable-bug-fixes-9) + - [Contributors](#contributors-9) +- [v18.0](#v180) + - [Experimental User Device (`vfio-user`) support](#experimental-user-device-vfio-user-support) + - [Migration support for `vhost-user` devices](#migration-support-for-vhost-user-devices) + - [VHDX disk image support](#vhdx-disk-image-support) + - [Device pass through on MSHV hypervisor](#device-pass-through-on-mshv-hypervisor) + - [AArch64 for support `virtio-mem`](#aarch64-for-support-virtio-mem) + - [Live migration on MSHV hypervisor](#live-migration-on-mshv-hypervisor) + - [AArch64 CPU topology support](#aarch64-cpu-topology-support) + - [Power button support on AArch64](#power-button-support-on-aarch64) + - [Notable bug fixes](#notable-bug-fixes-10) + - [Contributors](#contributors-10) +- [v17.0](#v170) + - [ARM64 NUMA support using ACPI](#arm64-numa-support-using-acpi) + - [`Seccomp` support for MSHV backend](#seccomp-support-for-mshv-backend) + - [Hotplug of `macvtap` devices](#hotplug-of-macvtap-devices) + - [Improved SGX support](#improved-sgx-support) + - [Inflight tracking for `vhost-user` devices](#inflight-tracking-for-vhost-user-devices) + - [Notable bug fixes](#notable-bug-fixes-11) + - [Contributors](#contributors-11) +- [v16.0](#v160) + - [Improved live migration support](#improved-live-migration-support) + - [Improved `vhost-user` support](#improved-vhost-user-support) + - [ARM64 ACPI and UEFI support](#arm64-acpi-and-uefi-support) + - [Notable bug fixes](#notable-bug-fixes-12) + - [Removed functionality](#removed-functionality) + - [Contributors](#contributors-12) +- [v15.0](#v150) + - [Version numbering and stability guarantees](#version-numbering-and-stability-guarantees) + - [Network device rate limiting](#network-device-rate-limiting) + - [Support for runtime control of `virtio-net` guest offload](#support-for-runtime-control-of-virtio-net-guest-offload) + - [`--api-socket` supports file descriptor parameter](#--api-socket-supports-file-descriptor-parameter) + - [Bug fixes](#bug-fixes) + - [Deprecations](#deprecations-4) + - [Contributors](#contributors-13) +- [v0.14.1](#v0141) +- [v0.14.0](#v0140) + - [Structured event monitoring](#structured-event-monitoring) + - [MSHV improvements](#mshv-improvements) + - [Improved aarch64 platform](#improved-aarch64-platform) + - [Updated hotplug documentation](#updated-hotplug-documentation) + - [PTY control for serial and `virtio-console`](#pty-control-for-serial-and-virtio-console) + - [Block device rate limiting](#block-device-rate-limiting) + - [Deprecations](#deprecations-5) + - [Contributors](#contributors-14) +- [v0.13.0](#v0130) + - [Wider VFIO device support](#wider-vfio-device-support) + - [Improved huge page support](#improved-huge-page-support) + - [MACvTAP support](#macvtap-support) + - [VHD disk image support](#vhd-disk-image-support) + - [Improved Virtio device threading](#improved-virtio-device-threading) + - [Clean shutdown support via synthetic power button](#clean-shutdown-support-via-synthetic-power-button) + - [Contributors](#contributors-15) +- [v0.12.0](#v0120) + - [ARM64 enhancements](#arm64-enhancements) + - [Removal of `vhost-user-net` and `vhost-user-block` self spawning](#removal-of-vhost-user-net-and-vhost-user-block-self-spawning) + - [Migration of `vhost-user-fs` backend](#migration-of-vhost-user-fs-backend) + - [Enhanced "info" API](#enhanced-info-api) + - [Contributors](#contributors-16) +- [v0.11.0](#v0110) + - [`io_uring` support by default for `virtio-block`](#io_uring-support-by-default-for-virtio-block) + - [Windows Guest Support](#windows-guest-support) + - [`vhost-user` "Self Spawning" Deprecation](#vhost-user-self-spawning-deprecation) + - [`virtio-mmio` Removal](#virtio-mmio-removal) + - [Snapshot/Restore support for ARM64](#snapshotrestore-support-for-arm64) + - [Improved Linux Boot Time](#improved-linux-boot-time) + - [`SIGTERM/SIGINT` Interrupt Signal Handling](#sigtermsigint-interrupt-signal-handling) + - [Default Log Level Changed](#default-log-level-changed) + - [New `--balloon` Parameter Added](#new---balloon-parameter-added) + - [Experimental `virtio-watchdog` Support](#experimental-virtio-watchdog-support) + - [Notable Bug Fixes](#notable-bug-fixes-13) + - [Contributors](#contributors-17) +- [v0.10.0](#v0100) + - [`virtio-block` Support for Multiple Descriptors](#virtio-block-support-for-multiple-descriptors) + - [Memory Zones](#memory-zones) + - [`Seccomp` Sandbox Improvements](#seccomp-sandbox-improvements) + - [Preliminary KVM HyperV Emulation Control](#preliminary-kvm-hyperv-emulation-control) + - [Notable Bug Fixes](#notable-bug-fixes-14) + - [Contributors](#contributors-18) +- [v0.9.0](#v090) + - [`io_uring` Based Block Device Support](#io_uring-based-block-device-support) + - [Block and Network Device Statistics](#block-and-network-device-statistics) + - [HTTP API Responses](#http-api-responses) + - [CPU Topology](#cpu-topology) + - [Release Build Optimization](#release-build-optimization) + - [Hypervisor Abstraction](#hypervisor-abstraction) + - [Snapshot/Restore Improvements](#snapshotrestore-improvements) + - [Virtio Memory Ballooning Support](#virtio-memory-ballooning-support) + - [Enhancements to ARM64 Support](#enhancements-to-arm64-support) + - [Intel SGX Support](#intel-sgx-support) + - [`Seccomp` Sandbox Improvements](#seccomp-sandbox-improvements-1) + - [Notable Bug Fixes](#notable-bug-fixes-15) + - [Contributors](#contributors-19) +- [v0.8.0](#v080) + - [Experimental Snapshot and Restore Support](#experimental-snapshot-and-restore-support) + - [Experimental ARM64 Support](#experimental-arm64-support) + - [Support for Using 5-level Paging in Guests](#support-for-using-5-level-paging-in-guests) + - [Virtio Device Interrupt Suppression for Network Devices](#virtio-device-interrupt-suppression-for-network-devices) + - [`vhost_user_fs` Improvements](#vhost_user_fs-improvements) + - [Notable Bug Fixes](#notable-bug-fixes-16) + - [Command Line and API Changes](#command-line-and-api-changes) + - [Contributors](#contributors-20) +- [v0.7.0](#v070) + - [Block, Network, Persistent Memory (PMEM), VirtioFS and Vsock hotplug](#block-network-persistent-memory-pmem-virtiofs-and-vsock-hotplug) + - [Alternative `libc` Support](#alternative-libc-support) + - [Multithreaded Multi Queued `vhost-user` Backends](#multithreaded-multi-queued-vhost-user-backends) + - [Initial RamFS Support](#initial-ramfs-support) + - [Alternative Memory Hotplug: `virtio-mem`](#alternative-memory-hotplug-virtio-mem) + - [`Seccomp` Sandboxing](#seccomp-sandboxing) + - [Updated Distribution Support](#updated-distribution-support) + - [Command Line and API Changes](#command-line-and-api-changes-1) + - [Contributors](#contributors-21) +- [v0.6.0](#v060) + - [Directly Assigned Devices Hotplug](#directly-assigned-devices-hotplug) + - [Shared Filesystem Improvements](#shared-filesystem-improvements) + - [Block and Networking IO Self Offloading](#block-and-networking-io-self-offloading) + - [Command Line Interface](#command-line-interface) + - [PVH Boot](#pvh-boot) + - [Contributors](#contributors-22) +- [v0.5.1](#v051) +- [v0.5.0](#v050) + - [Virtual Machine Dynamic Resizing](#virtual-machine-dynamic-resizing) + - [Multi-Queue, Multi-Threaded Paravirtualization](#multi-queue-multi-threaded-paravirtualization) + - [New Interrupt Management Framework](#new-interrupt-management-framework) + - [Development Tools](#development-tools) + - [Kata Containers Integration](#kata-containers-integration) + - [Contributors](#contributors-23) +- [v0.4.0](#v040) + - [Dynamic virtual CPUs addition](#dynamic-virtual-cpus-addition) + - [Programmatic firmware tables generation](#programmatic-firmware-tables-generation) + - [Filesystem and block devices vhost-user backends](#filesystem-and-block-devices-vhost-user-backends) + - [Guest pause and resume](#guest-pause-and-resume) + - [Userspace IOAPIC by default](#userspace-ioapic-by-default) + - [PCI BAR reprogramming](#pci-bar-reprogramming) + - [New `cloud-hypervisor` organization](#new-cloud-hypervisor-organization) + - [Contributors](#contributors-24) +- [v0.3.0](#v030) + - [Block device offloading](#block-device-offloading) + - [Network device backend](#network-device-backend) + - [Virtual sockets](#virtual-sockets) + - [HTTP based API](#http-based-api) + - [Memory mapped virtio transport](#memory-mapped-virtio-transport) + - [Paravirtualized IOMMU](#paravirtualized-iommu) + - [Ubuntu 19.10](#ubuntu-1910) + - [Large memory guests](#large-memory-guests) +- [v0.2.0](#v020) + - [Network device offloading](#network-device-offloading) + - [Minimal hardware-reduced ACPI](#minimal-hardware-reduced-acpi) + - [Debug I/O port](#debug-io-port) + - [Improved direct device assignment](#improved-direct-device-assignment) + - [Improved shared filesystem](#improved-shared-filesystem) + - [Ubuntu bionic based CI](#ubuntu-bionic-based-ci) +- [v0.1.0](#v010) + - [Shared filesystem](#shared-filesystem) + - [Initial direct device assignment support](#initial-direct-device-assignment-support) + - [Userspace IOAPIC](#userspace-ioapic) + - [Virtual persistent memory](#virtual-persistent-memory) + - [Linux kernel bzImage](#linux-kernel-bzimage) + - [Console over virtio](#console-over-virtio) + - [Unit testing](#unit-testing) + - [Integration tests parallelization](#integration-tests-parallelization) + +# v28.0 + +This release has been tracked in our new [roadmap +project](https://github.com/orgs/cloud-hypervisor/projects/6) as iteration +v28.0. + +### Community Engagement (Reminder) + +Just a reminder that we have a new mailing list to support broader community +discussions. Please consider +[subscribing](https://lists.cloudhypervisor.org/g/dev/). We plan to use +this to announce a regular meeting for those interested in talking about Cloud +Hypervisor development. + +### Long Term Support (LTS) Release + +This is the first version of Cloud Hypervisor to be released under the LTS +release process. Point releases for bug fixes will be made for the next 18 +months; live migration and live upgrade will be supported between the point +releases of the LTS. + +### Virtualised TPM Support + +Support for adding an emulated CRB TPM has been added. This has it's own [TPM +documentation](docs/tpm.md). + +### Transparent Huge Page Support + +By default, but controllable through `--memory thp=off` if it possible to back +the guest memory with Transparent Huge Pages (no file backing/`shared=off`) +then this will be used resulting in improved boot performance. + +### README Quick Start Improved + +The README has been refreshed with the quick start guide updated to reflect the +different firmware options and to recommend the use of pre-built binaries. + +### Notable Bug Fixes + +* Inappropriate Copy-on-Write of pinned pages (e.g. VFIO) leading to higher + memory consumption (#4835) +* Multiple `virtio` device bug fixes found through fuzzing (#4859, #4799) +* Large QCOW files (> 4TiB) are now supported (#4767) +* Support for > 31 vCPUS on aarch64 (#4863) +* Multiple fixes to OpenAPI specification file (#4720, #4811) +* Programming of the MSI-X table has been optimised leading to faster boot on + newer Linux kernels (#4744) +* Error on reboot from race to close TAP devices (#4871) +* Non-spec compliant virtio-block read-only support (#4888) + +### Removals + +The following functionality has been removed: + +* Support for non-PVH firmware booting has been removed (#4511) +* I/O ports used for older versions of firmware have been removed (#3926) +* Deprecated API options for kernel/cmdline/initramfs have been removed (#4737) + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Anatol Belski +* Bo Chen +* Fabiano Fidêncio +* Jianyong Wu +* Jinank Jain +* Jinrong Liang +* lv.mengzhao +* Michael Zhao +* Muminul Islam +* Praveen K Paladugu +* Rob Bradford +* Sebastien Boeuf +* Wei Liu + +# v27.0 + +This release has been tracked in our new [roadmap +project](https://github.com/orgs/cloud-hypervisor/projects/6) as iteration +v27.0. + +### Community Engagement + +A new mailing list has been created to support broader community discussions. +Please consider [subscribing](https://lists.cloudhypervisor.org/g/dev/); an +announcement of a regular meeting will be announced via this list shortly. + +### Prebuilt Packages + +Prebuilt packages are now available. Please see [this +document](https://github.com/cloud-hypervisor/obs-packaging/blob/main/README.md) +on how to install. These packages also include packages for the different +firmware options available. + +### Network Device MTU Exposed to Guest + +The MTU for the TAP device associated with a `virtio-net` device is now exposed +to the guest. If the user provides a MTU with `--net mtu=..` then that MTU is +applied to created TAP interfaces. This functionality is also exposed for +`vhost-user-net` devices including those created with the reference backend +(#4658, #4676.) + +### Boot Tracing + +Support for generating a trace report for the boot time has been added +including a script for generating an SVG from that trace (#4659.) + +### Simplified Build Feature Flags + +The set of feature flags, for e.g. experimental features, have been simplified: + +* `msvh` and `kvm` features provide support for those specific hypervisors + (with `kvm` enabled by default), +* `tdx` provides support for Intel TDX; and although there is no MSHV support + now it is now possible to compile with the `mshv` feature (#4696,) +* `tracing` adds support for boot tracing, +* `guest_debug` now covers both support for gdbing a guest (formerly `gdb` + feature) and dumping guest memory. + +The following feature flags were removed as the functionality was enabled by +default: `amx`, `fwdebug`, `cmos` and `common` (#4679, #4632.) + +### Asynchronous Kernel Loading + +AArch64 has gained support for loading the guest kernel asynchronously like +x86-64. (#4538) + +### GDB Support for AArch64 + +GDB stub support (accessed through `--gdb` under `guest_debug` feature) is now +available on AArch64 as well as as x86-64. + +### Notable Bug Fixes + +* This version incorporates a version of `virtio-queue` that addresses an issue + where a rogue guest can potentially DoS the VMM (rust-vmm/vm-virtio#196.) +* Improvements around PTY handling for `virtio-console` and serial devices + (#4520, #4533, #4535.) +* Improved error handling in virtio devices (#4626, #4605, #4509, #4631, #4697) + +### Deprecations + +Deprecated features will be removed in a subsequent release and users should +plan to use alternatives. + +* Booting legacy firmware (compiled without a PVH header) has been deprecated. + All the firmware options (Cloud Hypervisor OVMF and Rust Hypervisor Firmware) + support booting with PVH so support for loading firmware in a legacy mode is no + longer needed. This functionality will be removed in the next release. + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Anatol Belski +* Bo Chen +* James O. D. Hunt +* Jianyong Wu +* Markus Napierkowski +* Michael Zhao +* Nuno Das Neves +* Rob Bradford +* Sebastien Boeuf +* Smit Gardhariya +* Wei Liu + +# v26.0 + +This release has been tracked through the [v26.0 +project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/30). + +### SMBIOS Improvements via `--platform` + +`--platform` and the appropriate API structure has gained support for supplying +OEM strings (primarily used to communicate metadata to systemd in the guest) +(#4319, #4446) and support for specifying the UUID (#4389.) + +### Unified Binary MSHV and KVM Support + +Support for both the MSHV and KVM hypervisors can be compiled into the same +binary with the detection of the hypervisor to use made at runtime. + +### Notable Bug Fixes + +* The prefetchable flag is preserved on BARs for VFIO devices (#4353, #4454) +* PCI Express capabilities for functionality we do not support are now filtered + out (#4456) +* GDB breakpoint support is more reliable (#4354, #4363) +* `SIGINT` and `SIGTERM` signals are now handled before the VM has booted + (#4269, #4293) +* Multiple API event loop handling bug fixes (#4309, #4362) +* Incorrect assumptions in virtio queue numbering were addressed, allowing + the`virtio-fs` driver in OVMF to be used (#4341, #4314) +* VHDX file format header fix (#4291) +* The same VFIO device cannot be added twice (#4453, #4463) +* SMBIOS tables were being incorrectly generated (#4442) + +### Deprecations + +Deprecated features will be removed in a subsequent release and users should +plan to use alternatives. + +* The top-level `kernel` and `initramfs` members on the `VmConfig` have been + moved inside a `PayloadConfig` as the `payload` member. The OpenAPI document + has been updated to reflect the change and the old API members continue to + function and are mapped to the new version. The expectation is that these old + versions will be removed in the v28.0 release. + +### Removals + +The following functionality has been removed: + +* The unused `poll_queue` parameter has been removed from `--disk` and + equivalent. This was residual from the the removal of the `vhost-user-block` + spawning feature (#4402.) + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Alyssa Ross +* Anatol Belski +* Archana Shinde +* Bo Chen +* lizhaoxin1 +* Maximilian Nitsch +* Michael Zhao +* Rob Bradford +* Sebastien Boeuf +* Steven Dake +* Wei Liu + +# v25.0 + +This release has been tracked through the [v25.0 +project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/29). + +### `ch-remote` Improvements + +The `ch-remote` command has gained support for creating the VM from a JSON +config and support for booting and deleting the VM from the VMM. + +### VM "Coredump" Support + +Under the `guest_debug` feature flag it is now possible to extract the memory +of the guest for use in debugging with e.g. the `crash` utility. (#4012) + +### Notable Bug Fixes + +* Always restore console mode on exit (#4249, #4248) +* Restore vCPUs in numerical order which fixes aarch64 snapshot/restore (#4244) +* Don't try and configure `IFF_RUNNING` on TAP devices (#4279) +* Propagate configured queue size through to vhost-user backend (#4286) +* Always Program vCPU CPUID before running the vCPU to fix running on Linux + 5.16 (#4156) +* Enable ACPI MADT "Online Capable" flag for hotpluggable vCPUs to fix newer + Linux guest + +### Removals + +The following functionality has been removed: + +* The `mergeable` option from the `virtio-pmem` support has been removed + (#3968) +* The `dax` option from the `virtio-fs` support has been removed (#3889) + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Dylan Bargatze +* Jinank Jain +* Michael Zhao +* Rob Bradford +* Sebastien Boeuf +* Wei Liu +* Yi Wang + +# v24.0 + +This release has been tracked through the [v24.0 +project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/28). + +### Bypass Mode for `virtio-iommu` + +`virtio-iommu` specification describes how a device can be attached by default +to a bypass domain. This feature is particularly helpful for booting a VM with +guest software which doesn't support `virtio-iommu` but still need to access +the device. Now that Cloud Hypervisor supports this feature, it can boot a VM +with Rust Hypervisor Firmware or OVMF even if the `virtio-block` device exposing +the disk image is placed behind a virtual IOMMU. + +### Ensure Identifiers Uniqueness + +Multiple checks have been added to the code to prevent devices with identical +identifiers from being created, and therefore avoid unexpected behaviors at boot +or whenever a device was hot plugged into the VM. + +### Sparse Mmap support + +Sparse mmap support has been added to both VFIO and vfio-user devices. This +allows the device regions that are not fully mappable to be partially mapped. +And the more a device region can be mapped into the guest address space, the +fewer VM exits will be generated when this device is accessed. This directly +impacts the performance related to this device. + +### Expose Platform Serial Number + +A new `serial_number` option has been added to `--platform`, allowing a user to +set a specific serial number for the platform. This number is exposed to the +guest through the SMBIOS. + +### Notable Bug Fixes + +* Fix loading RAW firmware (#4072) +* Reject compressed QCOW images (#4055) +* Reject virtio-mem resize if device is not activated (#4003) +* Fix potential mmap leaks from VFIO/vfio-user MMIO regions (#4069) +* Fix algorithm finding HOB memory resources (#3983) + +### Notable Improvements + +* Refactor interrupt handling (#4083) +* Load kernel asynchronously (#4022) +* Only create ACPI memory manager DSDT when resizable (#4013) + +### Deprecations + +Deprecated features will be removed in a subsequent release and users should +plan to use alternatives + +* The `mergeable` option from the `virtio-pmem` support has been deprecated + (#3968) +* The `dax` option from the `virtio-fs` support has been deprecated (#3889) + +### New on the Website + +A new blog post [Achieving Bare Metal Performance Within a Virtual +Machine](https://www.cloudhypervisor.org/blog/achieving-bare-metal-performance-within-a-virtual-machine) +has been added to the Cloud Hypervisor website. + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Anatol Belski +* Bo Chen +* Fabiano Fidêncio +* LiHui +* Maksym Pavlenko +* Rob Bradford +* Sebastien Boeuf +* Steven Dake +* Vincent Batts +* Wei Liu + +# v23.1 + +This is a bug fix release. The following issues have been addressed: + +* Add some missing seccomp rules +* Remove `virtio-fs` filesystem entries from config on removal +* Do not delete API socket on API server start (#4026) +* Reject `virtio-mem` resize if the guest doesn't activate the device +* Fix OpenAPI naming of I/O throttling knobs + +# v23.0 + +This release has been tracked through the [v23.0 +project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/27). + +### vDPA Support + +A vDPA device has a datapath that complies with the virtio specification but +with a vendor specific control path. The addition of `--vdpa` and the REST API +equivalent allows the use of these devices with Cloud Hypervisor. + +### Updated OS Support list + +The list of officially supported and tested OS versions has been updated to +include Ubuntu "jammy" 22.04 and EOLed versions removed. + +### `AArch64` Memory Map Improvements + +The memory map when running on `AArch64` has been improved for the handling of +the UEFI region which means that the booted guest OS now has full access to its +allocated RAM. (#3938) + +### `AMX` Support + +Under a compile time gate of `amx` it is possible compile in support for the +`AMX` instruction set extension for guest use. This also requires runtime +enabling with `--cpu features=amx`. + +### Notable Bug Fixes + +* Generate error when incorrect HTTP method used for some API endpoints (#3887) +* CMOS based reset is now available to support rebooting on "jammy" (#3914) +* ACPI tables are not produced for memory hotplug when running with + `virtio-mem` (#3883) +* `virtio-iommu` backed PCI segments are now comprehensively placed behind the + vIOMMU (#3870) +* Seccomp rules have been extended for `virtio-fs` to support direct access + (#3848) + +### Deprecations + +Deprecated features will be removed in a subsequent release and users should +plan to use alternatives + +* The `mergeable` option from the `virtio-pmem` support has been deprecated + (#3968) +* The `dax` option from the `virtio-fs` support has been deprecated (#3889) + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Bo Chen +* Fabiano Fidêncio +* Henry Wang +* Jianyong Wu +* LiHui +* Michael Zhao +* Rob Bradford +* Sebastien Boeuf +* Wei Liu +* William Douglas +* Yi Wang + +# v22.1 + +This is a bug fix release. The following issues have been addressed: + +* VFIO ioctl reordering to fix MSI on AMD platforms (#3827) +* Fix `virtio-net` control queue (#3829) + +# v22.0 + +This release has been tracked through the [v22.0 +project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/25). + +### GDB Debug Stub Support + +Cloud Hypervisor can now be used as debug target with GDB. This is controlled +by the `gdb` compile time feature and details of how to use it can be found in +the [gdb +documentation](https://github.com/cloud-hypervisor/cloud-hypervisor/blob/main/docs/gdb.md). + +### `virtio-iommu` Backed Segments + +In order to facilitate hotplug devices that require being behind an IOMMU (e.g. +QAT) there is a new option `--platform iommu_segments=` that +will place all the specified segments behind the IOMMU. + +### Before Boot Configuration Changes + +It is now possible to change the VM configuration (e.g. add or remove devices, +resize) before the VM is booted. + +### `virtio-balloon` Free Page Reporting + +If `--balloon free_page_reporting=on` is used then the guest can report pages +that is it not using to the VMM. The VMM will then notify the host OS that +those pages are no longer in use and can be freed. This can result in improved +memory density. + +### Support for Direct Kernel Booting with TDX + +Through the use of `TD-Shim` lightweight firmware it is now possible to +directly boot into the kernel with TDX. The [TDX +documentation](https://github.com/cloud-hypervisor/cloud-hypervisor/blob/main/docs/intel_tdx.md#tdshim) +has been updated for this usage. + +### PMU Support for AArch64 + +A PMU is now available on AArch64 for guest performance profiling. This will be +exposed automatically if available from the host. + +### Documentation Under CC-BY-4.0 License + +The documentation is now licensed under the "Creative Commons Attribution 4.0 +International" license which is aligned with the project charter under the +Linux Foundation. + +### Deprecation of "Classic" `virtiofsd` + +The use of the Rust based [virtiofsd](https://gitlab.com/virtio-fs/virtiofsd) +is now recommended and we are no longer testing against the C based "classic" +version. + +### Notable Bug Fixes + +* Can now be used on kernels without `AF_INET` support (#3785) +* `virtio-balloon` size is now validated against guest RAM size (#3689) +* Ensure that I/O related KVM VM Exits are correctly handled (#3677) +* Multiple TAP file descriptors can be used for `virtio-net` device hotplug (#3607) +* Minor API improvements and fixes (#3756, #3766, #3647, #3578) +* Fix sporadic seccomp violation from glibc memory freeing (#3610, #3609) +* Fix Windows 11 on AArch64 due to wider MSI-X register accesses (#3714, #3720) +* Ensure `vhost-user` features are correct across migration (#3737) +* Improved vCPU topology on AArch64 (#3735, #3733) + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Akira Moroo +* Barret Rhoden +* Bo Chen +* Fabiano Fidêncio +* Feng Ye +* Henry Wang +* Jianyong Wu +* lizhaoxin1 +* Michael Zhao +* Rob Bradford +* Sebastien Boeuf +* Wei Liu + +# v21.0 + +This release has been tracked through the [v21.0 +project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/24). + +### Efficient Local Live Migration (for Live Upgrade) + +In order to support fast live upgrade of the VMM an optimised path has been +added in which the memory for the VM is not compared from source to +destination. This is activated by passing `--local` to the `ch-remote +send-migration` command. This means that the live upgrade can complete in the +order of 50ms vs 3s. (#3566) + +### Recommended Kernel is Now 5.15 + +Due to an issue in the `virtio-net` code in 5.14 the recommended Linux kernel +is now 5.15. (#3530) + +### Notable Bug fixes + +* Multiple fixes were made to the OpenAPI YAML file to match the implementation (#3555,#3562) +* Avoid live migration deadlock when triggered during the kernel boot (#3585) +* Support live migration within firmware (#3586) +* Validate the `virtio-net` descriptor chain (#3548) +* `direct=on` (`O_DIRECT`) can now be used with a guest that makes unaligned accesses (e.g. firmware) (#3587) + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Anatol Belski +* Barret Rhoden +* Bo Chen +* Fabiano Fidêncio +* Henry Wang +* Liang Zhou +* Michael Zhao +* Muhammad Falak R Wani +* Muminul Islam +* Rob Bradford +* Sebastien Boeuf +* Wei Liu +* Ziye Yang + +# v20.2 + +This is a bug fix release. The following issues have been addressed: + +* Don't error out when setting up the SIGWINCH handler (for console resize) + when this fails due to older kernel (#3456) +* Seccomp rules were refined to remove syscalls that are now unused +* Fix reboot on older host kernels when SIGWINCH handler was not initialised + (#3496) +* Fix virtio-vsock blocking issue (#3497) + +# v20.1 + +This is a bug fix release. The following issues have been addressed: + +* Networking performance regression with `virtio-net` (#3450) +* Limit file descriptors sent in `vfio-user` support (#3401) +* Fully advertise PCI MMIO config regions in ACPI tables (#3432) +* Set the TSS and KVM identity maps so they don't overlap with firmware RAM +* Correctly update the `DeviceTree` on restore + +# v20.0 + +This release has been tracked through the [v20.0 +project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/23). + +### Multiple PCI segments support + +Cloud Hypervisor is no longer limited to 31 PCI devices. For both `x86_64` and +`aarch64` architectures, it is now possible to create up to 16 PCI segments, +increasing the total amount of supported PCI devices to 496. + +### CPU pinning + +For each vCPU, the user can define a limited set of host CPUs on which it is +allowed to run. This can be useful when assigning a 1:1 mapping between host and +guest resources, or when running a VM on a specific NUMA node. + +### Improved VFIO support + +Based on VFIO region capabilities, all regions can be memory mapped, limiting +the amount of triggered VM exits, and therefore increasing the performance of +the passthrough device. + +### Safer code + +Several sections containing unsafe Rust code have been replaced with safe +alternatives, and multiple comments have been added to clarify why the remaining +unsafe sections are safe to use. + +### Extended documentation + +The documentation related to VFIO has been updated while some new documents have +been introduced to cover the usage of `--cpus` parameter as well as how to run +Cloud Hypervisor on Intel TDX. + +### Notable bug fixes + +* Naturally align PCI BARs on relocation (#3244) +* Fix panic in SIGWINCH listener thread when no seccomp filter set (#3338) +* Use the tty raw mode implementation from libc (#3344) +* Fix the emulation of register D for CMOS/RTC device (#3393) + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Alyssa Ross +* Bo Chen +* Fabiano Fidêncio +* Michael Zhao +* Muminul Islam +* Rob Bradford +* Sebastien Boeuf +* Wei Liu +* Willen Yang +* William Douglas +* Ziye Yang + +# v19.0 + +This release has been tracked through the [v19.0 +project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/22). + +### Improved PTY handling for serial and `virtio-console` + +The PTY support for serial has been enhanced with improved buffering when the +the PTY is not yet connected to. Using `virtio-console` with PTY now results in +the console being resized if the PTY window is also resized. + +### PCI boot time optimisations + +Multiple optimisations have been made to the PCI handling resulting in +significant improvements in the boot time of the guest. + +### Improved TDX support + +When using the latest TDVF firmware the ACPI tables created by the VMM are now +exposed via the firmware to the guest. + +### Live migration enhancements + +Live migration support has been enhanced to support migration with `virtio-mem` +based memory hotplug and the `virtio-balloon` device now supports live +migration. + +### `virtio-mem` support with `vfio-user` + +The use of `vfio-user` userspaces devices can now be used in conjunction with +`virtio-mem` based memory hotplug and unplug. + +### AArch64 for `virtio-iommu` + +A paravirtualised IOMMU can now be used on the AArch64 platform. + +### Notable bug fixes + +* ACPI hotplugged memory is correctly restored after a live migration or + snapshot/restore (#3165) +* Multiple devices from the same IOMMU group can be passed through via VFIO + (#3078 #3113) +* Live migration with large blocks of memory was buggy due to an in issue in + the underlying crate (#3157) + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Alyssa Ross +* Bo Chen +* Henry Wang +* Hui Zhu +* Jianyong Wu +* Li Yu +* Michael Zhao +* Muminul Islam +* Rob Bradford +* Sebastien Boeuf +* Wei Liu +* William Douglas +* Yu Li + +# v18.0 + +This release has been tracked through the [v18.0 +project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/21). + +### Experimental User Device (`vfio-user`) support + +Experimental support for running PCI devices in userspace via `vfio-user` +has been included. This allows the use of the SPDK NVMe `vfio-user` controller +with Cloud Hypervisor. This is enabled by `--user-device` on the command line. + +### Migration support for `vhost-user` devices + +Devices exposed into the VM via `vhost-user` can now be migrated using the live +migration support. This requires support from the backend however the commonly +used DPDK `vhost-user` backend does support this. + +### VHDX disk image support + +Images using the VHDX disk image format can now be used with Cloud Hypervisor. + +### Device pass through on MSHV hypervisor + +When running on the MSHV hypervisor it is possible to pass through devices from +the host through to the guest (e.g with `--device`) + +### AArch64 for support `virtio-mem` + +The reference Linux kernel we recommend for using with Cloud Hypervisor now supports `virtio-mem` on AArch64. + +### Live migration on MSHV hypervisor + +Live migration is now supported when running on the MSHV hypervisor including +efficient tracking of dirty pages. + +### AArch64 CPU topology support + +The CPU topology (as configured through `--cpu topology=`) can now be +configured on AArch64 platforms and is conveyed through either ACPI or device +tree. + +### Power button support on AArch64 + +Use of the ACPI power button (e.g `ch-remote --api-socket= power-button`) +is now supported when running on AArch64. + +### Notable bug fixes + +* Using two PTY outputs e.g. `--serial pty --console pty` now works correctly (#3012) +* TTY input is now always sent to the correct destination (#3005) +* The boot is no longer blocked when using a unattached PTY on the serial console (#3004) +* Live migration is now supported on AArch64 (#3049) +* Ensure signal handlers are run on the correct thread (#3069) + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Alyssa Ross +* Anatol Belski +* Arafatms +* Bo Chen +* Fazla Mehrab +* Henry Wang +* Jianyong Wu +* Jiaqi Gao +* Markus Theil +* Muminul Islam +* Rob Bradford +* Sebastien Boeuf +* Wei Liu +* Yu Li + +# v17.0 + +This release has been tracked through the [v17.0 +project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/20). + +### ARM64 NUMA support using ACPI + +The support for ACPI on ARM64 has been enhanced to include support for +specifying a NUMA configuration using the existing control options. + +### `Seccomp` support for MSHV backend + +The `seccomp` rules have now been extended to support running against the MSHV +hypervisor backend. + +### Hotplug of `macvtap` devices + +Hotplug of `macvtap` devices is now supported with the file descriptor for the +network device if opened by the user and passed to the VMM. The `ch-remote` +tool supports this functionality when adding a network device. + +### Improved SGX support + +The SGX support has been updated to match the latest Linux kernel support and +now supports SGX provisioning and associating EPC sections to NUMA nodes. + +### Inflight tracking for `vhost-user` devices + +Support for handling inflight tracking of I/O requests has been added to the +`vhost-user` devices allowing recovery after device reconnection. + +### Notable bug fixes + +* VFIO PCI BAR calculation code now correctly handles I/O BARs (#2821). +* The VMM side of `vhost-user` devices no longer advertise the + `VIRTIO_F_RING_PACKED` feature as they are not yet supported in the VMM +(#2833). +* On ARM64 VMs can be created with more than 16 vCPUs (#2763). + +### Contributors + +Many thanks to everyone who has contributed to our release: + +* Anatol Belski +* Arafatms +* Bo Chen +* Fei Li +* Henry Wang +* Jiachen Zhang +* Jianyong Wu +* Li Hangjing +* Michael Zhao +* Muminul Islam +* Rob Bradford +* Sebastien Boeuf +* Wei Liu +* Yukiteru + +# v16.0 + +This release has been tracked through the [v16.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/19). + +### Improved live migration support + +The live migration support inside Cloud Hypervisor has been improved with the addition of the tracking of dirty pages written by the VMM to complement the tracking of dirty pages made by the guest itself. Further the internal state of the VMM now is versioned which allows the safe migration of VMs from one version of the VMM to a newer one. However further testing is required so this should be done with care. See the [live migration documentation](docs/live_migration.md) for more details. + +### Improved `vhost-user` support + +When using `vhost-user` to access devices implemented in different processes there is now support for reconnection of those devices in the case of a restart of the backend. In addition it is now possible to operate with the direction of the `vhost-user-net` connection reversed with the server in the VMM and the client in the backend. This is aligns with the default approach recommended by Open vSwitch. + +### ARM64 ACPI and UEFI support + +Cloud Hypervisor now supports using ACPI and booting from a UEFI image on ARM64. This allows the use of stock OS images without direct kernel boot. + +### Notable bug fixes + +* Activating fewer `virtio-net` queues than advertised is now supported. This appeared when using OVMF with an MQ enabled device (#2578). +* When using MQ with `virtio` devices Cloud Hypervisor now enforces a minimum vCPU count which ensures that the user will not see adverse guest performance (#2563). +* The KVM clock is now correctly handled during live migration / snapshot & restore. + +### Removed functionality + +The following formerly deprecated features have been removed: + +* Support for booting with the "LinuxBoot" protocol for ELF and `bzImage` + binaries has been deprecated. When using direct boot users should configure + their kernel with `CONFIG_PVH=y`. + +### Contributors + +Many thanks to everyone who has contributed to our release including some new faces. + +* Anatol Belski +* Bo Chen +* Dayu Liu +* Henry Wang +* Jiachen Zhang +* Jianyong Wu +* Michael Zhao +* Mikko Ylinen +* Muminul Islam +* Ren Lei +* Rob Bradford +* Sebastien Boeuf +* Wei Liu +* Yi Wang + +# v15.0 + +This release has been tracked through the [v15.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/18). + +Highlights for `cloud-hypervisor` version v15.0 include: + +### Version numbering and stability guarantees + +This release is the first in a new version numbering scheme to represent that +we believe Cloud Hypervisor is maturing and entering a period of stability. +With this new release we are beginning our new stability guarantees: + +* The API (including command line options) will not be removed or changed in a + breaking way without a minimum of 2 releases notice. Where possible warnings + will be given about the use of deprecated functionality and the deprecations + will be documented in the release notes. +* Point releases will be made between individual releases where there are + substantial bug fixes or security issues that need to be fixed. + +Currently the following items are **not** guaranteed across updates: + +* Snapshot/restore is not supported across different versions +* Live migration is not supported across different versions +* The following features are considered experimental and may change + substantially between releases: TDX, SGX. + +### Network device rate limiting + +Building on our existing support for rate limiting block activity the network +device also now supports rate limiting. Full details of the controls are in the +[IO throttling documentation.](docs/io_throttling.md) + +### Support for runtime control of `virtio-net` guest offload + +The guest is now able to change the offload settings for the `virtio-net` +device. As well as providing a useful control this mitigates an issue in the +Linux kernel where the guest will attempt to reprogram the offload settings +even if they are not advertised as configurable (#2528). + +### `--api-socket` supports file descriptor parameter + +The `--api-socket` can now take an `fd=` parameter to specify an existing file +descriptor to use. This is particularly beneficial for frameworks that need to +programmatically control Cloud Hypervisor. + +### Bug fixes + +* A workaround has been put in place to mitigate a Linux kernel issues that + results in the CPU thread spinning at 100% when using `virtio-pmem` (#2277). +* PCI BARs are now correctly aligned removing the need for the guest to + reprogram them (#1797,#1798) +* Handle TAP interface not being writable within virtio-net (due to the buffer + exhaustion on the host) (#2517) +* The recommended Linux kernel is now v5.12.0 as it contains a fix that + prevents snapshot & restore working (#2535) + +### Deprecations + +Deprecated features will be removed in a subsequent release and users should plan to use alternatives + +* Support for booting with the "LinuxBoot" protocol for ELF and `bzImage` + binaries has been deprecated. When using direct boot users should configure + their kernel with `CONFIG_PVH=y`. Will be removed in v16.0. + +### Contributors + +Many thanks to everyone who has contributed to our release including some new faces. + +* Alyssa Ross +* Anatol Belski +* Bo Chen +* Gaelan Steele +* Jianyong Wu +* Michael Zhao +* Muminul Islam +* Rob Bradford +* Sebastien Boeuf +* Wei Liu +* William Douglas + +# v0.14.1 + +Bug fix release branched off the v0.14.0 release. The following bugs were fixed +in this release: + +* CPU hotplug on Windows failed due to misreported CPU state information and + the lack of HyperV CPUID bit enabled (#2437, #2449, #2436) +* A seccomp rule was missing that was triggered on CPU unplug (#2455) +* A bounds check in VIRTIO queue validation was erroneously generating + DescriptorChainTooShort errors in certain circumstances (#2450, #2424) + +# v0.14.0 + +This release has been tracked through the [0.14.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/17). + +Highlights for `cloud-hypervisor` version 0.14.0 include: + +### Structured event monitoring + +A new option was added to the VMM `--event-monitor` which reports structured +events (JSON) over a file or file descriptor at key events in the lifecycle of +the VM. The list of events is limited at the moment but will be further +extended over subsequent releases. The events exposed form part of the Cloud +Hypervisor API surface. + +### MSHV improvements + +Basic support has been added for running Windows guests atop the MSHV +hypervisor as an alternative to KVM and further improvements have been made to +the MSHV support. + +### Improved aarch64 platform + +The aarch64 platform has been enhanced with more devices exposed to the running +VM including an enhanced serial UART. + +### Updated hotplug documentation + +The documentation for the hotplug support has been updated to reflect the use +of the `ch-remote` tool and to include details of `virtio-mem` based hotplug as +well as documenting hotplug of paravirtualised and VFIO devices. + +### PTY control for serial and `virtio-console` + +The `--serial` and `--console` parameters can now direct the console to a PTY +allowing programmatic control of the console from another process through the +PTY subsystem. + +### Block device rate limiting + +The block device performance can now be constrained as part of the VM +configuration allowing rate limiting. Full details of the controls are in the +[IO throttling documentation.](docs/io_throttling.md) + + +### Deprecations + +Deprecated features will be removed in a subsequent release and users should plan to use alternatives + +* Support for booting with the "LinuxBoot" protocol for ELF and `bzImage` + binaries has been deprecated. When using direct boot users should configure + their kernel with `CONFIG_PVH=y`. + + +### Contributors + +Many thanks to everyone who has contributed to our 0.14.0 release including +some new faces. + +Bo Chen +Henry Wang +Iggy Jackson +Jiachen Zhang +Michael Zhao +Muminul Islam +Penny Zheng +Rob Bradford +Sebastien Boeuf +Vineeth Pillai +Wei Liu +William Douglas +Zide Chen + +# v0.13.0 + +This release has been tracked through the [0.13.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/16). + +Highlights for `cloud-hypervisor` version 0.13.0 include: + +### Wider VFIO device support + +It is now possible to use Cloud Hypervisor's VFIO support to passthrough PCI +devices that do not support MSI or MSI-X and instead rely on INTx interrupts. +Most notably this widens the support to most NVIDIA cards with the proprietary +drivers. + +### Improved huge page support + +Through the addition of `hugepage_size` on `--memory` it is now possible to +specify the desired size of the huge pages used when allocating the guest +memory. The user is required to ensure they have sufficient pages of the +desired size in their pool. + +### MACvTAP support + +It is now possible to provide file descriptors using the `fd` parameter to +`--net` which point at TAP devices that have already been opened by the user. +This aids integration with `libvirt` but also permits the use of MACvTAP +support. This is documented in dedicated [macvtap documentation](docs/macvtap-bridge.md). + +### VHD disk image support + +It is now possible to use VHD (fixed) disk images as well as QCOWv2 and raw +disk image with Cloud Hypervisor. + +### Improved Virtio device threading + +Device threads are now derived from the main VMM thread which allows more +restrictive seccomp filters to be applied to them. The threads also have a +predictable name derived from the device id. + +### Clean shutdown support via synthetic power button + +It is now possible to request that the guest VM shut itself down by triggering +a synthetic ACPI power button press from the VMM. If the guest is listening for +such an event (e.g. using systemd) then it will process the event and cleanly +shut down. This functionality is exposed through the HTTP API and can be +triggered via `ch-remote --api-socket= power-button`. + +### Contributors + +Many thanks to everyone who has contributed to our 0.13.0 release including +some new faces. + +* Bo Chen +* Mikko Ylinen +* Muminul Islam +* Rob Bradford +* Samuel Ortiz +* Sebastien Boeuf +* Vineeth Pillai +* Wei Liu +* William Douglas +* Xie Yongji + +# v0.12.0 + +This release has been tracked through the [0.12.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/15). + +Highlights for `cloud-hypervisor` version 0.12.0 include: + +### ARM64 enhancements + +The use of `--watchdog` is now fully supported as is the ability to reboot the +VM from within the guest when running Cloud Hypervisor on an ARM64 system. + +### Removal of `vhost-user-net` and `vhost-user-block` self spawning + +In order to use `vhost-user-net` or `vhost-user-block` backends the user is now +responsible for starting the backend and providing the socket for the VMM to +use. This functionality was deprecated in the last release and how now been +removed. + +### Migration of `vhost-user-fs` backend + +The `vhost-user-fs` backend is no longer included in Cloud Hypervisor and it is +instead hosted in [it's own +repository](https://gitlab.com/virtio-fs/virtiofsd-rs) + +### Enhanced "info" API + +The `vm.info` HTTP API endpoint has been extended to include the details of the +devices used by the VM including any VFIO devices used. + +### Contributors + +Many thanks to everyone who has contributed to our 0.12.0 release: + +* Anatol Belski +* Julio Montes +* Michael Zhao +* Muminul Islam +* Rob Bradford +* Samuel Ortiz +* Wei Liu + +# v0.11.0 + +This release has been tracked through the [0.11.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/14). + +Highlights for `cloud-hypervisor` version 0.11.0 include: + +### `io_uring` support by default for `virtio-block` + +Provided that the host OS supports it (Linux kernel 5.8+) then `io_uring` will +be used for a significantly higher performance block device. + +### Windows Guest Support + +This is the first release where we officially support Windows running as a +guest. Full details of how to setup the image and run Cloud Hypervisor with a +Windows guest can be found in the dedicated [Windows +documentation](docs/windows.md). + +### `vhost-user` "Self Spawning" Deprecation + +Automatically spawning a `vhost-user-net` or `vhost-user-block` backend is now +deprecated. Users of this functionality will receive a warning and should make +adjustments. The functionality will be removed in the next release. + +### `virtio-mmio` Removal + +Support for using the `virtio-mmio` transport, rather than using PCI, has been +removed. This has been to simplify the code and significantly +reduce the testing burden of the project. + +### Snapshot/Restore support for ARM64 + +When running on the ARM64 architecture snapshot and restore has now been +implemented. + +### Improved Linux Boot Time + +The time to boot the Linux kernel has been significantly improved by the +identifying some areas of delays around PCI bus probing, IOAPIC programming and +MPTABLE issues. Full details can be seen in #1728. + +### `SIGTERM/SIGINT` Interrupt Signal Handling + +When the VMM process receives the `SIGTERM` or `SIGINT` signals then it will +trigger the VMM process to cleanly deallocate resources before exiting. The +guest VM will not be cleanly shutdown but the VMM process will clean up its +resources. + +### Default Log Level Changed + +The default logging level was changed to include warnings which should make it +easier to see potential issues. New [logging +documentation](docs/logging) was also added. + +### New `--balloon` Parameter Added + +Control of the setup of `virtio-balloon` has been moved from `--memory` to its +own dedicated parameter. This makes it easier to add more balloon specific +controls without overloading `--memory`. + +### Experimental `virtio-watchdog` Support + +Support for using a new `virtio-watchdog` has been added which can be used to +have the VMM reboot the guest if the guest userspace fails to ping the +watchdog. This is enabled with `--watchdog` and requires kernel support. + +### Notable Bug Fixes + +* MTRR bit was missing from CPUID advertised to guest +* "Return" key could not be used under `CMD.EXE` under Windows SAC (#1170) +* CPU identification string is now exposed to the guest +* `virtio-pmem` with`discard_writes=on` no longer marks the guest memory as + read only so avoids excessive VM exits (#1795) +* PCI device hotplug after an unplug was fixed (#1802) +* When using the ACPI method to resize the guest memory the full reserved size + can be used (#1803) +* Snapshot and restore followed by a second snapshot and restore now works + correctly +* Snapshot and restore of VMs with more than 2GiB in one region now work + correctly + +### Contributors + +Many thanks to everyone who has contributed to our 0.11.0 release including some new faces. + +* Anatol Belski +* Bo Chen +* Daniel Verkamp +* Henry Wang +* Hui Zhu +* Jiangbo Wu +* Josh Soref +* Julio Montes +* Michael Zhao +* Muminul Islam +* pierwill <19642016+pierwill@users.noreply.github.com> +* Praveen Paladugu +* Rob Bradford +* Sebastien Boeuf +* Wei Liu + + +# v0.10.0 + +This release has been tracked through the [0.10.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/13). + +Highlights for `cloud-hypervisor` version 0.10.0 include: + +### `virtio-block` Support for Multiple Descriptors + +Some `virtio-block` device drivers may generate requests with multiple descriptors and support has been added for those drivers. + +### Memory Zones + +Support has been added for fine grained control of memory allocation for the guest. This includes controlling the backing of sections of guest memory, assigning to specific host NUMA nodes and assigning memory and vCPUs to specific memory nodes inside the guest. Full details of this can be found in the [memory documentation](docs/memory.md). + +### `Seccomp` Sandbox Improvements + +All the remaining threads and devices are now isolated within their own `seccomp` filters. This provides a layer of sandboxing and enhances the security model of `cloud-hypervisor`. + +### Preliminary KVM HyperV Emulation Control + +A new option (`kvm_hyperv`) has been added to `--cpus` to provide an option to toggle on KVM's HyperV emulation support. This enables progress towards booting Windows without adding extra emulated devices. + +### Notable Bug Fixes + +- When using `ch-remote` to resize the VM parameter now accepts the standard sizes suffices (#1596) +- `cloud-hypervisor` no longer panics when started with `--memory hotplug_method=virtio-mem` and no `hotplug_size` (#1564) +- After a reboot memory can remove when using `--memory hotplug_method=virtio-mem` (#1593) +- `--version` shows the version for released binaries (#1669) +- Errors generated by worker threads for `virtio` devices are now printed out (#1551) + +### Contributors + +Many thanks to everyone who has contributed to our 0.10.0 release including some new faces. + +* Alyssa Ross +* Amey Narkhede +* Anatol Belski +* Bo Chen +* Hui Zhu +* Michael Zhao +* Muminul Islam +* Rob Bradford +* Samuel Ortiz +* Sebastien Boeuf +* Wei Liu + +# v0.9.0 + +This release has been tracked through the [0.9.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/12). + +Highlights for `cloud-hypervisor` version 0.9.0 include: + +### `io_uring` Based Block Device Support + +If the `io_uring` feature is enabled and the host kernel supports it then `io_uring` will be used for block devices. This results a very significant performance improvement. + +### Block and Network Device Statistics + +Statistics for activity of the `virtio` network and block devices is now exposed through a new `vm.counters` HTTP API entry point. These take the form of simple counters which can be used to observe the activity of the VM. + +### HTTP API Responses + +The HTTP API for adding devices now responds with the name that was assigned to the device as well the PCI BDF. + +### CPU Topology + +A `topology` parameter has been added to `--cpus` which allows the configuration of the guest CPU topology allowing the user to specify the numbers of sockets, packages per socket, cores per package and threads per core. + +### Release Build Optimization + +Our release build is now built with LTO (*Link Time Optimization*) which results in a ~20% reduction in the binary size. + +### Hypervisor Abstraction + +A new abstraction has been introduced, in the form of a `hypervisor` crate so as to enable the support of additional hypervisors beyond `KVM`. + +### Snapshot/Restore Improvements + +Multiple improvements have been made to the VM snapshot/restore support that was added in the last release. This includes persisting more vCPU state and in particular preserving the guest paravirtualized clock in order to avoid vCPU hangs inside the guest when running with multiple vCPUs. + +### Virtio Memory Ballooning Support + +A `virtio-balloon` device has been added, controlled through the `resize` control, which allows the reclamation of host memory by resizing a memory balloon inside the guest. + +### Enhancements to ARM64 Support + +The ARM64 support introduced in the last release has been further enhanced with support for using PCI for exposing devices into the guest as well as multiple bug fixes. It also now supports using an initramfs when booting. + +### Intel SGX Support + +The guest can now use Intel SGX if the host supports it. Details can be found in the dedicated [SGX documentation](docs/intel_sgx.md). + +### `Seccomp` Sandbox Improvements + +The most frequently used virtio devices are now isolated with their own `seccomp` filters. It is also now possible to pass `--seccomp=log` which result in the logging of requests that would have otherwise been denied to further aid development. + +### Notable Bug Fixes + +* Our `virtio-vsock` implementation has been resynced with the implementation from Firecracker and includes multiple bug fixes. +* CPU hotplug has been fixed so that it is now possible to add, remove, and re-add vCPUs (#1338) +* A workaround is now in place for when KVM reports MSRs available MSRs that are in fact unreadable preventing snapshot/restore from working correctly (#1543). +* `virtio-mmio` based devices are now more widely tested (#275). +* Multiple issues have been fixed with virtio device configuration (#1217) +* Console input was wrongly consumed by both `virtio-console` and the serial. (#1521) + +### Contributors + +Many thanks to everyone who has contributed to our 0.9.0 release including some new faces. + +* Anatol Belski +* Bo Chen +* Dr. David Alan Gilbert +* Henry Wang +* Howard Zhang +* Hui Zhu +* Jianyong Wu +* Jose Carlos Venegas Munoz +* LiYa'nan +* Michael Zhao +* Muminul Islam +* Praveen Paladugu +* Ricardo Koller +* Rob Bradford +* Samuel Ortiz +* Sebastien Boeuf +* Stefano Garzarella +* Wei Liu + + +# v0.8.0 + +This release has been tracked through the [0.8.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/10). + +Highlights for `cloud-hypervisor` version 0.8.0 include: + +### Experimental Snapshot and Restore Support + +This release includes the first version of the snapshot and restore feature. +This allows a VM to be paused and then subsequently snapshotted. At a later +point that snapshot may be restored into a new running VM identical to the +original VM at the point it was paused. + +This feature can be used for offline migration from one VM host to another, to +allow the upgrading or rebooting of the host machine transparently to the guest +or for templating the VM. This is an experimental feature and cannot be used on +a VM using passthrough (VFIO) devices. Issues with SMP have also been observed +(#1176). + +### Experimental ARM64 Support + +Included in this release is experimental support for running on ARM64. +Currently only `virtio-mmio` devices and a serial port are supported. Full +details can be found in the [ARM64 documentation](docs/arm64.md). + +### Support for Using 5-level Paging in Guests + +If the host supports it the guest is now enabled for 5-level paging (aka LA57). +This works when booting the Linux kernel with a vmlinux, bzImage or firmware +based boot. However booting an ELF kernel built with `CONFIG_PVH=y` does not +work due to current limitations in the PVH boot process. + +### Virtio Device Interrupt Suppression for Network Devices + +With `virtio-net` and `vhost-user-net` devices the guest can suppress +interrupts from the VMM by using the `VIRTIO_RING_F_EVENT_IDX` feature. This +can lead to an improvement in performance by reducing the number of interrupts +the guest must service. + +### `vhost_user_fs` Improvements + +The implementation in Cloud Hypervisor of the VirtioFS server now supports sandboxing itself with `seccomp`. + + +### Notable Bug Fixes + +* VMs that have not yet been booted can now be deleted (#1110). +* By creating the `tap` device ahead of creating the VM it is not required to + run the `cloud-hypervisor` binary with `CAP_NET_ADMIN` (#1273). +* Block I/O via `virtio-block` or `vhost-user-block` now correctly adheres to + the specification and synchronizes to the underlying filesystem as required + based on guest feature negotiation. This avoids potential data loss (#399, + #1216). +* When booting with a large number of vCPUs then the ACPI table would be + overwritten by the SMP `MPTABLE`. When compiled with the `acpi` feature the + `MPTABLE` will no longer be generated (#1132). +* Shutting down VMs that have been paused is now supported (#816). +* Created socket files are deleted on shutdown (#1083). +* Trying to use passthrough devices (VFIO) will be rejected on `mmio` builds + (#751). + +### Command Line and API Changes + +This is non exhaustive list of HTTP API and command line changes: + +* All user visible socket parameters are now consistently called `socket` + rather than `sock` in some cases. +* The `ch-remote` tool now shows any error message generated by the VMM +* The `wce` parameter has been removed from `--disk` as the feature is always + offered for negotiation. +* `--net` has gained a `host_mac` option that allows the setting of the MAC + address for the `tap` device on the host. + +### Contributors + +Many thanks to everyone who has contributed to our 0.8.0 release including some new faces. + +* Anatol Belski +* Arron Wang +* Bo Chen +* Dr. David Alan Gilbert +* Henry Wang +* Hui Zhu +* LiYa'nan +* Michael Zhao +* Rob Bradford +* Samuel Ortiz +* Sebastien Boeuf +* Sergio Lopez + +# v0.7.0 + +This release has been tracked through the [0.7.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/7). + +Highlights for `cloud-hypervisor` version 0.7.0 include: + +### Block, Network, Persistent Memory (PMEM), VirtioFS and Vsock hotplug + +Further to our effort to support modifying a running guest we now support +hotplug and unplug of the following virtio backed devices: block, network, +pmem, virtio-fs and vsock. This functionality is available on the (default) PCI +based transport and is exposed through the HTTP API. The `ch-remote` utility +provides a CLI for adding or removing these device types after the VM has +booted. User can use the `id` parameter on the devices to choose names for +devices to ease their removal. + +### Alternative `libc` Support + +Cloud Hypervisor can now be compiled with the `musl` C library and this release +contains a static binary compiled using that toolchain. + +### Multithreaded Multi Queued `vhost-user` Backends + +The `vhost-user` backends for network and block support that are shipped by +Cloud Hypervisor have been enhanced to support multiple threads and queues to +improve throughput. These backends are used automatically if `vhost_user=true` +is passed when the devices are created. + +### Initial RamFS Support + +By passing the `--initramfs` command line option the user can specify a file to +be loaded into the guest memory to be used as the kernel initial filesystem. +This is usually used to allow the loading of drivers needed to be able to +access the real root filesystem but it can also be used standalone for a very +minimal image. + +### Alternative Memory Hotplug: `virtio-mem` + +As well as supporting ACPI based hotplug Cloud Hypervisor now supports using +the `virtio-mem` hotplug alternative. This can be controlled by the +`hotplug_method` parameter on the `--memory` command line option. It currently +requires kernel patches to be able to support it. + +### `Seccomp` Sandboxing + +Cloud Hypervisor now has support for restricting the system calls that the +process can use via the `seccomp` security API. This on by default and is +controlled by the `--seccomp` command line option. + +### Updated Distribution Support + +With the release of Ubuntu 20.04 we have added that to the list of supported +distributions and is part of our regular testing programme. + +### Command Line and API Changes + +This is non exhaustive list of HTTP API and command line changes + +* New `id` fields added for devices to allow them to be named to ease removal. + If no name is specified the VMM chooses one. +* Use `--memory`'s `shared` and `hugepages` controls for determining backing + memory instead of providing a path. +* The `--vsock` parameter only takes one device as the Linux kernel only + supports a single Vsock device. The REST API has removed the vector for this + option and replaced it with a single optional field. +* There is enhanced validation of the command line and API provided + configurations to ensure that the provided options are compatible e.g. that + shared memory is in use if any attempt is made to used a `vhost-user` backed + device. +* `ch-remote` has added `add-disk`, `add-fs`, `add-net`, `add-pmem` and + `add-vsock` subcommands. For removal `remove-device` is used. The REST API + has appropriate new HTTP endpoints too. +* Specifying a `size` with `--pmem` is no longer required and instead the size + will be obtained from the file. A `discard_writes` option has also been added + to provide the equivalent of a read-only file. +* The parameters to `--block-backend` have been changed to more closely align + with those used by `--disk`. + +### Contributors + +Many thanks to everyone who has contributed to our 0.7.0 release including some new faces. + +* Alejandro Jimenez +* Bo Chen +* Cathy Zhang +* Damjan Georgievski +* Dean Sheather +* Eryu Guan +* Hui Zhu +* Jose Carlos Venegas Munoz +* Martin Xu +* Muminul Islam +* Rob Bradford +* Samuel Ortiz +* Sebastien Boeuf +* Sergio Lopez +* Yang Zhong +* Yi Sun + +# v0.6.0 + +This release has been tracked through the [0.6.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/7). + +Highlights for `cloud-hypervisor` version 0.6.0 include: + +### Directly Assigned Devices Hotplug + +We continued our efforts around supporting dynamically changing the guest +resources. After adding support for CPU and memory hotplug, Cloud Hypervisor +now supports hot plugging and hot unplugging directly assigned (a.k.a. `VFIO`) +devices into an already running guest. This closes the features gap for +providing a complete Kata Containers workloads support with Cloud Hypervisor. + +### Shared Filesystem Improvements + +We enhanced our shared filesystem support through many `virtio-fs` improvements. +By adding support for DAX, parallel processing of multiple requests, `FS_IO`, +`LSEEK` and the `MMIO` virtio transport layer to our `vhost_user_fs` daemon, we +improved our filesystem sharing performance, but also made it more stable and +compatible with other `virtio-fs` implementations. + +### Block and Networking IO Self Offloading + +When choosing to offload the paravirtualized block and networking I/O to an +external process (through the `vhost-user` protocol), Cloud Hypervisor now +automatically spawns its default `vhost-user-blk` and `vhost-user-net` backends +into their own, separate processes. +This provides a seamless paravirtualized I/O user experience for those who want +to run their guest I/O into separate executions contexts. + +### Command Line Interface + +More and more Cloud Hypervisor services are exposed through the +[Rest API](vmm/src/api/openapi/cloud-hypervisor.yaml) and thus only +accessible via relatively cumbersome HTTP calls. In order to abstract +those calls into a more user friendly tool, we created a Cloud Hypervisor +Command Line Interface (CLI) called `ch-remote`. The `ch-remote` binary +is created with each build and available e.g. at +`cloud-hypervisor/target/debug/ch-remote` when doing a debug build. + +Please check `ch-remote --help` for a complete description of all available +commands. + +### PVH Boot + +In addition to the traditional Linux boot protocol, Cloud Hypervisor now +supports direct kernel booting through the [PVH ABI](https://xenbits.xen.org/docs/unstable/misc/pvh.html). + +### Contributors + +With the 0.6.0 release, we are welcoming a few new contributors. Many thanks +to them and to everyone that contributed to this release: + +* Alejandro Jimenez +* Arron Wang +* Bin Liu +* Bo Chen +* Cathy Zhang +* Eryu Guan +* Jose Carlos Venegas Munoz +* Liu Bo +* Qiu Wenbo +* Rob Bradford +* Samuel Ortiz +* Sebastien Boeuf +* Sergio Lopez + +# v0.5.1 + +This is a bugfix release branched off v0.5.0. It contains the following fixes: + +* Update DiskConfig to contain missing disk control features (#790) - Samuel Ortiz and Sergio Lopez +* Prevent memory overcommit via virtio-fs (#763) - Sebastien Boeuf +* Fixed error reporting for resize command - Samuel Ortiz +* Double reboot workaround (#783) - Rob Bradford +* Various CI and development tooling fixes - Sebastien Boeuf, Samuel Ortiz, Rob Bradford + +# v0.5.0 + +This release has been tracked through the [0.5.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/6). + +Highlights for `cloud-hypervisor` version 0.5.0 include: + +### Virtual Machine Dynamic Resizing + +With 0.4.0 we added support for CPU hot plug, and 0.5.0 adds CPU hot unplug and +memory hot plug as well. This allows to dynamically resize Cloud Hypervisor +guests which is needed for e.g. Kubernetes related use cases. +The memory hot plug implementation is based on the same framework as the CPU hot +plug/unplug one, i.e. hardware-reduced ACPI notifications to the guest. + +Next on our VM resizing roadmap is the PCI devices hotplug feature. + +### Multi-Queue, Multi-Threaded Paravirtualization + +We enhanced our virtio networking and block support by having both devices use +multiple I/O queues handled by multiple threads. This improves our default +paravirtualized networking and block devices throughput. + +### New Interrupt Management Framework + +We improved our interrupt management implementation by introducing an Interrupt +Manager framework, based on the currently on-going [rust-vmm vm-device](https://github.com/rust-vmm/vm-device) +crates discussions. This move made the code significantly cleaner, and allowed +us to remove several KVM related dependencies from crates like the PCI and +virtio ones. + +### Development Tools + +In order to provide a better developer experience, we worked on improving our +build, development and testing tools. +Somehow similar to the excellent +[Firecracker's devtool](https://github.com/firecracker-microvm/firecracker/blob/master/tools/devtool), +we now provide a [dev_cli script](scripts/dev_cli.sh). + +With this new tool, our users and contributors will be able to build and test +Cloud Hypervisor through a containerized environment. + +### Kata Containers Integration + +We spent some significant time and efforts debugging and fixing our integration +with the [Kata Containers](https://github.com/kata-containers) project. Cloud +Hypervisor is now a fully supported Kata Containers hypervisor, and is +integrated into the project's CI. + +### Contributors + +Many thanks to everyone that contributed to the 0.5.0 release: + +* Bo Chen +* Cathy Zhang +* Qiu Wenbo +* Rob Bradford +* Samuel Ortiz +* Sebastien Boeuf +* Sergio Lopez +* Yang Zhong + +# v0.4.0 + +This release has been tracked through the [0.4.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/4). + +Highlights for `cloud-hypervisor` version 0.4.0 include: + +### Dynamic virtual CPUs addition + +As a way to vertically scale Cloud Hypervisor guests, we now support dynamically +adding virtual CPUs to the guests, a mechanism also known as CPU hot plug. +Through hardware-reduced ACPI notifications, Cloud Hypervisor can now add CPUs +to an already running guest and the high level operations for that process are +documented [here](docs/hotplug.md) + +During the next release cycles we are planning to extend Cloud Hypervisor +hot plug framework to other resources, namely PCI devices and memory. + +### Programmatic firmware tables generation + +As part of the CPU hot plug feature enablement, and as a requirement for hot +plugging other resources like devices or RAM, we added support for +programmatically generating the needed ACPI tables. Through a dedicated +`acpi-tables` crate, we now have a flexible and clean way of generating those +tables based on the VMM device model and topology. + +### Filesystem and block devices vhost-user backends + +Our objective of running all Cloud Hypervisor paravirtualized I/O to a +vhost-user based framework is getting closer as we've added Rust based +implementations for vhost-user-blk and virtiofs backends. Together with the +vhost-user-net backend that came with the 0.3.0 release, this will form the +default Cloud Hypervisor I/O architecture. + +### Guest pause and resume + +As an initial requirement for enabling live migration, we added support for +pausing and resuming any VMM components. As an intermediate step towards live +migration, the upcoming guest snapshotting feature will be based on the pause +and resume capabilities. + +### Userspace IOAPIC by default + +As a way to simplify our device manager implementation, but also in order to +stay away from privileged rings as often as possible, any device that relies on +pin based interrupts will be using the userspace IOAPIC implementation by +default. + +### PCI BAR reprogramming + +In order to allow for a more flexible device model, and also support guests +that would want to move PCI devices, we added support for PCI devices BAR +reprogramming. + +### New `cloud-hypervisor` organization + +As we wanted to be more flexible on how we manage the Cloud Hypervisor project, +we decided to move it under a [dedicated GitHub organization](https://github.com/cloud-hypervisor). +Together with the [cloud-hypervisor](https://github.com/cloud-hypervisor/cloud-hypervisor) +project, this new organization also now hosts our [kernel](https://github.com/cloud-hypervisor/linux) +and [firmware](https://github.com/cloud-hypervisor/rust-hypervisor-firmware) +repositories. We may also use it to host any rust-vmm that we'd need to +temporarily fork. +Thanks to GitHub's seamless repository redirections, the move is completely +transparent to all Cloud Hypervisor contributors, users and followers. + +### Contributors + +Many thanks to everyone that contributed to the 0.4.0 release: + +* Cathy Zhang +* Emin Ghuliev +* Jose Carlos Venegas Munoz +* Qiu Wenbo +* Rob Bradford +* Samuel Ortiz +* Sebastien Boeuf +* Sergio Lopez +* Wu Zongyong + +# v0.3.0 + +This release has been tracked through the [0.3.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/3). + +Highlights for `cloud-hypervisor` version 0.3.0 include: + +### Block device offloading + +We continue to work on offloading paravirtualized I/O to external processes, +and we added support for +[vhost-user-blk](https://access.redhat.com/solutions/3394851) backends. +This enables `cloud-hypervisor` users to plug a `vhost-user` based block device +like [SPDK](https://spdk.io)) into the VMM as their paravirtualized storage +backend. + +### Network device backend + +The previous release provided support for +[vhost-user-net](https://access.redhat.com/solutions/3394851) backends. Now we +also provide a TAP based vhost-user-net backend, implemented in Rust. Together +with the vhost-user-net device implementation, this will eventually become the +Cloud Hypervisor default paravirtualized networking architecture. + +### Virtual sockets + +In order to more efficiently and securely communicate between host and guest, +we added an hybrid implementation of the +[VSOCK](http://man7.org/linux/man-pages/man7/vsock.7.html) socket address +family over virtio. Credits go to the +[Firecracker](https://github.com/firecracker-microvm/firecracker/blob/master/docs/vsock.md) +project as our implementation is a copy of theirs. + +### HTTP based API + +In anticipation of the need to support asynchronous operations to Cloud +Hypervisor guests (e.g. resources hotplug and guest migration), we added a HTTP +based API to the VMM. The API will be more extensively documented during the +next release cycle. + +### Memory mapped virtio transport + +In order to support potential PCI-free use cases, we added support for the +[virtio MMIO](https://docs.oasis-open.org/virtio/virtio/v1.1/cs01/virtio-v1.1-cs01.html#x1-1440002) +transport layer. This will allow us to support simple, minimal guest +configurations that do not require a PCI bus emulation. + +### Paravirtualized IOMMU + +As we want to improve our nested guests support, we added support for exposing +a [paravirtualized IOMMU](docs/iommu.md) device through virtio. This allows +for a safer nested virtio and directly assigned devices support. + +To add the IOMMU support, we had to make some CLI changes for Cloud Hypervisor +users to be able to specify if devices had to be handled through this virtual +IOMMU or not. In particular, the `--disk` option now expects disk paths to be +prefixed with a `path=` string, and supports an optional `iommu=[on|off]` +setting. + +### Ubuntu 19.10 + +With the latest [hypervisor firmware](https://github.com/cloud-hypervisor/rust-hypervisor-firmware), +we can now support the latest +[Ubuntu 19.10 (Eoan Ermine)](http://releases.ubuntu.com/19.10/) cloud images. + +### Large memory guests + +After simplifying and changing our guest address space handling, we can now +support guests with large amount of memory (more than 64GB). + +# v0.2.0 + +This release has been tracked through the [0.2.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/2). + +Highlights for `cloud-hypervisor` version 0.2.0 include: + +### Network device offloading + +As part of our general effort to offload paravirtualized I/O to external +processes, we added support for +[vhost-user-net](https://access.redhat.com/solutions/3394851) backends. This +enables `cloud-hypervisor` users to plug a `vhost-user` based networking device +(e.g. [DPDK](https://dpdk.org)) into the VMM as their virtio network backend. + +### Minimal hardware-reduced ACPI + +In order to properly implement and guest reset and shutdown, we implemented +a minimal version of the hardware-reduced ACPI specification. Together with +a tiny I/O port based ACPI device, this allows `cloud-hypervisor` guests to +cleanly reboot and shutdown. + +The ACPI implementation is a `cloud-hypervisor` build time option that is +enabled by default. + +### Debug I/O port + +Based on the Firecracker idea of using a dedicated I/O port to measure guest +boot times, we added support for logging guest events through the +[0x80](https://www.intel.com/content/www/us/en/support/articles/000005500/boards-and-kits.html) +PC debug port. This allows, among other things, for granular guest boot time +measurements. See our [debug port documentation](docs/debug-port.md) for more +details. + +### Improved direct device assignment + +We fixed a major performance issue with our initial VFIO implementation: When +enabling VT-d through the KVM and VFIO APIs, our guest memory writes and reads +were (in many cases) not cached. After correctly tagging the guest memory from +`cloud-hypervisor` we're now able to reach the expected performance from +directly assigned devices. + +### Improved shared filesystem + +We added shared memory region with [DAX](https://www.kernel.org/doc/Documentation/filesystems/dax.txt) +support to our [virtio-fs](https://virtio-fs.gitlab.io/) shared file system. +This provides better shared filesystem IO performance with a smaller guest +memory footprint. + +### Ubuntu bionic based CI + +Thanks to our [simple KVM firmware](https://github.com/cloud-hypervisor/rust-hypervisor-firmware) +improvements, we are now able to boot Ubuntu bionic images. We added those to +our CI pipeline. + +# v0.1.0 + +This release has been tracked through the [0.1.0 project](https://github.com/cloud-hypervisor/cloud-hypervisor/projects/1). + +Highlights for `cloud-hypervisor` version 0.1.0 include: + +### Shared filesystem + +We added support for the [virtio-fs](https://virtio-fs.gitlab.io/) shared file +system, allowing for an efficient and reliable way of sharing a filesystem +between the host and the `cloud-hypervisor` guest. + +See our [filesystem sharing](docs/fs.md) documentation for more details on how +to use virtio-fs with `cloud-hypervisor`. + +### Initial direct device assignment support + +VFIO (Virtual Function I/O) is a kernel framework that exposes direct device +access to userspace. `cloud-hypervisor` uses VFIO to directly assign host +physical devices into its guest. + +See our [VFIO](docs/vfio.md) documentation for more detail on how to directly +assign host devices to `cloud-hypervisor` guests. + +### Userspace IOAPIC + +`cloud-hypervisor` supports a so-called split IRQ chip implementation by +implementing support for the [IOAPIC](https://wiki.osdev.org/IOAPIC). +By moving part of the IRQ chip implementation from kernel space to user space, +the IRQ chip emulation does not always run in a fully privileged mode. + +### Virtual persistent memory + +The `virtio-pmem` implementation emulates a virtual persistent memory device +that `cloud-hypervisor` can e.g. boot from. Booting from a `virtio-pmem` device +allows to bypass the guest page cache and improve the guest memory footprint. + +### Linux kernel bzImage + +The `cloud-hypervisor` linux kernel loader now supports direct kernel boot from +`bzImage` kernel images, which is usually the format that Linux distributions +use to ship their kernels. For example, this allows for booting from the host +distribution kernel image. + +### Console over virtio + +`cloud-hypervisor` now exposes a `virtio-console` device to the guest. Although +using this device as a guest console can potentially cut some early boot +messages, it can reduce the guest boot time and provides a complete console +implementation. + +The `virtio-console` device is enabled by default for the guest console. +Switching back to the legacy serial port is done by selecting +`--serial tty --console off` from the command line. + +### Unit testing + +We now run all unit tests from all our crates directly from our CI. + +### Integration tests parallelization + +The CI cycle run time has been significantly reduced by refactoring our +integration tests; allowing them to all be run in parallel. diff --git a/vendor/cube-hypervisor/resources/Dockerfile b/vendor/cube-hypervisor/resources/Dockerfile new file mode 100644 index 0000000..fc481fb --- /dev/null +++ b/vendor/cube-hypervisor/resources/Dockerfile @@ -0,0 +1,133 @@ +# When changing this file don't forget to update the tag name in the +# .github/workflows/docker-image.yaml file if doing multiple per day + +FROM ubuntu:20.04 as dev + +ARG TARGETARCH +ARG RUST_TOOLCHAIN="1.77.0" +ARG CLH_SRC_DIR="/cloud-hypervisor" +ARG CLH_BUILD_DIR="$CLH_SRC_DIR/build" +ARG CARGO_REGISTRY_DIR="$CLH_BUILD_DIR/cargo_registry" +ARG CARGO_GIT_REGISTRY_DIR="$CLH_BUILD_DIR/cargo_git_registry" + +ENV CARGO_HOME=/usr/local/rust +ENV RUSTUP_HOME=$CARGO_HOME +ENV PATH="$PATH:$CARGO_HOME/bin" + +# Install all CI dependencies +RUN apt-get update \ + && apt-get -yq upgrade \ + && DEBIAN_FRONTEND=noninteractive apt-get install -yq \ + build-essential \ + bc \ + curl \ + wget \ + sudo \ + mtools \ + musl-tools \ + libssl-dev \ + pkg-config \ + flex \ + bison \ + libelf-dev \ + qemu-utils \ + libglib2.0-dev \ + libpixman-1-dev \ + libseccomp-dev \ + libcap-ng-dev \ + socat \ + dosfstools \ + cpio \ + python \ + python3 \ + python3-setuptools \ + ntfs-3g \ + openvswitch-switch-dpdk \ + python3-distutils \ + uuid-dev \ + iperf3 \ + zip \ + git-core \ + dnsmasq \ + dmsetup \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +RUN update-alternatives --set ovs-vswitchd /usr/lib/openvswitch-switch-dpdk/ovs-vswitchd-dpdk + +RUN if [ "$TARGETARCH" = "amd64" ]; then \ + apt-get update \ + && apt-get -yq upgrade \ + && DEBIAN_FRONTEND=noninteractive apt-get install -yq gcc-multilib \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/*; fi + +RUN if [ "$TARGETARCH" = "arm64" ]; then \ + # On AArch64, `setcap` binary should be installed via `libcap2-bin`. + # The `setcap` binary is used in integration tests. + # `libguestfs-tools` is used for modifying cloud image kernel, and it requires + # kernel (any version) image in `/boot` and modules in `/lib/modules`. + apt-get update \ + && apt-get -yq upgrade \ + && DEBIAN_FRONTEND=noninteractive apt-get install -yq \ + libcap2-bin \ + libguestfs-tools \ + linux-image-generic \ + autotools-dev \ + autoconf \ + automake \ + perl \ + texinfo \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/*; fi + +# Fix the libssl-dev install +RUN export ARCH="$(uname -m)" \ + && cp /usr/include/$ARCH-linux-gnu/openssl/opensslconf.h /usr/include/openssl/ +ENV X86_64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu/ +ENV X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu/ +ENV AARCH64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR=/usr/lib/aarch64-linux-gnu/ +ENV AARCH64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR=/usr/lib/aarch64-linux-gnu/ +ENV OPENSSL_INCLUDE_DIR=/usr/include/ + +# Install the rust toolchain +RUN export ARCH="$(uname -m)" \ + && nohup curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain "$RUST_TOOLCHAIN" \ + && rustup target add $ARCH-unknown-linux-musl --toolchain "$RUST_TOOLCHAIN" \ + && if [ "$TARGETARCH" = "amd64" ]; then rustup toolchain add --profile minimal $RUST_TOOLCHAIN-x86_64-unknown-linux-musl; fi \ + && if [ "$TARGETARCH" = "amd64" ]; then rustup component add rustfmt; fi \ + && if [ "$TARGETARCH" = "amd64" ]; then rustup component add clippy; fi \ + && rm -rf "$CARGO_HOME/registry" \ + && ln -s "$CARGO_REGISTRY_DIR" "$CARGO_HOME/registry" \ + && rm -rf "$CARGO_HOME/git" \ + && ln -s "$CARGO_GIT_REGISTRY_DIR" "$CARGO_HOME/git" + +# Set the rust environment +RUN echo 'source $CARGO_HOME/env' >> $HOME/.bashrc \ + && mkdir $HOME/.cargo \ + && ln -s $CARGO_HOME/env $HOME/.cargo/env + +# install SPDK NVMe +# only for 'x86_64' platform images as 'docker buildx' can't build 'spdk' +RUN if [ "$TARGETARCH" = "amd64" ]; then \ + git clone https://github.com/spdk/spdk \ + && cd spdk \ + && git checkout 6301f8915de32baed10dba1eebed556a6749211a \ + && git submodule update --init \ + && apt-get update \ + && ./scripts/pkgdep.sh \ + && apt-get clean \ + && ./configure --with-vfio-user \ + && make -j `nproc` \ + && mkdir /usr/local/bin/spdk-nvme \ + && cp ./build/bin/nvmf_tgt /usr/local/bin/spdk-nvme \ + && cp ./scripts/rpc.py /usr/local/bin/spdk-nvme \ + && cp -r ./scripts/rpc /usr/local/bin/spdk-nvme \ + && cd .. && rm -rf spdk; fi + +# install ethr tool for performance tests +RUN if [ "$TARGETARCH" = "amd64" ]; then \ + wget https://github.com/microsoft/ethr/releases/latest/download/ethr_linux.zip \ + && unzip ethr_linux.zip \ + && cp ethr /usr/local/bin \ + && rm ethr_linux.zip; fi diff --git a/vendor/cube-hypervisor/resources/linux-config-aarch64 b/vendor/cube-hypervisor/resources/linux-config-aarch64 new file mode 100644 index 0000000..732b466 --- /dev/null +++ b/vendor/cube-hypervisor/resources/linux-config-aarch64 @@ -0,0 +1,3091 @@ +# +# Automatically generated file; DO NOT EDIT. +# Linux/arm64 5.15.12 Kernel Configuration +# +CONFIG_CC_VERSION_TEXT="gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0" +CONFIG_CC_IS_GCC=y +CONFIG_GCC_VERSION=90300 +CONFIG_CLANG_VERSION=0 +CONFIG_AS_IS_GNU=y +CONFIG_AS_VERSION=23400 +CONFIG_LD_IS_BFD=y +CONFIG_LD_VERSION=23400 +CONFIG_LLD_VERSION=0 +CONFIG_CC_CAN_LINK=y +CONFIG_CC_CAN_LINK_STATIC=y +CONFIG_CC_HAS_ASM_GOTO=y +CONFIG_CC_HAS_ASM_INLINE=y +CONFIG_CC_HAS_NO_PROFILE_FN_ATTR=y +CONFIG_IRQ_WORK=y +CONFIG_BUILDTIME_TABLE_SORT=y +CONFIG_THREAD_INFO_IN_TASK=y + +# +# General setup +# +CONFIG_INIT_ENV_ARG_LIMIT=32 +# CONFIG_COMPILE_TEST is not set +# CONFIG_WERROR is not set +CONFIG_LOCALVERSION="" +# CONFIG_LOCALVERSION_AUTO is not set +CONFIG_BUILD_SALT="" +CONFIG_DEFAULT_INIT="" +CONFIG_DEFAULT_HOSTNAME="cloud-hypervisor" +CONFIG_SWAP=y +CONFIG_SYSVIPC=y +CONFIG_SYSVIPC_SYSCTL=y +CONFIG_POSIX_MQUEUE=y +CONFIG_POSIX_MQUEUE_SYSCTL=y +# CONFIG_WATCH_QUEUE is not set +CONFIG_CROSS_MEMORY_ATTACH=y +# CONFIG_USELIB is not set +CONFIG_AUDIT=y +CONFIG_HAVE_ARCH_AUDITSYSCALL=y +CONFIG_AUDITSYSCALL=y + +# +# IRQ subsystem +# +CONFIG_GENERIC_IRQ_PROBE=y +CONFIG_GENERIC_IRQ_SHOW=y +CONFIG_GENERIC_IRQ_SHOW_LEVEL=y +CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK=y +CONFIG_GENERIC_IRQ_MIGRATION=y +CONFIG_HARDIRQS_SW_RESEND=y +CONFIG_IRQ_DOMAIN=y +CONFIG_IRQ_DOMAIN_HIERARCHY=y +CONFIG_GENERIC_IRQ_IPI=y +CONFIG_GENERIC_MSI_IRQ=y +CONFIG_GENERIC_MSI_IRQ_DOMAIN=y +CONFIG_IRQ_MSI_IOMMU=y +CONFIG_HANDLE_DOMAIN_IRQ=y +CONFIG_IRQ_FORCED_THREADING=y +CONFIG_SPARSE_IRQ=y +# CONFIG_GENERIC_IRQ_DEBUGFS is not set +# end of IRQ subsystem + +CONFIG_GENERIC_TIME_VSYSCALL=y +CONFIG_GENERIC_CLOCKEVENTS=y +CONFIG_ARCH_HAS_TICK_BROADCAST=y +CONFIG_GENERIC_CLOCKEVENTS_BROADCAST=y + +# +# Timers subsystem +# +CONFIG_TICK_ONESHOT=y +CONFIG_NO_HZ_COMMON=y +# CONFIG_HZ_PERIODIC is not set +CONFIG_NO_HZ_IDLE=y +# CONFIG_NO_HZ_FULL is not set +CONFIG_NO_HZ=y +CONFIG_HIGH_RES_TIMERS=y +# end of Timers subsystem + +CONFIG_BPF=y +CONFIG_HAVE_EBPF_JIT=y +CONFIG_ARCH_WANT_DEFAULT_BPF_JIT=y + +# +# BPF subsystem +# +CONFIG_BPF_SYSCALL=y +# CONFIG_BPF_UNPRIV_DEFAULT_OFF is not set +# CONFIG_BPF_PRELOAD is not set +# end of BPF subsystem + +CONFIG_PREEMPT_NONE=y +# CONFIG_PREEMPT_VOLUNTARY is not set +# CONFIG_PREEMPT is not set +# CONFIG_SCHED_CORE is not set + +# +# CPU/Task time and stats accounting +# +CONFIG_TICK_CPU_ACCOUNTING=y +# CONFIG_VIRT_CPU_ACCOUNTING_GEN is not set +# CONFIG_IRQ_TIME_ACCOUNTING is not set +CONFIG_HAVE_SCHED_AVG_IRQ=y +CONFIG_BSD_PROCESS_ACCT=y +CONFIG_BSD_PROCESS_ACCT_V3=y +CONFIG_TASKSTATS=y +CONFIG_TASK_DELAY_ACCT=y +CONFIG_TASK_XACCT=y +CONFIG_TASK_IO_ACCOUNTING=y +# CONFIG_PSI is not set +# end of CPU/Task time and stats accounting + +CONFIG_CPU_ISOLATION=y + +# +# RCU Subsystem +# +CONFIG_TREE_RCU=y +# CONFIG_RCU_EXPERT is not set +CONFIG_SRCU=y +CONFIG_TREE_SRCU=y +CONFIG_TASKS_RCU_GENERIC=y +CONFIG_TASKS_TRACE_RCU=y +CONFIG_RCU_STALL_COMMON=y +CONFIG_RCU_NEED_SEGCBLIST=y +# end of RCU Subsystem + +# CONFIG_IKCONFIG is not set +# CONFIG_IKHEADERS is not set +CONFIG_LOG_BUF_SHIFT=21 +CONFIG_LOG_CPU_MAX_BUF_SHIFT=12 +CONFIG_PRINTK_SAFE_LOG_BUF_SHIFT=13 +# CONFIG_PRINTK_INDEX is not set +CONFIG_GENERIC_SCHED_CLOCK=y + +# +# Scheduler features +# +# end of Scheduler features + +CONFIG_ARCH_SUPPORTS_NUMA_BALANCING=y +CONFIG_CC_HAS_INT128=y +CONFIG_ARCH_SUPPORTS_INT128=y +CONFIG_NUMA_BALANCING=y +# CONFIG_NUMA_BALANCING_DEFAULT_ENABLED is not set +CONFIG_CGROUPS=y +CONFIG_PAGE_COUNTER=y +CONFIG_MEMCG=y +CONFIG_MEMCG_SWAP=y +CONFIG_MEMCG_KMEM=y +CONFIG_BLK_CGROUP=y +CONFIG_CGROUP_WRITEBACK=y +CONFIG_CGROUP_SCHED=y +CONFIG_FAIR_GROUP_SCHED=y +CONFIG_CFS_BANDWIDTH=y +CONFIG_RT_GROUP_SCHED=y +CONFIG_CGROUP_PIDS=y +# CONFIG_CGROUP_RDMA is not set +CONFIG_CGROUP_FREEZER=y +CONFIG_CGROUP_HUGETLB=y +CONFIG_CPUSETS=y +CONFIG_PROC_PID_CPUSET=y +CONFIG_CGROUP_DEVICE=y +CONFIG_CGROUP_CPUACCT=y +CONFIG_CGROUP_PERF=y +CONFIG_CGROUP_BPF=y +# CONFIG_CGROUP_MISC is not set +# CONFIG_CGROUP_DEBUG is not set +CONFIG_SOCK_CGROUP_DATA=y +CONFIG_NAMESPACES=y +CONFIG_UTS_NS=y +CONFIG_TIME_NS=y +CONFIG_IPC_NS=y +CONFIG_USER_NS=y +CONFIG_PID_NS=y +CONFIG_NET_NS=y +# CONFIG_CHECKPOINT_RESTORE is not set +CONFIG_SCHED_AUTOGROUP=y +# CONFIG_SYSFS_DEPRECATED is not set +CONFIG_RELAY=y +CONFIG_BLK_DEV_INITRD=y +CONFIG_INITRAMFS_SOURCE="" +CONFIG_RD_GZIP=y +CONFIG_RD_BZIP2=y +CONFIG_RD_LZMA=y +CONFIG_RD_XZ=y +CONFIG_RD_LZO=y +CONFIG_RD_LZ4=y +# CONFIG_RD_ZSTD is not set +# CONFIG_BOOT_CONFIG is not set +CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y +# CONFIG_CC_OPTIMIZE_FOR_SIZE is not set +CONFIG_LD_ORPHAN_WARN=y +CONFIG_SYSCTL=y +CONFIG_SYSCTL_EXCEPTION_TRACE=y +CONFIG_EXPERT=y +CONFIG_MULTIUSER=y +CONFIG_SGETMASK_SYSCALL=y +CONFIG_SYSFS_SYSCALL=y +CONFIG_FHANDLE=y +CONFIG_POSIX_TIMERS=y +CONFIG_PRINTK=y +CONFIG_BUG=y +CONFIG_ELF_CORE=y +CONFIG_BASE_FULL=y +CONFIG_FUTEX=y +CONFIG_FUTEX_PI=y +CONFIG_HAVE_FUTEX_CMPXCHG=y +CONFIG_EPOLL=y +CONFIG_SIGNALFD=y +CONFIG_TIMERFD=y +CONFIG_EVENTFD=y +CONFIG_SHMEM=y +CONFIG_AIO=y +CONFIG_IO_URING=y +CONFIG_ADVISE_SYSCALLS=y +CONFIG_HAVE_ARCH_USERFAULTFD_MINOR=y +CONFIG_MEMBARRIER=y +CONFIG_KALLSYMS=y +# CONFIG_KALLSYMS_ALL is not set +CONFIG_KALLSYMS_BASE_RELATIVE=y +CONFIG_USERFAULTFD=y +CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE=y +# CONFIG_KCMP is not set +CONFIG_RSEQ=y +# CONFIG_DEBUG_RSEQ is not set +# CONFIG_EMBEDDED is not set +CONFIG_HAVE_PERF_EVENTS=y +# CONFIG_PC104 is not set + +# +# Kernel Performance Events And Counters +# +CONFIG_PERF_EVENTS=y +# CONFIG_DEBUG_PERF_USE_VMALLOC is not set +# end of Kernel Performance Events And Counters + +CONFIG_VM_EVENT_COUNTERS=y +CONFIG_SLUB_DEBUG=y +# CONFIG_COMPAT_BRK is not set +# CONFIG_SLAB is not set +CONFIG_SLUB=y +# CONFIG_SLOB is not set +CONFIG_SLAB_MERGE_DEFAULT=y +# CONFIG_SLAB_FREELIST_RANDOM is not set +CONFIG_SLAB_FREELIST_HARDENED=y +# CONFIG_SHUFFLE_PAGE_ALLOCATOR is not set +CONFIG_SLUB_CPU_PARTIAL=y +# CONFIG_PROFILING is not set +# end of General setup + +CONFIG_ARM64=y +CONFIG_64BIT=y +CONFIG_MMU=y +CONFIG_ARM64_PAGE_SHIFT=12 +CONFIG_ARM64_CONT_PTE_SHIFT=4 +CONFIG_ARM64_CONT_PMD_SHIFT=4 +CONFIG_ARCH_MMAP_RND_BITS_MIN=18 +CONFIG_ARCH_MMAP_RND_BITS_MAX=33 +CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN=11 +CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX=16 +CONFIG_STACKTRACE_SUPPORT=y +CONFIG_ILLEGAL_POINTER_VALUE=0xdead000000000000 +CONFIG_LOCKDEP_SUPPORT=y +CONFIG_GENERIC_BUG=y +CONFIG_GENERIC_BUG_RELATIVE_POINTERS=y +CONFIG_GENERIC_HWEIGHT=y +CONFIG_GENERIC_CSUM=y +CONFIG_GENERIC_CALIBRATE_DELAY=y +CONFIG_ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE=y +CONFIG_SMP=y +CONFIG_KERNEL_MODE_NEON=y +CONFIG_FIX_EARLYCON_MEM=y +CONFIG_PGTABLE_LEVELS=4 +CONFIG_ARCH_SUPPORTS_UPROBES=y +CONFIG_ARCH_PROC_KCORE_TEXT=y + +# +# Platform selection +# +# CONFIG_ARCH_ACTIONS is not set +# CONFIG_ARCH_SUNXI is not set +# CONFIG_ARCH_ALPINE is not set +# CONFIG_ARCH_APPLE is not set +# CONFIG_ARCH_BCM2835 is not set +# CONFIG_ARCH_BCM4908 is not set +# CONFIG_ARCH_BCM_IPROC is not set +# CONFIG_ARCH_BERLIN is not set +# CONFIG_ARCH_BITMAIN is not set +# CONFIG_ARCH_BRCMSTB is not set +# CONFIG_ARCH_EXYNOS is not set +# CONFIG_ARCH_SPARX5 is not set +# CONFIG_ARCH_K3 is not set +# CONFIG_ARCH_LAYERSCAPE is not set +# CONFIG_ARCH_LG1K is not set +# CONFIG_ARCH_HISI is not set +# CONFIG_ARCH_KEEMBAY is not set +# CONFIG_ARCH_MEDIATEK is not set +# CONFIG_ARCH_MESON is not set +# CONFIG_ARCH_MVEBU is not set +# CONFIG_ARCH_MXC is not set +# CONFIG_ARCH_QCOM is not set +# CONFIG_ARCH_REALTEK is not set +# CONFIG_ARCH_RENESAS is not set +# CONFIG_ARCH_ROCKCHIP is not set +# CONFIG_ARCH_S32 is not set +# CONFIG_ARCH_SEATTLE is not set +# CONFIG_ARCH_INTEL_SOCFPGA is not set +# CONFIG_ARCH_SYNQUACER is not set +# CONFIG_ARCH_TEGRA is not set +# CONFIG_ARCH_SPRD is not set +# CONFIG_ARCH_THUNDER is not set +# CONFIG_ARCH_THUNDER2 is not set +# CONFIG_ARCH_UNIPHIER is not set +# CONFIG_ARCH_VEXPRESS is not set +# CONFIG_ARCH_VISCONTI is not set +# CONFIG_ARCH_XGENE is not set +# CONFIG_ARCH_ZYNQMP is not set +# end of Platform selection + +# +# Kernel Features +# + +# +# ARM errata workarounds via the alternatives framework +# +CONFIG_ARM64_WORKAROUND_CLEAN_CACHE=y +CONFIG_ARM64_ERRATUM_826319=y +CONFIG_ARM64_ERRATUM_827319=y +CONFIG_ARM64_ERRATUM_824069=y +CONFIG_ARM64_ERRATUM_819472=y +CONFIG_ARM64_ERRATUM_832075=y +CONFIG_ARM64_ERRATUM_834220=y +CONFIG_ARM64_ERRATUM_843419=y +CONFIG_ARM64_LD_HAS_FIX_ERRATUM_843419=y +CONFIG_ARM64_ERRATUM_1024718=y +CONFIG_ARM64_WORKAROUND_SPECULATIVE_AT=y +CONFIG_ARM64_ERRATUM_1165522=y +CONFIG_ARM64_ERRATUM_1319367=y +CONFIG_ARM64_ERRATUM_1530923=y +CONFIG_ARM64_WORKAROUND_REPEAT_TLBI=y +CONFIG_ARM64_ERRATUM_1286807=y +CONFIG_ARM64_ERRATUM_1463225=y +CONFIG_ARM64_ERRATUM_1542419=y +CONFIG_ARM64_ERRATUM_1508412=y +CONFIG_CAVIUM_ERRATUM_22375=y +CONFIG_CAVIUM_ERRATUM_23144=y +CONFIG_CAVIUM_ERRATUM_23154=y +CONFIG_CAVIUM_ERRATUM_27456=y +CONFIG_CAVIUM_ERRATUM_30115=y +CONFIG_CAVIUM_TX2_ERRATUM_219=y +CONFIG_FUJITSU_ERRATUM_010001=y +CONFIG_HISILICON_ERRATUM_161600802=y +CONFIG_QCOM_FALKOR_ERRATUM_1003=y +CONFIG_QCOM_FALKOR_ERRATUM_1009=y +CONFIG_QCOM_QDF2400_ERRATUM_0065=y +CONFIG_QCOM_FALKOR_ERRATUM_E1041=y +# CONFIG_NVIDIA_CARMEL_CNP_ERRATUM is not set +CONFIG_SOCIONEXT_SYNQUACER_PREITS=y +# end of ARM errata workarounds via the alternatives framework + +CONFIG_ARM64_4K_PAGES=y +# CONFIG_ARM64_16K_PAGES is not set +# CONFIG_ARM64_64K_PAGES is not set +# CONFIG_ARM64_VA_BITS_39 is not set +CONFIG_ARM64_VA_BITS_48=y +CONFIG_ARM64_VA_BITS=48 +CONFIG_ARM64_PA_BITS_48=y +CONFIG_ARM64_PA_BITS=48 +# CONFIG_CPU_BIG_ENDIAN is not set +CONFIG_CPU_LITTLE_ENDIAN=y +CONFIG_SCHED_MC=y +CONFIG_SCHED_SMT=y +CONFIG_NR_CPUS=128 +CONFIG_HOTPLUG_CPU=y +CONFIG_NUMA=y +CONFIG_NODES_SHIFT=10 +CONFIG_USE_PERCPU_NUMA_NODE_ID=y +CONFIG_HAVE_SETUP_PER_CPU_AREA=y +CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK=y +# CONFIG_HZ_100 is not set +CONFIG_HZ_250=y +# CONFIG_HZ_300 is not set +# CONFIG_HZ_1000 is not set +CONFIG_HZ=250 +CONFIG_SCHED_HRTICK=y +CONFIG_ARCH_SPARSEMEM_ENABLE=y +CONFIG_HW_PERF_EVENTS=y +CONFIG_ARCH_HAS_FILTER_PGPROT=y +CONFIG_PARAVIRT=y +CONFIG_PARAVIRT_TIME_ACCOUNTING=y +# CONFIG_KEXEC is not set +CONFIG_KEXEC_FILE=y +# CONFIG_KEXEC_SIG is not set +# CONFIG_CRASH_DUMP is not set +CONFIG_TRANS_TABLE=y +# CONFIG_XEN is not set +CONFIG_FORCE_MAX_ZONEORDER=11 +CONFIG_UNMAP_KERNEL_AT_EL0=y +CONFIG_RODATA_FULL_DEFAULT_ENABLED=y +# CONFIG_ARM64_SW_TTBR0_PAN is not set +CONFIG_ARM64_TAGGED_ADDR_ABI=y +# CONFIG_COMPAT is not set + +# +# ARMv8.1 architectural features +# +CONFIG_ARM64_HW_AFDBM=y +CONFIG_ARM64_PAN=y +CONFIG_AS_HAS_LDAPR=y +CONFIG_AS_HAS_LSE_ATOMICS=y +CONFIG_ARM64_LSE_ATOMICS=y +CONFIG_ARM64_USE_LSE_ATOMICS=y +# end of ARMv8.1 architectural features + +# +# ARMv8.2 architectural features +# +# CONFIG_ARM64_PMEM is not set +CONFIG_ARM64_RAS_EXTN=y +CONFIG_ARM64_CNP=y +# end of ARMv8.2 architectural features + +# +# ARMv8.3 architectural features +# +CONFIG_ARM64_PTR_AUTH=y +CONFIG_ARM64_PTR_AUTH_KERNEL=y +CONFIG_CC_HAS_BRANCH_PROT_PAC_RET=y +CONFIG_CC_HAS_SIGN_RETURN_ADDRESS=y +CONFIG_AS_HAS_PAC=y +CONFIG_AS_HAS_CFI_NEGATE_RA_STATE=y +# end of ARMv8.3 architectural features + +# +# ARMv8.4 architectural features +# +CONFIG_ARM64_AMU_EXTN=y +CONFIG_AS_HAS_ARMV8_4=y +CONFIG_ARM64_TLB_RANGE=y +# end of ARMv8.4 architectural features + +# +# ARMv8.5 architectural features +# +CONFIG_AS_HAS_ARMV8_5=y +CONFIG_ARM64_BTI=y +CONFIG_CC_HAS_BRANCH_PROT_PAC_RET_BTI=y +CONFIG_ARM64_E0PD=y +CONFIG_ARCH_RANDOM=y +CONFIG_ARM64_AS_HAS_MTE=y +CONFIG_ARM64_MTE=y +# end of ARMv8.5 architectural features + +# +# ARMv8.7 architectural features +# +CONFIG_ARM64_EPAN=y +# end of ARMv8.7 architectural features + +CONFIG_ARM64_SVE=y +# CONFIG_ARM64_PSEUDO_NMI is not set +CONFIG_RELOCATABLE=y +CONFIG_RANDOMIZE_BASE=y +CONFIG_RANDOMIZE_MODULE_REGION_FULL=y +CONFIG_CC_HAVE_STACKPROTECTOR_SYSREG=y +CONFIG_STACKPROTECTOR_PER_TASK=y +# end of Kernel Features + +# +# Boot options +# +# CONFIG_ARM64_ACPI_PARKING_PROTOCOL is not set +CONFIG_CMDLINE="" +CONFIG_EFI_STUB=y +CONFIG_EFI=y +CONFIG_DMI=y +# end of Boot options + +# +# Power management options +# +# CONFIG_SUSPEND is not set +CONFIG_HIBERNATE_CALLBACKS=y +CONFIG_HIBERNATION=y +CONFIG_HIBERNATION_SNAPSHOT_DEV=y +CONFIG_PM_STD_PARTITION="" +CONFIG_PM_SLEEP=y +CONFIG_PM_SLEEP_SMP=y +# CONFIG_PM_AUTOSLEEP is not set +# CONFIG_PM_WAKELOCKS is not set +CONFIG_PM=y +# CONFIG_PM_DEBUG is not set +CONFIG_PM_CLK=y +# CONFIG_WQ_POWER_EFFICIENT_DEFAULT is not set +CONFIG_CPU_PM=y +# CONFIG_ENERGY_MODEL is not set +CONFIG_ARCH_HIBERNATION_POSSIBLE=y +CONFIG_ARCH_HIBERNATION_HEADER=y +CONFIG_ARCH_SUSPEND_POSSIBLE=y +# end of Power management options + +# +# CPU Power Management +# + +# +# CPU Idle +# +CONFIG_CPU_IDLE=y +CONFIG_CPU_IDLE_GOV_LADDER=y +CONFIG_CPU_IDLE_GOV_MENU=y +# CONFIG_CPU_IDLE_GOV_TEO is not set + +# +# ARM CPU Idle Drivers +# +# CONFIG_ARM_CPUIDLE is not set +# CONFIG_ARM_PSCI_CPUIDLE is not set +# end of ARM CPU Idle Drivers +# end of CPU Idle + +# +# CPU Frequency scaling +# +CONFIG_CPU_FREQ=y +# CONFIG_CPU_FREQ_STAT is not set +CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE=y +# CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE is not set +# CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE is not set +# CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND is not set +# CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE is not set +# CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL is not set +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y +# CONFIG_CPU_FREQ_GOV_POWERSAVE is not set +# CONFIG_CPU_FREQ_GOV_USERSPACE is not set +# CONFIG_CPU_FREQ_GOV_ONDEMAND is not set +# CONFIG_CPU_FREQ_GOV_CONSERVATIVE is not set +# CONFIG_CPU_FREQ_GOV_SCHEDUTIL is not set + +# +# CPU frequency scaling drivers +# +# CONFIG_CPUFREQ_DT is not set +# CONFIG_ACPI_CPPC_CPUFREQ is not set +# end of CPU Frequency scaling +# end of CPU Power Management + +CONFIG_ARCH_SUPPORTS_ACPI=y +CONFIG_ACPI=y +CONFIG_ACPI_GENERIC_GSI=y +CONFIG_ACPI_CCA_REQUIRED=y +# CONFIG_ACPI_DEBUGGER is not set +CONFIG_ACPI_SPCR_TABLE=y +# CONFIG_ACPI_EC_DEBUGFS is not set +# CONFIG_ACPI_AC is not set +# CONFIG_ACPI_BATTERY is not set +# CONFIG_ACPI_BUTTON is not set +# CONFIG_ACPI_TINY_POWER_BUTTON is not set +# CONFIG_ACPI_FAN is not set +# CONFIG_ACPI_TAD is not set +# CONFIG_ACPI_DOCK is not set +CONFIG_ACPI_PROCESSOR_IDLE=y +CONFIG_ACPI_MCFG=y +CONFIG_ACPI_PROCESSOR=y +CONFIG_ACPI_HOTPLUG_CPU=y +CONFIG_ACPI_THERMAL=y +CONFIG_ARCH_HAS_ACPI_TABLE_UPGRADE=y +CONFIG_ACPI_TABLE_UPGRADE=y +# CONFIG_ACPI_DEBUG is not set +# CONFIG_ACPI_PCI_SLOT is not set +CONFIG_ACPI_CONTAINER=y +CONFIG_ACPI_HOTPLUG_MEMORY=y +# CONFIG_ACPI_HED is not set +# CONFIG_ACPI_CUSTOM_METHOD is not set +# CONFIG_ACPI_BGRT is not set +CONFIG_ACPI_REDUCED_HARDWARE_ONLY=y +CONFIG_ACPI_NUMA=y +# CONFIG_ACPI_HMAT is not set +CONFIG_HAVE_ACPI_APEI=y +# CONFIG_ACPI_APEI is not set +# CONFIG_ACPI_CONFIGFS is not set +CONFIG_ACPI_IORT=y +CONFIG_ACPI_GTDT=y +CONFIG_ACPI_PPTT=y +# CONFIG_PMIC_OPREGION is not set +CONFIG_ACPI_VIOT=y +CONFIG_IRQ_BYPASS_MANAGER=y +CONFIG_VIRTUALIZATION=y +CONFIG_KVM=y +CONFIG_HAVE_KVM_IRQCHIP=y +CONFIG_HAVE_KVM_IRQFD=y +CONFIG_HAVE_KVM_IRQ_ROUTING=y +CONFIG_HAVE_KVM_EVENTFD=y +CONFIG_KVM_MMIO=y +CONFIG_HAVE_KVM_MSI=y +CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT=y +CONFIG_KVM_VFIO=y +CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL=y +CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT=y +CONFIG_HAVE_KVM_IRQ_BYPASS=y +CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE=y +CONFIG_KVM_XFER_TO_GUEST_WORK=y +# CONFIG_NVHE_EL2_DEBUG is not set +# CONFIG_ARM64_CRYPTO is not set + +# +# General architecture-dependent options +# +CONFIG_CRASH_CORE=y +CONFIG_KEXEC_CORE=y +CONFIG_JUMP_LABEL=y +# CONFIG_STATIC_KEYS_SELFTEST is not set +CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS=y +CONFIG_HAVE_KPROBES=y +CONFIG_HAVE_KRETPROBES=y +CONFIG_HAVE_FUNCTION_ERROR_INJECTION=y +CONFIG_HAVE_NMI=y +CONFIG_TRACE_IRQFLAGS_SUPPORT=y +CONFIG_HAVE_ARCH_TRACEHOOK=y +CONFIG_HAVE_DMA_CONTIGUOUS=y +CONFIG_GENERIC_SMP_IDLE_THREAD=y +CONFIG_GENERIC_IDLE_POLL_SETUP=y +CONFIG_ARCH_HAS_FORTIFY_SOURCE=y +CONFIG_ARCH_HAS_KEEPINITRD=y +CONFIG_ARCH_HAS_SET_MEMORY=y +CONFIG_ARCH_HAS_SET_DIRECT_MAP=y +CONFIG_HAVE_ARCH_THREAD_STRUCT_WHITELIST=y +CONFIG_ARCH_WANTS_NO_INSTR=y +CONFIG_HAVE_ASM_MODVERSIONS=y +CONFIG_HAVE_REGS_AND_STACK_ACCESS_API=y +CONFIG_HAVE_RSEQ=y +CONFIG_HAVE_FUNCTION_ARG_ACCESS_API=y +CONFIG_HAVE_HW_BREAKPOINT=y +CONFIG_HAVE_PERF_REGS=y +CONFIG_HAVE_PERF_USER_STACK_DUMP=y +CONFIG_HAVE_ARCH_JUMP_LABEL=y +CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE=y +CONFIG_MMU_GATHER_TABLE_FREE=y +CONFIG_MMU_GATHER_RCU_TABLE_FREE=y +CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG=y +CONFIG_HAVE_ALIGNED_STRUCT_PAGE=y +CONFIG_HAVE_CMPXCHG_LOCAL=y +CONFIG_HAVE_CMPXCHG_DOUBLE=y +CONFIG_HAVE_ARCH_SECCOMP=y +CONFIG_HAVE_ARCH_SECCOMP_FILTER=y +CONFIG_SECCOMP=y +CONFIG_SECCOMP_FILTER=y +# CONFIG_SECCOMP_CACHE_DEBUG is not set +CONFIG_HAVE_ARCH_STACKLEAK=y +CONFIG_HAVE_STACKPROTECTOR=y +CONFIG_STACKPROTECTOR=y +CONFIG_STACKPROTECTOR_STRONG=y +CONFIG_ARCH_SUPPORTS_LTO_CLANG=y +CONFIG_ARCH_SUPPORTS_LTO_CLANG_THIN=y +CONFIG_LTO_NONE=y +CONFIG_ARCH_SUPPORTS_CFI_CLANG=y +CONFIG_HAVE_CONTEXT_TRACKING=y +CONFIG_HAVE_VIRT_CPU_ACCOUNTING_GEN=y +CONFIG_HAVE_IRQ_TIME_ACCOUNTING=y +CONFIG_HAVE_MOVE_PUD=y +CONFIG_HAVE_MOVE_PMD=y +CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE=y +CONFIG_HAVE_ARCH_HUGE_VMAP=y +CONFIG_ARCH_WANT_HUGE_PMD_SHARE=y +CONFIG_MODULES_USE_ELF_RELA=y +CONFIG_ARCH_HAS_ELF_RANDOMIZE=y +CONFIG_HAVE_ARCH_MMAP_RND_BITS=y +CONFIG_ARCH_MMAP_RND_BITS=18 +CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT=y +CONFIG_CLONE_BACKWARDS=y +# CONFIG_COMPAT_32BIT_TIME is not set +CONFIG_HAVE_ARCH_VMAP_STACK=y +CONFIG_VMAP_STACK=y +CONFIG_HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET=y +# CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT is not set +CONFIG_ARCH_HAS_STRICT_KERNEL_RWX=y +CONFIG_STRICT_KERNEL_RWX=y +CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y +CONFIG_HAVE_ARCH_COMPILER_H=y +CONFIG_HAVE_ARCH_PREL32_RELOCATIONS=y +CONFIG_ARCH_USE_MEMREMAP_PROT=y +# CONFIG_LOCK_EVENT_COUNTS is not set +CONFIG_ARCH_HAS_RELR=y +CONFIG_ARCH_WANT_LD_ORPHAN_WARN=y +CONFIG_HAVE_ARCH_PFN_VALID=y +CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC=y + +# +# GCOV-based kernel profiling +# +# CONFIG_GCOV_KERNEL is not set +CONFIG_ARCH_HAS_GCOV_PROFILE_ALL=y +# end of GCOV-based kernel profiling + +CONFIG_HAVE_GCC_PLUGINS=y +# end of General architecture-dependent options + +CONFIG_RT_MUTEXES=y +CONFIG_BASE_SMALL=0 +# CONFIG_MODULES is not set +CONFIG_MODULES_TREE_LOOKUP=y +CONFIG_BLOCK=y +CONFIG_BLK_CGROUP_RWSTAT=y +CONFIG_BLK_DEV_BSG_COMMON=y +CONFIG_BLK_DEV_BSGLIB=y +CONFIG_BLK_DEV_INTEGRITY=y +CONFIG_BLK_DEV_INTEGRITY_T10=y +# CONFIG_BLK_DEV_ZONED is not set +CONFIG_BLK_DEV_THROTTLING=y +# CONFIG_BLK_DEV_THROTTLING_LOW is not set +CONFIG_BLK_WBT=y +CONFIG_BLK_WBT_MQ=y +# CONFIG_BLK_CGROUP_IOLATENCY is not set +# CONFIG_BLK_CGROUP_IOCOST is not set +# CONFIG_BLK_CGROUP_IOPRIO is not set +CONFIG_BLK_DEBUG_FS=y +# CONFIG_BLK_SED_OPAL is not set +# CONFIG_BLK_INLINE_ENCRYPTION is not set + +# +# Partition Types +# +CONFIG_PARTITION_ADVANCED=y +# CONFIG_ACORN_PARTITION is not set +# CONFIG_AIX_PARTITION is not set +# CONFIG_OSF_PARTITION is not set +# CONFIG_AMIGA_PARTITION is not set +# CONFIG_ATARI_PARTITION is not set +# CONFIG_MAC_PARTITION is not set +# CONFIG_MSDOS_PARTITION is not set +# CONFIG_LDM_PARTITION is not set +# CONFIG_SGI_PARTITION is not set +# CONFIG_ULTRIX_PARTITION is not set +# CONFIG_SUN_PARTITION is not set +# CONFIG_KARMA_PARTITION is not set +CONFIG_EFI_PARTITION=y +# CONFIG_SYSV68_PARTITION is not set +# CONFIG_CMDLINE_PARTITION is not set +# end of Partition Types + +CONFIG_BLK_MQ_PCI=y +CONFIG_BLK_MQ_VIRTIO=y +CONFIG_BLK_PM=y + +# +# IO Schedulers +# +# CONFIG_MQ_IOSCHED_DEADLINE is not set +# CONFIG_MQ_IOSCHED_KYBER is not set +# CONFIG_IOSCHED_BFQ is not set +# end of IO Schedulers + +CONFIG_PREEMPT_NOTIFIERS=y +CONFIG_ARCH_INLINE_SPIN_TRYLOCK=y +CONFIG_ARCH_INLINE_SPIN_TRYLOCK_BH=y +CONFIG_ARCH_INLINE_SPIN_LOCK=y +CONFIG_ARCH_INLINE_SPIN_LOCK_BH=y +CONFIG_ARCH_INLINE_SPIN_LOCK_IRQ=y +CONFIG_ARCH_INLINE_SPIN_LOCK_IRQSAVE=y +CONFIG_ARCH_INLINE_SPIN_UNLOCK=y +CONFIG_ARCH_INLINE_SPIN_UNLOCK_BH=y +CONFIG_ARCH_INLINE_SPIN_UNLOCK_IRQ=y +CONFIG_ARCH_INLINE_SPIN_UNLOCK_IRQRESTORE=y +CONFIG_ARCH_INLINE_READ_LOCK=y +CONFIG_ARCH_INLINE_READ_LOCK_BH=y +CONFIG_ARCH_INLINE_READ_LOCK_IRQ=y +CONFIG_ARCH_INLINE_READ_LOCK_IRQSAVE=y +CONFIG_ARCH_INLINE_READ_UNLOCK=y +CONFIG_ARCH_INLINE_READ_UNLOCK_BH=y +CONFIG_ARCH_INLINE_READ_UNLOCK_IRQ=y +CONFIG_ARCH_INLINE_READ_UNLOCK_IRQRESTORE=y +CONFIG_ARCH_INLINE_WRITE_LOCK=y +CONFIG_ARCH_INLINE_WRITE_LOCK_BH=y +CONFIG_ARCH_INLINE_WRITE_LOCK_IRQ=y +CONFIG_ARCH_INLINE_WRITE_LOCK_IRQSAVE=y +CONFIG_ARCH_INLINE_WRITE_UNLOCK=y +CONFIG_ARCH_INLINE_WRITE_UNLOCK_BH=y +CONFIG_ARCH_INLINE_WRITE_UNLOCK_IRQ=y +CONFIG_ARCH_INLINE_WRITE_UNLOCK_IRQRESTORE=y +CONFIG_INLINE_SPIN_TRYLOCK=y +CONFIG_INLINE_SPIN_TRYLOCK_BH=y +CONFIG_INLINE_SPIN_LOCK=y +CONFIG_INLINE_SPIN_LOCK_BH=y +CONFIG_INLINE_SPIN_LOCK_IRQ=y +CONFIG_INLINE_SPIN_LOCK_IRQSAVE=y +CONFIG_INLINE_SPIN_UNLOCK_BH=y +CONFIG_INLINE_SPIN_UNLOCK_IRQ=y +CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE=y +CONFIG_INLINE_READ_LOCK=y +CONFIG_INLINE_READ_LOCK_BH=y +CONFIG_INLINE_READ_LOCK_IRQ=y +CONFIG_INLINE_READ_LOCK_IRQSAVE=y +CONFIG_INLINE_READ_UNLOCK=y +CONFIG_INLINE_READ_UNLOCK_BH=y +CONFIG_INLINE_READ_UNLOCK_IRQ=y +CONFIG_INLINE_READ_UNLOCK_IRQRESTORE=y +CONFIG_INLINE_WRITE_LOCK=y +CONFIG_INLINE_WRITE_LOCK_BH=y +CONFIG_INLINE_WRITE_LOCK_IRQ=y +CONFIG_INLINE_WRITE_LOCK_IRQSAVE=y +CONFIG_INLINE_WRITE_UNLOCK=y +CONFIG_INLINE_WRITE_UNLOCK_BH=y +CONFIG_INLINE_WRITE_UNLOCK_IRQ=y +CONFIG_INLINE_WRITE_UNLOCK_IRQRESTORE=y +CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=y +CONFIG_MUTEX_SPIN_ON_OWNER=y +CONFIG_RWSEM_SPIN_ON_OWNER=y +CONFIG_LOCK_SPIN_ON_OWNER=y +CONFIG_ARCH_USE_QUEUED_SPINLOCKS=y +CONFIG_QUEUED_SPINLOCKS=y +CONFIG_ARCH_USE_QUEUED_RWLOCKS=y +CONFIG_QUEUED_RWLOCKS=y +CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE=y +CONFIG_ARCH_HAS_SYSCALL_WRAPPER=y +CONFIG_FREEZER=y + +# +# Executable file formats +# +CONFIG_BINFMT_ELF=y +CONFIG_ARCH_BINFMT_ELF_STATE=y +CONFIG_ARCH_HAVE_ELF_PROT=y +CONFIG_ARCH_USE_GNU_PROPERTY=y +CONFIG_ELFCORE=y +CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS=y +CONFIG_BINFMT_SCRIPT=y +CONFIG_BINFMT_MISC=y +CONFIG_COREDUMP=y +# end of Executable file formats + +# +# Memory Management options +# +CONFIG_SPARSEMEM=y +CONFIG_SPARSEMEM_EXTREME=y +CONFIG_SPARSEMEM_VMEMMAP_ENABLE=y +CONFIG_SPARSEMEM_VMEMMAP=y +CONFIG_HAVE_FAST_GUP=y +CONFIG_ARCH_KEEP_MEMBLOCK=y +CONFIG_NUMA_KEEP_MEMINFO=y +CONFIG_MEMORY_ISOLATION=y +CONFIG_ARCH_ENABLE_MEMORY_HOTPLUG=y +CONFIG_MEMORY_HOTPLUG=y +CONFIG_MEMORY_HOTPLUG_SPARSE=y +CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE=y +CONFIG_ARCH_ENABLE_MEMORY_HOTREMOVE=y +CONFIG_MEMORY_HOTREMOVE=y +CONFIG_MHP_MEMMAP_ON_MEMORY=y +CONFIG_SPLIT_PTLOCK_CPUS=4 +CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK=y +CONFIG_MEMORY_BALLOON=y +CONFIG_BALLOON_COMPACTION=y +CONFIG_COMPACTION=y +CONFIG_PAGE_REPORTING=y +CONFIG_MIGRATION=y +CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION=y +CONFIG_ARCH_ENABLE_THP_MIGRATION=y +CONFIG_CONTIG_ALLOC=y +CONFIG_PHYS_ADDR_T_64BIT=y +CONFIG_MMU_NOTIFIER=y +CONFIG_KSM=y +CONFIG_DEFAULT_MMAP_MIN_ADDR=4096 +CONFIG_ARCH_SUPPORTS_MEMORY_FAILURE=y +# CONFIG_MEMORY_FAILURE is not set +CONFIG_TRANSPARENT_HUGEPAGE=y +# CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS is not set +CONFIG_TRANSPARENT_HUGEPAGE_MADVISE=y +CONFIG_CLEANCACHE=y +CONFIG_FRONTSWAP=y +# CONFIG_CMA is not set +CONFIG_ZSWAP=y +# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_DEFLATE is not set +CONFIG_ZSWAP_COMPRESSOR_DEFAULT_LZO=y +# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_842 is not set +# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_LZ4 is not set +# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_LZ4HC is not set +# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_ZSTD is not set +CONFIG_ZSWAP_COMPRESSOR_DEFAULT="lzo" +CONFIG_ZSWAP_ZPOOL_DEFAULT_ZBUD=y +# CONFIG_ZSWAP_ZPOOL_DEFAULT_Z3FOLD is not set +# CONFIG_ZSWAP_ZPOOL_DEFAULT_ZSMALLOC is not set +CONFIG_ZSWAP_ZPOOL_DEFAULT="zbud" +# CONFIG_ZSWAP_DEFAULT_ON is not set +CONFIG_ZPOOL=y +CONFIG_ZBUD=y +# CONFIG_Z3FOLD is not set +CONFIG_ZSMALLOC=y +CONFIG_ZSMALLOC_STAT=y +CONFIG_GENERIC_EARLY_IOREMAP=y +# CONFIG_DEFERRED_STRUCT_PAGE_INIT is not set +# CONFIG_IDLE_PAGE_TRACKING is not set +CONFIG_ARCH_HAS_CACHE_LINE_SIZE=y +CONFIG_ARCH_HAS_PTE_DEVMAP=y +CONFIG_ARCH_HAS_ZONE_DMA_SET=y +CONFIG_ZONE_DMA=y +CONFIG_ZONE_DMA32=y +CONFIG_ZONE_DEVICE=y +CONFIG_DEV_PAGEMAP_OPS=y +# CONFIG_DEVICE_PRIVATE is not set +CONFIG_ARCH_USES_HIGH_VMA_FLAGS=y +CONFIG_PERCPU_STATS=y +# CONFIG_GUP_TEST is not set +# CONFIG_READ_ONLY_THP_FOR_FS is not set +CONFIG_ARCH_HAS_PTE_SPECIAL=y +CONFIG_SECRETMEM=y + +# +# Data Access Monitoring +# +# CONFIG_DAMON is not set +# end of Data Access Monitoring +# end of Memory Management options + +CONFIG_NET=y +CONFIG_SKB_EXTENSIONS=y + +# +# Networking options +# +CONFIG_PACKET=y +CONFIG_PACKET_DIAG=y +CONFIG_UNIX=y +CONFIG_UNIX_SCM=y +CONFIG_AF_UNIX_OOB=y +CONFIG_UNIX_DIAG=y +CONFIG_TLS=y +# CONFIG_TLS_DEVICE is not set +# CONFIG_TLS_TOE is not set +CONFIG_XFRM=y +CONFIG_XFRM_ALGO=y +CONFIG_XFRM_USER=y +CONFIG_XFRM_SUB_POLICY=y +CONFIG_XFRM_MIGRATE=y +CONFIG_XFRM_STATISTICS=y +CONFIG_NET_KEY=y +CONFIG_NET_KEY_MIGRATE=y +# CONFIG_XDP_SOCKETS is not set +CONFIG_INET=y +# CONFIG_IP_MULTICAST is not set +# CONFIG_IP_ADVANCED_ROUTER is not set +CONFIG_IP_PNP=y +CONFIG_IP_PNP_DHCP=y +CONFIG_IP_PNP_BOOTP=y +CONFIG_IP_PNP_RARP=y +# CONFIG_NET_IPIP is not set +# CONFIG_NET_IPGRE_DEMUX is not set +CONFIG_SYN_COOKIES=y +# CONFIG_NET_IPVTI is not set +# CONFIG_NET_FOU is not set +# CONFIG_INET_AH is not set +# CONFIG_INET_ESP is not set +# CONFIG_INET_IPCOMP is not set +CONFIG_INET_DIAG=y +CONFIG_INET_TCP_DIAG=y +CONFIG_INET_UDP_DIAG=y +CONFIG_INET_RAW_DIAG=y +# CONFIG_INET_DIAG_DESTROY is not set +# CONFIG_TCP_CONG_ADVANCED is not set +CONFIG_TCP_CONG_CUBIC=y +CONFIG_DEFAULT_TCP_CONG="cubic" +# CONFIG_TCP_MD5SIG is not set +# CONFIG_IPV6 is not set +# CONFIG_MPTCP is not set +# CONFIG_NETWORK_SECMARK is not set +CONFIG_NET_PTP_CLASSIFY=y +# CONFIG_NETWORK_PHY_TIMESTAMPING is not set +# CONFIG_NETFILTER is not set +# CONFIG_BPFILTER is not set +# CONFIG_IP_DCCP is not set +# CONFIG_IP_SCTP is not set +# CONFIG_RDS is not set +# CONFIG_TIPC is not set +# CONFIG_ATM is not set +# CONFIG_L2TP is not set +# CONFIG_BRIDGE is not set +# CONFIG_NET_DSA is not set +# CONFIG_VLAN_8021Q is not set +# CONFIG_DECNET is not set +# CONFIG_LLC2 is not set +# CONFIG_ATALK is not set +# CONFIG_X25 is not set +# CONFIG_LAPB is not set +# CONFIG_PHONET is not set +# CONFIG_IEEE802154 is not set +# CONFIG_NET_SCHED is not set +# CONFIG_DCB is not set +# CONFIG_DNS_RESOLVER is not set +# CONFIG_BATMAN_ADV is not set +# CONFIG_OPENVSWITCH is not set +CONFIG_VSOCKETS=y +CONFIG_VSOCKETS_DIAG=y +# CONFIG_VSOCKETS_LOOPBACK is not set +CONFIG_VIRTIO_VSOCKETS=y +CONFIG_VIRTIO_VSOCKETS_COMMON=y +# CONFIG_NETLINK_DIAG is not set +# CONFIG_MPLS is not set +# CONFIG_NET_NSH is not set +# CONFIG_HSR is not set +# CONFIG_NET_SWITCHDEV is not set +# CONFIG_NET_L3_MASTER_DEV is not set +# CONFIG_QRTR is not set +# CONFIG_NET_NCSI is not set +CONFIG_PCPU_DEV_REFCNT=y +CONFIG_RPS=y +CONFIG_RFS_ACCEL=y +CONFIG_SOCK_RX_QUEUE_MAPPING=y +CONFIG_XPS=y +CONFIG_CGROUP_NET_PRIO=y +CONFIG_CGROUP_NET_CLASSID=y +CONFIG_NET_RX_BUSY_POLL=y +CONFIG_BQL=y +# CONFIG_BPF_STREAM_PARSER is not set +CONFIG_NET_FLOW_LIMIT=y + +# +# Network testing +# +# CONFIG_NET_PKTGEN is not set +# end of Network testing +# end of Networking options + +# CONFIG_HAMRADIO is not set +# CONFIG_CAN is not set +# CONFIG_BT is not set +# CONFIG_AF_RXRPC is not set +# CONFIG_AF_KCM is not set +CONFIG_STREAM_PARSER=y +# CONFIG_MCTP is not set +# CONFIG_WIRELESS is not set +# CONFIG_RFKILL is not set +# CONFIG_NET_9P is not set +# CONFIG_CAIF is not set +# CONFIG_CEPH_LIB is not set +# CONFIG_NFC is not set +# CONFIG_PSAMPLE is not set +# CONFIG_NET_IFE is not set +# CONFIG_LWTUNNEL is not set +CONFIG_GRO_CELLS=y +CONFIG_NET_SOCK_MSG=y +CONFIG_FAILOVER=y +# CONFIG_ETHTOOL_NETLINK is not set + +# +# Device Drivers +# +CONFIG_ARM_AMBA=y +CONFIG_HAVE_PCI=y +CONFIG_PCI=y +CONFIG_PCI_DOMAINS=y +CONFIG_PCI_DOMAINS_GENERIC=y +CONFIG_PCI_SYSCALL=y +CONFIG_PCIEPORTBUS=y +# CONFIG_HOTPLUG_PCI_PCIE is not set +CONFIG_PCIEAER=y +# CONFIG_PCIEAER_INJECT is not set +# CONFIG_PCIE_ECRC is not set +CONFIG_PCIEASPM=y +CONFIG_PCIEASPM_DEFAULT=y +# CONFIG_PCIEASPM_POWERSAVE is not set +# CONFIG_PCIEASPM_POWER_SUPERSAVE is not set +# CONFIG_PCIEASPM_PERFORMANCE is not set +CONFIG_PCIE_PME=y +# CONFIG_PCIE_DPC is not set +# CONFIG_PCIE_PTM is not set +CONFIG_PCI_MSI=y +CONFIG_PCI_MSI_IRQ_DOMAIN=y +CONFIG_PCI_QUIRKS=y +CONFIG_PCI_DEBUG=y +CONFIG_PCI_STUB=y +CONFIG_PCI_ECAM=y +# CONFIG_PCI_IOV is not set +# CONFIG_PCI_PRI is not set +# CONFIG_PCI_PASID is not set +# CONFIG_PCI_P2PDMA is not set +CONFIG_PCI_LABEL=y +# CONFIG_PCIE_BUS_TUNE_OFF is not set +CONFIG_PCIE_BUS_DEFAULT=y +# CONFIG_PCIE_BUS_SAFE is not set +# CONFIG_PCIE_BUS_PERFORMANCE is not set +# CONFIG_PCIE_BUS_PEER2PEER is not set +CONFIG_HOTPLUG_PCI=y +CONFIG_HOTPLUG_PCI_ACPI=y +# CONFIG_HOTPLUG_PCI_ACPI_IBM is not set +# CONFIG_HOTPLUG_PCI_CPCI is not set +# CONFIG_HOTPLUG_PCI_SHPC is not set + +# +# PCI controller drivers +# +# CONFIG_PCI_FTPCI100 is not set +CONFIG_PCI_HOST_COMMON=y +CONFIG_PCI_HOST_GENERIC=y +# CONFIG_PCIE_XILINX is not set +# CONFIG_PCI_XGENE is not set +# CONFIG_PCIE_ALTERA is not set +# CONFIG_PCI_HOST_THUNDER_PEM is not set +# CONFIG_PCI_HOST_THUNDER_ECAM is not set +# CONFIG_PCIE_MICROCHIP_HOST is not set + +# +# DesignWare PCI Core Support +# +# CONFIG_PCIE_DW_PLAT_HOST is not set +# CONFIG_PCI_HISI is not set +# CONFIG_PCIE_KIRIN is not set +# CONFIG_PCI_MESON is not set +# CONFIG_PCIE_AL is not set +# end of DesignWare PCI Core Support + +# +# Mobiveil PCIe Core Support +# +# end of Mobiveil PCIe Core Support + +# +# Cadence PCIe controllers support +# +# CONFIG_PCIE_CADENCE_PLAT_HOST is not set +# CONFIG_PCI_J721E_HOST is not set +# end of Cadence PCIe controllers support +# end of PCI controller drivers + +# +# PCI Endpoint +# +# CONFIG_PCI_ENDPOINT is not set +# end of PCI Endpoint + +# +# PCI switch controller drivers +# +# CONFIG_PCI_SW_SWITCHTEC is not set +# end of PCI switch controller drivers + +# CONFIG_CXL_BUS is not set +# CONFIG_PCCARD is not set +# CONFIG_RAPIDIO is not set + +# +# Generic Driver Options +# +CONFIG_UEVENT_HELPER=y +CONFIG_UEVENT_HELPER_PATH="/sbin/hotplug" +CONFIG_DEVTMPFS=y +CONFIG_DEVTMPFS_MOUNT=y +CONFIG_STANDALONE=y +CONFIG_PREVENT_FIRMWARE_BUILD=y + +# +# Firmware loader +# +CONFIG_FW_LOADER=y +CONFIG_FW_LOADER_PAGED_BUF=y +CONFIG_EXTRA_FIRMWARE="" +CONFIG_FW_LOADER_USER_HELPER=y +# CONFIG_FW_LOADER_USER_HELPER_FALLBACK is not set +# CONFIG_FW_LOADER_COMPRESS is not set +# CONFIG_FW_CACHE is not set +# end of Firmware loader + +CONFIG_ALLOW_DEV_COREDUMP=y +# CONFIG_DEBUG_DRIVER is not set +# CONFIG_DEBUG_DEVRES is not set +# CONFIG_DEBUG_TEST_DRIVER_REMOVE is not set +CONFIG_GENERIC_CPU_AUTOPROBE=y +CONFIG_GENERIC_CPU_VULNERABILITIES=y +CONFIG_GENERIC_ARCH_TOPOLOGY=y +CONFIG_GENERIC_ARCH_NUMA=y +# end of Generic Driver Options + +# +# Bus devices +# +# CONFIG_BRCMSTB_GISB_ARB is not set +# CONFIG_VEXPRESS_CONFIG is not set +# CONFIG_MHI_BUS is not set +# end of Bus devices + +CONFIG_CONNECTOR=y +CONFIG_PROC_EVENTS=y + +# +# Firmware Drivers +# + +# +# ARM System Control and Management Interface Protocol +# +# CONFIG_ARM_SCMI_PROTOCOL is not set +# end of ARM System Control and Management Interface Protocol + +# CONFIG_ARM_SCPI_PROTOCOL is not set +# CONFIG_ARM_SDE_INTERFACE is not set +CONFIG_FIRMWARE_MEMMAP=y +CONFIG_DMIID=y +CONFIG_DMI_SYSFS=y +# CONFIG_FW_CFG_SYSFS is not set +CONFIG_SYSFB=y +# CONFIG_SYSFB_SIMPLEFB is not set +# CONFIG_ARM_FFA_TRANSPORT is not set +# CONFIG_GOOGLE_FIRMWARE is not set + +# +# EFI (Extensible Firmware Interface) Support +# +CONFIG_EFI_ESRT=y +CONFIG_EFI_PARAMS_FROM_FDT=y +CONFIG_EFI_RUNTIME_WRAPPERS=y +CONFIG_EFI_GENERIC_STUB=y +CONFIG_EFI_ARMSTUB_DTB_LOADER=y +CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER=y +# CONFIG_EFI_BOOTLOADER_CONTROL is not set +# CONFIG_EFI_CAPSULE_LOADER is not set +# CONFIG_EFI_TEST is not set +# CONFIG_RESET_ATTACK_MITIGATION is not set +# CONFIG_EFI_DISABLE_PCI_DMA is not set +# end of EFI (Extensible Firmware Interface) Support + +CONFIG_EFI_EARLYCON=y +# CONFIG_EFI_CUSTOM_SSDT_OVERLAYS is not set +CONFIG_ARM_PSCI_FW=y +# CONFIG_ARM_PSCI_CHECKER is not set +CONFIG_HAVE_ARM_SMCCC=y +CONFIG_HAVE_ARM_SMCCC_DISCOVERY=y +# CONFIG_ARM_SMCCC_SOC_ID is not set + +# +# Tegra firmware driver +# +# end of Tegra firmware driver +# end of Firmware Drivers + +# CONFIG_GNSS is not set +# CONFIG_MTD is not set +CONFIG_DTC=y +CONFIG_OF=y +# CONFIG_OF_UNITTEST is not set +CONFIG_OF_FLATTREE=y +CONFIG_OF_EARLY_FLATTREE=y +CONFIG_OF_KOBJ=y +CONFIG_OF_ADDRESS=y +CONFIG_OF_IRQ=y +CONFIG_OF_NET=y +CONFIG_OF_RESERVED_MEM=y +# CONFIG_OF_OVERLAY is not set +CONFIG_OF_NUMA=y +# CONFIG_PARPORT is not set +CONFIG_PNP=y +# CONFIG_PNP_DEBUG_MESSAGES is not set + +# +# Protocols +# +CONFIG_PNPACPI=y +CONFIG_BLK_DEV=y +CONFIG_BLK_DEV_NULL_BLK=y +# CONFIG_BLK_DEV_PCIESSD_MTIP32XX is not set +CONFIG_ZRAM=y +CONFIG_ZRAM_DEF_COMP_LZORLE=y +# CONFIG_ZRAM_DEF_COMP_LZO is not set +CONFIG_ZRAM_DEF_COMP="lzo-rle" +# CONFIG_ZRAM_WRITEBACK is not set +# CONFIG_ZRAM_MEMORY_TRACKING is not set +CONFIG_BLK_DEV_LOOP=y +CONFIG_BLK_DEV_LOOP_MIN_COUNT=8 +CONFIG_BLK_DEV_CRYPTOLOOP=y +# CONFIG_BLK_DEV_DRBD is not set +# CONFIG_BLK_DEV_NBD is not set +# CONFIG_BLK_DEV_SX8 is not set +CONFIG_BLK_DEV_RAM=y +CONFIG_BLK_DEV_RAM_COUNT=16 +CONFIG_BLK_DEV_RAM_SIZE=16384 +# CONFIG_CDROM_PKTCDVD is not set +# CONFIG_ATA_OVER_ETH is not set +CONFIG_VIRTIO_BLK=y +# CONFIG_BLK_DEV_RBD is not set +# CONFIG_BLK_DEV_RSXX is not set + +# +# NVME Support +# +CONFIG_NVME_CORE=y +CONFIG_BLK_DEV_NVME=y +CONFIG_NVME_MULTIPATH=y +# CONFIG_NVME_FC is not set +# CONFIG_NVME_TCP is not set +# CONFIG_NVME_TARGET is not set +# end of NVME Support + +# +# Misc devices +# +# CONFIG_DUMMY_IRQ is not set +# CONFIG_PHANTOM is not set +# CONFIG_TIFM_CORE is not set +# CONFIG_ENCLOSURE_SERVICES is not set +# CONFIG_HP_ILO is not set +# CONFIG_SRAM is not set +# CONFIG_DW_XDATA_PCIE is not set +# CONFIG_PCI_ENDPOINT_TEST is not set +# CONFIG_XILINX_SDFEC is not set +# CONFIG_C2PORT is not set + +# +# EEPROM support +# +# CONFIG_EEPROM_93CX6 is not set +# end of EEPROM support + +# CONFIG_CB710_CORE is not set + +# +# Texas Instruments shared transport line discipline +# +# CONFIG_TI_ST is not set +# end of Texas Instruments shared transport line discipline + +# +# Altera FPGA firmware download module (requires I2C) +# +# CONFIG_GENWQE is not set +# CONFIG_ECHO is not set +# CONFIG_BCM_VK is not set +# CONFIG_MISC_ALCOR_PCI is not set +# CONFIG_MISC_RTSX_PCI is not set +# CONFIG_HABANA_AI is not set +# CONFIG_UACCE is not set +# CONFIG_PVPANIC is not set +# end of Misc devices + +# +# SCSI device support +# +CONFIG_SCSI_MOD=y +# CONFIG_RAID_ATTRS is not set +# CONFIG_SCSI is not set +# end of SCSI device support + +CONFIG_HAVE_PATA_PLATFORM=y +# CONFIG_ATA is not set +CONFIG_MD=y +# CONFIG_BLK_DEV_MD is not set +# CONFIG_BCACHE is not set +CONFIG_BLK_DEV_DM_BUILTIN=y +CONFIG_BLK_DEV_DM=y +# CONFIG_DM_DEBUG is not set +# CONFIG_DM_UNSTRIPED is not set +# CONFIG_DM_CRYPT is not set +# CONFIG_DM_SNAPSHOT is not set +# CONFIG_DM_THIN_PROVISIONING is not set +# CONFIG_DM_CACHE is not set +# CONFIG_DM_WRITECACHE is not set +# CONFIG_DM_EBS is not set +# CONFIG_DM_ERA is not set +# CONFIG_DM_CLONE is not set +# CONFIG_DM_MIRROR is not set +# CONFIG_DM_RAID is not set +# CONFIG_DM_ZERO is not set +CONFIG_DM_MULTIPATH=y +# CONFIG_DM_MULTIPATH_QL is not set +# CONFIG_DM_MULTIPATH_ST is not set +# CONFIG_DM_MULTIPATH_HST is not set +# CONFIG_DM_MULTIPATH_IOA is not set +# CONFIG_DM_DELAY is not set +# CONFIG_DM_DUST is not set +# CONFIG_DM_INIT is not set +# CONFIG_DM_UEVENT is not set +# CONFIG_DM_FLAKEY is not set +# CONFIG_DM_VERITY is not set +# CONFIG_DM_SWITCH is not set +# CONFIG_DM_LOG_WRITES is not set +# CONFIG_DM_INTEGRITY is not set +# CONFIG_TARGET_CORE is not set +# CONFIG_FUSION is not set + +# +# IEEE 1394 (FireWire) support +# +# CONFIG_FIREWIRE is not set +# CONFIG_FIREWIRE_NOSY is not set +# end of IEEE 1394 (FireWire) support + +CONFIG_NETDEVICES=y +CONFIG_NET_CORE=y +# CONFIG_BONDING is not set +# CONFIG_DUMMY is not set +# CONFIG_WIREGUARD is not set +# CONFIG_EQUALIZER is not set +# CONFIG_NET_TEAM is not set +# CONFIG_MACVLAN is not set +# CONFIG_IPVLAN is not set +# CONFIG_VXLAN is not set +# CONFIG_GENEVE is not set +# CONFIG_BAREUDP is not set +# CONFIG_GTP is not set +# CONFIG_MACSEC is not set +# CONFIG_NETCONSOLE is not set +CONFIG_TUN=y +# CONFIG_TUN_VNET_CROSS_LE is not set +CONFIG_VETH=y +CONFIG_VIRTIO_NET=y +# CONFIG_NLMON is not set +# CONFIG_ARCNET is not set +# CONFIG_ETHERNET is not set +# CONFIG_FDDI is not set +# CONFIG_HIPPI is not set +# CONFIG_NET_SB1000 is not set +# CONFIG_PHYLIB is not set +# CONFIG_MDIO_DEVICE is not set + +# +# PCS device drivers +# +# end of PCS device drivers + +# CONFIG_PPP is not set +# CONFIG_SLIP is not set + +# +# Host-side USB support is needed for USB Network Adapter support +# +# CONFIG_WLAN is not set +# CONFIG_WAN is not set + +# +# Wireless WAN +# +# CONFIG_WWAN is not set +# end of Wireless WAN + +# CONFIG_VMXNET3 is not set +# CONFIG_FUJITSU_ES is not set +# CONFIG_NETDEVSIM is not set +CONFIG_NET_FAILOVER=y +# CONFIG_ISDN is not set + +# +# Input device support +# +CONFIG_INPUT=y +CONFIG_INPUT_FF_MEMLESS=y +CONFIG_INPUT_SPARSEKMAP=y +# CONFIG_INPUT_MATRIXKMAP is not set + +# +# Userland interfaces +# +CONFIG_INPUT_MOUSEDEV=y +CONFIG_INPUT_MOUSEDEV_PSAUX=y +CONFIG_INPUT_MOUSEDEV_SCREEN_X=1024 +CONFIG_INPUT_MOUSEDEV_SCREEN_Y=768 +# CONFIG_INPUT_JOYDEV is not set +CONFIG_INPUT_EVDEV=y +# CONFIG_INPUT_EVBUG is not set + +# +# Input Device Drivers +# +CONFIG_INPUT_KEYBOARD=y +# CONFIG_KEYBOARD_ATKBD is not set +# CONFIG_KEYBOARD_LKKBD is not set +CONFIG_KEYBOARD_GPIO=y +CONFIG_KEYBOARD_GPIO_POLLED=y +# CONFIG_KEYBOARD_MATRIX is not set +# CONFIG_KEYBOARD_NEWTON is not set +# CONFIG_KEYBOARD_OPENCORES is not set +# CONFIG_KEYBOARD_SAMSUNG is not set +# CONFIG_KEYBOARD_STOWAWAY is not set +# CONFIG_KEYBOARD_SUNKBD is not set +# CONFIG_KEYBOARD_OMAP4 is not set +# CONFIG_KEYBOARD_XTKBD is not set +# CONFIG_KEYBOARD_BCM is not set +# CONFIG_INPUT_MOUSE is not set +# CONFIG_INPUT_JOYSTICK is not set +# CONFIG_INPUT_TABLET is not set +# CONFIG_INPUT_TOUCHSCREEN is not set +CONFIG_INPUT_MISC=y +# CONFIG_INPUT_AD714X is not set +# CONFIG_INPUT_E3X0_BUTTON is not set +# CONFIG_INPUT_GPIO_BEEPER is not set +# CONFIG_INPUT_GPIO_DECODER is not set +# CONFIG_INPUT_GPIO_VIBRA is not set +CONFIG_INPUT_UINPUT=y +# CONFIG_INPUT_GPIO_ROTARY_ENCODER is not set +# CONFIG_INPUT_ADXL34X is not set +# CONFIG_INPUT_CMA3000 is not set +# CONFIG_INPUT_SOC_BUTTON_ARRAY is not set +# CONFIG_RMI4_CORE is not set + +# +# Hardware I/O ports +# +# CONFIG_SERIO is not set +# CONFIG_GAMEPORT is not set +# end of Hardware I/O ports +# end of Input device support + +# +# Character devices +# +CONFIG_TTY=y +CONFIG_VT=y +CONFIG_CONSOLE_TRANSLATIONS=y +CONFIG_VT_CONSOLE=y +CONFIG_VT_CONSOLE_SLEEP=y +CONFIG_HW_CONSOLE=y +CONFIG_VT_HW_CONSOLE_BINDING=y +CONFIG_UNIX98_PTYS=y +# CONFIG_LEGACY_PTYS is not set +# CONFIG_LDISC_AUTOLOAD is not set + +# +# Serial drivers +# +CONFIG_SERIAL_EARLYCON=y +CONFIG_SERIAL_8250=y +# CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set +CONFIG_SERIAL_8250_PNP=y +# CONFIG_SERIAL_8250_16550A_VARIANTS is not set +# CONFIG_SERIAL_8250_FINTEK is not set +CONFIG_SERIAL_8250_CONSOLE=y +CONFIG_SERIAL_8250_DMA=y +CONFIG_SERIAL_8250_PCI=y +CONFIG_SERIAL_8250_EXAR=y +CONFIG_SERIAL_8250_NR_UARTS=1 +CONFIG_SERIAL_8250_RUNTIME_UARTS=1 +# CONFIG_SERIAL_8250_EXTENDED is not set +CONFIG_SERIAL_8250_FSL=y +# CONFIG_SERIAL_8250_DW is not set +# CONFIG_SERIAL_8250_RT288X is not set +CONFIG_SERIAL_OF_PLATFORM=y + +# +# Non-8250 serial port support +# +# CONFIG_SERIAL_AMBA_PL010 is not set +CONFIG_SERIAL_AMBA_PL011=y +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y +# CONFIG_SERIAL_EARLYCON_ARM_SEMIHOST is not set +# CONFIG_SERIAL_UARTLITE is not set +CONFIG_SERIAL_CORE=y +CONFIG_SERIAL_CORE_CONSOLE=y +# CONFIG_SERIAL_JSM is not set +# CONFIG_SERIAL_SIFIVE is not set +# CONFIG_SERIAL_SCCNXP is not set +# CONFIG_SERIAL_BCM63XX is not set +# CONFIG_SERIAL_ALTERA_JTAGUART is not set +# CONFIG_SERIAL_ALTERA_UART is not set +# CONFIG_SERIAL_XILINX_PS_UART is not set +CONFIG_SERIAL_ARC=y +# CONFIG_SERIAL_ARC_CONSOLE is not set +CONFIG_SERIAL_ARC_NR_PORTS=1 +# CONFIG_SERIAL_RP2 is not set +# CONFIG_SERIAL_FSL_LPUART is not set +# CONFIG_SERIAL_FSL_LINFLEXUART is not set +# CONFIG_SERIAL_CONEXANT_DIGICOLOR is not set +# CONFIG_SERIAL_SPRD is not set +# end of Serial drivers + +CONFIG_SERIAL_MCTRL_GPIO=y +# CONFIG_SERIAL_NONSTANDARD is not set +# CONFIG_N_GSM is not set +# CONFIG_NOZOMI is not set +# CONFIG_NULL_TTY is not set +CONFIG_HVC_DRIVER=y +# CONFIG_HVC_DCC is not set +CONFIG_SERIAL_DEV_BUS=y +CONFIG_SERIAL_DEV_CTRL_TTYPORT=y +# CONFIG_TTY_PRINTK is not set +CONFIG_VIRTIO_CONSOLE=y +# CONFIG_IPMI_HANDLER is not set +CONFIG_HW_RANDOM=y +# CONFIG_HW_RANDOM_TIMERIOMEM is not set +# CONFIG_HW_RANDOM_BA431 is not set +CONFIG_HW_RANDOM_VIRTIO=y +CONFIG_HW_RANDOM_CAVIUM=y +# CONFIG_HW_RANDOM_CCTRNG is not set +# CONFIG_HW_RANDOM_XIPHERA is not set +CONFIG_HW_RANDOM_ARM_SMCCC_TRNG=y +# CONFIG_APPLICOM is not set +CONFIG_DEVMEM=y +CONFIG_DEVPORT=y +# CONFIG_TCG_TPM is not set +# CONFIG_XILLYBUS is not set +# CONFIG_RANDOM_TRUST_CPU is not set +# CONFIG_RANDOM_TRUST_BOOTLOADER is not set +# end of Character devices + +# +# I2C support +# +# CONFIG_I2C is not set +# end of I2C support + +# CONFIG_I3C is not set +# CONFIG_SPI is not set +# CONFIG_SPMI is not set +# CONFIG_HSI is not set +CONFIG_PPS=y +CONFIG_PPS_DEBUG=y + +# +# PPS clients support +# +CONFIG_PPS_CLIENT_KTIMER=y +CONFIG_PPS_CLIENT_LDISC=y +CONFIG_PPS_CLIENT_GPIO=y + +# +# PPS generators support +# + +# +# PTP clock support +# +CONFIG_PTP_1588_CLOCK=y +CONFIG_PTP_1588_CLOCK_OPTIONAL=y + +# +# Enable PHYLIB and NETWORK_PHY_TIMESTAMPING to see the additional clocks. +# +CONFIG_PTP_1588_CLOCK_KVM=y +# end of PTP clock support + +# CONFIG_PINCTRL is not set +CONFIG_GPIOLIB=y +CONFIG_GPIOLIB_FASTPATH_LIMIT=512 +CONFIG_OF_GPIO=y +CONFIG_GPIO_ACPI=y +CONFIG_GPIOLIB_IRQCHIP=y +# CONFIG_DEBUG_GPIO is not set +# CONFIG_GPIO_SYSFS is not set +# CONFIG_GPIO_CDEV is not set + +# +# Memory mapped GPIO drivers +# +# CONFIG_GPIO_74XX_MMIO is not set +# CONFIG_GPIO_ALTERA is not set +# CONFIG_GPIO_AMDPT is not set +# CONFIG_GPIO_CADENCE is not set +# CONFIG_GPIO_DWAPB is not set +# CONFIG_GPIO_EXAR is not set +# CONFIG_GPIO_FTGPIO010 is not set +# CONFIG_GPIO_GENERIC_PLATFORM is not set +# CONFIG_GPIO_GRGPIO is not set +# CONFIG_GPIO_HISI is not set +# CONFIG_GPIO_HLWD is not set +# CONFIG_GPIO_MB86S7X is not set +CONFIG_GPIO_PL061=y +# CONFIG_GPIO_SIFIVE is not set +# CONFIG_GPIO_XGENE is not set +# CONFIG_GPIO_XILINX is not set +# CONFIG_GPIO_AMD_FCH is not set +# end of Memory mapped GPIO drivers + +# +# MFD GPIO expanders +# +# end of MFD GPIO expanders + +# +# PCI GPIO expanders +# +# CONFIG_GPIO_BT8XX is not set +# CONFIG_GPIO_PCI_IDIO_16 is not set +# CONFIG_GPIO_PCIE_IDIO_24 is not set +# CONFIG_GPIO_RDC321X is not set +# end of PCI GPIO expanders + +# +# Virtual GPIO drivers +# +# CONFIG_GPIO_AGGREGATOR is not set +# CONFIG_GPIO_MOCKUP is not set +# CONFIG_GPIO_VIRTIO is not set +# end of Virtual GPIO drivers + +# CONFIG_W1 is not set +CONFIG_POWER_RESET=y +# CONFIG_POWER_RESET_GPIO is not set +# CONFIG_POWER_RESET_GPIO_RESTART is not set +# CONFIG_POWER_RESET_LTC2952 is not set +# CONFIG_POWER_RESET_RESTART is not set +# CONFIG_POWER_RESET_XGENE is not set +# CONFIG_POWER_RESET_SYSCON is not set +# CONFIG_POWER_RESET_SYSCON_POWEROFF is not set +# CONFIG_NVMEM_REBOOT_MODE is not set +CONFIG_POWER_SUPPLY=y +# CONFIG_POWER_SUPPLY_DEBUG is not set +# CONFIG_PDA_POWER is not set +# CONFIG_TEST_POWER is not set +# CONFIG_BATTERY_DS2780 is not set +# CONFIG_BATTERY_DS2781 is not set +# CONFIG_BATTERY_BQ27XXX is not set +# CONFIG_CHARGER_MAX8903 is not set +# CONFIG_CHARGER_GPIO is not set +# CONFIG_CHARGER_LT3651 is not set +# CONFIG_BATTERY_GOLDFISH is not set +# CONFIG_HWMON is not set +CONFIG_THERMAL=y +# CONFIG_THERMAL_NETLINK is not set +# CONFIG_THERMAL_STATISTICS is not set +CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS=0 +CONFIG_THERMAL_OF=y +CONFIG_THERMAL_WRITABLE_TRIPS=y +CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE=y +# CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE is not set +# CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE is not set +CONFIG_THERMAL_GOV_FAIR_SHARE=y +CONFIG_THERMAL_GOV_STEP_WISE=y +# CONFIG_THERMAL_GOV_BANG_BANG is not set +CONFIG_THERMAL_GOV_USER_SPACE=y +# CONFIG_CPU_THERMAL is not set +# CONFIG_THERMAL_EMULATION is not set +# CONFIG_THERMAL_MMIO is not set +CONFIG_WATCHDOG=y +CONFIG_WATCHDOG_CORE=y +# CONFIG_WATCHDOG_NOWAYOUT is not set +CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED=y +CONFIG_WATCHDOG_OPEN_TIMEOUT=0 +# CONFIG_WATCHDOG_SYSFS is not set +# CONFIG_WATCHDOG_HRTIMER_PRETIMEOUT is not set + +# +# Watchdog Pretimeout Governors +# +# CONFIG_WATCHDOG_PRETIMEOUT_GOV is not set + +# +# Watchdog Device Drivers +# +# CONFIG_SOFT_WATCHDOG is not set +# CONFIG_GPIO_WATCHDOG is not set +# CONFIG_WDAT_WDT is not set +# CONFIG_XILINX_WATCHDOG is not set +# CONFIG_ARM_SP805_WATCHDOG is not set +# CONFIG_ARM_SBSA_WATCHDOG is not set +# CONFIG_CADENCE_WATCHDOG is not set +# CONFIG_DW_WATCHDOG is not set +# CONFIG_MAX63XX_WATCHDOG is not set +# CONFIG_ARM_SMC_WATCHDOG is not set +# CONFIG_ALIM7101_WDT is not set +# CONFIG_I6300ESB_WDT is not set +# CONFIG_MEN_A21_WDT is not set +CONFIG_VIRTIO_WDT=y + +# +# PCI-based Watchdog Cards +# +# CONFIG_PCIPCWATCHDOG is not set +# CONFIG_WDTPCI is not set +CONFIG_SSB_POSSIBLE=y +# CONFIG_SSB is not set +CONFIG_BCMA_POSSIBLE=y +# CONFIG_BCMA is not set + +# +# Multifunction device drivers +# +# CONFIG_MFD_ATMEL_FLEXCOM is not set +# CONFIG_MFD_ATMEL_HLCDC is not set +# CONFIG_MFD_MADERA is not set +# CONFIG_MFD_HI6421_PMIC is not set +# CONFIG_HTC_PASIC3 is not set +# CONFIG_LPC_ICH is not set +# CONFIG_LPC_SCH is not set +# CONFIG_MFD_INTEL_PMT is not set +# CONFIG_MFD_JANZ_CMODIO is not set +# CONFIG_MFD_KEMPLD is not set +# CONFIG_MFD_MT6397 is not set +# CONFIG_MFD_RDC321X is not set +# CONFIG_MFD_SM501 is not set +# CONFIG_MFD_SYSCON is not set +# CONFIG_MFD_TI_AM335X_TSCADC is not set +# CONFIG_MFD_TQMX86 is not set +# CONFIG_MFD_VX855 is not set +# CONFIG_RAVE_SP_CORE is not set +# end of Multifunction device drivers + +# CONFIG_REGULATOR is not set +# CONFIG_RC_CORE is not set +# CONFIG_MEDIA_CEC_SUPPORT is not set +# CONFIG_MEDIA_SUPPORT is not set + +# +# Graphics support +# +CONFIG_VGA_ARB=y +CONFIG_VGA_ARB_MAX_GPUS=16 +# CONFIG_DRM is not set + +# +# ARM devices +# +# end of ARM devices + +# +# Frame buffer Devices +# +CONFIG_FB_CMDLINE=y +CONFIG_FB_NOTIFY=y +CONFIG_FB=y +# CONFIG_FIRMWARE_EDID is not set +# CONFIG_FB_FOREIGN_ENDIAN is not set +# CONFIG_FB_MODE_HELPERS is not set +# CONFIG_FB_TILEBLITTING is not set + +# +# Frame buffer hardware drivers +# +# CONFIG_FB_CIRRUS is not set +# CONFIG_FB_PM2 is not set +# CONFIG_FB_ARMCLCD is not set +# CONFIG_FB_CYBER2000 is not set +# CONFIG_FB_ASILIANT is not set +# CONFIG_FB_IMSTT is not set +# CONFIG_FB_UVESA is not set +# CONFIG_FB_EFI is not set +# CONFIG_FB_OPENCORES is not set +# CONFIG_FB_S1D13XXX is not set +# CONFIG_FB_NVIDIA is not set +# CONFIG_FB_RIVA is not set +# CONFIG_FB_I740 is not set +# CONFIG_FB_MATROX is not set +# CONFIG_FB_RADEON is not set +# CONFIG_FB_ATY128 is not set +# CONFIG_FB_ATY is not set +# CONFIG_FB_S3 is not set +# CONFIG_FB_SAVAGE is not set +# CONFIG_FB_SIS is not set +# CONFIG_FB_NEOMAGIC is not set +# CONFIG_FB_KYRO is not set +# CONFIG_FB_3DFX is not set +# CONFIG_FB_VOODOO1 is not set +# CONFIG_FB_VT8623 is not set +# CONFIG_FB_TRIDENT is not set +# CONFIG_FB_ARK is not set +# CONFIG_FB_PM3 is not set +# CONFIG_FB_CARMINE is not set +# CONFIG_FB_IBM_GXT4500 is not set +# CONFIG_FB_VIRTUAL is not set +# CONFIG_FB_METRONOME is not set +# CONFIG_FB_MB862XX is not set +# CONFIG_FB_SIMPLE is not set +# CONFIG_FB_SM712 is not set +# end of Frame buffer Devices + +# +# Backlight & LCD device support +# +CONFIG_LCD_CLASS_DEVICE=y +# CONFIG_LCD_PLATFORM is not set +CONFIG_BACKLIGHT_CLASS_DEVICE=y +# CONFIG_BACKLIGHT_KTD253 is not set +# CONFIG_BACKLIGHT_QCOM_WLED is not set +# CONFIG_BACKLIGHT_GPIO is not set +# end of Backlight & LCD device support + +# +# Console display driver support +# +CONFIG_DUMMY_CONSOLE=y +CONFIG_DUMMY_CONSOLE_COLUMNS=80 +CONFIG_DUMMY_CONSOLE_ROWS=25 +CONFIG_FRAMEBUFFER_CONSOLE=y +CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY=y +# CONFIG_FRAMEBUFFER_CONSOLE_ROTATION is not set +# CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER is not set +# end of Console display driver support + +# CONFIG_LOGO is not set +# end of Graphics support + +# CONFIG_SOUND is not set + +# +# HID support +# +CONFIG_HID=y +# CONFIG_HID_BATTERY_STRENGTH is not set +CONFIG_HIDRAW=y +CONFIG_UHID=y +CONFIG_HID_GENERIC=y + +# +# Special HID drivers +# +# CONFIG_HID_A4TECH is not set +# CONFIG_HID_ACRUX is not set +# CONFIG_HID_APPLE is not set +# CONFIG_HID_AUREAL is not set +# CONFIG_HID_BELKIN is not set +# CONFIG_HID_CHERRY is not set +# CONFIG_HID_COUGAR is not set +# CONFIG_HID_MACALLY is not set +# CONFIG_HID_CMEDIA is not set +# CONFIG_HID_CYPRESS is not set +# CONFIG_HID_DRAGONRISE is not set +# CONFIG_HID_EMS_FF is not set +# CONFIG_HID_ELECOM is not set +# CONFIG_HID_EZKEY is not set +# CONFIG_HID_GEMBIRD is not set +# CONFIG_HID_GFRM is not set +# CONFIG_HID_GLORIOUS is not set +# CONFIG_HID_VIVALDI is not set +# CONFIG_HID_KEYTOUCH is not set +# CONFIG_HID_KYE is not set +# CONFIG_HID_WALTOP is not set +# CONFIG_HID_VIEWSONIC is not set +# CONFIG_HID_GYRATION is not set +# CONFIG_HID_ICADE is not set +# CONFIG_HID_ITE is not set +# CONFIG_HID_JABRA is not set +# CONFIG_HID_TWINHAN is not set +# CONFIG_HID_KENSINGTON is not set +# CONFIG_HID_LCPOWER is not set +# CONFIG_HID_LENOVO is not set +# CONFIG_HID_MAGICMOUSE is not set +# CONFIG_HID_MALTRON is not set +# CONFIG_HID_MAYFLASH is not set +CONFIG_HID_REDRAGON=y +# CONFIG_HID_MICROSOFT is not set +# CONFIG_HID_MONTEREY is not set +# CONFIG_HID_MULTITOUCH is not set +# CONFIG_HID_NTI is not set +# CONFIG_HID_ORTEK is not set +# CONFIG_HID_PANTHERLORD is not set +# CONFIG_HID_PETALYNX is not set +# CONFIG_HID_PICOLCD is not set +# CONFIG_HID_PLANTRONICS is not set +# CONFIG_HID_PLAYSTATION is not set +# CONFIG_HID_PRIMAX is not set +# CONFIG_HID_SAITEK is not set +# CONFIG_HID_SEMITEK is not set +# CONFIG_HID_SPEEDLINK is not set +# CONFIG_HID_STEAM is not set +# CONFIG_HID_STEELSERIES is not set +# CONFIG_HID_SUNPLUS is not set +# CONFIG_HID_RMI is not set +# CONFIG_HID_GREENASIA is not set +# CONFIG_HID_SMARTJOYPLUS is not set +# CONFIG_HID_TIVO is not set +# CONFIG_HID_TOPSEED is not set +# CONFIG_HID_UDRAW_PS3 is not set +# CONFIG_HID_XINMO is not set +# CONFIG_HID_ZEROPLUS is not set +# CONFIG_HID_ZYDACRON is not set +# CONFIG_HID_SENSOR_HUB is not set +# CONFIG_HID_ALPS is not set +# end of Special HID drivers +# end of HID support + +CONFIG_USB_OHCI_LITTLE_ENDIAN=y +# CONFIG_USB_SUPPORT is not set +# CONFIG_MMC is not set +# CONFIG_MEMSTICK is not set +# CONFIG_NEW_LEDS is not set +# CONFIG_ACCESSIBILITY is not set +# CONFIG_INFINIBAND is not set +CONFIG_EDAC_SUPPORT=y +# CONFIG_EDAC is not set +CONFIG_RTC_LIB=y +CONFIG_RTC_CLASS=y +CONFIG_RTC_HCTOSYS=y +CONFIG_RTC_HCTOSYS_DEVICE="rtc0" +CONFIG_RTC_SYSTOHC=y +CONFIG_RTC_SYSTOHC_DEVICE="rtc0" +# CONFIG_RTC_DEBUG is not set +CONFIG_RTC_NVMEM=y + +# +# RTC interfaces +# +CONFIG_RTC_INTF_SYSFS=y +CONFIG_RTC_INTF_PROC=y +CONFIG_RTC_INTF_DEV=y +# CONFIG_RTC_INTF_DEV_UIE_EMUL is not set +# CONFIG_RTC_DRV_TEST is not set + +# +# I2C RTC drivers +# + +# +# SPI RTC drivers +# + +# +# SPI and I2C RTC drivers +# + +# +# Platform RTC drivers +# +# CONFIG_RTC_DRV_DS1286 is not set +# CONFIG_RTC_DRV_DS1511 is not set +# CONFIG_RTC_DRV_DS1553 is not set +# CONFIG_RTC_DRV_DS1685_FAMILY is not set +# CONFIG_RTC_DRV_DS1742 is not set +# CONFIG_RTC_DRV_DS2404 is not set +CONFIG_RTC_DRV_EFI=y +# CONFIG_RTC_DRV_STK17TA8 is not set +# CONFIG_RTC_DRV_M48T86 is not set +# CONFIG_RTC_DRV_M48T35 is not set +# CONFIG_RTC_DRV_M48T59 is not set +# CONFIG_RTC_DRV_MSM6242 is not set +# CONFIG_RTC_DRV_BQ4802 is not set +# CONFIG_RTC_DRV_RP5C01 is not set +# CONFIG_RTC_DRV_V3020 is not set +# CONFIG_RTC_DRV_ZYNQMP is not set + +# +# on-CPU RTC drivers +# +CONFIG_RTC_DRV_PL030=y +CONFIG_RTC_DRV_PL031=y +# CONFIG_RTC_DRV_CADENCE is not set +# CONFIG_RTC_DRV_FTRTC010 is not set +# CONFIG_RTC_DRV_R7301 is not set + +# +# HID Sensor RTC drivers +# +# CONFIG_RTC_DRV_GOLDFISH is not set +CONFIG_DMADEVICES=y +# CONFIG_DMADEVICES_DEBUG is not set + +# +# DMA Devices +# +CONFIG_DMA_ENGINE=y +CONFIG_DMA_ACPI=y +CONFIG_DMA_OF=y +# CONFIG_ALTERA_MSGDMA is not set +# CONFIG_AMBA_PL08X is not set +# CONFIG_DW_AXI_DMAC is not set +# CONFIG_FSL_EDMA is not set +# CONFIG_FSL_QDMA is not set +# CONFIG_HISI_DMA is not set +# CONFIG_INTEL_IDMA64 is not set +# CONFIG_MV_XOR_V2 is not set +# CONFIG_PL330_DMA is not set +# CONFIG_PLX_DMA is not set +# CONFIG_XILINX_DMA is not set +# CONFIG_XILINX_ZYNQMP_DMA is not set +# CONFIG_XILINX_ZYNQMP_DPDMA is not set +# CONFIG_QCOM_HIDMA_MGMT is not set +# CONFIG_QCOM_HIDMA is not set +# CONFIG_DW_DMAC is not set +# CONFIG_DW_DMAC_PCI is not set +# CONFIG_DW_EDMA is not set +# CONFIG_DW_EDMA_PCIE is not set +# CONFIG_SF_PDMA is not set + +# +# DMA Clients +# +# CONFIG_ASYNC_TX_DMA is not set +# CONFIG_DMATEST is not set + +# +# DMABUF options +# +# CONFIG_SYNC_FILE is not set +# CONFIG_DMABUF_HEAPS is not set +# end of DMABUF options + +# CONFIG_AUXDISPLAY is not set +CONFIG_UIO=y +# CONFIG_UIO_CIF is not set +CONFIG_UIO_PDRV_GENIRQ=y +CONFIG_UIO_DMEM_GENIRQ=y +# CONFIG_UIO_AEC is not set +# CONFIG_UIO_SERCOS3 is not set +# CONFIG_UIO_PCI_GENERIC is not set +# CONFIG_UIO_NETX is not set +# CONFIG_UIO_PRUSS is not set +# CONFIG_UIO_MF624 is not set +CONFIG_VFIO=y +CONFIG_VFIO_IOMMU_TYPE1=y +CONFIG_VFIO_VIRQFD=y +# CONFIG_VFIO_NOIOMMU is not set +CONFIG_VFIO_PCI_CORE=y +CONFIG_VFIO_PCI_MMAP=y +CONFIG_VFIO_PCI_INTX=y +CONFIG_VFIO_PCI=y +# CONFIG_VFIO_PLATFORM is not set +# CONFIG_VFIO_MDEV is not set +# CONFIG_VIRT_DRIVERS is not set +CONFIG_VIRTIO=y +CONFIG_VIRTIO_PCI_LIB=y +CONFIG_VIRTIO_MENU=y +CONFIG_VIRTIO_PCI=y +CONFIG_VIRTIO_PCI_LEGACY=y +CONFIG_VIRTIO_PMEM=y +CONFIG_VIRTIO_BALLOON=y +CONFIG_VIRTIO_MEM=y +CONFIG_VIRTIO_INPUT=y +CONFIG_VIRTIO_MMIO=y +CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES=y +# CONFIG_VDPA is not set +CONFIG_VHOST_MENU=y +# CONFIG_VHOST_NET is not set +# CONFIG_VHOST_VSOCK is not set +# CONFIG_VHOST_CROSS_ENDIAN_LEGACY is not set + +# +# Microsoft Hyper-V guest support +# +# CONFIG_HYPERV is not set +# end of Microsoft Hyper-V guest support + +# CONFIG_GREYBUS is not set +# CONFIG_COMEDI is not set +# CONFIG_STAGING is not set +# CONFIG_GOLDFISH is not set +# CONFIG_CHROME_PLATFORMS is not set +# CONFIG_MELLANOX_PLATFORM is not set +# CONFIG_SURFACE_PLATFORMS is not set +CONFIG_HAVE_CLK=y +CONFIG_HAVE_CLK_PREPARE=y +CONFIG_COMMON_CLK=y + +# +# Clock driver for ARM Reference designs +# +# CONFIG_ICST is not set +# CONFIG_CLK_SP810 is not set +# end of Clock driver for ARM Reference designs + +# CONFIG_COMMON_CLK_AXI_CLKGEN is not set +# CONFIG_COMMON_CLK_XGENE is not set +# CONFIG_COMMON_CLK_FIXED_MMIO is not set +# CONFIG_XILINX_VCU is not set +# CONFIG_HWSPINLOCK is not set + +# +# Clock Source drivers +# +CONFIG_TIMER_OF=y +CONFIG_TIMER_ACPI=y +CONFIG_TIMER_PROBE=y +CONFIG_ARM_ARCH_TIMER=y +CONFIG_ARM_ARCH_TIMER_EVTSTREAM=y +CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND=y +CONFIG_FSL_ERRATUM_A008585=y +CONFIG_HISILICON_ERRATUM_161010101=y +CONFIG_ARM64_ERRATUM_858921=y +# CONFIG_MICROCHIP_PIT64B is not set +# end of Clock Source drivers + +CONFIG_MAILBOX=y +# CONFIG_ARM_MHU is not set +# CONFIG_ARM_MHU_V2 is not set +# CONFIG_PLATFORM_MHU is not set +# CONFIG_PL320_MBOX is not set +CONFIG_PCC=y +# CONFIG_ALTERA_MBOX is not set +# CONFIG_MAILBOX_TEST is not set +CONFIG_IOMMU_IOVA=y +CONFIG_IOMMU_API=y +CONFIG_IOMMU_SUPPORT=y + +# +# Generic IOMMU Pagetable Support +# +# CONFIG_IOMMU_IO_PGTABLE_LPAE is not set +# CONFIG_IOMMU_IO_PGTABLE_ARMV7S is not set +# end of Generic IOMMU Pagetable Support + +# CONFIG_IOMMU_DEBUGFS is not set +CONFIG_IOMMU_DEFAULT_DMA_STRICT=y +# CONFIG_IOMMU_DEFAULT_DMA_LAZY is not set +# CONFIG_IOMMU_DEFAULT_PASSTHROUGH is not set +CONFIG_OF_IOMMU=y +CONFIG_IOMMU_DMA=y +# CONFIG_ARM_SMMU is not set +# CONFIG_ARM_SMMU_V3 is not set +CONFIG_VIRTIO_IOMMU=y + +# +# Remoteproc drivers +# +# CONFIG_REMOTEPROC is not set +# end of Remoteproc drivers + +# +# Rpmsg drivers +# +# CONFIG_RPMSG_QCOM_GLINK_RPM is not set +# CONFIG_RPMSG_VIRTIO is not set +# end of Rpmsg drivers + +# CONFIG_SOUNDWIRE is not set + +# +# SOC (System On Chip) specific Drivers +# + +# +# Amlogic SoC drivers +# +# end of Amlogic SoC drivers + +# +# Broadcom SoC drivers +# +# CONFIG_SOC_BRCMSTB is not set +# end of Broadcom SoC drivers + +# +# NXP/Freescale QorIQ SoC drivers +# +# CONFIG_QUICC_ENGINE is not set +# CONFIG_FSL_RCPM is not set +# end of NXP/Freescale QorIQ SoC drivers + +# +# i.MX SoC drivers +# +# end of i.MX SoC drivers + +# +# Enable LiteX SoC Builder specific drivers +# +# CONFIG_LITEX_SOC_CONTROLLER is not set +# end of Enable LiteX SoC Builder specific drivers + +# +# Qualcomm SoC drivers +# +# end of Qualcomm SoC drivers + +# CONFIG_SOC_TI is not set + +# +# Xilinx SoC drivers +# +# end of Xilinx SoC drivers +# end of SOC (System On Chip) specific Drivers + +# CONFIG_PM_DEVFREQ is not set +# CONFIG_EXTCON is not set +# CONFIG_MEMORY is not set +# CONFIG_IIO is not set +# CONFIG_NTB is not set +# CONFIG_VME_BUS is not set +# CONFIG_PWM is not set + +# +# IRQ chip support +# +CONFIG_IRQCHIP=y +CONFIG_ARM_GIC=y +CONFIG_ARM_GIC_MAX_NR=1 +CONFIG_ARM_GIC_V2M=y +CONFIG_ARM_GIC_V3=y +CONFIG_ARM_GIC_V3_ITS=y +CONFIG_ARM_GIC_V3_ITS_PCI=y +# CONFIG_AL_FIC is not set +CONFIG_PARTITION_PERCPU=y +# end of IRQ chip support + +# CONFIG_IPACK_BUS is not set +# CONFIG_RESET_CONTROLLER is not set + +# +# PHY Subsystem +# +# CONFIG_GENERIC_PHY is not set +# CONFIG_PHY_XGENE is not set +# CONFIG_PHY_CAN_TRANSCEIVER is not set +# CONFIG_BCM_KONA_USB2_PHY is not set +# CONFIG_PHY_CADENCE_TORRENT is not set +# CONFIG_PHY_CADENCE_DPHY is not set +# CONFIG_PHY_CADENCE_SALVO is not set +# CONFIG_PHY_FSL_IMX8MQ_USB is not set +# CONFIG_PHY_MIXEL_MIPI_DPHY is not set +# CONFIG_PHY_PXA_28NM_HSIC is not set +# CONFIG_PHY_PXA_28NM_USB2 is not set +# end of PHY Subsystem + +# CONFIG_POWERCAP is not set +# CONFIG_MCB is not set + +# +# Performance monitor support +# +# CONFIG_ARM_CCI_PMU is not set +# CONFIG_ARM_CCN is not set +# CONFIG_ARM_CMN is not set +CONFIG_ARM_PMU=y +CONFIG_ARM_PMU_ACPI=y +# CONFIG_ARM_SMMU_V3_PMU is not set +# CONFIG_ARM_DSU_PMU is not set +# CONFIG_ARM_SPE_PMU is not set +# CONFIG_ARM_DMC620_PMU is not set +# CONFIG_HISI_PMU is not set +# end of Performance monitor support + +CONFIG_RAS=y +# CONFIG_USB4 is not set + +# +# Android +# +# CONFIG_ANDROID is not set +# end of Android + +CONFIG_LIBNVDIMM=y +CONFIG_BLK_DEV_PMEM=y +CONFIG_ND_BLK=y +CONFIG_ND_CLAIM=y +CONFIG_ND_BTT=y +CONFIG_BTT=y +CONFIG_ND_PFN=y +CONFIG_NVDIMM_PFN=y +CONFIG_NVDIMM_DAX=y +CONFIG_OF_PMEM=y +CONFIG_DAX_DRIVER=y +CONFIG_DAX=y +CONFIG_DEV_DAX=y +CONFIG_DEV_DAX_PMEM=y +CONFIG_DEV_DAX_KMEM=y +CONFIG_NVMEM=y +CONFIG_NVMEM_SYSFS=y +# CONFIG_NVMEM_RMEM is not set + +# +# HW tracing support +# +# CONFIG_STM is not set +# CONFIG_INTEL_TH is not set +# end of HW tracing support + +# CONFIG_FPGA is not set +# CONFIG_FSI is not set +# CONFIG_TEE is not set +# CONFIG_SIOX is not set +# CONFIG_SLIMBUS is not set +# CONFIG_INTERCONNECT is not set +# CONFIG_COUNTER is not set +# CONFIG_MOST is not set +# end of Device Drivers + +# +# File systems +# +CONFIG_DCACHE_WORD_ACCESS=y +# CONFIG_VALIDATE_FS_PARSER is not set +CONFIG_FS_IOMAP=y +# CONFIG_EXT2_FS is not set +# CONFIG_EXT3_FS is not set +CONFIG_EXT4_FS=y +CONFIG_EXT4_USE_FOR_EXT2=y +CONFIG_EXT4_FS_POSIX_ACL=y +CONFIG_EXT4_FS_SECURITY=y +CONFIG_EXT4_DEBUG=y +CONFIG_JBD2=y +CONFIG_JBD2_DEBUG=y +CONFIG_FS_MBCACHE=y +# CONFIG_REISERFS_FS is not set +# CONFIG_JFS_FS is not set +# CONFIG_XFS_FS is not set +# CONFIG_GFS2_FS is not set +# CONFIG_OCFS2_FS is not set +# CONFIG_BTRFS_FS is not set +# CONFIG_NILFS2_FS is not set +# CONFIG_F2FS_FS is not set +CONFIG_FS_DAX=y +CONFIG_FS_DAX_PMD=y +CONFIG_FS_POSIX_ACL=y +CONFIG_EXPORTFS=y +# CONFIG_EXPORTFS_BLOCK_OPS is not set +CONFIG_FILE_LOCKING=y +CONFIG_FS_ENCRYPTION=y +CONFIG_FS_ENCRYPTION_ALGS=y +# CONFIG_FS_VERITY is not set +CONFIG_FSNOTIFY=y +CONFIG_DNOTIFY=y +CONFIG_INOTIFY_USER=y +CONFIG_FANOTIFY=y +# CONFIG_QUOTA is not set +CONFIG_AUTOFS4_FS=y +CONFIG_AUTOFS_FS=y +CONFIG_FUSE_FS=y +CONFIG_CUSE=y +CONFIG_VIRTIO_FS=y +CONFIG_FUSE_DAX=y +CONFIG_OVERLAY_FS=y +# CONFIG_OVERLAY_FS_REDIRECT_DIR is not set +CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW=y +# CONFIG_OVERLAY_FS_INDEX is not set +# CONFIG_OVERLAY_FS_XINO_AUTO is not set +# CONFIG_OVERLAY_FS_METACOPY is not set + +# +# Caches +# +CONFIG_NETFS_SUPPORT=y +# CONFIG_NETFS_STATS is not set +CONFIG_FSCACHE=y +# CONFIG_FSCACHE_STATS is not set +# CONFIG_FSCACHE_DEBUG is not set +CONFIG_CACHEFILES=y +# CONFIG_CACHEFILES_DEBUG is not set +# end of Caches + +# +# CD-ROM/DVD Filesystems +# +# CONFIG_ISO9660_FS is not set +# CONFIG_UDF_FS is not set +# end of CD-ROM/DVD Filesystems + +# +# DOS/FAT/EXFAT/NT Filesystems +# +CONFIG_FAT_FS=y +CONFIG_MSDOS_FS=y +CONFIG_VFAT_FS=y +CONFIG_FAT_DEFAULT_CODEPAGE=437 +CONFIG_FAT_DEFAULT_IOCHARSET="ascii" +# CONFIG_FAT_DEFAULT_UTF8 is not set +# CONFIG_EXFAT_FS is not set +# CONFIG_NTFS_FS is not set +# CONFIG_NTFS3_FS is not set +# end of DOS/FAT/EXFAT/NT Filesystems + +# +# Pseudo filesystems +# +CONFIG_PROC_FS=y +CONFIG_PROC_KCORE=y +CONFIG_PROC_SYSCTL=y +CONFIG_PROC_PAGE_MONITOR=y +CONFIG_PROC_CHILDREN=y +CONFIG_KERNFS=y +CONFIG_SYSFS=y +CONFIG_TMPFS=y +CONFIG_TMPFS_POSIX_ACL=y +CONFIG_TMPFS_XATTR=y +# CONFIG_TMPFS_INODE64 is not set +CONFIG_ARCH_SUPPORTS_HUGETLBFS=y +CONFIG_HUGETLBFS=y +CONFIG_HUGETLB_PAGE=y +CONFIG_MEMFD_CREATE=y +CONFIG_ARCH_HAS_GIGANTIC_PAGE=y +CONFIG_CONFIGFS_FS=y +CONFIG_EFIVAR_FS=y +# end of Pseudo filesystems + +CONFIG_MISC_FILESYSTEMS=y +# CONFIG_ORANGEFS_FS is not set +# CONFIG_ADFS_FS is not set +# CONFIG_AFFS_FS is not set +# CONFIG_ECRYPT_FS is not set +# CONFIG_HFS_FS is not set +# CONFIG_HFSPLUS_FS is not set +# CONFIG_BEFS_FS is not set +# CONFIG_BFS_FS is not set +# CONFIG_EFS_FS is not set +# CONFIG_CRAMFS is not set +CONFIG_SQUASHFS=y +CONFIG_SQUASHFS_FILE_CACHE=y +# CONFIG_SQUASHFS_FILE_DIRECT is not set +CONFIG_SQUASHFS_DECOMP_SINGLE=y +# CONFIG_SQUASHFS_DECOMP_MULTI is not set +# CONFIG_SQUASHFS_DECOMP_MULTI_PERCPU is not set +# CONFIG_SQUASHFS_XATTR is not set +CONFIG_SQUASHFS_ZLIB=y +# CONFIG_SQUASHFS_LZ4 is not set +# CONFIG_SQUASHFS_LZO is not set +CONFIG_SQUASHFS_XZ=y +# CONFIG_SQUASHFS_ZSTD is not set +# CONFIG_SQUASHFS_4K_DEVBLK_SIZE is not set +# CONFIG_SQUASHFS_EMBEDDED is not set +CONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE=3 +# CONFIG_VXFS_FS is not set +# CONFIG_MINIX_FS is not set +# CONFIG_OMFS_FS is not set +# CONFIG_HPFS_FS is not set +# CONFIG_QNX4FS_FS is not set +# CONFIG_QNX6FS_FS is not set +# CONFIG_ROMFS_FS is not set +# CONFIG_PSTORE is not set +# CONFIG_SYSV_FS is not set +# CONFIG_UFS_FS is not set +# CONFIG_EROFS_FS is not set +# CONFIG_NETWORK_FILESYSTEMS is not set +CONFIG_NLS=y +CONFIG_NLS_DEFAULT="utf8" +CONFIG_NLS_CODEPAGE_437=y +CONFIG_NLS_CODEPAGE_737=y +CONFIG_NLS_CODEPAGE_775=y +CONFIG_NLS_CODEPAGE_850=y +CONFIG_NLS_CODEPAGE_852=y +CONFIG_NLS_CODEPAGE_855=y +CONFIG_NLS_CODEPAGE_857=y +CONFIG_NLS_CODEPAGE_860=y +CONFIG_NLS_CODEPAGE_861=y +CONFIG_NLS_CODEPAGE_862=y +CONFIG_NLS_CODEPAGE_863=y +CONFIG_NLS_CODEPAGE_864=y +CONFIG_NLS_CODEPAGE_865=y +CONFIG_NLS_CODEPAGE_866=y +CONFIG_NLS_CODEPAGE_869=y +CONFIG_NLS_CODEPAGE_936=y +CONFIG_NLS_CODEPAGE_950=y +CONFIG_NLS_CODEPAGE_932=y +CONFIG_NLS_CODEPAGE_949=y +CONFIG_NLS_CODEPAGE_874=y +CONFIG_NLS_ISO8859_8=y +CONFIG_NLS_CODEPAGE_1250=y +CONFIG_NLS_CODEPAGE_1251=y +CONFIG_NLS_ASCII=y +CONFIG_NLS_ISO8859_1=y +CONFIG_NLS_ISO8859_2=y +CONFIG_NLS_ISO8859_3=y +CONFIG_NLS_ISO8859_4=y +CONFIG_NLS_ISO8859_5=y +CONFIG_NLS_ISO8859_6=y +CONFIG_NLS_ISO8859_7=y +CONFIG_NLS_ISO8859_9=y +CONFIG_NLS_ISO8859_13=y +CONFIG_NLS_ISO8859_14=y +CONFIG_NLS_ISO8859_15=y +CONFIG_NLS_KOI8_R=y +CONFIG_NLS_KOI8_U=y +CONFIG_NLS_MAC_ROMAN=y +CONFIG_NLS_MAC_CELTIC=y +CONFIG_NLS_MAC_CENTEURO=y +CONFIG_NLS_MAC_CROATIAN=y +CONFIG_NLS_MAC_CYRILLIC=y +CONFIG_NLS_MAC_GAELIC=y +CONFIG_NLS_MAC_GREEK=y +CONFIG_NLS_MAC_ICELAND=y +CONFIG_NLS_MAC_INUIT=y +CONFIG_NLS_MAC_ROMANIAN=y +CONFIG_NLS_MAC_TURKISH=y +CONFIG_NLS_UTF8=y +# CONFIG_DLM is not set +# CONFIG_UNICODE is not set +CONFIG_IO_WQ=y +# end of File systems + +# +# Security options +# +CONFIG_KEYS=y +# CONFIG_KEYS_REQUEST_CACHE is not set +CONFIG_PERSISTENT_KEYRINGS=y +# CONFIG_ENCRYPTED_KEYS is not set +# CONFIG_KEY_DH_OPERATIONS is not set +# CONFIG_SECURITY_DMESG_RESTRICT is not set +# CONFIG_SECURITY is not set +CONFIG_SECURITYFS=y +CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR=y +# CONFIG_HARDENED_USERCOPY is not set +CONFIG_FORTIFY_SOURCE=y +# CONFIG_STATIC_USERMODEHELPER is not set +# CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT is not set +CONFIG_DEFAULT_SECURITY_DAC=y +CONFIG_LSM="yama,loadpin,safesetid,integrity" + +# +# Kernel hardening options +# + +# +# Memory initialization +# +CONFIG_INIT_STACK_NONE=y +# CONFIG_INIT_ON_ALLOC_DEFAULT_ON is not set +# CONFIG_INIT_ON_FREE_DEFAULT_ON is not set +# end of Memory initialization +# end of Kernel hardening options +# end of Security options + +CONFIG_CRYPTO=y + +# +# Crypto core or helper +# +# CONFIG_CRYPTO_FIPS is not set +CONFIG_CRYPTO_ALGAPI=y +CONFIG_CRYPTO_ALGAPI2=y +CONFIG_CRYPTO_AEAD=y +CONFIG_CRYPTO_AEAD2=y +CONFIG_CRYPTO_SKCIPHER=y +CONFIG_CRYPTO_SKCIPHER2=y +CONFIG_CRYPTO_HASH=y +CONFIG_CRYPTO_HASH2=y +CONFIG_CRYPTO_RNG=y +CONFIG_CRYPTO_RNG2=y +CONFIG_CRYPTO_RNG_DEFAULT=y +CONFIG_CRYPTO_AKCIPHER2=y +CONFIG_CRYPTO_KPP2=y +CONFIG_CRYPTO_ACOMP2=y +CONFIG_CRYPTO_MANAGER=y +CONFIG_CRYPTO_MANAGER2=y +# CONFIG_CRYPTO_USER is not set +# CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is not set +# CONFIG_CRYPTO_MANAGER_EXTRA_TESTS is not set +CONFIG_CRYPTO_GF128MUL=y +CONFIG_CRYPTO_NULL=y +CONFIG_CRYPTO_NULL2=y +# CONFIG_CRYPTO_PCRYPT is not set +CONFIG_CRYPTO_CRYPTD=y +# CONFIG_CRYPTO_AUTHENC is not set +# CONFIG_CRYPTO_TEST is not set + +# +# Public-key cryptography +# +# CONFIG_CRYPTO_RSA is not set +# CONFIG_CRYPTO_DH is not set +# CONFIG_CRYPTO_ECDH is not set +# CONFIG_CRYPTO_ECDSA is not set +# CONFIG_CRYPTO_ECRDSA is not set +# CONFIG_CRYPTO_SM2 is not set +# CONFIG_CRYPTO_CURVE25519 is not set + +# +# Authenticated Encryption with Associated Data +# +# CONFIG_CRYPTO_CCM is not set +CONFIG_CRYPTO_GCM=y +# CONFIG_CRYPTO_CHACHA20POLY1305 is not set +# CONFIG_CRYPTO_AEGIS128 is not set +CONFIG_CRYPTO_SEQIV=y +CONFIG_CRYPTO_ECHAINIV=y + +# +# Block modes +# +CONFIG_CRYPTO_CBC=y +# CONFIG_CRYPTO_CFB is not set +CONFIG_CRYPTO_CTR=y +CONFIG_CRYPTO_CTS=y +CONFIG_CRYPTO_ECB=y +CONFIG_CRYPTO_LRW=y +# CONFIG_CRYPTO_OFB is not set +CONFIG_CRYPTO_PCBC=y +CONFIG_CRYPTO_XTS=y +# CONFIG_CRYPTO_KEYWRAP is not set +# CONFIG_CRYPTO_ADIANTUM is not set +# CONFIG_CRYPTO_ESSIV is not set + +# +# Hash modes +# +CONFIG_CRYPTO_CMAC=y +CONFIG_CRYPTO_HMAC=y +# CONFIG_CRYPTO_XCBC is not set +# CONFIG_CRYPTO_VMAC is not set + +# +# Digest +# +CONFIG_CRYPTO_CRC32C=y +# CONFIG_CRYPTO_CRC32 is not set +# CONFIG_CRYPTO_XXHASH is not set +# CONFIG_CRYPTO_BLAKE2B is not set +# CONFIG_CRYPTO_BLAKE2S is not set +CONFIG_CRYPTO_CRCT10DIF=y +CONFIG_CRYPTO_GHASH=y +CONFIG_CRYPTO_POLY1305=y +# CONFIG_CRYPTO_MD4 is not set +CONFIG_CRYPTO_MD5=y +# CONFIG_CRYPTO_MICHAEL_MIC is not set +# CONFIG_CRYPTO_RMD160 is not set +# CONFIG_CRYPTO_SHA1 is not set +CONFIG_CRYPTO_SHA256=y +CONFIG_CRYPTO_SHA512=y +# CONFIG_CRYPTO_SHA3 is not set +# CONFIG_CRYPTO_SM3 is not set +# CONFIG_CRYPTO_STREEBOG is not set +# CONFIG_CRYPTO_WP512 is not set + +# +# Ciphers +# +CONFIG_CRYPTO_AES=y +# CONFIG_CRYPTO_AES_TI is not set +# CONFIG_CRYPTO_BLOWFISH is not set +# CONFIG_CRYPTO_CAMELLIA is not set +# CONFIG_CRYPTO_CAST5 is not set +# CONFIG_CRYPTO_CAST6 is not set +CONFIG_CRYPTO_DES=y +# CONFIG_CRYPTO_FCRYPT is not set +# CONFIG_CRYPTO_CHACHA20 is not set +# CONFIG_CRYPTO_SERPENT is not set +# CONFIG_CRYPTO_SM4 is not set +# CONFIG_CRYPTO_TWOFISH is not set + +# +# Compression +# +CONFIG_CRYPTO_DEFLATE=y +CONFIG_CRYPTO_LZO=y +# CONFIG_CRYPTO_842 is not set +# CONFIG_CRYPTO_LZ4 is not set +# CONFIG_CRYPTO_LZ4HC is not set +# CONFIG_CRYPTO_ZSTD is not set + +# +# Random Number Generation +# +# CONFIG_CRYPTO_ANSI_CPRNG is not set +CONFIG_CRYPTO_DRBG_MENU=y +CONFIG_CRYPTO_DRBG_HMAC=y +CONFIG_CRYPTO_DRBG_HASH=y +CONFIG_CRYPTO_DRBG_CTR=y +CONFIG_CRYPTO_DRBG=y +CONFIG_CRYPTO_JITTERENTROPY=y +CONFIG_CRYPTO_USER_API=y +# CONFIG_CRYPTO_USER_API_HASH is not set +# CONFIG_CRYPTO_USER_API_SKCIPHER is not set +CONFIG_CRYPTO_USER_API_RNG=y +# CONFIG_CRYPTO_USER_API_RNG_CAVP is not set +# CONFIG_CRYPTO_USER_API_AEAD is not set +# CONFIG_CRYPTO_USER_API_ENABLE_OBSOLETE is not set + +# +# Crypto library routines +# +CONFIG_CRYPTO_LIB_AES=y +# CONFIG_CRYPTO_LIB_BLAKE2S is not set +# CONFIG_CRYPTO_LIB_CHACHA is not set +# CONFIG_CRYPTO_LIB_CURVE25519 is not set +CONFIG_CRYPTO_LIB_DES=y +CONFIG_CRYPTO_LIB_POLY1305_RSIZE=9 +CONFIG_CRYPTO_LIB_POLY1305_GENERIC=y +# CONFIG_CRYPTO_LIB_POLY1305 is not set +# CONFIG_CRYPTO_LIB_CHACHA20POLY1305 is not set +CONFIG_CRYPTO_LIB_SHA256=y +# CONFIG_CRYPTO_HW is not set +# CONFIG_ASYMMETRIC_KEY_TYPE is not set + +# +# Certificates for signature checking +# +CONFIG_SYSTEM_BLACKLIST_KEYRING=y +CONFIG_SYSTEM_BLACKLIST_HASH_LIST="" +# end of Certificates for signature checking + +CONFIG_BINARY_PRINTF=y + +# +# Library routines +# +# CONFIG_PACKING is not set +CONFIG_BITREVERSE=y +CONFIG_HAVE_ARCH_BITREVERSE=y +CONFIG_GENERIC_STRNCPY_FROM_USER=y +CONFIG_GENERIC_STRNLEN_USER=y +CONFIG_GENERIC_NET_UTILS=y +CONFIG_GENERIC_FIND_FIRST_BIT=y +# CONFIG_CORDIC is not set +# CONFIG_PRIME_NUMBERS is not set +CONFIG_RATIONAL=y +CONFIG_GENERIC_PCI_IOMAP=y +CONFIG_ARCH_USE_CMPXCHG_LOCKREF=y +CONFIG_ARCH_HAS_FAST_MULTIPLIER=y +CONFIG_ARCH_USE_SYM_ANNOTATIONS=y +# CONFIG_INDIRECT_PIO is not set +CONFIG_CRC_CCITT=y +CONFIG_CRC16=y +CONFIG_CRC_T10DIF=y +# CONFIG_CRC_ITU_T is not set +CONFIG_CRC32=y +# CONFIG_CRC32_SELFTEST is not set +CONFIG_CRC32_SLICEBY8=y +# CONFIG_CRC32_SLICEBY4 is not set +# CONFIG_CRC32_SARWATE is not set +# CONFIG_CRC32_BIT is not set +# CONFIG_CRC64 is not set +# CONFIG_CRC4 is not set +# CONFIG_CRC7 is not set +CONFIG_LIBCRC32C=y +# CONFIG_CRC8 is not set +CONFIG_XXHASH=y +CONFIG_AUDIT_GENERIC=y +CONFIG_AUDIT_ARCH_COMPAT_GENERIC=y +# CONFIG_RANDOM32_SELFTEST is not set +CONFIG_ZLIB_INFLATE=y +CONFIG_ZLIB_DEFLATE=y +CONFIG_LZO_COMPRESS=y +CONFIG_LZO_DECOMPRESS=y +CONFIG_LZ4_DECOMPRESS=y +CONFIG_XZ_DEC=y +CONFIG_XZ_DEC_X86=y +CONFIG_XZ_DEC_POWERPC=y +CONFIG_XZ_DEC_IA64=y +CONFIG_XZ_DEC_ARM=y +CONFIG_XZ_DEC_ARMTHUMB=y +CONFIG_XZ_DEC_SPARC=y +CONFIG_XZ_DEC_BCJ=y +# CONFIG_XZ_DEC_TEST is not set +CONFIG_DECOMPRESS_GZIP=y +CONFIG_DECOMPRESS_BZIP2=y +CONFIG_DECOMPRESS_LZMA=y +CONFIG_DECOMPRESS_XZ=y +CONFIG_DECOMPRESS_LZO=y +CONFIG_DECOMPRESS_LZ4=y +CONFIG_GENERIC_ALLOCATOR=y +CONFIG_INTERVAL_TREE=y +CONFIG_XARRAY_MULTI=y +CONFIG_ASSOCIATIVE_ARRAY=y +CONFIG_HAS_IOMEM=y +CONFIG_HAS_IOPORT_MAP=y +CONFIG_HAS_DMA=y +CONFIG_DMA_OPS=y +CONFIG_NEED_SG_DMA_LENGTH=y +CONFIG_NEED_DMA_MAP_STATE=y +CONFIG_ARCH_DMA_ADDR_T_64BIT=y +CONFIG_DMA_DECLARE_COHERENT=y +CONFIG_ARCH_HAS_SETUP_DMA_OPS=y +CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS=y +CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE=y +CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU=y +CONFIG_ARCH_HAS_DMA_PREP_COHERENT=y +CONFIG_SWIOTLB=y +# CONFIG_DMA_RESTRICTED_POOL is not set +CONFIG_DMA_NONCOHERENT_MMAP=y +CONFIG_DMA_COHERENT_POOL=y +CONFIG_DMA_REMAP=y +CONFIG_DMA_DIRECT_REMAP=y +# CONFIG_DMA_API_DEBUG is not set +# CONFIG_DMA_MAP_BENCHMARK is not set +CONFIG_SGL_ALLOC=y +CONFIG_CPU_RMAP=y +CONFIG_DQL=y +CONFIG_NLATTR=y +CONFIG_IRQ_POLL=y +CONFIG_LIBFDT=y +CONFIG_UCS2_STRING=y +CONFIG_HAVE_GENERIC_VDSO=y +CONFIG_GENERIC_GETTIMEOFDAY=y +CONFIG_GENERIC_VDSO_TIME_NS=y +CONFIG_FONT_SUPPORT=y +# CONFIG_FONTS is not set +CONFIG_FONT_8x8=y +CONFIG_FONT_8x16=y +CONFIG_MEMREGION=y +CONFIG_ARCH_STACKWALK=y +CONFIG_SBITMAP=y +# end of Library routines + +CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED=y + +# +# Kernel hacking +# + +# +# printk and dmesg options +# +CONFIG_PRINTK_TIME=y +# CONFIG_PRINTK_CALLER is not set +# CONFIG_STACKTRACE_BUILD_ID is not set +CONFIG_CONSOLE_LOGLEVEL_DEFAULT=7 +CONFIG_CONSOLE_LOGLEVEL_QUIET=4 +CONFIG_MESSAGE_LOGLEVEL_DEFAULT=4 +# CONFIG_BOOT_PRINTK_DELAY is not set +CONFIG_DYNAMIC_DEBUG=y +CONFIG_DYNAMIC_DEBUG_CORE=y +CONFIG_SYMBOLIC_ERRNAME=y +# CONFIG_DEBUG_BUGVERBOSE is not set +# end of printk and dmesg options + +# +# Compile-time checks and compiler options +# +# CONFIG_DEBUG_INFO is not set +CONFIG_FRAME_WARN=2048 +CONFIG_STRIP_ASM_SYMS=y +# CONFIG_READABLE_ASM is not set +# CONFIG_HEADERS_INSTALL is not set +CONFIG_DEBUG_SECTION_MISMATCH=y +CONFIG_SECTION_MISMATCH_WARN_ONLY=y +# CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B is not set +CONFIG_ARCH_WANT_FRAME_POINTERS=y +CONFIG_FRAME_POINTER=y +# CONFIG_VMLINUX_MAP is not set +# CONFIG_DEBUG_FORCE_WEAK_PER_CPU is not set +# end of Compile-time checks and compiler options + +# +# Generic Kernel Debugging Instruments +# +CONFIG_MAGIC_SYSRQ=y +CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0x1 +CONFIG_MAGIC_SYSRQ_SERIAL=y +CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE="" +CONFIG_DEBUG_FS=y +CONFIG_DEBUG_FS_ALLOW_ALL=y +# CONFIG_DEBUG_FS_DISALLOW_MOUNT is not set +# CONFIG_DEBUG_FS_ALLOW_NONE is not set +CONFIG_HAVE_ARCH_KGDB=y +# CONFIG_KGDB is not set +CONFIG_ARCH_HAS_UBSAN_SANITIZE_ALL=y +# CONFIG_UBSAN is not set +# end of Generic Kernel Debugging Instruments + +CONFIG_DEBUG_KERNEL=y +CONFIG_DEBUG_MISC=y + +# +# Memory Debugging +# +# CONFIG_PAGE_EXTENSION is not set +# CONFIG_DEBUG_PAGEALLOC is not set +# CONFIG_PAGE_OWNER is not set +# CONFIG_PAGE_POISONING is not set +# CONFIG_DEBUG_RODATA_TEST is not set +CONFIG_ARCH_HAS_DEBUG_WX=y +# CONFIG_DEBUG_WX is not set +CONFIG_GENERIC_PTDUMP=y +# CONFIG_PTDUMP_DEBUGFS is not set +# CONFIG_DEBUG_OBJECTS is not set +# CONFIG_SLUB_DEBUG_ON is not set +# CONFIG_SLUB_STATS is not set +CONFIG_HAVE_DEBUG_KMEMLEAK=y +# CONFIG_DEBUG_KMEMLEAK is not set +# CONFIG_DEBUG_STACK_USAGE is not set +# CONFIG_SCHED_STACK_END_CHECK is not set +CONFIG_ARCH_HAS_DEBUG_VM_PGTABLE=y +# CONFIG_DEBUG_VM is not set +# CONFIG_DEBUG_VM_PGTABLE is not set +CONFIG_ARCH_HAS_DEBUG_VIRTUAL=y +# CONFIG_DEBUG_VIRTUAL is not set +CONFIG_DEBUG_MEMORY_INIT=y +# CONFIG_DEBUG_PER_CPU_MAPS is not set +CONFIG_HAVE_ARCH_KASAN=y +CONFIG_HAVE_ARCH_KASAN_SW_TAGS=y +CONFIG_HAVE_ARCH_KASAN_HW_TAGS=y +CONFIG_HAVE_ARCH_KASAN_VMALLOC=y +CONFIG_CC_HAS_KASAN_GENERIC=y +CONFIG_CC_HAS_WORKING_NOSANITIZE_ADDRESS=y +# CONFIG_KASAN is not set +CONFIG_HAVE_ARCH_KFENCE=y +# CONFIG_KFENCE is not set +# end of Memory Debugging + +# CONFIG_DEBUG_SHIRQ is not set + +# +# Debug Oops, Lockups and Hangs +# +# CONFIG_PANIC_ON_OOPS is not set +CONFIG_PANIC_ON_OOPS_VALUE=0 +CONFIG_PANIC_TIMEOUT=0 +# CONFIG_SOFTLOCKUP_DETECTOR is not set +# CONFIG_DETECT_HUNG_TASK is not set +# CONFIG_WQ_WATCHDOG is not set +# end of Debug Oops, Lockups and Hangs + +# +# Scheduler Debugging +# +# CONFIG_SCHED_DEBUG is not set +CONFIG_SCHED_INFO=y +# CONFIG_SCHEDSTATS is not set +# end of Scheduler Debugging + +# CONFIG_DEBUG_TIMEKEEPING is not set + +# +# Lock Debugging (spinlocks, mutexes, etc...) +# +CONFIG_LOCK_DEBUGGING_SUPPORT=y +# CONFIG_PROVE_LOCKING is not set +# CONFIG_LOCK_STAT is not set +# CONFIG_DEBUG_RT_MUTEXES is not set +# CONFIG_DEBUG_SPINLOCK is not set +# CONFIG_DEBUG_MUTEXES is not set +# CONFIG_DEBUG_WW_MUTEX_SLOWPATH is not set +# CONFIG_DEBUG_RWSEMS is not set +# CONFIG_DEBUG_LOCK_ALLOC is not set +# CONFIG_DEBUG_ATOMIC_SLEEP is not set +# CONFIG_DEBUG_LOCKING_API_SELFTESTS is not set +# CONFIG_LOCK_TORTURE_TEST is not set +# CONFIG_WW_MUTEX_SELFTEST is not set +# CONFIG_SCF_TORTURE_TEST is not set +# CONFIG_CSD_LOCK_WAIT_DEBUG is not set +# end of Lock Debugging (spinlocks, mutexes, etc...) + +# CONFIG_DEBUG_IRQFLAGS is not set +CONFIG_STACKTRACE=y +# CONFIG_WARN_ALL_UNSEEDED_RANDOM is not set +# CONFIG_DEBUG_KOBJECT is not set + +# +# Debug kernel data structures +# +CONFIG_DEBUG_LIST=y +# CONFIG_DEBUG_PLIST is not set +# CONFIG_DEBUG_SG is not set +# CONFIG_DEBUG_NOTIFIERS is not set +CONFIG_BUG_ON_DATA_CORRUPTION=y +# end of Debug kernel data structures + +# CONFIG_DEBUG_CREDENTIALS is not set + +# +# RCU Debugging +# +# CONFIG_RCU_SCALE_TEST is not set +# CONFIG_RCU_TORTURE_TEST is not set +# CONFIG_RCU_REF_SCALE_TEST is not set +CONFIG_RCU_CPU_STALL_TIMEOUT=59 +# CONFIG_RCU_TRACE is not set +# CONFIG_RCU_EQS_DEBUG is not set +# end of RCU Debugging + +# CONFIG_DEBUG_WQ_FORCE_RR_CPU is not set +# CONFIG_CPU_HOTPLUG_STATE_CONTROL is not set +# CONFIG_LATENCYTOP is not set +CONFIG_HAVE_FUNCTION_TRACER=y +CONFIG_HAVE_FUNCTION_GRAPH_TRACER=y +CONFIG_HAVE_DYNAMIC_FTRACE=y +CONFIG_HAVE_DYNAMIC_FTRACE_WITH_REGS=y +CONFIG_HAVE_FTRACE_MCOUNT_RECORD=y +CONFIG_HAVE_SYSCALL_TRACEPOINTS=y +CONFIG_HAVE_C_RECORDMCOUNT=y +CONFIG_TRACING_SUPPORT=y +# CONFIG_FTRACE is not set +# CONFIG_SAMPLES is not set +CONFIG_STRICT_DEVMEM=y +# CONFIG_IO_STRICT_DEVMEM is not set + +# +# arm64 Debugging +# +# CONFIG_PID_IN_CONTEXTIDR is not set +# CONFIG_CORESIGHT is not set +# end of arm64 Debugging + +# +# Kernel Testing and Coverage +# +# CONFIG_KUNIT is not set +# CONFIG_NOTIFIER_ERROR_INJECTION is not set +# CONFIG_FAULT_INJECTION is not set +CONFIG_ARCH_HAS_KCOV=y +CONFIG_CC_HAS_SANCOV_TRACE_PC=y +# CONFIG_KCOV is not set +CONFIG_RUNTIME_TESTING_MENU=y +# CONFIG_LKDTM is not set +# CONFIG_TEST_MIN_HEAP is not set +# CONFIG_TEST_DIV64 is not set +# CONFIG_BACKTRACE_SELF_TEST is not set +# CONFIG_RBTREE_TEST is not set +# CONFIG_REED_SOLOMON_TEST is not set +# CONFIG_INTERVAL_TREE_TEST is not set +# CONFIG_ATOMIC64_SELFTEST is not set +# CONFIG_TEST_HEXDUMP is not set +# CONFIG_STRING_SELFTEST is not set +# CONFIG_TEST_STRING_HELPERS is not set +# CONFIG_TEST_STRSCPY is not set +# CONFIG_TEST_KSTRTOX is not set +# CONFIG_TEST_PRINTF is not set +# CONFIG_TEST_SCANF is not set +# CONFIG_TEST_BITMAP is not set +# CONFIG_TEST_UUID is not set +# CONFIG_TEST_XARRAY is not set +# CONFIG_TEST_OVERFLOW is not set +# CONFIG_TEST_RHASHTABLE is not set +# CONFIG_TEST_HASH is not set +# CONFIG_TEST_IDA is not set +# CONFIG_FIND_BIT_BENCHMARK is not set +# CONFIG_TEST_FIRMWARE is not set +# CONFIG_TEST_SYSCTL is not set +# CONFIG_TEST_UDELAY is not set +# CONFIG_TEST_MEMCAT_P is not set +# CONFIG_TEST_STACKINIT is not set +# CONFIG_TEST_MEMINIT is not set +# CONFIG_TEST_FREE_PAGES is not set +CONFIG_ARCH_USE_MEMTEST=y +# CONFIG_MEMTEST is not set +# end of Kernel Testing and Coverage +# end of Kernel hacking diff --git a/vendor/cube-hypervisor/resources/linux-config-x86_64 b/vendor/cube-hypervisor/resources/linux-config-x86_64 new file mode 100644 index 0000000..e46af24 --- /dev/null +++ b/vendor/cube-hypervisor/resources/linux-config-x86_64 @@ -0,0 +1,3029 @@ +# +# Automatically generated file; DO NOT EDIT. +# Linux/x86 5.15.12 Kernel Configuration +# +CONFIG_CC_VERSION_TEXT="gcc (GCC) 11.2.1 20211203 (Red Hat 11.2.1-7)" +CONFIG_CC_IS_GCC=y +CONFIG_GCC_VERSION=110201 +CONFIG_CLANG_VERSION=0 +CONFIG_AS_IS_GNU=y +CONFIG_AS_VERSION=23700 +CONFIG_LD_IS_BFD=y +CONFIG_LD_VERSION=23700 +CONFIG_LLD_VERSION=0 +CONFIG_CC_CAN_LINK=y +CONFIG_CC_HAS_ASM_GOTO=y +CONFIG_CC_HAS_ASM_GOTO_OUTPUT=y +CONFIG_CC_HAS_ASM_INLINE=y +CONFIG_CC_HAS_NO_PROFILE_FN_ATTR=y +CONFIG_IRQ_WORK=y +CONFIG_BUILDTIME_TABLE_SORT=y +CONFIG_THREAD_INFO_IN_TASK=y + +# +# General setup +# +CONFIG_INIT_ENV_ARG_LIMIT=32 +# CONFIG_COMPILE_TEST is not set +# CONFIG_WERROR is not set +CONFIG_LOCALVERSION="" +# CONFIG_LOCALVERSION_AUTO is not set +CONFIG_BUILD_SALT="" +CONFIG_HAVE_KERNEL_GZIP=y +CONFIG_HAVE_KERNEL_BZIP2=y +CONFIG_HAVE_KERNEL_LZMA=y +CONFIG_HAVE_KERNEL_XZ=y +CONFIG_HAVE_KERNEL_LZO=y +CONFIG_HAVE_KERNEL_LZ4=y +CONFIG_HAVE_KERNEL_ZSTD=y +CONFIG_KERNEL_GZIP=y +# CONFIG_KERNEL_BZIP2 is not set +# CONFIG_KERNEL_LZMA is not set +# CONFIG_KERNEL_XZ is not set +# CONFIG_KERNEL_LZO is not set +# CONFIG_KERNEL_LZ4 is not set +# CONFIG_KERNEL_ZSTD is not set +CONFIG_DEFAULT_INIT="" +CONFIG_DEFAULT_HOSTNAME="cloud-hypervisor" +CONFIG_SWAP=y +CONFIG_SYSVIPC=y +CONFIG_SYSVIPC_SYSCTL=y +CONFIG_POSIX_MQUEUE=y +CONFIG_POSIX_MQUEUE_SYSCTL=y +# CONFIG_WATCH_QUEUE is not set +CONFIG_CROSS_MEMORY_ATTACH=y +# CONFIG_USELIB is not set +CONFIG_AUDIT=y +CONFIG_HAVE_ARCH_AUDITSYSCALL=y +CONFIG_AUDITSYSCALL=y + +# +# IRQ subsystem +# +CONFIG_GENERIC_IRQ_PROBE=y +CONFIG_GENERIC_IRQ_SHOW=y +CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK=y +CONFIG_GENERIC_PENDING_IRQ=y +CONFIG_GENERIC_IRQ_MIGRATION=y +CONFIG_HARDIRQS_SW_RESEND=y +CONFIG_IRQ_DOMAIN=y +CONFIG_IRQ_DOMAIN_HIERARCHY=y +CONFIG_GENERIC_MSI_IRQ=y +CONFIG_GENERIC_MSI_IRQ_DOMAIN=y +CONFIG_IRQ_MSI_IOMMU=y +CONFIG_GENERIC_IRQ_MATRIX_ALLOCATOR=y +CONFIG_GENERIC_IRQ_RESERVATION_MODE=y +CONFIG_IRQ_FORCED_THREADING=y +CONFIG_SPARSE_IRQ=y +# CONFIG_GENERIC_IRQ_DEBUGFS is not set +# end of IRQ subsystem + +CONFIG_CLOCKSOURCE_WATCHDOG=y +CONFIG_ARCH_CLOCKSOURCE_INIT=y +CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE=y +CONFIG_GENERIC_TIME_VSYSCALL=y +CONFIG_GENERIC_CLOCKEVENTS=y +CONFIG_GENERIC_CLOCKEVENTS_BROADCAST=y +CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST=y +CONFIG_GENERIC_CMOS_UPDATE=y +CONFIG_HAVE_POSIX_CPU_TIMERS_TASK_WORK=y +CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y + +# +# Timers subsystem +# +CONFIG_TICK_ONESHOT=y +CONFIG_NO_HZ_COMMON=y +# CONFIG_HZ_PERIODIC is not set +CONFIG_NO_HZ_IDLE=y +# CONFIG_NO_HZ_FULL is not set +CONFIG_NO_HZ=y +CONFIG_HIGH_RES_TIMERS=y +# end of Timers subsystem + +CONFIG_BPF=y +CONFIG_HAVE_EBPF_JIT=y +CONFIG_ARCH_WANT_DEFAULT_BPF_JIT=y + +# +# BPF subsystem +# +CONFIG_BPF_SYSCALL=y +# CONFIG_BPF_UNPRIV_DEFAULT_OFF is not set +# CONFIG_BPF_PRELOAD is not set +# end of BPF subsystem + +CONFIG_PREEMPT_NONE=y +# CONFIG_PREEMPT_VOLUNTARY is not set +# CONFIG_PREEMPT is not set +# CONFIG_SCHED_CORE is not set + +# +# CPU/Task time and stats accounting +# +CONFIG_TICK_CPU_ACCOUNTING=y +# CONFIG_VIRT_CPU_ACCOUNTING_GEN is not set +# CONFIG_IRQ_TIME_ACCOUNTING is not set +CONFIG_HAVE_SCHED_AVG_IRQ=y +CONFIG_BSD_PROCESS_ACCT=y +CONFIG_BSD_PROCESS_ACCT_V3=y +CONFIG_TASKSTATS=y +CONFIG_TASK_DELAY_ACCT=y +CONFIG_TASK_XACCT=y +CONFIG_TASK_IO_ACCOUNTING=y +# CONFIG_PSI is not set +# end of CPU/Task time and stats accounting + +CONFIG_CPU_ISOLATION=y + +# +# RCU Subsystem +# +CONFIG_TREE_RCU=y +# CONFIG_RCU_EXPERT is not set +CONFIG_SRCU=y +CONFIG_TREE_SRCU=y +CONFIG_TASKS_RCU_GENERIC=y +CONFIG_TASKS_TRACE_RCU=y +CONFIG_RCU_STALL_COMMON=y +CONFIG_RCU_NEED_SEGCBLIST=y +# end of RCU Subsystem + +CONFIG_BUILD_BIN2C=y +# CONFIG_IKCONFIG is not set +# CONFIG_IKHEADERS is not set +CONFIG_LOG_BUF_SHIFT=21 +CONFIG_LOG_CPU_MAX_BUF_SHIFT=12 +CONFIG_PRINTK_SAFE_LOG_BUF_SHIFT=13 +# CONFIG_PRINTK_INDEX is not set +CONFIG_HAVE_UNSTABLE_SCHED_CLOCK=y + +# +# Scheduler features +# +# CONFIG_UCLAMP_TASK is not set +# end of Scheduler features + +CONFIG_ARCH_SUPPORTS_NUMA_BALANCING=y +CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH=y +CONFIG_CC_HAS_INT128=y +CONFIG_ARCH_SUPPORTS_INT128=y +CONFIG_NUMA_BALANCING=y +# CONFIG_NUMA_BALANCING_DEFAULT_ENABLED is not set +CONFIG_CGROUPS=y +CONFIG_PAGE_COUNTER=y +CONFIG_MEMCG=y +CONFIG_MEMCG_SWAP=y +CONFIG_MEMCG_KMEM=y +CONFIG_BLK_CGROUP=y +CONFIG_CGROUP_WRITEBACK=y +CONFIG_CGROUP_SCHED=y +CONFIG_FAIR_GROUP_SCHED=y +CONFIG_CFS_BANDWIDTH=y +CONFIG_RT_GROUP_SCHED=y +CONFIG_CGROUP_PIDS=y +# CONFIG_CGROUP_RDMA is not set +CONFIG_CGROUP_FREEZER=y +CONFIG_CGROUP_HUGETLB=y +CONFIG_CPUSETS=y +CONFIG_PROC_PID_CPUSET=y +CONFIG_CGROUP_DEVICE=y +CONFIG_CGROUP_CPUACCT=y +CONFIG_CGROUP_PERF=y +CONFIG_CGROUP_BPF=y +# CONFIG_CGROUP_MISC is not set +# CONFIG_CGROUP_DEBUG is not set +CONFIG_SOCK_CGROUP_DATA=y +CONFIG_NAMESPACES=y +CONFIG_UTS_NS=y +# CONFIG_TIME_NS is not set +CONFIG_IPC_NS=y +CONFIG_USER_NS=y +CONFIG_PID_NS=y +CONFIG_NET_NS=y +# CONFIG_CHECKPOINT_RESTORE is not set +CONFIG_SCHED_AUTOGROUP=y +# CONFIG_SYSFS_DEPRECATED is not set +CONFIG_RELAY=y +CONFIG_BLK_DEV_INITRD=y +CONFIG_INITRAMFS_SOURCE="" +CONFIG_RD_GZIP=y +CONFIG_RD_BZIP2=y +CONFIG_RD_LZMA=y +CONFIG_RD_XZ=y +CONFIG_RD_LZO=y +CONFIG_RD_LZ4=y +CONFIG_RD_ZSTD=y +# CONFIG_BOOT_CONFIG is not set +CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y +# CONFIG_CC_OPTIMIZE_FOR_SIZE is not set +CONFIG_LD_ORPHAN_WARN=y +CONFIG_SYSCTL=y +CONFIG_SYSCTL_EXCEPTION_TRACE=y +CONFIG_HAVE_PCSPKR_PLATFORM=y +CONFIG_EXPERT=y +CONFIG_MULTIUSER=y +CONFIG_SGETMASK_SYSCALL=y +CONFIG_SYSFS_SYSCALL=y +CONFIG_FHANDLE=y +CONFIG_POSIX_TIMERS=y +CONFIG_PRINTK=y +CONFIG_BUG=y +CONFIG_ELF_CORE=y +CONFIG_PCSPKR_PLATFORM=y +CONFIG_BASE_FULL=y +CONFIG_FUTEX=y +CONFIG_FUTEX_PI=y +CONFIG_EPOLL=y +CONFIG_SIGNALFD=y +CONFIG_TIMERFD=y +CONFIG_EVENTFD=y +CONFIG_SHMEM=y +CONFIG_AIO=y +CONFIG_IO_URING=y +CONFIG_ADVISE_SYSCALLS=y +CONFIG_HAVE_ARCH_USERFAULTFD_WP=y +CONFIG_HAVE_ARCH_USERFAULTFD_MINOR=y +CONFIG_MEMBARRIER=y +CONFIG_KALLSYMS=y +# CONFIG_KALLSYMS_ALL is not set +CONFIG_KALLSYMS_ABSOLUTE_PERCPU=y +CONFIG_KALLSYMS_BASE_RELATIVE=y +CONFIG_USERFAULTFD=y +CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE=y +# CONFIG_KCMP is not set +CONFIG_RSEQ=y +# CONFIG_DEBUG_RSEQ is not set +# CONFIG_EMBEDDED is not set +CONFIG_HAVE_PERF_EVENTS=y +# CONFIG_PC104 is not set + +# +# Kernel Performance Events And Counters +# +CONFIG_PERF_EVENTS=y +# CONFIG_DEBUG_PERF_USE_VMALLOC is not set +# end of Kernel Performance Events And Counters + +CONFIG_VM_EVENT_COUNTERS=y +CONFIG_SLUB_DEBUG=y +# CONFIG_COMPAT_BRK is not set +# CONFIG_SLAB is not set +CONFIG_SLUB=y +# CONFIG_SLOB is not set +CONFIG_SLAB_MERGE_DEFAULT=y +# CONFIG_SLAB_FREELIST_RANDOM is not set +CONFIG_SLAB_FREELIST_HARDENED=y +# CONFIG_SHUFFLE_PAGE_ALLOCATOR is not set +CONFIG_SLUB_CPU_PARTIAL=y +# CONFIG_PROFILING is not set +# end of General setup + +CONFIG_64BIT=y +CONFIG_X86_64=y +CONFIG_X86=y +CONFIG_INSTRUCTION_DECODER=y +CONFIG_OUTPUT_FORMAT="elf64-x86-64" +CONFIG_LOCKDEP_SUPPORT=y +CONFIG_STACKTRACE_SUPPORT=y +CONFIG_MMU=y +CONFIG_ARCH_MMAP_RND_BITS_MIN=28 +CONFIG_ARCH_MMAP_RND_BITS_MAX=32 +CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN=8 +CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX=16 +CONFIG_GENERIC_BUG=y +CONFIG_GENERIC_BUG_RELATIVE_POINTERS=y +CONFIG_GENERIC_CALIBRATE_DELAY=y +CONFIG_ARCH_HAS_CPU_RELAX=y +CONFIG_ARCH_HAS_FILTER_PGPROT=y +CONFIG_HAVE_SETUP_PER_CPU_AREA=y +CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK=y +CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK=y +CONFIG_ARCH_HIBERNATION_POSSIBLE=y +CONFIG_ARCH_NR_GPIO=1024 +CONFIG_ARCH_SUSPEND_POSSIBLE=y +CONFIG_ARCH_WANT_GENERAL_HUGETLB=y +CONFIG_AUDIT_ARCH=y +CONFIG_X86_64_SMP=y +CONFIG_ARCH_SUPPORTS_UPROBES=y +CONFIG_FIX_EARLYCON_MEM=y +CONFIG_DYNAMIC_PHYSICAL_MASK=y +CONFIG_PGTABLE_LEVELS=5 +CONFIG_CC_HAS_SANE_STACKPROTECTOR=y + +# +# Processor type and features +# +CONFIG_SMP=y +CONFIG_X86_FEATURE_NAMES=y +CONFIG_X86_X2APIC=y +CONFIG_X86_MPPARSE=y +# CONFIG_GOLDFISH is not set +CONFIG_RETPOLINE=y +# CONFIG_X86_CPU_RESCTRL is not set +# CONFIG_X86_EXTENDED_PLATFORM is not set +# CONFIG_X86_INTEL_LPSS is not set +# CONFIG_X86_AMD_PLATFORM_DEVICE is not set +# CONFIG_IOSF_MBI is not set +CONFIG_SCHED_OMIT_FRAME_POINTER=y +CONFIG_HYPERVISOR_GUEST=y +CONFIG_PARAVIRT=y +# CONFIG_PARAVIRT_DEBUG is not set +CONFIG_PARAVIRT_SPINLOCKS=y +CONFIG_X86_HV_CALLBACK_VECTOR=y +# CONFIG_XEN is not set +CONFIG_KVM_GUEST=y +CONFIG_ARCH_CPUIDLE_HALTPOLL=y +CONFIG_PVH=y +CONFIG_PARAVIRT_TIME_ACCOUNTING=y +CONFIG_PARAVIRT_CLOCK=y +# CONFIG_JAILHOUSE_GUEST is not set +# CONFIG_ACRN_GUEST is not set +# CONFIG_MK8 is not set +# CONFIG_MPSC is not set +# CONFIG_MCORE2 is not set +# CONFIG_MATOM is not set +CONFIG_GENERIC_CPU=y +CONFIG_X86_INTERNODE_CACHE_SHIFT=6 +CONFIG_X86_L1_CACHE_SHIFT=6 +CONFIG_X86_TSC=y +CONFIG_X86_CMPXCHG64=y +CONFIG_X86_CMOV=y +CONFIG_X86_MINIMUM_CPU_FAMILY=64 +CONFIG_X86_DEBUGCTLMSR=y +CONFIG_IA32_FEAT_CTL=y +CONFIG_X86_VMX_FEATURE_NAMES=y +# CONFIG_PROCESSOR_SELECT is not set +CONFIG_CPU_SUP_INTEL=y +CONFIG_CPU_SUP_AMD=y +CONFIG_CPU_SUP_HYGON=y +CONFIG_CPU_SUP_CENTAUR=y +CONFIG_CPU_SUP_ZHAOXIN=y +CONFIG_HPET_TIMER=y +CONFIG_DMI=y +# CONFIG_GART_IOMMU is not set +# CONFIG_MAXSMP is not set +CONFIG_NR_CPUS_RANGE_BEGIN=2 +CONFIG_NR_CPUS_RANGE_END=512 +CONFIG_NR_CPUS_DEFAULT=64 +CONFIG_NR_CPUS=128 +CONFIG_SCHED_SMT=y +CONFIG_SCHED_MC=y +CONFIG_SCHED_MC_PRIO=y +CONFIG_X86_LOCAL_APIC=y +CONFIG_X86_IO_APIC=y +CONFIG_X86_REROUTE_FOR_BROKEN_BOOT_IRQS=y +# CONFIG_X86_MCE is not set + +# +# Performance monitoring +# +CONFIG_PERF_EVENTS_INTEL_UNCORE=y +CONFIG_PERF_EVENTS_INTEL_RAPL=y +CONFIG_PERF_EVENTS_INTEL_CSTATE=y +# CONFIG_PERF_EVENTS_AMD_POWER is not set +CONFIG_PERF_EVENTS_AMD_UNCORE=y +# end of Performance monitoring + +CONFIG_X86_16BIT=y +CONFIG_X86_ESPFIX64=y +CONFIG_X86_VSYSCALL_EMULATION=y +# CONFIG_X86_IOPL_IOPERM is not set +# CONFIG_MICROCODE is not set +CONFIG_X86_MSR=y +CONFIG_X86_CPUID=y +CONFIG_X86_5LEVEL=y +CONFIG_X86_DIRECT_GBPAGES=y +# CONFIG_X86_CPA_STATISTICS is not set +CONFIG_AMD_MEM_ENCRYPT=y +# CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT is not set +CONFIG_NUMA=y +# CONFIG_AMD_NUMA is not set +CONFIG_X86_64_ACPI_NUMA=y +# CONFIG_NUMA_EMU is not set +CONFIG_NODES_SHIFT=10 +CONFIG_ARCH_SPARSEMEM_ENABLE=y +CONFIG_ARCH_SPARSEMEM_DEFAULT=y +CONFIG_ARCH_SELECT_MEMORY_MODEL=y +CONFIG_ARCH_MEMORY_PROBE=y +CONFIG_ARCH_PROC_KCORE_TEXT=y +CONFIG_ILLEGAL_POINTER_VALUE=0xdead000000000000 +# CONFIG_X86_PMEM_LEGACY is not set +CONFIG_X86_CHECK_BIOS_CORRUPTION=y +CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK=y +CONFIG_MTRR=y +CONFIG_MTRR_SANITIZER=y +CONFIG_MTRR_SANITIZER_ENABLE_DEFAULT=0 +CONFIG_MTRR_SANITIZER_SPARE_REG_NR_DEFAULT=1 +CONFIG_X86_PAT=y +CONFIG_ARCH_USES_PG_UNCACHED=y +CONFIG_ARCH_RANDOM=y +CONFIG_X86_SMAP=y +# CONFIG_X86_UMIP is not set +# CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS is not set +CONFIG_X86_INTEL_TSX_MODE_OFF=y +# CONFIG_X86_INTEL_TSX_MODE_ON is not set +# CONFIG_X86_INTEL_TSX_MODE_AUTO is not set +# CONFIG_X86_SGX is not set +CONFIG_EFI=y +CONFIG_EFI_STUB=y +# CONFIG_EFI_MIXED is not set +# CONFIG_HZ_100 is not set +CONFIG_HZ_250=y +# CONFIG_HZ_300 is not set +# CONFIG_HZ_1000 is not set +CONFIG_HZ=250 +CONFIG_SCHED_HRTICK=y +# CONFIG_KEXEC is not set +CONFIG_KEXEC_FILE=y +CONFIG_ARCH_HAS_KEXEC_PURGATORY=y +# CONFIG_KEXEC_SIG is not set +# CONFIG_CRASH_DUMP is not set +CONFIG_PHYSICAL_START=0x1000000 +CONFIG_RELOCATABLE=y +CONFIG_RANDOMIZE_BASE=y +CONFIG_X86_NEED_RELOCS=y +CONFIG_PHYSICAL_ALIGN=0x1000000 +CONFIG_DYNAMIC_MEMORY_LAYOUT=y +CONFIG_RANDOMIZE_MEMORY=y +CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING=0xa +CONFIG_HOTPLUG_CPU=y +# CONFIG_BOOTPARAM_HOTPLUG_CPU0 is not set +# CONFIG_DEBUG_HOTPLUG_CPU0 is not set +CONFIG_LEGACY_VSYSCALL_EMULATE=y +# CONFIG_LEGACY_VSYSCALL_XONLY is not set +# CONFIG_LEGACY_VSYSCALL_NONE is not set +# CONFIG_CMDLINE_BOOL is not set +CONFIG_MODIFY_LDT_SYSCALL=y +CONFIG_HAVE_LIVEPATCH=y +# end of Processor type and features + +CONFIG_ARCH_HAS_ADD_PAGES=y +CONFIG_ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE=y +CONFIG_USE_PERCPU_NUMA_NODE_ID=y + +# +# Power management and ACPI options +# +CONFIG_ARCH_HIBERNATION_HEADER=y +# CONFIG_SUSPEND is not set +CONFIG_HIBERNATE_CALLBACKS=y +CONFIG_HIBERNATION=y +# CONFIG_HIBERNATION_SNAPSHOT_DEV is not set +CONFIG_PM_STD_PARTITION="" +CONFIG_PM_SLEEP=y +CONFIG_PM_SLEEP_SMP=y +# CONFIG_PM_AUTOSLEEP is not set +# CONFIG_PM_WAKELOCKS is not set +CONFIG_PM=y +# CONFIG_PM_DEBUG is not set +CONFIG_PM_CLK=y +# CONFIG_WQ_POWER_EFFICIENT_DEFAULT is not set +# CONFIG_ENERGY_MODEL is not set +CONFIG_ARCH_SUPPORTS_ACPI=y +CONFIG_ACPI=y +CONFIG_ACPI_LEGACY_TABLES_LOOKUP=y +CONFIG_ARCH_MIGHT_HAVE_ACPI_PDC=y +CONFIG_ACPI_SYSTEM_POWER_STATES_SUPPORT=y +# CONFIG_ACPI_DEBUGGER is not set +CONFIG_ACPI_SPCR_TABLE=y +# CONFIG_ACPI_FPDT is not set +CONFIG_ACPI_LPIT=y +CONFIG_ACPI_SLEEP=y +CONFIG_ACPI_REV_OVERRIDE_POSSIBLE=y +# CONFIG_ACPI_EC_DEBUGFS is not set +# CONFIG_ACPI_AC is not set +# CONFIG_ACPI_BATTERY is not set +CONFIG_ACPI_BUTTON=y +# CONFIG_ACPI_VIDEO is not set +# CONFIG_ACPI_FAN is not set +# CONFIG_ACPI_TAD is not set +# CONFIG_ACPI_DOCK is not set +CONFIG_ACPI_CPU_FREQ_PSS=y +CONFIG_ACPI_PROCESSOR_CSTATE=y +CONFIG_ACPI_PROCESSOR_IDLE=y +CONFIG_ACPI_CPPC_LIB=y +CONFIG_ACPI_PROCESSOR=y +CONFIG_ACPI_HOTPLUG_CPU=y +# CONFIG_ACPI_PROCESSOR_AGGREGATOR is not set +CONFIG_ACPI_THERMAL=y +CONFIG_ARCH_HAS_ACPI_TABLE_UPGRADE=y +CONFIG_ACPI_TABLE_UPGRADE=y +# CONFIG_ACPI_DEBUG is not set +# CONFIG_ACPI_PCI_SLOT is not set +CONFIG_ACPI_CONTAINER=y +CONFIG_ACPI_HOTPLUG_MEMORY=y +CONFIG_ACPI_HOTPLUG_IOAPIC=y +# CONFIG_ACPI_SBS is not set +# CONFIG_ACPI_HED is not set +# CONFIG_ACPI_CUSTOM_METHOD is not set +# CONFIG_ACPI_BGRT is not set +CONFIG_ACPI_REDUCED_HARDWARE_ONLY=y +# CONFIG_ACPI_NFIT is not set +CONFIG_ACPI_NUMA=y +# CONFIG_ACPI_HMAT is not set +CONFIG_HAVE_ACPI_APEI=y +CONFIG_HAVE_ACPI_APEI_NMI=y +# CONFIG_ACPI_APEI is not set +# CONFIG_ACPI_DPTF is not set +# CONFIG_ACPI_CONFIGFS is not set +# CONFIG_PMIC_OPREGION is not set +CONFIG_ACPI_VIOT=y +CONFIG_X86_PM_TIMER=y +# CONFIG_ACPI_PRMT is not set + +# +# CPU Frequency scaling +# +CONFIG_CPU_FREQ=y +CONFIG_CPU_FREQ_GOV_ATTR_SET=y +# CONFIG_CPU_FREQ_STAT is not set +CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE=y +# CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE is not set +# CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE is not set +# CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL is not set +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y +# CONFIG_CPU_FREQ_GOV_POWERSAVE is not set +# CONFIG_CPU_FREQ_GOV_USERSPACE is not set +# CONFIG_CPU_FREQ_GOV_ONDEMAND is not set +# CONFIG_CPU_FREQ_GOV_CONSERVATIVE is not set +CONFIG_CPU_FREQ_GOV_SCHEDUTIL=y + +# +# CPU frequency scaling drivers +# +CONFIG_X86_INTEL_PSTATE=y +# CONFIG_X86_PCC_CPUFREQ is not set +# CONFIG_X86_ACPI_CPUFREQ is not set +# CONFIG_X86_SPEEDSTEP_CENTRINO is not set +# CONFIG_X86_P4_CLOCKMOD is not set + +# +# shared options +# +# end of CPU Frequency scaling + +# +# CPU Idle +# +CONFIG_CPU_IDLE=y +CONFIG_CPU_IDLE_GOV_LADDER=y +CONFIG_CPU_IDLE_GOV_MENU=y +# CONFIG_CPU_IDLE_GOV_TEO is not set +# CONFIG_CPU_IDLE_GOV_HALTPOLL is not set +# CONFIG_HALTPOLL_CPUIDLE is not set +# end of CPU Idle + +CONFIG_INTEL_IDLE=y +# end of Power management and ACPI options + +# +# Bus options (PCI etc.) +# +CONFIG_PCI_DIRECT=y +CONFIG_PCI_MMCONFIG=y +CONFIG_MMCONF_FAM10H=y +# CONFIG_PCI_CNB20LE_QUIRK is not set +# CONFIG_ISA_BUS is not set +# CONFIG_ISA_DMA_API is not set +CONFIG_AMD_NB=y +# end of Bus options (PCI etc.) + +# +# Binary Emulations +# +# CONFIG_IA32_EMULATION is not set +# CONFIG_X86_X32 is not set +# end of Binary Emulations + +CONFIG_HAVE_KVM=y +CONFIG_HAVE_KVM_IRQCHIP=y +CONFIG_HAVE_KVM_IRQFD=y +CONFIG_HAVE_KVM_IRQ_ROUTING=y +CONFIG_HAVE_KVM_EVENTFD=y +CONFIG_KVM_MMIO=y +CONFIG_KVM_ASYNC_PF=y +CONFIG_HAVE_KVM_MSI=y +CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT=y +CONFIG_KVM_VFIO=y +CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT=y +CONFIG_HAVE_KVM_IRQ_BYPASS=y +CONFIG_HAVE_KVM_NO_POLL=y +CONFIG_KVM_XFER_TO_GUEST_WORK=y +CONFIG_HAVE_KVM_PM_NOTIFIER=y +CONFIG_VIRTUALIZATION=y +CONFIG_KVM=y +# CONFIG_KVM_WERROR is not set +CONFIG_KVM_INTEL=y +# CONFIG_KVM_AMD is not set +# CONFIG_KVM_XEN is not set +CONFIG_AS_AVX512=y +CONFIG_AS_SHA1_NI=y +CONFIG_AS_SHA256_NI=y +CONFIG_AS_TPAUSE=y + +# +# General architecture-dependent options +# +CONFIG_CRASH_CORE=y +CONFIG_KEXEC_CORE=y +CONFIG_HOTPLUG_SMT=y +CONFIG_GENERIC_ENTRY=y +CONFIG_JUMP_LABEL=y +# CONFIG_STATIC_KEYS_SELFTEST is not set +# CONFIG_STATIC_CALL_SELFTEST is not set +CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS=y +CONFIG_ARCH_USE_BUILTIN_BSWAP=y +CONFIG_USER_RETURN_NOTIFIER=y +CONFIG_HAVE_IOREMAP_PROT=y +CONFIG_HAVE_KPROBES=y +CONFIG_HAVE_KRETPROBES=y +CONFIG_HAVE_OPTPROBES=y +CONFIG_HAVE_KPROBES_ON_FTRACE=y +CONFIG_HAVE_FUNCTION_ERROR_INJECTION=y +CONFIG_HAVE_NMI=y +CONFIG_TRACE_IRQFLAGS_SUPPORT=y +CONFIG_HAVE_ARCH_TRACEHOOK=y +CONFIG_HAVE_DMA_CONTIGUOUS=y +CONFIG_GENERIC_SMP_IDLE_THREAD=y +CONFIG_ARCH_HAS_FORTIFY_SOURCE=y +CONFIG_ARCH_HAS_SET_MEMORY=y +CONFIG_ARCH_HAS_SET_DIRECT_MAP=y +CONFIG_HAVE_ARCH_THREAD_STRUCT_WHITELIST=y +CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT=y +CONFIG_ARCH_WANTS_NO_INSTR=y +CONFIG_HAVE_ASM_MODVERSIONS=y +CONFIG_HAVE_REGS_AND_STACK_ACCESS_API=y +CONFIG_HAVE_RSEQ=y +CONFIG_HAVE_FUNCTION_ARG_ACCESS_API=y +CONFIG_HAVE_HW_BREAKPOINT=y +CONFIG_HAVE_MIXED_BREAKPOINTS_REGS=y +CONFIG_HAVE_USER_RETURN_NOTIFIER=y +CONFIG_HAVE_PERF_EVENTS_NMI=y +CONFIG_HAVE_HARDLOCKUP_DETECTOR_PERF=y +CONFIG_HAVE_PERF_REGS=y +CONFIG_HAVE_PERF_USER_STACK_DUMP=y +CONFIG_HAVE_ARCH_JUMP_LABEL=y +CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE=y +CONFIG_MMU_GATHER_TABLE_FREE=y +CONFIG_MMU_GATHER_RCU_TABLE_FREE=y +CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG=y +CONFIG_HAVE_ALIGNED_STRUCT_PAGE=y +CONFIG_HAVE_CMPXCHG_LOCAL=y +CONFIG_HAVE_CMPXCHG_DOUBLE=y +CONFIG_HAVE_ARCH_SECCOMP=y +CONFIG_HAVE_ARCH_SECCOMP_FILTER=y +CONFIG_SECCOMP=y +CONFIG_SECCOMP_FILTER=y +# CONFIG_SECCOMP_CACHE_DEBUG is not set +CONFIG_HAVE_ARCH_STACKLEAK=y +CONFIG_HAVE_STACKPROTECTOR=y +CONFIG_STACKPROTECTOR=y +CONFIG_STACKPROTECTOR_STRONG=y +CONFIG_ARCH_SUPPORTS_LTO_CLANG=y +CONFIG_ARCH_SUPPORTS_LTO_CLANG_THIN=y +CONFIG_LTO_NONE=y +CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES=y +CONFIG_HAVE_CONTEXT_TRACKING=y +CONFIG_HAVE_CONTEXT_TRACKING_OFFSTACK=y +CONFIG_HAVE_VIRT_CPU_ACCOUNTING_GEN=y +CONFIG_HAVE_IRQ_TIME_ACCOUNTING=y +CONFIG_HAVE_MOVE_PUD=y +CONFIG_HAVE_MOVE_PMD=y +CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE=y +CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD=y +CONFIG_HAVE_ARCH_HUGE_VMAP=y +CONFIG_ARCH_WANT_HUGE_PMD_SHARE=y +CONFIG_HAVE_ARCH_SOFT_DIRTY=y +CONFIG_HAVE_MOD_ARCH_SPECIFIC=y +CONFIG_MODULES_USE_ELF_RELA=y +CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK=y +CONFIG_HAVE_SOFTIRQ_ON_OWN_STACK=y +CONFIG_ARCH_HAS_ELF_RANDOMIZE=y +CONFIG_HAVE_ARCH_MMAP_RND_BITS=y +CONFIG_HAVE_EXIT_THREAD=y +CONFIG_ARCH_MMAP_RND_BITS=28 +CONFIG_HAVE_STACK_VALIDATION=y +CONFIG_HAVE_RELIABLE_STACKTRACE=y +# CONFIG_COMPAT_32BIT_TIME is not set +CONFIG_HAVE_ARCH_VMAP_STACK=y +CONFIG_VMAP_STACK=y +CONFIG_HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET=y +# CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT is not set +CONFIG_ARCH_HAS_STRICT_KERNEL_RWX=y +CONFIG_STRICT_KERNEL_RWX=y +CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y +CONFIG_HAVE_ARCH_PREL32_RELOCATIONS=y +CONFIG_ARCH_USE_MEMREMAP_PROT=y +# CONFIG_LOCK_EVENT_COUNTS is not set +CONFIG_ARCH_HAS_MEM_ENCRYPT=y +CONFIG_ARCH_HAS_CC_PLATFORM=y +CONFIG_HAVE_STATIC_CALL=y +CONFIG_HAVE_STATIC_CALL_INLINE=y +CONFIG_HAVE_PREEMPT_DYNAMIC=y +CONFIG_ARCH_WANT_LD_ORPHAN_WARN=y +CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC=y +CONFIG_ARCH_HAS_ELFCORE_COMPAT=y +CONFIG_ARCH_HAS_PARANOID_L1D_FLUSH=y + +# +# GCOV-based kernel profiling +# +# CONFIG_GCOV_KERNEL is not set +CONFIG_ARCH_HAS_GCOV_PROFILE_ALL=y +# end of GCOV-based kernel profiling + +CONFIG_HAVE_GCC_PLUGINS=y +# end of General architecture-dependent options + +CONFIG_RT_MUTEXES=y +CONFIG_BASE_SMALL=0 +# CONFIG_MODULES is not set +CONFIG_MODULES_TREE_LOOKUP=y +CONFIG_BLOCK=y +CONFIG_BLK_CGROUP_RWSTAT=y +CONFIG_BLK_DEV_BSG_COMMON=y +CONFIG_BLK_DEV_BSGLIB=y +CONFIG_BLK_DEV_INTEGRITY=y +CONFIG_BLK_DEV_INTEGRITY_T10=y +# CONFIG_BLK_DEV_ZONED is not set +CONFIG_BLK_DEV_THROTTLING=y +# CONFIG_BLK_DEV_THROTTLING_LOW is not set +CONFIG_BLK_WBT=y +CONFIG_BLK_WBT_MQ=y +# CONFIG_BLK_CGROUP_IOLATENCY is not set +# CONFIG_BLK_CGROUP_IOCOST is not set +# CONFIG_BLK_CGROUP_IOPRIO is not set +CONFIG_BLK_DEBUG_FS=y +# CONFIG_BLK_SED_OPAL is not set +# CONFIG_BLK_INLINE_ENCRYPTION is not set + +# +# Partition Types +# +CONFIG_PARTITION_ADVANCED=y +# CONFIG_ACORN_PARTITION is not set +# CONFIG_AIX_PARTITION is not set +# CONFIG_OSF_PARTITION is not set +# CONFIG_AMIGA_PARTITION is not set +# CONFIG_ATARI_PARTITION is not set +# CONFIG_MAC_PARTITION is not set +# CONFIG_MSDOS_PARTITION is not set +# CONFIG_LDM_PARTITION is not set +# CONFIG_SGI_PARTITION is not set +# CONFIG_ULTRIX_PARTITION is not set +# CONFIG_SUN_PARTITION is not set +# CONFIG_KARMA_PARTITION is not set +CONFIG_EFI_PARTITION=y +# CONFIG_SYSV68_PARTITION is not set +# CONFIG_CMDLINE_PARTITION is not set +# end of Partition Types + +CONFIG_BLK_MQ_PCI=y +CONFIG_BLK_MQ_VIRTIO=y +CONFIG_BLK_PM=y +CONFIG_BLOCK_HOLDER_DEPRECATED=y + +# +# IO Schedulers +# +# CONFIG_MQ_IOSCHED_DEADLINE is not set +# CONFIG_MQ_IOSCHED_KYBER is not set +# CONFIG_IOSCHED_BFQ is not set +# end of IO Schedulers + +CONFIG_PREEMPT_NOTIFIERS=y +CONFIG_INLINE_SPIN_UNLOCK_IRQ=y +CONFIG_INLINE_READ_UNLOCK=y +CONFIG_INLINE_READ_UNLOCK_IRQ=y +CONFIG_INLINE_WRITE_UNLOCK=y +CONFIG_INLINE_WRITE_UNLOCK_IRQ=y +CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=y +CONFIG_MUTEX_SPIN_ON_OWNER=y +CONFIG_RWSEM_SPIN_ON_OWNER=y +CONFIG_LOCK_SPIN_ON_OWNER=y +CONFIG_ARCH_USE_QUEUED_SPINLOCKS=y +CONFIG_QUEUED_SPINLOCKS=y +CONFIG_ARCH_USE_QUEUED_RWLOCKS=y +CONFIG_QUEUED_RWLOCKS=y +CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE=y +CONFIG_ARCH_HAS_SYNC_CORE_BEFORE_USERMODE=y +CONFIG_ARCH_HAS_SYSCALL_WRAPPER=y +CONFIG_FREEZER=y + +# +# Executable file formats +# +CONFIG_BINFMT_ELF=y +CONFIG_ELFCORE=y +CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS=y +CONFIG_BINFMT_SCRIPT=y +CONFIG_BINFMT_MISC=y +CONFIG_COREDUMP=y +# end of Executable file formats + +# +# Memory Management options +# +CONFIG_SELECT_MEMORY_MODEL=y +CONFIG_SPARSEMEM_MANUAL=y +CONFIG_SPARSEMEM=y +CONFIG_SPARSEMEM_EXTREME=y +CONFIG_SPARSEMEM_VMEMMAP_ENABLE=y +CONFIG_SPARSEMEM_VMEMMAP=y +CONFIG_HAVE_FAST_GUP=y +CONFIG_NUMA_KEEP_MEMINFO=y +CONFIG_MEMORY_ISOLATION=y +CONFIG_HAVE_BOOTMEM_INFO_NODE=y +CONFIG_ARCH_ENABLE_MEMORY_HOTPLUG=y +CONFIG_MEMORY_HOTPLUG=y +CONFIG_MEMORY_HOTPLUG_SPARSE=y +CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE=y +CONFIG_ARCH_ENABLE_MEMORY_HOTREMOVE=y +CONFIG_MEMORY_HOTREMOVE=y +CONFIG_MHP_MEMMAP_ON_MEMORY=y +CONFIG_SPLIT_PTLOCK_CPUS=4 +CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK=y +CONFIG_MEMORY_BALLOON=y +CONFIG_BALLOON_COMPACTION=y +CONFIG_COMPACTION=y +CONFIG_PAGE_REPORTING=y +CONFIG_MIGRATION=y +CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION=y +CONFIG_ARCH_ENABLE_THP_MIGRATION=y +CONFIG_CONTIG_ALLOC=y +CONFIG_PHYS_ADDR_T_64BIT=y +CONFIG_VIRT_TO_BUS=y +CONFIG_MMU_NOTIFIER=y +CONFIG_KSM=y +CONFIG_DEFAULT_MMAP_MIN_ADDR=4096 +CONFIG_TRANSPARENT_HUGEPAGE=y +# CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS is not set +CONFIG_TRANSPARENT_HUGEPAGE_MADVISE=y +CONFIG_ARCH_WANTS_THP_SWAP=y +CONFIG_THP_SWAP=y +CONFIG_CLEANCACHE=y +CONFIG_FRONTSWAP=y +# CONFIG_CMA is not set +CONFIG_ZSWAP=y +# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_DEFLATE is not set +CONFIG_ZSWAP_COMPRESSOR_DEFAULT_LZO=y +# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_842 is not set +# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_LZ4 is not set +# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_LZ4HC is not set +# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_ZSTD is not set +CONFIG_ZSWAP_COMPRESSOR_DEFAULT="lzo" +CONFIG_ZSWAP_ZPOOL_DEFAULT_ZBUD=y +# CONFIG_ZSWAP_ZPOOL_DEFAULT_Z3FOLD is not set +# CONFIG_ZSWAP_ZPOOL_DEFAULT_ZSMALLOC is not set +CONFIG_ZSWAP_ZPOOL_DEFAULT="zbud" +# CONFIG_ZSWAP_DEFAULT_ON is not set +CONFIG_ZPOOL=y +CONFIG_ZBUD=y +# CONFIG_Z3FOLD is not set +CONFIG_ZSMALLOC=y +CONFIG_ZSMALLOC_STAT=y +CONFIG_GENERIC_EARLY_IOREMAP=y +# CONFIG_DEFERRED_STRUCT_PAGE_INIT is not set +# CONFIG_IDLE_PAGE_TRACKING is not set +CONFIG_ARCH_HAS_CACHE_LINE_SIZE=y +CONFIG_ARCH_HAS_PTE_DEVMAP=y +CONFIG_ARCH_HAS_ZONE_DMA_SET=y +CONFIG_ZONE_DMA=y +CONFIG_ZONE_DMA32=y +CONFIG_ZONE_DEVICE=y +CONFIG_DEV_PAGEMAP_OPS=y +# CONFIG_DEVICE_PRIVATE is not set +CONFIG_PERCPU_STATS=y +# CONFIG_GUP_TEST is not set +# CONFIG_READ_ONLY_THP_FOR_FS is not set +CONFIG_ARCH_HAS_PTE_SPECIAL=y +CONFIG_SECRETMEM=y + +# +# Data Access Monitoring +# +# CONFIG_DAMON is not set +# end of Data Access Monitoring +# end of Memory Management options + +CONFIG_NET=y +CONFIG_SKB_EXTENSIONS=y + +# +# Networking options +# +CONFIG_PACKET=y +CONFIG_PACKET_DIAG=y +CONFIG_UNIX=y +CONFIG_UNIX_SCM=y +CONFIG_AF_UNIX_OOB=y +CONFIG_UNIX_DIAG=y +CONFIG_TLS=y +# CONFIG_TLS_DEVICE is not set +# CONFIG_TLS_TOE is not set +CONFIG_XFRM=y +CONFIG_XFRM_ALGO=y +CONFIG_XFRM_USER=y +CONFIG_XFRM_SUB_POLICY=y +CONFIG_XFRM_MIGRATE=y +CONFIG_XFRM_STATISTICS=y +CONFIG_NET_KEY=y +CONFIG_NET_KEY_MIGRATE=y +# CONFIG_XDP_SOCKETS is not set +CONFIG_INET=y +# CONFIG_IP_MULTICAST is not set +# CONFIG_IP_ADVANCED_ROUTER is not set +CONFIG_IP_PNP=y +CONFIG_IP_PNP_DHCP=y +CONFIG_IP_PNP_BOOTP=y +CONFIG_IP_PNP_RARP=y +# CONFIG_NET_IPIP is not set +# CONFIG_NET_IPGRE_DEMUX is not set +CONFIG_SYN_COOKIES=y +# CONFIG_NET_IPVTI is not set +# CONFIG_NET_FOU is not set +# CONFIG_INET_AH is not set +# CONFIG_INET_ESP is not set +# CONFIG_INET_IPCOMP is not set +CONFIG_INET_DIAG=y +CONFIG_INET_TCP_DIAG=y +CONFIG_INET_UDP_DIAG=y +CONFIG_INET_RAW_DIAG=y +# CONFIG_INET_DIAG_DESTROY is not set +# CONFIG_TCP_CONG_ADVANCED is not set +CONFIG_TCP_CONG_CUBIC=y +CONFIG_DEFAULT_TCP_CONG="cubic" +# CONFIG_TCP_MD5SIG is not set +# CONFIG_IPV6 is not set +# CONFIG_MPTCP is not set +# CONFIG_NETWORK_SECMARK is not set +CONFIG_NET_PTP_CLASSIFY=y +# CONFIG_NETWORK_PHY_TIMESTAMPING is not set +# CONFIG_NETFILTER is not set +# CONFIG_BPFILTER is not set +# CONFIG_IP_DCCP is not set +# CONFIG_IP_SCTP is not set +# CONFIG_RDS is not set +# CONFIG_TIPC is not set +# CONFIG_ATM is not set +# CONFIG_L2TP is not set +# CONFIG_BRIDGE is not set +# CONFIG_NET_DSA is not set +# CONFIG_VLAN_8021Q is not set +# CONFIG_DECNET is not set +# CONFIG_LLC2 is not set +# CONFIG_ATALK is not set +# CONFIG_X25 is not set +# CONFIG_LAPB is not set +# CONFIG_PHONET is not set +# CONFIG_IEEE802154 is not set +# CONFIG_NET_SCHED is not set +# CONFIG_DCB is not set +# CONFIG_DNS_RESOLVER is not set +# CONFIG_BATMAN_ADV is not set +# CONFIG_OPENVSWITCH is not set +CONFIG_VSOCKETS=y +CONFIG_VSOCKETS_DIAG=y +# CONFIG_VSOCKETS_LOOPBACK is not set +CONFIG_VIRTIO_VSOCKETS=y +CONFIG_VIRTIO_VSOCKETS_COMMON=y +# CONFIG_HYPERV_VSOCKETS is not set +# CONFIG_NETLINK_DIAG is not set +# CONFIG_MPLS is not set +# CONFIG_NET_NSH is not set +# CONFIG_HSR is not set +# CONFIG_NET_SWITCHDEV is not set +# CONFIG_NET_L3_MASTER_DEV is not set +# CONFIG_QRTR is not set +# CONFIG_NET_NCSI is not set +CONFIG_PCPU_DEV_REFCNT=y +CONFIG_RPS=y +CONFIG_RFS_ACCEL=y +CONFIG_SOCK_RX_QUEUE_MAPPING=y +CONFIG_XPS=y +CONFIG_CGROUP_NET_PRIO=y +CONFIG_CGROUP_NET_CLASSID=y +CONFIG_NET_RX_BUSY_POLL=y +CONFIG_BQL=y +# CONFIG_BPF_STREAM_PARSER is not set +CONFIG_NET_FLOW_LIMIT=y + +# +# Network testing +# +# CONFIG_NET_PKTGEN is not set +# end of Network testing +# end of Networking options + +# CONFIG_HAMRADIO is not set +# CONFIG_CAN is not set +# CONFIG_BT is not set +# CONFIG_AF_RXRPC is not set +# CONFIG_AF_KCM is not set +CONFIG_STREAM_PARSER=y +# CONFIG_MCTP is not set +# CONFIG_WIRELESS is not set +# CONFIG_RFKILL is not set +# CONFIG_NET_9P is not set +# CONFIG_CAIF is not set +# CONFIG_CEPH_LIB is not set +# CONFIG_NFC is not set +# CONFIG_PSAMPLE is not set +# CONFIG_NET_IFE is not set +# CONFIG_LWTUNNEL is not set +CONFIG_GRO_CELLS=y +CONFIG_NET_SOCK_MSG=y +CONFIG_FAILOVER=y +# CONFIG_ETHTOOL_NETLINK is not set + +# +# Device Drivers +# +CONFIG_HAVE_EISA=y +# CONFIG_EISA is not set +CONFIG_HAVE_PCI=y +CONFIG_PCI=y +CONFIG_PCI_DOMAINS=y +CONFIG_PCIEPORTBUS=y +# CONFIG_HOTPLUG_PCI_PCIE is not set +CONFIG_PCIEAER=y +# CONFIG_PCIEAER_INJECT is not set +# CONFIG_PCIE_ECRC is not set +CONFIG_PCIEASPM=y +CONFIG_PCIEASPM_DEFAULT=y +# CONFIG_PCIEASPM_POWERSAVE is not set +# CONFIG_PCIEASPM_POWER_SUPERSAVE is not set +# CONFIG_PCIEASPM_PERFORMANCE is not set +CONFIG_PCIE_PME=y +# CONFIG_PCIE_DPC is not set +# CONFIG_PCIE_PTM is not set +CONFIG_PCI_MSI=y +CONFIG_PCI_MSI_IRQ_DOMAIN=y +CONFIG_PCI_QUIRKS=y +CONFIG_PCI_DEBUG=y +CONFIG_PCI_STUB=y +CONFIG_PCI_LOCKLESS_CONFIG=y +# CONFIG_PCI_IOV is not set +# CONFIG_PCI_PRI is not set +# CONFIG_PCI_PASID is not set +# CONFIG_PCI_P2PDMA is not set +CONFIG_PCI_LABEL=y +# CONFIG_PCI_HYPERV is not set +# CONFIG_PCIE_BUS_TUNE_OFF is not set +CONFIG_PCIE_BUS_DEFAULT=y +# CONFIG_PCIE_BUS_SAFE is not set +# CONFIG_PCIE_BUS_PERFORMANCE is not set +# CONFIG_PCIE_BUS_PEER2PEER is not set +CONFIG_HOTPLUG_PCI=y +CONFIG_HOTPLUG_PCI_ACPI=y +# CONFIG_HOTPLUG_PCI_ACPI_IBM is not set +# CONFIG_HOTPLUG_PCI_CPCI is not set +# CONFIG_HOTPLUG_PCI_SHPC is not set + +# +# PCI controller drivers +# +# CONFIG_VMD is not set +# CONFIG_PCI_HYPERV_INTERFACE is not set + +# +# DesignWare PCI Core Support +# +# CONFIG_PCIE_DW_PLAT_HOST is not set +# CONFIG_PCI_MESON is not set +# end of DesignWare PCI Core Support + +# +# Mobiveil PCIe Core Support +# +# end of Mobiveil PCIe Core Support + +# +# Cadence PCIe controllers support +# +# end of Cadence PCIe controllers support +# end of PCI controller drivers + +# +# PCI Endpoint +# +# CONFIG_PCI_ENDPOINT is not set +# end of PCI Endpoint + +# +# PCI switch controller drivers +# +# CONFIG_PCI_SW_SWITCHTEC is not set +# end of PCI switch controller drivers + +# CONFIG_CXL_BUS is not set +# CONFIG_PCCARD is not set +# CONFIG_RAPIDIO is not set + +# +# Generic Driver Options +# +CONFIG_UEVENT_HELPER=y +CONFIG_UEVENT_HELPER_PATH="/sbin/hotplug" +CONFIG_DEVTMPFS=y +CONFIG_DEVTMPFS_MOUNT=y +CONFIG_STANDALONE=y +CONFIG_PREVENT_FIRMWARE_BUILD=y + +# +# Firmware loader +# +CONFIG_FW_LOADER=y +CONFIG_FW_LOADER_PAGED_BUF=y +CONFIG_EXTRA_FIRMWARE="" +CONFIG_FW_LOADER_USER_HELPER=y +# CONFIG_FW_LOADER_USER_HELPER_FALLBACK is not set +# CONFIG_FW_LOADER_COMPRESS is not set +# CONFIG_FW_CACHE is not set +# end of Firmware loader + +CONFIG_ALLOW_DEV_COREDUMP=y +# CONFIG_DEBUG_DRIVER is not set +# CONFIG_DEBUG_DEVRES is not set +# CONFIG_DEBUG_TEST_DRIVER_REMOVE is not set +CONFIG_GENERIC_CPU_AUTOPROBE=y +CONFIG_GENERIC_CPU_VULNERABILITIES=y +# end of Generic Driver Options + +# +# Bus devices +# +# CONFIG_MHI_BUS is not set +# end of Bus devices + +CONFIG_CONNECTOR=y +CONFIG_PROC_EVENTS=y + +# +# Firmware Drivers +# + +# +# ARM System Control and Management Interface Protocol +# +# end of ARM System Control and Management Interface Protocol + +# CONFIG_EDD is not set +CONFIG_FIRMWARE_MEMMAP=y +CONFIG_DMIID=y +CONFIG_DMI_SYSFS=y +CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK=y +# CONFIG_FW_CFG_SYSFS is not set +CONFIG_SYSFB=y +# CONFIG_SYSFB_SIMPLEFB is not set +# CONFIG_GOOGLE_FIRMWARE is not set + +# +# EFI (Extensible Firmware Interface) Support +# +# CONFIG_EFI_VARS is not set +CONFIG_EFI_ESRT=y +CONFIG_EFI_RUNTIME_MAP=y +# CONFIG_EFI_FAKE_MEMMAP is not set +CONFIG_EFI_RUNTIME_WRAPPERS=y +CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER=y +# CONFIG_EFI_BOOTLOADER_CONTROL is not set +# CONFIG_EFI_CAPSULE_LOADER is not set +# CONFIG_EFI_TEST is not set +# CONFIG_APPLE_PROPERTIES is not set +# CONFIG_RESET_ATTACK_MITIGATION is not set +# CONFIG_EFI_RCI2_TABLE is not set +# CONFIG_EFI_DISABLE_PCI_DMA is not set +# end of EFI (Extensible Firmware Interface) Support + +CONFIG_EFI_EARLYCON=y +# CONFIG_EFI_CUSTOM_SSDT_OVERLAYS is not set + +# +# Tegra firmware driver +# +# end of Tegra firmware driver +# end of Firmware Drivers + +# CONFIG_GNSS is not set +# CONFIG_MTD is not set +# CONFIG_OF is not set +CONFIG_ARCH_MIGHT_HAVE_PC_PARPORT=y +# CONFIG_PARPORT is not set +CONFIG_PNP=y +# CONFIG_PNP_DEBUG_MESSAGES is not set + +# +# Protocols +# +CONFIG_PNPACPI=y +CONFIG_BLK_DEV=y +CONFIG_BLK_DEV_NULL_BLK=y +# CONFIG_BLK_DEV_PCIESSD_MTIP32XX is not set +CONFIG_ZRAM=y +CONFIG_ZRAM_DEF_COMP_LZORLE=y +# CONFIG_ZRAM_DEF_COMP_LZO is not set +CONFIG_ZRAM_DEF_COMP="lzo-rle" +# CONFIG_ZRAM_WRITEBACK is not set +# CONFIG_ZRAM_MEMORY_TRACKING is not set +CONFIG_BLK_DEV_LOOP=y +CONFIG_BLK_DEV_LOOP_MIN_COUNT=8 +CONFIG_BLK_DEV_CRYPTOLOOP=y +# CONFIG_BLK_DEV_DRBD is not set +# CONFIG_BLK_DEV_NBD is not set +# CONFIG_BLK_DEV_SX8 is not set +CONFIG_BLK_DEV_RAM=y +CONFIG_BLK_DEV_RAM_COUNT=16 +CONFIG_BLK_DEV_RAM_SIZE=16384 +# CONFIG_CDROM_PKTCDVD is not set +# CONFIG_ATA_OVER_ETH is not set +CONFIG_VIRTIO_BLK=y +# CONFIG_BLK_DEV_RBD is not set +# CONFIG_BLK_DEV_RSXX is not set + +# +# NVME Support +# +CONFIG_NVME_CORE=y +CONFIG_BLK_DEV_NVME=y +CONFIG_NVME_MULTIPATH=y +# CONFIG_NVME_FC is not set +# CONFIG_NVME_TCP is not set +# CONFIG_NVME_TARGET is not set +# end of NVME Support + +# +# Misc devices +# +# CONFIG_DUMMY_IRQ is not set +# CONFIG_IBM_ASM is not set +# CONFIG_PHANTOM is not set +# CONFIG_TIFM_CORE is not set +# CONFIG_ENCLOSURE_SERVICES is not set +# CONFIG_HP_ILO is not set +# CONFIG_SRAM is not set +# CONFIG_DW_XDATA_PCIE is not set +# CONFIG_PCI_ENDPOINT_TEST is not set +# CONFIG_XILINX_SDFEC is not set +# CONFIG_C2PORT is not set + +# +# EEPROM support +# +# CONFIG_EEPROM_93CX6 is not set +# end of EEPROM support + +# CONFIG_CB710_CORE is not set + +# +# Texas Instruments shared transport line discipline +# +# CONFIG_TI_ST is not set +# end of Texas Instruments shared transport line discipline + +# +# Altera FPGA firmware download module (requires I2C) +# +# CONFIG_INTEL_MEI is not set +# CONFIG_INTEL_MEI_ME is not set +# CONFIG_INTEL_MEI_TXE is not set +# CONFIG_VMWARE_VMCI is not set +# CONFIG_GENWQE is not set +# CONFIG_ECHO is not set +# CONFIG_BCM_VK is not set +# CONFIG_MISC_ALCOR_PCI is not set +# CONFIG_MISC_RTSX_PCI is not set +# CONFIG_HABANA_AI is not set +# CONFIG_UACCE is not set +# CONFIG_PVPANIC is not set +# end of Misc devices + +# +# SCSI device support +# +CONFIG_SCSI_MOD=y +# CONFIG_RAID_ATTRS is not set +# CONFIG_SCSI is not set +# end of SCSI device support + +# CONFIG_ATA is not set +CONFIG_MD=y +# CONFIG_BLK_DEV_MD is not set +# CONFIG_BCACHE is not set +CONFIG_BLK_DEV_DM_BUILTIN=y +CONFIG_BLK_DEV_DM=y +# CONFIG_DM_DEBUG is not set +# CONFIG_DM_UNSTRIPED is not set +# CONFIG_DM_CRYPT is not set +# CONFIG_DM_SNAPSHOT is not set +# CONFIG_DM_THIN_PROVISIONING is not set +# CONFIG_DM_CACHE is not set +# CONFIG_DM_WRITECACHE is not set +# CONFIG_DM_EBS is not set +# CONFIG_DM_ERA is not set +# CONFIG_DM_CLONE is not set +# CONFIG_DM_MIRROR is not set +# CONFIG_DM_RAID is not set +# CONFIG_DM_ZERO is not set +CONFIG_DM_MULTIPATH=y +# CONFIG_DM_MULTIPATH_QL is not set +# CONFIG_DM_MULTIPATH_ST is not set +# CONFIG_DM_MULTIPATH_HST is not set +# CONFIG_DM_MULTIPATH_IOA is not set +# CONFIG_DM_DELAY is not set +# CONFIG_DM_DUST is not set +# CONFIG_DM_INIT is not set +# CONFIG_DM_UEVENT is not set +# CONFIG_DM_FLAKEY is not set +# CONFIG_DM_VERITY is not set +# CONFIG_DM_SWITCH is not set +# CONFIG_DM_LOG_WRITES is not set +# CONFIG_DM_INTEGRITY is not set +# CONFIG_TARGET_CORE is not set +# CONFIG_FUSION is not set + +# +# IEEE 1394 (FireWire) support +# +# CONFIG_FIREWIRE is not set +# CONFIG_FIREWIRE_NOSY is not set +# end of IEEE 1394 (FireWire) support + +# CONFIG_MACINTOSH_DRIVERS is not set +CONFIG_NETDEVICES=y +CONFIG_NET_CORE=y +# CONFIG_BONDING is not set +# CONFIG_DUMMY is not set +# CONFIG_WIREGUARD is not set +# CONFIG_EQUALIZER is not set +# CONFIG_NET_TEAM is not set +# CONFIG_MACVLAN is not set +# CONFIG_IPVLAN is not set +# CONFIG_VXLAN is not set +# CONFIG_GENEVE is not set +# CONFIG_BAREUDP is not set +# CONFIG_GTP is not set +# CONFIG_MACSEC is not set +# CONFIG_NETCONSOLE is not set +CONFIG_TUN=y +# CONFIG_TUN_VNET_CROSS_LE is not set +CONFIG_VETH=y +CONFIG_VIRTIO_NET=y +# CONFIG_NLMON is not set +# CONFIG_ARCNET is not set +# CONFIG_ETHERNET is not set +# CONFIG_FDDI is not set +# CONFIG_HIPPI is not set +# CONFIG_NET_SB1000 is not set +# CONFIG_PHYLIB is not set +# CONFIG_MDIO_DEVICE is not set + +# +# PCS device drivers +# +# end of PCS device drivers + +# CONFIG_PPP is not set +# CONFIG_SLIP is not set + +# +# Host-side USB support is needed for USB Network Adapter support +# +# CONFIG_WLAN is not set +# CONFIG_WAN is not set + +# +# Wireless WAN +# +# CONFIG_WWAN is not set +# end of Wireless WAN + +# CONFIG_VMXNET3 is not set +# CONFIG_FUJITSU_ES is not set +# CONFIG_HYPERV_NET is not set +# CONFIG_NETDEVSIM is not set +CONFIG_NET_FAILOVER=y +# CONFIG_ISDN is not set + +# +# Input device support +# +CONFIG_INPUT=y +CONFIG_INPUT_FF_MEMLESS=y +CONFIG_INPUT_SPARSEKMAP=y +# CONFIG_INPUT_MATRIXKMAP is not set + +# +# Userland interfaces +# +CONFIG_INPUT_MOUSEDEV=y +CONFIG_INPUT_MOUSEDEV_PSAUX=y +CONFIG_INPUT_MOUSEDEV_SCREEN_X=1024 +CONFIG_INPUT_MOUSEDEV_SCREEN_Y=768 +# CONFIG_INPUT_JOYDEV is not set +CONFIG_INPUT_EVDEV=y +# CONFIG_INPUT_EVBUG is not set + +# +# Input Device Drivers +# +CONFIG_INPUT_KEYBOARD=y +# CONFIG_KEYBOARD_ATKBD is not set +# CONFIG_KEYBOARD_LKKBD is not set +# CONFIG_KEYBOARD_GPIO is not set +# CONFIG_KEYBOARD_GPIO_POLLED is not set +# CONFIG_KEYBOARD_MATRIX is not set +# CONFIG_KEYBOARD_NEWTON is not set +# CONFIG_KEYBOARD_OPENCORES is not set +# CONFIG_KEYBOARD_SAMSUNG is not set +# CONFIG_KEYBOARD_STOWAWAY is not set +# CONFIG_KEYBOARD_SUNKBD is not set +# CONFIG_KEYBOARD_XTKBD is not set +# CONFIG_INPUT_MOUSE is not set +# CONFIG_INPUT_JOYSTICK is not set +# CONFIG_INPUT_TABLET is not set +# CONFIG_INPUT_TOUCHSCREEN is not set +CONFIG_INPUT_MISC=y +# CONFIG_INPUT_AD714X is not set +# CONFIG_INPUT_E3X0_BUTTON is not set +# CONFIG_INPUT_PCSPKR is not set +# CONFIG_INPUT_GPIO_BEEPER is not set +# CONFIG_INPUT_GPIO_DECODER is not set +# CONFIG_INPUT_GPIO_VIBRA is not set +# CONFIG_INPUT_ATLAS_BTNS is not set +CONFIG_INPUT_UINPUT=y +# CONFIG_INPUT_GPIO_ROTARY_ENCODER is not set +# CONFIG_INPUT_ADXL34X is not set +# CONFIG_INPUT_CMA3000 is not set +# CONFIG_RMI4_CORE is not set + +# +# Hardware I/O ports +# +# CONFIG_SERIO is not set +CONFIG_ARCH_MIGHT_HAVE_PC_SERIO=y +# CONFIG_GAMEPORT is not set +# end of Hardware I/O ports +# end of Input device support + +# +# Character devices +# +CONFIG_TTY=y +CONFIG_VT=y +CONFIG_CONSOLE_TRANSLATIONS=y +CONFIG_VT_CONSOLE=y +CONFIG_VT_CONSOLE_SLEEP=y +CONFIG_HW_CONSOLE=y +CONFIG_VT_HW_CONSOLE_BINDING=y +CONFIG_UNIX98_PTYS=y +# CONFIG_LEGACY_PTYS is not set +# CONFIG_LDISC_AUTOLOAD is not set + +# +# Serial drivers +# +CONFIG_SERIAL_EARLYCON=y +CONFIG_SERIAL_8250=y +# CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set +CONFIG_SERIAL_8250_PNP=y +# CONFIG_SERIAL_8250_16550A_VARIANTS is not set +# CONFIG_SERIAL_8250_FINTEK is not set +CONFIG_SERIAL_8250_CONSOLE=y +CONFIG_SERIAL_8250_DMA=y +CONFIG_SERIAL_8250_PCI=y +CONFIG_SERIAL_8250_EXAR=y +CONFIG_SERIAL_8250_NR_UARTS=1 +CONFIG_SERIAL_8250_RUNTIME_UARTS=1 +# CONFIG_SERIAL_8250_EXTENDED is not set +CONFIG_SERIAL_8250_DWLIB=y +# CONFIG_SERIAL_8250_DW is not set +# CONFIG_SERIAL_8250_RT288X is not set +CONFIG_SERIAL_8250_LPSS=y +CONFIG_SERIAL_8250_MID=y + +# +# Non-8250 serial port support +# +# CONFIG_SERIAL_UARTLITE is not set +CONFIG_SERIAL_CORE=y +CONFIG_SERIAL_CORE_CONSOLE=y +# CONFIG_SERIAL_JSM is not set +# CONFIG_SERIAL_LANTIQ is not set +# CONFIG_SERIAL_SCCNXP is not set +# CONFIG_SERIAL_BCM63XX is not set +# CONFIG_SERIAL_ALTERA_JTAGUART is not set +# CONFIG_SERIAL_ALTERA_UART is not set +CONFIG_SERIAL_ARC=y +# CONFIG_SERIAL_ARC_CONSOLE is not set +CONFIG_SERIAL_ARC_NR_PORTS=1 +# CONFIG_SERIAL_RP2 is not set +# CONFIG_SERIAL_FSL_LPUART is not set +# CONFIG_SERIAL_FSL_LINFLEXUART is not set +# CONFIG_SERIAL_SPRD is not set +# end of Serial drivers + +CONFIG_SERIAL_MCTRL_GPIO=y +# CONFIG_SERIAL_NONSTANDARD is not set +# CONFIG_N_GSM is not set +# CONFIG_NOZOMI is not set +# CONFIG_NULL_TTY is not set +CONFIG_HVC_DRIVER=y +CONFIG_SERIAL_DEV_BUS=y +CONFIG_SERIAL_DEV_CTRL_TTYPORT=y +# CONFIG_TTY_PRINTK is not set +CONFIG_VIRTIO_CONSOLE=y +# CONFIG_IPMI_HANDLER is not set +CONFIG_HW_RANDOM=y +# CONFIG_HW_RANDOM_TIMERIOMEM is not set +CONFIG_HW_RANDOM_INTEL=y +CONFIG_HW_RANDOM_AMD=y +# CONFIG_HW_RANDOM_BA431 is not set +CONFIG_HW_RANDOM_VIA=y +CONFIG_HW_RANDOM_VIRTIO=y +# CONFIG_HW_RANDOM_XIPHERA is not set +# CONFIG_APPLICOM is not set +# CONFIG_MWAVE is not set +CONFIG_DEVMEM=y +CONFIG_NVRAM=y +CONFIG_DEVPORT=y +# CONFIG_HPET is not set +CONFIG_HANGCHECK_TIMER=y +# CONFIG_TCG_TPM is not set +# CONFIG_TELCLOCK is not set +# CONFIG_XILLYBUS is not set +# CONFIG_RANDOM_TRUST_CPU is not set +# CONFIG_RANDOM_TRUST_BOOTLOADER is not set +# end of Character devices + +# +# I2C support +# +# CONFIG_I2C is not set +# end of I2C support + +# CONFIG_I3C is not set +# CONFIG_SPI is not set +# CONFIG_SPMI is not set +# CONFIG_HSI is not set +CONFIG_PPS=y +# CONFIG_PPS_DEBUG is not set + +# +# PPS clients support +# +# CONFIG_PPS_CLIENT_KTIMER is not set +# CONFIG_PPS_CLIENT_LDISC is not set +# CONFIG_PPS_CLIENT_GPIO is not set + +# +# PPS generators support +# + +# +# PTP clock support +# +CONFIG_PTP_1588_CLOCK=y +CONFIG_PTP_1588_CLOCK_OPTIONAL=y + +# +# Enable PHYLIB and NETWORK_PHY_TIMESTAMPING to see the additional clocks. +# +CONFIG_PTP_1588_CLOCK_KVM=y +# CONFIG_PTP_1588_CLOCK_VMW is not set +# end of PTP clock support + +# CONFIG_PINCTRL is not set +CONFIG_GPIOLIB=y +CONFIG_GPIOLIB_FASTPATH_LIMIT=512 +CONFIG_GPIO_ACPI=y +CONFIG_GPIOLIB_IRQCHIP=y +# CONFIG_DEBUG_GPIO is not set +CONFIG_GPIO_SYSFS=y +CONFIG_GPIO_CDEV=y +CONFIG_GPIO_CDEV_V1=y + +# +# Memory mapped GPIO drivers +# +# CONFIG_GPIO_AMDPT is not set +# CONFIG_GPIO_DWAPB is not set +# CONFIG_GPIO_EXAR is not set +# CONFIG_GPIO_GENERIC_PLATFORM is not set +# CONFIG_GPIO_MB86S7X is not set +# CONFIG_GPIO_VX855 is not set +# CONFIG_GPIO_AMD_FCH is not set +# end of Memory mapped GPIO drivers + +# +# Port-mapped I/O GPIO drivers +# +# CONFIG_GPIO_F7188X is not set +# CONFIG_GPIO_IT87 is not set +# CONFIG_GPIO_SCH311X is not set +# CONFIG_GPIO_WINBOND is not set +# CONFIG_GPIO_WS16C48 is not set +# end of Port-mapped I/O GPIO drivers + +# +# MFD GPIO expanders +# +# end of MFD GPIO expanders + +# +# PCI GPIO expanders +# +# CONFIG_GPIO_AMD8111 is not set +# CONFIG_GPIO_BT8XX is not set +# CONFIG_GPIO_ML_IOH is not set +CONFIG_GPIO_PCI_IDIO_16=y +# CONFIG_GPIO_PCIE_IDIO_24 is not set +# CONFIG_GPIO_RDC321X is not set +# end of PCI GPIO expanders + +# +# Virtual GPIO drivers +# +# CONFIG_GPIO_AGGREGATOR is not set +# CONFIG_GPIO_MOCKUP is not set +# CONFIG_GPIO_VIRTIO is not set +# end of Virtual GPIO drivers + +# CONFIG_W1 is not set +# CONFIG_POWER_RESET is not set +# CONFIG_POWER_SUPPLY is not set +# CONFIG_HWMON is not set +CONFIG_THERMAL=y +# CONFIG_THERMAL_NETLINK is not set +# CONFIG_THERMAL_STATISTICS is not set +CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS=0 +CONFIG_THERMAL_WRITABLE_TRIPS=y +CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE=y +# CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE is not set +# CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE is not set +CONFIG_THERMAL_GOV_FAIR_SHARE=y +CONFIG_THERMAL_GOV_STEP_WISE=y +# CONFIG_THERMAL_GOV_BANG_BANG is not set +CONFIG_THERMAL_GOV_USER_SPACE=y +# CONFIG_THERMAL_EMULATION is not set + +# +# Intel thermal drivers +# +# CONFIG_INTEL_POWERCLAMP is not set +CONFIG_X86_THERMAL_VECTOR=y +# CONFIG_X86_PKG_TEMP_THERMAL is not set +# CONFIG_INTEL_SOC_DTS_THERMAL is not set + +# +# ACPI INT340X thermal drivers +# +# CONFIG_INT340X_THERMAL is not set +# end of ACPI INT340X thermal drivers + +# CONFIG_INTEL_PCH_THERMAL is not set +# CONFIG_INTEL_TCC_COOLING is not set +# CONFIG_INTEL_MENLOW is not set +# end of Intel thermal drivers + +CONFIG_WATCHDOG=y +CONFIG_WATCHDOG_CORE=y +# CONFIG_WATCHDOG_NOWAYOUT is not set +CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED=y +CONFIG_WATCHDOG_OPEN_TIMEOUT=0 +# CONFIG_WATCHDOG_SYSFS is not set +# CONFIG_WATCHDOG_HRTIMER_PRETIMEOUT is not set + +# +# Watchdog Pretimeout Governors +# +# CONFIG_WATCHDOG_PRETIMEOUT_GOV is not set + +# +# Watchdog Device Drivers +# +# CONFIG_SOFT_WATCHDOG is not set +# CONFIG_WDAT_WDT is not set +# CONFIG_XILINX_WATCHDOG is not set +# CONFIG_CADENCE_WATCHDOG is not set +# CONFIG_DW_WATCHDOG is not set +# CONFIG_MAX63XX_WATCHDOG is not set +# CONFIG_ACQUIRE_WDT is not set +# CONFIG_ADVANTECH_WDT is not set +# CONFIG_ALIM1535_WDT is not set +# CONFIG_ALIM7101_WDT is not set +# CONFIG_EBC_C384_WDT is not set +# CONFIG_F71808E_WDT is not set +# CONFIG_SP5100_TCO is not set +# CONFIG_SBC_FITPC2_WATCHDOG is not set +# CONFIG_EUROTECH_WDT is not set +# CONFIG_IB700_WDT is not set +# CONFIG_IBMASR is not set +# CONFIG_WAFER_WDT is not set +# CONFIG_I6300ESB_WDT is not set +# CONFIG_IE6XX_WDT is not set +# CONFIG_ITCO_WDT is not set +# CONFIG_IT8712F_WDT is not set +# CONFIG_IT87_WDT is not set +# CONFIG_HP_WATCHDOG is not set +# CONFIG_SC1200_WDT is not set +# CONFIG_PC87413_WDT is not set +# CONFIG_NV_TCO is not set +# CONFIG_60XX_WDT is not set +# CONFIG_CPU5_WDT is not set +# CONFIG_SMSC_SCH311X_WDT is not set +# CONFIG_SMSC37B787_WDT is not set +# CONFIG_TQMX86_WDT is not set +# CONFIG_VIA_WDT is not set +# CONFIG_W83627HF_WDT is not set +# CONFIG_W83877F_WDT is not set +# CONFIG_W83977F_WDT is not set +# CONFIG_MACHZ_WDT is not set +# CONFIG_SBC_EPX_C3_WATCHDOG is not set +# CONFIG_NI903X_WDT is not set +# CONFIG_NIC7018_WDT is not set +# CONFIG_MEN_A21_WDT is not set +CONFIG_VIRTIO_WDT=y + +# +# PCI-based Watchdog Cards +# +# CONFIG_PCIPCWATCHDOG is not set +# CONFIG_WDTPCI is not set +CONFIG_SSB_POSSIBLE=y +# CONFIG_SSB is not set +CONFIG_BCMA_POSSIBLE=y +# CONFIG_BCMA is not set + +# +# Multifunction device drivers +# +# CONFIG_MFD_MADERA is not set +# CONFIG_HTC_PASIC3 is not set +# CONFIG_MFD_INTEL_QUARK_I2C_GPIO is not set +# CONFIG_LPC_ICH is not set +# CONFIG_LPC_SCH is not set +# CONFIG_MFD_INTEL_LPSS_ACPI is not set +# CONFIG_MFD_INTEL_LPSS_PCI is not set +# CONFIG_MFD_INTEL_PMT is not set +# CONFIG_MFD_JANZ_CMODIO is not set +# CONFIG_MFD_KEMPLD is not set +# CONFIG_MFD_MT6397 is not set +# CONFIG_MFD_RDC321X is not set +# CONFIG_MFD_SM501 is not set +# CONFIG_MFD_SYSCON is not set +# CONFIG_MFD_TI_AM335X_TSCADC is not set +# CONFIG_MFD_TQMX86 is not set +# CONFIG_MFD_VX855 is not set +# CONFIG_RAVE_SP_CORE is not set +# end of Multifunction device drivers + +# CONFIG_REGULATOR is not set +# CONFIG_RC_CORE is not set +# CONFIG_MEDIA_CEC_SUPPORT is not set +# CONFIG_MEDIA_SUPPORT is not set + +# +# Graphics support +# +# CONFIG_AGP is not set +CONFIG_VGA_ARB=y +CONFIG_VGA_ARB_MAX_GPUS=16 +# CONFIG_VGA_SWITCHEROO is not set +# CONFIG_DRM is not set + +# +# ARM devices +# +# end of ARM devices + +# +# Frame buffer Devices +# +CONFIG_FB_CMDLINE=y +CONFIG_FB_NOTIFY=y +CONFIG_FB=y +# CONFIG_FIRMWARE_EDID is not set +# CONFIG_FB_FOREIGN_ENDIAN is not set +# CONFIG_FB_MODE_HELPERS is not set +# CONFIG_FB_TILEBLITTING is not set + +# +# Frame buffer hardware drivers +# +# CONFIG_FB_CIRRUS is not set +# CONFIG_FB_PM2 is not set +# CONFIG_FB_CYBER2000 is not set +# CONFIG_FB_ARC is not set +# CONFIG_FB_ASILIANT is not set +# CONFIG_FB_IMSTT is not set +# CONFIG_FB_VGA16 is not set +# CONFIG_FB_UVESA is not set +# CONFIG_FB_VESA is not set +# CONFIG_FB_EFI is not set +# CONFIG_FB_N411 is not set +# CONFIG_FB_HGA is not set +# CONFIG_FB_OPENCORES is not set +# CONFIG_FB_S1D13XXX is not set +# CONFIG_FB_NVIDIA is not set +# CONFIG_FB_RIVA is not set +# CONFIG_FB_I740 is not set +# CONFIG_FB_LE80578 is not set +# CONFIG_FB_MATROX is not set +# CONFIG_FB_RADEON is not set +# CONFIG_FB_ATY128 is not set +# CONFIG_FB_ATY is not set +# CONFIG_FB_S3 is not set +# CONFIG_FB_SAVAGE is not set +# CONFIG_FB_SIS is not set +# CONFIG_FB_NEOMAGIC is not set +# CONFIG_FB_KYRO is not set +# CONFIG_FB_3DFX is not set +# CONFIG_FB_VOODOO1 is not set +# CONFIG_FB_VT8623 is not set +# CONFIG_FB_TRIDENT is not set +# CONFIG_FB_ARK is not set +# CONFIG_FB_PM3 is not set +# CONFIG_FB_CARMINE is not set +# CONFIG_FB_IBM_GXT4500 is not set +# CONFIG_FB_VIRTUAL is not set +# CONFIG_FB_METRONOME is not set +# CONFIG_FB_MB862XX is not set +# CONFIG_FB_HYPERV is not set +# CONFIG_FB_SIMPLE is not set +# CONFIG_FB_SM712 is not set +# end of Frame buffer Devices + +# +# Backlight & LCD device support +# +CONFIG_LCD_CLASS_DEVICE=y +# CONFIG_LCD_PLATFORM is not set +CONFIG_BACKLIGHT_CLASS_DEVICE=y +# CONFIG_BACKLIGHT_KTD253 is not set +# CONFIG_BACKLIGHT_APPLE is not set +# CONFIG_BACKLIGHT_QCOM_WLED is not set +# CONFIG_BACKLIGHT_SAHARA is not set +# CONFIG_BACKLIGHT_GPIO is not set +# end of Backlight & LCD device support + +# +# Console display driver support +# +CONFIG_VGA_CONSOLE=y +CONFIG_DUMMY_CONSOLE=y +CONFIG_DUMMY_CONSOLE_COLUMNS=80 +CONFIG_DUMMY_CONSOLE_ROWS=25 +CONFIG_FRAMEBUFFER_CONSOLE=y +CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY=y +# CONFIG_FRAMEBUFFER_CONSOLE_ROTATION is not set +# CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER is not set +# end of Console display driver support + +# CONFIG_LOGO is not set +# end of Graphics support + +# CONFIG_SOUND is not set + +# +# HID support +# +CONFIG_HID=y +# CONFIG_HID_BATTERY_STRENGTH is not set +CONFIG_HIDRAW=y +CONFIG_UHID=y +CONFIG_HID_GENERIC=y + +# +# Special HID drivers +# +# CONFIG_HID_A4TECH is not set +# CONFIG_HID_ACRUX is not set +# CONFIG_HID_APPLE is not set +# CONFIG_HID_AUREAL is not set +# CONFIG_HID_BELKIN is not set +# CONFIG_HID_CHERRY is not set +# CONFIG_HID_COUGAR is not set +# CONFIG_HID_MACALLY is not set +# CONFIG_HID_CMEDIA is not set +# CONFIG_HID_CYPRESS is not set +# CONFIG_HID_DRAGONRISE is not set +# CONFIG_HID_EMS_FF is not set +# CONFIG_HID_ELECOM is not set +# CONFIG_HID_EZKEY is not set +# CONFIG_HID_GEMBIRD is not set +# CONFIG_HID_GFRM is not set +# CONFIG_HID_GLORIOUS is not set +# CONFIG_HID_VIVALDI is not set +# CONFIG_HID_KEYTOUCH is not set +# CONFIG_HID_KYE is not set +# CONFIG_HID_WALTOP is not set +# CONFIG_HID_VIEWSONIC is not set +# CONFIG_HID_GYRATION is not set +# CONFIG_HID_ICADE is not set +# CONFIG_HID_ITE is not set +# CONFIG_HID_JABRA is not set +# CONFIG_HID_TWINHAN is not set +# CONFIG_HID_KENSINGTON is not set +# CONFIG_HID_LCPOWER is not set +# CONFIG_HID_LENOVO is not set +# CONFIG_HID_MAGICMOUSE is not set +# CONFIG_HID_MALTRON is not set +# CONFIG_HID_MAYFLASH is not set +CONFIG_HID_REDRAGON=y +# CONFIG_HID_MICROSOFT is not set +# CONFIG_HID_MONTEREY is not set +# CONFIG_HID_MULTITOUCH is not set +# CONFIG_HID_NTI is not set +# CONFIG_HID_ORTEK is not set +# CONFIG_HID_PANTHERLORD is not set +# CONFIG_HID_PETALYNX is not set +# CONFIG_HID_PICOLCD is not set +# CONFIG_HID_PLANTRONICS is not set +# CONFIG_HID_PLAYSTATION is not set +# CONFIG_HID_PRIMAX is not set +# CONFIG_HID_SAITEK is not set +# CONFIG_HID_SEMITEK is not set +# CONFIG_HID_SPEEDLINK is not set +# CONFIG_HID_STEAM is not set +# CONFIG_HID_STEELSERIES is not set +# CONFIG_HID_SUNPLUS is not set +# CONFIG_HID_RMI is not set +# CONFIG_HID_GREENASIA is not set +# CONFIG_HID_HYPERV_MOUSE is not set +# CONFIG_HID_SMARTJOYPLUS is not set +# CONFIG_HID_TIVO is not set +# CONFIG_HID_TOPSEED is not set +# CONFIG_HID_UDRAW_PS3 is not set +# CONFIG_HID_XINMO is not set +# CONFIG_HID_ZEROPLUS is not set +# CONFIG_HID_ZYDACRON is not set +# CONFIG_HID_SENSOR_HUB is not set +# CONFIG_HID_ALPS is not set +# end of Special HID drivers + +# +# Intel ISH HID support +# +# CONFIG_INTEL_ISH_HID is not set +# end of Intel ISH HID support + +# +# AMD SFH HID Support +# +# CONFIG_AMD_SFH_HID is not set +# end of AMD SFH HID Support +# end of HID support + +CONFIG_USB_OHCI_LITTLE_ENDIAN=y +# CONFIG_USB_SUPPORT is not set +# CONFIG_MMC is not set +# CONFIG_MEMSTICK is not set +# CONFIG_NEW_LEDS is not set +# CONFIG_ACCESSIBILITY is not set +# CONFIG_INFINIBAND is not set +CONFIG_EDAC_ATOMIC_SCRUB=y +CONFIG_EDAC_SUPPORT=y +# CONFIG_EDAC is not set +CONFIG_RTC_LIB=y +CONFIG_RTC_MC146818_LIB=y +# CONFIG_RTC_CLASS is not set +CONFIG_DMADEVICES=y +# CONFIG_DMADEVICES_DEBUG is not set + +# +# DMA Devices +# +CONFIG_DMA_ENGINE=y +CONFIG_DMA_VIRTUAL_CHANNELS=y +CONFIG_DMA_ACPI=y +# CONFIG_ALTERA_MSGDMA is not set +# CONFIG_INTEL_IDMA64 is not set +# CONFIG_INTEL_IDXD_COMPAT is not set +# CONFIG_INTEL_IOATDMA is not set +# CONFIG_PLX_DMA is not set +# CONFIG_AMD_PTDMA is not set +# CONFIG_QCOM_HIDMA_MGMT is not set +# CONFIG_QCOM_HIDMA is not set +CONFIG_DW_DMAC_CORE=y +# CONFIG_DW_DMAC is not set +# CONFIG_DW_DMAC_PCI is not set +# CONFIG_DW_EDMA is not set +# CONFIG_DW_EDMA_PCIE is not set +CONFIG_HSU_DMA=y +# CONFIG_SF_PDMA is not set +# CONFIG_INTEL_LDMA is not set + +# +# DMA Clients +# +# CONFIG_ASYNC_TX_DMA is not set +# CONFIG_DMATEST is not set + +# +# DMABUF options +# +# CONFIG_SYNC_FILE is not set +# CONFIG_DMABUF_HEAPS is not set +# end of DMABUF options + +# CONFIG_AUXDISPLAY is not set +CONFIG_UIO=y +# CONFIG_UIO_CIF is not set +CONFIG_UIO_PDRV_GENIRQ=y +CONFIG_UIO_DMEM_GENIRQ=y +# CONFIG_UIO_AEC is not set +# CONFIG_UIO_SERCOS3 is not set +# CONFIG_UIO_PCI_GENERIC is not set +# CONFIG_UIO_NETX is not set +# CONFIG_UIO_PRUSS is not set +# CONFIG_UIO_MF624 is not set +# CONFIG_UIO_HV_GENERIC is not set +CONFIG_VFIO=y +CONFIG_VFIO_IOMMU_TYPE1=y +CONFIG_VFIO_VIRQFD=y +# CONFIG_VFIO_NOIOMMU is not set +CONFIG_VFIO_PCI_CORE=y +CONFIG_VFIO_PCI_MMAP=y +CONFIG_VFIO_PCI_INTX=y +CONFIG_VFIO_PCI=y +# CONFIG_VFIO_PCI_VGA is not set +# CONFIG_VFIO_PCI_IGD is not set +# CONFIG_VFIO_MDEV is not set +CONFIG_IRQ_BYPASS_MANAGER=y +# CONFIG_VIRT_DRIVERS is not set +CONFIG_VIRTIO=y +CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS=y +CONFIG_VIRTIO_PCI_LIB=y +CONFIG_VIRTIO_MENU=y +CONFIG_VIRTIO_PCI=y +CONFIG_VIRTIO_PCI_LEGACY=y +CONFIG_VIRTIO_PMEM=y +CONFIG_VIRTIO_BALLOON=y +CONFIG_VIRTIO_MEM=y +CONFIG_VIRTIO_INPUT=y +CONFIG_VIRTIO_MMIO=y +CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES=y +# CONFIG_VDPA is not set +# CONFIG_VHOST_MENU is not set + +# +# Microsoft Hyper-V guest support +# +CONFIG_HYPERV=y +CONFIG_HYPERV_TIMER=y +# CONFIG_HYPERV_UTILS is not set +# CONFIG_HYPERV_BALLOON is not set +# end of Microsoft Hyper-V guest support + +# CONFIG_GREYBUS is not set +# CONFIG_COMEDI is not set +# CONFIG_STAGING is not set +# CONFIG_X86_PLATFORM_DEVICES is not set +CONFIG_PMC_ATOM=y +# CONFIG_CHROME_PLATFORMS is not set +# CONFIG_MELLANOX_PLATFORM is not set +# CONFIG_SURFACE_PLATFORMS is not set +CONFIG_HAVE_CLK=y +CONFIG_HAVE_CLK_PREPARE=y +CONFIG_COMMON_CLK=y + +# +# Clock driver for ARM Reference designs +# +# CONFIG_ICST is not set +# CONFIG_CLK_SP810 is not set +# end of Clock driver for ARM Reference designs + +# CONFIG_XILINX_VCU is not set +# CONFIG_HWSPINLOCK is not set + +# +# Clock Source drivers +# +CONFIG_CLKEVT_I8253=y +CONFIG_I8253_LOCK=y +CONFIG_CLKBLD_I8253=y +# end of Clock Source drivers + +CONFIG_MAILBOX=y +CONFIG_PCC=y +# CONFIG_ALTERA_MBOX is not set +CONFIG_IOMMU_IOVA=y +CONFIG_IOMMU_API=y +CONFIG_IOMMU_SUPPORT=y + +# +# Generic IOMMU Pagetable Support +# +# end of Generic IOMMU Pagetable Support + +# CONFIG_IOMMU_DEBUGFS is not set +# CONFIG_IOMMU_DEFAULT_DMA_STRICT is not set +CONFIG_IOMMU_DEFAULT_DMA_LAZY=y +# CONFIG_IOMMU_DEFAULT_PASSTHROUGH is not set +CONFIG_IOMMU_DMA=y +# CONFIG_AMD_IOMMU is not set +# CONFIG_INTEL_IOMMU is not set +# CONFIG_IRQ_REMAP is not set +CONFIG_HYPERV_IOMMU=y +CONFIG_VIRTIO_IOMMU=y + +# +# Remoteproc drivers +# +# CONFIG_REMOTEPROC is not set +# end of Remoteproc drivers + +# +# Rpmsg drivers +# +# CONFIG_RPMSG_QCOM_GLINK_RPM is not set +# CONFIG_RPMSG_VIRTIO is not set +# end of Rpmsg drivers + +# CONFIG_SOUNDWIRE is not set + +# +# SOC (System On Chip) specific Drivers +# + +# +# Amlogic SoC drivers +# +# end of Amlogic SoC drivers + +# +# Broadcom SoC drivers +# +# end of Broadcom SoC drivers + +# +# NXP/Freescale QorIQ SoC drivers +# +# end of NXP/Freescale QorIQ SoC drivers + +# +# i.MX SoC drivers +# +# end of i.MX SoC drivers + +# +# Enable LiteX SoC Builder specific drivers +# +# end of Enable LiteX SoC Builder specific drivers + +# +# Qualcomm SoC drivers +# +# end of Qualcomm SoC drivers + +# CONFIG_SOC_TI is not set + +# +# Xilinx SoC drivers +# +# end of Xilinx SoC drivers +# end of SOC (System On Chip) specific Drivers + +# CONFIG_PM_DEVFREQ is not set +# CONFIG_EXTCON is not set +# CONFIG_MEMORY is not set +# CONFIG_IIO is not set +# CONFIG_NTB is not set +# CONFIG_VME_BUS is not set +# CONFIG_PWM is not set + +# +# IRQ chip support +# +# end of IRQ chip support + +# CONFIG_IPACK_BUS is not set +# CONFIG_RESET_CONTROLLER is not set + +# +# PHY Subsystem +# +# CONFIG_GENERIC_PHY is not set +# CONFIG_PHY_CAN_TRANSCEIVER is not set +# CONFIG_BCM_KONA_USB2_PHY is not set +# CONFIG_PHY_PXA_28NM_HSIC is not set +# CONFIG_PHY_PXA_28NM_USB2 is not set +# CONFIG_PHY_INTEL_LGM_EMMC is not set +# end of PHY Subsystem + +# CONFIG_POWERCAP is not set +# CONFIG_MCB is not set + +# +# Performance monitor support +# +# end of Performance monitor support + +CONFIG_RAS=y +# CONFIG_USB4 is not set + +# +# Android +# +# CONFIG_ANDROID is not set +# end of Android + +CONFIG_LIBNVDIMM=y +CONFIG_BLK_DEV_PMEM=y +CONFIG_ND_BLK=y +CONFIG_ND_CLAIM=y +CONFIG_ND_BTT=y +CONFIG_BTT=y +CONFIG_ND_PFN=y +CONFIG_NVDIMM_PFN=y +CONFIG_NVDIMM_DAX=y +CONFIG_DAX_DRIVER=y +CONFIG_DAX=y +# CONFIG_DEV_DAX is not set +# CONFIG_NVMEM is not set + +# +# HW tracing support +# +# CONFIG_STM is not set +# CONFIG_INTEL_TH is not set +# end of HW tracing support + +# CONFIG_FPGA is not set +# CONFIG_TEE is not set +# CONFIG_UNISYS_VISORBUS is not set +# CONFIG_SIOX is not set +# CONFIG_SLIMBUS is not set +# CONFIG_INTERCONNECT is not set +# CONFIG_COUNTER is not set +# CONFIG_MOST is not set +# end of Device Drivers + +# +# File systems +# +CONFIG_DCACHE_WORD_ACCESS=y +# CONFIG_VALIDATE_FS_PARSER is not set +CONFIG_FS_IOMAP=y +# CONFIG_EXT2_FS is not set +# CONFIG_EXT3_FS is not set +CONFIG_EXT4_FS=y +CONFIG_EXT4_USE_FOR_EXT2=y +CONFIG_EXT4_FS_POSIX_ACL=y +CONFIG_EXT4_FS_SECURITY=y +CONFIG_EXT4_DEBUG=y +CONFIG_JBD2=y +CONFIG_JBD2_DEBUG=y +CONFIG_FS_MBCACHE=y +# CONFIG_REISERFS_FS is not set +# CONFIG_JFS_FS is not set +# CONFIG_XFS_FS is not set +# CONFIG_GFS2_FS is not set +# CONFIG_OCFS2_FS is not set +# CONFIG_BTRFS_FS is not set +# CONFIG_NILFS2_FS is not set +# CONFIG_F2FS_FS is not set +CONFIG_FS_DAX=y +CONFIG_FS_DAX_PMD=y +CONFIG_FS_POSIX_ACL=y +CONFIG_EXPORTFS=y +# CONFIG_EXPORTFS_BLOCK_OPS is not set +CONFIG_FILE_LOCKING=y +CONFIG_FS_ENCRYPTION=y +CONFIG_FS_ENCRYPTION_ALGS=y +# CONFIG_FS_VERITY is not set +CONFIG_FSNOTIFY=y +CONFIG_DNOTIFY=y +CONFIG_INOTIFY_USER=y +CONFIG_FANOTIFY=y +# CONFIG_QUOTA is not set +CONFIG_AUTOFS4_FS=y +CONFIG_AUTOFS_FS=y +CONFIG_FUSE_FS=y +CONFIG_CUSE=y +CONFIG_VIRTIO_FS=y +CONFIG_FUSE_DAX=y +CONFIG_OVERLAY_FS=y +# CONFIG_OVERLAY_FS_REDIRECT_DIR is not set +CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW=y +# CONFIG_OVERLAY_FS_INDEX is not set +# CONFIG_OVERLAY_FS_XINO_AUTO is not set +# CONFIG_OVERLAY_FS_METACOPY is not set + +# +# Caches +# +CONFIG_NETFS_SUPPORT=y +# CONFIG_NETFS_STATS is not set +CONFIG_FSCACHE=y +# CONFIG_FSCACHE_STATS is not set +# CONFIG_FSCACHE_DEBUG is not set +CONFIG_CACHEFILES=y +# CONFIG_CACHEFILES_DEBUG is not set +# end of Caches + +# +# CD-ROM/DVD Filesystems +# +# CONFIG_ISO9660_FS is not set +# CONFIG_UDF_FS is not set +# end of CD-ROM/DVD Filesystems + +# +# DOS/FAT/EXFAT/NT Filesystems +# +CONFIG_FAT_FS=y +CONFIG_MSDOS_FS=y +CONFIG_VFAT_FS=y +CONFIG_FAT_DEFAULT_CODEPAGE=437 +CONFIG_FAT_DEFAULT_IOCHARSET="ascii" +# CONFIG_FAT_DEFAULT_UTF8 is not set +# CONFIG_EXFAT_FS is not set +# CONFIG_NTFS_FS is not set +# CONFIG_NTFS3_FS is not set +# end of DOS/FAT/EXFAT/NT Filesystems + +# +# Pseudo filesystems +# +CONFIG_PROC_FS=y +CONFIG_PROC_KCORE=y +CONFIG_PROC_SYSCTL=y +CONFIG_PROC_PAGE_MONITOR=y +CONFIG_PROC_CHILDREN=y +CONFIG_PROC_PID_ARCH_STATUS=y +CONFIG_KERNFS=y +CONFIG_SYSFS=y +CONFIG_TMPFS=y +CONFIG_TMPFS_POSIX_ACL=y +CONFIG_TMPFS_XATTR=y +# CONFIG_TMPFS_INODE64 is not set +CONFIG_HUGETLBFS=y +CONFIG_HUGETLB_PAGE=y +CONFIG_HUGETLB_PAGE_FREE_VMEMMAP=y +# CONFIG_HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON is not set +CONFIG_MEMFD_CREATE=y +CONFIG_ARCH_HAS_GIGANTIC_PAGE=y +CONFIG_CONFIGFS_FS=y +CONFIG_EFIVAR_FS=y +# end of Pseudo filesystems + +# CONFIG_MISC_FILESYSTEMS is not set +# CONFIG_NETWORK_FILESYSTEMS is not set +CONFIG_NLS=y +CONFIG_NLS_DEFAULT="utf8" +CONFIG_NLS_CODEPAGE_437=y +CONFIG_NLS_CODEPAGE_737=y +CONFIG_NLS_CODEPAGE_775=y +CONFIG_NLS_CODEPAGE_850=y +CONFIG_NLS_CODEPAGE_852=y +CONFIG_NLS_CODEPAGE_855=y +CONFIG_NLS_CODEPAGE_857=y +CONFIG_NLS_CODEPAGE_860=y +CONFIG_NLS_CODEPAGE_861=y +CONFIG_NLS_CODEPAGE_862=y +CONFIG_NLS_CODEPAGE_863=y +CONFIG_NLS_CODEPAGE_864=y +CONFIG_NLS_CODEPAGE_865=y +CONFIG_NLS_CODEPAGE_866=y +CONFIG_NLS_CODEPAGE_869=y +CONFIG_NLS_CODEPAGE_936=y +CONFIG_NLS_CODEPAGE_950=y +CONFIG_NLS_CODEPAGE_932=y +CONFIG_NLS_CODEPAGE_949=y +CONFIG_NLS_CODEPAGE_874=y +CONFIG_NLS_ISO8859_8=y +CONFIG_NLS_CODEPAGE_1250=y +CONFIG_NLS_CODEPAGE_1251=y +CONFIG_NLS_ASCII=y +CONFIG_NLS_ISO8859_1=y +CONFIG_NLS_ISO8859_2=y +CONFIG_NLS_ISO8859_3=y +CONFIG_NLS_ISO8859_4=y +CONFIG_NLS_ISO8859_5=y +CONFIG_NLS_ISO8859_6=y +CONFIG_NLS_ISO8859_7=y +CONFIG_NLS_ISO8859_9=y +CONFIG_NLS_ISO8859_13=y +CONFIG_NLS_ISO8859_14=y +CONFIG_NLS_ISO8859_15=y +CONFIG_NLS_KOI8_R=y +CONFIG_NLS_KOI8_U=y +CONFIG_NLS_MAC_ROMAN=y +CONFIG_NLS_MAC_CELTIC=y +CONFIG_NLS_MAC_CENTEURO=y +CONFIG_NLS_MAC_CROATIAN=y +CONFIG_NLS_MAC_CYRILLIC=y +CONFIG_NLS_MAC_GAELIC=y +CONFIG_NLS_MAC_GREEK=y +CONFIG_NLS_MAC_ICELAND=y +CONFIG_NLS_MAC_INUIT=y +CONFIG_NLS_MAC_ROMANIAN=y +CONFIG_NLS_MAC_TURKISH=y +CONFIG_NLS_UTF8=y +# CONFIG_DLM is not set +# CONFIG_UNICODE is not set +CONFIG_IO_WQ=y +# end of File systems + +# +# Security options +# +CONFIG_KEYS=y +# CONFIG_KEYS_REQUEST_CACHE is not set +CONFIG_PERSISTENT_KEYRINGS=y +# CONFIG_ENCRYPTED_KEYS is not set +# CONFIG_KEY_DH_OPERATIONS is not set +# CONFIG_SECURITY_DMESG_RESTRICT is not set +# CONFIG_SECURITY is not set +CONFIG_SECURITYFS=y +CONFIG_PAGE_TABLE_ISOLATION=y +CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR=y +# CONFIG_HARDENED_USERCOPY is not set +CONFIG_FORTIFY_SOURCE=y +# CONFIG_STATIC_USERMODEHELPER is not set +# CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT is not set +CONFIG_DEFAULT_SECURITY_DAC=y +CONFIG_LSM="yama,loadpin,safesetid,integrity" + +# +# Kernel hardening options +# + +# +# Memory initialization +# +CONFIG_INIT_STACK_NONE=y +# CONFIG_INIT_ON_ALLOC_DEFAULT_ON is not set +# CONFIG_INIT_ON_FREE_DEFAULT_ON is not set +CONFIG_CC_HAS_ZERO_CALL_USED_REGS=y +# CONFIG_ZERO_CALL_USED_REGS is not set +# end of Memory initialization +# end of Kernel hardening options +# end of Security options + +CONFIG_CRYPTO=y + +# +# Crypto core or helper +# +# CONFIG_CRYPTO_FIPS is not set +CONFIG_CRYPTO_ALGAPI=y +CONFIG_CRYPTO_ALGAPI2=y +CONFIG_CRYPTO_AEAD=y +CONFIG_CRYPTO_AEAD2=y +CONFIG_CRYPTO_SKCIPHER=y +CONFIG_CRYPTO_SKCIPHER2=y +CONFIG_CRYPTO_HASH=y +CONFIG_CRYPTO_HASH2=y +CONFIG_CRYPTO_RNG=y +CONFIG_CRYPTO_RNG2=y +CONFIG_CRYPTO_RNG_DEFAULT=y +CONFIG_CRYPTO_AKCIPHER2=y +CONFIG_CRYPTO_KPP2=y +CONFIG_CRYPTO_ACOMP2=y +CONFIG_CRYPTO_MANAGER=y +CONFIG_CRYPTO_MANAGER2=y +# CONFIG_CRYPTO_USER is not set +# CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is not set +# CONFIG_CRYPTO_MANAGER_EXTRA_TESTS is not set +CONFIG_CRYPTO_GF128MUL=y +CONFIG_CRYPTO_NULL=y +CONFIG_CRYPTO_NULL2=y +# CONFIG_CRYPTO_PCRYPT is not set +CONFIG_CRYPTO_CRYPTD=y +# CONFIG_CRYPTO_AUTHENC is not set +# CONFIG_CRYPTO_TEST is not set +CONFIG_CRYPTO_SIMD=y + +# +# Public-key cryptography +# +# CONFIG_CRYPTO_RSA is not set +# CONFIG_CRYPTO_DH is not set +# CONFIG_CRYPTO_ECDH is not set +# CONFIG_CRYPTO_ECDSA is not set +# CONFIG_CRYPTO_ECRDSA is not set +# CONFIG_CRYPTO_SM2 is not set +# CONFIG_CRYPTO_CURVE25519 is not set +# CONFIG_CRYPTO_CURVE25519_X86 is not set + +# +# Authenticated Encryption with Associated Data +# +# CONFIG_CRYPTO_CCM is not set +CONFIG_CRYPTO_GCM=y +# CONFIG_CRYPTO_CHACHA20POLY1305 is not set +# CONFIG_CRYPTO_AEGIS128 is not set +# CONFIG_CRYPTO_AEGIS128_AESNI_SSE2 is not set +CONFIG_CRYPTO_SEQIV=y +CONFIG_CRYPTO_ECHAINIV=y + +# +# Block modes +# +CONFIG_CRYPTO_CBC=y +# CONFIG_CRYPTO_CFB is not set +CONFIG_CRYPTO_CTR=y +CONFIG_CRYPTO_CTS=y +CONFIG_CRYPTO_ECB=y +CONFIG_CRYPTO_LRW=y +# CONFIG_CRYPTO_OFB is not set +CONFIG_CRYPTO_PCBC=y +CONFIG_CRYPTO_XTS=y +# CONFIG_CRYPTO_KEYWRAP is not set +# CONFIG_CRYPTO_NHPOLY1305_SSE2 is not set +# CONFIG_CRYPTO_NHPOLY1305_AVX2 is not set +# CONFIG_CRYPTO_ADIANTUM is not set +# CONFIG_CRYPTO_ESSIV is not set + +# +# Hash modes +# +CONFIG_CRYPTO_CMAC=y +CONFIG_CRYPTO_HMAC=y +# CONFIG_CRYPTO_XCBC is not set +# CONFIG_CRYPTO_VMAC is not set + +# +# Digest +# +CONFIG_CRYPTO_CRC32C=y +CONFIG_CRYPTO_CRC32C_INTEL=y +# CONFIG_CRYPTO_CRC32 is not set +# CONFIG_CRYPTO_CRC32_PCLMUL is not set +# CONFIG_CRYPTO_XXHASH is not set +# CONFIG_CRYPTO_BLAKE2B is not set +# CONFIG_CRYPTO_BLAKE2S is not set +# CONFIG_CRYPTO_BLAKE2S_X86 is not set +CONFIG_CRYPTO_CRCT10DIF=y +# CONFIG_CRYPTO_CRCT10DIF_PCLMUL is not set +CONFIG_CRYPTO_GHASH=y +CONFIG_CRYPTO_POLY1305=y +# CONFIG_CRYPTO_POLY1305_X86_64 is not set +# CONFIG_CRYPTO_MD4 is not set +CONFIG_CRYPTO_MD5=y +# CONFIG_CRYPTO_MICHAEL_MIC is not set +# CONFIG_CRYPTO_RMD160 is not set +# CONFIG_CRYPTO_SHA1 is not set +# CONFIG_CRYPTO_SHA1_SSSE3 is not set +# CONFIG_CRYPTO_SHA256_SSSE3 is not set +# CONFIG_CRYPTO_SHA512_SSSE3 is not set +CONFIG_CRYPTO_SHA256=y +CONFIG_CRYPTO_SHA512=y +# CONFIG_CRYPTO_SHA3 is not set +# CONFIG_CRYPTO_SM3 is not set +# CONFIG_CRYPTO_STREEBOG is not set +# CONFIG_CRYPTO_WP512 is not set +# CONFIG_CRYPTO_GHASH_CLMUL_NI_INTEL is not set + +# +# Ciphers +# +CONFIG_CRYPTO_AES=y +# CONFIG_CRYPTO_AES_TI is not set +CONFIG_CRYPTO_AES_NI_INTEL=y +# CONFIG_CRYPTO_BLOWFISH is not set +# CONFIG_CRYPTO_BLOWFISH_X86_64 is not set +# CONFIG_CRYPTO_CAMELLIA is not set +# CONFIG_CRYPTO_CAMELLIA_X86_64 is not set +# CONFIG_CRYPTO_CAMELLIA_AESNI_AVX_X86_64 is not set +# CONFIG_CRYPTO_CAMELLIA_AESNI_AVX2_X86_64 is not set +# CONFIG_CRYPTO_CAST5 is not set +# CONFIG_CRYPTO_CAST5_AVX_X86_64 is not set +# CONFIG_CRYPTO_CAST6 is not set +# CONFIG_CRYPTO_CAST6_AVX_X86_64 is not set +CONFIG_CRYPTO_DES=y +# CONFIG_CRYPTO_DES3_EDE_X86_64 is not set +# CONFIG_CRYPTO_FCRYPT is not set +# CONFIG_CRYPTO_CHACHA20 is not set +# CONFIG_CRYPTO_CHACHA20_X86_64 is not set +# CONFIG_CRYPTO_SERPENT is not set +# CONFIG_CRYPTO_SERPENT_SSE2_X86_64 is not set +# CONFIG_CRYPTO_SERPENT_AVX_X86_64 is not set +# CONFIG_CRYPTO_SERPENT_AVX2_X86_64 is not set +# CONFIG_CRYPTO_SM4 is not set +# CONFIG_CRYPTO_SM4_AESNI_AVX_X86_64 is not set +# CONFIG_CRYPTO_SM4_AESNI_AVX2_X86_64 is not set +# CONFIG_CRYPTO_TWOFISH is not set +CONFIG_CRYPTO_TWOFISH_COMMON=y +CONFIG_CRYPTO_TWOFISH_X86_64=y +CONFIG_CRYPTO_TWOFISH_X86_64_3WAY=y +CONFIG_CRYPTO_TWOFISH_AVX_X86_64=y + +# +# Compression +# +CONFIG_CRYPTO_DEFLATE=y +CONFIG_CRYPTO_LZO=y +# CONFIG_CRYPTO_842 is not set +# CONFIG_CRYPTO_LZ4 is not set +# CONFIG_CRYPTO_LZ4HC is not set +# CONFIG_CRYPTO_ZSTD is not set + +# +# Random Number Generation +# +# CONFIG_CRYPTO_ANSI_CPRNG is not set +CONFIG_CRYPTO_DRBG_MENU=y +CONFIG_CRYPTO_DRBG_HMAC=y +CONFIG_CRYPTO_DRBG_HASH=y +CONFIG_CRYPTO_DRBG_CTR=y +CONFIG_CRYPTO_DRBG=y +CONFIG_CRYPTO_JITTERENTROPY=y +CONFIG_CRYPTO_USER_API=y +# CONFIG_CRYPTO_USER_API_HASH is not set +# CONFIG_CRYPTO_USER_API_SKCIPHER is not set +CONFIG_CRYPTO_USER_API_RNG=y +# CONFIG_CRYPTO_USER_API_RNG_CAVP is not set +# CONFIG_CRYPTO_USER_API_AEAD is not set +# CONFIG_CRYPTO_USER_API_ENABLE_OBSOLETE is not set + +# +# Crypto library routines +# +CONFIG_CRYPTO_LIB_AES=y +# CONFIG_CRYPTO_LIB_BLAKE2S is not set +# CONFIG_CRYPTO_LIB_CHACHA is not set +# CONFIG_CRYPTO_LIB_CURVE25519 is not set +CONFIG_CRYPTO_LIB_DES=y +CONFIG_CRYPTO_LIB_POLY1305_RSIZE=11 +CONFIG_CRYPTO_LIB_POLY1305_GENERIC=y +# CONFIG_CRYPTO_LIB_POLY1305 is not set +# CONFIG_CRYPTO_LIB_CHACHA20POLY1305 is not set +CONFIG_CRYPTO_LIB_SHA256=y +# CONFIG_CRYPTO_HW is not set +# CONFIG_ASYMMETRIC_KEY_TYPE is not set + +# +# Certificates for signature checking +# +CONFIG_SYSTEM_BLACKLIST_KEYRING=y +CONFIG_SYSTEM_BLACKLIST_HASH_LIST="" +# end of Certificates for signature checking + +CONFIG_BINARY_PRINTF=y + +# +# Library routines +# +# CONFIG_PACKING is not set +CONFIG_BITREVERSE=y +CONFIG_GENERIC_STRNCPY_FROM_USER=y +CONFIG_GENERIC_STRNLEN_USER=y +CONFIG_GENERIC_NET_UTILS=y +CONFIG_GENERIC_FIND_FIRST_BIT=y +# CONFIG_CORDIC is not set +# CONFIG_PRIME_NUMBERS is not set +CONFIG_RATIONAL=y +CONFIG_GENERIC_PCI_IOMAP=y +CONFIG_GENERIC_IOMAP=y +CONFIG_ARCH_USE_CMPXCHG_LOCKREF=y +CONFIG_ARCH_HAS_FAST_MULTIPLIER=y +CONFIG_ARCH_USE_SYM_ANNOTATIONS=y +CONFIG_CRC_CCITT=y +CONFIG_CRC16=y +CONFIG_CRC_T10DIF=y +# CONFIG_CRC_ITU_T is not set +CONFIG_CRC32=y +# CONFIG_CRC32_SELFTEST is not set +CONFIG_CRC32_SLICEBY8=y +# CONFIG_CRC32_SLICEBY4 is not set +# CONFIG_CRC32_SARWATE is not set +# CONFIG_CRC32_BIT is not set +# CONFIG_CRC64 is not set +# CONFIG_CRC4 is not set +# CONFIG_CRC7 is not set +CONFIG_LIBCRC32C=y +# CONFIG_CRC8 is not set +CONFIG_XXHASH=y +# CONFIG_RANDOM32_SELFTEST is not set +CONFIG_ZLIB_INFLATE=y +CONFIG_ZLIB_DEFLATE=y +CONFIG_LZO_COMPRESS=y +CONFIG_LZO_DECOMPRESS=y +CONFIG_LZ4_DECOMPRESS=y +CONFIG_ZSTD_DECOMPRESS=y +CONFIG_XZ_DEC=y +CONFIG_XZ_DEC_X86=y +CONFIG_XZ_DEC_POWERPC=y +CONFIG_XZ_DEC_IA64=y +CONFIG_XZ_DEC_ARM=y +CONFIG_XZ_DEC_ARMTHUMB=y +CONFIG_XZ_DEC_SPARC=y +CONFIG_XZ_DEC_BCJ=y +# CONFIG_XZ_DEC_TEST is not set +CONFIG_DECOMPRESS_GZIP=y +CONFIG_DECOMPRESS_BZIP2=y +CONFIG_DECOMPRESS_LZMA=y +CONFIG_DECOMPRESS_XZ=y +CONFIG_DECOMPRESS_LZO=y +CONFIG_DECOMPRESS_LZ4=y +CONFIG_DECOMPRESS_ZSTD=y +CONFIG_GENERIC_ALLOCATOR=y +CONFIG_INTERVAL_TREE=y +CONFIG_XARRAY_MULTI=y +CONFIG_ASSOCIATIVE_ARRAY=y +CONFIG_HAS_IOMEM=y +CONFIG_HAS_IOPORT_MAP=y +CONFIG_HAS_DMA=y +CONFIG_DMA_OPS=y +CONFIG_NEED_SG_DMA_LENGTH=y +CONFIG_NEED_DMA_MAP_STATE=y +CONFIG_ARCH_DMA_ADDR_T_64BIT=y +CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED=y +CONFIG_SWIOTLB=y +CONFIG_DMA_COHERENT_POOL=y +# CONFIG_DMA_API_DEBUG is not set +# CONFIG_DMA_MAP_BENCHMARK is not set +CONFIG_SGL_ALLOC=y +CONFIG_CPU_RMAP=y +CONFIG_DQL=y +CONFIG_NLATTR=y +CONFIG_IRQ_POLL=y +CONFIG_UCS2_STRING=y +CONFIG_HAVE_GENERIC_VDSO=y +CONFIG_GENERIC_GETTIMEOFDAY=y +CONFIG_GENERIC_VDSO_TIME_NS=y +CONFIG_FONT_SUPPORT=y +# CONFIG_FONTS is not set +CONFIG_FONT_8x8=y +CONFIG_FONT_8x16=y +CONFIG_ARCH_HAS_PMEM_API=y +CONFIG_MEMREGION=y +CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE=y +CONFIG_ARCH_HAS_COPY_MC=y +CONFIG_ARCH_STACKWALK=y +CONFIG_SBITMAP=y +# end of Library routines + +# +# Kernel hacking +# + +# +# printk and dmesg options +# +CONFIG_PRINTK_TIME=y +# CONFIG_PRINTK_CALLER is not set +# CONFIG_STACKTRACE_BUILD_ID is not set +CONFIG_CONSOLE_LOGLEVEL_DEFAULT=7 +CONFIG_CONSOLE_LOGLEVEL_QUIET=4 +CONFIG_MESSAGE_LOGLEVEL_DEFAULT=4 +# CONFIG_BOOT_PRINTK_DELAY is not set +CONFIG_DYNAMIC_DEBUG=y +CONFIG_DYNAMIC_DEBUG_CORE=y +CONFIG_SYMBOLIC_ERRNAME=y +# CONFIG_DEBUG_BUGVERBOSE is not set +# end of printk and dmesg options + +# +# Compile-time checks and compiler options +# +# CONFIG_DEBUG_INFO is not set +CONFIG_FRAME_WARN=2048 +CONFIG_STRIP_ASM_SYMS=y +# CONFIG_READABLE_ASM is not set +# CONFIG_HEADERS_INSTALL is not set +CONFIG_DEBUG_SECTION_MISMATCH=y +CONFIG_SECTION_MISMATCH_WARN_ONLY=y +# CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B is not set +CONFIG_FRAME_POINTER=y +CONFIG_STACK_VALIDATION=y +# CONFIG_VMLINUX_MAP is not set +# CONFIG_DEBUG_FORCE_WEAK_PER_CPU is not set +# end of Compile-time checks and compiler options + +# +# Generic Kernel Debugging Instruments +# +CONFIG_MAGIC_SYSRQ=y +CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0x1 +CONFIG_MAGIC_SYSRQ_SERIAL=y +CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE="" +CONFIG_DEBUG_FS=y +CONFIG_DEBUG_FS_ALLOW_ALL=y +# CONFIG_DEBUG_FS_DISALLOW_MOUNT is not set +# CONFIG_DEBUG_FS_ALLOW_NONE is not set +CONFIG_HAVE_ARCH_KGDB=y +# CONFIG_KGDB is not set +CONFIG_ARCH_HAS_UBSAN_SANITIZE_ALL=y +# CONFIG_UBSAN is not set +CONFIG_HAVE_ARCH_KCSAN=y +CONFIG_HAVE_KCSAN_COMPILER=y +# CONFIG_KCSAN is not set +# end of Generic Kernel Debugging Instruments + +CONFIG_DEBUG_KERNEL=y +CONFIG_DEBUG_MISC=y + +# +# Memory Debugging +# +# CONFIG_PAGE_EXTENSION is not set +# CONFIG_DEBUG_PAGEALLOC is not set +# CONFIG_PAGE_OWNER is not set +# CONFIG_PAGE_POISONING is not set +# CONFIG_DEBUG_RODATA_TEST is not set +CONFIG_ARCH_HAS_DEBUG_WX=y +# CONFIG_DEBUG_WX is not set +CONFIG_GENERIC_PTDUMP=y +# CONFIG_PTDUMP_DEBUGFS is not set +# CONFIG_DEBUG_OBJECTS is not set +# CONFIG_SLUB_DEBUG_ON is not set +# CONFIG_SLUB_STATS is not set +CONFIG_HAVE_DEBUG_KMEMLEAK=y +# CONFIG_DEBUG_KMEMLEAK is not set +# CONFIG_DEBUG_STACK_USAGE is not set +# CONFIG_SCHED_STACK_END_CHECK is not set +CONFIG_ARCH_HAS_DEBUG_VM_PGTABLE=y +# CONFIG_DEBUG_VM is not set +# CONFIG_DEBUG_VM_PGTABLE is not set +CONFIG_ARCH_HAS_DEBUG_VIRTUAL=y +# CONFIG_DEBUG_VIRTUAL is not set +CONFIG_DEBUG_MEMORY_INIT=y +# CONFIG_DEBUG_PER_CPU_MAPS is not set +CONFIG_ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP=y +# CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP is not set +CONFIG_HAVE_ARCH_KASAN=y +CONFIG_HAVE_ARCH_KASAN_VMALLOC=y +CONFIG_CC_HAS_KASAN_GENERIC=y +CONFIG_CC_HAS_WORKING_NOSANITIZE_ADDRESS=y +# CONFIG_KASAN is not set +CONFIG_HAVE_ARCH_KFENCE=y +# CONFIG_KFENCE is not set +# end of Memory Debugging + +# CONFIG_DEBUG_SHIRQ is not set + +# +# Debug Oops, Lockups and Hangs +# +# CONFIG_PANIC_ON_OOPS is not set +CONFIG_PANIC_ON_OOPS_VALUE=0 +CONFIG_PANIC_TIMEOUT=0 +# CONFIG_SOFTLOCKUP_DETECTOR is not set +CONFIG_HARDLOCKUP_CHECK_TIMESTAMP=y +# CONFIG_HARDLOCKUP_DETECTOR is not set +# CONFIG_DETECT_HUNG_TASK is not set +# CONFIG_WQ_WATCHDOG is not set +# end of Debug Oops, Lockups and Hangs + +# +# Scheduler Debugging +# +# CONFIG_SCHED_DEBUG is not set +CONFIG_SCHED_INFO=y +# CONFIG_SCHEDSTATS is not set +# end of Scheduler Debugging + +# CONFIG_DEBUG_TIMEKEEPING is not set + +# +# Lock Debugging (spinlocks, mutexes, etc...) +# +CONFIG_LOCK_DEBUGGING_SUPPORT=y +# CONFIG_PROVE_LOCKING is not set +# CONFIG_LOCK_STAT is not set +# CONFIG_DEBUG_RT_MUTEXES is not set +# CONFIG_DEBUG_SPINLOCK is not set +# CONFIG_DEBUG_MUTEXES is not set +# CONFIG_DEBUG_WW_MUTEX_SLOWPATH is not set +# CONFIG_DEBUG_RWSEMS is not set +# CONFIG_DEBUG_LOCK_ALLOC is not set +# CONFIG_DEBUG_ATOMIC_SLEEP is not set +# CONFIG_DEBUG_LOCKING_API_SELFTESTS is not set +# CONFIG_LOCK_TORTURE_TEST is not set +# CONFIG_WW_MUTEX_SELFTEST is not set +# CONFIG_SCF_TORTURE_TEST is not set +# CONFIG_CSD_LOCK_WAIT_DEBUG is not set +# end of Lock Debugging (spinlocks, mutexes, etc...) + +# CONFIG_DEBUG_IRQFLAGS is not set +CONFIG_STACKTRACE=y +# CONFIG_WARN_ALL_UNSEEDED_RANDOM is not set +# CONFIG_DEBUG_KOBJECT is not set + +# +# Debug kernel data structures +# +CONFIG_DEBUG_LIST=y +# CONFIG_DEBUG_PLIST is not set +# CONFIG_DEBUG_SG is not set +# CONFIG_DEBUG_NOTIFIERS is not set +CONFIG_BUG_ON_DATA_CORRUPTION=y +# end of Debug kernel data structures + +# CONFIG_DEBUG_CREDENTIALS is not set + +# +# RCU Debugging +# +# CONFIG_RCU_SCALE_TEST is not set +# CONFIG_RCU_TORTURE_TEST is not set +# CONFIG_RCU_REF_SCALE_TEST is not set +CONFIG_RCU_CPU_STALL_TIMEOUT=59 +# CONFIG_RCU_TRACE is not set +# CONFIG_RCU_EQS_DEBUG is not set +# end of RCU Debugging + +# CONFIG_DEBUG_WQ_FORCE_RR_CPU is not set +# CONFIG_CPU_HOTPLUG_STATE_CONTROL is not set +# CONFIG_LATENCYTOP is not set +CONFIG_USER_STACKTRACE_SUPPORT=y +CONFIG_HAVE_FUNCTION_TRACER=y +CONFIG_HAVE_FUNCTION_GRAPH_TRACER=y +CONFIG_HAVE_DYNAMIC_FTRACE=y +CONFIG_HAVE_DYNAMIC_FTRACE_WITH_REGS=y +CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS=y +CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS=y +CONFIG_HAVE_FTRACE_MCOUNT_RECORD=y +CONFIG_HAVE_SYSCALL_TRACEPOINTS=y +CONFIG_HAVE_FENTRY=y +CONFIG_HAVE_OBJTOOL_MCOUNT=y +CONFIG_HAVE_C_RECORDMCOUNT=y +CONFIG_TRACING_SUPPORT=y +# CONFIG_FTRACE is not set +# CONFIG_PROVIDE_OHCI1394_DMA_INIT is not set +# CONFIG_SAMPLES is not set +CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y +CONFIG_STRICT_DEVMEM=y +# CONFIG_IO_STRICT_DEVMEM is not set + +# +# x86 Debugging +# +CONFIG_TRACE_IRQFLAGS_NMI_SUPPORT=y +CONFIG_X86_VERBOSE_BOOTUP=y +CONFIG_EARLY_PRINTK=y +# CONFIG_EARLY_PRINTK_DBGP is not set +# CONFIG_EARLY_PRINTK_USB_XDBC is not set +# CONFIG_EFI_PGT_DUMP is not set +# CONFIG_DEBUG_TLBFLUSH is not set +CONFIG_HAVE_MMIOTRACE_SUPPORT=y +# CONFIG_X86_DECODER_SELFTEST is not set +CONFIG_IO_DELAY_0X80=y +# CONFIG_IO_DELAY_0XED is not set +# CONFIG_IO_DELAY_UDELAY is not set +# CONFIG_IO_DELAY_NONE is not set +# CONFIG_DEBUG_BOOT_PARAMS is not set +# CONFIG_CPA_DEBUG is not set +# CONFIG_DEBUG_ENTRY is not set +# CONFIG_DEBUG_NMI_SELFTEST is not set +# CONFIG_X86_DEBUG_FPU is not set +# CONFIG_PUNIT_ATOM_DEBUG is not set +# CONFIG_UNWINDER_ORC is not set +CONFIG_UNWINDER_FRAME_POINTER=y +# CONFIG_UNWINDER_GUESS is not set +# end of x86 Debugging + +# +# Kernel Testing and Coverage +# +# CONFIG_KUNIT is not set +# CONFIG_NOTIFIER_ERROR_INJECTION is not set +# CONFIG_FAULT_INJECTION is not set +CONFIG_ARCH_HAS_KCOV=y +CONFIG_CC_HAS_SANCOV_TRACE_PC=y +# CONFIG_KCOV is not set +CONFIG_RUNTIME_TESTING_MENU=y +# CONFIG_LKDTM is not set +# CONFIG_TEST_MIN_HEAP is not set +# CONFIG_TEST_DIV64 is not set +# CONFIG_BACKTRACE_SELF_TEST is not set +# CONFIG_RBTREE_TEST is not set +# CONFIG_REED_SOLOMON_TEST is not set +# CONFIG_INTERVAL_TREE_TEST is not set +# CONFIG_ATOMIC64_SELFTEST is not set +# CONFIG_TEST_HEXDUMP is not set +# CONFIG_STRING_SELFTEST is not set +# CONFIG_TEST_STRING_HELPERS is not set +# CONFIG_TEST_STRSCPY is not set +# CONFIG_TEST_KSTRTOX is not set +# CONFIG_TEST_PRINTF is not set +# CONFIG_TEST_SCANF is not set +# CONFIG_TEST_BITMAP is not set +# CONFIG_TEST_UUID is not set +# CONFIG_TEST_XARRAY is not set +# CONFIG_TEST_OVERFLOW is not set +# CONFIG_TEST_RHASHTABLE is not set +# CONFIG_TEST_HASH is not set +# CONFIG_TEST_IDA is not set +# CONFIG_FIND_BIT_BENCHMARK is not set +# CONFIG_TEST_FIRMWARE is not set +# CONFIG_TEST_SYSCTL is not set +# CONFIG_TEST_UDELAY is not set +# CONFIG_TEST_MEMCAT_P is not set +# CONFIG_TEST_STACKINIT is not set +# CONFIG_TEST_MEMINIT is not set +# CONFIG_TEST_FREE_PAGES is not set +# CONFIG_TEST_FPU is not set +# CONFIG_TEST_CLOCKSOURCE_WATCHDOG is not set +CONFIG_ARCH_USE_MEMTEST=y +# CONFIG_MEMTEST is not set +# CONFIG_HYPERV_TESTING is not set +# end of Kernel Testing and Coverage +# end of Kernel hacking diff --git a/vendor/cube-hypervisor/scripts/ch-trace-visualiser.py b/vendor/cube-hypervisor/scripts/ch-trace-visualiser.py new file mode 100755 index 0000000..a2af15d --- /dev/null +++ b/vendor/cube-hypervisor/scripts/ch-trace-visualiser.py @@ -0,0 +1,98 @@ +#!/bin/env python3 +# +# Copyright © 2022 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 +# + +from colorsys import hsv_to_rgb +from random import random +import xml +import json +from sys import argv, stderr +import xml.etree.ElementTree as ET + +width = 1000 +height = 200 +padding = 10 + +if len(argv) < 3: + stderr.write("./ch-trace-visualiser \n") + exit(1) + + +def nano_time(duration): + return (duration["secs"] * 10 ** 9) + duration["nanos"] + + +def duration_to_px_x(start): + return (nano_time(start) * (width - 2 * padding)) / total_time + + +def duration_to_px_width(start, end): + return ((nano_time(end) - nano_time(start)) * (width - 2 * padding)) / total_time + + +def duration_ms(start, end): + return (nano_time(end) - nano_time(start)) // 1000000 + + +f = open(argv[1]) +report = json.load(f) +total_time = nano_time(report["duration"]) + +svg = ET.Element("svg", attrib={"width": str(width), "height": str(height), + "xmlns": "http://www.w3.org/2000/svg", + "xmlns:svg": "http://www.w3.org/2000/svg" + }) + + +def add_traced_block(thread_group, depth, traced_block): + g = ET.SubElement(thread_group, "g", + attrib={"transform": "translate(%d,%d)" % ( + duration_to_px_x(traced_block["timestamp"]), + (depth * 18))}) + width = str(duration_to_px_width( + traced_block["timestamp"], traced_block["end_timestamp"])) + + clip = ET.SubElement(g, "clipPath", attrib={ + "id": "clip_%s" % (traced_block["event"]), + }) + ET.SubElement(clip, "rect", attrib={ + "width": width, + "height": "1.5em", + "x": "0", + "y": "0" + }) + + (red, green, blue) = hsv_to_rgb(random(), 0.3, 0.75) + ET.SubElement(g, "rect", attrib={ + "width": width, + "height": "1.5em", + "fill": "#%x%x%x" % (int(red * 255), int(green * 255), int(blue * 255)) + }) + text = ET.SubElement(g, "text", attrib={ + "x": "0.2em", "y": "1em", "clip-path": "url(#clip_%s)" % (traced_block["event"])}) + text.text = "%s (%dms)" % (traced_block["event"], duration_ms( + traced_block["timestamp"], traced_block["end_timestamp"])) + + +thread_size = (height - (2 * padding)) / len(report["events"]) +thread_offset = padding + +for thread in report["events"]: + thread_events = report["events"][thread] + thread_events = sorted( + thread_events, key=lambda traced_block: nano_time(traced_block["timestamp"])) + thread_group = ET.SubElement( + svg, "g", attrib={"transform": "translate(%d,%d)" % (padding, thread_offset)}) + thread_text = ET.SubElement(thread_group, "text", attrib={ + "y": "1em"}).text = "Thread: %s" % (thread) + thread_children = ET.SubElement(thread_group, "g", attrib={ + "transform": "translate(0, 18)"}) + for traced_block in thread_events: + add_traced_block(thread_children, traced_block["depth"], traced_block) + thread_offset += thread_size + padding + +xml = ET.ElementTree(element=svg) +xml.write(argv[2], xml_declaration=True) diff --git a/vendor/cube-hypervisor/scripts/check-image-compatability.sh b/vendor/cube-hypervisor/scripts/check-image-compatability.sh new file mode 100755 index 0000000..22063c8 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/check-image-compatability.sh @@ -0,0 +1,215 @@ +#!/bin/bash +: ' + This script checks if an image is compatible with Cloud Hypervisor. + At first, it detects the image type(raw or qcow2), + partition type whether it is DOS or GPT. + Then it mounts the image and checks if VIRTIO Configs + are enabled in the kernel config. In the end, it provides + a message about the compatibility of the image. +' + + + +usage="$(basename "$0") [-h] -f -w -- program to check Cloud Hypervisor compatible image + +where: + -h show this help text + -f image file location + -w directory to be used for temporary files" + +function check_command { + if ! command -v $1 &> /dev/null + then + echo "Command $1 could not be found" + exit 1 + fi +}; + +function check_if_root { + if [ "$EUID" -ne 0 ]; then + echo "Please run as root" + exit 1 + fi + +}; + +check_if_root +working_dir="" +while getopts ':hf:w:' option; do + case "$option" in + h) echo "$usage" + exit + ;; + f) file_name=$OPTARG + ;; + w) working_dir=$OPTARG + ;; + :) printf "missing argument for -%s\n" "$OPTARG" >&2 + echo "$usage" >&2 + exit 1 + ;; + \?) printf "illegal option: -%s\n" "$OPTARG" >&2 + echo "$usage" >&2 + exit 1 + ;; + esac +done + +shift $((OPTIND - 1)) + +if [ -z "${file_name}" ]; then + echo "You must provide the image file name" + exit 1 +fi +if [[ ! -f ${file_name} ]]; then + echo "File ${file_name} does not exist" + exit 1 +fi + +file_abs_path=`readlink -m ${file_name}` +if [[ "${working_dir}" != "" && ! -d "${working_dir}" ]]; then + echo "Directory ${working_dir} does not exist" + exit 1 +elif [[ "${working_dir}" == "" ]]; then + working_dir=`mktemp -d` + tmp_created=1 +else + working_dir=`readlink -m ${working_dir}` +fi + +#get file extension and image type +extension="${file_name##*.}" +filename="${file_name%.*}" +dest_file=${working_dir}/${filename}.raw +image_type=$(qemu-img info ${file_abs_path} | grep 'file format:' | awk '{ print $3 }') +echo "Image type detected as ${image_type}" + + +if [[ "${image_type}" == "raw" ]]; then + dest_file=${file_abs_path} +elif [[ "$image_type" == "qcow2" ]]; then + if lsmod | grep "nbd" &> /dev/null ; then + echo "Module nbd is loaded!" + else + echo "Module nbd is not loaded. Trying to load the module" + modprobe nbd max_part=8 + if [ $? != 0 ]; then + echo "failed to load nbd module. Exiting" + exit 1 + fi + fi + check_command qemu-img + dest_file=/dev/nbd0 + qemu-nbd --connect=${dest_file} ${file_abs_path} --read-only +fi + +check_command blkid +#get part info +part_type=$(blkid -o value -s PTTYPE ${dest_file}) + +check_command partx +nr_partitions=`partx -g ${dest_file} | wc -l` + +check_command fdisk +out=`fdisk -l ${dest_file} --bytes | grep -i -A ${nr_partitions} 'Device' | tail -n +2` + +IFS=' +' +i=0 +declare -A liness +for x in $out ; do + lines[$i]=$x + i=$((i+1)) +done + +declare -A partitions +IFS=' ' +i=0 +ROWS=${#lines[@]} + +for line in "${lines[@]}"; +do + j=0 + read -a str_arr <<< "$line" + for val in "${str_arr[@]}"; + do + if [[ "$val" != "*" ]]; then + partitions[$i,$j]=$val + j=$((j+1)) + fi + done + i=$((i+1)) +done + +COLUMNS=$j +COUNT=${#partitions[@]} +START_ADDRESS_INDEX=1 +FILE_SYS_INDEX2=$((COLUMNS-1)) +FILE_SYS_INDEX1=$((COLUMNS-2)) +DEVICE_INDEX=0 +# Here we have all the partition info now lets mount and analyze the contents +for ((i=0;i /dev/null +fi + +result="" +if [[ "${part_type}" == "dos" ]]; then + result="dos mode not supported" +fi +if [[ "${HAS_VIRTIO}" == "0" ]]; then + if [[ "$result" != "" ]]; then + result="${result}," + fi + result="$result VirtIO module not found in the image" +fi +if [[ "$result" == "" ]];then + echo "No incompatibilities found" +else + echo "$result" +fi diff --git a/vendor/cube-hypervisor/scripts/common-aarch64.sh b/vendor/cube-hypervisor/scripts/common-aarch64.sh new file mode 100644 index 0000000..e7a4ab1 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/common-aarch64.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +export BUILD_TARGET=${BUILD_TARGET-aarch64-unknown-linux-gnu} + +WORKLOADS_DIR="$HOME/workloads" + +mkdir -p "$WORKLOADS_DIR" + +build_edk2() { + EDK2_BUILD_DIR="$WORKLOADS_DIR/edk2_build" + EDK2_REPO="https://github.com/tianocore/edk2.git" + EDK2_DIR="$EDK2_BUILD_DIR/edk2" + EDK2_PLAT_REPO="https://github.com/tianocore/edk2-platforms.git" + EDK2_PLAT_DIR="$EDK2_BUILD_DIR/edk2-platforms" + ACPICA_REPO="https://github.com/acpica/acpica.git" + ACPICA_DIR="$EDK2_BUILD_DIR/acpica" + export WORKSPACE="$EDK2_BUILD_DIR" + export PACKAGES_PATH="$EDK2_DIR:$EDK2_PLAT_DIR" + export IASL_PREFIX="$ACPICA_DIR/generate/unix/bin/" + + if [ ! -d "$EDK2_BUILD_DIR" ]; then + mkdir -p "$EDK2_BUILD_DIR" + fi + + # Prepare source code + checkout_repo "$EDK2_DIR" "$EDK2_REPO" master "46b4606ba23498d3d0e66b53e498eb3d5d592586" + pushd "$EDK2_DIR" + git submodule update --init + popd + checkout_repo "$EDK2_PLAT_DIR" "$EDK2_PLAT_REPO" master "8227e9e9f6a8aefbd772b40138f835121ccb2307" + checkout_repo "$ACPICA_DIR" "$ACPICA_REPO" master "b9c69f81a05c45611c91ea9cbce8756078d76233" + + if [[ ! -f "$EDK2_DIR/.built" || \ + ! -f "$EDK2_PLAT_DIR/.built" || \ + ! -f "$ACPICA_DIR/.built" ]]; then + pushd "$EDK2_BUILD_DIR" + # Build + make -C acpica -j `nproc` + source edk2/edksetup.sh + make -C edk2/BaseTools -j `nproc` + build -a AARCH64 -t GCC5 -p ArmVirtPkg/ArmVirtCloudHv.dsc -b RELEASE -n 0 + cp Build/ArmVirtCloudHv-AARCH64/RELEASE_GCC5/FV/CLOUDHV_EFI.fd "$WORKLOADS_DIR" + touch "$EDK2_DIR"/.built + touch "$EDK2_PLAT_DIR"/.built + touch "$ACPICA_DIR"/.built + popd + fi +} + diff --git a/vendor/cube-hypervisor/scripts/create-cloud-init.sh b/vendor/cube-hypervisor/scripts/create-cloud-init.sh new file mode 100755 index 0000000..ac48462 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/create-cloud-init.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -x + +rm -f /tmp/ubuntu-cloudinit.img +mkdosfs -n cidata -C /tmp/ubuntu-cloudinit.img 8192 +mcopy -oi /tmp/ubuntu-cloudinit.img -s test_data/cloud-init/ubuntu/user-data :: +mcopy -oi /tmp/ubuntu-cloudinit.img -s test_data/cloud-init/ubuntu/meta-data :: +mcopy -oi /tmp/ubuntu-cloudinit.img -s test_data/cloud-init/ubuntu/network-config :: + diff --git a/vendor/cube-hypervisor/scripts/dev_cli.sh b/vendor/cube-hypervisor/scripts/dev_cli.sh new file mode 100755 index 0000000..cc82651 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/dev_cli.sh @@ -0,0 +1,665 @@ +#!/bin/bash + +# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +# Copyright © 2020 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +CLI_NAME="Cloud Hypervisor" + +#CTR_IMAGE_TAG="cloudhypervisor/dev" +#CTR_IMAGE_VERSION="20220705-0" +CTR_IMAGE_TAG="ghcr.io/cloud-hypervisor/cloud-hypervisor" +CTR_IMAGE_VERSION="20240507-0" +CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}" + +DOCKER_RUNTIME="docker" + +# Host paths +CLH_SCRIPTS_DIR=$(cd "$(dirname "$0")" && pwd) +CLH_ROOT_DIR=$(cd "${CLH_SCRIPTS_DIR}/.." && pwd) +CLH_BUILD_DIR="${CLH_ROOT_DIR}/build" +CLH_CARGO_TARGET="${CLH_BUILD_DIR}/cargo_target" +CLH_DOCKERFILE="${CLH_SCRIPTS_DIR}/../resources/Dockerfile" +CLH_CTR_BUILD_DIR="/tmp/cloud-hypervisor/ctr-build" +CLH_INTEGRATION_WORKLOADS="${HOME}/workloads" + +# Container paths +CTR_CLH_ROOT_DIR="/cloud-hypervisor" +CTR_CLH_CARGO_BUILT_DIR="${CTR_CLH_ROOT_DIR}/build" +CTR_CLH_CARGO_TARGET="${CTR_CLH_CARGO_BUILT_DIR}/cargo_target" +CTR_CLH_INTEGRATION_WORKLOADS="/root/workloads" + +# Container networking option +CTR_CLH_NET="bridge" + +# Cargo paths +# Full path to the cargo registry dir on the host. This appears on the host +# because we want to persist the cargo registry across container invocations. +# Otherwise, any rust crates from crates.io would be downloaded again each time +# we build or test. +CARGO_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_registry" + +# Full path to the cargo git registry on the host. This serves the same purpose +# as CARGO_REGISTRY_DIR, for crates downloaded from GitHub repos instead of +# crates.io. +CARGO_GIT_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_git_registry" + +# Full path to the cargo target dir on the host. +CARGO_TARGET_DIR="${CLH_BUILD_DIR}/cargo_target" + +# Send a decorated message to stdout, followed by a new line +# +say() { + [ -t 1 ] && [ -n "$TERM" ] && + echo "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" || + echo "[$CLI_NAME] $*" +} + +# Send a decorated message to stdout, without a trailing new line +# +say_noln() { + [ -t 1 ] && [ -n "$TERM" ] && + echo -n "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" || + echo "[$CLI_NAME] $*" +} + +# Send a text message to stderr +# +say_err() { + [ -t 2 ] && [ -n "$TERM" ] && + echo "$(tput setaf 1)[$CLI_NAME] $*$(tput sgr0)" 1>&2 || + echo "[$CLI_NAME] $*" 1>&2 +} + +# Send a warning-highlighted text to stdout +say_warn() { + [ -t 1 ] && [ -n "$TERM" ] && + echo "$(tput setaf 3)[$CLI_NAME] $*$(tput sgr0)" || + echo "[$CLI_NAME] $*" +} + +# Exit with an error message and (optional) code +# Usage: die [-c ] +# +die() { + code=1 + [[ "$1" = "-c" ]] && { + code="$2" + shift 2 + } + say_err "$@" + exit "$code" +} + +# Exit with an error message if the last exit code is not 0 +# +ok_or_die() { + code=$? + [[ $code -eq 0 ]] || die -c $code "$@" +} + +# Make sure the build/ dirs are available. Exit if we can't create them. +# Upon returning from this call, the caller can be certain the build/ dirs exist. +# +ensure_build_dir() { + for dir in "$CLH_BUILD_DIR" \ + "$CLH_INTEGRATION_WORKLOADS" \ + "$CLH_CTR_BUILD_DIR" \ + "$CARGO_TARGET_DIR" \ + "$CARGO_REGISTRY_DIR" \ + "$CARGO_GIT_REGISTRY_DIR"; do + mkdir -p "$dir" || die "Error: cannot create dir $dir" + [ -x "$dir" ] && [ -w "$dir" ] || + { + say "Wrong permissions for $dir. Attempting to fix them ..." + chmod +x+w "$dir" + } || + die "Error: wrong permissions for $dir. Should be +x+w" + done +} + +# Make sure we're using the latest dev container, by just pulling it. +ensure_latest_ctr() { + if [ "$CTR_IMAGE_VERSION" = "local" ]; then + build_container + else + $DOCKER_RUNTIME pull "$CTR_IMAGE" + + if [ $? -ne 0 ]; then + build_container + fi + + ok_or_die "Error pulling/building container image. Aborting." + fi +} + +# Fix main directory permissions after a container ran as root. +# Since the container ran as root, any files it creates will be owned by root. +# This fixes that by recursively changing the ownership of /cloud-hypervisor to the +# current user. +# +fix_dir_perms() { + # Yes, running Docker to get elevated privileges, just to chown some files + # is a dirty hack. + $DOCKER_RUNTIME run \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + "$CTR_IMAGE" \ + chown -R "$(id -u):$(id -g)" "$CTR_CLH_ROOT_DIR" + + return "$1" +} +# Process exported volumes argument, separate the volumes and make docker compatible +# Sample input: --volumes /a:/a#/b:/b +# Sample output: --volume /a:/a --volume /b:/b +# +process_volumes_args() { + if [ -z "$arg_vols" ]; then + return + fi + exported_volumes="" + arr_vols=(${arg_vols//#/ }) + for var in "${arr_vols[@]}"; do + parts=(${var//:/ }) + if [[ ! -e "${parts[0]}" ]]; then + echo "The volume ${parts[0]} does not exist." + exit 1 + fi + exported_volumes="$exported_volumes --volume $var" + done +} +cmd_help() { + echo "" + echo "Cloud Hypervisor $(basename "$0")" + echo "Usage: $(basename "$0") []" + echo "" + echo "Available commands:" + echo "" + echo " build [--debug|--release] [--libc musl|gnu] [-- []]" + echo " Build the Cloud Hypervisor binaries." + echo " --debug Build the debug binaries. This is the default." + echo " --release Build the release binaries." + echo " --libc Select the C library Cloud Hypervisor will be built against. Default is gnu" + echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" + echo " --hypervisor Underlying hypervisor. Options kvm, mshv" + echo "" + echo " tests [] [--libc musl|gnu] [-- [] [-- []]] " + echo " Run the Cloud Hypervisor tests." + echo " --unit Run the unit tests." + echo " --integration Run the integration tests." + echo " --integration-sgx Run the SGX integration tests." + echo " --integration-vfio Run the VFIO integration tests." + echo " --integration-windows Run the Windows guest integration tests." + echo " --integration-live-migration Run the live-migration integration tests." + echo " --integration-rate-limiter Run the rate-limiter integration tests." + echo " --libc Select the C library Cloud Hypervisor will be built against. Default is gnu" + echo " --metrics Generate performance metrics" + echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" + echo " --hypervisor Underlying hypervisor. Options kvm, mshv" + echo " --quick Run only core smoke tests (5 priority levels)" + echo " --all Run all tests." + echo "" + echo " build-container [--type]" + echo " Build the Cloud Hypervisor container." + echo "" + echo " clean []]" + echo " Remove the Cloud Hypervisor artifacts." + echo "" + echo " shell" + echo " Run the development container into an interactive, privileged BASH shell." + echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" + echo "" + echo " help" + echo " Display this help message." + echo "" +} + +cmd_build() { + build="debug" + libc="gnu" + hypervisor="kvm" + features_build="" + exported_device="/dev/kvm" + while [ $# -gt 0 ]; do + case "$1" in + "-h" | "--help") { + cmd_help + exit 1 + } ;; + "--debug") { build="debug"; } ;; + "--release") { build="release"; } ;; + "--runtime") + shift + DOCKER_RUNTIME="$1" + export DOCKER_RUNTIME + ;; + "--libc") + shift + [[ "$1" =~ ^(musl|gnu)$ ]] || + die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"." + libc="$1" + ;; + "--volumes") + shift + arg_vols="$1" + ;; + "--hypervisor") + shift + hypervisor="$1" + ;; + "--features") + shift + features_build="--features $1" + ;; + "--") { + shift + break + } ;; + *) + die "Unknown build argument: $1. Please use --help for help." + ;; + esac + shift + done + + ensure_build_dir + ensure_latest_ctr + + process_volumes_args + if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then + die "Hypervisor value must be kvm or mshv" + fi + if [[ "$hypervisor" = "mshv" ]]; then + exported_device="/dev/mshv" + fi + target="$(uname -m)-unknown-linux-${libc}" + + cargo_args=("$@") + [ $build = "release" ] && cargo_args+=("--release") + cargo_args+=(--target "$target") + + rustflags="" + target_cc="" + if [ "$(uname -m)" = "aarch64" ] && [ "$libc" = "musl" ]; then + rustflags="-C link-arg=-lgcc -C link_arg=-specs -C link_arg=/usr/lib/aarch64-linux-musl/musl-gcc.specs" + target_cc="musl-gcc" + fi + + $DOCKER_RUNTIME run \ + --user "$(id -u):$(id -g)" \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --volume $exported_device \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + --env RUSTFLAGS="$rustflags" \ + --env TARGET_CC="$target_cc" \ + "$CTR_IMAGE" \ + cargo build --all $features_build \ + --target-dir "$CTR_CLH_CARGO_TARGET" \ + "${cargo_args[@]}" && say "Binaries placed under $CLH_CARGO_TARGET/$target/$build" +} + +cmd_clean() { + cargo_args=("$@") + + ensure_build_dir + ensure_latest_ctr + + $DOCKER_RUNTIME run \ + --user "$(id -u):$(id -g)" \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + "$CTR_IMAGE" \ + cargo clean \ + --target-dir "$CTR_CLH_CARGO_TARGET" \ + "${cargo_args[@]}" +} + +cmd_tests() { + unit=false + integration=false + integration_sgx=false + integration_vfio=false + integration_windows=false + integration_live_migration=false + integration_rate_limiter=false + metrics=false + quick=false + libc="gnu" + arg_vols="" + hypervisor="kvm" + exported_device="/dev/kvm" + while [ $# -gt 0 ]; do + case "$1" in + "-h" | "--help") { + cmd_help + exit 1 + } ;; + "--unit") { unit=true; } ;; + "--integration") { integration=true; } ;; + "--integration-sgx") { integration_sgx=true; } ;; + "--integration-vfio") { integration_vfio=true; } ;; + "--integration-windows") { integration_windows=true; } ;; + "--integration-live-migration") { integration_live_migration=true; } ;; + "--integration-rate-limiter") { integration_rate_limiter=true; } ;; + "--metrics") { metrics=true; } ;; + "--libc") + shift + [[ "$1" =~ ^(musl|gnu)$ ]] || + die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"." + libc="$1" + ;; + "--volumes") + shift + arg_vols="$1" + ;; + "--hypervisor") + shift + hypervisor="$1" + ;; + "--quick") { quick=true; } ;; + "--all") { + cargo=true + unit=true + integration=true + } ;; + "--") { + shift + break + } ;; + *) + die "Unknown tests argument: $1. Please use --help for help." + ;; + esac + shift + done + if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then + die "Hypervisor value must be kvm or mshv" + fi + + if [[ "$hypervisor" = "mshv" ]]; then + exported_device="/dev/mshv" + fi + + if [ "$quick" = true ]; then + set -- '--hypervisor' "$hypervisor" '--quick' "$@" + else + set -- '--hypervisor' "$hypervisor" "$@" + fi + + ensure_build_dir + ensure_latest_ctr + + process_volumes_args + target="$(uname -m)-unknown-linux-${libc}" + + if [[ "$unit" = true ]]; then + say "Running unit tests for $target..." + $DOCKER_RUNTIME run \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --device $exported_device \ + --device /dev/net/tun \ + --cap-add net_admin \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + --env BUILD_TARGET="$target" \ + "$CTR_IMAGE" \ + ./scripts/run_unit_tests.sh "$@" || fix_dir_perms $? || exit $? + fi + + if [ "$integration" = true ]; then + say "Running integration tests for $target..." + $DOCKER_RUNTIME run \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --privileged \ + --security-opt seccomp=unconfined \ + --ipc=host \ + --net="$CTR_CLH_NET" \ + --mount type=tmpfs,destination=/tmp \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ + --env USER="root" \ + --env CH_LIBC="${libc}" \ + "$CTR_IMAGE" \ + ./scripts/run_integration_tests_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $? + fi + + if [ "$integration_sgx" = true ]; then + say "Running SGX integration tests for $target..." + $DOCKER_RUNTIME run \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --privileged \ + --security-opt seccomp=unconfined \ + --ipc=host \ + --net="$CTR_CLH_NET" \ + --mount type=tmpfs,destination=/tmp \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ + --env USER="root" \ + --env CH_LIBC="${libc}" \ + "$CTR_IMAGE" \ + ./scripts/run_integration_tests_sgx.sh "$@" || fix_dir_perms $? || exit $? + fi + + if [ "$integration_vfio" = true ]; then + say "Running VFIO integration tests for $target..." + $DOCKER_RUNTIME run \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --privileged \ + --security-opt seccomp=unconfined \ + --ipc=host \ + --net="$CTR_CLH_NET" \ + --mount type=tmpfs,destination=/tmp \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ + --env USER="root" \ + --env CH_LIBC="${libc}" \ + "$CTR_IMAGE" \ + ./scripts/run_integration_tests_vfio.sh "$@" || fix_dir_perms $? || exit $? + fi + + if [ "$integration_windows" = true ]; then + say "Running Windows integration tests for $target..." + $DOCKER_RUNTIME run \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --privileged \ + --security-opt seccomp=unconfined \ + --ipc=host \ + --net="$CTR_CLH_NET" \ + --mount type=tmpfs,destination=/tmp \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ + --env USER="root" \ + --env CH_LIBC="${libc}" \ + "$CTR_IMAGE" \ + ./scripts/run_integration_tests_windows_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $? + fi + + if [ "$integration_live_migration" = true ]; then + say "Running 'live migration' integration tests for $target..." + $DOCKER_RUNTIME run \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --privileged \ + --security-opt seccomp=unconfined \ + --ipc=host \ + --net="$CTR_CLH_NET" \ + --mount type=tmpfs,destination=/tmp \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ + --env USER="root" \ + --env CH_LIBC="${libc}" \ + "$CTR_IMAGE" \ + ./scripts/run_integration_tests_live_migration.sh "$@" || fix_dir_perms $? || exit $? + fi + + if [ "$integration_rate_limiter" = true ]; then + say "Running 'rate limiter' integration tests for $target..." + $DOCKER_RUNTIME run \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --privileged \ + --security-opt seccomp=unconfined \ + --ipc=host \ + --net="$CTR_CLH_NET" \ + --mount type=tmpfs,destination=/tmp \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ + --env USER="root" \ + --env CH_LIBC="${libc}" \ + "$CTR_IMAGE" \ + ./scripts/run_integration_tests_rate_limiter.sh "$@" || fix_dir_perms $? || exit $? + fi + + if [ "$metrics" = true ]; then + say "Generating performance metrics for $target..." + $DOCKER_RUNTIME run \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --privileged \ + --security-opt seccomp=unconfined \ + --ipc=host \ + --net="$CTR_CLH_NET" \ + --mount type=tmpfs,destination=/tmp \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ + --env USER="root" \ + --env CH_LIBC="${libc}" \ + "$CTR_IMAGE" \ + ./scripts/run_metrics.sh "$@" || fix_dir_perms $? || exit $? + fi + + fix_dir_perms $? +} + +build_container() { + ensure_build_dir + + BUILD_DIR=/tmp/cloud-hypervisor/container/ + + mkdir -p $BUILD_DIR + cp "$CLH_DOCKERFILE" $BUILD_DIR + + [ "$(uname -m)" = "aarch64" ] && TARGETARCH="arm64" + [ "$(uname -m)" = "x86_64" ] && TARGETARCH="amd64" + + $DOCKER_RUNTIME build \ + --target dev \ + -t $CTR_IMAGE \ + -f $BUILD_DIR/Dockerfile \ + --build-arg TARGETARCH=$TARGETARCH \ + $BUILD_DIR +} + +cmd_build-container() { + while [ $# -gt 0 ]; do + case "$1" in + "-h" | "--help") { + cmd_help + exit 1 + } ;; + "--") { + shift + break + } ;; + *) + die "Unknown build-container argument: $1. Please use --help for help." + ;; + esac + shift + done + + build_container +} + +cmd_shell() { + while [ $# -gt 0 ]; do + case "$1" in + "-h" | "--help") { + cmd_help + exit 1 + } ;; + "--volumes") + shift + arg_vols="$1" + ;; + "--") { + shift + break + } ;; + *) ;; + + esac + shift + done + ensure_build_dir + ensure_latest_ctr + process_volumes_args + say_warn "Starting a privileged shell prompt as root ..." + say_warn "WARNING: Your $CLH_ROOT_DIR folder will be bind-mounted in the container under $CTR_CLH_ROOT_DIR" + $DOCKER_RUNTIME run \ + -ti \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --privileged \ + --security-opt seccomp=unconfined \ + --ipc=host \ + --net="$CTR_CLH_NET" \ + --tmpfs /tmp:exec \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ + --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ + --env USER="root" \ + --entrypoint bash \ + "$CTR_IMAGE" + + fix_dir_perms $? +} + +# Parse main command line args. +# +while [ $# -gt 0 ]; do + case "$1" in + -h | --help) { + cmd_help + exit 1 + } ;; + --local) { + CTR_IMAGE_VERSION="local" + CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}" + } ;; + -*) + die "Unknown arg: $1. Please use \`$0 help\` for help." + ;; + *) + break + ;; + esac + shift +done + +# $1 is now a command name. Check if it is a valid command and, if so, +# run it. +# +declare -f "cmd_$1" >/dev/null +ok_or_die "Unknown command: $1. Please use \`$0 help\` for help." + +cmd=cmd_$1 +shift + +$cmd "$@" diff --git a/vendor/cube-hypervisor/scripts/prepare_vdpa.sh b/vendor/cube-hypervisor/scripts/prepare_vdpa.sh new file mode 100755 index 0000000..73441eb --- /dev/null +++ b/vendor/cube-hypervisor/scripts/prepare_vdpa.sh @@ -0,0 +1,33 @@ +#!/bin/bash +set -x + +sudo apt install -y libncurses-dev gawk flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf git make dpkg-dev libmnl-dev pkg-config iproute2 +sudo sed -i -- 's/# deb-src/deb-src/g' /etc/apt/sources.list +sudo apt update +apt-get source linux-image-unsigned-`uname -r` +pushd linux-azure*/drivers/vdpa/vdpa_sim/ +cat <<'EOF' > Makefile +# SPDX-License-Identifier: GPL-2.0 +obj-m += vdpa_sim.o +obj-m += vdpa_sim_net.o +obj-m += vdpa_sim_blk.o +EOF +make -C /lib/modules/`uname -r`/build M=$PWD +sudo make -C /lib/modules/`uname -r`/build M=$PWD modules_install +popd +sudo depmod -a +sudo modprobe vdpa +sudo modprobe vhost_vdpa +sudo modprobe vdpa_sim +sudo modprobe vdpa_sim_blk +sudo modprobe vdpa_sim_net +# Create /dev/vhost-vdpa-0 +sudo vdpa dev add name vdpa-blk1 mgmtdev vdpasim_blk +# Create /dev/vhost-vdpa-1 +sudo vdpa dev add name vdpa-blk2 mgmtdev vdpasim_blk +# Create /dev/vhost-vdpa-2 +sudo vdpa dev add name vdpa-net1 mgmtdev vdpasim_net +sudo chmod 660 /dev/vhost-vdpa-0 +sudo chmod 660 /dev/vhost-vdpa-1 +sudo chmod 660 /dev/vhost-vdpa-2 +vdpa dev show -jp diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_aarch64.sh b/vendor/cube-hypervisor/scripts/run_integration_tests_aarch64.sh new file mode 100755 index 0000000..0003181 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_integration_tests_aarch64.sh @@ -0,0 +1,288 @@ +#!/bin/bash +set -x + +source $HOME/.cargo/env +source $(dirname "$0")/test-util.sh +source $(dirname "$0")/common-aarch64.sh + +WORKLOADS_LOCK="$WORKLOADS_DIR/integration_test.lock" + +build_spdk_nvme() { + SPDK_DIR="$WORKLOADS_DIR/spdk" + SPDK_REPO="https://github.com/spdk/spdk.git" + SPDK_DEPLOY_DIR="/usr/local/bin/spdk-nvme" + checkout_repo "$SPDK_DIR" "$SPDK_REPO" master "6301f8915de32baed10dba1eebed556a6749211a" + + if [ ! -f "$SPDK_DIR/.built" ]; then + pushd $SPDK_DIR + git submodule update --init + apt-get update + ./scripts/pkgdep.sh + ./configure --with-vfio-user + chmod +x /usr/local/lib/python3.8/dist-packages/ninja/data/bin/ninja + make -j `nproc` || exit 1 + touch .built + popd + fi + if [ ! -d "/usr/local/bin/spdk-nvme" ]; then + mkdir -p $SPDK_DEPLOY_DIR + fi + cp "$WORKLOADS_DIR/spdk/build/bin/nvmf_tgt" $SPDK_DEPLOY_DIR/nvmf_tgt + cp "$WORKLOADS_DIR/spdk/scripts/rpc.py" $SPDK_DEPLOY_DIR/rpc.py + cp -r "$WORKLOADS_DIR/spdk/scripts/rpc" $SPDK_DEPLOY_DIR/rpc +} + +build_virtiofsd() { + VIRTIOFSD_DIR="$WORKLOADS_DIR/virtiofsd_build" + VIRTIOFSD_REPO="https://gitlab.com/virtio-fs/virtiofsd.git" + + checkout_repo "$VIRTIOFSD_DIR" "$VIRTIOFSD_REPO" v1.1.0 "220405d7a2606c92636d31992b5cb3036a41047b" + + if [ ! -f "$VIRTIOFSD_DIR/.built" ]; then + pushd $VIRTIOFSD_DIR + time cargo build --release + cp target/release/virtiofsd "$WORKLOADS_DIR/" || exit 1 + touch .built + popd + fi +} + +update_workloads() { + cp scripts/sha1sums-aarch64 $WORKLOADS_DIR + + BIONIC_OS_IMAGE_DOWNLOAD_NAME="bionic-server-cloudimg-arm64.img" + BIONIC_OS_IMAGE_DOWNLOAD_URL="https://cloud-hypervisor.azureedge.net/$BIONIC_OS_IMAGE_DOWNLOAD_NAME" + BIONIC_OS_DOWNLOAD_IMAGE="$WORKLOADS_DIR/$BIONIC_OS_IMAGE_DOWNLOAD_NAME" + if [ ! -f "$BIONIC_OS_DOWNLOAD_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $BIONIC_OS_IMAGE_DOWNLOAD_URL || exit 1 + popd + fi + + BIONIC_OS_RAW_IMAGE_NAME="bionic-server-cloudimg-arm64.raw" + BIONIC_OS_RAW_IMAGE="$WORKLOADS_DIR/$BIONIC_OS_RAW_IMAGE_NAME" + if [ ! -f "$BIONIC_OS_RAW_IMAGE" ]; then + pushd $WORKLOADS_DIR + time qemu-img convert -p -f qcow2 -O raw $BIONIC_OS_IMAGE_DOWNLOAD_NAME $BIONIC_OS_RAW_IMAGE_NAME || exit 1 + popd + fi + + # Convert the raw image to qcow2 image to remove compressed blocks from the disk. Therefore letting the + # qcow2 format image can be directly used in the integration test. + BIONIC_OS_QCOW2_IMAGE_UNCOMPRESSED_NAME="bionic-server-cloudimg-arm64.qcow2" + BIONIC_OS_QCOW2_UNCOMPRESSED_IMAGE="$WORKLOADS_DIR/$BIONIC_OS_QCOW2_IMAGE_UNCOMPRESSED_NAME" + if [ ! -f "$BIONIC_OS_QCOW2_UNCOMPRESSED_IMAGE" ]; then + pushd $WORKLOADS_DIR + time qemu-img convert -p -f raw -O qcow2 $BIONIC_OS_RAW_IMAGE_NAME $BIONIC_OS_QCOW2_UNCOMPRESSED_IMAGE || exit 1 + popd + fi + + FOCAL_OS_RAW_IMAGE_NAME="focal-server-cloudimg-arm64-custom-20210929-0.raw" + FOCAL_OS_RAW_IMAGE_DOWNLOAD_URL="https://cloud-hypervisor.azureedge.net/$FOCAL_OS_RAW_IMAGE_NAME" + FOCAL_OS_RAW_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_RAW_IMAGE_NAME" + if [ ! -f "$FOCAL_OS_RAW_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $FOCAL_OS_RAW_IMAGE_DOWNLOAD_URL || exit 1 + popd + fi + + FOCAL_OS_QCOW2_IMAGE_UNCOMPRESSED_NAME="focal-server-cloudimg-arm64-custom-20210929-0.qcow2" + FOCAL_OS_QCOW2_IMAGE_UNCOMPRESSED_DOWNLOAD_URL="https://cloud-hypervisor.azureedge.net/$FOCAL_OS_QCOW2_IMAGE_UNCOMPRESSED_NAME" + FOCAL_OS_QCOW2_UNCOMPRESSED_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_QCOW2_IMAGE_UNCOMPRESSED_NAME" + if [ ! -f "$FOCAL_OS_QCOW2_UNCOMPRESSED_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $FOCAL_OS_QCOW2_IMAGE_UNCOMPRESSED_DOWNLOAD_URL || exit 1 + popd + fi + + JAMMY_OS_RAW_IMAGE_NAME="jammy-server-cloudimg-arm64-custom-20220329-0.raw" + JAMMY_OS_RAW_IMAGE_DOWNLOAD_URL="https://cloud-hypervisor.azureedge.net/$JAMMY_OS_RAW_IMAGE_NAME" + JAMMY_OS_RAW_IMAGE="$WORKLOADS_DIR/$JAMMY_OS_RAW_IMAGE_NAME" + if [ ! -f "$JAMMY_OS_RAW_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $JAMMY_OS_RAW_IMAGE_DOWNLOAD_URL || exit 1 + popd + fi + + JAMMY_OS_QCOW2_IMAGE_UNCOMPRESSED_NAME="jammy-server-cloudimg-arm64-custom-20220329-0.qcow2" + JAMMY_OS_QCOW2_IMAGE_UNCOMPRESSED_DOWNLOAD_URL="https://cloud-hypervisor.azureedge.net/$JAMMY_OS_QCOW2_IMAGE_UNCOMPRESSED_NAME" + JAMMY_OS_QCOW2_UNCOMPRESSED_IMAGE="$WORKLOADS_DIR/$JAMMY_OS_QCOW2_IMAGE_UNCOMPRESSED_NAME" + if [ ! -f "$JAMMY_OS_QCOW2_UNCOMPRESSED_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $JAMMY_OS_QCOW2_IMAGE_UNCOMPRESSED_DOWNLOAD_URL || exit 1 + popd + fi + + ALPINE_MINIROOTFS_URL="http://dl-cdn.alpinelinux.org/alpine/v3.11/releases/aarch64/alpine-minirootfs-3.11.3-aarch64.tar.gz" + ALPINE_MINIROOTFS_TARBALL="$WORKLOADS_DIR/alpine-minirootfs-aarch64.tar.gz" + if [ ! -f "$ALPINE_MINIROOTFS_TARBALL" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $ALPINE_MINIROOTFS_URL -O $ALPINE_MINIROOTFS_TARBALL || exit 1 + popd + fi + + ALPINE_INITRAMFS_IMAGE="$WORKLOADS_DIR/alpine_initramfs.img" + if [ ! -f "$ALPINE_INITRAMFS_IMAGE" ]; then + pushd $WORKLOADS_DIR + mkdir alpine-minirootfs + tar xf "$ALPINE_MINIROOTFS_TARBALL" -C alpine-minirootfs + cat > alpine-minirootfs/init <<-EOF + #! /bin/sh + mount -t devtmpfs dev /dev + echo \$TEST_STRING > /dev/console + poweroff -f + EOF + chmod +x alpine-minirootfs/init + cd alpine-minirootfs + find . -print0 | + cpio --null --create --verbose --owner root:root --format=newc > "$ALPINE_INITRAMFS_IMAGE" + popd + fi + + pushd $WORKLOADS_DIR + sha1sum sha1sums-aarch64 --check + if [ $? -ne 0 ]; then + echo "sha1sum validation of images failed, remove invalid images to fix the issue." + exit 1 + fi + popd + + # Download Cloud Hypervisor binary from its last stable release + LAST_RELEASE_VERSION="v26.0" + CH_RELEASE_URL="https://github.com/cloud-hypervisor/cloud-hypervisor/releases/download/$LAST_RELEASE_VERSION/cloud-hypervisor-static-aarch64" + CH_RELEASE_NAME="cloud-hypervisor-static-aarch64" + pushd $WORKLOADS_DIR + time wget --quiet $CH_RELEASE_URL -O "$CH_RELEASE_NAME" || exit 1 + chmod +x $CH_RELEASE_NAME + popd + + # Build custom kernel for guest VMs + build_custom_linux + + # Update the kernel in the cloud image for some tests that requires recent kernel version + FOCAL_OS_RAW_IMAGE_UPDATE_KERNEL_NAME="focal-server-cloudimg-arm64-custom-20210929-0-update-kernel.raw" + cp "$WORKLOADS_DIR/$FOCAL_OS_RAW_IMAGE_NAME" "$WORKLOADS_DIR/$FOCAL_OS_RAW_IMAGE_UPDATE_KERNEL_NAME" + FOCAL_OS_RAW_IMAGE_UPDATE_KERNEL_ROOT_DIR="$WORKLOADS_DIR/focal-server-cloudimg-root" + mkdir -p "$FOCAL_OS_RAW_IMAGE_UPDATE_KERNEL_ROOT_DIR" + # Mount the 'raw' image, replace the compressed kernel file and umount the working folder + guestmount -a "$WORKLOADS_DIR/$FOCAL_OS_RAW_IMAGE_UPDATE_KERNEL_NAME" -m /dev/sda1 "$FOCAL_OS_RAW_IMAGE_UPDATE_KERNEL_ROOT_DIR" || exit 1 + cp "$WORKLOADS_DIR"/Image.gz "$FOCAL_OS_RAW_IMAGE_UPDATE_KERNEL_ROOT_DIR"/boot/vmlinuz + guestunmount "$FOCAL_OS_RAW_IMAGE_UPDATE_KERNEL_ROOT_DIR" + + # Build virtiofsd + build_virtiofsd + + BLK_IMAGE="$WORKLOADS_DIR/blk.img" + MNT_DIR="mount_image" + if [ ! -f "$BLK_IMAGE" ]; then + pushd $WORKLOADS_DIR + fallocate -l 16M $BLK_IMAGE + mkfs.ext4 -j $BLK_IMAGE + mkdir $MNT_DIR + sudo mount -t ext4 $BLK_IMAGE $MNT_DIR + sudo bash -c "echo bar > $MNT_DIR/foo" || exit 1 + sudo umount $BLK_IMAGE + rm -r $MNT_DIR + popd + fi + + SHARED_DIR="$WORKLOADS_DIR/shared_dir" + if [ ! -d "$SHARED_DIR" ]; then + mkdir -p $SHARED_DIR + echo "foo" > "$SHARED_DIR/file1" + echo "bar" > "$SHARED_DIR/file3" || exit 1 + fi + + # Checkout and build SPDK NVMe + build_spdk_nvme + + # Checkout and build EDK2 + build_edk2 +} + +process_common_args "$@" + +# aarch64 not supported for MSHV +if [[ "$hypervisor" = "mshv" ]]; then + echo "AArch64 is not supported in Microsoft Hypervisor" + exit 1 +fi + +# For now these values are deafult for kvm +features="" + +# lock the workloads folder to avoid parallel updating by different containers +( + echo "try to lock $WORKLOADS_DIR folder and update" + flock -x 12 && update_workloads +) 12>$WORKLOADS_LOCK + +# Check if there is any error in the execution of `update_workloads`. +# If there is any error, then kill the shell. Otherwise the script will continue +# running even if the `update_workloads` function was failed. +RES=$? +if [ $RES -ne 0 ]; then + exit 1 +fi + +BUILD_TARGET="aarch64-unknown-linux-${CH_LIBC}" +if [[ "${BUILD_TARGET}" == "aarch64-unknown-linux-musl" ]]; then +export TARGET_CC="musl-gcc" +export RUSTFLAGS="-C link-arg=-lgcc -C link_arg=-specs -C link_arg=/usr/lib/aarch64-linux-musl/musl-gcc.specs" +fi + +export RUST_BACKTRACE=1 + +# Test without ACPI +cargo build --all --release $features --target $BUILD_TARGET +strip target/$BUILD_TARGET/release/cloud-hypervisor +strip target/$BUILD_TARGET/release/vhost_user_net +strip target/$BUILD_TARGET/release/ch-remote + +# Enable KSM with some reasonable parameters so that it won't take too long +# for the memory to be merged between two processes. +sudo bash -c "echo 1000000 > /sys/kernel/mm/ksm/pages_to_scan" +sudo bash -c "echo 10 > /sys/kernel/mm/ksm/sleep_millisecs" +sudo bash -c "echo 1 > /sys/kernel/mm/ksm/run" + +# Both test_vfio and ovs-dpdk rely on hugepages +echo 6144 | sudo tee /proc/sys/vm/nr_hugepages +sudo chmod a+rwX /dev/hugepages + +# Run all direct kernel boot (Device Tree) test cases in mod `parallel` +time cargo test $features "common_parallel::$test_filter" --target $BUILD_TARGET -- ${test_binary_args[*]} +RES=$? + +# Run some tests in sequence since the result could be affected by other tests +# running in parallel. +if [ $RES -eq 0 ]; then + time cargo test $features "common_sequential::$test_filter" --target $BUILD_TARGET -- --test-threads=1 ${test_binary_args[*]} + RES=$? +else + exit $RES +fi + +# Run all ACPI test cases +if [ $RES -eq 0 ]; then + time cargo test $features "aarch64_acpi::$test_filter" --target $BUILD_TARGET -- ${test_binary_args[*]} + RES=$? +else + exit $RES +fi + +# Run all test cases related to live migration +if [ $RES -eq 0 ]; then + time cargo test $features "live_migration_parallel::$test_filter" --target $BUILD_TARGET -- ${test_binary_args[*]} + RES=$? +else + exit $RES +fi + +if [ $RES -eq 0 ]; then + time cargo test $features "live_migration_sequential::$test_filter" --target $BUILD_TARGET -- --test-threads=1 ${test_binary_args[*]} + RES=$? +else + exit $RES +fi + +exit $RES diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_live_migration.sh b/vendor/cube-hypervisor/scripts/run_integration_tests_live_migration.sh new file mode 100755 index 0000000..7f8575c --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_integration_tests_live_migration.sh @@ -0,0 +1,110 @@ +#!/bin/bash +set -x + +source $HOME/.cargo/env +source $(dirname "$0")/test-util.sh + +export BUILD_TARGET=${BUILD_TARGET-x86_64-unknown-linux-gnu} + +WORKLOADS_DIR="$HOME/workloads" +mkdir -p "$WORKLOADS_DIR" + +process_common_args "$@" + +# For now these values are default for kvm +features="" + +if [ "$hypervisor" = "mshv" ] ; then + features="--no-default-features --features mshv" +fi + +cp scripts/sha1sums-x86_64 $WORKLOADS_DIR + +FOCAL_OS_IMAGE_NAME="focal-server-cloudimg-amd64-custom-20210609-0.qcow2" +FOCAL_OS_IMAGE_URL="https://cloud-hypervisor.azureedge.net/$FOCAL_OS_IMAGE_NAME" +FOCAL_OS_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_IMAGE_NAME" +if [ ! -f "$FOCAL_OS_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $FOCAL_OS_IMAGE_URL || exit 1 + popd +fi + +FOCAL_OS_RAW_IMAGE_NAME="focal-server-cloudimg-amd64-custom-20210609-0.raw" +FOCAL_OS_RAW_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_RAW_IMAGE_NAME" +if [ ! -f "$FOCAL_OS_RAW_IMAGE" ]; then + pushd $WORKLOADS_DIR + time qemu-img convert -p -f qcow2 -O raw $FOCAL_OS_IMAGE_NAME $FOCAL_OS_RAW_IMAGE_NAME || exit 1 + popd +fi + +pushd $WORKLOADS_DIR +grep focal sha1sums-x86_64 | sha1sum --check +if [ $? -ne 0 ]; then + echo "sha1sum validation of images failed, remove invalid images to fix the issue." + exit 1 +fi +popd + +# Build custom kernel based on virtio-pmem and virtio-fs upstream patches +VMLINUX_IMAGE="$WORKLOADS_DIR/vmlinux" + +LINUX_CUSTOM_DIR="$WORKLOADS_DIR/linux-custom" + +if [ ! -f "$VMLINUX_IMAGE" ]; then + SRCDIR=$PWD + pushd $WORKLOADS_DIR + time git clone --depth 1 "https://github.com/cloud-hypervisor/linux.git" -b "ch-5.15.12" $LINUX_CUSTOM_DIR + cp $SRCDIR/resources/linux-config-x86_64 $LINUX_CUSTOM_DIR/.config + popd +fi + +if [ ! -f "$VMLINUX_IMAGE" ]; then + pushd $LINUX_CUSTOM_DIR + time make bzImage -j `nproc` + cp vmlinux $VMLINUX_IMAGE || exit 1 + popd +fi + +if [ -d "$LINUX_CUSTOM_DIR" ]; then + rm -rf $LINUX_CUSTOM_DIR +fi + +BUILD_TARGET="$(uname -m)-unknown-linux-${CH_LIBC}" +CFLAGS="" +TARGET_CC="" +if [[ "${BUILD_TARGET}" == "x86_64-unknown-linux-musl" ]]; then + TARGET_CC="musl-gcc" + CFLAGS="-I /usr/include/x86_64-linux-musl/ -idirafter /usr/include/" +fi + +cargo build --all --release $features --target $BUILD_TARGET +strip target/$BUILD_TARGET/release/cube-hypervisor +strip target/$BUILD_TARGET/release/vhost_user_net +strip target/$BUILD_TARGET/release/ch-remote + +# Use locally-built cube-hypervisor as the "old release" binary for live +# upgrade tests. Upstream cloud-hypervisor-static v26 lacks PVM CPUID +# support and cannot boot the PVM guest kernel. Both source and +# destination use the same binary, so the cross-version upgrade path +# is not exercised on PVM. +CH_RELEASE_NAME="cloud-hypervisor-static" +cp -f target/$BUILD_TARGET/release/cube-hypervisor "$WORKLOADS_DIR"/"$CH_RELEASE_NAME" || exit 1 +chmod +x "$WORKLOADS_DIR"/"$CH_RELEASE_NAME" + +# Test ovs-dpdk relies on hugepages +echo 6144 | sudo tee /proc/sys/vm/nr_hugepages +sudo chmod a+rwX /dev/hugepages + +export RUST_BACKTRACE=1 +time cargo test $features "live_migration_parallel::$test_filter" -- ${test_binary_args[*]} +RES=$? + +# Run some tests in sequence since the result could be affected by other tests +# running in parallel. +if [ $RES -eq 0 ]; then + export RUST_BACKTRACE=1 + time cargo test $features "live_migration_sequential::$test_filter" -- --test-threads=1 ${test_binary_args[*]} + RES=$? +fi + +exit $RES diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_rate_limiter.sh b/vendor/cube-hypervisor/scripts/run_integration_tests_rate_limiter.sh new file mode 100755 index 0000000..62df368 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_integration_tests_rate_limiter.sh @@ -0,0 +1,89 @@ +#!/bin/bash +set -x + +source $HOME/.cargo/env +source $(dirname "$0")/test-util.sh + +export BUILD_TARGET=${BUILD_TARGET-x86_64-unknown-linux-gnu} + +WORKLOADS_DIR="$HOME/workloads" +mkdir -p "$WORKLOADS_DIR" + +process_common_args "$@" + +# For now these values are default for kvm +features="" + +if [ "$hypervisor" = "mshv" ] ; then + features="--no-default-features --features mshv" +fi + +cp scripts/sha1sums-x86_64 $WORKLOADS_DIR + +FOCAL_OS_IMAGE_NAME="focal-server-cloudimg-amd64-custom-20210609-0.qcow2" +FOCAL_OS_IMAGE_URL="https://cloud-hypervisor.azureedge.net/$FOCAL_OS_IMAGE_NAME" +FOCAL_OS_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_IMAGE_NAME" +if [ ! -f "$FOCAL_OS_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $FOCAL_OS_IMAGE_URL || exit 1 + popd +fi + +FOCAL_OS_RAW_IMAGE_NAME="focal-server-cloudimg-amd64-custom-20210609-0.raw" +FOCAL_OS_RAW_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_RAW_IMAGE_NAME" +if [ ! -f "$FOCAL_OS_RAW_IMAGE" ]; then + pushd $WORKLOADS_DIR + time qemu-img convert -p -f qcow2 -O raw $FOCAL_OS_IMAGE_NAME $FOCAL_OS_RAW_IMAGE_NAME || exit 1 + popd +fi + +pushd $WORKLOADS_DIR +grep focal sha1sums-x86_64 | sha1sum --check +if [ $? -ne 0 ]; then + echo "sha1sum validation of images failed, remove invalid images to fix the issue." + exit 1 +fi +popd + +# Build custom kernel based on virtio-pmem and virtio-fs upstream patches +VMLINUX_IMAGE="$WORKLOADS_DIR/vmlinux" + +LINUX_CUSTOM_DIR="$WORKLOADS_DIR/linux-custom" + +if [ ! -f "$VMLINUX_IMAGE" ]; then + SRCDIR=$PWD + pushd $WORKLOADS_DIR + time git clone --depth 1 "https://github.com/cloud-hypervisor/linux.git" -b "ch-5.15.12" $LINUX_CUSTOM_DIR + cp $SRCDIR/resources/linux-config-x86_64 $LINUX_CUSTOM_DIR/.config + popd +fi + +if [ ! -f "$VMLINUX_IMAGE" ]; then + pushd $LINUX_CUSTOM_DIR + time make bzImage -j `nproc` + cp vmlinux $VMLINUX_IMAGE || exit 1 + popd +fi + +if [ -d "$LINUX_CUSTOM_DIR" ]; then + rm -rf $LINUX_CUSTOM_DIR +fi + +BUILD_TARGET="$(uname -m)-unknown-linux-${CH_LIBC}" +CFLAGS="" +TARGET_CC="" +if [[ "${BUILD_TARGET}" == "x86_64-unknown-linux-musl" ]]; then + TARGET_CC="musl-gcc" + CFLAGS="-I /usr/include/x86_64-linux-musl/ -idirafter /usr/include/" +fi + +cargo build --all --release $features --target $BUILD_TARGET +strip target/$BUILD_TARGET/release/cloud-hypervisor +strip target/$BUILD_TARGET/release/vhost_user_net +strip target/$BUILD_TARGET/release/ch-remote + +export RUST_BACKTRACE=1 +time cargo test $features "rate_limiter::$test_filter" -- --test-threads=1 ${test_binary_args[*]} +RES=$? + +exit $RES diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_sgx.sh b/vendor/cube-hypervisor/scripts/run_integration_tests_sgx.sh new file mode 100755 index 0000000..6a5334a --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_integration_tests_sgx.sh @@ -0,0 +1,33 @@ +#!/bin/bash +set -x + +source $HOME/.cargo/env +source $(dirname "$0")/test-util.sh + +process_common_args "$@" + +if [[ "$hypervisor" = "mshv" ]]; then + echo "Unsupported SGX test for MSHV" + exit 1 +fi + +# For now these values are default for kvm +features="" + +BUILD_TARGET="$(uname -m)-unknown-linux-${CH_LIBC}" +CFLAGS="" +TARGET_CC="" +if [[ "${BUILD_TARGET}" == "x86_64-unknown-linux-musl" ]]; then +TARGET_CC="musl-gcc" +CFLAGS="-I /usr/include/x86_64-linux-musl/ -idirafter /usr/include/" +fi + +cargo build --all --release $features --target $BUILD_TARGET +strip target/$BUILD_TARGET/release/cloud-hypervisor + +export RUST_BACKTRACE=1 + +time cargo test $features "sgx::$test_filter" -- ${test_binary_args[*]} +RES=$? + +exit $RES diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_vfio.sh b/vendor/cube-hypervisor/scripts/run_integration_tests_vfio.sh new file mode 100755 index 0000000..77db605 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_integration_tests_vfio.sh @@ -0,0 +1,67 @@ +#!/bin/bash +set -x + +source $HOME/.cargo/env +source $(dirname "$0")/test-util.sh + +process_common_args "$@" +# For now these values are default for kvm +features="" + +WORKLOADS_DIR="$HOME/workloads" +FOCAL_OS_IMAGE_NAME="focal-server-cloudimg-amd64-custom-20210609-0.raw" +FOCAL_OS_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_IMAGE_NAME" +FW="$WORKLOADS_DIR/hypervisor-fw" +VMLINUX_IMAGE="$WORKLOADS_DIR/vmlinux" + +BLK_IMAGE="$WORKLOADS_DIR/blk.img" +MNT_DIR="mount_image" +rm -rf $BLK_IMAGE +pushd $WORKLOADS_DIR +fallocate -l 16M $BLK_IMAGE +mkfs.ext4 -j $BLK_IMAGE +mkdir $MNT_DIR +sudo mount -t ext4 $BLK_IMAGE $MNT_DIR +sudo bash -c "echo bar > $MNT_DIR/foo" || exit 1 +sudo umount $BLK_IMAGE +rm -r $MNT_DIR +popd + +VFIO_DIR="$WORKLOADS_DIR/vfio" +VFIO_DISK_IMAGE="$WORKLOADS_DIR/vfio.img" +rm -rf $VFIO_DIR $VFIO_DISK_IMAGE +mkdir -p $VFIO_DIR +cp $FOCAL_OS_IMAGE $VFIO_DIR +cp $FW $VFIO_DIR +cp $VMLINUX_IMAGE $VFIO_DIR || exit 1 + +BUILD_TARGET="$(uname -m)-unknown-linux-${CH_LIBC}" +CFLAGS="" +TARGET_CC="" +if [[ "${BUILD_TARGET}" == "x86_64-unknown-linux-musl" ]]; then +TARGET_CC="musl-gcc" +CFLAGS="-I /usr/include/x86_64-linux-musl/ -idirafter /usr/include/" +fi + +cargo build --all --release $features --target $BUILD_TARGET +strip target/$BUILD_TARGET/release/cloud-hypervisor +strip target/$BUILD_TARGET/release/ch-remote + +# We always copy a fresh version of our binary for our L2 guest. +cp target/$BUILD_TARGET/release/cloud-hypervisor $VFIO_DIR +cp target/$BUILD_TARGET/release/ch-remote $VFIO_DIR + +# test_vfio rely on hugepages +echo 6144 | sudo tee /proc/sys/vm/nr_hugepages +sudo chmod a+rwX /dev/hugepages + +export RUST_BACKTRACE=1 +time cargo test $features "vfio::test_vfio" -- ${test_binary_args[*]} +RES=$? + +if [ $RES -eq 0 ]; then + time cargo test $features "vfio::test_nvidia" -- --test-threads=1 ${test_binary_args[*]} + RES=$? +fi + +exit $RES diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_windows_aarch64.sh b/vendor/cube-hypervisor/scripts/run_integration_tests_windows_aarch64.sh new file mode 100755 index 0000000..91d5819 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_integration_tests_windows_aarch64.sh @@ -0,0 +1,60 @@ +#!/bin/bash +set -x + +source $HOME/.cargo/env +source $(dirname "$0")/test-util.sh +source $(dirname "$0")/common-aarch64.sh + +process_common_args "$@" +# For now these values are default for kvm +features="" + +# aarch64 not supported for MSHV +if [[ "$hypervisor" = "mshv" ]]; then + echo "AArch64 is not supported in Microsoft Hypervisor" + exit 1 +fi + +WIN_IMAGE_BASENAME="windows-11-iot-enterprise-aarch64.raw" +WIN_IMAGE_FILE="$WORKLOADS_DIR/$WIN_IMAGE_BASENAME" + +# Checkout and build EDK2 +OVMF_FW="$WORKLOADS_DIR/CLOUDHV_EFI.fd" +build_edk2 + +BUILD_TARGET="$(uname -m)-unknown-linux-${CH_LIBC}" +CFLAGS="" +TARGET_CC="" +if [[ "${BUILD_TARGET}" == "aarch64-unknown-linux-musl" ]]; then +export TARGET_CC="musl-gcc" +export RUSTFLAGS="-C link-arg=-lgcc -C link_arg=-specs -C link_arg=/usr/lib/aarch64-linux-musl/musl-gcc.specs" +fi + +# Check if the images are present +if [[ ! -f ${WIN_IMAGE_FILE} || ! -f ${OVMF_FW} ]]; then + echo "Windows image/firmware not present in the host" + exit 1 +fi + +# Use device mapper to create a snapshot of the Windows image +img_blk_size=$(du -b -B 512 ${WIN_IMAGE_FILE} | awk '{print $1;}') +loop_device=$(losetup --find --show --read-only ${WIN_IMAGE_FILE}) +dmsetup create windows-base --table "0 $img_blk_size linear $loop_device 0" +dmsetup mknodes +dmsetup create windows-snapshot-base --table "0 $img_blk_size snapshot-origin /dev/mapper/windows-base" +dmsetup mknodes + +export RUST_BACKTRACE=1 + +cargo build --all --release $features --target $BUILD_TARGET +strip target/$BUILD_TARGET/release/cloud-hypervisor + +# Only run with 1 thread to avoid tests interfering with one another because +# Windows has a static IP configured +time cargo test $features "windows::$test_filter" --target $BUILD_TARGET -- ${test_binary_args[*]} +RES=$? + +dmsetup remove_all -f +losetup -D + +exit $RES diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_windows_x86_64.sh b/vendor/cube-hypervisor/scripts/run_integration_tests_windows_x86_64.sh new file mode 100755 index 0000000..fe226bc --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_integration_tests_windows_x86_64.sh @@ -0,0 +1,60 @@ +#!/bin/bash +set -x + +source $HOME/.cargo/env +source $(dirname "$0")/test-util.sh + +process_common_args "$@" +# For now these values are default for kvm +features="" + +if [ "$hypervisor" = "mshv" ] ; then + features="--no-default-features --features mshv" +fi +WIN_IMAGE_FILE="/root/workloads/windows-server-2019.raw" + +WORKLOADS_DIR="/root/workloads" +OVMF_FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/edk2/releases/latest | grep "browser_download_url" | grep -o 'https://.*[^ "]') +OVMF_FW="$WORKLOADS_DIR/CLOUDHV.fd" +if [ ! -f "$OVMF_FW" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $OVMF_FW_URL || exit 1 + popd +fi + +BUILD_TARGET="$(uname -m)-unknown-linux-${CH_LIBC}" +CFLAGS="" +TARGET_CC="" +if [[ "${BUILD_TARGET}" == "x86_64-unknown-linux-musl" ]]; then +TARGET_CC="musl-gcc" +CFLAGS="-I /usr/include/x86_64-linux-musl/ -idirafter /usr/include/" +fi + +# Check if the images are present +if [[ ! -f ${WIN_IMAGE_FILE} || ! -f ${OVMF_FW} ]]; then + echo "Windows image/firmware not present in the host" + exit 1 +fi + +# Use device mapper to create a snapshot of the Windows image +img_blk_size=$(du -b -B 512 ${WIN_IMAGE_FILE} | awk '{print $1;}') +loop_device=$(losetup --find --show --read-only ${WIN_IMAGE_FILE}) +dmsetup create windows-base --table "0 $img_blk_size linear $loop_device 0" +dmsetup mknodes +dmsetup create windows-snapshot-base --table "0 $img_blk_size snapshot-origin /dev/mapper/windows-base" +dmsetup mknodes + +cargo build --all --release $features --target $BUILD_TARGET +strip target/$BUILD_TARGET/release/cloud-hypervisor + +export RUST_BACKTRACE=1 + +# Only run with 1 thread to avoid tests interfering with one another because +# Windows has a static IP configured +time cargo test $features "windows::$test_filter" -- ${test_binary_args[*]} +RES=$? + +dmsetup remove_all -f +losetup -D + +exit $RES diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_x86_64.sh b/vendor/cube-hypervisor/scripts/run_integration_tests_x86_64.sh new file mode 100755 index 0000000..2d0ecb4 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_integration_tests_x86_64.sh @@ -0,0 +1,326 @@ +#!/bin/bash +set -x + +source $HOME/.cargo/env +source $(dirname "$0")/test-util.sh + +export BUILD_TARGET=${BUILD_TARGET-x86_64-unknown-linux-gnu} + +# Clean up leftover temp dirs from previous test runs +rm -rf /tmp/ch[A-Za-z]* + +WORKLOADS_DIR="$HOME/workloads" +mkdir -p "$WORKLOADS_DIR" + +process_common_args "$@" + +# Print machine specs and disk usage +echo "=== Machine Specs ===" +echo "Hostname: $(hostname)" +echo "CPU Model: $(grep -m1 'model name' /proc/cpuinfo | awk -F: '{print $2}' | xargs)" +echo "CPU Cores: $(nproc)" +echo "Total Memory: $(free -h | awk '/^Mem:/{print $2}')" +echo "Kernel: $(uname -r)" +echo "Architecture: $(uname -m)" +echo "" +echo "=== Disk Usage ===" +df -h / /tmp $HOME 2>/dev/null | sort -u +echo "" + +# For now these values are default for kvm +features="" + +if [ "$hypervisor" = "mshv" ] ; then + features="--no-default-features --features mshv" +fi + +cp scripts/sha1sums-x86_64 $WORKLOADS_DIR + +FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/latest | grep "browser_download_url" | grep -o 'https://.*[^ "]') +FW="$WORKLOADS_DIR/hypervisor-fw" +if [ ! -f "$FW" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $FW_URL || exit 1 + popd +fi + +OVMF_FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/edk2/releases/latest | grep "browser_download_url" | grep -o 'https://.*[^ "]') +OVMF_FW="$WORKLOADS_DIR/CLOUDHV.fd" +if [ ! -f "$OVMF_FW" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $OVMF_FW_URL || exit 1 + popd +fi + +BIONIC_OS_IMAGE_NAME="bionic-server-cloudimg-amd64.qcow2" +BIONIC_OS_IMAGE_URL="https://github.com/lisongqian/CubeSandbox/releases/download/ci/$BIONIC_OS_IMAGE_NAME.zip" +BIONIC_OS_IMAGE="$WORKLOADS_DIR/$BIONIC_OS_IMAGE_NAME" +if [ ! -f "$BIONIC_OS_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $BIONIC_OS_IMAGE_URL || exit 1 + mv "$BIONIC_OS_IMAGE_NAME.zip" $BIONIC_OS_IMAGE_NAME + popd +fi + +BIONIC_OS_RAW_IMAGE_NAME="bionic-server-cloudimg-amd64.raw" +BIONIC_OS_RAW_IMAGE="$WORKLOADS_DIR/$BIONIC_OS_RAW_IMAGE_NAME" +if [ ! -f "$BIONIC_OS_RAW_IMAGE" ]; then + pushd $WORKLOADS_DIR + time qemu-img convert -p -f qcow2 -O raw $BIONIC_OS_IMAGE_NAME $BIONIC_OS_RAW_IMAGE_NAME || exit 1 + popd +fi + + +FOCAL_OS_IMAGE_NAME="focal-server-cloudimg-amd64-custom-20210609-0.qcow2" +FOCAL_OS_IMAGE_URL="https://github.com/lisongqian/CubeSandbox/releases/download/ci/$FOCAL_OS_IMAGE_NAME.zip" +FOCAL_OS_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_IMAGE_NAME" +if [ ! -f "$FOCAL_OS_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $FOCAL_OS_IMAGE_URL || exit 1 + mv "$FOCAL_OS_IMAGE_NAME.zip" $FOCAL_OS_IMAGE_NAME + popd +fi + +FOCAL_OS_RAW_IMAGE_NAME="focal-server-cloudimg-amd64-custom-20210609-0.raw" +FOCAL_OS_RAW_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_RAW_IMAGE_NAME" +if [ ! -f "$FOCAL_OS_RAW_IMAGE" ]; then + pushd $WORKLOADS_DIR + time qemu-img convert -p -f qcow2 -O raw $FOCAL_OS_IMAGE_NAME $FOCAL_OS_RAW_IMAGE_NAME || exit 1 + popd +fi + +JAMMY_OS_IMAGE_NAME="jammy-server-cloudimg-amd64-custom-20220329-0.qcow2" +JAMMY_OS_IMAGE_URL="https://github.com/lisongqian/CubeSandbox/releases/download/ci/$JAMMY_OS_IMAGE_NAME.zip" +JAMMY_OS_IMAGE="$WORKLOADS_DIR/$JAMMY_OS_IMAGE_NAME" +if [ ! -f "$JAMMY_OS_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $JAMMY_OS_IMAGE_URL || exit 1 + mv "$JAMMY_OS_IMAGE_NAME.zip" $JAMMY_OS_IMAGE_NAME + popd +fi + +JAMMY_OS_RAW_IMAGE_NAME="jammy-server-cloudimg-amd64-custom-20220329-0.raw" +JAMMY_OS_RAW_IMAGE="$WORKLOADS_DIR/$JAMMY_OS_RAW_IMAGE_NAME" +if [ ! -f "$JAMMY_OS_RAW_IMAGE" ]; then + pushd $WORKLOADS_DIR + time qemu-img convert -p -f qcow2 -O raw $JAMMY_OS_IMAGE_NAME $JAMMY_OS_RAW_IMAGE_NAME || exit 1 + popd +fi + +ALPINE_MINIROOTFS_URL="http://dl-cdn.alpinelinux.org/alpine/v3.11/releases/x86_64/alpine-minirootfs-3.11.3-x86_64.tar.gz" +ALPINE_MINIROOTFS_TARBALL="$WORKLOADS_DIR/alpine-minirootfs-x86_64.tar.gz" +if [ ! -f "$ALPINE_MINIROOTFS_TARBALL" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $ALPINE_MINIROOTFS_URL -O $ALPINE_MINIROOTFS_TARBALL || exit 1 + popd +fi + +ALPINE_INITRAMFS_IMAGE="$WORKLOADS_DIR/alpine_initramfs.img" +if [ ! -f "$ALPINE_INITRAMFS_IMAGE" ]; then + pushd $WORKLOADS_DIR + mkdir alpine-minirootfs + tar xf "$ALPINE_MINIROOTFS_TARBALL" -C alpine-minirootfs + cat > alpine-minirootfs/init <<-EOF + #! /bin/sh + mount -t devtmpfs dev /dev + echo \$TEST_STRING > /dev/console + poweroff -f + EOF + chmod +x alpine-minirootfs/init + cd alpine-minirootfs + find . -print0 | + cpio --null --create --verbose --owner root:root --format=newc > "$ALPINE_INITRAMFS_IMAGE" + popd +fi + +pushd $WORKLOADS_DIR +sha1sum sha1sums-x86_64 --check +if [ $? -ne 0 ]; then + echo "sha1sum validation of images failed, remove invalid images to fix the issue." + exit 1 +fi +popd + +# Build custom kernel based on virtio-pmem and virtio-fs upstream patches +VMLINUX_IMAGE="$WORKLOADS_DIR/vmlinux" +if [ ! -f "$VMLINUX_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet https://github.com/lisongqian/CubeSandbox/releases/download/vmlinux/vmlinux || exit 1 +# build_custom_linux + popd +fi + +VIRTIOFSD="$WORKLOADS_DIR/virtiofsd" +VIRTIOFSD_DIR="virtiofsd_build" +if [ ! -f "$VIRTIOFSD" ]; then + pushd $WORKLOADS_DIR + git clone "https://gitlab.com/virtio-fs/virtiofsd.git" $VIRTIOFSD_DIR + pushd $VIRTIOFSD_DIR + git checkout v1.1.0 + time cargo build --release + cp target/release/virtiofsd $VIRTIOFSD || exit 1 + popd + rm -rf $VIRTIOFSD_DIR + popd +fi + + +BLK_IMAGE="$WORKLOADS_DIR/blk.img" +MNT_DIR="mount_image" +if [ ! -f "$BLK_IMAGE" ]; then + pushd $WORKLOADS_DIR + fallocate -l 16M $BLK_IMAGE + mkfs.ext4 -j $BLK_IMAGE + mkdir $MNT_DIR + sudo mount -t ext4 $BLK_IMAGE $MNT_DIR + sudo bash -c "echo bar > $MNT_DIR/foo" || exit 1 + sudo umount $BLK_IMAGE + rm -r $MNT_DIR + popd +fi + +SHARED_DIR="$WORKLOADS_DIR/shared_dir" +if [ ! -d "$SHARED_DIR" ]; then + mkdir -p $SHARED_DIR + echo "foo" > "$SHARED_DIR/file1" + echo "bar" > "$SHARED_DIR/file3" || exit 1 +fi + +VFIO_DIR="$WORKLOADS_DIR/vfio" +VFIO_DISK_IMAGE="$WORKLOADS_DIR/vfio.img" +rm -rf $VFIO_DIR $VFIO_DISK_IMAGE +mkdir -p $VFIO_DIR +cp $FOCAL_OS_RAW_IMAGE $VFIO_DIR +cp $FW $VFIO_DIR +cp $VMLINUX_IMAGE $VFIO_DIR || exit 1 + +BUILD_TARGET="$(uname -m)-unknown-linux-${CH_LIBC}" + +cargo build --all --release $features --target $BUILD_TARGET +strip target/$BUILD_TARGET/release/cube-hypervisor +strip target/$BUILD_TARGET/release/vhost_user_net +strip target/$BUILD_TARGET/release/ch-remote + +# We always copy a fresh version of our binary for our L2 guest. +cp target/$BUILD_TARGET/release/cube-hypervisor $VFIO_DIR +cp target/$BUILD_TARGET/release/ch-remote $VFIO_DIR + +# Enable KSM with some reasonable parameters so that it won't take too long +# for the memory to be merged between two processes. +sudo bash -c "echo 1000000 > /sys/kernel/mm/ksm/pages_to_scan" +sudo bash -c "echo 10 > /sys/kernel/mm/ksm/sleep_millisecs" +sudo bash -c "echo 1 > /sys/kernel/mm/ksm/run" + +# Both test_vfio, ovs-dpdk and vDPA tests rely on hugepages +echo 6144 | sudo tee /proc/sys/vm/nr_hugepages +sudo chmod a+rwX /dev/hugepages + +# Update max locked memory to 'unlimited' to avoid issues with vDPA +ulimit -l unlimited + +export RUST_BACKTRACE=1 + +# Quick mode: run only core smoke tests across 5 priority levels +if [ "$quick_mode" = "true" ]; then + echo "=== Quick mode: running core smoke tests ===" + + # Priority 1: Boot & Lifecycle + PRIORITY1_TESTS="test_focal_hypervisor_fw|test_direct_kernel_boot|test_multi_cpu|test_power_button|test_api_create_boot|test_api_shutdown|test_api_pause_resume" + + # Priority 2: Core I/O Devices + PRIORITY2_TESTS="test_virtio_block|test_virtio_net_ctrl_queue|test_native_virtio_fs_hotplug|test_virtio_vsock|test_virtio_console|test_serial_tty" + + # Priority 3: Hotplug + PRIORITY3_TESTS="test_cpu_hotplug|test_memory_hotplug|test_disk_hotplug|test_net_hotplug" + + # Priority 4: Snapshot & Live Migration + PRIORITY4_TESTS="test_snapshot_restore_basic|test_live_migration_basic" + + # Priority 5: VMM Instance API (lib mode) + PRIORITY5_TESTS="test_api_create_boot_and_shutdown|test_api_snapshot_restore" + + # Step 1: Priority 1 - Boot & Lifecycle (parallel) + echo "" + echo ">>> [Step 1/5] Priority 1: Boot & Lifecycle" + build_test_filters "common_parallel" "$PRIORITY1_TESTS" + time cargo test $features -- --exact --test-threads=1 ${test_binary_args[*]} ${test_filters[*]} + RES=$? + + # Step 2: Priority 2 - Core I/O Devices (parallel) + if [ $RES -eq 0 ]; then + echo "" + echo ">>> [Step 2/5] Priority 2: Core I/O Devices" + build_test_filters "common_parallel" "$PRIORITY2_TESTS" + time cargo test $features -- --exact --test-threads=1 ${test_binary_args[*]} ${test_filters[*]} + RES=$? + fi + + # Step 3: Priority 3 - Hotplug (parallel) + if [ $RES -eq 0 ]; then + echo "" + echo ">>> [Step 3/5] Priority 3: Hotplug" + build_test_filters "common_parallel" "$PRIORITY3_TESTS" + time cargo test $features -- --exact --test-threads=1 ${test_binary_args[*]} ${test_filters[*]} + RES=$? + fi + + # Step 4: Priority 4 - Snapshot & Live Migration (parallel + sequential) + if [ $RES -eq 0 ]; then + echo "" + echo ">>> [Step 4/5] Priority 4: Snapshot & Live Migration" + time cargo test $features -- --exact --test-threads=1 ${test_binary_args[*]} "common_sequential::test_snapshot_restore_basic" + RES=$? + fi + if [ $RES -eq 0 ]; then + time cargo test $features -- --exact --test-threads=1 ${test_binary_args[*]} "live_migration::test_live_migration_basic" + RES=$? + fi + + # Step 5: Priority 5 - VMM Instance API (lib mode, sequential) + if [ $RES -eq 0 ]; then + echo "" + echo ">>> [Step 5/5] Priority 5: VMM Instance API (lib mode)" + build_test_filters "vmm_instance" "$PRIORITY5_TESTS" + time cargo test $features --features lib_support -- --test-threads=1 ${test_binary_args[*]} ${test_filters[*]} + RES=$? + fi + + if [ $RES -eq 0 ]; then + echo "" + echo "=== Quick mode: all core smoke tests PASSED ===" + else + echo "" + echo "=== Quick mode: some tests FAILED ===" + fi + + exit $RES +fi + +# Full mode: run all tests +echo "=== Full mode: running all tests ===" + +build_test_filters "common_parallel" "$test_filter" +time cargo test $features -- --test-threads=$(($(nproc)/2)) ${test_binary_args[*]} ${test_filters[*]} +RES=$? + +if [ $RES -eq 0 ]; then + build_test_filters "vmm_instance" "$test_filter" + time cargo test $features --features lib_support -- --test-threads=1 ${test_binary_args[*]} ${test_filters[*]} + RES=$? +fi + +# Run some tests in sequence since the result could be affected by other tests +# running in parallel. +if [ $RES -eq 0 ]; then + build_test_filters "common_sequential" "$test_filter" + time cargo test $features -- --test-threads=1 ${test_binary_args[*]} ${test_filters[*]} + RES=$? +fi + +if [ $RES -eq 0 ]; then + build_test_filters "compatibility" "$test_filter" + time cargo test $features -- --test-threads=1 ${test_binary_args[*]} ${test_filters[*]} + RES=$? +fi + +exit $RES diff --git a/vendor/cube-hypervisor/scripts/run_metrics.sh b/vendor/cube-hypervisor/scripts/run_metrics.sh new file mode 100755 index 0000000..1521270 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_metrics.sh @@ -0,0 +1,123 @@ +#!/bin/bash +set -x + +source $HOME/.cargo/env +source $(dirname "$0")/test-util.sh + +export TEST_ARCH=$(uname -m) +export BUILD_TARGET=${BUILD_TARGET-${TEST_ARCH}-unknown-linux-gnu} + + +WORKLOADS_DIR="$HOME/workloads" +mkdir -p "$WORKLOADS_DIR" + +build_fio() { + FIO_DIR="$WORKLOADS_DIR/fio_build" + FIO_REPO="https://github.com/axboe/fio.git" + + checkout_repo "$FIO_DIR" "$FIO_REPO" master "1953e1adb5a28ed21370e85991d7f5c3cdc699f3" + if [ ! -f "$FIO_DIR/.built" ]; then + pushd $FIO_DIR + ./configure + make -j `nproc` + cp fio "$WORKLOADS_DIR/fio" + touch .built + popd + fi +} + +process_common_args "$@" + +# For now these values are default for kvm +features="" + +if [ "$hypervisor" = "mshv" ]; then + features="--no-default-features --features mshv" +fi + +cp scripts/sha1sums-${TEST_ARCH} $WORKLOADS_DIR + +if [ ${TEST_ARCH} == "aarch64" ]; then + FOCAL_OS_IMAGE_NAME="focal-server-cloudimg-arm64-custom-20210929-0.qcow2" +else + FOCAL_OS_IMAGE_NAME="focal-server-cloudimg-amd64-custom-20210609-0.qcow2" +fi + +FOCAL_OS_IMAGE_URL="https://cloud-hypervisor.azureedge.net/$FOCAL_OS_IMAGE_NAME" +FOCAL_OS_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_IMAGE_NAME" +if [ ! -f "$FOCAL_OS_IMAGE" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $FOCAL_OS_IMAGE_URL || exit 1 + popd +fi + +if [ ${TEST_ARCH} == "aarch64" ]; then + FOCAL_OS_RAW_IMAGE_NAME="focal-server-cloudimg-arm64-custom-20210929-0.raw" +else + FOCAL_OS_RAW_IMAGE_NAME="focal-server-cloudimg-amd64-custom-20210609-0.raw" +fi + +FOCAL_OS_RAW_IMAGE="$WORKLOADS_DIR/$FOCAL_OS_RAW_IMAGE_NAME" +if [ ! -f "$FOCAL_OS_RAW_IMAGE" ]; then + pushd $WORKLOADS_DIR + time qemu-img convert -p -f qcow2 -O raw $FOCAL_OS_IMAGE_NAME $FOCAL_OS_RAW_IMAGE_NAME || exit 1 + popd +fi + +pushd $WORKLOADS_DIR +grep focal sha1sums-${TEST_ARCH} | sha1sum --check +if [ $? -ne 0 ]; then + echo "sha1sum validation of images failed, remove invalid images to fix the issue." + exit 1 +fi +popd + +if [ ${TEST_ARCH} == "aarch64" ]; then + build_fio + + # Update the fio in the cloud image to use io_uring on AArch64 + FOCAL_OS_RAW_IMAGE_UPDATE_TOOL_NAME="focal-server-cloudimg-arm64-custom-20210929-0-update-tool.raw" + cp "$FOCAL_OS_RAW_IMAGE" "$WORKLOADS_DIR/$FOCAL_OS_RAW_IMAGE_UPDATE_TOOL_NAME" + FOCAL_OS_RAW_IMAGE_UPDATE_TOOL_ROOT_DIR="$WORKLOADS_DIR/focal-server-cloudimg-root" + if [ ! -d "$FOCAL_OS_RAW_IMAGE_UPDATE_TOOL_ROOT_DIR" ]; then + mkdir -p "$FOCAL_OS_RAW_IMAGE_UPDATE_TOOL_ROOT_DIR" + fi + # Mount the 'raw' image, replace the fio and umount the working folder + guestmount -a "$WORKLOADS_DIR/$FOCAL_OS_RAW_IMAGE_UPDATE_TOOL_NAME" -m /dev/sda1 "$FOCAL_OS_RAW_IMAGE_UPDATE_TOOL_ROOT_DIR" || exit 1 + cp "$WORKLOADS_DIR"/fio "$FOCAL_OS_RAW_IMAGE_UPDATE_TOOL_ROOT_DIR"/usr/bin/fio + guestunmount "$FOCAL_OS_RAW_IMAGE_UPDATE_TOOL_ROOT_DIR" +fi + +# Build custom kernel based on virtio-pmem and virtio-fs upstream patches +build_custom_linux + +BUILD_TARGET="${TEST_ARCH}-unknown-linux-${CH_LIBC}" +CFLAGS="" +TARGET_CC="" +if [[ "${BUILD_TARGET}" == "${TEST_ARCH}-unknown-linux-musl" ]]; then + TARGET_CC="musl-gcc" + CFLAGS="-I /usr/include/${TEST_ARCH}-linux-musl/ -idirafter /usr/include/" +fi + +cargo build --all --release $features --target $BUILD_TARGET +strip target/$BUILD_TARGET/release/cloud-hypervisor +strip target/$BUILD_TARGET/release/vhost_user_net +strip target/$BUILD_TARGET/release/ch-remote +strip target/$BUILD_TARGET/release/performance-metrics + +# setup hugepages +echo 6144 | sudo tee /proc/sys/vm/nr_hugepages +sudo chmod a+rwX /dev/hugepages + +if [ -n "$test_filter" ]; then + test_binary_args+=("--test-filter $test_filter") +fi + +# Ensure that git commands can be run in this directory (for metrics report) +git config --global --add safe.directory $PWD + +export RUST_BACKTRACE=1 +time target/$BUILD_TARGET/release/performance-metrics ${test_binary_args[*]} +RES=$? + +exit $RES diff --git a/vendor/cube-hypervisor/scripts/run_openapi_tests.sh b/vendor/cube-hypervisor/scripts/run_openapi_tests.sh new file mode 100755 index 0000000..04ca14d --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_openapi_tests.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -e +set -x + +sudo docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli validate -i /local/vmm/src/api/openapi/cloud-hypervisor.yaml diff --git a/vendor/cube-hypervisor/scripts/run_unit_tests.sh b/vendor/cube-hypervisor/scripts/run_unit_tests.sh new file mode 100755 index 0000000..5ad9206 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/run_unit_tests.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +source $HOME/.cargo/env +source $(dirname "$0")/test-util.sh + +process_common_args "$@" + +BUILD_TARGET=${BUILD_TARGET-x86_64-unknown-linux-gnu} +cargo_args=("") + +if [[ $hypervisor = "mshv" ]]; then + cargo_args+=("--no-default-features") + cargo_args+=("--features $hypervisor") +elif [[ $(uname -m) = "x86_64" ]]; then + cargo_args+=("--features tdx") +fi + +if [[ "${BUILD_TARGET}" == "aarch64-unknown-linux-musl" ]]; then + export TARGET_CC="musl-gcc" + export RUSTFLAGS="-C link-arg=-lgcc -C link_arg=-specs -C link_arg=/usr/lib/aarch64-linux-musl/musl-gcc.specs" +fi + +export RUST_BACKTRACE=1 +cargo test --lib --bins --target $BUILD_TARGET --workspace ${cargo_args[@]} || exit 1 diff --git a/vendor/cube-hypervisor/scripts/sha1sums-aarch64 b/vendor/cube-hypervisor/scripts/sha1sums-aarch64 new file mode 100644 index 0000000..a3c0af3 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/sha1sums-aarch64 @@ -0,0 +1,8 @@ +6fee67adbfed8db7a225be23ee9d90b5bd7f19e6 bionic-server-cloudimg-arm64.img +786fe1c33588334e92b35c65e414da068df180bc bionic-server-cloudimg-arm64.raw +6e66f9f4b01adc72c884c1c1111e60afadc9c871 bionic-server-cloudimg-arm64.qcow2 +e4addb6e212a298144f9eb0eb6e36019d013f0e7 alpine-minirootfs-aarch64.tar.gz +25b4f9ac308898d63b73d7db0e0e2d4768853723 focal-server-cloudimg-arm64-custom-20210929-0.qcow2 +9953b31bb1923cdd8d91b1b7cc9ad3a9be1e0a59 focal-server-cloudimg-arm64-custom-20210929-0.raw +7118f4d4cad18c8357bc2ad9824a50f9a82a860a jammy-server-cloudimg-arm64-custom-20220329-0.qcow2 +1f2b71be43b8f748f01306c4454e5c921343faa4 jammy-server-cloudimg-arm64-custom-20220329-0.raw diff --git a/vendor/cube-hypervisor/scripts/sha1sums-x86_64 b/vendor/cube-hypervisor/scripts/sha1sums-x86_64 new file mode 100644 index 0000000..38b1b12 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/sha1sums-x86_64 @@ -0,0 +1,7 @@ +27f3b17962ace69b51f0ddc2012095e3109e6ed8 bionic-server-cloudimg-amd64.qcow2 +8db9cc58b01452ce2d06c313177e6e74d8582d93 bionic-server-cloudimg-amd64.raw +d4a44acc6014d5f83dea1c625c43d677a95fa75f alpine-minirootfs-x86_64.tar.gz +f1eccdc5e1b515dbad294426ab081b47ebfb97c0 focal-server-cloudimg-amd64-custom-20210609-0.qcow2 +7f5a8358243a96adf61f5c20139b29f308f2c0e3 focal-server-cloudimg-amd64-custom-20210609-0.raw +272102ac243e544bc5c2303f383eeb217bd2e446 jammy-server-cloudimg-amd64-custom-20220329-0.qcow2 +0b5b4816a0976c458f8e8bab061e9d63ebc1b806 jammy-server-cloudimg-amd64-custom-20220329-0.raw \ No newline at end of file diff --git a/vendor/cube-hypervisor/scripts/test-util.sh b/vendor/cube-hypervisor/scripts/test-util.sh new file mode 100644 index 0000000..c1ca304 --- /dev/null +++ b/vendor/cube-hypervisor/scripts/test-util.sh @@ -0,0 +1,137 @@ +#!/bin/bash +hypervisor="kvm" +test_filter="" +quick_mode="false" + +# Checkout source code of a GIT repo with specified branch and commit +# Args: +# $1: Target directory +# $2: GIT URL of the repo +# $3: Required branch +# $4: Required commit (optional) +checkout_repo() { + SRC_DIR="$1" + GIT_URL="$2" + GIT_BRANCH="$3" + GIT_COMMIT="$4" + + # Check whether the local HEAD commit same as the requested commit or not. + # If commit is not specified, compare local HEAD and remote HEAD. + # Remove the folder if there is difference. + if [ -d "$SRC_DIR" ]; then + pushd $SRC_DIR + git fetch + SRC_LOCAL_COMMIT=$(git rev-parse HEAD) + if [ -z "$GIT_COMMIT" ]; then + GIT_COMMIT=$(git rev-parse remotes/origin/"$GIT_BRANCH") + fi + popd + if [ "$SRC_LOCAL_COMMIT" != "$GIT_COMMIT" ]; then + rm -rf "$SRC_DIR" + fi + fi + + # Checkout the specified branch and commit (if required) + if [ ! -d "$SRC_DIR" ]; then + git clone --depth 1 "$GIT_URL" -b "$GIT_BRANCH" "$SRC_DIR" + if [ "$GIT_COMMIT" ]; then + pushd "$SRC_DIR" + git fetch --depth 1 origin "$GIT_COMMIT" + git reset --hard FETCH_HEAD + popd + fi + fi +} + +build_custom_linux() { + ARCH=$(uname -m) + SRCDIR=$PWD + LINUX_CUSTOM_DIR="$WORKLOADS_DIR/linux-custom" + LINUX_CUSTOM_BRANCH="ch-5.15.12" + LINUX_CUSTOM_URL="https://github.com/cloud-hypervisor/linux.git" + + checkout_repo "$LINUX_CUSTOM_DIR" "$LINUX_CUSTOM_URL" "$LINUX_CUSTOM_BRANCH" + + cp $SRCDIR/resources/linux-config-${ARCH} $LINUX_CUSTOM_DIR/.config + + pushd $LINUX_CUSTOM_DIR + make -j `nproc` + if [ ${ARCH} == "x86_64" ]; then + cp vmlinux "$WORKLOADS_DIR/" || exit 1 + elif [ ${ARCH} == "aarch64" ]; then + cp arch/arm64/boot/Image "$WORKLOADS_DIR/" || exit 1 + cp arch/arm64/boot/Image.gz "$WORKLOADS_DIR/" || exit 1 + fi + popd +} + +cmd_help() { + echo "" + echo "Cloud Hypervisor $(basename $0)" + echo "Usage: $(basename $0) []" + echo "" + echo "Available arguments:" + echo "" + echo " --hypervisor Underlying hypervisor. Options kvm, mshv" + echo " --test-filter Tests to run" + echo " --quick Run only core smoke tests (5 priority levels)" + echo "" + echo " --help Display this help message." + echo "" +} + +process_common_args() { + while [ $# -gt 0 ]; do + case "$1" in + "-h"|"--help") { cmd_help; exit 1; } ;; + "--hypervisor") + shift + hypervisor="$1" + ;; + "--test-filter") + shift + test_filter="$1" + ;; + "--quick") + quick_mode="true" + ;; + "--") { + shift + break + } ;; + *) + echo "Unknown test scripts argument: $1. Please use '-- --help' for help." + exit + ;; + esac + shift + done + if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then + die "Hypervisor value must be kvm or mshv" + fi + + test_binary_args=($@) +} + +# Convert pipe-separated test names into an array of "module::test_name" filters. +# Usage: build_test_filters +# Result is stored in the global array: test_filters +# Example: +# build_test_filters "common_parallel" "test_a|test_b" +# => test_filters=("common_parallel::test_a" "common_parallel::test_b") +# +# If pipe_separated_tests is empty, test_filters will contain just the module prefix +# so that all tests under that module are matched. +build_test_filters() { + local module="$1" + local tests="$2" + test_filters=() + if [ -z "$tests" ]; then + test_filters=("$module") + else + IFS='|' read -ra names <<< "$tests" + for name in "${names[@]}"; do + test_filters+=("${module}::${name}") + done + fi +} \ No newline at end of file diff --git a/vendor/cube-hypervisor/scripts/windows-11-iot-enterprise-aarch64.raw.sha1 b/vendor/cube-hypervisor/scripts/windows-11-iot-enterprise-aarch64.raw.sha1 new file mode 100644 index 0000000..b7c23bb --- /dev/null +++ b/vendor/cube-hypervisor/scripts/windows-11-iot-enterprise-aarch64.raw.sha1 @@ -0,0 +1 @@ +707e939654b341717d3f54b41c57eea4432978e5 windows-11-iot-enterprise-aarch64.raw diff --git a/vendor/cube-hypervisor/serial_buffer/Cargo.toml b/vendor/cube-hypervisor/serial_buffer/Cargo.toml new file mode 100644 index 0000000..f5329eb --- /dev/null +++ b/vendor/cube-hypervisor/serial_buffer/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "serial_buffer" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" \ No newline at end of file diff --git a/vendor/cube-hypervisor/serial_buffer/src/lib.rs b/vendor/cube-hypervisor/serial_buffer/src/lib.rs new file mode 100644 index 0000000..f01f31a --- /dev/null +++ b/vendor/cube-hypervisor/serial_buffer/src/lib.rs @@ -0,0 +1,115 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use std::{ + collections::VecDeque, + io::Write, + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, + }, +}; + +const MAX_BUFFER_SIZE: usize = 1 << 20; + +// Circular buffer implementation for serial output. +// Read from head; push to tail +pub struct SerialBuffer { + buffer: VecDeque, + out: Box, + write_out: Arc, +} + +impl SerialBuffer { + pub fn new(out: Box, write_out: Arc) -> Self { + Self { + buffer: VecDeque::new(), + out, + write_out, + } + } + + fn fill_buffer(&mut self, buf: &[u8]) { + if buf.len() >= MAX_BUFFER_SIZE { + let offset = buf.len() - MAX_BUFFER_SIZE; + self.buffer = VecDeque::from(buf[offset..].to_vec()); + return; + } + + let num_allowed_bytes = MAX_BUFFER_SIZE - buf.len(); + if self.buffer.len() > num_allowed_bytes { + let num_bytes_to_remove = self.buffer.len() - num_allowed_bytes; + self.buffer.drain(..num_bytes_to_remove); + } + + self.buffer.extend(buf); + } +} + +impl Write for SerialBuffer { + fn write(&mut self, buf: &[u8]) -> Result { + // Simply fill the buffer if we're not allowed to write to the out + // device. + if !self.write_out.load(Ordering::Acquire) { + self.fill_buffer(buf); + return Ok(buf.len()); + } + + // In case we're allowed to write to the out device, we flush the + // content of the buffer. + self.flush()?; + + // If after flushing the buffer, it's still not empty, that means + // only a subset of the bytes was written and we should fill the buffer + // with what's coming from the serial. + if !self.buffer.is_empty() { + self.fill_buffer(buf); + return Ok(buf.len()); + } + + // We reach this point if we're allowed to write to the out device + // and we know there's nothing left in the buffer. + let mut offset = 0; + loop { + match self.out.write(&buf[offset..]) { + Ok(written_bytes) => { + if written_bytes < buf.len() - offset { + offset += written_bytes; + continue; + } + } + Err(e) => { + if !matches!(e.kind(), std::io::ErrorKind::WouldBlock) { + return Err(e); + } + self.fill_buffer(&buf[offset..]); + } + } + break; + } + + // Make sure we flush anything that might have been written to the + // out device. + self.out.flush()?; + + Ok(buf.len()) + } + + // This function flushes the content of the buffer to the out device if + // it is allowed to, otherwise this is a no-op. + fn flush(&mut self) -> Result<(), std::io::Error> { + if !self.write_out.load(Ordering::Acquire) { + return Ok(()); + } + + while let Some(byte) = self.buffer.pop_front() { + if self.out.write_all(&[byte]).is_err() { + self.buffer.push_front(byte); + break; + } + } + self.out.flush() + } +} diff --git a/vendor/cube-hypervisor/src/bin/ch-remote.rs b/vendor/cube-hypervisor/src/bin/ch-remote.rs new file mode 100644 index 0000000..7744256 --- /dev/null +++ b/vendor/cube-hypervisor/src/bin/ch-remote.rs @@ -0,0 +1,787 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#[macro_use(crate_authors)] +extern crate clap; + +use api_client::simple_api_command; +use api_client::simple_api_command_with_fds; +use api_client::Error as ApiClientError; +use clap::{Arg, ArgAction, ArgMatches, Command}; +use option_parser::{ByteSized, ByteSizedParseError}; +use std::fmt; +use std::io::Read; +use std::os::unix::net::UnixStream; +use std::process; +use vmm::config::RestoreConfig; +use vmm::vm_config::{ + DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, UserDeviceConfig, VdpaConfig, + VsockConfig, +}; +use vmm::{SnapshotConfig, SnapshotType}; + +const MEMORY_VOL_URL_HELP: &str = + "Optional existing memory blob path for storing memory range data on a separate volume \ + (for example /dev/vdb or file:///dev/vdb)"; + +#[derive(Debug)] +enum Error { + Connect(std::io::Error), + ApiClient(ApiClientError), + InvalidCpuCount(std::num::ParseIntError), + InvalidMemorySize(ByteSizedParseError), + InvalidBalloonSize(ByteSizedParseError), + AddDeviceConfig(vmm::config::Error), + AddDiskConfig(vmm::config::Error), + AddFsConfig(vmm::config::Error), + AddPmemConfig(vmm::config::Error), + AddNetConfig(vmm::config::Error), + AddUserDeviceConfig(vmm::config::Error), + AddVdpaConfig(vmm::config::Error), + AddVsockConfig(vmm::config::Error), + Restore(vmm::config::Error), + ReadingStdin(std::io::Error), + ReadingFile(std::io::Error), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use Error::*; + match self { + ApiClient(e) => e.fmt(f), + Connect(e) => write!(f, "Error opening HTTP socket: {}", e), + InvalidCpuCount(e) => write!(f, "Error parsing CPU count: {}", e), + InvalidMemorySize(e) => write!(f, "Error parsing memory size: {:?}", e), + InvalidBalloonSize(e) => write!(f, "Error parsing balloon size: {:?}", e), + AddDeviceConfig(e) => write!(f, "Error parsing device syntax: {}", e), + AddDiskConfig(e) => write!(f, "Error parsing disk syntax: {}", e), + AddFsConfig(e) => write!(f, "Error parsing filesystem syntax: {}", e), + AddPmemConfig(e) => write!(f, "Error parsing persistent memory syntax: {}", e), + AddNetConfig(e) => write!(f, "Error parsing network syntax: {}", e), + AddUserDeviceConfig(e) => write!(f, "Error parsing user device syntax: {}", e), + AddVdpaConfig(e) => write!(f, "Error parsing vDPA device syntax: {}", e), + AddVsockConfig(e) => write!(f, "Error parsing vsock syntax: {}", e), + Restore(e) => write!(f, "Error parsing restore syntax: {}", e), + ReadingStdin(e) => write!(f, "Error reading from stdin: {}", e), + ReadingFile(e) => write!(f, "Error reading from file: {}", e), + } + } +} + +fn resize_api_command( + socket: &mut UnixStream, + cpus: Option<&str>, + memory: Option<&str>, + balloon: Option<&str>, +) -> Result<(), Error> { + let desired_vcpus: Option = if let Some(cpus) = cpus { + Some(cpus.parse().map_err(Error::InvalidCpuCount)?) + } else { + None + }; + + let desired_ram: Option = if let Some(memory) = memory { + Some( + memory + .parse::() + .map_err(Error::InvalidMemorySize)? + .0, + ) + } else { + None + }; + + let desired_balloon: Option = if let Some(balloon) = balloon { + Some( + balloon + .parse::() + .map_err(Error::InvalidBalloonSize)? + .0, + ) + } else { + None + }; + + let resize = vmm::api::VmResizeData { + desired_vcpus, + desired_ram, + desired_balloon, + }; + + simple_api_command( + socket, + "PUT", + "resize", + Some(&serde_json::to_string(&resize).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn resize_zone_api_command(socket: &mut UnixStream, id: &str, size: &str) -> Result<(), Error> { + let resize_zone = vmm::api::VmResizeZoneData { + id: id.to_owned(), + desired_ram: size + .parse::() + .map_err(Error::InvalidMemorySize)? + .0, + }; + + simple_api_command( + socket, + "PUT", + "resize-zone", + Some(&serde_json::to_string(&resize_zone).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn add_device_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let device_config = DeviceConfig::parse(config).map_err(Error::AddDeviceConfig)?; + + simple_api_command( + socket, + "PUT", + "add-device", + Some(&serde_json::to_string(&device_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn add_user_device_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let device_config = UserDeviceConfig::parse(config).map_err(Error::AddUserDeviceConfig)?; + + simple_api_command( + socket, + "PUT", + "add-user-device", + Some(&serde_json::to_string(&device_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn remove_device_api_command(socket: &mut UnixStream, id: &str) -> Result<(), Error> { + let remove_device_data = vmm::api::VmRemoveDeviceData { id: id.to_owned() }; + + simple_api_command( + socket, + "PUT", + "remove-device", + Some(&serde_json::to_string(&remove_device_data).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn add_disk_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let disk_config = DiskConfig::parse(config).map_err(Error::AddDiskConfig)?; + + simple_api_command( + socket, + "PUT", + "add-disk", + Some(&serde_json::to_string(&disk_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn add_fs_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let fs_config = FsConfig::parse(config).map_err(Error::AddFsConfig)?; + + simple_api_command( + socket, + "PUT", + "add-fs", + Some(&serde_json::to_string(&fs_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn add_pmem_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let pmem_config = PmemConfig::parse(config).map_err(Error::AddPmemConfig)?; + + simple_api_command( + socket, + "PUT", + "add-pmem", + Some(&serde_json::to_string(&pmem_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn add_net_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let mut net_config = NetConfig::parse(config).map_err(Error::AddNetConfig)?; + + // NetConfig is modified on purpose here by taking the list of file + // descriptors out. Keeping the list and send it to the server side + // process would not make any sense since the file descriptor may be + // represented with different values. + let fds = net_config.fds.take().unwrap_or_default(); + + simple_api_command_with_fds( + socket, + "PUT", + "add-net", + Some(&serde_json::to_string(&net_config).unwrap()), + fds, + ) + .map_err(Error::ApiClient) +} + +fn add_vdpa_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let vdpa_config = VdpaConfig::parse(config).map_err(Error::AddVdpaConfig)?; + + simple_api_command( + socket, + "PUT", + "add-vdpa", + Some(&serde_json::to_string(&vdpa_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn add_vsock_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let vsock_config = VsockConfig::parse(config).map_err(Error::AddVsockConfig)?; + + simple_api_command( + socket, + "PUT", + "add-vsock", + Some(&serde_json::to_string(&vsock_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn snapshot_api_command( + socket: &mut UnixStream, + url: &str, + snapshot_type: SnapshotType, + memory_vol_url: Option, +) -> Result<(), Error> { + snapshot_command(socket, "snapshot", url, snapshot_type, memory_vol_url) +} + +fn snapshot_command( + socket: &mut UnixStream, + endpoint: &str, + url: &str, + snapshot_type: SnapshotType, + memory_vol_url: Option, +) -> Result<(), Error> { + let snapshot_config = SnapshotConfig { + destination_url: String::from(url), + snapshot_type, + memory_vol_url, + }; + + simple_api_command( + socket, + "PUT", + endpoint, + Some(&serde_json::to_string(&snapshot_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn parse_snapshot_command_args(sub_matches: &ArgMatches) -> (&str, SnapshotType, Option) { + let snapshot_type = sub_matches + .get_one::("snapshot_type") + .map(|s| s.parse::().unwrap_or_default()) + .unwrap_or_default(); + let memory_vol_url = sub_matches.get_one::("memory_vol_url").cloned(); + + ( + sub_matches.get_one::("snapshot_config").unwrap(), + snapshot_type, + memory_vol_url, + ) +} + +fn restore_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let restore_config = RestoreConfig::parse(config).map_err(Error::Restore)?; + + simple_api_command( + socket, + "PUT", + "restore", + Some(&serde_json::to_string(&restore_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn pause2snapshot_api_command( + socket: &mut UnixStream, + url: &str, + snapshot_type: SnapshotType, + memory_vol_url: Option, +) -> Result<(), Error> { + snapshot_command(socket, "pause2snapshot", url, snapshot_type, memory_vol_url) +} + +fn resume_from_snap_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let restore_config = RestoreConfig::parse(config).map_err(Error::Restore)?; + + simple_api_command( + socket, + "PUT", + "resume-from-snapshot", + Some(&serde_json::to_string(&restore_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn coredump_api_command(socket: &mut UnixStream, destination_url: &str) -> Result<(), Error> { + let coredump_config = vmm::api::VmCoredumpData { + destination_url: String::from(destination_url), + }; + + simple_api_command( + socket, + "PUT", + "coredump", + Some(&serde_json::to_string(&coredump_config).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn receive_migration_api_command(socket: &mut UnixStream, url: &str) -> Result<(), Error> { + let receive_migration_data = vmm::api::VmReceiveMigrationData { + receiver_url: url.to_owned(), + }; + simple_api_command( + socket, + "PUT", + "receive-migration", + Some(&serde_json::to_string(&receive_migration_data).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn send_migration_api_command( + socket: &mut UnixStream, + url: &str, + local: bool, +) -> Result<(), Error> { + let send_migration_data = vmm::api::VmSendMigrationData { + destination_url: url.to_owned(), + local, + }; + simple_api_command( + socket, + "PUT", + "send-migration", + Some(&serde_json::to_string(&send_migration_data).unwrap()), + ) + .map_err(Error::ApiClient) +} + +fn create_api_command(socket: &mut UnixStream, path: &str) -> Result<(), Error> { + let mut data = String::default(); + if path == "-" { + std::io::stdin() + .read_to_string(&mut data) + .map_err(Error::ReadingStdin)?; + } else { + data = std::fs::read_to_string(path).map_err(Error::ReadingFile)?; + } + + simple_api_command(socket, "PUT", "create", Some(&data)).map_err(Error::ApiClient) +} + +fn do_command(matches: &ArgMatches) -> Result<(), Error> { + let mut socket = UnixStream::connect(matches.get_one::("api-socket").unwrap()) + .map_err(Error::Connect)?; + + match matches.subcommand_name() { + Some("info") => { + simple_api_command(&mut socket, "GET", "info", None).map_err(Error::ApiClient) + } + Some("counters") => { + simple_api_command(&mut socket, "GET", "counters", None).map_err(Error::ApiClient) + } + Some("resize") => resize_api_command( + &mut socket, + matches + .subcommand_matches("resize") + .unwrap() + .get_one::("cpus") + .map(|x| x as &str), + matches + .subcommand_matches("resize") + .unwrap() + .get_one::("memory") + .map(|x| x as &str), + matches + .subcommand_matches("resize") + .unwrap() + .get_one::("balloon") + .map(|x| x as &str), + ), + Some("resize-zone") => resize_zone_api_command( + &mut socket, + matches + .subcommand_matches("resize-zone") + .unwrap() + .get_one::("id") + .unwrap(), + matches + .subcommand_matches("resize-zone") + .unwrap() + .get_one::("size") + .unwrap(), + ), + Some("add-device") => add_device_api_command( + &mut socket, + matches + .subcommand_matches("add-device") + .unwrap() + .get_one::("device_config") + .unwrap(), + ), + Some("remove-device") => remove_device_api_command( + &mut socket, + matches + .subcommand_matches("remove-device") + .unwrap() + .get_one::("id") + .unwrap(), + ), + Some("add-disk") => add_disk_api_command( + &mut socket, + matches + .subcommand_matches("add-disk") + .unwrap() + .get_one::("disk_config") + .unwrap(), + ), + Some("add-fs") => add_fs_api_command( + &mut socket, + matches + .subcommand_matches("add-fs") + .unwrap() + .get_one::("fs_config") + .unwrap(), + ), + Some("add-pmem") => add_pmem_api_command( + &mut socket, + matches + .subcommand_matches("add-pmem") + .unwrap() + .get_one::("pmem_config") + .unwrap(), + ), + Some("add-net") => add_net_api_command( + &mut socket, + matches + .subcommand_matches("add-net") + .unwrap() + .get_one::("net_config") + .unwrap(), + ), + Some("add-user-device") => add_user_device_api_command( + &mut socket, + matches + .subcommand_matches("add-user-device") + .unwrap() + .get_one::("device_config") + .unwrap(), + ), + Some("add-vdpa") => add_vdpa_api_command( + &mut socket, + matches + .subcommand_matches("add-vdpa") + .unwrap() + .get_one::("vdpa_config") + .unwrap(), + ), + Some("add-vsock") => add_vsock_api_command( + &mut socket, + matches + .subcommand_matches("add-vsock") + .unwrap() + .get_one::("vsock_config") + .unwrap(), + ), + Some("snapshot") => { + let sub_matches = matches.subcommand_matches("snapshot").unwrap(); + let (snapshot_config, snapshot_type, memory_vol_url) = + parse_snapshot_command_args(sub_matches); + snapshot_api_command(&mut socket, snapshot_config, snapshot_type, memory_vol_url) + } + Some("restore") => restore_api_command( + &mut socket, + matches + .subcommand_matches("restore") + .unwrap() + .get_one::("restore_config") + .unwrap(), + ), + Some("pause2snapshot") => { + let sub_matches = matches.subcommand_matches("pause2snapshot").unwrap(); + let (snapshot_config, snapshot_type, memory_vol_url) = + parse_snapshot_command_args(sub_matches); + pause2snapshot_api_command(&mut socket, snapshot_config, snapshot_type, memory_vol_url) + } + Some("resume-from-snapshot") => resume_from_snap_api_command( + &mut socket, + matches + .subcommand_matches("resume-from-snapshot") + .unwrap() + .get_one::("restore_config") + .unwrap(), + ), + Some("coredump") => coredump_api_command( + &mut socket, + matches + .subcommand_matches("coredump") + .unwrap() + .get_one::("coredump_config") + .unwrap(), + ), + Some("send-migration") => send_migration_api_command( + &mut socket, + matches + .subcommand_matches("send-migration") + .unwrap() + .get_one::("send_migration_config") + .unwrap(), + matches + .subcommand_matches("send-migration") + .unwrap() + .get_flag("send_migration_local"), + ), + Some("receive-migration") => receive_migration_api_command( + &mut socket, + matches + .subcommand_matches("receive-migration") + .unwrap() + .get_one::("receive_migration_config") + .unwrap(), + ), + Some("create") => create_api_command( + &mut socket, + matches + .subcommand_matches("create") + .unwrap() + .get_one::("path") + .unwrap(), + ), + Some(c) => simple_api_command(&mut socket, "PUT", c, None).map_err(Error::ApiClient), + None => unreachable!(), + } +} + +fn main() { + let app = Command::new("ch-remote") + .author(crate_authors!()) + .subcommand_required(true) + .about("Remotely control a cloud-hypervisor VMM.") + .arg( + Arg::new("api-socket") + .long("api-socket") + .help("HTTP API socket path (UNIX domain socket).") + .num_args(1) + .required(true), + ) + .subcommand( + Command::new("add-device").about("Add VFIO device").arg( + Arg::new("device_config") + .index(1) + .help(DeviceConfig::SYNTAX), + ), + ) + .subcommand( + Command::new("add-disk") + .about("Add block device") + .arg(Arg::new("disk_config").index(1).help(DiskConfig::SYNTAX)), + ) + .subcommand( + Command::new("add-fs") + .about("Add virtio-fs backed fs device") + .arg(Arg::new("fs_config").index(1).help(FsConfig::SYNTAX)), + ) + .subcommand( + Command::new("add-pmem") + .about("Add persistent memory device") + .arg(Arg::new("pmem_config").index(1).help(PmemConfig::SYNTAX)), + ) + .subcommand( + Command::new("add-net") + .about("Add network device") + .arg(Arg::new("net_config").index(1).help(NetConfig::SYNTAX)), + ) + .subcommand( + Command::new("add-user-device") + .about("Add userspace device") + .arg( + Arg::new("device_config") + .index(1) + .help(UserDeviceConfig::SYNTAX), + ), + ) + .subcommand( + Command::new("add-vdpa") + .about("Add vDPA device") + .arg(Arg::new("vdpa_config").index(1).help(VdpaConfig::SYNTAX)), + ) + .subcommand( + Command::new("add-vsock") + .about("Add vsock device") + .arg(Arg::new("vsock_config").index(1).help(VsockConfig::SYNTAX)), + ) + .subcommand( + Command::new("remove-device") + .about("Remove VFIO device") + .arg(Arg::new("id").index(1).help("")), + ) + .subcommand(Command::new("info").about("Info on the VM")) + .subcommand(Command::new("counters").about("Counters from the VM")) + .subcommand(Command::new("pause").about("Pause the VM")) + .subcommand(Command::new("reboot").about("Reboot the VM")) + .subcommand(Command::new("power-button").about("Trigger a power button in the VM")) + .subcommand( + Command::new("resize") + .about("Resize the VM") + .arg( + Arg::new("cpus") + .long("cpus") + .help("New vCPUs count") + .num_args(1), + ) + .arg( + Arg::new("memory") + .long("memory") + .help("New memory size in bytes (supports K/M/G suffix)") + .num_args(1), + ) + .arg( + Arg::new("balloon") + .long("balloon") + .help("New balloon size in bytes (supports K/M/G suffix)") + .num_args(1), + ), + ) + .subcommand( + Command::new("resize-zone") + .about("Resize a memory zone") + .arg( + Arg::new("id") + .long("id") + .help("Memory zone identifier") + .num_args(1), + ) + .arg( + Arg::new("size") + .long("size") + .help("New memory zone size in bytes (supports K/M/G suffix)") + .num_args(1), + ), + ) + .subcommand(Command::new("resume").about("Resume the VM")) + .subcommand(Command::new("boot").about("Boot a created VM")) + .subcommand(Command::new("delete").about("Delete a VM")) + .subcommand(Command::new("shutdown").about("Shutdown the VM")) + .subcommand( + Command::new("snapshot") + .about("Create a snapshot from VM") + .arg( + Arg::new("snapshot_config") + .index(1) + .help(""), + ) + .arg( + Arg::new("snapshot_type") + .long("snapshot-type") + .help("Snapshot type: 'full', 'incremental' (saves only CoW anonymous pages) or 'soft-dirty' (true delta of pages written since the previous soft-dirty snapshot; falls back to 'incremental' on kernels without CONFIG_MEM_SOFT_DIRTY)") + .num_args(1) + .default_value("full"), + ) + .arg( + Arg::new("memory_vol_url") + .long("memory-vol-url") + .help(MEMORY_VOL_URL_HELP) + .num_args(1), + ), + ) + .subcommand( + Command::new("restore") + .about("Restore VM from a snapshot") + .arg( + Arg::new("restore_config") + .index(1) + .help(RestoreConfig::SYNTAX), + ), + ) + .subcommand( + Command::new("pause2snapshot") + .about("Pause and create a snapshot from VM") + .arg( + Arg::new("snapshot_config") + .index(1) + .help(""), + ) + .arg( + Arg::new("snapshot_type") + .long("snapshot-type") + .help("Snapshot type: 'full', 'incremental' (saves only CoW anonymous pages) or 'soft-dirty' (true delta of pages written since the previous soft-dirty snapshot; falls back to 'incremental' on kernels without CONFIG_MEM_SOFT_DIRTY)") + .num_args(1) + .default_value("full"), + ) + .arg( + Arg::new("memory_vol_url") + .long("memory-vol-url") + .help(MEMORY_VOL_URL_HELP) + .num_args(1), + ), + ) + .subcommand( + Command::new("resume-from-snapshot") + .about("Resume VM from a snapshot") + .arg( + Arg::new("restore_config") + .index(1) + .help(RestoreConfig::SYNTAX), + ), + ) + .subcommand( + Command::new("coredump") + .about("Create a coredump from VM") + .arg(Arg::new("coredump_config").index(1).help("")), + ) + .subcommand( + Command::new("send-migration") + .about("Initiate a VM migration") + .arg( + Arg::new("send_migration_config") + .index(1) + .help(""), + ) + .arg( + Arg::new("send_migration_local") + .long("local") + .num_args(0) + .action(ArgAction::SetTrue), + ), + ) + .subcommand( + Command::new("receive-migration") + .about("Receive a VM migration") + .arg( + Arg::new("receive_migration_config") + .index(1) + .help(""), + ), + ) + .subcommand( + Command::new("create") + .about("Create VM from a JSON configuration") + .arg(Arg::new("path").index(1).default_value("-")), + ); + + let matches = app.get_matches(); + + if let Err(e) = do_command(&matches) { + eprintln!("Error running command: {}", e); + process::exit(1) + }; +} diff --git a/vendor/cube-hypervisor/src/bin/cube-cpuid.rs b/vendor/cube-hypervisor/src/bin/cube-cpuid.rs new file mode 100644 index 0000000..f370139 --- /dev/null +++ b/vendor/cube-hypervisor/src/bin/cube-cpuid.rs @@ -0,0 +1,248 @@ +// Copyright © 2023 Tencent Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use clap::{Arg, ArgAction, ArgMatches, Command}; +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +use hypervisor::kvm::kvm_bindings; +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +use hypervisor::kvm::kvm_ioctls::Kvm; +use serde::de::Error as SerdeError; +use serde::{Deserialize, Deserializer, Serializer}; +use std::fs::File; +use std::io::Write; +use std::path::Path; +use std::process; + +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +pub use {kvm_bindings::kvm_cpuid_entry2, kvm_bindings::KVM_CPUID_FLAG_SIGNIFCANT_INDEX}; + +pub const CPUID_FLAG_VALID_INDEX: u32 = 1; + +#[derive(Debug)] +enum Error { + // Connect(std::io::Error), + // ReadingStdin(std::io::Error), + // ReadingFile(std::io::Error), +} + +/// Serializes number to hex +pub fn serialize_to_hex_str(number: &N, serializer: S) -> Result +where + S: Serializer, + N: std::fmt::LowerHex, +{ + serializer.serialize_str(format!("{:#x}", number).as_str()) +} + +macro_rules! deserialize_from_str { + ($name:ident, $type:tt) => { + /// Deserializes number from string. + /// Number can be in binary, hex or dec formats. + pub fn $name<'de, D>(deserializer: D) -> Result<$type, D::Error> + where + D: Deserializer<'de>, + { + let number_str = String::deserialize(deserializer)?; + let deserialized_number = if let Some(s) = number_str.strip_prefix("0b") { + $type::from_str_radix(s, 2) + } else if let Some(s) = number_str.strip_prefix("0x") { + $type::from_str_radix(s, 16) + } else { + return Err(D::Error::custom(format!( + "No supported number system prefix found in value [{}]. Make sure to prefix \ + the number with '0x' for hexadecimal numbers or '0b' for binary numbers.", + number_str, + ))); + } + .map_err(|err| { + D::Error::custom(format!( + "Failed to parse string [{}] as a number for CPU template - {:?}", + number_str, err + )) + })?; + Ok(deserialized_number) + } + }; +} + +deserialize_from_str!(deserialize_from_str_u32, u32); +deserialize_from_str!(deserialize_from_str_u64, u64); + +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)] +pub struct CpuIdEntry { + #[serde( + deserialize_with = "deserialize_from_str_u32", + serialize_with = "serialize_to_hex_str" + )] + pub function: u32, + #[serde( + deserialize_with = "deserialize_from_str_u32", + serialize_with = "serialize_to_hex_str" + )] + pub index: u32, + pub flags: u32, + #[serde( + deserialize_with = "deserialize_from_str_u32", + serialize_with = "serialize_to_hex_str" + )] + pub eax: u32, + #[serde( + deserialize_with = "deserialize_from_str_u32", + serialize_with = "serialize_to_hex_str" + )] + pub ebx: u32, + #[serde( + deserialize_with = "deserialize_from_str_u32", + serialize_with = "serialize_to_hex_str" + )] + pub ecx: u32, + #[serde( + deserialize_with = "deserialize_from_str_u32", + serialize_with = "serialize_to_hex_str" + )] + pub edx: u32, +} + +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +impl From for kvm_cpuid_entry2 { + fn from(e: CpuIdEntry) -> Self { + let flags = if e.flags & CPUID_FLAG_VALID_INDEX != 0 { + KVM_CPUID_FLAG_SIGNIFCANT_INDEX + } else { + 0 + }; + Self { + function: e.function, + index: e.index, + flags, + eax: e.eax, + ebx: e.ebx, + ecx: e.ecx, + edx: e.edx, + ..Default::default() + } + } +} + +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +impl From for CpuIdEntry { + fn from(e: kvm_cpuid_entry2) -> Self { + let flags = if e.flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX != 0 { + CPUID_FLAG_VALID_INDEX + } else { + 0 + }; + Self { + function: e.function, + index: e.index, + flags, + eax: e.eax, + ebx: e.ebx, + ecx: e.ecx, + edx: e.edx, + } + } +} + +pub fn intersect_cpuid(cpuid: &mut [CpuIdEntry], patches: Vec) { + for entry in cpuid { + for patch in patches.iter() { + if entry.function == patch.function && entry.index == patch.index { + entry.eax &= patch.eax; + entry.ebx &= patch.ebx; + entry.ecx &= patch.ecx; + entry.edx &= patch.edx; + } + } + } +} + +fn get_cpuid_from_file(file: &str) -> Vec { + serde_json::from_str(&std::fs::read_to_string(Path::new(file)).unwrap()).unwrap() +} + +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +fn get_local_host_cpuid() -> Vec { + let kvm = Kvm::new().unwrap(); + let kvm_cpuid = kvm + .get_supported_cpuid(kvm_bindings::KVM_MAX_CPUID_ENTRIES) + .unwrap(); + + let v = kvm_cpuid.as_slice().iter().map(|e| (*e).into()).collect(); + + let mut f = File::create(Path::new("cpuid.json")).unwrap(); + f.write_all(serde_json::to_string_pretty(&v).unwrap().as_bytes()) + .unwrap(); + + v +} + +fn intersect_json_files(files: Vec<&String>) { + let mut compatible_cpuid = get_cpuid_from_file(files[0]); + + for f in files.iter() { + let cpuid = get_cpuid_from_file(f); + + intersect_cpuid(&mut compatible_cpuid, cpuid); + } + + println!("{:x?}", compatible_cpuid); + let mut compatible_file = File::create(Path::new("compatible.json")).unwrap(); + compatible_file + .write_all( + serde_json::to_string_pretty(&compatible_cpuid) + .unwrap() + .as_bytes(), + ) + .unwrap(); +} + +fn do_command(matches: &ArgMatches) -> Result<(), Error> { + match matches.subcommand_name() { + Some("get") => { + println!("Get cpuid from host machine to cpuid.json"); + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + let _ = get_local_host_cpuid(); + } + Some("intersect") => { + let files: Vec<_> = matches + .subcommand_matches("intersect") + .unwrap() + .get_many::("file") + .unwrap() + .collect(); + intersect_json_files(files); + } + None => unreachable!(), + Some(&_) => todo!(), + } + + Ok(()) +} + +fn main() { + let app = Command::new("cube-cpuid") + .subcommand_required(true) + .about("Get or Intersect of cpuid") + .subcommand(Command::new("get").about("Get local machine cpuid")) + .subcommand( + Command::new("intersect") + .about("intersect cpuid json files") + .arg( + Arg::new("file") + .action(ArgAction::Append) + .num_args(2..) + .short('F') + .help(""), + ), + ); + + let matches = app.get_matches(); + + if do_command(&matches).is_err() { + println!("Error running command"); + process::exit(1) + }; +} diff --git a/vendor/cube-hypervisor/src/common.rs b/vendor/cube-hypervisor/src/common.rs new file mode 100644 index 0000000..84d6563 --- /dev/null +++ b/vendor/cube-hypervisor/src/common.rs @@ -0,0 +1,257 @@ +// Copyright © 2024 Tencent Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use std::io::Write; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex}; + +use arc_swap::ArcSwap; +use lazy_static::lazy_static; +use logging::LOG_CTRL_REOPEN; +use slog::slog_info; + +pub const DEFAULT_LOG_FILE: &str = "/data/log/CubeVmm/vmm.log"; +#[allow(dead_code)] +pub const DEFAULT_LOG_JSON_FILE: &str = "/data/log/CubeVmm/vmm.json"; + +#[allow(dead_code)] +pub fn default_coredump_filter() -> String { + "0x33".to_string() +} + +#[allow(dead_code)] +pub fn default_coredump_limit() -> u64 { + 2 * 1024 * 1024 * 1024 +} + +pub const DEFAULT_LOGGER_BUFFER_SIZE: usize = 100; + +lazy_static! { + static ref LOG_GUARD: ArcSwap> = + ArcSwap::from(Arc::new(None)); +} + +pub struct Logger { + output: Mutex>>, + start: std::time::Instant, + sandbox_id: String, + log_async: bool, + defer_logger_thread: bool, + buffer: Arc>>, + logger_thread_started: Arc>, + log_file_name: String, + vcpu_started: Arc, + need_flush: Arc, + #[cfg(feature = "logger_debug")] + logger_dbg_file: Arc>, +} + +impl Logger { + #[allow(clippy::too_many_arguments)] + pub fn new( + output: Mutex>>, + start: std::time::Instant, + sandbox_id: String, + log_async: bool, + defer_logger_thread: bool, + buffer: Arc>>, + logger_thread_started: Arc>, + log_file_name: String, + vcpu_started: Arc, + need_flush: Arc, + #[cfg(feature = "logger_debug")] logger_dbg_file: Arc>, + ) -> Self { + Self { + output, + start, + sandbox_id, + log_async, + defer_logger_thread, + buffer, + logger_thread_started, + log_file_name, + vcpu_started, + need_flush, + #[cfg(feature = "logger_debug")] + logger_dbg_file, + } + } + fn log_async_flush(&self, buf: &[u8]) { + let mut output = self.output.lock().unwrap(); + if output.is_none() { + *output = match std::fs::File::options() + .create(true) + .append(true) + .open(std::path::Path::new(&self.log_file_name)) + { + Ok(file) => Some(Box::new(file)), + Err(_) => Some(Box::new(std::io::stderr())), + }; + } + if output.is_some() { + (*(output.as_mut().unwrap())).write(buf).ok(); + } + } + + fn build_logger_thread(&self) { + let logger = logging::create_logger(self.log_file_name.clone()); + let guard = Some(slog_scope::set_global_logger(logger)); + LOG_GUARD.store(Arc::new(guard)); + } + + fn log_async(&self, data: String, target: &str) { + #[cfg(feature = "logger_debug")] + let _ = self + .logger_dbg_file + .lock() + .unwrap() + .write_all(data.as_bytes()); + + if self + .logger_thread_started + .lock() + .unwrap() + .load(Ordering::SeqCst) + { + match target { + LOG_CTRL_REOPEN => slog_info!(slog_scope::logger(), #LOG_CTRL_REOPEN, "{}", data), + _ => slog_info!(slog_scope::logger(), "{}", data), + } + return; + } + + if self.need_flush.load(Ordering::SeqCst) { + self.log_async_flush(data.as_bytes()); + } + + if !self.defer_logger_thread { + let logger_startd = self.logger_thread_started.lock().unwrap(); + if !logger_startd.load(Ordering::SeqCst) { + self.build_logger_thread(); + match target { + LOG_CTRL_REOPEN => { + slog_info!(slog_scope::logger(), #LOG_CTRL_REOPEN, "{}", data) + } + _ => slog_info!(slog_scope::logger(), "{}", data), + }; + logger_startd.store(true, Ordering::SeqCst); + } else { + // logger thread has been started, slog those waiting for + // logger_thread_started lock + match target { + LOG_CTRL_REOPEN => { + slog_info!(slog_scope::logger(), #LOG_CTRL_REOPEN, "{}", data) + } + _ => slog_info!(slog_scope::logger(), "{}", data), + }; + } + + return; + } + + if self.vcpu_started.load(Ordering::SeqCst) { + let logger_startd = self.logger_thread_started.lock().unwrap(); + if !logger_startd.load(Ordering::SeqCst) { + self.build_logger_thread(); + for v in self.buffer.lock().unwrap().drain(..) { + slog_info!(slog_scope::logger(), "{}", v); + } + self.buffer.lock().unwrap().shrink_to_fit(); + // log this info/debug/warn, 'cause this + // data has not been pushed into buffer + match target { + LOG_CTRL_REOPEN => { + slog_info!(slog_scope::logger(), #LOG_CTRL_REOPEN, "{}", data) + } + _ => slog_info!(slog_scope::logger(), "{}", data), + }; + logger_startd.store(true, Ordering::SeqCst); + } else { + // logger thread has been started, slog those waiting for + // logger_thread_started lock + match target { + LOG_CTRL_REOPEN => { + slog_info!(slog_scope::logger(), #LOG_CTRL_REOPEN, "{}", data) + } + _ => slog_info!(slog_scope::logger(), "{}", data), + }; + } + } else { + self.buffer.lock().unwrap().push(data); + } + } + + fn log_flush(&self) { + if !self.log_async { + return; + } + + for v in self.buffer.lock().unwrap().drain(..) { + self.log_async_flush(v.as_bytes()); + } + self.need_flush.store(true, Ordering::SeqCst); + } +} + +impl log::Log for Logger { + fn enabled(&self, _metadata: &log::Metadata) -> bool { + true + } + + fn log(&self, record: &log::Record) { + if !self.enabled(record.metadata()) { + return; + } + + // Drop control log under sync mode. + if !self.log_async && record.target() == LOG_CTRL_REOPEN { + return; + } + + let now = std::time::Instant::now(); + let duration = now.duration_since(self.start); + + if record.file().is_some() && record.line().is_some() { + let t = format!( + "{} --- {:?} --- {} --- <{}> {}:{} -- {}\n", + self.sandbox_id, + duration.as_millis(), + record.level(), + std::thread::current().name().unwrap_or("anonymous"), + record.file().unwrap(), + record.line().unwrap(), + record.args(), + ); + if self.log_async { + self.log_async(t, record.target()); + } else { + (*(self.output.lock().unwrap().as_mut().unwrap())) + .write(t.as_bytes()) + .ok(); + } + } else { + let t = format!( + "{} --- {:?} --- {} --- <{}> {} -- {}\n", + self.sandbox_id, + duration.as_millis(), + record.level(), + std::thread::current().name().unwrap_or("anonymous"), + record.target(), + record.args(), + ); + if self.log_async { + self.log_async(t, record.target()); + } else { + (*(self.output.lock().unwrap().as_mut().unwrap())) + .write(t.as_bytes()) + .ok(); + } + } + } + + fn flush(&self) { + self.log_flush(); + } +} diff --git a/vendor/cube-hypervisor/src/lib.rs b/vendor/cube-hypervisor/src/lib.rs new file mode 100644 index 0000000..c4acfbf --- /dev/null +++ b/vendor/cube-hypervisor/src/lib.rs @@ -0,0 +1,359 @@ +mod common; +pub mod vmm_config; + +pub use event_notifier::NotifyEvent; +pub use vmm::api::*; +pub use vmm::config; +pub use vmm::seccomp_filters::*; +pub use vmm::vm_config; +pub use vmm::{SnapshotConfig, SnapshotType}; + +use chrono::Local; +use libc::{EFD_NONBLOCK, SIGSYS, SIGTERM}; +use log::{error, info, LevelFilter}; +use logging::START_TM; +use seccompiler::SeccompAction; +use std::fs::File; +use std::os::fd::FromRawFd; +use std::sync::atomic::AtomicBool; +use std::sync::mpsc::{channel, RecvError, SendError}; +use std::sync::{Arc, Barrier, Mutex}; +use std::thread; +use std::thread::JoinHandle; +use std::time::{Duration, Instant}; +use thiserror::Error; +use vmm::api::service::{Error as VmmServiceError, VMM_SERVICE}; +use vmm::Error as VmmError; +use vmm_sys_util::eventfd::EventFd; +use vmm_sys_util::signal::{block_signal, Killable}; +use vmm_sys_util::terminal::Terminal; + +use crate::common::DEFAULT_LOGGER_BUFFER_SIZE; +use crate::vmm_config::VmmConfig; + +#[derive(Debug, Error)] +pub enum Error { + #[error("Failed to recv ApiResponse by channel: {0}")] + ReviverChannel(#[source] RecvError), + #[error("Failed to send ApiRequest by channel: {0}")] + SenderChannel(#[source] SendError), + #[error("Failed to start vmm: {0}")] + StartVmm(#[source] VmmError), + #[error("Failed to create EventFd: {0}")] + CreateEventFd(#[source] std::io::Error), + #[error("Failed to create Hypervisor: {0}")] + CreateHypervisor(#[source] hypervisor::HypervisorError), + #[error("Error setting up logger: {0}")] + LoggerSetup(#[source] log::SetLoggerError), + #[error("Error setting up coredump: {0}")] + CoredumpSetup(#[source] std::io::Error), + #[error("Error parsing --event-monitor: path or fd required")] + BareEventMonitor, + #[error("Error doing event monitor I/O: {0}")] + EventMonitorIo(#[source] std::io::Error), + #[error("Error doing log json I/O: {0}")] + LogJsonIo(#[source] std::io::Error), + #[error("Error init vmm service: {0}")] + InitVmmService(#[source] VmmServiceError), + #[error("Failed to send request: {0}")] + SendRequest(#[source] VmmServiceError), + #[error("Failed to join on VMM thread: {0:?}")] + ThreadJoin(Box), + #[error("VMM thread exited with error: {0}")] + VmmThread(#[source] vmm::Error), +} + +/// # Vmm instance for CH +/// +/// VmmInstance provides vmm management interfaces for upper layer access. +pub struct VmmInstance { + vmm_thread: Option>>, +} + +impl VmmInstance { + /// Create a vmm instance + /// + /// This function will prepare the vmm resources and start a vmm thread, the vmm + /// thread is ready when this function returns. + /// + /// # Examples: + /// ``` + /// use cube_hypervisor::vmm_config::VmmConfig; + /// use cube_hypervisor::VmmInstance; + /// + /// let vmm = VmmInstance::new(VmmConfig::default()).expect("Failed to create vmm"); + /// ``` + pub fn new(vmm_config: VmmConfig) -> Result { + let now = std::time::Instant::now(); + let local = Local::now(); + let tm1 = std::time::Instant::now().duration_since(now); + + let defer_logger_thread = vmm_config.log_level <= LevelFilter::Info; + + // Init log file and log async mode. + let (log_file, log_async) = { + if !vmm_config.log_stderr { + // Open async log file later in cube-log + // thread. Let cube-log thread handle the + // log file rotate(reopen) logic. + (None, true) + } else { + let file: Box = Box::new(std::io::stderr()); + (Some(file), false) + } + }; + + #[cfg(feature = "logger_debug")] + let mut logger_dbg_file_name = vmm_config.log_file.clone(); + #[cfg(feature = "logger_debug")] + logger_dbg_file_name.push_str(".dbg"); + #[cfg(feature = "logger_debug")] + let logger_dbg_file = Arc::new(Mutex::new( + std::fs::OpenOptions::new() + .write(true) + .create(true) + .append(true) + .open(logger_dbg_file_name) + .unwrap(), + )); + + let wait_vcpu_started = Arc::new(AtomicBool::new(false)); + *START_TM.lock().unwrap() = now; + + // set_boxed_logger only return an error when initialized multiple times。 + if log::set_boxed_logger(Box::new(common::Logger::new( + Mutex::new(log_file), + now, + vmm_config.sandbox_id.clone(), + log_async, + defer_logger_thread, + Arc::new(Mutex::new(Vec::with_capacity(DEFAULT_LOGGER_BUFFER_SIZE))), + Arc::new(Mutex::new(AtomicBool::new(false))), + vmm_config.log_file, + wait_vcpu_started.clone(), + Arc::new(AtomicBool::new(false)), + #[cfg(feature = "logger_debug")] + logger_dbg_file, + ))) + .map(|()| log::set_max_level(vmm_config.log_level)) + .is_err() + { + error!( + "Logger has been initialized, please check if VMM has been created multiple times!" + ); + } + + let tm2 = std::time::Instant::now().duration_since(now); + info!( + "Cube-Hypervisor version {}, prepare main {}ms, prepare vmm {}ms, start {}", + env!("BUILT_VERSION"), + tm1.as_millis(), + tm2.as_millis(), + local + ); + + if let Some(monitor_config) = vmm_config.event_monitor { + let file = if let Some(fd) = monitor_config.fd { + // SAFETY: fd is valid + unsafe { File::from_raw_fd(fd) } + } else if let Some(path) = monitor_config.path { + std::fs::OpenOptions::new() + .write(true) + .create(true) + .open(path.as_str()) + .map_err(Error::EventMonitorIo)? + } else { + return Err(Error::BareEventMonitor); + }; + event_monitor::set_monitor(file).map_err(Error::EventMonitorIo)?; + } + + if let Some(notifier_config) = vmm_config.event_notifier { + event_notifier::setup_notifier(notifier_config.notifier); + } + + if let Some(file) = vmm_config.log_json_file { + log_json::set_log_file(file).map_err(Error::LogJsonIo)?; + } + + let (req_sender, req_receiver) = channel(); + let (res_sender, res_receiver) = channel(); + let api_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::CreateEventFd)?; + + VMM_SERVICE + .lock() + .unwrap() + .init(req_sender, api_evt.try_clone().unwrap(), res_receiver) + .map_err(Error::InitVmmService)?; + + let hypervisor = hypervisor::new().map_err(Error::CreateHypervisor)?; + + #[cfg(feature = "guest_debug")] + let gdb_socket_path = None; + #[cfg(feature = "guest_debug")] + let debug_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::CreateEventFd)?; + #[cfg(feature = "guest_debug")] + let vm_debug_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::CreateEventFd)?; + + if vmm_config.seccomp == SeccompAction::Trap + || vmm_config.seccomp == SeccompAction::KillProcess + { + // SAFETY: We only using signal_hook for managing signals and only execute signal + // handler safe functions (writing to stderr) and manipulating signals. + unsafe { + signal_hook::low_level::register(SIGSYS, || { + eprint!("\nError signal: SIGSYS, possible seccomp violation.\n"); + error!("Error signal: SIGSYS, possible seccomp violation."); + }) + } + .map_err(|e| error!("Error adding SIGSYS signal handler: {}", e)) + .ok(); + } + + // Before we start any threads, mask the signals we'll be + // installing handlers for, to make sure they only ever run on the + // dedicated signal handling thread we'll start in a bit. + for sig in &vmm::vm::Vm::HANDLED_SIGNALS { + if let Err(e) = block_signal(*sig) { + error!("Error blocking signals: {}", e); + } + } + + for sig in &vmm::Vmm::HANDLED_SIGNALS { + if let Err(e) = block_signal(*sig) { + error!("Error blocking signals: {}", e); + } + } + + // waiting for vmm thread + let barrier = Arc::new(Barrier::new(2)); + + let vmm_thread = vmm::start_vmm_thread( + env!("CARGO_PKG_VERSION").to_string(), + &vmm_config.http_path, + None, + api_evt, + req_receiver, + res_sender.clone(), + #[cfg(feature = "guest_debug")] + gdb_socket_path, + #[cfg(feature = "guest_debug")] + debug_evt.try_clone().unwrap(), + #[cfg(feature = "guest_debug")] + vm_debug_evt.try_clone().unwrap(), + &vmm_config.seccomp, + hypervisor, + vmm_config.sandbox_id, + wait_vcpu_started, + Some(barrier.clone()), + ) + .map_err(Error::StartVmm)?; + + barrier.wait(); + + Ok(Self { + vmm_thread: Some(vmm_thread), + }) + } + + /// Send api request to vmm + /// + /// This function provides a interface for up layer to send request to vmm. + /// + /// # Examples: + /// ``` + /// use cube_hypervisor::vmm_config::VmmConfig; + /// use cube_hypervisor::VmmInstance; + /// use vmm::api::ApiRequest; + /// + /// let vmm = VmmInstance::new(VmmConfig::default()).expect("Failed to create vmm"); + /// let response = vmm.send_request(ApiRequest::VmmPing) + /// .expect("Failed to send vmm ping request"); + /// ``` + pub fn send_request(&self, request: ApiRequest) -> Result { + VMM_SERVICE + .lock() + .unwrap() + .send_request(request) + .map_err(Error::SendRequest) + } + + /// Wait for the vmm thread to finish. + /// + /// This function will return immediately if the associated thread has already finished. + /// + /// # Examples: + /// ``` + /// use cube_hypervisor::vmm_config::VmmConfig; + /// use cube_hypervisor::VmmInstance; + /// use vmm::api::ApiRequest; + /// + /// let mut vmm = VmmInstance::new(VmmConfig::default()).expect("Failed to create vmm"); + /// vmm.join().unwrap(); + /// ``` + pub fn join(&mut self) -> Result<(), Error> { + if let Some(thread) = self.vmm_thread.take() { + thread + .join() + .map_err(Error::ThreadJoin)? + .map_err(Error::VmmThread)?; + } + Ok(()) + } + + /// Wait timeout for the vmm thread to finish. + /// + /// This function will return immediately if the associated thread has already finished. The + /// vmm thread will be terminated after timeout. The default timeout is 5s. + /// + /// # Examples: + /// ``` + /// use std::time::Duration; + /// use cube_hypervisor::vmm_config::VmmConfig; + /// use cube_hypervisor::VmmInstance; + /// use vmm::api::ApiRequest; + /// + /// let mut vmm = VmmInstance::new(VmmConfig::default()).expect("Failed to create vmm"); + /// vmm.join_timeout(Some(Duration::from_secs(5))).unwrap(); + /// ``` + pub fn join_timeout(&mut self, timeout: Option) -> Result<(), Error> { + let timeout_duration = if let Some(duration) = timeout { + duration + } else { + Duration::from_secs(5) + }; + let start_time = Instant::now(); + + if let Some(thread) = self.vmm_thread.take() { + while !thread.is_finished() { + if start_time.elapsed() >= timeout_duration { + error!("Vmm thread join timeout, kill with SIGTERM"); + thread.kill(SIGTERM).expect("vmm thread kill err"); + } + thread::sleep(Duration::from_millis(100)); + } + } + Ok(()) + } +} + +impl Drop for VmmInstance { + fn drop(&mut self) { + if let Some(thread) = self.vmm_thread.as_ref() { + if !thread.is_finished() && self.send_request(ApiRequest::VmmShutdown).is_err() { + error!("Failed to shutdown vmm, kill with SIGTERM"); + thread.kill(SIGTERM).expect("vmm thread kill err"); + } + } + + self.join().unwrap(); + log::logger().flush(); + // SAFETY: trivially safe + let on_tty = unsafe { libc::isatty(libc::STDIN_FILENO) } != 0; + if on_tty { + // Don't forget to set the terminal in canonical mode + // before to exit. + std::io::stdin().lock().set_canon_mode().unwrap(); + } + } +} diff --git a/vendor/cube-hypervisor/src/main.rs b/vendor/cube-hypervisor/src/main.rs new file mode 100644 index 0000000..0380551 --- /dev/null +++ b/vendor/cube-hypervisor/src/main.rs @@ -0,0 +1,1831 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +mod common; +#[macro_use(crate_authors)] +extern crate clap; +extern crate log_json; + +use std::env; +use std::fs::File; +use std::io::Write; +use std::os::fd::RawFd; +use std::os::unix::io::FromRawFd; +use std::sync::atomic::AtomicBool; +use std::sync::{Arc, Mutex}; + +use crate::common::{default_coredump_filter, default_coredump_limit, DEFAULT_LOGGER_BUFFER_SIZE}; +use chrono::{DateTime, Local}; +use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command}; +use event_monitor::event; +use libc::{EFD_NONBLOCK, SIGSYS}; +use log::{error, info, LevelFilter}; +use logging::START_TM; +use option_parser::OptionParser; +use rlimit::{setrlimit, Resource}; +use seccompiler::SeccompAction; +use std::sync::mpsc::channel; +use thiserror::Error; +use vmm::api::service::{Error as VmmServiceError, VMM_SERVICE}; +use vmm::config::{RestoreConfig, VmParams}; +#[cfg(target_arch = "x86_64")] +use vmm::vm_config::SgxEpcConfig; +use vmm::vm_config::{ + BalloonConfig, DeviceConfig, DiskConfig, FsConfig, IvshmemConfig, NetConfig, NumaConfig, + PmemConfig, TpmConfig, UserDeviceConfig, VdpaConfig, VmConfig, VsockConfig, + DEFAULT_MAX_PHYS_BITS, DEFAULT_MEMORY_MB, DEFAULT_RNG_SOURCE, DEFAULT_VCPUS, +}; +use vmm_sys_util::eventfd::EventFd; +use vmm_sys_util::signal::block_signal; +use vmm_sys_util::terminal::Terminal; + +extern crate slog; +extern crate slog_scope; + +#[derive(Error, Debug)] +enum Error { + #[error("Failed to create API EventFd: {0}")] + CreateApiEventFd(#[source] std::io::Error), + #[cfg(feature = "guest_debug")] + #[error("Failed to create Debug EventFd: {0}")] + CreateDebugEventFd(#[source] std::io::Error), + #[error("Failed to open hypervisor interface (is hypervisor interface available?): {0}")] + CreateHypervisor(#[source] hypervisor::HypervisorError), + #[error("Failed to start the VMM thread: {0}")] + StartVmmThread(#[source] vmm::Error), + #[error("Error parsing config: {0}")] + ParsingConfig(vmm::config::Error), + #[error("Error creating VM: {0:?}")] + VmCreate(vmm::api::ApiError), + #[error("Error booting VM: {0:?}")] + VmBoot(vmm::api::ApiError), + #[error("Error restoring VM: {0:?}")] + VmRestore(vmm::api::ApiError), + #[error("Error parsing restore: {0}")] + ParsingRestore(vmm::config::Error), + #[error("Failed to join on VMM thread: {0:?}")] + ThreadJoin(std::boxed::Box), + #[error("VMM thread exited with error: {0}")] + VmmThread(#[source] vmm::Error), + #[error("Error parsing --api-socket: {0}")] + ParsingApiSocket(std::num::ParseIntError), + #[error("Error parsing --event-monitor: {0}")] + ParsingEventMonitor(option_parser::OptionParserError), + #[error("Error parsing --event-monitor: path or fd required")] + BareEventMonitor, + #[error("Error doing event monitor I/O: {0}")] + EventMonitorIo(std::io::Error), + #[cfg(feature = "guest_debug")] + #[error("Error parsing --gdb: {0}")] + ParsingGdb(option_parser::OptionParserError), + #[cfg(feature = "guest_debug")] + #[error("Error parsing --gdb: path required")] + BareGdb, + #[error("Error creating log file: {0}")] + LogFileCreation(std::io::Error), + #[error("Error setting up logger: {0}")] + LoggerSetup(log::SetLoggerError), + #[error("Error setting up coredump: {0}")] + CoredumpSetup(std::io::Error), + #[error("Error init vmm service: {0}")] + InitVmmService(VmmServiceError), +} + +fn prepare_default_values() -> (String, String, String) { + let default_vcpus = format! {"boot={},max_phys_bits={}", DEFAULT_VCPUS,DEFAULT_MAX_PHYS_BITS}; + let default_memory = format! {"size={}M", DEFAULT_MEMORY_MB}; + let default_rng = format! {"src={}", DEFAULT_RNG_SOURCE}; + + (default_vcpus, default_memory, default_rng) +} + +fn create_app(default_vcpus: String, default_memory: String, default_rng: String) -> Command { + let app = Command::new("cube-hypervisor") + // 'BUILT_VERSION' is set by the build script 'build.rs' at + // compile time + .version(env!("BUILT_VERSION")) + .author(crate_authors!()) + .about("Launch a cloud-hypervisor VMM.") + .group(ArgGroup::new("vm-config").multiple(true)) + .group(ArgGroup::new("vmm-config").multiple(true)) + .group(ArgGroup::new("logging").multiple(true)) + .arg( + Arg::new("cpus") + .long("cpus") + .help( + "boot=,max=,\ + topology=:::,\ + kvm_hyperv=on|off,max_phys_bits=,\ + affinity=,\ + features=,compatible=vendor|max|ignore", + ) + .default_value(default_vcpus) + .group("vm-config"), + ) + .arg( + Arg::new("platform") + .long("platform") + .help( + "num_pci_segments=,iommu_segments=,serial_number=,uuid=,oem_strings=", + ) + .num_args(1) + .group("vm-config"), + ) + .arg( + Arg::new("memory") + .long("memory") + .help( + "Memory parameters \ + \"size=,mergeable=on|off,shared=on|off,\ + hugepages=on|off,hugepage_size=,\ + hotplug_method=acpi|virtio-mem,\ + hotplug_size=,\ + hotplugged_size=,\ + prefault=on|off,thp=on|off\"", + ) + .default_value(default_memory) + .group("vm-config"), + ) + .arg( + Arg::new("memory-zone") + .long("memory-zone") + .help( + "User defined memory zone parameters \ + \"size=,file=,\ + shared=on|off,\ + hugepages=on|off,hugepage_size=,\ + host_numa_node=,\ + id=,hotplug_size=,\ + hotplugged_size=,\ + prefault=on|off\"", + ) + .num_args(1..) + .group("vm-config"), + ) + .arg( + Arg::new("firmware") + .long("firmware") + .help("Path to firmware that is loaded in an architectural specific way") + .num_args(1) + .group("vm-config"), + ) + .arg( + Arg::new("kernel") + .long("kernel") + .help( + "Path to kernel to load. This may be a kernel or firmware that supports a PVH \ + entry point (e.g. vmlinux) or architecture equivalent", + ) + .num_args(1) + .group("vm-config"), + ) + .arg( + Arg::new("initramfs") + .long("initramfs") + .help("Path to initramfs image") + .num_args(1) + .group("vm-config"), + ) + .arg( + Arg::new("cmdline") + .long("cmdline") + .help("Kernel command line") + .num_args(1) + .group("vm-config"), + ) + .arg( + Arg::new("disk") + .long("disk") + .help(DiskConfig::SYNTAX) + .num_args(1..) + .group("vm-config"), + ) + .arg( + Arg::new("net") + .long("net") + .help(NetConfig::SYNTAX) + .num_args(1..) + .group("vm-config"), + ) + .arg( + Arg::new("rng") + .long("rng") + .help( + "Random number generator parameters \"src=,iommu=on|off\"", + ) + .default_value(default_rng) + .group("vm-config"), + ) + .arg( + Arg::new("balloon") + .long("balloon") + .help(BalloonConfig::SYNTAX) + .num_args(1) + .group("vm-config"), + ) + .arg( + Arg::new("fs") + .long("fs") + .help(FsConfig::SYNTAX) + .num_args(1..) + .group("vm-config"), + ) + .arg( + Arg::new("pmem") + .long("pmem") + .help(PmemConfig::SYNTAX) + .num_args(1..) + .group("vm-config"), + ) + .arg( + Arg::new("serial") + .long("serial") + .help("Control serial port: off|null|pty|tty|file=/path/to/a/file") + .default_value("null") + .group("vm-config"), + ) + .arg( + Arg::new("console") + .long("console") + .help( + "Control (virtio) console: \"off|null|pty|tty|file=/path/to/a/file,iommu=on|off\"", + ) + .default_value("tty") + .group("vm-config"), + ) + .arg( + Arg::new("device") + .long("device") + .help(DeviceConfig::SYNTAX) + .num_args(1..) + .group("vm-config"), + ) + .arg( + Arg::new("user-device") + .long("user-device") + .help(UserDeviceConfig::SYNTAX) + .num_args(1..) + .group("vm-config"), + ) + .arg( + Arg::new("vdpa") + .long("vdpa") + .help(VdpaConfig::SYNTAX) + .num_args(1..) + .group("vm-config"), + ) + .arg( + Arg::new("vsock") + .long("vsock") + .help(VsockConfig::SYNTAX) + .num_args(1) + .group("vm-config"), + ) + .arg( + Arg::new("numa") + .long("numa") + .help(NumaConfig::SYNTAX) + .num_args(1..) + .group("vm-config"), + ) + .arg( + Arg::new("watchdog") + .long("watchdog") + .help("Enable virtio-watchdog") + .num_args(0) + .action(ArgAction::SetTrue) + .group("vm-config"), + ) + .arg( + Arg::new("v") + .short('v') + .action(ArgAction::Count) + .help("Sets the level of debugging output") + .group("logging"), + ) + .arg( + Arg::new("log-file") + .long("log-file") + .help("Log file. Standard error is used if not specified") + .num_args(1) + .group("logging"), + ) + .arg( + Arg::new("sandbox-id") + .long("sandbox-id") + .help("Ssandbox ID, will show in logs") + .num_args(1) + .group("logging"), + ) + .arg( + Arg::new("log-stderr") + .long("log-stderr") + .help("Logging to stderr, disable log-file.") + .num_args(0) + .action(ArgAction::SetTrue) + .group("logging"), + ) + .arg( + Arg::new("sys-ctrl") + .long("sys-ctrl") + .help("Enable system controller") + .num_args(0) + .action(ArgAction::SetTrue) + .group("vm-config"), + ) + .arg( + Arg::new("api-socket") + .long("api-socket") + .help("HTTP API socket (UNIX domain socket): path=
or fd=.") + .num_args(1) + .group("vmm-config"), + ) + .arg( + Arg::new("event-monitor") + .long("event-monitor") + .help("File to report events on: path=
or fd=") + .num_args(1) + .group("vmm-config"), + ) + .arg( + Arg::new("restore") + .long("restore") + .help(RestoreConfig::SYNTAX) + .num_args(1) + .group("vmm-config"), + ) + .arg( + Arg::new("seccomp") + .long("seccomp") + .num_args(1) + .value_parser(["true", "false", "log", "process"]) + .default_value("process"), + ) + .arg( + Arg::new("tpm") + .long("tpm") + .num_args(1) + .help(TpmConfig::SYNTAX) + .group("vm-config"), + + ) + .arg( + Arg::new("coredump") + .long("coredump") + .help("filter=,limit=") + .num_args(1..) + .group("vmm-config"), + ) + .arg( + Arg::new("pvpanic") + .long("pvpanic") + .help("Enable pvpanic-pci") + .num_args(0) + .action(ArgAction::SetTrue) + .group("vm-config"), + ) + .arg( + Arg::new("ivshmem") + .long("ivshmem") + .help(IvshmemConfig::SYNTAX) + .num_args(1..) + .group("vm-config"), + ); + + #[cfg(target_arch = "x86_64")] + let app = app.arg( + Arg::new("sgx-epc") + .long("sgx-epc") + .help(SgxEpcConfig::SYNTAX) + .num_args(1..) + .group("vm-config"), + ); + + #[cfg(feature = "guest_debug")] + let app = app.arg( + Arg::new("gdb") + .long("gdb") + .help("GDB socket (UNIX domain socket): path=") + .num_args(1) + .group("vmm-config"), + ); + + let app = app.arg( + Arg::new("snapshot-version") + .short('D') + .long("snapshot-version") + .exclusive(true) + .action(clap::ArgAction::SetTrue) + .help("snapshot version, restore from different snapshot version is not supported"), + ); + + app +} + +fn start_vmm( + cmd_arguments: ArgMatches, + now: std::time::Instant, + local: DateTime, +) -> Result, Error> { + let tm1 = std::time::Instant::now().duration_since(now); + + // Create dummy socket and dup it to large fd, so that we could trigger + // expand_files in kernel. And this tricky work will avoid later multi + // thread try to invoke expand_files at same time, there will be lock + // contention in expand_files. + // SAFETY: FFI call + unsafe { + let dummy = libc::socket(libc::AF_UNIX, libc::SOCK_STREAM | libc::SOCK_CLOEXEC, 0); + if dummy > 0 { + libc::dup2(dummy, 512); + } + } + + let mut defer_logger_thread: bool = true; + let log_level = match cmd_arguments.get_count("v") { + 0 => LevelFilter::Warn, + 1 => LevelFilter::Info, + 2 => LevelFilter::Debug, + _ => LevelFilter::Trace, + }; + + if log_level > LevelFilter::Info { + defer_logger_thread = false; + } + + // Init log file name. + let log_file_name = if let Some(file) = cmd_arguments.get_one::("log-file") { + file.clone() + } else { + String::from(crate::common::DEFAULT_LOG_FILE) + }; + + // Init log file and log async mode. + let (log_file, log_async) = { + if !cmd_arguments.get_flag("log-stderr") { + // Create directories. We need this when we add new machine in + // Cube environment. + let dir = std::path::Path::new(&log_file_name).parent().unwrap(); + std::fs::create_dir_all(dir).map_err(Error::LogFileCreation)?; + + // Open async log file later in cube-log + // thread. Let cube-log thread handle the + // log file rotate(reopen) logic. + (None, true) + } else { + let file: Box = Box::new(std::io::stderr()); + (Some(file), false) + } + }; + + let sandbox_id: String = if let Some(sandboxid) = cmd_arguments.get_one::("sandbox-id") + { + sandboxid.to_string() + } else { + String::from("cube-hypervisor") + }; + + #[cfg(feature = "logger_debug")] + let mut logger_dbg_file_name = log_file_name.clone(); + #[cfg(feature = "logger_debug")] + logger_dbg_file_name.push_str(".dbg"); + #[cfg(feature = "logger_debug")] + let logger_dbg_file = Arc::new(Mutex::new( + std::fs::OpenOptions::new() + .write(true) + .create(true) + .append(true) + .open(logger_dbg_file_name) + .unwrap(), + )); + + let wait_vcpu_started = Arc::new(AtomicBool::new(false)); + *START_TM.lock().unwrap() = now; + + log::set_boxed_logger(Box::new(crate::common::Logger::new( + Mutex::new(log_file), + now, + sandbox_id.clone(), + log_async, + defer_logger_thread, + Arc::new(Mutex::new(Vec::with_capacity(DEFAULT_LOGGER_BUFFER_SIZE))), + Arc::new(Mutex::new(AtomicBool::new(false))), + log_file_name, + wait_vcpu_started.clone(), + Arc::new(AtomicBool::new(false)), + #[cfg(feature = "logger_debug")] + logger_dbg_file, + ))) + .map(|()| log::set_max_level(log_level)) + .map_err(Error::LoggerSetup)?; + + #[cfg(not(feature = "lib_support"))] + std::panic::set_hook(Box::new(|info| { + info!("{info:?}"); + log::logger().flush(); + })); + + let tm2 = std::time::Instant::now().duration_since(now); + + info!( + "Cube-Hypervisor version {} started with PID: {}, prepare main {}ms, prepare vmm {}ms, start {}", + env!("BUILT_VERSION"), + std::process::id(), + tm1.as_millis(), + tm2.as_millis(), + local + ); + + let mut coredump_limit = default_coredump_limit(); + let mut coredump_filter = default_coredump_filter(); + if let Some(coredump_config) = cmd_arguments.get_one::("coredump") { + let mut parser = OptionParser::new(); + + parser.add("filter").add("limit"); + parser.parse(coredump_config).unwrap_or_default(); + + if let Some(filter) = parser.get("filter") { + coredump_filter = filter; + } + + if let Some(limit) = parser.get("limit") { + coredump_limit = limit.parse::().unwrap(); + } + } + + let mut filter_file = File::options() + .write(true) + .open("/proc/self/coredump_filter") + .map_err(Error::CoredumpSetup)?; + filter_file + .write(coredump_filter.as_bytes()) + .map_err(Error::CoredumpSetup)?; + setrlimit(Resource::CORE, coredump_limit, coredump_limit).map_err(Error::CoredumpSetup)?; + + let (api_socket_path, api_socket_fd) = + if let Some(socket_config) = cmd_arguments.get_one::("api-socket") { + let mut parser = OptionParser::new(); + parser.add("path").add("fd"); + parser.parse(socket_config).unwrap_or_default(); + + if let Some(fd) = parser.get("fd") { + ( + None, + Some(fd.parse::().map_err(Error::ParsingApiSocket)?), + ) + } else if let Some(path) = parser.get("path") { + (Some(path), None) + } else { + ( + cmd_arguments + .get_one::("api-socket") + .map(|s| s.to_string()), + None, + ) + } + } else { + (None, None) + }; + + if let Some(monitor_config) = cmd_arguments.get_one::("event-monitor") { + let mut parser = OptionParser::new(); + parser.add("path").add("fd"); + parser + .parse(monitor_config) + .map_err(Error::ParsingEventMonitor)?; + + let file = if parser.is_set("fd") { + let fd = parser + .convert("fd") + .map_err(Error::ParsingEventMonitor)? + .unwrap(); + unsafe { File::from_raw_fd(fd) } + } else if parser.is_set("path") { + std::fs::OpenOptions::new() + .write(true) + .create(true) + .open(parser.get("path").unwrap()) + .map_err(Error::EventMonitorIo)? + } else { + return Err(Error::BareEventMonitor); + }; + event_monitor::set_monitor(file).map_err(Error::EventMonitorIo)?; + } + + let (api_request_sender, api_request_receiver) = channel(); + let (res_sender, res_receiver) = channel(); + let api_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::CreateApiEventFd)?; + + VMM_SERVICE + .lock() + .unwrap() + .init( + api_request_sender, + api_evt.try_clone().unwrap(), + res_receiver, + ) + .map_err(Error::InitVmmService)?; + + let seccomp_action = if let Some(seccomp_value) = cmd_arguments.get_one::("seccomp") { + match seccomp_value as &str { + "true" => SeccompAction::Trap, + "false" => SeccompAction::Allow, + "log" => SeccompAction::Log, + "process" => SeccompAction::KillProcess, + _ => { + // The user providing an invalid value will be rejected by clap + panic!("Invalid parameter {} for \"--seccomp\" flag", seccomp_value); + } + } + } else { + SeccompAction::KillProcess + }; + + if seccomp_action == SeccompAction::Trap || seccomp_action == SeccompAction::KillProcess { + // SAFETY: We only using signal_hook for managing signals and only execute signal + // handler safe functions (writing to stderr) and manipulating signals. + unsafe { + signal_hook::low_level::register(SIGSYS, || { + eprint!("\nError signal: SIGSYS, possible seccomp violation.\n"); + error!("Error signal: SIGSYS, possible seccomp violation."); + signal_hook::low_level::emulate_default_handler(SIGSYS).unwrap(); + }) + } + .map_err(|e| error!("Error adding SIGSYS signal handler: {e}")) + .ok(); + } + + // Before we start any threads, mask the signals we'll be + // installing handlers for, to make sure they only ever run on the + // dedicated signal handling thread we'll start in a bit. + for sig in &vmm::vm::Vm::HANDLED_SIGNALS { + if let Err(e) = block_signal(*sig) { + error!("Error blocking signals: {e}"); + } + } + + for sig in &vmm::Vmm::HANDLED_SIGNALS { + if let Err(e) = block_signal(*sig) { + error!("Error blocking signals: {e}"); + } + } + + event!("vmm", "starting"); + + let hypervisor = hypervisor::new().map_err(Error::CreateHypervisor)?; + + #[cfg(feature = "guest_debug")] + let gdb_socket_path = if let Some(gdb_config) = cmd_arguments.get_one::("gdb") { + let mut parser = OptionParser::new(); + parser.add("path"); + parser.parse(gdb_config).map_err(Error::ParsingGdb)?; + + if parser.is_set("path") { + Some(std::path::PathBuf::from(parser.get("path").unwrap())) + } else { + return Err(Error::BareGdb); + } + } else { + None + }; + #[cfg(feature = "guest_debug")] + let debug_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::CreateDebugEventFd)?; + #[cfg(feature = "guest_debug")] + let vm_debug_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::CreateDebugEventFd)?; + + let vmm_thread = vmm::start_vmm_thread( + env!("CARGO_PKG_VERSION").to_string(), + &api_socket_path, + api_socket_fd, + api_evt, + api_request_receiver, + res_sender, + #[cfg(feature = "guest_debug")] + gdb_socket_path, + #[cfg(feature = "guest_debug")] + debug_evt.try_clone().unwrap(), + #[cfg(feature = "guest_debug")] + vm_debug_evt.try_clone().unwrap(), + &seccomp_action, + hypervisor, + sandbox_id, + wait_vcpu_started, + None, + ) + .map_err(Error::StartVmmThread)?; + + let payload_present = + cmd_arguments.contains_id("kernel") || cmd_arguments.contains_id("firmware"); + + if payload_present { + let vm_params = VmParams::from_arg_matches(&cmd_arguments); + let vm_config = VmConfig::parse(vm_params).map_err(Error::ParsingConfig)?; + + // Create and boot the VM based off the VM config we just built. + vmm::api::vm_create(Box::new(vm_config)).map_err(Error::VmCreate)?; + vmm::api::vm_boot().map_err(Error::VmBoot)?; + } else if let Some(restore_params) = cmd_arguments.get_one::("restore") { + vmm::api::vm_restore(Arc::new( + RestoreConfig::parse(restore_params).map_err(Error::ParsingRestore)?, + )) + .map_err(Error::VmRestore)?; + } + + vmm_thread + .join() + .map_err(Error::ThreadJoin)? + .map_err(Error::VmmThread)?; + + Ok(api_socket_path) +} + +fn main() { + let now = std::time::Instant::now(); + let local = Local::now(); + // Ensure all created files (.e.g sockets) are only accessible by this user + let _ = unsafe { libc::umask(0o077) }; + + let (default_vcpus, default_memory, default_rng) = prepare_default_values(); + let cmd_arguments = create_app(default_vcpus, default_memory, default_rng).get_matches(); + if cmd_arguments.get_flag("snapshot-version") { + println!("Sanpshot Version: {}", vmm::api::SNAPSHOT_VERSION); + return; + } + let exit_code = match start_vmm(cmd_arguments, now, local) { + Ok(path) => { + path.map(|s| std::fs::remove_file(s).ok()); + 0 + } + Err(e) => { + eprintln!("{}", e); + log::logger().flush(); + 1 + } + }; + + let on_tty = unsafe { libc::isatty(libc::STDIN_FILENO) } != 0; + if on_tty { + // Don't forget to set the terminal in canonical mode + // before to exit. + std::io::stdin().lock().set_canon_mode().unwrap(); + } + + std::process::exit(exit_code); +} + +#[cfg(test)] +mod unit_tests { + use crate::{create_app, prepare_default_values}; + use std::path::PathBuf; + use vmm::config::VmParams; + use vmm::vm_config::{ + CompatibleMode, ConsoleConfig, ConsoleOutputMode, CpuFeatures, CpusConfig, HotplugMethod, + MemoryConfig, PayloadConfig, RngConfig, VmConfig, + }; + + fn get_vm_config_from_vec(args: &[&str]) -> VmConfig { + let (default_vcpus, default_memory, default_rng) = prepare_default_values(); + let cmd_arguments = + create_app(default_vcpus, default_memory, default_rng).get_matches_from(args); + + let vm_params = VmParams::from_arg_matches(&cmd_arguments); + + VmConfig::parse(vm_params).unwrap() + } + + fn compare_vm_config_cli_vs_json( + cli: &[&str], + openapi: &str, + equal: bool, + ) -> (VmConfig, VmConfig) { + let cli_vm_config = get_vm_config_from_vec(cli); + let openapi_vm_config: VmConfig = serde_json::from_str(openapi).unwrap(); + + if equal { + assert_eq!(cli_vm_config, openapi_vm_config); + } else { + assert_ne!(cli_vm_config, openapi_vm_config); + } + + (cli_vm_config, openapi_vm_config) + } + + #[test] + fn test_valid_vm_config_default() { + let cli = vec!["cloud-hypervisor", "--kernel", "/path/to/kernel"]; + let openapi = r#"{ "payload": {"kernel": "/path/to/kernel"} }"#; + + // First we check we get identical VmConfig structures. + let (result_vm_config, _) = compare_vm_config_cli_vs_json(&cli, openapi, true); + + // As a second step, we validate all the default values. + let expected_vm_config = VmConfig { + cpus: CpusConfig { + boot_vcpus: 1, + max_vcpus: 1, + topology: None, + kvm_hyperv: false, + max_phys_bits: 46, + affinity: None, + features: CpuFeatures::default(), + compatible: CompatibleMode::Vendor, + }, + memory: MemoryConfig { + size: 536_870_912, + mergeable: false, + hotplug_method: HotplugMethod::Acpi, + hotplug_size: None, + hotplugged_size: None, + shared: false, + hugepages: false, + hugepage_size: None, + prefault: false, + zones: None, + thp: true, + dirty_log: false, + }, + payload: Some(PayloadConfig { + kernel: Some(PathBuf::from("/path/to/kernel")), + ..Default::default() + }), + disks: None, + net: None, + rng: RngConfig { + src: PathBuf::from("/dev/urandom"), + iommu: false, + }, + balloon: None, + fs: None, + pmem: None, + serial: ConsoleConfig { + file: None, + mode: ConsoleOutputMode::Null, + sigwinch: false, + iommu: false, + }, + console: ConsoleConfig { + file: None, + mode: ConsoleOutputMode::Tty, + sigwinch: false, + iommu: false, + }, + devices: None, + user_devices: None, + vdpa: None, + vsock: None, + pvpanic: false, + iommu: false, + #[cfg(target_arch = "x86_64")] + sgx_epc: None, + numa: None, + watchdog: false, + #[cfg(feature = "guest_debug")] + gdb: false, + platform: None, + tpm: None, + sys_ctrl: false, + ivshmem: None, + }; + + assert_eq!(expected_vm_config, result_vm_config); + } + + #[test] + fn test_valid_vm_config_cpus() { + vec![ + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--cpus", + "boot=1", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "cpus": {"boot_vcpus": 1, "max_vcpus": 1} + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--cpus", + "boot=1,max=3", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "cpus": {"boot_vcpus": 1, "max_vcpus": 3} + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--cpus", + "boot=2,max=4", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "cpus": {"boot_vcpus": 1, "max_vcpus": 3} + }"#, + false, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_memory() { + vec![ + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel", "--memory", "size=1073741824"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory": {"size": 1073741824} + }"#, + true, + ), + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel", "--memory", "size=1G"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory": {"size": 1073741824} + }"#, + true, + ), + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel", "--memory", "size=1G,mergeable=on"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory": {"size": 1073741824, "mergeable": true} + }"#, + true, + ), + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel", "--memory", "size=1G,mergeable=off"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory": {"size": 1073741824, "mergeable": false} + }"#, + true, + ), + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel", "--memory", "size=1G,mergeable=on"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory": {"size": 1073741824, "mergeable": false} + }"#, + false, + ), + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel", "--memory", "size=1G,hotplug_size=1G"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory": {"size": 1073741824, "hotplug_method": "Acpi", "hotplug_size": 1073741824} + }"#, + true, + ), + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel", "--memory", "size=1G,hotplug_method=virtio-mem,hotplug_size=1G"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory": {"size": 1073741824, "hotplug_method": "VirtioMem", "hotplug_size": 1073741824} + }"#, + true, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_kernel() { + vec![( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel"], + r#"{ + "payload": {"kernel": "/path/to/kernel"} + }"#, + true, + )] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_cmdline() { + vec![( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--cmdline", + "arg1=foo arg2=bar", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel", "cmdline": "arg1=foo arg2=bar"} + }"#, + true, + )] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_disks() { + vec![ + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--disk", + "path=/path/to/disk/1", + "path=/path/to/disk/2", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "disks": [ + {"path": "/path/to/disk/1"}, + {"path": "/path/to/disk/2"} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--disk", + "path=/path/to/disk/1", + "path=/path/to/disk/2", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "disks": [ + {"path": "/path/to/disk/1"} + ] + }"#, + false, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--memory", + "shared=true", + "--disk", + "vhost_user=true,socket=/tmp/sock1", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory" : { "shared": true, "size": 536870912 }, + "disks": [ + {"vhost_user":true, "vhost_socket":"/tmp/sock1"} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--memory", + "shared=true", + "--disk", + "vhost_user=true,socket=/tmp/sock1", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory" : { "shared": true, "size": 536870912 }, + "disks": [ + {"vhost_user":true, "vhost_socket":"/tmp/sock1"} + ] + }"#, + true, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_net() { + vec![ + // This test is expected to fail because the default MAC address is + // randomly generated. There's no way we can have twice the same + // default value. + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel", "--net", "mac="], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "net": [] + }"#, + false, + ), + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel", "--net", "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd"} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--net", + "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,tap=tap0", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "tap": "tap0"} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--net", + "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,tap=tap0,ip=1.2.3.4", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "tap": "tap0", "ip": "1.2.3.4"} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--net", + "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,tap=tap0,ip=1.2.3.4,mask=5.6.7.8", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "tap": "tap0", "ip": "1.2.3.4", "mask": "5.6.7.8"} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--cpus", "boot=2", + "--net", + "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,tap=tap0,ip=1.2.3.4,mask=5.6.7.8,num_queues=4", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "cpus": {"boot_vcpus": 2, "max_vcpus": 2}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "tap": "tap0", "ip": "1.2.3.4", "mask": "5.6.7.8", "num_queues": 4} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--cpus", "boot=2", + "--net", + "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,tap=tap0,ip=1.2.3.4,mask=5.6.7.8,num_queues=4,queue_size=128", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "cpus": {"boot_vcpus": 2, "max_vcpus": 2}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "tap": "tap0", "ip": "1.2.3.4", "mask": "5.6.7.8", "num_queues": 4, "queue_size": 128} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--net", + "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,tap=tap0,ip=1.2.3.4,mask=5.6.7.8,num_queues=2,queue_size=256", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "tap": "tap0", "ip": "1.2.3.4", "mask": "5.6.7.8"} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--net", + "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,tap=tap0,ip=1.2.3.4,mask=5.6.7.8", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "tap": "tap0", "ip": "1.2.3.4", "mask": "5.6.7.8", "num_queues": 2, "queue_size": 256} + ] + }"#, + true, + ), + #[cfg(target_arch = "x86_64")] + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--net", + "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,tap=tap0,ip=1.2.3.4,mask=5.6.7.8,num_queues=2,queue_size=256,iommu=on", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "tap": "tap0", "ip": "1.2.3.4", "mask": "5.6.7.8", "num_queues": 2, "queue_size": 256, "iommu": true} + ] + }"#, + false, + ), + #[cfg(target_arch = "x86_64")] + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--net", + "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,tap=tap0,ip=1.2.3.4,mask=5.6.7.8,num_queues=2,queue_size=256,iommu=on", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "tap": "tap0", "ip": "1.2.3.4", "mask": "5.6.7.8", "num_queues": 2, "queue_size": 256, "iommu": true} + ], + "iommu": true + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--net", + "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,tap=tap0,ip=1.2.3.4,mask=5.6.7.8,num_queues=2,queue_size=256,iommu=off", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "tap": "tap0", "ip": "1.2.3.4", "mask": "5.6.7.8", "num_queues": 2, "queue_size": 256, "iommu": false} + ] + }"#, + true, + ), + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel", "--memory", "shared=true", "--net", "mac=12:34:56:78:90:ab,host_mac=34:56:78:90:ab:cd,vhost_user=true,socket=/tmp/sock"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory" : { "shared": true, "size": 536870912 }, + "net": [ + {"mac": "12:34:56:78:90:ab", "host_mac": "34:56:78:90:ab:cd", "vhost_user": true, "vhost_socket": "/tmp/sock"} + ] + }"#, + true, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_rng() { + vec![( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--rng", + "src=/path/to/entropy/source", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "rng": {"src": "/path/to/entropy/source"} + }"#, + true, + )] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_fs() { + vec![ + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--memory", "shared=true", + "--fs", + "tag=virtiofs1,socket=/path/to/sock1", + "tag=virtiofs2,socket=/path/to/sock2", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory" : { "shared": true, "size": 536870912 }, + "fs": [ + {"tag": "virtiofs1", "socket": "/path/to/sock1"}, + {"tag": "virtiofs2", "socket": "/path/to/sock2"} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--memory", "shared=true", + "--fs", + "tag=virtiofs1,socket=/path/to/sock1", + "tag=virtiofs2,socket=/path/to/sock2", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory" : { "shared": true, "size": 536870912 }, + "fs": [ + {"tag": "virtiofs1", "socket": "/path/to/sock1"} + ] + }"#, + false, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--memory", "shared=true", "--cpus", "boot=4", + "--fs", + "tag=virtiofs1,socket=/path/to/sock1,num_queues=4", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory" : { "shared": true, "size": 536870912 }, + "cpus": {"boot_vcpus": 4, "max_vcpus": 4}, + "fs": [ + {"tag": "virtiofs1", "socket": "/path/to/sock1", "num_queues": 4} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", "--kernel", "/path/to/kernel", + "--memory", "shared=true", "--cpus", "boot=4", + "--fs", + "tag=virtiofs1,socket=/path/to/sock1,num_queues=4,queue_size=128" + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "memory" : { "shared": true, "size": 536870912 }, + "cpus": {"boot_vcpus": 4, "max_vcpus": 4}, + "fs": [ + {"tag": "virtiofs1", "socket": "/path/to/sock1", "num_queues": 4, "queue_size": 128} + ] + }"#, + true, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_pmem() { + vec![ + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--pmem", + "file=/path/to/img/1,size=1G", + "file=/path/to/img/2,size=2G", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "pmem": [ + {"file": "/path/to/img/1", "size": 1073741824}, + {"file": "/path/to/img/2", "size": 2147483648} + ] + }"#, + true, + ), + #[cfg(target_arch = "x86_64")] + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--pmem", + "file=/path/to/img/1,size=1G,iommu=on", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "pmem": [ + {"file": "/path/to/img/1", "size": 1073741824, "iommu": true} + ], + "iommu": true + }"#, + true, + ), + #[cfg(target_arch = "x86_64")] + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--pmem", + "file=/path/to/img/1,size=1G,iommu=on", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "pmem": [ + {"file": "/path/to/img/1", "size": 1073741824, "iommu": true} + ] + }"#, + false, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_serial_console() { + vec![ + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "serial": {"mode": "Null"}, + "console": {"mode": "Tty"} + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--serial", + "null", + "--console", + "tty", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"} + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--serial", + "tty", + "--console", + "off", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "serial": {"mode": "Tty"}, + "console": {"mode": "Off"} + }"#, + true, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_serial_pty_console_pty() { + vec![ + ( + vec!["cloud-hypervisor", "--kernel", "/path/to/kernel"], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "serial": {"mode": "Null"}, + "console": {"mode": "Tty"} + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--serial", + "null", + "--console", + "tty", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"} + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--serial", + "pty", + "--console", + "pty", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "serial": {"mode": "Pty"}, + "console": {"mode": "Pty"} + }"#, + true, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + #[cfg(target_arch = "x86_64")] + fn test_valid_vm_config_devices() { + vec![ + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--device", + "path=/path/to/device/1", + "path=/path/to/device/2", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "devices": [ + {"path": "/path/to/device/1"}, + {"path": "/path/to/device/2"} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--device", + "path=/path/to/device/1", + "path=/path/to/device/2", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "devices": [ + {"path": "/path/to/device/1"} + ] + }"#, + false, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--device", + "path=/path/to/device,iommu=on", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "devices": [ + {"path": "/path/to/device", "iommu": true} + ], + "iommu": true + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--device", + "path=/path/to/device,iommu=on", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "devices": [ + {"path": "/path/to/device", "iommu": true} + ] + }"#, + false, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--device", + "path=/path/to/device,iommu=off", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "devices": [ + {"path": "/path/to/device", "iommu": false} + ] + }"#, + true, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_vdpa() { + vec![ + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--vdpa", + "path=/path/to/device/1", + "path=/path/to/device/2,num_queues=2", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "vdpa": [ + {"path": "/path/to/device/1", "num_queues": 1}, + {"path": "/path/to/device/2", "num_queues": 2} + ] + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--vdpa", + "path=/path/to/device/1", + "path=/path/to/device/2", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "vdpa": [ + {"path": "/path/to/device/1"} + ] + }"#, + false, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_vsock() { + vec![ + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--vsock", + "cid=123,socket=/path/to/sock/1", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "vsock": {"cid": 123, "socket": "/path/to/sock/1"} + }"#, + true, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--vsock", + "cid=124,socket=/path/to/sock/1", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "vsock": {"cid": 123, "socket": "/path/to/sock/1"} + }"#, + false, + ), + #[cfg(target_arch = "x86_64")] + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--vsock", + "cid=123,socket=/path/to/sock/1,iommu=on", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "vsock": {"cid": 123, "socket": "/path/to/sock/1", "iommu": true}, + "iommu": true + }"#, + true, + ), + #[cfg(target_arch = "x86_64")] + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--vsock", + "cid=123,socket=/path/to/sock/1,iommu=on", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "vsock": {"cid": 123, "socket": "/path/to/sock/1", "iommu": true} + }"#, + false, + ), + ( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--vsock", + "cid=123,socket=/path/to/sock/1,iommu=off", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "vsock": {"cid": 123, "socket": "/path/to/sock/1", "iommu": false} + }"#, + true, + ), + ] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } + + #[test] + fn test_valid_vm_config_tpm_socket() { + vec![( + vec![ + "cloud-hypervisor", + "--kernel", + "/path/to/kernel", + "--tpm", + "socket=/path/to/tpm/sock", + ], + r#"{ + "payload": {"kernel": "/path/to/kernel"}, + "tpm": {"socket": "/path/to/tpm/sock"} + }"#, + true, + )] + .iter() + .for_each(|(cli, openapi, equal)| { + compare_vm_config_cli_vs_json(cli, openapi, *equal); + }); + } +} diff --git a/vendor/cube-hypervisor/src/vmm_config.rs b/vendor/cube-hypervisor/src/vmm_config.rs new file mode 100644 index 0000000..c6db296 --- /dev/null +++ b/vendor/cube-hypervisor/src/vmm_config.rs @@ -0,0 +1,64 @@ +use std::os::fd::RawFd; +use std::path::PathBuf; +use std::sync::mpsc::Sender; + +use event_notifier::NotifyEvent; +use log::LevelFilter; +use seccompiler::SeccompAction; + +#[allow(dead_code)] +#[derive(Clone, Debug)] +pub struct VmmConfig { + pub core_dump: Option, + pub event_monitor: Option, + pub gdb: Option, + pub log_file: String, + pub log_json_file: Option, + pub log_level: LevelFilter, + pub log_stderr: bool, + pub sandbox_id: String, + pub seccomp: SeccompAction, + pub event_notifier: Option, + pub http_path: Option, +} + +impl Default for VmmConfig { + fn default() -> Self { + Self { + core_dump: None, + event_monitor: None, + gdb: None, + log_file: String::from(crate::common::DEFAULT_LOG_FILE), + log_json_file: Some(String::from(crate::common::DEFAULT_LOG_JSON_FILE)), + log_level: LevelFilter::Info, + log_stderr: false, + sandbox_id: String::from("cube-hypervisor"), + seccomp: SeccompAction::KillProcess, + event_notifier: None, + http_path: None, + } + } +} + +#[allow(dead_code)] +#[derive(Clone, Debug)] +pub struct GdbConfig { + path: PathBuf, +} + +#[derive(Clone, Debug)] +pub struct CoreDumpConfig { + pub filter: String, + pub limit: Option, +} + +#[derive(Clone, Debug)] +pub struct EventMonitorConfig { + pub path: Option, + pub fd: Option, +} + +#[derive(Clone, Debug)] +pub struct EventNotifyConfig { + pub notifier: Sender, +} diff --git a/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/meta-data b/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/meta-data new file mode 100644 index 0000000..bf86919 --- /dev/null +++ b/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/meta-data @@ -0,0 +1,2 @@ +instance-id: cloud +local-hostname: cloud diff --git a/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/network-config b/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/network-config new file mode 100644 index 0000000..ce5ed03 --- /dev/null +++ b/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/network-config @@ -0,0 +1,26 @@ +version: 2 +ethernets: + id0: + match: + macaddress: 12:34:56:78:90:ab + addresses: + - 192.168.2.2/24 + gateway4: 192.168.2.1 + id1: + match: + macaddress: de:ad:be:ef:12:34 + addresses: + - 192.168.2.3/24 + gateway4: 192.168.2.1 + id2: + match: + macaddress: de:ad:be:ef:34:56 + addresses: + - 192.168.2.4/24 + gateway4: 192.168.2.1 + id3: + match: + macaddress: de:ad:be:ef:56:78 + addresses: + - 192.168.2.5/24 + gateway4: 192.168.2.1 diff --git a/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/user-data b/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/user-data new file mode 100644 index 0000000..d2c7ac7 --- /dev/null +++ b/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/user-data @@ -0,0 +1,76 @@ +#cloud-config +users: + - name: cloud + passwd: $6$7125787751a8d18a$sHwGySomUA1PawiNFWVCKYQN.Ec.Wzz0JtPPL1MvzFrkwmop2dq7.4CYf03A5oemPQ4pOFCCrtCelvFBEle/K. + sudo: ALL=(ALL) NOPASSWD:ALL + lock_passwd: False + inactive: False + shell: /bin/bash + +ssh_pwauth: True + +runcmd: + - [ systemctl, daemon-reload] + - [ systemctl, enable, notify-booted.service] + - [ systemctl, start, --no-block, notify-booted.service ] + +write_files: + - + path: /etc/systemd/system/vfio.service + permissions: 0644 + content: | + [Unit] + Description=VFIO test systemd service + + [Service] + Type=simple + ExecStart=/bin/bash /usr/bin/cloud-hypervisor-vfio.sh + + [Install] + WantedBy=multi-user.target + + - + path: /usr/bin/cloud-hypervisor-vfio.sh + permissions: 0755 + content: | + #!/bin/bash + + mount /dev/vdc /mnt + bash -c "echo 1af4 1042 > /sys/bus/pci/drivers/vfio-pci/new_id" + bash -c "echo 0000:00:05.0 > /sys/bus/pci/devices/0000\:00\:05.0/driver/unbind" + bash -c "echo 0000:00:05.0 > /sys/bus/pci/drivers/vfio-pci/bind" + bash -c "echo 1af4 1041 > /sys/bus/pci/drivers/vfio-pci/new_id" + bash -c "echo 0000:00:07.0 > /sys/bus/pci/devices/0000\:00\:07.0/driver/unbind" + bash -c "echo 0000:00:07.0 > /sys/bus/pci/drivers/vfio-pci/bind" + bash -c "echo 0000:00:08.0 > /sys/bus/pci/devices/0000\:00\:08.0/driver/unbind" + bash -c "echo 0000:00:08.0 > /sys/bus/pci/drivers/vfio-pci/bind" + # 1G ram requires 512 pages + echo 512 | sudo tee /proc/sys/vm/nr_hugepages + sudo chmod a+rwX /dev/hugepages + /mnt/cloud-hypervisor --kernel /mnt/vmlinux --cmdline "console=hvc0 reboot=k panic=1 nomodules root=/dev/vda1 VFIOTAG" --disk path=/mnt/focal-server-cloudimg-amd64-custom-20210609-0.raw path=/mnt/cloudinit.img --cpus boot=1 --memory size=512M,hotplug_size=1G,hugepages=on --device path=/sys/bus/pci/devices/0000:00:05.0/ path=/sys/bus/pci/devices/0000:00:07.0/ path=/sys/bus/pci/devices/0000:00:08.0/ --api-socket /tmp/ch_api.sock + + - + path: /etc/systemd/system/notify-booted.service + permissions: 0644 + content: | + [Unit] + Description=Notify the tcp listener on the host that the guest is booted + After=sshd.service + + [Service] + Type=simple + ExecStart=/usr/bin/cloud-hypervisor-notify-booted.sh + Restart=on-failure + RestartSec=2 + + [Install] + WantedBy=multi-user.target + + - + path: /usr/bin/cloud-hypervisor-notify-booted.sh + permissions: 0755 + content: | + #!/bin/bash + set -e + + echo -n "@DEFAULT_TCP_LISTENER_MESSAGE" > /dev/tcp/@HOST_IP/@TCP_LISTENER_PORT diff --git a/vendor/cube-hypervisor/test_infra/Cargo.toml b/vendor/cube-hypervisor/test_infra/Cargo.toml new file mode 100644 index 0000000..d5cc613 --- /dev/null +++ b/vendor/cube-hypervisor/test_infra/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "test_infra" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[dependencies] +dirs = "4.0.0" +epoll = "4.3.1" +libc = "0.2.137" +once_cell = "1.16.0" +serde = { workspace = true, features = ["rc", "derive"] } +serde_json = "1.0.87" +ssh2 = { version = "0.9.3", features = ["vendored-openssl"] } +vmm-sys-util = "0.12.1" +wait-timeout = "0.2.0" diff --git a/vendor/cube-hypervisor/test_infra/src/lib.rs b/vendor/cube-hypervisor/test_infra/src/lib.rs new file mode 100644 index 0000000..7ea36a5 --- /dev/null +++ b/vendor/cube-hypervisor/test_infra/src/lib.rs @@ -0,0 +1,1821 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#![allow(clippy::undocumented_unsafe_blocks)] + +use std::env; +use std::ffi::OsStr; +use std::io::{self, BufRead, BufReader, Read, Write}; +use std::net::TcpListener; +use std::net::TcpStream; +use std::os::unix::{ + fs::PermissionsExt, + io::{AsRawFd, FromRawFd}, + net::UnixStream, +}; +use std::path::Path; +use std::process::{Child, Command, ExitStatus, Output, Stdio}; +use std::str::FromStr; +use std::sync::Mutex; +use std::thread; +use std::time::Duration; +use std::{fmt, fs}; + +use once_cell::sync::Lazy; +use serde_json::Value; +use ssh2::Session; +use vmm_sys_util::sock_ctrl_msg::ScmSocket; +use vmm_sys_util::tempdir::TempDir; +use wait_timeout::ChildExt; + +#[derive(Debug)] +pub enum WaitTimeoutError { + Timedout, + ExitStatus, + General(std::io::Error), +} + +#[derive(Debug)] +pub enum Error { + Parsing(std::num::ParseIntError), + SshCommand(SshCommandError), + WaitForBoot(WaitForBootError), + EthrLogFile(std::io::Error), + EthrLogParse, + FioOutputParse, + Iperf3Parse, + Spawn(std::io::Error), + WaitTimeout(WaitTimeoutError), +} + +impl From for Error { + fn from(e: SshCommandError) -> Self { + Self::SshCommand(e) + } +} + +pub struct GuestNetworkConfig { + pub guest_ip: String, + pub l2_guest_ip1: String, + pub l2_guest_ip2: String, + pub l2_guest_ip3: String, + pub host_ip: String, + pub guest_mac: String, + pub l2_guest_mac1: String, + pub l2_guest_mac2: String, + pub l2_guest_mac3: String, + pub tcp_listener_port: u16, +} + +pub const DEFAULT_TCP_LISTENER_MESSAGE: &str = "booted"; +pub const DEFAULT_TCP_LISTENER_PORT: u16 = 8000; +pub const DEFAULT_TCP_LISTENER_TIMEOUT: i32 = 120; + +#[derive(Debug)] +pub enum WaitForBootError { + EpollWait(std::io::Error), + Listen(std::io::Error), + EpollWaitTimeout, + WrongGuestAddr, + Accept(std::io::Error), +} + +impl GuestNetworkConfig { + pub fn wait_vm_boot(&self, custom_timeout: Option) -> Result<(), WaitForBootError> { + let start = std::time::Instant::now(); + // The 'port' is unique per 'GUEST' and listening to wild-card ip avoids retrying on 'TcpListener::bind()' + let listen_addr = format!("0.0.0.0:{}", self.tcp_listener_port); + let expected_guest_addr = self.guest_ip.as_str(); + let mut s = String::new(); + let timeout = match custom_timeout { + Some(t) => t, + None => DEFAULT_TCP_LISTENER_TIMEOUT, + }; + + match (|| -> Result<(), WaitForBootError> { + let listener = + TcpListener::bind(listen_addr.as_str()).map_err(WaitForBootError::Listen)?; + listener + .set_nonblocking(true) + .expect("Cannot set non-blocking for tcp listener"); + + // Reply on epoll w/ timeout to wait for guest connections faithfully + let epoll_fd = epoll::create(true).expect("Cannot create epoll fd"); + // Use 'File' to enforce closing on 'epoll_fd' + let _epoll_file = unsafe { fs::File::from_raw_fd(epoll_fd) }; + epoll::ctl( + epoll_fd, + epoll::ControlOptions::EPOLL_CTL_ADD, + listener.as_raw_fd(), + epoll::Event::new(epoll::Events::EPOLLIN, 0), + ) + .expect("Cannot add 'tcp_listener' event to epoll"); + let mut events = vec![epoll::Event::new(epoll::Events::empty(), 0); 1]; + loop { + let num_events = match epoll::wait(epoll_fd, timeout * 1000_i32, &mut events[..]) { + Ok(num_events) => Ok(num_events), + Err(e) => match e.raw_os_error() { + Some(libc::EAGAIN) | Some(libc::EINTR) => continue, + _ => Err(e), + }, + } + .map_err(WaitForBootError::EpollWait)?; + if num_events == 0 { + return Err(WaitForBootError::EpollWaitTimeout); + } + break; + } + + match listener.accept() { + Ok((_, addr)) => { + // Make sure the connection is from the expected 'guest_addr' + if addr.ip() != std::net::IpAddr::from_str(expected_guest_addr).unwrap() { + s = format!( + "Expecting the guest ip '{}' while being connected with ip '{}'", + expected_guest_addr, + addr.ip() + ); + return Err(WaitForBootError::WrongGuestAddr); + } + + Ok(()) + } + Err(e) => { + s = "TcpListener::accept() failed".to_string(); + Err(WaitForBootError::Accept(e)) + } + } + })() { + Err(e) => { + let duration = start.elapsed(); + eprintln!( + "\n\n==== Start 'wait_vm_boot' (FAILED) ====\n\n\ + duration =\"{:?}, timeout = {}s\"\n\ + listen_addr=\"{}\"\n\ + expected_guest_addr=\"{}\"\n\ + message=\"{}\"\n\ + error=\"{:?}\"\n\ + \n==== End 'wait_vm_boot' outout ====\n\n", + duration, timeout, listen_addr, expected_guest_addr, s, e + ); + + Err(e) + } + Ok(_) => Ok(()), + } + } +} + +pub enum DiskType { + OperatingSystem, + CloudInit, +} + +pub trait DiskConfig { + fn prepare_files(&mut self, tmp_dir: &TempDir, network: &GuestNetworkConfig); + fn prepare_cloudinit(&self, tmp_dir: &TempDir, network: &GuestNetworkConfig) -> String; + fn disk(&self, disk_type: DiskType) -> Option; +} + +#[derive(Clone)] +pub struct UbuntuDiskConfig { + osdisk_path: String, + cloudinit_path: String, + image_name: String, +} + +impl UbuntuDiskConfig { + pub fn new(image_name: String) -> Self { + UbuntuDiskConfig { + image_name, + osdisk_path: String::new(), + cloudinit_path: String::new(), + } + } +} + +pub struct WindowsDiskConfig { + image_name: String, + osdisk_path: String, + loopback_device: String, + windows_snapshot_cow: String, + windows_snapshot: String, +} + +impl WindowsDiskConfig { + pub fn new(image_name: String) -> Self { + WindowsDiskConfig { + image_name, + osdisk_path: String::new(), + loopback_device: String::new(), + windows_snapshot_cow: String::new(), + windows_snapshot: String::new(), + } + } +} + +impl Drop for WindowsDiskConfig { + fn drop(&mut self) { + // dmsetup remove windows-snapshot-1 + std::process::Command::new("dmsetup") + .arg("remove") + .arg(self.windows_snapshot.as_str()) + .output() + .expect("Expect removing Windows snapshot with 'dmsetup' to succeed"); + + // dmsetup remove windows-snapshot-cow-1 + std::process::Command::new("dmsetup") + .arg("remove") + .arg(self.windows_snapshot_cow.as_str()) + .output() + .expect("Expect removing Windows snapshot CoW with 'dmsetup' to succeed"); + + // losetup -d + std::process::Command::new("losetup") + .args(["-d", self.loopback_device.as_str()]) + .output() + .expect("Expect removing loopback device to succeed"); + } +} + +impl DiskConfig for UbuntuDiskConfig { + fn prepare_cloudinit(&self, tmp_dir: &TempDir, network: &GuestNetworkConfig) -> String { + let cloudinit_file_path = + String::from(tmp_dir.as_path().join("cloudinit").to_str().unwrap()); + + let cloud_init_directory = tmp_dir.as_path().join("cloud-init").join("ubuntu"); + + fs::create_dir_all(&cloud_init_directory) + .expect("Expect creating cloud-init directory to succeed"); + + let source_file_dir = std::env::current_dir() + .unwrap() + .join("test_data") + .join("cloud-init") + .join("ubuntu"); + + vec!["meta-data"].iter().for_each(|x| { + rate_limited_copy(source_file_dir.join(x), cloud_init_directory.join(x)) + .expect("Expect copying cloud-init meta-data to succeed"); + }); + + let mut user_data_string = String::new(); + fs::File::open(source_file_dir.join("user-data")) + .unwrap() + .read_to_string(&mut user_data_string) + .expect("Expected reading user-data file in to succeed"); + user_data_string = user_data_string.replace( + "@DEFAULT_TCP_LISTENER_MESSAGE", + DEFAULT_TCP_LISTENER_MESSAGE, + ); + user_data_string = user_data_string.replace("@HOST_IP", &network.host_ip); + user_data_string = + user_data_string.replace("@TCP_LISTENER_PORT", &network.tcp_listener_port.to_string()); + + fs::File::create(cloud_init_directory.join("user-data")) + .unwrap() + .write_all(user_data_string.as_bytes()) + .expect("Expected writing out user-data to succeed"); + + let mut network_config_string = String::new(); + + fs::File::open(source_file_dir.join("network-config")) + .unwrap() + .read_to_string(&mut network_config_string) + .expect("Expected reading network-config file in to succeed"); + + network_config_string = network_config_string.replace("192.168.2.1", &network.host_ip); + network_config_string = network_config_string.replace("192.168.2.2", &network.guest_ip); + network_config_string = network_config_string.replace("192.168.2.3", &network.l2_guest_ip1); + network_config_string = network_config_string.replace("192.168.2.4", &network.l2_guest_ip2); + network_config_string = network_config_string.replace("192.168.2.5", &network.l2_guest_ip3); + network_config_string = + network_config_string.replace("12:34:56:78:90:ab", &network.guest_mac); + network_config_string = + network_config_string.replace("de:ad:be:ef:12:34", &network.l2_guest_mac1); + network_config_string = + network_config_string.replace("de:ad:be:ef:34:56", &network.l2_guest_mac2); + network_config_string = + network_config_string.replace("de:ad:be:ef:56:78", &network.l2_guest_mac3); + + fs::File::create(cloud_init_directory.join("network-config")) + .unwrap() + .write_all(network_config_string.as_bytes()) + .expect("Expected writing out network-config to succeed"); + + std::process::Command::new("mkdosfs") + .args(["-n", "cidata"]) + .args(["-C", cloudinit_file_path.as_str()]) + .arg("8192") + .output() + .expect("Expect creating disk image to succeed"); + + vec!["user-data", "meta-data", "network-config"] + .iter() + .for_each(|x| { + std::process::Command::new("mcopy") + .arg("-o") + .args(["-i", cloudinit_file_path.as_str()]) + .args(["-s", cloud_init_directory.join(x).to_str().unwrap(), "::"]) + .output() + .expect("Expect copying files to disk image to succeed"); + }); + + cloudinit_file_path + } + + fn prepare_files(&mut self, tmp_dir: &TempDir, network: &GuestNetworkConfig) { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut osdisk_base_path = workload_path; + osdisk_base_path.push(&self.image_name); + + let osdisk_path = String::from(tmp_dir.as_path().join("osdisk.img").to_str().unwrap()); + let cloudinit_path = self.prepare_cloudinit(tmp_dir, network); + + rate_limited_copy(osdisk_base_path, &osdisk_path) + .expect("copying of OS source disk image failed"); + + self.cloudinit_path = cloudinit_path; + self.osdisk_path = osdisk_path; + } + + fn disk(&self, disk_type: DiskType) -> Option { + match disk_type { + DiskType::OperatingSystem => Some(self.osdisk_path.clone()), + DiskType::CloudInit => Some(self.cloudinit_path.clone()), + } + } +} + +impl DiskConfig for WindowsDiskConfig { + fn prepare_cloudinit(&self, _tmp_dir: &TempDir, _network: &GuestNetworkConfig) -> String { + String::new() + } + + fn prepare_files(&mut self, tmp_dir: &TempDir, _network: &GuestNetworkConfig) { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut osdisk_path = workload_path; + osdisk_path.push(&self.image_name); + + let osdisk_blk_size = fs::metadata(osdisk_path) + .expect("Expect retrieving Windows image metadata") + .len() + >> 9; + + let snapshot_cow_path = + String::from(tmp_dir.as_path().join("snapshot_cow").to_str().unwrap()); + + // Create and truncate CoW file for device mapper + let cow_file_size: u64 = 1 << 30; + let cow_file_blk_size = cow_file_size >> 9; + let cow_file = std::fs::File::create(snapshot_cow_path.as_str()) + .expect("Expect creating CoW image to succeed"); + cow_file + .set_len(cow_file_size) + .expect("Expect truncating CoW image to succeed"); + + // losetup --find --show /tmp/snapshot_cow + let loopback_device = std::process::Command::new("losetup") + .arg("--find") + .arg("--show") + .arg(snapshot_cow_path.as_str()) + .output() + .expect("Expect creating loopback device from snapshot CoW image to succeed"); + + self.loopback_device = String::from_utf8_lossy(&loopback_device.stdout) + .trim() + .to_string(); + + let random_extension = tmp_dir.as_path().file_name().unwrap(); + let windows_snapshot_cow = format!( + "windows-snapshot-cow-{}", + random_extension.to_str().unwrap() + ); + + // dmsetup create windows-snapshot-cow-1 --table '0 2097152 linear /dev/loop1 0' + std::process::Command::new("dmsetup") + .arg("create") + .arg(windows_snapshot_cow.as_str()) + .args([ + "--table", + format!("0 {} linear {} 0", cow_file_blk_size, self.loopback_device).as_str(), + ]) + .output() + .expect("Expect creating Windows snapshot CoW with 'dmsetup' to succeed"); + + let windows_snapshot = format!("windows-snapshot-{}", random_extension.to_str().unwrap()); + + // dmsetup mknodes + std::process::Command::new("dmsetup") + .arg("mknodes") + .output() + .expect("Expect device mapper nodes to be ready"); + + // dmsetup create windows-snapshot-1 --table '0 41943040 snapshot /dev/mapper/windows-base /dev/mapper/windows-snapshot-cow-1 P 8' + std::process::Command::new("dmsetup") + .arg("create") + .arg(windows_snapshot.as_str()) + .args([ + "--table", + format!( + "0 {} snapshot /dev/mapper/windows-base /dev/mapper/{} P 8", + osdisk_blk_size, + windows_snapshot_cow.as_str() + ) + .as_str(), + ]) + .output() + .expect("Expect creating Windows snapshot with 'dmsetup' to succeed"); + + // dmsetup mknodes + std::process::Command::new("dmsetup") + .arg("mknodes") + .output() + .expect("Expect device mapper nodes to be ready"); + + self.osdisk_path = format!("/dev/mapper/{}", windows_snapshot); + self.windows_snapshot_cow = windows_snapshot_cow; + self.windows_snapshot = windows_snapshot; + } + + fn disk(&self, disk_type: DiskType) -> Option { + match disk_type { + DiskType::OperatingSystem => Some(self.osdisk_path.clone()), + DiskType::CloudInit => None, + } + } +} + +pub fn rate_limited_copy, Q: AsRef>(from: P, to: Q) -> io::Result { + for i in 0..2 { + let free_bytes = unsafe { + let mut stats = std::mem::MaybeUninit::zeroed(); + let fs_name = std::ffi::CString::new("/tmp").unwrap(); + libc::statvfs(fs_name.as_ptr(), stats.as_mut_ptr()); + + let free_blocks = stats.assume_init().f_bfree; + let block_size = stats.assume_init().f_bsize; + + free_blocks * block_size + }; + + // Make sure there is at least 6 GiB of space + if free_bytes < 4 << 30 { + eprintln!( + "Not enough space on disk ({}). Attempt {} of 10. Sleeping.", + free_bytes, i + ); + thread::sleep(std::time::Duration::new(30, 0)); + continue; + } + + match fs::copy(&from, &to) { + Err(e) => { + if let Some(errno) = e.raw_os_error() { + if errno == libc::ENOSPC { + eprintln!("Copy returned ENOSPC. Attempt {} of 2. Sleeping.", i); + thread::sleep(std::time::Duration::new(30, 0)); + continue; + } + } + return Err(e); + } + Ok(i) => return Ok(i), + } + } + Err(io::Error::last_os_error()) +} + +pub fn handle_child_output( + r: Result<(), std::boxed::Box>, + output: &std::process::Output, +) { + use std::os::unix::process::ExitStatusExt; + if r.is_ok() && output.status.success() { + return; + } + + match output.status.code() { + None => { + // Don't treat child.kill() as a problem + if output.status.signal() == Some(9) && r.is_ok() { + return; + } + + eprintln!( + "==== child killed by signal: {} ====", + output.status.signal().unwrap() + ); + } + Some(code) => { + eprintln!("\n\n==== child exit code: {} ====", code); + } + } + + eprintln!( + "\n\n==== Start child stdout ====\n\n{}\n\n==== End child stdout ====", + String::from_utf8_lossy(&output.stdout) + ); + eprintln!( + "\n\n==== Start child stderr ====\n\n{}\n\n==== End child stderr ====", + String::from_utf8_lossy(&output.stderr) + ); + + panic!("Test failed") +} + +#[derive(Debug)] +pub struct PasswordAuth { + pub username: String, + pub password: String, +} + +pub const DEFAULT_SSH_RETRIES: u8 = 6; +pub const DEFAULT_SSH_TIMEOUT: u8 = 10; + +#[derive(Debug)] +pub enum SshCommandError { + Connection(std::io::Error), + Handshake(ssh2::Error), + Authentication(ssh2::Error), + ChannelSession(ssh2::Error), + Command(ssh2::Error), + ExitStatus(ssh2::Error), + NonZeroExitStatus(i32), + FileRead(std::io::Error), + FileMetadata(std::io::Error), + ScpSend(ssh2::Error), + WriteAll(std::io::Error), + SendEof(ssh2::Error), + WaitEof(ssh2::Error), +} + +fn scp_to_guest_with_auth( + path: &Path, + remote_path: &Path, + auth: &PasswordAuth, + ip: &str, + retries: u8, + timeout: u8, +) -> Result<(), SshCommandError> { + let mut counter = 0; + loop { + match (|| -> Result<(), SshCommandError> { + let tcp = + TcpStream::connect(format!("{}:22", ip)).map_err(SshCommandError::Connection)?; + let mut sess = Session::new().unwrap(); + sess.set_tcp_stream(tcp); + sess.handshake().map_err(SshCommandError::Handshake)?; + + sess.userauth_password(&auth.username, &auth.password) + .map_err(SshCommandError::Authentication)?; + assert!(sess.authenticated()); + + let content = fs::read(path).map_err(SshCommandError::FileRead)?; + let mode = fs::metadata(path) + .map_err(SshCommandError::FileMetadata)? + .permissions() + .mode() + & 0o777; + + let mut channel = sess + .scp_send(remote_path, mode as i32, content.len() as u64, None) + .map_err(SshCommandError::ScpSend)?; + channel + .write_all(&content) + .map_err(SshCommandError::WriteAll)?; + channel.send_eof().map_err(SshCommandError::SendEof)?; + channel.wait_eof().map_err(SshCommandError::WaitEof)?; + + // Intentionally ignore these results here as their failure + // does not precipitate a repeat + let _ = channel.close(); + let _ = channel.wait_close(); + + Ok(()) + })() { + Ok(_) => break, + Err(e) => { + counter += 1; + if counter >= retries { + eprintln!( + "\n\n==== Start scp command output (FAILED) ====\n\n\ + path =\"{:?}\"\n\ + remote_path =\"{:?}\"\n\ + auth=\"{:#?}\"\n\ + ip=\"{}\"\n\ + error=\"{:?}\"\n\ + \n==== End scp command outout ====\n\n", + path, remote_path, auth, ip, e + ); + + return Err(e); + } + } + }; + thread::sleep(std::time::Duration::new((timeout * counter).into(), 0)); + } + Ok(()) +} + +pub fn scp_to_guest( + path: &Path, + remote_path: &Path, + ip: &str, + retries: u8, + timeout: u8, +) -> Result<(), SshCommandError> { + scp_to_guest_with_auth( + path, + remote_path, + &PasswordAuth { + username: String::from("cloud"), + password: String::from("cloud123"), + }, + ip, + retries, + timeout, + ) +} + +pub fn ssh_command_ip_with_auth( + command: &str, + auth: &PasswordAuth, + ip: &str, + retries: u8, + timeout: u8, +) -> Result { + let mut s = String::new(); + let mut e = String::new(); + + let mut counter = 0; + loop { + // Reset captured stdout/stderr for each retry attempt so we only + // report the output of the final (failed) attempt. + s.clear(); + e.clear(); + + let mut closure = || -> Result<(), SshCommandError> { + let tcp = + TcpStream::connect(format!("{}:22", ip)).map_err(SshCommandError::Connection)?; + let mut sess = Session::new().unwrap(); + sess.set_tcp_stream(tcp); + sess.handshake().map_err(SshCommandError::Handshake)?; + + sess.userauth_password(&auth.username, &auth.password) + .map_err(SshCommandError::Authentication)?; + assert!(sess.authenticated()); + + let mut channel = sess + .channel_session() + .map_err(SshCommandError::ChannelSession)?; + channel.exec(command).map_err(SshCommandError::Command)?; + + // Intentionally ignore these results here as their failure + // does not precipitate a repeat + let _ = channel.read_to_string(&mut s); + let _ = channel.stderr().read_to_string(&mut e); + let _ = channel.close(); + let _ = channel.wait_close(); + + let status = channel.exit_status().map_err(SshCommandError::ExitStatus)?; + + if status != 0 { + Err(SshCommandError::NonZeroExitStatus(status)) + } else { + Ok(()) + } + }; + + match closure() { + Ok(_) => break, + Err(err) => { + counter += 1; + if counter >= retries { + eprintln!( + "\n\n==== Start ssh command output (FAILED) ====\n\n\ + command=\"{}\"\n\ + auth=\"{:#?}\"\n\ + ip=\"{}\"\n\ + stdout=\"{}\"\n\ + stderr=\"{}\"\n\ + error=\"{:?}\"\n\ + \n==== End ssh command outout ====\n\n", + command, auth, ip, s, e, err + ); + + return Err(err); + } + } + }; + thread::sleep(std::time::Duration::new((timeout * counter).into(), 0)); + } + Ok(s) +} + +pub fn ssh_command_ip( + command: &str, + ip: &str, + retries: u8, + timeout: u8, +) -> Result { + ssh_command_ip_with_auth( + command, + &PasswordAuth { + username: String::from("cloud"), + password: String::from("cloud123"), + }, + ip, + retries, + timeout, + ) +} + +pub fn exec_host_command_status(command: &str) -> ExitStatus { + std::process::Command::new("bash") + .args(["-c", command]) + .status() + .unwrap_or_else(|_| panic!("Expected '{}' to run", command)) +} + +pub fn exec_host_command_output(command: &str) -> Output { + std::process::Command::new("bash") + .args(["-c", command]) + .output() + .unwrap_or_else(|_| panic!("Expected '{}' to run", command)) +} + +pub fn kill_child(child: &mut Child) { + let r = unsafe { libc::kill(child.id() as i32, libc::SIGTERM) }; + if r != 0 { + let e = io::Error::last_os_error(); + if e.raw_os_error().unwrap() == libc::ESRCH { + return; + } + eprintln!("Failed to kill child with SIGTERM: {e:?}"); + } + + // The timeout period elapsed without the child exiting + if child.wait_timeout(Duration::new(5, 0)).unwrap().is_none() { + let _ = child.kill(); + } +} + +pub const PIPE_SIZE: i32 = 32 << 20; + +static NEXT_VM_ID: Lazy> = Lazy::new(|| Mutex::new(1)); + +pub struct Guest { + pub tmp_dir: TempDir, + pub disk_config: Box, + pub network: GuestNetworkConfig, +} + +// Safe to implement as we know we have no interior mutability +impl std::panic::RefUnwindSafe for Guest {} + +impl Drop for Guest { + fn drop(&mut self) { + let tmp_path = self.tmp_dir.as_path().to_path_buf(); + // Explicitly remove the temp directory to ensure cleanup even in panic scenarios. + // TempDir's own Drop also does this, but we add logging for better observability. + match self.tmp_dir.remove() { + Ok(_) => eprintln!("Successfully cleaned up guest temp dir: {:?}", tmp_path), + Err(e) => eprintln!( + "Warning: failed to clean up guest temp dir {:?}: {:?}", + tmp_path, e + ), + } + } +} + +impl Guest { + pub fn new_from_ip_range(mut disk_config: Box, class: &str, id: u8) -> Self { + let tmp_dir = TempDir::new_with_prefix("/tmp/ch").unwrap(); + + let network = GuestNetworkConfig { + guest_ip: format!("{}.{}.2", class, id), + l2_guest_ip1: format!("{}.{}.3", class, id), + l2_guest_ip2: format!("{}.{}.4", class, id), + l2_guest_ip3: format!("{}.{}.5", class, id), + host_ip: format!("{}.{}.1", class, id), + guest_mac: format!("12:34:56:78:90:{:02x}", id), + l2_guest_mac1: format!("de:ad:be:ef:12:{:02x}", id), + l2_guest_mac2: format!("de:ad:be:ef:34:{:02x}", id), + l2_guest_mac3: format!("de:ad:be:ef:56:{:02x}", id), + tcp_listener_port: DEFAULT_TCP_LISTENER_PORT + id as u16, + }; + + disk_config.prepare_files(&tmp_dir, &network); + + Guest { + tmp_dir, + disk_config, + network, + } + } + + pub fn new(disk_config: Box) -> Self { + let mut guard = NEXT_VM_ID.lock().unwrap(); + let id = *guard; + *guard = id + 1; + + Self::new_from_ip_range(disk_config, "192.168", id) + } + + pub fn default_net_string(&self) -> String { + format!( + "tap=,mac={},ip={},mask=255.255.255.0", + self.network.guest_mac, self.network.host_ip + ) + } + + pub fn default_net_string_w_iommu(&self) -> String { + format!( + "tap=,mac={},ip={},mask=255.255.255.0,iommu=on", + self.network.guest_mac, self.network.host_ip + ) + } + + pub fn default_net_string_w_mtu(&self, mtu: u16) -> String { + format!( + "tap=,mac={},ip={},mask=255.255.255.0,mtu={}", + self.network.guest_mac, self.network.host_ip, mtu + ) + } + + pub fn ssh_command(&self, command: &str) -> Result { + ssh_command_ip( + command, + &self.network.guest_ip, + DEFAULT_SSH_RETRIES, + DEFAULT_SSH_TIMEOUT, + ) + } + + #[cfg(target_arch = "x86_64")] + pub fn ssh_command_l1(&self, command: &str) -> Result { + ssh_command_ip( + command, + &self.network.guest_ip, + DEFAULT_SSH_RETRIES, + DEFAULT_SSH_TIMEOUT, + ) + } + + #[cfg(target_arch = "x86_64")] + pub fn ssh_command_l2_1(&self, command: &str) -> Result { + ssh_command_ip( + command, + &self.network.l2_guest_ip1, + DEFAULT_SSH_RETRIES, + DEFAULT_SSH_TIMEOUT, + ) + } + + #[cfg(target_arch = "x86_64")] + pub fn ssh_command_l2_2(&self, command: &str) -> Result { + ssh_command_ip( + command, + &self.network.l2_guest_ip2, + DEFAULT_SSH_RETRIES, + DEFAULT_SSH_TIMEOUT, + ) + } + + #[cfg(target_arch = "x86_64")] + pub fn ssh_command_l2_3(&self, command: &str) -> Result { + ssh_command_ip( + command, + &self.network.l2_guest_ip3, + DEFAULT_SSH_RETRIES, + DEFAULT_SSH_TIMEOUT, + ) + } + + pub fn api_create_body(&self, cpu_count: u8, kernel_path: &str, kernel_cmd: &str) -> String { + format! {"{{\"cpus\":{{\"boot_vcpus\":{},\"max_vcpus\":{}}},\"payload\":{{\"kernel\":\"{}\",\"cmdline\": \"{}\"}},\"net\":[{{\"ip\":\"{}\", \"mask\":\"255.255.255.0\", \"mac\":\"{}\"}}], \"disks\":[{{\"path\":\"{}\"}}, {{\"path\":\"{}\"}}]}}", + cpu_count, + cpu_count, + kernel_path, + kernel_cmd, + self.network.host_ip, + self.network.guest_mac, + self.disk_config.disk(DiskType::OperatingSystem).unwrap().as_str(), + self.disk_config.disk(DiskType::CloudInit).unwrap().as_str(), + } + } + + pub fn get_cpu_count(&self) -> Result { + self.ssh_command("grep -c processor /proc/cpuinfo")? + .trim() + .parse() + .map_err(Error::Parsing) + } + + #[cfg(target_arch = "x86_64")] + pub fn get_initial_apicid(&self) -> Result { + self.ssh_command("grep \"initial apicid\" /proc/cpuinfo | grep -o \"[0-9]*\"")? + .trim() + .parse() + .map_err(Error::Parsing) + } + + pub fn get_total_memory(&self) -> Result { + self.ssh_command("grep MemTotal /proc/meminfo | grep -o \"[0-9]*\"")? + .trim() + .parse() + .map_err(Error::Parsing) + } + + #[cfg(target_arch = "x86_64")] + pub fn get_total_memory_l2(&self) -> Result { + self.ssh_command_l2_1("grep MemTotal /proc/meminfo | grep -o \"[0-9]*\"")? + .trim() + .parse() + .map_err(Error::Parsing) + } + + pub fn get_numa_node_memory(&self, node_id: usize) -> Result { + self.ssh_command( + format!( + "grep MemTotal /sys/devices/system/node/node{}/meminfo \ + | cut -d \":\" -f 2 | grep -o \"[0-9]*\"", + node_id + ) + .as_str(), + )? + .trim() + .parse() + .map_err(Error::Parsing) + } + + pub fn wait_vm_boot(&self, custom_timeout: Option) -> Result<(), Error> { + self.network + .wait_vm_boot(custom_timeout) + .map_err(Error::WaitForBoot) + } + + pub fn check_numa_node_cpus(&self, node_id: usize, cpus: Vec) -> Result<(), Error> { + for cpu in cpus.iter() { + let cmd = format!( + "[ -d \"/sys/devices/system/node/node{}/cpu{}\" ]", + node_id, cpu + ); + self.ssh_command(cmd.as_str())?; + } + + Ok(()) + } + + pub fn check_numa_node_distances( + &self, + node_id: usize, + distances: &str, + ) -> Result { + let cmd = format!("cat /sys/devices/system/node/node{}/distance", node_id); + if self.ssh_command(cmd.as_str())?.trim() == distances { + Ok(true) + } else { + Ok(false) + } + } + + pub fn check_numa_common( + &self, + mem_ref: Option<&[u32]>, + node_ref: Option<&[Vec]>, + distance_ref: Option<&[&str]>, + ) { + if let Some(mem_ref) = mem_ref { + // Check each NUMA node has been assigned the right amount of + // memory. + for (i, &m) in mem_ref.iter().enumerate() { + assert!(self.get_numa_node_memory(i).unwrap_or_default() > m); + } + } + + if let Some(node_ref) = node_ref { + // Check each NUMA node has been assigned the right CPUs set. + for (i, n) in node_ref.iter().enumerate() { + self.check_numa_node_cpus(i, n.clone()).unwrap(); + } + } + + if let Some(distance_ref) = distance_ref { + // Check each NUMA node has been assigned the right distances. + for (i, &d) in distance_ref.iter().enumerate() { + assert!(self.check_numa_node_distances(i, d).unwrap()); + } + } + } + + #[cfg(target_arch = "x86_64")] + pub fn check_sgx_support(&self) -> Result<(), Error> { + self.ssh_command( + "cpuid -l 0x7 -s 0 | tr -s [:space:] | grep -q 'SGX: \ + Software Guard Extensions supported = true'", + )?; + self.ssh_command( + "cpuid -l 0x7 -s 0 | tr -s [:space:] | grep -q 'SGX_LC: \ + SGX launch config supported = true'", + )?; + self.ssh_command( + "cpuid -l 0x12 -s 0 | tr -s [:space:] | grep -q 'SGX1 \ + supported = true'", + )?; + + Ok(()) + } + + pub fn get_pci_bridge_class(&self) -> Result { + Ok(self + .ssh_command("cat /sys/bus/pci/devices/0000:00:00.0/class")? + .trim() + .to_string()) + } + + pub fn get_pci_device_ids(&self) -> Result { + Ok(self + .ssh_command("cat /sys/bus/pci/devices/*/device")? + .trim() + .to_string()) + } + + pub fn get_pci_vendor_ids(&self) -> Result { + Ok(self + .ssh_command("cat /sys/bus/pci/devices/*/vendor")? + .trim() + .to_string()) + } + + pub fn does_device_vendor_pair_match( + &self, + device_id: &str, + vendor_id: &str, + ) -> Result { + // We are checking if console device's device id and vendor id pair matches + let devices = self.get_pci_device_ids()?; + let devices: Vec<&str> = devices.split('\n').collect(); + let vendors = self.get_pci_vendor_ids()?; + let vendors: Vec<&str> = vendors.split('\n').collect(); + + for (index, d_id) in devices.iter().enumerate() { + if *d_id == device_id { + if let Some(v_id) = vendors.get(index) { + if *v_id == vendor_id { + return Ok(true); + } + } + } + } + + Ok(false) + } + + pub fn check_vsock(&self, socket: &str) { + // Listen from guest on vsock CID=3 PORT=16 + // SOCKET-LISTEN::: + let guest_ip = self.network.guest_ip.clone(); + let listen_socat = thread::spawn(move || { + ssh_command_ip("sudo socat - SOCKET-LISTEN:40:0:x00x00x10x00x00x00x03x00x00x00x00x00x00x00 > vsock_log", &guest_ip, DEFAULT_SSH_RETRIES, DEFAULT_SSH_TIMEOUT).unwrap(); + }); + + // Make sure socat is listening, which might take a few second on slow systems + thread::sleep(std::time::Duration::new(10, 0)); + + // Write something to vsock from the host + assert!(exec_host_command_status(&format!( + "echo -e \"CONNECT 16\\nHelloWorld!\" | socat - UNIX-CONNECT:{}", + socket + )) + .success()); + + // Wait for the thread to terminate. + listen_socat.join().unwrap(); + + assert_eq!( + self.ssh_command("cat vsock_log").unwrap().trim(), + "HelloWorld!" + ); + } + + pub fn start_vsock_passthrough_fd_listener(&self) { + // Listen from guest on vsock CID=3 PORT=16 + self.ssh_command( + "rm -f vsock_passfd_log vsock_passfd_socat.log vsock_passfd_socat.pid; sudo sh -c \ + 'socat SOCKET-LISTEN:40:0:x00x00x10x00x00x00x03x00x00x00x00x00x00x00,fork \ + SYSTEM:\"head -n 1 >> vsock_passfd_log; printf GuestReply\" \ + > vsock_passfd_socat.log 2>&1 & pid=$!; echo $pid > vsock_passfd_socat.pid; \ + sleep 1; kill -0 $pid'", + ) + .unwrap(); + } + + pub fn vsock_passthrough_fd_listener_identity(&self) -> String { + let identity = self + .ssh_command( + "sudo sh -c 'pid=$(cat vsock_passfd_socat.pid) || exit 1; \ + for fd_path in /proc/$pid/fd/*; do \ + target=$(readlink \"$fd_path\") || continue; \ + case \"$target\" in \ + socket:*) printf \"%s:%s:%s\\n\" \ + \"$pid\" \"${fd_path##*/}\" \"$target\";; \ + esac; \ + done'", + ) + .unwrap() + .trim() + .to_string(); + assert!(!identity.is_empty(), "socat has no open socket FD"); + identity + } + + pub fn check_vsock_passthrough_fd_bidirectional( + &self, + socket: &str, + payload: &str, + expected_log: &str, + ) { + // Connect via Unix socket + + let mut stream = (0..30) + .find_map(|_| match UnixStream::connect(socket) { + Ok(stream) => Some(stream), + Err(_) => { + thread::sleep(Duration::from_secs(1)); + None + } + }) + .expect("Failed to connect to vsock unix socket within 30 seconds"); + stream + .set_read_timeout(Some(Duration::from_secs(30))) + .unwrap(); + + let (fd1, mut fd2) = UnixStream::pair().expect("Failed to create unix socket pair"); + fd2.set_read_timeout(Some(Duration::from_secs(30))).unwrap(); + + let iov = [b"passfd 16 stream\n".as_slice()]; + let fds = [fd1.as_raw_fd()]; + stream.send_with_fds(&iov, &fds).unwrap(); + drop(fd1); + + // Read and validate the complete "OK stream " response. + let mut rsp = String::new(); + BufReader::new(&mut stream).read_line(&mut rsp).unwrap(); + let mut fields = rsp.split_whitespace(); + assert_eq!(fields.next(), Some("OK")); + assert_eq!(fields.next(), Some("stream")); + fields + .next() + .expect("passfd response is missing the local port") + .parse::() + .expect("passfd response contains an invalid local port"); + assert_eq!(fields.next(), None, "passfd response has extra fields"); + + // The guest consumes one newline-terminated request and replies through the same passed + // FD. Keep the write direction open: this test verifies bidirectional passfd I/O without + // imposing half-close semantics on the generic vsock connection state machine. + fd2.write_all(payload.as_bytes()).unwrap(); + + let mut reply = [0u8; 10]; + fd2.read_exact(&mut reply).unwrap(); + assert_eq!(&reply, b"GuestReply"); + + let actual_log = self.ssh_command("cat vsock_passfd_log").unwrap(); + assert_eq!( + actual_log.trim(), + expected_log, + "guest passfd log does not contain the expected payload" + ); + } + + #[cfg(target_arch = "x86_64")] + pub fn check_nvidia_gpu(&self) { + // Run CUDA sample to validate it can find the device + let device_query_result = self + .ssh_command("sudo /root/NVIDIA_CUDA-11.3_Samples/bin/x86_64/linux/release/deviceQuery") + .unwrap(); + assert!(device_query_result.contains("Detected 1 CUDA Capable device")); + assert!(device_query_result.contains("Device 0: \"NVIDIA Tesla T4\"")); + assert!(device_query_result.contains("Result = PASS")); + + // Run NVIDIA DCGM Diagnostics to validate the device is functional + self.ssh_command("sudo nv-hostengine").unwrap(); + + assert!(self + .ssh_command("sudo dcgmi discovery -l") + .unwrap() + .contains("Name: NVIDIA Tesla T4")); + assert_eq!( + self.ssh_command("sudo dcgmi diag -r 'diagnostic' | grep Pass | wc -l") + .unwrap() + .trim(), + "10" + ); + } + + pub fn reboot_linux(&self, current_reboot_count: u32, custom_timeout: Option) { + let list_boots_cmd = "sudo last | grep -c reboot"; + let boot_count = self + .ssh_command(list_boots_cmd) + .unwrap() + .trim() + .parse::() + .unwrap_or_default(); + + assert_eq!(boot_count, current_reboot_count + 1); + self.ssh_command("sudo reboot").unwrap(); + + self.wait_vm_boot(custom_timeout).unwrap(); + let boot_count = self + .ssh_command(list_boots_cmd) + .unwrap() + .trim() + .parse::() + .unwrap_or_default(); + assert_eq!(boot_count, current_reboot_count + 2); + } + + pub fn enable_memory_hotplug(&self) { + self.ssh_command("echo online | sudo tee /sys/devices/system/memory/auto_online_blocks") + .unwrap(); + } + + pub fn check_devices_common( + &self, + socket: Option<&String>, + console_text: Option<&String>, + pmem_path: Option<&String>, + ) { + // Check block devices are readable + self.ssh_command("sudo dd if=/dev/vda of=/dev/null bs=1M iflag=direct count=1024") + .unwrap(); + self.ssh_command("sudo dd if=/dev/vdb of=/dev/null bs=1M iflag=direct count=8") + .unwrap(); + // Check if the rng device is readable + self.ssh_command("sudo head -c 1000 /dev/hwrng > /dev/null") + .unwrap(); + // Check vsock + if let Some(socket) = socket { + self.check_vsock(socket.as_str()); + } + // Check if the console is usable + if let Some(console_text) = console_text { + let console_cmd = format!("echo {} | sudo tee /dev/hvc0", console_text); + self.ssh_command(&console_cmd).unwrap(); + } + // The net device is 'automatically' exercised through the above 'ssh' commands + + // Check if the pmem device is usable + if let Some(pmem_path) = pmem_path { + assert_eq!( + self.ssh_command(&format!("ls {}", pmem_path)) + .unwrap() + .trim(), + pmem_path + ); + assert_eq!( + self.ssh_command(&format!("sudo mount {} /mnt", pmem_path)) + .unwrap(), + "" + ); + assert_eq!(self.ssh_command("ls /mnt").unwrap(), "lost+found\n"); + self.ssh_command("echo test123 | sudo tee /mnt/test") + .unwrap(); + assert_eq!(self.ssh_command("sudo umount /mnt").unwrap(), ""); + assert_eq!(self.ssh_command("ls /mnt").unwrap(), ""); + + assert_eq!( + self.ssh_command(&format!("sudo mount {} /mnt", pmem_path)) + .unwrap(), + "" + ); + assert_eq!( + self.ssh_command("sudo cat /mnt/test || true") + .unwrap() + .trim(), + "test123" + ); + self.ssh_command("sudo rm /mnt/test").unwrap(); + assert_eq!(self.ssh_command("sudo umount /mnt").unwrap(), ""); + } + } +} + +pub enum VerbosityLevel { + Warn, + Info, + Debug, +} + +impl Default for VerbosityLevel { + fn default() -> Self { + Self::Warn + } +} + +impl ToString for VerbosityLevel { + fn to_string(&self) -> String { + use VerbosityLevel::*; + match self { + Warn => "".to_string(), + Info => "-v".to_string(), + Debug => "-vv".to_string(), + } + } +} + +pub struct GuestCommand<'a> { + command: Command, + guest: &'a Guest, + capture_output: bool, + print_cmd: bool, + verbosity: VerbosityLevel, +} + +impl<'a> GuestCommand<'a> { + pub fn new(guest: &'a Guest) -> Self { + Self::new_with_binary_path(guest, &clh_command("cube-hypervisor")) + } + + pub fn new_with_binary_path(guest: &'a Guest, binary_path: &str) -> Self { + Self { + command: Command::new(binary_path), + guest, + capture_output: false, + print_cmd: true, + verbosity: VerbosityLevel::Info, + } + } + + pub fn verbosity(&mut self, verbosity: VerbosityLevel) -> &mut Self { + self.verbosity = verbosity; + self + } + + pub fn capture_output(&mut self) -> &mut Self { + self.capture_output = true; + self + } + + pub fn set_print_cmd(&mut self, print_cmd: bool) -> &mut Self { + self.print_cmd = print_cmd; + self + } + + pub fn spawn(&mut self) -> io::Result { + use VerbosityLevel::*; + match &self.verbosity { + Warn => {} + Info => { + self.command.arg("-v"); + } + Debug => { + self.command.arg("-vv"); + } + }; + + if self.print_cmd { + println!( + "\n\n==== Start cube-hypervisor command-line ====\n\n\ + {:?}\n\ + \n==== End cube-hypervisor command-line ====\n\n", + self.command + ); + } + + if self.capture_output { + let child = self + .command + .stderr(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); + + let fd = child.stdout.as_ref().unwrap().as_raw_fd(); + let pipesize = unsafe { libc::fcntl(fd, libc::F_SETPIPE_SZ, PIPE_SIZE) }; + if pipesize == -1 { + return Err(io::Error::last_os_error()); + } + let fd = child.stderr.as_ref().unwrap().as_raw_fd(); + let pipesize1 = unsafe { libc::fcntl(fd, libc::F_SETPIPE_SZ, PIPE_SIZE) }; + if pipesize1 == -1 { + return Err(io::Error::last_os_error()); + } + + if pipesize >= PIPE_SIZE && pipesize1 >= PIPE_SIZE { + Ok(child) + } else { + Err(std::io::Error::new( + std::io::ErrorKind::Other, + format!( + "resizing pipe w/ 'fnctl' failed: stdout pipesize {}, stderr pipesize {}", + pipesize, pipesize1 + ), + )) + } + } else { + self.command.spawn() + } + } + + pub fn args(&mut self, args: I) -> &mut Self + where + I: IntoIterator, + S: AsRef, + { + self.command.args(args); + self + } + + pub fn default_disks(&mut self) -> &mut Self { + if self.guest.disk_config.disk(DiskType::CloudInit).is_some() { + self.args([ + "--disk", + format!( + "path={}", + self.guest + .disk_config + .disk(DiskType::OperatingSystem) + .unwrap() + ) + .as_str(), + format!( + "path={}", + self.guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + ]) + } else { + self.args([ + "--disk", + format!( + "path={}", + self.guest + .disk_config + .disk(DiskType::OperatingSystem) + .unwrap() + ) + .as_str(), + ]) + } + } + + pub fn default_net(&mut self) -> &mut Self { + self.args(["--net", self.guest.default_net_string().as_str()]) + } +} + +pub fn clh_command(cmd: &str) -> String { + env::var("BUILD_TARGET").map_or( + format!("target/x86_64-unknown-linux-gnu/release/{}", cmd), + |target| format!("target/{}/release/{}", target, cmd), + ) +} + +pub fn parse_iperf3_output(output: &[u8], sender: bool) -> Result { + std::panic::catch_unwind(|| { + let s = String::from_utf8_lossy(output); + let v: Value = serde_json::from_str(&s).expect("'iperf3' parse error: invalid json output"); + + let bps: f64 = if sender { + v["end"]["sum_sent"]["bits_per_second"] + .as_f64() + .expect("'iperf3' parse error: missing entry 'end.sum_sent.bits_per_second'") + } else { + v["end"]["sum_received"]["bits_per_second"] + .as_f64() + .expect("'iperf3' parse error: missing entry 'end.sum_received.bits_per_second'") + }; + + bps + }) + .map_err(|_| { + eprintln!( + "=============== iperf3 output ===============\n\n{}\n\n===========end============\n\n", + String::from_utf8_lossy(output) + ); + Error::Iperf3Parse + }) +} + +pub enum FioOps { + Read, + RandomRead, + Write, + RandomWrite, + ReadWrite, + RandRW, +} + +impl fmt::Display for FioOps { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + FioOps::Read => write!(f, "read"), + FioOps::RandomRead => write!(f, "randread"), + FioOps::Write => write!(f, "write"), + FioOps::RandomWrite => write!(f, "randwrite"), + FioOps::ReadWrite => write!(f, "rw"), + FioOps::RandRW => write!(f, "randrw"), + } + } +} + +pub fn parse_fio_output(output: &str, fio_ops: &FioOps, num_jobs: u32) -> Result { + std::panic::catch_unwind(|| { + let v: Value = + serde_json::from_str(output).expect("'fio' parse error: invalid json output"); + let jobs = v["jobs"] + .as_array() + .expect("'fio' parse error: missing entry 'jobs'"); + assert_eq!( + jobs.len(), + num_jobs as usize, + "'fio' parse error: Unexpected number of 'fio' jobs." + ); + + let (read, write) = match fio_ops { + FioOps::Read | FioOps::RandomRead => (true, false), + FioOps::Write | FioOps::RandomWrite => (false, true), + FioOps::ReadWrite | FioOps::RandRW => (true, true), + }; + + let mut total_bps = 0_f64; + for j in jobs { + if read { + let bytes = j["read"]["io_bytes"] + .as_u64() + .expect("'fio' parse error: missing entry 'read.io_bytes'"); + let runtime = j["read"]["runtime"] + .as_u64() + .expect("'fio' parse error: missing entry 'read.runtime'") + as f64 + / 1000_f64; + total_bps += bytes as f64 / runtime; + } + if write { + let bytes = j["write"]["io_bytes"] + .as_u64() + .expect("'fio' parse error: missing entry 'write.io_bytes'"); + let runtime = j["write"]["runtime"] + .as_u64() + .expect("'fio' parse error: missing entry 'write.runtime'") + as f64 + / 1000_f64; + total_bps += bytes as f64 / runtime; + } + } + + total_bps + }) + .map_err(|_| { + eprintln!( + "=============== Fio output ===============\n\n{}\n\n===========end============\n\n", + output + ); + Error::FioOutputParse + }) +} + +pub fn parse_fio_output_iops(output: &str, fio_ops: &FioOps, num_jobs: u32) -> Result { + std::panic::catch_unwind(|| { + let v: Value = + serde_json::from_str(output).expect("'fio' parse error: invalid json output"); + let jobs = v["jobs"] + .as_array() + .expect("'fio' parse error: missing entry 'jobs'"); + assert_eq!( + jobs.len(), + num_jobs as usize, + "'fio' parse error: Unexpected number of 'fio' jobs." + ); + + let (read, write) = match fio_ops { + FioOps::Read | FioOps::RandomRead => (true, false), + FioOps::Write | FioOps::RandomWrite => (false, true), + FioOps::ReadWrite | FioOps::RandRW => (true, true), + }; + + let mut total_iops = 0_f64; + for j in jobs { + if read { + let ios = j["read"]["total_ios"] + .as_u64() + .expect("'fio' parse error: missing entry 'read.total_ios'"); + let runtime = j["read"]["runtime"] + .as_u64() + .expect("'fio' parse error: missing entry 'read.runtime'") + as f64 + / 1000_f64; + total_iops += ios as f64 / runtime; + } + if write { + let ios = j["write"]["total_ios"] + .as_u64() + .expect("'fio' parse error: missing entry 'write.total_ios'"); + let runtime = j["write"]["runtime"] + .as_u64() + .expect("'fio' parse error: missing entry 'write.runtime'") + as f64 + / 1000_f64; + total_iops += ios as f64 / runtime; + } + } + + total_iops + }) + .map_err(|_| { + eprintln!( + "=============== Fio output ===============\n\n{}\n\n===========end============\n\n", + output + ); + Error::FioOutputParse + }) +} + +// Wait the child process for a given timeout +fn child_wait_timeout(child: &mut Child, timeout: u64) -> Result<(), WaitTimeoutError> { + match child.wait_timeout(Duration::from_secs(timeout)) { + Err(e) => { + return Err(WaitTimeoutError::General(e)); + } + Ok(s) => match s { + None => { + return Err(WaitTimeoutError::Timedout); + } + Some(s) => { + if !s.success() { + return Err(WaitTimeoutError::ExitStatus); + } + } + }, + } + + Ok(()) +} + +pub fn measure_virtio_net_throughput( + test_timeout: u32, + queue_pairs: u32, + guest: &Guest, + receive: bool, +) -> Result { + let default_port = 5201; + + // 1. start the iperf3 server on the guest + for n in 0..queue_pairs { + guest.ssh_command(&format!("iperf3 -s -p {} -D", default_port + n))?; + } + + thread::sleep(Duration::new(1, 0)); + + // 2. start the iperf3 client on host to measure RX through-put + let mut clients = Vec::new(); + for n in 0..queue_pairs { + let mut cmd = Command::new("iperf3"); + cmd.args([ + "-J", // Output in JSON format + "-c", + &guest.network.guest_ip, + "-p", + &format!("{}", default_port + n), + "-t", + &format!("{}", test_timeout), + ]); + // For measuring the guest transmit throughput (as a sender), + // use reverse mode of the iperf3 client on the host + if !receive { + cmd.args(["-R"]); + } + let client = cmd + .stderr(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .map_err(Error::Spawn)?; + + clients.push(client); + } + + let mut err: Option = None; + let mut bps = Vec::new(); + let mut failed = false; + for c in clients { + let mut c = c; + if let Err(e) = child_wait_timeout(&mut c, test_timeout as u64 + 5) { + err = Some(Error::WaitTimeout(e)); + failed = true; + } + + if !failed { + // Safe to unwrap as we know the child has terminated succesffully + let output = c.wait_with_output().unwrap(); + bps.push(parse_iperf3_output(&output.stdout, receive)?); + } else { + let _ = c.kill(); + let output = c.wait_with_output().unwrap(); + println!( + "=============== Client output [Error] ===============\n\n{}\n\n===========end============\n\n", + String::from_utf8_lossy(&output.stdout) + ); + } + } + + if let Some(e) = err { + Err(e) + } else { + Ok(bps.iter().sum()) + } +} + +pub fn parse_ethr_latency_output(output: &[u8]) -> Result, Error> { + std::panic::catch_unwind(|| { + let s = String::from_utf8_lossy(output); + let mut latency = Vec::new(); + for l in s.lines() { + let v: Value = serde_json::from_str(l).expect("'ethr' parse error: invalid json line"); + // Skip header/summary lines + if let Some(avg) = v["Avg"].as_str() { + // Assume the latency unit is always "us" + latency.push( + avg.split("us").collect::>()[0] + .parse::() + .expect("'ethr' parse error: invalid 'Avg' entry"), + ); + } + } + + assert!( + !latency.is_empty(), + "'ethr' parse error: no valid latency data found" + ); + + latency + }) + .map_err(|_| { + eprintln!( + "=============== ethr output ===============\n\n{}\n\n===========end============\n\n", + String::from_utf8_lossy(output) + ); + Error::EthrLogParse + }) +} + +pub fn measure_virtio_net_latency(guest: &Guest, test_timeout: u32) -> Result, Error> { + // copy the 'ethr' tool to the guest image + let ethr_path = "/usr/local/bin/ethr"; + let ethr_remote_path = "/tmp/ethr"; + scp_to_guest( + Path::new(ethr_path), + Path::new(ethr_remote_path), + &guest.network.guest_ip, + //DEFAULT_SSH_RETRIES, + 1, + DEFAULT_SSH_TIMEOUT, + )?; + + // Start the ethr server on the guest + guest.ssh_command(&format!("{} -s &> /dev/null &", ethr_remote_path))?; + + thread::sleep(Duration::new(10, 0)); + + // Start the ethr client on the host + let log_file = guest + .tmp_dir + .as_path() + .join("ethr.client.log") + .to_str() + .unwrap() + .to_string(); + let mut c = Command::new(ethr_path) + .args([ + "-c", + &guest.network.guest_ip, + "-t", + "l", + "-o", + &log_file, // file output is JSON format + "-d", + &format!("{}s", test_timeout), + ]) + .stderr(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .map_err(Error::Spawn)?; + + if let Err(e) = child_wait_timeout(&mut c, test_timeout as u64 + 5).map_err(Error::WaitTimeout) + { + let _ = c.kill(); + return Err(e); + } + + // Parse the ethr latency test output + let content = fs::read(log_file).map_err(Error::EthrLogFile)?; + parse_ethr_latency_output(&content) +} diff --git a/vendor/cube-hypervisor/tests/integration.rs b/vendor/cube-hypervisor/tests/integration.rs new file mode 100644 index 0000000..56a1218 --- /dev/null +++ b/vendor/cube-hypervisor/tests/integration.rs @@ -0,0 +1,11745 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +// When enabling the `mshv` feature, we skip quite some tests and +// hence have known dead-code. This annotation silences dead-code +// related warnings for our quality workflow to pass. +#![allow(dead_code)] + +extern crate test_infra; + +use std::collections::HashMap; +use std::fs; +use std::io; +use std::io::BufRead; +use std::io::Read; +use std::io::Seek; +use std::io::Write; +use std::os::unix::io::AsRawFd; +use std::path::PathBuf; +use std::process::{Child, Command, Stdio}; +use std::string::String; +use std::sync::mpsc; +use std::sync::mpsc::Receiver; +use std::sync::Mutex; +use std::thread; +use std::time::{Duration, Instant}; + +use net_util::MacAddr; +use test_infra::*; +use vmm::config::RestoreConfig; +use vmm::vm_config::{DiskConfig, FsConfig, MemoryConfig, NetConfig, PmemConfig, VsockConfig}; +use vmm_sys_util::{tempdir::TempDir, tempfile::TempFile}; +use wait_timeout::ChildExt; + +/// Minimum expected MemTotal (in kB) for a 512 MB VM guest. +/// With CONFIG_VFAT_FS enabled the kernel reports ~469,000 kB on a +/// 512 MB VM, leaving ~19,000 kB of headroom above this threshold. +const MIN_EXPECTED_MEMORY_KB: u32 = 450_000; + +#[cfg(target_arch = "x86_64")] +mod x86_64 { + pub const BIONIC_IMAGE_NAME: &str = "bionic-server-cloudimg-amd64.raw"; + pub const FOCAL_IMAGE_NAME: &str = "focal-server-cloudimg-amd64-custom-20210609-0.raw"; + pub const FOCAL_SGX_IMAGE_NAME: &str = "focal-server-cloudimg-amd64-sgx.raw"; + pub const HIRSUTE_NVIDIA_IMAGE_NAME: &str = "hirsute-server-cloudimg-amd64-nvidia.raw"; + pub const FOCAL_IMAGE_NAME_QCOW2: &str = "focal-server-cloudimg-amd64-custom-20210609-0.qcow2"; + pub const FOCAL_IMAGE_NAME_VHD: &str = "focal-server-cloudimg-amd64-custom-20210609-0.vhd"; + pub const FOCAL_IMAGE_NAME_VHDX: &str = "focal-server-cloudimg-amd64-custom-20210609-0.vhdx"; + pub const JAMMY_IMAGE_NAME: &str = "jammy-server-cloudimg-amd64-custom-20220329-0.raw"; + pub const WINDOWS_IMAGE_NAME: &str = "windows-server-2019.raw"; + pub const OVMF_NAME: &str = "CLOUDHV.fd"; + pub const GREP_SERIAL_IRQ_CMD: &str = "grep -c 'IO-APIC.*ttyS0' /proc/interrupts || true"; +} + +#[cfg(target_arch = "x86_64")] +use x86_64::*; + +#[cfg(target_arch = "aarch64")] +mod aarch64 { + pub const BIONIC_IMAGE_NAME: &str = "bionic-server-cloudimg-arm64.raw"; + pub const FOCAL_IMAGE_NAME: &str = "focal-server-cloudimg-arm64-custom-20210929-0.raw"; + pub const FOCAL_IMAGE_UPDATE_KERNEL_NAME: &str = + "focal-server-cloudimg-arm64-custom-20210929-0-update-kernel.raw"; + pub const FOCAL_IMAGE_NAME_QCOW2: &str = "focal-server-cloudimg-arm64-custom-20210929-0.qcow2"; + pub const FOCAL_IMAGE_NAME_VHD: &str = "focal-server-cloudimg-arm64-custom-20210929-0.vhd"; + pub const FOCAL_IMAGE_NAME_VHDX: &str = "focal-server-cloudimg-arm64-custom-20210929-0.vhdx"; + pub const JAMMY_IMAGE_NAME: &str = "jammy-server-cloudimg-arm64-custom-20220329-0.raw"; + pub const WINDOWS_IMAGE_NAME: &str = "windows-11-iot-enterprise-aarch64.raw"; + pub const OVMF_NAME: &str = "CLOUDHV_EFI.fd"; + pub const GREP_SERIAL_IRQ_CMD: &str = "grep -c 'GICv3.*uart-pl011' /proc/interrupts || true"; + pub const GREP_PMU_IRQ_CMD: &str = "grep -c 'GICv3.*arm-pmu' /proc/interrupts || true"; +} + +#[cfg(target_arch = "aarch64")] +use aarch64::*; + +const DIRECT_KERNEL_BOOT_CMDLINE: &str = + "root=/dev/vda1 console=hvc0 rw systemd.journald.forward_to_console=1"; + +const CONSOLE_TEST_STRING: &str = "Started OpenBSD Secure Shell server"; + +fn prepare_virtiofsd(tmp_dir: &TempDir, shared_dir: &str) -> (std::process::Child, String) { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut virtiofsd_path = workload_path; + virtiofsd_path.push("virtiofsd"); + let virtiofsd_path = String::from(virtiofsd_path.to_str().unwrap()); + + let virtiofsd_socket_path = + String::from(tmp_dir.as_path().join("virtiofs.sock").to_str().unwrap()); + + // Start the daemon + let child = Command::new(virtiofsd_path.as_str()) + .args(["--shared-dir", shared_dir]) + .args(["--socket-path", virtiofsd_socket_path.as_str()]) + .args(["--cache", "never"]) + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(10, 0)); + + (child, virtiofsd_socket_path) +} + +fn prepare_vubd( + tmp_dir: &TempDir, + blk_img: &str, + num_queues: usize, + rdonly: bool, + direct: bool, +) -> (std::process::Child, String) { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut blk_file_path = workload_path; + blk_file_path.push(blk_img); + let blk_file_path = String::from(blk_file_path.to_str().unwrap()); + + let vubd_socket_path = String::from(tmp_dir.as_path().join("vub.sock").to_str().unwrap()); + + // Start the daemon + let child = Command::new(clh_command("vhost_user_block")) + .args([ + "--block-backend", + format!( + "path={},socket={},num_queues={},readonly={},direct={}", + blk_file_path, vubd_socket_path, num_queues, rdonly, direct + ) + .as_str(), + ]) + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(10, 0)); + + (child, vubd_socket_path) +} + +fn temp_vsock_path(tmp_dir: &TempDir) -> String { + String::from(tmp_dir.as_path().join("vsock").to_str().unwrap()) +} + +fn temp_api_path(tmp_dir: &TempDir) -> String { + String::from( + tmp_dir + .as_path() + .join("cloud-hypervisor.sock") + .to_str() + .unwrap(), + ) +} + +fn temp_event_monitor_path(tmp_dir: &TempDir) -> String { + String::from(tmp_dir.as_path().join("event.json").to_str().unwrap()) +} + +// Creates the directory and returns the path. +fn temp_snapshot_dir_path(tmp_dir: &TempDir) -> String { + let snapshot_dir = String::from(tmp_dir.as_path().join("snapshot").to_str().unwrap()); + std::fs::create_dir(&snapshot_dir).unwrap(); + snapshot_dir +} + +fn temp_vmcore_file_path(tmp_dir: &TempDir) -> String { + let vmcore_file = String::from(tmp_dir.as_path().join("vmcore").to_str().unwrap()); + vmcore_file +} + +// Creates the path for direct kernel boot and return the path. +// For x86_64, this function returns the vmlinux kernel path. +// For AArch64, this function returns the PE kernel path. +fn direct_kernel_boot_path() -> PathBuf { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut kernel_path = workload_path; + #[cfg(target_arch = "x86_64")] + kernel_path.push("vmlinux"); + #[cfg(target_arch = "aarch64")] + kernel_path.push("Image"); + + kernel_path +} + +fn edk2_path() -> PathBuf { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + let mut edk2_path = workload_path; + edk2_path.push(OVMF_NAME); + + edk2_path +} + +fn cloud_hypervisor_release_path() -> String { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut ch_release_path = workload_path; + #[cfg(target_arch = "x86_64")] + ch_release_path.push("cloud-hypervisor-static"); + #[cfg(target_arch = "aarch64")] + ch_release_path.push("cloud-hypervisor-static-aarch64"); + + ch_release_path.into_os_string().into_string().unwrap() +} + +fn prepare_vhost_user_net_daemon( + tmp_dir: &TempDir, + ip: &str, + tap: Option<&str>, + mtu: Option, + num_queues: usize, + client_mode: bool, +) -> (std::process::Command, String) { + let vunet_socket_path = String::from(tmp_dir.as_path().join("vunet.sock").to_str().unwrap()); + + // Start the daemon + let mut net_params = format!( + "ip={},mask=255.255.255.0,socket={},num_queues={},queue_size=1024,client={}", + ip, vunet_socket_path, num_queues, client_mode + ); + + if let Some(tap) = tap { + net_params.push_str(format!(",tap={}", tap).as_str()); + } + + if let Some(mtu) = mtu { + net_params.push_str(format!(",mtu={}", mtu).as_str()); + } + + let mut command = Command::new(clh_command("vhost_user_net")); + command.args(["--net-backend", net_params.as_str()]); + + (command, vunet_socket_path) +} + +fn curl_command(api_socket: &str, method: &str, url: &str, http_body: Option<&str>) { + let mut curl_args: Vec<&str> = ["--unix-socket", api_socket, "-i", "-X", method, url].to_vec(); + + if let Some(body) = http_body { + curl_args.push("-H"); + curl_args.push("Accept: application/json"); + curl_args.push("-H"); + curl_args.push("Content-Type: application/json"); + curl_args.push("-d"); + curl_args.push(body); + } + + let status = Command::new("curl") + .args(curl_args) + .status() + .expect("Failed to launch curl command"); + + assert!(status.success()); +} + +fn remote_command(api_socket: &str, command: &str, arg: Option<&str>) -> bool { + match arg { + Some(a) => remote_command_w_args(api_socket, command, &[a]), + None => remote_command_w_args(api_socket, command, &[]), + } +} + +fn remote_command_w_args(api_socket: &str, command: &str, args: &[&str]) -> bool { + remote_command_w_args_output(api_socket, command, args).0 +} + +fn remote_command_w_args_output(api_socket: &str, command: &str, args: &[&str]) -> (bool, String) { + let mut cmd = Command::new(clh_command("ch-remote")); + cmd.args([&format!("--api-socket={}", api_socket), command]); + cmd.args(args); + + let output = cmd.output().unwrap(); + let stderr = String::from_utf8_lossy(&output.stderr).into_owned(); + if output.status.success() { + (true, stderr) + } else { + eprintln!("Error running ch-remote command: {:?}", &cmd); + eprintln!("stderr: {}", stderr); + (false, stderr) + } +} + +fn remote_command_w_output(api_socket: &str, command: &str, arg: Option<&str>) -> (bool, Vec) { + let mut cmd = Command::new(clh_command("ch-remote")); + cmd.args([&format!("--api-socket={}", api_socket), command]); + + if let Some(arg) = arg { + cmd.arg(arg); + } + + let output = cmd.output().expect("Failed to launch ch-remote"); + + (output.status.success(), output.stdout) +} + +fn resize_command( + api_socket: &str, + desired_vcpus: Option, + desired_ram: Option, + desired_balloon: Option, + event_file: Option<&str>, +) -> bool { + let mut cmd = Command::new(clh_command("ch-remote")); + cmd.args([&format!("--api-socket={}", api_socket), "resize"]); + + if let Some(desired_vcpus) = desired_vcpus { + cmd.arg(format!("--cpus={}", desired_vcpus)); + } + + if let Some(desired_ram) = desired_ram { + cmd.arg(format!("--memory={}", desired_ram)); + } + + if let Some(desired_balloon) = desired_balloon { + cmd.arg(format!("--balloon={}", desired_balloon)); + } + + let ret = cmd.status().expect("Failed to launch ch-remote").success(); + + if let Some(event_path) = event_file { + let latest_events = [ + &MetaEvent { + event: "resizing".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resized".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact(&latest_events, event_path)); + } + + ret +} + +fn resize_zone_command(api_socket: &str, id: &str, desired_size: &str) -> bool { + let mut cmd = Command::new(clh_command("ch-remote")); + cmd.args([ + &format!("--api-socket={}", api_socket), + "resize-zone", + &format!("--id={}", id), + &format!("--size={}", desired_size), + ]); + + cmd.status().expect("Failed to launch ch-remote").success() +} + +// setup OVS-DPDK bridge and ports +fn setup_ovs_dpdk() { + // setup OVS-DPDK + assert!(exec_host_command_status("service openvswitch-switch start").success()); + assert!(exec_host_command_status("ovs-vsctl init").success()); + assert!( + exec_host_command_status("ovs-vsctl set Open_vSwitch . other_config:dpdk-init=true") + .success() + ); + assert!(exec_host_command_status("service openvswitch-switch restart").success()); + + // Create OVS-DPDK bridge and ports + assert!(exec_host_command_status( + "ovs-vsctl add-br ovsbr0 -- set bridge ovsbr0 datapath_type=netdev", + ) + .success()); + assert!(exec_host_command_status("ovs-vsctl add-port ovsbr0 vhost-user1 -- set Interface vhost-user1 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient1").success()); + assert!(exec_host_command_status("ovs-vsctl add-port ovsbr0 vhost-user2 -- set Interface vhost-user2 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient2").success()); + assert!(exec_host_command_status("ip link set up dev ovsbr0").success()); + assert!(exec_host_command_status("service openvswitch-switch restart").success()); +} +fn cleanup_ovs_dpdk() { + assert!(exec_host_command_status("ovs-vsctl del-br ovsbr0").success()); + exec_host_command_status("rm -f ovs-vsctl /tmp/dpdkvhostclient1 /tmp/dpdkvhostclient2"); +} +// Setup two guests and ensure they are connected through ovs-dpdk +fn setup_ovs_dpdk_guests( + guest1: &Guest, + guest2: &Guest, + api_socket: &str, + release_binary: bool, +) -> (Child, Child) { + setup_ovs_dpdk(); + + let clh_path = if !release_binary { + clh_command("cube-hypervisor") + } else { + cloud_hypervisor_release_path() + }; + + let mut child1 = GuestCommand::new_with_binary_path(guest1, &clh_path) + .args(["--cpus", "boot=2"]) + .args(["--memory", "size=0,shared=on"]) + .args(["--memory-zone", "id=mem0,size=1G,shared=on,host_numa_node=0"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", guest1.default_net_string().as_str(), "vhost_user=true,socket=/tmp/dpdkvhostclient1,num_queues=2,queue_size=256,vhost_mode=server"]) + .capture_output() + .spawn() + .unwrap(); + + #[cfg(target_arch = "x86_64")] + let guest_net_iface = "ens5"; + #[cfg(target_arch = "aarch64")] + let guest_net_iface = "enp0s5"; + + let r = std::panic::catch_unwind(|| { + guest1.wait_vm_boot(None).unwrap(); + + guest1 + .ssh_command(&format!( + "sudo ip addr add 172.100.0.1/24 dev {}", + guest_net_iface + )) + .unwrap(); + guest1 + .ssh_command(&format!("sudo ip link set up dev {}", guest_net_iface)) + .unwrap(); + + let guest_ip = guest1.network.guest_ip.clone(); + thread::spawn(move || { + ssh_command_ip( + "nc -l 12345", + &guest_ip, + DEFAULT_SSH_RETRIES, + DEFAULT_SSH_TIMEOUT, + ) + .unwrap(); + }); + }); + if r.is_err() { + cleanup_ovs_dpdk(); + + let _ = child1.kill(); + let output = child1.wait_with_output().unwrap(); + handle_child_output(r, &output); + panic!("Test should already be failed/panicked"); // To explicitly mark this block never return + } + + let mut child2 = GuestCommand::new_with_binary_path(guest2, &clh_path) + .args(["--api-socket", api_socket]) + .args(["--cpus", "boot=2"]) + .args(["--memory", "size=0,shared=on"]) + .args(["--memory-zone", "id=mem0,size=1G,shared=on,host_numa_node=0"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", guest2.default_net_string().as_str(), "vhost_user=true,socket=/tmp/dpdkvhostclient2,num_queues=2,queue_size=256,vhost_mode=server"]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest2.wait_vm_boot(None).unwrap(); + + guest2 + .ssh_command(&format!( + "sudo ip addr add 172.100.0.2/24 dev {}", + guest_net_iface + )) + .unwrap(); + guest2 + .ssh_command(&format!("sudo ip link set up dev {}", guest_net_iface)) + .unwrap(); + + // Check the connection works properly between the two VMs + guest2.ssh_command("nc -vz 172.100.0.1 12345").unwrap(); + }); + if r.is_err() { + cleanup_ovs_dpdk(); + + let _ = child1.kill(); + let _ = child2.kill(); + let output = child2.wait_with_output().unwrap(); + handle_child_output(r, &output); + panic!("Test should already be failed/panicked"); // To explicitly mark this block never return + } + + (child1, child2) +} + +enum FwType { + Ovmf, + RustHypervisorFirmware, +} + +fn fw_path(_fw_type: FwType) -> String { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut fw_path = workload_path; + #[cfg(target_arch = "aarch64")] + fw_path.push("CLOUDHV_EFI.fd"); + #[cfg(target_arch = "x86_64")] + { + match _fw_type { + FwType::Ovmf => fw_path.push(OVMF_NAME), + FwType::RustHypervisorFirmware => fw_path.push("hypervisor-fw"), + } + } + + fw_path.to_str().unwrap().to_string() +} + +struct MetaEvent { + event: String, + device_id: Option, +} + +impl MetaEvent { + pub fn match_with_json_event(&self, v: &serde_json::Value) -> bool { + let mut matched = false; + if v["event"].as_str().unwrap() == self.event { + if let Some(device_id) = &self.device_id { + if v["properties"]["id"].as_str().unwrap() == device_id { + matched = true + } + } else { + matched = true; + } + } + matched + } +} + +// Parse the event_monitor file based on the format that each event +// is followed by a double newline +fn parse_event_file(event_file: &str) -> Vec { + let content = fs::read(event_file).unwrap(); + let mut ret = Vec::new(); + for entry in String::from_utf8_lossy(&content) + .trim() + .split("\n\n") + .collect::>() + { + ret.push(serde_json::from_str(entry).unwrap()); + } + + ret +} + +// Return true if all events from the input 'expected_events' are matched sequentially +// with events from the 'event_file' +fn check_sequential_events(expected_events: &[&MetaEvent], event_file: &str) -> bool { + let json_events = parse_event_file(event_file); + let len = expected_events.len(); + let mut idx = 0; + for e in &json_events { + if idx == len { + break; + } + if expected_events[idx].match_with_json_event(e) { + idx += 1; + } + } + + idx == len +} + +// Return true if all events from the input 'expected_events' are matched exactly +// with events from the 'event_file' +fn check_sequential_events_exact(expected_events: &[&MetaEvent], event_file: &str) -> bool { + let json_events = parse_event_file(event_file); + assert!(expected_events.len() <= json_events.len()); + let json_events = &json_events[..expected_events.len()]; + + for (idx, e) in json_events.iter().enumerate() { + if !expected_events[idx].match_with_json_event(e) { + return false; + } + } + + true +} + +// Return true if events from the input 'expected_events' are matched exactly +// with the most recent events from the 'event_file' +fn check_latest_events_exact(latest_events: &[&MetaEvent], event_file: &str) -> bool { + let json_events = parse_event_file(event_file); + assert!(latest_events.len() <= json_events.len()); + let json_events = &json_events[(json_events.len() - latest_events.len())..]; + + for (idx, e) in json_events.iter().enumerate() { + if !latest_events[idx].match_with_json_event(e) { + return false; + } + } + + true +} + +fn test_cpu_topology(threads_per_core: u8, cores_per_package: u8, packages: u8, use_fw: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let total_vcpus = threads_per_core * cores_per_package * packages; + let direct_kernel_boot_path = direct_kernel_boot_path(); + let mut kernel_path = direct_kernel_boot_path.to_str().unwrap(); + let fw_path = fw_path(FwType::RustHypervisorFirmware); + if use_fw { + kernel_path = fw_path.as_str(); + } + + let mut child = GuestCommand::new(&guest) + .args([ + "--cpus", + &format!( + "boot={},topology={}:{}:1:{}", + total_vcpus, threads_per_core, cores_per_package, packages + ), + ]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + assert_eq!( + guest.get_cpu_count().unwrap_or_default(), + u32::from(total_vcpus) + ); + assert_eq!( + guest + .ssh_command("lscpu | grep \"per core\" | cut -f 2 -d \":\" | sed \"s# *##\"") + .unwrap() + .trim() + .parse::() + .unwrap_or(0), + threads_per_core + ); + + assert_eq!( + guest + .ssh_command("lscpu | grep \"per socket\" | cut -f 2 -d \":\" | sed \"s# *##\"") + .unwrap() + .trim() + .parse::() + .unwrap_or(0), + cores_per_package + ); + + assert_eq!( + guest + .ssh_command("lscpu | grep \"Socket\" | cut -f 2 -d \":\" | sed \"s# *##\"") + .unwrap() + .trim() + .parse::() + .unwrap_or(0), + packages + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); +} + +#[allow(unused_variables)] +fn _test_guest_numa_nodes(acpi: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = if acpi { + edk2_path() + } else { + direct_kernel_boot_path() + }; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=6,max=12"]) + .args(["--memory", "size=0,hotplug_method=virtio-mem"]) + .args([ + "--memory-zone", + "id=mem0,size=1G,hotplug_size=3G", + "id=mem1,size=2G,hotplug_size=3G", + "id=mem2,size=3G,hotplug_size=3G", + ]) + .args([ + "--numa", + "guest_numa_id=0,cpus=[0-2,9],distances=[1@15,2@20],memory_zones=mem0", + "guest_numa_id=1,cpus=[3-4,6-8],distances=[0@20,2@25],memory_zones=mem1", + "guest_numa_id=2,cpus=[5,10-11],distances=[0@25,1@30],memory_zones=mem2", + ]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--api-socket", &api_socket]) + .capture_output() + .default_disks() + .default_net() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + guest.check_numa_common( + Some(&[960_000, 1_920_000, 2_880_000]), + Some(&[vec![0, 1, 2], vec![3, 4], vec![5]]), + Some(&["10 15 20", "20 10 25", "25 30 10"]), + ); + + // AArch64 currently does not support hotplug, and therefore we only + // test hotplug-related function on x86_64 here. + #[cfg(target_arch = "x86_64")] + { + guest.enable_memory_hotplug(); + + // Resize every memory zone and check each associated NUMA node + // has been assigned the right amount of memory. + resize_zone_command(&api_socket, "mem0", "4G"); + resize_zone_command(&api_socket, "mem1", "4G"); + resize_zone_command(&api_socket, "mem2", "4G"); + // Resize to the maximum amount of CPUs and check each NUMA + // node has been assigned the right CPUs set. + resize_command(&api_socket, Some(12), None, None, None); + thread::sleep(std::time::Duration::new(5, 0)); + + guest.check_numa_common( + Some(&[3_840_000, 3_840_000, 3_840_000]), + Some(&[vec![0, 1, 2, 9], vec![3, 4, 6, 7, 8], vec![5, 10, 11]]), + None, + ); + } + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); +} + +#[allow(unused_variables)] +fn _test_power_button(acpi: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let mut cmd = GuestCommand::new(&guest); + let api_socket = temp_api_path(&guest.tmp_dir); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = if acpi { + edk2_path() + } else { + direct_kernel_boot_path() + }; + + cmd.args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .capture_output() + .default_disks() + .default_net() + .args(["--api-socket", &api_socket]); + + let child = cmd.spawn().unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + assert!(remote_command(&api_socket, "power-button", None)); + }); + + let output = child.wait_with_output().unwrap(); + assert!(output.status.success()); + handle_child_output(r, &output); +} + +type PrepareNetDaemon = dyn Fn( + &TempDir, + &str, + Option<&str>, + Option, + usize, + bool, +) -> (std::process::Command, String); + +fn test_vhost_user_net( + tap: Option<&str>, + num_queues: usize, + prepare_daemon: &PrepareNetDaemon, + generate_host_mac: bool, + client_mode_daemon: bool, +) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let kernel_path = direct_kernel_boot_path(); + + let host_mac = if generate_host_mac { + Some(MacAddr::local_random()) + } else { + None + }; + + let mtu = Some(3000); + + let (mut daemon_command, vunet_socket_path) = prepare_daemon( + &guest.tmp_dir, + &guest.network.host_ip, + tap, + mtu, + num_queues, + client_mode_daemon, + ); + + let net_params = format!( + "vhost_user=true,mac={},socket={},num_queues={},queue_size=1024{},vhost_mode={},mtu=3000", + guest.network.guest_mac, + vunet_socket_path, + num_queues, + if let Some(host_mac) = host_mac { + format!(",host_mac={}", host_mac) + } else { + "".to_owned() + }, + if client_mode_daemon { + "server" + } else { + "client" + }, + ); + + let mut ch_command = GuestCommand::new(&guest); + ch_command + .args(["--cpus", format!("boot={}", num_queues / 2).as_str()]) + .args(["--memory", "size=512M,hotplug_size=2048M,shared=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", net_params.as_str()]) + .args(["--api-socket", &api_socket]) + .capture_output(); + + let mut daemon_child: std::process::Child; + let mut child: std::process::Child; + + if client_mode_daemon { + child = ch_command.spawn().unwrap(); + // Make sure the VMM is waiting for the backend to connect + thread::sleep(std::time::Duration::new(10, 0)); + daemon_child = daemon_command.spawn().unwrap(); + } else { + daemon_child = daemon_command.spawn().unwrap(); + // Make sure the backend is waiting for the VMM to connect + thread::sleep(std::time::Duration::new(10, 0)); + child = ch_command.spawn().unwrap(); + } + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + if let Some(tap_name) = tap { + let tap_count = exec_host_command_output(&format!("ip link | grep -c {}", tap_name)); + assert_eq!(String::from_utf8_lossy(&tap_count.stdout).trim(), "1"); + } + + if let Some(host_mac) = tap { + let mac_count = exec_host_command_output(&format!("ip link | grep -c {}", host_mac)); + assert_eq!(String::from_utf8_lossy(&mac_count.stdout).trim(), "1"); + } + + #[cfg(target_arch = "aarch64")] + let iface = "enp0s4"; + #[cfg(target_arch = "x86_64")] + let iface = "ens4"; + + assert_eq!( + guest + .ssh_command(format!("cat /sys/class/net/{}/mtu", iface).as_str()) + .unwrap() + .trim(), + "3000" + ); + + // 1 network interface + default localhost ==> 2 interfaces + // It's important to note that this test is fully exercising the + // vhost-user-net implementation and the associated backend since + // it does not define any --net network interface. That means all + // the ssh communication in that test happens through the network + // interface backed by vhost-user-net. + assert_eq!( + guest + .ssh_command("ip -o link | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 2 + ); + + // The following pci devices will appear on guest with PCI-MSI + // interrupt vectors assigned. + // 1 virtio-console with 3 vectors: config, Rx, Tx + // 1 virtio-blk with 2 vectors: config, Request + // 1 virtio-blk with 2 vectors: config, Request + // 1 virtio-rng with 2 vectors: config, Request + // Since virtio-net has 2 queue pairs, its vectors is as follows: + // 1 virtio-net with 5 vectors: config, Rx (2), Tx (2) + // Based on the above, the total vectors should 14. + #[cfg(target_arch = "x86_64")] + let grep_cmd = "grep -c PCI-MSI /proc/interrupts"; + #[cfg(target_arch = "aarch64")] + let grep_cmd = "grep -c ITS-MSI /proc/interrupts"; + assert_eq!( + guest + .ssh_command(grep_cmd) + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 10 + (num_queues as u32) + ); + + // ACPI feature is needed. + #[cfg(target_arch = "x86_64")] + { + guest.enable_memory_hotplug(); + + // Add RAM to the VM + let desired_ram = 1024 << 20; + resize_command(&api_socket, None, Some(desired_ram), None, None); + + thread::sleep(std::time::Duration::new(10, 0)); + + // Here by simply checking the size (through ssh), we validate + // the connection is still working, which means vhost-user-net + // keeps working after the resize. + assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + } + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + thread::sleep(std::time::Duration::new(5, 0)); + let _ = daemon_child.kill(); + let _ = daemon_child.wait(); + + handle_child_output(r, &output); +} + +type PrepareBlkDaemon = dyn Fn(&TempDir, &str, usize, bool, bool) -> (std::process::Child, String); + +fn test_vhost_user_blk( + num_queues: usize, + readonly: bool, + direct: bool, + prepare_vhost_user_blk_daemon: Option<&PrepareBlkDaemon>, +) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let kernel_path = direct_kernel_boot_path(); + + let (blk_params, daemon_child) = { + let prepare_daemon = prepare_vhost_user_blk_daemon.unwrap(); + // Start the daemon + let (daemon_child, vubd_socket_path) = + prepare_daemon(&guest.tmp_dir, "blk.img", num_queues, readonly, direct); + + ( + format!( + "vhost_user=true,socket={},num_queues={},queue_size=128", + vubd_socket_path, num_queues, + ), + Some(daemon_child), + ) + }; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", format!("boot={}", num_queues).as_str()]) + .args(["--memory", "size=512M,hotplug_size=2048M,shared=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + blk_params.as_str(), + ]) + .default_net() + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check both if /dev/vdc exists and if the block size is 16M. + assert_eq!( + guest + .ssh_command("lsblk | grep vdc | grep -c 16M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Check if this block is RO or RW. + assert_eq!( + guest + .ssh_command("lsblk | grep vdc | awk '{print $5}'") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + readonly as u32 + ); + + // Check if the number of queues in /sys/block/vdc/mq matches the + // expected num_queues. + assert_eq!( + guest + .ssh_command("ls -ll /sys/block/vdc/mq | grep ^d | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + num_queues as u32 + ); + + // Mount the device + let mount_ro_rw_flag = if readonly { "ro,noload" } else { "rw" }; + guest.ssh_command("mkdir mount_image").unwrap(); + guest + .ssh_command( + format!( + "sudo mount -o {} -t ext4 /dev/vdc mount_image/", + mount_ro_rw_flag + ) + .as_str(), + ) + .unwrap(); + + // Check the content of the block device. The file "foo" should + // contain "bar". + assert_eq!( + guest.ssh_command("cat mount_image/foo").unwrap().trim(), + "bar" + ); + + // ACPI feature is needed. + #[cfg(target_arch = "x86_64")] + { + guest.enable_memory_hotplug(); + + // Add RAM to the VM + let desired_ram = 1024 << 20; + resize_command(&api_socket, None, Some(desired_ram), None, None); + + thread::sleep(std::time::Duration::new(10, 0)); + + assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + + // Check again the content of the block device after the resize + // has been performed. + assert_eq!( + guest.ssh_command("cat mount_image/foo").unwrap().trim(), + "bar" + ); + } + + // Unmount the device + guest.ssh_command("sudo umount /dev/vdc").unwrap(); + guest.ssh_command("rm -r mount_image").unwrap(); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + if let Some(mut daemon_child) = daemon_child { + thread::sleep(std::time::Duration::new(5, 0)); + let _ = daemon_child.kill(); + let _ = daemon_child.wait(); + } + + handle_child_output(r, &output); +} + +fn test_boot_from_vhost_user_blk( + num_queues: usize, + readonly: bool, + direct: bool, + prepare_vhost_user_blk_daemon: Option<&PrepareBlkDaemon>, +) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + let disk_path = guest.disk_config.disk(DiskType::OperatingSystem).unwrap(); + + let (blk_boot_params, daemon_child) = { + let prepare_daemon = prepare_vhost_user_blk_daemon.unwrap(); + // Start the daemon + let (daemon_child, vubd_socket_path) = prepare_daemon( + &guest.tmp_dir, + disk_path.as_str(), + num_queues, + readonly, + direct, + ); + + ( + format!( + "vhost_user=true,socket={},num_queues={},queue_size=128", + vubd_socket_path, num_queues, + ), + Some(daemon_child), + ) + }; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", format!("boot={}", num_queues).as_str()]) + .args(["--memory", "size=512M,shared=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args([ + "--disk", + blk_boot_params.as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + ]) + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Just check the VM booted correctly. + assert_eq!(guest.get_cpu_count().unwrap_or_default(), num_queues as u32); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + }); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + if let Some(mut daemon_child) = daemon_child { + thread::sleep(std::time::Duration::new(5, 0)); + let _ = daemon_child.kill(); + let _ = daemon_child.wait(); + } + + handle_child_output(r, &output); +} + +fn _test_native_virtio_fs(hotplug: bool, pci_segment: Option) { + #[cfg(target_arch = "x86_64")] + let focal_image = FOCAL_IMAGE_NAME.to_string(); + let focal = UbuntuDiskConfig::new(focal_image); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut shared_dir = workload_path; + shared_dir.push("shared_dir"); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + + let mut guest_command = GuestCommand::new(&guest); + guest_command + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M,hotplug_size=2048M,shared=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--api-socket", &api_socket]); + if pci_segment.is_some() { + guest_command.args(["--platform", "num_pci_segments=16"]); + } + + let fs_params = format!( + "id=myfs0,tag=myfs,native=true,shared_dir={},cache={},num_queues=1,queue_size=1024{}", + shared_dir.to_str().unwrap(), + "always".to_owned(), + if let Some(pci_segment) = pci_segment { + format!(",pci_segment={}", pci_segment) + } else { + "".to_owned() + } + ); + + if !hotplug { + guest_command.args(["--fs", fs_params.as_str()]); + } + + let mut child = guest_command.capture_output().spawn().unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + if hotplug { + // Add fs to the VM + let (cmd_success, cmd_output) = + remote_command_w_output(&api_socket, "add-fs", Some(&fs_params)); + assert!(cmd_success); + + if let Some(pci_segment) = pci_segment { + assert!(String::from_utf8_lossy(&cmd_output).contains(&format!( + "{{\"id\":\"myfs0\",\"bdf\":\"{:04x}:00:01.0\"}}", + pci_segment + ))); + } else { + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"myfs0\",\"bdf\":\"0000:00:06.0\"}")); + } + + thread::sleep(std::time::Duration::new(10, 0)); + } + + // Mount shared directory through virtio_fs filesystem + guest + .ssh_command("mkdir -p mount_dir && sudo mount -t virtiofs myfs mount_dir/") + .unwrap(); + + // Check file1 exists and its content is "foo" + assert_eq!( + guest.ssh_command("cat mount_dir/file1").unwrap().trim(), + "foo" + ); + // Check file2 does not exist + guest + .ssh_command("[ ! -f 'mount_dir/file2' ] || true") + .unwrap(); + + // Check file3 exists and its content is "bar" + assert_eq!( + guest.ssh_command("cat mount_dir/file3").unwrap().trim(), + "bar" + ); + + // ACPI feature is needed. + #[cfg(target_arch = "x86_64")] + { + guest.enable_memory_hotplug(); + + // Add RAM to the VM + let desired_ram = 1024 << 20; + resize_command(&api_socket, None, Some(desired_ram), None, None); + + thread::sleep(std::time::Duration::new(30, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + + // After the resize, check again that file1 exists and its + // content is "foo". + assert_eq!( + guest.ssh_command("cat mount_dir/file1").unwrap().trim(), + "foo" + ); + } + + if hotplug { + // Remove from VM + guest.ssh_command("sudo umount mount_dir").unwrap(); + assert!(remote_command(&api_socket, "remove-device", Some("myfs0"))); + } + }); + + let r = if r.is_ok() && hotplug { + std::panic::catch_unwind(|| { + thread::sleep(std::time::Duration::new(10, 0)); + let fs_params = format!( + "id=myfs0,tag=myfs,native=true,shared_dir={},cache={},num_queues=1,queue_size=1024{}", + shared_dir.to_str().unwrap(), + "always".to_owned(), + if let Some(pci_segment) = pci_segment { + format!(",pci_segment={}", pci_segment) + } else { + "".to_owned() + } + ); + + // Add back and check it works + let (cmd_success, cmd_output) = + remote_command_w_output(&api_socket, "add-fs", Some(&fs_params)); + assert!(cmd_success); + if let Some(pci_segment) = pci_segment { + assert!(String::from_utf8_lossy(&cmd_output).contains(&format!( + "{{\"id\":\"myfs0\",\"bdf\":\"{:04x}:00:01.0\"}}", + pci_segment + ))); + } else { + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"myfs0\",\"bdf\":\"0000:00:06.0\"}")); + } + + thread::sleep(std::time::Duration::new(10, 0)); + // Mount shared directory through virtio_fs filesystem + guest + .ssh_command("mkdir -p mount_dir && sudo mount -t virtiofs myfs mount_dir/") + .unwrap(); + + // Check file1 exists and its content is "foo" + assert_eq!( + guest.ssh_command("cat mount_dir/file1").unwrap().trim(), + "foo" + ); + }) + } else { + r + }; + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); +} + +fn _test_virtio_fs( + prepare_daemon: &dyn Fn(&TempDir, &str) -> (std::process::Child, String), + hotplug: bool, + pci_segment: Option, +) { + #[cfg(target_arch = "aarch64")] + let focal_image = if hotplug { + FOCAL_IMAGE_UPDATE_KERNEL_NAME.to_string() + } else { + FOCAL_IMAGE_NAME.to_string() + }; + #[cfg(target_arch = "x86_64")] + let focal_image = FOCAL_IMAGE_NAME.to_string(); + let focal = UbuntuDiskConfig::new(focal_image); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut shared_dir = workload_path; + shared_dir.push("shared_dir"); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = if hotplug { + edk2_path() + } else { + direct_kernel_boot_path() + }; + + let (mut daemon_child, virtiofsd_socket_path) = + prepare_daemon(&guest.tmp_dir, shared_dir.to_str().unwrap()); + + let mut guest_command = GuestCommand::new(&guest); + guest_command + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M,hotplug_size=2048M,shared=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--api-socket", &api_socket]); + if pci_segment.is_some() { + guest_command.args(["--platform", "num_pci_segments=16"]); + } + + let fs_params = format!( + "id=myfs0,tag=myfs,socket={},num_queues=1,queue_size=1024{}", + virtiofsd_socket_path, + if let Some(pci_segment) = pci_segment { + format!(",pci_segment={}", pci_segment) + } else { + "".to_owned() + } + ); + + if !hotplug { + guest_command.args(["--fs", fs_params.as_str()]); + } + + let mut child = guest_command.capture_output().spawn().unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + if hotplug { + // Add fs to the VM + let (cmd_success, cmd_output) = + remote_command_w_output(&api_socket, "add-fs", Some(&fs_params)); + assert!(cmd_success); + + if let Some(pci_segment) = pci_segment { + assert!(String::from_utf8_lossy(&cmd_output).contains(&format!( + "{{\"id\":\"myfs0\",\"bdf\":\"{:04x}:00:01.0\"}}", + pci_segment + ))); + } else { + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"myfs0\",\"bdf\":\"0000:00:06.0\"}")); + } + + thread::sleep(std::time::Duration::new(10, 0)); + } + + // Mount shared directory through virtio_fs filesystem + guest + .ssh_command("mkdir -p mount_dir && sudo mount -t virtiofs myfs mount_dir/") + .unwrap(); + + // Check file1 exists and its content is "foo" + assert_eq!( + guest.ssh_command("cat mount_dir/file1").unwrap().trim(), + "foo" + ); + // Check file2 does not exist + guest + .ssh_command("[ ! -f 'mount_dir/file2' ] || true") + .unwrap(); + + // Check file3 exists and its content is "bar" + assert_eq!( + guest.ssh_command("cat mount_dir/file3").unwrap().trim(), + "bar" + ); + + // ACPI feature is needed. + #[cfg(target_arch = "x86_64")] + { + guest.enable_memory_hotplug(); + + // Add RAM to the VM + let desired_ram = 1024 << 20; + resize_command(&api_socket, None, Some(desired_ram), None, None); + + thread::sleep(std::time::Duration::new(30, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + + // After the resize, check again that file1 exists and its + // content is "foo". + assert_eq!( + guest.ssh_command("cat mount_dir/file1").unwrap().trim(), + "foo" + ); + } + + if hotplug { + // Remove from VM + guest.ssh_command("sudo umount mount_dir").unwrap(); + assert!(remote_command(&api_socket, "remove-device", Some("myfs0"))); + } + }); + + let (r, hotplug_daemon_child) = if r.is_ok() && hotplug { + thread::sleep(std::time::Duration::new(10, 0)); + let (daemon_child, virtiofsd_socket_path) = + prepare_daemon(&guest.tmp_dir, shared_dir.to_str().unwrap()); + + let r = std::panic::catch_unwind(|| { + thread::sleep(std::time::Duration::new(10, 0)); + let fs_params = format!( + "id=myfs0,tag=myfs,socket={},num_queues=1,queue_size=1024{}", + virtiofsd_socket_path, + if let Some(pci_segment) = pci_segment { + format!(",pci_segment={}", pci_segment) + } else { + "".to_owned() + } + ); + + // Add back and check it works + let (cmd_success, cmd_output) = + remote_command_w_output(&api_socket, "add-fs", Some(&fs_params)); + assert!(cmd_success); + if let Some(pci_segment) = pci_segment { + assert!(String::from_utf8_lossy(&cmd_output).contains(&format!( + "{{\"id\":\"myfs0\",\"bdf\":\"{:04x}:00:01.0\"}}", + pci_segment + ))); + } else { + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"myfs0\",\"bdf\":\"0000:00:06.0\"}")); + } + + thread::sleep(std::time::Duration::new(10, 0)); + // Mount shared directory through virtio_fs filesystem + guest + .ssh_command("mkdir -p mount_dir && sudo mount -t virtiofs myfs mount_dir/") + .unwrap(); + + // Check file1 exists and its content is "foo" + assert_eq!( + guest.ssh_command("cat mount_dir/file1").unwrap().trim(), + "foo" + ); + }); + + (r, Some(daemon_child)) + } else { + (r, None) + }; + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let _ = daemon_child.kill(); + let _ = daemon_child.wait(); + + if let Some(mut daemon_child) = hotplug_daemon_child { + let _ = daemon_child.kill(); + let _ = daemon_child.wait(); + } + + handle_child_output(r, &output); +} + +fn test_virtio_pmem(discard_writes: bool, specify_size: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + let pmem_temp_file = TempFile::new().unwrap(); + pmem_temp_file.as_file().set_len(128 << 20).unwrap(); + + std::process::Command::new("mkfs.ext4") + .arg(pmem_temp_file.as_path()) + .output() + .expect("Expect creating disk image to succeed"); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args([ + "--pmem", + format!( + "file={}{}{}", + pmem_temp_file.as_path().to_str().unwrap(), + if specify_size { ",size=128M" } else { "" }, + if discard_writes { + ",discard_writes=on" + } else { + "" + } + ) + .as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check for the presence of /dev/pmem0 + assert_eq!( + guest.ssh_command("ls /dev/pmem0").unwrap().trim(), + "/dev/pmem0" + ); + + // Check changes persist after reboot + assert_eq!(guest.ssh_command("sudo mount /dev/pmem0 /mnt").unwrap(), ""); + assert_eq!(guest.ssh_command("ls /mnt").unwrap(), "lost+found\n"); + guest + .ssh_command("echo test123 | sudo tee /mnt/test") + .unwrap(); + assert_eq!(guest.ssh_command("sudo umount /mnt").unwrap(), ""); + assert_eq!(guest.ssh_command("ls /mnt").unwrap(), ""); + + // guest.reboot_linux(0, None); + // assert_eq!(guest.ssh_command("sudo mount /dev/pmem0 /mnt").unwrap(), ""); + // assert_eq!( + // guest + // .ssh_command("sudo cat /mnt/test || true") + // .unwrap() + // .trim(), + // if discard_writes { "" } else { "test123" } + // ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); +} + +fn get_fd_count(pid: u32) -> usize { + fs::read_dir(format!("/proc/{}/fd", pid)).unwrap().count() +} + +fn _test_virtio_vsock(hotplug: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = if hotplug { + edk2_path() + } else { + direct_kernel_boot_path() + }; + + let socket = temp_vsock_path(&guest.tmp_dir); + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--api-socket", &api_socket]); + cmd.args(["--cpus", "boot=1"]); + cmd.args(["--memory", "size=512M"]); + cmd.args(["--kernel", kernel_path.to_str().unwrap()]); + cmd.args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]); + cmd.default_disks(); + cmd.default_net(); + + if !hotplug { + cmd.args(["--vsock", format!("cid=3,socket={}", socket).as_str()]); + } + + let mut child = cmd.capture_output().spawn().unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + if hotplug { + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-vsock", + Some(format!("cid=3,socket={},id=test0", socket).as_str()), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"test0\",\"bdf\":\"0000:00:06.0\"}")); + thread::sleep(std::time::Duration::new(10, 0)); + // Check adding a second one fails + assert!(!remote_command( + &api_socket, + "add-vsock", + Some("cid=1234,socket=/tmp/fail") + )); + } + + // Validate vsock works as expected. + guest.check_vsock(socket.as_str()); + // guest.reboot_linux(0, None); + // Validate vsock still works after a reboot. + // guest.check_vsock(socket.as_str()); + + if hotplug { + assert!(remote_command(&api_socket, "remove-device", Some("test0"))); + } + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); +} + +fn _test_virtio_vsock_passthrough_fd(hotplug: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = if hotplug { + edk2_path() + } else { + direct_kernel_boot_path() + }; + + let socket = temp_vsock_path(&guest.tmp_dir); + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--api-socket", &api_socket]); + cmd.args(["--cpus", "boot=1"]); + cmd.args(["--memory", "size=512M"]); + cmd.args(["--kernel", kernel_path.to_str().unwrap()]); + cmd.args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]); + cmd.default_disks(); + cmd.default_net(); + + if !hotplug { + cmd.args(["--vsock", format!("cid=3,socket={}", socket).as_str()]); + } + + let mut child = cmd.capture_output().spawn().unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + if hotplug { + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-vsock", + Some(format!("cid=3,socket={},id=test0", socket).as_str()), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"test0\",\"bdf\":\"0000:00:06.0\"}")); + thread::sleep(std::time::Duration::new(10, 0)); + } + + // Validate vsock passthrough fd works as expected. + guest.start_vsock_passthrough_fd_listener(); + guest.check_vsock_passthrough_fd_bidirectional( + socket.as_str(), + "HelloWorld!\n", + "HelloWorld!", + ); + + if hotplug { + assert!(remote_command(&api_socket, "remove-device", Some("test0"))); + } + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); +} + +fn get_ksm_pages_shared() -> u32 { + fs::read_to_string("/sys/kernel/mm/ksm/pages_shared") + .unwrap() + .trim() + .parse::() + .unwrap() +} + +fn test_memory_mergeable(mergeable: bool) { + let memory_param = if mergeable { + "mergeable=on" + } else { + "mergeable=off" + }; + + // We are assuming the rest of the system in our CI is not using mergeable memeory + let ksm_ps_init = get_ksm_pages_shared(); + assert!(ksm_ps_init == 0); + + let focal1 = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest1 = Guest::new(Box::new(focal1)); + let mut child1 = GuestCommand::new(&guest1) + .args(["--cpus", "boot=1"]) + .args(["--memory", format!("size=512M,{}", memory_param).as_str()]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", guest1.default_net_string().as_str()]) + .args(["--serial", "tty", "--console", "off"]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest1.wait_vm_boot(None).unwrap(); + }); + if r.is_err() { + let _ = child1.kill(); + let output = child1.wait_with_output().unwrap(); + handle_child_output(r, &output); + panic!("Test should already be failed/panicked"); // To explicitly mark this block never return + } + + let ksm_ps_guest1 = get_ksm_pages_shared(); + + let focal2 = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest2 = Guest::new(Box::new(focal2)); + let mut child2 = GuestCommand::new(&guest2) + .args(["--cpus", "boot=1"]) + .args(["--memory", format!("size=512M,{}", memory_param).as_str()]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", guest2.default_net_string().as_str()]) + .args(["--serial", "tty", "--console", "off"]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest2.wait_vm_boot(None).unwrap(); + let ksm_ps_guest2 = get_ksm_pages_shared(); + + if mergeable { + println!( + "ksm pages_shared after vm1 booted '{}', ksm pages_shared after vm2 booted '{}'", + ksm_ps_guest1, ksm_ps_guest2 + ); + // We are expecting the number of shared pages to increase as the number of VM increases + assert!(ksm_ps_guest1 < ksm_ps_guest2); + } else { + assert!(ksm_ps_guest1 == 0); + assert!(ksm_ps_guest2 == 0); + } + }); + + let _ = child1.kill(); + let _ = child2.kill(); + + let output = child1.wait_with_output().unwrap(); + child2.wait().unwrap(); + + handle_child_output(r, &output); +} + +fn _get_vmm_overhead(pid: u32, guest_memory_size: u32) -> HashMap { + let smaps = fs::File::open(format!("/proc/{}/smaps", pid)).unwrap(); + let reader = io::BufReader::new(smaps); + + let mut skip_map: bool = false; + let mut region_name: String = "".to_string(); + let mut region_maps = HashMap::new(); + for line in reader.lines() { + let l = line.unwrap(); + + if l.contains('-') { + let values: Vec<&str> = l.split_whitespace().collect(); + region_name = values.last().unwrap().trim().to_string(); + if region_name == "0" { + region_name = "anonymous".to_string() + } + } + + // Each section begins with something that looks like: + // Size: 2184 kB + if l.starts_with("Size:") { + let values: Vec<&str> = l.split_whitespace().collect(); + let map_size = values[1].parse::().unwrap(); + // We skip the assigned guest RAM map, its RSS is only + // dependent on the guest actual memory usage. + // Everything else can be added to the VMM overhead. + skip_map = map_size >= guest_memory_size; + continue; + } + + // If this is a map we're taking into account, then we only + // count the RSS. The sum of all counted RSS is the VMM overhead. + if !skip_map && l.starts_with("Rss:") { + let values: Vec<&str> = l.split_whitespace().collect(); + let value = values[1].trim().parse::().unwrap(); + *region_maps.entry(region_name.clone()).or_insert(0) += value; + } + } + + region_maps +} + +fn get_vmm_overhead(pid: u32, guest_memory_size: u32) -> u32 { + let mut total = 0; + + for (region_name, value) in &_get_vmm_overhead(pid, guest_memory_size) { + eprintln!("{}: {}", region_name, value); + total += value; + } + + total +} + +fn process_rss_kib(pid: u32) -> usize { + let command = format!("ps -q {} -o rss=", pid); + let rss = exec_host_command_output(&command); + String::from_utf8_lossy(&rss.stdout).trim().parse().unwrap() +} + +const FREE_PAGE_REPORTING_BASELINE_LIMIT_KIB: usize = 512 * 1024; +const FREE_PAGE_REPORTING_PEAK_DELTA_KIB: usize = 1024 * 1024; +const FREE_PAGE_REPORTING_RELEASE_SLACK_KIB: usize = 384 * 1024; + +fn wait_for_process_rss( + pid: u32, + description: &str, + timeout: Duration, + mut predicate: F, +) -> usize +where + F: FnMut(usize) -> bool, +{ + let deadline = Instant::now() + timeout; + let mut last_rss = process_rss_kib(pid); + loop { + if predicate(last_rss) { + return last_rss; + } + if Instant::now() >= deadline { + panic!("Timed out waiting for {description}; last VMM RSS was {last_rss} KiB"); + } + thread::sleep(Duration::from_secs(1)); + last_rss = process_rss_kib(pid); + } +} + +fn verify_free_page_reporting(guest: &Guest, vmm_pid: u32, phase: &str) { + let baseline = wait_for_process_rss( + vmm_pid, + &format!("{phase} RSS baseline"), + Duration::from_secs(90), + |rss| rss <= FREE_PAGE_REPORTING_BASELINE_LIMIT_KIB, + ); + + let stress_pid = guest + .ssh_command( + "nohup stress --vm 1 --vm-bytes 1536M --vm-keep --timeout 120s \ + >/tmp/free-page-reporting-stress.log 2>&1 () + .unwrap(); + + let peak = wait_for_process_rss( + vmm_pid, + &format!("{phase} RSS increase"), + Duration::from_secs(90), + |rss| rss >= baseline + FREE_PAGE_REPORTING_PEAK_DELTA_KIB, + ); + + guest + .ssh_command(&format!( + "kill -TERM {stress_pid}; \ + for i in $(seq 1 100); do \ + kill -0 {stress_pid} 2>/dev/null || exit 0; \ + sleep 0.1; \ + done; \ + kill -KILL {stress_pid}" + )) + .unwrap(); + + let released = wait_for_process_rss( + vmm_pid, + &format!("{phase} RSS reclamation"), + Duration::from_secs(90), + |rss| rss <= baseline + FREE_PAGE_REPORTING_RELEASE_SLACK_KIB, + ); + + println!( + "Free page reporting ({phase}): baseline={baseline} KiB peak={peak} KiB released={released} KiB" + ); +} + +// 10MB is our maximum accepted overhead. +const MAXIMUM_VMM_OVERHEAD_KB: u32 = 10 * 1024; + +#[derive(PartialEq, Eq, PartialOrd)] +struct Counters { + rx_bytes: u64, + rx_frames: u64, + tx_bytes: u64, + tx_frames: u64, + read_bytes: u64, + write_bytes: u64, + read_ops: u64, + write_ops: u64, +} + +fn get_counters(api_socket: &str) -> Counters { + // Get counters + let (cmd_success, cmd_output) = remote_command_w_output(api_socket, "counters", None); + assert!(cmd_success); + + let counters: HashMap<&str, HashMap<&str, u64>> = + serde_json::from_slice(&cmd_output).unwrap_or_default(); + + let rx_bytes = *counters.get("_net2").unwrap().get("rx_bytes").unwrap(); + let rx_frames = *counters.get("_net2").unwrap().get("rx_frames").unwrap(); + let tx_bytes = *counters.get("_net2").unwrap().get("tx_bytes").unwrap(); + let tx_frames = *counters.get("_net2").unwrap().get("tx_frames").unwrap(); + + let read_bytes = *counters.get("_disk0").unwrap().get("read_bytes").unwrap(); + let write_bytes = *counters.get("_disk0").unwrap().get("write_bytes").unwrap(); + let read_ops = *counters.get("_disk0").unwrap().get("read_ops").unwrap(); + let write_ops = *counters.get("_disk0").unwrap().get("write_ops").unwrap(); + + Counters { + rx_bytes, + rx_frames, + tx_bytes, + tx_frames, + read_bytes, + write_bytes, + read_ops, + write_ops, + } +} + +fn pty_read(mut pty: std::fs::File) -> Receiver { + let (tx, rx) = mpsc::channel::(); + thread::spawn(move || loop { + thread::sleep(std::time::Duration::new(1, 0)); + let mut buf = [0; 512]; + match pty.read(&mut buf) { + Ok(_) => { + let output = std::str::from_utf8(&buf).unwrap().to_string(); + match tx.send(output) { + Ok(_) => (), + Err(_) => break, + } + } + Err(_) => break, + } + }); + rx +} + +fn get_pty_path(api_socket: &str, pty_type: &str) -> PathBuf { + let (cmd_success, cmd_output) = remote_command_w_output(api_socket, "info", None); + assert!(cmd_success); + let info: serde_json::Value = serde_json::from_slice(&cmd_output).unwrap_or_default(); + assert_eq!("Pty", info["config"][pty_type]["mode"]); + PathBuf::from( + info["config"][pty_type]["file"] + .as_str() + .expect("Missing pty path"), + ) +} + +// VFIO test network setup. +// We reserve a different IP class for it: 172.18.0.0/24. +#[cfg(target_arch = "x86_64")] +fn setup_vfio_network_interfaces() { + // 'vfio-br0' + assert!(exec_host_command_status("sudo ip link add name vfio-br0 type bridge").success()); + assert!(exec_host_command_status("sudo ip link set vfio-br0 up").success()); + assert!(exec_host_command_status("sudo ip addr add 172.18.0.1/24 dev vfio-br0").success()); + // 'vfio-tap0' + assert!(exec_host_command_status("sudo ip tuntap add vfio-tap0 mode tap").success()); + assert!(exec_host_command_status("sudo ip link set vfio-tap0 master vfio-br0").success()); + assert!(exec_host_command_status("sudo ip link set vfio-tap0 up").success()); + // 'vfio-tap1' + assert!(exec_host_command_status("sudo ip tuntap add vfio-tap1 mode tap").success()); + assert!(exec_host_command_status("sudo ip link set vfio-tap1 master vfio-br0").success()); + assert!(exec_host_command_status("sudo ip link set vfio-tap1 up").success()); + // 'vfio-tap2' + assert!(exec_host_command_status("sudo ip tuntap add vfio-tap2 mode tap").success()); + assert!(exec_host_command_status("sudo ip link set vfio-tap2 master vfio-br0").success()); + assert!(exec_host_command_status("sudo ip link set vfio-tap2 up").success()); + // 'vfio-tap3' + assert!(exec_host_command_status("sudo ip tuntap add vfio-tap3 mode tap").success()); + assert!(exec_host_command_status("sudo ip link set vfio-tap3 master vfio-br0").success()); + assert!(exec_host_command_status("sudo ip link set vfio-tap3 up").success()); +} + +// Tear VFIO test network down +#[cfg(target_arch = "x86_64")] +fn cleanup_vfio_network_interfaces() { + assert!(exec_host_command_status("sudo ip link del vfio-br0").success()); + assert!(exec_host_command_status("sudo ip link del vfio-tap0").success()); + assert!(exec_host_command_status("sudo ip link del vfio-tap1").success()); + assert!(exec_host_command_status("sudo ip link del vfio-tap2").success()); + assert!(exec_host_command_status("sudo ip link del vfio-tap3").success()); +} + +fn balloon_size(api_socket: &str) -> u64 { + let (cmd_success, cmd_output) = remote_command_w_output(api_socket, "info", None); + assert!(cmd_success); + + let info: serde_json::Value = serde_json::from_slice(&cmd_output).unwrap_or_default(); + let total_mem = &info["config"]["memory"]["size"] + .to_string() + .parse::() + .unwrap(); + let actual_mem = &info["memory_actual_size"] + .to_string() + .parse::() + .unwrap(); + total_mem - actual_mem +} + +// This test validates that it can find the virtio-iommu device at first. +// It also verifies that both disks and the network card are attached to +// the virtual IOMMU by looking at /sys/kernel/iommu_groups directory. +// The last interesting part of this test is that it exercises the network +// interface attached to the virtual IOMMU since this is the one used to +// send all commands through SSH. +fn _test_virtio_iommu(acpi: bool) { + // Virtio-iommu support is ready in recent kernel (v5.14). But the kernel in + // Focal image is still old. + // So if ACPI is enabled on AArch64, we use a modified Focal image in which + // the kernel binary has been updated. + #[cfg(target_arch = "aarch64")] + let focal_image = FOCAL_IMAGE_UPDATE_KERNEL_NAME.to_string(); + #[cfg(target_arch = "x86_64")] + let focal_image = FOCAL_IMAGE_NAME.to_string(); + let focal = UbuntuDiskConfig::new(focal_image); + let guest = Guest::new(Box::new(focal)); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = if acpi { + edk2_path() + } else { + direct_kernel_boot_path() + }; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args([ + "--disk", + format!( + "path={},iommu=on", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "path={},iommu=on", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + ]) + .args(["--net", guest.default_net_string_w_iommu().as_str()]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Verify the virtio-iommu device is present. + assert!(guest + .does_device_vendor_pair_match("0x1057", "0x1af4") + .unwrap_or_default()); + + // On AArch64, if the guest system boots from FDT, the behavior of IOMMU is a bit + // different with ACPI. + // All devices on the PCI bus will be attached to the virtual IOMMU, except the + // virtio-iommu device itself. So these devices will all be added to IOMMU groups, + // and appear under folder '/sys/kernel/iommu_groups/'. + // The result is, in the case of FDT, IOMMU group '0' contains "0000:00:01.0" + // which is the console. The first disk "0000:00:02.0" is in group '1'. + // While on ACPI, console device is not attached to IOMMU. So the IOMMU group '0' + // contains "0000:00:02.0" which is the first disk. + // + // Verify the iommu group of the first disk. + let iommu_group = !acpi as i32; + assert_eq!( + guest + .ssh_command( + format!("ls /sys/kernel/iommu_groups/{}/devices", iommu_group).as_str() + ) + .unwrap() + .trim(), + "0000:00:02.0" + ); + + // Verify the iommu group of the second disk. + let iommu_group = if acpi { 1 } else { 2 }; + assert_eq!( + guest + .ssh_command( + format!("ls /sys/kernel/iommu_groups/{}/devices", iommu_group).as_str() + ) + .unwrap() + .trim(), + "0000:00:03.0" + ); + + // Verify the iommu group of the network card. + let iommu_group = if acpi { 2 } else { 3 }; + assert_eq!( + guest + .ssh_command( + format!("ls /sys/kernel/iommu_groups/{}/devices", iommu_group).as_str() + ) + .unwrap() + .trim(), + "0000:00:04.0" + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); +} + +fn get_reboot_count(guest: &Guest) -> u32 { + guest + .ssh_command("sudo last | grep -c reboot") + .unwrap() + .trim() + .parse::() + .unwrap_or_default() +} + +fn enable_guest_watchdog(guest: &Guest, watchdog_sec: u32) { + // Check for PCI device + assert!(guest + .does_device_vendor_pair_match("0x1063", "0x1af4") + .unwrap_or_default()); + + // Enable systemd watchdog + guest + .ssh_command(&format!( + "echo RuntimeWatchdogSec={}s | sudo tee -a /etc/systemd/system.conf", + watchdog_sec + )) + .unwrap(); +} + +fn snapshot_and_check_events(api_socket: &str, snapshot_dir: &str, event_path: &str) { + snapshot_with_extra_args_and_check_events(api_socket, snapshot_dir, event_path, &[]); +} + +// Incremental (pagemap_anon) snapshot: dest must already contain a +// memory-ranges base file. Only CoW anonymous pages are overwritten. +fn incremental_snapshot_and_check_events(api_socket: &str, snapshot_dir: &str, event_path: &str) { + snapshot_with_extra_args_and_check_events( + api_socket, + snapshot_dir, + event_path, + &["--snapshot-type", "incremental"], + ); +} + +fn snapshot_with_extra_args_and_check_events( + api_socket: &str, + snapshot_dir: &str, + event_path: &str, + extra_args: &[&str], +) { + // Pause the VM + assert!(remote_command(api_socket, "pause", None)); + let latest_events = [ + &MetaEvent { + event: "pausing".to_string(), + device_id: None, + }, + &MetaEvent { + event: "paused".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact(&latest_events, event_path)); + + let url = format!("file://{}", snapshot_dir); + let mut args = Vec::with_capacity(1 + extra_args.len()); + args.push(url.as_str()); + args.extend_from_slice(extra_args); + assert!(remote_command_w_args(api_socket, "snapshot", &args)); + + // Wait to make sure the snapshot is completed + thread::sleep(std::time::Duration::new(10, 0)); + + let latest_events = [ + &MetaEvent { + event: "snapshotting".to_string(), + device_id: None, + }, + &MetaEvent { + event: "snapshotted".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact(&latest_events, event_path)); +} + +fn assert_memory_ranges_full_size(snapshot_dir: &str, mem_params: &str) { + let memory_ranges = std::path::Path::new(snapshot_dir).join("memory-ranges"); + assert!( + memory_ranges.exists(), + "incremental snapshot did not produce {}", + memory_ranges.display() + ); + let mem_config = MemoryConfig::parse(mem_params, None).unwrap(); + let file_len = std::fs::metadata(&memory_ranges).unwrap().len(); + assert_eq!( + file_len, mem_config.size, + "memory-ranges logical size {} != guest RAM {}", + file_len, mem_config.size + ); +} + +#[allow(clippy::too_many_arguments)] +fn _test_restore_from_config( + binary_path: &str, + guest: &Guest, + snapshot_dir: String, + vsock_conf: Option, + net_conf: Option>, + disk_conf: Option>, + pmem_conf: Option>, + fs_conf: Option>, +) { + let api_socket_restored = format!("{}.2", temp_api_path(&guest.tmp_dir)); + let event_path_restored = format!("{}.2", temp_event_monitor_path(&guest.tmp_dir)); + let console_text = String::from("On a branch floating down river a cricket, singing."); + + let restore_config = RestoreConfig { + source_url: PathBuf::from(format!("file://{}", snapshot_dir)), + vsock: vsock_conf.clone(), + net: net_conf.clone(), + disks: disk_conf.clone(), + pmem: pmem_conf.clone(), + fs: fs_conf.clone(), + ..Default::default() + }; + + // Start the hypervisor + let mut child = GuestCommand::new_with_binary_path(guest, binary_path) + .args(["--api-socket", &api_socket_restored]) + .args([ + "--event-monitor", + format!("path={}", event_path_restored).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(1, 0)); + + // Restore the VM from the snapshot + let body = serde_json::to_string(&restore_config).unwrap(); + curl_command( + api_socket_restored.as_str(), + "PUT", + "http://localhost/api/v1/vm.restore", + Some(body.as_str()), + ); + // Wait for the VM to be restored + thread::sleep(std::time::Duration::new(10, 0)); + let r = std::panic::catch_unwind(|| { + // Automatically restart the VM after it has been restored + let latest_events = [ + &MetaEvent { + event: "restored".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resuming".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resumed".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact( + &latest_events, + &event_path_restored + )); + + let (cmd_success, cmd_output) = + remote_command_w_output(api_socket_restored.as_str(), "info", None); + assert!(cmd_success); + println!( + "cmd_output: {}", + String::from_utf8_lossy(&cmd_output).trim() + ); + + guest.check_devices_common(None, Some(&console_text), None); + if let Some(config) = vsock_conf { + let socket = String::from(config.socket.to_str().unwrap()); + assert!(String::from_utf8_lossy(&cmd_output) + .trim() + .contains(format!("\"socket\":\"{}\"", socket.as_str()).as_str())); + // Check vsock + guest.check_devices_common(Some(&socket), None, None); + } + if let Some(config_list) = net_conf { + for config in config_list { + if let Some(tap_name) = config.tap { + // Check tap from RestoreConfig + let tap_count = exec_host_command_output(&format!( + "ip link | grep -c {}", + tap_name.as_str() + )); + assert_eq!(String::from_utf8_lossy(&tap_count.stdout).trim(), "1"); + assert!(String::from_utf8_lossy(&cmd_output) + .trim() + .contains(format!("\"tap\":\"{}\"", tap_name.as_str()).as_str())); + } + } + } + if let Some(config_list) = disk_conf { + for config in config_list { + if let Some(path) = config.path { + assert!(String::from_utf8_lossy(&cmd_output) + .trim() + .contains(format!("\"path\":\"{}\"", path.to_str().unwrap()).as_str())); + } + } + } + if let Some(config_list) = pmem_conf { + for (index, config) in config_list.iter().enumerate() { + let pmem_path = format!("/dev/pmem{}", index); + + let pmem_file = String::from(config.file.to_str().unwrap()); + assert!(String::from_utf8_lossy(&cmd_output) + .trim() + .contains(format!("\"file\":\"{}\"", pmem_file.as_str()).as_str())); + // Check pmem + guest.check_devices_common(None, None, Some(&pmem_path)); + } + } + if let Some(config_list) = fs_conf { + for config in config_list { + if let Some(backend) = config.backendfs_config.as_ref() { + assert!(String::from_utf8_lossy(&cmd_output).trim().contains( + format!("\"shared_dir\":\"{}\"", backend.shared_dir.as_str()).as_str() + )); + } + } + } + }); + // Shutdown the target VM and check console output + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&output.stdout).contains(&console_text)); + }); + + handle_child_output(r, &output); +} + +fn _test_snapshot_restore_from_different_binary( + snapshot_binary_path: &str, + restore_binary_path: &str, +) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + + let api_socket_source = format!("{}.1", temp_api_path(&guest.tmp_dir)); + let vsock_id = "_vsock0"; + + let net_id = "net123"; + let net_params = format!( + "id={},tap=,mac={},ip={},mask=255.255.255.0", + net_id, guest.network.guest_mac, guest.network.host_ip + ); + + let socket = temp_vsock_path(&guest.tmp_dir); + let event_path = temp_event_monitor_path(&guest.tmp_dir); + + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + // Prepare native virtiofs + let mut shared_dir = workload_path.clone(); + shared_dir.push("shared_dir"); + let fs_params = format!( + "id=myfs0,tag=myfs,native=true,shared_dir={},cache=always,num_queues=1,queue_size=1024", + shared_dir.to_str().unwrap() + ); + + // check_devices_common() needs two disks, we need to mount a empty disk here. + let mut blk_file_path = workload_path.clone(); + blk_file_path.push("blk.img"); + + let mut child = GuestCommand::new_with_binary_path(&guest, snapshot_binary_path) + .args(["--api-socket", &api_socket_source]) + .args(["--event-monitor", format!("path={}", event_path).as_str()]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=1024M,hotplug_method=acpi,hotplug_size=4G"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args([ + "--disk", + format!( + "id=cloudinit,path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + format!("id=blk-0,path={}", blk_file_path.to_str().unwrap()).as_str(), + ]) + .args([ + "--pmem", + format!( + "file={},size={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap(), + fs::metadata(guest.disk_config.disk(DiskType::OperatingSystem).unwrap()) + .unwrap() + .len() + ) + .as_str(), + ]) + .args([ + "--cmdline", + DIRECT_KERNEL_BOOT_CMDLINE + .replace("vda1", "pmem0p1") + .as_str(), + ]) + .args(["--fs", fs_params.as_str()]) + .args(["--serial", "tty", "--console", "tty"]) + .args(["--net", net_params.as_str()]) + .args([ + "--vsock", + format!("cid=3,id={},socket={}", vsock_id, socket).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + let console_text = String::from("On a branch floating down river a cricket, singing."); + // Create the snapshot directory + let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check the number of vCPUs + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 1); + + // Check the guest virtio-devices, e.g. block, rng, vsock, console, and net + guest.check_devices_common(Some(&socket), Some(&console_text), None); + + snapshot_and_check_events( + api_socket_source.as_str(), + snapshot_dir.as_str(), + event_path.as_str(), + ); + }); + + // Shutdown the source VM and check console output + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&output.stdout).contains(&console_text)); + }); + + handle_child_output(r, &output); + + // Remove the vsock socket file. + Command::new("rm") + .arg("-f") + .arg(socket.as_str()) + .output() + .unwrap(); + + // Prepare restore config + // vsock config + let socket_restored = format!("{}.2", temp_vsock_path(&guest.tmp_dir)); + let vsock_config = + VsockConfig::parse(format!("cid=3,id={},socket={}", vsock_id, socket_restored).as_str()) + .unwrap(); + + // net config + let tap_name_restored = "restore-tap"; + let net_params_restored = format!( + "id={},tap={},mac={},ip={},mask=255.255.255.0", + net_id, tap_name_restored, guest.network.guest_mac, guest.network.host_ip + ); + let net_config = NetConfig::parse(net_params_restored.as_str()).unwrap(); + + // Pre-create restore-tap since v1.0.9 can't create a named tap + assert!(exec_host_command_status( + format!("sudo ip tuntap add {} mode tap", tap_name_restored).as_str() + ) + .success()); + assert!(exec_host_command_status( + format!( + "sudo ip link set dev {} address {}", + tap_name_restored, guest.network.guest_mac + ) + .as_str() + ) + .success()); + assert!(exec_host_command_status( + format!( + "sudo ip addr add {}/24 dev {}", + guest.network.host_ip, tap_name_restored + ) + .as_str() + ) + .success()); + assert!(exec_host_command_status( + format!("sudo ip link set {} up", tap_name_restored).as_str() + ) + .success()); + + // disk config + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + let mut blk_file_path = workload_path.clone(); + blk_file_path.push("blk.img"); + + let disk_path_restored = + String::from(guest.tmp_dir.as_path().join("blk2.img").to_str().unwrap()); + rate_limited_copy(blk_file_path, &disk_path_restored) + .expect("copying of OS source disk image failed"); + let disk_params_restored = format!("id=blk-0,path={}", disk_path_restored); + let disk_config = DiskConfig::parse(disk_params_restored.as_str()).unwrap(); + + // fs config + let mut shared_dir_restored = workload_path.clone(); + shared_dir_restored.push("restored_shared_dir"); + let fs_params = format!( + "id=myfs0,tag=myfs,native=true,shared_dir={},cache=always,num_queues=1,queue_size=1024", + shared_dir_restored.to_str().unwrap() + ); + let fs_config = FsConfig::parse(fs_params.as_str()).unwrap(); + + _test_restore_from_config( + restore_binary_path, + &guest, + snapshot_dir, + Some(vsock_config), + Some(vec![net_config]), + Some(vec![disk_config]), + None, + Some(vec![fs_config]), + ); + assert!( + exec_host_command_status(format!("sudo ip link del {}", tap_name_restored).as_str()) + .success() + ); +} + +mod common_parallel { + use std::{fs::OpenOptions, io::SeekFrom}; + + use crate::*; + + #[test] + #[cfg(target_arch = "x86_64")] + #[ignore = "PVM host does not support hypervisor-fw/OVMF firmware boot chain"] + fn test_bionic_hypervisor_fw() { + test_simple_launch(fw_path(FwType::RustHypervisorFirmware), BIONIC_IMAGE_NAME) + } + + #[test] + #[cfg(target_arch = "x86_64")] + #[ignore = "PVM host does not support hypervisor-fw/OVMF firmware boot chain"] + fn test_focal_hypervisor_fw() { + test_simple_launch(fw_path(FwType::RustHypervisorFirmware), FOCAL_IMAGE_NAME) + } + + #[test] + #[cfg(target_arch = "x86_64")] + #[ignore = "PVM host does not support hypervisor-fw/OVMF firmware boot chain"] + fn test_bionic_ovmf() { + test_simple_launch(fw_path(FwType::Ovmf), BIONIC_IMAGE_NAME) + } + + #[test] + #[cfg(target_arch = "x86_64")] + #[ignore = "PVM host does not support hypervisor-fw/OVMF firmware boot chain"] + fn test_focal_ovmf() { + test_simple_launch(fw_path(FwType::Ovmf), FOCAL_IMAGE_NAME) + } + + #[cfg(target_arch = "x86_64")] + fn test_simple_launch(fw_path: String, disk_path: &str) { + let disk_config = Box::new(UbuntuDiskConfig::new(disk_path.to_string())); + let guest = Guest::new(disk_config); + let event_path = temp_event_monitor_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", fw_path.as_str()]) + .default_disks() + .default_net() + .args(["--serial", "tty", "--console", "off"]) + .args(["--event-monitor", format!("path={}", event_path).as_str()]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(Some(120)).unwrap(); + + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 1); + assert_eq!(guest.get_initial_apicid().unwrap_or(1), 0); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + assert_eq!(guest.get_pci_bridge_class().unwrap_or_default(), "0x060000"); + + let expected_sequential_events = [ + &MetaEvent { + event: "starting".to_string(), + device_id: None, + }, + &MetaEvent { + event: "booting".to_string(), + device_id: None, + }, + &MetaEvent { + event: "booted".to_string(), + device_id: None, + }, + &MetaEvent { + event: "activated".to_string(), + device_id: Some("_disk0".to_string()), + }, + &MetaEvent { + event: "reset".to_string(), + device_id: Some("_disk0".to_string()), + }, + ]; + assert!(check_sequential_events( + &expected_sequential_events, + &event_path + )); + + // It's been observed on the Bionic image that udev and snapd + // services can cause some delay in the VM's shutdown. Disabling + // them improves the reliability of this test. + let _ = guest.ssh_command("sudo systemctl disable udev"); + let _ = guest.ssh_command("sudo systemctl stop udev"); + let _ = guest.ssh_command("sudo systemctl disable snapd"); + let _ = guest.ssh_command("sudo systemctl stop snapd"); + + guest.ssh_command("sudo poweroff").unwrap(); + thread::sleep(std::time::Duration::new(10, 0)); + let latest_events = [ + &MetaEvent { + event: "shutdown".to_string(), + device_id: None, + }, + &MetaEvent { + event: "deleted".to_string(), + device_id: None, + }, + &MetaEvent { + event: "shutdown".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact(&latest_events, &event_path)); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_multi_cpu() { + let bionic = UbuntuDiskConfig::new(BIONIC_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(bionic)); + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--cpus", "boot=2,max=4"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .capture_output() + .default_disks() + .default_net(); + + let mut child = cmd.spawn().unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(Some(120)).unwrap(); + + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 2); + + #[cfg(target_arch = "x86_64")] + { + let dmesg_output = guest + .ssh_command(r#"sudo dmesg | grep -o "Allowing.*hotplug CPU[s]*""#) + .unwrap(); + let trimmed = dmesg_output.trim(); + assert!( + trimmed.contains("2 hotplug CPU"), + "Expected dmesg to contain '2 hotplug CPU', got: '{}'", + trimmed + ); + } + #[cfg(target_arch = "aarch64")] + assert_eq!( + guest + .ssh_command(r#"dmesg | grep "smp: Brought up" | sed "s/\[\ *[0-9.]*\] //""#) + .unwrap() + .trim(), + "smp: Brought up 1 node, 2 CPUs" + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_cpu_topology_421() { + test_cpu_topology(4, 2, 1, false); + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_cpu_topology_142() { + test_cpu_topology(1, 4, 2, false); + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_cpu_topology_262() { + test_cpu_topology(2, 6, 2, false); + } + + #[test] + #[cfg(target_arch = "x86_64")] + #[cfg(not(feature = "mshv"))] + fn test_cpu_physical_bits() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let max_phys_bits: u8 = 36; + let mut child = GuestCommand::new(&guest) + .args(["--cpus", &format!("max_phys_bits={}", max_phys_bits)]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert!( + guest + .ssh_command("lscpu | grep \"Address sizes:\" | cut -f 2 -d \":\" | sed \"s# *##\" | cut -f 1 -d \" \"") + .unwrap() + .trim() + .parse::() + .unwrap_or(max_phys_bits + 1) <= max_phys_bits, + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_cpu_affinity() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + // We need the host to have at least 4 CPUs if we want to be able + // to run this test. + let host_cpus_count = exec_host_command_output("nproc"); + assert!( + String::from_utf8_lossy(&host_cpus_count.stdout) + .trim() + .parse::() + .unwrap_or(0) + >= 4 + ); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=2,affinity=[0@[0,2],1@[1,3]]"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + let pid = child.id(); + let taskset_vcpu0 = exec_host_command_output(format!("taskset -pc $(ps -T -p {} | grep vcpu0 | xargs | cut -f 2 -d \" \") | cut -f 6 -d \" \"", pid).as_str()); + assert_eq!(String::from_utf8_lossy(&taskset_vcpu0.stdout).trim(), "0,2"); + let taskset_vcpu1 = exec_host_command_output(format!("taskset -pc $(ps -T -p {} | grep vcpu1 | xargs | cut -f 2 -d \" \") | cut -f 6 -d \" \"", pid).as_str()); + assert_eq!(String::from_utf8_lossy(&taskset_vcpu1.stdout).trim(), "1,3"); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_large_vm() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--cpus", "boot=48"]) + .args(["--memory", "size=5120M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .capture_output() + .default_disks() + .default_net(); + + let mut child = cmd.spawn().unwrap(); + + guest.wait_vm_boot(None).unwrap(); + + let r = std::panic::catch_unwind(|| { + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 48); + assert_eq!( + guest + .ssh_command("lscpu | grep \"On-line\" | cut -f 2 -d \":\" | sed \"s# *##\"") + .unwrap() + .trim(), + "0-47" + ); + + assert!(guest.get_total_memory().unwrap_or_default() > 5_000_000); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_huge_memory() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--cpus", "boot=1"]) + .args(["--memory", "size=128G"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .capture_output() + .default_disks() + .default_net(); + + let mut child = cmd.spawn().unwrap(); + + guest.wait_vm_boot(Some(120)).unwrap(); + + let r = std::panic::catch_unwind(|| { + assert!(guest.get_total_memory().unwrap_or_default() > 128_000_000); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_power_button() { + _test_power_button(false); + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_user_defined_memory_regions() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=0,hotplug_method=virtio-mem"]) + .args([ + "--memory-zone", + "id=mem0,size=1G,hotplug_size=2G", + "id=mem1,size=1G,file=/dev/shm", + "id=mem2,size=1G,host_numa_node=0,hotplug_size=2G", + ]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--api-socket", &api_socket]) + .capture_output() + .default_disks() + .default_net() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert!(guest.get_total_memory().unwrap_or_default() > 2_880_000); + + guest.enable_memory_hotplug(); + + resize_zone_command(&api_socket, "mem0", "3G"); + thread::sleep(std::time::Duration::new(5, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 4_800_000); + resize_zone_command(&api_socket, "mem2", "3G"); + thread::sleep(std::time::Duration::new(5, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 6_720_000); + resize_zone_command(&api_socket, "mem0", "2G"); + thread::sleep(std::time::Duration::new(5, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 5_760_000); + resize_zone_command(&api_socket, "mem2", "2G"); + thread::sleep(std::time::Duration::new(5, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 4_800_000); + + // guest.reboot_linux(0, None); + // + // // Check the amount of RAM after reboot + // assert!(guest.get_total_memory().unwrap_or_default() > 4_800_000); + // assert!(guest.get_total_memory().unwrap_or_default() < 5_760_000); + // + // // Check if we can still resize down to the initial 'boot'size + // resize_zone_command(&api_socket, "mem0", "1G"); + // thread::sleep(std::time::Duration::new(5, 0)); + // assert!(guest.get_total_memory().unwrap_or_default() < 4_800_000); + // resize_zone_command(&api_socket, "mem2", "1G"); + // thread::sleep(std::time::Duration::new(5, 0)); + // assert!(guest.get_total_memory().unwrap_or_default() < 3_840_000); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_guest_numa_nodes() { + _test_guest_numa_nodes(false); + } + + #[test] + #[cfg(target_arch = "x86_64")] + fn test_iommu_segments() { + let focal_image = FOCAL_IMAGE_NAME.to_string(); + let focal = UbuntuDiskConfig::new(focal_image); + let guest = Guest::new(Box::new(focal)); + + // Prepare another disk file for the virtio-disk device + let test_disk_path = String::from( + guest + .tmp_dir + .as_path() + .join("test-disk.raw") + .to_str() + .unwrap(), + ); + assert!( + exec_host_command_status(format!("truncate {} -s 4M", test_disk_path).as_str()) + .success() + ); + assert!( + exec_host_command_status(format!("mkfs.ext4 {}", test_disk_path).as_str()).success() + ); + + let api_socket = temp_api_path(&guest.tmp_dir); + let mut cmd = GuestCommand::new(&guest); + + cmd.args(["--cpus", "boot=1"]) + .args(["--api-socket", &api_socket]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--platform", "num_pci_segments=16,iommu_segments=[1]"]) + .default_disks() + .capture_output() + .default_net(); + + let mut child = cmd.spawn().unwrap(); + + guest.wait_vm_boot(None).unwrap(); + + let r = std::panic::catch_unwind(|| { + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-disk", + Some( + format!( + "path={},id=test0,pci_segment=1,iommu=on", + test_disk_path.as_str() + ) + .as_str(), + ), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"test0\",\"bdf\":\"0001:00:01.0\"}")); + + // Check IOMMU setup + assert!(guest + .does_device_vendor_pair_match("0x1057", "0x1af4") + .unwrap_or_default()); + assert_eq!( + guest + .ssh_command("ls /sys/kernel/iommu_groups/0/devices") + .unwrap() + .trim(), + "0001:00:01.0" + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_pci_msi() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .capture_output() + .default_disks() + .default_net(); + + let mut child = cmd.spawn().unwrap(); + + guest.wait_vm_boot(None).unwrap(); + + #[cfg(target_arch = "x86_64")] + let grep_cmd = "grep -c PCI-MSI /proc/interrupts"; + #[cfg(target_arch = "aarch64")] + let grep_cmd = "grep -c ITS-MSI /proc/interrupts"; + + let r = std::panic::catch_unwind(|| { + assert_eq!( + guest + .ssh_command(grep_cmd) + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 12 + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_virtio_net_ctrl_queue() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--net", guest.default_net_string_w_mtu(3000).as_str()]) + .capture_output() + .default_disks(); + + let mut child = cmd.spawn().unwrap(); + + guest.wait_vm_boot(None).unwrap(); + + #[cfg(target_arch = "aarch64")] + let iface = "enp0s4"; + #[cfg(target_arch = "x86_64")] + let iface = "ens4"; + + let r = std::panic::catch_unwind(|| { + assert_eq!( + guest + .ssh_command( + format!("sudo ethtool -K {} rx-gro-hw off && echo success", iface).as_str() + ) + .unwrap() + .trim(), + "success" + ); + assert_eq!( + guest + .ssh_command(format!("cat /sys/class/net/{}/mtu", iface).as_str()) + .unwrap() + .trim(), + "3000" + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_pci_multiple_segments() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + // Prepare another disk file for the virtio-disk device + let test_disk_path = String::from( + guest + .tmp_dir + .as_path() + .join("test-disk.raw") + .to_str() + .unwrap(), + ); + assert!( + exec_host_command_status(format!("truncate {} -s 4M", test_disk_path).as_str()) + .success() + ); + assert!( + exec_host_command_status(format!("mkfs.ext4 {}", test_disk_path).as_str()).success() + ); + + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--platform", "num_pci_segments=16"]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + format!("path={},pci_segment=15", test_disk_path).as_str(), + ]) + .capture_output() + .default_net(); + + let mut child = cmd.spawn().unwrap(); + + guest.wait_vm_boot(None).unwrap(); + + let grep_cmd = "lspci | grep \"Host bridge\" | wc -l"; + + let r = std::panic::catch_unwind(|| { + // There should be 16 PCI host bridges in the guest. + assert_eq!( + guest + .ssh_command(grep_cmd) + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 16 + ); + + // Check both if /dev/vdc exists and if the block size is 4M. + assert_eq!( + guest + .ssh_command("lsblk | grep vdc | grep -c 4M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Mount the device. + guest.ssh_command("mkdir mount_image").unwrap(); + guest + .ssh_command("sudo mount -o rw -t ext4 /dev/vdc mount_image/") + .unwrap(); + // Grant all users with write permission. + guest.ssh_command("sudo chmod a+w mount_image/").unwrap(); + + // Write something to the device. + guest + .ssh_command("sudo echo \"bar\" >> mount_image/foo") + .unwrap(); + + // Check the content of the block device. The file "foo" should + // contain "bar". + assert_eq!( + guest + .ssh_command("sudo cat mount_image/foo") + .unwrap() + .trim(), + "bar" + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_direct_kernel_boot() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 1); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + let grep_cmd = if cfg!(target_arch = "x86_64") { + "grep -c PCI-MSI /proc/interrupts" + } else { + "grep -c ITS-MSI /proc/interrupts" + }; + assert_eq!( + guest + .ssh_command(grep_cmd) + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 12 + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + fn _test_virtio_block(image_name: &str, disable_io_uring: bool) { + let focal = UbuntuDiskConfig::new(image_name.to_string()); + let guest = Guest::new(Box::new(focal)); + + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut blk_file_path = workload_path; + blk_file_path.push("blk.img"); + + let kernel_path = direct_kernel_boot_path(); + + let mut cloud_child = GuestCommand::new(&guest) + .args(["--cpus", "boot=4"]) + .args(["--memory", "size=512M,shared=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + format!( + "path={},readonly=on,direct=on,num_queues=4,_disable_io_uring={}", + blk_file_path.to_str().unwrap(), + disable_io_uring + ) + .as_str(), + ]) + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check both if /dev/vdc exists and if the block size is 16M. + assert_eq!( + guest + .ssh_command("lsblk | grep vdc | grep -c 16M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Check both if /dev/vdc exists and if this block is RO. + assert_eq!( + guest + .ssh_command("lsblk | grep vdc | awk '{print $5}'") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Check if the number of queues is 4. + assert_eq!( + guest + .ssh_command("ls -ll /sys/block/vdc/mq | grep ^d | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 4 + ); + }); + + let _ = cloud_child.kill(); + let output = cloud_child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_virtio_block() { + _test_virtio_block(FOCAL_IMAGE_NAME, false) + } + + #[test] + fn test_virtio_block_disable_io_uring() { + _test_virtio_block(FOCAL_IMAGE_NAME, true) + } + + #[test] + fn test_virtio_block_qcow2() { + _test_virtio_block(FOCAL_IMAGE_NAME_QCOW2, false) + } + + #[test] + fn test_virtio_block_vhd() { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut raw_file_path = workload_path.clone(); + let mut vhd_file_path = workload_path; + raw_file_path.push(FOCAL_IMAGE_NAME); + vhd_file_path.push(FOCAL_IMAGE_NAME_VHD); + + // Generate VHD file from RAW file + std::process::Command::new("qemu-img") + .arg("convert") + .arg("-p") + .args(["-f", "raw"]) + .args(["-O", "vpc"]) + .args(["-o", "subformat=fixed"]) + .arg(raw_file_path.to_str().unwrap()) + .arg(vhd_file_path.to_str().unwrap()) + .output() + .expect("Expect generating VHD image from RAW image"); + + _test_virtio_block(FOCAL_IMAGE_NAME_VHD, false) + } + + #[test] + #[ignore = "PVM: vhdx toolchain/firmware compatibility (aligned with intranet cube skip)"] + fn test_virtio_block_vhdx() { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut raw_file_path = workload_path.clone(); + let mut vhdx_file_path = workload_path; + raw_file_path.push(FOCAL_IMAGE_NAME); + vhdx_file_path.push(FOCAL_IMAGE_NAME_VHDX); + + // Generate dynamic VHDX file from RAW file + std::process::Command::new("qemu-img") + .arg("convert") + .arg("-p") + .args(["-f", "raw"]) + .args(["-O", "vhdx"]) + .arg(raw_file_path.to_str().unwrap()) + .arg(vhdx_file_path.to_str().unwrap()) + .output() + .expect("Expect generating dynamic VHDx image from RAW image"); + + _test_virtio_block(FOCAL_IMAGE_NAME_VHDX, false) + } + + #[test] + fn test_virtio_block_dynamic_vhdx_expand() { + const VIRTUAL_DISK_SIZE: u64 = 100 << 20; + const EMPTY_VHDX_FILE_SIZE: u64 = 8 << 20; + const FULL_VHDX_FILE_SIZE: u64 = 112 << 20; + const DYNAMIC_VHDX_NAME: &str = "dynamic.vhdx"; + + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut vhdx_file_path = workload_path; + vhdx_file_path.push(DYNAMIC_VHDX_NAME); + let vhdx_path = vhdx_file_path.to_str().unwrap(); + + // Generate a 100 MiB dynamic VHDX file + std::process::Command::new("qemu-img") + .arg("create") + .args(["-f", "vhdx"]) + .arg(vhdx_path) + .arg(VIRTUAL_DISK_SIZE.to_string()) + .output() + .expect("Expect generating dynamic VHDx image from RAW image"); + + // Check if the size matches with empty VHDx file size + assert_eq!(vhdx_image_size(vhdx_path), EMPTY_VHDX_FILE_SIZE); + + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + + let mut cloud_child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + format!("path={}", vhdx_path).as_str(), + ]) + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check both if /dev/vdc exists and if the block size is 100 MiB. + assert_eq!( + guest + .ssh_command("lsblk | grep vdc | grep -c 100M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Write 100 MB of data to the VHDx disk + guest + .ssh_command("sudo dd if=/dev/urandom of=/dev/vdc bs=1M count=100") + .unwrap(); + }); + + // Check if the size matches with expected expanded VHDx file size + assert_eq!(vhdx_image_size(vhdx_path), FULL_VHDX_FILE_SIZE); + + let _ = cloud_child.kill(); + let output = cloud_child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + fn vhdx_image_size(disk_name: &str) -> u64 { + std::fs::File::open(disk_name) + .unwrap() + .seek(SeekFrom::End(0)) + .unwrap() + } + + #[test] + #[ignore = "PVM host does not support firmware boot"] + fn test_virtio_block_direct_and_firmware() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + // The OS disk must be copied to a location that is not backed by + // tmpfs, otherwise the syscall openat(2) with O_DIRECT simply fails + // with EINVAL because tmpfs doesn't support this flag. + let mut workloads_path = dirs::home_dir().unwrap(); + workloads_path.push("workloads"); + let os_dir = TempDir::new_in(workloads_path.as_path()).unwrap(); + let mut os_path = os_dir.as_path().to_path_buf(); + os_path.push("osdisk.img"); + rate_limited_copy( + guest.disk_config.disk(DiskType::OperatingSystem).unwrap(), + os_path.as_path(), + ) + .expect("copying of OS disk failed"); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", fw_path(FwType::RustHypervisorFirmware).as_str()]) + .args([ + "--disk", + format!("path={},direct=on", os_path.as_path().to_str().unwrap()).as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + ]) + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(Some(120)).unwrap(); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_vhost_user_net_default() { + test_vhost_user_net(None, 2, &prepare_vhost_user_net_daemon, false, false) + } + + #[test] + fn test_vhost_user_net_named_tap() { + test_vhost_user_net( + Some("mytap0"), + 2, + &prepare_vhost_user_net_daemon, + false, + false, + ) + } + + #[test] + fn test_vhost_user_net_existing_tap() { + test_vhost_user_net( + Some("vunet-tap0"), + 2, + &prepare_vhost_user_net_daemon, + false, + false, + ) + } + + #[test] + fn test_vhost_user_net_multiple_queues() { + test_vhost_user_net(None, 4, &prepare_vhost_user_net_daemon, false, false) + } + + #[test] + fn test_vhost_user_net_tap_multiple_queues() { + test_vhost_user_net( + Some("vunet-tap1"), + 4, + &prepare_vhost_user_net_daemon, + false, + false, + ) + } + + #[test] + fn test_vhost_user_net_host_mac() { + test_vhost_user_net(None, 2, &prepare_vhost_user_net_daemon, true, false) + } + + #[test] + fn test_vhost_user_net_client_mode() { + test_vhost_user_net(None, 2, &prepare_vhost_user_net_daemon, false, true) + } + + #[test] + fn test_vhost_user_blk_default() { + test_vhost_user_blk(2, false, false, Some(&prepare_vubd)) + } + + #[test] + fn test_vhost_user_blk_readonly() { + test_vhost_user_blk(1, true, false, Some(&prepare_vubd)) + } + + #[test] + fn test_vhost_user_blk_direct() { + test_vhost_user_blk(1, false, true, Some(&prepare_vubd)) + } + + #[test] + fn test_boot_from_vhost_user_blk_default() { + test_boot_from_vhost_user_blk(1, false, false, Some(&prepare_vubd)) + } + + #[test] + #[cfg(target_arch = "x86_64")] + fn test_split_irqchip() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert_eq!( + guest + .ssh_command("grep -c IO-APIC.*timer /proc/interrupts || true") + .unwrap() + .trim() + .parse::() + .unwrap_or(1), + 0 + ); + assert_eq!( + guest + .ssh_command("grep -c IO-APIC.*cascade /proc/interrupts || true") + .unwrap() + .trim() + .parse::() + .unwrap_or(1), + 0 + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(target_arch = "x86_64")] + fn test_dmi_serial_number() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--platform", "serial_number=a=b;c=d"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert_eq!( + guest + .ssh_command("sudo cat /sys/class/dmi/id/product_serial") + .unwrap() + .trim(), + "a=b;c=d" + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(target_arch = "x86_64")] + fn test_dmi_uuid() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--platform", "uuid=1e8aa28a-435d-4027-87f4-40dceff1fa0a"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert_eq!( + guest + .ssh_command("sudo cat /sys/class/dmi/id/product_uuid") + .unwrap() + .trim(), + "1e8aa28a-435d-4027-87f4-40dceff1fa0a" + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(target_arch = "x86_64")] + fn test_dmi_oem_strings() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let s1 = "io.systemd.credential:xx=yy"; + let s2 = "This is a test string"; + + let oem_strings = format!("oem_strings=[{},{}]", s1, s2); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--platform", &oem_strings]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert_eq!( + guest + .ssh_command("sudo dmidecode --oem-string count") + .unwrap() + .trim(), + "2" + ); + + assert_eq!( + guest + .ssh_command("sudo dmidecode --oem-string 1") + .unwrap() + .trim(), + s1 + ); + + assert_eq!( + guest + .ssh_command("sudo dmidecode --oem-string 2") + .unwrap() + .trim(), + s2 + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_virtio_fs() { + _test_virtio_fs(&prepare_virtiofsd, false, None) + } + + #[test] + fn test_virtio_fs_hotplug() { + _test_virtio_fs(&prepare_virtiofsd, true, None) + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_virtio_fs_multi_segment_hotplug() { + _test_virtio_fs(&prepare_virtiofsd, true, Some(15)) + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_virtio_fs_multi_segment() { + _test_virtio_fs(&prepare_virtiofsd, false, Some(15)) + } + + #[test] + fn test_native_virtio_fs() { + _test_native_virtio_fs(false, None) + } + + #[test] + fn test_native_virtio_fs_hotplug() { + _test_native_virtio_fs(true, None) + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_native_virtio_fs_multi_segment() { + _test_native_virtio_fs(false, Some(15)) + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_native_virtio_fs_multi_segment_hotplug() { + _test_native_virtio_fs(true, Some(15)) + } + + #[test] + fn test_virtio_pmem_persist_writes() { + test_virtio_pmem(false, false) + } + + #[test] + fn test_virtio_pmem_discard_writes() { + test_virtio_pmem(true, false) + } + + #[test] + fn test_virtio_pmem_with_size() { + test_virtio_pmem(true, true) + } + + #[test] + fn test_boot_from_virtio_pmem() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + ]) + .default_net() + .args([ + "--pmem", + format!( + "file={},size={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap(), + fs::metadata(guest.disk_config.disk(DiskType::OperatingSystem).unwrap()) + .unwrap() + .len() + ) + .as_str(), + ]) + .args([ + "--cmdline", + DIRECT_KERNEL_BOOT_CMDLINE + .replace("vda1", "pmem0p1") + .as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Simple checks to validate the VM booted properly + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 1); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_multiple_network_interfaces() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args([ + "--net", + guest.default_net_string().as_str(), + "tap=,mac=8a:6b:6f:5a:de:ac,ip=192.168.3.1,mask=255.255.255.0", + "tap=mytap1,mac=fe:1f:9e:e1:60:f2,ip=192.168.4.1,mask=255.255.255.0", + ]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + let tap_count = exec_host_command_output("ip link | grep -c mytap1"); + assert_eq!(String::from_utf8_lossy(&tap_count.stdout).trim(), "1"); + + // 3 network interfaces + default localhost ==> 4 interfaces + assert_eq!( + guest + .ssh_command("ip -o link | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 4 + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(target_arch = "aarch64")] + fn test_pmu_on() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Test that PMU exists. + assert_eq!( + guest + .ssh_command(GREP_PMU_IRQ_CMD) + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_serial_off() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--serial", "off"]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Test that there is no ttyS0 + assert_eq!( + guest + .ssh_command(GREP_SERIAL_IRQ_CMD) + .unwrap() + .trim() + .parse::() + .unwrap_or(1), + 0 + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_serial_null() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let mut cmd = GuestCommand::new(&guest); + #[cfg(target_arch = "x86_64")] + let console_str: &str = "console=ttyS0"; + #[cfg(target_arch = "aarch64")] + let console_str: &str = "console=ttyAMA0"; + + cmd.args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args([ + "--cmdline", + DIRECT_KERNEL_BOOT_CMDLINE + .replace("console=hvc0 ", console_str) + .as_str(), + ]) + .default_disks() + .default_net() + .args(["--serial", "null"]) + .args(["--console", "off"]) + .capture_output(); + + let mut child = cmd.spawn().unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Test that there is a ttyS0 + assert_eq!( + guest + .ssh_command(GREP_SERIAL_IRQ_CMD) + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + assert!(!String::from_utf8_lossy(&output.stdout).contains(CONSOLE_TEST_STRING)); + }); + + handle_child_output(r, &output); + } + + #[test] + fn test_serial_tty() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + #[cfg(target_arch = "x86_64")] + let console_str: &str = "console=ttyS0"; + #[cfg(target_arch = "aarch64")] + let console_str: &str = "console=ttyAMA0"; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args([ + "--cmdline", + DIRECT_KERNEL_BOOT_CMDLINE + .replace("console=hvc0 ", console_str) + .as_str(), + ]) + .default_disks() + .default_net() + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Test that there is a ttyS0 + assert_eq!( + guest + .ssh_command(GREP_SERIAL_IRQ_CMD) + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + }); + + // This sleep is needed to wait for the login prompt + thread::sleep(std::time::Duration::new(2, 0)); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&output.stdout).contains(CONSOLE_TEST_STRING)); + }); + + handle_child_output(r, &output); + } + + #[test] + fn test_serial_file() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let serial_path = guest.tmp_dir.as_path().join("serial-output"); + #[cfg(target_arch = "x86_64")] + let console_str: &str = "console=ttyS0"; + #[cfg(target_arch = "aarch64")] + let console_str: &str = "console=ttyAMA0"; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args([ + "--cmdline", + DIRECT_KERNEL_BOOT_CMDLINE + .replace("console=hvc0 ", console_str) + .as_str(), + ]) + .default_disks() + .default_net() + .args([ + "--serial", + format!("file={}", serial_path.to_str().unwrap()).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Test that there is a ttyS0 + assert_eq!( + guest + .ssh_command(GREP_SERIAL_IRQ_CMD) + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + guest.ssh_command("sudo shutdown -h now").unwrap(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(20)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + // Check that the cloud-hypervisor binary actually terminated + assert!(output.status.success()); + + // Do this check after shutdown of the VM as an easy way to ensure + // all writes are flushed to disk + let mut f = std::fs::File::open(serial_path).unwrap(); + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + assert!(buf.contains(CONSOLE_TEST_STRING)); + }); + + handle_child_output(r, &output); + } + + #[test] + fn test_pty_interaction() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + let serial_option = if cfg!(target_arch = "x86_64") { + " console=ttyS0" + } else { + " console=ttyAMA0" + }; + let cmdline = DIRECT_KERNEL_BOOT_CMDLINE.to_owned() + serial_option; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", &cmdline]) + .default_disks() + .default_net() + .args(["--serial", "null"]) + .args(["--console", "pty"]) + .args(["--api-socket", &api_socket]) + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + // Get pty fd for console + let console_path = get_pty_path(&api_socket, "console"); + // TODO: Get serial pty test working + let mut cf = std::fs::OpenOptions::new() + .write(true) + .read(true) + .open(console_path) + .unwrap(); + + // Some dumb sleeps but we don't want to write + // before the console is up and we don't want + // to try and write the next line before the + // login process is ready. + thread::sleep(std::time::Duration::new(5, 0)); + assert_eq!(cf.write(b"cloud\n").unwrap(), 6); + thread::sleep(std::time::Duration::new(2, 0)); + assert_eq!(cf.write(b"cloud123\n").unwrap(), 9); + thread::sleep(std::time::Duration::new(2, 0)); + assert_eq!(cf.write(b"echo test_pty_console\n").unwrap(), 22); + thread::sleep(std::time::Duration::new(2, 0)); + + // read pty and ensure they have a login shell + // some fairly hacky workarounds to avoid looping + // forever in case the channel is blocked getting output + let ptyc = pty_read(cf); + let mut empty = 0; + let mut prev = String::new(); + loop { + thread::sleep(std::time::Duration::new(2, 0)); + match ptyc.try_recv() { + Ok(line) => { + empty = 0; + prev = prev + &line; + if prev.contains("test_pty_console") { + break; + } + } + Err(mpsc::TryRecvError::Empty) => { + empty += 1; + assert!(empty <= 5, "No login on pty"); + } + _ => panic!("No login on pty"), + } + } + + guest.ssh_command("sudo shutdown -h now").unwrap(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(20)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + // Check that the cloud-hypervisor binary actually terminated + assert!(output.status.success()) + }); + handle_child_output(r, &output); + } + + #[test] + fn test_virtio_console() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--console", "tty"]) + .args(["--serial", "null"]) + .capture_output() + .spawn() + .unwrap(); + + let text = String::from("On a branch floating down river a cricket, singing."); + let cmd = format!("echo {} | sudo tee /dev/hvc0", text); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert!(guest + .does_device_vendor_pair_match("0x1043", "0x1af4") + .unwrap_or_default()); + + guest.ssh_command(&cmd).unwrap(); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&output.stdout).contains(&text)); + }); + + handle_child_output(r, &output); + } + + #[test] + fn test_console_file() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let console_path = guest.tmp_dir.as_path().join("console-output"); + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args([ + "--console", + format!("file={}", console_path.to_str().unwrap()).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + guest.wait_vm_boot(None).unwrap(); + + guest.ssh_command("sudo shutdown -h now").unwrap(); + + let _ = child.wait_timeout(std::time::Duration::from_secs(20)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let r = std::panic::catch_unwind(|| { + // Check that the cloud-hypervisor binary actually terminated + assert!(output.status.success()); + + // Do this check after shutdown of the VM as an easy way to ensure + // all writes are flushed to disk + let mut f = std::fs::File::open(console_path).unwrap(); + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + if !buf.contains(CONSOLE_TEST_STRING) { + eprintln!( + "\n\n==== Console file output ====\n\n{}\n\n==== End console file output ====", + buf + ); + } + assert!(buf.contains(CONSOLE_TEST_STRING)); + }); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(target_arch = "x86_64")] + #[cfg(not(feature = "mshv"))] + #[ignore = "See #4324"] + // The VFIO integration test starts cloud-hypervisor guest with 3 TAP + // backed networking interfaces, bound through a simple bridge on the host. + // So if the nested cloud-hypervisor succeeds in getting a directly + // assigned interface from its cloud-hypervisor host, we should be able to + // ssh into it, and verify that it's running with the right kernel command + // line (We tag the command line from cloud-hypervisor for that purpose). + // The third device is added to validate that hotplug works correctly since + // it is being added to the L2 VM through hotplugging mechanism. + // Also, we pass-through a vitio-blk device to the L2 VM to test the 32-bit + // vfio device support + fn test_vfio() { + setup_vfio_network_interfaces(); + + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new_from_ip_range(Box::new(focal), "172.18", 0); + + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let kernel_path = direct_kernel_boot_path(); + + let mut vfio_path = workload_path.clone(); + vfio_path.push("vfio"); + + let mut cloud_init_vfio_base_path = vfio_path.clone(); + cloud_init_vfio_base_path.push("cloudinit.img"); + + // We copy our cloudinit into the vfio mount point, for the nested + // cloud-hypervisor guest to use. + rate_limited_copy( + guest.disk_config.disk(DiskType::CloudInit).unwrap(), + &cloud_init_vfio_base_path, + ) + .expect("copying of cloud-init disk failed"); + + let mut vfio_disk_path = workload_path.clone(); + vfio_disk_path.push("vfio.img"); + + // Create the vfio disk image + let output = Command::new("mkfs.ext4") + .arg("-d") + .arg(vfio_path.to_str().unwrap()) + .arg(vfio_disk_path.to_str().unwrap()) + .arg("2g") + .output() + .unwrap(); + if !output.status.success() { + eprintln!("{}", String::from_utf8_lossy(&output.stderr)); + panic!("mkfs.ext4 command generated an error"); + } + + let mut blk_file_path = workload_path; + blk_file_path.push("blk.img"); + + let vfio_tap0 = "vfio-tap0"; + let vfio_tap1 = "vfio-tap1"; + let vfio_tap2 = "vfio-tap2"; + let vfio_tap3 = "vfio-tap3"; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=4"]) + .args(["--memory", "size=2G,hugepages=on,shared=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + format!("path={}", vfio_disk_path.to_str().unwrap()).as_str(), + format!("path={},iommu=on", blk_file_path.to_str().unwrap()).as_str(), + ]) + .args([ + "--cmdline", + format!( + "{} kvm-intel.nested=1 vfio_iommu_type1.allow_unsafe_interrupts", + DIRECT_KERNEL_BOOT_CMDLINE + ) + .as_str(), + ]) + .args([ + "--net", + format!("tap={},mac={}", vfio_tap0, guest.network.guest_mac).as_str(), + format!( + "tap={},mac={},iommu=on", + vfio_tap1, guest.network.l2_guest_mac1 + ) + .as_str(), + format!( + "tap={},mac={},iommu=on", + vfio_tap2, guest.network.l2_guest_mac2 + ) + .as_str(), + format!( + "tap={},mac={},iommu=on", + vfio_tap3, guest.network.l2_guest_mac3 + ) + .as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(30, 0)); + + let r = std::panic::catch_unwind(|| { + guest.ssh_command_l1("sudo systemctl start vfio").unwrap(); + thread::sleep(std::time::Duration::new(120, 0)); + + // We booted our cloud hypervisor L2 guest with a "VFIOTAG" tag + // added to its kernel command line. + // Let's ssh into it and verify that it's there. If it is it means + // we're in the right guest (The L2 one) because the QEMU L1 guest + // does not have this command line tag. + assert_eq!( + guest + .ssh_command_l2_1("grep -c VFIOTAG /proc/cmdline") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Let's also verify from the second virtio-net device passed to + // the L2 VM. + assert_eq!( + guest + .ssh_command_l2_2("grep -c VFIOTAG /proc/cmdline") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Check the amount of PCI devices appearing in L2 VM. + assert_eq!( + guest + .ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 8, + ); + + // Check both if /dev/vdc exists and if the block size is 16M in L2 VM + assert_eq!( + guest + .ssh_command_l2_1("lsblk | grep vdc | grep -c 16M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Hotplug an extra virtio-net device through L2 VM. + guest + .ssh_command_l1( + "echo 0000:00:09.0 | sudo tee /sys/bus/pci/devices/0000:00:09.0/driver/unbind", + ) + .unwrap(); + guest + .ssh_command_l1("echo 0000:00:09.0 | sudo tee /sys/bus/pci/drivers/vfio-pci/bind") + .unwrap(); + let vfio_hotplug_output = guest + .ssh_command_l1( + "sudo /mnt/ch-remote \ + --api-socket=/tmp/ch_api.sock \ + add-device path=/sys/bus/pci/devices/0000:00:09.0,id=vfio123", + ) + .unwrap(); + assert!(vfio_hotplug_output.contains("{\"id\":\"vfio123\",\"bdf\":\"0000:00:08.0\"}")); + + thread::sleep(std::time::Duration::new(10, 0)); + + // Let's also verify from the third virtio-net device passed to + // the L2 VM. This third device has been hotplugged through the L2 + // VM, so this is our way to validate hotplug works for VFIO PCI. + assert_eq!( + guest + .ssh_command_l2_3("grep -c VFIOTAG /proc/cmdline") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Check the amount of PCI devices appearing in L2 VM. + // There should be one more device than before, raising the count + // up to 9 PCI devices. + assert_eq!( + guest + .ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 9, + ); + + // Let's now verify that we can correctly remove the virtio-net + // device through the "remove-device" command responsible for + // unplugging VFIO devices. + guest + .ssh_command_l1( + "sudo /mnt/ch-remote \ + --api-socket=/tmp/ch_api.sock \ + remove-device vfio123", + ) + .unwrap(); + thread::sleep(std::time::Duration::new(10, 0)); + + // Check the amount of PCI devices appearing in L2 VM is back down + // to 8 devices. + assert_eq!( + guest + .ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 8, + ); + + // Perform memory hotplug in L2 and validate the memory is showing + // up as expected. In order to check, we will use the virtio-net + // device already passed through L2 as a VFIO device, this will + // verify that VFIO devices are functional with memory hotplug. + assert!(guest.get_total_memory_l2().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + guest + .ssh_command_l2_1( + "sudo bash -c 'echo online > /sys/devices/system/memory/auto_online_blocks'", + ) + .unwrap(); + guest + .ssh_command_l1( + "sudo /mnt/ch-remote \ + --api-socket=/tmp/ch_api.sock \ + resize --memory=1073741824", + ) + .unwrap(); + assert!(guest.get_total_memory_l2().unwrap_or_default() > 960_000); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + cleanup_vfio_network_interfaces(); + + handle_child_output(r, &output); + } + + #[test] + #[ignore = "PVM guest: TSC deadline timer is disabled; with acpi=off, no clockevent works (HPET/ACPI PM-Timer unavailable, 8259 PIC not emulated), kernel boot is expected to hang"] + fn test_direct_kernel_boot_noacpi() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args([ + "--cmdline", + format!("{} acpi=off", DIRECT_KERNEL_BOOT_CMDLINE).as_str(), + ]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 1); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_virtio_vsock() { + _test_virtio_vsock(false) + } + + #[test] + fn test_virtio_vsock_hotplug() { + _test_virtio_vsock(true); + } + + #[test] + fn test_virtio_vsock_passthrough_fd() { + _test_virtio_vsock_passthrough_fd(false); + } + + #[test] + fn test_virtio_vsock_passthrough_fd_hotplug() { + _test_virtio_vsock_passthrough_fd(true); + } + + #[test] + // Start cloud-hypervisor with no VM parameters, only the API server running. + // From the API: Create a VM, boot it and check that it looks as expected. + fn test_api_create_boot() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(1, 0)); + + // Verify API server is running + curl_command(&api_socket, "GET", "http://localhost/api/v1/vmm.ping", None); + + // Create the VM first + let cpu_count: u8 = 4; + let http_body = guest.api_create_body( + cpu_count, + direct_kernel_boot_path().to_str().unwrap(), + DIRECT_KERNEL_BOOT_CMDLINE, + ); + + let temp_config_path = guest.tmp_dir.as_path().join("config"); + std::fs::write(&temp_config_path, http_body).unwrap(); + + remote_command( + &api_socket, + "create", + Some(temp_config_path.as_os_str().to_str().unwrap()), + ); + + // Then boot it + remote_command(&api_socket, "boot", None); + thread::sleep(std::time::Duration::new(20, 0)); + + let r = std::panic::catch_unwind(|| { + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + // Start cloud-hypervisor with no VM parameters, only the API server running. + // From the API: Create a VM, boot it and check it can be shutdown and then + // booted again + fn test_api_shutdown() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(1, 0)); + + // Verify API server is running + curl_command(&api_socket, "GET", "http://localhost/api/v1/vmm.ping", None); + + // Create the VM first + let cpu_count: u8 = 4; + let http_body = guest.api_create_body( + cpu_count, + direct_kernel_boot_path().to_str().unwrap(), + DIRECT_KERNEL_BOOT_CMDLINE, + ); + + let r = std::panic::catch_unwind(|| { + curl_command( + &api_socket, + "PUT", + "http://localhost/api/v1/vm.create", + Some(&http_body), + ); + + // Then boot it + curl_command(&api_socket, "PUT", "http://localhost/api/v1/vm.boot", None); + + guest.wait_vm_boot(None).unwrap(); + + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + // Sync and shutdown without powering off to prevent filesystem + // corruption. + guest.ssh_command("sync").unwrap(); + guest.ssh_command("sudo shutdown -H now").unwrap(); + + // Wait for the guest to be fully shutdown + thread::sleep(std::time::Duration::new(20, 0)); + + // Then shut it down + curl_command( + &api_socket, + "PUT", + "http://localhost/api/v1/vm.shutdown", + None, + ); + + // Then boot it again + curl_command(&api_socket, "PUT", "http://localhost/api/v1/vm.boot", None); + + guest.wait_vm_boot(None).unwrap(); + + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + // Start cloud-hypervisor with no VM parameters, only the API server running. + // From the API: Create a VM, boot it and check it can be deleted and then recreated + // booted again. + fn test_api_delete() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(1, 0)); + + // Verify API server is running + curl_command(&api_socket, "GET", "http://localhost/api/v1/vmm.ping", None); + + // Create the VM first + let cpu_count: u8 = 4; + let http_body = guest.api_create_body( + cpu_count, + direct_kernel_boot_path().to_str().unwrap(), + DIRECT_KERNEL_BOOT_CMDLINE, + ); + + let r = std::panic::catch_unwind(|| { + curl_command( + &api_socket, + "PUT", + "http://localhost/api/v1/vm.create", + Some(&http_body), + ); + + // Then boot it + curl_command(&api_socket, "PUT", "http://localhost/api/v1/vm.boot", None); + + guest.wait_vm_boot(None).unwrap(); + + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + // Sync and shutdown without powering off to prevent filesystem + // corruption. + guest.ssh_command("sync").unwrap(); + guest.ssh_command("sudo shutdown -H now").unwrap(); + + // Wait for the guest to be fully shutdown + thread::sleep(std::time::Duration::new(20, 0)); + + // Then delete it + curl_command( + &api_socket, + "PUT", + "http://localhost/api/v1/vm.delete", + None, + ); + + curl_command( + &api_socket, + "PUT", + "http://localhost/api/v1/vm.create", + Some(&http_body), + ); + + // Then boot it again + curl_command(&api_socket, "PUT", "http://localhost/api/v1/vm.boot", None); + + guest.wait_vm_boot(None).unwrap(); + + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + // Start cloud-hypervisor with no VM parameters, only the API server running. + // From the API: Create a VM, boot it and check that it looks as expected. + // Then we pause the VM, check that it's no longer available. + // Finally we resume the VM and check that it's available. + fn test_api_pause_resume() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(1, 0)); + + // Verify API server is running + curl_command(&api_socket, "GET", "http://localhost/api/v1/vmm.ping", None); + + // Create the VM first + let cpu_count: u8 = 4; + let http_body = guest.api_create_body( + cpu_count, + direct_kernel_boot_path().to_str().unwrap(), + DIRECT_KERNEL_BOOT_CMDLINE, + ); + curl_command( + &api_socket, + "PUT", + "http://localhost/api/v1/vm.create", + Some(&http_body), + ); + + // Then boot it + curl_command(&api_socket, "PUT", "http://localhost/api/v1/vm.boot", None); + thread::sleep(std::time::Duration::new(20, 0)); + + let r = std::panic::catch_unwind(|| { + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + // We now pause the VM + assert!(remote_command(&api_socket, "pause", None)); + + // Check pausing again fails + assert!(!remote_command(&api_socket, "pause", None)); + + thread::sleep(std::time::Duration::new(2, 0)); + + // SSH into the VM should fail + assert!(ssh_command_ip( + "grep -c processor /proc/cpuinfo", + &guest.network.guest_ip, + 2, + 5 + ) + .is_err()); + + // Resume the VM + assert!(remote_command(&api_socket, "resume", None)); + + // Check resuming again fails + assert!(!remote_command(&api_socket, "resume", None)); + + thread::sleep(std::time::Duration::new(2, 0)); + + // Now we should be able to SSH back in and get the right number of CPUs + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_virtio_iommu() { + _test_virtio_iommu(cfg!(target_arch = "x86_64")) + } + + #[test] + // We cannot force the software running in the guest to reprogram the BAR + // with some different addresses, but we have a reliable way of testing it + // with a standard Linux kernel. + // By removing a device from the PCI tree, and then rescanning the tree, + // Linux consistently chooses to reorganize the PCI device BARs to other + // locations in the guest address space. + // This test creates a dedicated PCI network device to be checked as being + // properly probed first, then removing it, and adding it again by doing a + // rescan. + fn test_pci_bar_reprogramming() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = edk2_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args([ + "--net", + guest.default_net_string().as_str(), + "tap=,mac=8a:6b:6f:5a:de:ac,ip=192.168.3.1,mask=255.255.255.0", + ]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // 2 network interfaces + default localhost ==> 3 interfaces + assert_eq!( + guest + .ssh_command("ip -o link | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 3 + ); + + let init_bar_addr = guest + .ssh_command( + "sudo awk '{print $1; exit}' /sys/bus/pci/devices/0000:00:05.0/resource", + ) + .unwrap(); + + // Remove the PCI device + guest + .ssh_command("echo 1 | sudo tee /sys/bus/pci/devices/0000:00:05.0/remove") + .unwrap(); + + // Only 1 network interface left + default localhost ==> 2 interfaces + assert_eq!( + guest + .ssh_command("ip -o link | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 2 + ); + + // Remove the PCI device + guest + .ssh_command("echo 1 | sudo tee /sys/bus/pci/rescan") + .unwrap(); + + // Back to 2 network interface + default localhost ==> 3 interfaces + assert_eq!( + guest + .ssh_command("ip -o link | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 3 + ); + + let new_bar_addr = guest + .ssh_command( + "sudo awk '{print $1; exit}' /sys/bus/pci/devices/0000:00:05.0/resource", + ) + .unwrap(); + + // Let's compare the BAR addresses for our virtio-net device. + // They should be different as we expect the BAR reprogramming + // to have happened. + assert_ne!(init_bar_addr, new_bar_addr); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_memory_mergeable_off() { + test_memory_mergeable(false) + } + + #[test] + #[cfg(target_arch = "x86_64")] + fn test_cpu_hotplug() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=2,max=4"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 2); + + // Resize the VM + let desired_vcpus = 4; + resize_command(&api_socket, Some(desired_vcpus), None, None, None); + + guest + .ssh_command("echo 1 | sudo tee /sys/bus/cpu/devices/cpu2/online") + .unwrap(); + guest + .ssh_command("echo 1 | sudo tee /sys/bus/cpu/devices/cpu3/online") + .unwrap(); + thread::sleep(std::time::Duration::new(10, 0)); + assert_eq!( + guest.get_cpu_count().unwrap_or_default(), + u32::from(desired_vcpus) + ); + + // guest.reboot_linux(0, None); + // + // assert_eq!( + // guest.get_cpu_count().unwrap_or_default(), + // u32::from(desired_vcpus) + // ); + // + // // Resize the VM + // let desired_vcpus = 2; + // resize_command(&api_socket, Some(desired_vcpus), None, None, None); + // + // thread::sleep(std::time::Duration::new(10, 0)); + // assert_eq!( + // guest.get_cpu_count().unwrap_or_default(), + // u32::from(desired_vcpus) + // ); + // + // // Resize the VM back up to 4 + // let desired_vcpus = 4; + // resize_command(&api_socket, Some(desired_vcpus), None, None, None); + // + // guest + // .ssh_command("echo 1 | sudo tee /sys/bus/cpu/devices/cpu2/online") + // .unwrap(); + // guest + // .ssh_command("echo 1 | sudo tee /sys/bus/cpu/devices/cpu3/online") + // .unwrap(); + // thread::sleep(std::time::Duration::new(10, 0)); + // assert_eq!( + // guest.get_cpu_count().unwrap_or_default(), + // u32::from(desired_vcpus) + // ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_memory_hotplug() { + #[cfg(target_arch = "aarch64")] + let focal_image = FOCAL_IMAGE_UPDATE_KERNEL_NAME.to_string(); + #[cfg(target_arch = "x86_64")] + let focal_image = FOCAL_IMAGE_NAME.to_string(); + let focal = UbuntuDiskConfig::new(focal_image); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + #[cfg(target_arch = "aarch64")] + let kernel_path = edk2_path(); + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=2,max=4"]) + .args(["--memory", "size=512M,hotplug_size=8192M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--balloon", "size=0"]) + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + guest.enable_memory_hotplug(); + + // Add RAM to the VM + let desired_ram = 1024 << 20; + resize_command(&api_socket, None, Some(desired_ram), None, None); + + thread::sleep(std::time::Duration::new(10, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + + // Use balloon to remove RAM from the VM + let desired_balloon = 512 << 20; + resize_command(&api_socket, None, None, Some(desired_balloon), None); + + thread::sleep(std::time::Duration::new(10, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + assert!(guest.get_total_memory().unwrap_or_default() < 960_000); + + // guest.reboot_linux(0, None); + // + // assert!(guest.get_total_memory().unwrap_or_default() < 960_000); + // + // // Use balloon add RAM to the VM + // let desired_balloon = 0; + // resize_command(&api_socket, None, None, Some(desired_balloon), None); + // + // thread::sleep(std::time::Duration::new(10, 0)); + // + // assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + // + // guest.enable_memory_hotplug(); + // + // // Add RAM to the VM + // let desired_ram = 2048 << 20; + // resize_command(&api_socket, None, Some(desired_ram), None, None); + // + // thread::sleep(std::time::Duration::new(10, 0)); + // assert!(guest.get_total_memory().unwrap_or_default() > 1_920_000); + // + // // Remove RAM to the VM (only applies after reboot) + // let desired_ram = 1024 << 20; + // resize_command(&api_socket, None, Some(desired_ram), None, None); + // + // guest.reboot_linux(1, None); + // + // assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + // assert!(guest.get_total_memory().unwrap_or_default() < 1_920_000); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_virtio_mem() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=2,max=4"]) + .args([ + "--memory", + "size=512M,hotplug_method=virtio-mem,hotplug_size=8192M", + ]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + guest.enable_memory_hotplug(); + + // Add RAM to the VM + let desired_ram = 1024 << 20; + resize_command(&api_socket, None, Some(desired_ram), None, None); + + thread::sleep(std::time::Duration::new(10, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + + // Add RAM to the VM + let desired_ram = 2048 << 20; + resize_command(&api_socket, None, Some(desired_ram), None, None); + + thread::sleep(std::time::Duration::new(10, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 1_920_000); + + // Remove RAM from the VM + let desired_ram = 1024 << 20; + resize_command(&api_socket, None, Some(desired_ram), None, None); + + thread::sleep(std::time::Duration::new(10, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + assert!(guest.get_total_memory().unwrap_or_default() < 1_920_000); + + // guest.reboot_linux(0, None); + // + // // Check the amount of memory after reboot is 1GiB + // assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + // assert!(guest.get_total_memory().unwrap_or_default() < 1_920_000); + // + // // Check we can still resize to 512MiB + // let desired_ram = 512 << 20; + // resize_command(&api_socket, None, Some(desired_ram), None, None); + // thread::sleep(std::time::Duration::new(10, 0)); + // assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + // assert!(guest.get_total_memory().unwrap_or_default() < 960_000); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(target_arch = "x86_64")] + #[cfg(not(feature = "mshv"))] + // Test both vCPU and memory resizing together + fn test_resize() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=2,max=4"]) + .args(["--memory", "size=512M,hotplug_size=8192M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 2); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + guest.enable_memory_hotplug(); + + // Resize the VM + let desired_vcpus = 4; + let desired_ram = 1024 << 20; + resize_command( + &api_socket, + Some(desired_vcpus), + Some(desired_ram), + None, + None, + ); + + guest + .ssh_command("echo 1 | sudo tee /sys/bus/cpu/devices/cpu2/online") + .unwrap(); + guest + .ssh_command("echo 1 | sudo tee /sys/bus/cpu/devices/cpu3/online") + .unwrap(); + thread::sleep(std::time::Duration::new(10, 0)); + assert_eq!( + guest.get_cpu_count().unwrap_or_default(), + u32::from(desired_vcpus) + ); + + assert!(guest.get_total_memory().unwrap_or_default() > 960_000); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_memory_overhead() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + let guest_memory_size_kb = 512 * 1024; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args([ + "--memory", + format!("size={}K", guest_memory_size_kb).as_str(), + ]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(20, 0)); + + let r = std::panic::catch_unwind(|| { + let overhead = get_vmm_overhead(child.id(), guest_memory_size_kb); + eprintln!( + "Guest memory overhead: {} vs {}", + overhead, MAXIMUM_VMM_OVERHEAD_KB + ); + assert!(overhead <= MAXIMUM_VMM_OVERHEAD_KB); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_disk_hotplug() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = edk2_path(); + + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check /dev/vdc is not there + assert_eq!( + guest + .ssh_command("lsblk | grep -c vdc.*16M || true") + .unwrap() + .trim() + .parse::() + .unwrap_or(1), + 0 + ); + + // Now let's add the extra disk. + let mut blk_file_path = dirs::home_dir().unwrap(); + blk_file_path.push("workloads"); + blk_file_path.push("blk.img"); + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-disk", + Some(format!("path={},id=test0", blk_file_path.to_str().unwrap()).as_str()), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"test0\",\"bdf\":\"0000:00:06.0\"}")); + + thread::sleep(std::time::Duration::new(10, 0)); + + // Check that /dev/vdc exists and the block size is 16M. + assert_eq!( + guest + .ssh_command("lsblk | grep vdc | grep -c 16M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + // And check the block device can be read. + guest + .ssh_command("sudo dd if=/dev/vdc of=/dev/null bs=1M iflag=direct count=16") + .unwrap(); + + // Let's remove it the extra disk. + assert!(remote_command(&api_socket, "remove-device", Some("test0"))); + thread::sleep(std::time::Duration::new(5, 0)); + // And check /dev/vdc is not there + assert_eq!( + guest + .ssh_command("lsblk | grep -c vdc.*16M || true") + .unwrap() + .trim() + .parse::() + .unwrap_or(1), + 0 + ); + + // And add it back to validate unplug did work correctly. + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-disk", + Some(format!("path={},id=test0", blk_file_path.to_str().unwrap()).as_str()), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"test0\",\"bdf\":\"0000:00:06.0\"}")); + + thread::sleep(std::time::Duration::new(10, 0)); + + // Check that /dev/vdc exists and the block size is 16M. + assert_eq!( + guest + .ssh_command("lsblk | grep vdc | grep -c 16M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + // And check the block device can be read. + guest + .ssh_command("sudo dd if=/dev/vdc of=/dev/null bs=1M iflag=direct count=16") + .unwrap(); + + // Reboot the VM. + // guest.reboot_linux(0, None); + + // Check still there after reboot + // assert_eq!( + // guest + // .ssh_command("lsblk | grep vdc | grep -c 16M") + // .unwrap() + // .trim() + // .parse::() + // .unwrap_or_default(), + // 1 + // ); + // + // assert!(remote_command(&api_socket, "remove-device", Some("test0"))); + // + // thread::sleep(std::time::Duration::new(20, 0)); + // + // // Check device has gone away + // assert_eq!( + // guest + // .ssh_command("lsblk | grep -c vdc.*16M || true") + // .unwrap() + // .trim() + // .parse::() + // .unwrap_or(1), + // 0 + // ); + // + // guest.reboot_linux(1, None); + // + // // Check device still absent + // assert_eq!( + // guest + // .ssh_command("lsblk | grep -c vdc.*16M || true") + // .unwrap() + // .trim() + // .parse::() + // .unwrap_or(1), + // 0 + // ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[allow(clippy::useless_conversion)] + fn create_loop_device(backing_file_path: &str, block_size: u32, num_retries: usize) -> String { + const LOOP_CONFIGURE: u64 = 0x4c0a; + const LOOP_CTL_GET_FREE: u64 = 0x4c82; + const LOOP_CTL_PATH: &str = "/dev/loop-control"; + const LOOP_DEVICE_PREFIX: &str = "/dev/loop"; + + #[repr(C)] + struct LoopInfo64 { + lo_device: u64, + lo_inode: u64, + lo_rdevice: u64, + lo_offset: u64, + lo_sizelimit: u64, + lo_number: u32, + lo_encrypt_type: u32, + lo_encrypt_key_size: u32, + lo_flags: u32, + lo_file_name: [u8; 64], + lo_crypt_name: [u8; 64], + lo_encrypt_key: [u8; 32], + lo_init: [u64; 2], + } + + impl Default for LoopInfo64 { + fn default() -> Self { + LoopInfo64 { + lo_device: 0, + lo_inode: 0, + lo_rdevice: 0, + lo_offset: 0, + lo_sizelimit: 0, + lo_number: 0, + lo_encrypt_type: 0, + lo_encrypt_key_size: 0, + lo_flags: 0, + lo_file_name: [0; 64], + lo_crypt_name: [0; 64], + lo_encrypt_key: [0; 32], + lo_init: [0; 2], + } + } + } + + #[derive(Default)] + #[repr(C)] + struct LoopConfig { + fd: u32, + block_size: u32, + info: LoopInfo64, + _reserved: [u64; 8], + } + + // Open loop-control device + let loop_ctl_file = OpenOptions::new() + .read(true) + .write(true) + .open(LOOP_CTL_PATH) + .unwrap(); + + // Request a free loop device + let loop_device_number = unsafe { + libc::ioctl( + loop_ctl_file.as_raw_fd(), + LOOP_CTL_GET_FREE.try_into().unwrap(), + ) + }; + if loop_device_number < 0 { + panic!("Couldn't find a free loop device"); + } + + // Create loop device path + let loop_device_path = format!("{}{}", LOOP_DEVICE_PREFIX, loop_device_number); + + // Open loop device + let loop_device_file = OpenOptions::new() + .read(true) + .write(true) + .open(&loop_device_path) + .unwrap(); + + // Open backing file + let backing_file = OpenOptions::new() + .read(true) + .write(true) + .open(backing_file_path) + .unwrap(); + + let loop_config = LoopConfig { + fd: backing_file.as_raw_fd() as u32, + block_size, + ..Default::default() + }; + + for i in 0..num_retries { + let ret = unsafe { + libc::ioctl( + loop_device_file.as_raw_fd(), + LOOP_CONFIGURE.try_into().unwrap(), + &loop_config, + ) + }; + if ret != 0 { + if i < num_retries - 1 { + println!( + "Iteration {}: Failed to configure the loop device {}: {}", + i, + loop_device_path, + std::io::Error::last_os_error() + ); + } else { + panic!( + "Failed {} times trying to configure the loop device {}: {}", + num_retries, + loop_device_path, + std::io::Error::last_os_error() + ); + } + } else { + break; + } + + // Wait for a bit before retrying + thread::sleep(std::time::Duration::new(5, 0)); + } + + loop_device_path + } + + #[test] + fn test_virtio_block_topology() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + let test_disk_path = guest.tmp_dir.as_path().join("test.img"); + + let output = exec_host_command_output( + format!( + "qemu-img create -f raw {} 16M", + test_disk_path.to_str().unwrap() + ) + .as_str(), + ); + if !output.status.success() { + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + panic!( + "qemu-img command failed\nstdout\n{}\nstderr\n{}", + stdout, stderr + ); + } + + let loop_dev = create_loop_device(test_disk_path.to_str().unwrap(), 4096, 5); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + format!("path={}", &loop_dev).as_str(), + ]) + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // MIN-IO column + assert_eq!( + guest + .ssh_command("lsblk -t| grep vdc | awk '{print $3}'") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 4096 + ); + // PHY-SEC column + assert_eq!( + guest + .ssh_command("lsblk -t| grep vdc | awk '{print $5}'") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 4096 + ); + // LOG-SEC column + assert_eq!( + guest + .ssh_command("lsblk -t| grep vdc | awk '{print $6}'") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 4096 + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + + Command::new("losetup") + .args(["-d", &loop_dev]) + .output() + .expect("loop device not found"); + } + + #[test] + fn test_virtio_balloon_deflate_on_oom() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + let api_socket = temp_api_path(&guest.tmp_dir); + + //Let's start a 4G guest with balloon occupied 2G memory + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--balloon", "size=2G,deflate_on_oom=on"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Wait for balloon memory's initialization and check its size. + // The virtio-balloon driver might take a few seconds to report the + // balloon effective size back to the VMM. + thread::sleep(std::time::Duration::new(20, 0)); + + let orig_balloon = balloon_size(&api_socket); + println!("The original balloon memory size is {} bytes", orig_balloon); + assert!(orig_balloon == 2147483648); + + // Two steps to verify if the 'deflate_on_oom' parameter works. + // 1st: run a command to trigger an OOM in the guest. + guest + .ssh_command("echo f | sudo tee /proc/sysrq-trigger") + .unwrap(); + + // Give some time for the OOM to happen in the guest and be reported + // back to the host. + thread::sleep(std::time::Duration::new(20, 0)); + + // 2nd: check balloon_mem's value to verify balloon has been automatically deflated + let deflated_balloon = balloon_size(&api_socket); + println!( + "After deflating, balloon memory size is {} bytes", + deflated_balloon + ); + // Verify the balloon size deflated + assert!(deflated_balloon < 2147483648); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_pmem_hotplug() { + _test_pmem_hotplug(None) + } + + #[test] + fn test_pmem_multi_segment_hotplug() { + _test_pmem_hotplug(Some(15)) + } + + fn _test_pmem_hotplug(pci_segment: Option) { + #[cfg(target_arch = "aarch64")] + let focal_image = FOCAL_IMAGE_UPDATE_KERNEL_NAME.to_string(); + #[cfg(target_arch = "x86_64")] + let focal_image = FOCAL_IMAGE_NAME.to_string(); + let focal = UbuntuDiskConfig::new(focal_image); + let guest = Guest::new(Box::new(focal)); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = edk2_path(); + + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut cmd = GuestCommand::new(&guest); + + cmd.args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .capture_output(); + + if pci_segment.is_some() { + cmd.args(["--platform", "num_pci_segments=16"]); + } + + let mut child = cmd.spawn().unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check /dev/pmem0 is not there + assert_eq!( + guest + .ssh_command("lsblk | grep -c pmem0 || true") + .unwrap() + .trim() + .parse::() + .unwrap_or(1), + 0 + ); + + let pmem_temp_file = TempFile::new().unwrap(); + pmem_temp_file.as_file().set_len(128 << 20).unwrap(); + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-pmem", + Some(&format!( + "file={},id=test0{}", + pmem_temp_file.as_path().to_str().unwrap(), + if let Some(pci_segment) = pci_segment { + format!(",pci_segment={}", pci_segment) + } else { + "".to_owned() + } + )), + ); + assert!(cmd_success); + if let Some(pci_segment) = pci_segment { + assert!(String::from_utf8_lossy(&cmd_output).contains(&format!( + "{{\"id\":\"test0\",\"bdf\":\"{:04x}:00:01.0\"}}", + pci_segment + ))); + } else { + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"test0\",\"bdf\":\"0000:00:06.0\"}")); + } + + // Check that /dev/pmem0 exists and the block size is 128M + assert_eq!( + guest + .ssh_command("lsblk | grep pmem0 | grep -c 128M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // guest.reboot_linux(0, None); + // + // // Check still there after reboot + // assert_eq!( + // guest + // .ssh_command("lsblk | grep pmem0 | grep -c 128M") + // .unwrap() + // .trim() + // .parse::() + // .unwrap_or_default(), + // 1 + // ); + // + // assert!(remote_command(&api_socket, "remove-device", Some("test0"))); + // + // thread::sleep(std::time::Duration::new(20, 0)); + // + // // Check device has gone away + // assert_eq!( + // guest + // .ssh_command("lsblk | grep -c pmem0.*128M || true") + // .unwrap() + // .trim() + // .parse::() + // .unwrap_or(1), + // 0 + // ); + // + // guest.reboot_linux(1, None); + // + // // Check still absent after reboot + // assert_eq!( + // guest + // .ssh_command("lsblk | grep -c pmem0.*128M || true") + // .unwrap() + // .trim() + // .parse::() + // .unwrap_or(1), + // 0 + // ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_net_hotplug() { + _test_net_hotplug(None) + } + + #[test] + fn test_net_multi_segment_hotplug() { + _test_net_hotplug(Some(15)) + } + + fn _test_net_hotplug(pci_segment: Option) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = edk2_path(); + + let api_socket = temp_api_path(&guest.tmp_dir); + + // Boot without network + let mut cmd = GuestCommand::new(&guest); + + cmd.args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .capture_output(); + + if pci_segment.is_some() { + cmd.args(["--platform", "num_pci_segments=16"]); + } + + let mut child = cmd.spawn().unwrap(); + + thread::sleep(std::time::Duration::new(20, 0)); + + let r = std::panic::catch_unwind(|| { + // Add network + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-net", + Some( + format!( + "{}{},id=test0", + guest.default_net_string(), + if let Some(pci_segment) = pci_segment { + format!(",pci_segment={}", pci_segment) + } else { + "".to_owned() + } + ) + .as_str(), + ), + ); + assert!(cmd_success); + + if let Some(pci_segment) = pci_segment { + assert!(String::from_utf8_lossy(&cmd_output).contains(&format!( + "{{\"id\":\"test0\",\"bdf\":\"{:04x}:00:01.0\"}}", + pci_segment + ))); + } else { + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"test0\",\"bdf\":\"0000:00:05.0\"}")); + } + + thread::sleep(std::time::Duration::new(5, 0)); + + // 1 network interfaces + default localhost ==> 2 interfaces + assert_eq!( + guest + .ssh_command("ip -o link | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 2 + ); + + // Remove network + assert!(remote_command(&api_socket, "remove-device", Some("test0"),)); + thread::sleep(std::time::Duration::new(5, 0)); + + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-net", + Some( + format!( + "{}{},id=test1", + guest.default_net_string(), + if let Some(pci_segment) = pci_segment { + format!(",pci_segment={}", pci_segment) + } else { + "".to_owned() + } + ) + .as_str(), + ), + ); + assert!(cmd_success); + + if let Some(pci_segment) = pci_segment { + assert!(String::from_utf8_lossy(&cmd_output).contains(&format!( + "{{\"id\":\"test1\",\"bdf\":\"{:04x}:00:01.0\"}}", + pci_segment + ))); + } else { + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"test1\",\"bdf\":\"0000:00:05.0\"}")); + } + + thread::sleep(std::time::Duration::new(5, 0)); + + // 1 network interfaces + default localhost ==> 2 interfaces + assert_eq!( + guest + .ssh_command("ip -o link | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 2 + ); + + // guest.reboot_linux(0, None); + // + // // Check still there after reboot + // // 1 network interfaces + default localhost ==> 2 interfaces + // assert_eq!( + // guest + // .ssh_command("ip -o link | wc -l") + // .unwrap() + // .trim() + // .parse::() + // .unwrap_or_default(), + // 2 + // ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_initramfs() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + #[cfg(target_arch = "x86_64")] + let mut kernels = vec![direct_kernel_boot_path()]; + #[cfg(target_arch = "aarch64")] + let kernels = vec![direct_kernel_boot_path()]; + + #[cfg(target_arch = "x86_64")] + { + let mut pvh_kernel_path = workload_path.clone(); + pvh_kernel_path.push("vmlinux"); + kernels.push(pvh_kernel_path); + } + + let mut initramfs_path = workload_path; + initramfs_path.push("alpine_initramfs.img"); + + let test_string = String::from("axz34i9rylotd8n50wbv6kcj7f2qushme1pg"); + let cmdline = format!("console=hvc0 quiet TEST_STRING={}", test_string); + + kernels.iter().for_each(|k_path| { + let mut child = GuestCommand::new(&guest) + .args(["--kernel", k_path.to_str().unwrap()]) + .args(["--initramfs", initramfs_path.to_str().unwrap()]) + .args(["--cmdline", &cmdline]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(20, 0)); + + let _ = child.kill(); + let output = child.wait_with_output().unwrap(); + + let r = std::panic::catch_unwind(|| { + let s = String::from_utf8_lossy(&output.stdout); + + assert_ne!(s.lines().position(|line| line == test_string), None); + }); + + handle_child_output(r, &output); + }); + } + + // One thing to note about this test. The virtio-net device is heavily used + // through each ssh command. There's no need to perform a dedicated test to + // verify the migration went well for virtio-net. + #[test] + #[cfg(not(feature = "mshv"))] + fn test_snapshot_restore_hotplug_virtiomem() { + _test_snapshot_restore(true); + } + + #[test] + fn test_snapshot_restore_basic() { + _test_snapshot_restore(false); + } + + fn _test_snapshot_restore(use_hotplug: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + + let api_socket_source = format!("{}.1", temp_api_path(&guest.tmp_dir)); + + let net_id = "net123"; + let net_params = format!( + "id={},tap=,mac={},ip={},mask=255.255.255.0", + net_id, guest.network.guest_mac, guest.network.host_ip + ); + let mut mem_params = "size=512M"; + + if use_hotplug { + mem_params = "size=512M,hotplug_method=virtio-mem,hotplug_size=1G" + } + + let cloudinit_params = format!( + "path={},iommu=on", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ); + + let socket = temp_vsock_path(&guest.tmp_dir); + let event_path = temp_event_monitor_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket_source]) + .args(["--event-monitor", format!("path={}", event_path).as_str()]) + .args(["--cpus", "boot=4"]) + .args(["--memory", mem_params]) + .args(["--balloon", "size=0"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + cloudinit_params.as_str(), + ]) + .args(["--net", net_params.as_str()]) + .args(["--vsock", format!("cid=3,socket={}", socket).as_str()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .capture_output() + .spawn() + .unwrap(); + + let console_text = String::from("On a branch floating down river a cricket, singing."); + // Create the snapshot directory + let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check the number of vCPUs + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 4); + // Check the guest RAM + assert!(guest.get_total_memory().unwrap_or_default() > 400_000); + if use_hotplug { + // Increase guest RAM with virtio-mem + resize_command( + &api_socket_source, + None, + Some(1536 << 20), + None, + Some(&event_path), + ); + thread::sleep(std::time::Duration::new(5, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 1_400_000); + // Use balloon to remove RAM from the VM + resize_command( + &api_socket_source, + None, + None, + Some(512 << 20), + Some(&event_path), + ); + thread::sleep(std::time::Duration::new(5, 0)); + let total_memory = guest.get_total_memory().unwrap_or_default(); + assert!(total_memory > 900_000); + assert!(total_memory < 1_400_000); + } + // Check the guest virtio-devices, e.g. block, rng, vsock, console, and net + guest.check_devices_common(Some(&socket), Some(&console_text), None); + + // x86_64: We check that removing and adding back the virtio-net device + // does not break the snapshot/restore support for virtio-pci. + // This is an important thing to test as the hotplug will + // trigger a PCI BAR reprogramming, which is a good way of + // checking if the stored resources are correctly restored. + // Unplug the virtio-net device + // AArch64: Device hotplug is currently not supported, skipping here. + #[cfg(target_arch = "x86_64")] + { + assert!(remote_command( + &api_socket_source, + "remove-device", + Some(net_id), + )); + thread::sleep(std::time::Duration::new(10, 0)); + let latest_events = [&MetaEvent { + event: "device-removed".to_string(), + device_id: Some(net_id.to_string()), + }]; + assert!(check_latest_events_exact(&latest_events, &event_path)); + + // Plug the virtio-net device again + assert!(remote_command( + &api_socket_source, + "add-net", + Some(net_params.as_str()), + )); + thread::sleep(std::time::Duration::new(10, 0)); + } + snapshot_and_check_events( + api_socket_source.as_str(), + snapshot_dir.as_str(), + event_path.as_str(), + ); + }); + + // Shutdown the source VM and check console output + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&output.stdout).contains(&console_text)); + }); + + handle_child_output(r, &output); + + // Remove the vsock socket file. + Command::new("rm") + .arg("-f") + .arg(socket.as_str()) + .output() + .unwrap(); + + let api_socket_restored = format!("{}.2", temp_api_path(&guest.tmp_dir)); + let event_path_restored = format!("{}.2", temp_event_monitor_path(&guest.tmp_dir)); + + // Restore the VM from the snapshot + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket_restored]) + .args([ + "--event-monitor", + format!("path={}", event_path_restored).as_str(), + ]) + .args([ + "--restore", + format!("source_url=file://{}", snapshot_dir).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + // Wait for the VM to be restored + thread::sleep(std::time::Duration::new(10, 0)); + let expected_events = [ + &MetaEvent { + event: "starting".to_string(), + device_id: None, + }, + &MetaEvent { + event: "restoring".to_string(), + device_id: None, + }, + ]; + assert!(check_sequential_events_exact( + &expected_events, + &event_path_restored + )); + + let r = std::panic::catch_unwind(|| { + // Automatically restart the VM after it has been restored + let latest_events = [ + &MetaEvent { + event: "restored".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resuming".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resumed".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact( + &latest_events, + &event_path_restored + )); + + // Perform same checks to validate VM has been properly restored + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 4); + let total_memory = guest.get_total_memory().unwrap_or_default(); + if !use_hotplug { + assert!(guest.get_total_memory().unwrap_or_default() > 400_000); + } else { + assert!(total_memory > 900_000); + assert!(total_memory < 1_400_000); + // Deflate balloon to restore entire RAM to the VM + resize_command(&api_socket_restored, None, None, Some(0), None); + thread::sleep(std::time::Duration::new(5, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 1_400_000); + // Decrease guest RAM with virtio-mem + resize_command(&api_socket_restored, None, Some(1 << 30), None, None); + thread::sleep(std::time::Duration::new(5, 0)); + let total_memory = guest.get_total_memory().unwrap_or_default(); + assert!(total_memory > 900_000); + assert!(total_memory < 1_400_000); + } + + guest.check_devices_common(Some(&socket), Some(&console_text), None); + }); + // Shutdown the target VM and check console output + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&output.stdout).contains(&console_text)); + }); + + handle_child_output(r, &output); + } + + // Incremental (pagemap_anon) overlays CoW pages onto an existing dest + // memory-ranges file. An empty dest has no base, so the snapshot API + // must fail and must not create memory-ranges. + #[test] + fn test_incremental_snapshot_requires_base_memory_ranges() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", guest.default_net_string().as_str()]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + assert!(remote_command(&api_socket, "pause", None)); + + let url = format!("file://{}", snapshot_dir); + let (ok, stderr) = remote_command_w_args_output( + &api_socket, + "snapshot", + &[&url, "--snapshot-type", "incremental"], + ); + assert!( + !ok, + "incremental snapshot without dest memory-ranges must fail" + ); + assert!( + stderr.contains("Base snapshot file not found"), + "expected missing-base error, got: {}", + stderr + ); + assert!( + !std::path::Path::new(&snapshot_dir) + .join("memory-ranges") + .exists(), + "failed incremental must not create memory-ranges" + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + } + + // Incremental (pagemap_anon) after restore: Cubelet Tier 2 path. + // Full snapshot becomes the restore base; after restore, guest RAM is a + // MAP_PRIVATE mmap of that file. Copy the base memory-ranges to a new + // dest (Cubelet does reflink), then incremental overlays CoW pages. + #[test] + fn test_snapshot_restore_incremental_after_restore() { + let mem_params = "size=1G"; + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + + let api_socket_source = format!("{}.1", temp_api_path(&guest.tmp_dir)); + + let net_id = "net123"; + let net_params = format!( + "id={},tap=,mac={},ip={},mask=255.255.255.0", + net_id, guest.network.guest_mac, guest.network.host_ip + ); + + let socket = temp_vsock_path(&guest.tmp_dir); + let event_path = temp_event_monitor_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket_source]) + .args(["--event-monitor", format!("path={}", event_path).as_str()]) + .args(["--cpus", "boot=2"]) + .args(["--memory", mem_params]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .default_disks() + .args(["--net", net_params.as_str()]) + .args(["--vsock", format!("cid=3,socket={}", socket).as_str()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .capture_output() + .spawn() + .unwrap(); + + let console_text = String::from("On a branch floating down river a cricket, singing."); + let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir); + let base_dir = String::from( + guest + .tmp_dir + .as_path() + .join("snapshot_base") + .to_str() + .unwrap(), + ); + std::fs::create_dir(&base_dir).unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + snapshot_and_check_events( + api_socket_source.as_str(), + base_dir.as_str(), + event_path.as_str(), + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + Command::new("rm") + .arg("-f") + .arg(socket.as_str()) + .output() + .unwrap(); + + let api_socket_base_restored = format!("{}.base", temp_api_path(&guest.tmp_dir)); + let event_path_base_restored = format!("{}.base", temp_event_monitor_path(&guest.tmp_dir)); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket_base_restored]) + .args([ + "--event-monitor", + format!("path={}", event_path_base_restored).as_str(), + ]) + .args([ + "--restore", + format!("source_url=file://{}", base_dir).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(10, 0)); + + // tmpfs-backed payload: only survives the next restore if incremental + // actually captured the CoW pages (disk-backed files would persist + // even if the memory snapshot dropped them). + let dirty_md5 = std::sync::Mutex::new(String::new()); + + let r = std::panic::catch_unwind(|| { + let latest_events = [ + &MetaEvent { + event: "restored".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resuming".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resumed".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact( + &latest_events, + &event_path_base_restored + )); + + guest.check_devices_common(Some(&socket), Some(&console_text), None); + + thread::sleep(std::time::Duration::new(5, 0)); + guest + .ssh_command("sudo dd if=/dev/urandom of=/dev/shm/dirty.bin bs=1M count=64") + .unwrap(); + let sum = guest.ssh_command("md5sum /dev/shm/dirty.bin").unwrap(); + *dirty_md5.lock().unwrap() = sum.trim().to_string(); + + // Simulate Cubelet reflink: dest must already hold the base image. + std::fs::copy( + std::path::Path::new(&base_dir).join("memory-ranges"), + std::path::Path::new(&snapshot_dir).join("memory-ranges"), + ) + .expect("copy base memory-ranges into incremental dest"); + + incremental_snapshot_and_check_events( + api_socket_base_restored.as_str(), + snapshot_dir.as_str(), + event_path_base_restored.as_str(), + ); + + assert_memory_ranges_full_size(snapshot_dir.as_str(), mem_params); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&output.stdout).contains(&console_text)); + }); + handle_child_output(r, &output); + + Command::new("rm") + .arg("-f") + .arg(socket.as_str()) + .output() + .unwrap(); + + let api_socket_restored = format!("{}.2", temp_api_path(&guest.tmp_dir)); + let event_path_restored = format!("{}.2", temp_event_monitor_path(&guest.tmp_dir)); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket_restored]) + .args([ + "--event-monitor", + format!("path={}", event_path_restored).as_str(), + ]) + .args([ + "--restore", + format!("source_url=file://{}", snapshot_dir).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(10, 0)); + let expected_events = [ + &MetaEvent { + event: "starting".to_string(), + device_id: None, + }, + &MetaEvent { + event: "restoring".to_string(), + device_id: None, + }, + ]; + assert!(check_sequential_events_exact( + &expected_events, + &event_path_restored + )); + + let r = std::panic::catch_unwind(|| { + let latest_events = [ + &MetaEvent { + event: "restored".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resuming".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resumed".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact( + &latest_events, + &event_path_restored + )); + + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 2); + guest.check_devices_common(Some(&socket), Some(&console_text), None); + + let restored_md5 = guest.ssh_command("md5sum /dev/shm/dirty.bin").unwrap(); + assert_eq!( + restored_md5.trim(), + dirty_md5.lock().unwrap().as_str(), + "tmpfs payload mismatch after incremental restore" + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&output.stdout).contains(&console_text)); + }); + handle_child_output(r, &output); + } + + #[test] + fn test_counters() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", guest.default_net_string().as_str()]) + .args(["--api-socket", &api_socket]) + .capture_output(); + + let mut child = cmd.spawn().unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + let orig_counters = get_counters(&api_socket); + guest + .ssh_command("dd if=/dev/zero of=test count=8 bs=1M") + .unwrap(); + + let new_counters = get_counters(&api_socket); + + // Check that all the counters have increased + assert!(new_counters > orig_counters); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(feature = "guest_debug")] + fn test_coredump() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--cpus", "boot=4"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", fw_path(FwType::RustHypervisorFirmware).as_str()]) + .default_disks() + .args(["--net", guest.default_net_string().as_str()]) + .args(["--api-socket", &api_socket]) + .capture_output(); + + let mut child = cmd.spawn().unwrap(); + let vmcore_file = temp_vmcore_file_path(&guest.tmp_dir); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert!(remote_command(&api_socket, "pause", None)); + + assert!(remote_command( + &api_socket, + "coredump", + Some(format!("file://{}", vmcore_file).as_str()), + )); + + // the num of CORE notes should equals to vcpu + let readelf_core_num_cmd = format!( + "readelf --all {} |grep CORE |grep -v Type |wc -l", + vmcore_file + ); + let core_num_in_elf = exec_host_command_output(&readelf_core_num_cmd); + assert_eq!(String::from_utf8_lossy(&core_num_in_elf.stdout).trim(), "4"); + + // the num of QEMU notes should equals to vcpu + let readelf_vmm_num_cmd = format!("readelf --all {} |grep QEMU |wc -l", vmcore_file); + let vmm_num_in_elf = exec_host_command_output(&readelf_vmm_num_cmd); + assert_eq!(String::from_utf8_lossy(&vmm_num_in_elf.stdout).trim(), "4"); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[ignore = "PVM guest kernel has no virtio-watchdog driver (CONFIG_VIRTIO_WDT not enabled)"] + fn test_watchdog() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let kernel_path = direct_kernel_boot_path(); + + let mut cmd = GuestCommand::new(&guest); + cmd.args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", guest.default_net_string().as_str()]) + .args(["--watchdog"]) + .args(["--api-socket", &api_socket]) + .capture_output(); + + let mut child = cmd.spawn().unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + let mut expected_reboot_count = 1; + + // Enable the watchdog with a 15s timeout + enable_guest_watchdog(&guest, 15); + + // Reboot and check that systemd has activated the watchdog + guest.ssh_command("sudo reboot").unwrap(); + guest.wait_vm_boot(None).unwrap(); + expected_reboot_count += 1; + assert_eq!(get_reboot_count(&guest), expected_reboot_count); + assert_eq!( + guest + .ssh_command("sudo journalctl | grep -c -- \"Watchdog started\"") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 2 + ); + + // Allow some normal time to elapse to check we don't get spurious reboots + thread::sleep(std::time::Duration::new(40, 0)); + // Check no reboot + assert_eq!(get_reboot_count(&guest), expected_reboot_count); + + // Trigger a panic (sync first). We need to do this inside a screen with a delay so the SSH command returns. + guest.ssh_command("screen -dmS reboot sh -c \"sleep 5; echo s | tee /proc/sysrq-trigger; echo c | sudo tee /proc/sysrq-trigger\"").unwrap(); + // Allow some time for the watchdog to trigger (max 30s) and reboot to happen + guest.wait_vm_boot(Some(50)).unwrap(); + // Check a reboot is triggerred by the watchdog + expected_reboot_count += 1; + assert_eq!(get_reboot_count(&guest), expected_reboot_count); + + #[cfg(target_arch = "x86_64")] + { + // Now pause the VM and remain offline for 30s + assert!(remote_command(&api_socket, "pause", None)); + thread::sleep(std::time::Duration::new(30, 0)); + assert!(remote_command(&api_socket, "resume", None)); + + // Check no reboot + assert_eq!(get_reboot_count(&guest), expected_reboot_count); + } + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_tap_from_fd() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + + // Create a TAP interface with multi-queue enabled + let num_queue_pairs: usize = 2; + + use std::str::FromStr; + let taps = net_util::open_tap( + Some("chtap0"), + Some(std::net::Ipv4Addr::from_str(&guest.network.host_ip).unwrap()), + None, + &mut None, + None, + num_queue_pairs, + Some(libc::O_RDWR | libc::O_NONBLOCK), + None, + ) + .unwrap(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", &format!("boot={}", num_queue_pairs)]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args([ + "--net", + &format!( + "fd=[{},{}],mac={},num_queues={}", + taps[0].as_raw_fd(), + taps[1].as_raw_fd(), + guest.network.guest_mac, + num_queue_pairs * 2 + ), + ]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert_eq!( + guest + .ssh_command("ip -o link | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 2 + ); + + // guest.reboot_linux(0, None); + // + // assert_eq!( + // guest + // .ssh_command("ip -o link | wc -l") + // .unwrap() + // .trim() + // .parse::() + // .unwrap_or_default(), + // 2 + // ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + // By design, a guest VM won't be able to connect to the host + // machine when using a macvtap network interface (while it can + // communicate externally). As a workaround, this integration + // test creates two macvtap interfaces in 'bridge' mode on the + // same physical net interface, one for the guest and one for + // the host. With additional setup on the IP address and the + // routing table, it enables the communications between the + // guest VM and the host machine. + // Details: https://wiki.libvirt.org/page/TroubleshootMacvtapHostFail + fn _test_macvtap(hotplug: bool, guest_macvtap_name: &str, host_macvtap_name: &str) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + #[cfg(target_arch = "x86_64")] + let kernel_path = direct_kernel_boot_path(); + #[cfg(target_arch = "aarch64")] + let kernel_path = edk2_path(); + + let phy_net = "eth0"; + + // Create a macvtap interface for the guest VM to use + assert!(exec_host_command_status(&format!( + "sudo ip link add link {} name {} type macvtap mod bridge", + phy_net, guest_macvtap_name + )) + .success()); + assert!(exec_host_command_status(&format!( + "sudo ip link set {} address {} up", + guest_macvtap_name, guest.network.guest_mac + )) + .success()); + assert!( + exec_host_command_status(&format!("sudo ip link show {}", guest_macvtap_name)) + .success() + ); + + let tap_index = + fs::read_to_string(format!("/sys/class/net/{}/ifindex", guest_macvtap_name)).unwrap(); + let tap_device = format!("/dev/tap{}", tap_index.trim()); + + // In a container netns (non init_net), devtmpfs does not auto-create + // /dev/tap for macvtap devices. On bare metal (init_net) the + // node is auto-created by devtmpfs, so this workaround is a no-op there. + if !std::path::Path::new(&tap_device).exists() { + let dev = fs::read_to_string(format!("/sys/class/macvtap/tap{}/dev", tap_index.trim())) + .expect("failed to read macvtap dev from sysfs"); + let dev = dev.trim(); + let (major, minor) = dev.split_once(':').expect("invalid macvtap dev format"); + assert!(exec_host_command_status(&format!( + "sudo mknod {} c {} {}", + tap_device, major, minor + )) + .success()); + } + + assert!( + exec_host_command_status(&format!("sudo chown $UID.$UID {}", tap_device)).success() + ); + + let cstr_tap_device = std::ffi::CString::new(tap_device).unwrap(); + let tap_fd1 = unsafe { libc::open(cstr_tap_device.as_ptr(), libc::O_RDWR) }; + assert!(tap_fd1 > 0); + let tap_fd2 = unsafe { libc::open(cstr_tap_device.as_ptr(), libc::O_RDWR) }; + assert!(tap_fd2 > 0); + + // Create a macvtap on the same physical net interface for + // the host machine to use + assert!(exec_host_command_status(&format!( + "sudo ip link add link {} name {} type macvtap mod bridge", + phy_net, host_macvtap_name + )) + .success()); + // Use default mask "255.255.255.0" + assert!(exec_host_command_status(&format!( + "sudo ip address add {}/24 dev {}", + guest.network.host_ip, host_macvtap_name + )) + .success()); + assert!(exec_host_command_status(&format!( + "sudo ip link set dev {} up", + host_macvtap_name + )) + .success()); + + let mut guest_command = GuestCommand::new(&guest); + guest_command + .args(["--cpus", "boot=2"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--api-socket", &api_socket]); + + let net_params = format!( + "fd=[{},{}],mac={},num_queues=4", + tap_fd1, tap_fd2, guest.network.guest_mac + ); + + if !hotplug { + guest_command.args(["--net", &net_params]); + } + + let mut child = guest_command.capture_output().spawn().unwrap(); + + if hotplug { + // Give some time to the VMM process to listen to the API + // socket. This is the only requirement to avoid the following + // call to ch-remote from failing. + thread::sleep(std::time::Duration::new(10, 0)); + // Hotplug the virtio-net device + let (cmd_success, cmd_output) = + remote_command_w_output(&api_socket, "add-net", Some(&net_params)); + assert!(cmd_success); + #[cfg(target_arch = "x86_64")] + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"_net2\",\"bdf\":\"0000:00:05.0\"}")); + #[cfg(target_arch = "aarch64")] + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"_net0\",\"bdf\":\"0000:00:05.0\"}")); + } + + // The functional connectivity provided by the virtio-net device + // gets tested through wait_vm_boot() as it expects to receive a + // HTTP request, and through the SSH command as well. + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert_eq!( + guest + .ssh_command("ip -o link | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 2 + ); + }); + + let _ = child.kill(); + + exec_host_command_status(&format!("sudo ip link del {}", guest_macvtap_name)); + exec_host_command_status(&format!("sudo ip link del {}", host_macvtap_name)); + + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_macvtap() { + _test_macvtap(false, "guestmacvtap0", "hostmacvtap0") + } + + #[test] + fn test_macvtap_hotplug() { + _test_macvtap(true, "guestmacvtap1", "hostmacvtap1") + } + + #[test] + #[cfg(not(feature = "mshv"))] + #[ignore = "PVM guest kernel has CONFIG_OPENVSWITCH disabled (aligned with intranet cube skip)"] + fn test_ovs_dpdk() { + let focal1 = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest1 = Guest::new(Box::new(focal1)); + + let focal2 = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest2 = Guest::new(Box::new(focal2)); + let api_socket_source = format!("{}.1", temp_api_path(&guest2.tmp_dir)); + + let (mut child1, mut child2) = + setup_ovs_dpdk_guests(&guest1, &guest2, &api_socket_source, false); + + // Create the snapshot directory + let snapshot_dir = temp_snapshot_dir_path(&guest2.tmp_dir); + + let r = std::panic::catch_unwind(|| { + // Remove one of the two ports from the OVS bridge + assert!(exec_host_command_status("ovs-vsctl del-port vhost-user1").success()); + + // Spawn a new netcat listener in the first VM + let guest_ip = guest1.network.guest_ip.clone(); + thread::spawn(move || { + ssh_command_ip( + "nc -l 12345", + &guest_ip, + DEFAULT_SSH_RETRIES, + DEFAULT_SSH_TIMEOUT, + ) + .unwrap(); + }); + + // Wait for the server to be listening + thread::sleep(std::time::Duration::new(5, 0)); + + // Check the connection fails this time + assert!(guest2.ssh_command("nc -vz 172.100.0.1 12345").is_err()); + + // Add the OVS port back + assert!(exec_host_command_status("ovs-vsctl add-port ovsbr0 vhost-user1 -- set Interface vhost-user1 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient1").success()); + + // And finally check the connection is functional again + guest2.ssh_command("nc -vz 172.100.0.1 12345").unwrap(); + + // Pause the VM + assert!(remote_command(&api_socket_source, "pause", None)); + + // Take a snapshot from the VM + assert!(remote_command( + &api_socket_source, + "snapshot", + Some(format!("file://{}", snapshot_dir).as_str()), + )); + + // Wait to make sure the snapshot is completed + thread::sleep(std::time::Duration::new(10, 0)); + }); + + // Shutdown the source VM + let _ = child2.kill(); + let output = child2.wait_with_output().unwrap(); + handle_child_output(r, &output); + + // Remove the vhost-user socket file. + Command::new("rm") + .arg("-f") + .arg("/tmp/dpdkvhostclient2") + .output() + .unwrap(); + + let api_socket_restored = format!("{}.2", temp_api_path(&guest2.tmp_dir)); + // Restore the VM from the snapshot + let mut child2 = GuestCommand::new(&guest2) + .args(["--api-socket", &api_socket_restored]) + .args([ + "--restore", + format!("source_url=file://{}", snapshot_dir).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + // Wait for the VM to be restored + thread::sleep(std::time::Duration::new(10, 0)); + + let r = std::panic::catch_unwind(|| { + // Resume the VM + assert!(remote_command(&api_socket_restored, "resume", None)); + + // Spawn a new netcat listener in the first VM + let guest_ip = guest1.network.guest_ip.clone(); + thread::spawn(move || { + ssh_command_ip( + "nc -l 12345", + &guest_ip, + DEFAULT_SSH_RETRIES, + DEFAULT_SSH_TIMEOUT, + ) + .unwrap(); + }); + + // Wait for the server to be listening + thread::sleep(std::time::Duration::new(5, 0)); + + // And check the connection is still functional after restore + guest2.ssh_command("nc -vz 172.100.0.1 12345").unwrap(); + }); + + let _ = child1.kill(); + let _ = child2.kill(); + + let output = child1.wait_with_output().unwrap(); + child2.wait().unwrap(); + + cleanup_ovs_dpdk(); + + handle_child_output(r, &output); + } + + fn setup_spdk_nvme(nvme_dir: &std::path::Path) { + cleanup_spdk_nvme(); + + assert!(exec_host_command_status(&format!( + "mkdir -p {}", + nvme_dir.join("nvme-vfio-user").to_str().unwrap() + )) + .success()); + assert!(exec_host_command_status(&format!( + "truncate {} -s 128M", + nvme_dir.join("test-disk.raw").to_str().unwrap() + )) + .success()); + assert!(exec_host_command_status(&format!( + "mkfs.ext4 {}", + nvme_dir.join("test-disk.raw").to_str().unwrap() + )) + .success()); + + // Start the SPDK nvmf_tgt daemon to present NVMe device as a VFIO user device + Command::new("/usr/local/bin/spdk-nvme/nvmf_tgt") + .args(["-i", "0", "-m", "0x1"]) + .spawn() + .unwrap(); + thread::sleep(std::time::Duration::new(2, 0)); + + assert!(exec_host_command_status( + "/usr/local/bin/spdk-nvme/rpc.py nvmf_create_transport -t VFIOUSER" + ) + .success()); + assert!(exec_host_command_status(&format!( + "/usr/local/bin/spdk-nvme/rpc.py bdev_aio_create {} test 512", + nvme_dir.join("test-disk.raw").to_str().unwrap() + )) + .success()); + assert!(exec_host_command_status( + "/usr/local/bin/spdk-nvme/rpc.py nvmf_create_subsystem nqn.2019-07.io.spdk:cnode -a -s test" + ) + .success()); + assert!(exec_host_command_status( + "/usr/local/bin/spdk-nvme/rpc.py nvmf_subsystem_add_ns nqn.2019-07.io.spdk:cnode test" + ) + .success()); + assert!(exec_host_command_status(&format!( + "/usr/local/bin/spdk-nvme/rpc.py nvmf_subsystem_add_listener nqn.2019-07.io.spdk:cnode -t VFIOUSER -a {} -s 0", + nvme_dir.join("nvme-vfio-user").to_str().unwrap() + )) + .success()); + } + + fn cleanup_spdk_nvme() { + exec_host_command_status("pkill -f nvmf_tgt"); + } + + #[test] + #[ignore = "PVM kernel does not support vfio-user (no VFIO support in PVM host/guest kernel)"] + fn test_vfio_user() { + let jammy_image = JAMMY_IMAGE_NAME.to_string(); + let jammy = UbuntuDiskConfig::new(jammy_image); + let guest = Guest::new(Box::new(jammy)); + + let spdk_nvme_dir = guest.tmp_dir.as_path().join("test-vfio-user"); + setup_spdk_nvme(spdk_nvme_dir.as_path()); + + let api_socket = temp_api_path(&guest.tmp_dir); + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M,shared=on,hugepages=on"]) + .args(["--kernel", fw_path(FwType::RustHypervisorFirmware).as_str()]) + .args(["--serial", "tty", "--console", "off"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Hotplug the SPDK-NVMe device to the VM + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-user-device", + Some(&format!( + "socket={},id=vfio_user0", + spdk_nvme_dir + .as_path() + .join("nvme-vfio-user/cntrl") + .to_str() + .unwrap(), + )), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"vfio_user0\",\"bdf\":\"0000:00:05.0\"}")); + + thread::sleep(std::time::Duration::new(10, 0)); + + // Check both if /dev/nvme exists and if the block size is 128M. + assert_eq!( + guest + .ssh_command("lsblk | grep nvme0n1 | grep -c 128M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Check changes persist after reboot + assert_eq!( + guest.ssh_command("sudo mount /dev/nvme0n1 /mnt").unwrap(), + "" + ); + assert_eq!(guest.ssh_command("ls /mnt").unwrap(), "lost+found\n"); + guest + .ssh_command("echo test123 | sudo tee /mnt/test") + .unwrap(); + assert_eq!(guest.ssh_command("sudo umount /mnt").unwrap(), ""); + assert_eq!(guest.ssh_command("ls /mnt").unwrap(), ""); + + // guest.reboot_linux(0, None); + // assert_eq!( + // guest.ssh_command("sudo mount /dev/nvme0n1 /mnt").unwrap(), + // "" + // ); + // assert_eq!( + // guest.ssh_command("sudo cat /mnt/test").unwrap().trim(), + // "test123" + // ); + }); + + cleanup_spdk_nvme(); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(target_arch = "x86_64")] + fn test_vdpa_block() { + // Before trying to run the test, verify the vdpa_sim_blk module is correctly loaded. + if !exec_host_command_status("lsmod | grep vdpa_sim_blk").success() { + return; + } + + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=2"]) + .args(["--memory", "size=512M,hugepages=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--vdpa", "path=/dev/vhost-vdpa-0,num_queues=1"]) + .args(["--platform", "num_pci_segments=2,iommu_segments=1"]) + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check both if /dev/vdc exists and if the block size is 128M. + assert_eq!( + guest + .ssh_command("lsblk | grep vdc | grep -c 128M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Check the content of the block device after we wrote to it. + // The vpda-sim-blk should let us read what we previously wrote. + guest + .ssh_command("sudo bash -c 'echo foobar > /dev/vdc'") + .unwrap(); + assert_eq!( + guest.ssh_command("sudo head -1 /dev/vdc").unwrap().trim(), + "foobar" + ); + + // Hotplug an extra vDPA block device behind the vIOMMU + // Add a new vDPA device to the VM + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-vdpa", + Some("id=myvdpa0,path=/dev/vhost-vdpa-1,num_queues=1,pci_segment=1,iommu=on"), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"myvdpa0\",\"bdf\":\"0001:00:01.0\"}")); + + thread::sleep(std::time::Duration::new(10, 0)); + + // Check IOMMU setup + assert!(guest + .does_device_vendor_pair_match("0x1057", "0x1af4") + .unwrap_or_default()); + assert_eq!( + guest + .ssh_command("ls /sys/kernel/iommu_groups/0/devices") + .unwrap() + .trim(), + "0001:00:01.0" + ); + + // Check both if /dev/vdd exists and if the block size is 128M. + assert_eq!( + guest + .ssh_command("lsblk | grep vdd | grep -c 128M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Write some content to the block device we've just plugged. + guest + .ssh_command("sudo bash -c 'echo foobar > /dev/vdd'") + .unwrap(); + + // Check we can read the content back. + assert_eq!( + guest.ssh_command("sudo head -1 /dev/vdd").unwrap().trim(), + "foobar" + ); + + // Unplug the device + let cmd_success = remote_command(&api_socket, "remove-device", Some("myvdpa0")); + assert!(cmd_success); + thread::sleep(std::time::Duration::new(10, 0)); + + // Check /dev/vdd doesn't exist anymore + assert_eq!( + guest + .ssh_command("lsblk | grep -c vdd || true") + .unwrap() + .trim() + .parse::() + .unwrap_or(1), + 0 + ); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(target_arch = "x86_64")] + fn test_vdpa_net() { + // Before trying to run the test, verify the vdpa_sim_net module is correctly loaded. + if !exec_host_command_status("lsmod | grep vdpa_sim_net").success() { + return; + } + + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let kernel_path = direct_kernel_boot_path(); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=2"]) + .args(["--memory", "size=512M,hugepages=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--vdpa", "path=/dev/vhost-vdpa-2,num_queues=2"]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check we can find network interface related to vDPA device + assert_eq!( + guest + .ssh_command("ip -o link | grep -c ens6") + .unwrap() + .trim() + .parse::() + .unwrap_or(0), + 1 + ); + + guest + .ssh_command("sudo ip addr add 172.16.1.2/24 dev ens6") + .unwrap(); + guest.ssh_command("sudo ip link set up dev ens6").unwrap(); + + // Check there is no packet yet on both TX/RX of the network interface + assert_eq!( + guest + .ssh_command("ip -j -p -s link show ens6 | grep -c '\"packets\": 0'") + .unwrap() + .trim() + .parse::() + .unwrap_or(0), + 2 + ); + + // Send 6 packets with ping command + guest.ssh_command("ping 172.16.1.10 -c 6 || true").unwrap(); + + // Check we can find 6 packets on both TX/RX of the network interface + assert_eq!( + guest + .ssh_command("ip -j -p -s link show ens6 | grep -c '\"packets\": 6'") + .unwrap() + .trim() + .parse::() + .unwrap_or(0), + 2 + ); + + // No need to check for hotplug as we already tested it through + // test_vdpa_block() + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } +} + +mod common_sequential { + use vmm::vm_config::{DiskConfig, FsConfig, NetConfig, PmemConfig, VsockConfig}; + + use crate::*; + + #[test] + fn test_virtio_balloon_free_page_reporting() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=2G"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--balloon", "size=0,free_page_reporting=on"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let pid = child.id(); + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + verify_free_page_reporting(&guest, pid, "cold boot"); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + } + + #[test] + fn test_virtio_balloon_free_page_reporting_after_snapshot_restore_with_seccomp() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = format!("{}.source", temp_api_path(&guest.tmp_dir)); + let event_path = format!("{}.source", temp_event_monitor_path(&guest.tmp_dir)); + let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir); + + let mut source = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket]) + .args(["--event-monitor", format!("path={event_path}").as_str()]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=2G"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--balloon", "size=0,free_page_reporting=on"]) + .args(["--seccomp", "true"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + snapshot_and_check_events(&api_socket, &snapshot_dir, &event_path); + }); + kill_child(&mut source); + let output = source.wait_with_output().unwrap(); + handle_child_output(r, &output); + + let mut restored = GuestCommand::new(&guest) + .args([ + "--restore", + format!("source_url=file://{snapshot_dir}").as_str(), + ]) + .args(["--seccomp", "true"]) + .capture_output() + .spawn() + .unwrap(); + + let pid = restored.id(); + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(Some(120)).unwrap(); + verify_free_page_reporting(&guest, pid, "snapshot restore"); + }); + + kill_child(&mut restored); + let output = restored.wait_with_output().unwrap(); + handle_child_output(r, &output); + } + + #[test] + fn test_memory_mergeable_on() { + test_memory_mergeable(true) + } + + #[test] + fn test_snapshot_restore_from_config() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + + let api_socket_source = format!("{}.1", temp_api_path(&guest.tmp_dir)); + let vsock_id = "_vsock0"; + + let net_id = "net123"; + // Use a name that cannot collide with the auto-assigned `vmtap%d` + // taps used by parallel tests (open_named("vmtap%d") starts at + // vmtap0), otherwise OpenTap fails with "Device or resource busy". + let tap_name = "src-tap0"; + let net_params = format!( + "id={},tap={},mac={},ip={},mask=255.255.255.0", + net_id, tap_name, guest.network.guest_mac, guest.network.host_ip + ); + + let socket = temp_vsock_path(&guest.tmp_dir); + let event_path = temp_event_monitor_path(&guest.tmp_dir); + + let pmem_id = "pmem0"; + let pmem_temp_file = TempFile::new().unwrap(); + pmem_temp_file.as_file().set_len(128 << 20).unwrap(); + Command::new("mkfs.ext4") + .arg(pmem_temp_file.as_path()) + .output() + .expect("Expect creating disk image to succeed"); + + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + // Prepare native virtiofs + let mut shared_dir = workload_path.clone(); + shared_dir.push("shared_dir"); + let fs_params = format!( + "id=myfs0,tag=myfs,native=true,shared_dir={},cache=always,num_queues=1,queue_size=1024", + shared_dir.to_str().unwrap() + ); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket_source]) + .args(["--event-monitor", format!("path={}", event_path).as_str()]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=1G"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--net", net_params.as_str()]) + .args([ + "--disk", + format!( + "id=osdisk,path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "id=cloudinit,path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + ]) + .args([ + "--pmem", + format!( + "id={},file={}", + pmem_id, + pmem_temp_file.as_path().to_str().unwrap(), + ) + .as_str(), + ]) + .args(["--fs", fs_params.as_str()]) + .args([ + "--vsock", + format!("cid=3,id={},socket={}", vsock_id, socket).as_str(), + ]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .capture_output() + .spawn() + .unwrap(); + + // Create the snapshot directory + let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check tap device name + let tap_count = exec_host_command_output(&format!("ip link | grep -c {}", tap_name)); + assert_eq!(String::from_utf8_lossy(&tap_count.stdout).trim(), "1"); + + snapshot_and_check_events( + api_socket_source.as_str(), + snapshot_dir.as_str(), + event_path.as_str(), + ); + }); + + // Shutdown the source VM and check console output + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + // Remove the vsock socket file. + Command::new("rm") + .arg("-f") + .arg(socket.as_str()) + .output() + .unwrap(); + + // Prepare restore config + // vsock config + let socket_restored = format!("{}.2", temp_vsock_path(&guest.tmp_dir)); + let vsock_config = VsockConfig::parse( + format!("cid=3,id={},socket={}", vsock_id, socket_restored).as_str(), + ) + .unwrap(); + + // net config + let tap_name_restored = "restore-tap"; + let net_params_restored = format!( + "id={},tap={},mac={},ip={},mask=255.255.255.0", + net_id, tap_name_restored, guest.network.guest_mac, guest.network.host_ip + ); + let net_config = NetConfig::parse(net_params_restored.as_str()).unwrap(); + + // disk config + let mut osdisk_base_path = workload_path.clone(); + osdisk_base_path.push(FOCAL_IMAGE_NAME); + let osdisk_path_restored = String::from( + guest + .tmp_dir + .as_path() + .join("osdisk2.img") + .to_str() + .unwrap(), + ); + rate_limited_copy(osdisk_base_path, &osdisk_path_restored) + .expect("copying of OS source disk image failed"); + let disk_params_restored = format!("id=osdisk,path={}", osdisk_path_restored); + let disk_config = DiskConfig::parse(disk_params_restored.as_str()).unwrap(); + + // pmem config + let pmem_temp_file_retored = TempFile::new().unwrap(); + pmem_temp_file_retored.as_file().set_len(128 << 20).unwrap(); + std::process::Command::new("mkfs.ext4") + .arg(pmem_temp_file_retored.as_path()) + .output() + .expect("Expect creating disk image to succeed"); + let pmem_config = PmemConfig::parse( + format!( + "id={},file={}", + pmem_id, + pmem_temp_file_retored.as_path().to_str().unwrap() + ) + .as_str(), + ) + .unwrap(); + + // fs config + let mut shared_dir_restored = workload_path.clone(); + shared_dir_restored.push("restored_shared_dir"); + let fs_params = format!( + "id=myfs0,tag=myfs,native=true,shared_dir={},cache=always,num_queues=1,queue_size=1024", + shared_dir_restored.to_str().unwrap() + ); + let fs_config = FsConfig::parse(fs_params.as_str()).unwrap(); + + _test_restore_from_config( + clh_command("cube-hypervisor").as_str(), + &guest, + snapshot_dir, + Some(vsock_config), + Some(vec![net_config]), + Some(vec![disk_config]), + Some(vec![pmem_config]), + Some(vec![fs_config]), + ); + } + + #[test] + fn test_restored_vsock_reuses_guest_listener_for_new_passthrough_fd() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + + let api_socket_source = format!("{}.1", temp_api_path(&guest.tmp_dir)); + let socket = temp_vsock_path(&guest.tmp_dir); + let event_path = temp_event_monitor_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket_source]) + .args(["--event-monitor", format!("path={}", event_path).as_str()]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args(["--vsock", format!("cid=3,socket={}", socket).as_str()]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir); + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Start socat before taking the snapshot. The restored VM must + // continue using this guest listener rather than starting a new + // one after restore. + guest.start_vsock_passthrough_fd_listener(); + + // Establish passfd and verify the source VM data path before + // taking the snapshot. + guest.check_vsock_passthrough_fd_bidirectional( + socket.as_str(), + "BeforeSnapshot\n", + "BeforeSnapshot", + ); + let listener_identity = guest.vsock_passthrough_fd_listener_identity(); + + // Pause and snapshot using the shared event-checked helper. + snapshot_and_check_events( + api_socket_source.as_str(), + snapshot_dir.as_str(), + event_path.as_str(), + ); + listener_identity + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + let listener_identity = r.as_ref().ok().cloned(); + handle_child_output(r.map(|_| ()), &output); + let listener_identity = listener_identity.unwrap(); + + // The restored VMM must recreate its host Unix listener; the guest + // socat listener remains part of the VM state captured above. + let _ = std::fs::remove_file(&socket); + let api_socket_restored = format!("{}.2", temp_api_path(&guest.tmp_dir)); + let event_path_restored = format!("{}.2", temp_event_monitor_path(&guest.tmp_dir)); + let mut restored_child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket_restored]) + .args([ + "--event-monitor", + format!("path={}", event_path_restored).as_str(), + ]) + .args([ + "--restore", + format!("source_url=file://{}", snapshot_dir).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(10, 0)); + + let r = std::panic::catch_unwind(|| { + let restored_events = [ + &MetaEvent { + event: "restored".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resuming".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resumed".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact( + &restored_events, + &event_path_restored + )); + + assert_eq!( + guest.vsock_passthrough_fd_listener_identity(), + listener_identity, + "restore replaced the socat listener FD captured in the snapshot" + ); + + // Prove the unchanged listener FD still accepts a new + // passfd-backed connection through the restored VMM, and that + // its log continues from the pre-snapshot write. + guest.check_vsock_passthrough_fd_bidirectional( + socket.as_str(), + "AfterRestore\n", + "BeforeSnapshot\nAfterRestore", + ); + }); + + kill_child(&mut restored_child); + let output = restored_child.wait_with_output().unwrap(); + handle_child_output(r, &output); + } + + /// Snapshot a VM that has a native virtiofs share, delete one of the + /// backing files referenced by that snapshot, then restore the VM. + /// + /// This exercises the `migration_on_error: GuestError` policy in the + /// native virtiofs server (see `virtio-devices/src/fs.rs` + /// `init_backend_fs_server`): + /// + /// * with `MigrationOnError::Abort` (the upstream default) the missing + /// inode would tear down the whole live restore; + /// * with `MigrationOnError::GuestError` (our setting) the restore must + /// succeed end-to-end, the surviving files must still be readable, + /// the FS must still accept new operations, and only access to the + /// deleted file must surface an error to the guest. + #[test] + fn test_snapshot_restore_native_virtiofs_with_deleted_backing_file() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + + let api_socket_source = format!("{}.1", temp_api_path(&guest.tmp_dir)); + let event_path = temp_event_monitor_path(&guest.tmp_dir); + + // Carve a private shared_dir under tmp_dir so we can mutate it + // without disturbing other tests that point at ~/workloads/shared_dir. + let shared_dir = guest.tmp_dir.as_path().join("virtiofs_shared"); + std::fs::create_dir_all(&shared_dir).unwrap(); + let survivor_path = shared_dir.join("survivor.txt"); + let victim_path = shared_dir.join("victim.txt"); + std::fs::write(&survivor_path, b"survivor-before-snapshot\n").unwrap(); + std::fs::write(&victim_path, b"victim-before-snapshot\n").unwrap(); + + let fs_params = format!( + "id=myfs0,tag=myfs,native=true,shared_dir={},cache=always,read_only=false,num_queues=1,queue_size=1024", + shared_dir.to_str().unwrap() + ); + + let mut child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket_source]) + .args(["--event-monitor", format!("path={}", event_path).as_str()]) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M,shared=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--fs", fs_params.as_str()]) + .capture_output() + .spawn() + .unwrap(); + + let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Mount virtiofs and prime the inode cache for both files. + // `stat` forces virtiofsd to record an entry for each one in + // its inode store before we take the snapshot. + guest + .ssh_command("mkdir -p mount_dir && sudo mount -t virtiofs myfs mount_dir/") + .unwrap(); + assert_eq!( + guest + .ssh_command("cat mount_dir/survivor.txt") + .unwrap() + .trim(), + "survivor-before-snapshot", + ); + assert_eq!( + guest + .ssh_command("cat mount_dir/victim.txt") + .unwrap() + .trim(), + "victim-before-snapshot", + ); + guest + .ssh_command("stat mount_dir/survivor.txt mount_dir/victim.txt >/dev/null") + .unwrap(); + + snapshot_and_check_events( + api_socket_source.as_str(), + snapshot_dir.as_str(), + event_path.as_str(), + ); + }); + + // Tear down the source VM so its O_PATH fds against the shared dir + // are released. This is what makes the next restore's lookup of the + // deleted file actually fail (rather than succeed via a still-open + // unlinked inode). + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + + // Delete the file the snapshot indexed. The directory itself + // survives so `` still passes virtiofsd's startup checks. + std::fs::remove_file(&victim_path).unwrap(); + assert!(survivor_path.exists()); + assert!(!victim_path.exists()); + + // Restore against the same (now-mutated) shared_dir. + let api_socket_restored = format!("{}.2", temp_api_path(&guest.tmp_dir)); + let event_path_restored = format!("{}.2", temp_event_monitor_path(&guest.tmp_dir)); + + let mut restored_child = GuestCommand::new(&guest) + .args(["--api-socket", &api_socket_restored]) + .args([ + "--event-monitor", + format!("path={}", event_path_restored).as_str(), + ]) + .args([ + "--restore", + format!("source_url=file://{}", snapshot_dir).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + // Give the restore time to complete and the guest to come back. + // Poll the event monitor instead of a fixed sleep — restore time + // varies with host load, and SSH retries below cover the rest. + let restored_events = [ + &MetaEvent { + event: "restored".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resuming".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resumed".to_string(), + device_id: None, + }, + ]; + let mut restored = false; + for _ in 0..30 { + if std::path::Path::new(&event_path_restored).exists() + && check_latest_events_exact(&restored_events, &event_path_restored) + { + restored = true; + break; + } + thread::sleep(std::time::Duration::new(1, 0)); + } + + let r = std::panic::catch_unwind(|| { + // (a) Restore is reported successful even though one indexed + // backing file vanished. This is the migration_on_error = + // GuestError contract — without it we would have aborted. + assert!( + restored, + "VM did not reach `resumed` within the 30s budget — restore likely aborted", + ); + + // (b) The restored guest is alive and the surviving file still + // reads through. virtiofs as a whole has to be functional. + assert_eq!( + guest + .ssh_command("cat mount_dir/survivor.txt") + .unwrap() + .trim(), + "survivor-before-snapshot", + ); + + // (c) Access to the deleted file errors out on the guest side + // (ENOENT). The error is *guest-visible*, not VM-fatal. + // With cache=always the guest retains dentries from before + // snapshot, so `test -e` would still report the file as + // present. Drop the dentry cache first to force a fresh + // FUSE LOOKUP, which correctly returns ENOENT for the + // deleted backing file. + guest + .ssh_command("sync && sudo sh -c 'echo 2 > /proc/sys/vm/drop_caches'") + .unwrap(); + let probe = guest + .ssh_command( + "cat mount_dir/victim.txt >/dev/null 2>&1; \ + echo rc=$?; \ + test -e mount_dir/victim.txt && echo present || echo missing", + ) + .unwrap(); + assert!( + probe.contains("rc=") && !probe.contains("rc=0"), + "expected non-zero exit reading deleted file, got: {probe:?}", + ); + assert!( + probe.contains("missing"), + "expected victim.txt to be missing on guest, got: {probe:?}", + ); + + // (d) The FS must still accept new operations after the + // guest-visible error — i.e. the error did NOT poison the + // mount or panic the guest VFS layer. + guest + .ssh_command("echo post-restore | sudo tee mount_dir/post_restore.txt >/dev/null") + .unwrap(); + assert_eq!( + guest + .ssh_command("cat mount_dir/post_restore.txt") + .unwrap() + .trim(), + "post-restore", + ); + // And the host sees the new file via the same shared_dir. + assert!(shared_dir.join("post_restore.txt").exists()); + }); + + kill_child(&mut restored_child); + let restored_output = restored_child.wait_with_output().unwrap(); + handle_child_output(r, &restored_output); + } +} + +mod compatibility { + use test_infra::clh_command; + + use crate::_test_snapshot_restore_from_different_binary; + + #[test] + fn test_snapshot_from_release_restore_on_head() { + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + let mut release_path = workload_path.clone(); + release_path.push("cube-release"); + release_path.push("cube-hypervisor"); + + let head_path = clh_command("cube-hypervisor"); + _test_snapshot_restore_from_different_binary( + release_path.to_str().unwrap(), + head_path.as_str(), + ); + } +} + +mod vmm_instance { + use crate::MIN_EXPECTED_MEMORY_KB; + use std::fs::File; + use std::io::Write; + use std::path::PathBuf; + #[cfg(feature = "lib_support")] + use std::sync::mpsc::channel; + use std::sync::Arc; + use std::thread; + use std::time::Duration; + + #[cfg(feature = "lib_support")] + use cube_hypervisor::vmm_config::EventNotifyConfig; + use cube_hypervisor::vmm_config::{EventMonitorConfig, VmmConfig}; + #[cfg(feature = "lib_support")] + use cube_hypervisor::NotifyEvent; + use cube_hypervisor::VmmInstance; + use log::info; + use rlimit::{setrlimit, Resource}; + use test_infra::{ssh_command_ip, DiskType, Guest, UbuntuDiskConfig}; + use vm_migration::SnapshotConfig; + use vmm::api::{ApiRequest, ApiResponsePayload}; + use vmm::config::RestoreConfig; + use vmm::vm_config::{ + ConsoleConfig, ConsoleOutputMode, CpusConfig, DiskConfig, NetConfig, PayloadConfig, + VmConfig, + }; + + use crate::x86_64::FOCAL_IMAGE_NAME; + use crate::{ + check_latest_events_exact, check_sequential_events, direct_kernel_boot_path, + temp_event_monitor_path, temp_snapshot_dir_path, MetaEvent, DIRECT_KERNEL_BOOT_CMDLINE, + }; + + struct DummyVmm { + vmm: VmmInstance, + } + + impl DummyVmm { + pub fn new(config: VmmConfig) -> Self { + let vmm = VmmInstance::new(config).unwrap(); + Self { vmm } + } + + pub fn send_request(&self, req: ApiRequest) -> bool { + let req_info = format!("req {req:?}"); + let response = self.vmm.send_request(req).expect("send request error"); + match response { + Ok(body) => match body { + ApiResponsePayload::Empty => true, + ApiResponsePayload::VmInfo(_vm_info) => true, + ApiResponsePayload::VmmPing(_ping_res) => true, + ApiResponsePayload::VmWaitStart(_wait_start_res) => true, + ApiResponsePayload::VmAction(_) => true, + }, + Err(_) => { + println!("error: {req_info}"); + false + } + } + } + + pub fn join(&mut self) { + self.vmm.join().unwrap(); + } + + pub fn join_timeout(&mut self, timeout: Option) { + self.vmm.join_timeout(timeout).unwrap(); + } + } + + fn default_vmm_config() -> VmmConfig { + VmmConfig { + log_stderr: false, + log_json_file: None, + ..Default::default() + } + } + + fn default_vm_config() -> VmConfig { + VmConfig { + payload: Some(PayloadConfig { + kernel: Some(direct_kernel_boot_path()), + cmdline: Some(String::from(DIRECT_KERNEL_BOOT_CMDLINE)), + ..Default::default() + }), + serial: ConsoleConfig { + mode: ConsoleOutputMode::Tty, + ..Default::default() + }, + console: ConsoleConfig { + mode: ConsoleOutputMode::Off, + ..Default::default() + }, + ..Default::default() + } + } + + fn dummy_vmm_setup() { + // Ensure all created files (.e.g sockets) are only accessible by this user + // SAFETY: trivially safe + let _ = unsafe { libc::umask(0o077) }; + + // Create dummy socket and dup it to large fd, so that we could trigger + // expand_files in kernel. And this tricky work will avoid later multi + // thread try to invoke expand_files at same time, there will be lock + // contention in expand_files. + // SAFETY: FFI call + unsafe { + let dummy = libc::socket(libc::AF_UNIX, libc::SOCK_STREAM | libc::SOCK_CLOEXEC, 0); + if dummy > 0 { + libc::dup2(dummy, 512); + } + } + + std::panic::set_hook(Box::new(|info| { + info!("{info:?}"); + log::logger().flush(); + })); + + let coredump_limit: u64 = 2 * 1024 * 1024 * 1024; + let coredump_filter = "0x33".to_string(); + let mut filter_file = File::options() + .write(true) + .open("/proc/self/coredump_filter") + .unwrap(); + filter_file.write_all(coredump_filter.as_bytes()).unwrap(); + setrlimit(Resource::CORE, coredump_limit, coredump_limit).unwrap(); + + cube_hypervisor::set_runtime_seccomp_rules(vec![ + (libc::SYS_sysinfo, vec![]), + (libc::SYS_getcwd, vec![]), + ]); + } + + #[test] + // Start cube-hypervisor with no VM parameters, only the API server running. + // From the API: Create a VM, boot it and check that it looks as expected. + fn test_api_create_boot_and_shutdown() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + dummy_vmm_setup(); + let vmm_config = default_vmm_config(); + let mut vmm = DummyVmm::new(vmm_config); + + // Verify VMM service is running + assert!(vmm.send_request(ApiRequest::VmmPing)); + + // Create the VM first + let mut vm_config = default_vm_config(); + let cpu_count = 4; + vm_config.cpus = CpusConfig { + boot_vcpus: cpu_count, + max_vcpus: cpu_count, + ..Default::default() + }; + let mut disk_config = vec![]; + disk_config.push(DiskConfig { + path: Some(PathBuf::from( + guest.disk_config.disk(DiskType::OperatingSystem).unwrap(), + )), + ..Default::default() + }); + if guest.disk_config.disk(DiskType::CloudInit).is_some() { + disk_config.push(DiskConfig { + path: Some(PathBuf::from( + guest.disk_config.disk(DiskType::CloudInit).unwrap(), + )), + ..Default::default() + }); + } + + vm_config.disks = Some(disk_config); + + let net_string = guest.default_net_string(); + vm_config.net = Some(vec![NetConfig::parse(net_string.as_str()).unwrap()]); + + assert!(vmm.send_request(ApiRequest::VmCreate(Box::new(vm_config)))); + + // Then boot it + assert!(vmm.send_request(ApiRequest::VmBoot)); + + guest.wait_vm_boot(None).unwrap(); + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + assert!(vmm.send_request(ApiRequest::VmmShutdown)); + vmm.join_timeout(Some(Duration::from_secs(5))); + } + + #[test] + fn test_api_delete() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + dummy_vmm_setup(); + let vmm_config = default_vmm_config(); + let mut vmm = DummyVmm::new(vmm_config); + + // Verify VMM service is running + assert!(vmm.send_request(ApiRequest::VmmPing)); + + // Create the VM first + let mut vm_config = default_vm_config(); + let cpu_count = 4; + vm_config.cpus = CpusConfig { + boot_vcpus: cpu_count, + max_vcpus: cpu_count, + ..Default::default() + }; + let mut disk_config = vec![]; + disk_config.push(DiskConfig { + path: Some(PathBuf::from( + guest.disk_config.disk(DiskType::OperatingSystem).unwrap(), + )), + ..Default::default() + }); + if guest.disk_config.disk(DiskType::CloudInit).is_some() { + disk_config.push(DiskConfig { + path: Some(PathBuf::from( + guest.disk_config.disk(DiskType::CloudInit).unwrap(), + )), + ..Default::default() + }); + } + + vm_config.disks = Some(disk_config); + + let net_string = guest.default_net_string(); + vm_config.net = Some(vec![NetConfig::parse(net_string.as_str()).unwrap()]); + + assert!(vmm.send_request(ApiRequest::VmCreate(Box::new(vm_config.clone())))); + + // Then boot it + assert!(vmm.send_request(ApiRequest::VmBoot)); + + guest.wait_vm_boot(None).unwrap(); + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + // Waiting for ssh + thread::sleep(std::time::Duration::new(5, 0)); + + assert!(vmm.send_request(ApiRequest::VmDelete)); + + // Waiting for vm shuwdown + thread::sleep(std::time::Duration::new(5, 0)); + + assert!(vmm.send_request(ApiRequest::VmCreate(Box::new(vm_config)))); + assert!(vmm.send_request(ApiRequest::VmBoot)); + + guest.wait_vm_boot(None).unwrap(); + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + assert!(vmm.send_request(ApiRequest::VmmShutdown)); + vmm.join_timeout(Some(Duration::from_secs(5))); + } + + #[test] + fn test_api_pause_resume() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + dummy_vmm_setup(); + let vmm_config = default_vmm_config(); + let mut vmm = DummyVmm::new(vmm_config); + + // Verify VMM service is running + assert!(vmm.send_request(ApiRequest::VmmPing)); + + // Create the VM first + let mut vm_config = default_vm_config(); + let cpu_count = 4; + vm_config.cpus = CpusConfig { + boot_vcpus: cpu_count, + max_vcpus: cpu_count, + ..Default::default() + }; + let mut disk_config = vec![]; + disk_config.push(DiskConfig { + path: Some(PathBuf::from( + guest.disk_config.disk(DiskType::OperatingSystem).unwrap(), + )), + ..Default::default() + }); + if guest.disk_config.disk(DiskType::CloudInit).is_some() { + disk_config.push(DiskConfig { + path: Some(PathBuf::from( + guest.disk_config.disk(DiskType::CloudInit).unwrap(), + )), + ..Default::default() + }); + } + + vm_config.disks = Some(disk_config); + + let net_string = guest.default_net_string(); + vm_config.net = Some(vec![NetConfig::parse(net_string.as_str()).unwrap()]); + + assert!(vmm.send_request(ApiRequest::VmCreate(Box::new(vm_config)))); + + // Then boot it + assert!(vmm.send_request(ApiRequest::VmBoot)); + + guest.wait_vm_boot(None).unwrap(); + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + // We now pause the VM + assert!(vmm.send_request(ApiRequest::VmPause)); + // Check pausing again fails + assert!(!vmm.send_request(ApiRequest::VmPause)); + + thread::sleep(std::time::Duration::new(1, 0)); + // SSH into the VM should fail + assert!(ssh_command_ip( + "grep -c processor /proc/cpuinfo", + &guest.network.guest_ip, + 2, + 5 + ) + .is_err()); + + // Resume the VM + assert!(vmm.send_request(ApiRequest::VmResume)); + // Check resuming again fails + assert!(!vmm.send_request(ApiRequest::VmResume)); + + thread::sleep(std::time::Duration::new(1, 0)); + // Now we should be able to SSH back in and get the right number of CPUs + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + + assert!(vmm.send_request(ApiRequest::VmmShutdown)); + vmm.join_timeout(Some(Duration::from_secs(5))); + } + + #[test] + fn test_api_snapshot_restore() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let event_path = temp_event_monitor_path(&guest.tmp_dir); + // Create the snapshot directory + let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir); + + dummy_vmm_setup(); + let mut vmm_config = default_vmm_config(); + vmm_config.event_monitor = Some(EventMonitorConfig { + path: Some(event_path.clone()), + fd: None, + }); + let mut vmm = DummyVmm::new(vmm_config); + // Verify VMM service is running + assert!(vmm.send_request(ApiRequest::VmmPing)); + + // Create the VM first + let mut vm_config = default_vm_config(); + let cpu_count = 4; + vm_config.cpus = CpusConfig { + boot_vcpus: cpu_count, + max_vcpus: cpu_count, + ..Default::default() + }; + let mut disk_config = vec![]; + disk_config.push(DiskConfig { + path: Some(PathBuf::from( + guest.disk_config.disk(DiskType::OperatingSystem).unwrap(), + )), + ..Default::default() + }); + if guest.disk_config.disk(DiskType::CloudInit).is_some() { + disk_config.push(DiskConfig { + path: Some(PathBuf::from( + guest.disk_config.disk(DiskType::CloudInit).unwrap(), + )), + ..Default::default() + }); + } + + vm_config.disks = Some(disk_config); + + let net_string = guest.default_net_string(); + vm_config.net = Some(vec![NetConfig::parse(net_string.as_str()).unwrap()]); + + assert!(vmm.send_request(ApiRequest::VmCreate(Box::new(vm_config)))); + + // Then boot it + assert!(vmm.send_request(ApiRequest::VmBoot)); + + guest.wait_vm_boot(None).unwrap(); + // Check that the VM booted as expected + assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + assert!(vmm.send_request(ApiRequest::VmPause)); + let latest_events = [ + &MetaEvent { + event: "pausing".to_string(), + device_id: None, + }, + &MetaEvent { + event: "paused".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact(&latest_events, &event_path)); + + let snapshot_config = SnapshotConfig { + destination_url: format!("file://{snapshot_dir}"), + ..Default::default() + }; + assert!(vmm.send_request(ApiRequest::VmSnapshot(Arc::new(snapshot_config)))); + let latest_events = [ + &MetaEvent { + event: "snapshotting".to_string(), + device_id: None, + }, + &MetaEvent { + event: "snapshotted".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact(&latest_events, &event_path)); + + assert!(vmm.send_request(ApiRequest::VmDelete)); + + let restore_config = RestoreConfig { + source_url: PathBuf::from(format!("file://{snapshot_dir}")), + ..Default::default() + }; + assert!(vmm.send_request(ApiRequest::VmRestore(Arc::new(restore_config)))); + // Wait for the VM to be restored + thread::sleep(std::time::Duration::new(10, 0)); + let expected_events = [&MetaEvent { + event: "restoring".to_string(), + device_id: None, + }]; + assert!(check_sequential_events(&expected_events, &event_path)); + + // Automatically restart the VM after it has been restored + let latest_events = [ + &MetaEvent { + event: "restored".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resuming".to_string(), + device_id: None, + }, + &MetaEvent { + event: "resumed".to_string(), + device_id: None, + }, + ]; + assert!(check_latest_events_exact(&latest_events, &event_path)); + // Perform same checks to validate VM has been properly restored + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 4); + assert!(guest.get_total_memory().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + + assert!(vmm.send_request(ApiRequest::VmmShutdown)); + vmm.join_timeout(Some(Duration::from_secs(5))); + } + + #[cfg(feature = "lib_support")] + #[test] + fn test_get_shutdown_notifier() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + dummy_vmm_setup(); + let mut vmm_config = default_vmm_config(); + let (sender, receiver) = channel(); + vmm_config.event_notifier = Some(EventNotifyConfig { notifier: sender }); + let vmm = DummyVmm::new(vmm_config); + + // Verify VMM service is running + assert!(vmm.send_request(ApiRequest::VmmPing)); + + // Create the VM first + let mut vm_config = default_vm_config(); + let cpu_count = 4; + vm_config.cpus = CpusConfig { + boot_vcpus: cpu_count, + max_vcpus: cpu_count, + ..Default::default() + }; + let mut disk_config = vec![]; + disk_config.push(DiskConfig { + path: Some(PathBuf::from( + guest.disk_config.disk(DiskType::OperatingSystem).unwrap(), + )), + ..Default::default() + }); + if guest.disk_config.disk(DiskType::CloudInit).is_some() { + disk_config.push(DiskConfig { + path: Some(PathBuf::from( + guest.disk_config.disk(DiskType::CloudInit).unwrap(), + )), + ..Default::default() + }); + } + vm_config.disks = Some(disk_config); + let net_string = guest.default_net_string(); + vm_config.net = Some(vec![NetConfig::parse(net_string.as_str()).unwrap()]); + assert!(vmm.send_request(ApiRequest::VmCreate(Box::new(vm_config)))); + + // Then boot it + assert!(vmm.send_request(ApiRequest::VmBoot)); + + guest.ssh_command("sudo poweroff").unwrap(); + + let mut recv_evt_flag = false; + for _ in 0..3 { + let data = receiver.recv_timeout(Duration::from_secs(10)).unwrap(); + if data == NotifyEvent::VmShutdown { + recv_evt_flag = true; + break; + } + } + assert!(recv_evt_flag); + } +} + +mod windows { + use once_cell::sync::Lazy; + + use crate::*; + + static NEXT_DISK_ID: Lazy> = Lazy::new(|| Mutex::new(1)); + + struct WindowsGuest { + guest: Guest, + auth: PasswordAuth, + } + + trait FsType { + const FS_FAT: u8; + const FS_NTFS: u8; + } + impl FsType for WindowsGuest { + const FS_FAT: u8 = 0; + const FS_NTFS: u8 = 1; + } + + impl WindowsGuest { + fn new() -> Self { + let disk = WindowsDiskConfig::new(WINDOWS_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(disk)); + let auth = PasswordAuth { + username: String::from("administrator"), + password: String::from("Admin123"), + }; + + WindowsGuest { guest, auth } + } + + fn guest(&self) -> &Guest { + &self.guest + } + + fn ssh_cmd(&self, cmd: &str) -> String { + ssh_command_ip_with_auth( + cmd, + &self.auth, + &self.guest.network.guest_ip, + DEFAULT_SSH_RETRIES, + DEFAULT_SSH_TIMEOUT, + ) + .unwrap() + } + + fn cpu_count(&self) -> u8 { + self.ssh_cmd("powershell -Command \"(Get-CimInstance win32_computersystem).NumberOfLogicalProcessors\"") + .trim() + .parse::() + .unwrap_or(0) + } + + fn ram_size(&self) -> usize { + self.ssh_cmd("powershell -Command \"(Get-CimInstance win32_computersystem).TotalPhysicalMemory\"") + .trim() + .parse::() + .unwrap_or(0) + } + + fn netdev_count(&self) -> u8 { + self.ssh_cmd("powershell -Command \"netsh int ipv4 show interfaces | Select-String ethernet | Measure-Object -Line | Format-Table -HideTableHeaders\"") + .trim() + .parse::() + .unwrap_or(0) + } + + fn disk_count(&self) -> u8 { + self.ssh_cmd("powershell -Command \"Get-Disk | Measure-Object -Line | Format-Table -HideTableHeaders\"") + .trim() + .parse::() + .unwrap_or(0) + } + + fn reboot(&self) { + let _ = self.ssh_cmd("shutdown /r /t 0"); + } + + fn shutdown(&self) { + let _ = self.ssh_cmd("shutdown /s /t 0"); + } + + fn run_dnsmasq(&self) -> std::process::Child { + let listen_address = format!("--listen-address={}", self.guest.network.host_ip); + let dhcp_host = format!( + "--dhcp-host={},{}", + self.guest.network.guest_mac, self.guest.network.guest_ip + ); + let dhcp_range = format!( + "--dhcp-range=eth,{},{}", + self.guest.network.guest_ip, self.guest.network.guest_ip + ); + + Command::new("dnsmasq") + .arg("--no-daemon") + .arg("--log-queries") + .arg(listen_address.as_str()) + .arg("--except-interface=lo") + .arg("--bind-dynamic") // Allow listening to host_ip while the interface is not ready yet. + .arg("--conf-file=/dev/null") + .arg(dhcp_host.as_str()) + .arg(dhcp_range.as_str()) + .spawn() + .unwrap() + } + + // TODO Cleanup image file explicitly after test, if there's some space issues. + fn disk_new(&self, fs: u8, sz: usize) -> String { + let mut guard = NEXT_DISK_ID.lock().unwrap(); + let id = *guard; + *guard = id + 1; + + let img = PathBuf::from(format!("/tmp/test-hotplug-{}.raw", id)); + let _ = fs::remove_file(&img); + + // Create an image file + let out = Command::new("qemu-img") + .args([ + "create", + "-f", + "raw", + img.to_str().unwrap(), + format!("{}m", sz).as_str(), + ]) + .output() + .expect("qemu-img command failed") + .stdout; + println!("{:?}", out); + + // Associate image to a loop device + let out = Command::new("losetup") + .args(["--show", "-f", img.to_str().unwrap()]) + .output() + .expect("failed to create loop device") + .stdout; + let _tmp = String::from_utf8_lossy(&out); + let loop_dev = _tmp.trim(); + println!("{:?}", out); + + // Create a partition table + // echo 'type=7' | sudo sfdisk "${LOOP}" + let mut child = Command::new("sfdisk") + .args([loop_dev]) + .stdin(Stdio::piped()) + .spawn() + .unwrap(); + let stdin = child.stdin.as_mut().expect("failed to open stdin"); + stdin + .write_all("type=7".as_bytes()) + .expect("failed to write stdin"); + let out = child.wait_with_output().expect("sfdisk failed").stdout; + println!("{:?}", out); + + // Disengage the loop device + let out = Command::new("losetup") + .args(["-d", loop_dev]) + .output() + .expect("loop device not found") + .stdout; + println!("{:?}", out); + + // Re-associate loop device pointing to the partition only + let out = Command::new("losetup") + .args([ + "--show", + "--offset", + (512 * 2048).to_string().as_str(), + "-f", + img.to_str().unwrap(), + ]) + .output() + .expect("failed to create loop device") + .stdout; + let _tmp = String::from_utf8_lossy(&out); + let loop_dev = _tmp.trim(); + println!("{:?}", out); + + // Create filesystem. + let fs_cmd = match fs { + WindowsGuest::FS_FAT => "mkfs.msdos", + WindowsGuest::FS_NTFS => "mkfs.ntfs", + _ => panic!("Unknown filesystem type '{}'", fs), + }; + let out = Command::new(fs_cmd) + .args([&loop_dev]) + .output() + .unwrap_or_else(|_| panic!("{} failed", fs_cmd)) + .stdout; + println!("{:?}", out); + + // Disengage the loop device + let out = Command::new("losetup") + .args(["-d", loop_dev]) + .output() + .unwrap_or_else(|_| panic!("loop device '{}' not found", loop_dev)) + .stdout; + println!("{:?}", out); + + img.to_str().unwrap().to_string() + } + + fn disks_set_rw(&self) { + let _ = self.ssh_cmd("powershell -Command \"Get-Disk | Where-Object IsOffline -eq $True | Set-Disk -IsReadOnly $False\""); + } + + fn disks_online(&self) { + let _ = self.ssh_cmd("powershell -Command \"Get-Disk | Where-Object IsOffline -eq $True | Set-Disk -IsOffline $False\""); + } + + fn disk_file_put(&self, fname: &str, data: &str) { + let _ = self.ssh_cmd(&format!( + "powershell -Command \"'{}' | Set-Content -Path {}\"", + data, fname + )); + } + + fn disk_file_read(&self, fname: &str) -> String { + self.ssh_cmd(&format!( + "powershell -Command \"Get-Content -Path {}\"", + fname + )) + } + + fn wait_for_boot(&self) -> bool { + let cmd = "dir /b c:\\ | find \"Windows\""; + let tmo_max = 180; + // The timeout increase by n*1+n*2+n*3+..., therefore the initial + // interval must be small. + let tmo_int = 2; + let out = ssh_command_ip_with_auth( + cmd, + &self.auth, + &self.guest.network.guest_ip, + { + let mut ret = 1; + let mut tmo_acc = 0; + loop { + tmo_acc += tmo_int * ret; + if tmo_acc >= tmo_max { + break; + } + ret += 1; + } + ret + }, + tmo_int, + ) + .unwrap(); + + if "Windows" == out.trim() { + return true; + } + + false + } + } + + fn vcpu_threads_count(pid: u32) -> u8 { + // ps -T -p 12345 | grep vcpu | wc -l + let out = Command::new("ps") + .args(["-T", "-p", format!("{}", pid).as_str()]) + .output() + .expect("ps command failed") + .stdout; + return String::from_utf8_lossy(&out).matches("vcpu").count() as u8; + } + + fn netdev_ctrl_threads_count(pid: u32) -> u8 { + // ps -T -p 12345 | grep "_net[0-9]*_ctrl" | wc -l + let out = Command::new("ps") + .args(["-T", "-p", format!("{}", pid).as_str()]) + .output() + .expect("ps command failed") + .stdout; + let mut n = 0; + String::from_utf8_lossy(&out) + .split_whitespace() + .for_each(|s| n += (s.starts_with("_net") && s.ends_with("_ctrl")) as u8); // _net1_ctrl + n + } + + fn disk_ctrl_threads_count(pid: u32) -> u8 { + // ps -T -p 15782 | grep "_disk[0-9]*_q0" | wc -l + let out = Command::new("ps") + .args(["-T", "-p", format!("{}", pid).as_str()]) + .output() + .expect("ps command failed") + .stdout; + let mut n = 0; + String::from_utf8_lossy(&out) + .split_whitespace() + .for_each(|s| n += (s.starts_with("_disk") && s.ends_with("_q0")) as u8); // _disk0_q0, don't care about multiple queues as they're related to the same hdd + n + } + + #[test] + fn test_windows_guest() { + let windows_guest = WindowsGuest::new(); + + let mut child = GuestCommand::new(windows_guest.guest()) + .args(["--cpus", "boot=2,kvm_hyperv=on"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", edk2_path().to_str().unwrap()]) + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let fd = child.stdout.as_ref().unwrap().as_raw_fd(); + let pipesize = unsafe { libc::fcntl(fd, libc::F_SETPIPE_SZ, PIPE_SIZE) }; + let fd = child.stderr.as_ref().unwrap().as_raw_fd(); + let pipesize1 = unsafe { libc::fcntl(fd, libc::F_SETPIPE_SZ, PIPE_SIZE) }; + + assert!(pipesize >= PIPE_SIZE && pipesize1 >= PIPE_SIZE); + + let mut child_dnsmasq = windows_guest.run_dnsmasq(); + + let r = std::panic::catch_unwind(|| { + // Wait to make sure Windows boots up + assert!(windows_guest.wait_for_boot()); + + windows_guest.shutdown(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(60)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let _ = child_dnsmasq.kill(); + let _ = child_dnsmasq.wait(); + + handle_child_output(r, &output); + } + + #[test] + fn test_windows_guest_multiple_queues() { + let windows_guest = WindowsGuest::new(); + + let mut ovmf_path = dirs::home_dir().unwrap(); + ovmf_path.push("workloads"); + ovmf_path.push(OVMF_NAME); + + let mut child = GuestCommand::new(windows_guest.guest()) + .args(["--cpus", "boot=4,kvm_hyperv=on"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", ovmf_path.to_str().unwrap()]) + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .args([ + "--disk", + format!( + "path={},num_queues=4", + windows_guest + .guest() + .disk_config + .disk(DiskType::OperatingSystem) + .unwrap() + ) + .as_str(), + ]) + .args([ + "--net", + format!( + "tap=,mac={},ip={},mask=255.255.255.0,num_queues=8", + windows_guest.guest().network.guest_mac, + windows_guest.guest().network.host_ip + ) + .as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + let fd = child.stdout.as_ref().unwrap().as_raw_fd(); + let pipesize = unsafe { libc::fcntl(fd, libc::F_SETPIPE_SZ, PIPE_SIZE) }; + let fd = child.stderr.as_ref().unwrap().as_raw_fd(); + let pipesize1 = unsafe { libc::fcntl(fd, libc::F_SETPIPE_SZ, PIPE_SIZE) }; + + assert!(pipesize >= PIPE_SIZE && pipesize1 >= PIPE_SIZE); + + let mut child_dnsmasq = windows_guest.run_dnsmasq(); + + let r = std::panic::catch_unwind(|| { + // Wait to make sure Windows boots up + assert!(windows_guest.wait_for_boot()); + + windows_guest.shutdown(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(60)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let _ = child_dnsmasq.kill(); + let _ = child_dnsmasq.wait(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + #[ignore = "See #4327"] + fn test_windows_guest_snapshot_restore() { + let windows_guest = WindowsGuest::new(); + + let mut ovmf_path = dirs::home_dir().unwrap(); + ovmf_path.push("workloads"); + ovmf_path.push(OVMF_NAME); + + let tmp_dir = TempDir::new_with_prefix("/tmp/ch").unwrap(); + let api_socket_source = format!("{}.1", temp_api_path(&tmp_dir)); + + let mut child = GuestCommand::new(windows_guest.guest()) + .args(["--api-socket", &api_socket_source]) + .args(["--cpus", "boot=2,kvm_hyperv=on"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", ovmf_path.to_str().unwrap()]) + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let fd = child.stdout.as_ref().unwrap().as_raw_fd(); + let pipesize = unsafe { libc::fcntl(fd, libc::F_SETPIPE_SZ, PIPE_SIZE) }; + let fd = child.stderr.as_ref().unwrap().as_raw_fd(); + let pipesize1 = unsafe { libc::fcntl(fd, libc::F_SETPIPE_SZ, PIPE_SIZE) }; + + assert!(pipesize >= PIPE_SIZE && pipesize1 >= PIPE_SIZE); + + let mut child_dnsmasq = windows_guest.run_dnsmasq(); + + // Wait to make sure Windows boots up + assert!(windows_guest.wait_for_boot()); + + let snapshot_dir = temp_snapshot_dir_path(&tmp_dir); + + // Pause the VM + assert!(remote_command(&api_socket_source, "pause", None)); + + // Take a snapshot from the VM + assert!(remote_command( + &api_socket_source, + "snapshot", + Some(format!("file://{}", snapshot_dir).as_str()), + )); + + // Wait to make sure the snapshot is completed + thread::sleep(std::time::Duration::new(30, 0)); + + let _ = child.kill(); + child.wait().unwrap(); + + let api_socket_restored = format!("{}.2", temp_api_path(&tmp_dir)); + + // Restore the VM from the snapshot + let mut child = GuestCommand::new(windows_guest.guest()) + .args(["--api-socket", &api_socket_restored]) + .args([ + "--restore", + format!("source_url=file://{}", snapshot_dir).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + // Wait for the VM to be restored + thread::sleep(std::time::Duration::new(20, 0)); + + let r = std::panic::catch_unwind(|| { + // Resume the VM + assert!(remote_command(&api_socket_restored, "resume", None)); + + windows_guest.shutdown(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(60)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let _ = child_dnsmasq.kill(); + let _ = child_dnsmasq.wait(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + #[cfg(not(target_arch = "aarch64"))] + fn test_windows_guest_cpu_hotplug() { + let windows_guest = WindowsGuest::new(); + + let mut ovmf_path = dirs::home_dir().unwrap(); + ovmf_path.push("workloads"); + ovmf_path.push(OVMF_NAME); + + let tmp_dir = TempDir::new_with_prefix("/tmp/ch").unwrap(); + let api_socket = temp_api_path(&tmp_dir); + + let mut child = GuestCommand::new(windows_guest.guest()) + .args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=2,max=8,kvm_hyperv=on"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", ovmf_path.to_str().unwrap()]) + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let mut child_dnsmasq = windows_guest.run_dnsmasq(); + + let r = std::panic::catch_unwind(|| { + // Wait to make sure Windows boots up + assert!(windows_guest.wait_for_boot()); + + let vcpu_num = 2; + // Check the initial number of CPUs the guest sees + assert_eq!(windows_guest.cpu_count(), vcpu_num); + // Check the initial number of vcpu threads in the CH process + assert_eq!(vcpu_threads_count(child.id()), vcpu_num); + + let vcpu_num = 6; + // Hotplug some CPUs + resize_command(&api_socket, Some(vcpu_num), None, None, None); + // Wait to make sure CPUs are added + thread::sleep(std::time::Duration::new(10, 0)); + // Check the guest sees the correct number + assert_eq!(windows_guest.cpu_count(), vcpu_num); + // Check the CH process has the correct number of vcpu threads + assert_eq!(vcpu_threads_count(child.id()), vcpu_num); + + let vcpu_num = 4; + // Remove some CPUs. Note that Windows doesn't support hot-remove. + resize_command(&api_socket, Some(vcpu_num), None, None, None); + // Wait to make sure CPUs are removed + thread::sleep(std::time::Duration::new(10, 0)); + // Reboot to let Windows catch up + windows_guest.reboot(); + // Wait to make sure Windows completely rebooted + thread::sleep(std::time::Duration::new(60, 0)); + // Check the guest sees the correct number + assert_eq!(windows_guest.cpu_count(), vcpu_num); + // Check the CH process has the correct number of vcpu threads + assert_eq!(vcpu_threads_count(child.id()), vcpu_num); + + windows_guest.shutdown(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(60)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let _ = child_dnsmasq.kill(); + let _ = child_dnsmasq.wait(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + #[cfg(not(target_arch = "aarch64"))] + fn test_windows_guest_ram_hotplug() { + let windows_guest = WindowsGuest::new(); + + let mut ovmf_path = dirs::home_dir().unwrap(); + ovmf_path.push("workloads"); + ovmf_path.push(OVMF_NAME); + + let tmp_dir = TempDir::new_with_prefix("/tmp/ch").unwrap(); + let api_socket = temp_api_path(&tmp_dir); + + let mut child = GuestCommand::new(windows_guest.guest()) + .args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=2,kvm_hyperv=on"]) + .args(["--memory", "size=2G,hotplug_size=5G"]) + .args(["--kernel", ovmf_path.to_str().unwrap()]) + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let mut child_dnsmasq = windows_guest.run_dnsmasq(); + + let r = std::panic::catch_unwind(|| { + // Wait to make sure Windows boots up + assert!(windows_guest.wait_for_boot()); + + let ram_size = 2 * 1024 * 1024 * 1024; + // Check the initial number of RAM the guest sees + let current_ram_size = windows_guest.ram_size(); + // This size seems to be reserved by the system and thus the + // reported amount differs by this constant value. + let reserved_ram_size = ram_size - current_ram_size; + // Verify that there's not more than 4mb constant diff wasted + // by the reserved ram. + assert!(reserved_ram_size < 4 * 1024 * 1024); + + let ram_size = 4 * 1024 * 1024 * 1024; + // Hotplug some RAM + resize_command(&api_socket, None, Some(ram_size), None, None); + // Wait to make sure RAM has been added + thread::sleep(std::time::Duration::new(10, 0)); + // Check the guest sees the correct number + assert_eq!(windows_guest.ram_size(), ram_size - reserved_ram_size); + + let ram_size = 3 * 1024 * 1024 * 1024; + // Unplug some RAM. Note that hot-remove most likely won't work. + resize_command(&api_socket, None, Some(ram_size), None, None); + // Wait to make sure RAM has been added + thread::sleep(std::time::Duration::new(10, 0)); + // Reboot to let Windows catch up + windows_guest.reboot(); + // Wait to make sure guest completely rebooted + thread::sleep(std::time::Duration::new(60, 0)); + // Check the guest sees the correct number + assert_eq!(windows_guest.ram_size(), ram_size - reserved_ram_size); + + windows_guest.shutdown(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(60)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let _ = child_dnsmasq.kill(); + let _ = child_dnsmasq.wait(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_windows_guest_netdev_hotplug() { + let windows_guest = WindowsGuest::new(); + + let mut ovmf_path = dirs::home_dir().unwrap(); + ovmf_path.push("workloads"); + ovmf_path.push(OVMF_NAME); + + let tmp_dir = TempDir::new_with_prefix("/tmp/ch").unwrap(); + let api_socket = temp_api_path(&tmp_dir); + + let mut child = GuestCommand::new(windows_guest.guest()) + .args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=2,kvm_hyperv=on"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", ovmf_path.to_str().unwrap()]) + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let mut child_dnsmasq = windows_guest.run_dnsmasq(); + + let r = std::panic::catch_unwind(|| { + // Wait to make sure Windows boots up + assert!(windows_guest.wait_for_boot()); + + // Initially present network device + let netdev_num = 1; + assert_eq!(windows_guest.netdev_count(), netdev_num); + assert_eq!(netdev_ctrl_threads_count(child.id()), netdev_num); + + // Hotplug network device + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-net", + Some(windows_guest.guest().default_net_string().as_str()), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output).contains("\"id\":\"_net2\"")); + thread::sleep(std::time::Duration::new(5, 0)); + // Verify the device is on the system + let netdev_num = 2; + assert_eq!(windows_guest.netdev_count(), netdev_num); + assert_eq!(netdev_ctrl_threads_count(child.id()), netdev_num); + + // Remove network device + let cmd_success = remote_command(&api_socket, "remove-device", Some("_net2")); + assert!(cmd_success); + thread::sleep(std::time::Duration::new(5, 0)); + // Verify the device has been removed + let netdev_num = 1; + assert_eq!(windows_guest.netdev_count(), netdev_num); + assert_eq!(netdev_ctrl_threads_count(child.id()), netdev_num); + + windows_guest.shutdown(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(60)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let _ = child_dnsmasq.kill(); + let _ = child_dnsmasq.wait(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + #[cfg(not(target_arch = "aarch64"))] + fn test_windows_guest_disk_hotplug() { + let windows_guest = WindowsGuest::new(); + + let mut ovmf_path = dirs::home_dir().unwrap(); + ovmf_path.push("workloads"); + ovmf_path.push(OVMF_NAME); + + let tmp_dir = TempDir::new_with_prefix("/tmp/ch").unwrap(); + let api_socket = temp_api_path(&tmp_dir); + + let mut child = GuestCommand::new(windows_guest.guest()) + .args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=2,kvm_hyperv=on"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", ovmf_path.to_str().unwrap()]) + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let mut child_dnsmasq = windows_guest.run_dnsmasq(); + + let disk = windows_guest.disk_new(WindowsGuest::FS_FAT, 100); + + let r = std::panic::catch_unwind(|| { + // Wait to make sure Windows boots up + assert!(windows_guest.wait_for_boot()); + + // Initially present disk device + let disk_num = 1; + assert_eq!(windows_guest.disk_count(), disk_num); + assert_eq!(disk_ctrl_threads_count(child.id()), disk_num); + + // Hotplug disk device + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-disk", + Some(format!("path={},readonly=off", disk).as_str()), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output).contains("\"id\":\"_disk2\"")); + thread::sleep(std::time::Duration::new(5, 0)); + // Online disk device + windows_guest.disks_set_rw(); + windows_guest.disks_online(); + // Verify the device is on the system + let disk_num = 2; + assert_eq!(windows_guest.disk_count(), disk_num); + assert_eq!(disk_ctrl_threads_count(child.id()), disk_num); + + let data = "hello"; + let fname = "d:\\world"; + windows_guest.disk_file_put(fname, data); + + // Unmount disk device + let cmd_success = remote_command(&api_socket, "remove-device", Some("_disk2")); + assert!(cmd_success); + thread::sleep(std::time::Duration::new(5, 0)); + // Verify the device has been removed + let disk_num = 1; + assert_eq!(windows_guest.disk_count(), disk_num); + assert_eq!(disk_ctrl_threads_count(child.id()), disk_num); + + // Remount and check the file exists with the expected contents + let (cmd_success, _cmd_output) = remote_command_w_output( + &api_socket, + "add-disk", + Some(format!("path={},readonly=off", disk).as_str()), + ); + assert!(cmd_success); + thread::sleep(std::time::Duration::new(5, 0)); + let out = windows_guest.disk_file_read(fname); + assert_eq!(data, out.trim()); + + // Intentionally no unmount, it'll happen at shutdown. + + windows_guest.shutdown(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(60)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let _ = child_dnsmasq.kill(); + let _ = child_dnsmasq.wait(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + #[cfg(not(target_arch = "aarch64"))] + fn test_windows_guest_disk_hotplug_multi() { + let windows_guest = WindowsGuest::new(); + + let mut ovmf_path = dirs::home_dir().unwrap(); + ovmf_path.push("workloads"); + ovmf_path.push(OVMF_NAME); + + let tmp_dir = TempDir::new_with_prefix("/tmp/ch").unwrap(); + let api_socket = temp_api_path(&tmp_dir); + + let mut child = GuestCommand::new(windows_guest.guest()) + .args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=2,kvm_hyperv=on"]) + .args(["--memory", "size=2G"]) + .args(["--kernel", ovmf_path.to_str().unwrap()]) + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let mut child_dnsmasq = windows_guest.run_dnsmasq(); + + // Predefined data to used at various test stages + let disk_test_data: [[String; 4]; 2] = [ + [ + "_disk2".to_string(), + windows_guest.disk_new(WindowsGuest::FS_FAT, 123), + "d:\\world".to_string(), + "hello".to_string(), + ], + [ + "_disk3".to_string(), + windows_guest.disk_new(WindowsGuest::FS_NTFS, 333), + "e:\\hello".to_string(), + "world".to_string(), + ], + ]; + + let r = std::panic::catch_unwind(|| { + // Wait to make sure Windows boots up + assert!(windows_guest.wait_for_boot()); + + // Initially present disk device + let disk_num = 1; + assert_eq!(windows_guest.disk_count(), disk_num); + assert_eq!(disk_ctrl_threads_count(child.id()), disk_num); + + for it in &disk_test_data { + let disk_id = it[0].as_str(); + let disk = it[1].as_str(); + // Hotplug disk device + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-disk", + Some(format!("path={},readonly=off", disk).as_str()), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output) + .contains(format!("\"id\":\"{}\"", disk_id).as_str())); + thread::sleep(std::time::Duration::new(5, 0)); + // Online disk devices + windows_guest.disks_set_rw(); + windows_guest.disks_online(); + } + // Verify the devices are on the system + let disk_num = (disk_test_data.len() + 1) as u8; + assert_eq!(windows_guest.disk_count(), disk_num); + assert_eq!(disk_ctrl_threads_count(child.id()), disk_num); + + // Put test data + for it in &disk_test_data { + let fname = it[2].as_str(); + let data = it[3].as_str(); + windows_guest.disk_file_put(fname, data); + } + + // Unmount disk devices + for it in &disk_test_data { + let disk_id = it[0].as_str(); + let cmd_success = remote_command(&api_socket, "remove-device", Some(disk_id)); + assert!(cmd_success); + thread::sleep(std::time::Duration::new(5, 0)); + } + + // Verify the devices have been removed + let disk_num = 1; + assert_eq!(windows_guest.disk_count(), disk_num); + assert_eq!(disk_ctrl_threads_count(child.id()), disk_num); + + // Remount + for it in &disk_test_data { + let disk = it[1].as_str(); + let (cmd_success, _cmd_output) = remote_command_w_output( + &api_socket, + "add-disk", + Some(format!("path={},readonly=off", disk).as_str()), + ); + assert!(cmd_success); + thread::sleep(std::time::Duration::new(5, 0)); + } + + // Check the files exists with the expected contents + for it in &disk_test_data { + let fname = it[2].as_str(); + let data = it[3].as_str(); + let out = windows_guest.disk_file_read(fname); + assert_eq!(data, out.trim()); + } + + // Intentionally no unmount, it'll happen at shutdown. + + windows_guest.shutdown(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(60)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let _ = child_dnsmasq.kill(); + let _ = child_dnsmasq.wait(); + + handle_child_output(r, &output); + } + + #[test] + #[cfg(not(feature = "mshv"))] + #[cfg(not(target_arch = "aarch64"))] + fn test_windows_guest_netdev_multi() { + let windows_guest = WindowsGuest::new(); + + let mut ovmf_path = dirs::home_dir().unwrap(); + ovmf_path.push("workloads"); + ovmf_path.push(OVMF_NAME); + + let tmp_dir = TempDir::new_with_prefix("/tmp/ch").unwrap(); + let api_socket = temp_api_path(&tmp_dir); + + let mut child = GuestCommand::new(windows_guest.guest()) + .args(["--api-socket", &api_socket]) + .args(["--cpus", "boot=2,kvm_hyperv=on"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", ovmf_path.to_str().unwrap()]) + .args(["--serial", "tty"]) + .args(["--console", "off"]) + .default_disks() + // The multi net dev config is borrowed from test_multiple_network_interfaces + .args([ + "--net", + windows_guest.guest().default_net_string().as_str(), + "tap=,mac=8a:6b:6f:5a:de:ac,ip=192.168.3.1,mask=255.255.255.0", + "tap=mytap42,mac=fe:1f:9e:e1:60:f2,ip=192.168.4.1,mask=255.255.255.0", + ]) + .capture_output() + .spawn() + .unwrap(); + + let mut child_dnsmasq = windows_guest.run_dnsmasq(); + + let r = std::panic::catch_unwind(|| { + // Wait to make sure Windows boots up + assert!(windows_guest.wait_for_boot()); + + let netdev_num = 3; + assert_eq!(windows_guest.netdev_count(), netdev_num); + assert_eq!(netdev_ctrl_threads_count(child.id()), netdev_num); + + let tap_count = exec_host_command_output("ip link | grep -c mytap42"); + assert_eq!(String::from_utf8_lossy(&tap_count.stdout).trim(), "1"); + + windows_guest.shutdown(); + }); + + let _ = child.wait_timeout(std::time::Duration::from_secs(60)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let _ = child_dnsmasq.kill(); + let _ = child_dnsmasq.wait(); + + handle_child_output(r, &output); + } +} + +#[cfg(target_arch = "x86_64")] +mod sgx { + use crate::*; + + #[test] + fn test_sgx() { + let focal = UbuntuDiskConfig::new(FOCAL_SGX_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut kernel_path = workload_path; + kernel_path.push("vmlinux_w_sgx"); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .default_net() + .args(["--sgx-epc", "id=epc0,size=64M"]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check if SGX is correctly detected in the guest. + guest.check_sgx_support().unwrap(); + + // Validate the SGX EPC section is 64MiB. + assert_eq!( + guest + .ssh_command("cpuid -l 0x12 -s 2 | grep 'section size' | cut -d '=' -f 2") + .unwrap() + .trim(), + "0x0000000004000000" + ); + + // Run a test relying on SGX enclaves and check if it runs + // successfully. + assert!(guest + .ssh_command("cd /linux-sgx/SampleCode/LocalAttestation/bin/ && sudo ./app") + .unwrap() + .trim() + .contains( + "succeed to load enclaves.\nsucceed to \ + establish secure channel.\nSucceed to exchange \ + secure message...\nSucceed to close Session..." + )); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } +} + +#[cfg(target_arch = "x86_64")] +mod vfio { + use crate::*; + + #[test] + // The VFIO integration test starts cloud-hypervisor guest with 3 TAP + // backed networking interfaces, bound through a simple bridge on the host. + // So if the nested cloud-hypervisor succeeds in getting a directly + // assigned interface from its cloud-hypervisor host, we should be able to + // ssh into it, and verify that it's running with the right kernel command + // line (We tag the command line from cloud-hypervisor for that purpose). + // The third device is added to validate that hotplug works correctly since + // it is being added to the L2 VM through hotplugging mechanism. + // Also, we pass-through a vitio-blk device to the L2 VM to test the 32-bit + // vfio device support + fn test_vfio() { + setup_vfio_network_interfaces(); + + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new_from_ip_range(Box::new(focal), "172.18", 0); + + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let kernel_path = direct_kernel_boot_path(); + + let mut vfio_path = workload_path.clone(); + vfio_path.push("vfio"); + + let mut cloud_init_vfio_base_path = vfio_path.clone(); + cloud_init_vfio_base_path.push("cloudinit.img"); + + // We copy our cloudinit into the vfio mount point, for the nested + // cloud-hypervisor guest to use. + rate_limited_copy( + guest.disk_config.disk(DiskType::CloudInit).unwrap(), + &cloud_init_vfio_base_path, + ) + .expect("copying of cloud-init disk failed"); + + let mut vfio_disk_path = workload_path.clone(); + vfio_disk_path.push("vfio.img"); + + // Create the vfio disk image + let output = Command::new("mkfs.ext4") + .arg("-d") + .arg(vfio_path.to_str().unwrap()) + .arg(vfio_disk_path.to_str().unwrap()) + .arg("2g") + .output() + .unwrap(); + if !output.status.success() { + eprintln!("{}", String::from_utf8_lossy(&output.stderr)); + panic!("mkfs.ext4 command generated an error"); + } + + let mut blk_file_path = workload_path; + blk_file_path.push("blk.img"); + + let vfio_tap0 = "vfio-tap0"; + let vfio_tap1 = "vfio-tap1"; + let vfio_tap2 = "vfio-tap2"; + let vfio_tap3 = "vfio-tap3"; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=4"]) + .args(["--memory", "size=2G,hugepages=on,shared=on"]) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + format!("path={}", vfio_disk_path.to_str().unwrap()).as_str(), + format!("path={},iommu=on", blk_file_path.to_str().unwrap()).as_str(), + ]) + .args([ + "--cmdline", + format!( + "{} kvm-intel.nested=1 vfio_iommu_type1.allow_unsafe_interrupts", + DIRECT_KERNEL_BOOT_CMDLINE + ) + .as_str(), + ]) + .args([ + "--net", + format!("tap={},mac={}", vfio_tap0, guest.network.guest_mac).as_str(), + format!( + "tap={},mac={},iommu=on", + vfio_tap1, guest.network.l2_guest_mac1 + ) + .as_str(), + format!( + "tap={},mac={},iommu=on", + vfio_tap2, guest.network.l2_guest_mac2 + ) + .as_str(), + format!( + "tap={},mac={},iommu=on", + vfio_tap3, guest.network.l2_guest_mac3 + ) + .as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(30, 0)); + + let r = std::panic::catch_unwind(|| { + guest.ssh_command_l1("sudo systemctl start vfio").unwrap(); + thread::sleep(std::time::Duration::new(120, 0)); + + // We booted our cloud hypervisor L2 guest with a "VFIOTAG" tag + // added to its kernel command line. + // Let's ssh into it and verify that it's there. If it is it means + // we're in the right guest (The L2 one) because the QEMU L1 guest + // does not have this command line tag. + assert_eq!( + guest + .ssh_command_l2_1("grep -c VFIOTAG /proc/cmdline") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Let's also verify from the second virtio-net device passed to + // the L2 VM. + assert_eq!( + guest + .ssh_command_l2_2("grep -c VFIOTAG /proc/cmdline") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Check the amount of PCI devices appearing in L2 VM. + assert_eq!( + guest + .ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 8, + ); + + // Check both if /dev/vdc exists and if the block size is 16M in L2 VM + assert_eq!( + guest + .ssh_command_l2_1("lsblk | grep vdc | grep -c 16M") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Hotplug an extra virtio-net device through L2 VM. + guest + .ssh_command_l1( + "echo 0000:00:09.0 | sudo tee /sys/bus/pci/devices/0000:00:09.0/driver/unbind", + ) + .unwrap(); + guest + .ssh_command_l1("echo 0000:00:09.0 | sudo tee /sys/bus/pci/drivers/vfio-pci/bind") + .unwrap(); + let vfio_hotplug_output = guest + .ssh_command_l1( + "sudo /mnt/ch-remote \ + --api-socket=/tmp/ch_api.sock \ + add-device path=/sys/bus/pci/devices/0000:00:09.0,id=vfio123", + ) + .unwrap(); + assert!(vfio_hotplug_output.contains("{\"id\":\"vfio123\",\"bdf\":\"0000:00:08.0\"}")); + + thread::sleep(std::time::Duration::new(10, 0)); + + // Let's also verify from the third virtio-net device passed to + // the L2 VM. This third device has been hotplugged through the L2 + // VM, so this is our way to validate hotplug works for VFIO PCI. + assert_eq!( + guest + .ssh_command_l2_3("grep -c VFIOTAG /proc/cmdline") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 1 + ); + + // Check the amount of PCI devices appearing in L2 VM. + // There should be one more device than before, raising the count + // up to 9 PCI devices. + assert_eq!( + guest + .ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 9, + ); + + // Let's now verify that we can correctly remove the virtio-net + // device through the "remove-device" command responsible for + // unplugging VFIO devices. + guest + .ssh_command_l1( + "sudo /mnt/ch-remote \ + --api-socket=/tmp/ch_api.sock \ + remove-device vfio123", + ) + .unwrap(); + thread::sleep(std::time::Duration::new(10, 0)); + + // Check the amount of PCI devices appearing in L2 VM is back down + // to 8 devices. + assert_eq!( + guest + .ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 8, + ); + + // Perform memory hotplug in L2 and validate the memory is showing + // up as expected. In order to check, we will use the virtio-net + // device already passed through L2 as a VFIO device, this will + // verify that VFIO devices are functional with memory hotplug. + assert!(guest.get_total_memory_l2().unwrap_or_default() > MIN_EXPECTED_MEMORY_KB); + guest + .ssh_command_l2_1( + "sudo bash -c 'echo online > /sys/devices/system/memory/auto_online_blocks'", + ) + .unwrap(); + guest + .ssh_command_l1( + "sudo /mnt/ch-remote \ + --api-socket=/tmp/ch_api.sock \ + resize --memory=1073741824", + ) + .unwrap(); + assert!(guest.get_total_memory_l2().unwrap_or_default() > 960_000); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + cleanup_vfio_network_interfaces(); + + handle_child_output(r, &output); + } + + fn test_nvidia_card_memory_hotplug(hotplug_method: &str) { + let hirsute = UbuntuDiskConfig::new(HIRSUTE_NVIDIA_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(hirsute)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=4"]) + .args([ + "--memory", + format!("size=4G,hotplug_size=4G,hotplug_method={}", hotplug_method).as_str(), + ]) + .args(["--kernel", fw_path(FwType::RustHypervisorFirmware).as_str()]) + .args(["--device", "path=/sys/bus/pci/devices/0000:31:00.0/"]) + .args(["--api-socket", &api_socket]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000); + + guest.enable_memory_hotplug(); + + // Add RAM to the VM + let desired_ram = 6 << 30; + resize_command(&api_socket, None, Some(desired_ram), None, None); + thread::sleep(std::time::Duration::new(30, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 5_760_000); + + // Check the VFIO device works when RAM is increased to 6GiB + guest.check_nvidia_gpu(); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_nvidia_card_memory_hotplug_acpi() { + test_nvidia_card_memory_hotplug("acpi") + } + + #[test] + fn test_nvidia_card_memory_hotplug_virtio_mem() { + test_nvidia_card_memory_hotplug("virtio-mem") + } + + #[test] + fn test_nvidia_card_pci_hotplug() { + let hirsute = UbuntuDiskConfig::new(HIRSUTE_NVIDIA_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(hirsute)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=4"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", fw_path(FwType::RustHypervisorFirmware).as_str()]) + .args(["--api-socket", &api_socket]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Hotplug the card to the VM + let (cmd_success, cmd_output) = remote_command_w_output( + &api_socket, + "add-device", + Some("id=vfio0,path=/sys/bus/pci/devices/0000:31:00.0/"), + ); + assert!(cmd_success); + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"vfio0\",\"bdf\":\"0000:00:06.0\"}")); + + thread::sleep(std::time::Duration::new(10, 0)); + + // Check the VFIO device works after hotplug + guest.check_nvidia_gpu(); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } + + #[test] + fn test_nvidia_card_reboot() { + let hirsute = UbuntuDiskConfig::new(HIRSUTE_NVIDIA_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(hirsute)); + let api_socket = temp_api_path(&guest.tmp_dir); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=4"]) + .args(["--memory", "size=4G"]) + .args(["--kernel", fw_path(FwType::RustHypervisorFirmware).as_str()]) + .args(["--device", "path=/sys/bus/pci/devices/0000:31:00.0/"]) + .args(["--api-socket", &api_socket]) + .default_disks() + .default_net() + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Check the VFIO device works after boot + guest.check_nvidia_gpu(); + + // guest.reboot_linux(0, None); + // + // // Check the VFIO device works after reboot + // guest.check_nvidia_gpu(); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + } +} + +mod live_migration { + use crate::*; + + fn start_live_migration( + migration_socket: &str, + src_api_socket: &str, + dest_api_socket: &str, + local: bool, + ) -> bool { + // Start to receive migration from the destintion VM + let mut receive_migration = Command::new(clh_command("ch-remote")) + .args([ + &format!("--api-socket={}", dest_api_socket), + "receive-migration", + &format! {"unix:{}", migration_socket}, + ]) + .stderr(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); + // Give it '1s' to make sure the 'migration_socket' file is properly created + thread::sleep(std::time::Duration::new(1, 0)); + // Start to send migration from the source VM + + let mut args = [ + format!("--api-socket={}", &src_api_socket), + "send-migration".to_string(), + format! {"unix:{}", migration_socket}, + ] + .to_vec(); + + if local { + args.insert(2, "--local".to_string()); + } + + let mut send_migration = Command::new(clh_command("ch-remote")) + .args(&args) + .stderr(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); + + // The 'send-migration' command should be executed successfully within the given timeout + let send_success = if let Some(status) = send_migration + .wait_timeout(std::time::Duration::from_secs(30)) + .unwrap() + { + status.success() + } else { + false + }; + + if !send_success { + let _ = send_migration.kill(); + let output = send_migration.wait_with_output().unwrap(); + eprintln!("\n\n==== Start 'send_migration' output ====\n\n---stdout---\n{}\n\n---stderr---\n{}\n\n==== End 'send_migration' output ====\n\n", + String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + } + + // The 'receive-migration' command should be executed successfully within the given timeout + let receive_success = if let Some(status) = receive_migration + .wait_timeout(std::time::Duration::from_secs(30)) + .unwrap() + { + status.success() + } else { + false + }; + + if !receive_success { + let _ = receive_migration.kill(); + let output = receive_migration.wait_with_output().unwrap(); + eprintln!("\n\n==== Start 'receive_migration' output ====\n\n---stdout---\n{}\n\n---stderr---\n{}\n\n==== End 'receive_migration' output ====\n\n", + String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + } + + send_success && receive_success + } + + fn print_and_panic(src_vm: Child, dest_vm: Child, ovs_vm: Option, message: &str) -> ! { + let mut src_vm = src_vm; + let mut dest_vm = dest_vm; + + let _ = src_vm.kill(); + let src_output = src_vm.wait_with_output().unwrap(); + eprintln!( + "\n\n==== Start 'source_vm' stdout ====\n\n{}\n\n==== End 'source_vm' stdout ====", + String::from_utf8_lossy(&src_output.stdout) + ); + eprintln!( + "\n\n==== Start 'source_vm' stderr ====\n\n{}\n\n==== End 'source_vm' stderr ====", + String::from_utf8_lossy(&src_output.stderr) + ); + let _ = dest_vm.kill(); + let dest_output = dest_vm.wait_with_output().unwrap(); + eprintln!( + "\n\n==== Start 'destination_vm' stdout ====\n\n{}\n\n==== End 'destination_vm' stdout ====", + String::from_utf8_lossy(&dest_output.stdout) + ); + eprintln!( + "\n\n==== Start 'destination_vm' stderr ====\n\n{}\n\n==== End 'destination_vm' stderr ====", + String::from_utf8_lossy(&dest_output.stderr) + ); + + if let Some(ovs_vm) = ovs_vm { + let mut ovs_vm = ovs_vm; + let _ = ovs_vm.kill(); + let ovs_output = ovs_vm.wait_with_output().unwrap(); + eprintln!( + "\n\n==== Start 'ovs_vm' stdout ====\n\n{}\n\n==== End 'ovs_vm' stdout ====", + String::from_utf8_lossy(&ovs_output.stdout) + ); + eprintln!( + "\n\n==== Start 'ovs_vm' stderr ====\n\n{}\n\n==== End 'ovs_vm' stderr ====", + String::from_utf8_lossy(&ovs_output.stderr) + ); + + cleanup_ovs_dpdk(); + } + + panic!("Test failed: {}", message) + } + + // This test exercises the local live-migration between two Cloud Hypervisor VMs on the + // same host. It ensures the following behaviors: + // 1. The source VM is up and functional (including various virtio-devices are working properly); + // 2. The 'send-migration' and 'receive-migration' command finished successfully; + // 3. The source VM terminated gracefully after live migration; + // 4. The destination VM is functional (including various virtio-devices are working properly) after + // live migration; + // Note: This test does not use vsock as we can't create two identical vsock on the same host. + fn _test_live_migration(upgrade_test: bool, local: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + let console_text = String::from("On a branch floating down river a cricket, singing."); + let net_id = "net123"; + let net_params = format!( + "id={},tap=,mac={},ip={},mask=255.255.255.0", + net_id, guest.network.guest_mac, guest.network.host_ip + ); + + let memory_param: &[&str] = if local { + &["--memory", "size=4G,shared=on"] + } else { + &["--memory", "size=4G"] + }; + + let boot_vcpus = 2; + let max_vcpus = 4; + + let pmem_temp_file = TempFile::new().unwrap(); + pmem_temp_file.as_file().set_len(128 << 20).unwrap(); + std::process::Command::new("mkfs.ext4") + .arg(pmem_temp_file.as_path()) + .output() + .expect("Expect creating disk image to succeed"); + let pmem_path = String::from("/dev/pmem0"); + + // Start the source VM + let src_vm_path = if !upgrade_test { + clh_command("cube-hypervisor") + } else { + cloud_hypervisor_release_path() + }; + let src_api_socket = temp_api_path(&guest.tmp_dir); + let mut src_vm_cmd = GuestCommand::new_with_binary_path(&guest, &src_vm_path); + src_vm_cmd + .args([ + "--cpus", + format!("boot={},max={}", boot_vcpus, max_vcpus).as_str(), + ]) + .args(memory_param) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", net_params.as_str()]) + .args(["--api-socket", &src_api_socket]) + .args([ + "--pmem", + format!("file={}", pmem_temp_file.as_path().to_str().unwrap(),).as_str(), + ]); + let mut src_child = src_vm_cmd.capture_output().spawn().unwrap(); + + // Start the destination VM + let mut dest_api_socket = temp_api_path(&guest.tmp_dir); + dest_api_socket.push_str(".dest"); + let mut dest_child = GuestCommand::new(&guest) + .args(["--api-socket", &dest_api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Make sure the source VM is functaionl + // Check the number of vCPUs + assert_eq!(guest.get_cpu_count().unwrap_or_default(), boot_vcpus); + + // Check the guest RAM + assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000); + + // Check the guest virtio-devices, e.g. block, rng, console, and net + guest.check_devices_common(None, Some(&console_text), Some(&pmem_path)); + + // x86_64: Following what's done in the `test_snapshot_restore`, we need + // to make sure that removing and adding back the virtio-net device does + // not break the live-migration support for virtio-pci. + #[cfg(target_arch = "x86_64")] + { + assert!(remote_command( + &src_api_socket, + "remove-device", + Some(net_id), + )); + thread::sleep(std::time::Duration::new(10, 0)); + + // Plug the virtio-net device again + assert!(remote_command( + &src_api_socket, + "add-net", + Some(net_params.as_str()), + )); + thread::sleep(std::time::Duration::new(10, 0)); + } + + // Start the live-migration + let migration_socket = String::from( + guest + .tmp_dir + .as_path() + .join("live-migration.sock") + .to_str() + .unwrap(), + ); + + assert!( + start_live_migration(&migration_socket, &src_api_socket, &dest_api_socket, local), + "Unsuccessful command: 'send-migration' or 'receive-migration'." + ); + }); + + // Check and report any errors occured during the live-migration + if r.is_err() { + print_and_panic( + src_child, + dest_child, + None, + "Error occured during live-migration", + ); + } + + // Check the source vm has been terminated successful (give it '3s' to settle) + thread::sleep(std::time::Duration::new(3, 0)); + if !src_child.try_wait().unwrap().map_or(false, |s| s.success()) { + print_and_panic( + src_child, + dest_child, + None, + "source VM was not terminated successfully.", + ); + }; + + // Post live-migration check to make sure the destination VM is funcational + let r = std::panic::catch_unwind(|| { + // Perform same checks to validate VM has been properly migrated + assert_eq!(guest.get_cpu_count().unwrap_or_default(), boot_vcpus); + assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000); + + guest.check_devices_common(None, Some(&console_text), Some(&pmem_path)); + }); + + // Clean-up the destination VM and make sure it terminated correctly + let _ = dest_child.kill(); + let dest_output = dest_child.wait_with_output().unwrap(); + handle_child_output(r, &dest_output); + + // Check the destination VM has the expected 'concole_text' from its output + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&dest_output.stdout).contains(&console_text)); + }); + handle_child_output(r, &dest_output); + } + + fn _test_live_migration_balloon(upgrade_test: bool, local: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + let console_text = String::from("On a branch floating down river a cricket, singing."); + let net_id = "net123"; + let net_params = format!( + "id={},tap=,mac={},ip={},mask=255.255.255.0", + net_id, guest.network.guest_mac, guest.network.host_ip + ); + + let memory_param: &[&str] = if local { + &[ + "--memory", + "size=4G,hotplug_method=virtio-mem,hotplug_size=8G,shared=on", + "--balloon", + "size=0", + ] + } else { + &[ + "--memory", + "size=4G,hotplug_method=virtio-mem,hotplug_size=8G", + "--balloon", + "size=0", + ] + }; + + let boot_vcpus = 2; + let max_vcpus = 4; + + let pmem_temp_file = TempFile::new().unwrap(); + pmem_temp_file.as_file().set_len(128 << 20).unwrap(); + std::process::Command::new("mkfs.ext4") + .arg(pmem_temp_file.as_path()) + .output() + .expect("Expect creating disk image to succeed"); + let pmem_path = String::from("/dev/pmem0"); + + // Start the source VM + let src_vm_path = if !upgrade_test { + clh_command("cube-hypervisor") + } else { + cloud_hypervisor_release_path() + }; + let src_api_socket = temp_api_path(&guest.tmp_dir); + let mut src_vm_cmd = GuestCommand::new_with_binary_path(&guest, &src_vm_path); + src_vm_cmd + .args([ + "--cpus", + format!("boot={},max={}", boot_vcpus, max_vcpus).as_str(), + ]) + .args(memory_param) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", net_params.as_str()]) + .args(["--api-socket", &src_api_socket]) + .args([ + "--pmem", + format!("file={}", pmem_temp_file.as_path().to_str().unwrap(),).as_str(), + ]); + let mut src_child = src_vm_cmd.capture_output().spawn().unwrap(); + + // Start the destination VM + let mut dest_api_socket = temp_api_path(&guest.tmp_dir); + dest_api_socket.push_str(".dest"); + let mut dest_child = GuestCommand::new(&guest) + .args(["--api-socket", &dest_api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Make sure the source VM is functaionl + // Check the number of vCPUs + assert_eq!(guest.get_cpu_count().unwrap_or_default(), boot_vcpus); + + // Check the guest RAM + assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000); + // Increase the guest RAM + resize_command(&src_api_socket, None, Some(6 << 30), None, None); + thread::sleep(std::time::Duration::new(5, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 5_760_000); + // Use balloon to remove RAM from the VM + resize_command(&src_api_socket, None, None, Some(1 << 30), None); + thread::sleep(std::time::Duration::new(5, 0)); + let total_memory = guest.get_total_memory().unwrap_or_default(); + assert!(total_memory > 4_800_000); + assert!(total_memory < 5_760_000); + + // Check the guest virtio-devices, e.g. block, rng, console, and net + guest.check_devices_common(None, Some(&console_text), Some(&pmem_path)); + + // x86_64: Following what's done in the `test_snapshot_restore`, we need + // to make sure that removing and adding back the virtio-net device does + // not break the live-migration support for virtio-pci. + #[cfg(target_arch = "x86_64")] + { + assert!(remote_command( + &src_api_socket, + "remove-device", + Some(net_id), + )); + thread::sleep(std::time::Duration::new(10, 0)); + + // Plug the virtio-net device again + assert!(remote_command( + &src_api_socket, + "add-net", + Some(net_params.as_str()), + )); + thread::sleep(std::time::Duration::new(10, 0)); + } + + // Start the live-migration + let migration_socket = String::from( + guest + .tmp_dir + .as_path() + .join("live-migration.sock") + .to_str() + .unwrap(), + ); + + assert!( + start_live_migration(&migration_socket, &src_api_socket, &dest_api_socket, local), + "Unsuccessful command: 'send-migration' or 'receive-migration'." + ); + }); + + // Check and report any errors occured during the live-migration + if r.is_err() { + print_and_panic( + src_child, + dest_child, + None, + "Error occured during live-migration", + ); + } + + // Check the source vm has been terminated successful (give it '3s' to settle) + thread::sleep(std::time::Duration::new(3, 0)); + if !src_child.try_wait().unwrap().map_or(false, |s| s.success()) { + print_and_panic( + src_child, + dest_child, + None, + "source VM was not terminated successfully.", + ); + }; + + // Post live-migration check to make sure the destination VM is funcational + let r = std::panic::catch_unwind(|| { + // Perform same checks to validate VM has been properly migrated + assert_eq!(guest.get_cpu_count().unwrap_or_default(), boot_vcpus); + assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000); + + guest.check_devices_common(None, Some(&console_text), Some(&pmem_path)); + + // Perform checks on guest RAM using balloon + let total_memory = guest.get_total_memory().unwrap_or_default(); + assert!(total_memory > 4_800_000); + assert!(total_memory < 5_760_000); + // Deflate balloon to restore entire RAM to the VM + resize_command(&dest_api_socket, None, None, Some(0), None); + thread::sleep(std::time::Duration::new(5, 0)); + assert!(guest.get_total_memory().unwrap_or_default() > 5_760_000); + // Decrease guest RAM with virtio-mem + resize_command(&dest_api_socket, None, Some(5 << 30), None, None); + thread::sleep(std::time::Duration::new(5, 0)); + let total_memory = guest.get_total_memory().unwrap_or_default(); + assert!(total_memory > 4_800_000); + assert!(total_memory < 5_760_000); + }); + + // Clean-up the destination VM and make sure it terminated correctly + let _ = dest_child.kill(); + let dest_output = dest_child.wait_with_output().unwrap(); + handle_child_output(r, &dest_output); + + // Check the destination VM has the expected 'concole_text' from its output + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&dest_output.stdout).contains(&console_text)); + }); + handle_child_output(r, &dest_output); + } + + fn _test_live_migration_numa(upgrade_test: bool, local: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + let console_text = String::from("On a branch floating down river a cricket, singing."); + let net_id = "net123"; + let net_params = format!( + "id={},tap=,mac={},ip={},mask=255.255.255.0", + net_id, guest.network.guest_mac, guest.network.host_ip + ); + + let memory_param: &[&str] = if local { + &[ + "--memory", + "size=0,hotplug_method=virtio-mem,shared=on", + "--memory-zone", + "id=mem0,size=1G,hotplug_size=4G,shared=on", + "id=mem1,size=1G,hotplug_size=4G,shared=on", + "id=mem2,size=2G,hotplug_size=4G,shared=on", + "--numa", + "guest_numa_id=0,cpus=[0-2,9],distances=[1@15,2@20],memory_zones=mem0", + "guest_numa_id=1,cpus=[3-4,6-8],distances=[0@20,2@25],memory_zones=mem1", + "guest_numa_id=2,cpus=[5,10-11],distances=[0@25,1@30],memory_zones=mem2", + ] + } else { + &[ + "--memory", + "size=0,hotplug_method=virtio-mem", + "--memory-zone", + "id=mem0,size=1G,hotplug_size=4G", + "id=mem1,size=1G,hotplug_size=4G", + "id=mem2,size=2G,hotplug_size=4G", + "--numa", + "guest_numa_id=0,cpus=[0-2,9],distances=[1@15,2@20],memory_zones=mem0", + "guest_numa_id=1,cpus=[3-4,6-8],distances=[0@20,2@25],memory_zones=mem1", + "guest_numa_id=2,cpus=[5,10-11],distances=[0@25,1@30],memory_zones=mem2", + ] + }; + + let boot_vcpus = 6; + let max_vcpus = 12; + + let pmem_temp_file = TempFile::new().unwrap(); + pmem_temp_file.as_file().set_len(128 << 20).unwrap(); + std::process::Command::new("mkfs.ext4") + .arg(pmem_temp_file.as_path()) + .output() + .expect("Expect creating disk image to succeed"); + let pmem_path = String::from("/dev/pmem0"); + + // Start the source VM + let src_vm_path = if !upgrade_test { + clh_command("cube-hypervisor") + } else { + cloud_hypervisor_release_path() + }; + let src_api_socket = temp_api_path(&guest.tmp_dir); + let mut src_vm_cmd = GuestCommand::new_with_binary_path(&guest, &src_vm_path); + src_vm_cmd + .args([ + "--cpus", + format!("boot={},max={}", boot_vcpus, max_vcpus).as_str(), + ]) + .args(memory_param) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", net_params.as_str()]) + .args(["--api-socket", &src_api_socket]) + .args([ + "--pmem", + format!("file={}", pmem_temp_file.as_path().to_str().unwrap(),).as_str(), + ]); + let mut src_child = src_vm_cmd.capture_output().spawn().unwrap(); + + // Start the destination VM + let mut dest_api_socket = temp_api_path(&guest.tmp_dir); + dest_api_socket.push_str(".dest"); + let mut dest_child = GuestCommand::new(&guest) + .args(["--api-socket", &dest_api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Make sure the source VM is functaionl + // Check the number of vCPUs + assert_eq!(guest.get_cpu_count().unwrap_or_default(), boot_vcpus); + + // Check the guest RAM + assert!(guest.get_total_memory().unwrap_or_default() > 2_880_000); + + // Check the guest virtio-devices, e.g. block, rng, console, and net + guest.check_devices_common(None, Some(&console_text), Some(&pmem_path)); + + // Check the NUMA parameters are applied correctly and resize + // each zone to test the case where we migrate a VM with the + // virtio-mem regions being used. + { + guest.check_numa_common( + Some(&[960_000, 960_000, 1_920_000]), + Some(&[vec![0, 1, 2], vec![3, 4], vec![5]]), + Some(&["10 15 20", "20 10 25", "25 30 10"]), + ); + + // AArch64 currently does not support hotplug, and therefore we only + // test hotplug-related function on x86_64 here. + #[cfg(target_arch = "x86_64")] + { + guest.enable_memory_hotplug(); + + // Resize every memory zone and check each associated NUMA node + // has been assigned the right amount of memory. + resize_zone_command(&src_api_socket, "mem0", "2G"); + resize_zone_command(&src_api_socket, "mem1", "2G"); + resize_zone_command(&src_api_socket, "mem2", "3G"); + thread::sleep(std::time::Duration::new(5, 0)); + + guest.check_numa_common(Some(&[1_920_000, 1_920_000, 1_920_000]), None, None); + } + } + + // x86_64: Following what's done in the `test_snapshot_restore`, we need + // to make sure that removing and adding back the virtio-net device does + // not break the live-migration support for virtio-pci. + #[cfg(target_arch = "x86_64")] + { + assert!(remote_command( + &src_api_socket, + "remove-device", + Some(net_id), + )); + thread::sleep(std::time::Duration::new(10, 0)); + + // Plug the virtio-net device again + assert!(remote_command( + &src_api_socket, + "add-net", + Some(net_params.as_str()), + )); + thread::sleep(std::time::Duration::new(10, 0)); + } + + // Start the live-migration + let migration_socket = String::from( + guest + .tmp_dir + .as_path() + .join("live-migration.sock") + .to_str() + .unwrap(), + ); + + assert!( + start_live_migration(&migration_socket, &src_api_socket, &dest_api_socket, local), + "Unsuccessful command: 'send-migration' or 'receive-migration'." + ); + }); + + // Check and report any errors occured during the live-migration + if r.is_err() { + print_and_panic( + src_child, + dest_child, + None, + "Error occured during live-migration", + ); + } + + // Check the source vm has been terminated successful (give it '3s' to settle) + thread::sleep(std::time::Duration::new(3, 0)); + if !src_child.try_wait().unwrap().map_or(false, |s| s.success()) { + print_and_panic( + src_child, + dest_child, + None, + "source VM was not terminated successfully.", + ); + }; + + // Post live-migration check to make sure the destination VM is funcational + let r = std::panic::catch_unwind(|| { + // Perform same checks to validate VM has been properly migrated + assert_eq!(guest.get_cpu_count().unwrap_or_default(), boot_vcpus); + #[cfg(target_arch = "x86_64")] + assert!(guest.get_total_memory().unwrap_or_default() > 6_720_000); + #[cfg(target_arch = "aarch64")] + assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000); + + guest.check_devices_common(None, Some(&console_text), Some(&pmem_path)); + + // Perform NUMA related checks + { + #[cfg(target_arch = "aarch64")] + { + guest.check_numa_common( + Some(&[960_000, 960_000, 1_920_000]), + Some(&[vec![0, 1, 2], vec![3, 4], vec![5]]), + Some(&["10 15 20", "20 10 25", "25 30 10"]), + ); + } + + // AArch64 currently does not support hotplug, and therefore we only + // test hotplug-related function on x86_64 here. + #[cfg(target_arch = "x86_64")] + { + guest.check_numa_common( + Some(&[1_920_000, 1_920_000, 2_880_000]), + Some(&[vec![0, 1, 2], vec![3, 4], vec![5]]), + Some(&["10 15 20", "20 10 25", "25 30 10"]), + ); + + guest.enable_memory_hotplug(); + + // Resize every memory zone and check each associated NUMA node + // has been assigned the right amount of memory. + resize_zone_command(&dest_api_socket, "mem0", "4G"); + resize_zone_command(&dest_api_socket, "mem1", "4G"); + resize_zone_command(&dest_api_socket, "mem2", "4G"); + // Resize to the maximum amount of CPUs and check each NUMA + // node has been assigned the right CPUs set. + resize_command(&dest_api_socket, Some(max_vcpus), None, None, None); + thread::sleep(std::time::Duration::new(5, 0)); + + guest.check_numa_common( + Some(&[3_840_000, 3_840_000, 3_840_000]), + Some(&[vec![0, 1, 2, 9], vec![3, 4, 6, 7, 8], vec![5, 10, 11]]), + None, + ); + } + } + }); + + // Clean-up the destination VM and make sure it terminated correctly + let _ = dest_child.kill(); + let dest_output = dest_child.wait_with_output().unwrap(); + handle_child_output(r, &dest_output); + + // Check the destination VM has the expected 'concole_text' from its output + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&dest_output.stdout).contains(&console_text)); + }); + handle_child_output(r, &dest_output); + } + + fn _test_live_migration_watchdog(upgrade_test: bool, local: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let kernel_path = direct_kernel_boot_path(); + let console_text = String::from("On a branch floating down river a cricket, singing."); + let net_id = "net123"; + let net_params = format!( + "id={},tap=,mac={},ip={},mask=255.255.255.0", + net_id, guest.network.guest_mac, guest.network.host_ip + ); + + let memory_param: &[&str] = if local { + &["--memory", "size=4G,shared=on"] + } else { + &["--memory", "size=4G"] + }; + + let boot_vcpus = 2; + let max_vcpus = 4; + + let pmem_temp_file = TempFile::new().unwrap(); + pmem_temp_file.as_file().set_len(128 << 20).unwrap(); + std::process::Command::new("mkfs.ext4") + .arg(pmem_temp_file.as_path()) + .output() + .expect("Expect creating disk image to succeed"); + let pmem_path = String::from("/dev/pmem0"); + + // Start the source VM + let src_vm_path = if !upgrade_test { + clh_command("cube-hypervisor") + } else { + cloud_hypervisor_release_path() + }; + let src_api_socket = temp_api_path(&guest.tmp_dir); + let mut src_vm_cmd = GuestCommand::new_with_binary_path(&guest, &src_vm_path); + src_vm_cmd + .args([ + "--cpus", + format!("boot={},max={}", boot_vcpus, max_vcpus).as_str(), + ]) + .args(memory_param) + .args(["--kernel", kernel_path.to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", net_params.as_str()]) + .args(["--api-socket", &src_api_socket]) + .args([ + "--pmem", + format!("file={}", pmem_temp_file.as_path().to_str().unwrap(),).as_str(), + ]) + .args(["--watchdog"]); + let mut src_child = src_vm_cmd.capture_output().spawn().unwrap(); + + // Start the destination VM + let mut dest_api_socket = temp_api_path(&guest.tmp_dir); + dest_api_socket.push_str(".dest"); + let mut dest_child = GuestCommand::new(&guest) + .args(["--api-socket", &dest_api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + // Make sure the source VM is functaionl + // Check the number of vCPUs + assert_eq!(guest.get_cpu_count().unwrap_or_default(), boot_vcpus); + // Check the guest RAM + assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000); + // Check the guest virtio-devices, e.g. block, rng, console, and net + guest.check_devices_common(None, Some(&console_text), Some(&pmem_path)); + // x86_64: Following what's done in the `test_snapshot_restore`, we need + // to make sure that removing and adding back the virtio-net device does + // not break the live-migration support for virtio-pci. + #[cfg(target_arch = "x86_64")] + { + assert!(remote_command( + &src_api_socket, + "remove-device", + Some(net_id), + )); + thread::sleep(std::time::Duration::new(10, 0)); + + // Plug the virtio-net device again + assert!(remote_command( + &src_api_socket, + "add-net", + Some(net_params.as_str()), + )); + thread::sleep(std::time::Duration::new(10, 0)); + } + + // Enable watchdog and ensure its functional + let mut expected_reboot_count = 1; + // Enable the watchdog with a 15s timeout + enable_guest_watchdog(&guest, 15); + // Reboot and check that systemd has activated the watchdog + guest.ssh_command("sudo reboot").unwrap(); + guest.wait_vm_boot(None).unwrap(); + expected_reboot_count += 1; + assert_eq!(get_reboot_count(&guest), expected_reboot_count); + assert_eq!( + guest + .ssh_command("sudo journalctl | grep -c -- \"Watchdog started\"") + .unwrap() + .trim() + .parse::() + .unwrap_or_default(), + 2 + ); + // Allow some normal time to elapse to check we don't get spurious reboots + thread::sleep(std::time::Duration::new(40, 0)); + // Check no reboot + assert_eq!(get_reboot_count(&guest), expected_reboot_count); + + // Start the live-migration + let migration_socket = String::from( + guest + .tmp_dir + .as_path() + .join("live-migration.sock") + .to_str() + .unwrap(), + ); + + assert!( + start_live_migration(&migration_socket, &src_api_socket, &dest_api_socket, local), + "Unsuccessful command: 'send-migration' or 'receive-migration'." + ); + }); + + // Check and report any errors occured during the live-migration + if r.is_err() { + print_and_panic( + src_child, + dest_child, + None, + "Error occured during live-migration", + ); + } + + // Check the source vm has been terminated successful (give it '3s' to settle) + thread::sleep(std::time::Duration::new(3, 0)); + if !src_child.try_wait().unwrap().map_or(false, |s| s.success()) { + print_and_panic( + src_child, + dest_child, + None, + "source VM was not terminated successfully.", + ); + }; + + // Post live-migration check to make sure the destination VM is funcational + let r = std::panic::catch_unwind(|| { + // Perform same checks to validate VM has been properly migrated + assert_eq!(guest.get_cpu_count().unwrap_or_default(), boot_vcpus); + assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000); + + guest.check_devices_common(None, Some(&console_text), Some(&pmem_path)); + + // Perform checks on watchdog + let mut expected_reboot_count = 2; + + // Allow some normal time to elapse to check we don't get spurious reboots + thread::sleep(std::time::Duration::new(40, 0)); + // Check no reboot + assert_eq!(get_reboot_count(&guest), expected_reboot_count); + + // Trigger a panic (sync first). We need to do this inside a screen with a delay so the SSH command returns. + guest.ssh_command("screen -dmS reboot sh -c \"sleep 5; echo s | tee /proc/sysrq-trigger; echo c | sudo tee /proc/sysrq-trigger\"").unwrap(); + // Allow some time for the watchdog to trigger (max 30s) and reboot to happen + guest.wait_vm_boot(Some(50)).unwrap(); + // Check a reboot is triggerred by the watchdog + expected_reboot_count += 1; + assert_eq!(get_reboot_count(&guest), expected_reboot_count); + + #[cfg(target_arch = "x86_64")] + { + // Now pause the VM and remain offline for 30s + assert!(remote_command(&dest_api_socket, "pause", None)); + thread::sleep(std::time::Duration::new(30, 0)); + assert!(remote_command(&dest_api_socket, "resume", None)); + + // Check no reboot + assert_eq!(get_reboot_count(&guest), expected_reboot_count); + } + }); + + // Clean-up the destination VM and make sure it terminated correctly + let _ = dest_child.kill(); + let dest_output = dest_child.wait_with_output().unwrap(); + handle_child_output(r, &dest_output); + + // Check the destination VM has the expected 'concole_text' from its output + let r = std::panic::catch_unwind(|| { + assert!(String::from_utf8_lossy(&dest_output.stdout).contains(&console_text)); + }); + handle_child_output(r, &dest_output); + } + + fn _test_live_migration_ovs_dpdk(upgrade_test: bool, local: bool) { + let ovs_focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let ovs_guest = Guest::new(Box::new(ovs_focal)); + + let migration_focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let migration_guest = Guest::new(Box::new(migration_focal)); + let src_api_socket = temp_api_path(&migration_guest.tmp_dir); + + // Start two VMs that are connected through ovs-dpdk and one of the VMs is the source VM for live-migration + let (mut ovs_child, mut src_child) = + setup_ovs_dpdk_guests(&ovs_guest, &migration_guest, &src_api_socket, upgrade_test); + + // Start the destination VM + let mut dest_api_socket = temp_api_path(&migration_guest.tmp_dir); + dest_api_socket.push_str(".dest"); + let mut dest_child = GuestCommand::new(&migration_guest) + .args(["--api-socket", &dest_api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + // Give it '1s' to make sure the 'dest_api_socket' file is properly created + thread::sleep(std::time::Duration::new(1, 0)); + + // Start the live-migration + let migration_socket = String::from( + migration_guest + .tmp_dir + .as_path() + .join("live-migration.sock") + .to_str() + .unwrap(), + ); + + assert!( + start_live_migration(&migration_socket, &src_api_socket, &dest_api_socket, local), + "Unsuccessful command: 'send-migration' or 'receive-migration'." + ); + }); + + // Check and report any errors occured during the live-migration + if r.is_err() { + print_and_panic( + src_child, + dest_child, + Some(ovs_child), + "Error occured during live-migration", + ); + } + + // Check the source vm has been terminated successful (give it '3s' to settle) + thread::sleep(std::time::Duration::new(3, 0)); + if !src_child.try_wait().unwrap().map_or(false, |s| s.success()) { + print_and_panic( + src_child, + dest_child, + Some(ovs_child), + "source VM was not terminated successfully.", + ); + }; + + // Post live-migration check to make sure the destination VM is funcational + let r = std::panic::catch_unwind(|| { + // Perform same checks to validate VM has been properly migrated + // Spawn a new netcat listener in the OVS VM + let guest_ip = ovs_guest.network.guest_ip.clone(); + thread::spawn(move || { + ssh_command_ip( + "nc -l 12345", + &guest_ip, + DEFAULT_SSH_RETRIES, + DEFAULT_SSH_TIMEOUT, + ) + .unwrap(); + }); + + // Wait for the server to be listening + thread::sleep(std::time::Duration::new(5, 0)); + + // And check the connection is still functional after live-migration + migration_guest + .ssh_command("nc -vz 172.100.0.1 12345") + .unwrap(); + }); + + // Clean-up the destination VM and OVS VM, and make sure they terminated correctly + let _ = dest_child.kill(); + let _ = ovs_child.kill(); + let dest_output = dest_child.wait_with_output().unwrap(); + let ovs_output = ovs_child.wait_with_output().unwrap(); + + cleanup_ovs_dpdk(); + + handle_child_output(r, &dest_output); + handle_child_output(Ok(()), &ovs_output); + } + + mod live_migration_parallel { + use super::*; + #[test] + fn test_live_migration_basic() { + _test_live_migration(false, false) + } + + #[test] + fn test_live_migration_local() { + _test_live_migration(false, true) + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_live_migration_numa() { + _test_live_migration_numa(false, false) + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_live_migration_numa_local() { + _test_live_migration_numa(false, true) + } + + #[test] + #[ignore = "PVM guest kernel has no virtio-watchdog driver (CONFIG_VIRTIO_WDT not enabled)"] + fn test_live_migration_watchdog() { + _test_live_migration_watchdog(false, false) + } + + #[test] + #[ignore = "PVM guest kernel has no virtio-watchdog driver (CONFIG_VIRTIO_WDT not enabled)"] + fn test_live_migration_watchdog_local() { + _test_live_migration_watchdog(false, true) + } + + #[test] + fn test_live_migration_balloon() { + _test_live_migration_balloon(false, false) + } + + #[test] + fn test_live_migration_balloon_local() { + _test_live_migration_balloon(false, true) + } + + #[test] + fn test_live_upgrade_basic() { + _test_live_migration(true, false) + } + + #[test] + fn test_live_upgrade_local() { + _test_live_migration(true, true) + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_live_upgrade_numa() { + _test_live_migration_numa(true, false) + } + + #[test] + #[cfg(not(feature = "mshv"))] + fn test_live_upgrade_numa_local() { + _test_live_migration_numa(true, true) + } + + #[test] + #[ignore = "PVM guest kernel has no virtio-watchdog driver (CONFIG_VIRTIO_WDT not enabled)"] + fn test_live_upgrade_watchdog() { + _test_live_migration_watchdog(true, false) + } + + #[test] + #[ignore = "PVM guest kernel has no virtio-watchdog driver (CONFIG_VIRTIO_WDT not enabled)"] + fn test_live_upgrade_watchdog_local() { + _test_live_migration_watchdog(true, true) + } + + #[test] + fn test_live_upgrade_balloon() { + _test_live_migration_balloon(true, false) + } + + #[test] + fn test_live_upgrade_balloon_local() { + _test_live_migration_balloon(true, true) + } + } + + mod live_migration_sequential { + #[cfg(target_arch = "x86_64")] + #[cfg(not(feature = "mshv"))] + use super::*; + + // Require to run ovs-dpdk tests sequentially because they rely on the same ovs-dpdk setup + #[test] + #[cfg(target_arch = "x86_64")] + #[cfg(not(feature = "mshv"))] + #[ignore = "PVM guest kernel has CONFIG_OPENVSWITCH disabled (aligned with intranet cube skip)"] + fn test_live_migration_ovs_dpdk() { + _test_live_migration_ovs_dpdk(false, false); + } + + #[test] + #[cfg(target_arch = "x86_64")] + #[cfg(not(feature = "mshv"))] + #[ignore = "PVM guest kernel has CONFIG_OPENVSWITCH disabled (aligned with intranet cube skip)"] + fn test_live_migration_ovs_dpdk_local() { + _test_live_migration_ovs_dpdk(false, true); + } + + #[test] + #[cfg(target_arch = "x86_64")] + #[cfg(not(feature = "mshv"))] + #[ignore = "PVM guest kernel has CONFIG_OPENVSWITCH disabled (aligned with intranet cube skip)"] + fn test_live_upgrade_ovs_dpdk() { + _test_live_migration_ovs_dpdk(true, false); + } + + #[test] + #[cfg(target_arch = "x86_64")] + #[cfg(not(feature = "mshv"))] + #[ignore = "PVM guest kernel has CONFIG_OPENVSWITCH disabled (aligned with intranet cube skip)"] + fn test_live_upgrade_ovs_dpdk_local() { + _test_live_migration_ovs_dpdk(true, true); + } + } +} + +#[cfg(target_arch = "aarch64")] +mod aarch64_acpi { + use crate::*; + + #[test] + fn test_simple_launch_acpi() { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + + vec![Box::new(focal)].drain(..).for_each(|disk_config| { + let guest = Guest::new(disk_config); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", "boot=1"]) + .args(["--memory", "size=512M"]) + .args(["--kernel", edk2_path().to_str().unwrap()]) + .default_disks() + .default_net() + .args(["--serial", "tty", "--console", "off"]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(Some(120)).unwrap(); + + assert_eq!(guest.get_cpu_count().unwrap_or_default(), 1); + assert!(guest.get_total_memory().unwrap_or_default() > 400_000); + assert_eq!(guest.get_pci_bridge_class().unwrap_or_default(), "0x060000"); + }); + + let _ = child.kill(); + let output = child.wait_with_output().unwrap(); + + handle_child_output(r, &output); + }); + } + + #[test] + fn test_guest_numa_nodes_acpi() { + _test_guest_numa_nodes(true); + } + + #[test] + fn test_cpu_topology_421_acpi() { + test_cpu_topology(4, 2, 1, true); + } + + #[test] + fn test_cpu_topology_142_acpi() { + test_cpu_topology(1, 4, 2, true); + } + + #[test] + fn test_cpu_topology_262_acpi() { + test_cpu_topology(2, 6, 2, true); + } + + #[test] + fn test_power_button_acpi() { + _test_power_button(true); + } + + #[test] + fn test_virtio_iommu() { + _test_virtio_iommu(true) + } +} + +mod rate_limiter { + use super::*; + + // Check if the 'measured' rate is within the expected 'difference' (in percentage) + // compared to given 'limit' rate. + fn check_rate_limit(measured: f64, limit: f64, difference: f64) -> bool { + let upper_limit = limit * (1_f64 + difference); + let lower_limit = limit * (1_f64 - difference); + + if measured > lower_limit && measured < upper_limit { + return true; + } + + eprintln!( + "\n\n==== check_rate_limit failed! ====\n\nmeasured={}, , lower_limit={}, upper_limit={}\n\n", + measured, lower_limit, upper_limit + ); + + false + } + + fn _test_rate_limiter_net(rx: bool) { + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + + let test_timeout = 10; + let num_queues = 2; + let queue_size = 256; + let bw_size = 10485760_u64; // bytes + let bw_refill_time = 100; // ms + let limit_bps = (bw_size * 8 * 1000) as f64 / bw_refill_time as f64; + + let net_params = format!( + "tap=,mac={},ip={},mask=255.255.255.0,num_queues={},queue_size={},bw_size={},bw_refill_time={}", + guest.network.guest_mac, + guest.network.host_ip, + num_queues, + queue_size, + bw_size, + bw_refill_time, + ); + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", &format!("boot={}", num_queues / 2)]) + .args(["--memory", "size=4G"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .default_disks() + .args(["--net", net_params.as_str()]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + let measured_bps = + measure_virtio_net_throughput(test_timeout, num_queues / 2, &guest, rx).unwrap(); + assert!(check_rate_limit(measured_bps, limit_bps, 0.1)); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + } + + #[test] + fn test_rate_limiter_net_rx() { + _test_rate_limiter_net(true); + } + + #[test] + fn test_rate_limiter_net_tx() { + _test_rate_limiter_net(false); + } + + fn _test_rate_limiter_block(bandwidth: bool) { + let test_timeout = 10; + let num_queues = 1; + let fio_ops = FioOps::RandRW; + + let bw_size = if bandwidth { + 10485760_u64 // bytes + } else { + 100_u64 // I/O + }; + let bw_refill_time = 100; // ms + let limit_rate = (bw_size * 1000) as f64 / bw_refill_time as f64; + + let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); + let guest = Guest::new(Box::new(focal)); + let api_socket = temp_api_path(&guest.tmp_dir); + let test_img_dir = TempDir::new_with_prefix("/var/tmp/ch").unwrap(); + let blk_rate_limiter_test_img = + String::from(test_img_dir.as_path().join("blk.img").to_str().unwrap()); + + // Create the test block image + assert!(exec_host_command_output(&format!( + "dd if=/dev/zero of={} bs=1M count=1024", + blk_rate_limiter_test_img + )) + .status + .success()); + + let test_blk_params = if bandwidth { + format!( + "path={},bw_size={},bw_refill_time={}", + blk_rate_limiter_test_img, bw_size, bw_refill_time + ) + } else { + format!( + "path={},ops_size={},ops_refill_time={}", + blk_rate_limiter_test_img, bw_size, bw_refill_time + ) + }; + + let mut child = GuestCommand::new(&guest) + .args(["--cpus", &format!("boot={}", num_queues)]) + .args(["--memory", "size=4G"]) + .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) + .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) + .args([ + "--disk", + format!( + "path={}", + guest.disk_config.disk(DiskType::OperatingSystem).unwrap() + ) + .as_str(), + format!( + "path={}", + guest.disk_config.disk(DiskType::CloudInit).unwrap() + ) + .as_str(), + test_blk_params.as_str(), + ]) + .default_net() + .args(["--api-socket", &api_socket]) + .capture_output() + .spawn() + .unwrap(); + + let r = std::panic::catch_unwind(|| { + guest.wait_vm_boot(None).unwrap(); + + let fio_command = format!( + "sudo fio --filename=/dev/vdc --name=test --output-format=json \ + --direct=1 --bs=4k --ioengine=io_uring --iodepth=64 \ + --rw={} --runtime={} --numjobs={}", + fio_ops, test_timeout, num_queues + ); + let output = guest.ssh_command(&fio_command).unwrap(); + + // Parse fio output + let measured_rate = if bandwidth { + parse_fio_output(&output, &fio_ops, num_queues).unwrap() + } else { + parse_fio_output_iops(&output, &fio_ops, num_queues).unwrap() + }; + assert!(check_rate_limit(measured_rate, limit_rate, 0.1)); + }); + + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + handle_child_output(r, &output); + } + + #[test] + fn test_rate_limiter_block_bandwidth() { + _test_rate_limiter_block(true) + } + + #[test] + fn test_rate_limiter_block_iops() { + _test_rate_limiter_block(false) + } +} diff --git a/vendor/cube-hypervisor/tpm/Cargo.toml b/vendor/cube-hypervisor/tpm/Cargo.toml new file mode 100644 index 0000000..6481d43 --- /dev/null +++ b/vendor/cube-hypervisor/tpm/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "tpm" +edition = "2021" +authors = ["Microsoft Authors"] +license = "Apache-2.0" +version = "0.1.0" + +[dependencies] +anyhow = "1.0.86" +byteorder = "1.4.3" +libc = "0.2.133" +log = "0.4.14" +thiserror = "1.0.30" +vmm-sys-util = "0.12.1" diff --git a/vendor/cube-hypervisor/tpm/src/emulator.rs b/vendor/cube-hypervisor/tpm/src/emulator.rs new file mode 100644 index 0000000..f780b4e --- /dev/null +++ b/vendor/cube-hypervisor/tpm/src/emulator.rs @@ -0,0 +1,461 @@ +// Copyright © 2022, Microsoft Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::socket::SocketDev; +use crate::{Commands, MemberType, Ptm, PtmCap, PtmEst, PtmInit, PtmResult, PtmSetBufferSize}; +use crate::{TPM_CRB_BUFFER_MAX, TPM_SUCCESS}; +use anyhow::anyhow; +use libc::c_void; +use libc::{sockaddr_storage, socklen_t}; +use std::convert::TryInto; +use std::os::unix::io::RawFd; +use std::path::Path; +use std::{mem, ptr}; +use thiserror::Error; + +const TPM_REQ_HDR_SIZE: usize = 10; + +/* capability flags returned by PTM_GET_CAPABILITY */ +const PTM_CAP_INIT: u64 = 1; +const PTM_CAP_SHUTDOWN: u64 = 1 << 1; +const PTM_CAP_GET_TPMESTABLISHED: u64 = 1 << 2; +const PTM_CAP_SET_LOCALITY: u64 = 1 << 3; +const PTM_CAP_CANCEL_TPM_CMD: u64 = 1 << 5; +const PTM_CAP_RESET_TPMESTABLISHED: u64 = 1 << 7; +const PTM_CAP_STOP: u64 = 1 << 10; +const PTM_CAP_SET_DATAFD: u64 = 1 << 12; +const PTM_CAP_SET_BUFFERSIZE: u64 = 1 << 13; + +///Check if the input command is selftest +/// +pub fn is_selftest(input: Vec, in_len: usize) -> bool { + if in_len >= TPM_REQ_HDR_SIZE { + let ordinal: &[u8; 4] = input[6..6 + 4] + .try_into() + .expect("slice with incorrect length"); + + return u32::from_ne_bytes(*ordinal).to_be() == 0x143; + } + false +} + +#[derive(Error, Debug)] +pub enum Error { + #[error("Could not initialize emulator's backend: {0}")] + InitializeEmulator(#[source] anyhow::Error), + #[error("Failed to create data fd to pass to swtpm: {0}")] + PrepareDataFd(#[source] anyhow::Error), + #[error("Failed to run Control Cmd: {0}")] + RunControlCmd(#[source] anyhow::Error), + #[error("Emulator doesn't implement min required capabilities: {0}")] + CheckCaps(#[source] anyhow::Error), + #[error("Emulator failed to deliver request: {0}")] + DeliverRequest(#[source] anyhow::Error), + #[error("Emulator failed to send/receive msg on data fd: {0}")] + SendReceive(#[source] anyhow::Error), + #[error("Incorrect response to Self Test: {0}")] + SelfTest(#[source] anyhow::Error), +} + +type Result = anyhow::Result; + +#[derive(Clone)] +pub struct BackendCmd { + pub locality: u8, + pub input: Vec, + pub input_len: usize, + pub output: Vec, + pub output_len: usize, + pub selftest_done: bool, +} + +pub struct Emulator { + cmd: Option, + caps: PtmCap, /* capabilities of the TPM */ + control_socket: SocketDev, + data_fd: RawFd, + established_flag_cached: bool, + established_flag: bool, +} + +impl Emulator { + /// Create Emulator Instance + /// + /// # Arguments + /// + /// * `path` - A path to the Unix Domain Socket swtpm is listening on + /// + pub fn new(path: String) -> Result { + if !Path::new(&path).exists() { + return Err(Error::InitializeEmulator(anyhow!( + "The input TPM Socket path: {:?} does not exist", + path + ))); + } + let mut socket = SocketDev::new(); + socket.init(path).map_err(|e| { + Error::InitializeEmulator(anyhow!("Failed while initializing tpm emulator: {:?}", e)) + })?; + + let mut emulator = Self { + cmd: None, + caps: 0, + control_socket: socket, + data_fd: -1, + established_flag_cached: false, + established_flag: false, + }; + + emulator.prepare_data_fd()?; + + emulator.probe_caps()?; + if !emulator.check_caps() { + return Err(Error::InitializeEmulator(anyhow!( + "Required capabilities not supported by tpm backend" + ))); + } + + if !emulator.get_established_flag() { + return Err(Error::InitializeEmulator(anyhow!( + "TPM not in established state" + ))); + } + + Ok(emulator) + } + + /// Create socketpair, assign one socket/FD as data_fd to Control Socket + /// The other socket/FD will be assigned to msg_fd, which will be sent to swtpm + /// via CmdSetDatafd control command + fn prepare_data_fd(&mut self) -> Result<()> { + let mut res: PtmResult = 0; + + let mut fds = [-1, -1]; + // Safe because return value of the unsafe call is checked + unsafe { + let ret = libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()); + if ret == -1 { + return Err(Error::PrepareDataFd(anyhow!( + "Failed to prepare data fd for tpm emulator. Error Code {:?}", + std::io::Error::last_os_error() + ))); + } + } + self.control_socket.set_msgfd(fds[1]); + debug!("data fd to be configured in swtpm = {:?}", fds[1]); + self.run_control_cmd(Commands::CmdSetDatafd, &mut res, 0, mem::size_of::())?; + debug!("data fd in cloud-hypervisor = {:?}", fds[0]); + self.data_fd = fds[0]; + self.control_socket.set_datafd(fds[0]); + Ok(()) + } + + /// Gather TPM Capabilities and cache them in Emulator + /// + fn probe_caps(&mut self) -> Result<()> { + let mut caps: u64 = 0; + self.run_control_cmd( + Commands::CmdGetCapability, + &mut caps, + 0, + mem::size_of::(), + )?; + self.caps = caps; + Ok(()) + } + + /// Check if minimum set of capabitlies are supported + fn check_caps(&mut self) -> bool { + /* min. required capabilities for TPM 2.0*/ + let caps: PtmCap = PTM_CAP_INIT + | PTM_CAP_SHUTDOWN + | PTM_CAP_GET_TPMESTABLISHED + | PTM_CAP_SET_LOCALITY + | PTM_CAP_RESET_TPMESTABLISHED + | PTM_CAP_SET_DATAFD + | PTM_CAP_STOP + | PTM_CAP_SET_BUFFERSIZE; + + if (self.caps & caps) != caps { + return false; + } + true + } + + /// + /// # Arguments + /// + /// * `cmd` - Control Command to run + /// * `msg` - Optional msg to be sent along with Control Command + /// * `msg_len_in` - len of 'msg' in bytes, if passed + /// * `msg_len_out` - length of expected output from Control Command in bytes + /// + fn run_control_cmd( + &mut self, + cmd: Commands, + msg: &mut dyn Ptm, + msg_len_in: usize, + msg_len_out: usize, + ) -> Result<()> { + debug!("Control Cmd to send : {:02X?}", cmd); + + let cmd_no = (cmd as u32).to_be_bytes(); + let n: isize = (mem::size_of::() + msg_len_in) as isize; + + let converted_req = msg.ptm_to_request(); + debug!("converted request: {:02X?}", converted_req); + + let mut buf = Vec::::with_capacity(n as usize); + + buf.extend(cmd_no); + buf.extend(converted_req); + debug!("full Control request {:02X?}", buf); + + let _res = self.control_socket.write(&buf).map_err(|e| { + Error::RunControlCmd(anyhow!( + "Failed while running {:02X?} Control Cmd. Error: {:?}", + cmd, + e + )) + })?; + + let mut output = [0_u8; TPM_CRB_BUFFER_MAX]; + + // Every Control Cmd gets atleast a result code in response. Read it + let read_size = self.control_socket.read(&mut output).map_err(|e| { + Error::RunControlCmd(anyhow!( + "Failed while reading response for Control Cmd: {:02X?}. Error: {:?}", + cmd, + e + )) + })?; + + if msg_len_out != 0 { + msg.update_ptm_with_response(&output[0..read_size]) + .map_err(|e| { + Error::RunControlCmd(anyhow!( + "Failed while converting response of Control Cmd: {:02X?} to PTM. Error: {:?}", + cmd, + e + )) + })?; + } else { + // No response expected, only handle return code + msg.set_member_type(MemberType::Response); + } + + if msg.get_result_code() != TPM_SUCCESS { + return Err(Error::RunControlCmd(anyhow!( + "Control Cmd returned error code : {:?}", + msg.get_result_code() + ))); + } + debug!("Control Cmd Response : {:02X?}", &output[0..read_size]); + Ok(()) + } + + pub fn get_established_flag(&mut self) -> bool { + let mut est: PtmEst = PtmEst::new(); + + if self.established_flag_cached { + return self.established_flag; + } + + if let Err(e) = self.run_control_cmd( + Commands::CmdGetTpmEstablished, + &mut est, + 0, + 2 * mem::size_of::(), + ) { + error!( + "Failed to run CmdGetTpmEstablished Control Cmd. Error: {:?}", + e + ); + return false; + } + + self.established_flag_cached = true; + self.established_flag = est.resp.bit == 0; + + self.established_flag + } + + /// Function to write to data socket and read the response from it + fn unix_tx_bufs(&mut self) -> Result<()> { + let isselftest: bool; + // Safe as type "sockaddr_storage" is valid with an all-zero byte-pattern value + let mut addr: sockaddr_storage = unsafe { mem::zeroed() }; + let mut len = mem::size_of::() as socklen_t; + + if let Some(ref mut cmd) = self.cmd { + cmd.selftest_done = false; + isselftest = is_selftest(cmd.input.to_vec(), cmd.input_len); + + debug!( + "Send cmd: {:02X?} of len {:?} on data_ioc ", + cmd.input, cmd.input_len + ); + + let data_vecs = [libc::iovec { + iov_base: cmd.input.as_mut_ptr() as *mut libc::c_void, + iov_len: cmd.input.len(), + }; 1]; + + // Safe because all zero values from the unsafe method are updated before usage + let mut msghdr: libc::msghdr = unsafe { mem::zeroed() }; + msghdr.msg_name = ptr::null_mut(); + msghdr.msg_namelen = 0; + msghdr.msg_iov = data_vecs.as_ptr() as *mut libc::iovec; + msghdr.msg_iovlen = data_vecs.len() as _; + msghdr.msg_control = ptr::null_mut(); + msghdr.msg_controllen = 0; + msghdr.msg_flags = 0; + // Safe as the return value of the unsafe method is checked + unsafe { + let ret = libc::sendmsg(self.data_fd, &msghdr, 0); + if ret == -1 { + return Err(Error::SendReceive(anyhow!( + "Failed to send tpm command over Data FD. Error Code {:?}", + std::io::Error::last_os_error() + ))); + } + } + + cmd.output.fill(0); + // Safe as return value from unsafe method is checked + unsafe { + let ret = libc::recvfrom( + self.data_fd, + cmd.output.as_ptr() as *mut c_void, + cmd.output.len(), + 0, + &mut addr as *mut libc::sockaddr_storage as *mut libc::sockaddr, + &mut len as *mut socklen_t, + ); + if ret == -1 { + return Err(Error::SendReceive(anyhow!( + "Failed to receive response for tpm command over Data FD. Error Code {:?}", + std::io::Error::last_os_error() + ))); + } + cmd.output_len = ret as usize; + } + debug!( + "response = {:02X?} len = {:?} selftest = {:?}", + cmd.output, cmd.output_len, isselftest + ); + + if isselftest { + if cmd.output_len < 10 { + return Err(Error::SelfTest(anyhow!( + "Self test response should have 10 bytes. Only {:?} returned", + cmd.output_len + ))); + } + let mut errcode: [u8; 4] = [0; 4]; + errcode.copy_from_slice(&cmd.output[6..6 + 4]); + cmd.selftest_done = u32::from_ne_bytes(errcode).to_be() == 0; + } + } + + Ok(()) + } + + pub fn deliver_request(&mut self, in_cmd: &mut BackendCmd) -> Result> { + if self.cmd.is_some() { + //previous request did not finish cleanly + return Err(Error::DeliverRequest(anyhow!( + "Cannot deliver tpm Request as previous cmd did not complete." + ))); + } + self.cmd = Some(in_cmd.clone()); + + self.unix_tx_bufs()?; + + let output = self.cmd.as_ref().unwrap().output.clone(); + in_cmd.output.fill(0); + in_cmd.output.clone_from(&output); + + self.backend_request_completed(); + Ok(output) + } + + pub fn backend_request_completed(&mut self) { + self.cmd = None; + } + + pub fn cancel_cmd(&mut self) -> Result<()> { + let mut res: PtmResult = 0; + + // Check if emulator implements Cancel Cmd + if (self.caps & PTM_CAP_CANCEL_TPM_CMD) != PTM_CAP_CANCEL_TPM_CMD { + return Err(Error::CheckCaps(anyhow!( + "Emulator does not implement 'Cancel Command' Capability" + ))); + } + self.run_control_cmd( + Commands::CmdCancelTpmCmd, + &mut res, + 0, + mem::size_of::(), + )?; + Ok(()) + } + + /// Configure buffersize to use while communicating with swtpm + fn set_buffer_size(&mut self, wantedsize: usize, actualsize: &mut usize) -> Result<()> { + let mut psbs: PtmSetBufferSize = PtmSetBufferSize::new(wantedsize as u32); + + self.stop_tpm()?; + + self.run_control_cmd( + Commands::CmdSetBufferSize, + &mut psbs, + mem::size_of::(), + 4 * mem::size_of::(), + )?; + + *actualsize = psbs.get_bufsize() as usize; + + Ok(()) + } + + pub fn startup_tpm(&mut self, buffersize: usize) -> Result<()> { + let mut init: PtmInit = PtmInit::new(); + + let mut actual_size: usize = 0; + + if buffersize != 0 { + self.set_buffer_size(buffersize, &mut actual_size)?; + debug!("set tpm buffersize to {:?} during Startup", buffersize); + } + + self.run_control_cmd( + Commands::CmdInit, + &mut init, + mem::size_of::(), + mem::size_of::(), + )?; + + Ok(()) + } + + fn stop_tpm(&mut self) -> Result<()> { + let mut res: PtmResult = 0; + + self.run_control_cmd(Commands::CmdStop, &mut res, 0, mem::size_of::())?; + + Ok(()) + } + + pub fn get_buffer_size(&mut self) -> Result { + let mut curr_buf_size: usize = 0; + + match self.set_buffer_size(0, &mut curr_buf_size) { + Err(_) => Ok(TPM_CRB_BUFFER_MAX), + _ => Ok(curr_buf_size), + } + } +} diff --git a/vendor/cube-hypervisor/tpm/src/lib.rs b/vendor/cube-hypervisor/tpm/src/lib.rs new file mode 100644 index 0000000..0bc2020 --- /dev/null +++ b/vendor/cube-hypervisor/tpm/src/lib.rs @@ -0,0 +1,440 @@ +// Copyright © 2022, Microsoft Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#[macro_use] +extern crate log; + +pub mod emulator; +pub mod socket; + +use anyhow::anyhow; +use byteorder::{BigEndian, ReadBytesExt}; +use std::convert::TryInto; +use thiserror::Error; + +pub const TPM_CRB_BUFFER_MAX: usize = 3968; // 0x1_000 - 0x80 +pub const TPM_SUCCESS: u32 = 0x0; + +/* + * Structures required to process Request and Responses of Control commands + * used by control channel over UNIX socket interface + * + * All messages contain big-endian data. + * + * Reference: https://github.com/stefanberger/swtpm/blob/master/man/man3/swtpm_ioctls.pod + */ +#[derive(Debug, Clone, Copy)] +pub enum Commands { + CmdGetCapability = 1, + CmdInit, + CmdShutdown, + CmdGetTpmEstablished, + CmdSetLocality, + CmdHashStart, + CmdHashData, + CmdHashEnd, + CmdCancelTpmCmd, + CmdStoreVolatile, + CmdResetTpmEstablished, + CmdGetStateBlob, + CmdSetStateBlob, + CmdStop, + CmdGetConfig, + CmdSetDatafd, + CmdSetBufferSize, +} + +#[derive(Error, Debug)] +pub enum Error { + #[error("Failed converting buf to PTM : {0}")] + ConvertToPtm(#[source] anyhow::Error), +} +type Result = anyhow::Result; + +#[derive(PartialEq, Eq, Copy, Clone, Debug)] +pub enum MemberType { + Request, + Response, + Error, + Cap, +} + +pub trait Ptm { + // Get Member Type + fn get_member_type(&self) -> MemberType; + + // Set Member Type + fn set_member_type(&mut self, mem: MemberType); + + // Convert PTM Request to bytes to be sent to tpm + fn ptm_to_request(&self) -> Vec; + + // Update PTM from tpm's reponse + fn update_ptm_with_response(&mut self, buf: &[u8]) -> Result<()>; + + // Update tpm result + fn set_result_code(&mut self, res: u32); + + fn get_result_code(&self) -> u32; +} + +/* + * Every response for a tpm Control Command execution must hold tpm return + * code (PtmResult) as its first element. + * Based on the type of input Control Command additional data could be + * appended to the response. + */ +pub type PtmResult = u32; + +impl Ptm for PtmResult { + fn ptm_to_request(&self) -> Vec { + let buf: Vec = Vec::::new(); + buf + } + + fn get_member_type(&self) -> MemberType { + MemberType::Response + } + + fn update_ptm_with_response(&mut self, buf: &[u8]) -> Result<()> { + if buf.len() < 4 { + return Err(Error::ConvertToPtm(anyhow!( + "PtmRes buffer is of insufficient length. Buffer length should be atleast 4" + ))); + } + + *self = u32::from_be_bytes(buf[0..4].try_into().unwrap()); + Ok(()) + } + + fn set_member_type(&mut self, _mem: MemberType) {} + + fn set_result_code(&mut self, res: u32) { + *self = res; + } + + fn get_result_code(&self) -> u32 { + *self + } +} + +/* GET_CAPABILITY Response */ +pub type PtmCap = u64; +impl Ptm for PtmCap { + fn ptm_to_request(&self) -> Vec { + // tpm's GetCapability call doesn't need any supporting message + // return an empty Buffer + let buf: Vec = Vec::::new(); + buf + } + + fn get_member_type(&self) -> MemberType { + MemberType::Cap + } + + fn update_ptm_with_response(&mut self, mut buf: &[u8]) -> Result<()> { + let buf_len = buf.len(); + if buf_len != 8 { + return Err(Error::ConvertToPtm(anyhow!( + "Response for GetCapability cmd is of incorrect length: {:?}. Response buffer should be 8 bytes long", + buf_len))); + } + *self = buf.read_u64::().unwrap(); + Ok(()) + } + + fn set_member_type(&mut self, _mem: MemberType) {} + + fn set_result_code(&mut self, _res: u32) {} + + fn get_result_code(&self) -> u32 { + ((*self) >> 32) as u32 + } +} + +/* GET_TPMESTABLISHED Reponse */ +#[derive(Debug)] +pub struct PtmEstResp { + pub bit: u8, +} + +#[derive(Debug)] +pub struct PtmEst { + member: MemberType, + pub resp: PtmEstResp, + pub result_code: PtmResult, +} + +impl PtmEst { + pub fn new() -> Self { + Self { + member: MemberType::Response, + result_code: 0, + resp: PtmEstResp { bit: 0 }, + } + } +} + +impl Default for PtmEst { + fn default() -> Self { + Self::new() + } +} + +impl Ptm for PtmEst { + fn ptm_to_request(&self) -> Vec { + // tpm's GetTpmEstablished call doesn't need any supporting message + // return an empty Buffer + let buf: Vec = Vec::::new(); + buf + } + + fn get_member_type(&self) -> MemberType { + self.member + } + + fn update_ptm_with_response(&mut self, buf: &[u8]) -> Result<()> { + if buf.len() < 5 { + return Err(Error::ConvertToPtm(anyhow!( + "Response for GetTpmEstablished cmd is of incorrect length. Response buffer should be 5 bytes long" + ))); + } + let mut res = &buf[0..4]; + self.set_result_code(res.read_u32::().unwrap()); + let bit = &buf[4]; + self.resp.bit = *bit; + Ok(()) + } + + fn set_member_type(&mut self, _mem: MemberType) {} + + fn set_result_code(&mut self, res: u32) { + self.result_code = res + } + + fn get_result_code(&self) -> u32 { + self.result_code + } +} + +/* INIT Response */ + +#[derive(Debug)] +pub struct PtmInit { + pub member: MemberType, + /* request */ + pub init_flags: u32, + /* response */ + pub result_code: PtmResult, +} + +impl Default for PtmInit { + fn default() -> Self { + Self::new() + } +} + +impl PtmInit { + pub fn new() -> Self { + Self { + member: MemberType::Request, + init_flags: 0, + result_code: 0, + } + } +} + +impl Ptm for PtmInit { + fn ptm_to_request(&self) -> Vec { + let mut buf: Vec = Vec::::new(); + buf.extend_from_slice(&self.init_flags.to_be_bytes()); + buf + } + + fn get_member_type(&self) -> MemberType { + self.member + } + + fn update_ptm_with_response(&mut self, buf: &[u8]) -> Result<()> { + if buf.len() != 4 { + return Err(Error::ConvertToPtm(anyhow!( + "Response for Init cmd is of incorrect length. Response buffer should be 4 bytes long" + ))); + } + self.set_member_type(MemberType::Response); + let mut res = &buf[0..4]; + self.set_result_code(res.read_u32::().unwrap()); + Ok(()) + } + + fn set_member_type(&mut self, mem: MemberType) { + self.member = mem + } + + fn set_result_code(&mut self, res: u32) { + self.result_code = res + } + + fn get_result_code(&self) -> u32 { + self.result_code + } +} + +/* + * PTM_SET_BUFFERSIZE: Set the buffer size to be used by the tpm. + * A 0 on input queries for the current buffer size. Any other + * number will try to set the buffer size. The returned number is + * the buffer size that will be used, which can be larger than the + * requested one, if it was below the minimum, or smaller than the + * requested one, if it was above the maximum. + * + * SET_BUFFERSIZE Response + */ +#[derive(Debug)] +pub struct PtmSBSReq { + buffersize: u32, +} + +#[derive(Debug)] +pub struct PtmSBSResp { + bufsize: u32, + minsize: u32, + maxsize: u32, +} + +#[derive(Debug)] +pub struct PtmSetBufferSize { + pub mem: MemberType, + /* request */ + pub req: PtmSBSReq, + /* response */ + pub resp: PtmSBSResp, + pub result_code: PtmResult, +} + +impl PtmSetBufferSize { + pub fn new(req_buffsize: u32) -> Self { + Self { + mem: MemberType::Request, + req: PtmSBSReq { + buffersize: req_buffsize, + }, + resp: PtmSBSResp { + bufsize: 0, + minsize: 0, + maxsize: 0, + }, + result_code: 0, + } + } + pub fn get_bufsize(&self) -> u32 { + self.resp.bufsize + } +} + +impl Ptm for PtmSetBufferSize { + fn ptm_to_request(&self) -> Vec { + let mut buf: Vec = Vec::::new(); + buf.extend_from_slice(&self.req.buffersize.to_be_bytes()); + buf + } + + fn get_member_type(&self) -> MemberType { + self.mem + } + + fn update_ptm_with_response(&mut self, buf: &[u8]) -> Result<()> { + if buf.len() != 16 { + return Err(Error::ConvertToPtm(anyhow!( + "Response for CmdSetBufferSize cmd is of incorrect length. Response buffer should be 16 bytes long" + ))); + } + self.set_member_type(MemberType::Response); + let mut res = &buf[0..4]; + self.set_result_code(res.read_u32::().unwrap()); + + let mut bufsize = &buf[4..8]; + self.resp.bufsize = bufsize.read_u32::().unwrap(); + + let mut minsize = &buf[8..12]; + self.resp.minsize = minsize.read_u32::().unwrap(); + + let mut maxsize = &buf[12..16]; + self.resp.maxsize = maxsize.read_u32::().unwrap(); + + Ok(()) + } + + fn set_member_type(&mut self, mem: MemberType) { + self.mem = mem + } + + fn set_result_code(&mut self, res: u32) { + self.result_code = res + } + + fn get_result_code(&self) -> u32 { + self.result_code + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_ptmresult() -> Result<()> { + let buf: &[u8] = &[0, 0, 0, 1]; + let mut result_code: PtmResult = 0; + result_code.update_ptm_with_response(buf)?; + assert_eq!(result_code.get_result_code(), 0x1); + Ok(()) + } + #[test] + fn test_ptmcap() -> Result<()> { + let mut cap: PtmCap = 0x0; + let buf: &[u8] = &[0, 0, 0, 0xE, 0, 0, 0xFF, 0xFF]; + cap.update_ptm_with_response(buf)?; + assert_eq!(cap.get_result_code(), 0xE); + Ok(()) + } + #[test] + fn test_ptmest() -> Result<()> { + let mut est: PtmEst = PtmEst::new(); + let buf: &[u8] = &[0, 0, 0xE, 0, 0xC, 0, 1, 1]; + est.update_ptm_with_response(buf)?; + assert_eq!(est.get_result_code(), 0xE00); + assert_eq!(est.resp.bit, 0xC); + Ok(()) + } + #[test] + /*PtmInit Testing */ + fn test_ptminit() -> Result<()> { + let mut init: PtmInit = PtmInit::new(); + init.init_flags = 0x1; + let buf = init.ptm_to_request(); + assert_eq!(buf, [0x0, 0x0, 0x0, 0x1]); + let response_buf: &[u8] = &[0, 0, 0xE, 0]; + init.update_ptm_with_response(response_buf)?; + assert_eq!(init.get_result_code(), 0xE00); + Ok(()) + } + #[test] + /* PtmSetBufferSize Testing */ + fn test_ptmsetbuffersize() -> Result<()> { + let mut psbs: PtmSetBufferSize = PtmSetBufferSize::new(1024); + // Member type should be Request after initialization + assert_eq!(psbs.get_member_type(), MemberType::Request); + let buf: &[u8] = &[ + 0, 0x12, 0x34, 0x56, 0, 0, 0, 0xA, 0, 0, 0, 0xB, 0, 0, 0, 0xC, + ]; + psbs.update_ptm_with_response(buf)?; + assert_eq!(psbs.get_member_type(), MemberType::Response); + assert_eq!(psbs.get_result_code(), 0x123456); + assert_eq!(psbs.resp.bufsize, 0xA); + assert_eq!(psbs.resp.minsize, 0xB); + assert_eq!(psbs.resp.maxsize, 0xC); + Ok(()) + } +} diff --git a/vendor/cube-hypervisor/tpm/src/socket.rs b/vendor/cube-hypervisor/tpm/src/socket.rs new file mode 100644 index 0000000..60db930 --- /dev/null +++ b/vendor/cube-hypervisor/tpm/src/socket.rs @@ -0,0 +1,144 @@ +// Copyright © 2022, Microsoft Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::TPM_CRB_BUFFER_MAX; +use anyhow::anyhow; +use std::io::Read; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::os::unix::net::UnixStream; +use thiserror::Error; +use vmm_sys_util::sock_ctrl_msg::ScmSocket; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Cannot connect to tpm Socket: {0}")] + ConnectToSocket(#[source] anyhow::Error), + #[error("Failed to read from socket: {0}")] + ReadFromSocket(#[source] anyhow::Error), + #[error("Failed to write to socket: {0}")] + WriteToSocket(#[source] anyhow::Error), +} +type Result = anyhow::Result; + +#[derive(PartialEq)] +enum SocketDevState { + Disconnected, + Connecting, + Connected, +} + +pub struct SocketDev { + state: SocketDevState, + stream: Option, + // Fd sent to swtpm process for Data Channel + write_msgfd: RawFd, + // Data Channel used by Cloud-Hypervisor + data_fd: RawFd, + // Control Channel used by Cloud-Hypervisor + control_fd: RawFd, +} + +impl Default for SocketDev { + fn default() -> Self { + Self::new() + } +} + +impl SocketDev { + pub fn new() -> Self { + Self { + state: SocketDevState::Disconnected, + stream: None, + write_msgfd: -1, + control_fd: -1, + data_fd: -1, + } + } + + pub fn init(&mut self, path: String) -> Result<()> { + self.connect(&path)?; + Ok(()) + } + + pub fn connect(&mut self, socket_path: &str) -> Result<()> { + self.state = SocketDevState::Connecting; + + let s = UnixStream::connect(socket_path).map_err(|e| { + Error::ConnectToSocket(anyhow!("Failed to connect to tpm Socket. Error: {:?}", e)) + })?; + self.control_fd = s.as_raw_fd(); + self.stream = Some(s); + self.state = SocketDevState::Connected; + debug!("Connected to tpm socket path : {:?}", socket_path); + Ok(()) + } + + pub fn set_datafd(&mut self, fd: RawFd) { + self.data_fd = fd; + } + + pub fn set_msgfd(&mut self, fd: RawFd) { + self.write_msgfd = fd; + } + + pub fn send_full(&self, buf: &[u8]) -> Result { + let write_fd = self.write_msgfd; + + let size = self + .stream + .as_ref() + .unwrap() + .send_with_fd(buf, write_fd) + .map_err(|e| { + Error::WriteToSocket(anyhow!("Failed to write to Socket. Error: {:?}", e)) + })?; + + Ok(size) + } + + pub fn write(&mut self, buf: &[u8]) -> Result { + if self.stream.is_none() { + return Err(Error::WriteToSocket(anyhow!( + "Stream for tpm socket was not initialized" + ))); + } + + if matches!(self.state, SocketDevState::Connected) { + let ret = self.send_full(buf)?; + // swtpm will receive data Fd after a successful send + // Reset cached write_msgfd after a successful send + // Ideally, write_msgfd is reset after first Ctrl Command + if ret > 0 && self.write_msgfd != 0 { + self.write_msgfd = 0; + } + Ok(ret) + } else { + Err(Error::WriteToSocket(anyhow!( + "TPM Socket was not in Connected State" + ))) + } + } + + pub fn read(&mut self, buf: &mut [u8]) -> Result { + let newbuf: &mut [u8] = &mut [0; TPM_CRB_BUFFER_MAX]; + + if self.stream.is_none() { + return Err(Error::ReadFromSocket(anyhow!( + "Stream for tpm socket was not initialized" + ))); + } + let mut socket = self.stream.as_ref().unwrap(); + let size: usize = socket.read(newbuf).map_err(|e| { + Error::ReadFromSocket(anyhow!("Failed to read from socket. Error Code {:?}", e)) + })?; + if buf.len() < size { + return Err(Error::ReadFromSocket(anyhow!( + "Input buffer is of insufficient size" + ))); + } + buf[0..size].clone_from_slice(&newbuf[0..size]); + Ok(size) + } +} diff --git a/vendor/cube-hypervisor/tracer/Cargo.toml b/vendor/cube-hypervisor/tracer/Cargo.toml new file mode 100644 index 0000000..8e2a265 --- /dev/null +++ b/vendor/cube-hypervisor/tracer/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "tracer" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[dependencies] +libc = "0.2.137" +log = "0.4.17" +once_cell = "1.16.0" +serde = { workspace = true, features = ["rc", "derive"] } +serde_json = "1.0.87" + +[features] +tracing = [] diff --git a/vendor/cube-hypervisor/tracer/src/lib.rs b/vendor/cube-hypervisor/tracer/src/lib.rs new file mode 100644 index 0000000..40677cf --- /dev/null +++ b/vendor/cube-hypervisor/tracer/src/lib.rs @@ -0,0 +1,18 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#[cfg(feature = "tracing")] +#[macro_use] +extern crate log; + +#[cfg(not(feature = "tracing"))] +mod tracer_noop; +#[cfg(not(feature = "tracing"))] +pub use tracer_noop::*; + +#[cfg(feature = "tracing")] +mod tracer; +#[cfg(feature = "tracing")] +pub use tracer::*; diff --git a/vendor/cube-hypervisor/tracer/src/tracer.rs b/vendor/cube-hypervisor/tracer/src/tracer.rs new file mode 100644 index 0000000..404afc7 --- /dev/null +++ b/vendor/cube-hypervisor/tracer/src/tracer.rs @@ -0,0 +1,174 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use once_cell::unsync::OnceCell; +use serde::Serialize; +use std::collections::HashMap; +use std::fs::File; +use std::io::Write; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +#[derive(Debug)] +struct Tracer { + events: Arc>>>, + thread_depths: HashMap>, + start: Instant, +} + +impl Tracer { + fn new() -> Self { + Self { + events: Arc::new(Mutex::new(HashMap::default())), + start: Instant::now(), + thread_depths: HashMap::default(), + } + } + + fn end(&self) { + let end = Instant::now(); + let path = format!("cloud-hypervisor-{}.trace", unsafe { libc::getpid() }); + let mut file = File::create(&path).unwrap(); + + #[derive(Serialize)] + struct TraceReport { + duration: Duration, + events: Arc>>>, + } + + let trace_report = TraceReport { + duration: end.duration_since(self.start), + events: self.events.clone(), + }; + + serde_json::to_writer_pretty(&file, &trace_report).unwrap(); + + file.flush().unwrap(); + + warn!("Trace output: {}", path); + } + + fn add_event(&mut self, event: TraceEvent) { + let current = std::thread::current(); + let thread_name = current.name().unwrap_or(""); + let mut events = self.events.lock().unwrap(); + if let Some(thread_events) = events.get_mut(thread_name) { + thread_events.push(event); + } else { + events.insert(thread_name.to_string(), vec![event]); + } + } + + fn increase_thread_depth(&mut self) { + let current = std::thread::current(); + let thread_name = current.name().unwrap_or(""); + if let Some(depth) = self.thread_depths.get_mut(thread_name) { + depth.fetch_add(1, Ordering::SeqCst); + } else { + self.thread_depths + .insert(thread_name.to_string(), Arc::new(AtomicU64::new(0))); + } + } + + fn decrease_thread_depth(&mut self) { + let current = std::thread::current(); + let thread_name = current.name().unwrap_or(""); + if let Some(depth) = self.thread_depths.get_mut(thread_name) { + depth.fetch_sub(1, Ordering::SeqCst); + } else { + panic!("Unmatched decrease for thread: {}", thread_name); + } + } + + fn thread_depth(&self) -> u64 { + let current = std::thread::current(); + let thread_name = current.name().unwrap_or(""); + self.thread_depths + .get(thread_name) + .map(|v| v.load(Ordering::SeqCst)) + .unwrap_or_default() + } +} + +static mut TRACER: OnceCell = OnceCell::new(); + +#[derive(Clone, Debug, Serialize)] +struct TraceEvent { + timestamp: Duration, + event: &'static str, + end_timestamp: Option, + depth: u64, +} + +pub fn trace_point_log(event: &'static str) { + let trace_event = TraceEvent { + timestamp: Instant::now().duration_since(unsafe { TRACER.get().unwrap().start }), + event, + end_timestamp: None, + depth: unsafe { TRACER.get().unwrap().thread_depth() }, + }; + unsafe { + TRACER.get_mut().unwrap().add_event(trace_event); + } +} + +pub struct TraceBlock { + start: Instant, + event: &'static str, +} + +impl TraceBlock { + pub fn new(event: &'static str) -> Self { + unsafe { + TRACER.get_mut().unwrap().increase_thread_depth(); + } + Self { + start: Instant::now(), + event, + } + } +} + +impl Drop for TraceBlock { + fn drop(&mut self) { + let trace_event = TraceEvent { + timestamp: self + .start + .duration_since(unsafe { TRACER.get().unwrap().start }), + event: self.event, + end_timestamp: Some( + Instant::now().duration_since(unsafe { TRACER.get().unwrap().start }), + ), + depth: unsafe { TRACER.get().unwrap().thread_depth() }, + }; + unsafe { + TRACER.get_mut().unwrap().add_event(trace_event); + TRACER.get_mut().unwrap().decrease_thread_depth(); + } + } +} + +#[macro_export] +macro_rules! trace_point { + ($event:expr) => { + $crate::trace_point_log($event) + }; +} + +#[macro_export] +macro_rules! trace_scoped { + ($event:expr) => { + let _trace_scoped = $crate::TraceBlock::new($event); + }; +} + +pub fn end() { + unsafe { TRACER.get().unwrap().end() } +} + +pub fn start() { + unsafe { TRACER.set(Tracer::new()).unwrap() } +} diff --git a/vendor/cube-hypervisor/tracer/src/tracer_noop.rs b/vendor/cube-hypervisor/tracer/src/tracer_noop.rs new file mode 100644 index 0000000..8cffbcb --- /dev/null +++ b/vendor/cube-hypervisor/tracer/src/tracer_noop.rs @@ -0,0 +1,17 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#[macro_export] +macro_rules! trace_scoped { + ($event:expr) => {}; +} + +#[macro_export] +macro_rules! trace_point { + ($event:expr) => {}; +} + +pub fn end() {} +pub fn start() {} diff --git a/vendor/cube-hypervisor/vfio_user/Cargo.toml b/vendor/cube-hypervisor/vfio_user/Cargo.toml new file mode 100644 index 0000000..61dd648 --- /dev/null +++ b/vendor/cube-hypervisor/vfio_user/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "vfio_user" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[dependencies] +libc = "0.2.137" +log = "0.4.17" +serde = { workspace = true, features = ["rc"] } +serde_derive = "1.0.142" +serde_json = "1.0.87" +thiserror = "1.0.37" +vm-memory = { version = "0.14.1", features = ["backend-mmap", "backend-atomic"] } +vmm-sys-util = "0.12.1" + +[dependencies.vfio-bindings] +version = "0.3.1" +features = ["fam-wrappers"] diff --git a/vendor/cube-hypervisor/vfio_user/src/lib.rs b/vendor/cube-hypervisor/vfio_user/src/lib.rs new file mode 100644 index 0000000..acdbfe3 --- /dev/null +++ b/vendor/cube-hypervisor/vfio_user/src/lib.rs @@ -0,0 +1,783 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use std::ffi::CString; +use std::fs::File; +use std::io::{IoSlice, Read, Write}; +use std::mem::size_of; +use std::num::Wrapping; +use std::os::unix::net::UnixStream; +use std::os::unix::prelude::RawFd; +use std::path::Path; +use thiserror::Error; +use vfio_bindings::bindings::vfio::*; +use vm_memory::{ByteValued, FileOffset}; +use vmm_sys_util::sock_ctrl_msg::ScmSocket; + +#[macro_use] +extern crate serde_derive; + +#[macro_use] +extern crate log; + +#[allow(dead_code)] +#[repr(u16)] +#[derive(Clone, Copy, Debug)] +enum Command { + Unknown = 0, + Version = 1, + DmaMap = 2, + DmaUnmap = 3, + DeviceGetInfo = 4, + DeviceGetRegionInfo = 5, + GetRegionIoFds = 6, + GetIrqInfo = 7, + SetIrqs = 8, + RegionRead = 9, + RegionWrite = 10, + DmaRead = 11, + DmaWrite = 12, + DeviceReset = 13, + UserDirtyPages = 14, +} + +impl Default for Command { + fn default() -> Self { + Command::Unknown + } +} + +#[allow(dead_code)] +#[repr(u32)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum HeaderFlags { + Command = 0, + Reply = 1, + NoReply = 1 << 4, + Error = 1 << 5, +} + +impl Default for HeaderFlags { + fn default() -> Self { + HeaderFlags::Command + } +} + +#[repr(C)] +#[derive(Default, Clone, Copy, Debug)] +struct Header { + message_id: u16, + command: Command, + message_size: u32, + flags: u32, + error: u32, +} + +#[repr(C)] +#[derive(Default, Clone, Copy, Debug)] +struct Version { + header: Header, + major: u16, + minor: u16, +} + +#[derive(Serialize, Deserialize, Debug)] +struct MigrationCapabilities { + pgsize: u32, +} + +const fn default_max_msg_fds() -> u32 { + 1 +} + +const fn default_max_data_xfer_size() -> u32 { + 1048576 +} + +const fn default_migration_capabilities() -> MigrationCapabilities { + MigrationCapabilities { pgsize: 4096 } +} + +#[repr(u32)] +#[derive(Clone, Copy, Debug)] +#[allow(dead_code)] +enum DmaMapFlags { + Unknown = 0, + ReadOnly = 1, + WriteOnly = 2, + ReadWrite = 3, +} + +impl Default for DmaMapFlags { + fn default() -> Self { + Self::Unknown + } +} + +#[repr(C)] +#[derive(Default, Clone, Copy, Debug)] +struct DmaMap { + header: Header, + argsz: u32, + flags: DmaMapFlags, + offset: u64, + address: u64, + size: u64, +} + +#[repr(C)] +#[derive(Default, Clone, Copy, Debug)] +struct DmaUnmap { + header: Header, + argsz: u32, + flags: u32, + address: u64, + size: u64, +} + +#[repr(C)] +#[derive(Default, Clone, Copy, Debug)] +struct DeviceGetInfo { + header: Header, + argsz: u32, + flags: u32, + num_regions: u32, + num_irqs: u32, +} + +#[repr(C)] +#[derive(Default, Clone, Copy, Debug)] +struct DeviceGetRegionInfo { + header: Header, + region_info: vfio_region_info, +} + +#[repr(C)] +#[derive(Default, Clone, Copy, Debug)] +struct RegionAccess { + header: Header, + offset: u64, + region: u32, + count: u32, +} + +#[repr(C)] +#[derive(Default, Clone, Copy, Debug)] +struct GetIrqInfo { + header: Header, + argsz: u32, + flags: u32, + index: u32, + count: u32, +} + +#[repr(C)] +#[derive(Default, Clone, Copy, Debug)] +struct SetIrqs { + header: Header, + argsz: u32, + flags: u32, + index: u32, + start: u32, + count: u32, +} + +#[repr(C)] +#[derive(Default, Clone, Copy, Debug)] +struct DeviceReset { + header: Header, +} + +// SAFETY: these data structures only contain a sereis of integers +unsafe impl ByteValued for Header {} +unsafe impl ByteValued for Version {} +unsafe impl ByteValued for DmaMap {} +unsafe impl ByteValued for DmaUnmap {} +unsafe impl ByteValued for DeviceGetInfo {} +unsafe impl ByteValued for DeviceGetRegionInfo {} +unsafe impl ByteValued for RegionAccess {} +unsafe impl ByteValued for GetIrqInfo {} +unsafe impl ByteValued for SetIrqs {} +unsafe impl ByteValued for DeviceReset {} + +#[derive(Serialize, Deserialize, Debug)] +struct Capabilities { + #[serde(default = "default_max_msg_fds")] + max_msg_fds: u32, + #[serde(default = "default_max_data_xfer_size")] + max_data_xfer_size: u32, + #[serde(default = "default_migration_capabilities")] + migration: MigrationCapabilities, +} + +impl Default for Capabilities { + fn default() -> Self { + Self { + max_msg_fds: default_max_msg_fds(), + max_data_xfer_size: default_max_data_xfer_size(), + migration: default_migration_capabilities(), + } + } +} + +pub struct Client { + stream: UnixStream, + next_message_id: Wrapping, + num_irqs: u32, + resettable: bool, + regions: Vec, +} + +#[derive(Debug)] +pub struct Region { + pub flags: u32, + pub index: u32, + pub size: u64, + pub file_offset: Option, + pub sparse_areas: Vec, +} + +#[derive(Debug)] +pub struct IrqInfo { + pub index: u32, + pub flags: u32, + pub count: u32, +} + +#[derive(Error, Debug)] +pub enum Error { + #[error("Error connecting: {0}")] + Connect(#[source] std::io::Error), + #[error("Error serializing capabilities: {0}")] + SerializeCapabilites(#[source] serde_json::Error), + #[error("Error deserializing capabilities: {0}")] + DeserializeCapabilites(#[source] serde_json::Error), + #[error("Error writing to stream: {0}")] + StreamWrite(#[source] std::io::Error), + #[error("Error reading from stream: {0}")] + StreamRead(#[source] std::io::Error), + #[error("Error shutting down stream: {0}")] + StreamShutdown(#[source] std::io::Error), + #[error("Error writing with file descriptors: {0}")] + SendWithFd(#[source] vmm_sys_util::errno::Error), + #[error("Error reading with file descriptors: {0}")] + ReceiveWithFd(#[source] vmm_sys_util::errno::Error), + #[error("Not a PCI device")] + NotPciDevice, +} + +impl Client { + pub fn new(path: &Path) -> Result { + let stream = UnixStream::connect(path).map_err(Error::Connect)?; + + let mut client = Client { + next_message_id: Wrapping(0), + stream, + num_irqs: 0, + resettable: false, + regions: Vec::new(), + }; + + client.negotiate_version()?; + + client.regions = client.get_regions()?; + + Ok(client) + } + + fn negotiate_version(&mut self) -> Result<(), Error> { + let caps = Capabilities::default(); + + let version_data = serde_json::to_string(&caps).map_err(Error::SerializeCapabilites)?; + + let version = Version { + header: Header { + message_id: self.next_message_id.0, + command: Command::Version, + flags: HeaderFlags::Command as u32, + message_size: (size_of::() + version_data.len() + 1) as u32, + ..Default::default() + }, + major: 0, + minor: 1, + }; + debug!("Command: {:?}", version); + + let version_data = CString::new(version_data.as_bytes()).unwrap(); + let bufs = vec![ + IoSlice::new(version.as_slice()), + IoSlice::new(version_data.as_bytes_with_nul()), + ]; + + // TODO: Use write_all_vectored() when ready + let _ = self + .stream + .write_vectored(&bufs) + .map_err(Error::StreamWrite)?; + + debug!( + "Sent client version information: major = {} minor = {} capabilities = {:?}", + version.major, version.minor, &caps + ); + + self.next_message_id += Wrapping(1); + + let mut server_version: Version = Version::default(); + self.stream + .read_exact(server_version.as_mut_slice()) + .map_err(Error::StreamRead)?; + + debug!("Reply: {:?}", server_version); + + let mut server_version_data = + vec![0; server_version.header.message_size as usize - size_of::()]; + self.stream + .read_exact(server_version_data.as_mut_slice()) + .map_err(Error::StreamRead)?; + + let server_caps: Capabilities = + serde_json::from_slice(&server_version_data[0..server_version_data.len() - 1]) + .map_err(Error::DeserializeCapabilites)?; + + debug!( + "Received server version information: major = {} minor = {} capabilities = {:?}", + server_version.major, server_version.minor, &server_caps + ); + + Ok(()) + } + + pub fn dma_map( + &mut self, + offset: u64, + address: u64, + size: u64, + fd: RawFd, + ) -> Result<(), Error> { + let dma_map = DmaMap { + header: Header { + message_id: self.next_message_id.0, + command: Command::DmaMap, + flags: HeaderFlags::Command as u32, + message_size: size_of::() as u32, + ..Default::default() + }, + argsz: (size_of::() - size_of::
()) as u32, + flags: DmaMapFlags::ReadWrite, + offset, + address, + size, + }; + debug!("Command: {:?}", dma_map); + self.next_message_id += Wrapping(1); + self.stream + .send_with_fd(dma_map.as_slice(), fd) + .map_err(Error::SendWithFd)?; + + let mut reply = Header::default(); + self.stream + .read_exact(reply.as_mut_slice()) + .map_err(Error::StreamRead)?; + debug!("Reply: {:?}", reply); + + Ok(()) + } + + pub fn dma_unmap(&mut self, address: u64, size: u64) -> Result<(), Error> { + let dma_unmap = DmaUnmap { + header: Header { + message_id: self.next_message_id.0, + command: Command::DmaUnmap, + flags: HeaderFlags::Command as u32, + message_size: size_of::() as u32, + ..Default::default() + }, + argsz: (size_of::() - size_of::
()) as u32, + flags: 0, + address, + size, + }; + debug!("Command: {:?}", dma_unmap); + self.next_message_id += Wrapping(1); + self.stream + .write_all(dma_unmap.as_slice()) + .map_err(Error::StreamWrite)?; + + let mut reply = DmaUnmap::default(); + self.stream + .read_exact(reply.as_mut_slice()) + .map_err(Error::StreamRead)?; + debug!("Reply: {:?}", reply); + + Ok(()) + } + + pub fn reset(&mut self) -> Result<(), Error> { + let reset = DeviceReset { + header: Header { + message_id: self.next_message_id.0, + command: Command::DeviceReset, + flags: HeaderFlags::Command as u32, + message_size: size_of::() as u32, + ..Default::default() + }, + }; + debug!("Command: {:?}", reset); + self.next_message_id += Wrapping(1); + self.stream + .write_all(reset.as_slice()) + .map_err(Error::StreamWrite)?; + + let mut reply = Header::default(); + self.stream + .read_exact(reply.as_mut_slice()) + .map_err(Error::StreamRead)?; + debug!("Reply: {:?}", reply); + + Ok(()) + } + + fn get_regions(&mut self) -> Result, Error> { + let get_info = DeviceGetInfo { + header: Header { + message_id: self.next_message_id.0, + command: Command::DeviceGetInfo, + flags: HeaderFlags::Command as u32, + message_size: size_of::() as u32, + ..Default::default() + }, + argsz: size_of::() as u32, + ..Default::default() + }; + debug!("Command: {:?}", get_info); + self.next_message_id += Wrapping(1); + + self.stream + .write_all(get_info.as_slice()) + .map_err(Error::StreamWrite)?; + + let mut reply = DeviceGetInfo::default(); + self.stream + .read_exact(reply.as_mut_slice()) + .map_err(Error::StreamRead)?; + debug!("Reply: {:?}", reply); + self.num_irqs = reply.num_irqs; + + if reply.flags & VFIO_DEVICE_FLAGS_PCI != VFIO_DEVICE_FLAGS_PCI { + return Err(Error::NotPciDevice); + } + + self.resettable = reply.flags & VFIO_DEVICE_FLAGS_RESET != VFIO_DEVICE_FLAGS_RESET; + + let num_regions = reply.num_regions; + let mut regions = Vec::new(); + for index in 0..num_regions { + let (region_info, fd, sparse_areas) = self.get_region_info(index)?; + regions.push(Region { + flags: region_info.flags, + index: region_info.index, + size: region_info.size, + file_offset: fd.map(|fd| FileOffset::new(fd, region_info.offset)), + sparse_areas, + }); + } + + Ok(regions) + } + + fn get_region_info( + &mut self, + index: u32, + ) -> Result< + ( + vfio_region_info, + Option, + Vec, + ), + Error, + > { + // Retrieve the region info without capability + let mut get_region_info = DeviceGetRegionInfo { + header: Header { + message_id: self.next_message_id.0, + command: Command::DeviceGetRegionInfo, + flags: HeaderFlags::Command as u32, + message_size: std::mem::size_of::() as u32, + ..Default::default() + }, + region_info: vfio_region_info { + argsz: size_of::() as u32, + index, + ..Default::default() + }, + }; + debug!("Command: {:?}", get_region_info); + self.next_message_id += Wrapping(1); + + self.stream + .write_all(get_region_info.as_slice()) + .map_err(Error::StreamWrite)?; + + let mut reply = DeviceGetRegionInfo::default(); + let (_, fd) = self + .stream + .recv_with_fd(reply.as_mut_slice()) + .map_err(Error::ReceiveWithFd)?; + debug!("Reply: {:?}", reply); + + // Retrieve the region info again with capabilities if needed + if reply.region_info.argsz > std::mem::size_of::() as u32 { + get_region_info.region_info.argsz = reply.region_info.argsz; + debug!("Command: {:?}", get_region_info); + self.next_message_id += Wrapping(1); + + self.stream + .write_all(get_region_info.as_slice()) + .map_err(Error::StreamWrite)?; + + let mut reply = DeviceGetRegionInfo::default(); + let (_, fd) = self + .stream + .recv_with_fd(reply.as_mut_slice()) + .map_err(Error::ReceiveWithFd)?; + debug!("Reply: {:?}", reply); + + let cap_size = reply.region_info.argsz - std::mem::size_of::() as u32; + assert_eq!( + cap_size, + reply.header.message_size - size_of::() as u32 + ); + let mut cap_data = vec![0; cap_size as usize]; + self.stream + .read_exact(cap_data.as_mut_slice()) + .map_err(Error::StreamRead)?; + + let sparse_areas = Self::parse_region_caps(&cap_data, &reply.region_info)?; + + Ok((reply.region_info, fd, sparse_areas)) + } else { + Ok((reply.region_info, fd, Vec::new())) + } + } + + fn parse_region_caps( + cap_data: &[u8], + region_info: &vfio_region_info, + ) -> Result, Error> { + let mut sparse_areas: Vec = Vec::new(); + + let cap_size = cap_data.len() as u32; + let cap_header_size = size_of::() as u32; + let mmap_cap_size = size_of::() as u32; + let mmap_area_size = size_of::() as u32; + + let cap_data_ptr = cap_data.as_ptr(); + let mut region_info_offset = region_info.cap_offset; + while region_info_offset != 0 { + // calculate the offset from the begining of the cap_data based on the offset + // that is relative to the begining of the VFIO region info structure + let cap_offset = region_info_offset - size_of::() as u32; + if cap_offset + cap_header_size > cap_size { + warn!( + "Unexpected end of cap data: 'cap_offset + cap_header_size > cap_size' \ + cap_offset = {}, cap_header_size = {}, cap_size = {}", + cap_offset, cap_header_size, cap_size + ); + break; + } + + // Safe because the `cap_data_ptr` is valid and the `cap_offset` is checked above + let cap_ptr = unsafe { cap_data_ptr.offset(cap_offset as isize) }; + let cap_header = unsafe { &*(cap_ptr as *const vfio_info_cap_header) }; + match cap_header.id as u32 { + VFIO_REGION_INFO_CAP_SPARSE_MMAP => { + if cap_offset + mmap_cap_size > cap_size { + warn!( + "Unexpected end of cap data: 'cap_offset + mmap_cap_size > cap_size' \ + cap_offset = {}, mmap_cap_size = {}, cap_size = {}", + cap_offset, mmap_cap_size, cap_size + ); + break; + } + // Safe because the `cap_ptr` is valid and its size is also checked above + let sparse_mmap = unsafe { + &*(cap_ptr as *mut u8 as *const vfio_region_info_cap_sparse_mmap) + }; + + let area_num = sparse_mmap.nr_areas; + if cap_offset + mmap_cap_size + area_num * mmap_area_size > cap_size { + warn!("Unexpected end of cap data: 'cap_offset + mmap_cap_size + area_num * mmap_area_size > cap_size' \ + cap_offset = {}, mmap_cap_size = {}, area_num = {}, mmap_area_size = {}, cap_size = {}", + cap_offset, mmap_cap_size, area_num, mmap_area_size, cap_size); + break; + } + // Safe because the `sparse_mmap` is valid and its size is also checked above + let areas = + unsafe { sparse_mmap.areas.as_slice(sparse_mmap.nr_areas as usize) }; + for area in areas.iter() { + sparse_areas.push(*area); + } + } + _ => { + warn!( + "Ignoring unsupported vfio region capability (id = '{}')", + cap_header.id + ); + } + } + region_info_offset = cap_header.next; + } + + Ok(sparse_areas) + } + + pub fn region_read(&mut self, region: u32, offset: u64, data: &mut [u8]) -> Result<(), Error> { + let region_read = RegionAccess { + header: Header { + message_id: self.next_message_id.0, + command: Command::RegionRead, + flags: HeaderFlags::Command as u32, + message_size: size_of::() as u32, + ..Default::default() + }, + offset, + count: data.len() as u32, + region, + }; + debug!("Command: {:?}", region_read); + self.next_message_id += Wrapping(1); + self.stream + .write_all(region_read.as_slice()) + .map_err(Error::StreamWrite)?; + + let mut reply = RegionAccess::default(); + self.stream + .read_exact(reply.as_mut_slice()) + .map_err(Error::StreamRead)?; + debug!("Reply: {:?}", reply); + self.stream.read_exact(data).map_err(Error::StreamRead)?; + Ok(()) + } + + pub fn region_write(&mut self, region: u32, offset: u64, data: &[u8]) -> Result<(), Error> { + let region_write = RegionAccess { + header: Header { + message_id: self.next_message_id.0, + command: Command::RegionWrite, + flags: HeaderFlags::Command as u32, + message_size: (size_of::() + data.len()) as u32, + ..Default::default() + }, + offset, + count: data.len() as u32, + region, + }; + debug!("Command: {:?}", region_write); + self.next_message_id += Wrapping(1); + + let bufs = vec![IoSlice::new(region_write.as_slice()), IoSlice::new(data)]; + + // TODO: Use write_all_vectored() when ready + let _ = self + .stream + .write_vectored(&bufs) + .map_err(Error::StreamWrite)?; + + let mut reply = RegionAccess::default(); + self.stream + .read_exact(reply.as_mut_slice()) + .map_err(Error::StreamRead)?; + debug!("Reply: {:?}", reply); + Ok(()) + } + + pub fn get_irq_info(&mut self, index: u32) -> Result { + let get_irq_info = GetIrqInfo { + header: Header { + message_id: self.next_message_id.0, + command: Command::GetIrqInfo, + flags: HeaderFlags::Command as u32, + message_size: size_of::() as u32, + ..Default::default() + }, + argsz: (size_of::() - size_of::
()) as u32, + flags: 0, + index, + count: 0, + }; + debug!("Command: {:?}", get_irq_info); + self.next_message_id += Wrapping(1); + + self.stream + .write_all(get_irq_info.as_slice()) + .map_err(Error::StreamWrite)?; + + let mut reply = GetIrqInfo::default(); + self.stream + .read_exact(reply.as_mut_slice()) + .map_err(Error::StreamRead)?; + debug!("Reply: {:?}", reply); + + Ok(IrqInfo { + index: reply.index, + flags: reply.flags, + count: reply.count, + }) + } + + pub fn set_irqs( + &mut self, + index: u32, + flags: u32, + start: u32, + count: u32, + fds: &[RawFd], + ) -> Result<(), Error> { + let set_irqs = SetIrqs { + header: Header { + message_id: self.next_message_id.0, + command: Command::SetIrqs, + flags: HeaderFlags::Command as u32, + message_size: size_of::() as u32, + ..Default::default() + }, + argsz: (size_of::() - size_of::
()) as u32, + flags, + start, + index, + count, + }; + debug!("Command: {:?}", set_irqs); + self.next_message_id += Wrapping(1); + + self.stream + .send_with_fds(&[set_irqs.as_slice()], fds) + .map_err(Error::SendWithFd)?; + + let mut reply = Header::default(); + self.stream + .read_exact(reply.as_mut_slice()) + .map_err(Error::StreamRead)?; + debug!("Reply: {:?}", reply); + + Ok(()) + } + + pub fn region(&self, region_index: u32) -> Option<&Region> { + self.regions + .iter() + .find(|®ion| region.index == region_index) + } + + pub fn resettable(&self) -> bool { + self.resettable + } + + pub fn shutdown(&self) -> Result<(), Error> { + self.stream + .shutdown(std::net::Shutdown::Both) + .map_err(Error::StreamShutdown) + } +} diff --git a/vendor/cube-hypervisor/vhdx/Cargo.toml b/vendor/cube-hypervisor/vhdx/Cargo.toml new file mode 100644 index 0000000..77c18d3 --- /dev/null +++ b/vendor/cube-hypervisor/vhdx/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "vhdx" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +byteorder = "1.4.3" +crc32c = "0.6.3" +libc = "0.2.137" +log = "0.4.17" +remain = "0.2.5" +thiserror = "1.0.37" +uuid = { version = "1.2.1", features = ["v4"] } diff --git a/vendor/cube-hypervisor/vhdx/src/lib.rs b/vendor/cube-hypervisor/vhdx/src/lib.rs new file mode 100644 index 0000000..64e171a --- /dev/null +++ b/vendor/cube-hypervisor/vhdx/src/lib.rs @@ -0,0 +1,30 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use byteorder::{BigEndian, ByteOrder}; +use uuid::Uuid; + +macro_rules! div_round_up { + ($n:expr,$d:expr) => { + ($n + $d - 1) / $d + }; +} + +pub mod vhdx; +mod vhdx_bat; +mod vhdx_header; +mod vhdx_io; +mod vhdx_metadata; + +pub(crate) fn uuid_from_guid(buf: &[u8]) -> Uuid { + // The first 3 fields of UUID are stored in Big Endian format, and + // the last 8 bytes are stored as byte array. Therefore, we read the + // first 3 fields in Big Endian format instead of Little Endian. + Uuid::from_fields_le( + BigEndian::read_u32(&buf[0..4]), + BigEndian::read_u16(&buf[4..6]), + BigEndian::read_u16(&buf[6..8]), + buf[8..16].try_into().unwrap(), + ) +} diff --git a/vendor/cube-hypervisor/vhdx/src/vhdx.rs b/vendor/cube-hypervisor/vhdx/src/vhdx.rs new file mode 100644 index 0000000..73261fe --- /dev/null +++ b/vendor/cube-hypervisor/vhdx/src/vhdx.rs @@ -0,0 +1,212 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use crate::vhdx_bat::{BatEntry, VhdxBatError}; +use crate::vhdx_header::{self, RegionInfo, RegionTableEntry, VhdxHeader, VhdxHeaderError}; +use crate::vhdx_io::{self, VhdxIoError}; +use crate::vhdx_metadata::{DiskSpec, VhdxMetadataError}; +use remain::sorted; +use std::collections::btree_map::BTreeMap; +use std::fs::File; +use std::io::{Read, Seek, SeekFrom, Write}; +use thiserror::Error; + +#[sorted] +#[derive(Error, Debug)] +pub enum VhdxError { + #[error("Not a VHDx file {0}")] + NotVhdx(#[source] VhdxHeaderError), + #[error("Failed to parse VHDx header {0}")] + ParseVhdxHeader(#[source] VhdxHeaderError), + #[error("Failed to parse VHDx metadata {0}")] + ParseVhdxMetadata(#[source] VhdxMetadataError), + #[error("Failed to parse VHDx region entries {0}")] + ParseVhdxRegionEntry(#[source] VhdxHeaderError), + #[error("Failed reading metadata {0}")] + ReadBatEntry(#[source] VhdxBatError), + #[error("Failed reading sector from disk {0}")] + ReadFailed(#[source] VhdxIoError), + #[error("Failed writing to sector on disk {0}")] + WriteFailed(#[source] VhdxIoError), +} + +pub type Result = std::result::Result; + +#[derive(Debug)] +pub struct Vhdx { + file: File, + vhdx_header: VhdxHeader, + region_entries: BTreeMap, + bat_entry: RegionTableEntry, + mdr_entry: RegionTableEntry, + disk_spec: DiskSpec, + bat_entries: Vec, + current_offset: u64, + first_write: bool, +} + +impl Vhdx { + /// Parse the Vhdx header, BAT, and metadata from a file and store info + // in Vhdx structure. + pub fn new(mut file: File) -> Result { + let vhdx_header = VhdxHeader::new(&mut file).map_err(VhdxError::ParseVhdxHeader)?; + + let collected_entries = RegionInfo::new( + &mut file, + vhdx_header::REGION_TABLE_1_START, + vhdx_header.region_entry_count(), + ) + .map_err(VhdxError::ParseVhdxRegionEntry)?; + + let bat_entry = collected_entries.bat_entry; + let mdr_entry = collected_entries.mdr_entry; + + let disk_spec = + DiskSpec::new(&mut file, &mdr_entry).map_err(VhdxError::ParseVhdxMetadata)?; + let bat_entries = BatEntry::collect_bat_entries(&mut file, &disk_spec, &bat_entry) + .map_err(VhdxError::ReadBatEntry)?; + + Ok(Vhdx { + file, + vhdx_header, + region_entries: collected_entries.region_entries, + bat_entry, + mdr_entry, + disk_spec, + bat_entries, + current_offset: 0, + first_write: true, + }) + } + + pub fn virtual_disk_size(&self) -> u64 { + self.disk_spec.virtual_disk_size + } +} + +impl Read for Vhdx { + /// Wrapper function to satisfy Read trait implementation for VHDx disk. + /// Convert the offset to sector index and buffer length to sector count. + fn read(&mut self, buf: &mut [u8]) -> std::result::Result { + let sector_count = + div_round_up!(buf.len() as u64, self.disk_spec.logical_sector_size as u64); + let sector_index = self.current_offset / self.disk_spec.logical_sector_size as u64; + + vhdx_io::read( + &mut self.file, + buf, + &self.disk_spec, + &self.bat_entries, + sector_index, + sector_count, + ) + .map_err(|e| { + std::io::Error::new( + std::io::ErrorKind::Other, + format!( + "Failed reading {} sectors from VHDx at index {}: {}", + sector_count, sector_index, e + ), + ) + }) + } +} + +impl Write for Vhdx { + fn flush(&mut self) -> std::result::Result<(), std::io::Error> { + self.file.flush() + } + + /// Wrapper function to satisfy Write trait implementation for VHDx disk. + /// Convert the offset to sector index and buffer length to sector count. + fn write(&mut self, buf: &[u8]) -> std::result::Result { + let sector_count = + div_round_up!(buf.len() as u64, self.disk_spec.logical_sector_size as u64); + let sector_index = self.current_offset / self.disk_spec.logical_sector_size as u64; + + if self.first_write { + self.first_write = false; + self.vhdx_header.update(&mut self.file).map_err(|e| { + std::io::Error::new( + std::io::ErrorKind::Other, + format!("Failed to update VHDx header: {}", e), + ) + })?; + } + + vhdx_io::write( + &mut self.file, + buf, + &mut self.disk_spec, + self.bat_entry.file_offset, + &mut self.bat_entries, + sector_index, + sector_count, + ) + .map_err(|e| { + std::io::Error::new( + std::io::ErrorKind::Other, + format!( + "Failed writing {} sectors on VHDx at index {}: {}", + sector_count, sector_index, e + ), + ) + }) + } +} + +impl Seek for Vhdx { + /// Wrapper function to satisfy Seek trait implementation for VHDx disk. + /// Updates the offset field in the Vhdx struct. + fn seek(&mut self, pos: SeekFrom) -> std::io::Result { + let new_offset: Option = match pos { + SeekFrom::Start(off) => Some(off), + SeekFrom::End(off) => { + if off < 0 { + 0i64.checked_sub(off).and_then(|increment| { + self.virtual_disk_size().checked_sub(increment as u64) + }) + } else { + self.virtual_disk_size().checked_add(off as u64) + } + } + SeekFrom::Current(off) => { + if off < 0 { + 0i64.checked_sub(off) + .and_then(|increment| self.current_offset.checked_sub(increment as u64)) + } else { + self.current_offset.checked_add(off as u64) + } + } + }; + + if let Some(o) = new_offset { + if o <= self.virtual_disk_size() { + self.current_offset = o; + return Ok(o); + } + } + + Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "Failed seek operation", + )) + } +} + +impl Clone for Vhdx { + fn clone(&self) -> Self { + Vhdx { + file: self.file.try_clone().unwrap(), + vhdx_header: self.vhdx_header.clone(), + region_entries: self.region_entries.clone(), + bat_entry: self.bat_entry, + mdr_entry: self.mdr_entry, + disk_spec: self.disk_spec.clone(), + bat_entries: self.bat_entries.clone(), + current_offset: self.current_offset, + first_write: self.first_write, + } + } +} diff --git a/vendor/cube-hypervisor/vhdx/src/vhdx_bat.rs b/vendor/cube-hypervisor/vhdx/src/vhdx_bat.rs new file mode 100644 index 0000000..a110bd8 --- /dev/null +++ b/vendor/cube-hypervisor/vhdx/src/vhdx_bat.rs @@ -0,0 +1,104 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use crate::vhdx_header::RegionTableEntry; +use crate::vhdx_metadata::DiskSpec; +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +use remain::sorted; +use std::fs::File; +use std::io::{self, Seek, SeekFrom}; +use std::mem::size_of; +use thiserror::Error; + +// Payload BAT Entry States +pub const PAYLOAD_BLOCK_NOT_PRESENT: u64 = 0; +pub const PAYLOAD_BLOCK_UNDEFINED: u64 = 1; +pub const PAYLOAD_BLOCK_ZERO: u64 = 2; +pub const PAYLOAD_BLOCK_UNMAPPED: u64 = 3; +pub const PAYLOAD_BLOCK_FULLY_PRESENT: u64 = 6; +pub const PAYLOAD_BLOCK_PARTIALLY_PRESENT: u64 = 7; + +// Mask for the BAT state +pub const BAT_STATE_BIT_MASK: u64 = 0x07; +// Mask for the offset within the file in units of 1 MB +pub const BAT_FILE_OFF_MASK: u64 = 0xFFFFFFFFFFF00000; + +#[sorted] +#[derive(Error, Debug)] +pub enum VhdxBatError { + #[error("Invalid BAT entry")] + InvalidBatEntry, + #[error("Invalid BAT entry count")] + InvalidEntryCount, + #[error("Failed to read BAT entry {0}")] + ReadBat(#[source] io::Error), + #[error("Failed to write BAT entry {0}")] + WriteBat(#[source] io::Error), +} + +pub type Result = std::result::Result; + +#[derive(Default, Clone, Debug)] +pub struct BatEntry(pub u64); + +impl BatEntry { + // Read all BAT entries presented on the disk and insert them to a vector + pub fn collect_bat_entries( + f: &mut File, + disk_spec: &DiskSpec, + bat_entry: &RegionTableEntry, + ) -> Result> { + let entry_count = BatEntry::calculate_entries( + disk_spec.block_size, + disk_spec.virtual_disk_size, + disk_spec.chunk_ratio, + ); + if entry_count as usize > (bat_entry.length as usize / size_of::()) { + return Err(VhdxBatError::InvalidEntryCount); + } + + let mut bat: Vec = Vec::with_capacity(bat_entry.length as usize); + let offset = bat_entry.file_offset; + for i in 0..entry_count { + f.seek(SeekFrom::Start(offset + i * size_of::() as u64)) + .map_err(VhdxBatError::ReadBat)?; + + let bat_entry = BatEntry( + f.read_u64::() + .map_err(VhdxBatError::ReadBat)?, + ); + bat.insert(i as usize, bat_entry); + } + + Ok(bat) + } + + // Calculate the number of entries in the BAT + fn calculate_entries(block_size: u32, virtual_disk_size: u64, chunk_ratio: u64) -> u64 { + let data_blocks_count = div_round_up!(virtual_disk_size, block_size as u64); + data_blocks_count + (data_blocks_count - 1) / chunk_ratio + } + + // Routine for writing BAT entries to the disk + pub fn write_bat_entries( + f: &mut File, + bat_offset: u64, + bat_entries: &[BatEntry], + ) -> Result<()> { + for i in 0..bat_entries.len() as u64 { + f.seek(SeekFrom::Start(bat_offset + i * size_of::() as u64)) + .map_err(VhdxBatError::WriteBat)?; + let bat_entry = match bat_entries.get(i as usize) { + Some(entry) => entry.0, + None => { + return Err(VhdxBatError::InvalidBatEntry); + } + }; + + f.write_u64::(bat_entry) + .map_err(VhdxBatError::WriteBat)?; + } + Ok(()) + } +} diff --git a/vendor/cube-hypervisor/vhdx/src/vhdx_header.rs b/vendor/cube-hypervisor/vhdx/src/vhdx_header.rs new file mode 100644 index 0000000..09697bb --- /dev/null +++ b/vendor/cube-hypervisor/vhdx/src/vhdx_header.rs @@ -0,0 +1,483 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +extern crate log; + +use byteorder::{ByteOrder, LittleEndian, ReadBytesExt}; +use remain::sorted; +use std::collections::btree_map::BTreeMap; +use std::convert::TryInto; +use std::fs::File; +use std::io::{self, Read, Seek, SeekFrom, Write}; +use std::mem::size_of; +use thiserror::Error; +use uuid::Uuid; + +const VHDX_SIGN: u64 = 0x656C_6966_7864_6876; // "vhdxfile" +const HEADER_SIGN: u32 = 0x6461_6568; // "head" +const REGION_SIGN: u32 = 0x6967_6572; // "regi" + +const FILE_START: u64 = 0; // The first element +const HEADER_1_START: u64 = 64 * 1024; // Header 1 start in Bytes +const HEADER_2_START: u64 = 128 * 1024; // Header 2 start in Bytes +pub const REGION_TABLE_1_START: u64 = 192 * 1024; // Region 1 start in Bytes +const REGION_TABLE_2_START: u64 = 256 * 1024; // Region 2 start in Bytes + +const HEADER_SIZE: u64 = 4 * 1024; // Each header is 64 KiB, but only first 4 kiB contains info +const REGION_SIZE: u64 = 64 * 1024; // Each region size is 64 KiB + +const REGION_ENTRY_REQUIRED: u32 = 1; + +const BAT_GUID: &str = "2DC27766-F623-4200-9D64-115E9BFD4A08"; // BAT GUID +const MDR_GUID: &str = "8B7CA206-4790-4B9A-B8FE-575F050F886E"; // Metadata GUID + +#[sorted] +#[derive(Error, Debug)] +pub enum VhdxHeaderError { + #[error("Failed to calculate checksum")] + CalculateChecksum, + #[error("BAT entry is not unique")] + DuplicateBATEntry, + #[error("Metadata region entry is not unique")] + DuplicateMDREntry, + #[error("Checksum doesn't match for")] + InvalidChecksum(String), + #[error("Invalid entry count")] + InvalidEntryCount, + #[error("Not a valid VHDx header")] + InvalidHeaderSign, + #[error("Not a valid VHDx region")] + InvalidRegionSign, + #[error("Couldn't parse Uuid for region entry {0}")] + InvalidUuid(#[source] uuid::Error), + #[error("Not a VHDx file")] + InvalidVHDXSign, + #[error("No valid header found")] + NoValidHeader, + #[error("Cannot read checksum")] + ReadChecksum, + #[error("Failed to read File Type Identifier {0}")] + ReadFileTypeIdentifier(#[source] io::Error), + #[error("Failed to read headers {0}")] + ReadHeader(#[source] io::Error), + #[error("Failed to read metadata {0}")] + ReadMetadata(#[source] std::io::Error), + #[error("Failed to read region table entries {0}")] + ReadRegionTableEntries(#[source] io::Error), + #[error("Failed to read region table header {0}")] + ReadRegionTableHeader(#[source] io::Error), + #[error("Failed to read region entries")] + RegionEntryCollectionFailed, + #[error("Overlapping regions found")] + RegionOverlap, + #[error("Reserved region has non-zero value")] + ReservedIsNonZero, + #[error("Failed to seek in File Type Identifier {0}")] + SeekFileTypeIdentifier(#[source] io::Error), + #[error("Failed to seek in headers {0}")] + SeekHeader(#[source] io::Error), + #[error("Failed to seek in region table entries {0}")] + SeekRegionTableEntries(#[source] io::Error), + #[error("Failed to seek in region table header {0}")] + SeekRegionTableHeader(#[source] io::Error), + #[error("We do not recongize this entry")] + UnrecognizedRegionEntry, + #[error("Failed to write header {0}")] + WriteHeader(#[source] io::Error), +} + +pub type Result = std::result::Result; + +#[derive(Clone, Debug)] +pub struct FileTypeIdentifier { + pub signature: u64, +} + +impl FileTypeIdentifier { + /// Reads the File Type Identifier structure from a reference VHDx file + pub fn new(f: &mut File) -> Result { + f.seek(SeekFrom::Start(FILE_START)) + .map_err(VhdxHeaderError::SeekFileTypeIdentifier)?; + let signature = f + .read_u64::() + .map_err(VhdxHeaderError::ReadFileTypeIdentifier)?; + if signature != VHDX_SIGN { + return Err(VhdxHeaderError::InvalidVHDXSign); + } + + Ok(FileTypeIdentifier { signature }) + } +} + +#[repr(packed)] +#[derive(Clone, Copy, Debug)] +pub struct Header { + pub signature: u32, + pub checksum: u32, + pub sequence_number: u64, + pub file_write_guid: u128, + pub data_write_guid: u128, + pub log_guid: u128, + pub log_version: u16, + pub version: u16, + pub log_length: u32, + pub log_offset: u64, +} + +impl Header { + /// Reads the Header structure from a reference VHDx file + pub fn new(f: &mut File, start: u64) -> Result
{ + // Read the whole header in to a buffer. We will need it for + // calculating checksum. + let mut buffer = [0; HEADER_SIZE as usize]; + f.seek(SeekFrom::Start(start)) + .map_err(VhdxHeaderError::SeekHeader)?; + f.read_exact(&mut buffer) + .map_err(VhdxHeaderError::ReadHeader)?; + + let header = unsafe { *(buffer.as_ptr() as *mut Header) }; + if header.signature != HEADER_SIGN { + return Err(VhdxHeaderError::InvalidHeaderSign); + } + + let new_checksum = calculate_checksum(&mut buffer, size_of::())?; + if header.checksum != new_checksum { + return Err(VhdxHeaderError::InvalidChecksum(String::from("Header"))); + } + + Ok(header) + } + + /// Converts the header structure into a buffer + fn get_header_as_buffer(&self, buffer: &mut [u8; HEADER_SIZE as usize]) { + let reference = unsafe { + std::slice::from_raw_parts(self as *const Header as *const u8, HEADER_SIZE as usize) + }; + *buffer = reference.try_into().unwrap(); + } + + /// Creates and returns new updated header from the provided current header + pub fn update_header( + f: &mut File, + current_header: &Header, + change_data_guid: bool, + mut file_write_guid: u128, + start: u64, + ) -> Result
{ + let mut buffer = [0u8; HEADER_SIZE as usize]; + let mut data_write_guid = current_header.data_write_guid; + + if change_data_guid { + data_write_guid = Uuid::new_v4().as_u128(); + } + + if file_write_guid == 0 { + file_write_guid = current_header.file_write_guid; + } + + let mut new_header = Header { + signature: current_header.signature, + checksum: 0, + sequence_number: current_header.sequence_number + 1, + file_write_guid, + data_write_guid, + log_guid: current_header.log_guid, + log_version: current_header.log_version, + version: current_header.version, + log_length: current_header.log_length, + log_offset: current_header.log_offset, + }; + + new_header.get_header_as_buffer(&mut buffer); + new_header.checksum = crc32c::crc32c(&buffer); + new_header.get_header_as_buffer(&mut buffer); + + f.seek(SeekFrom::Start(start)) + .map_err(VhdxHeaderError::SeekHeader)?; + f.write(&buffer).map_err(VhdxHeaderError::WriteHeader)?; + + Ok(new_header) + } +} + +#[repr(packed)] +#[derive(Clone, Copy, Debug)] +struct RegionTableHeader { + pub signature: u32, + pub checksum: u32, + pub entry_count: u32, + pub reserved: u32, +} + +impl RegionTableHeader { + /// Reads the Region Table Header structure from a reference VHDx file + pub fn new(f: &mut File, start: u64) -> Result { + // Read the whole header into a buffer. We will need it for calculating + // checksum. + let mut buffer = [0u8; REGION_SIZE as usize]; + f.seek(SeekFrom::Start(start)) + .map_err(VhdxHeaderError::SeekRegionTableHeader)?; + f.read_exact(&mut buffer) + .map_err(VhdxHeaderError::ReadRegionTableHeader)?; + + let region_table_header = unsafe { *(buffer.as_ptr() as *mut RegionTableHeader) }; + if region_table_header.signature != REGION_SIGN { + return Err(VhdxHeaderError::InvalidRegionSign); + } + + let new_checksum = calculate_checksum(&mut buffer, size_of::())?; + if region_table_header.checksum != new_checksum { + return Err(VhdxHeaderError::InvalidChecksum(String::from("Region"))); + } + + if region_table_header.entry_count > 2047 { + return Err(VhdxHeaderError::InvalidEntryCount); + } + + if region_table_header.reserved != 0 { + return Err(VhdxHeaderError::ReservedIsNonZero); + } + + Ok(region_table_header) + } +} + +pub struct RegionInfo { + pub bat_entry: RegionTableEntry, + pub mdr_entry: RegionTableEntry, + pub region_entries: BTreeMap, +} + +impl RegionInfo { + /// Collect all entries in a BTreeMap from the Region Table and identifies + /// BAT and metadata regions + pub fn new(f: &mut File, region_start: u64, entry_count: u32) -> Result { + let mut bat_entry: Option = None; + let mut mdr_entry: Option = None; + + let mut offset = 0; + let mut region_entries = BTreeMap::new(); + + let mut buffer = [0; REGION_SIZE as usize]; + // Seek after the Region Table Header + f.seek(SeekFrom::Start( + region_start + size_of::() as u64, + )) + .map_err(VhdxHeaderError::SeekRegionTableEntries)?; + f.read_exact(&mut buffer) + .map_err(VhdxHeaderError::ReadRegionTableEntries)?; + + for _ in 0..entry_count { + let entry = + RegionTableEntry::new(&buffer[offset..offset + size_of::()])?; + + offset += size_of::(); + let start = entry.file_offset; + let end = start + entry.length as u64; + + for (region_ent_start, region_ent_end) in region_entries.iter() { + if !((start >= *region_ent_start) || (end <= *region_ent_end)) { + return Err(VhdxHeaderError::RegionOverlap); + } + } + + region_entries.insert(entry.file_offset, entry.file_offset + entry.length as u64); + + if entry.guid == Uuid::parse_str(BAT_GUID).map_err(VhdxHeaderError::InvalidUuid)? { + if bat_entry.is_none() { + bat_entry = Some(entry); + continue; + } + return Err(VhdxHeaderError::DuplicateBATEntry); + } + + if entry.guid == Uuid::parse_str(MDR_GUID).map_err(VhdxHeaderError::InvalidUuid)? { + if mdr_entry.is_none() { + mdr_entry = Some(entry); + continue; + } + return Err(VhdxHeaderError::DuplicateMDREntry); + } + + if (entry.required & REGION_ENTRY_REQUIRED) == 1 { + // This implementation doesn't recognize this field. + // Therefore, accoding to the spec, we are throwing an error. + return Err(VhdxHeaderError::UnrecognizedRegionEntry); + } + } + + if bat_entry.is_none() || mdr_entry.is_none() { + region_entries.clear(); + return Err(VhdxHeaderError::RegionEntryCollectionFailed); + } + + // It's safe to unwrap as we checked both entries have been filled. + // Otherwise, an error is already returned. + let bat_entry = bat_entry.unwrap(); + let mdr_entry = mdr_entry.unwrap(); + + Ok(RegionInfo { + bat_entry, + mdr_entry, + region_entries, + }) + } +} + +#[repr(packed)] +#[derive(Clone, Copy, Debug)] +pub struct RegionTableEntry { + pub guid: Uuid, + pub file_offset: u64, + pub length: u32, + pub required: u32, +} + +impl RegionTableEntry { + /// Reads one Region Entry from a Region Table index that starts from 0 + pub fn new(buffer: &[u8]) -> Result { + let mut region_table_entry = unsafe { *(buffer.as_ptr() as *mut RegionTableEntry) }; + + let uuid = crate::uuid_from_guid(buffer); + region_table_entry.guid = uuid; + + Ok(region_table_entry) + } +} + +#[derive(Clone, Debug)] +struct RegionEntry { + _start: u64, + _end: u64, +} + +enum HeaderNo { + First, + Second, +} + +/// Contains the information from the header of a VHDx file +#[derive(Clone, Debug)] +pub struct VhdxHeader { + _file_type_identifier: FileTypeIdentifier, + header_1: Header, + header_2: Header, + region_table_1: RegionTableHeader, + _region_table_2: RegionTableHeader, +} + +impl VhdxHeader { + /// Creates a VhdxHeader from a reference to a file + pub fn new(f: &mut File) -> Result { + let _file_type_identifier: FileTypeIdentifier = FileTypeIdentifier::new(f)?; + let header_1 = Header::new(f, HEADER_1_START); + let header_2 = Header::new(f, HEADER_2_START); + + let mut file_write_guid: u128 = 0; + let metadata = f.metadata().map_err(VhdxHeaderError::ReadMetadata)?; + if !metadata.permissions().readonly() { + file_write_guid = Uuid::new_v4().as_u128(); + } + + let (header_1, header_2) = + VhdxHeader::update_headers(f, header_1, header_2, file_write_guid)?; + Ok(VhdxHeader { + _file_type_identifier, + header_1, + header_2, + region_table_1: RegionTableHeader::new(f, REGION_TABLE_1_START)?, + _region_table_2: RegionTableHeader::new(f, REGION_TABLE_2_START)?, + }) + } + + /// Identify the current header and return both headers along with an + /// integer indicating the current header. + fn current_header( + header_1: Result
, + header_2: Result
, + ) -> Result<(HeaderNo, Header)> { + let header_1 = header_1.ok(); + let header_2 = header_2.ok(); + + match (header_1, header_2) { + (None, None) => Err(VhdxHeaderError::NoValidHeader), + (Some(header_1), None) => Ok((HeaderNo::First, header_1)), + (None, Some(header_2)) => Ok((HeaderNo::Second, header_2)), + (Some(header_1), Some(header_2)) => { + if header_1.sequence_number >= header_2.sequence_number { + Ok((HeaderNo::First, header_1)) + } else { + Ok((HeaderNo::Second, header_2)) + } + } + } + } + + /// This takes two headers and update the noncurrent header with the + /// current one. Returns both headers as a tuple sequenced the way it was + /// received from the parameter list. + fn update_header( + f: &mut File, + header_1: Result
, + header_2: Result
, + guid: u128, + ) -> Result<(Header, Header)> { + let (header_no, current_header) = VhdxHeader::current_header(header_1, header_2)?; + + match header_no { + HeaderNo::First => { + let other_header = + Header::update_header(f, ¤t_header, true, guid, HEADER_2_START)?; + Ok((current_header, other_header)) + } + HeaderNo::Second => { + let other_header = + Header::update_header(f, ¤t_header, true, guid, HEADER_1_START)?; + Ok((other_header, current_header)) + } + } + } + + // Update the provided headers accoding to the spec + fn update_headers( + f: &mut File, + header_1: Result
, + header_2: Result
, + guid: u128, + ) -> Result<(Header, Header)> { + // According to the spec, update twice + let (header_1, header_2) = VhdxHeader::update_header(f, header_1, header_2, guid)?; + VhdxHeader::update_header(f, Ok(header_1), Ok(header_2), guid) + } + + pub fn update(&mut self, f: &mut File) -> Result<()> { + let headers = VhdxHeader::update_headers(f, Ok(self.header_1), Ok(self.header_2), 0)?; + self.header_1 = headers.0; + self.header_2 = headers.1; + Ok(()) + } + + pub fn region_entry_count(&self) -> u32 { + self.region_table_1.entry_count + } +} + +/// Calculates the checksum of a buffer that itself containts its checksum +/// Therefore, before calculating, the existing checksum is retrieved and the +/// corresponding field is made zero. After the calculation, the existing checksum +/// is put back to the buffer. +pub fn calculate_checksum(buffer: &mut [u8], csum_offset: usize) -> Result { + // Read the checksum into a mutable slice + let csum_buf = &mut buffer[csum_offset..csum_offset + 4]; + // Convert the checksum chunk in to a u32 integer + let orig_csum = LittleEndian::read_u32(csum_buf); + // Zero the checksum in the buffer + LittleEndian::write_u32(csum_buf, 0); + // Calculate the checksum on the resulting buffer + let new_csum = crc32c::crc32c(buffer); + // Put back the original checksum in the buffer + LittleEndian::write_u32(&mut buffer[csum_offset..csum_offset + 4], orig_csum); + + Ok(new_csum) +} diff --git a/vendor/cube-hypervisor/vhdx/src/vhdx_io.rs b/vendor/cube-hypervisor/vhdx/src/vhdx_io.rs new file mode 100644 index 0000000..9fbb950 --- /dev/null +++ b/vendor/cube-hypervisor/vhdx/src/vhdx_io.rs @@ -0,0 +1,224 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use crate::vhdx_bat::{self, BatEntry, VhdxBatError}; +use crate::vhdx_metadata::{self, DiskSpec}; +use remain::sorted; +use std::fs::File; +use std::io::{self, Read, Seek, SeekFrom, Write}; +use thiserror::Error; + +const SECTOR_SIZE: u64 = 512; + +#[sorted] +#[derive(Error, Debug)] +pub enum VhdxIoError { + #[error("Invalid BAT entry state")] + InvalidBatEntryState, + #[error("Invalid BAT entry count")] + InvalidBatIndex, + #[error("Invalid disk size")] + InvalidDiskSize, + #[error("Failed reading sector blocks from file {0}")] + ReadSectorBlock(#[source] io::Error), + #[error("Failed changing file length {0}")] + ResizeFile(#[source] io::Error), + #[error("Differencing mode is not supported yet")] + UnsupportedMode, + #[error("Failed writing BAT to file {0}")] + WriteBat(#[source] VhdxBatError), +} + +pub type Result = std::result::Result; + +macro_rules! align { + ($n:expr, $align:expr) => {{ + (($n + $align - 1) / $align) * $align + }}; +} + +#[derive(Default)] +struct Sector { + bat_index: u64, + free_sectors: u64, + free_bytes: u64, + file_offset: u64, + block_offset: u64, +} + +impl Sector { + /// Translate sector index and count of data in file to actual offsets and + /// BAT index. + pub fn new( + disk_spec: &DiskSpec, + bat: &[BatEntry], + sector_index: u64, + sector_count: u64, + ) -> Result { + let mut sector = Sector::default(); + + sector.bat_index = sector_index / disk_spec.sectors_per_block as u64; + sector.block_offset = sector_index % disk_spec.sectors_per_block as u64; + sector.free_sectors = disk_spec.sectors_per_block as u64 - sector.block_offset; + if sector.free_sectors > sector_count { + sector.free_sectors = sector_count; + } + + sector.free_bytes = sector.free_sectors * disk_spec.logical_sector_size as u64; + sector.block_offset *= disk_spec.logical_sector_size as u64; + + let bat_entry = match bat.get(sector.bat_index as usize) { + Some(entry) => entry.0, + None => { + return Err(VhdxIoError::InvalidBatIndex); + } + }; + sector.file_offset = bat_entry & vhdx_bat::BAT_FILE_OFF_MASK; + if sector.file_offset != 0 { + sector.file_offset += sector.block_offset; + } + + Ok(sector) + } +} + +/// VHDx IO read routine: requires relative sector index and count for the +/// requested data. +pub fn read( + f: &mut File, + buf: &mut [u8], + disk_spec: &DiskSpec, + bat: &[BatEntry], + mut sector_index: u64, + mut sector_count: u64, +) -> Result { + let mut read_count: usize = 0; + + while sector_count > 0 { + if disk_spec.has_parent { + return Err(VhdxIoError::UnsupportedMode); + } else { + let sector = Sector::new(disk_spec, bat, sector_index, sector_count)?; + + let bat_entry = match bat.get(sector.bat_index as usize) { + Some(entry) => entry.0, + None => { + return Err(VhdxIoError::InvalidBatIndex); + } + }; + + match bat_entry & vhdx_bat::BAT_STATE_BIT_MASK { + vhdx_bat::PAYLOAD_BLOCK_NOT_PRESENT + | vhdx_bat::PAYLOAD_BLOCK_UNDEFINED + | vhdx_bat::PAYLOAD_BLOCK_UNMAPPED + | vhdx_bat::PAYLOAD_BLOCK_ZERO => {} + vhdx_bat::PAYLOAD_BLOCK_FULLY_PRESENT => { + f.seek(SeekFrom::Start(sector.file_offset)) + .map_err(VhdxIoError::ReadSectorBlock)?; + f.read_exact( + &mut buf[read_count + ..(read_count + (sector.free_sectors * SECTOR_SIZE) as usize)], + ) + .map_err(VhdxIoError::ReadSectorBlock)?; + } + vhdx_bat::PAYLOAD_BLOCK_PARTIALLY_PRESENT => { + return Err(VhdxIoError::UnsupportedMode); + } + _ => { + return Err(VhdxIoError::InvalidBatEntryState); + } + }; + sector_count -= sector.free_sectors; + sector_index += sector.free_sectors; + read_count = sector.free_bytes as usize; + }; + } + Ok(read_count) +} + +/// VHDx IO write routine: requires relative sector index and count for the +/// requested data. +pub fn write( + f: &mut File, + buf: &[u8], + disk_spec: &mut DiskSpec, + bat_offset: u64, + bat: &mut [BatEntry], + mut sector_index: u64, + mut sector_count: u64, +) -> Result { + let mut write_count: usize = 0; + + while sector_count > 0 { + if disk_spec.has_parent { + return Err(VhdxIoError::UnsupportedMode); + } else { + let sector = Sector::new(disk_spec, bat, sector_index, sector_count)?; + + let bat_entry = match bat.get(sector.bat_index as usize) { + Some(entry) => entry.0, + None => { + return Err(VhdxIoError::InvalidBatIndex); + } + }; + + match bat_entry & vhdx_bat::BAT_STATE_BIT_MASK { + vhdx_bat::PAYLOAD_BLOCK_NOT_PRESENT + | vhdx_bat::PAYLOAD_BLOCK_UNDEFINED + | vhdx_bat::PAYLOAD_BLOCK_UNMAPPED + | vhdx_bat::PAYLOAD_BLOCK_ZERO => { + let file_offset = + align!(disk_spec.image_size, vhdx_metadata::BLOCK_SIZE_MIN as u64); + let new_size = file_offset + .checked_add(disk_spec.block_size as u64) + .ok_or(VhdxIoError::InvalidDiskSize)?; + + f.set_len(new_size).map_err(VhdxIoError::ResizeFile)?; + disk_spec.image_size = new_size; + + let new_bat_entry = file_offset + | (vhdx_bat::PAYLOAD_BLOCK_FULLY_PRESENT & vhdx_bat::BAT_STATE_BIT_MASK); + bat[sector.bat_index as usize] = BatEntry(new_bat_entry); + BatEntry::write_bat_entries(f, bat_offset, bat) + .map_err(VhdxIoError::WriteBat)?; + + if file_offset < vhdx_metadata::BLOCK_SIZE_MIN as u64 { + break; + } + + f.seek(SeekFrom::Start(file_offset)) + .map_err(VhdxIoError::ReadSectorBlock)?; + f.write_all( + &buf[write_count + ..(write_count + (sector.free_sectors * SECTOR_SIZE) as usize)], + ) + .map_err(VhdxIoError::ReadSectorBlock)?; + } + vhdx_bat::PAYLOAD_BLOCK_FULLY_PRESENT => { + if sector.file_offset < vhdx_metadata::BLOCK_SIZE_MIN as u64 { + break; + } + + f.seek(SeekFrom::Start(sector.file_offset)) + .map_err(VhdxIoError::ReadSectorBlock)?; + f.write_all( + &buf[write_count + ..(write_count + (sector.free_sectors * SECTOR_SIZE) as usize)], + ) + .map_err(VhdxIoError::ReadSectorBlock)?; + } + vhdx_bat::PAYLOAD_BLOCK_PARTIALLY_PRESENT => { + return Err(VhdxIoError::UnsupportedMode); + } + _ => { + return Err(VhdxIoError::InvalidBatEntryState); + } + }; + sector_count -= sector.free_sectors; + sector_index += sector.free_sectors; + write_count = sector.free_bytes as usize; + }; + } + Ok(write_count) +} diff --git a/vendor/cube-hypervisor/vhdx/src/vhdx_metadata.rs b/vendor/cube-hypervisor/vhdx/src/vhdx_metadata.rs new file mode 100644 index 0000000..660bb8f --- /dev/null +++ b/vendor/cube-hypervisor/vhdx/src/vhdx_metadata.rs @@ -0,0 +1,319 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use crate::vhdx_header::RegionTableEntry; +use byteorder::{LittleEndian, ReadBytesExt}; +use remain::sorted; +use std::fs::File; +use std::io::{self, Read, Seek, SeekFrom}; +use std::mem::size_of; +use thiserror::Error; +use uuid::Uuid; + +const METADATA_SIGN: u64 = 0x6174_6164_6174_656D; +const METADATA_ENTRY_SIZE: usize = 32; +const METADATA_MAX_ENTRIES: u16 = 2047; +// The size including the table header and entries +const METADATA_TABLE_MAX_SIZE: usize = METADATA_ENTRY_SIZE * (METADATA_MAX_ENTRIES as usize + 1); + +const METADATA_FLAGS_IS_REQUIRED: u32 = 0x04; + +pub const BLOCK_SIZE_MIN: u32 = 1 << 20; // 1 MiB +const BLOCK_SIZE_MAX: u32 = 256 << 20; // 256 MiB +const MAX_SECTORS_PER_BLOCK: u64 = 1 << 23; + +const BLOCK_HAS_PARENT: u32 = 0x02; // Has a parent or a backing file + +// GUID for known metadata items +const METADATA_FILE_PARAMETER: &str = "CAA16737-FA36-4D43-B3B6-33F0AA44E76B"; +const METADATA_VIRTUAL_DISK_SIZE: &str = "2FA54224-CD1B-4876-B211-5DBED83BF4B8"; +const METADATA_VIRTUAL_DISK_ID: &str = "BECA12AB-B2E6-4523-93EF-C309E000C746"; +const METADATA_LOGICAL_SECTOR_SIZE: &str = "8141BF1D-A96F-4709-BA47-F233A8FAAB5F"; +const METADATA_PHYSICAL_SECTOR_SIZE: &str = "CDA348C7-445D-4471-9CC9-E9885251C556"; +const METADATA_PARENT_LOCATOR: &str = "A8D35F2D-B30B-454D-ABF7-D3D84834AB0C"; + +const METADATA_FILE_PARAMETER_PRESENT: u16 = 0x01; +const METADATA_VIRTUAL_DISK_SIZE_PRESENT: u16 = 0x02; +const METADATA_VIRTUAL_DISK_ID_PRESENT: u16 = 0x04; +const METADATA_LOGICAL_SECTOR_SIZE_PRESENT: u16 = 0x08; +const METADATA_PHYSICAL_SECTOR_SIZE_PRESENT: u16 = 0x10; +const METADATA_PARENT_LOCATOR_PRESENT: u16 = 0x20; + +const METADATA_ALL_PRESENT: u16 = METADATA_FILE_PARAMETER_PRESENT + | METADATA_VIRTUAL_DISK_SIZE_PRESENT + | METADATA_VIRTUAL_DISK_ID_PRESENT + | METADATA_LOGICAL_SECTOR_SIZE_PRESENT + | METADATA_PHYSICAL_SECTOR_SIZE_PRESENT; + +const METADATA_LENGTH_MAX: u32 = 1 << 20; // 1 MiB + +#[sorted] +#[derive(Error, Debug)] +pub enum VhdxMetadataError { + #[error("Invalid block size count")] + InvalidBlockSize, + #[error("Invalid metadata entry count")] + InvalidEntryCount, + #[error("Invalid logical sector size")] + InvalidLogicalSectorSize, + #[error("Invalid metadata ID")] + InvalidMetadataItem, + #[error("Invalid metadata length")] + InvalidMetadataLength, + #[error("Metadata sign doesn't match")] + InvalidMetadataSign, + #[error("Invalid physical sector size")] + InvalidPhysicalSectorSize, + #[error("Invalid UUID")] + InvalidUuid(#[source] uuid::Error), + #[error("Invalid value")] + InvalidValue, + #[error("Not all required metadata found")] + MissingMetadata, + #[error("Failed to read metadata headers {0}")] + ReadMetadata(#[source] io::Error), + #[error("Reserved region has non-zero value")] + ReservedIsNonZero, + #[error("This implementation doesn't support this metadata flag")] + UnsupportedFlag, +} + +pub type Result = std::result::Result; + +#[derive(Default, Clone, Debug)] +pub struct DiskSpec { + pub disk_id: u128, + pub image_size: u64, + pub block_size: u32, + pub has_parent: bool, + pub sectors_per_block: u32, + pub virtual_disk_size: u64, + pub logical_sector_size: u32, + pub physical_sector_size: u32, + pub chunk_ratio: u64, + pub total_sectors: u64, +} + +impl DiskSpec { + /// Parse all meatadata from the provided file and store info in DiskSpec + /// structure. + pub fn new(f: &mut File, metadata_region: &RegionTableEntry) -> Result { + let mut disk_spec = DiskSpec::default(); + let mut metadata_presence: u16 = 0; + let mut offset = 0; + let metadata = f.metadata().map_err(VhdxMetadataError::ReadMetadata)?; + disk_spec.image_size = metadata.len(); + + let mut buffer = [0u8; METADATA_TABLE_MAX_SIZE]; + f.seek(SeekFrom::Start(metadata_region.file_offset)) + .map_err(VhdxMetadataError::ReadMetadata)?; + f.read_exact(&mut buffer) + .map_err(VhdxMetadataError::ReadMetadata)?; + + let metadata_header = + MetadataTableHeader::new(&buffer[0..size_of::()])?; + + offset += size_of::(); + for _ in 0..metadata_header.entry_count { + let metadata_entry = + MetadataTableEntry::new(&buffer[offset..offset + size_of::()])?; + + f.seek(SeekFrom::Start( + metadata_region.file_offset + metadata_entry.offset as u64, + )) + .map_err(VhdxMetadataError::ReadMetadata)?; + + if metadata_entry.item_id + == Uuid::parse_str(METADATA_FILE_PARAMETER) + .map_err(VhdxMetadataError::InvalidUuid)? + { + disk_spec.block_size = f + .read_u32::() + .map_err(VhdxMetadataError::ReadMetadata)?; + + // MUST be at least 1 MiB and not greater than 256 MiB + if disk_spec.block_size < BLOCK_SIZE_MIN && disk_spec.block_size > BLOCK_SIZE_MAX { + return Err(VhdxMetadataError::InvalidBlockSize); + } + + // MUST be power of 2 + if !disk_spec.block_size.is_power_of_two() { + return Err(VhdxMetadataError::InvalidBlockSize); + } + + let bits = f + .read_u32::() + .map_err(VhdxMetadataError::ReadMetadata)?; + disk_spec.has_parent = bits & BLOCK_HAS_PARENT != 0; + + metadata_presence |= METADATA_FILE_PARAMETER_PRESENT; + } else if metadata_entry.item_id + == Uuid::parse_str(METADATA_VIRTUAL_DISK_SIZE) + .map_err(VhdxMetadataError::InvalidUuid)? + { + disk_spec.virtual_disk_size = f + .read_u64::() + .map_err(VhdxMetadataError::ReadMetadata)?; + + metadata_presence |= METADATA_VIRTUAL_DISK_SIZE_PRESENT; + } else if metadata_entry.item_id + == Uuid::parse_str(METADATA_VIRTUAL_DISK_ID) + .map_err(VhdxMetadataError::InvalidUuid)? + { + disk_spec.disk_id = f + .read_u128::() + .map_err(VhdxMetadataError::ReadMetadata)?; + + metadata_presence |= METADATA_VIRTUAL_DISK_ID_PRESENT; + } else if metadata_entry.item_id + == Uuid::parse_str(METADATA_LOGICAL_SECTOR_SIZE) + .map_err(VhdxMetadataError::InvalidUuid)? + { + disk_spec.logical_sector_size = f + .read_u32::() + .map_err(VhdxMetadataError::ReadMetadata)?; + if !(disk_spec.logical_sector_size == 512 || disk_spec.logical_sector_size == 4096) + { + return Err(VhdxMetadataError::InvalidLogicalSectorSize); + } + + metadata_presence |= METADATA_LOGICAL_SECTOR_SIZE_PRESENT; + } else if metadata_entry.item_id + == Uuid::parse_str(METADATA_PHYSICAL_SECTOR_SIZE) + .map_err(VhdxMetadataError::InvalidUuid)? + { + disk_spec.physical_sector_size = f + .read_u32::() + .map_err(VhdxMetadataError::ReadMetadata)?; + if !(disk_spec.physical_sector_size == 512 + || disk_spec.physical_sector_size == 4096) + { + return Err(VhdxMetadataError::InvalidPhysicalSectorSize); + } + + metadata_presence |= METADATA_PHYSICAL_SECTOR_SIZE_PRESENT; + } else if metadata_entry.item_id + == Uuid::parse_str(METADATA_PARENT_LOCATOR) + .map_err(VhdxMetadataError::InvalidUuid)? + { + metadata_presence |= METADATA_PARENT_LOCATOR_PRESENT; + } else { + return Err(VhdxMetadataError::InvalidMetadataItem); + } + + if (metadata_entry.flag_bits & METADATA_FLAGS_IS_REQUIRED) == 0 { + return Err(VhdxMetadataError::UnsupportedFlag); + } + offset += size_of::(); + } + + // Check if all required metadata are present + if metadata_presence != METADATA_ALL_PRESENT { + return Err(VhdxMetadataError::MissingMetadata); + } + // Check if the virtual disk size is a multiple of the logical sector + // size. + if ((metadata_presence & METADATA_LOGICAL_SECTOR_SIZE_PRESENT) != 0) + && (disk_spec.virtual_disk_size % disk_spec.logical_sector_size as u64 != 0) + { + return Err(VhdxMetadataError::InvalidBlockSize); + } + + disk_spec.sectors_per_block = + DiskSpec::sectors_per_block(disk_spec.block_size, disk_spec.logical_sector_size)?; + + disk_spec.chunk_ratio = + DiskSpec::chunk_ratio(disk_spec.block_size, disk_spec.logical_sector_size)?; + + disk_spec.total_sectors = + disk_spec.virtual_disk_size / disk_spec.logical_sector_size as u64; + + Ok(disk_spec) + } + + /// Calculates the number of sectors per block + fn sectors_per_block(block_size: u32, logical_sector_size: u32) -> Result { + let sectors_per_block = block_size / logical_sector_size; + + if !sectors_per_block.is_power_of_two() { + return Err(VhdxMetadataError::InvalidValue); + } + + Ok(sectors_per_block) + } + + /// Calculate the chunk ratio + fn chunk_ratio(block_size: u32, logical_sector_size: u32) -> Result { + let chunk_ratio = (MAX_SECTORS_PER_BLOCK * logical_sector_size as u64) / block_size as u64; + + if !chunk_ratio.is_power_of_two() { + return Err(VhdxMetadataError::InvalidValue); + } + + Ok(chunk_ratio) + } +} + +#[repr(packed)] +#[derive(Default, Debug, Clone, Copy)] +struct MetadataTableHeader { + signature: u64, + reserved: u16, + entry_count: u16, + _reserved2: [u8; 20], +} + +impl MetadataTableHeader { + pub fn new(buffer: &[u8]) -> Result { + let metadata_table_header = unsafe { *(buffer.as_ptr() as *mut MetadataTableHeader) }; + + if metadata_table_header.signature != METADATA_SIGN { + return Err(VhdxMetadataError::InvalidMetadataSign); + } + + if metadata_table_header.entry_count > METADATA_MAX_ENTRIES { + return Err(VhdxMetadataError::InvalidEntryCount); + } + + if metadata_table_header.reserved != 0 { + return Err(VhdxMetadataError::ReservedIsNonZero); + } + + Ok(metadata_table_header) + } +} + +#[repr(packed)] +#[derive(Default, Debug, Clone, Copy)] +pub struct MetadataTableEntry { + item_id: Uuid, + offset: u32, + length: u32, + flag_bits: u32, + reserved: u32, +} + +impl MetadataTableEntry { + /// Parse one metadata entry from the buffer + fn new(buffer: &[u8]) -> Result { + let mut metadata_table_entry = unsafe { *(buffer.as_ptr() as *mut MetadataTableEntry) }; + + let uuid = crate::uuid_from_guid(buffer); + metadata_table_entry.item_id = uuid; + + if metadata_table_entry.length > METADATA_LENGTH_MAX { + return Err(VhdxMetadataError::InvalidMetadataLength); + } + + if metadata_table_entry.length == 0 && metadata_table_entry.offset != 0 { + return Err(VhdxMetadataError::InvalidMetadataLength); + } + + if metadata_table_entry.reserved != 0 { + return Err(VhdxMetadataError::ReservedIsNonZero); + } + + Ok(metadata_table_entry) + } +} diff --git a/vendor/cube-hypervisor/vhost_user_block/Cargo.toml b/vendor/cube-hypervisor/vhost_user_block/Cargo.toml new file mode 100644 index 0000000..203886e --- /dev/null +++ b/vendor/cube-hypervisor/vhost_user_block/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "vhost_user_block" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[dependencies] +block_util = { path = "../block_util" } +clap = { version = "4.4.7", features = ["wrap_help", "cargo"] } +env_logger = "0.10.2" +epoll = "4.3.1" +libc = "0.2.137" +log = "0.4.17" +option_parser = { path = "../option_parser" } +qcow = { path = "../qcow" } +vhost = { workspace = true, features = ["vhost-user-backend"] } +vhost-user-backend = { workspace = true } +virtio-bindings = { workspace = true } +virtio-queue = { workspace = true } +vm-memory = { workspace = true } +vmm-sys-util = { workspace = true } + +[build-dependencies] +clap = { version = "4.4.7", features = ["cargo"] } diff --git a/vendor/cube-hypervisor/vhost_user_block/src/lib.rs b/vendor/cube-hypervisor/vhost_user_block/src/lib.rs new file mode 100644 index 0000000..a9cfc3f --- /dev/null +++ b/vendor/cube-hypervisor/vhost_user_block/src/lib.rs @@ -0,0 +1,556 @@ +// Copyright 2019 Red Hat, Inc. All Rights Reserved. +// +// Portions Copyright 2019 Intel Corporation. All Rights Reserved. +// +// Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// +// SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause) + +#![allow(clippy::significant_drop_in_scrutinee)] + +use block_util::{build_disk_image_id, Request, VirtioBlockConfig}; +use libc::EFD_NONBLOCK; +use log::*; +use option_parser::{OptionParser, OptionParserError, Toggle}; +use qcow::{self, ImageType, QcowFile}; +use std::fs::File; +use std::fs::OpenOptions; +use std::io::Read; +use std::io::{Seek, SeekFrom, Write}; +use std::ops::Deref; +use std::ops::DerefMut; +use std::os::unix::fs::OpenOptionsExt; +use std::path::PathBuf; +use std::process; +use std::result; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex, RwLock, RwLockWriteGuard}; +use std::time::Instant; +use std::vec::Vec; +use std::{convert, error, fmt, io}; +use vhost::vhost_user::message::*; +use vhost::vhost_user::Listener; +use vhost_user_backend::{ + bitmap::BitmapMmapRegion, VhostUserBackendMut, VhostUserDaemon, VringRwLock, VringState, VringT, +}; +use virtio_bindings::bindings::virtio_blk::*; +use virtio_bindings::bindings::virtio_ring::VIRTIO_RING_F_EVENT_IDX; +use virtio_queue::QueueT; +use vm_memory::GuestAddressSpace; +use vm_memory::{ByteValued, Bytes, GuestMemoryAtomic}; +use vmm_sys_util::{epoll::EventSet, eventfd::EventFd}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +const SECTOR_SHIFT: u8 = 9; +const SECTOR_SIZE: u64 = 0x01 << SECTOR_SHIFT; +const BLK_SIZE: u32 = 512; +// Current (2020) enterprise SSDs have a latency lower than 30us. +// Polling for 50us should be enough to cover for the device latency +// and the overhead of the emulation layer. +const POLL_QUEUE_US: u128 = 50; + +trait DiskFile: Read + Seek + Write + Send {} +impl DiskFile for D {} + +type Result = std::result::Result; +type VhostUserBackendResult = std::result::Result; + +#[allow(dead_code)] +#[derive(Debug)] +enum Error { + /// Failed to create kill eventfd + CreateKillEventFd(io::Error), + /// Failed to parse configuration string + FailedConfigParse(OptionParserError), + /// Failed to handle event other than input event. + HandleEventNotEpollIn, + /// Failed to handle unknown event. + HandleEventUnknownEvent, + /// No path provided + PathParameterMissing, + /// No socket provided + SocketParameterMissing, +} + +pub const SYNTAX: &str = "vhost-user-block backend parameters \ + \"path=,socket=,num_queues=,\ + queue_size=,readonly=true|false,direct=true|false,\ + poll_queue=true|false\""; + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "vhost_user_block_error: {:?}", self) + } +} + +impl error::Error for Error {} + +impl convert::From for io::Error { + fn from(e: Error) -> Self { + io::Error::new(io::ErrorKind::Other, e) + } +} + +struct VhostUserBlkThread { + disk_image: Arc>, + disk_image_id: Vec, + disk_nsectors: u64, + event_idx: bool, + kill_evt: EventFd, + writeback: Arc, + mem: GuestMemoryAtomic, +} + +impl VhostUserBlkThread { + fn new( + disk_image: Arc>, + disk_image_id: Vec, + disk_nsectors: u64, + writeback: Arc, + mem: GuestMemoryAtomic, + ) -> Result { + Ok(VhostUserBlkThread { + disk_image, + disk_image_id, + disk_nsectors, + event_idx: false, + kill_evt: EventFd::new(EFD_NONBLOCK).map_err(Error::CreateKillEventFd)?, + writeback, + mem, + }) + } + + fn process_queue( + &mut self, + vring: &mut RwLockWriteGuard>>, + ) -> bool { + let mut used_descs = false; + + while let Some(mut desc_chain) = vring + .get_queue_mut() + .pop_descriptor_chain(self.mem.memory()) + { + debug!("got an element in the queue"); + let len; + match Request::parse(&mut desc_chain, None) { + Ok(mut request) => { + debug!("element is a valid request"); + request.set_writeback(self.writeback.load(Ordering::Acquire)); + let status = match request.execute( + &mut self.disk_image.lock().unwrap().deref_mut(), + self.disk_nsectors, + desc_chain.memory(), + &self.disk_image_id, + ) { + Ok(l) => { + len = l; + VIRTIO_BLK_S_OK + } + Err(e) => { + len = 1; + e.status() + } + }; + desc_chain + .memory() + .write_obj(status, request.status_addr) + .unwrap(); + } + Err(err) => { + error!("failed to parse available descriptor chain: {:?}", err); + len = 0; + } + } + + vring + .get_queue_mut() + .add_used(desc_chain.memory(), desc_chain.head_index(), len) + .unwrap(); + used_descs = true; + } + + let mut needs_signalling = false; + if self.event_idx { + if vring + .get_queue_mut() + .needs_notification(self.mem.memory().deref()) + .unwrap() + { + debug!("signalling queue"); + needs_signalling = true; + } else { + debug!("omitting signal (event_idx)"); + } + } else { + debug!("signalling queue"); + needs_signalling = true; + } + + if needs_signalling { + vring.signal_used_queue().unwrap(); + } + + used_descs + } +} + +struct VhostUserBlkBackend { + threads: Vec>, + config: VirtioBlockConfig, + rdonly: bool, + poll_queue: bool, + queues_per_thread: Vec, + queue_size: usize, + acked_features: u64, + writeback: Arc, + mem: GuestMemoryAtomic, +} + +impl VhostUserBlkBackend { + fn new( + image_path: String, + num_queues: usize, + rdonly: bool, + direct: bool, + poll_queue: bool, + queue_size: usize, + mem: GuestMemoryAtomic, + ) -> Result { + let mut options = OpenOptions::new(); + options.read(true); + options.write(!rdonly); + if direct { + options.custom_flags(libc::O_DIRECT); + } + let image: File = options.open(&image_path).unwrap(); + let mut raw_img: qcow::RawFile = qcow::RawFile::new(image, direct); + + let image_id = build_disk_image_id(&PathBuf::from(&image_path)); + let image_type = qcow::detect_image_type(&mut raw_img).unwrap(); + let image = match image_type { + ImageType::Raw => Arc::new(Mutex::new(raw_img)) as Arc>, + ImageType::Qcow2 => { + Arc::new(Mutex::new(QcowFile::from(raw_img).unwrap())) as Arc> + } + }; + + let nsectors = (image.lock().unwrap().seek(SeekFrom::End(0)).unwrap()) / SECTOR_SIZE; + let config = VirtioBlockConfig { + capacity: nsectors, + blk_size: BLK_SIZE, + size_max: 65535, + seg_max: 128 - 2, + min_io_size: 1, + opt_io_size: 1, + num_queues: num_queues as u16, + writeback: 1, + ..Default::default() + }; + + let mut queues_per_thread = Vec::new(); + let mut threads = Vec::new(); + let writeback = Arc::new(AtomicBool::new(true)); + for i in 0..num_queues { + let thread = Mutex::new(VhostUserBlkThread::new( + image.clone(), + image_id.clone(), + nsectors, + writeback.clone(), + mem.clone(), + )?); + threads.push(thread); + queues_per_thread.push(0b1 << i); + } + + Ok(VhostUserBlkBackend { + threads, + config, + rdonly, + poll_queue, + queues_per_thread, + queue_size, + acked_features: 0, + writeback, + mem, + }) + } + + fn update_writeback(&mut self) { + // Use writeback from config if VIRTIO_BLK_F_CONFIG_WCE + let writeback = + if self.acked_features & 1 << VIRTIO_BLK_F_CONFIG_WCE == 1 << VIRTIO_BLK_F_CONFIG_WCE { + self.config.writeback == 1 + } else { + // Else check if VIRTIO_BLK_F_FLUSH negotiated + self.acked_features & 1 << VIRTIO_BLK_F_FLUSH == 1 << VIRTIO_BLK_F_FLUSH + }; + + info!( + "Changing cache mode to {}", + if writeback { + "writeback" + } else { + "writethrough" + } + ); + self.writeback.store(writeback, Ordering::Release); + } +} + +impl VhostUserBackendMut for VhostUserBlkBackend { + type Bitmap = BitmapMmapRegion; + type Vring = VringRwLock>; + fn num_queues(&self) -> usize { + self.config.num_queues as usize + } + + fn max_queue_size(&self) -> usize { + self.queue_size + } + + fn features(&self) -> u64 { + let mut avail_features = 1 << VIRTIO_BLK_F_SEG_MAX + | 1 << VIRTIO_BLK_F_BLK_SIZE + | 1 << VIRTIO_BLK_F_FLUSH + | 1 << VIRTIO_BLK_F_TOPOLOGY + | 1 << VIRTIO_BLK_F_MQ + | 1 << VIRTIO_BLK_F_CONFIG_WCE + | 1 << VIRTIO_RING_F_EVENT_IDX + | 1 << VIRTIO_F_VERSION_1 + | VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits(); + + if self.rdonly { + avail_features |= 1 << VIRTIO_BLK_F_RO; + } + avail_features + } + + fn acked_features(&mut self, features: u64) { + self.acked_features = features; + self.update_writeback(); + } + + fn protocol_features(&self) -> VhostUserProtocolFeatures { + VhostUserProtocolFeatures::CONFIG + | VhostUserProtocolFeatures::MQ + | VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS + } + + fn set_event_idx(&mut self, enabled: bool) { + for thread in self.threads.iter() { + thread.lock().unwrap().event_idx = enabled; + } + } + + fn handle_event( + &mut self, + device_event: u16, + evset: EventSet, + vrings: &[VringRwLock>], + thread_id: usize, + ) -> VhostUserBackendResult<()> { + if evset != EventSet::IN { + return Err(Error::HandleEventNotEpollIn.into()); + } + + debug!("event received: {:?}", device_event); + + let mut thread = self.threads[thread_id].lock().unwrap(); + match device_event { + 0 => { + let mut vring = vrings[0].get_mut(); + + if self.poll_queue { + // Actively poll the queue until POLL_QUEUE_US has passed + // without seeing a new request. + let mut now = Instant::now(); + loop { + if thread.process_queue(&mut vring) { + now = Instant::now(); + } else if now.elapsed().as_micros() > POLL_QUEUE_US { + break; + } + } + } + + if thread.event_idx { + // vm-virtio's Queue implementation only checks avail_index + // once, so to properly support EVENT_IDX we need to keep + // calling process_queue() until it stops finding new + // requests on the queue. + loop { + vring + .get_queue_mut() + .enable_notification(self.mem.memory().deref()) + .unwrap(); + if !thread.process_queue(&mut vring) { + break; + } + } + } else { + // Without EVENT_IDX, a single call is enough. + thread.process_queue(&mut vring); + } + + Ok(()) + } + _ => Err(Error::HandleEventUnknownEvent.into()), + } + } + + fn get_config(&self, _offset: u32, _size: u32) -> Vec { + self.config.as_slice().to_vec() + } + + fn set_config(&mut self, offset: u32, data: &[u8]) -> result::Result<(), io::Error> { + let config_slice = self.config.as_mut_slice(); + let data_len = data.len() as u32; + let config_len = config_slice.len() as u32; + if offset + data_len > config_len { + error!("Failed to write config space"); + return Err(io::Error::from_raw_os_error(libc::EINVAL)); + } + let (_, right) = config_slice.split_at_mut(offset as usize); + right.copy_from_slice(data); + self.update_writeback(); + Ok(()) + } + + fn exit_event(&self, thread_index: usize) -> Option { + Some( + self.threads[thread_index] + .lock() + .unwrap() + .kill_evt + .try_clone() + .unwrap(), + ) + } + + fn queues_per_thread(&self) -> Vec { + self.queues_per_thread.clone() + } + + fn update_memory( + &mut self, + _mem: GuestMemoryAtomic, + ) -> VhostUserBackendResult<()> { + Ok(()) + } +} + +struct VhostUserBlkBackendConfig { + path: String, + socket: String, + num_queues: usize, + queue_size: usize, + readonly: bool, + direct: bool, + poll_queue: bool, +} + +impl VhostUserBlkBackendConfig { + fn parse(backend: &str) -> Result { + let mut parser = OptionParser::new(); + parser + .add("path") + .add("readonly") + .add("direct") + .add("num_queues") + .add("queue_size") + .add("socket") + .add("poll_queue"); + parser.parse(backend).map_err(Error::FailedConfigParse)?; + + let path = parser.get("path").ok_or(Error::PathParameterMissing)?; + let readonly = parser + .convert::("readonly") + .map_err(Error::FailedConfigParse)? + .unwrap_or(Toggle(false)) + .0; + let direct = parser + .convert::("direct") + .map_err(Error::FailedConfigParse)? + .unwrap_or(Toggle(false)) + .0; + let num_queues = parser + .convert("num_queues") + .map_err(Error::FailedConfigParse)? + .unwrap_or(1); + let socket = parser.get("socket").ok_or(Error::SocketParameterMissing)?; + let poll_queue = parser + .convert::("poll_queue") + .map_err(Error::FailedConfigParse)? + .unwrap_or(Toggle(true)) + .0; + let queue_size = parser + .convert("queue_size") + .map_err(Error::FailedConfigParse)? + .unwrap_or(1024); + + Ok(VhostUserBlkBackendConfig { + path, + socket, + num_queues, + queue_size, + readonly, + direct, + poll_queue, + }) + } +} + +pub fn start_block_backend(backend_command: &str) { + let backend_config = match VhostUserBlkBackendConfig::parse(backend_command) { + Ok(config) => config, + Err(e) => { + println!("Failed parsing parameters {:?}", e); + process::exit(1); + } + }; + + let mem = GuestMemoryAtomic::new(GuestMemoryMmap::new()); + + let blk_backend = Arc::new(RwLock::new( + VhostUserBlkBackend::new( + backend_config.path, + backend_config.num_queues, + backend_config.readonly, + backend_config.direct, + backend_config.poll_queue, + backend_config.queue_size, + mem.clone(), + ) + .unwrap(), + )); + + debug!("blk_backend is created!\n"); + + let listener = Listener::new(&backend_config.socket, true).unwrap(); + + let name = "vhost-user-blk-backend"; + let mut blk_daemon = VhostUserDaemon::new(name.to_string(), blk_backend.clone(), mem).unwrap(); + + debug!("blk_daemon is created!\n"); + + if let Err(e) = blk_daemon.start(listener) { + error!( + "Failed to start daemon for vhost-user-block with error: {:?}\n", + e + ); + process::exit(1); + } + + if let Err(e) = blk_daemon.wait() { + error!("Error from the main thread: {:?}", e); + } + + for thread in blk_backend.read().unwrap().threads.iter() { + if let Err(e) = thread.lock().unwrap().kill_evt.write(1) { + error!("Error shutting down worker thread: {:?}", e) + } + } +} diff --git a/vendor/cube-hypervisor/vhost_user_block/src/main.rs b/vendor/cube-hypervisor/vhost_user_block/src/main.rs new file mode 100644 index 0000000..9f03aab --- /dev/null +++ b/vendor/cube-hypervisor/vhost_user_block/src/main.rs @@ -0,0 +1,36 @@ +// Copyright 2019 Red Hat, Inc. All Rights Reserved. +// +// Portions Copyright 2019 Intel Corporation. All Rights Reserved. +// +// Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// +// SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause) + +#[macro_use(crate_version, crate_authors)] +extern crate clap; +extern crate vhost_user_block; + +use clap::{Arg, Command}; +use vhost_user_block::start_block_backend; + +fn main() { + env_logger::init(); + + let cmd_arguments = Command::new("vhost-user-blk backend") + .version(crate_version!()) + .author(crate_authors!()) + .about("Launch a vhost-user-blk backend.") + .arg( + Arg::new("block-backend") + .long("block-backend") + .help(vhost_user_block::SYNTAX) + .num_args(1) + .required(true), + ) + .get_matches(); + + let backend_command = cmd_arguments.get_one::("block-backend").unwrap(); + start_block_backend(backend_command); +} diff --git a/vendor/cube-hypervisor/vhost_user_net/Cargo.toml b/vendor/cube-hypervisor/vhost_user_net/Cargo.toml new file mode 100644 index 0000000..4a4a776 --- /dev/null +++ b/vendor/cube-hypervisor/vhost_user_net/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "vhost_user_net" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[dependencies] +clap = { version = "4.4.7", features = ["wrap_help", "cargo"] } +env_logger = "0.10.2" +epoll = "4.3.1" +libc = "0.2.137" +log = "0.4.17" +net_util = { path = "../net_util" } +option_parser = { path = "../option_parser" } +vhost = { workspace = true, features = ["vhost-user-backend"] } +vhost-user-backend = { workspace = true } +virtio-bindings = { workspace = true } +vm-memory = { workspace = true } +vmm-sys-util = { workspace = true } + +[build-dependencies] +clap = { version = "4.4.7", features = ["cargo"] } diff --git a/vendor/cube-hypervisor/vhost_user_net/src/lib.rs b/vendor/cube-hypervisor/vhost_user_net/src/lib.rs new file mode 100644 index 0000000..1c396d2 --- /dev/null +++ b/vendor/cube-hypervisor/vhost_user_net/src/lib.rs @@ -0,0 +1,420 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// +// Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// +// SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause) + +#![allow(clippy::significant_drop_in_scrutinee)] + +use libc::{self, EFD_NONBLOCK}; +use log::*; +use net_util::{ + open_tap, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, Tap, TxVirtio, +}; +use option_parser::Toggle; +use option_parser::{OptionParser, OptionParserError}; +use std::fmt; +use std::io; +use std::net::Ipv4Addr; +use std::ops::Deref; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::process; +use std::sync::{Arc, Mutex, RwLock}; +use std::vec::Vec; +use vhost::vhost_user::message::*; +use vhost::vhost_user::Listener; +use vhost_user_backend::bitmap::BitmapMmapRegion; +use vhost_user_backend::{VhostUserBackendMut, VhostUserDaemon, VringRwLock, VringT}; +use virtio_bindings::bindings::virtio_net::*; +use vm_memory::GuestAddressSpace; +use vm_memory::GuestMemoryAtomic; +use vmm_sys_util::{epoll::EventSet, eventfd::EventFd}; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +pub type Result = std::result::Result; +type VhostUserBackendResult = std::result::Result; + +#[derive(Debug)] +pub enum Error { + /// Failed to create kill eventfd. + CreateKillEventFd(io::Error), + /// Failed to parse configuration string. + FailedConfigParse(OptionParserError), + /// Failed to signal used queue. + FailedSignalingUsedQueue(io::Error), + /// Failed to handle event other than input event. + HandleEventNotEpollIn, + /// Failed to handle unknown event. + HandleEventUnknownEvent, + /// Failed to open tap device. + OpenTap(OpenTapError), + /// No socket provided. + SocketParameterMissing, + /// Underlying QueuePair error. + NetQueuePair(net_util::NetQueuePairError), + /// Failed to register the TAP listener. + RegisterTapListener(io::Error), +} + +pub const SYNTAX: &str = "vhost-user-net backend parameters \ +\"ip=,mask=,socket=,client=on|off,\ +num_queues=,queue_size=,tap=\""; + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "vhost_user_net_error: {:?}", self) + } +} + +impl std::error::Error for Error {} + +impl std::convert::From for std::io::Error { + fn from(e: Error) -> Self { + std::io::Error::new(io::ErrorKind::Other, e) + } +} + +struct VhostUserNetThread { + net: NetQueuePair, + kill_evt: EventFd, +} + +impl VhostUserNetThread { + /// Create a new virtio network device with the given TAP interface. + fn new(tap: Tap) -> Result { + Ok(VhostUserNetThread { + kill_evt: EventFd::new(EFD_NONBLOCK).map_err(Error::CreateKillEventFd)?, + net: NetQueuePair { + tap_for_write_epoll: tap.clone(), + tap, + rx: RxVirtio::new(), + tx: TxVirtio::new(), + rx_tap_listening: false, + tx_tap_listening: false, + epoll_fd: None, + counters: NetCounters::default(), + tap_rx_event_id: 3, + tap_tx_event_id: 4, + rx_desc_avail: false, + rx_rate_limiter: None, + tx_rate_limiter: None, + access_platform: None, + rx_rate_limited: std::sync::Once::new(), + tx_rate_limited: std::sync::Once::new(), + id: "".to_string(), + }, + }) + } + + pub fn set_epoll_fd(&mut self, fd: RawFd) { + self.net.epoll_fd = Some(fd); + } +} + +pub struct VhostUserNetBackend { + threads: Vec>, + num_queues: usize, + queue_size: u16, + queues_per_thread: Vec, + mem: GuestMemoryAtomic, +} + +impl VhostUserNetBackend { + #[allow(clippy::too_many_arguments)] + fn new( + ip_addr: Ipv4Addr, + host_mac: MacAddr, + netmask: Ipv4Addr, + mtu: Option, + num_queues: usize, + queue_size: u16, + ifname: Option<&str>, + mem: GuestMemoryAtomic, + ) -> Result { + let mut taps = open_tap( + ifname, + Some(ip_addr), + Some(netmask), + &mut Some(host_mac), + mtu, + num_queues / 2, + None, + None, + ) + .map_err(Error::OpenTap)?; + + let mut queues_per_thread = Vec::new(); + let mut threads = Vec::new(); + for (i, tap) in taps.drain(..).enumerate() { + let thread = Mutex::new(VhostUserNetThread::new(tap)?); + threads.push(thread); + queues_per_thread.push(0b11 << (i * 2)); + } + + Ok(VhostUserNetBackend { + threads, + num_queues, + queue_size, + queues_per_thread, + mem, + }) + } +} + +impl VhostUserBackendMut for VhostUserNetBackend { + type Bitmap = BitmapMmapRegion; + type Vring = VringRwLock>; + fn num_queues(&self) -> usize { + self.num_queues + } + + fn max_queue_size(&self) -> usize { + self.queue_size as usize + } + + fn features(&self) -> u64 { + 1 << VIRTIO_NET_F_GUEST_CSUM + | 1 << VIRTIO_NET_F_CSUM + | 1 << VIRTIO_NET_F_GUEST_TSO4 + | 1 << VIRTIO_NET_F_GUEST_TSO6 + | 1 << VIRTIO_NET_F_GUEST_ECN + | 1 << VIRTIO_NET_F_GUEST_UFO + | 1 << VIRTIO_NET_F_HOST_TSO4 + | 1 << VIRTIO_NET_F_HOST_TSO6 + | 1 << VIRTIO_NET_F_HOST_ECN + | 1 << VIRTIO_NET_F_HOST_UFO + | 1 << VIRTIO_NET_F_CTRL_VQ + | 1 << VIRTIO_NET_F_MQ + | 1 << VIRTIO_NET_F_MAC + | 1 << VIRTIO_NET_F_MTU + | 1 << VIRTIO_F_NOTIFY_ON_EMPTY + | 1 << VIRTIO_F_VERSION_1 + | VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits() + } + + fn protocol_features(&self) -> VhostUserProtocolFeatures { + VhostUserProtocolFeatures::MQ + | VhostUserProtocolFeatures::REPLY_ACK + | VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS + } + + fn set_event_idx(&mut self, _enabled: bool) {} + + fn handle_event( + &mut self, + device_event: u16, + _evset: EventSet, + vrings: &[VringRwLock>], + thread_id: usize, + ) -> VhostUserBackendResult<()> { + let mut thread = self.threads[thread_id].lock().unwrap(); + match device_event { + 0 => { + if !thread.net.rx_tap_listening { + net_util::register_listener( + thread.net.epoll_fd.unwrap(), + thread.net.tap.as_raw_fd(), + epoll::Events::EPOLLIN, + u64::from(thread.net.tap_rx_event_id), + ) + .map_err(Error::RegisterTapListener)?; + thread.net.rx_tap_listening = true; + } + } + 1 | 4 => { + let mut vring = vrings[1].get_mut(); + if thread + .net + .process_tx(self.mem.memory().deref(), vring.get_queue_mut()) + .map_err(Error::NetQueuePair)? + { + vring + .signal_used_queue() + .map_err(Error::FailedSignalingUsedQueue)? + } + } + 3 => { + let mut vring = vrings[0].get_mut(); + if thread + .net + .process_rx(self.mem.memory().deref(), vring.get_queue_mut()) + .map_err(Error::NetQueuePair)? + { + vring + .signal_used_queue() + .map_err(Error::FailedSignalingUsedQueue)? + } + } + _ => return Err(Error::HandleEventUnknownEvent.into()), + } + + Ok(()) + } + + fn exit_event(&self, thread_index: usize) -> Option { + Some( + self.threads[thread_index] + .lock() + .unwrap() + .kill_evt + .try_clone() + .unwrap(), + ) + } + + fn queues_per_thread(&self) -> Vec { + self.queues_per_thread.clone() + } + + fn update_memory( + &mut self, + _mem: GuestMemoryAtomic, + ) -> VhostUserBackendResult<()> { + Ok(()) + } +} + +pub struct VhostUserNetBackendConfig { + pub ip: Ipv4Addr, + pub host_mac: MacAddr, + pub mask: Ipv4Addr, + pub mtu: Option, + pub socket: String, + pub num_queues: usize, + pub queue_size: u16, + pub tap: Option, + pub client: bool, +} + +impl VhostUserNetBackendConfig { + pub fn parse(backend: &str) -> Result { + let mut parser = OptionParser::new(); + + parser + .add("tap") + .add("ip") + .add("host_mac") + .add("mask") + .add("mtu") + .add("queue_size") + .add("num_queues") + .add("socket") + .add("client"); + + parser.parse(backend).map_err(Error::FailedConfigParse)?; + + let tap = parser.get("tap"); + let ip = parser + .convert("ip") + .map_err(Error::FailedConfigParse)? + .unwrap_or_else(|| Ipv4Addr::new(192, 168, 100, 1)); + let host_mac = parser + .convert("host_mac") + .map_err(Error::FailedConfigParse)? + .unwrap_or_else(MacAddr::local_random); + let mask = parser + .convert("mask") + .map_err(Error::FailedConfigParse)? + .unwrap_or_else(|| Ipv4Addr::new(255, 255, 255, 0)); + let mtu = parser.convert("mtu").map_err(Error::FailedConfigParse)?; + let queue_size = parser + .convert("queue_size") + .map_err(Error::FailedConfigParse)? + .unwrap_or(256); + let num_queues = parser + .convert("num_queues") + .map_err(Error::FailedConfigParse)? + .unwrap_or(2); + let socket = parser.get("socket").ok_or(Error::SocketParameterMissing)?; + let client = parser + .convert::("client") + .map_err(Error::FailedConfigParse)? + .unwrap_or(Toggle(false)) + .0; + + Ok(VhostUserNetBackendConfig { + ip, + host_mac, + mask, + mtu, + socket, + num_queues, + queue_size, + tap, + client, + }) + } +} + +pub fn start_net_backend(backend_command: &str) { + let backend_config = match VhostUserNetBackendConfig::parse(backend_command) { + Ok(config) => config, + Err(e) => { + eprintln!("Failed parsing parameters {:?}", e); + process::exit(1); + } + }; + + let tap = backend_config.tap.as_deref(); + + let mem = GuestMemoryAtomic::new(GuestMemoryMmap::new()); + + let net_backend = Arc::new(RwLock::new( + VhostUserNetBackend::new( + backend_config.ip, + backend_config.host_mac, + backend_config.mask, + backend_config.mtu, + backend_config.num_queues, + backend_config.queue_size, + tap, + mem.clone(), + ) + .unwrap(), + )); + + let mut net_daemon = VhostUserDaemon::new( + "vhost-user-net-backend".to_string(), + net_backend.clone(), + mem, + ) + .unwrap(); + + let epoll_handlers = net_daemon.get_epoll_handlers(); + if epoll_handlers.len() != net_backend.read().unwrap().threads.len() { + error!("Number of vring workers must be identical to the number of backend threads"); + process::exit(1); + } + + for (index, thread) in net_backend.read().unwrap().threads.iter().enumerate() { + thread + .lock() + .unwrap() + .set_epoll_fd(epoll_handlers[index].as_raw_fd()); + } + + if let Err(e) = if backend_config.client { + net_daemon.start_client(&backend_config.socket) + } else { + net_daemon.start(Listener::new(&backend_config.socket, true).unwrap()) + } { + error!( + "failed to start daemon for vhost-user-net with error: {:?}", + e + ); + process::exit(1); + } + + if let Err(e) = net_daemon.wait() { + error!("Error from the main thread: {:?}", e); + } + + for thread in net_backend.read().unwrap().threads.iter() { + if let Err(e) = thread.lock().unwrap().kill_evt.write(1) { + error!("Error shutting down worker thread: {:?}", e) + } + } +} diff --git a/vendor/cube-hypervisor/vhost_user_net/src/main.rs b/vendor/cube-hypervisor/vhost_user_net/src/main.rs new file mode 100644 index 0000000..25aa29c --- /dev/null +++ b/vendor/cube-hypervisor/vhost_user_net/src/main.rs @@ -0,0 +1,33 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// +// Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// +// SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause) + +#[macro_use(crate_version, crate_authors)] +extern crate clap; + +use clap::{Arg, Command}; +use vhost_user_net::start_net_backend; + +fn main() { + env_logger::init(); + + let cmd_arguments = Command::new("vhost-user-net backend") + .version(crate_version!()) + .author(crate_authors!()) + .about("Launch a vhost-user-net backend.") + .arg( + Arg::new("net-backend") + .long("net-backend") + .help(vhost_user_net::SYNTAX) + .num_args(1) + .required(true), + ) + .get_matches(); + + let backend_command = cmd_arguments.get_one::("net-backend").unwrap(); + start_net_backend(backend_command); +} diff --git a/vendor/cube-hypervisor/virtio-devices/Cargo.toml b/vendor/cube-hypervisor/virtio-devices/Cargo.toml new file mode 100644 index 0000000..5b61edd --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/Cargo.toml @@ -0,0 +1,40 @@ +[package] +name = "virtio-devices" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[features] +default = [] + +[dependencies] +anyhow = "1.0.86" +arc-swap = "1.5.1" +block_util = { path = "../block_util" } +byteorder = "1.4.3" +epoll = "4.3.1" +event_monitor = { path = "../event_monitor" } +io-uring = "0.5.8" +libc = "0.2.137" +log = "0.4.17" +net_gen = { path = "../net_gen" } +net_util = { path = "../net_util" } +pci = { path = "../pci" } +rate_limiter = { path = "../rate_limiter" } +seccompiler = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = "1.0.87" +sendfd = "0.4.3" +serde_with = { version = "3.4.0", default-features = false, features = ["macros"] } +serial_buffer = { path = "../serial_buffer" } +thiserror = "1.0.37" +vhost = { workspace = true, features = ["vhost-user-frontend", "vhost-user-backend", "vhost-kern", "vhost-vdpa"] } +virtio-bindings = { version = "0.1.0", features = ["virtio-v5_0_0"] } +virtio-queue = { workspace = true } +vm-allocator = { path = "../vm-allocator" } +vm-device = { path = "../vm-device" } +vm-memory = { workspace = true, features = ["backend-mmap", "backend-atomic", "backend-bitmap"] } +vm-migration = { path = "../vm-migration" } +vm-virtio = { path = "../vm-virtio" } +vmm-sys-util = "0.12.1" +virtiofsd = { workspace = true } diff --git a/vendor/cube-hypervisor/virtio-devices/src/balloon.rs b/vendor/cube-hypervisor/virtio-devices/src/balloon.rs new file mode 100644 index 0000000..b0bd96b --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/balloon.rs @@ -0,0 +1,950 @@ +// Copyright (c) 2020 Ant Financial +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::{ + seccomp_filters::Thread, thread_helper::spawn_virtio_thread, ActivateResult, EpollHelper, + EpollHelperError, EpollHelperHandler, GuestMemoryMmap, VirtioCommon, VirtioDevice, + VirtioDeviceType, VirtioInterrupt, VirtioInterruptType, EPOLL_HELPER_EVENT_LAST, + VIRTIO_F_VERSION_1, +}; +use anyhow::anyhow; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::io::{self, Write}; +use std::mem::size_of; +use std::os::unix::io::AsRawFd; +use std::result; +use std::sync::{atomic::AtomicBool, Arc, Barrier}; +use thiserror::Error; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{ + Address, ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic, + GuestMemoryError, GuestMemoryRegion, +}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vm_virtio::checked_descriptor::DescriptorChainExt; +use vmm_sys_util::eventfd::EventFd; + +const QUEUE_SIZE: u16 = 128; +const REPORTING_QUEUE_SIZE: u16 = 32; +const MIN_NUM_QUEUES: usize = 2; + +// Inflate virtio queue event. +const INFLATE_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; +// Deflate virtio queue event. +const DEFLATE_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2; +// Reporting virtio queue event. +const REPORTING_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 3; + +// Size of a PFN in the balloon interface. +const VIRTIO_BALLOON_PFN_SHIFT: u64 = 12; + +// Upper bound on a single inflate or deflate descriptor length, in +// bytes. Matches the Linux driver, which submits at most +// VIRTIO_BALLOON_ARRAY_PFNS_MAX of 256 PFN entries of 4 bytes each per +// descriptor. +const VIRTIO_BALLOON_MAX_PFN_BYTES: u32 = 256 * 4; + +// Deflate balloon on OOM +const VIRTIO_BALLOON_F_DEFLATE_ON_OOM: u64 = 2; +// Enable an additional virtqueue to let the guest notify the host about free +// pages. +const VIRTIO_BALLOON_F_REPORTING: u64 = 5; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Guest gave us bad memory addresses.: {0}")] + GuestMemory(GuestMemoryError), + #[error("Madvise fail.: {0}")] + MadviseFail(std::io::Error), + #[error("Failed to EventFd write.: {0}")] + EventFdWriteFail(std::io::Error), + #[error("Invalid queue index: {0}")] + InvalidQueueIndex(usize), + #[error("Fail tp signal: {0}")] + FailedSignal(io::Error), + #[error("Failed adding used index: {0}")] + QueueAddUsed(virtio_queue::Error), + #[error("Failed creating an iterator over the queue: {0}")] + QueueIterator(virtio_queue::Error), +} + +// Got from include/uapi/linux/virtio_balloon.h +#[repr(C)] +#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] +pub struct VirtioBalloonConfig { + // Number of pages host wants Guest to give up. + num_pages: u32, + // Number of pages we've actually got in balloon. + actual: u32, +} + +const CONFIG_ACTUAL_OFFSET: u64 = 4; +const CONFIG_ACTUAL_SIZE: usize = 4; + +// SAFETY: it only has data and has no implicit padding. +unsafe impl ByteValued for VirtioBalloonConfig {} + +struct BalloonEpollHandler { + mem: GuestMemoryAtomic, + queues: Vec, + interrupt_cb: Arc, + inflate_queue_evt: EventFd, + deflate_queue_evt: EventFd, + reporting_queue_evt: Option, + kill_evt: EventFd, + pause_evt: EventFd, +} + +impl BalloonEpollHandler { + fn signal(&self, int_type: VirtioInterruptType) -> result::Result<(), Error> { + self.interrupt_cb.trigger(int_type).map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + Error::FailedSignal(e) + }) + } + + fn advise_memory_range( + memory: &GuestMemoryMmap, + range_base: GuestAddress, + range_len: usize, + advice: libc::c_int, + ) -> result::Result<(), Error> { + let hva = memory + .get_host_address(range_base) + .map_err(Error::GuestMemory)?; + // Need unsafe to do syscall madvise + let res = + unsafe { libc::madvise(hva as *mut libc::c_void, range_len as libc::size_t, advice) }; + if res != 0 { + return Err(Error::MadviseFail(io::Error::last_os_error())); + } + Ok(()) + } + + fn release_memory_range( + memory: &GuestMemoryMmap, + range_base: GuestAddress, + range_len: usize, + ) -> result::Result<(), Error> { + let region = memory.find_region(range_base).ok_or(Error::GuestMemory( + GuestMemoryError::InvalidGuestAddress(range_base), + ))?; + + // No underflow possible because range_base was found in the region by `find_region`. + let offset = range_base.0 - region.start_addr().0; + let region_limit = region.len() - offset; + let len = std::cmp::min(range_len as u64, region_limit); + if len < range_len as u64 { + warn!( + "Clamping reported range at GPA 0x{:x} from {} to {} bytes \ + to fit inside its memory region", + range_base.0, range_len, len + ); + } + if len == 0 { + return Ok(()); + } + + // Never punch a MAP_PRIVATE backing file: it can be an immutable snapshot or a + // base shared by multiple VMs. MADV_DONTNEED below discards this mapping's CoW pages. + if region.flags() & libc::MAP_SHARED == libc::MAP_SHARED { + if let Some(f_off) = region.file_offset() { + let res = unsafe { + libc::fallocate64( + f_off.file().as_raw_fd(), + libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_KEEP_SIZE, + (offset + f_off.start()) as libc::off64_t, + len as libc::off64_t, + ) + }; + + if res != 0 { + let error = io::Error::last_os_error(); + warn!( + "Failed to punch shared backing for reported range at GPA 0x{:x}: {error}; \ + falling back to MADV_DONTNEED", + range_base.0 + ); + } + } + } + + Self::advise_memory_range(memory, range_base, len as usize, libc::MADV_DONTNEED) + } + + fn process_queue(&mut self, queue_index: usize) -> result::Result<(), Error> { + let mut used_descs = false; + while let Some(mut desc_chain) = + self.queues[queue_index].pop_descriptor_chain(self.mem.memory()) + { + let desc = match desc_chain.next_checked(None) { + Ok(Some(desc)) => desc, + Ok(None) => { + warn!("Skipping empty balloon descriptor chain"); + self.queues[queue_index] + .add_used(desc_chain.memory(), desc_chain.head_index(), 0) + .map_err(Error::QueueAddUsed)?; + used_descs = true; + continue; + } + Err(addr) => { + warn!( + "Skipping balloon descriptor outside guest memory at 0x{:x}", + addr.0 + ); + self.queues[queue_index] + .add_used(desc_chain.memory(), desc_chain.head_index(), 0) + .map_err(Error::QueueAddUsed)?; + used_descs = true; + continue; + } + }; + + let data_chunk_size = size_of::(); + + if desc.is_write_only() { + warn!("Skipping device-writable descriptor on inflate/deflate queue"); + } else if desc.len() as usize % data_chunk_size != 0 { + warn!( + "Skipping descriptor with length {} not a multiple of {data_chunk_size}", + desc.len() + ); + } else if desc.len() > VIRTIO_BALLOON_MAX_PFN_BYTES { + warn!( + "Skipping descriptor with length {} exceeding cap {VIRTIO_BALLOON_MAX_PFN_BYTES}", + desc.len() + ); + } else { + let mut offset = 0u64; + while offset < desc.len() as u64 { + let Some(addr) = desc.addr().checked_add(offset) else { + warn!("Address overflow in balloon descriptor"); + break; + }; + let pfn: u32 = match desc_chain.memory().read_obj(addr) { + Ok(value) => value, + Err(e) => { + warn!("Failed to read PFN from descriptor: {e}"); + break; + } + }; + offset += data_chunk_size as u64; + + let range_base = GuestAddress((pfn as u64) << VIRTIO_BALLOON_PFN_SHIFT); + let range_len = 1 << VIRTIO_BALLOON_PFN_SHIFT; + + match queue_index { + 0 => { + if let Err(e) = Self::release_memory_range( + desc_chain.memory(), + range_base, + range_len, + ) { + warn!("Failed to release memory for PFN {pfn:#x}: {e}"); + } + } + 1 => { + if let Err(e) = Self::advise_memory_range( + desc_chain.memory(), + range_base, + range_len, + libc::MADV_WILLNEED, + ) { + warn!("Failed to advise memory for PFN {pfn:#x}: {e}"); + } + } + _ => return Err(Error::InvalidQueueIndex(queue_index)), + } + } + } + + self.queues[queue_index] + .add_used(desc_chain.memory(), desc_chain.head_index(), desc.len()) + .map_err(Error::QueueAddUsed)?; + used_descs = true; + } + + if used_descs { + self.signal(VirtioInterruptType::Queue(queue_index as u16)) + } else { + Ok(()) + } + } + + fn process_reporting_queue(&mut self, queue_index: usize) -> result::Result<(), Error> { + let mut used_descs = false; + while let Some(mut desc_chain) = + self.queues[queue_index].pop_descriptor_chain(self.mem.memory()) + { + let mut descs_len: u32 = 0; + let results: Vec<_> = desc_chain.checked_iter(None).collect(); + for result in results { + let desc = match result { + Ok(desc) => desc, + Err(_) => break, + }; + descs_len = descs_len.saturating_add(desc.len()); + if let Err(e) = Self::release_memory_range( + desc_chain.memory(), + desc.addr(), + desc.len() as usize, + ) { + warn!("Failed to release reported memory range: {e}"); + } + } + + self.queues[queue_index] + .add_used(desc_chain.memory(), desc_chain.head_index(), descs_len) + .map_err(Error::QueueAddUsed)?; + used_descs = true; + } + + if used_descs { + self.signal(VirtioInterruptType::Queue(queue_index as u16)) + } else { + Ok(()) + } + } + + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.inflate_queue_evt.as_raw_fd(), INFLATE_QUEUE_EVENT)?; + helper.add_event(self.deflate_queue_evt.as_raw_fd(), DEFLATE_QUEUE_EVENT)?; + if let Some(reporting_queue_evt) = self.reporting_queue_evt.as_ref() { + helper.add_event(reporting_queue_evt.as_raw_fd(), REPORTING_QUEUE_EVENT)?; + } + helper.run(paused, paused_sync, self)?; + + Ok(()) + } +} + +impl EpollHelperHandler for BalloonEpollHandler { + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + INFLATE_QUEUE_EVENT => { + self.inflate_queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to get inflate queue event: {:?}", + e + )) + })?; + self.process_queue(0).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used inflate queue: {:?}", + e + )) + })?; + } + DEFLATE_QUEUE_EVENT => { + self.deflate_queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to get deflate queue event: {:?}", + e + )) + })?; + self.process_queue(1).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used deflate queue: {:?}", + e + )) + })?; + } + REPORTING_QUEUE_EVENT => { + if let Some(reporting_queue_evt) = self.reporting_queue_evt.as_ref() { + reporting_queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to get reporting queue event: {:?}", + e + )) + })?; + self.process_reporting_queue(2).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used inflate queue: {:?}", + e + )) + })?; + } else { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Invalid reporting queue event as no eventfd registered" + ))); + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unknown event for virtio-balloon" + ))); + } + } + + Ok(()) + } +} + +#[derive(Serialize, Deserialize)] +pub struct BalloonState { + pub avail_features: u64, + pub acked_features: u64, + pub config: VirtioBalloonConfig, +} + +// Virtio device for exposing entropy to the guest OS through virtio. +pub struct Balloon { + common: VirtioCommon, + id: String, + config: VirtioBalloonConfig, + seccomp_action: SeccompAction, + exit_evt: EventFd, + interrupt_cb: Option>, +} + +impl Balloon { + // Create a new virtio-balloon. + pub fn new( + id: String, + size: u64, + deflate_on_oom: bool, + free_page_reporting: bool, + seccomp_action: SeccompAction, + exit_evt: EventFd, + state: Option, + ) -> io::Result { + let mut queue_sizes = vec![QUEUE_SIZE; MIN_NUM_QUEUES]; + + let (avail_features, acked_features, config) = if let Some(state) = state { + info!("Restoring virtio-balloon {}", id); + (state.avail_features, state.acked_features, state.config) + } else { + let mut avail_features = 1u64 << VIRTIO_F_VERSION_1; + if deflate_on_oom { + avail_features |= 1u64 << VIRTIO_BALLOON_F_DEFLATE_ON_OOM; + } + if free_page_reporting { + avail_features |= 1u64 << VIRTIO_BALLOON_F_REPORTING; + } + + let config = VirtioBalloonConfig { + num_pages: (size >> VIRTIO_BALLOON_PFN_SHIFT) as u32, + ..Default::default() + }; + + (avail_features, 0, config) + }; + + if avail_features & (1u64 << VIRTIO_BALLOON_F_REPORTING) != 0 { + queue_sizes.push(REPORTING_QUEUE_SIZE); + } + + Ok(Balloon { + common: VirtioCommon { + device_type: VirtioDeviceType::Balloon as u32, + avail_features, + acked_features, + paused_sync: Some(Arc::new(Barrier::new(2))), + queue_sizes, + min_queues: MIN_NUM_QUEUES as u16, + ..Default::default() + }, + id, + config, + seccomp_action, + exit_evt, + interrupt_cb: None, + }) + } + + pub fn resize(&mut self, size: u64) -> Result<(), Error> { + self.config.num_pages = (size >> VIRTIO_BALLOON_PFN_SHIFT) as u32; + + if let Some(interrupt_cb) = &self.interrupt_cb { + interrupt_cb + .trigger(VirtioInterruptType::Config) + .map_err(Error::FailedSignal) + } else { + Ok(()) + } + } + + // Get the actual size of the virtio-balloon. + pub fn get_actual(&self) -> u64 { + (self.config.actual as u64) << VIRTIO_BALLOON_PFN_SHIFT + } + + fn state(&self) -> BalloonState { + BalloonState { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + config: self.config, + } + } + + #[cfg(fuzzing)] + pub fn wait_for_epoll_threads(&mut self) { + self.common.wait_for_epoll_threads(); + } +} + +impl Drop for Balloon { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +impl VirtioDevice for Balloon { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.as_slice(), offset, data); + } + + fn write_config(&mut self, offset: u64, data: &[u8]) { + // The "actual" field is the only mutable field + if offset != CONFIG_ACTUAL_OFFSET || data.len() != CONFIG_ACTUAL_SIZE { + error!( + "Attempt to write to read-only field: offset {:x} length {}", + offset, + data.len() + ); + return; + } + + let config = self.config.as_mut_slice(); + let config_len = config.len() as u64; + let data_len = data.len() as u64; + if offset + data_len > config_len { + error!( + "Out-of-bound access to configuration: config_len = {} offset = {:x} length = {} for {}", + config_len, + offset, + data_len, + self.device_type() + ); + return; + } + + if let Some(end) = offset.checked_add(config.len() as u64) { + let mut offset_config = + &mut config[offset as usize..std::cmp::min(end, config_len) as usize]; + offset_config.write_all(data).unwrap(); + } + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let mut virtqueues = Vec::new(); + let (_, queue, queue_evt) = queues.remove(0); + virtqueues.push(queue); + let inflate_queue_evt = queue_evt; + let (_, queue, queue_evt) = queues.remove(0); + virtqueues.push(queue); + let deflate_queue_evt = queue_evt; + let reporting_queue_evt = + if self.common.feature_acked(VIRTIO_BALLOON_F_REPORTING) && !queues.is_empty() { + let (_, queue, queue_evt) = queues.remove(0); + virtqueues.push(queue); + Some(queue_evt) + } else { + None + }; + + self.interrupt_cb = Some(interrupt_cb.clone()); + + let mut handler = BalloonEpollHandler { + mem, + queues: virtqueues, + interrupt_cb, + inflate_queue_evt, + deflate_queue_evt, + reporting_queue_evt, + kill_evt, + pause_evt, + }; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + let mut epoll_threads = Vec::new(); + + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioBalloon, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + self.common.epoll_threads = Some(epoll_threads); + + event!("virtio-device", "activated", "id", &self.id); + Ok(()) + } + + fn reset(&mut self) -> Option> { + let result = self.common.reset(); + event!("virtio-device", "reset", "id", &self.id); + result + } +} + +impl Pausable for Balloon { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume() + } +} + +impl Snapshottable for Balloon { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id(), &self.state()) + } +} +impl Transportable for Balloon {} +impl Migratable for Balloon {} + +#[cfg(test)] +mod tests { + use super::{ + Balloon, BalloonEpollHandler, BalloonState, VirtioBalloonConfig, QUEUE_SIZE, + REPORTING_QUEUE_SIZE, VIRTIO_BALLOON_F_REPORTING, VIRTIO_BALLOON_MAX_PFN_BYTES, + VIRTIO_BALLOON_PFN_SHIFT, + }; + use crate::{ + GuestMemoryMmap, GuestRegionMmap, MmapRegion, VirtioInterrupt, VirtioInterruptType, + }; + use seccompiler::SeccompAction; + use std::fs::{self, File, OpenOptions}; + use std::io::Write; + use std::mem::size_of; + use std::os::unix::io::AsRawFd; + use std::sync::Arc; + use std::time::{SystemTime, UNIX_EPOCH}; + use vm_memory::{Bytes, FileOffset, GuestAddress, GuestMemoryAtomic}; + use vm_virtio::queue::testing::VirtQueue as GuestQ; + use vmm_sys_util::eventfd::EventFd; + + const PAGE_SIZE: usize = 4096; + + struct NoopVirtioInterrupt; + + impl VirtioInterrupt for NoopVirtioInterrupt { + fn trigger(&self, _int_type: VirtioInterruptType) -> std::io::Result<()> { + Ok(()) + } + } + + fn temp_file(name: &str, contents: &[u8]) -> (std::path::PathBuf, File) { + let nonce = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + let path = std::env::temp_dir().join(format!("balloon-{name}-{nonce}")); + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(true) + .open(&path) + .unwrap(); + file.write_all(contents).unwrap(); + (path, file) + } + + #[test] + fn release_zero_length_range_is_a_noop() { + let memory = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), PAGE_SIZE)]).unwrap(); + + BalloonEpollHandler::release_memory_range(&memory, GuestAddress(0), 0).unwrap(); + } + + #[test] + fn release_range_is_clamped_to_its_memory_region() { + let contents = vec![0x5a; PAGE_SIZE * 2]; + let (path, snapshot) = temp_file("region-boundary", &contents); + let mmap = MmapRegion::build( + Some(FileOffset::new(snapshot, 0)), + PAGE_SIZE, + libc::PROT_READ | libc::PROT_WRITE, + libc::MAP_SHARED, + ) + .unwrap(); + let region = GuestRegionMmap::new(mmap, GuestAddress(0)).unwrap(); + let memory = GuestMemoryMmap::from_regions(vec![region]).unwrap(); + + BalloonEpollHandler::release_memory_range(&memory, GuestAddress(0), PAGE_SIZE * 2).unwrap(); + + let after = fs::read(&path).unwrap(); + assert_eq!(&after[..PAGE_SIZE], &vec![0; PAGE_SIZE]); + assert_eq!(&after[PAGE_SIZE..], &contents[PAGE_SIZE..]); + fs::remove_file(path).unwrap(); + } + + #[test] + fn release_private_page_does_not_modify_writable_backing_file() { + let (path, backing) = temp_file("writable-private", &vec![0x5a; PAGE_SIZE]); + let mmap = MmapRegion::build( + Some(FileOffset::new(backing, 0)), + PAGE_SIZE, + libc::PROT_READ | libc::PROT_WRITE, + libc::MAP_PRIVATE, + ) + .unwrap(); + let region = GuestRegionMmap::new(mmap, GuestAddress(0)).unwrap(); + let memory = GuestMemoryMmap::from_regions(vec![region]).unwrap(); + + memory.write_obj(0xa5_u8, GuestAddress(0)).unwrap(); + BalloonEpollHandler::release_memory_range(&memory, GuestAddress(0), PAGE_SIZE).unwrap(); + + assert_eq!(memory.read_obj::(GuestAddress(0)).unwrap(), 0x5a); + assert_eq!(fs::read(&path).unwrap(), vec![0x5a; PAGE_SIZE]); + fs::remove_file(path).unwrap(); + } + + #[test] + fn reporting_queue_continues_after_an_invalid_range() { + const QUEUE_ADDRESS: GuestAddress = GuestAddress(0x1_0000); + const VALID_RANGE: GuestAddress = GuestAddress(0x2_0000); + const INVALID_RANGE: GuestAddress = GuestAddress(0x8_0000); + + let memory = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 0x4_0000)]).unwrap(); + memory.write_obj(0xa5_u8, VALID_RANGE).unwrap(); + let guest_queue = GuestQ::new(QUEUE_ADDRESS, &memory, 16); + guest_queue.dtable[0].set(INVALID_RANGE.0, PAGE_SIZE as u32, 0, 0); + guest_queue.dtable[1].set(VALID_RANGE.0, PAGE_SIZE as u32, 0, 0); + guest_queue.avail.ring[0].set(0); + guest_queue.avail.ring[1].set(1); + guest_queue.avail.idx.set(2); + + let mut handler = BalloonEpollHandler { + mem: GuestMemoryAtomic::new(memory.clone()), + queues: vec![guest_queue.create_queue()], + interrupt_cb: Arc::new(NoopVirtioInterrupt), + inflate_queue_evt: EventFd::new(0).unwrap(), + deflate_queue_evt: EventFd::new(0).unwrap(), + reporting_queue_evt: None, + kill_evt: EventFd::new(0).unwrap(), + pause_evt: EventFd::new(0).unwrap(), + }; + + handler.process_reporting_queue(0).unwrap(); + + assert_eq!(memory.read_obj::(VALID_RANGE).unwrap(), 0); + assert_eq!(guest_queue.used.idx.get(), 2); + } + + #[test] + fn inflate_queue_skips_oversized_descriptor_and_continues() { + const QUEUE_ADDRESS: GuestAddress = GuestAddress(0x1_0000); + const OVERSIZED_PFN_LIST: GuestAddress = GuestAddress(0x2_0000); + const VALID_PFN_LIST: GuestAddress = GuestAddress(0x2_1000); + const PROTECTED_RANGE: GuestAddress = GuestAddress(0x3_0000); + const VALID_RANGE: GuestAddress = GuestAddress(0x4_0000); + + let memory = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 0x5_0000)]).unwrap(); + memory.write_obj(0xa5_u8, PROTECTED_RANGE).unwrap(); + memory.write_obj(0xa5_u8, VALID_RANGE).unwrap(); + let protected_pfn = (PROTECTED_RANGE.0 >> VIRTIO_BALLOON_PFN_SHIFT) as u32; + for offset in (0..=VIRTIO_BALLOON_MAX_PFN_BYTES).step_by(size_of::()) { + memory + .write_obj( + protected_pfn, + GuestAddress(OVERSIZED_PFN_LIST.0 + offset as u64), + ) + .unwrap(); + } + memory + .write_obj( + (VALID_RANGE.0 >> VIRTIO_BALLOON_PFN_SHIFT) as u32, + VALID_PFN_LIST, + ) + .unwrap(); + + let guest_queue = GuestQ::new(QUEUE_ADDRESS, &memory, 16); + guest_queue.dtable[0].set( + OVERSIZED_PFN_LIST.0, + VIRTIO_BALLOON_MAX_PFN_BYTES + size_of::() as u32, + 0, + 0, + ); + guest_queue.dtable[1].set(VALID_PFN_LIST.0, size_of::() as u32, 0, 0); + guest_queue.avail.ring[0].set(0); + guest_queue.avail.ring[1].set(1); + guest_queue.avail.idx.set(2); + + let mut handler = BalloonEpollHandler { + mem: GuestMemoryAtomic::new(memory.clone()), + queues: vec![guest_queue.create_queue()], + interrupt_cb: Arc::new(NoopVirtioInterrupt), + inflate_queue_evt: EventFd::new(0).unwrap(), + deflate_queue_evt: EventFd::new(0).unwrap(), + reporting_queue_evt: None, + kill_evt: EventFd::new(0).unwrap(), + pause_evt: EventFd::new(0).unwrap(), + }; + + handler.process_queue(0).unwrap(); + + assert_eq!(memory.read_obj::(PROTECTED_RANGE).unwrap(), 0xa5); + assert_eq!(memory.read_obj::(VALID_RANGE).unwrap(), 0); + assert_eq!(guest_queue.used.idx.get(), 2); + } + + #[test] + fn inflate_queue_continues_after_an_invalid_pfn() { + const QUEUE_ADDRESS: GuestAddress = GuestAddress(0x1_0000); + const PFN_LIST: GuestAddress = GuestAddress(0x2_0000); + const VALID_RANGE: GuestAddress = GuestAddress(0x3_0000); + const INVALID_RANGE: GuestAddress = GuestAddress(0x8_0000); + + let memory = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 0x4_0000)]).unwrap(); + memory.write_obj(0xa5_u8, VALID_RANGE).unwrap(); + memory + .write_obj( + (INVALID_RANGE.0 >> VIRTIO_BALLOON_PFN_SHIFT) as u32, + PFN_LIST, + ) + .unwrap(); + memory + .write_obj( + (VALID_RANGE.0 >> VIRTIO_BALLOON_PFN_SHIFT) as u32, + GuestAddress(PFN_LIST.0 + size_of::() as u64), + ) + .unwrap(); + let guest_queue = GuestQ::new(QUEUE_ADDRESS, &memory, 16); + guest_queue.dtable[0].set(PFN_LIST.0, (size_of::() * 2) as u32, 0, 0); + guest_queue.avail.ring[0].set(0); + guest_queue.avail.idx.set(1); + + let mut handler = BalloonEpollHandler { + mem: GuestMemoryAtomic::new(memory.clone()), + queues: vec![guest_queue.create_queue()], + interrupt_cb: Arc::new(NoopVirtioInterrupt), + inflate_queue_evt: EventFd::new(0).unwrap(), + deflate_queue_evt: EventFd::new(0).unwrap(), + reporting_queue_evt: None, + kill_evt: EventFd::new(0).unwrap(), + pause_evt: EventFd::new(0).unwrap(), + }; + + handler.process_queue(0).unwrap(); + + assert_eq!(memory.read_obj::(VALID_RANGE).unwrap(), 0); + assert_eq!(guest_queue.used.idx.get(), 1); + } + + #[test] + fn restore_uses_saved_reporting_feature_for_queue_topology() { + let reporting_feature = 1u64 << VIRTIO_BALLOON_F_REPORTING; + let restored_with_reporting = Balloon::new( + "balloon0".to_string(), + 0, + false, + false, + SeccompAction::Allow, + EventFd::new(0).unwrap(), + Some(BalloonState { + avail_features: reporting_feature, + acked_features: reporting_feature, + config: VirtioBalloonConfig::default(), + }), + ) + .unwrap(); + assert_eq!( + restored_with_reporting.common.queue_sizes, + vec![QUEUE_SIZE, QUEUE_SIZE, REPORTING_QUEUE_SIZE] + ); + + let restored_without_reporting = Balloon::new( + "balloon0".to_string(), + 0, + false, + true, + SeccompAction::Allow, + EventFd::new(0).unwrap(), + Some(BalloonState { + avail_features: 0, + acked_features: 0, + config: VirtioBalloonConfig::default(), + }), + ) + .unwrap(); + assert_eq!( + restored_without_reporting.common.queue_sizes, + vec![QUEUE_SIZE, QUEUE_SIZE] + ); + } + + #[test] + fn release_private_page_with_read_only_backing_file() { + let (path, snapshot) = temp_file("read-only", &vec![0x5a; PAGE_SIZE]); + drop(snapshot); + + let snapshot = OpenOptions::new().read(true).open(&path).unwrap(); + let mmap = MmapRegion::build( + Some(FileOffset::new(snapshot, 0)), + PAGE_SIZE, + libc::PROT_READ | libc::PROT_WRITE, + libc::MAP_PRIVATE, + ) + .unwrap(); + let region = GuestRegionMmap::new(mmap, GuestAddress(0)).unwrap(); + let memory = GuestMemoryMmap::from_regions(vec![region]).unwrap(); + + memory.write_obj(0xa5_u8, GuestAddress(0)).unwrap(); + BalloonEpollHandler::release_memory_range(&memory, GuestAddress(0), PAGE_SIZE).unwrap(); + + assert_eq!(memory.read_obj::(GuestAddress(0)).unwrap(), 0x5a); + assert_eq!(fs::read(&path).unwrap(), vec![0x5a; PAGE_SIZE]); + fs::remove_file(path).unwrap(); + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/block.rs b/vendor/cube-hypervisor/virtio-devices/src/block.rs new file mode 100644 index 0000000..e881846 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/block.rs @@ -0,0 +1,912 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use super::Error as DeviceError; +use super::{ + ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, + RateLimiterConfig, VirtioCommon, VirtioDevice, VirtioDeviceType, VirtioInterruptType, + EPOLL_HELPER_EVENT_LAST, +}; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::GuestMemoryMmap; +use crate::VirtioInterrupt; +use anyhow::anyhow; +use block_util::{ + async_io::AsyncIo, async_io::AsyncIoError, async_io::DiskFile, build_disk_image_id, Request, + RequestType, VirtioBlockConfig, +}; +use rate_limiter::{BucketReduction, RateLimiter, TokenType}; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::io; +use std::num::Wrapping; +use std::ops::Deref; +use std::os::unix::io::AsRawFd; +use std::path::PathBuf; +use std::result; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::{Arc, Barrier}; +use std::time::{Duration, Instant}; +use std::{collections::HashMap, convert::TryInto}; +use thiserror::Error; +use virtio_bindings::bindings::virtio_blk::*; +use virtio_bindings::bindings::virtio_ring::{ + VIRTIO_RING_F_EVENT_IDX, VIRTIO_RING_F_INDIRECT_DESC, +}; +use virtio_queue::{Queue, QueueOwnedT, QueueT}; +use vm_memory::{ByteValued, Bytes, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryError}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vm_virtio::AccessPlatform; +use vmm_sys_util::eventfd::EventFd; + +const SECTOR_SHIFT: u8 = 9; +pub const SECTOR_SIZE: u64 = 0x01 << SECTOR_SHIFT; + +pub const MINIMUM_BLOCK_QUEUE_SIZE: u16 = 2; + +// New descriptors are pending on the virtio queue. +const QUEUE_AVAIL_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; +// New completed tasks are pending on the completion ring. +const COMPLETION_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2; +// New 'wake up' event from the rate limiter +const RATE_LIMITER_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 3; + +// Maximum time we wait for inflight IO to drain when the device is +// paused. Pause is the synchronization point a snapshot relies on, so +// we err on the side of giving the backend (network or local) enough +// time to settle. If this expires we mark drain as failed so the +// snapshot is aborted by the upper layer instead of being captured in +// an inconsistent state. +const PAUSE_DRAIN_TIMEOUT: Duration = Duration::from_secs(20); +// Per-iteration poll budget while draining; bounds CPU when the +// completion notifier is quiet but we have not yet hit the deadline. +const PAUSE_DRAIN_POLL_MS: i32 = 100; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Failed to parse the request: {0}")] + RequestParsing(block_util::Error), + #[error("Failed to execute the request: {0}")] + RequestExecuting(block_util::ExecuteError), + #[error("Failed to complete the request: {0}")] + RequestCompleting(block_util::Error), + #[error("Missing the expected entry in the list of requests")] + MissingEntryRequestList, + #[error("The asynchronous request returned with failure")] + AsyncRequestFailure, + #[error("Failed synchronizing the file: {0}")] + Fsync(AsyncIoError), + #[error("Failed adding used index: {0}")] + QueueAddUsed(virtio_queue::Error), + #[error("Failed creating an iterator over the queue: {0}")] + QueueIterator(virtio_queue::Error), + #[error("Failed to update request status: {0}")] + RequestStatus(GuestMemoryError), + #[error("Can't execute an unsupported request")] + Unsupported(u32), + #[error("Failed to enable notification: {0}")] + QueueEnableNotification(virtio_queue::Error), +} + +pub type Result = result::Result; + +#[derive(Default, Clone)] +pub struct BlockCounters { + read_bytes: Arc, + read_ops: Arc, + write_bytes: Arc, + write_ops: Arc, + limit_by_bytes: Arc, + limit_by_ops: Arc, +} + +struct BlockEpollHandler { + queue_index: u16, + queue: Queue, + mem: GuestMemoryAtomic, + disk_image: Box, + disk_nsectors: u64, + interrupt_cb: Arc, + disk_image_id: Vec, + kill_evt: EventFd, + pause_evt: EventFd, + writeback: Arc, + counters: BlockCounters, + queue_evt: EventFd, + request_list: HashMap, + rate_limiter: Option, + access_platform: Option>, + read_only: bool, + rate_limited: std::sync::Once, + id: String, + // Set by `before_pause` when inflight drain or pause-time flush + // could not be completed within the deadline. Read by + // `Block::pause` after the pause barrier returns to decide whether + // the snapshot must be aborted. Shared (not owned) so a multi-queue + // device aggregates failures from any queue. + drain_failed: Arc, +} + +impl BlockEpollHandler { + fn process_queue_submit(&mut self) -> Result<()> { + let queue = &mut self.queue; + + let mut limit_by_bytes = Wrapping(0); + let mut limit_by_ops = Wrapping(0); + + while let Some(mut desc_chain) = queue.pop_descriptor_chain(self.mem.memory()) { + let mut request = Request::parse(&mut desc_chain, self.access_platform.as_ref()) + .map_err(Error::RequestParsing)?; + + // For virtio spec compliance + // "A device MUST set the status byte to VIRTIO_BLK_S_IOERR for a write request + // if the VIRTIO_BLK_F_RO feature if offered, and MUST NOT write any data." + if self.read_only + && (request.request_type == RequestType::Out + || request.request_type == RequestType::Flush) + { + desc_chain + .memory() + .write_obj(VIRTIO_BLK_S_IOERR, request.status_addr) + .map_err(Error::RequestStatus)?; + + // If no asynchronous operation has been submitted, we can + // simply return the used descriptor. + queue + .add_used(desc_chain.memory(), desc_chain.head_index(), 0) + .map_err(Error::QueueAddUsed)?; + queue + .enable_notification(self.mem.memory().deref()) + .map_err(Error::QueueEnableNotification)?; + continue; + } + + if let Some(rate_limiter) = &mut self.rate_limiter { + // If limiter.consume() fails it means there is no more TokenType::Ops + // budget and rate limiting is in effect. + let rate_limit_reached = rate_limiter.consume(1, TokenType::Ops); + if rate_limit_reached != BucketReduction::Success { + // Stop processing the queue and return this descriptor chain to the + // avail ring, for later processing. + queue.go_to_previous_position(); + limit_by_ops = Wrapping(1); + self.rate_limited + .call_once(|| info!("{} block ops ratelimit fired", self.id)); + break; + } + // Exercise the rate limiter only if this request is of data transfer type. + if request.request_type == RequestType::In + || request.request_type == RequestType::Out + { + let mut bytes = Wrapping(0); + for (_, data_len) in &request.data_descriptors { + bytes += Wrapping(*data_len as u64); + } + + // If limiter.consume() fails it means there is no more TokenType::Bytes + // budget and rate limiting is in effect. + let rate_limit_reached = rate_limiter.consume(bytes.0, TokenType::Bytes); + match rate_limit_reached { + BucketReduction::Failure => { + // Revert the OPS consume(). + rate_limiter.manual_replenish(1, TokenType::Ops); + // Stop processing the queue and return this descriptor chain to the + // avail ring, for later processing. + queue.go_to_previous_position(); + limit_by_bytes = Wrapping(1); + self.rate_limited + .call_once(|| info!("{} block bw ratelimit fired", self.id)); + break; + } + BucketReduction::OverConsumption(_r) => { + limit_by_bytes = Wrapping(1); + self.rate_limited + .call_once(|| info!("{} block bw ratelimit fired", self.id)); + break; + } + BucketReduction::Success => {} + } + }; + } + + request.set_writeback(self.writeback.load(Ordering::Acquire)); + + if request + .execute_async( + desc_chain.memory(), + self.disk_nsectors, + self.disk_image.as_mut(), + &self.disk_image_id, + desc_chain.head_index() as u64, + ) + .map_err(Error::RequestExecuting)? + { + self.request_list.insert(desc_chain.head_index(), request); + } else { + desc_chain + .memory() + .write_obj(VIRTIO_BLK_S_OK, request.status_addr) + .map_err(Error::RequestStatus)?; + + // If no asynchronous operation has been submitted, we can + // simply return the used descriptor. + queue + .add_used(desc_chain.memory(), desc_chain.head_index(), 0) + .map_err(Error::QueueAddUsed)?; + queue + .enable_notification(self.mem.memory().deref()) + .map_err(Error::QueueEnableNotification)?; + } + } + + self.counters + .limit_by_bytes + .fetch_add(limit_by_bytes.0, Ordering::AcqRel); + self.counters + .limit_by_ops + .fetch_add(limit_by_ops.0, Ordering::AcqRel); + Ok(()) + } + + fn try_signal_used_queue(&mut self) -> result::Result<(), EpollHelperError> { + if self + .queue + .needs_notification(self.mem.memory().deref()) + .map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to check needs_notification: {:?}", + e + )) + })? + { + self.signal_used_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to signal used queue: {:?}", e)) + })?; + } + + Ok(()) + } + + fn process_queue_submit_and_signal(&mut self) -> result::Result<(), EpollHelperError> { + self.process_queue_submit().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to process queue (submit): {:?}", e)) + })?; + + self.try_signal_used_queue() + } + + fn process_queue_complete(&mut self) -> Result<()> { + let queue = &mut self.queue; + + let mem = self.mem.memory(); + let mut read_bytes = Wrapping(0); + let mut write_bytes = Wrapping(0); + let mut read_ops = Wrapping(0); + let mut write_ops = Wrapping(0); + + let completion_list = self.disk_image.complete(); + for (user_data, result) in completion_list { + let desc_index = user_data as u16; + let mut request = self + .request_list + .remove(&desc_index) + .ok_or(Error::MissingEntryRequestList)?; + request.complete_async().map_err(Error::RequestCompleting)?; + + let (status, len) = if result >= 0 { + match request.request_type { + RequestType::In => { + for (_, data_len) in &request.data_descriptors { + read_bytes += Wrapping(*data_len as u64); + } + read_ops += Wrapping(1); + } + RequestType::Out => { + if !request.writeback { + self.disk_image.fsync(None).map_err(Error::Fsync)?; + } + for (_, data_len) in &request.data_descriptors { + write_bytes += Wrapping(*data_len as u64); + } + write_ops += Wrapping(1); + } + _ => {} + } + + (VIRTIO_BLK_S_OK, result as u32) + } else { + error!( + "Request failed: {:x?} {:?}", + request, + io::Error::from_raw_os_error(-result) + ); + return Err(Error::AsyncRequestFailure); + }; + + mem.write_obj(status, request.status_addr) + .map_err(Error::RequestStatus)?; + + queue + .add_used(mem.deref(), desc_index, len) + .map_err(Error::QueueAddUsed)?; + queue + .enable_notification(mem.deref()) + .map_err(Error::QueueEnableNotification)?; + } + + self.counters + .write_bytes + .fetch_add(write_bytes.0, Ordering::AcqRel); + self.counters + .write_ops + .fetch_add(write_ops.0, Ordering::AcqRel); + + self.counters + .read_bytes + .fetch_add(read_bytes.0, Ordering::AcqRel); + self.counters + .read_ops + .fetch_add(read_ops.0, Ordering::AcqRel); + + Ok(()) + } + + fn signal_used_queue(&self) -> result::Result<(), DeviceError> { + self.interrupt_cb + .trigger(VirtioInterruptType::Queue(self.queue_index)) + .map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.queue_evt.as_raw_fd(), QUEUE_AVAIL_EVENT)?; + helper.add_event(self.disk_image.notifier().as_raw_fd(), COMPLETION_EVENT)?; + if let Some(rate_limiter) = &self.rate_limiter { + helper.add_event(rate_limiter.as_raw_fd(), RATE_LIMITER_EVENT)?; + } + helper.run(paused, paused_sync, self)?; + + Ok(()) + } +} + +impl EpollHelperHandler for BlockEpollHandler { + // Drain inflight IO and (in writeback mode) issue a synchronous + // FLUSH before the pause barrier. Any failure is reported via + // `drain_failed` rather than `Err`, because returning Err here + // would skip `paused_sync.wait()` and deadlock the pausing thread. + fn before_pause(&mut self, _helper: &mut EpollHelper) -> result::Result<(), EpollHelperError> { + let deadline = Instant::now() + PAUSE_DRAIN_TIMEOUT; + let efd = self.disk_image.notifier().as_raw_fd(); + + while !self.request_list.is_empty() { + if Instant::now() >= deadline { + error!( + "virtio-blk {}: pause drain timed out, {} IO still inflight", + self.id, + self.request_list.len() + ); + self.drain_failed.store(true, Ordering::SeqCst); + return Ok(()); + } + + // Wait briefly for the backend completion notifier. We do + // not consume the eventfd counter here; `process_queue_complete` + // pulls completions directly from the AsyncIo queue, which + // is the source of truth. + let mut pfd = libc::pollfd { + fd: efd, + events: libc::POLLIN, + revents: 0, + }; + // SAFETY: pfd is initialised; libc::poll only writes revents. + let _ = unsafe { libc::poll(&mut pfd, 1, PAUSE_DRAIN_POLL_MS) }; + + if let Err(e) = self.process_queue_complete() { + error!( + "virtio-blk {}: drain process_queue_complete failed: {:?}", + self.id, e + ); + self.drain_failed.store(true, Ordering::SeqCst); + return Ok(()); + } + } + + // Make the freshly-added used entries visible to the guest. + // Failures here only affect interrupt delivery (state is + // already correct in memory); log and continue. + if let Err(e) = self.try_signal_used_queue() { + warn!( + "virtio-blk {}: pre-pause used-queue signal failed: {:?}", + self.id, e + ); + } + + // Write barrier: once data is fully drained, force a synchronous + // FLUSH so the backend's completed writes are durable before the + // snapshot is taken. Skipped in writethrough because each write + // already implies a flush. + if self.writeback.load(Ordering::Acquire) { + if let Err(e) = self.disk_image.fsync(None) { + error!("virtio-blk {}: pause-time flush failed: {:?}", self.id, e); + self.drain_failed.store(true, Ordering::SeqCst); + } + } + + Ok(()) + } + + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + QUEUE_AVAIL_EVENT => { + self.queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + + let rate_limit_reached = + self.rate_limiter.as_ref().map_or(false, |r| r.is_blocked()); + + // Process the queue only when the rate limit is not reached + if !rate_limit_reached { + self.process_queue_submit_and_signal()? + } + } + COMPLETION_EVENT => { + self.disk_image.notifier().read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + + self.process_queue_complete().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process queue (complete): {:?}", + e + )) + })?; + + let rate_limit_reached = + self.rate_limiter.as_ref().map_or(false, |r| r.is_blocked()); + + // Process the queue only when the rate limit is not reached + if !rate_limit_reached { + self.process_queue_submit().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process queue (submit): {:?}", + e + )) + })?; + } + self.try_signal_used_queue()?; + } + RATE_LIMITER_EVENT => { + if let Some(rate_limiter) = &mut self.rate_limiter { + // Upon rate limiter event, call the rate limiter handler + // and restart processing the queue. + rate_limiter.event_handler().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process rate limiter event: {:?}", + e + )) + })?; + + self.process_queue_submit_and_signal()? + } else { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected 'RATE_LIMITER_EVENT' when rate_limiter is not enabled." + ))); + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected event: {}", + ev_type + ))); + } + } + Ok(()) + } +} + +/// Virtio device for exposing block level read/write operations on a host file. +pub struct Block { + common: VirtioCommon, + id: String, + disk_image: Box, + disk_path: PathBuf, + disk_nsectors: u64, + config: VirtioBlockConfig, + writeback: Arc, + counters: BlockCounters, + seccomp_action: SeccompAction, + rate_limiter_config: Option, + exit_evt: EventFd, + read_only: bool, + /// Shared with each per-queue `BlockEpollHandler`. Set by an epoll + /// thread when its pause-time drain or flush could not complete + /// within `PAUSE_DRAIN_TIMEOUT`; checked by `pause()` after the + /// pause barrier so the upper layer can abort the snapshot. + drain_failed: Arc, +} + +#[derive(Serialize, Deserialize)] +pub struct BlockState { + pub disk_path: String, + pub disk_nsectors: u64, + pub avail_features: u64, + pub acked_features: u64, + pub config: VirtioBlockConfig, +} + +impl Block { + /// Create a new virtio block device that operates on the given file. + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + mut disk_image: Box, + disk_path: PathBuf, + read_only: bool, + iommu: bool, + num_queues: usize, + queue_size: u16, + seccomp_action: SeccompAction, + rate_limiter_config: Option, + exit_evt: EventFd, + state: Option, + ) -> io::Result { + let (disk_nsectors, avail_features, acked_features, config) = if let Some(state) = state { + debug!("Restoring virtio-block {}", id); + ( + state.disk_nsectors, + state.avail_features, + state.acked_features, + state.config, + ) + } else { + let disk_size = disk_image.size().map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("Failed getting disk size: {}", e), + ) + })?; + if disk_size % SECTOR_SIZE != 0 { + warn!( + "Disk size {} is not a multiple of sector size {}; \ + the remainder will not be visible to the guest.", + disk_size, SECTOR_SIZE + ); + } + + let mut avail_features = (1u64 << VIRTIO_F_VERSION_1) + | (1u64 << VIRTIO_BLK_F_FLUSH) + | (1u64 << VIRTIO_BLK_F_CONFIG_WCE) + | (1u64 << VIRTIO_BLK_F_BLK_SIZE) + | (1u64 << VIRTIO_BLK_F_TOPOLOGY) + | (1u64 << VIRTIO_BLK_F_SEG_MAX) + | (1u64 << VIRTIO_RING_F_EVENT_IDX) + | (1u64 << VIRTIO_RING_F_INDIRECT_DESC); + + if iommu { + avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM; + } + + if read_only { + avail_features |= 1u64 << VIRTIO_BLK_F_RO; + } + + let topology = disk_image.topology(); + info!("Disk topology: {:?}", topology); + + let logical_block_size = if topology.logical_block_size > 512 { + topology.logical_block_size + } else { + 512 + }; + + // Calculate the exponent that maps physical block to logical block + let mut physical_block_exp = 0; + let mut size = logical_block_size; + while size < topology.physical_block_size { + physical_block_exp += 1; + size <<= 1; + } + + let disk_nsectors = disk_size / SECTOR_SIZE; + let mut config = VirtioBlockConfig { + capacity: disk_nsectors, + writeback: 1, + blk_size: topology.logical_block_size as u32, + physical_block_exp, + min_io_size: (topology.minimum_io_size / logical_block_size) as u16, + opt_io_size: (topology.optimal_io_size / logical_block_size) as u32, + seg_max: (queue_size - MINIMUM_BLOCK_QUEUE_SIZE) as u32, + ..Default::default() + }; + + if num_queues > 1 { + avail_features |= 1u64 << VIRTIO_BLK_F_MQ; + config.num_queues = num_queues as u16; + } + + (disk_nsectors, avail_features, 0, config) + }; + + Ok(Block { + common: VirtioCommon { + device_type: VirtioDeviceType::Block as u32, + avail_features, + acked_features, + paused_sync: Some(Arc::new(Barrier::new(num_queues + 1))), + queue_sizes: vec![queue_size; num_queues], + min_queues: 1, + ..Default::default() + }, + id, + disk_image, + disk_path, + disk_nsectors, + config, + writeback: Arc::new(AtomicBool::new(true)), + counters: BlockCounters::default(), + seccomp_action, + rate_limiter_config, + exit_evt, + read_only, + drain_failed: Arc::new(AtomicBool::new(false)), + }) + } + + fn state(&self) -> BlockState { + BlockState { + disk_path: self.disk_path.to_str().unwrap().to_owned(), + disk_nsectors: self.disk_nsectors, + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + config: self.config, + } + } + + fn update_writeback(&mut self) { + // Use writeback from config if VIRTIO_BLK_F_CONFIG_WCE + let writeback = if self.common.feature_acked(VIRTIO_BLK_F_CONFIG_WCE.into()) { + self.config.writeback == 1 + } else { + // Else check if VIRTIO_BLK_F_FLUSH negotiated + self.common.feature_acked(VIRTIO_BLK_F_FLUSH.into()) + }; + + info!( + "Changing cache mode to {}", + if writeback { + "writeback" + } else { + "writethrough" + } + ); + self.writeback.store(writeback, Ordering::Release); + } + + #[cfg(fuzzing)] + pub fn wait_for_epoll_threads(&mut self) { + self.common.wait_for_epoll_threads(); + } +} + +impl Drop for Block { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +impl VirtioDevice for Block { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.as_slice(), offset, data); + } + + fn write_config(&mut self, offset: u64, data: &[u8]) { + // The "writeback" field is the only mutable field + let writeback_offset = + (&self.config.writeback as *const _ as u64) - (&self.config as *const _ as u64); + if offset != writeback_offset || data.len() != std::mem::size_of_val(&self.config.writeback) + { + error!( + "Attempt to write to read-only field: offset {:x} length {}", + offset, + data.len() + ); + return; + } + + self.config.writeback = data[0]; + self.update_writeback(); + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + + let disk_image_id = build_disk_image_id(&self.disk_path); + self.update_writeback(); + + let mut epoll_threads = Vec::new(); + let event_idx = self.common.feature_acked(VIRTIO_RING_F_EVENT_IDX.into()); + + for i in 0..queues.len() { + let (_, mut queue, queue_evt) = queues.remove(0); + queue.set_event_idx(event_idx); + + let queue_size = queue.size(); + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let rate_limiter: Option = self + .rate_limiter_config + .map(RateLimiterConfig::try_into) + .transpose() + .map_err(ActivateError::CreateRateLimiter)?; + + let mut handler = BlockEpollHandler { + queue_index: i as u16, + queue, + mem: mem.clone(), + disk_image: self + .disk_image + .new_async_io(queue_size as u32) + .map_err(|e| { + error!("failed to create new AsyncIo: {}", e); + ActivateError::BadActivate + })?, + disk_nsectors: self.disk_nsectors, + interrupt_cb: interrupt_cb.clone(), + disk_image_id: disk_image_id.clone(), + kill_evt, + pause_evt, + writeback: self.writeback.clone(), + counters: self.counters.clone(), + queue_evt, + request_list: HashMap::with_capacity(queue_size.into()), + rate_limiter, + access_platform: self.common.access_platform.clone(), + read_only: self.read_only, + rate_limited: std::sync::Once::new(), + id: self.id.clone(), + drain_failed: self.drain_failed.clone(), + }; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + + spawn_virtio_thread( + &format!("{}_q{}", self.id.clone(), i), + &self.seccomp_action, + Thread::VirtioBlock, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + } + + self.common.epoll_threads = Some(epoll_threads); + event!("virtio-device", "activated", "id", &self.id); + + Ok(()) + } + + fn reset(&mut self) -> Option> { + let result = self.common.reset(); + event!("virtio-device", "reset", "id", &self.id); + result + } + + fn counters(&self) -> Option>> { + let mut counters = HashMap::new(); + + counters.insert( + "read_bytes", + Wrapping(self.counters.read_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "write_bytes", + Wrapping(self.counters.write_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "read_ops", + Wrapping(self.counters.read_ops.load(Ordering::Acquire)), + ); + counters.insert( + "write_ops", + Wrapping(self.counters.write_ops.load(Ordering::Acquire)), + ); + counters.insert( + "limit_by_bytes", + Wrapping(self.counters.limit_by_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "limit_by_ops", + Wrapping(self.counters.limit_by_ops.load(Ordering::Acquire)), + ); + + Some(counters) + } + + fn set_access_platform(&mut self, access_platform: Arc) { + self.common.set_access_platform(access_platform) + } +} + +impl Pausable for Block { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause()?; + + // After common.pause() returns, every per-queue epoll thread has + // executed its `before_pause` hook and crossed the barrier. Any + // queue that failed to drain inflight IO or to issue the + // pause-time flush within `PAUSE_DRAIN_TIMEOUT` will have set + // this flag. Aborting pause here propagates upward and prevents + // a snapshot from being captured in an inconsistent state. + if self.drain_failed.swap(false, Ordering::SeqCst) { + return Err(MigratableError::Pause(anyhow!( + "Failed to drain inflight IO for block device {} within timeout", + self.id + ))); + } + + Ok(()) + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume() + } +} + +impl Snapshottable for Block { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id(), &self.state()) + } +} +impl Transportable for Block {} +impl Migratable for Block {} diff --git a/vendor/cube-hypervisor/virtio-devices/src/console.rs b/vendor/cube-hypervisor/virtio-devices/src/console.rs new file mode 100644 index 0000000..361cc6c --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/console.rs @@ -0,0 +1,821 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use super::Error as DeviceError; +use super::{ + ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, VirtioCommon, VirtioDevice, + VirtioDeviceType, VirtioInterruptType, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_IOMMU_PLATFORM, + VIRTIO_F_VERSION_1, +}; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::GuestMemoryMmap; +use crate::VirtioInterrupt; +use anyhow::anyhow; +use libc::{EFD_NONBLOCK, TIOCGWINSZ}; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use serial_buffer::SerialBuffer; +use std::cmp; +use std::collections::VecDeque; +use std::fs::File; +use std::io; +use std::io::{Read, Write}; +use std::os::unix::io::AsRawFd; +use std::result; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::{Arc, Barrier, Mutex}; +use thiserror::Error; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{ByteValued, Bytes, GuestAddressSpace, GuestMemory, GuestMemoryAtomic}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vm_virtio::{AccessPlatform, Translatable}; +use vmm_sys_util::eventfd::EventFd; + +const QUEUE_SIZE: u16 = 256; +const NUM_QUEUES: usize = 2; +const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE; NUM_QUEUES]; + +// New descriptors are pending on the virtio queue. +const INPUT_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; +const OUTPUT_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2; +// Console configuration change event is triggered. +const CONFIG_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 3; +// File written to (input ready) +const FILE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 4; +// Console resized +const RESIZE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 5; + +//Console size feature bit +const VIRTIO_CONSOLE_F_SIZE: u64 = 0; + +#[derive(Error, Debug)] +enum Error { + #[error("Descriptor chain too short")] + DescriptorChainTooShort, + #[error("Failed to read from guest memory: {0}")] + GuestMemoryRead(vm_memory::guest_memory::Error), + #[error("Failed to write to guest memory: {0}")] + GuestMemoryWrite(vm_memory::guest_memory::Error), + #[error("Failed to write_all output: {0}")] + OutputWriteAll(io::Error), + #[error("Failed to flush output: {0}")] + OutputFlush(io::Error), + #[error("Failed to add used index: {0}")] + QueueAddUsed(virtio_queue::Error), +} + +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +#[repr(C, packed)] +pub struct VirtioConsoleConfig { + cols: u16, + rows: u16, + max_nr_ports: u32, + emerg_wr: u32, +} + +impl Default for VirtioConsoleConfig { + fn default() -> Self { + VirtioConsoleConfig { + cols: 0, + rows: 0, + max_nr_ports: 1, + emerg_wr: 0, + } + } +} + +// SAFETY: it only has data and has no implicit padding. +unsafe impl ByteValued for VirtioConsoleConfig {} + +struct ConsoleEpollHandler { + mem: GuestMemoryAtomic, + input_queue: Queue, + output_queue: Queue, + interrupt_cb: Arc, + in_buffer: Arc>>, + resizer: Arc, + endpoint: Endpoint, + input_queue_evt: EventFd, + output_queue_evt: EventFd, + config_evt: EventFd, + resize_pipe: Option, + kill_evt: EventFd, + pause_evt: EventFd, + access_platform: Option>, + out: Option>, + write_out: Option>, + file_event_registered: bool, +} + +pub enum Endpoint { + File(File), + FilePair(File, File), + PtyPair(File, File), + Null, +} + +impl Endpoint { + fn out_file(&self) -> Option<&File> { + match self { + Self::File(f) => Some(f), + Self::FilePair(f, _) => Some(f), + Self::PtyPair(f, _) => Some(f), + Self::Null => None, + } + } + + fn in_file(&self) -> Option<&File> { + match self { + Self::File(_) => None, + Self::FilePair(_, f) => Some(f), + Self::PtyPair(_, f) => Some(f), + Self::Null => None, + } + } + + fn is_pty(&self) -> bool { + matches!(self, Self::PtyPair(_, _)) + } +} + +impl Clone for Endpoint { + fn clone(&self) -> Self { + match self { + Self::File(f) => Self::File(f.try_clone().unwrap()), + Self::FilePair(f_out, f_in) => { + Self::FilePair(f_out.try_clone().unwrap(), f_in.try_clone().unwrap()) + } + Self::PtyPair(f_out, f_in) => { + Self::PtyPair(f_out.try_clone().unwrap(), f_in.try_clone().unwrap()) + } + Self::Null => Self::Null, + } + } +} + +impl ConsoleEpollHandler { + #[allow(clippy::too_many_arguments)] + fn new( + mem: GuestMemoryAtomic, + input_queue: Queue, + output_queue: Queue, + interrupt_cb: Arc, + in_buffer: Arc>>, + resizer: Arc, + endpoint: Endpoint, + input_queue_evt: EventFd, + output_queue_evt: EventFd, + config_evt: EventFd, + resize_pipe: Option, + kill_evt: EventFd, + pause_evt: EventFd, + access_platform: Option>, + ) -> Self { + let out_file = endpoint.out_file(); + let (out, write_out) = if let Some(out_file) = out_file { + let writer = out_file.try_clone().unwrap(); + if endpoint.is_pty() { + let pty_write_out = Arc::new(AtomicBool::new(false)); + let write_out = Some(pty_write_out.clone()); + let buffer = SerialBuffer::new(Box::new(writer), pty_write_out); + (Some(Box::new(buffer) as Box), write_out) + } else { + (Some(Box::new(writer) as Box), None) + } + } else { + (None, None) + }; + + ConsoleEpollHandler { + mem, + input_queue, + output_queue, + interrupt_cb, + in_buffer, + resizer, + endpoint, + input_queue_evt, + output_queue_evt, + config_evt, + resize_pipe, + kill_evt, + pause_evt, + access_platform, + out, + write_out, + file_event_registered: false, + } + } + + /* + * Each port of virtio console device has one receive + * queue. One or more empty buffers are placed by the + * driver in the receive queue for incoming data. Here, + * we place the input data to these empty buffers. + */ + fn process_input_queue(&mut self) -> Result { + let mut in_buffer = self.in_buffer.lock().unwrap(); + let recv_queue = &mut self.input_queue; //receiveq + let mut used_descs = false; + + if in_buffer.is_empty() { + return Ok(false); + } + + while let Some(mut desc_chain) = recv_queue.pop_descriptor_chain(self.mem.memory()) { + let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?; + let len = cmp::min(desc.len(), in_buffer.len() as u32); + let source_slice = in_buffer.drain(..len as usize).collect::>(); + + desc_chain + .memory() + .write_slice( + &source_slice[..], + desc.addr() + .translate_gva(self.access_platform.as_ref(), desc.len() as usize), + ) + .map_err(Error::GuestMemoryWrite)?; + + recv_queue + .add_used(desc_chain.memory(), desc_chain.head_index(), len) + .map_err(Error::QueueAddUsed)?; + used_descs = true; + + if in_buffer.is_empty() { + break; + } + } + + Ok(used_descs) + } + + /* + * Each port of virtio console device has one transmit + * queue. For outgoing data, characters are placed in + * the transmit queue by the driver. Therefore, here + * we read data from the transmit queue and flush them + * to the referenced address. + */ + fn process_output_queue(&mut self) -> Result { + let trans_queue = &mut self.output_queue; //transmitq + let mut used_descs = false; + + while let Some(mut desc_chain) = trans_queue.pop_descriptor_chain(self.mem.memory()) { + let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?; + if let Some(out) = &mut self.out { + let mut buf: Vec = Vec::new(); + desc_chain + .memory() + .write_volatile_to( + desc.addr() + .translate_gva(self.access_platform.as_ref(), desc.len() as usize), + &mut buf, + desc.len() as usize, + ) + .map_err(Error::GuestMemoryRead)?; + + out.write_all(&buf).map_err(Error::OutputWriteAll)?; + out.flush().map_err(Error::OutputFlush)?; + } + trans_queue + .add_used(desc_chain.memory(), desc_chain.head_index(), desc.len()) + .map_err(Error::QueueAddUsed)?; + used_descs = true; + } + + Ok(used_descs) + } + + fn signal_used_queue(&self, queue_index: u16) -> result::Result<(), DeviceError> { + self.interrupt_cb + .trigger(VirtioInterruptType::Queue(queue_index)) + .map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.input_queue_evt.as_raw_fd(), INPUT_QUEUE_EVENT)?; + helper.add_event(self.output_queue_evt.as_raw_fd(), OUTPUT_QUEUE_EVENT)?; + helper.add_event(self.config_evt.as_raw_fd(), CONFIG_EVENT)?; + if let Some(resize_pipe) = self.resize_pipe.as_ref() { + helper.add_event(resize_pipe.as_raw_fd(), RESIZE_EVENT)?; + } + if let Some(in_file) = self.endpoint.in_file() { + let mut events = epoll::Events::EPOLLIN; + if self.endpoint.is_pty() { + events |= epoll::Events::EPOLLONESHOT; + } + helper.add_event_custom(in_file.as_raw_fd(), FILE_EVENT, events)?; + self.file_event_registered = true; + } + + // In case of PTY, we want to be able to detect a connection on the + // other end of the PTY. This is done by detecting there's no event + // triggered on the epoll, which is the reason why we want the + // epoll_wait() function to return after the timeout expired. + // In case of TTY, we don't expect to detect such behavior, which is + // why we can afford to block until an actual event is triggered. + let (timeout, enable_event_list) = if self.endpoint.is_pty() { + (500, true) + } else { + (-1, false) + }; + helper.run_with_timeout(paused, paused_sync, self, timeout, enable_event_list)?; + + Ok(()) + } + + // This function should be called when the other end of the PTY is + // connected. It verifies if this is the first time it's been invoked + // after the connection happened, and if that's the case it flushes + // all output from the console to the PTY. Otherwise, it's a no-op. + fn trigger_pty_flush(&mut self) -> result::Result<(), anyhow::Error> { + if let (Some(pty_write_out), Some(out)) = (&self.write_out, &mut self.out) { + if pty_write_out.load(Ordering::Acquire) { + return Ok(()); + } + pty_write_out.store(true, Ordering::Release); + out.flush() + .map_err(|e| anyhow!("Failed to flush PTY: {:?}", e)) + } else { + Ok(()) + } + } + + fn register_file_event( + &mut self, + helper: &mut EpollHelper, + ) -> result::Result<(), EpollHelperError> { + if self.file_event_registered { + return Ok(()); + } + + // Re-arm the file event. + helper.mod_event_custom( + self.endpoint.in_file().unwrap().as_raw_fd(), + FILE_EVENT, + epoll::Events::EPOLLIN | epoll::Events::EPOLLONESHOT, + )?; + self.file_event_registered = true; + + Ok(()) + } +} + +impl EpollHelperHandler for ConsoleEpollHandler { + fn handle_event( + &mut self, + helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + + match ev_type { + INPUT_QUEUE_EVENT => { + self.input_queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + let needs_notification = self.process_input_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process input queue : {:?}", + e + )) + })?; + if needs_notification { + self.signal_used_queue(0).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", + e + )) + })?; + } + } + OUTPUT_QUEUE_EVENT => { + self.output_queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + let needs_notification = self.process_output_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process output queue : {:?}", + e + )) + })?; + if needs_notification { + self.signal_used_queue(1).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", + e + )) + })?; + } + } + CONFIG_EVENT => { + self.config_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get config event: {:?}", e)) + })?; + self.interrupt_cb + .trigger(VirtioInterruptType::Config) + .map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal console driver: {:?}", + e + )) + })?; + } + RESIZE_EVENT => { + self.resize_pipe + .as_ref() + .unwrap() + .read_exact(&mut [0]) + .map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to get resize event: {:?}", + e + )) + })?; + self.resizer.update_console_size(); + } + FILE_EVENT => { + if event.events & libc::EPOLLIN as u32 != 0 { + let mut input = [0u8; 64]; + if let Some(ref mut in_file) = self.endpoint.in_file() { + if let Ok(count) = in_file.read(&mut input) { + let mut in_buffer = self.in_buffer.lock().unwrap(); + in_buffer.extend(&input[..count]); + } + + let needs_notification = self.process_input_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process input queue : {:?}", + e + )) + })?; + if needs_notification { + self.signal_used_queue(0).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", + e + )) + })?; + } + } + } + if self.endpoint.is_pty() { + self.file_event_registered = false; + if event.events & libc::EPOLLHUP as u32 != 0 { + if let Some(pty_write_out) = &self.write_out { + if pty_write_out.load(Ordering::Acquire) { + pty_write_out.store(false, Ordering::Release); + } + } + } else { + // If the EPOLLHUP flag is not up on the associated event, we + // can assume the other end of the PTY is connected and therefore + // we can flush the output of the serial to it. + self.trigger_pty_flush() + .map_err(EpollHelperError::HandleTimeout)?; + + self.register_file_event(helper)?; + } + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unknown event for virtio-console" + ))); + } + } + Ok(()) + } + + // This function will be invoked whenever the timeout is reached before + // any other event was triggered while waiting for the epoll. + fn handle_timeout(&mut self, helper: &mut EpollHelper) -> Result<(), EpollHelperError> { + if !self.endpoint.is_pty() { + return Ok(()); + } + + if self.file_event_registered { + // This very specific case happens when the console is connected + // to a PTY. We know EPOLLHUP is always present when there's nothing + // connected at the other end of the PTY. That's why getting no event + // means we can flush the output of the console through the PTY. + self.trigger_pty_flush() + .map_err(EpollHelperError::HandleTimeout)?; + } + + // Every time we hit the timeout, let's register the FILE_EVENT to give + // us a chance to catch a possible event that might have been triggered. + self.register_file_event(helper) + } + + // This function returns the full list of events found on the epoll before + // iterating through it calling handle_event(). It allows the detection of + // the PTY connection even when the timeout is not being triggered, which + // happens when there are other events preventing the timeout from being + // reached. This is an additional way of detecting a PTY connection. + fn event_list( + &mut self, + helper: &mut EpollHelper, + events: &[epoll::Event], + ) -> Result<(), EpollHelperError> { + if self.file_event_registered { + for event in events { + if event.data as u16 == FILE_EVENT && (event.events & libc::EPOLLHUP as u32) != 0 { + return Ok(()); + } + } + + // This very specific case happens when the console is connected + // to a PTY. We know EPOLLHUP is always present when there's nothing + // connected at the other end of the PTY. That's why getting no event + // means we can flush the output of the console through the PTY. + self.trigger_pty_flush() + .map_err(EpollHelperError::HandleTimeout)?; + } + + self.register_file_event(helper) + } +} + +/// Resize handler +pub struct ConsoleResizer { + config_evt: EventFd, + tty: Option, + config: Arc>, + acked_features: AtomicU64, +} + +impl ConsoleResizer { + pub fn update_console_size(&self) { + if let Some(tty) = self.tty.as_ref() { + let (cols, rows) = get_win_size(tty); + self.config.lock().unwrap().update_console_size(cols, rows); + if self + .acked_features + .fetch_and(1u64 << VIRTIO_CONSOLE_F_SIZE, Ordering::AcqRel) + != 0 + { + // Send the interrupt to the driver + let _ = self.config_evt.write(1); + } + } + } +} + +impl VirtioConsoleConfig { + pub fn update_console_size(&mut self, cols: u16, rows: u16) { + self.cols = cols; + self.rows = rows; + } +} + +/// Virtio device for exposing console to the guest OS through virtio. +pub struct Console { + common: VirtioCommon, + id: String, + config: Arc>, + resizer: Arc, + resize_pipe: Option, + endpoint: Endpoint, + seccomp_action: SeccompAction, + in_buffer: Arc>>, + exit_evt: EventFd, +} + +#[derive(Serialize, Deserialize)] +pub struct ConsoleState { + avail_features: u64, + acked_features: u64, + config: VirtioConsoleConfig, + in_buffer: Vec, +} + +fn get_win_size(tty: &dyn AsRawFd) -> (u16, u16) { + #[repr(C)] + #[derive(Default)] + struct WindowSize { + rows: u16, + cols: u16, + xpixel: u16, + ypixel: u16, + } + let ws: WindowSize = WindowSize::default(); + + unsafe { + libc::ioctl(tty.as_raw_fd(), TIOCGWINSZ, &ws); + } + + (ws.cols, ws.rows) +} +impl Console { + /// Create a new virtio console device + pub fn new( + id: String, + endpoint: Endpoint, + resize_pipe: Option, + iommu: bool, + seccomp_action: SeccompAction, + exit_evt: EventFd, + state: Option, + ) -> io::Result<(Console, Arc)> { + let (avail_features, acked_features, config, in_buffer) = if let Some(state) = state { + info!("Restoring virtio-console {}", id); + ( + state.avail_features, + state.acked_features, + state.config, + state.in_buffer.into(), + ) + } else { + let mut avail_features = 1u64 << VIRTIO_F_VERSION_1 | 1u64 << VIRTIO_CONSOLE_F_SIZE; + if iommu { + avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM; + } + + ( + avail_features, + 0, + VirtioConsoleConfig::default(), + VecDeque::new(), + ) + }; + + let config_evt = EventFd::new(EFD_NONBLOCK).unwrap(); + let console_config = Arc::new(Mutex::new(config)); + let resizer = Arc::new(ConsoleResizer { + config_evt, + config: console_config.clone(), + tty: endpoint.out_file().as_ref().map(|t| t.try_clone().unwrap()), + acked_features: AtomicU64::new(acked_features), + }); + + resizer.update_console_size(); + + Ok(( + Console { + common: VirtioCommon { + device_type: VirtioDeviceType::Console as u32, + queue_sizes: QUEUE_SIZES.to_vec(), + avail_features, + acked_features, + paused_sync: Some(Arc::new(Barrier::new(2))), + min_queues: NUM_QUEUES as u16, + ..Default::default() + }, + id, + config: console_config, + resizer: resizer.clone(), + resize_pipe, + endpoint, + seccomp_action, + in_buffer: Arc::new(Mutex::new(in_buffer)), + exit_evt, + }, + resizer, + )) + } + + fn state(&self) -> ConsoleState { + ConsoleState { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + config: *(self.config.lock().unwrap()), + in_buffer: self.in_buffer.lock().unwrap().clone().into(), + } + } + + #[cfg(fuzzing)] + pub fn wait_for_epoll_threads(&mut self) { + self.common.wait_for_epoll_threads(); + } +} + +impl Drop for Console { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +impl VirtioDevice for Console { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.lock().unwrap().as_slice(), offset, data); + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + self.resizer + .acked_features + .store(self.common.acked_features, Ordering::Relaxed); + + if self.common.feature_acked(VIRTIO_CONSOLE_F_SIZE) { + if let Err(e) = interrupt_cb.trigger(VirtioInterruptType::Config) { + error!("Failed to signal console driver: {:?}", e); + } + } + + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let (_, input_queue, input_queue_evt) = queues.remove(0); + let (_, output_queue, output_queue_evt) = queues.remove(0); + + let mut handler = ConsoleEpollHandler::new( + mem, + input_queue, + output_queue, + interrupt_cb, + self.in_buffer.clone(), + Arc::clone(&self.resizer), + self.endpoint.clone(), + input_queue_evt, + output_queue_evt, + self.resizer.config_evt.try_clone().unwrap(), + self.resize_pipe.as_ref().map(|p| p.try_clone().unwrap()), + kill_evt, + pause_evt, + self.common.access_platform.clone(), + ); + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + let mut epoll_threads = Vec::new(); + + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioConsole, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + + self.common.epoll_threads = Some(epoll_threads); + + event!("virtio-device", "activated", "id", &self.id); + Ok(()) + } + + fn reset(&mut self) -> Option> { + let result = self.common.reset(); + event!("virtio-device", "reset", "id", &self.id); + result + } + + fn set_access_platform(&mut self, access_platform: Arc) { + self.common.set_access_platform(access_platform) + } +} + +impl Pausable for Console { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume() + } +} + +impl Snapshottable for Console { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } +} +impl Transportable for Console {} +impl Migratable for Console {} diff --git a/vendor/cube-hypervisor/virtio-devices/src/device.rs b/vendor/cube-hypervisor/virtio-devices/src/device.rs new file mode 100644 index 0000000..2552dff --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/device.rs @@ -0,0 +1,396 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use crate::{ + ActivateError, ActivateResult, Error, GuestMemoryMmap, GuestRegionMmap, + VIRTIO_F_RING_INDIRECT_DESC, +}; +use libc::EFD_NONBLOCK; +use std::collections::HashMap; +use std::io::Write; +use std::num::Wrapping; +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, Barrier, +}; +use std::thread; + +use anyhow::anyhow; +use virtio_queue::Queue; +use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestUsize}; +use vm_migration::{MigratableError, Pausable}; +use vm_virtio::AccessPlatform; +use vm_virtio::VirtioDeviceType; +use vmm_sys_util::eventfd::EventFd; + +pub enum VirtioInterruptType { + Config, + Queue(u16), +} + +pub trait VirtioInterrupt: Send + Sync { + fn trigger(&self, int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error>; + fn notifier(&self, _int_type: VirtioInterruptType) -> Option { + None + } +} + +#[derive(Clone)] +pub struct UserspaceMapping { + pub host_addr: u64, + pub mem_slot: u32, + pub addr: GuestAddress, + pub len: GuestUsize, + pub mergeable: bool, +} + +#[derive(Clone)] +pub struct VirtioSharedMemory { + pub offset: u64, + pub len: u64, +} + +#[derive(Clone)] +pub struct VirtioSharedMemoryList { + pub host_addr: u64, + pub mem_slot: u32, + pub addr: GuestAddress, + pub len: GuestUsize, + pub region_list: Vec, +} + +/// Trait for virtio devices to be driven by a virtio transport. +/// +/// The lifecycle of a virtio device is to be moved to a virtio transport, which will then query the +/// device. Once the guest driver has configured the device, `VirtioDevice::activate` will be called +/// and all the events, memory, and queues for device operation will be moved into the device. +/// Optionally, a virtio device can implement device reset in which it returns said resources and +/// resets its internal. +pub trait VirtioDevice: Send { + /// The virtio device type. + fn device_type(&self) -> u32; + + /// The maximum size of each queue that this device supports. + fn queue_max_sizes(&self) -> &[u16]; + + /// The set of feature bits that this device supports. + fn features(&self) -> u64 { + 0 + } + + /// Acknowledges that this set of features should be enabled. + fn ack_features(&mut self, value: u64) { + let _ = value; + } + + /// Reads this device configuration space at `offset`. + fn read_config(&self, _offset: u64, _data: &mut [u8]) { + warn!( + "No readable configuration fields for {}", + VirtioDeviceType::from(self.device_type()) + ); + } + + /// Writes to this device configuration space at `offset`. + fn write_config(&mut self, _offset: u64, _data: &[u8]) { + warn!( + "No writable configuration fields for {}", + VirtioDeviceType::from(self.device_type()) + ); + } + + /// Activates this device for real usage. + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_evt: Arc, + queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult; + + /// Optionally deactivates this device and returns ownership of the guest memory map, interrupt + /// event, and queue events. + fn reset(&mut self) -> Option> { + None + } + + /// Returns the list of shared memory regions required by the device. + fn get_shm_regions(&self) -> Option { + None + } + + /// Updates the list of shared memory regions required by the device. + fn set_shm_regions( + &mut self, + _shm_regions: VirtioSharedMemoryList, + ) -> std::result::Result<(), Error> { + std::unimplemented!() + } + + /// Some devices may need to do some explicit shutdown work. This method + /// may be implemented to do this. The VMM should call shutdown() on + /// every device as part of shutting down the VM. Acting on the device + /// after a shutdown() can lead to unpredictable results. + fn shutdown(&mut self) {} + + fn add_memory_region( + &mut self, + _region: &Arc, + ) -> std::result::Result<(), Error> { + Ok(()) + } + + /// Returns the list of userspace mappings associated with this device. + fn userspace_mappings(&self) -> Vec { + Vec::new() + } + + /// Return the counters that this device exposes + fn counters(&self) -> Option>> { + None + } + + /// Helper to allow common implementation of read_config + fn read_config_from_slice(&self, config: &[u8], offset: u64, mut data: &mut [u8]) { + let config_len = config.len() as u64; + let data_len = data.len() as u64; + if offset + data_len > config_len { + error!( + "Out-of-bound access to configuration: config_len = {} offset = {:x} length = {} for {}", + config_len, + offset, + data_len, + self.device_type() + ); + return; + } + if let Some(end) = offset.checked_add(data.len() as u64) { + data.write_all(&config[offset as usize..std::cmp::min(end, config_len) as usize]) + .unwrap(); + } + } + + /// Set the access platform trait to let the device perform address + /// translations if needed. + fn set_access_platform(&mut self, _access_platform: Arc) {} +} + +/// Trait providing address translation the same way a physical DMA remapping +/// table would provide translation between an IOVA and a physical address. +/// The goal of this trait is to be used by virtio devices to perform the +/// address translation before they try to read from the guest physical address. +/// On the other side, the implementation itself should be provided by the code +/// emulating the IOMMU for the guest. +pub trait DmaRemapping { + /// Provide a way to translate GVA address ranges into GPAs. + fn translate_gva(&self, id: u32, addr: u64) -> std::result::Result; + /// Provide a way to translate GPA address ranges into GVAs. + fn translate_gpa(&self, id: u32, addr: u64) -> std::result::Result; +} + +/// Structure to handle device state common to all devices +#[derive(Default)] +pub struct VirtioCommon { + pub avail_features: u64, + pub acked_features: u64, + pub kill_evt: Option, + pub interrupt_cb: Option>, + pub pause_evt: Option, + pub paused: Arc, + pub paused_sync: Option>, + pub epoll_threads: Option>>, + pub queue_sizes: Vec, + pub queue_evts: Vec<(u16, EventFd)>, + pub device_type: u32, + pub min_queues: u16, + pub access_platform: Option>, +} + +impl VirtioCommon { + pub fn feature_acked(&self, feature: u64) -> bool { + self.acked_features & 1 << feature == 1 << feature + } + + pub fn ack_features(&mut self, value: u64) { + let mut v = value; + // Check if the guest is ACK'ing a feature that we didn't claim to have. + let unrequested_features = v & !self.avail_features; + if unrequested_features != 0 { + warn!("Received acknowledge request for unknown feature."); + + // Don't count these features as acked. + v &= !unrequested_features; + } + self.acked_features |= v; + } + + pub fn activate( + &mut self, + queues: &[(usize, Queue, EventFd)], + interrupt_cb: &Arc, + ) -> ActivateResult { + if queues.len() < self.min_queues.into() { + error!( + "Number of enabled queues lower than min: {} vs {}", + queues.len(), + self.min_queues + ); + return Err(ActivateError::BadActivate); + } + + self.queue_evts = queues + .iter() + .map(|(queue_index, _, queue_evt)| { + queue_evt + .try_clone() + .map(|evt| (*queue_index as u16, evt)) + .map_err(|e| { + error!("failed cloning queue EventFd: {e}"); + ActivateError::BadActivate + }) + }) + .collect::, _>>()?; + + let kill_evt = EventFd::new(EFD_NONBLOCK).map_err(|e| { + error!("failed creating kill EventFd: {}", e); + ActivateError::BadActivate + })?; + self.kill_evt = Some(kill_evt); + + let pause_evt = EventFd::new(EFD_NONBLOCK).map_err(|e| { + error!("failed creating pause EventFd: {}", e); + ActivateError::BadActivate + })?; + self.pause_evt = Some(pause_evt); + + // Save the interrupt EventFD as we need to return it on reset + // but clone it to pass into the thread. + self.interrupt_cb = Some(interrupt_cb.clone()); + + Ok(()) + } + + pub fn reset(&mut self) -> Option> { + self.queue_evts.clear(); + + // We first must resume the virtio thread if it was paused. + if self.pause_evt.take().is_some() { + self.resume().ok()?; + } + + if let Some(kill_evt) = self.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + + if let Some(mut threads) = self.epoll_threads.take() { + for t in threads.drain(..) { + if let Err(e) = t.join() { + error!("Error joining thread: {:?}", e); + } + } + } + + // Return the interrupt + Some(self.interrupt_cb.take().unwrap()) + } + + // Wait for the worker thread to finish and return + pub fn wait_for_epoll_threads(&mut self) { + if let Some(mut threads) = self.epoll_threads.take() { + for t in threads.drain(..) { + if let Err(e) = t.join() { + error!("Error joining thread: {:?}", e); + } + } + } + } + + pub fn dup_eventfds(&self) -> (EventFd, EventFd) { + ( + self.kill_evt.as_ref().unwrap().try_clone().unwrap(), + self.pause_evt.as_ref().unwrap().try_clone().unwrap(), + ) + } + + pub fn set_access_platform(&mut self, access_platform: Arc) { + self.access_platform = Some(access_platform); + // Indirect descriptors feature is not supported when the device + // requires the addresses held by the descriptors to be translated. + self.avail_features &= !(1 << VIRTIO_F_RING_INDIRECT_DESC); + } +} + +impl Pausable for VirtioCommon { + fn pause(&mut self) -> std::result::Result<(), MigratableError> { + debug!( + "Pausing virtio-{}", + VirtioDeviceType::from(self.device_type) + ); + self.paused.store(true, Ordering::SeqCst); + if let Some(pause_evt) = &self.pause_evt { + pause_evt + .write(1) + .map_err(|e| MigratableError::Pause(e.into()))?; + + // Wait for all threads to acknowledge the pause before going + // any further. This is exclusively performed when pause_evt + // eventfd is Some(), as this means the virtio device has been + // activated. One specific case where the device can be paused + // while it hasn't been yet activated is snapshot/restore. + self.paused_sync.as_ref().unwrap().wait(); + } + + Ok(()) + } + + fn resume(&mut self) -> std::result::Result<(), MigratableError> { + debug!( + "Resuming virtio-{}", + VirtioDeviceType::from(self.device_type) + ); + self.paused.store(false, Ordering::SeqCst); + if let Some(epoll_threads) = &self.epoll_threads { + for t in epoll_threads.iter() { + t.thread().unpark(); + } + } + + // Signal each activated queue eventfd so workers process restored queues + // that may already contain pending requests. + for (_, queue_evt) in &self.queue_evts { + queue_evt.write(1).map_err(|e| { + MigratableError::Resume(anyhow!( + "Could not notify restored virtio worker on resume: {e}" + )) + })?; + } + + // Also trigger interrupts into the guest to wake up the driver to avoid a "livelock" + // if a used-ring completion interrupt was lost across restore. + // + // Use the real virtio queue index rather than the position in + // `queue_evts`. `VirtioInterruptType::Queue(x)` is defined to carry + // the real queue index, and `VirtioInterruptMsix` indexes + // `queues_vectors` (whose length is `total_queues`, filled by the + // guest driver at real-index positions) with it. When ready queues + // are non-contiguous (e.g. virtio-net control queue at index 2N when + // only a subset of queue pairs are enabled) the position in + // `queue_evts` differs from the real queue index, and using the + // position would either land on a `VIRTQ_MSI_NO_VECTOR` slot + // (silently dropping the interrupt) or on an unrelated queue's vector. + if let Some(interrupt_cb) = &self.interrupt_cb { + for &(queue_index, _) in &self.queue_evts { + interrupt_cb + .trigger(crate::VirtioInterruptType::Queue(queue_index)) + .ok(); + } + } + + Ok(()) + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/epoll_helper.rs b/vendor/cube-hypervisor/virtio-devices/src/epoll_helper.rs new file mode 100644 index 0000000..9f40388 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/epoll_helper.rs @@ -0,0 +1,343 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use std::fs::File; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Barrier}; +use std::thread; +use thiserror::Error; +use vmm_sys_util::eventfd::EventFd; + +pub struct EpollHelper { + pause_evt: EventFd, + epoll_file: File, +} + +#[derive(Error, Debug)] +pub enum EpollHelperError { + #[error("Failed to create Fd: {0}")] + CreateFd(std::io::Error), + #[error("Failed to epoll_ctl: {0}")] + Ctl(std::io::Error), + #[error("IO error: {0}")] + IoError(std::io::Error), + #[error("Failed to epoll_wait: {0}")] + Wait(std::io::Error), + #[error("Failed to get virtio-queue index: {0}")] + QueueRingIndex(virtio_queue::Error), + #[error("Failed to handle virtio device events: {0}")] + HandleEvent(anyhow::Error), + #[error("Failed to handle timeout: {0}")] + HandleTimeout(anyhow::Error), +} + +pub const EPOLL_HELPER_EVENT_PAUSE: u16 = 0; +pub const EPOLL_HELPER_EVENT_KILL: u16 = 1; +pub const EPOLL_HELPER_EVENT_LAST: u16 = 15; + +pub trait EpollHelperHandler { + // Handle one event at a time. The EpollHelper iterates over a list of + // events that have been returned by epoll_wait(). For each event, the + // current method is invoked to let the implementation decide how to process + // the incoming event. + fn handle_event( + &mut self, + helper: &mut EpollHelper, + event: &epoll::Event, + ) -> Result<(), EpollHelperError>; + + // Called when PAUSE_EVENT is received, before the pause barrier and + // park(). Implementations can use this hook to drain inflight work + // and bring device state to a quiescent point so that a subsequent + // snapshot is consistent. The hook MUST NOT propagate errors via + // Err here: returning Err would cause the epoll loop to exit early + // and skip paused_sync.wait(), deadlocking the pausing thread. + // Implementations should swallow failures internally (e.g. via a + // shared atomic flag) and always return Ok. + fn before_pause(&mut self, _helper: &mut EpollHelper) -> Result<(), EpollHelperError> { + Ok(()) + } + + // This method is only invoked if the EpollHelper was configured to call + // epoll_wait() with a valid timeout (different from -1), meaning the call + // won't block forever. When the timeout is reached, and if no even has been + // triggered, this function will be called to let the implementation decide + // how to interpret such situation. By default, it provides a no-op + // implementation. + fn handle_timeout(&mut self, _helper: &mut EpollHelper) -> Result<(), EpollHelperError> { + Ok(()) + } + + // In some situations, it might be useful to know the full list of events + // triggered while waiting on epoll_wait(). And having this list provided + // prior to the iterations over each event might help make some informed + // decisions. This function should not replace handle_event(), otherwise it + // would completely defeat the purpose of having the loop being factorized + // through the EpollHelper structure. + fn event_list( + &mut self, + _helper: &mut EpollHelper, + _events: &[epoll::Event], + ) -> Result<(), EpollHelperError> { + Ok(()) + } +} + +impl EpollHelper { + pub fn new( + kill_evt: &EventFd, + pause_evt: &EventFd, + ) -> std::result::Result { + // Create the epoll file descriptor + let epoll_fd = epoll::create(true).map_err(EpollHelperError::CreateFd)?; + // Use 'File' to enforce closing on 'epoll_fd' + let epoll_file = unsafe { File::from_raw_fd(epoll_fd) }; + + let mut helper = Self { + pause_evt: pause_evt.try_clone().unwrap(), + epoll_file, + }; + + helper.add_event(kill_evt.as_raw_fd(), EPOLL_HELPER_EVENT_KILL)?; + helper.add_event(pause_evt.as_raw_fd(), EPOLL_HELPER_EVENT_PAUSE)?; + Ok(helper) + } + + pub fn add_event(&mut self, fd: RawFd, id: u16) -> std::result::Result<(), EpollHelperError> { + self.add_event_custom(fd, id, epoll::Events::EPOLLIN) + } + + pub fn add_event_custom( + &mut self, + fd: RawFd, + id: u16, + evts: epoll::Events, + ) -> std::result::Result<(), EpollHelperError> { + epoll::ctl( + self.epoll_file.as_raw_fd(), + epoll::ControlOptions::EPOLL_CTL_ADD, + fd, + epoll::Event::new(evts, id.into()), + ) + .map_err(EpollHelperError::Ctl) + } + + pub fn mod_event_custom( + &mut self, + fd: RawFd, + id: u16, + evts: epoll::Events, + ) -> std::result::Result<(), EpollHelperError> { + epoll::ctl( + self.epoll_file.as_raw_fd(), + epoll::ControlOptions::EPOLL_CTL_MOD, + fd, + epoll::Event::new(evts, id.into()), + ) + .map_err(EpollHelperError::Ctl) + } + + pub fn del_event_custom( + &mut self, + fd: RawFd, + id: u16, + evts: epoll::Events, + ) -> std::result::Result<(), EpollHelperError> { + epoll::ctl( + self.epoll_file.as_raw_fd(), + epoll::ControlOptions::EPOLL_CTL_DEL, + fd, + epoll::Event::new(evts, id.into()), + ) + .map_err(EpollHelperError::Ctl) + } + + pub fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + handler: &mut dyn EpollHelperHandler, + ) -> std::result::Result<(), EpollHelperError> { + self.run_with_timeout(paused, paused_sync, handler, -1, false) + } + + #[cfg(not(fuzzing))] + pub fn run_with_timeout( + &mut self, + paused: Arc, + paused_sync: Arc, + handler: &mut dyn EpollHelperHandler, + timeout: i32, + enable_event_list: bool, + ) -> std::result::Result<(), EpollHelperError> { + const EPOLL_EVENTS_LEN: usize = 100; + let mut events = vec![epoll::Event::new(epoll::Events::empty(), 0); EPOLL_EVENTS_LEN]; + + // Before jumping into the epoll loop, check if the device is expected + // to be in a paused state. This is helpful for the restore code path + // as the device thread should not start processing anything before the + // device has been resumed. + while paused.load(Ordering::SeqCst) { + thread::park(); + } + + loop { + let num_events = + match epoll::wait(self.epoll_file.as_raw_fd(), timeout, &mut events[..]) { + Ok(res) => res, + Err(e) => { + if e.kind() == std::io::ErrorKind::Interrupted { + // It's well defined from the epoll_wait() syscall + // documentation that the epoll loop can be interrupted + // before any of the requested events occurred or the + // timeout expired. In both those cases, epoll_wait() + // returns an error of type EINTR, but this should not + // be considered as a regular error. Instead it is more + // appropriate to retry, by calling into epoll_wait(). + continue; + } + return Err(EpollHelperError::Wait(e)); + } + }; + + if num_events == 0 { + // This case happens when the timeout is reached before any of + // the registered events is triggered. + handler.handle_timeout(self)?; + continue; + } + + if enable_event_list { + handler.event_list(self, &events[..num_events])?; + } + + for event in events.iter().take(num_events) { + let ev_type = event.data as u16; + + match ev_type { + EPOLL_HELPER_EVENT_KILL => { + info!("KILL_EVENT received, stopping epoll loop"); + return Ok(()); + } + EPOLL_HELPER_EVENT_PAUSE => { + info!("PAUSE_EVENT received, pausing epoll loop"); + + // Per-device pre-pause hook (e.g. drain inflight IO + // for virtio-blk). Contract: hook always returns Ok + // so we never skip the barrier below. + handler.before_pause(self)?; + + // Acknowledge the pause is effective by using the + // paused_sync barrier. + paused_sync.wait(); + + // We loop here to handle spurious park() returns. + // Until we have not resumed, the paused boolean will + // be true. + while paused.load(Ordering::SeqCst) { + thread::park(); + } + + // Drain pause event after the device has been resumed. + // This ensures the pause event has been seen by each + // thread related to this virtio device. + let _ = self.pause_evt.read(); + } + _ => { + handler.handle_event(self, event)?; + } + } + } + } + } + + #[cfg(fuzzing)] + // Require to have a 'queue_evt' being kicked before calling + // and return when no epoll events are active + pub fn run_with_timeout( + &mut self, + paused: Arc, + paused_sync: Arc, + handler: &mut dyn EpollHelperHandler, + _timeout: i32, + _enable_event_list: bool, + ) -> std::result::Result<(), EpollHelperError> { + const EPOLL_EVENTS_LEN: usize = 100; + let mut events = vec![epoll::Event::new(epoll::Events::empty(), 0); EPOLL_EVENTS_LEN]; + + loop { + let num_events = match epoll::wait(self.epoll_file.as_raw_fd(), 0, &mut events[..]) { + Ok(res) => res, + Err(e) => { + if e.kind() == std::io::ErrorKind::Interrupted { + // It's well defined from the epoll_wait() syscall + // documentation that the epoll loop can be interrupted + // before any of the requested events occurred or the + // timeout expired. In both those cases, epoll_wait() + // returns an error of type EINTR, but this should not + // be considered as a regular error. Instead it is more + // appropriate to retry, by calling into epoll_wait(). + continue; + } + return Err(EpollHelperError::Wait(e)); + } + }; + + // Return when no epoll events are active + if num_events == 0 { + return Ok(()); + } + + for event in events.iter().take(num_events) { + let ev_type = event.data as u16; + + match ev_type { + EPOLL_HELPER_EVENT_KILL => { + info!("KILL_EVENT received, stopping epoll loop"); + return Ok(()); + } + EPOLL_HELPER_EVENT_PAUSE => { + info!("PAUSE_EVENT received, pausing epoll loop"); + + // Per-device pre-pause hook. See the non-fuzzing + // branch for the contract. + handler.before_pause(self)?; + + // Acknowledge the pause is effective by using the + // paused_sync barrier. + paused_sync.wait(); + + // We loop here to handle spurious park() returns. + // Until we have not resumed, the paused boolean will + // be true. + while paused.load(Ordering::SeqCst) { + thread::park(); + } + + // Drain pause event after the device has been resumed. + // This ensures the pause event has been seen by each + // thread related to this virtio device. + let _ = self.pause_evt.read(); + } + _ => { + handler.handle_event(self, event)?; + } + } + } + } + } +} + +impl AsRawFd for EpollHelper { + fn as_raw_fd(&self) -> RawFd { + self.epoll_file.as_raw_fd() + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/fs.rs b/vendor/cube-hypervisor/virtio-devices/src/fs.rs new file mode 100644 index 0000000..78e25e9 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/fs.rs @@ -0,0 +1,1368 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use super::RateLimiterConfig; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::Error as DeviceError; +use crate::VirtioInterruptType; +use crate::{ + ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, + UserspaceMapping, VirtioCommon, VirtioDevice, VirtioDeviceType, VirtioInterrupt, + VirtioSharedMemoryList, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_IN_ORDER, VIRTIO_F_IOMMU_PLATFORM, + VIRTIO_F_NOTIFICATION_DATA, VIRTIO_F_ORDER_PLATFORM, VIRTIO_F_RING_INDIRECT_DESC, + VIRTIO_F_VERSION_1, +}; +use crate::{GuestMemoryMmap, MmapRegion}; +use anyhow::anyhow; +use rate_limiter::{BucketReduction, RateLimiter, TokenType}; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use serde_with::{serde_as, Bytes}; +use std::fs; +use std::io; +use std::num::Wrapping; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::result; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::{Arc, Barrier, Mutex}; +use std::time::Instant; +use std::{collections::HashMap, convert::TryInto}; +use thiserror::Error; +use virtio_queue::{DescriptorChain, Queue, QueueOwnedT, QueueT}; +use virtiofsd::{ + descriptor_utils::{Error as VufDescriptorError, Reader, Writer}, + filesystem::SerializableFileSystem, + fs_cache_req_handler::FsCacheReqHandler, + fuse::{InHeader, OutHeader, RemovemappingOne}, + limits, + passthrough::{ + self, read_only::PassthroughFsRo, xattrmap::XattrMap, CachePolicy, MigrationOnError, + PassthroughFs, + }, + server::Server, + Error as VhostUserFsError, +}; +use vm_memory::{ByteValued, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryLoadGuard}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vmm_sys_util::eventfd::EventFd; + +const NUM_QUEUE_OFFSET: usize = 1; +const DEFAULT_QUEUE_NUMBER: usize = 2; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Processing queue failed: {0}")] + ProcessQueue(VhostUserFsError), + #[error("Creating a queue reader failed: {0}")] + QueueReader(VufDescriptorError), + #[error("Creating a queue writer failed: {0}")] + QueueWriter(VufDescriptorError), +} + +pub type Result = result::Result; + +pub const BACKEND_FS_CACHE_AUTO: u8 = 0; +pub const BACKEND_FS_CACHE_ALWAYS: u8 = 1; +pub const BACKEND_FS_CACHE_NEVER: u8 = 2; +pub const BACKEND_FS_CACHE_NONE: u8 = 3; +pub const DEFAULT_FS_LIMIT_NOFILE: u64 = 0; +pub const DEFAULT_FS_THREAD_POOL_SIZE: usize = 0; + +pub fn default_fsconfig_cache() -> u8 { + BACKEND_FS_CACHE_NEVER +} + +pub fn default_fsconfig_no_readdirplus() -> bool { + true +} + +pub fn default_fsconfig_read_only() -> bool { + true +} + +pub fn default_fsconfig_rlimit_nofile() -> u64 { + DEFAULT_FS_LIMIT_NOFILE +} + +pub fn default_fsconfig_thread_pool_size() -> usize { + DEFAULT_FS_THREAD_POOL_SIZE +} + +pub fn default_fsconfig_xattr() -> bool { + true +} + +#[derive(Serialize, Deserialize)] +pub struct State { + pub avail_features: u64, + pub acked_features: u64, + pub config: VirtioFsConfig, + #[serde(default)] + pub back_state: Vec, +} + +#[serde_as] +#[derive(Copy, Clone, Serialize, Deserialize)] +#[repr(C, packed)] +pub struct VirtioFsConfig { + #[serde_as(as = "Bytes")] + pub tag: [u8; 36], + pub num_request_queues: u32, +} + +impl Default for VirtioFsConfig { + fn default() -> Self { + VirtioFsConfig { + tag: [0; 36], + num_request_queues: 0, + } + } +} + +#[derive(Default, Clone)] +pub struct FsCounters { + total_bytes: Arc, + total_ops: Arc, + min_latency: Arc, + max_latency: Arc, + avg_latency: Arc, + limit_by_bytes: Arc, + limit_by_ops: Arc, +} + +#[derive(Clone)] +pub struct FsEvent { + pub tx: std::sync::mpsc::Sender, + pub backendfs_config: BackendFsConfig, +} + +#[derive(Clone)] +struct CacheHandler { + cache_size: u64, + mmap_cache_addr: u64, +} + +impl CacheHandler { + // Make sure request is within cache range + fn is_req_valid(&self, offset: u64, len: u64) -> bool { + let end = match offset.checked_add(len) { + Some(n) => n, + None => return false, + }; + + !(offset >= self.cache_size || end > self.cache_size) + } +} + +impl FsCacheReqHandler for CacheHandler { + fn map( + &mut self, + foffset: u64, + moffset: u64, + len: u64, + flags: u64, + fd: RawFd, + ) -> result::Result<(), io::Error> { + debug!("fs_slave_map"); + + // Ignore if the length is 0. + if len == 0 { + return Ok(()); + } + + if !self.is_req_valid(moffset, len) { + return Err(io::Error::from_raw_os_error(libc::EINVAL)); + } + + let addr = self.mmap_cache_addr + moffset; + // SAFETY: FFI call with valid arguments + let ret = unsafe { + libc::mmap( + addr as *mut libc::c_void, + len as usize, + flags as i32, + libc::MAP_SHARED | libc::MAP_FIXED, + fd.as_raw_fd(), + foffset as libc::off_t, + ) + }; + if ret == libc::MAP_FAILED { + return Err(io::Error::last_os_error()); + } + + Ok(()) + } + + fn unmap(&mut self, requests: Vec) -> std::result::Result<(), io::Error> { + debug!("fs_slave_unmap"); + + for req in requests { + let mut offset = req.moffset; + let mut len = req.len; + + // Ignore if the length is 0. + if len == 0 { + continue; + } + + // Need to handle a special case where the slave ask for the unmapping + // of the entire mapping. + if len == 0xffff_ffff_ffff_ffff { + len = self.cache_size; + offset = 0; + } + + if !self.is_req_valid(offset, len) { + return Err(io::Error::from_raw_os_error(libc::EINVAL)); + } + + let addr = self.mmap_cache_addr + offset; + // SAFETY: FFI call with valid arguments + let ret = unsafe { + libc::mmap( + addr as *mut libc::c_void, + len as usize, + libc::PROT_NONE, + libc::MAP_ANONYMOUS | libc::MAP_PRIVATE | libc::MAP_FIXED, + -1, + 0, + ) + }; + if ret == libc::MAP_FAILED { + return Err(io::Error::last_os_error()); + } + } + + Ok(()) + } +} + +struct FsEpollHandler { + queue_index: u16, + queue_evt: EventFd, + queue: Queue, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + kill_evt: EventFd, + pause_evt: EventFd, + server: Arc, + cache_handler: Option, + dev_evt: EventFd, + pending_dev_message: Arc>>, + rate_limiter: Option, + counters: FsCounters, + rate_limited: std::sync::Once, + id: String, +} + +// New descriptors are pending on the virtio queue. +const QUEUE_AVAIL_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; +// New 'wake up' event from the rate limiter +const RATE_LIMITER_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 3; +// New event to backend device. +const DEVICE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 4; + +// latency scale, for reduce precision loss in calculate. +const LATENCY_SCALE: u64 = 10000; + +impl FsEpollHandler { + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + + helper.add_event(self.queue_evt.as_raw_fd(), QUEUE_AVAIL_EVENT)?; + if let Some(rate_limiter) = &self.rate_limiter { + helper.add_event(rate_limiter.as_raw_fd(), RATE_LIMITER_EVENT)?; + } + // There always multi queue support for virtiofs, we only + // need one thread to listen the DEVICE_EVENT. + // Here we just add DEVICE_EVENT to queue which index is 0, + // because this queue is always exist. + if self.queue_index == 0 { + helper.add_event(self.dev_evt.as_raw_fd(), DEVICE_EVENT)?; + } + helper.run(paused, paused_sync, self)?; + + Ok(()) + } + + fn signal_used_queue(&self) -> result::Result<(), DeviceError> { + self.interrupt_cb + .trigger(VirtioInterruptType::Queue(self.queue_index)) + .map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + fn return_descriptor(queue: &mut Queue, mem: &GuestMemoryMmap, head_index: u16, len: usize) { + // In FUSE, a single response should never reach 4 GiB; if it ever does + // (most likely a bug or a malformed request) we report 0 bytes used + // and log a warning instead of panicking. Reporting 0 is safer than + // saturating to u32::MAX because the Guest virtio-ring layer rejects + // any used_len that exceeds the descriptor chain's writable capacity; + // a 0-length reply makes the Guest FUSE driver fall back to its + // short-reply / unmatched-reply path and complete the request with + // -EIO, while the worker thread keeps serving the rest of the queue. + let used_len: u32 = u32::try_from(len).unwrap_or_else(|_| { + warn!( + "virtio-fs: response length {} exceeds u32::MAX, reporting 0 used bytes; \ + Guest will see a short reply and fail the request with -EIO", + len + ); + 0 + }); + + if queue.add_used(mem, head_index, used_len).is_err() { + warn!("Couldn't return used descriptors to the ring"); + } + } + + /// Try to peek the FUSE InHeader from a fresh Reader built on a clone of + /// `desc_chain`, returning the request's `unique` id. The original reader + /// used by the FUSE server is unaffected (Reader keeps its own cursor). + /// + /// On any failure (descriptor chain too short, guest memory error, etc.) + /// returns `None`. + fn peek_in_header_unique( + mem: &GuestMemoryMmap, + desc_chain: DescriptorChain>, + ) -> Option { + let mut peek_reader = Reader::new(mem, desc_chain).ok()?; + let in_header: InHeader = peek_reader.read_obj().ok()?; + Some(in_header.unique) + } + + /// Try to write a minimal FUSE error reply (`OutHeader { len: 16, + /// error: -EIO, unique }`) into a freshly built Writer over `desc_chain`, + /// returning the number of bytes actually written (always 16 on success, + /// 0 on failure). + /// + /// This is used as a best-effort recovery path when reader/writer + /// construction or `Server::handle_message` fails: instead of letting the + /// worker thread die, we try to surface a clean `-EIO` to the Guest so + /// the in-flight FUSE request is woken up immediately. + fn try_write_fuse_eio( + mem: &GuestMemoryMmap, + desc_chain: DescriptorChain>, + unique: u64, + ) -> usize { + let mut writer = match Writer::new(mem, desc_chain) { + Ok(w) => w, + Err(_) => return 0, + }; + // Bail out early when the descriptor chain has no room for a full + // OutHeader, otherwise write_obj would partially succeed and report + // a misleading bytes_written back to the Guest. + if writer.available_bytes() < std::mem::size_of::() { + return 0; + } + let header = OutHeader { + len: std::mem::size_of::() as u32, + error: -libc::EIO, + unique, + }; + writer + .write_obj(header) + .map_or(0, |_| writer.bytes_written()) + } + + fn process_queue_serial(&mut self) -> Result { + let queue = &mut self.queue; + let mut cache_handler = self.cache_handler.clone(); + let mut used_descs = false; + let mut total_ops = Wrapping(0); + let mut total_bytes = Wrapping(0); + let ops_last = self.counters.total_ops.load(Ordering::Relaxed) as i64; + let mut avg_latency = self.counters.avg_latency.load(Ordering::Relaxed) as i64; + let mut limit_by_ops = Wrapping(0); + let mut limit_by_bytes = Wrapping(0); + + while let Some(desc_chain) = queue.pop_descriptor_chain(self.mem.memory()) { + let head_index = desc_chain.head_index(); + let mem_ref = desc_chain.memory(); + + // Best-effort: peek the FUSE in_header.unique from a separate + // Reader built on a clone of desc_chain, so we can produce a + // matching FUSE error reply if anything below fails. If even the + // peek fails (bad descriptor chain), fall back to unique=0; the + // Guest driver will then fall back to its short-reply / unmatched + // reply path and complete the request with -EIO. + let peek_unique = Self::peek_in_header_unique(mem_ref, desc_chain.clone()).unwrap_or(0); + + let reader = match Reader::new(mem_ref, desc_chain.clone()) { + Ok(r) => r, + Err(e) => { + warn!( + "virtio-fs: failed to build Reader for request (unique={}): {:?}; \ + replying -EIO and continuing", + peek_unique, e + ); + let written = + Self::try_write_fuse_eio(mem_ref, desc_chain.clone(), peek_unique); + Self::return_descriptor(queue, mem_ref, head_index, written); + used_descs = true; + continue; + } + }; + let writer = match Writer::new(mem_ref, desc_chain.clone()) { + Ok(w) => w, + Err(e) => { + // Without a working Writer there is no way to deliver any + // reply at all; just hand the descriptor back with len=0 + // and let the Guest's FUSE layer recover (modern kernels + // turn a short reply into -EIO). + warn!( + "virtio-fs: failed to build Writer for request (unique={}): {:?}; \ + returning descriptor with len=0", + peek_unique, e + ); + Self::return_descriptor(queue, mem_ref, head_index, 0); + used_descs = true; + continue; + } + }; + + let mut rate_limited = BucketReduction::Success; + let mut rate_limited_type = TokenType::Ops; + let req_start = Instant::now(); + let len = match self.server.handle_message( + reader, + writer, + cache_handler.as_mut(), + &mut self.rate_limiter, + &mut rate_limited, + &mut rate_limited_type, + ) { + Ok(n) => n, + Err(e) => { + // The FUSE server failed mid-way; the original writer may + // contain a partially-written reply with a stale `len` + // field. Build a fresh Writer from a clone of desc_chain + // (which always starts at the writable region's offset 0) + // and write a minimal -EIO header there. This overwrites + // whatever the server may have left behind, so the Guest + // sees a single, well-formed FUSE error reply. + warn!( + "virtio-fs: handle_message failed (unique={}): {:?}; replying -EIO", + peek_unique, e + ); + let written = + Self::try_write_fuse_eio(mem_ref, desc_chain.clone(), peek_unique); + Self::return_descriptor(queue, mem_ref, head_index, written); + used_descs = true; + continue; + } + }; + + if rate_limited != BucketReduction::Success { + match rate_limited_type { + TokenType::Ops => { + limit_by_ops += Wrapping(1); + self.rate_limited + .call_once(|| info!("{} fs ops ratelimit fired", self.id)); + } + TokenType::Bytes => { + limit_by_bytes += Wrapping(1); + self.rate_limited + .call_once(|| info!("{} fs bw ratelimit fired", self.id)); + } + } + + // Stop processing the queue and return this descriptor chain to the + // avail ring, for later processing. + if rate_limited == BucketReduction::Failure { + queue.go_to_previous_position(); + break; + } + } + + let latency = req_start.elapsed().as_micros() as u64; + total_bytes += Wrapping(len as u64); + total_ops += Wrapping(1); + if (ops_last == 0) || (latency < self.counters.min_latency.load(Ordering::Relaxed)) { + self.counters.min_latency.store(latency, Ordering::Relaxed); + } + if latency > self.counters.max_latency.load(Ordering::Relaxed) { + self.counters.max_latency.store(latency, Ordering::Relaxed); + } + if latency > 20_000 { + warn!("Virtio-fs IO latency too long {}", latency); + } + avg_latency = avg_latency + + ((latency * LATENCY_SCALE) as i64 - avg_latency) + / (ops_last + total_ops.0 as i64); + Self::return_descriptor(queue, desc_chain.memory(), head_index, len); + used_descs = true; + + // Proces BucketReduction::OverConsumption. + if let BucketReduction::OverConsumption(_) = rate_limited { + break; + } + } + + self.counters + .total_bytes + .fetch_add(total_bytes.0, Ordering::AcqRel); + self.counters + .total_ops + .fetch_add(total_ops.0, Ordering::AcqRel); + self.counters + .avg_latency + .store(avg_latency as u64, Ordering::Relaxed); + self.counters + .limit_by_bytes + .fetch_add(limit_by_bytes.0, Ordering::AcqRel); + self.counters + .limit_by_ops + .fetch_add(limit_by_ops.0, Ordering::AcqRel); + + Ok(used_descs) + } + + fn handle_event_impl(&mut self) -> result::Result<(), EpollHelperError> { + let needs_notification = self.process_queue_serial().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to process queue (submit): {:?}", e)) + })?; + + if needs_notification { + self.signal_used_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to signal used queue: {:?}", e)) + })? + }; + + Ok(()) + } +} + +impl EpollHelperHandler for FsEpollHandler { + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + QUEUE_AVAIL_EVENT => { + self.queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + let rate_limit_reached = + self.rate_limiter.as_ref().map_or(false, |r| r.is_blocked()); + + // Process the queue only when the rate limit is not reached + if !rate_limit_reached { + self.handle_event_impl()? + } + } + RATE_LIMITER_EVENT => { + if let Some(rate_limiter) = &mut self.rate_limiter { + // Upon rate limiter event, call the rate limiter handler + // and restart processing the queue. + rate_limiter.event_handler().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process rate limiter event: {:?}", + e + )) + })?; + + self.handle_event_impl()? + } else { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected 'RATE_LIMITER_EVENT' when rate_limiter is not enabled." + ))); + } + } + DEVICE_EVENT => { + self.dev_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + for event in self.pending_dev_message.lock().unwrap().drain(..) { + info!( + "Fs device update with configuration {:?}", + event.backendfs_config + ); + let ret = self + .server + .update_filter(&event.backendfs_config.allowed_dirs) + .map_or_else( + |e| { + info!("Failed to update filter: {:?}", e); + false + }, + |_| true, + ); + let _ = event.tx.send(ret); + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected event: {}", + ev_type + ))); + } + } + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct BackendFsConfig { + #[serde(default)] + pub shared_dir: String, + #[serde(default = "default_fsconfig_thread_pool_size")] + pub thread_pool_size: usize, + #[serde(default = "default_fsconfig_xattr")] + pub xattr: bool, + #[serde(default)] + pub posix_acl: bool, + #[serde(default)] + pub xattrmap: Option, + #[serde(default)] + pub announce_submounts: bool, + #[serde(default = "default_fsconfig_cache")] + pub cache: u8, + #[serde(default = "default_fsconfig_no_readdirplus")] + pub no_readdirplus: bool, + #[serde(default)] + pub writeback: bool, + #[serde(default)] + pub allow_direct_io: bool, + #[serde(default = "default_fsconfig_read_only")] + pub read_only: bool, + #[serde(default = "default_fsconfig_rlimit_nofile")] + pub rlimit_nofile: u64, + #[serde(default)] + pub killpriv_v2: bool, + #[serde(default)] + pub security_label: bool, + #[serde(default)] + pub allowed_dirs: Option>, + #[serde(default)] + pub remap_filter: bool, +} + +impl Default for BackendFsConfig { + fn default() -> Self { + BackendFsConfig { + shared_dir: "".to_owned(), + thread_pool_size: default_fsconfig_thread_pool_size(), + xattr: default_fsconfig_xattr(), + posix_acl: false, + xattrmap: None, + announce_submounts: false, + cache: default_fsconfig_cache(), + no_readdirplus: default_fsconfig_no_readdirplus(), + writeback: false, + allow_direct_io: false, + read_only: default_fsconfig_read_only(), + rlimit_nofile: default_fsconfig_rlimit_nofile(), + killpriv_v2: false, + security_label: false, + allowed_dirs: None, + remap_filter: false, + } + } +} + +// SAFETY: only a series of integers +unsafe impl ByteValued for VirtioFsConfig {} + +enum ServerType { + PassthroughFs(Server), + PassthroughFsRo(Server), +} + +#[must_use] +struct BackendSerializationGuard<'a> { + pre_serialization: &'a AtomicBool, +} + +impl<'a> BackendSerializationGuard<'a> { + fn new(pre_serialization: &'a AtomicBool) -> Self { + Self { pre_serialization } + } +} + +impl Drop for BackendSerializationGuard<'_> { + fn drop(&mut self) { + self.pre_serialization.store(false, Ordering::Release); + } +} + +// Macro to forward method calls to the underlying server variant +// Supports both explicit parameter passing and automatic forwarding +macro_rules! forward_to_server { + // Pattern: forward method with explicit arguments + ($self:expr, $method:ident $(, $args:expr)*) => { + match $self { + ServerType::PassthroughFs(server) => server.$method($($args),*), + ServerType::PassthroughFsRo(server) => server.$method($($args),*), + } + }; +} + +// Macro to define a method that forwards to the server with automatic parameter passing +macro_rules! impl_forward_method { + // Pattern: method without generic parameters + ( + $(#[$attr:meta])* + $vis:vis fn $method:ident + ( &$self:ident $(, $param:ident: $param_ty:ty)* ) -> $ret:ty + ) => { + $(#[$attr])* + $vis fn $method ( &$self $(, $param: $param_ty)* ) -> $ret + { + forward_to_server!($self, $method $(, $param)*) + } + }; + + // Pattern: method with generic parameters (using where clause) + ( + $(#[$attr:meta])* + $vis:vis fn $method:ident <$($gen:ident),+> + ( &$self:ident $(, $param:ident: $param_ty:ty)* ) -> $ret:ty + where $($bounds:tt)+ + ) => { + $(#[$attr])* + $vis fn $method <$($gen),+> ( &$self $(, $param: $param_ty)* ) -> $ret + where $($bounds)+ + { + forward_to_server!($self, $method $(, $param)*) + } + }; +} + +impl ServerType { + impl_forward_method! { + #[allow(dead_code)] // Used by virtiofsd migration (not yet enabled in open branch) + fn prepare_serialization(&self, cancel: Arc) -> io::Result<()> + } + + impl_forward_method! { + #[allow(dead_code)] // Used by virtiofsd migration (not yet enabled in open branch) + fn serialize_data(&self) -> io::Result> + } + + impl_forward_method! { + fn deserialize_and_apply_data(&self, data: &Vec) -> io::Result<()> + } + + impl_forward_method! { + fn update_filter(&self, whitelist: &Option>) -> virtiofsd::Result<()> + } + + impl_forward_method! { + #[allow(clippy::cognitive_complexity)] + pub fn handle_message( + &self, + r: Reader, + w: Writer, + vu_req: Option<&mut T>, + rl: &mut Option, + rlret: &mut BucketReduction, + rltyp: &mut TokenType + ) -> virtiofsd::Result + where T: FsCacheReqHandler + } +} + +pub struct Fs { + common: VirtioCommon, + id: String, + config: VirtioFsConfig, + seccomp_action: SeccompAction, + exit_evt: EventFd, + backendfs_config: BackendFsConfig, + cache: Option<(VirtioSharedMemoryList, MmapRegion)>, + rate_limiter_config: Option, + dev_evt: EventFd, + pending_dev_message: Arc>>, + counters: FsCounters, + server: Option>, + back_state: Vec, + restore: bool, + #[allow(dead_code)] // Used by virtiofsd migration (not yet enabled in open branch) + pre_serialization: AtomicBool, +} + +impl Fs { + /// Create a new virtio-fs device. + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + tag: &str, + req_num_queues: usize, + queue_size: u16, + seccomp_action: SeccompAction, + exit_evt: EventFd, + iommu: bool, + state: Option, + backendfs_config: &BackendFsConfig, + cache: Option<(VirtioSharedMemoryList, MmapRegion)>, + rate_limiter_config: Option, + dev_evt: EventFd, + pending_dev_message: Arc>>, + ) -> io::Result { + let mut back_state = Vec::new(); + // Calculate the actual number of queues needed. + let num_queues = NUM_QUEUE_OFFSET + req_num_queues; + if num_queues > DEFAULT_QUEUE_NUMBER { + error!( + "virtio-fs requested too many queues ({}) since the backend only supports {}\n", + num_queues, DEFAULT_QUEUE_NUMBER + ); + return Err(io::Error::new( + io::ErrorKind::Other, + format!("requested too many queues"), + )); + } + + let (avail_features, acked_features, config, paused) = if let Some(state) = state { + debug!("Restoring virtio-fs {}", id); + back_state = state.back_state; + + ( + state.avail_features, + state.acked_features, + state.config, + true, + ) + } else { + // Filling device and vring features VMM supports. + let mut avail_features: u64 = 1 << VIRTIO_F_RING_INDIRECT_DESC + | 1 << VIRTIO_F_VERSION_1 + | 1 << VIRTIO_F_IN_ORDER + | 1 << VIRTIO_F_ORDER_PLATFORM + | 1 << VIRTIO_F_NOTIFICATION_DATA; + + if iommu { + avail_features |= 1 << VIRTIO_F_IOMMU_PLATFORM; + } + + // Create virtio-fs device configuration. + let mut config = VirtioFsConfig::default(); + let tag_bytes_vec = tag.to_string().into_bytes(); + config.tag[..tag_bytes_vec.len()].copy_from_slice(tag_bytes_vec.as_slice()); + config.num_request_queues = req_num_queues as u32; + + (avail_features, 0, config, false) + }; + Ok(Fs { + common: VirtioCommon { + device_type: VirtioDeviceType::Fs as u32, + avail_features, + acked_features, + queue_sizes: vec![queue_size; num_queues], + paused_sync: Some(Arc::new(Barrier::new(num_queues + 1))), + min_queues: 1, + paused: Arc::new(AtomicBool::new(paused)), + ..Default::default() + }, + id, + config, + seccomp_action, + exit_evt, + backendfs_config: backendfs_config.clone(), + cache, + rate_limiter_config, + dev_evt, + pending_dev_message, + counters: FsCounters::default(), + server: None, + back_state, + restore: paused, + pre_serialization: AtomicBool::new(false), + }) + } + + fn state(&self) -> State { + State { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + config: self.config, + back_state: Vec::new(), + } + } + + fn init_backend_fs_server( + &self, + backendfs_config: &BackendFsConfig, + ) -> result::Result, ActivateError> { + let shared_dir = &backendfs_config.shared_dir; + let shared_dir_rp = + fs::canonicalize(shared_dir).map_err(ActivateError::ActivateVirtioFs)?; + let shared_dir_rp_str = shared_dir_rp + .to_str() + .ok_or_else(|| io::Error::from_raw_os_error(libc::EINVAL)) + .map_err(ActivateError::ActivateVirtioFs)?; + + let xattrmap = backendfs_config + .xattrmap + .as_ref() + .map(|s| XattrMap::try_from(s.as_str()).unwrap()); + + let cache = match backendfs_config.cache { + BACKEND_FS_CACHE_AUTO => CachePolicy::Auto, + BACKEND_FS_CACHE_ALWAYS => CachePolicy::Always, + BACKEND_FS_CACHE_NEVER => CachePolicy::Never, + BACKEND_FS_CACHE_NONE => CachePolicy::None, + num => { + return Err(ActivateError::ActivateVirtioFs(io::Error::other(format!( + "unknown cache policy: {}, valid input: 0(auto), 1(always), 2(never), 3(none)", + num + )))) + } + }; + let readdirplus = match cache { + CachePolicy::Never => false, + CachePolicy::None => false, + _ => !backendfs_config.no_readdirplus, + }; + + limits::setup_rlimit_nofile(Some(backendfs_config.rlimit_nofile)) + .map_err(|error| io::Error::other(format!("setup rlimit nofile error {:?}", error))) + .map_err(ActivateError::ActivateVirtioFs)?; + + let fs_cfg = passthrough::Config { + cache_policy: cache, + root_dir: shared_dir_rp_str.into(), + mountinfo_prefix: None, + xattr: backendfs_config.xattr, + xattrmap, + proc_sfd_rawfd: None, + proc_mountinfo_rawfd: None, + announce_submounts: backendfs_config.announce_submounts, + readdirplus, + writeback: backendfs_config.writeback, + allow_direct_io: backendfs_config.allow_direct_io, + killpriv_v2: backendfs_config.killpriv_v2, + security_label: backendfs_config.security_label, + posix_acl: backendfs_config.posix_acl, + // Hardcoded override (not driven by BackendFsConfig): surface + // per-inode restore failures to the guest instead of aborting + // the whole live migration. Upstream default is `Abort`. + migration_on_error: MigrationOnError::GuestError, + ..Default::default() + }; + + if backendfs_config.read_only { + let fs = PassthroughFsRo::new(fs_cfg).map_err(|e| { + ActivateError::ActivateVirtioFs(io::Error::other(format!( + "Failed to create internal ro filesystem representation: {e:?}", + ))) + })?; + let server = Server::new(fs, &self.backendfs_config.allowed_dirs) + .map_err(ActivateError::InvalidServerConfig)?; + server.set_remap_filter_enabled(self.backendfs_config.remap_filter); + Ok(Arc::new(ServerType::PassthroughFsRo(server))) + } else { + let fs = PassthroughFs::new(fs_cfg).map_err(|e| { + ActivateError::ActivateVirtioFs(io::Error::other(format!( + "Failed to create internal filesystem representation: {e:?}", + ))) + })?; + let server = Server::new(fs, &self.backendfs_config.allowed_dirs) + .map_err(ActivateError::InvalidServerConfig)?; + server.set_remap_filter_enabled(self.backendfs_config.remap_filter); + Ok(Arc::new(ServerType::PassthroughFs(server))) + } + } + + fn prepare_backend_serialization( + &self, + server: &ServerType, + ) -> std::result::Result<(), MigratableError> { + let start = Instant::now(); + let cancel = Arc::new(AtomicBool::new(false)); + debug!("serialize prepare"); + server.prepare_serialization(cancel).map_err(|e| { + warn!("{} preserialization failed ({:?})", self.id, e); + MigratableError::Snapshot(anyhow!("serialize prepare failed: {:?}", e)) + })?; + self.pre_serialization.store(true, Ordering::Release); + info!( + "{} preserialization success after {}(us)", + self.id, + start.elapsed().as_micros() + ); + Ok(()) + } + + fn prepare_backend_serialization_if_needed( + &self, + server: &ServerType, + ) -> std::result::Result<(), MigratableError> { + if self.pre_serialization.load(Ordering::Acquire) { + return Ok(()); + } + self.prepare_backend_serialization(server) + } +} + +impl Drop for Fs { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +impl VirtioDevice for Fs { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.as_slice(), offset, data); + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + let server = self.init_backend_fs_server(&self.backendfs_config)?; + if self.restore { + if !self.back_state.is_empty() { + let start = Instant::now(); + server + .deserialize_and_apply_data(self.back_state.as_ref()) + .map_err(|e| { + warn!("{} deserialization failed ({:?})", self.id, e); + ActivateError::InvalidVirtioFsState(e) + })?; + info!( + "{} deserialization success after {}(us)", + self.id, + start.elapsed().as_micros() + ); + } + self.restore = false; + } + self.server = Some(server.clone()); + let mut epoll_threads = Vec::new(); + for i in 0..queues.len() { + let (_, queue, queue_evt) = queues.remove(0); + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let rate_limiter: Option = self + .rate_limiter_config + .map(RateLimiterConfig::try_into) + .transpose() + .map_err(ActivateError::CreateRateLimiter)?; + + let cache_handler = if let Some(cache) = self.cache.as_ref() { + let handler = CacheHandler { + cache_size: cache.0.len, + mmap_cache_addr: cache.0.host_addr, + }; + + Some(handler) + } else { + None + }; + let mut handler = FsEpollHandler { + queue_index: i as u16, + queue_evt, + queue, + mem: mem.clone(), + interrupt_cb: interrupt_cb.clone(), + kill_evt, + pause_evt, + server: server.clone(), + cache_handler, + dev_evt: self.dev_evt.try_clone().unwrap(), + pending_dev_message: self.pending_dev_message.clone(), + rate_limiter, + counters: self.counters.clone(), + rate_limited: std::sync::Once::new(), + id: self.id.clone(), + }; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + + spawn_virtio_thread( + &format!("{}_q{}", self.id.clone(), i), + &self.seccomp_action, + Thread::VirtioFs, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + } + + self.common.epoll_threads = Some(epoll_threads); + event!("virtio-device", "activated", "id", &self.id); + + Ok(()) + } + + fn reset(&mut self) -> Option> { + // We first must resume the virtio thread if it was paused. + if self.common.pause_evt.take().is_some() { + self.common.resume().ok()?; + } + + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + + event!("virtio-device", "reset", "id", &self.id); + + // Return the interrupt + Some(self.common.interrupt_cb.take().unwrap()) + } + + fn get_shm_regions(&self) -> Option { + self.cache.as_ref().map(|cache| cache.0.clone()) + } + + fn set_shm_regions( + &mut self, + shm_regions: VirtioSharedMemoryList, + ) -> std::result::Result<(), crate::Error> { + if let Some(cache) = self.cache.as_mut() { + cache.0 = shm_regions; + Ok(()) + } else { + Err(crate::Error::SetShmRegionsNotSupported) + } + } + + fn userspace_mappings(&self) -> Vec { + let mut mappings = Vec::new(); + if let Some(cache) = self.cache.as_ref() { + mappings.push(UserspaceMapping { + host_addr: cache.0.host_addr, + mem_slot: cache.0.mem_slot, + addr: cache.0.addr, + len: cache.0.len, + mergeable: false, + }) + } + + mappings + } + + fn counters(&self) -> Option>> { + let mut counters = HashMap::new(); + + counters.insert( + "total_bytes", + Wrapping(self.counters.total_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "total_ops", + Wrapping(self.counters.total_ops.load(Ordering::Acquire)), + ); + counters.insert( + "min_latency", + Wrapping(self.counters.min_latency.load(Ordering::Acquire)), + ); + counters.insert( + "max_latency", + Wrapping(self.counters.max_latency.load(Ordering::Acquire)), + ); + counters.insert( + "avg_latency", + Wrapping(self.counters.avg_latency.load(Ordering::Acquire) / LATENCY_SCALE), + ); + counters.insert( + "limit_by_bytes", + Wrapping(self.counters.limit_by_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "limit_by_ops", + Wrapping(self.counters.limit_by_ops.load(Ordering::Acquire)), + ); + + Some(counters) + } +} + +impl Pausable for Fs { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume() + } +} + +impl Snapshottable for Fs { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + let mut state = self.state(); + if let Some(server) = &self.server { + self.prepare_backend_serialization_if_needed(server)?; + let _serialization_cleanup = BackendSerializationGuard::new(&self.pre_serialization); + let start = Instant::now(); + let result = server + .serialize_data() + .map_err(|e| { + warn!("{} serialization failed ({:?})", self.id, e); + MigratableError::Snapshot(anyhow!("serialize process failed: {:?}", e)) + }) + .map(|data| { + state.back_state = data; + }); + result?; + info!( + "{} serialization success after {}(us)", + self.id, + start.elapsed().as_micros() + ); + } + Snapshot::new_from_state(&self.id(), &state) + } +} + +impl Transportable for Fs {} + +impl Migratable for Fs { + fn start_migration(&mut self) -> std::result::Result<(), MigratableError> { + if let Some(server) = &self.server { + self.prepare_backend_serialization(server)?; + } + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + use std::path::{Path, PathBuf}; + use std::time::{SystemTime, UNIX_EPOCH}; + + struct TestDir { + path: PathBuf, + } + + impl TestDir { + fn new(name: &str) -> Self { + let unique = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + let path = std::env::temp_dir().join(format!( + "virtio-devices-fs-{name}-{}-{unique}", + std::process::id() + )); + fs::create_dir_all(&path).unwrap(); + Self { path } + } + + fn path(&self) -> &Path { + &self.path + } + } + + impl Drop for TestDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.path); + } + } + + #[test] + fn backend_serialization_guard_clears_marker_on_drop() { + let pre_serialization = AtomicBool::new(true); + + { + let _guard = BackendSerializationGuard::new(&pre_serialization); + assert!(pre_serialization.load(Ordering::Acquire)); + } + + assert!(!pre_serialization.load(Ordering::Acquire)); + } + + fn new_test_fs(root: &Path) -> Fs { + let backendfs_config = BackendFsConfig { + shared_dir: root.to_string_lossy().into_owned(), + read_only: false, + cache: BACKEND_FS_CACHE_NONE, + ..Default::default() + }; + let mut fs = Fs::new( + "testfs".to_string(), + "testfs", + 1, + 128, + SeccompAction::Allow, + EventFd::new(libc::EFD_NONBLOCK).unwrap(), + false, + None, + &backendfs_config, + None, + None, + EventFd::new(libc::EFD_NONBLOCK).unwrap(), + Arc::new(Mutex::new(Vec::new())), + ) + .unwrap(); + + let passthrough = PassthroughFs::new(passthrough::Config { + root_dir: root.to_string_lossy().into_owned(), + ..Default::default() + }) + .unwrap(); + passthrough.open_root_node().unwrap(); + fs.server = Some(Arc::new(ServerType::PassthroughFs( + Server::new(passthrough, &Option::>::None).unwrap(), + ))); + fs + } + + #[test] + fn snapshot_prepares_backend_serialization_when_needed() { + let root = TestDir::new("snapshot-prepare"); + let mut fs = new_test_fs(root.path()); + + let snapshot = fs.snapshot().unwrap(); + let state: State = snapshot.to_state("testfs").unwrap(); + + assert!(!state.back_state.is_empty()); + assert!(!fs.pre_serialization.load(Ordering::Acquire)); + } + + #[test] + fn snapshot_reuses_started_migration_and_clears_marker() { + let root = TestDir::new("snapshot-reuse"); + let mut fs = new_test_fs(root.path()); + + fs.start_migration().unwrap(); + assert!(fs.pre_serialization.load(Ordering::Acquire)); + + let snapshot = fs.snapshot().unwrap(); + let state: State = snapshot.to_state("testfs").unwrap(); + + assert!(!state.back_state.is_empty()); + assert!(!fs.pre_serialization.load(Ordering::Acquire)); + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/iommu.rs b/vendor/cube-hypervisor/virtio-devices/src/iommu.rs new file mode 100644 index 0000000..6a4190b --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/iommu.rs @@ -0,0 +1,1097 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use super::Error as DeviceError; +use super::{ + ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, VirtioCommon, VirtioDevice, + VirtioDeviceType, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_VERSION_1, +}; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::GuestMemoryMmap; +use crate::{DmaRemapping, VirtioInterrupt, VirtioInterruptType}; +use anyhow::anyhow; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; +use std::io; +use std::mem::size_of; +use std::ops::Bound::Included; +use std::os::unix::io::AsRawFd; +use std::result; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Barrier, Mutex, RwLock}; +use thiserror::Error; +use virtio_queue::{DescriptorChain, Queue, QueueT}; +use vm_device::dma_mapping::ExternalDmaMapping; +use vm_memory::{ + Address, ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, + GuestMemoryError, GuestMemoryLoadGuard, +}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vm_virtio::AccessPlatform; +use vmm_sys_util::eventfd::EventFd; + +/// Queues sizes +const QUEUE_SIZE: u16 = 256; +const NUM_QUEUES: usize = 2; +const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE; NUM_QUEUES]; + +/// New descriptors are pending on the request queue. +/// "requestq" is meant to be used anytime an action is required to be +/// performed on behalf of the guest driver. +const REQUEST_Q_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; +/// New descriptors are pending on the event queue. +/// "eventq" lets the device report any fault or other asynchronous event to +/// the guest driver. +const _EVENT_Q_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2; + +/// PROBE properties size. +/// This is the minimal size to provide at least one RESV_MEM property. +/// Because virtio-iommu expects one MSI reserved region, we must provide it, +/// otherwise the driver in the guest will define a predefined one between +/// 0x8000000 and 0x80FFFFF, which is only relevant for ARM architecture, but +/// will conflict with x86. +const PROBE_PROP_SIZE: u32 = + (size_of::() + size_of::()) as u32; + +/// Virtio IOMMU features +#[allow(unused)] +const VIRTIO_IOMMU_F_INPUT_RANGE: u32 = 0; +#[allow(unused)] +const VIRTIO_IOMMU_F_DOMAIN_RANGE: u32 = 1; +#[allow(unused)] +const VIRTIO_IOMMU_F_MAP_UNMAP: u32 = 2; +#[allow(unused)] +const VIRTIO_IOMMU_F_BYPASS: u32 = 3; +const VIRTIO_IOMMU_F_PROBE: u32 = 4; +#[allow(unused)] +const VIRTIO_IOMMU_F_MMIO: u32 = 5; +const VIRTIO_IOMMU_F_BYPASS_CONFIG: u32 = 6; + +// Support 2MiB and 4KiB page sizes. +const VIRTIO_IOMMU_PAGE_SIZE_MASK: u64 = (2 << 20) | (4 << 10); + +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +#[allow(dead_code)] +struct VirtioIommuRange32 { + start: u32, + end: u32, +} + +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +#[allow(dead_code)] +struct VirtioIommuRange64 { + start: u64, + end: u64, +} + +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +#[allow(dead_code)] +struct VirtioIommuConfig { + page_size_mask: u64, + input_range: VirtioIommuRange64, + domain_range: VirtioIommuRange32, + probe_size: u32, + bypass: u8, + _reserved: [u8; 7], +} + +/// Virtio IOMMU request type +const VIRTIO_IOMMU_T_ATTACH: u8 = 1; +const VIRTIO_IOMMU_T_DETACH: u8 = 2; +const VIRTIO_IOMMU_T_MAP: u8 = 3; +const VIRTIO_IOMMU_T_UNMAP: u8 = 4; +const VIRTIO_IOMMU_T_PROBE: u8 = 5; + +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +struct VirtioIommuReqHead { + type_: u8, + _reserved: [u8; 3], +} + +/// Virtio IOMMU request status +const VIRTIO_IOMMU_S_OK: u8 = 0; +#[allow(unused)] +const VIRTIO_IOMMU_S_IOERR: u8 = 1; +#[allow(unused)] +const VIRTIO_IOMMU_S_UNSUPP: u8 = 2; +#[allow(unused)] +const VIRTIO_IOMMU_S_DEVERR: u8 = 3; +#[allow(unused)] +const VIRTIO_IOMMU_S_INVAL: u8 = 4; +#[allow(unused)] +const VIRTIO_IOMMU_S_RANGE: u8 = 5; +#[allow(unused)] +const VIRTIO_IOMMU_S_NOENT: u8 = 6; +#[allow(unused)] +const VIRTIO_IOMMU_S_FAULT: u8 = 7; +#[allow(unused)] +const VIRTIO_IOMMU_S_NOMEM: u8 = 8; + +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +#[allow(dead_code)] +struct VirtioIommuReqTail { + status: u8, + _reserved: [u8; 3], +} + +/// ATTACH request +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +struct VirtioIommuReqAttach { + domain: u32, + endpoint: u32, + flags: u32, + _reserved: [u8; 4], +} + +const VIRTIO_IOMMU_ATTACH_F_BYPASS: u32 = 1; + +/// DETACH request +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +struct VirtioIommuReqDetach { + domain: u32, + endpoint: u32, + _reserved: [u8; 8], +} + +/// Virtio IOMMU request MAP flags +#[allow(unused)] +const VIRTIO_IOMMU_MAP_F_READ: u32 = 1; +#[allow(unused)] +const VIRTIO_IOMMU_MAP_F_WRITE: u32 = 1 << 1; +#[allow(unused)] +const VIRTIO_IOMMU_MAP_F_MMIO: u32 = 1 << 2; +#[allow(unused)] +const VIRTIO_IOMMU_MAP_F_MASK: u32 = + VIRTIO_IOMMU_MAP_F_READ | VIRTIO_IOMMU_MAP_F_WRITE | VIRTIO_IOMMU_MAP_F_MMIO; + +/// MAP request +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +struct VirtioIommuReqMap { + domain: u32, + virt_start: u64, + virt_end: u64, + phys_start: u64, + _flags: u32, +} + +/// UNMAP request +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +struct VirtioIommuReqUnmap { + domain: u32, + virt_start: u64, + virt_end: u64, + _reserved: [u8; 4], +} + +/// Virtio IOMMU request PROBE types +#[allow(unused)] +const VIRTIO_IOMMU_PROBE_T_NONE: u16 = 0; +const VIRTIO_IOMMU_PROBE_T_RESV_MEM: u16 = 1; +#[allow(unused)] +const VIRTIO_IOMMU_PROBE_T_MASK: u16 = 0xfff; + +/// PROBE request +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +#[allow(dead_code)] +struct VirtioIommuReqProbe { + endpoint: u32, + _reserved: [u64; 8], +} + +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +#[allow(dead_code)] +struct VirtioIommuProbeProperty { + type_: u16, + length: u16, +} + +/// Virtio IOMMU request PROBE property RESV_MEM subtypes +#[allow(unused)] +const VIRTIO_IOMMU_RESV_MEM_T_RESERVED: u8 = 0; +const VIRTIO_IOMMU_RESV_MEM_T_MSI: u8 = 1; + +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +#[allow(dead_code)] +struct VirtioIommuProbeResvMem { + subtype: u8, + _reserved: [u8; 3], + start: u64, + end: u64, +} + +/// Virtio IOMMU fault flags +#[allow(unused)] +const VIRTIO_IOMMU_FAULT_F_READ: u32 = 1; +#[allow(unused)] +const VIRTIO_IOMMU_FAULT_F_WRITE: u32 = 1 << 1; +#[allow(unused)] +const VIRTIO_IOMMU_FAULT_F_EXEC: u32 = 1 << 2; +#[allow(unused)] +const VIRTIO_IOMMU_FAULT_F_ADDRESS: u32 = 1 << 8; + +/// Virtio IOMMU fault reasons +#[allow(unused)] +const VIRTIO_IOMMU_FAULT_R_UNKNOWN: u32 = 0; +#[allow(unused)] +const VIRTIO_IOMMU_FAULT_R_DOMAIN: u32 = 1; +#[allow(unused)] +const VIRTIO_IOMMU_FAULT_R_MAPPING: u32 = 2; + +/// Fault reporting through eventq +#[allow(unused)] +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +struct VirtioIommuFault { + reason: u8, + reserved: [u8; 3], + flags: u32, + endpoint: u32, + reserved2: [u8; 4], + address: u64, +} + +// SAFETY: these data structures only contain integers and have no implicit padding +unsafe impl ByteValued for VirtioIommuRange32 {} +unsafe impl ByteValued for VirtioIommuRange64 {} +unsafe impl ByteValued for VirtioIommuConfig {} +unsafe impl ByteValued for VirtioIommuReqHead {} +unsafe impl ByteValued for VirtioIommuReqTail {} +unsafe impl ByteValued for VirtioIommuReqAttach {} +unsafe impl ByteValued for VirtioIommuReqDetach {} +unsafe impl ByteValued for VirtioIommuReqMap {} +unsafe impl ByteValued for VirtioIommuReqUnmap {} +unsafe impl ByteValued for VirtioIommuReqProbe {} +unsafe impl ByteValued for VirtioIommuProbeProperty {} +unsafe impl ByteValued for VirtioIommuProbeResvMem {} +unsafe impl ByteValued for VirtioIommuFault {} + +#[derive(Error, Debug)] +enum Error { + #[error("Guest gave us bad memory addresses: {0}")] + GuestMemory(GuestMemoryError), + #[error("Guest gave us a write only descriptor that protocol says to read from")] + UnexpectedWriteOnlyDescriptor, + #[error("Guest gave us a read only descriptor that protocol says to write to")] + UnexpectedReadOnlyDescriptor, + #[error("Guest gave us too few descriptors in a descriptor chain")] + DescriptorChainTooShort, + #[error("Guest gave us a buffer that was too short to use")] + BufferLengthTooSmall, + #[error("Guest sent us invalid request")] + InvalidRequest, + #[error("Guest sent us invalid ATTACH request")] + InvalidAttachRequest, + #[error("Guest sent us invalid DETACH request")] + InvalidDetachRequest, + #[error("Guest sent us invalid MAP request")] + InvalidMapRequest, + #[error("Invalid to map because the domain is in bypass mode")] + InvalidMapRequestBypassDomain, + #[error("Invalid to map because the domain is missing")] + InvalidMapRequestMissingDomain, + #[error("Guest sent us invalid UNMAP request")] + InvalidUnmapRequest, + #[error("Invalid to unmap because the domain is in bypass mode")] + InvalidUnmapRequestBypassDomain, + #[error("Invalid to unmap because the domain is missing")] + InvalidUnmapRequestMissingDomain, + #[error("Guest sent us invalid PROBE request")] + InvalidProbeRequest, + #[error("Failed to performing external mapping: {0}")] + ExternalMapping(io::Error), + #[error("Failed to performing external unmapping: {0}")] + ExternalUnmapping(io::Error), + #[error("Failed adding used index: {0}")] + QueueAddUsed(virtio_queue::Error), +} + +struct Request {} + +impl Request { + // Parse the available vring buffer. Based on the hashmap table of external + // mappings required from various devices such as VFIO or vhost-user ones, + // this function might update the hashmap table of external mappings per + // domain. + // Basically, the VMM knows about the device_id <=> mapping relationship + // before running the VM, but at runtime, a new domain <=> mapping hashmap + // is created based on the information provided from the guest driver for + // virtio-iommu (giving the link device_id <=> domain). + fn parse( + desc_chain: &mut DescriptorChain>, + mapping: &Arc, + ext_mapping: &BTreeMap>, + msi_iova_space: (u64, u64), + ) -> result::Result { + let desc = desc_chain + .next() + .ok_or(Error::DescriptorChainTooShort) + .map_err(|e| { + error!("Missing head descriptor"); + e + })?; + + // The descriptor contains the request type which MUST be readable. + if desc.is_write_only() { + return Err(Error::UnexpectedWriteOnlyDescriptor); + } + + if (desc.len() as usize) < size_of::() { + return Err(Error::InvalidRequest); + } + + let req_head: VirtioIommuReqHead = desc_chain + .memory() + .read_obj(desc.addr()) + .map_err(Error::GuestMemory)?; + let req_offset = size_of::(); + let desc_size_left = (desc.len() as usize) - req_offset; + let req_addr = if let Some(addr) = desc.addr().checked_add(req_offset as u64) { + addr + } else { + return Err(Error::InvalidRequest); + }; + + let (msi_iova_start, msi_iova_end) = msi_iova_space; + + // Create the reply + let mut reply: Vec = Vec::new(); + let mut status = VIRTIO_IOMMU_S_OK; + let mut hdr_len = 0; + + let result = (|| { + match req_head.type_ { + VIRTIO_IOMMU_T_ATTACH => { + if desc_size_left != size_of::() { + status = VIRTIO_IOMMU_S_INVAL; + return Err(Error::InvalidAttachRequest); + } + + let req: VirtioIommuReqAttach = desc_chain + .memory() + .read_obj(req_addr as GuestAddress) + .map_err(Error::GuestMemory)?; + debug!("Attach request {:?}", req); + + // Copy the value to use it as a proper reference. + let domain_id = req.domain; + let endpoint = req.endpoint; + let bypass = + (req.flags & VIRTIO_IOMMU_ATTACH_F_BYPASS) == VIRTIO_IOMMU_ATTACH_F_BYPASS; + + // Add endpoint associated with specific domain + mapping + .endpoints + .write() + .unwrap() + .insert(endpoint, domain_id); + + // Add new domain with no mapping if the entry didn't exist yet + let mut domains = mapping.domains.write().unwrap(); + let domain = Domain { + mappings: BTreeMap::new(), + bypass, + }; + domains.entry(domain_id).or_insert_with(|| domain); + } + VIRTIO_IOMMU_T_DETACH => { + if desc_size_left != size_of::() { + status = VIRTIO_IOMMU_S_INVAL; + return Err(Error::InvalidDetachRequest); + } + + let req: VirtioIommuReqDetach = desc_chain + .memory() + .read_obj(req_addr as GuestAddress) + .map_err(Error::GuestMemory)?; + debug!("Detach request {:?}", req); + + // Copy the value to use it as a proper reference. + let domain_id = req.domain; + let endpoint = req.endpoint; + + // Remove endpoint associated with specific domain + mapping.endpoints.write().unwrap().remove(&endpoint); + + // After all endpoints have been successfully detached from a + // domain, the domain can be removed. This means we must remove + // the mappings associated with this domain. + if mapping + .endpoints + .write() + .unwrap() + .iter() + .filter(|(_, &d)| d == domain_id) + .count() + == 0 + { + mapping.domains.write().unwrap().remove(&domain_id); + } + } + VIRTIO_IOMMU_T_MAP => { + if desc_size_left != size_of::() { + status = VIRTIO_IOMMU_S_INVAL; + return Err(Error::InvalidMapRequest); + } + + let req: VirtioIommuReqMap = desc_chain + .memory() + .read_obj(req_addr as GuestAddress) + .map_err(Error::GuestMemory)?; + debug!("Map request {:?}", req); + + // Copy the value to use it as a proper reference. + let domain_id = req.domain; + + if let Some(domain) = mapping.domains.read().unwrap().get(&domain_id) { + if domain.bypass { + status = VIRTIO_IOMMU_S_INVAL; + return Err(Error::InvalidMapRequestBypassDomain); + } + } else { + status = VIRTIO_IOMMU_S_INVAL; + return Err(Error::InvalidMapRequestMissingDomain); + } + + // Find the list of endpoints attached to the given domain. + let endpoints: Vec = mapping + .endpoints + .write() + .unwrap() + .iter() + .filter(|(_, &d)| d == domain_id) + .map(|(&e, _)| e) + .collect(); + + // Trigger external mapping if necessary. + for endpoint in endpoints { + if let Some(ext_map) = ext_mapping.get(&endpoint) { + let size = req.virt_end - req.virt_start + 1; + ext_map + .map(req.virt_start, req.phys_start, size) + .map_err(Error::ExternalMapping)?; + } + } + + // Add new mapping associated with the domain + mapping + .domains + .write() + .unwrap() + .get_mut(&domain_id) + .unwrap() + .mappings + .insert( + req.virt_start, + Mapping { + gpa: req.phys_start, + size: req.virt_end - req.virt_start + 1, + }, + ); + } + VIRTIO_IOMMU_T_UNMAP => { + if desc_size_left != size_of::() { + status = VIRTIO_IOMMU_S_INVAL; + return Err(Error::InvalidUnmapRequest); + } + + let req: VirtioIommuReqUnmap = desc_chain + .memory() + .read_obj(req_addr as GuestAddress) + .map_err(Error::GuestMemory)?; + debug!("Unmap request {:?}", req); + + // Copy the value to use it as a proper reference. + let domain_id = req.domain; + let virt_start = req.virt_start; + + if let Some(domain) = mapping.domains.read().unwrap().get(&domain_id) { + if domain.bypass { + status = VIRTIO_IOMMU_S_INVAL; + return Err(Error::InvalidUnmapRequestBypassDomain); + } + } else { + status = VIRTIO_IOMMU_S_INVAL; + return Err(Error::InvalidUnmapRequestMissingDomain); + } + + // Find the list of endpoints attached to the given domain. + let endpoints: Vec = mapping + .endpoints + .write() + .unwrap() + .iter() + .filter(|(_, &d)| d == domain_id) + .map(|(&e, _)| e) + .collect(); + + // Trigger external unmapping if necessary. + for endpoint in endpoints { + if let Some(ext_map) = ext_mapping.get(&endpoint) { + let size = req.virt_end - virt_start + 1; + ext_map + .unmap(virt_start, size) + .map_err(Error::ExternalUnmapping)?; + } + } + + // Remove mapping associated with the domain + mapping + .domains + .write() + .unwrap() + .get_mut(&domain_id) + .unwrap() + .mappings + .remove(&virt_start); + } + VIRTIO_IOMMU_T_PROBE => { + if desc_size_left != size_of::() { + status = VIRTIO_IOMMU_S_INVAL; + return Err(Error::InvalidProbeRequest); + } + + let req: VirtioIommuReqProbe = desc_chain + .memory() + .read_obj(req_addr as GuestAddress) + .map_err(Error::GuestMemory)?; + debug!("Probe request {:?}", req); + + let probe_prop = VirtioIommuProbeProperty { + type_: VIRTIO_IOMMU_PROBE_T_RESV_MEM, + length: size_of::() as u16, + }; + reply.extend_from_slice(probe_prop.as_slice()); + + let resv_mem = VirtioIommuProbeResvMem { + subtype: VIRTIO_IOMMU_RESV_MEM_T_MSI, + start: msi_iova_start, + end: msi_iova_end, + ..Default::default() + }; + reply.extend_from_slice(resv_mem.as_slice()); + + hdr_len = PROBE_PROP_SIZE; + } + _ => { + status = VIRTIO_IOMMU_S_INVAL; + return Err(Error::InvalidRequest); + } + } + Ok(()) + })(); + + let status_desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?; + + // The status MUST always be writable + if !status_desc.is_write_only() { + return Err(Error::UnexpectedReadOnlyDescriptor); + } + + if status_desc.len() < hdr_len + size_of::() as u32 { + return Err(Error::BufferLengthTooSmall); + } + + let tail = VirtioIommuReqTail { + status, + ..Default::default() + }; + reply.extend_from_slice(tail.as_slice()); + + // Make sure we return the result of the request to the guest before + // we return a potential error internally. + desc_chain + .memory() + .write_slice(reply.as_slice(), status_desc.addr()) + .map_err(Error::GuestMemory)?; + + // Return the error if the result was not Ok(). + result?; + + Ok((hdr_len as usize) + size_of::()) + } +} + +struct IommuEpollHandler { + mem: GuestMemoryAtomic, + request_queue: Queue, + _event_queue: Queue, + interrupt_cb: Arc, + request_queue_evt: EventFd, + _event_queue_evt: EventFd, + kill_evt: EventFd, + pause_evt: EventFd, + mapping: Arc, + ext_mapping: Arc>>>, + msi_iova_space: (u64, u64), +} + +impl IommuEpollHandler { + fn request_queue(&mut self) -> Result { + let mut used_descs = false; + while let Some(mut desc_chain) = self.request_queue.pop_descriptor_chain(self.mem.memory()) + { + let len = Request::parse( + &mut desc_chain, + &self.mapping, + &self.ext_mapping.lock().unwrap(), + self.msi_iova_space, + )?; + + self.request_queue + .add_used(desc_chain.memory(), desc_chain.head_index(), len as u32) + .map_err(Error::QueueAddUsed)?; + + used_descs = true; + } + + Ok(used_descs) + } + + fn signal_used_queue(&self, queue_index: u16) -> result::Result<(), DeviceError> { + self.interrupt_cb + .trigger(VirtioInterruptType::Queue(queue_index)) + .map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.request_queue_evt.as_raw_fd(), REQUEST_Q_EVENT)?; + helper.run(paused, paused_sync, self)?; + + Ok(()) + } +} + +impl EpollHelperHandler for IommuEpollHandler { + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + REQUEST_Q_EVENT => { + self.request_queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + + let needs_notification = self.request_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process request queue : {:?}", + e + )) + })?; + if needs_notification { + self.signal_used_queue(0).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", + e + )) + })?; + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected event: {}", + ev_type + ))); + } + } + Ok(()) + } +} + +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +struct Mapping { + gpa: u64, + size: u64, +} + +#[derive(Clone, Debug)] +struct Domain { + mappings: BTreeMap, + bypass: bool, +} + +#[derive(Debug)] +pub struct IommuMapping { + // Domain related to an endpoint. + endpoints: Arc>>, + // Information related to each domain. + domains: Arc>>, + // Global flag indicating if endpoints that are not attached to any domain + // are in bypass mode. + bypass: AtomicBool, +} + +impl DmaRemapping for IommuMapping { + fn translate_gva(&self, id: u32, addr: u64) -> std::result::Result { + debug!("Translate GVA addr 0x{:x}", addr); + if let Some(domain_id) = self.endpoints.read().unwrap().get(&id) { + if let Some(domain) = self.domains.read().unwrap().get(domain_id) { + // Directly return identity mapping in case the domain is in + // bypass mode. + if domain.bypass { + return Ok(addr); + } + + let range_start = if VIRTIO_IOMMU_PAGE_SIZE_MASK > addr { + 0 + } else { + addr - VIRTIO_IOMMU_PAGE_SIZE_MASK + }; + for (&key, &value) in domain + .mappings + .range((Included(&range_start), Included(&addr))) + { + if addr >= key && addr < key + value.size { + let new_addr = addr - key + value.gpa; + debug!("Into GPA addr 0x{:x}", new_addr); + return Ok(new_addr); + } + } + } + } else if self.bypass.load(Ordering::Acquire) { + return Ok(addr); + } + + Err(io::Error::new( + io::ErrorKind::Other, + format!("failed to translate GVA addr 0x{:x}", addr), + )) + } + + fn translate_gpa(&self, id: u32, addr: u64) -> std::result::Result { + debug!("Translate GPA addr 0x{:x}", addr); + if let Some(domain_id) = self.endpoints.read().unwrap().get(&id) { + if let Some(domain) = self.domains.read().unwrap().get(domain_id) { + // Directly return identity mapping in case the domain is in + // bypass mode. + if domain.bypass { + return Ok(addr); + } + + for (&key, &value) in domain.mappings.iter() { + if addr >= value.gpa && addr < value.gpa + value.size { + let new_addr = addr - value.gpa + key; + debug!("Into GVA addr 0x{:x}", new_addr); + return Ok(new_addr); + } + } + } + } else if self.bypass.load(Ordering::Acquire) { + return Ok(addr); + } + + Err(io::Error::new( + io::ErrorKind::Other, + format!("failed to translate GPA addr 0x{:x}", addr), + )) + } +} + +#[derive(Debug)] +pub struct AccessPlatformMapping { + id: u32, + mapping: Arc, +} + +impl AccessPlatformMapping { + pub fn new(id: u32, mapping: Arc) -> Self { + AccessPlatformMapping { id, mapping } + } +} + +impl AccessPlatform for AccessPlatformMapping { + fn translate_gva(&self, base: u64, _size: u64) -> std::result::Result { + self.mapping.translate_gva(self.id, base) + } + fn translate_gpa(&self, base: u64, _size: u64) -> std::result::Result { + self.mapping.translate_gpa(self.id, base) + } +} + +pub struct Iommu { + common: VirtioCommon, + id: String, + config: VirtioIommuConfig, + mapping: Arc, + ext_mapping: Arc>>>, + seccomp_action: SeccompAction, + exit_evt: EventFd, + msi_iova_space: (u64, u64), +} + +type EndpointsState = Vec<(u32, u32)>; +type DomainsState = Vec<(u32, (Vec<(u64, Mapping)>, bool))>; + +#[derive(Serialize, Deserialize)] +pub struct IommuState { + avail_features: u64, + acked_features: u64, + endpoints: EndpointsState, + domains: DomainsState, +} + +impl Iommu { + pub fn new( + id: String, + seccomp_action: SeccompAction, + exit_evt: EventFd, + msi_iova_space: (u64, u64), + state: Option, + ) -> io::Result<(Self, Arc)> { + let (avail_features, acked_features, endpoints, domains) = if let Some(state) = state { + info!("Restoring virtio-iommu {}", id); + ( + state.avail_features, + state.acked_features, + state.endpoints.into_iter().collect(), + state + .domains + .into_iter() + .map(|(k, v)| { + ( + k, + Domain { + mappings: v.0.into_iter().collect(), + bypass: v.1, + }, + ) + }) + .collect(), + ) + } else { + let avail_features = 1u64 << VIRTIO_F_VERSION_1 + | 1u64 << VIRTIO_IOMMU_F_MAP_UNMAP + | 1u64 << VIRTIO_IOMMU_F_PROBE + | 1u64 << VIRTIO_IOMMU_F_BYPASS_CONFIG; + + (avail_features, 0, BTreeMap::new(), BTreeMap::new()) + }; + + let config = VirtioIommuConfig { + page_size_mask: VIRTIO_IOMMU_PAGE_SIZE_MASK, + probe_size: PROBE_PROP_SIZE, + ..Default::default() + }; + + let mapping = Arc::new(IommuMapping { + endpoints: Arc::new(RwLock::new(endpoints)), + domains: Arc::new(RwLock::new(domains)), + bypass: AtomicBool::new(true), + }); + + Ok(( + Iommu { + id, + common: VirtioCommon { + device_type: VirtioDeviceType::Iommu as u32, + queue_sizes: QUEUE_SIZES.to_vec(), + avail_features, + acked_features, + paused_sync: Some(Arc::new(Barrier::new(2))), + min_queues: NUM_QUEUES as u16, + ..Default::default() + }, + config, + mapping: mapping.clone(), + ext_mapping: Arc::new(Mutex::new(BTreeMap::new())), + seccomp_action, + exit_evt, + msi_iova_space, + }, + mapping, + )) + } + + fn state(&self) -> IommuState { + IommuState { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + endpoints: self + .mapping + .endpoints + .read() + .unwrap() + .clone() + .into_iter() + .collect(), + domains: self + .mapping + .domains + .read() + .unwrap() + .clone() + .into_iter() + .map(|(k, v)| (k, (v.mappings.into_iter().collect(), v.bypass))) + .collect(), + } + } + + fn update_bypass(&mut self) { + // Use bypass from config if VIRTIO_IOMMU_F_BYPASS_CONFIG has been negotiated + if !self + .common + .feature_acked(VIRTIO_IOMMU_F_BYPASS_CONFIG.into()) + { + return; + } + + let bypass = self.config.bypass == 1; + info!("Updating bypass mode to {}", bypass); + self.mapping.bypass.store(bypass, Ordering::Release); + } + + pub fn add_external_mapping(&mut self, device_id: u32, mapping: Arc) { + self.ext_mapping.lock().unwrap().insert(device_id, mapping); + } + + #[cfg(fuzzing)] + pub fn wait_for_epoll_threads(&mut self) { + self.common.wait_for_epoll_threads(); + } +} + +impl Drop for Iommu { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +impl VirtioDevice for Iommu { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.as_slice(), offset, data); + } + + fn write_config(&mut self, offset: u64, data: &[u8]) { + // The "bypass" field is the only mutable field + let bypass_offset = + (&self.config.bypass as *const _ as u64) - (&self.config as *const _ as u64); + if offset != bypass_offset || data.len() != std::mem::size_of_val(&self.config.bypass) { + error!( + "Attempt to write to read-only field: offset {:x} length {}", + offset, + data.len() + ); + return; + } + + self.config.bypass = data[0]; + + self.update_bypass(); + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let (_, request_queue, request_queue_evt) = queues.remove(0); + let (_, _event_queue, _event_queue_evt) = queues.remove(0); + + let mut handler = IommuEpollHandler { + mem, + request_queue, + _event_queue, + interrupt_cb, + request_queue_evt, + _event_queue_evt, + kill_evt, + pause_evt, + mapping: self.mapping.clone(), + ext_mapping: self.ext_mapping.clone(), + msi_iova_space: self.msi_iova_space, + }; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + let mut epoll_threads = Vec::new(); + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioIommu, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + + self.common.epoll_threads = Some(epoll_threads); + + event!("virtio-device", "activated", "id", &self.id); + Ok(()) + } + + fn reset(&mut self) -> Option> { + let result = self.common.reset(); + event!("virtio-device", "reset", "id", &self.id); + result + } +} + +impl Pausable for Iommu { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume() + } +} + +impl Snapshottable for Iommu { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } +} +impl Transportable for Iommu {} +impl Migratable for Iommu {} diff --git a/vendor/cube-hypervisor/virtio-devices/src/lib.rs b/vendor/cube-hypervisor/virtio-devices/src/lib.rs new file mode 100644 index 0000000..dce71a2 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/lib.rs @@ -0,0 +1,186 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +//! Implements virtio devices, queues, and transport mechanisms. + +#![allow(clippy::significant_drop_in_scrutinee)] + +#[macro_use] +extern crate event_monitor; +#[macro_use] +extern crate log; + +use serde::{Deserialize, Serialize}; +use std::convert::TryInto; +use std::io; +use thiserror::Error; + +#[macro_use] +mod device; +pub mod balloon; +pub mod block; +mod console; +pub mod epoll_helper; +pub mod fs; +mod iommu; +pub mod mem; +pub mod net; +mod pmem; +mod rng; +pub mod seccomp_filters; +mod thread_helper; +pub mod transport; +pub mod vdpa; +pub mod vhost_user; +pub mod vsock; +pub mod watchdog; + +pub use self::balloon::Balloon; +pub use self::block::{Block, BlockState}; +pub use self::console::{Console, ConsoleResizer, Endpoint}; +pub use self::device::{ + DmaRemapping, UserspaceMapping, VirtioCommon, VirtioDevice, VirtioInterrupt, + VirtioInterruptType, VirtioSharedMemoryList, +}; +pub use self::epoll_helper::{ + EpollHelper, EpollHelperError, EpollHelperHandler, EPOLL_HELPER_EVENT_LAST, +}; +pub use self::fs::*; +pub use self::iommu::{AccessPlatformMapping, Iommu, IommuMapping}; +pub use self::mem::{BlocksState, Mem, VirtioMemMappingSource, VIRTIO_MEM_ALIGN_SIZE}; +pub use self::net::{Net, NetCtrlEpollHandler}; +pub use self::pmem::Pmem; +pub use self::rng::Rng; +pub use self::vdpa::{Vdpa, VdpaDmaMapping}; +pub use self::vsock::Vsock; +pub use self::watchdog::Watchdog; + +use vm_memory::{bitmap::AtomicBitmap, GuestAddress, GuestMemory}; +use vm_virtio::VirtioDeviceType; + +use virtiofsd::Error as VirtioFsError; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; +type GuestRegionMmap = vm_memory::GuestRegionMmap; +type MmapRegion = vm_memory::MmapRegion; + +const DEVICE_INIT: u32 = 0x00; +const DEVICE_ACKNOWLEDGE: u32 = 0x01; +const DEVICE_DRIVER: u32 = 0x02; +const DEVICE_DRIVER_OK: u32 = 0x04; +const DEVICE_FEATURES_OK: u32 = 0x08; +const DEVICE_FAILED: u32 = 0x80; + +const VIRTIO_F_RING_INDIRECT_DESC: u32 = 28; +const VIRTIO_F_RING_EVENT_IDX: u32 = 29; +const VIRTIO_F_VERSION_1: u32 = 32; +const VIRTIO_F_IOMMU_PLATFORM: u32 = 33; +const VIRTIO_F_IN_ORDER: u32 = 35; +const VIRTIO_F_ORDER_PLATFORM: u32 = 36; +#[allow(dead_code)] +const VIRTIO_F_SR_IOV: u32 = 37; +const VIRTIO_F_NOTIFICATION_DATA: u32 = 38; + +#[derive(Error, Debug)] +pub enum ActivateError { + #[error("Failed to activate virtio device")] + BadActivate, + #[error("Failed to clone exit event fd: {0}")] + CloneExitEventFd(std::io::Error), + #[error("Failed to spawn thread: {0}")] + ThreadSpawn(std::io::Error), + #[error("Failed to setup vhost-user-fs daemon: {0}")] + VhostUserFsSetup(vhost_user::Error), + #[error("Failed to setup vhost-user-blk daemon: {0}")] + VhostUserBlkSetup(vhost_user::Error), + #[error("Failed to create seccomp filter: {0}")] + CreateSeccompFilter(seccompiler::Error), + #[error("Failed to create rate limiter: {0}")] + CreateRateLimiter(std::io::Error), + #[error("Failed to activate the vDPA device: {0}")] + ActivateVdpa(vdpa::Error), + #[error("Failed to activate the virtio-fs device: {0}")] + ActivateVirtioFs(std::io::Error), + #[error("Failed to create virtiofs server: {0}")] + InvalidServerConfig(VirtioFsError), + #[error("Failed to restore virtiofs state: {0}")] + InvalidVirtioFsState(std::io::Error), +} + +pub type ActivateResult = std::result::Result<(), ActivateError>; + +pub type DeviceEventT = u16; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Failed to single used queue: {0}")] + FailedSignalingUsedQueue(io::Error), + #[error("I/O Error: {0}")] + IoError(io::Error), + #[error("Failed to update memory vhost-user: {0}")] + VhostUserUpdateMemory(vhost_user::Error), + #[error("Failed to add memory region vhost-user: {0}")] + VhostUserAddMemoryRegion(vhost_user::Error), + #[error("Failed to set shared memory region")] + SetShmRegionsNotSupported, + #[error("Failed to process net queue: {0}")] + NetQueuePair(::net_util::NetQueuePairError), + #[error("Failed to : {0}")] + QueueAddUsed(virtio_queue::Error), + #[error("Failed to : {0}")] + QueueIterator(virtio_queue::Error), +} + +#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] +pub struct TokenBucketConfig { + pub size: u64, + pub one_time_burst: Option, + pub refill_time: u64, +} + +#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] +#[serde(deny_unknown_fields)] +pub struct RateLimiterConfig { + pub bandwidth: Option, + pub ops: Option, +} + +impl TryInto for RateLimiterConfig { + type Error = io::Error; + + fn try_into(self) -> std::result::Result { + let bw = self.bandwidth.unwrap_or_default(); + let ops = self.ops.unwrap_or_default(); + rate_limiter::RateLimiter::new( + bw.size, + bw.one_time_burst.unwrap_or(0), + bw.refill_time, + ops.size, + ops.one_time_burst.unwrap_or(0), + ops.refill_time, + ) + } +} + +/// Convert an absolute address into an address space (GuestMemory) +/// to a host pointer and verify that the provided size define a valid +/// range within a single memory region. +/// Return None if it is out of bounds or if addr+size overlaps a single region. +pub fn get_host_address_range( + mem: &M, + addr: GuestAddress, + size: usize, +) -> Option<*mut u8> { + if mem.check_range(addr, size) { + Some(mem.get_host_address(addr).unwrap()) + } else { + None + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/mem.rs b/vendor/cube-hypervisor/virtio-devices/src/mem.rs new file mode 100644 index 0000000..bfe6e70 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/mem.rs @@ -0,0 +1,1008 @@ +// Copyright (c) 2020 Ant Financial +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::Error as DeviceError; +use super::{ + ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, VirtioCommon, + VirtioDevice, VirtioDeviceType, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_VERSION_1, +}; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::{GuestMemoryMmap, GuestRegionMmap}; +use crate::{VirtioInterrupt, VirtioInterruptType}; +use anyhow::anyhow; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; +use std::io; +use std::mem::size_of; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::result; +use std::sync::atomic::AtomicBool; +use std::sync::mpsc; +use std::sync::{Arc, Barrier, Mutex}; +use thiserror::Error; +use virtio_queue::{DescriptorChain, Queue, QueueT}; +use vm_device::dma_mapping::ExternalDmaMapping; +use vm_memory::{ + Address, ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, + GuestMemoryError, GuestMemoryLoadGuard, GuestMemoryRegion, +}; +use vm_migration::protocol::MemoryRangeTable; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vmm_sys_util::eventfd::EventFd; + +const QUEUE_SIZE: u16 = 128; +const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE]; + +// 128MiB is the standard memory block size in Linux. A virtio-mem region must +// be aligned on this size, and the region size must be a multiple of it. +pub const VIRTIO_MEM_ALIGN_SIZE: u64 = 128 << 20; +// Use 2 MiB alignment so transparent hugepages can be used by KVM. +const VIRTIO_MEM_DEFAULT_BLOCK_SIZE: u64 = 2 << 20; + +// Request processed successfully, applicable for +// - VIRTIO_MEM_REQ_PLUG +// - VIRTIO_MEM_REQ_UNPLUG +// - VIRTIO_MEM_REQ_UNPLUG_ALL +// - VIRTIO_MEM_REQ_STATE +const VIRTIO_MEM_RESP_ACK: u16 = 0; + +// Request denied - e.g. trying to plug more than requested, applicable for +// - VIRTIO_MEM_REQ_PLUG +const VIRTIO_MEM_RESP_NACK: u16 = 1; + +// Request cannot be processed right now, try again later, applicable for +// - VIRTIO_MEM_REQ_PLUG +// - VIRTIO_MEM_REQ_UNPLUG +// - VIRTIO_MEM_REQ_UNPLUG_ALL +#[allow(unused)] +const VIRTIO_MEM_RESP_BUSY: u16 = 2; + +// Error in request (e.g. addresses/alignment), applicable for +// - VIRTIO_MEM_REQ_PLUG +// - VIRTIO_MEM_REQ_UNPLUG +// - VIRTIO_MEM_REQ_STATE +const VIRTIO_MEM_RESP_ERROR: u16 = 3; + +// State of memory blocks is "plugged" +const VIRTIO_MEM_STATE_PLUGGED: u16 = 0; +// State of memory blocks is "unplugged" +const VIRTIO_MEM_STATE_UNPLUGGED: u16 = 1; +// State of memory blocks is "mixed" +const VIRTIO_MEM_STATE_MIXED: u16 = 2; + +// request to plug memory blocks +const VIRTIO_MEM_REQ_PLUG: u16 = 0; +// request to unplug memory blocks +const VIRTIO_MEM_REQ_UNPLUG: u16 = 1; +// request to unplug all blocks and shrink the usable size +const VIRTIO_MEM_REQ_UNPLUG_ALL: u16 = 2; +// request information about the plugged state of memory blocks +const VIRTIO_MEM_REQ_STATE: u16 = 3; + +// New descriptors are pending on the virtio queue. +const QUEUE_AVAIL_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; + +// Virtio features +const VIRTIO_MEM_F_ACPI_PXM: u8 = 0; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Guest gave us bad memory addresses: {0}")] + GuestMemory(GuestMemoryError), + #[error("Guest gave us a write only descriptor that protocol says to read from")] + UnexpectedWriteOnlyDescriptor, + #[error("Guest gave us a read only descriptor that protocol says to write to")] + UnexpectedReadOnlyDescriptor, + #[error("Guest gave us too few descriptors in a descriptor chain")] + DescriptorChainTooShort, + #[error("Guest gave us a buffer that was too short to use")] + BufferLengthTooSmall, + #[error("Guest sent us invalid request")] + InvalidRequest, + #[error("Failed to EventFd write: {0}")] + EventFdWriteFail(std::io::Error), + #[error("Failed to EventFd try_clone: {0}")] + EventFdTryCloneFail(std::io::Error), + #[error("Failed to MpscRecv: {0}")] + MpscRecvFail(mpsc::RecvError), + #[error("Resize invalid argument: {0}")] + ResizeError(anyhow::Error), + #[error("Fail to resize trigger: {0}")] + ResizeTriggerFail(DeviceError), + #[error("Invalid configuration: {0}")] + ValidateError(anyhow::Error), + #[error("Failed discarding memory range: {0}")] + DiscardMemoryRange(std::io::Error), + #[error("Failed DMA mapping: {0}")] + DmaMap(std::io::Error), + #[error("Failed DMA unmapping: {0}")] + DmaUnmap(std::io::Error), + #[error("Invalid DMA mapping handler")] + InvalidDmaMappingHandler, + #[error("Not activated by the guest")] + NotActivatedByGuest, + #[error("Unknown request type: {0}")] + UnkownRequestType(u16), + #[error("Failed adding used index: {0}")] + QueueAddUsed(virtio_queue::Error), +} + +#[repr(C)] +#[derive(Copy, Clone, Debug, Default)] +struct VirtioMemReq { + req_type: u16, + padding: [u16; 3], + addr: u64, + nb_blocks: u16, + padding_1: [u16; 3], +} + +// SAFETY: it only has data and has no implicit padding. +unsafe impl ByteValued for VirtioMemReq {} + +#[repr(C)] +#[derive(Copy, Clone, Debug, Default)] +struct VirtioMemResp { + resp_type: u16, + padding: [u16; 3], + state: u16, +} + +// SAFETY: it only has data and has no implicit padding. +unsafe impl ByteValued for VirtioMemResp {} + +#[repr(C)] +#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] +pub struct VirtioMemConfig { + // Block size and alignment. Cannot change. + block_size: u64, + // Valid with VIRTIO_MEM_F_ACPI_PXM. Cannot change. + node_id: u16, + padding: [u8; 6], + // Start address of the memory region. Cannot change. + addr: u64, + // Region size (maximum). Cannot change. + region_size: u64, + // Currently usable region size. Can grow up to region_size. Can + // shrink due to VIRTIO_MEM_REQ_UNPLUG_ALL (in which case no config + // update will be sent). + usable_region_size: u64, + // Currently used size. Changes due to plug/unplug requests, but no + // config updates will be sent. + plugged_size: u64, + // Requested size. New plug requests cannot exceed it. Can change. + requested_size: u64, +} + +// SAFETY: it only has data and has no implicit padding. +unsafe impl ByteValued for VirtioMemConfig {} + +impl VirtioMemConfig { + fn validate(&self) -> result::Result<(), Error> { + if self.addr % self.block_size != 0 { + return Err(Error::ValidateError(anyhow!( + "addr 0x{:x} is not aligned on block_size 0x{:x}", + self.addr, + self.block_size + ))); + } + if self.region_size % self.block_size != 0 { + return Err(Error::ValidateError(anyhow!( + "region_size 0x{:x} is not aligned on block_size 0x{:x}", + self.region_size, + self.block_size + ))); + } + if self.usable_region_size % self.block_size != 0 { + return Err(Error::ValidateError(anyhow!( + "usable_region_size 0x{:x} is not aligned on block_size 0x{:x}", + self.usable_region_size, + self.block_size + ))); + } + if self.plugged_size % self.block_size != 0 { + return Err(Error::ValidateError(anyhow!( + "plugged_size 0x{:x} is not aligned on block_size 0x{:x}", + self.plugged_size, + self.block_size + ))); + } + if self.requested_size % self.block_size != 0 { + return Err(Error::ValidateError(anyhow!( + "requested_size 0x{:x} is not aligned on block_size 0x{:x}", + self.requested_size, + self.block_size + ))); + } + + Ok(()) + } + + fn resize(&mut self, size: u64) -> result::Result<(), Error> { + if self.requested_size == size { + return Err(Error::ResizeError(anyhow!( + "new size 0x{:x} and requested_size are identical", + size + ))); + } else if size > self.region_size { + return Err(Error::ResizeError(anyhow!( + "new size 0x{:x} is bigger than region_size 0x{:x}", + size, + self.region_size + ))); + } else if size % self.block_size != 0 { + return Err(Error::ResizeError(anyhow!( + "new size 0x{:x} is not aligned on block_size 0x{:x}", + size, + self.block_size + ))); + } + + self.requested_size = size; + + Ok(()) + } + + fn is_valid_range(&self, addr: u64, size: u64) -> bool { + // Ensure no overflow from adding 'addr' and 'size' whose value are both + // controlled by the guest driver + if addr.checked_add(size).is_none() { + return false; + } + + // Start address must be aligned on block_size, the size must be + // greater than 0, and all blocks covered by the request must be + // in the usable region. + if addr % self.block_size != 0 + || size == 0 + || (addr < self.addr || addr + size > self.addr + self.usable_region_size) + { + return false; + } + + true + } +} + +struct Request { + req: VirtioMemReq, + status_addr: GuestAddress, +} + +impl Request { + fn parse( + desc_chain: &mut DescriptorChain>, + ) -> result::Result { + let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?; + // The descriptor contains the request type which MUST be readable. + if desc.is_write_only() { + return Err(Error::UnexpectedWriteOnlyDescriptor); + } + if desc.len() as usize != size_of::() { + return Err(Error::InvalidRequest); + } + let req: VirtioMemReq = desc_chain + .memory() + .read_obj(desc.addr()) + .map_err(Error::GuestMemory)?; + + let status_desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?; + + // The status MUST always be writable + if !status_desc.is_write_only() { + return Err(Error::UnexpectedReadOnlyDescriptor); + } + + if (status_desc.len() as usize) < size_of::() { + return Err(Error::BufferLengthTooSmall); + } + + Ok(Request { + req, + status_addr: status_desc.addr(), + }) + } + + fn send_response( + &self, + mem: &GuestMemoryMmap, + resp_type: u16, + state: u16, + ) -> Result { + let resp = VirtioMemResp { + resp_type, + state, + ..Default::default() + }; + mem.write_obj(resp, self.status_addr) + .map_err(Error::GuestMemory)?; + Ok(size_of::() as u32) + } +} + +#[derive(Clone, Serialize, Deserialize)] +pub struct BlocksState { + bitmap: Vec, +} + +impl BlocksState { + pub fn new(region_size: u64) -> Self { + BlocksState { + bitmap: vec![false; (region_size / VIRTIO_MEM_DEFAULT_BLOCK_SIZE) as usize], + } + } + + fn is_range_state(&self, first_block_index: usize, nb_blocks: u16, plug: bool) -> bool { + for state in self + .bitmap + .iter() + .skip(first_block_index) + .take(nb_blocks as usize) + { + if *state != plug { + return false; + } + } + true + } + + fn set_range(&mut self, first_block_index: usize, nb_blocks: u16, plug: bool) { + for state in self + .bitmap + .iter_mut() + .skip(first_block_index) + .take(nb_blocks as usize) + { + *state = plug; + } + } + + fn inner(&self) -> &Vec { + &self.bitmap + } + + pub fn memory_ranges(&self, start_addr: u64, plugged: bool) -> MemoryRangeTable { + let mut bitmap: Vec = Vec::new(); + let mut i = 0; + for (j, bit) in self.bitmap.iter().enumerate() { + if j % 64 == 0 { + bitmap.push(0); + + if j != 0 { + i += 1; + } + } + + if *bit == plugged { + bitmap[i] |= 1 << (j % 64); + } + } + + MemoryRangeTable::from_bitmap(bitmap, start_addr, VIRTIO_MEM_DEFAULT_BLOCK_SIZE) + } +} + +struct MemEpollHandler { + mem: GuestMemoryAtomic, + host_addr: u64, + host_fd: Option, + blocks_state: Arc>, + config: Arc>, + queue: Queue, + interrupt_cb: Arc, + queue_evt: EventFd, + kill_evt: EventFd, + pause_evt: EventFd, + hugepages: bool, + dma_mapping_handlers: Arc>>>, +} + +impl MemEpollHandler { + fn discard_memory_range(&self, offset: u64, size: u64) -> Result<(), Error> { + // Use fallocate if the memory region is backed by a file. + if let Some(fd) = self.host_fd { + let res = unsafe { + libc::fallocate64( + fd, + libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_KEEP_SIZE, + offset as libc::off64_t, + size as libc::off64_t, + ) + }; + if res != 0 { + let err = io::Error::last_os_error(); + error!("Deallocating file space failed: {}", err); + return Err(Error::DiscardMemoryRange(err)); + } + } + + // Only use madvise if the memory region is not allocated with + // hugepages. + if !self.hugepages { + let res = unsafe { + libc::madvise( + (self.host_addr + offset) as *mut libc::c_void, + size as libc::size_t, + libc::MADV_DONTNEED, + ) + }; + if res != 0 { + let err = io::Error::last_os_error(); + error!("Advising kernel about pages range failed: {}", err); + return Err(Error::DiscardMemoryRange(err)); + } + } + + Ok(()) + } + + fn state_change_request(&mut self, addr: u64, nb_blocks: u16, plug: bool) -> u16 { + let mut config = self.config.lock().unwrap(); + let size: u64 = nb_blocks as u64 * config.block_size; + + if plug && (config.plugged_size + size > config.requested_size) { + return VIRTIO_MEM_RESP_NACK; + } + if !config.is_valid_range(addr, size) { + return VIRTIO_MEM_RESP_ERROR; + } + + let offset = addr - config.addr; + + let first_block_index = (offset / config.block_size) as usize; + if !self + .blocks_state + .lock() + .unwrap() + .is_range_state(first_block_index, nb_blocks, !plug) + { + return VIRTIO_MEM_RESP_ERROR; + } + + if !plug { + if let Err(e) = self.discard_memory_range(offset, size) { + error!("failed discarding memory range: {:?}", e); + return VIRTIO_MEM_RESP_ERROR; + } + } + + self.blocks_state + .lock() + .unwrap() + .set_range(first_block_index, nb_blocks, plug); + + let handlers = self.dma_mapping_handlers.lock().unwrap(); + if plug { + let mut gpa = addr; + for _ in 0..nb_blocks { + for (_, handler) in handlers.iter() { + if let Err(e) = handler.map(gpa, gpa, config.block_size) { + error!( + "failed DMA mapping addr 0x{:x} size 0x{:x}: {}", + gpa, config.block_size, e + ); + return VIRTIO_MEM_RESP_ERROR; + } + } + + gpa += config.block_size; + } + + config.plugged_size += size; + } else { + for (_, handler) in handlers.iter() { + if let Err(e) = handler.unmap(addr, size) { + error!( + "failed DMA unmapping addr 0x{:x} size 0x{:x}: {}", + addr, size, e + ); + return VIRTIO_MEM_RESP_ERROR; + } + } + + config.plugged_size -= size; + } + + VIRTIO_MEM_RESP_ACK + } + + fn unplug_all(&mut self) -> u16 { + let mut config = self.config.lock().unwrap(); + if let Err(e) = self.discard_memory_range(0, config.region_size) { + error!("failed discarding memory range: {:?}", e); + return VIRTIO_MEM_RESP_ERROR; + } + + // Remaining plugged blocks are unmapped. + if config.plugged_size > 0 { + let handlers = self.dma_mapping_handlers.lock().unwrap(); + for (idx, plugged) in self.blocks_state.lock().unwrap().inner().iter().enumerate() { + if *plugged { + let gpa = config.addr + (idx as u64 * config.block_size); + for (_, handler) in handlers.iter() { + if let Err(e) = handler.unmap(gpa, config.block_size) { + error!( + "failed DMA unmapping addr 0x{:x} size 0x{:x}: {}", + gpa, config.block_size, e + ); + return VIRTIO_MEM_RESP_ERROR; + } + } + } + } + } + + self.blocks_state.lock().unwrap().set_range( + 0, + (config.region_size / config.block_size) as u16, + false, + ); + + config.plugged_size = 0; + + VIRTIO_MEM_RESP_ACK + } + + fn state_request(&self, addr: u64, nb_blocks: u16) -> (u16, u16) { + let config = self.config.lock().unwrap(); + let size: u64 = nb_blocks as u64 * config.block_size; + + let resp_type = if config.is_valid_range(addr, size) { + VIRTIO_MEM_RESP_ACK + } else { + VIRTIO_MEM_RESP_ERROR + }; + + let offset = addr - config.addr; + let first_block_index = (offset / config.block_size) as usize; + let resp_state = + if self + .blocks_state + .lock() + .unwrap() + .is_range_state(first_block_index, nb_blocks, true) + { + VIRTIO_MEM_STATE_PLUGGED + } else if self.blocks_state.lock().unwrap().is_range_state( + first_block_index, + nb_blocks, + false, + ) { + VIRTIO_MEM_STATE_UNPLUGGED + } else { + VIRTIO_MEM_STATE_MIXED + }; + + (resp_type, resp_state) + } + + fn signal(&self, int_type: VirtioInterruptType) -> result::Result<(), DeviceError> { + self.interrupt_cb.trigger(int_type).map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + fn process_queue(&mut self) -> Result { + let mut used_descs = false; + + while let Some(mut desc_chain) = self.queue.pop_descriptor_chain(self.mem.memory()) { + let r = Request::parse(&mut desc_chain)?; + let (resp_type, resp_state) = match r.req.req_type { + VIRTIO_MEM_REQ_PLUG => ( + self.state_change_request(r.req.addr, r.req.nb_blocks, true), + 0u16, + ), + VIRTIO_MEM_REQ_UNPLUG => ( + self.state_change_request(r.req.addr, r.req.nb_blocks, false), + 0u16, + ), + VIRTIO_MEM_REQ_UNPLUG_ALL => (self.unplug_all(), 0u16), + VIRTIO_MEM_REQ_STATE => self.state_request(r.req.addr, r.req.nb_blocks), + _ => { + return Err(Error::UnkownRequestType(r.req.req_type)); + } + }; + let len = r.send_response(desc_chain.memory(), resp_type, resp_state)?; + self.queue + .add_used(desc_chain.memory(), desc_chain.head_index(), len) + .map_err(Error::QueueAddUsed)?; + used_descs = true; + } + + Ok(used_descs) + } + + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.queue_evt.as_raw_fd(), QUEUE_AVAIL_EVENT)?; + helper.run(paused, paused_sync, self)?; + + Ok(()) + } +} + +impl EpollHelperHandler for MemEpollHandler { + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + QUEUE_AVAIL_EVENT => { + self.queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + + let needs_notification = self.process_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to process queue : {:?}", e)) + })?; + if needs_notification { + self.signal(VirtioInterruptType::Queue(0)).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", + e + )) + })?; + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected event: {}", + ev_type + ))); + } + } + Ok(()) + } +} + +#[derive(PartialEq, Eq, PartialOrd, Ord)] +pub enum VirtioMemMappingSource { + Container, + Device(u32), +} + +#[derive(Serialize, Deserialize)] +pub struct MemState { + pub avail_features: u64, + pub acked_features: u64, + pub config: VirtioMemConfig, + pub blocks_state: BlocksState, +} + +pub struct Mem { + common: VirtioCommon, + id: String, + host_addr: u64, + host_fd: Option, + config: Arc>, + seccomp_action: SeccompAction, + hugepages: bool, + dma_mapping_handlers: Arc>>>, + blocks_state: Arc>, + exit_evt: EventFd, + interrupt_cb: Option>, +} + +impl Mem { + // Create a new virtio-mem device. + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + region: &Arc, + seccomp_action: SeccompAction, + numa_node_id: Option, + initial_size: u64, + hugepages: bool, + exit_evt: EventFd, + blocks_state: Arc>, + state: Option, + ) -> io::Result { + let region_len = region.len(); + + if region_len != region_len / VIRTIO_MEM_ALIGN_SIZE * VIRTIO_MEM_ALIGN_SIZE { + return Err(io::Error::new( + io::ErrorKind::Other, + format!( + "Virtio-mem size is not aligned with {}", + VIRTIO_MEM_ALIGN_SIZE + ), + )); + } + + let (avail_features, acked_features, config) = if let Some(state) = state { + info!("Restoring virtio-mem {}", id); + *(blocks_state.lock().unwrap()) = state.blocks_state.clone(); + (state.avail_features, state.acked_features, state.config) + } else { + let mut avail_features = 1u64 << VIRTIO_F_VERSION_1; + + let mut config = VirtioMemConfig { + block_size: VIRTIO_MEM_DEFAULT_BLOCK_SIZE, + addr: region.start_addr().raw_value(), + region_size: region.len(), + usable_region_size: region.len(), + plugged_size: 0, + requested_size: 0, + ..Default::default() + }; + + if initial_size != 0 { + config.resize(initial_size).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!( + "Failed to resize virtio-mem configuration to {}: {:?}", + initial_size, e + ), + ) + })?; + } + + if let Some(node_id) = numa_node_id { + avail_features |= 1u64 << VIRTIO_MEM_F_ACPI_PXM; + config.node_id = node_id; + } + + // Make sure the virtio-mem configuration complies with the + // specification. + config.validate().map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("Invalid virtio-mem configuration: {:?}", e), + ) + })?; + + (avail_features, 0, config) + }; + + let host_fd = region + .file_offset() + .map(|f_offset| f_offset.file().as_raw_fd()); + + Ok(Mem { + common: VirtioCommon { + device_type: VirtioDeviceType::Mem as u32, + avail_features, + acked_features, + paused_sync: Some(Arc::new(Barrier::new(2))), + queue_sizes: QUEUE_SIZES.to_vec(), + min_queues: 1, + ..Default::default() + }, + id, + host_addr: region.as_ptr() as u64, + host_fd, + config: Arc::new(Mutex::new(config)), + seccomp_action, + hugepages, + dma_mapping_handlers: Arc::new(Mutex::new(BTreeMap::new())), + blocks_state, + exit_evt, + interrupt_cb: None, + }) + } + + pub fn resize(&mut self, size: u64) -> result::Result<(), Error> { + let mut config = self.config.lock().unwrap(); + config.resize(size).map_err(|e| { + Error::ResizeError(anyhow!("Failed to update virtio configuration: {:?}", e)) + })?; + + if let Some(interrupt_cb) = self.interrupt_cb.as_ref() { + interrupt_cb + .trigger(VirtioInterruptType::Config) + .map_err(|e| { + Error::ResizeError(anyhow!("Failed to signal the guest about resize: {:?}", e)) + }) + } else { + Ok(()) + } + } + + pub fn add_dma_mapping_handler( + &mut self, + source: VirtioMemMappingSource, + handler: Arc, + ) -> result::Result<(), Error> { + let config = self.config.lock().unwrap(); + + if config.plugged_size > 0 { + for (idx, plugged) in self.blocks_state.lock().unwrap().inner().iter().enumerate() { + if *plugged { + let gpa = config.addr + (idx as u64 * config.block_size); + handler + .map(gpa, gpa, config.block_size) + .map_err(Error::DmaMap)?; + } + } + } + + self.dma_mapping_handlers + .lock() + .unwrap() + .insert(source, handler); + + Ok(()) + } + + pub fn remove_dma_mapping_handler( + &mut self, + source: VirtioMemMappingSource, + ) -> result::Result<(), Error> { + let handler = self + .dma_mapping_handlers + .lock() + .unwrap() + .remove(&source) + .ok_or(Error::InvalidDmaMappingHandler)?; + + let config = self.config.lock().unwrap(); + + if config.plugged_size > 0 { + for (idx, plugged) in self.blocks_state.lock().unwrap().inner().iter().enumerate() { + if *plugged { + let gpa = config.addr + (idx as u64 * config.block_size); + handler + .unmap(gpa, config.block_size) + .map_err(Error::DmaUnmap)?; + } + } + } + + Ok(()) + } + + fn state(&self) -> MemState { + MemState { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + config: *(self.config.lock().unwrap()), + blocks_state: self.blocks_state.lock().unwrap().clone(), + } + } + + #[cfg(fuzzing)] + pub fn wait_for_epoll_threads(&mut self) { + self.common.wait_for_epoll_threads(); + } +} + +impl Drop for Mem { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +impl VirtioDevice for Mem { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.lock().unwrap().as_slice(), offset, data); + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let (_, queue, queue_evt) = queues.remove(0); + + self.interrupt_cb = Some(interrupt_cb.clone()); + + let mut handler = MemEpollHandler { + mem, + host_addr: self.host_addr, + host_fd: self.host_fd, + blocks_state: Arc::clone(&self.blocks_state), + config: self.config.clone(), + queue, + interrupt_cb, + queue_evt, + kill_evt, + pause_evt, + hugepages: self.hugepages, + dma_mapping_handlers: Arc::clone(&self.dma_mapping_handlers), + }; + + let unplugged_memory_ranges = self.blocks_state.lock().unwrap().memory_ranges(0, false); + for range in unplugged_memory_ranges.regions() { + handler + .discard_memory_range(range.gpa, range.length) + .map_err(|e| { + error!( + "failed discarding memory range [0x{:x}-0x{:x}]: {:?}", + range.gpa, + range.gpa + range.length - 1, + e + ); + ActivateError::BadActivate + })?; + } + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + let mut epoll_threads = Vec::new(); + + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioMem, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + self.common.epoll_threads = Some(epoll_threads); + + event!("virtio-device", "activated", "id", &self.id); + Ok(()) + } + + fn reset(&mut self) -> Option> { + let result = self.common.reset(); + event!("virtio-device", "reset", "id", &self.id); + result + } +} + +impl Pausable for Mem { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume() + } +} + +impl Snapshottable for Mem { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id(), &self.state()) + } +} +impl Transportable for Mem {} +impl Migratable for Mem {} diff --git a/vendor/cube-hypervisor/virtio-devices/src/net.rs b/vendor/cube-hypervisor/virtio-devices/src/net.rs new file mode 100644 index 0000000..cb6083e --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/net.rs @@ -0,0 +1,862 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the THIRD-PARTY file. + +use super::Error as DeviceError; +use super::{ + ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, + RateLimiterConfig, VirtioCommon, VirtioDevice, VirtioDeviceType, VirtioInterruptType, + EPOLL_HELPER_EVENT_LAST, +}; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::GuestMemoryMmap; +use crate::VirtioInterrupt; +use anyhow::anyhow; +use net_util::CtrlQueue; +use net_util::{ + build_net_config_space, build_net_config_space_with_mq, open_tap, + virtio_features_to_tap_offload, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, + Tap, TapError, TxVirtio, VirtioNetConfig, +}; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::net::Ipv4Addr; +use std::num::Wrapping; +use std::ops::Deref; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::result; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Barrier}; +use std::thread; +use std::vec::Vec; +use std::{collections::HashMap, convert::TryInto}; +use thiserror::Error; +use virtio_bindings::bindings::virtio_net::*; +use virtio_bindings::bindings::virtio_ring::VIRTIO_RING_F_EVENT_IDX; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{ByteValued, GuestAddressSpace, GuestMemoryAtomic}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vm_virtio::AccessPlatform; +use vmm_sys_util::eventfd::EventFd; + +/// Control queue +// Event available on the control queue. +const CTRL_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; + +// Following the VIRTIO specification, the MTU should be at least 1280. +pub const MIN_MTU: u16 = 1280; + +pub struct NetCtrlEpollHandler { + pub mem: GuestMemoryAtomic, + pub kill_evt: EventFd, + pub pause_evt: EventFd, + pub ctrl_q: CtrlQueue, + pub queue_evt: EventFd, + pub queue: Queue, + pub access_platform: Option>, + pub interrupt_cb: Arc, + pub queue_index: u16, +} + +impl NetCtrlEpollHandler { + fn signal_used_queue(&self, queue_index: u16) -> result::Result<(), DeviceError> { + self.interrupt_cb + .trigger(VirtioInterruptType::Queue(queue_index)) + .map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + pub fn run_ctrl( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> std::result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.queue_evt.as_raw_fd(), CTRL_QUEUE_EVENT)?; + helper.run(paused, paused_sync, self)?; + + Ok(()) + } +} + +impl EpollHelperHandler for NetCtrlEpollHandler { + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + CTRL_QUEUE_EVENT => { + let mem = self.mem.memory(); + self.queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to get control queue event: {:?}", + e + )) + })?; + self.ctrl_q + .process(mem.deref(), &mut self.queue, self.access_platform.as_ref()) + .map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process control queue: {:?}", + e + )) + })?; + match self.queue.needs_notification(mem.deref()) { + Ok(true) => { + self.signal_used_queue(self.queue_index).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Error signalling that control queue was used: {:?}", + e + )) + })?; + } + Ok(false) => {} + Err(e) => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Error getting notification state of control queue: {}", + e + ))); + } + }; + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unknown event for virtio-net control queue" + ))); + } + } + + Ok(()) + } +} + +/// Rx/Tx queue pair +// The guest has made a buffer available to receive a frame into. +pub const RX_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; +// The transmit queue has a frame that is ready to send from the guest. +pub const TX_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2; +// A frame is available for reading from the tap device to receive in the guest. +pub const RX_TAP_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 3; +// The TAP can be written to. Used after an EAGAIN error to retry TX. +pub const TX_TAP_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 4; +// New 'wake up' event from the rx rate limiter +pub const RX_RATE_LIMITER_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 5; +// New 'wake up' event from the tx rate limiter +pub const TX_RATE_LIMITER_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 6; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Failed to open taps: {0}")] + OpenTap(OpenTapError), + #[error("Using existing tap: {0}")] + TapError(TapError), + #[error("Error calling dup() on tap fd: {0}")] + DuplicateTapFd(std::io::Error), +} + +pub type Result = result::Result; + +struct NetEpollHandler { + net: NetQueuePair, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + kill_evt: EventFd, + pause_evt: EventFd, + queue_index_base: u16, + queue_pair: (Queue, Queue), + queue_evt_pair: (EventFd, EventFd), +} + +impl NetEpollHandler { + fn signal_used_queue(&self, queue_index: u16) -> result::Result<(), DeviceError> { + self.interrupt_cb + .trigger(VirtioInterruptType::Queue(queue_index)) + .map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + fn handle_rx_event(&mut self) -> result::Result<(), DeviceError> { + let queue_evt = &self.queue_evt_pair.0; + if let Err(e) = queue_evt.read() { + error!("Failed to get rx queue event: {:?}", e); + } + + self.net.rx_desc_avail = true; + + let rate_limit_reached = self + .net + .rx_rate_limiter + .as_ref() + .map_or(false, |r| r.is_blocked()); + + // Start to listen on RX_TAP_EVENT only when the rate limit is not reached + if !self.net.rx_tap_listening && !rate_limit_reached { + net_util::register_listener( + self.net.epoll_fd.unwrap(), + self.net.tap.as_raw_fd(), + epoll::Events::EPOLLIN, + u64::from(self.net.tap_rx_event_id), + ) + .map_err(DeviceError::IoError)?; + self.net.rx_tap_listening = true; + } + + Ok(()) + } + + fn process_tx(&mut self) -> result::Result<(), DeviceError> { + if self + .net + .process_tx(&self.mem.memory(), &mut self.queue_pair.1) + .map_err(DeviceError::NetQueuePair)? + { + self.signal_used_queue(self.queue_index_base + 1)?; + debug!("Signalling TX queue"); + } else { + debug!("Not signalling TX queue"); + } + Ok(()) + } + + fn handle_tx_event(&mut self) -> result::Result<(), DeviceError> { + let rate_limit_reached = self + .net + .tx_rate_limiter + .as_ref() + .map_or(false, |r| r.is_blocked()); + + if !rate_limit_reached { + self.process_tx()?; + } + + Ok(()) + } + + fn handle_rx_tap_event(&mut self) -> result::Result<(), DeviceError> { + if self + .net + .process_rx(&self.mem.memory(), &mut self.queue_pair.0) + .map_err(DeviceError::NetQueuePair)? + { + self.signal_used_queue(self.queue_index_base)?; + debug!("Signalling RX queue"); + } else { + debug!("Not signalling RX queue"); + } + Ok(()) + } + + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.queue_evt_pair.0.as_raw_fd(), RX_QUEUE_EVENT)?; + helper.add_event(self.queue_evt_pair.1.as_raw_fd(), TX_QUEUE_EVENT)?; + if let Some(rate_limiter) = &self.net.rx_rate_limiter { + helper.add_event(rate_limiter.as_raw_fd(), RX_RATE_LIMITER_EVENT)?; + } + if let Some(rate_limiter) = &self.net.tx_rate_limiter { + helper.add_event(rate_limiter.as_raw_fd(), TX_RATE_LIMITER_EVENT)?; + } + + let mem = self.mem.memory(); + // If there are some already available descriptors on the RX queue, + // then we can start the thread while listening onto the TAP. + if self + .queue_pair + .0 + .used_idx(mem.deref(), Ordering::Acquire) + .map_err(EpollHelperError::QueueRingIndex)? + < self + .queue_pair + .0 + .avail_idx(mem.deref(), Ordering::Acquire) + .map_err(EpollHelperError::QueueRingIndex)? + { + helper.add_event(self.net.tap.as_raw_fd(), RX_TAP_EVENT)?; + self.net.rx_tap_listening = true; + info!("Listener registered at start"); + } + + // The NetQueuePair needs the epoll fd. + self.net.epoll_fd = Some(helper.as_raw_fd()); + + helper.run(paused, paused_sync, self)?; + + Ok(()) + } +} + +impl EpollHelperHandler for NetEpollHandler { + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + RX_QUEUE_EVENT => { + self.handle_rx_event().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Error processing RX queue: {:?}", e)) + })?; + } + TX_QUEUE_EVENT => { + let queue_evt = &self.queue_evt_pair.1; + if let Err(e) = queue_evt.read() { + error!("Failed to get tx queue event: {:?}", e); + } + self.handle_tx_event().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Error processing TX queue: {:?}", e)) + })?; + } + TX_TAP_EVENT => { + self.handle_tx_event().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Error processing TX queue (TAP event): {:?}", + e + )) + })?; + } + RX_TAP_EVENT => { + self.handle_rx_tap_event().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Error processing tap queue: {:?}", e)) + })?; + } + RX_RATE_LIMITER_EVENT => { + if let Some(rate_limiter) = &mut self.net.rx_rate_limiter { + // Upon rate limiter event, call the rate limiter handler and register the + // TAP fd for further processing if some RX buffers are available + rate_limiter.event_handler().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Error from 'rate_limiter.event_handler()': {:?}", + e + )) + })?; + + if !self.net.rx_tap_listening && self.net.rx_desc_avail { + net_util::register_listener( + self.net.epoll_fd.unwrap(), + self.net.tap.as_raw_fd(), + epoll::Events::EPOLLIN, + u64::from(self.net.tap_rx_event_id), + ) + .map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Error register_listener with `RX_RATE_LIMITER_EVENT`: {:?}", + e + )) + })?; + + self.net.rx_tap_listening = true; + } + } else { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected RX_RATE_LIMITER_EVENT" + ))); + } + } + TX_RATE_LIMITER_EVENT => { + if let Some(rate_limiter) = &mut self.net.tx_rate_limiter { + // Upon rate limiter event, call the rate limiter handler + // and restart processing the queue. + rate_limiter.event_handler().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Error from 'rate_limiter.event_handler()': {:?}", + e + )) + })?; + + self.process_tx().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Error processing TX queue: {:?}", e)) + })?; + } else { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected TX_RATE_LIMITER_EVENT" + ))); + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected event: {}", + ev_type + ))); + } + } + Ok(()) + } +} + +pub struct Net { + common: VirtioCommon, + id: String, + taps: Vec, + config: VirtioNetConfig, + ctrl_queue_epoll_thread: Option>, + counters: NetCounters, + seccomp_action: SeccompAction, + rate_limiter_config: Option, + exit_evt: EventFd, +} + +#[derive(Serialize, Deserialize)] +pub struct NetState { + pub avail_features: u64, + pub acked_features: u64, + pub config: VirtioNetConfig, + pub queue_size: Vec, +} + +impl Net { + /// Create a new virtio network device with the given TAP interface. + #[allow(clippy::too_many_arguments)] + fn new_with_tap( + id: String, + taps: Vec, + guest_mac: Option, + iommu: bool, + num_queues: usize, + queue_size: u16, + seccomp_action: SeccompAction, + rate_limiter_config: Option, + exit_evt: EventFd, + state: Option, + ) -> Result { + assert!(!taps.is_empty()); + + let mtu = taps[0].mtu().map_err(Error::TapError)? as u16; + + let (avail_features, acked_features, config, queue_sizes) = if let Some(state) = state { + debug!("Restoring virtio-net {}", id); + ( + state.avail_features, + state.acked_features, + state.config, + state.queue_size, + ) + } else { + let mut avail_features = 1 << VIRTIO_NET_F_CSUM + | 1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS + | 1 << VIRTIO_NET_F_GUEST_CSUM + | 1 << VIRTIO_NET_F_GUEST_ECN + | 1 << VIRTIO_NET_F_GUEST_TSO4 + | 1 << VIRTIO_NET_F_GUEST_TSO6 + | 1 << VIRTIO_NET_F_GUEST_UFO + | 1 << VIRTIO_NET_F_HOST_ECN + | 1 << VIRTIO_NET_F_HOST_TSO4 + | 1 << VIRTIO_NET_F_HOST_TSO6 + | 1 << VIRTIO_NET_F_HOST_UFO + | 1 << VIRTIO_NET_F_MTU + | 1 << VIRTIO_RING_F_EVENT_IDX + | 1 << VIRTIO_F_VERSION_1; + + if iommu { + avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM; + } + + avail_features |= 1 << VIRTIO_NET_F_CTRL_VQ; + let queue_num = num_queues + 1; + + let mut config = VirtioNetConfig::default(); + if let Some(mac) = guest_mac { + build_net_config_space( + &mut config, + mac, + num_queues, + Some(mtu), + &mut avail_features, + ); + } else { + build_net_config_space_with_mq( + &mut config, + num_queues, + Some(mtu), + &mut avail_features, + ); + } + + (avail_features, 0, config, vec![queue_size; queue_num]) + }; + + Ok(Net { + common: VirtioCommon { + device_type: VirtioDeviceType::Net as u32, + avail_features, + acked_features, + queue_sizes, + paused_sync: Some(Arc::new(Barrier::new((num_queues / 2) + 1))), + min_queues: 2, + ..Default::default() + }, + id, + taps, + config, + ctrl_queue_epoll_thread: None, + counters: NetCounters::default(), + seccomp_action, + rate_limiter_config, + exit_evt, + }) + } + + /// Create a new virtio network device with the given IP address and + /// netmask. + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + if_name: Option<&str>, + ip_addr: Option, + netmask: Option, + guest_mac: Option, + host_mac: &mut Option, + mtu: Option, + iommu: bool, + num_queues: usize, + queue_size: u16, + seccomp_action: SeccompAction, + rate_limiter_config: Option, + exit_evt: EventFd, + state: Option, + #[allow(unused_variables)] sandbox_id: Option, + ) -> Result { + let taps = open_tap( + if_name, + ip_addr, + netmask, + host_mac, + mtu, + num_queues / 2, + None, + sandbox_id, + ) + .map_err(Error::OpenTap)?; + + Self::new_with_tap( + id, + taps, + guest_mac, + iommu, + num_queues, + queue_size, + seccomp_action, + rate_limiter_config, + exit_evt, + state, + ) + } + + #[allow(clippy::too_many_arguments)] + pub fn from_tap_fds( + id: String, + fds: &[RawFd], + guest_mac: Option, + mtu: Option, + iommu: bool, + queue_size: u16, + seccomp_action: SeccompAction, + rate_limiter_config: Option, + exit_evt: EventFd, + state: Option, + ) -> Result { + let mut taps: Vec = Vec::new(); + let num_queue_pairs = fds.len(); + + for fd in fds.iter() { + // Duplicate so that it can survive reboots + // SAFETY: FFI call to dup. Trivially safe. + let fd = unsafe { libc::dup(*fd) }; + if fd < 0 { + return Err(Error::DuplicateTapFd(std::io::Error::last_os_error())); + } + let tap = Tap::from_tap_fd(fd, num_queue_pairs).map_err(Error::TapError)?; + taps.push(tap); + } + + assert!(!taps.is_empty()); + + if let Some(mtu) = mtu { + taps[0].set_mtu(mtu as i32).map_err(Error::TapError)?; + } + + Self::new_with_tap( + id, + taps, + guest_mac, + iommu, + num_queue_pairs * 2, + queue_size, + seccomp_action, + rate_limiter_config, + exit_evt, + state, + ) + } + + fn state(&self) -> NetState { + NetState { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + config: self.config, + queue_size: self.common.queue_sizes.clone(), + } + } +} + +impl Drop for Net { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + // Needed to ensure all references to tap FDs are dropped (#4868) + self.common.wait_for_epoll_threads(); + } +} + +impl VirtioDevice for Net { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.as_slice(), offset, data); + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + + let num_queues = queues.len(); + let event_idx = self.common.feature_acked(VIRTIO_RING_F_EVENT_IDX.into()); + if self.common.feature_acked(VIRTIO_NET_F_CTRL_VQ.into()) && num_queues % 2 != 0 { + let ctrl_queue_index = num_queues - 1; + let (_, mut ctrl_queue, ctrl_queue_evt) = queues.remove(ctrl_queue_index); + + ctrl_queue.set_event_idx(event_idx); + + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + let mut ctrl_handler = NetCtrlEpollHandler { + mem: mem.clone(), + kill_evt, + pause_evt, + ctrl_q: CtrlQueue::new(self.taps.clone()), + queue: ctrl_queue, + queue_evt: ctrl_queue_evt, + access_platform: self.common.access_platform.clone(), + queue_index: ctrl_queue_index as u16, + interrupt_cb: interrupt_cb.clone(), + }; + + let paused = self.common.paused.clone(); + // Let's update the barrier as we need 1 for each RX/TX pair + + // 1 for the control queue + 1 for the main thread signalling + // the pause. + self.common.paused_sync = Some(Arc::new(Barrier::new(self.taps.len() + 2))); + let paused_sync = self.common.paused_sync.clone(); + + let mut epoll_threads = Vec::new(); + spawn_virtio_thread( + &format!("{}_ctrl", &self.id), + &self.seccomp_action, + Thread::VirtioNetCtl, + &mut epoll_threads, + &self.exit_evt, + move || ctrl_handler.run_ctrl(paused, paused_sync.unwrap()), + )?; + self.ctrl_queue_epoll_thread = Some(epoll_threads.remove(0)); + } + + let mut epoll_threads = Vec::new(); + let mut taps = self.taps.clone(); + for i in 0..queues.len() / 2 { + let rx = RxVirtio::new(); + let tx = TxVirtio::new(); + let rx_tap_listening = false; + + let (_, queue_0, queue_evt_0) = queues.remove(0); + let (_, queue_1, queue_evt_1) = queues.remove(0); + let mut queue_pair = (queue_0, queue_1); + queue_pair.0.set_event_idx(event_idx); + queue_pair.1.set_event_idx(event_idx); + + let queue_evt_pair = (queue_evt_0, queue_evt_1); + + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let rx_rate_limiter: Option = self + .rate_limiter_config + .map(RateLimiterConfig::try_into) + .transpose() + .map_err(ActivateError::CreateRateLimiter)?; + + let tx_rate_limiter: Option = self + .rate_limiter_config + .map(RateLimiterConfig::try_into) + .transpose() + .map_err(ActivateError::CreateRateLimiter)?; + + let tap = taps.remove(0); + + if !tap.is_donated() { + tap.set_offload(virtio_features_to_tap_offload(self.common.acked_features)) + .map_err(|e| { + error!("Error programming tap offload: {:?}", e); + ActivateError::BadActivate + })?; + } + + let mut handler = NetEpollHandler { + net: NetQueuePair { + tap_for_write_epoll: tap.clone(), + tap, + rx, + tx, + epoll_fd: None, + rx_tap_listening, + tx_tap_listening: false, + counters: self.counters.clone(), + tap_rx_event_id: RX_TAP_EVENT, + tap_tx_event_id: TX_TAP_EVENT, + rx_desc_avail: false, + rx_rate_limiter, + tx_rate_limiter, + access_platform: self.common.access_platform.clone(), + rx_rate_limited: std::sync::Once::new(), + tx_rate_limited: std::sync::Once::new(), + id: self.id.clone(), + }, + mem: mem.clone(), + queue_index_base: (i * 2) as u16, + queue_pair, + queue_evt_pair, + interrupt_cb: interrupt_cb.clone(), + kill_evt, + pause_evt, + }; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + + spawn_virtio_thread( + &format!("{}_qp{}", self.id.clone(), i), + &self.seccomp_action, + Thread::VirtioNet, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + } + + self.common.epoll_threads = Some(epoll_threads); + + event!("virtio-device", "activated", "id", &self.id); + Ok(()) + } + + fn reset(&mut self) -> Option> { + let result = self.common.reset(); + event!("virtio-device", "reset", "id", &self.id); + result + } + + fn counters(&self) -> Option>> { + let mut counters = HashMap::new(); + + counters.insert( + "rx_bytes", + Wrapping(self.counters.rx_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "rx_frames", + Wrapping(self.counters.rx_frames.load(Ordering::Acquire)), + ); + counters.insert( + "rx_limit_bytes", + Wrapping(self.counters.rx_limit_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "rx_limit_frames", + Wrapping(self.counters.rx_limit_frames.load(Ordering::Acquire)), + ); + counters.insert( + "tx_bytes", + Wrapping(self.counters.tx_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "tx_frames", + Wrapping(self.counters.tx_frames.load(Ordering::Acquire)), + ); + counters.insert( + "tx_dropped_frames", + Wrapping(self.counters.tx_dropped_frames.load(Ordering::Acquire)), + ); + counters.insert( + "tx_limit_bytes", + Wrapping(self.counters.tx_limit_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "tx_limit_frames", + Wrapping(self.counters.tx_limit_frames.load(Ordering::Acquire)), + ); + + Some(counters) + } + + fn set_access_platform(&mut self, access_platform: Arc) { + self.common.set_access_platform(access_platform) + } +} + +impl Pausable for Net { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume()?; + + if let Some(ctrl_queue_epoll_thread) = &self.ctrl_queue_epoll_thread { + ctrl_queue_epoll_thread.thread().unpark(); + } + Ok(()) + } +} + +impl Snapshottable for Net { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } +} +impl Transportable for Net {} +impl Migratable for Net {} diff --git a/vendor/cube-hypervisor/virtio-devices/src/pmem.rs b/vendor/cube-hypervisor/virtio-devices/src/pmem.rs new file mode 100644 index 0000000..5aa9aa4 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/pmem.rs @@ -0,0 +1,479 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use super::Error as DeviceError; +use super::{ + ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, + UserspaceMapping, VirtioCommon, VirtioDevice, VirtioDeviceType, EPOLL_HELPER_EVENT_LAST, + VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_VERSION_1, +}; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::{GuestMemoryMmap, GuestRegionMmap}; +use crate::{VirtioInterrupt, VirtioInterruptType}; +use anyhow::anyhow; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::fs::File; +use std::io; +use std::mem::size_of; +use std::os::unix::io::AsRawFd; +use std::result; +use std::sync::atomic::AtomicBool; +use std::sync::{Arc, Barrier}; +use thiserror::Error; +use virtio_queue::{DescriptorChain, Queue, QueueT}; +use vm_memory::{ + Address, ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, + GuestMemoryError, GuestMemoryLoadGuard, GuestMemoryRegion, +}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vm_virtio::{AccessPlatform, Translatable}; +use vmm_sys_util::eventfd::EventFd; + +const QUEUE_SIZE: u16 = 256; +const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE]; + +const VIRTIO_PMEM_REQ_TYPE_FLUSH: u32 = 0; +const VIRTIO_PMEM_RESP_TYPE_OK: u32 = 0; +const VIRTIO_PMEM_RESP_TYPE_EIO: u32 = 1; + +// New descriptors are pending on the virtio queue. +const QUEUE_AVAIL_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; + +#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] +#[repr(C)] +struct VirtioPmemConfig { + start: u64, + size: u64, +} + +// SAFETY: it only has data and has no implicit padding. +unsafe impl ByteValued for VirtioPmemConfig {} + +#[derive(Copy, Clone, Debug, Default)] +#[repr(C)] +struct VirtioPmemReq { + type_: u32, +} + +// SAFETY: it only has data and has no implicit padding. +unsafe impl ByteValued for VirtioPmemReq {} + +#[derive(Copy, Clone, Debug, Default)] +#[repr(C)] +struct VirtioPmemResp { + ret: u32, +} + +// SAFETY: it only has data and has no implicit padding. +unsafe impl ByteValued for VirtioPmemResp {} + +#[derive(Error, Debug)] +enum Error { + #[error("Bad guest memory addresses: {0}")] + GuestMemory(GuestMemoryError), + #[error("Unexpected write-only descriptor")] + UnexpectedWriteOnlyDescriptor, + #[error("Unexpected read-only descriptor")] + UnexpectedReadOnlyDescriptor, + #[error("Descriptor chain too short")] + DescriptorChainTooShort, + #[error("Buffer length too small")] + BufferLengthTooSmall, + #[error("Invalid request")] + InvalidRequest, + #[error("Failed adding used index: {0}")] + QueueAddUsed(virtio_queue::Error), +} + +#[derive(Debug, PartialEq, Eq)] +enum RequestType { + Flush, +} + +struct Request { + type_: RequestType, + status_addr: GuestAddress, +} + +impl Request { + fn parse( + desc_chain: &mut DescriptorChain>, + access_platform: Option<&Arc>, + ) -> result::Result { + let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?; + // The descriptor contains the request type which MUST be readable. + if desc.is_write_only() { + return Err(Error::UnexpectedWriteOnlyDescriptor); + } + + if desc.len() as usize != size_of::() { + return Err(Error::InvalidRequest); + } + + let request: VirtioPmemReq = desc_chain + .memory() + .read_obj( + desc.addr() + .translate_gva(access_platform, desc.len() as usize), + ) + .map_err(Error::GuestMemory)?; + + let request_type = match request.type_ { + VIRTIO_PMEM_REQ_TYPE_FLUSH => RequestType::Flush, + _ => return Err(Error::InvalidRequest), + }; + + let status_desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?; + + // The status MUST always be writable + if !status_desc.is_write_only() { + return Err(Error::UnexpectedReadOnlyDescriptor); + } + + if (status_desc.len() as usize) < size_of::() { + return Err(Error::BufferLengthTooSmall); + } + + Ok(Request { + type_: request_type, + status_addr: status_desc + .addr() + .translate_gva(access_platform, status_desc.len() as usize), + }) + } +} + +struct PmemEpollHandler { + mem: GuestMemoryAtomic, + queue: Queue, + disk: File, + interrupt_cb: Arc, + queue_evt: EventFd, + kill_evt: EventFd, + pause_evt: EventFd, + access_platform: Option>, +} + +impl PmemEpollHandler { + fn process_queue(&mut self) -> result::Result { + let mut used_descs = false; + while let Some(mut desc_chain) = self.queue.pop_descriptor_chain(self.mem.memory()) { + let len = match Request::parse(&mut desc_chain, self.access_platform.as_ref()) { + Ok(ref req) if (req.type_ == RequestType::Flush) => { + let status_code = match self.disk.sync_all() { + Ok(()) => VIRTIO_PMEM_RESP_TYPE_OK, + Err(e) => { + error!("failed flushing disk image: {}", e); + VIRTIO_PMEM_RESP_TYPE_EIO + } + }; + + let resp = VirtioPmemResp { ret: status_code }; + match desc_chain.memory().write_obj(resp, req.status_addr) { + Ok(_) => size_of::() as u32, + Err(e) => { + error!("bad guest memory address: {}", e); + 0 + } + } + } + Ok(ref req) => { + // Currently, there is only one virtio-pmem request, FLUSH. + error!("Invalid virtio request type {:?}", req.type_); + 0 + } + Err(e) => { + error!("Failed to parse available descriptor chain: {:?}", e); + 0 + } + }; + + self.queue + .add_used(desc_chain.memory(), desc_chain.head_index(), len) + .map_err(Error::QueueAddUsed)?; + used_descs = true; + } + + Ok(used_descs) + } + + fn signal_used_queue(&self) -> result::Result<(), DeviceError> { + self.interrupt_cb + .trigger(VirtioInterruptType::Queue(0)) + .map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.queue_evt.as_raw_fd(), QUEUE_AVAIL_EVENT)?; + helper.run(paused, paused_sync, self)?; + + Ok(()) + } +} + +impl EpollHelperHandler for PmemEpollHandler { + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + QUEUE_AVAIL_EVENT => { + self.queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + + let needs_notification = self.process_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to process queue : {:?}", e)) + })?; + + if needs_notification { + self.signal_used_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", + e + )) + })?; + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected event: {}", + ev_type + ))); + } + } + Ok(()) + } +} + +pub struct Pmem { + common: VirtioCommon, + id: String, + disk: Option, + config: VirtioPmemConfig, + mapping: UserspaceMapping, + seccomp_action: SeccompAction, + exit_evt: EventFd, + + // Hold ownership of the memory that is allocated for the device + // which will be automatically dropped when the device is dropped. + // The same Arc is also held by MemoryManager::device_memory so that + // virtio backends can dereference GPAs that fall inside this PMEM + // region. + _region: Arc, +} + +#[derive(Serialize, Deserialize)] +pub struct PmemState { + avail_features: u64, + acked_features: u64, + config: VirtioPmemConfig, +} + +impl Pmem { + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + disk: File, + addr: GuestAddress, + mapping: UserspaceMapping, + _region: Arc, + iommu: bool, + seccomp_action: SeccompAction, + exit_evt: EventFd, + state: Option, + ) -> io::Result { + let (avail_features, acked_features, config) = if let Some(state) = state { + debug!("Restoring virtio-pmem {}", id); + (state.avail_features, state.acked_features, state.config) + } else { + let config = VirtioPmemConfig { + start: addr.raw_value().to_le(), + size: _region.len().to_le(), + }; + + let mut avail_features = 1u64 << VIRTIO_F_VERSION_1; + + if iommu { + avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM; + } + (avail_features, 0, config) + }; + + Ok(Pmem { + common: VirtioCommon { + device_type: VirtioDeviceType::Pmem as u32, + queue_sizes: QUEUE_SIZES.to_vec(), + paused_sync: Some(Arc::new(Barrier::new(2))), + avail_features, + acked_features, + min_queues: 1, + ..Default::default() + }, + id, + disk: Some(disk), + config, + mapping, + seccomp_action, + _region, + exit_evt, + }) + } + + fn state(&self) -> PmemState { + PmemState { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + config: self.config, + } + } + + #[cfg(fuzzing)] + pub fn wait_for_epoll_threads(&mut self) { + self.common.wait_for_epoll_threads(); + } +} + +impl Drop for Pmem { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +impl VirtioDevice for Pmem { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.as_slice(), offset, data); + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + if let Some(disk) = self.disk.as_ref() { + let disk = disk.try_clone().map_err(|e| { + error!("failed cloning pmem disk: {}", e); + ActivateError::BadActivate + })?; + + let (_, queue, queue_evt) = queues.remove(0); + + let mut handler = PmemEpollHandler { + mem, + queue, + disk, + interrupt_cb, + queue_evt, + kill_evt, + pause_evt, + access_platform: self.common.access_platform.clone(), + }; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + let mut epoll_threads = Vec::new(); + + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioPmem, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + + self.common.epoll_threads = Some(epoll_threads); + + event!("virtio-device", "activated", "id", &self.id); + return Ok(()); + } + Err(ActivateError::BadActivate) + } + + fn reset(&mut self) -> Option> { + let result = self.common.reset(); + event!("virtio-device", "reset", "id", &self.id); + result + } + + fn userspace_mappings(&self) -> Vec { + vec![self.mapping.clone()] + } + + fn set_access_platform(&mut self, access_platform: Arc) { + self.common.set_access_platform(access_platform) + } +} + +impl Pausable for Pmem { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause()?; + + // Flush pmem backend file data to disk for cross-machine pause-snapshot + // scenarios, ensuring all written data is persisted before the + // snapshot is transferred to a remote host. + if let Some(disk) = self.disk.as_ref() { + disk.sync_all().map_err(|e| { + MigratableError::Pause(anyhow!("Failed to fsync pmem device {}: {}", self.id, e)) + })?; + } + + Ok(()) + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume() + } +} + +impl Snapshottable for Pmem { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } +} + +impl Transportable for Pmem {} +impl Migratable for Pmem {} diff --git a/vendor/cube-hypervisor/virtio-devices/src/rng.rs b/vendor/cube-hypervisor/virtio-devices/src/rng.rs new file mode 100644 index 0000000..4abe904 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/rng.rs @@ -0,0 +1,326 @@ +// Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use super::Error as DeviceError; +use super::{ + ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, VirtioCommon, + VirtioDevice, VirtioDeviceType, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_IOMMU_PLATFORM, + VIRTIO_F_VERSION_1, +}; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::GuestMemoryMmap; +use crate::{VirtioInterrupt, VirtioInterruptType}; +use anyhow::anyhow; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::fs::File; +use std::io; +use std::os::unix::io::AsRawFd; +use std::result; +use std::sync::atomic::AtomicBool; +use std::sync::{Arc, Barrier}; +use thiserror::Error; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{GuestAddressSpace, GuestMemory, GuestMemoryAtomic}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vm_virtio::{AccessPlatform, Translatable}; +use vmm_sys_util::eventfd::EventFd; + +const QUEUE_SIZE: u16 = 256; +const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE]; + +// New descriptors are pending on the virtio queue. +const QUEUE_AVAIL_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; + +#[derive(Error, Debug)] +enum Error { + #[error("Descriptor chain too short")] + DescriptorChainTooShort, + #[error("Invalid descriptor")] + InvalidDescriptor, + #[error("Failed to write to guest memory: {0}")] + GuestMemoryWrite(vm_memory::guest_memory::Error), + #[error("Failed adding used index: {0}")] + QueueAddUsed(virtio_queue::Error), +} + +struct RngEpollHandler { + mem: GuestMemoryAtomic, + queue: Queue, + random_file: File, + interrupt_cb: Arc, + queue_evt: EventFd, + kill_evt: EventFd, + pause_evt: EventFd, + access_platform: Option>, +} + +impl RngEpollHandler { + fn process_queue(&mut self) -> result::Result { + let queue = &mut self.queue; + + let mut used_descs = false; + while let Some(mut desc_chain) = queue.pop_descriptor_chain(self.mem.memory()) { + let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?; + + // The descriptor must be write-only and non-zero length + if !(desc.is_write_only() && desc.len() > 0) { + return Err(Error::InvalidDescriptor); + } + + // Fill the read with data from the random device on the host. + let len = desc_chain + .memory() + .read_volatile_from( + desc.addr() + .translate_gva(self.access_platform.as_ref(), desc.len() as usize), + &mut self.random_file, + desc.len() as usize, + ) + .map_err(Error::GuestMemoryWrite)?; + + queue + .add_used(desc_chain.memory(), desc_chain.head_index(), len as u32) + .map_err(Error::QueueAddUsed)?; + used_descs = true; + } + + Ok(used_descs) + } + + fn signal_used_queue(&self) -> result::Result<(), DeviceError> { + self.interrupt_cb + .trigger(VirtioInterruptType::Queue(0)) + .map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.queue_evt.as_raw_fd(), QUEUE_AVAIL_EVENT)?; + helper.run(paused, paused_sync, self)?; + + Ok(()) + } +} + +impl EpollHelperHandler for RngEpollHandler { + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + QUEUE_AVAIL_EVENT => { + self.queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + let needs_notification = self.process_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to process queue : {:?}", e)) + })?; + if needs_notification { + self.signal_used_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", + e + )) + })?; + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected event: {}", + ev_type + ))); + } + } + Ok(()) + } +} + +/// Virtio device for exposing entropy to the guest OS through virtio. +pub struct Rng { + common: VirtioCommon, + id: String, + random_file: Option, + seccomp_action: SeccompAction, + exit_evt: EventFd, +} + +#[derive(Serialize, Deserialize)] +pub struct RngState { + pub avail_features: u64, + pub acked_features: u64, +} + +impl Rng { + /// Create a new virtio rng device that gets random data from /dev/urandom. + pub fn new( + id: String, + path: &str, + iommu: bool, + seccomp_action: SeccompAction, + exit_evt: EventFd, + state: Option, + ) -> io::Result { + let random_file = File::open(path)?; + + let (avail_features, acked_features) = if let Some(state) = state { + info!("Restoring virtio-rng {}", id); + (state.avail_features, state.acked_features) + } else { + let mut avail_features = 1u64 << VIRTIO_F_VERSION_1; + + if iommu { + avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM; + } + + (avail_features, 0) + }; + + Ok(Rng { + common: VirtioCommon { + device_type: VirtioDeviceType::Rng as u32, + queue_sizes: QUEUE_SIZES.to_vec(), + paused_sync: Some(Arc::new(Barrier::new(2))), + avail_features, + acked_features, + min_queues: 1, + ..Default::default() + }, + id, + random_file: Some(random_file), + seccomp_action, + exit_evt, + }) + } + + fn state(&self) -> RngState { + RngState { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + } + } + + #[cfg(fuzzing)] + pub fn wait_for_epoll_threads(&mut self) { + self.common.wait_for_epoll_threads(); + } +} + +impl Drop for Rng { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +impl VirtioDevice for Rng { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + if let Some(file) = self.random_file.as_ref() { + let random_file = file.try_clone().map_err(|e| { + error!("failed cloning rng source: {}", e); + ActivateError::BadActivate + })?; + + let (_, queue, queue_evt) = queues.remove(0); + + let mut handler = RngEpollHandler { + mem, + queue, + random_file, + interrupt_cb, + queue_evt, + kill_evt, + pause_evt, + access_platform: self.common.access_platform.clone(), + }; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + let mut epoll_threads = Vec::new(); + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioRng, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + + self.common.epoll_threads = Some(epoll_threads); + + event!("virtio-device", "activated", "id", &self.id); + return Ok(()); + } + Err(ActivateError::BadActivate) + } + + fn reset(&mut self) -> Option> { + let result = self.common.reset(); + event!("virtio-device", "reset", "id", &self.id); + result + } + + fn set_access_platform(&mut self, access_platform: Arc) { + self.common.set_access_platform(access_platform) + } +} + +impl Pausable for Rng { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume() + } +} + +impl Snapshottable for Rng { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } +} + +impl Transportable for Rng {} +impl Migratable for Rng {} diff --git a/vendor/cube-hypervisor/virtio-devices/src/seccomp_filters.rs b/vendor/cube-hypervisor/virtio-devices/src/seccomp_filters.rs new file mode 100644 index 0000000..1f44e37 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/seccomp_filters.rs @@ -0,0 +1,577 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use std::convert::TryInto; + +use seccompiler::{ + BpfProgram, Error, SeccompAction, SeccompCmpArgLen as ArgLen, SeccompCmpOp::Eq, + SeccompCondition as Cond, SeccompFilter, SeccompRule, +}; + +pub enum Thread { + VirtioBalloon, + VirtioBlock, + VirtioConsole, + VirtioIommu, + VirtioMem, + VirtioNet, + VirtioNetCtl, + VirtioPmem, + VirtioRng, + VirtioVhostBlock, + VirtioVhostFs, + VirtioVhostNet, + VirtioVhostNetCtl, + VirtioVsock, + VirtioWatchdog, + VirtioFs, +} + +/// Shorthand for chaining `SeccompCondition`s with the `and` operator in a `SeccompRule`. +/// The rule will take the `Allow` action if _all_ the conditions are true. +/// +/// [`SeccompCondition`]: struct.SeccompCondition.html +/// [`SeccompRule`]: struct.SeccompRule.html +macro_rules! and { + ($($x:expr),*) => (SeccompRule::new(vec![$($x),*]).unwrap()) +} + +/// Shorthand for chaining `SeccompRule`s with the `or` operator in a `SeccompFilter`. +/// +/// [`SeccompFilter`]: struct.SeccompFilter.html +/// [`SeccompRule`]: struct.SeccompRule.html +macro_rules! or { + ($($x:expr,)*) => (vec![$($x),*]); + ($($x:expr),*) => (vec![$($x),*]) +} + +// See include/uapi/asm-generic/ioctls.h in the kernel code. +const TIOCGWINSZ: u64 = 0x5413; +const FIONBIO: u64 = 0x5421; + +// See include/uapi/linux/vfio.h in the kernel code. +const VFIO_IOMMU_MAP_DMA: u64 = 0x3b71; +const VFIO_IOMMU_UNMAP_DMA: u64 = 0x3b72; + +// See include/uapi/linux/if_tun.h in the kernel code. +const TUNSETOFFLOAD: u64 = 0x4004_54d0; + +fn create_virtio_console_ioctl_seccomp_rule() -> Vec { + or![and![Cond::new(1, ArgLen::Dword, Eq, TIOCGWINSZ).unwrap()]] +} + +fn create_virtio_iommu_ioctl_seccomp_rule() -> Vec { + or![ + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_IOMMU_MAP_DMA).unwrap()], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_IOMMU_UNMAP_DMA).unwrap()], + ] +} + +fn create_virtio_mem_ioctl_seccomp_rule() -> Vec { + or![ + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_IOMMU_MAP_DMA).unwrap()], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_IOMMU_UNMAP_DMA).unwrap()], + ] +} + +fn virtio_balloon_thread_rules() -> Vec<(i64, Vec)> { + vec![(libc::SYS_fallocate, vec![])] +} + +fn virtio_block_thread_rules() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_fallocate, vec![]), + (libc::SYS_fdatasync, vec![]), + (libc::SYS_fsync, vec![]), + (libc::SYS_ftruncate, vec![]), + (libc::SYS_getrandom, vec![]), + (libc::SYS_io_uring_enter, vec![]), + (libc::SYS_lseek, vec![]), + // before_pause() drains in-flight IO via libc::poll; glibc maps this + // to SYS_poll on x86_64 and SYS_ppoll on aarch64. Without these a + // pause-snapshot raises SIGSYS when seccomp is not Allow. + #[cfg(target_arch = "x86_64")] + (libc::SYS_poll, vec![]), + #[cfg(target_arch = "aarch64")] + (libc::SYS_ppoll, vec![]), + (libc::SYS_prctl, vec![]), + (libc::SYS_pread64, vec![]), + (libc::SYS_preadv, vec![]), + (libc::SYS_pwritev, vec![]), + (libc::SYS_pwrite64, vec![]), + (libc::SYS_sched_getaffinity, vec![]), + (libc::SYS_set_robust_list, vec![]), + (libc::SYS_timerfd_settime, vec![]), + ] +} + +fn virtio_console_thread_rules() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_ioctl, create_virtio_console_ioctl_seccomp_rule()), + (libc::SYS_prctl, vec![]), + (libc::SYS_sched_getaffinity, vec![]), + (libc::SYS_set_robust_list, vec![]), + ] +} + +fn virtio_iommu_thread_rules() -> Vec<(i64, Vec)> { + vec![(libc::SYS_ioctl, create_virtio_iommu_ioctl_seccomp_rule())] +} + +fn virtio_mem_thread_rules() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_fallocate, vec![]), + (libc::SYS_ioctl, create_virtio_mem_ioctl_seccomp_rule()), + (libc::SYS_recvfrom, vec![]), + (libc::SYS_sendmsg, vec![]), + ] +} + +fn virtio_net_thread_rules() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_readv, vec![]), + (libc::SYS_timerfd_settime, vec![]), + (libc::SYS_writev, vec![]), + ] +} + +fn create_virtio_net_ctl_ioctl_seccomp_rule() -> Vec { + or![and![Cond::new(1, ArgLen::Dword, Eq, TUNSETOFFLOAD).unwrap()],] +} + +fn virtio_net_ctl_thread_rules() -> Vec<(i64, Vec)> { + vec![(libc::SYS_ioctl, create_virtio_net_ctl_ioctl_seccomp_rule())] +} + +fn virtio_pmem_thread_rules() -> Vec<(i64, Vec)> { + vec![(libc::SYS_fsync, vec![])] +} + +fn virtio_rng_thread_rules() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_prctl, vec![]), + (libc::SYS_sched_getaffinity, vec![]), + (libc::SYS_set_robust_list, vec![]), + ] +} + +fn virtio_vhost_fs_thread_rules() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_connect, vec![]), + (libc::SYS_nanosleep, vec![]), + (libc::SYS_pread64, vec![]), + (libc::SYS_pwrite64, vec![]), + (libc::SYS_recvmsg, vec![]), + (libc::SYS_sendmsg, vec![]), + (libc::SYS_sendto, vec![]), + (libc::SYS_socket, vec![]), + ] +} + +fn virtio_vhost_net_ctl_thread_rules() -> Vec<(i64, Vec)> { + vec![] +} + +fn virtio_vhost_net_thread_rules() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_accept4, vec![]), + (libc::SYS_bind, vec![]), + (libc::SYS_getcwd, vec![]), + (libc::SYS_listen, vec![]), + (libc::SYS_recvmsg, vec![]), + (libc::SYS_sendmsg, vec![]), + (libc::SYS_sendto, vec![]), + (libc::SYS_socket, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_unlink, vec![]), + #[cfg(target_arch = "aarch64")] + (libc::SYS_unlinkat, vec![]), + ] +} + +fn virtio_vhost_block_thread_rules() -> Vec<(i64, Vec)> { + vec![] +} + +fn create_vsock_ioctl_seccomp_rule() -> Vec { + or![and![Cond::new(1, ArgLen::Dword, Eq, FIONBIO,).unwrap()],] +} + +fn create_vsock_fcntl_seccomp_rule() -> Vec { + or![ + and![Cond::new(1, ArgLen::Dword, Eq, libc::F_GETFD as u64).unwrap()], + and![Cond::new(1, ArgLen::Dword, Eq, libc::F_GETFL as u64).unwrap()], + and![Cond::new(1, ArgLen::Dword, Eq, libc::F_SETFL as u64).unwrap()], + and![Cond::new(1, ArgLen::Dword, Eq, libc::F_DUPFD_CLOEXEC as u64).unwrap()], + ] +} + +pub fn create_virtio_device_getsockopt_seccomp_rule() -> Vec { + vec![and![ + Cond::new(1, ArgLen::Dword, Eq, libc::SOL_SOCKET as u64).unwrap(), + Cond::new(2, ArgLen::Dword, Eq, libc::SO_PEERCRED as u64).unwrap() + ]] +} + +fn create_vsock_recvmsg_seccomp_rule() -> Vec { + vec![and![Cond::new(2, ArgLen::Dword, Eq, 0).unwrap()]] +} + +fn virtio_vsock_thread_rules() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_accept4, vec![]), + (libc::SYS_connect, vec![]), + // Passfd validates received SCM_RIGHTS descriptors and sets O_NONBLOCK. + (libc::SYS_fcntl, create_vsock_fcntl_seccomp_rule()), + (libc::SYS_fstat, vec![]), + // VsockMuxer validates every host-side UDS peer with + // getsockopt(SO_PEERCRED) before accepting its connect/passfd command. + (libc::SYS_geteuid, vec![]), + ( + libc::SYS_getsockopt, + create_virtio_device_getsockopt_seccomp_rule(), + ), + (libc::SYS_ioctl, create_vsock_ioctl_seccomp_rule()), + (libc::SYS_recvfrom, vec![]), + // recv_with_fd() receives passfd commands and SCM_RIGHTS. + (libc::SYS_recvmsg, create_vsock_recvmsg_seccomp_rule()), + (libc::SYS_socket, vec![]), + ] +} + +fn virtio_watchdog_thread_rules() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_prctl, vec![]), + (libc::SYS_sched_getaffinity, vec![]), + (libc::SYS_set_robust_list, vec![]), + (libc::SYS_timerfd_settime, vec![]), + ] +} + +fn virtio_fs_thread_rules() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_accept4, vec![]), + (libc::SYS_brk, vec![]), + (libc::SYS_capget, vec![]), // For CAP_FSETID + (libc::SYS_capset, vec![]), + (libc::SYS_clock_gettime, vec![]), + (libc::SYS_clone, vec![]), + (libc::SYS_clone3, vec![]), + (libc::SYS_close, vec![]), + (libc::SYS_copy_file_range, vec![]), + (libc::SYS_dup, vec![]), + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64le" + ))] + (libc::SYS_epoll_create, vec![]), + (libc::SYS_epoll_create1, vec![]), + (libc::SYS_epoll_ctl, vec![]), + (libc::SYS_epoll_pwait, vec![]), + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64le" + ))] + (libc::SYS_epoll_wait, vec![]), + (libc::SYS_eventfd2, vec![]), + (libc::SYS_exit, vec![]), + (libc::SYS_exit_group, vec![]), + (libc::SYS_fallocate, vec![]), + (libc::SYS_fchdir, vec![]), + (libc::SYS_fchmod, vec![]), + (libc::SYS_fchmodat, vec![]), + (libc::SYS_fchownat, vec![]), + (libc::SYS_fcntl, vec![]), + (libc::SYS_fdatasync, vec![]), + (libc::SYS_fgetxattr, vec![]), + (libc::SYS_flistxattr, vec![]), + (libc::SYS_flock, vec![]), + (libc::SYS_fremovexattr, vec![]), + (libc::SYS_fsetxattr, vec![]), + (libc::SYS_fstat, vec![]), + #[cfg(target_arch = "s390x")] + (libc::SYS_fstatfs64, vec![]), + (libc::SYS_fstatfs, vec![]), + (libc::SYS_fsync, vec![]), + (libc::SYS_ftruncate, vec![]), + (libc::SYS_futex, vec![]), + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64le" + ))] + (libc::SYS_getdents, vec![]), + (libc::SYS_getdents64, vec![]), + (libc::SYS_getegid, vec![]), + (libc::SYS_geteuid, vec![]), + (libc::SYS_getpid, vec![]), + (libc::SYS_gettid, vec![]), + (libc::SYS_gettimeofday, vec![]), + (libc::SYS_getxattr, vec![]), + (libc::SYS_linkat, vec![]), + (libc::SYS_listxattr, vec![]), + (libc::SYS_lseek, vec![]), + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64le" + ))] + (libc::SYS_lstat, vec![]), + (libc::SYS_madvise, vec![]), + (libc::SYS_mkdirat, vec![]), + (libc::SYS_mknodat, vec![]), + (libc::SYS_mmap, vec![]), + (libc::SYS_mprotect, vec![]), + (libc::SYS_mremap, vec![]), + (libc::SYS_munmap, vec![]), + (libc::SYS_name_to_handle_at, vec![]), + (libc::SYS_newfstatat, vec![]), + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64le" + ))] + (libc::SYS_open, vec![]), + (libc::SYS_openat, vec![]), + (libc::SYS_openat2, vec![]), + (libc::SYS_open_by_handle_at, vec![]), + (libc::SYS_prctl, vec![]), + (libc::SYS_preadv, vec![]), + (libc::SYS_prlimit64, vec![]), + (libc::SYS_pwritev, vec![]), + (libc::SYS_pwritev2, vec![]), + (libc::SYS_read, vec![]), + (libc::SYS_readlinkat, vec![]), + (libc::SYS_recvmsg, vec![]), + (libc::SYS_renameat, vec![]), + (libc::SYS_renameat2, vec![]), + (libc::SYS_removexattr, vec![]), + (libc::SYS_rt_sigaction, vec![]), + (libc::SYS_rt_sigprocmask, vec![]), + (libc::SYS_rt_sigreturn, vec![]), + (libc::SYS_sched_getaffinity, vec![]), + (libc::SYS_sendmsg, vec![]), + (libc::SYS_set_robust_list, vec![]), + (libc::SYS_setresgid, vec![]), + (libc::SYS_setresuid, vec![]), + //(libc::SYS_setresgid32); Needed on some platforms, + //(libc::SYS_setresuid32); Needed on some platforms + (libc::SYS_setxattr, vec![]), + (libc::SYS_sigaltstack, vec![]), + #[cfg(target_arch = "s390x")] + (libc::SYS_sigreturn, vec![]), + (libc::SYS_statx, vec![]), + (libc::SYS_symlinkat, vec![]), + (libc::SYS_syncfs, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_time, vec![]), // Rarely needed, except on static builds + (libc::SYS_umask, vec![]), + (libc::SYS_unshare, vec![]), + (libc::SYS_utimensat, vec![]), + (libc::SYS_write, vec![]), + (libc::SYS_writev, vec![]), + (libc::SYS_timerfd_create, vec![]), + (libc::SYS_timerfd_settime, vec![]), + ] +} + +pub fn create_virtio_device_ioctl_seccomp_rule() -> Vec { + let mut common_rules = vec![]; + let rules = create_virtio_console_ioctl_seccomp_rule(); + common_rules.extend(rules); + let rules = create_virtio_net_ctl_ioctl_seccomp_rule(); + common_rules.extend(rules); + let rules = create_vsock_ioctl_seccomp_rule(); + common_rules.extend(rules); + common_rules +} + +pub fn virtio_device_thread_rules() -> Vec<(i64, Vec)> { + let mut rules = vec![ + (libc::SYS_gettid, vec![]), + (libc::SYS_gettimeofday, vec![]), + (libc::SYS_getuid, vec![]), + // to support operations in init_backendfs() + #[cfg(target_arch = "x86_64")] + (libc::SYS_lstat, vec![]), + (libc::SYS_prlimit64, vec![]), + ]; + + rules.append(&mut virtio_balloon_thread_rules()); + rules.append(&mut virtio_block_thread_rules()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut virtio_net_thread_rules()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut virtio_net_ctl_thread_rules()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut virtio_pmem_thread_rules()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut virtio_rng_thread_rules()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut virtio_vsock_thread_rules()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut virtio_fs_thread_rules()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut virtio_thread_common()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + + let mut ioctl_rules = vec![(libc::SYS_ioctl, create_virtio_device_ioctl_seccomp_rule())]; + rules.append(&mut ioctl_rules); + + rules +} + +fn get_seccomp_rules(thread_type: Thread) -> Vec<(i64, Vec)> { + let mut rules = match thread_type { + Thread::VirtioBalloon => virtio_balloon_thread_rules(), + Thread::VirtioBlock => virtio_block_thread_rules(), + Thread::VirtioConsole => virtio_console_thread_rules(), + Thread::VirtioIommu => virtio_iommu_thread_rules(), + Thread::VirtioMem => virtio_mem_thread_rules(), + Thread::VirtioNet => virtio_net_thread_rules(), + Thread::VirtioNetCtl => virtio_net_ctl_thread_rules(), + Thread::VirtioPmem => virtio_pmem_thread_rules(), + Thread::VirtioRng => virtio_rng_thread_rules(), + Thread::VirtioVhostBlock => virtio_vhost_block_thread_rules(), + Thread::VirtioVhostFs => virtio_vhost_fs_thread_rules(), + Thread::VirtioVhostNet => virtio_vhost_net_thread_rules(), + Thread::VirtioVhostNetCtl => virtio_vhost_net_ctl_thread_rules(), + Thread::VirtioVsock => virtio_vsock_thread_rules(), + Thread::VirtioWatchdog => virtio_watchdog_thread_rules(), + Thread::VirtioFs => virtio_fs_thread_rules(), + }; + rules.append(&mut virtio_thread_common()); + rules +} + +fn virtio_thread_common() -> Vec<(i64, Vec)> { + vec![ + (libc::SYS_brk, vec![]), + (libc::SYS_clock_gettime, vec![]), + (libc::SYS_close, vec![]), + (libc::SYS_dup, vec![]), + (libc::SYS_epoll_create1, vec![]), + (libc::SYS_epoll_ctl, vec![]), + (libc::SYS_epoll_pwait, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_epoll_wait, vec![]), + (libc::SYS_exit, vec![]), + (libc::SYS_futex, vec![]), + (libc::SYS_madvise, vec![]), + (libc::SYS_mmap, vec![]), + (libc::SYS_mprotect, vec![]), + (libc::SYS_munmap, vec![]), + (libc::SYS_openat, vec![]), + (libc::SYS_read, vec![]), + (libc::SYS_rt_sigprocmask, vec![]), + (libc::SYS_rt_sigreturn, vec![]), + (libc::SYS_sigaltstack, vec![]), + (libc::SYS_write, vec![]), + ] +} + +/// Generate a BPF program based on the seccomp_action value +pub fn get_seccomp_filter( + seccomp_action: &SeccompAction, + thread_type: Thread, +) -> Result { + match seccomp_action { + SeccompAction::Allow => Ok(vec![]), + SeccompAction::KillProcess => Ok(vec![]), + SeccompAction::Log => SeccompFilter::new( + get_seccomp_rules(thread_type).into_iter().collect(), + SeccompAction::Log, + SeccompAction::Allow, + std::env::consts::ARCH.try_into().unwrap(), + ) + .and_then(|filter| filter.try_into()) + .map_err(Error::Backend), + _ => SeccompFilter::new( + get_seccomp_rules(thread_type).into_iter().collect(), + SeccompAction::Trap, + SeccompAction::Allow, + std::env::consts::ARCH.try_into().unwrap(), + ) + .and_then(|filter| filter.try_into()) + .map_err(Error::Backend), + } +} + +#[cfg(test)] +mod tests { + use super::{ + get_seccomp_filter, virtio_balloon_thread_rules, virtio_device_thread_rules, Thread, + }; + #[cfg(target_os = "linux")] + use seccompiler::apply_filter; + use seccompiler::SeccompAction; + #[cfg(target_os = "linux")] + use std::os::unix::io::AsRawFd; + #[cfg(target_os = "linux")] + use std::os::unix::net::UnixStream; + + #[test] + fn balloon_rules_cover_shared_backing_release() { + let rules = virtio_balloon_thread_rules(); + assert!(rules + .iter() + .any(|(syscall, _)| *syscall == libc::SYS_fallocate)); + } + + #[test] + fn process_wide_rules_include_balloon_syscalls() { + let rules = virtio_device_thread_rules(); + assert!(rules + .iter() + .any(|(syscall, _)| *syscall == libc::SYS_fallocate)); + } + + #[test] + fn strict_balloon_filter_compiles() { + let filter = get_seccomp_filter(&SeccompAction::Trap, Thread::VirtioBalloon).unwrap(); + assert!(!filter.is_empty()); + } + + #[cfg(target_os = "linux")] + #[test] + fn strict_vsock_filter_allows_peer_credentials() { + let (socket, _peer) = UnixStream::pair().unwrap(); + let filter = get_seccomp_filter(&SeccompAction::Trap, Thread::VirtioVsock).unwrap(); + let pid = unsafe { libc::fork() }; + assert!(pid >= 0, "fork failed: {}", std::io::Error::last_os_error()); + + if pid == 0 { + if apply_filter(&filter).is_err() { + unsafe { libc::syscall(libc::SYS_exit, 3) }; + } + let mut credentials = std::mem::MaybeUninit::::uninit(); + let mut length = std::mem::size_of::() as libc::socklen_t; + let result = unsafe { + libc::getsockopt( + socket.as_raw_fd(), + libc::SOL_SOCKET, + libc::SO_PEERCRED, + credentials.as_mut_ptr().cast(), + &mut length, + ) + }; + let exit_code = if result == 0 { 0 } else { 2 }; + unsafe { + libc::syscall(libc::SYS_exit, exit_code); + } + unreachable!(); + } + + let mut status = 0; + assert_eq!(unsafe { libc::waitpid(pid, &mut status, 0) }, pid); + assert!( + libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0, + "strict vsock filter rejected getsockopt(SOL_SOCKET, SO_PEERCRED): wait status {status:#x}" + ); + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/thread_helper.rs b/vendor/cube-hypervisor/virtio-devices/src/thread_helper.rs new file mode 100644 index 0000000..5d6a113 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/thread_helper.rs @@ -0,0 +1,66 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::{ + epoll_helper::EpollHelperError, + seccomp_filters::{get_seccomp_filter, Thread}, + ActivateError, +}; +use seccompiler::{apply_filter, SeccompAction}; +use std::{ + panic::AssertUnwindSafe, + thread::{self, JoinHandle}, +}; +use vmm_sys_util::eventfd::EventFd; + +pub(crate) fn spawn_virtio_thread( + name: &str, + seccomp_action: &SeccompAction, + thread_type: Thread, + epoll_threads: &mut Vec>, + exit_evt: &EventFd, + f: F, +) -> Result<(), ActivateError> +where + F: FnOnce() -> std::result::Result<(), EpollHelperError>, + F: Send + 'static, +{ + let seccomp_filter = get_seccomp_filter(seccomp_action, thread_type) + .map_err(ActivateError::CreateSeccompFilter)?; + + let thread_exit_evt = exit_evt + .try_clone() + .map_err(ActivateError::CloneExitEventFd)?; + let thread_name = name.to_string(); + + thread::Builder::new() + .name(name.to_string()) + .spawn(move || { + if !seccomp_filter.is_empty() { + if let Err(e) = apply_filter(&seccomp_filter) { + error!("Error applying seccomp filter: {:?}", e); + thread_exit_evt.write(1).ok(); + return; + } + } + match std::panic::catch_unwind(AssertUnwindSafe(f)) { + Err(_) => { + error!("{} thread panicked", thread_name); + thread_exit_evt.write(1).ok(); + } + Ok(r) => { + if let Err(e) = r { + error!("Error running worker: {:?}", e); + thread_exit_evt.write(1).ok(); + } + } + }; + }) + .map(|thread| epoll_threads.push(thread)) + .map_err(|e| { + error!("Failed to spawn thread for {}: {}", name, e); + ActivateError::ThreadSpawn(e) + }) +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/transport/mod.rs b/vendor/cube-hypervisor/virtio-devices/src/transport/mod.rs new file mode 100644 index 0000000..cb38c6f --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/transport/mod.rs @@ -0,0 +1,13 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use vmm_sys_util::eventfd::EventFd; +mod pci_common_config; +mod pci_device; +pub use pci_common_config::VirtioPciCommonConfig; +pub use pci_device::{VirtioPciDevice, VirtioPciDeviceActivator}; + +pub trait VirtioTransport { + fn ioeventfds(&self, base_addr: u64) -> Vec<(&EventFd, u64)>; +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/transport/pci_common_config.rs b/vendor/cube-hypervisor/virtio-devices/src/transport/pci_common_config.rs new file mode 100644 index 0000000..7ac55ff --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/transport/pci_common_config.rs @@ -0,0 +1,414 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use crate::VirtioDevice; +use byteorder::{ByteOrder, LittleEndian}; +use serde::{Deserialize, Serialize}; +use std::sync::atomic::{AtomicU16, Ordering}; +use std::sync::{Arc, Mutex}; +use virtio_queue::{Queue, QueueT}; +use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable}; +use vm_virtio::AccessPlatform; + +#[derive(Clone, Serialize, Deserialize)] +pub struct VirtioPciCommonConfigState { + pub driver_status: u8, + pub config_generation: u8, + pub device_feature_select: u32, + pub driver_feature_select: u32, + pub queue_select: u16, + pub msix_config: u16, + pub msix_queues: Vec, +} + +/// Contains the data for reading and writing the common configuration structure of a virtio PCI +/// device. +/// +/// * Registers: +/// ** About the whole device. +/// le32 device_feature_select; // 0x00 // read-write +/// le32 device_feature; // 0x04 // read-only for driver +/// le32 driver_feature_select; // 0x08 // read-write +/// le32 driver_feature; // 0x0C // read-write +/// le16 msix_config; // 0x10 // read-write +/// le16 num_queues; // 0x12 // read-only for driver +/// u8 device_status; // 0x14 // read-write (driver_status) +/// u8 config_generation; // 0x15 // read-only for driver +/// ** About a specific virtqueue. +/// le16 queue_select; // 0x16 // read-write +/// le16 queue_size; // 0x18 // read-write, power of 2, or 0. +/// le16 queue_msix_vector; // 0x1A // read-write +/// le16 queue_enable; // 0x1C // read-write (Ready) +/// le16 queue_notify_off; // 0x1E // read-only for driver +/// le64 queue_desc; // 0x20 // read-write +/// le64 queue_avail; // 0x28 // read-write +/// le64 queue_used; // 0x30 // read-write +pub struct VirtioPciCommonConfig { + pub access_platform: Option>, + pub driver_status: u8, + pub config_generation: u8, + pub device_feature_select: u32, + pub driver_feature_select: u32, + pub queue_select: u16, + pub msix_config: Arc, + pub msix_queues: Arc>>, +} + +impl VirtioPciCommonConfig { + fn state(&self) -> VirtioPciCommonConfigState { + VirtioPciCommonConfigState { + driver_status: self.driver_status, + config_generation: self.config_generation, + device_feature_select: self.device_feature_select, + driver_feature_select: self.driver_feature_select, + queue_select: self.queue_select, + msix_config: self.msix_config.load(Ordering::Acquire), + msix_queues: self.msix_queues.lock().unwrap().clone(), + } + } + + fn set_state(&mut self, state: &VirtioPciCommonConfigState) { + self.driver_status = state.driver_status; + self.config_generation = state.config_generation; + self.device_feature_select = state.device_feature_select; + self.driver_feature_select = state.driver_feature_select; + self.queue_select = state.queue_select; + self.msix_config.store(state.msix_config, Ordering::Release); + *(self.msix_queues.lock().unwrap()) = state.msix_queues.clone(); + } + + pub fn read( + &mut self, + offset: u64, + data: &mut [u8], + queues: &mut [Queue], + device: Arc>, + ) { + assert!(data.len() <= 8); + + match data.len() { + 1 => { + let v = self.read_common_config_byte(offset); + data[0] = v; + } + 2 => { + let v = self.read_common_config_word(offset, queues); + LittleEndian::write_u16(data, v); + } + 4 => { + let v = self.read_common_config_dword(offset, device); + LittleEndian::write_u32(data, v); + } + 8 => { + let v = self.read_common_config_qword(offset); + LittleEndian::write_u64(data, v); + } + _ => error!("invalid data length for virtio read: len {}", data.len()), + } + } + + pub fn write( + &mut self, + offset: u64, + data: &[u8], + queues: &mut [Queue], + device: Arc>, + ) { + assert!(data.len() <= 8); + + match data.len() { + 1 => self.write_common_config_byte(offset, data[0]), + 2 => self.write_common_config_word(offset, LittleEndian::read_u16(data), queues), + 4 => { + self.write_common_config_dword(offset, LittleEndian::read_u32(data), queues, device) + } + 8 => self.write_common_config_qword(offset, LittleEndian::read_u64(data), queues), + _ => error!("invalid data length for virtio write: len {}", data.len()), + } + } + + fn read_common_config_byte(&self, offset: u64) -> u8 { + debug!("read_common_config_byte: offset 0x{:x}", offset); + // The driver is only allowed to do aligned, properly sized access. + match offset { + 0x14 => self.driver_status, + 0x15 => self.config_generation, + _ => { + warn!("invalid virtio config byte read: 0x{:x}", offset); + 0 + } + } + } + + fn write_common_config_byte(&mut self, offset: u64, value: u8) { + debug!("write_common_config_byte: offset 0x{:x}", offset); + match offset { + 0x14 => self.driver_status = value, + _ => { + warn!("invalid virtio config byte write: 0x{:x}", offset); + } + } + } + + fn read_common_config_word(&self, offset: u64, queues: &[Queue]) -> u16 { + debug!("read_common_config_word: offset 0x{:x}", offset); + match offset { + 0x10 => self.msix_config.load(Ordering::Acquire), + 0x12 => queues.len() as u16, // num_queues + 0x16 => self.queue_select, + 0x18 => self.with_queue(queues, |q| q.size()).unwrap_or(0), + 0x1a => self.msix_queues.lock().unwrap()[self.queue_select as usize], + 0x1c => u16::from(self.with_queue(queues, |q| q.ready()).unwrap_or(false)), + 0x1e => self.queue_select, // notify_off + _ => { + warn!("invalid virtio register word read: 0x{:x}", offset); + 0 + } + } + } + + fn write_common_config_word(&mut self, offset: u64, value: u16, queues: &mut [Queue]) { + debug!("write_common_config_word: offset 0x{:x}", offset); + match offset { + 0x10 => self.msix_config.store(value, Ordering::Release), + 0x16 => self.queue_select = value, + 0x18 => self.with_queue_mut(queues, |q| q.set_size(value)), + 0x1a => self.msix_queues.lock().unwrap()[self.queue_select as usize] = value, + 0x1c => self.with_queue_mut(queues, |q| { + let ready = value == 1; + q.set_ready(ready); + // Translate address of descriptor table and vrings. + if let Some(access_platform) = &self.access_platform { + if ready { + let desc_table = access_platform.translate_gva(q.desc_table(), 0).unwrap(); + let avail_ring = access_platform.translate_gva(q.avail_ring(), 0).unwrap(); + let used_ring = access_platform.translate_gva(q.used_ring(), 0).unwrap(); + q.set_desc_table_address( + Some((desc_table & 0xffff_ffff) as u32), + Some((desc_table >> 32) as u32), + ); + q.set_avail_ring_address( + Some((avail_ring & 0xffff_ffff) as u32), + Some((avail_ring >> 32) as u32), + ); + q.set_used_ring_address( + Some((used_ring & 0xffff_ffff) as u32), + Some((used_ring >> 32) as u32), + ); + } + } + }), + _ => { + warn!("invalid virtio register word write: 0x{:x}", offset); + } + } + } + + fn read_common_config_dword(&self, offset: u64, device: Arc>) -> u32 { + debug!("read_common_config_dword: offset 0x{:x}", offset); + match offset { + 0x00 => self.device_feature_select, + 0x04 => { + let locked_device = device.lock().unwrap(); + // Only 64 bits of features (2 pages) are defined for now, so limit + // device_feature_select to avoid shifting by 64 or more bits. + if self.device_feature_select < 2 { + (locked_device.features() >> (self.device_feature_select * 32)) as u32 + } else { + 0 + } + } + 0x08 => self.driver_feature_select, + _ => { + warn!("invalid virtio register dword read: 0x{:x}", offset); + 0 + } + } + } + + fn write_common_config_dword( + &mut self, + offset: u64, + value: u32, + queues: &mut [Queue], + device: Arc>, + ) { + debug!("write_common_config_dword: offset 0x{:x}", offset); + + match offset { + 0x00 => self.device_feature_select = value, + 0x08 => self.driver_feature_select = value, + 0x0c => { + if self.driver_feature_select < 2 { + let mut locked_device = device.lock().unwrap(); + locked_device + .ack_features(u64::from(value) << (self.driver_feature_select * 32)); + } else { + warn!( + "invalid ack_features (page {}, value 0x{:x})", + self.driver_feature_select, value + ); + } + } + 0x20 => self.with_queue_mut(queues, |q| q.set_desc_table_address(Some(value), None)), + 0x24 => self.with_queue_mut(queues, |q| q.set_desc_table_address(None, Some(value))), + 0x28 => self.with_queue_mut(queues, |q| q.set_avail_ring_address(Some(value), None)), + 0x2c => self.with_queue_mut(queues, |q| q.set_avail_ring_address(None, Some(value))), + 0x30 => self.with_queue_mut(queues, |q| q.set_used_ring_address(Some(value), None)), + 0x34 => self.with_queue_mut(queues, |q| q.set_used_ring_address(None, Some(value))), + _ => { + warn!("invalid virtio register dword write: 0x{:x}", offset); + } + } + } + + fn read_common_config_qword(&self, _offset: u64) -> u64 { + debug!("read_common_config_qword: offset 0x{:x}", _offset); + 0 // Assume the guest has no reason to read write-only registers. + } + + fn write_common_config_qword(&mut self, offset: u64, value: u64, queues: &mut [Queue]) { + debug!("write_common_config_qword: offset 0x{:x}", offset); + + let low = Some((value & 0xffff_ffff) as u32); + let high = Some((value >> 32) as u32); + + match offset { + 0x20 => self.with_queue_mut(queues, |q| q.set_desc_table_address(low, high)), + 0x28 => self.with_queue_mut(queues, |q| q.set_avail_ring_address(low, high)), + 0x30 => self.with_queue_mut(queues, |q| q.set_used_ring_address(low, high)), + _ => { + warn!("invalid virtio register qword write: 0x{:x}", offset); + } + } + } + + fn with_queue(&self, queues: &[Queue], f: F) -> Option + where + F: FnOnce(&Queue) -> U, + { + queues.get(self.queue_select as usize).map(f) + } + + fn with_queue_mut(&self, queues: &mut [Queue], f: F) { + if let Some(queue) = queues.get_mut(self.queue_select as usize) { + f(queue); + } + } +} + +impl Pausable for VirtioPciCommonConfig {} + +impl Snapshottable for VirtioPciCommonConfig { + fn id(&self) -> String { + String::from("virtio_pci_common_config") + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id(), &self.state()) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + self.set_state(&snapshot.to_state(&self.id())?); + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::GuestMemoryMmap; + use crate::{ActivateResult, VirtioInterrupt}; + use std::sync::Arc; + use virtio_queue::Queue; + use vm_memory::GuestMemoryAtomic; + use vmm_sys_util::eventfd::EventFd; + + struct DummyDevice(u32); + const QUEUE_SIZE: u16 = 256; + const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE]; + const DUMMY_FEATURES: u64 = 0x5555_aaaa; + impl VirtioDevice for DummyDevice { + fn device_type(&self) -> u32 { + self.0 + } + fn queue_max_sizes(&self) -> &[u16] { + QUEUE_SIZES + } + fn activate( + &mut self, + _mem: GuestMemoryAtomic, + _interrupt_evt: Arc, + _queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + Ok(()) + } + + fn features(&self) -> u64 { + DUMMY_FEATURES + } + + fn ack_features(&mut self, _value: u64) {} + + fn read_config(&self, _offset: u64, _data: &mut [u8]) {} + + fn write_config(&mut self, _offset: u64, _data: &[u8]) {} + } + + #[test] + fn write_base_regs() { + let mut regs = VirtioPciCommonConfig { + access_platform: None, + driver_status: 0xaa, + config_generation: 0x55, + device_feature_select: 0x0, + driver_feature_select: 0x0, + queue_select: 0xff, + msix_config: Arc::new(AtomicU16::new(0)), + msix_queues: Arc::new(Mutex::new(vec![0; 3])), + }; + + let dev = Arc::new(Mutex::new(DummyDevice(0))); + let mut queues = Vec::new(); + + // Can set all bits of driver_status. + regs.write(0x14, &[0x55], &mut queues, dev.clone()); + let mut read_back = vec![0x00]; + regs.read(0x14, &mut read_back, &mut queues, dev.clone()); + assert_eq!(read_back[0], 0x55); + + // The config generation register is read only. + regs.write(0x15, &[0xaa], &mut queues, dev.clone()); + let mut read_back = vec![0x00]; + regs.read(0x15, &mut read_back, &mut queues, dev.clone()); + assert_eq!(read_back[0], 0x55); + + // Device features is read-only and passed through from the device. + regs.write(0x04, &[0, 0, 0, 0], &mut queues, dev.clone()); + let mut read_back = vec![0, 0, 0, 0]; + regs.read(0x04, &mut read_back, &mut queues, dev.clone()); + assert_eq!(LittleEndian::read_u32(&read_back), DUMMY_FEATURES as u32); + + // Feature select registers are read/write. + regs.write(0x00, &[1, 2, 3, 4], &mut queues, dev.clone()); + let mut read_back = vec![0, 0, 0, 0]; + regs.read(0x00, &mut read_back, &mut queues, dev.clone()); + assert_eq!(LittleEndian::read_u32(&read_back), 0x0403_0201); + regs.write(0x08, &[1, 2, 3, 4], &mut queues, dev.clone()); + let mut read_back = vec![0, 0, 0, 0]; + regs.read(0x08, &mut read_back, &mut queues, dev.clone()); + assert_eq!(LittleEndian::read_u32(&read_back), 0x0403_0201); + + // 'queue_select' can be read and written. + regs.write(0x16, &[0xaa, 0x55], &mut queues, dev.clone()); + let mut read_back = vec![0x00, 0x00]; + regs.read(0x16, &mut read_back, &mut queues, dev); + assert_eq!(read_back[0], 0xaa); + assert_eq!(read_back[1], 0x55); + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/transport/pci_device.rs b/vendor/cube-hypervisor/virtio-devices/src/transport/pci_device.rs new file mode 100644 index 0000000..ccb6241 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/transport/pci_device.rs @@ -0,0 +1,1254 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use super::VirtioPciCommonConfig; +use crate::transport::VirtioTransport; +use crate::GuestMemoryMmap; +use crate::{ + ActivateResult, VirtioDevice, VirtioDeviceType, VirtioInterrupt, VirtioInterruptType, + DEVICE_ACKNOWLEDGE, DEVICE_DRIVER, DEVICE_DRIVER_OK, DEVICE_FAILED, DEVICE_FEATURES_OK, + DEVICE_INIT, +}; +use anyhow::anyhow; +use libc::EFD_NONBLOCK; +use pci::{ + BarReprogrammingParams, MsixCap, MsixConfig, PciBarConfiguration, PciBarRegionType, + PciCapability, PciCapabilityId, PciClassCode, PciConfiguration, PciDevice, PciDeviceError, + PciHeaderType, PciMassStorageSubclass, PciNetworkControllerSubclass, PciSubclass, +}; +use serde::{Deserialize, Serialize}; +use std::any::Any; +use std::cmp; +use std::io::Write; +use std::ops::Deref; +use std::result; +use std::sync::atomic::{AtomicBool, AtomicU16, AtomicUsize, Ordering}; +use std::sync::{Arc, Barrier, Mutex}; +use virtio_queue::{Error as QueueError, Queue, QueueT}; +use vm_allocator::{AddressAllocator, SystemAllocator}; +use vm_device::dma_mapping::ExternalDmaMapping; +use vm_device::interrupt::{ + InterruptIndex, InterruptManager, InterruptSourceGroup, MsiIrqGroupConfig, +}; +use vm_device::{BusDevice, PciBarType, Resource}; +use vm_memory::{Address, ByteValued, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, Le32}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vm_virtio::AccessPlatform; +use vmm_sys_util::{errno::Result, eventfd::EventFd}; + +/// Vector value used to disable MSI for a queue. +const VIRTQ_MSI_NO_VECTOR: u16 = 0xffff; + +pub enum Error { + /// Failed to retrieve queue ring's index. + QueueRingIndex(QueueError), + /// Missing virtqueue state in snapshot. + QueueStateMissing(usize), +} + +impl std::fmt::Debug for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::QueueRingIndex(e) => f.debug_tuple("QueueRingIndex").field(e).finish(), + Self::QueueStateMissing(i) => f.debug_tuple("QueueStateMissing").field(i).finish(), + } + } +} + +#[allow(clippy::enum_variant_names)] +enum PciCapabilityType { + CommonConfig = 1, + NotifyConfig = 2, + IsrConfig = 3, + DeviceConfig = 4, + PciConfig = 5, + SharedMemoryConfig = 8, +} + +// This offset represents the 2 bytes omitted from the VirtioPciCap structure +// as they are already handled through add_capability(). These 2 bytes are the +// fields cap_vndr (1 byte) and cap_next (1 byte) defined in the virtio spec. +const VIRTIO_PCI_CAP_OFFSET: usize = 2; + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Clone, Copy, Default)] +struct VirtioPciCap { + cap_len: u8, // Generic PCI field: capability length + cfg_type: u8, // Identifies the structure. + pci_bar: u8, // Where to find it. + id: u8, // Multiple capabilities of the same type + padding: [u8; 2], // Pad to full dword. + offset: Le32, // Offset within bar. + length: Le32, // Length of the structure, in bytes. +} +// SAFETY: All members are simple numbers and any value is valid. +unsafe impl ByteValued for VirtioPciCap {} + +impl PciCapability for VirtioPciCap { + fn bytes(&self) -> &[u8] { + self.as_slice() + } + + fn id(&self) -> PciCapabilityId { + PciCapabilityId::VendorSpecific + } +} + +const VIRTIO_PCI_CAP_LEN_OFFSET: u8 = 2; + +impl VirtioPciCap { + pub fn new(cfg_type: PciCapabilityType, pci_bar: u8, offset: u32, length: u32) -> Self { + VirtioPciCap { + cap_len: (std::mem::size_of::() as u8) + VIRTIO_PCI_CAP_LEN_OFFSET, + cfg_type: cfg_type as u8, + pci_bar, + id: 0, + padding: [0; 2], + offset: Le32::from(offset), + length: Le32::from(length), + } + } +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Clone, Copy, Default)] +struct VirtioPciNotifyCap { + cap: VirtioPciCap, + notify_off_multiplier: Le32, +} +// SAFETY: All members are simple numbers and any value is valid. +unsafe impl ByteValued for VirtioPciNotifyCap {} + +impl PciCapability for VirtioPciNotifyCap { + fn bytes(&self) -> &[u8] { + self.as_slice() + } + + fn id(&self) -> PciCapabilityId { + PciCapabilityId::VendorSpecific + } +} + +impl VirtioPciNotifyCap { + pub fn new( + cfg_type: PciCapabilityType, + pci_bar: u8, + offset: u32, + length: u32, + multiplier: Le32, + ) -> Self { + VirtioPciNotifyCap { + cap: VirtioPciCap { + cap_len: (std::mem::size_of::() as u8) + + VIRTIO_PCI_CAP_LEN_OFFSET, + cfg_type: cfg_type as u8, + pci_bar, + id: 0, + padding: [0; 2], + offset: Le32::from(offset), + length: Le32::from(length), + }, + notify_off_multiplier: multiplier, + } + } +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Clone, Copy, Default)] +struct VirtioPciCap64 { + cap: VirtioPciCap, + offset_hi: Le32, + length_hi: Le32, +} +// SAFETY: All members are simple numbers and any value is valid. +unsafe impl ByteValued for VirtioPciCap64 {} + +impl PciCapability for VirtioPciCap64 { + fn bytes(&self) -> &[u8] { + self.as_slice() + } + + fn id(&self) -> PciCapabilityId { + PciCapabilityId::VendorSpecific + } +} + +impl VirtioPciCap64 { + pub fn new(cfg_type: PciCapabilityType, pci_bar: u8, id: u8, offset: u64, length: u64) -> Self { + VirtioPciCap64 { + cap: VirtioPciCap { + cap_len: (std::mem::size_of::() as u8) + VIRTIO_PCI_CAP_LEN_OFFSET, + cfg_type: cfg_type as u8, + pci_bar, + id, + padding: [0; 2], + offset: Le32::from(offset as u32), + length: Le32::from(length as u32), + }, + offset_hi: Le32::from((offset >> 32) as u32), + length_hi: Le32::from((length >> 32) as u32), + } + } +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Clone, Copy, Default)] +struct VirtioPciCfgCap { + cap: VirtioPciCap, + pci_cfg_data: [u8; 4], +} +// SAFETY: All members are simple numbers and any value is valid. +unsafe impl ByteValued for VirtioPciCfgCap {} + +impl PciCapability for VirtioPciCfgCap { + fn bytes(&self) -> &[u8] { + self.as_slice() + } + + fn id(&self) -> PciCapabilityId { + PciCapabilityId::VendorSpecific + } +} + +impl VirtioPciCfgCap { + fn new() -> Self { + VirtioPciCfgCap { + cap: VirtioPciCap::new(PciCapabilityType::PciConfig, 0, 0, 0), + ..Default::default() + } + } +} + +#[derive(Clone, Copy, Default)] +struct VirtioPciCfgCapInfo { + offset: usize, + cap: VirtioPciCfgCap, +} + +#[allow(dead_code)] +#[derive(Copy, Clone)] +pub enum PciVirtioSubclass { + NonTransitionalBase = 0xff, +} + +impl PciSubclass for PciVirtioSubclass { + fn get_register_value(&self) -> u8 { + *self as u8 + } +} + +// Allocate one bar for the structs pointed to by the capability structures. +// As per the PCI specification, because the same BAR shares MSI-X and non +// MSI-X structures, it is recommended to use 8KiB alignment for all those +// structures. +const COMMON_CONFIG_BAR_OFFSET: u64 = 0x0000; +const COMMON_CONFIG_SIZE: u64 = 56; +const ISR_CONFIG_BAR_OFFSET: u64 = 0x2000; +const ISR_CONFIG_SIZE: u64 = 1; +const DEVICE_CONFIG_BAR_OFFSET: u64 = 0x4000; +const DEVICE_CONFIG_SIZE: u64 = 0x1000; +const NOTIFICATION_BAR_OFFSET: u64 = 0x6000; +const NOTIFICATION_SIZE: u64 = 0x1000; +const MSIX_TABLE_BAR_OFFSET: u64 = 0x8000; +// The size is 256KiB because the table can hold up to 2048 entries, with each +// entry being 128 bits (4 DWORDS). +const MSIX_TABLE_SIZE: u64 = 0x40000; +const MSIX_PBA_BAR_OFFSET: u64 = 0x48000; +// The size is 2KiB because the Pending Bit Array has one bit per vector and it +// can support up to 2048 vectors. +const MSIX_PBA_SIZE: u64 = 0x800; +// The BAR size must be a power of 2. +const CAPABILITY_BAR_SIZE: u64 = 0x80000; +const VIRTIO_COMMON_BAR_INDEX: usize = 0; +const VIRTIO_SHM_BAR_INDEX: usize = 2; + +const NOTIFY_OFF_MULTIPLIER: u32 = 4; // A dword per notification address. + +const VIRTIO_PCI_VENDOR_ID: u16 = 0x1af4; +const VIRTIO_PCI_DEVICE_ID_BASE: u16 = 0x1040; // Add to device type to get device ID. + +#[derive(Serialize, Deserialize)] +struct QueueState { + max_size: u16, + size: u16, + ready: bool, + desc_table: u64, + avail_ring: u64, + used_ring: u64, +} + +#[derive(Serialize, Deserialize)] +struct VirtioPciDeviceState { + device_activated: bool, + queues: Vec, + interrupt_status: usize, +} + +pub struct VirtioPciDeviceActivator { + interrupt: Option>, + memory: Option>, + device: Arc>, + device_activated: Arc, + queues: Option>, + barrier: Option>, + id: String, +} + +impl VirtioPciDeviceActivator { + pub fn activate(&mut self) -> ActivateResult { + self.device.lock().unwrap().activate( + self.memory.take().unwrap(), + self.interrupt.take().unwrap(), + self.queues.take().unwrap(), + )?; + self.device_activated.store(true, Ordering::SeqCst); + + if let Some(barrier) = self.barrier.take() { + info!("{}: Waiting for barrier", self.id); + barrier.wait(); + info!("{}: Barrier released", self.id); + } + + Ok(()) + } +} + +pub struct VirtioPciDevice { + id: String, + + // PCI configuration registers. + configuration: PciConfiguration, + + // virtio PCI common configuration + common_config: VirtioPciCommonConfig, + + // MSI-X config + msix_config: Option>>, + + // Number of MSI-X vectors + msix_num: u16, + + // Virtio device reference and status + device: Arc>, + device_activated: Arc, + + // PCI interrupts. + interrupt_status: Arc, + virtio_interrupt: Option>, + interrupt_source_group: Arc, + + // virtio queues + queues: Vec, + queue_evts: Vec, + + // Guest memory + memory: GuestMemoryAtomic, + + // Settings PCI BAR + settings_bar: u8, + + // Whether to use 64-bit bar location or 32-bit + use_64bit_bar: bool, + + // Add a dedicated structure to hold information about the very specific + // virtio-pci capability VIRTIO_PCI_CAP_PCI_CFG. This is needed to support + // the legacy/backward compatible mechanism of letting the guest access the + // other virtio capabilities without mapping the PCI BARs. This can be + // needed when the guest tries to early access the virtio configuration of + // a device. + cap_pci_cfg_info: VirtioPciCfgCapInfo, + + // Details of bar regions to free + bar_regions: Vec, + + // EventFd to signal on to request activation + activate_evt: EventFd, + + // Optional DMA handler + dma_handler: Option>, + + // Pending activations + pending_activations: Arc>>, +} + +impl VirtioPciDevice { + /// Constructs a new PCI transport for the given virtio device. + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + memory: GuestMemoryAtomic, + device: Arc>, + msix_num: u16, + access_platform: Option>, + interrupt_manager: &Arc>, + pci_device_bdf: u32, + activate_evt: EventFd, + use_64bit_bar: bool, + dma_handler: Option>, + pending_activations: Arc>>, + ) -> Result { + let device_clone = device.clone(); + let mut locked_device = device_clone.lock().unwrap(); + let mut queue_evts = Vec::new(); + for _ in locked_device.queue_max_sizes().iter() { + queue_evts.push(EventFd::new(EFD_NONBLOCK)?) + } + let num_queues = locked_device.queue_max_sizes().len(); + + if let Some(access_platform) = &access_platform { + locked_device.set_access_platform(access_platform.clone()); + } + + let queues = locked_device + .queue_max_sizes() + .iter() + .map(|&s| Queue::new(s).unwrap()) + .collect(); + + let pci_device_id = VIRTIO_PCI_DEVICE_ID_BASE + locked_device.device_type() as u16; + + let interrupt_source_group = interrupt_manager.create_group(MsiIrqGroupConfig { + base: 0, + count: msix_num as InterruptIndex, + })?; + + let (msix_config, msix_config_clone) = if msix_num > 0 { + let msix_config = Arc::new(Mutex::new(MsixConfig::new( + msix_num, + interrupt_source_group.clone(), + pci_device_bdf, + ))); + let msix_config_clone = msix_config.clone(); + (Some(msix_config), Some(msix_config_clone)) + } else { + (None, None) + }; + + let (class, subclass) = match VirtioDeviceType::from(locked_device.device_type()) { + VirtioDeviceType::Net => ( + PciClassCode::NetworkController, + &PciNetworkControllerSubclass::EthernetController as &dyn PciSubclass, + ), + VirtioDeviceType::Block => ( + PciClassCode::MassStorage, + &PciMassStorageSubclass::MassStorage as &dyn PciSubclass, + ), + _ => ( + PciClassCode::Other, + &PciVirtioSubclass::NonTransitionalBase as &dyn PciSubclass, + ), + }; + + let configuration = PciConfiguration::new( + VIRTIO_PCI_VENDOR_ID, + pci_device_id, + 0x1, // For modern virtio-PCI devices + class, + subclass, + None, + PciHeaderType::Device, + VIRTIO_PCI_VENDOR_ID, + pci_device_id, + msix_config_clone, + ); + + let mut virtio_pci_device = VirtioPciDevice { + id, + configuration, + common_config: VirtioPciCommonConfig { + access_platform, + driver_status: 0, + config_generation: 0, + device_feature_select: 0, + driver_feature_select: 0, + queue_select: 0, + msix_config: Arc::new(AtomicU16::new(VIRTQ_MSI_NO_VECTOR)), + msix_queues: Arc::new(Mutex::new(vec![VIRTQ_MSI_NO_VECTOR; num_queues])), + }, + msix_config, + msix_num, + device, + device_activated: Arc::new(AtomicBool::new(false)), + interrupt_status: Arc::new(AtomicUsize::new(0)), + virtio_interrupt: None, + queues, + queue_evts, + memory, + settings_bar: 0, + use_64bit_bar, + interrupt_source_group, + cap_pci_cfg_info: VirtioPciCfgCapInfo::default(), + bar_regions: vec![], + activate_evt, + dma_handler, + pending_activations, + }; + + if let Some(msix_config) = &virtio_pci_device.msix_config { + virtio_pci_device.virtio_interrupt = Some(Arc::new(VirtioInterruptMsix::new( + msix_config.clone(), + virtio_pci_device.common_config.msix_config.clone(), + virtio_pci_device.common_config.msix_queues.clone(), + virtio_pci_device.interrupt_source_group.clone(), + ))); + } + + Ok(virtio_pci_device) + } + + fn state(&self) -> VirtioPciDeviceState { + VirtioPciDeviceState { + device_activated: self.device_activated.load(Ordering::Acquire), + interrupt_status: self.interrupt_status.load(Ordering::Acquire), + queues: self + .queues + .iter() + .map(|q| QueueState { + max_size: q.max_size(), + size: q.size(), + ready: q.ready(), + desc_table: q.desc_table(), + avail_ring: q.avail_ring(), + used_ring: q.used_ring(), + }) + .collect(), + } + } + + fn set_state(&mut self, state: &VirtioPciDeviceState) -> std::result::Result<(), Error> { + self.device_activated + .store(state.device_activated, Ordering::Release); + self.interrupt_status + .store(state.interrupt_status, Ordering::Release); + + // Update virtqueues indexes for both available and used rings. + for (i, queue) in self.queues.iter_mut().enumerate() { + let queue_state = state.queues.get(i).ok_or(Error::QueueStateMissing(i))?; + + queue.set_size(queue_state.size); + queue.set_ready(queue_state.ready); + + if !queue_state.ready { + continue; + } + + queue + .try_set_desc_table_address(GuestAddress(queue_state.desc_table)) + .unwrap(); + queue + .try_set_avail_ring_address(GuestAddress(queue_state.avail_ring)) + .unwrap(); + queue + .try_set_used_ring_address(GuestAddress(queue_state.used_ring)) + .unwrap(); + queue.set_next_avail( + queue + .used_idx(self.memory.memory().deref(), Ordering::Acquire) + .map_err(Error::QueueRingIndex)? + .0, + ); + queue.set_next_used( + queue + .used_idx(self.memory.memory().deref(), Ordering::Acquire) + .map_err(Error::QueueRingIndex)? + .0, + ); + } + + Ok(()) + } + + /// Gets the list of queue events that must be triggered whenever the VM writes to + /// `virtio::NOTIFY_REG_OFFSET` past the MMIO base. Each event must be triggered when the + /// value being written equals the index of the event in this list. + fn queue_evts(&self) -> &[EventFd] { + self.queue_evts.as_slice() + } + + fn is_driver_ready(&self) -> bool { + let ready_bits = + (DEVICE_ACKNOWLEDGE | DEVICE_DRIVER | DEVICE_DRIVER_OK | DEVICE_FEATURES_OK) as u8; + self.common_config.driver_status == ready_bits + && self.common_config.driver_status & DEVICE_FAILED as u8 == 0 + } + + /// Determines if the driver has requested the device (re)init / reset itself + fn is_driver_init(&self) -> bool { + self.common_config.driver_status == DEVICE_INIT as u8 + } + + pub fn config_bar_addr(&self) -> u64 { + self.configuration.get_bar_addr(self.settings_bar as usize) + } + + fn add_pci_capabilities( + &mut self, + settings_bar: u8, + ) -> std::result::Result<(), PciDeviceError> { + // Add pointers to the different configuration structures from the PCI capabilities. + let common_cap = VirtioPciCap::new( + PciCapabilityType::CommonConfig, + settings_bar, + COMMON_CONFIG_BAR_OFFSET as u32, + COMMON_CONFIG_SIZE as u32, + ); + self.configuration + .add_capability(&common_cap) + .map_err(PciDeviceError::CapabilitiesSetup)?; + + let isr_cap = VirtioPciCap::new( + PciCapabilityType::IsrConfig, + settings_bar, + ISR_CONFIG_BAR_OFFSET as u32, + ISR_CONFIG_SIZE as u32, + ); + self.configuration + .add_capability(&isr_cap) + .map_err(PciDeviceError::CapabilitiesSetup)?; + + // TODO(dgreid) - set based on device's configuration size? + let device_cap = VirtioPciCap::new( + PciCapabilityType::DeviceConfig, + settings_bar, + DEVICE_CONFIG_BAR_OFFSET as u32, + DEVICE_CONFIG_SIZE as u32, + ); + self.configuration + .add_capability(&device_cap) + .map_err(PciDeviceError::CapabilitiesSetup)?; + + let notify_cap = VirtioPciNotifyCap::new( + PciCapabilityType::NotifyConfig, + settings_bar, + NOTIFICATION_BAR_OFFSET as u32, + NOTIFICATION_SIZE as u32, + Le32::from(NOTIFY_OFF_MULTIPLIER), + ); + self.configuration + .add_capability(¬ify_cap) + .map_err(PciDeviceError::CapabilitiesSetup)?; + + let configuration_cap = VirtioPciCfgCap::new(); + self.cap_pci_cfg_info.offset = self + .configuration + .add_capability(&configuration_cap) + .map_err(PciDeviceError::CapabilitiesSetup)? + + VIRTIO_PCI_CAP_OFFSET; + self.cap_pci_cfg_info.cap = configuration_cap; + + if self.msix_config.is_some() { + let msix_cap = MsixCap::new( + settings_bar, + self.msix_num, + MSIX_TABLE_BAR_OFFSET as u32, + settings_bar, + MSIX_PBA_BAR_OFFSET as u32, + ); + self.configuration + .add_capability(&msix_cap) + .map_err(PciDeviceError::CapabilitiesSetup)?; + } + + self.settings_bar = settings_bar; + Ok(()) + } + + fn read_cap_pci_cfg(&mut self, offset: usize, mut data: &mut [u8]) { + let cap_slice = self.cap_pci_cfg_info.cap.as_slice(); + let data_len = data.len(); + let cap_len = cap_slice.len(); + if offset + data_len > cap_len { + error!("Failed to read cap_pci_cfg from config space"); + return; + } + + if offset < std::mem::size_of::() { + if let Some(end) = offset.checked_add(data_len) { + // This write can't fail, offset and end are checked against config_len. + data.write_all(&cap_slice[offset..cmp::min(end, cap_len)]) + .unwrap(); + } + } else { + // Safe since we know self.cap_pci_cfg_info.cap.cap.offset is 32bits long. + let bar_offset: u32 = + unsafe { std::mem::transmute(self.cap_pci_cfg_info.cap.cap.offset) }; + self.read_bar(0, bar_offset as u64, data) + } + } + + fn write_cap_pci_cfg(&mut self, offset: usize, data: &[u8]) -> Option> { + let cap_slice = self.cap_pci_cfg_info.cap.as_mut_slice(); + let data_len = data.len(); + let cap_len = cap_slice.len(); + if offset + data_len > cap_len { + error!("Failed to write cap_pci_cfg to config space"); + return None; + } + + if offset < std::mem::size_of::() { + let (_, right) = cap_slice.split_at_mut(offset); + right[..data_len].copy_from_slice(data); + None + } else { + // Safe since we know self.cap_pci_cfg_info.cap.cap.offset is 32bits long. + let bar_offset: u32 = + unsafe { std::mem::transmute(self.cap_pci_cfg_info.cap.cap.offset) }; + self.write_bar(0, bar_offset as u64, data) + } + } + + pub fn virtio_device(&self) -> Arc> { + self.device.clone() + } + + fn prepare_activator(&mut self, barrier: Option>) -> VirtioPciDeviceActivator { + let mut queues = Vec::new(); + + for (queue_index, queue) in self.queues.iter().enumerate() { + if !queue.ready() { + continue; + } + + if !queue.is_valid(self.memory.memory().deref()) { + error!("Queue {} is not valid", queue_index); + } + + queues.push(( + queue_index, + vm_virtio::clone_queue(queue), + self.queue_evts[queue_index].try_clone().unwrap(), + )); + } + + VirtioPciDeviceActivator { + interrupt: self.virtio_interrupt.take(), + memory: Some(self.memory.clone()), + device: self.device.clone(), + queues: Some(queues), + device_activated: self.device_activated.clone(), + barrier, + id: self.id.clone(), + } + } + + fn activate(&mut self) -> ActivateResult { + self.prepare_activator(None).activate() + } + + fn needs_activation(&self) -> bool { + !self.device_activated.load(Ordering::SeqCst) && self.is_driver_ready() + } + + pub fn dma_handler(&self) -> Option<&Arc> { + self.dma_handler.as_ref() + } +} + +impl VirtioTransport for VirtioPciDevice { + fn ioeventfds(&self, base_addr: u64) -> Vec<(&EventFd, u64)> { + let notify_base = base_addr + NOTIFICATION_BAR_OFFSET; + self.queue_evts() + .iter() + .enumerate() + .map(|(i, event)| { + ( + event, + notify_base + i as u64 * u64::from(NOTIFY_OFF_MULTIPLIER), + ) + }) + .collect() + } +} + +pub struct VirtioInterruptMsix { + msix_config: Arc>, + config_vector: Arc, + queues_vectors: Arc>>, + interrupt_source_group: Arc, +} + +impl VirtioInterruptMsix { + pub fn new( + msix_config: Arc>, + config_vector: Arc, + queues_vectors: Arc>>, + interrupt_source_group: Arc, + ) -> Self { + VirtioInterruptMsix { + msix_config, + config_vector, + queues_vectors, + interrupt_source_group, + } + } +} + +impl VirtioInterrupt for VirtioInterruptMsix { + fn trigger(&self, int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { + let vector = match int_type { + VirtioInterruptType::Config => self.config_vector.load(Ordering::Acquire), + VirtioInterruptType::Queue(queue_index) => { + self.queues_vectors.lock().unwrap()[queue_index as usize] + } + }; + + if vector == VIRTQ_MSI_NO_VECTOR { + return Ok(()); + } + + let config = &mut self.msix_config.lock().unwrap(); + let entry = &config.table_entries[vector as usize]; + // In case the vector control register associated with the entry + // has its first bit set, this means the vector is masked and the + // device should not inject the interrupt. + // Instead, the Pending Bit Array table is updated to reflect there + // is a pending interrupt for this specific vector. + if config.masked() || entry.masked() { + config.set_pba_bit(vector, false); + return Ok(()); + } + + self.interrupt_source_group + .trigger(vector as InterruptIndex) + } + + fn notifier(&self, int_type: VirtioInterruptType) -> Option { + let vector = match int_type { + VirtioInterruptType::Config => self.config_vector.load(Ordering::Acquire), + VirtioInterruptType::Queue(queue_index) => { + self.queues_vectors.lock().unwrap()[queue_index as usize] + } + }; + + self.interrupt_source_group + .notifier(vector as InterruptIndex) + } +} + +impl PciDevice for VirtioPciDevice { + fn write_config_register( + &mut self, + reg_idx: usize, + offset: u64, + data: &[u8], + ) -> Option> { + // Handle the special case where the capability VIRTIO_PCI_CAP_PCI_CFG + // is accessed. This capability has a special meaning as it allows the + // guest to access other capabilities without mapping the PCI BAR. + let base = reg_idx * 4; + if base + offset as usize >= self.cap_pci_cfg_info.offset + && base + offset as usize + data.len() + <= self.cap_pci_cfg_info.offset + self.cap_pci_cfg_info.cap.bytes().len() + { + let offset = base + offset as usize - self.cap_pci_cfg_info.offset; + self.write_cap_pci_cfg(offset, data) + } else { + self.configuration + .write_config_register(reg_idx, offset, data); + None + } + } + + fn read_config_register(&mut self, reg_idx: usize) -> u32 { + // Handle the special case where the capability VIRTIO_PCI_CAP_PCI_CFG + // is accessed. This capability has a special meaning as it allows the + // guest to access other capabilities without mapping the PCI BAR. + let base = reg_idx * 4; + if base >= self.cap_pci_cfg_info.offset + && base + 4 <= self.cap_pci_cfg_info.offset + self.cap_pci_cfg_info.cap.bytes().len() + { + let offset = base - self.cap_pci_cfg_info.offset; + let mut data = [0u8; 4]; + self.read_cap_pci_cfg(offset, &mut data); + u32::from_le_bytes(data) + } else { + self.configuration.read_reg(reg_idx) + } + } + + fn detect_bar_reprogramming( + &mut self, + reg_idx: usize, + data: &[u8], + ) -> Option { + self.configuration.detect_bar_reprogramming(reg_idx, data) + } + + fn allocate_bars( + &mut self, + allocator: &Arc>, + mmio_allocator: &mut AddressAllocator, + resources: Option>, + ) -> std::result::Result, PciDeviceError> { + let mut bars = Vec::new(); + let device_clone = self.device.clone(); + let device = device_clone.lock().unwrap(); + + let mut settings_bar_addr = None; + let mut use_64bit_bar = self.use_64bit_bar; + if let Some(resources) = resources { + for resource in resources { + if let Resource::PciBar { + index, base, type_, .. + } = resource + { + if index == VIRTIO_COMMON_BAR_INDEX { + settings_bar_addr = Some(GuestAddress(base)); + use_64bit_bar = match type_ { + PciBarType::Io => { + return Err(PciDeviceError::InvalidResource(resource)) + } + PciBarType::Mmio32 => false, + PciBarType::Mmio64 => true, + }; + break; + } + } + } + // Error out if no resource was matching the BAR id. + if settings_bar_addr.is_none() { + return Err(PciDeviceError::MissingResource); + } + } + + // Allocate the virtio-pci capability BAR. + // See http://docs.oasis-open.org/virtio/virtio/v1.0/cs04/virtio-v1.0-cs04.html#x1-740004 + let (virtio_pci_bar_addr, region_type) = if use_64bit_bar { + let region_type = PciBarRegionType::Memory64BitRegion; + let addr = mmio_allocator + .allocate( + settings_bar_addr, + CAPABILITY_BAR_SIZE, + Some(CAPABILITY_BAR_SIZE), + ) + .ok_or(PciDeviceError::IoAllocationFailed(CAPABILITY_BAR_SIZE))?; + (addr, region_type) + } else { + let region_type = PciBarRegionType::Memory32BitRegion; + let addr = allocator + .lock() + .unwrap() + .allocate_mmio_hole_addresses( + settings_bar_addr, + CAPABILITY_BAR_SIZE, + Some(CAPABILITY_BAR_SIZE), + ) + .ok_or(PciDeviceError::IoAllocationFailed(CAPABILITY_BAR_SIZE))?; + (addr, region_type) + }; + + let bar = PciBarConfiguration::default() + .set_index(VIRTIO_COMMON_BAR_INDEX) + .set_address(virtio_pci_bar_addr.raw_value()) + .set_size(CAPABILITY_BAR_SIZE) + .set_region_type(region_type); + self.configuration.add_pci_bar(&bar).map_err(|e| { + PciDeviceError::IoRegistrationFailed(virtio_pci_bar_addr.raw_value(), e) + })?; + + bars.push(bar); + + // Once the BARs are allocated, the capabilities can be added to the PCI configuration. + self.add_pci_capabilities(VIRTIO_COMMON_BAR_INDEX as u8)?; + + // Allocate a dedicated BAR if there are some shared memory regions. + if let Some(shm_list) = device.get_shm_regions() { + let bar = PciBarConfiguration::default() + .set_index(VIRTIO_SHM_BAR_INDEX) + .set_address(shm_list.addr.raw_value()) + .set_size(shm_list.len); + self.configuration + .add_pci_bar(&bar) + .map_err(|e| PciDeviceError::IoRegistrationFailed(shm_list.addr.raw_value(), e))?; + + bars.push(bar); + + for (idx, shm) in shm_list.region_list.iter().enumerate() { + let shm_cap = VirtioPciCap64::new( + PciCapabilityType::SharedMemoryConfig, + VIRTIO_SHM_BAR_INDEX as u8, + idx as u8, + shm.offset, + shm.len, + ); + self.configuration + .add_capability(&shm_cap) + .map_err(PciDeviceError::CapabilitiesSetup)?; + } + } + + self.bar_regions = bars.clone(); + + Ok(bars) + } + + fn free_bars( + &mut self, + allocator: &mut SystemAllocator, + mmio_allocator: &mut AddressAllocator, + ) -> std::result::Result<(), PciDeviceError> { + for bar in self.bar_regions.drain(..) { + match bar.region_type() { + PciBarRegionType::Memory32BitRegion => { + allocator.free_mmio_hole_addresses(GuestAddress(bar.addr()), bar.size()); + } + PciBarRegionType::Memory64BitRegion => { + mmio_allocator.free(GuestAddress(bar.addr()), bar.size()); + } + _ => error!("Unexpected PCI bar type"), + } + } + Ok(()) + } + + fn move_bar(&mut self, old_base: u64, new_base: u64) -> result::Result<(), std::io::Error> { + // We only update our idea of the bar in order to support free_bars() above. + // The majority of the reallocation is done inside DeviceManager. + for bar in self.bar_regions.iter_mut() { + if bar.addr() == old_base { + *bar = bar.set_address(new_base); + } + } + + Ok(()) + } + + fn read_bar(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + match offset { + o if o < COMMON_CONFIG_BAR_OFFSET + COMMON_CONFIG_SIZE => self.common_config.read( + o - COMMON_CONFIG_BAR_OFFSET, + data, + &mut self.queues, + self.device.clone(), + ), + o if (ISR_CONFIG_BAR_OFFSET..ISR_CONFIG_BAR_OFFSET + ISR_CONFIG_SIZE).contains(&o) => { + if let Some(v) = data.get_mut(0) { + // Reading this register resets it to 0. + *v = self.interrupt_status.swap(0, Ordering::AcqRel) as u8; + } + } + o if (DEVICE_CONFIG_BAR_OFFSET..DEVICE_CONFIG_BAR_OFFSET + DEVICE_CONFIG_SIZE) + .contains(&o) => + { + let device = self.device.lock().unwrap(); + device.read_config(o - DEVICE_CONFIG_BAR_OFFSET, data); + } + o if (NOTIFICATION_BAR_OFFSET..NOTIFICATION_BAR_OFFSET + NOTIFICATION_SIZE) + .contains(&o) => + { + // Handled with ioeventfds. + } + o if (MSIX_TABLE_BAR_OFFSET..MSIX_TABLE_BAR_OFFSET + MSIX_TABLE_SIZE).contains(&o) => { + if let Some(msix_config) = &self.msix_config { + msix_config + .lock() + .unwrap() + .read_table(o - MSIX_TABLE_BAR_OFFSET, data); + } + } + o if (MSIX_PBA_BAR_OFFSET..MSIX_PBA_BAR_OFFSET + MSIX_PBA_SIZE).contains(&o) => { + if let Some(msix_config) = &self.msix_config { + msix_config + .lock() + .unwrap() + .read_pba(o - MSIX_PBA_BAR_OFFSET, data); + } + } + _ => (), + } + } + + fn write_bar(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + match offset { + o if o < COMMON_CONFIG_BAR_OFFSET + COMMON_CONFIG_SIZE => self.common_config.write( + o - COMMON_CONFIG_BAR_OFFSET, + data, + &mut self.queues, + self.device.clone(), + ), + o if (ISR_CONFIG_BAR_OFFSET..ISR_CONFIG_BAR_OFFSET + ISR_CONFIG_SIZE).contains(&o) => { + if let Some(v) = data.first() { + self.interrupt_status + .fetch_and(!(*v as usize), Ordering::AcqRel); + } + } + o if (DEVICE_CONFIG_BAR_OFFSET..DEVICE_CONFIG_BAR_OFFSET + DEVICE_CONFIG_SIZE) + .contains(&o) => + { + let mut device = self.device.lock().unwrap(); + device.write_config(o - DEVICE_CONFIG_BAR_OFFSET, data); + } + o if (NOTIFICATION_BAR_OFFSET..NOTIFICATION_BAR_OFFSET + NOTIFICATION_SIZE) + .contains(&o) => + { + // Handled with ioeventfds. + error!("Unexpected write to notification BAR: offset = 0x{:x}", o); + } + o if (MSIX_TABLE_BAR_OFFSET..MSIX_TABLE_BAR_OFFSET + MSIX_TABLE_SIZE).contains(&o) => { + if let Some(msix_config) = &self.msix_config { + msix_config + .lock() + .unwrap() + .write_table(o - MSIX_TABLE_BAR_OFFSET, data); + } + } + o if (MSIX_PBA_BAR_OFFSET..MSIX_PBA_BAR_OFFSET + MSIX_PBA_SIZE).contains(&o) => { + if let Some(msix_config) = &self.msix_config { + msix_config + .lock() + .unwrap() + .write_pba(o - MSIX_PBA_BAR_OFFSET, data); + } + } + _ => (), + }; + + // Try and activate the device if the driver status has changed + if self.needs_activation() { + let barrier = Arc::new(Barrier::new(2)); + let activator = self.prepare_activator(Some(barrier.clone())); + self.pending_activations.lock().unwrap().push(activator); + info!( + "{}: Needs activation; writing to activate event fd", + self.id + ); + self.activate_evt.write(1).ok(); + info!("{}: Needs activation; returning barrier", self.id); + return Some(barrier); + } + + // Device has been reset by the driver + if self.device_activated.load(Ordering::SeqCst) && self.is_driver_init() { + let mut device = self.device.lock().unwrap(); + if let Some(virtio_interrupt) = device.reset() { + // Upon reset the device returns its interrupt EventFD + self.virtio_interrupt = Some(virtio_interrupt); + self.device_activated.store(false, Ordering::SeqCst); + + // Reset queue readiness (changes queue_enable), queue sizes + // and selected_queue as per spec for reset + self.queues.iter_mut().for_each(Queue::reset); + self.common_config.queue_select = 0; + } else { + error!("Attempt to reset device when not implemented in underlying device"); + self.common_config.driver_status = crate::DEVICE_FAILED as u8; + } + } + + None + } + + fn as_any(&mut self) -> &mut dyn Any { + self + } + + fn id(&self) -> Option { + Some(self.id.clone()) + } +} + +impl BusDevice for VirtioPciDevice { + fn read(&mut self, base: u64, offset: u64, data: &mut [u8]) { + self.read_bar(base, offset, data) + } + + fn write(&mut self, base: u64, offset: u64, data: &[u8]) -> Option> { + self.write_bar(base, offset, data) + } +} + +impl Pausable for VirtioPciDevice { + fn pause(&mut self) -> result::Result<(), MigratableError> { + Ok(()) + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + Ok(()) + } +} + +impl Snapshottable for VirtioPciDevice { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + let mut virtio_pci_dev_snapshot = Snapshot::new_from_state(&self.id, &self.state())?; + + // Snapshot PciConfiguration + virtio_pci_dev_snapshot.add_snapshot(self.configuration.snapshot()?); + + // Snapshot VirtioPciCommonConfig + virtio_pci_dev_snapshot.add_snapshot(self.common_config.snapshot()?); + + // Snapshot MSI-X + if let Some(msix_config) = &self.msix_config { + virtio_pci_dev_snapshot.add_snapshot(msix_config.lock().unwrap().snapshot()?); + } + + Ok(virtio_pci_dev_snapshot) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + if let Some(virtio_pci_dev_section) = + snapshot.snapshot_data.get(&format!("{}-section", self.id)) + { + // Restore MSI-X + if let Some(msix_config) = &self.msix_config { + let id = msix_config.lock().unwrap().id(); + if let Some(msix_snapshot) = snapshot.snapshots.get(&id) { + msix_config + .lock() + .unwrap() + .restore(*msix_snapshot.clone())?; + } + } + + // Restore VirtioPciCommonConfig + if let Some(virtio_config_snapshot) = snapshot.snapshots.get(&self.common_config.id()) { + self.common_config + .restore(*virtio_config_snapshot.clone())?; + } + + // Restore PciConfiguration + if let Some(pci_config_snapshot) = snapshot.snapshots.get(&self.configuration.id()) { + self.configuration.restore(*pci_config_snapshot.clone())?; + } + + // First restore the status of the virtqueues. + self.set_state(&virtio_pci_dev_section.to_state()?) + .map_err(|e| { + MigratableError::Restore(anyhow!( + "Could not restore VIRTIO_PCI_DEVICE state {:?}", + e + )) + })?; + + // Then we can activate the device, as we know at this point that + // the virtqueues are in the right state and the device is ready + // to be activated, which will spawn each virtio worker thread. + if self.device_activated.load(Ordering::SeqCst) && self.is_driver_ready() { + self.activate().map_err(|e| { + MigratableError::Restore(anyhow!("Failed activating the device: {:?}", e)) + })?; + } + + return Ok(()); + } + + Err(MigratableError::Restore(anyhow!( + "Could not find VIRTIO_PCI_DEVICE snapshot section" + ))) + } +} +impl Transportable for VirtioPciDevice {} +impl Migratable for VirtioPciDevice {} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vdpa.rs b/vendor/cube-hypervisor/virtio-devices/src/vdpa.rs new file mode 100644 index 0000000..f14fce9 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vdpa.rs @@ -0,0 +1,586 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::{ + ActivateError, ActivateResult, GuestMemoryMmap, VirtioCommon, VirtioDevice, VirtioInterrupt, + VirtioInterruptType, DEVICE_ACKNOWLEDGE, DEVICE_DRIVER, DEVICE_DRIVER_OK, DEVICE_FEATURES_OK, + VIRTIO_F_IOMMU_PLATFORM, +}; +use anyhow::anyhow; +use serde::{Deserialize, Serialize}; +use std::{ + collections::BTreeMap, + io, result, + sync::{atomic::Ordering, Arc, Mutex}, +}; +use thiserror::Error; +use vhost::{ + vdpa::{VhostVdpa, VhostVdpaIovaRange}, + vhost_kern::VhostKernFeatures, + vhost_kern::{vdpa::VhostKernVdpa, vhost_binding::VHOST_BACKEND_F_SUSPEND}, + VhostBackend, VringConfigData, +}; +use virtio_queue::{Descriptor, Queue, QueueT}; +use vm_device::dma_mapping::ExternalDmaMapping; +use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vm_virtio::{AccessPlatform, Translatable}; +use vmm_sys_util::eventfd::EventFd; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Failed to create vhost-vdpa: {0}")] + CreateVhostVdpa(vhost::Error), + #[error("Failed to map DMA range: {0}")] + DmaMap(vhost::Error), + #[error("Failed to unmap DMA range: {0}")] + DmaUnmap(vhost::Error), + #[error("Failed to get address range")] + GetAddressRange, + #[error("Failed to get the available index from the virtio queue: {0}")] + GetAvailableIndex(virtio_queue::Error), + #[error("Get virtio configuration size: {0}")] + GetConfigSize(vhost::Error), + #[error("Get virtio device identifier: {0}")] + GetDeviceId(vhost::Error), + #[error("Failed to get backend specific features: {0}")] + GetBackendFeatures(vhost::Error), + #[error("Failed to get virtio features: {0}")] + GetFeatures(vhost::Error), + #[error("Failed to get the IOVA range: {0}")] + GetIovaRange(vhost::Error), + #[error("Failed to get queue size: {0}")] + GetVringNum(vhost::Error), + #[error("Invalid IOVA range: {0}-{1}")] + InvalidIovaRange(u64, u64), + #[error("Missing VIRTIO_F_ACCESS_PLATFORM feature")] + MissingAccessPlatformVirtioFeature, + #[error("Failed to reset owner: {0}")] + ResetOwner(vhost::Error), + #[error("Failed to set backend specific features: {0}")] + SetBackendFeatures(vhost::Error), + #[error("Failed to set backend configuration: {0}")] + SetConfig(vhost::Error), + #[error("Failed to set eventfd notifying about a configuration change: {0}")] + SetConfigCall(vhost::Error), + #[error("Failed to set virtio features: {0}")] + SetFeatures(vhost::Error), + #[error("Failed to set memory table: {0}")] + SetMemTable(vhost::Error), + #[error("Failed to set owner: {0}")] + SetOwner(vhost::Error), + #[error("Failed to set virtio status: {0}")] + SetStatus(vhost::Error), + #[error("Failed to set vring address: {0}")] + SetVringAddr(vhost::Error), + #[error("Failed to set vring base: {0}")] + SetVringBase(vhost::Error), + #[error("Failed to set vring eventfd when buffer are used: {0}")] + SetVringCall(vhost::Error), + #[error("Failed to enable/disable vring: {0}")] + SetVringEnable(vhost::Error), + #[error("Failed to set vring eventfd when new descriptors are available: {0}")] + SetVringKick(vhost::Error), + #[error("Failed to set vring size: {0}")] + SetVringNum(vhost::Error), +} + +pub type Result = std::result::Result; + +#[derive(Serialize, Deserialize)] +pub struct VdpaState { + pub avail_features: u64, + pub acked_features: u64, + pub device_type: u32, + pub iova_range_first: u64, + pub iova_range_last: u64, + pub config: Vec, + pub queue_sizes: Vec, + pub backend_features: u64, +} + +pub struct Vdpa { + common: VirtioCommon, + id: String, + vhost: Option>>, + iova_range: VhostVdpaIovaRange, + enabled_queues: BTreeMap, + backend_features: u64, + migrating: bool, +} + +impl Vdpa { + pub fn new( + id: String, + device_path: &str, + mem: GuestMemoryAtomic, + num_queues: u16, + state: Option, + ) -> Result { + let mut vhost = VhostKernVdpa::new(device_path, mem).map_err(Error::CreateVhostVdpa)?; + vhost.set_owner().map_err(Error::SetOwner)?; + + let ( + device_type, + avail_features, + acked_features, + queue_sizes, + iova_range, + backend_features, + ) = if let Some(state) = state { + info!("Restoring vDPA {}", id); + + vhost.set_backend_features_acked(state.backend_features); + vhost + .set_config(0, state.config.as_slice()) + .map_err(Error::SetConfig)?; + + ( + state.device_type, + state.avail_features, + state.acked_features, + state.queue_sizes, + VhostVdpaIovaRange { + first: state.iova_range_first, + last: state.iova_range_last, + }, + state.backend_features, + ) + } else { + let device_type = vhost.get_device_id().map_err(Error::GetDeviceId)?; + let queue_size = vhost.get_vring_num().map_err(Error::GetVringNum)?; + let avail_features = vhost.get_features().map_err(Error::GetFeatures)?; + let backend_features = vhost + .get_backend_features() + .map_err(Error::GetBackendFeatures)?; + vhost.set_backend_features_acked(backend_features); + + let iova_range = vhost.get_iova_range().map_err(Error::GetIovaRange)?; + + if avail_features & (1u64 << VIRTIO_F_IOMMU_PLATFORM) == 0 { + return Err(Error::MissingAccessPlatformVirtioFeature); + } + + ( + device_type, + avail_features, + 0, + vec![queue_size; num_queues as usize], + iova_range, + backend_features, + ) + }; + + Ok(Vdpa { + common: VirtioCommon { + device_type, + queue_sizes, + avail_features, + acked_features, + min_queues: num_queues, + ..Default::default() + }, + id, + vhost: Some(vhost), + iova_range, + enabled_queues: BTreeMap::new(), + backend_features, + migrating: false, + }) + } + + fn enable_vrings(&mut self, enable: bool) -> Result<()> { + assert!(self.vhost.is_some()); + + for (queue_index, enabled) in self.enabled_queues.iter_mut() { + if *enabled != enable { + self.vhost + .as_ref() + .unwrap() + .set_vring_enable(*queue_index, enable) + .map_err(Error::SetVringEnable)?; + *enabled = enable; + } + } + + Ok(()) + } + + fn activate_vdpa( + &mut self, + mem: &GuestMemoryMmap, + virtio_interrupt: &Arc, + queues: Vec<(usize, Queue, EventFd)>, + ) -> Result<()> { + assert!(self.vhost.is_some()); + self.vhost + .as_ref() + .unwrap() + .set_features(self.common.acked_features) + .map_err(Error::SetFeatures)?; + self.vhost + .as_mut() + .unwrap() + .set_backend_features(self.backend_features) + .map_err(Error::SetBackendFeatures)?; + + for (queue_index, queue, queue_evt) in queues.iter() { + let queue_max_size = queue.max_size(); + let queue_size = queue.size(); + self.vhost + .as_ref() + .unwrap() + .set_vring_num(*queue_index, queue_size) + .map_err(Error::SetVringNum)?; + + let config_data = VringConfigData { + queue_max_size, + queue_size, + flags: 0u32, + desc_table_addr: queue.desc_table().translate_gpa( + self.common.access_platform.as_ref(), + queue_size as usize * std::mem::size_of::(), + ), + used_ring_addr: queue.used_ring().translate_gpa( + self.common.access_platform.as_ref(), + 4 + queue_size as usize * 8, + ), + avail_ring_addr: queue.avail_ring().translate_gpa( + self.common.access_platform.as_ref(), + 4 + queue_size as usize * 2, + ), + log_addr: None, + }; + + self.vhost + .as_ref() + .unwrap() + .set_vring_addr(*queue_index, &config_data) + .map_err(Error::SetVringAddr)?; + self.vhost + .as_ref() + .unwrap() + .set_vring_base( + *queue_index, + queue + .avail_idx(mem, Ordering::Acquire) + .map_err(Error::GetAvailableIndex)? + .0, + ) + .map_err(Error::SetVringBase)?; + + if let Some(eventfd) = + virtio_interrupt.notifier(VirtioInterruptType::Queue(*queue_index as u16)) + { + self.vhost + .as_ref() + .unwrap() + .set_vring_call(*queue_index, &eventfd) + .map_err(Error::SetVringCall)?; + } + + self.vhost + .as_ref() + .unwrap() + .set_vring_kick(*queue_index, queue_evt) + .map_err(Error::SetVringKick)?; + + self.enabled_queues.insert(*queue_index, false); + } + + // Setup the config eventfd if there is one + if let Some(eventfd) = virtio_interrupt.notifier(VirtioInterruptType::Config) { + self.vhost + .as_ref() + .unwrap() + .set_config_call(&eventfd) + .map_err(Error::SetConfigCall)?; + } + + self.enable_vrings(true)?; + + self.vhost + .as_ref() + .unwrap() + .set_status( + (DEVICE_ACKNOWLEDGE | DEVICE_DRIVER | DEVICE_DRIVER_OK | DEVICE_FEATURES_OK) as u8, + ) + .map_err(Error::SetStatus) + } + + fn reset_vdpa(&mut self) -> Result<()> { + self.enable_vrings(false)?; + + assert!(self.vhost.is_some()); + self.vhost + .as_ref() + .unwrap() + .set_status(0) + .map_err(Error::SetStatus) + } + + fn dma_map( + &mut self, + iova: u64, + size: u64, + host_vaddr: *const u8, + readonly: bool, + ) -> Result<()> { + let iova_last = iova + size - 1; + if iova < self.iova_range.first || iova_last > self.iova_range.last { + return Err(Error::InvalidIovaRange(iova, iova_last)); + } + + assert!(self.vhost.is_some()); + self.vhost + .as_ref() + .unwrap() + .dma_map(iova, size, host_vaddr, readonly) + .map_err(Error::DmaMap) + } + + fn dma_unmap(&self, iova: u64, size: u64) -> Result<()> { + let iova_last = iova + size - 1; + if iova < self.iova_range.first || iova_last > self.iova_range.last { + return Err(Error::InvalidIovaRange(iova, iova_last)); + } + + assert!(self.vhost.is_some()); + self.vhost + .as_ref() + .unwrap() + .dma_unmap(iova, size) + .map_err(Error::DmaUnmap) + } + + fn state(&self) -> Result { + assert!(self.vhost.is_some()); + let config_size = self + .vhost + .as_ref() + .unwrap() + .get_config_size() + .map_err(Error::GetConfigSize)?; + let mut config = vec![0; config_size as usize]; + self.read_config(0, config.as_mut_slice()); + + Ok(VdpaState { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + device_type: self.common.device_type, + queue_sizes: self.common.queue_sizes.clone(), + iova_range_first: self.iova_range.first, + iova_range_last: self.iova_range.last, + config, + backend_features: self.backend_features, + }) + } +} + +impl VirtioDevice for Vdpa { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + assert!(self.vhost.is_some()); + if let Err(e) = self.vhost.as_ref().unwrap().get_config(offset as u32, data) { + error!("Failed reading virtio config: {}", e); + } + } + + fn write_config(&mut self, offset: u64, data: &[u8]) { + assert!(self.vhost.is_some()); + if let Err(e) = self.vhost.as_ref().unwrap().set_config(offset as u32, data) { + error!("Failed writing virtio config: {}", e); + } + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + virtio_interrupt: Arc, + queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.activate_vdpa(&mem.memory(), &virtio_interrupt, queues) + .map_err(ActivateError::ActivateVdpa)?; + + // Store the virtio interrupt handler as we need to return it on reset + self.common.interrupt_cb = Some(virtio_interrupt); + + event!("vdpa", "activated", "id", &self.id); + Ok(()) + } + + fn reset(&mut self) -> Option> { + if let Err(e) = self.reset_vdpa() { + error!("Failed to reset vhost-vdpa: {:?}", e); + return None; + } + + event!("vdpa", "reset", "id", &self.id); + + // Return the virtio interrupt handler + self.common.interrupt_cb.take() + } + + fn set_access_platform(&mut self, access_platform: Arc) { + self.common.set_access_platform(access_platform) + } +} + +impl Pausable for Vdpa { + fn pause(&mut self) -> std::result::Result<(), MigratableError> { + if !self.migrating { + Err(MigratableError::Pause(anyhow!( + "Can't pause a vDPA device outside live migration" + ))) + } else { + Ok(()) + } + } + + fn resume(&mut self) -> std::result::Result<(), MigratableError> { + if !self.migrating { + Err(MigratableError::Resume(anyhow!( + "Can't resume a vDPA device outside live migration" + ))) + } else { + Ok(()) + } + } +} + +impl Snapshottable for Vdpa { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + if !self.migrating { + return Err(MigratableError::Snapshot(anyhow!( + "Can't snapshot a vDPA device outside live migration" + ))); + } + + let snapshot = Snapshot::new_from_state( + &self.id(), + &self.state().map_err(|e| { + MigratableError::Snapshot(anyhow!("Error snapshotting vDPA device: {:?}", e)) + })?, + )?; + + // Force the vhost handler to be dropped in order to close the vDPA + // file. This will ensure the device can be accessed if the VM is + // migrated on the same host machine. + self.vhost.take(); + + Ok(snapshot) + } +} + +impl Transportable for Vdpa {} + +impl Migratable for Vdpa { + fn start_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.migrating = true; + // Given there's no way to track dirty pages, we must suspend the + // device as soon as the migration process starts. + if self.backend_features & (1 << VHOST_BACKEND_F_SUSPEND) != 0 { + assert!(self.vhost.is_some()); + self.vhost.as_ref().unwrap().suspend().map_err(|e| { + MigratableError::StartMigration(anyhow!("Error suspending vDPA device: {:?}", e)) + }) + } else { + Err(MigratableError::StartMigration(anyhow!( + "vDPA device can't be suspended" + ))) + } + } + + fn complete_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.migrating = false; + Ok(()) + } +} + +pub struct VdpaDmaMapping { + device: Arc>, + memory: Arc, +} + +impl VdpaDmaMapping { + pub fn new(device: Arc>, memory: Arc) -> Self { + Self { device, memory } + } +} + +impl ExternalDmaMapping for VdpaDmaMapping { + fn map(&self, iova: u64, gpa: u64, size: u64) -> result::Result<(), io::Error> { + let mem = self.memory.memory(); + let guest_addr = GuestAddress(gpa); + let user_addr = if mem.check_range(guest_addr, size as usize) { + mem.get_host_address(guest_addr).unwrap() as *const u8 + } else { + return Err(io::Error::new( + io::ErrorKind::Other, + format!( + "failed to convert guest address 0x{:x} into \ + host user virtual address", + gpa + ), + )); + }; + + debug!( + "DMA map iova 0x{:x}, gpa 0x{:x}, size 0x{:x}, host_addr 0x{:x}", + iova, gpa, size, user_addr as u64 + ); + self.device + .lock() + .unwrap() + .dma_map(iova, size, user_addr, false) + .map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!( + "failed to map memory for vDPA device, \ + iova 0x{:x}, gpa 0x{:x}, size 0x{:x}: {:?}", + iova, gpa, size, e + ), + ) + }) + } + + fn unmap(&self, iova: u64, size: u64) -> std::result::Result<(), std::io::Error> { + debug!("DMA unmap iova 0x{:x} size 0x{:x}", iova, size); + self.device + .lock() + .unwrap() + .dma_unmap(iova, size) + .map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!( + "failed to unmap memory for vDPA device, \ + iova 0x{:x}, size 0x{:x}: {:?}", + iova, size, e + ), + ) + }) + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vhost_user/blk.rs b/vendor/cube-hypervisor/virtio-devices/src/vhost_user/blk.rs new file mode 100644 index 0000000..96933a0 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vhost_user/blk.rs @@ -0,0 +1,399 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use super::super::{ActivateResult, VirtioCommon, VirtioDevice, VirtioDeviceType}; +use super::vu_common_ctrl::{VhostUserConfig, VhostUserHandle}; +use super::{Error, Result, DEFAULT_VIRTIO_FEATURES}; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::vhost_user::VhostUserCommon; +use crate::{GuestMemoryMmap, GuestRegionMmap}; +use crate::{VirtioInterrupt, VIRTIO_F_IOMMU_PLATFORM}; +use block_util::VirtioBlockConfig; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::mem; +use std::result; +use std::sync::{Arc, Barrier, Mutex}; +use std::thread; +use std::vec::Vec; +use vhost::vhost_user::message::{ + VhostUserConfigFlags, VhostUserProtocolFeatures, VhostUserVirtioFeatures, + VHOST_USER_CONFIG_OFFSET, +}; +use vhost::vhost_user::{FrontendReqHandler, VhostUserFrontend, VhostUserFrontendReqHandler}; +use virtio_bindings::bindings::virtio_blk::{ + VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_CONFIG_WCE, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_FLUSH, + VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_SEG_MAX, + VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_WRITE_ZEROES, +}; +use virtio_queue::Queue; +use vm_memory::{ByteValued, GuestMemoryAtomic}; +use vm_migration::{ + protocol::MemoryRangeTable, Migratable, MigratableError, Pausable, Snapshot, Snapshottable, + Transportable, +}; +use vmm_sys_util::eventfd::EventFd; + +const DEFAULT_QUEUE_NUMBER: usize = 1; + +#[derive(Serialize, Deserialize)] +pub struct State { + pub avail_features: u64, + pub acked_features: u64, + pub config: VirtioBlockConfig, + pub acked_protocol_features: u64, + pub vu_num_queues: usize, +} + +struct BackendReqHandler {} +impl VhostUserFrontendReqHandler for BackendReqHandler {} + +pub struct Blk { + common: VirtioCommon, + vu_common: VhostUserCommon, + id: String, + config: VirtioBlockConfig, + guest_memory: Option>, + epoll_thread: Option>, + seccomp_action: SeccompAction, + exit_evt: EventFd, + iommu: bool, +} + +impl Blk { + /// Create a new vhost-user-blk device + pub fn new( + id: String, + vu_cfg: VhostUserConfig, + seccomp_action: SeccompAction, + exit_evt: EventFd, + iommu: bool, + state: Option, + ) -> Result { + let num_queues = vu_cfg.num_queues; + + let mut vu = + VhostUserHandle::connect_vhost_user(false, &vu_cfg.socket, num_queues as u64, false)?; + + let (avail_features, acked_features, acked_protocol_features, vu_num_queues, config) = + if let Some(state) = state { + info!("Restoring vhost-user-block {}", id); + + vu.set_protocol_features_vhost_user( + state.acked_features, + state.acked_protocol_features, + )?; + + ( + state.avail_features, + state.acked_features, + state.acked_protocol_features, + state.vu_num_queues, + state.config, + ) + } else { + // Filling device and vring features VMM supports. + let mut avail_features = 1 << VIRTIO_BLK_F_SIZE_MAX + | 1 << VIRTIO_BLK_F_SEG_MAX + | 1 << VIRTIO_BLK_F_GEOMETRY + | 1 << VIRTIO_BLK_F_RO + | 1 << VIRTIO_BLK_F_BLK_SIZE + | 1 << VIRTIO_BLK_F_FLUSH + | 1 << VIRTIO_BLK_F_TOPOLOGY + | 1 << VIRTIO_BLK_F_CONFIG_WCE + | 1 << VIRTIO_BLK_F_DISCARD + | 1 << VIRTIO_BLK_F_WRITE_ZEROES + | DEFAULT_VIRTIO_FEATURES; + + if num_queues > 1 { + avail_features |= 1 << VIRTIO_BLK_F_MQ; + } + + let avail_protocol_features = VhostUserProtocolFeatures::CONFIG + | VhostUserProtocolFeatures::MQ + | VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS + | VhostUserProtocolFeatures::REPLY_ACK + | VhostUserProtocolFeatures::INFLIGHT_SHMFD + | VhostUserProtocolFeatures::LOG_SHMFD; + + let (acked_features, acked_protocol_features) = + vu.negotiate_features_vhost_user(avail_features, avail_protocol_features)?; + + let backend_num_queues = + if acked_protocol_features & VhostUserProtocolFeatures::MQ.bits() != 0 { + vu.socket_handle() + .get_queue_num() + .map_err(Error::VhostUserGetQueueMaxNum)? + as usize + } else { + DEFAULT_QUEUE_NUMBER + }; + + if num_queues > backend_num_queues { + error!("vhost-user-blk requested too many queues ({}) since the backend only supports {}\n", + num_queues, backend_num_queues); + return Err(Error::BadQueueNum); + } + + let config_len = mem::size_of::(); + let config_space: Vec = vec![0u8; config_len]; + let (_, config_space) = vu + .socket_handle() + .get_config( + VHOST_USER_CONFIG_OFFSET, + config_len as u32, + VhostUserConfigFlags::WRITABLE, + config_space.as_slice(), + ) + .map_err(Error::VhostUserGetConfig)?; + let mut config = VirtioBlockConfig::default(); + if let Some(backend_config) = VirtioBlockConfig::from_slice(config_space.as_slice()) + { + config = *backend_config; + config.num_queues = num_queues as u16; + } + + ( + acked_features, + // If part of the available features that have been acked, + // the PROTOCOL_FEATURES bit must be already set through + // the VIRTIO acked features as we know the guest would + // never ack it, thus the feature would be lost. + acked_features & VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits(), + acked_protocol_features, + num_queues, + config, + ) + }; + + Ok(Blk { + common: VirtioCommon { + device_type: VirtioDeviceType::Block as u32, + queue_sizes: vec![vu_cfg.queue_size; num_queues], + avail_features, + acked_features, + paused_sync: Some(Arc::new(Barrier::new(2))), + min_queues: DEFAULT_QUEUE_NUMBER as u16, + ..Default::default() + }, + vu_common: VhostUserCommon { + vu: Some(Arc::new(Mutex::new(vu))), + acked_protocol_features, + socket_path: vu_cfg.socket, + vu_num_queues, + ..Default::default() + }, + id, + config, + guest_memory: None, + epoll_thread: None, + seccomp_action, + exit_evt, + iommu, + }) + } + + fn state(&self) -> State { + State { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + config: self.config, + acked_protocol_features: self.vu_common.acked_protocol_features, + vu_num_queues: self.vu_common.vu_num_queues, + } + } +} + +impl Drop for Blk { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + if let Err(e) = kill_evt.write(1) { + error!("failed to kill vhost-user-blk: {:?}", e); + } + } + } +} + +impl VirtioDevice for Blk { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + let mut features = self.common.avail_features; + if self.iommu { + features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM; + } + features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.as_slice(), offset, data); + } + + fn write_config(&mut self, offset: u64, data: &[u8]) { + // The "writeback" field is the only mutable field + let writeback_offset = + (&self.config.writeback as *const _ as u64) - (&self.config as *const _ as u64); + if offset != writeback_offset || data.len() != std::mem::size_of_val(&self.config.writeback) + { + error!( + "Attempt to write to read-only field: offset {:x} length {}", + offset, + data.len() + ); + return; + } + + self.config.writeback = data[0]; + if let Some(vu) = &self.vu_common.vu { + if let Err(e) = vu + .lock() + .unwrap() + .socket_handle() + .set_config(offset as u32, VhostUserConfigFlags::WRITABLE, data) + .map_err(Error::VhostUserSetConfig) + { + error!("Failed setting vhost-user-blk configuration: {:?}", e); + } + } + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + self.guest_memory = Some(mem.clone()); + + let backend_req_handler: Option> = None; + + // Run a dedicated thread for handling potential reconnections with + // the backend. + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let mut handler = self.vu_common.activate( + mem, + queues, + interrupt_cb, + self.common.acked_features, + backend_req_handler, + kill_evt, + pause_evt, + )?; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + + let mut epoll_threads = Vec::new(); + + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioVhostBlock, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + self.epoll_thread = Some(epoll_threads.remove(0)); + + Ok(()) + } + + fn reset(&mut self) -> Option> { + // We first must resume the virtio thread if it was paused. + if self.common.pause_evt.take().is_some() { + self.common.resume().ok()?; + } + + if let Some(vu) = &self.vu_common.vu { + if let Err(e) = vu.lock().unwrap().reset_vhost_user() { + error!("Failed to reset vhost-user daemon: {:?}", e); + return None; + } + } + + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + + event!("virtio-device", "reset", "id", &self.id); + + // Return the interrupt + Some(self.common.interrupt_cb.take().unwrap()) + } + + fn shutdown(&mut self) { + self.vu_common.shutdown() + } + + fn add_memory_region( + &mut self, + region: &Arc, + ) -> std::result::Result<(), crate::Error> { + self.vu_common.add_memory_region(&self.guest_memory, region) + } +} + +impl Pausable for Blk { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.vu_common.pause()?; + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume()?; + + if let Some(epoll_thread) = &self.epoll_thread { + epoll_thread.thread().unpark(); + } + + self.vu_common.resume() + } +} + +impl Snapshottable for Blk { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + self.vu_common.snapshot(&self.id(), &self.state()) + } +} +impl Transportable for Blk {} + +impl Migratable for Blk { + fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common.start_dirty_log(&self.guest_memory) + } + + fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common.stop_dirty_log() + } + + fn dirty_log(&mut self) -> std::result::Result { + self.vu_common.dirty_log(&self.guest_memory) + } + + fn start_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common.start_migration() + } + + fn complete_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common + .complete_migration(self.common.kill_evt.take()) + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vhost_user/fs.rs b/vendor/cube-hypervisor/virtio-devices/src/vhost_user/fs.rs new file mode 100644 index 0000000..311e757 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vhost_user/fs.rs @@ -0,0 +1,400 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use super::vu_common_ctrl::VhostUserHandle; +use super::{Error, Result, DEFAULT_VIRTIO_FEATURES}; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::vhost_user::VhostUserCommon; +use crate::{ + ActivateResult, UserspaceMapping, VirtioCommon, VirtioDevice, VirtioDeviceType, + VirtioInterrupt, VirtioSharedMemoryList, VIRTIO_F_IOMMU_PLATFORM, +}; +use crate::{GuestMemoryMmap, GuestRegionMmap, MmapRegion}; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use serde_with::{serde_as, Bytes}; +use std::result; +use std::sync::{Arc, Barrier, Mutex}; +use std::thread; +use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures}; +use vhost::vhost_user::{FrontendReqHandler, VhostUserFrontend, VhostUserFrontendReqHandler}; +use virtio_queue::Queue; +use vm_memory::{ByteValued, GuestMemoryAtomic}; +use vm_migration::{ + protocol::MemoryRangeTable, Migratable, MigratableError, Pausable, Snapshot, Snapshottable, + Transportable, +}; +use vmm_sys_util::eventfd::EventFd; + +const NUM_QUEUE_OFFSET: usize = 1; +const DEFAULT_QUEUE_NUMBER: usize = 2; + +#[derive(Serialize, Deserialize)] +pub struct State { + pub avail_features: u64, + pub acked_features: u64, + pub config: VirtioFsConfig, + pub acked_protocol_features: u64, + pub vu_num_queues: usize, + pub backend_req_support: bool, +} + +struct BackendReqHandler {} +impl VhostUserFrontendReqHandler for BackendReqHandler {} + +#[serde_as] +#[derive(Copy, Clone, Serialize, Deserialize)] +#[repr(C, packed)] +pub struct VirtioFsConfig { + #[serde_as(as = "Bytes")] + pub tag: [u8; 36], + pub num_request_queues: u32, +} + +impl Default for VirtioFsConfig { + fn default() -> Self { + VirtioFsConfig { + tag: [0; 36], + num_request_queues: 0, + } + } +} + +unsafe impl ByteValued for VirtioFsConfig {} + +pub struct Fs { + common: VirtioCommon, + vu_common: VhostUserCommon, + id: String, + config: VirtioFsConfig, + // Hold ownership of the memory that is allocated for the device + // which will be automatically dropped when the device is dropped + cache: Option<(VirtioSharedMemoryList, MmapRegion)>, + seccomp_action: SeccompAction, + guest_memory: Option>, + epoll_thread: Option>, + exit_evt: EventFd, + iommu: bool, +} + +impl Fs { + /// Create a new virtio-fs device. + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + path: &str, + tag: &str, + req_num_queues: usize, + queue_size: u16, + cache: Option<(VirtioSharedMemoryList, MmapRegion)>, + seccomp_action: SeccompAction, + exit_evt: EventFd, + iommu: bool, + state: Option, + ) -> Result { + // Calculate the actual number of queues needed. + let num_queues = NUM_QUEUE_OFFSET + req_num_queues; + + // Connect to the vhost-user socket. + let mut vu = VhostUserHandle::connect_vhost_user(false, path, num_queues as u64, false)?; + + let (avail_features, acked_features, acked_protocol_features, vu_num_queues, config) = + if let Some(state) = state { + info!("Restoring vhost-user-fs {}", id); + + vu.set_protocol_features_vhost_user( + state.acked_features, + state.acked_protocol_features, + )?; + + ( + state.avail_features, + state.acked_features, + state.acked_protocol_features, + state.vu_num_queues, + state.config, + ) + } else { + // Filling device and vring features VMM supports. + let avail_features = DEFAULT_VIRTIO_FEATURES; + + let avail_protocol_features = VhostUserProtocolFeatures::MQ + | VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS + | VhostUserProtocolFeatures::REPLY_ACK + | VhostUserProtocolFeatures::INFLIGHT_SHMFD + | VhostUserProtocolFeatures::LOG_SHMFD; + + let (acked_features, acked_protocol_features) = + vu.negotiate_features_vhost_user(avail_features, avail_protocol_features)?; + + let backend_num_queues = + if acked_protocol_features & VhostUserProtocolFeatures::MQ.bits() != 0 { + vu.socket_handle() + .get_queue_num() + .map_err(Error::VhostUserGetQueueMaxNum)? + as usize + } else { + DEFAULT_QUEUE_NUMBER + }; + + if num_queues > backend_num_queues { + error!( + "vhost-user-fs requested too many queues ({}) since the backend only supports {}\n", + num_queues, backend_num_queues + ); + return Err(Error::BadQueueNum); + } + + // Create virtio-fs device configuration. + let mut config = VirtioFsConfig::default(); + let tag_bytes_vec = tag.to_string().into_bytes(); + config.tag[..tag_bytes_vec.len()].copy_from_slice(tag_bytes_vec.as_slice()); + config.num_request_queues = req_num_queues as u32; + + ( + acked_features, + // If part of the available features that have been acked, the + // PROTOCOL_FEATURES bit must be already set through the VIRTIO + // acked features as we know the guest would never ack it, thus + // the feature would be lost. + acked_features & VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits(), + acked_protocol_features, + num_queues, + config, + ) + }; + + Ok(Fs { + common: VirtioCommon { + device_type: VirtioDeviceType::Fs as u32, + avail_features, + acked_features, + queue_sizes: vec![queue_size; num_queues], + paused_sync: Some(Arc::new(Barrier::new(2))), + min_queues: 1, + ..Default::default() + }, + vu_common: VhostUserCommon { + vu: Some(Arc::new(Mutex::new(vu))), + acked_protocol_features, + socket_path: path.to_string(), + vu_num_queues, + ..Default::default() + }, + id, + config, + cache, + seccomp_action, + guest_memory: None, + epoll_thread: None, + exit_evt, + iommu, + }) + } + + fn state(&self) -> State { + State { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + config: self.config, + acked_protocol_features: self.vu_common.acked_protocol_features, + vu_num_queues: self.vu_common.vu_num_queues, + backend_req_support: false, + } + } +} + +impl Drop for Fs { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +impl VirtioDevice for Fs { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + let mut features = self.common.avail_features; + if self.iommu { + features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM; + } + features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.as_slice(), offset, data); + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + self.guest_memory = Some(mem.clone()); + + let backend_req_handler: Option> = None; + // Run a dedicated thread for handling potential reconnections with + // the backend. + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let mut handler = self.vu_common.activate( + mem, + queues, + interrupt_cb, + self.common.acked_features, + backend_req_handler, + kill_evt, + pause_evt, + )?; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + + let mut epoll_threads = Vec::new(); + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioVhostFs, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + self.epoll_thread = Some(epoll_threads.remove(0)); + + event!("virtio-device", "activated", "id", &self.id); + Ok(()) + } + + fn reset(&mut self) -> Option> { + // We first must resume the virtio thread if it was paused. + if self.common.pause_evt.take().is_some() { + self.common.resume().ok()?; + } + + if let Some(vu) = &self.vu_common.vu { + if let Err(e) = vu.lock().unwrap().reset_vhost_user() { + error!("Failed to reset vhost-user daemon: {:?}", e); + return None; + } + } + + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + + event!("virtio-device", "reset", "id", &self.id); + + // Return the interrupt + Some(self.common.interrupt_cb.take().unwrap()) + } + + fn shutdown(&mut self) { + self.vu_common.shutdown() + } + + fn get_shm_regions(&self) -> Option { + self.cache.as_ref().map(|cache| cache.0.clone()) + } + + fn set_shm_regions( + &mut self, + shm_regions: VirtioSharedMemoryList, + ) -> std::result::Result<(), crate::Error> { + if let Some(cache) = self.cache.as_mut() { + cache.0 = shm_regions; + Ok(()) + } else { + Err(crate::Error::SetShmRegionsNotSupported) + } + } + + fn add_memory_region( + &mut self, + region: &Arc, + ) -> std::result::Result<(), crate::Error> { + self.vu_common.add_memory_region(&self.guest_memory, region) + } + + fn userspace_mappings(&self) -> Vec { + let mut mappings = Vec::new(); + if let Some(cache) = self.cache.as_ref() { + mappings.push(UserspaceMapping { + host_addr: cache.0.host_addr, + mem_slot: cache.0.mem_slot, + addr: cache.0.addr, + len: cache.0.len, + mergeable: false, + }) + } + + mappings + } +} + +impl Pausable for Fs { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.vu_common.pause()?; + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume()?; + + if let Some(epoll_thread) = &self.epoll_thread { + epoll_thread.thread().unpark(); + } + + self.vu_common.resume() + } +} + +impl Snapshottable for Fs { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + self.vu_common.snapshot(&self.id(), &self.state()) + } +} +impl Transportable for Fs {} + +impl Migratable for Fs { + fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common.start_dirty_log(&self.guest_memory) + } + + fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common.stop_dirty_log() + } + + fn dirty_log(&mut self) -> std::result::Result { + self.vu_common.dirty_log(&self.guest_memory) + } + + fn start_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common.start_migration() + } + + fn complete_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common + .complete_migration(self.common.kill_evt.take()) + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vhost_user/mod.rs b/vendor/cube-hypervisor/virtio-devices/src/vhost_user/mod.rs new file mode 100644 index 0000000..497c150 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vhost_user/mod.rs @@ -0,0 +1,537 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use crate::{ + ActivateError, EpollHelper, EpollHelperError, EpollHelperHandler, GuestMemoryMmap, + GuestRegionMmap, VirtioInterrupt, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_IN_ORDER, + VIRTIO_F_NOTIFICATION_DATA, VIRTIO_F_ORDER_PLATFORM, VIRTIO_F_RING_EVENT_IDX, + VIRTIO_F_RING_INDIRECT_DESC, VIRTIO_F_VERSION_1, +}; +use anyhow::anyhow; +use serde::{Deserialize, Serialize}; +use std::io; +use std::ops::Deref; +use std::os::unix::io::AsRawFd; +use std::sync::{atomic::AtomicBool, Arc, Barrier, Mutex}; +use thiserror::Error; +use vhost::vhost_user::message::{ + VhostUserInflight, VhostUserProtocolFeatures, VhostUserVirtioFeatures, +}; +use vhost::vhost_user::{FrontendReqHandler, VhostUserFrontendReqHandler}; +use vhost::Error as VhostError; +use virtio_queue::Error as QueueError; +use virtio_queue::Queue; +use vm_memory::{ + mmap::MmapRegionError, Address, Error as MmapError, GuestAddressSpace, GuestMemory, + GuestMemoryAtomic, +}; +use vm_migration::{protocol::MemoryRangeTable, MigratableError, Snapshot}; +use vmm_sys_util::eventfd::EventFd; +use vu_common_ctrl::VhostUserHandle; + +pub mod blk; +pub mod fs; +pub mod net; +pub mod vu_common_ctrl; + +pub use self::blk::Blk; +pub use self::fs::*; +pub use self::net::Net; +pub use self::vu_common_ctrl::VhostUserConfig; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Failed accepting connection: {0}")] + AcceptConnection(io::Error), + #[error("Invalid available address")] + AvailAddress, + #[error("Queue number is not correct")] + BadQueueNum, + #[error("Failed binding vhost-user socket: {0}")] + BindSocket(io::Error), + #[error("Creating kill eventfd failed: {0}")] + CreateKillEventFd(io::Error), + #[error("Cloning kill eventfd failed: {0}")] + CloneKillEventFd(io::Error), + #[error("Invalid descriptor table address")] + DescriptorTableAddress, + #[error("Signal used queue failed: {0}")] + FailedSignalingUsedQueue(io::Error), + #[error("Failed to read vhost eventfd: {0}")] + MemoryRegions(MmapError), + #[error("Failed removing socket path: {0}")] + RemoveSocketPath(io::Error), + #[error("Failed to create frontend: {0}")] + VhostUserCreateFrontend(VhostError), + #[error("Failed to open vhost device: {0}")] + VhostUserOpen(VhostError), + #[error("Connection to socket failed")] + VhostUserConnect, + #[error("Get features failed: {0}")] + VhostUserGetFeatures(VhostError), + #[error("Get queue max number failed: {0}")] + VhostUserGetQueueMaxNum(VhostError), + #[error("Get protocol features failed: {0}")] + VhostUserGetProtocolFeatures(VhostError), + #[error("Get vring base failed: {0}")] + VhostUserGetVringBase(VhostError), + #[error("Vhost-user Backend not support vhost-user protocol")] + VhostUserProtocolNotSupport, + #[error("Set owner failed: {0}")] + VhostUserSetOwner(VhostError), + #[error("Reset owner failed: {0}")] + VhostUserResetOwner(VhostError), + #[error("Set features failed: {0}")] + VhostUserSetFeatures(VhostError), + #[error("Set protocol features failed: {0}")] + VhostUserSetProtocolFeatures(VhostError), + #[error("Set mem table failed: {0}")] + VhostUserSetMemTable(VhostError), + #[error("Set vring num failed: {0}")] + VhostUserSetVringNum(VhostError), + #[error("Set vring addr failed: {0}")] + VhostUserSetVringAddr(VhostError), + #[error("Set vring base failed: {0}")] + VhostUserSetVringBase(VhostError), + #[error("Set vring call failed: {0}")] + VhostUserSetVringCall(VhostError), + #[error("Set vring kick failed: {0}")] + VhostUserSetVringKick(VhostError), + #[error("Set vring enable failed: {0}")] + VhostUserSetVringEnable(VhostError), + #[error("Failed to create vhost eventfd: {0}")] + VhostIrqCreate(io::Error), + #[error("Failed to read vhost eventfd: {0}")] + VhostIrqRead(io::Error), + #[error("Failed to read vhost eventfd: {0}")] + VhostUserMemoryRegion(MmapError), + #[error("Failed to create the frontend request handler from backend: {0}")] + FrontendReqHandlerCreation(vhost::vhost_user::Error), + #[error("Set backend request fd failed: {0}")] + VhostUserSetBackendRequestFd(vhost::Error), + #[error("Add memory region failed: {0}")] + VhostUserAddMemReg(VhostError), + #[error("Failed getting the configuration: {0}")] + VhostUserGetConfig(VhostError), + #[error("Failed setting the configuration: {0}")] + VhostUserSetConfig(VhostError), + #[error("Failed getting inflight shm log: {0}")] + VhostUserGetInflight(VhostError), + #[error("Failed setting inflight shm log: {0}")] + VhostUserSetInflight(VhostError), + #[error("Failed setting the log base: {0}")] + VhostUserSetLogBase(VhostError), + #[error("Invalid used address")] + UsedAddress, + #[error("Invalid features provided from vhost-user backe")] + InvalidFeatures, + #[error("Missing file descriptor for the region")] + MissingRegionFd, + #[error("Missing IrqFd")] + MissingIrqFd, + #[error("Failed getting the available index: {0}")] + GetAvailableIndex(QueueError), + #[error("Migration is not supported by this vhost-user device")] + MigrationNotSupported, + #[error("Failed creating memfd: {0}")] + MemfdCreate(io::Error), + #[error("Failed truncating the file size to the expected size: {0}")] + SetFileSize(io::Error), + #[error("Failed to set the seals on the file: {0}")] + SetSeals(io::Error), + #[error("Failed creating new mmap region: {0}")] + NewMmapRegion(MmapRegionError), + #[error("Could not find the shm log region")] + MissingShmLogRegion, +} +type Result = std::result::Result; + +pub const DEFAULT_VIRTIO_FEATURES: u64 = 1 << VIRTIO_F_RING_INDIRECT_DESC + | 1 << VIRTIO_F_RING_EVENT_IDX + | 1 << VIRTIO_F_VERSION_1 + | 1 << VIRTIO_F_IN_ORDER + | 1 << VIRTIO_F_ORDER_PLATFORM + | 1 << VIRTIO_F_NOTIFICATION_DATA + | VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits(); + +const HUP_CONNECTION_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; +const BACKEND_REQ_EVNET: u16 = EPOLL_HELPER_EVENT_LAST + 2; + +#[derive(Default)] +pub struct Inflight { + pub info: VhostUserInflight, + pub fd: Option, +} + +pub struct VhostUserEpollHandler { + pub vu: Arc>, + pub mem: GuestMemoryAtomic, + pub kill_evt: EventFd, + pub pause_evt: EventFd, + pub queues: Vec<(usize, Queue, EventFd)>, + pub virtio_interrupt: Arc, + pub acked_features: u64, + pub acked_protocol_features: u64, + pub socket_path: String, + pub server: bool, + pub backend_req_handler: Option>, + pub inflight: Option, +} + +impl VhostUserEpollHandler { + pub fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> std::result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event_custom( + self.vu.lock().unwrap().socket_handle().as_raw_fd(), + HUP_CONNECTION_EVENT, + epoll::Events::EPOLLHUP, + )?; + + if let Some(backend_req_handler) = &self.backend_req_handler { + helper.add_event(backend_req_handler.as_raw_fd(), BACKEND_REQ_EVNET)?; + } + + helper.run(paused, paused_sync, self)?; + + Ok(()) + } + + fn reconnect(&mut self, helper: &mut EpollHelper) -> std::result::Result<(), EpollHelperError> { + helper.del_event_custom( + self.vu.lock().unwrap().socket_handle().as_raw_fd(), + HUP_CONNECTION_EVENT, + epoll::Events::EPOLLHUP, + )?; + + let mut vhost_user = VhostUserHandle::connect_vhost_user( + self.server, + &self.socket_path, + self.queues.len() as u64, + true, + ) + .map_err(|e| { + EpollHelperError::IoError(std::io::Error::new( + std::io::ErrorKind::Other, + format!("failed connecting vhost-user backend {:?}", e), + )) + })?; + + // Initialize the backend + vhost_user + .reinitialize_vhost_user( + self.mem.memory().deref(), + self.queues + .iter() + .map(|(i, q, e)| (*i, vm_virtio::clone_queue(q), e.try_clone().unwrap())) + .collect(), + &self.virtio_interrupt, + self.acked_features, + self.acked_protocol_features, + &self.backend_req_handler, + self.inflight.as_mut(), + ) + .map_err(|e| { + EpollHelperError::IoError(std::io::Error::new( + std::io::ErrorKind::Other, + format!("failed reconnecting vhost-user backend{:?}", e), + )) + })?; + + helper.add_event_custom( + vhost_user.socket_handle().as_raw_fd(), + HUP_CONNECTION_EVENT, + epoll::Events::EPOLLHUP, + )?; + + // Update vhost-user reference + let mut vu = self.vu.lock().unwrap(); + *vu = vhost_user; + + Ok(()) + } +} + +impl EpollHelperHandler for VhostUserEpollHandler { + fn handle_event( + &mut self, + helper: &mut EpollHelper, + event: &epoll::Event, + ) -> std::result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + HUP_CONNECTION_EVENT => { + self.reconnect(helper).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "failed to reconnect vhost-user backend: {:?}", + e + )) + })?; + } + BACKEND_REQ_EVNET => { + if let Some(backend_req_handler) = self.backend_req_handler.as_mut() { + backend_req_handler.handle_request().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to handle request from vhost-user backend: {:?}", + e + )) + })?; + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unknown event for vhost-user thread" + ))); + } + } + + Ok(()) + } +} + +#[derive(Default)] +pub struct VhostUserCommon { + pub vu: Option>>, + pub acked_protocol_features: u64, + pub socket_path: String, + pub vu_num_queues: usize, + pub migration_started: bool, + pub server: bool, +} + +impl VhostUserCommon { + #[allow(clippy::too_many_arguments)] + pub fn activate( + &mut self, + mem: GuestMemoryAtomic, + queues: Vec<(usize, Queue, EventFd)>, + interrupt_cb: Arc, + acked_features: u64, + backend_req_handler: Option>, + kill_evt: EventFd, + pause_evt: EventFd, + ) -> std::result::Result, ActivateError> { + let mut inflight: Option = + if self.acked_protocol_features & VhostUserProtocolFeatures::INFLIGHT_SHMFD.bits() != 0 + { + Some(Inflight::default()) + } else { + None + }; + + if self.vu.is_none() { + error!("Missing vhost-user handle"); + return Err(ActivateError::BadActivate); + } + let vu = self.vu.as_ref().unwrap(); + vu.lock() + .unwrap() + .setup_vhost_user( + &mem.memory(), + queues + .iter() + .map(|(i, q, e)| (*i, vm_virtio::clone_queue(q), e.try_clone().unwrap())) + .collect(), + &interrupt_cb, + acked_features, + &backend_req_handler, + inflight.as_mut(), + ) + .map_err(ActivateError::VhostUserBlkSetup)?; + + Ok(VhostUserEpollHandler { + vu: vu.clone(), + mem, + kill_evt, + pause_evt, + queues, + virtio_interrupt: interrupt_cb, + acked_features, + acked_protocol_features: self.acked_protocol_features, + socket_path: self.socket_path.clone(), + server: self.server, + backend_req_handler, + inflight, + }) + } + + pub fn restore_backend_connection(&mut self, acked_features: u64) -> Result<()> { + let mut vu = VhostUserHandle::connect_vhost_user( + self.server, + &self.socket_path, + self.vu_num_queues as u64, + false, + )?; + + vu.set_protocol_features_vhost_user(acked_features, self.acked_protocol_features)?; + + self.vu = Some(Arc::new(Mutex::new(vu))); + + Ok(()) + } + + pub fn shutdown(&mut self) { + if let Some(vu) = &self.vu { + let _ = unsafe { libc::close(vu.lock().unwrap().socket_handle().as_raw_fd()) }; + } + + // Remove socket path if needed + if self.server { + let _ = std::fs::remove_file(&self.socket_path); + } + } + + pub fn add_memory_region( + &mut self, + guest_memory: &Option>, + region: &Arc, + ) -> std::result::Result<(), crate::Error> { + if let Some(vu) = &self.vu { + if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits() + != 0 + { + return vu + .lock() + .unwrap() + .add_memory_region(region) + .map_err(crate::Error::VhostUserAddMemoryRegion); + } else if let Some(guest_memory) = guest_memory { + return vu + .lock() + .unwrap() + .update_mem_table(guest_memory.memory().deref()) + .map_err(crate::Error::VhostUserUpdateMemory); + } + } + Ok(()) + } + + pub fn pause(&mut self) -> std::result::Result<(), MigratableError> { + if let Some(vu) = &self.vu { + vu.lock().unwrap().pause_vhost_user().map_err(|e| { + MigratableError::Pause(anyhow!("Error pausing vhost-user-blk backend: {:?}", e)) + }) + } else { + Ok(()) + } + } + + pub fn resume(&mut self) -> std::result::Result<(), MigratableError> { + if let Some(vu) = &self.vu { + vu.lock().unwrap().resume_vhost_user().map_err(|e| { + MigratableError::Resume(anyhow!("Error resuming vhost-user-blk backend: {:?}", e)) + }) + } else { + Ok(()) + } + } + + pub fn snapshot<'a, T>( + &mut self, + id: &str, + state: &T, + ) -> std::result::Result + where + T: Serialize + Deserialize<'a>, + { + let snapshot = Snapshot::new_from_state(id, state)?; + + if self.migration_started { + self.shutdown(); + } + + Ok(snapshot) + } + + pub fn start_dirty_log( + &mut self, + guest_memory: &Option>, + ) -> std::result::Result<(), MigratableError> { + if let Some(vu) = &self.vu { + if let Some(guest_memory) = guest_memory { + let last_ram_addr = guest_memory.memory().last_addr().raw_value(); + vu.lock() + .unwrap() + .start_dirty_log(last_ram_addr) + .map_err(|e| { + MigratableError::StartDirtyLog(anyhow!( + "Error starting migration for vhost-user backend: {:?}", + e + )) + }) + } else { + Err(MigratableError::StartDirtyLog(anyhow!( + "Missing guest memory" + ))) + } + } else { + Ok(()) + } + } + + pub fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + if let Some(vu) = &self.vu { + vu.lock().unwrap().stop_dirty_log().map_err(|e| { + MigratableError::StopDirtyLog(anyhow!( + "Error stopping migration for vhost-user backend: {:?}", + e + )) + }) + } else { + Ok(()) + } + } + + pub fn dirty_log( + &mut self, + guest_memory: &Option>, + ) -> std::result::Result { + if let Some(vu) = &self.vu { + if let Some(guest_memory) = guest_memory { + let last_ram_addr = guest_memory.memory().last_addr().raw_value(); + vu.lock().unwrap().dirty_log(last_ram_addr).map_err(|e| { + MigratableError::DirtyLog(anyhow!( + "Error retrieving dirty ranges from vhost-user backend: {:?}", + e + )) + }) + } else { + Err(MigratableError::DirtyLog(anyhow!("Missing guest memory"))) + } + } else { + Ok(MemoryRangeTable::default()) + } + } + + pub fn start_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.migration_started = true; + Ok(()) + } + + pub fn complete_migration( + &mut self, + kill_evt: Option, + ) -> std::result::Result<(), MigratableError> { + self.migration_started = false; + + // Make sure the device thread is killed in order to prevent from + // reconnections to the socket. + if let Some(kill_evt) = kill_evt { + kill_evt.write(1).map_err(|e| { + MigratableError::CompleteMigration(anyhow!( + "Error killing vhost-user thread: {:?}", + e + )) + })?; + } + + // Drop the vhost-user handler to avoid further calls to fail because + // the connection with the backend has been closed. + self.vu = None; + + Ok(()) + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vhost_user/net.rs b/vendor/cube-hypervisor/virtio-devices/src/vhost_user/net.rs new file mode 100644 index 0000000..3733c55 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vhost_user/net.rs @@ -0,0 +1,435 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::vhost_user::vu_common_ctrl::{VhostUserConfig, VhostUserHandle}; +use crate::vhost_user::{Error, Result, VhostUserCommon}; +use crate::{ + ActivateResult, NetCtrlEpollHandler, VirtioCommon, VirtioDevice, VirtioDeviceType, + VirtioInterrupt, VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_RING_EVENT_IDX, VIRTIO_F_VERSION_1, +}; +use crate::{GuestMemoryMmap, GuestRegionMmap}; +use net_util::{build_net_config_space, CtrlQueue, MacAddr, VirtioNetConfig}; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::result; +use std::sync::{Arc, Barrier, Mutex}; +use std::thread; +use std::vec::Vec; +use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures}; +use vhost::vhost_user::{FrontendReqHandler, VhostUserFrontend, VhostUserFrontendReqHandler}; +use virtio_bindings::bindings::virtio_net::{ + VIRTIO_NET_F_CSUM, VIRTIO_NET_F_CTRL_VQ, VIRTIO_NET_F_GUEST_CSUM, VIRTIO_NET_F_GUEST_ECN, + VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, VIRTIO_NET_F_GUEST_UFO, + VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_TSO6, VIRTIO_NET_F_HOST_UFO, + VIRTIO_NET_F_MAC, VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_MTU, +}; +use virtio_bindings::bindings::virtio_ring::VIRTIO_RING_F_EVENT_IDX; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{ByteValued, GuestMemoryAtomic}; +use vm_migration::{ + protocol::MemoryRangeTable, Migratable, MigratableError, Pausable, Snapshot, Snapshottable, + Transportable, +}; +use vmm_sys_util::eventfd::EventFd; + +const DEFAULT_QUEUE_NUMBER: usize = 2; + +#[derive(Serialize, Deserialize)] +pub struct State { + pub avail_features: u64, + pub acked_features: u64, + pub config: VirtioNetConfig, + pub acked_protocol_features: u64, + pub vu_num_queues: usize, +} + +struct BackendReqHandler {} +impl VhostUserFrontendReqHandler for BackendReqHandler {} + +pub struct Net { + common: VirtioCommon, + vu_common: VhostUserCommon, + id: String, + config: VirtioNetConfig, + guest_memory: Option>, + ctrl_queue_epoll_thread: Option>, + epoll_thread: Option>, + seccomp_action: SeccompAction, + exit_evt: EventFd, + iommu: bool, +} + +impl Net { + /// Create a new vhost-user-net device + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + mac_addr: MacAddr, + mtu: Option, + vu_cfg: VhostUserConfig, + server: bool, + seccomp_action: SeccompAction, + exit_evt: EventFd, + iommu: bool, + state: Option, + ) -> Result { + let mut num_queues = vu_cfg.num_queues; + + let mut vu = + VhostUserHandle::connect_vhost_user(server, &vu_cfg.socket, num_queues as u64, false)?; + + let (avail_features, acked_features, acked_protocol_features, vu_num_queues, config) = + if let Some(state) = state { + info!("Restoring vhost-user-net {}", id); + + // The backend acknowledged features must not contain + // VIRTIO_NET_F_MAC since we don't expect the backend + // to handle it. + let backend_acked_features = state.acked_features & !(1 << VIRTIO_NET_F_MAC); + + vu.set_protocol_features_vhost_user( + backend_acked_features, + state.acked_protocol_features, + )?; + + // If the control queue feature has been negotiated, let's + // increase the number of queues. + if state.acked_features & (1 << VIRTIO_NET_F_CTRL_VQ) != 0 { + num_queues += 1; + } + + ( + state.avail_features, + state.acked_features, + state.acked_protocol_features, + state.vu_num_queues, + state.config, + ) + } else { + // Filling device and vring features VMM supports. + let mut avail_features = 1 << VIRTIO_NET_F_CSUM + | 1 << VIRTIO_NET_F_GUEST_CSUM + | 1 << VIRTIO_NET_F_GUEST_TSO4 + | 1 << VIRTIO_NET_F_GUEST_TSO6 + | 1 << VIRTIO_NET_F_GUEST_ECN + | 1 << VIRTIO_NET_F_GUEST_UFO + | 1 << VIRTIO_NET_F_HOST_TSO4 + | 1 << VIRTIO_NET_F_HOST_TSO6 + | 1 << VIRTIO_NET_F_HOST_ECN + | 1 << VIRTIO_NET_F_HOST_UFO + | 1 << VIRTIO_NET_F_MRG_RXBUF + | 1 << VIRTIO_NET_F_CTRL_VQ + | 1 << VIRTIO_F_RING_EVENT_IDX + | 1 << VIRTIO_F_VERSION_1 + | VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits(); + + if mtu.is_some() { + avail_features |= 1u64 << VIRTIO_NET_F_MTU; + } + + let mut config = VirtioNetConfig::default(); + build_net_config_space(&mut config, mac_addr, num_queues, mtu, &mut avail_features); + + let avail_protocol_features = VhostUserProtocolFeatures::MQ + | VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS + | VhostUserProtocolFeatures::REPLY_ACK + | VhostUserProtocolFeatures::INFLIGHT_SHMFD + | VhostUserProtocolFeatures::LOG_SHMFD; + + let (mut acked_features, acked_protocol_features) = + vu.negotiate_features_vhost_user(avail_features, avail_protocol_features)?; + + let backend_num_queues = + if acked_protocol_features & VhostUserProtocolFeatures::MQ.bits() != 0 { + vu.socket_handle() + .get_queue_num() + .map_err(Error::VhostUserGetQueueMaxNum)? + as usize + } else { + DEFAULT_QUEUE_NUMBER + }; + + if num_queues > backend_num_queues { + error!("vhost-user-net requested too many queues ({}) since the backend only supports {}\n", + num_queues, backend_num_queues); + return Err(Error::BadQueueNum); + } + + // If the control queue feature has been negotiated, let's increase + // the number of queues. + let vu_num_queues = num_queues; + if acked_features & (1 << VIRTIO_NET_F_CTRL_VQ) != 0 { + num_queues += 1; + } + + // Make sure the virtio feature to set the MAC address is exposed to + // the guest, even if it hasn't been negotiated with the backend. + acked_features |= 1 << VIRTIO_NET_F_MAC; + + ( + acked_features, + // If part of the available features that have been acked, + // the PROTOCOL_FEATURES bit must be already set through + // the VIRTIO acked features as we know the guest would + // never ack it, thus the feature would be lost. + acked_features & VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits(), + acked_protocol_features, + vu_num_queues, + config, + ) + }; + + Ok(Net { + id, + common: VirtioCommon { + device_type: VirtioDeviceType::Net as u32, + queue_sizes: vec![vu_cfg.queue_size; num_queues], + avail_features, + acked_features, + paused_sync: Some(Arc::new(Barrier::new(2))), + min_queues: DEFAULT_QUEUE_NUMBER as u16, + ..Default::default() + }, + vu_common: VhostUserCommon { + vu: Some(Arc::new(Mutex::new(vu))), + acked_protocol_features, + socket_path: vu_cfg.socket, + vu_num_queues, + server, + ..Default::default() + }, + config, + guest_memory: None, + ctrl_queue_epoll_thread: None, + epoll_thread: None, + seccomp_action, + exit_evt, + iommu, + }) + } + + fn state(&self) -> State { + State { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + config: self.config, + acked_protocol_features: self.vu_common.acked_protocol_features, + vu_num_queues: self.vu_common.vu_num_queues, + } + } +} + +impl Drop for Net { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + if let Err(e) = kill_evt.write(1) { + error!("failed to kill vhost-user-net: {:?}", e); + } + } + } +} + +impl VirtioDevice for Net { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + let mut features = self.common.avail_features; + if self.iommu { + features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM; + } + features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + self.read_config_from_slice(self.config.as_slice(), offset, data); + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + self.guest_memory = Some(mem.clone()); + + let num_queues = queues.len(); + let event_idx = self.common.feature_acked(VIRTIO_RING_F_EVENT_IDX.into()); + if self.common.feature_acked(VIRTIO_NET_F_CTRL_VQ.into()) && num_queues % 2 != 0 { + let ctrl_queue_index = num_queues - 1; + let (_, mut ctrl_queue, ctrl_queue_evt) = queues.remove(ctrl_queue_index); + + ctrl_queue.set_event_idx(event_idx); + + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let mut ctrl_handler = NetCtrlEpollHandler { + mem: mem.clone(), + kill_evt, + pause_evt, + ctrl_q: CtrlQueue::new(Vec::new()), + queue: ctrl_queue, + queue_evt: ctrl_queue_evt, + access_platform: None, + interrupt_cb: interrupt_cb.clone(), + queue_index: ctrl_queue_index as u16, + }; + + let paused = self.common.paused.clone(); + // Let's update the barrier as we need 1 for the control queue + // thread + 1 for the common vhost-user thread + 1 for the main + // thread signalling the pause. + self.common.paused_sync = Some(Arc::new(Barrier::new(3))); + let paused_sync = self.common.paused_sync.clone(); + + let mut epoll_threads = Vec::new(); + spawn_virtio_thread( + &format!("{}_ctrl", &self.id), + &self.seccomp_action, + Thread::VirtioVhostNetCtl, + &mut epoll_threads, + &self.exit_evt, + move || ctrl_handler.run_ctrl(paused, paused_sync.unwrap()), + )?; + self.ctrl_queue_epoll_thread = Some(epoll_threads.remove(0)); + } + + let backend_req_handler: Option> = None; + + // The backend acknowledged features must not contain VIRTIO_NET_F_MAC + // since we don't expect the backend to handle it. + let backend_acked_features = self.common.acked_features & !(1 << VIRTIO_NET_F_MAC); + + // Run a dedicated thread for handling potential reconnections with + // the backend. + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let mut handler = self.vu_common.activate( + mem, + queues, + interrupt_cb, + backend_acked_features, + backend_req_handler, + kill_evt, + pause_evt, + )?; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + + let mut epoll_threads = Vec::new(); + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioVhostNet, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + self.epoll_thread = Some(epoll_threads.remove(0)); + + Ok(()) + } + + fn reset(&mut self) -> Option> { + // We first must resume the virtio thread if it was paused. + if self.common.pause_evt.take().is_some() { + self.common.resume().ok()?; + } + + if let Some(vu) = &self.vu_common.vu { + if let Err(e) = vu.lock().unwrap().reset_vhost_user() { + error!("Failed to reset vhost-user daemon: {:?}", e); + return None; + } + } + + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + + event!("virtio-device", "reset", "id", &self.id); + + // Return the interrupt + Some(self.common.interrupt_cb.take().unwrap()) + } + + fn shutdown(&mut self) { + self.vu_common.shutdown(); + } + + fn add_memory_region( + &mut self, + region: &Arc, + ) -> std::result::Result<(), crate::Error> { + self.vu_common.add_memory_region(&self.guest_memory, region) + } +} + +impl Pausable for Net { + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.vu_common.pause()?; + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume()?; + + if let Some(epoll_thread) = &self.epoll_thread { + epoll_thread.thread().unpark(); + } + + if let Some(ctrl_queue_epoll_thread) = &self.ctrl_queue_epoll_thread { + ctrl_queue_epoll_thread.thread().unpark(); + } + + self.vu_common.resume() + } +} + +impl Snapshottable for Net { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + self.vu_common.snapshot(&self.id(), &self.state()) + } +} +impl Transportable for Net {} + +impl Migratable for Net { + fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common.start_dirty_log(&self.guest_memory) + } + + fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common.stop_dirty_log() + } + + fn dirty_log(&mut self) -> std::result::Result { + self.vu_common.dirty_log(&self.guest_memory) + } + + fn start_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common.start_migration() + } + + fn complete_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.vu_common + .complete_migration(self.common.kill_evt.take()) + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vhost_user/vu_common_ctrl.rs b/vendor/cube-hypervisor/virtio-devices/src/vhost_user/vu_common_ctrl.rs new file mode 100644 index 0000000..066994e --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vhost_user/vu_common_ctrl.rs @@ -0,0 +1,594 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use super::{Error, Result}; +use crate::vhost_user::Inflight; +use crate::{ + get_host_address_range, GuestMemoryMmap, GuestRegionMmap, MmapRegion, VirtioInterrupt, + VirtioInterruptType, +}; +use std::convert::TryInto; +use std::ffi; +use std::fs::File; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::os::unix::net::UnixListener; +use std::sync::atomic::Ordering; +use std::sync::Arc; +use std::thread::sleep; +use std::time::{Duration, Instant}; +use std::vec::Vec; +use vhost::vhost_kern::vhost_binding::{VHOST_F_LOG_ALL, VHOST_VRING_F_LOG}; +use vhost::vhost_user::message::{ + VhostUserHeaderFlag, VhostUserInflight, VhostUserProtocolFeatures, VhostUserVirtioFeatures, +}; +use vhost::vhost_user::{ + Frontend, FrontendReqHandler, VhostUserFrontend, VhostUserFrontendReqHandler, +}; +use vhost::{VhostBackend, VhostUserDirtyLogRegion, VhostUserMemoryRegionInfo, VringConfigData}; +use virtio_queue::{Descriptor, Queue, QueueT}; +use vm_memory::{ + Address, Error as MmapError, FileOffset, GuestAddress, GuestMemory, GuestMemoryRegion, +}; +use vm_migration::protocol::MemoryRangeTable; +use vmm_sys_util::eventfd::EventFd; + +// Size of a dirty page for vhost-user. +const VHOST_LOG_PAGE: u64 = 0x1000; + +#[derive(Debug, Clone)] +pub struct VhostUserConfig { + pub socket: String, + pub num_queues: usize, + pub queue_size: u16, +} + +#[derive(Clone)] +struct VringInfo { + config_data: VringConfigData, + used_guest_addr: u64, +} + +#[derive(Clone)] +pub struct VhostUserHandle { + vu: Frontend, + ready: bool, + supports_migration: bool, + shm_log: Option>, + acked_features: u64, + vrings_info: Option>, + queue_indexes: Vec, +} + +impl VhostUserHandle { + pub fn update_mem_table(&mut self, mem: &GuestMemoryMmap) -> Result<()> { + let mut regions: Vec = Vec::new(); + for region in mem.iter() { + let (mmap_handle, mmap_offset) = match region.file_offset() { + Some(_file_offset) => (_file_offset.file().as_raw_fd(), _file_offset.start()), + None => return Err(Error::VhostUserMemoryRegion(MmapError::NoMemoryRegion)), + }; + + let vhost_user_net_reg = VhostUserMemoryRegionInfo { + guest_phys_addr: region.start_addr().raw_value(), + memory_size: region.len(), + userspace_addr: region.as_ptr() as u64, + mmap_offset, + mmap_handle, + }; + + regions.push(vhost_user_net_reg); + } + + self.vu + .set_mem_table(regions.as_slice()) + .map_err(Error::VhostUserSetMemTable)?; + + Ok(()) + } + + pub fn add_memory_region(&mut self, region: &Arc) -> Result<()> { + let (mmap_handle, mmap_offset) = match region.file_offset() { + Some(file_offset) => (file_offset.file().as_raw_fd(), file_offset.start()), + None => return Err(Error::MissingRegionFd), + }; + + let region = VhostUserMemoryRegionInfo { + guest_phys_addr: region.start_addr().raw_value(), + memory_size: region.len(), + userspace_addr: region.as_ptr() as u64, + mmap_offset, + mmap_handle, + }; + + self.vu + .add_mem_region(®ion) + .map_err(Error::VhostUserAddMemReg) + } + + pub fn negotiate_features_vhost_user( + &mut self, + avail_features: u64, + avail_protocol_features: VhostUserProtocolFeatures, + ) -> Result<(u64, u64)> { + // Set vhost-user owner. + self.vu.set_owner().map_err(Error::VhostUserSetOwner)?; + + // Get features from backend, do negotiation to get a feature collection which + // both VMM and backend support. + let backend_features = self + .vu + .get_features() + .map_err(Error::VhostUserGetFeatures)?; + let acked_features = avail_features & backend_features; + + let acked_protocol_features = + if acked_features & VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits() != 0 { + let backend_protocol_features = self + .vu + .get_protocol_features() + .map_err(Error::VhostUserGetProtocolFeatures)?; + + let acked_protocol_features = avail_protocol_features & backend_protocol_features; + + self.vu + .set_protocol_features(acked_protocol_features) + .map_err(Error::VhostUserSetProtocolFeatures)?; + + acked_protocol_features + } else { + VhostUserProtocolFeatures::empty() + }; + + if avail_protocol_features.contains(VhostUserProtocolFeatures::REPLY_ACK) + && acked_protocol_features.contains(VhostUserProtocolFeatures::REPLY_ACK) + { + self.vu.set_hdr_flags(VhostUserHeaderFlag::NEED_REPLY); + } + + self.update_supports_migration(acked_features, acked_protocol_features.bits()); + + Ok((acked_features, acked_protocol_features.bits())) + } + + #[allow(clippy::too_many_arguments)] + pub fn setup_vhost_user( + &mut self, + mem: &GuestMemoryMmap, + queues: Vec<(usize, Queue, EventFd)>, + virtio_interrupt: &Arc, + acked_features: u64, + backend_req_handler: &Option>, + inflight: Option<&mut Inflight>, + ) -> Result<()> { + self.vu + .set_features(acked_features) + .map_err(Error::VhostUserSetFeatures)?; + + // Update internal value after it's been sent to the backend. + self.acked_features = acked_features; + + // Let's first provide the memory table to the backend. + self.update_mem_table(mem)?; + + // Send set_vring_num here, since it could tell backends, like SPDK, + // how many virt queues to be handled, which backend required to know + // at early stage. + for (queue_index, queue, _) in queues.iter() { + self.vu + .set_vring_num(*queue_index, queue.size()) + .map_err(Error::VhostUserSetVringNum)?; + } + + // Setup for inflight I/O tracking shared memory. + if let Some(inflight) = inflight { + if inflight.fd.is_none() { + let inflight_req_info = VhostUserInflight { + mmap_size: 0, + mmap_offset: 0, + num_queues: queues.len() as u16, + queue_size: queues[0].1.size(), + }; + let (info, fd) = self + .vu + .get_inflight_fd(&inflight_req_info) + .map_err(Error::VhostUserGetInflight)?; + inflight.info = info; + inflight.fd = Some(fd); + } + // Unwrapping the inflight fd is safe here since we know it can't be None. + self.vu + .set_inflight_fd(&inflight.info, inflight.fd.as_ref().unwrap().as_raw_fd()) + .map_err(Error::VhostUserSetInflight)?; + } + + let mut vrings_info = Vec::new(); + for (queue_index, queue, queue_evt) in queues.iter() { + let actual_size: usize = queue.size().try_into().unwrap(); + + let config_data = VringConfigData { + queue_max_size: queue.max_size(), + queue_size: queue.size(), + flags: 0u32, + desc_table_addr: get_host_address_range( + mem, + GuestAddress(queue.desc_table()), + actual_size * std::mem::size_of::(), + ) + .ok_or(Error::DescriptorTableAddress)? as u64, + // The used ring is {flags: u16; idx: u16; virtq_used_elem [{id: u16, len: u16}; actual_size]}, + // i.e. 4 + (4 + 4) * actual_size. + used_ring_addr: get_host_address_range( + mem, + GuestAddress(queue.used_ring()), + 4 + actual_size * 8, + ) + .ok_or(Error::UsedAddress)? as u64, + // The used ring is {flags: u16; idx: u16; elem [u16; actual_size]}, + // i.e. 4 + (2) * actual_size. + avail_ring_addr: get_host_address_range( + mem, + GuestAddress(queue.avail_ring()), + 4 + actual_size * 2, + ) + .ok_or(Error::AvailAddress)? as u64, + log_addr: None, + }; + + vrings_info.push(VringInfo { + config_data, + used_guest_addr: queue.used_ring(), + }); + + self.vu + .set_vring_addr(*queue_index, &config_data) + .map_err(Error::VhostUserSetVringAddr)?; + self.vu + .set_vring_base( + *queue_index, + queue + .avail_idx(mem, Ordering::Acquire) + .map_err(Error::GetAvailableIndex)? + .0, + ) + .map_err(Error::VhostUserSetVringBase)?; + + if let Some(eventfd) = + virtio_interrupt.notifier(VirtioInterruptType::Queue(*queue_index as u16)) + { + self.vu + .set_vring_call(*queue_index, &eventfd) + .map_err(Error::VhostUserSetVringCall)?; + } + + self.vu + .set_vring_kick(*queue_index, queue_evt) + .map_err(Error::VhostUserSetVringKick)?; + + self.queue_indexes.push(*queue_index); + } + + self.enable_vhost_user_vrings(self.queue_indexes.clone(), true)?; + + if let Some(backend_req_handler) = backend_req_handler { + self.vu + .set_backend_request_fd(&backend_req_handler.get_tx_raw_fd()) + .map_err(Error::VhostUserSetBackendRequestFd)?; + } + + self.vrings_info = Some(vrings_info); + self.ready = true; + + Ok(()) + } + + fn enable_vhost_user_vrings(&mut self, queue_indexes: Vec, enable: bool) -> Result<()> { + for queue_index in queue_indexes { + self.vu + .set_vring_enable(queue_index, enable) + .map_err(Error::VhostUserSetVringEnable)?; + } + + Ok(()) + } + + pub fn reset_vhost_user(&mut self) -> Result<()> { + for queue_index in self.queue_indexes.drain(..) { + self.vu + .set_vring_enable(queue_index, false) + .map_err(Error::VhostUserSetVringEnable)?; + + let _ = self + .vu + .get_vring_base(queue_index) + .map_err(Error::VhostUserGetVringBase)?; + } + + Ok(()) + } + + pub fn set_protocol_features_vhost_user( + &mut self, + acked_features: u64, + acked_protocol_features: u64, + ) -> Result<()> { + self.vu.set_owner().map_err(Error::VhostUserSetOwner)?; + self.vu + .get_features() + .map_err(Error::VhostUserGetFeatures)?; + + if acked_features & VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits() != 0 { + if let Some(acked_protocol_features) = + VhostUserProtocolFeatures::from_bits(acked_protocol_features) + { + self.vu + .set_protocol_features(acked_protocol_features) + .map_err(Error::VhostUserSetProtocolFeatures)?; + + if acked_protocol_features.contains(VhostUserProtocolFeatures::REPLY_ACK) { + self.vu.set_hdr_flags(VhostUserHeaderFlag::NEED_REPLY); + } + } + } + + self.update_supports_migration(acked_features, acked_protocol_features); + + Ok(()) + } + + #[allow(clippy::too_many_arguments)] + pub fn reinitialize_vhost_user( + &mut self, + mem: &GuestMemoryMmap, + queues: Vec<(usize, Queue, EventFd)>, + virtio_interrupt: &Arc, + acked_features: u64, + acked_protocol_features: u64, + backend_req_handler: &Option>, + inflight: Option<&mut Inflight>, + ) -> Result<()> { + self.set_protocol_features_vhost_user(acked_features, acked_protocol_features)?; + + self.setup_vhost_user( + mem, + queues, + virtio_interrupt, + acked_features, + backend_req_handler, + inflight, + ) + } + + pub fn connect_vhost_user( + server: bool, + socket_path: &str, + num_queues: u64, + unlink_socket: bool, + ) -> Result { + if server { + if unlink_socket { + std::fs::remove_file(socket_path).map_err(Error::RemoveSocketPath)?; + } + + info!("Binding vhost-user listener..."); + let listener = UnixListener::bind(socket_path).map_err(Error::BindSocket)?; + info!("Waiting for incoming vhost-user connection..."); + let (stream, _) = listener.accept().map_err(Error::AcceptConnection)?; + + Ok(VhostUserHandle { + vu: Frontend::from_stream(stream, num_queues), + ready: false, + supports_migration: false, + shm_log: None, + acked_features: 0, + vrings_info: None, + queue_indexes: Vec::new(), + }) + } else { + let now = Instant::now(); + + // Retry connecting for a full minute + let err = loop { + let err = match Frontend::connect(socket_path, num_queues) { + Ok(m) => { + return Ok(VhostUserHandle { + vu: m, + ready: false, + supports_migration: false, + shm_log: None, + acked_features: 0, + vrings_info: None, + queue_indexes: Vec::new(), + }) + } + Err(e) => e, + }; + sleep(Duration::from_millis(100)); + + if now.elapsed().as_secs() >= 60 { + break err; + } + }; + + error!( + "Failed connecting the backend after trying for 1 minute: {:?}", + err + ); + Err(Error::VhostUserConnect) + } + } + + pub fn socket_handle(&mut self) -> &mut Frontend { + &mut self.vu + } + + pub fn pause_vhost_user(&mut self) -> Result<()> { + if self.ready { + self.enable_vhost_user_vrings(self.queue_indexes.clone(), false)?; + } + + Ok(()) + } + + pub fn resume_vhost_user(&mut self) -> Result<()> { + if self.ready { + self.enable_vhost_user_vrings(self.queue_indexes.clone(), true)?; + } + + Ok(()) + } + + fn update_supports_migration(&mut self, acked_features: u64, acked_protocol_features: u64) { + if (acked_features & u64::from(vhost::vhost_kern::vhost_binding::VHOST_F_LOG_ALL) != 0) + && (acked_protocol_features & VhostUserProtocolFeatures::LOG_SHMFD.bits() != 0) + { + self.supports_migration = true; + } + } + + fn update_log_base(&mut self, last_ram_addr: u64) -> Result>> { + // Create the memfd + let fd = memfd_create( + &ffi::CString::new("vhost_user_dirty_log").unwrap(), + libc::MFD_CLOEXEC | libc::MFD_ALLOW_SEALING, + ) + .map_err(Error::MemfdCreate)?; + + // Safe because we checked the file descriptor is valid + let file = unsafe { File::from_raw_fd(fd) }; + // The size of the memory mapping corresponds to the size of a bitmap + // covering all guest pages for addresses from 0 to the last physical + // address in guest RAM. + // A page is always 4kiB from a vhost-user perspective, and each bit is + // a page. That's how we can compute mmap_size from the last address. + let mmap_size = (last_ram_addr / (VHOST_LOG_PAGE * 8)) + 1; + let mmap_handle = file.as_raw_fd(); + + // Set shm_log region size + file.set_len(mmap_size).map_err(Error::SetFileSize)?; + + // Set the seals + let res = unsafe { + libc::fcntl( + file.as_raw_fd(), + libc::F_ADD_SEALS, + libc::F_SEAL_GROW | libc::F_SEAL_SHRINK | libc::F_SEAL_SEAL, + ) + }; + if res < 0 { + return Err(Error::SetSeals(std::io::Error::last_os_error())); + } + + // Mmap shm_log region + let region = MmapRegion::build( + Some(FileOffset::new(file, 0)), + mmap_size as usize, + libc::PROT_READ | libc::PROT_WRITE, + libc::MAP_SHARED, + ) + .map_err(Error::NewMmapRegion)?; + + // Make sure we hold onto the region to prevent the mapping from being + // released. + let old_region = self.shm_log.replace(Arc::new(region)); + + // Send the shm_log fd over to the backend + let log = VhostUserDirtyLogRegion { + mmap_size, + mmap_offset: 0, + mmap_handle, + }; + self.vu + .set_log_base(0, Some(log)) + .map_err(Error::VhostUserSetLogBase)?; + + Ok(old_region) + } + + fn set_vring_logging(&mut self, enable: bool) -> Result<()> { + if let Some(vrings_info) = &self.vrings_info { + for (i, vring_info) in vrings_info.iter().enumerate() { + let mut config_data = vring_info.config_data; + config_data.flags = if enable { 1 << VHOST_VRING_F_LOG } else { 0 }; + config_data.log_addr = if enable { + Some(vring_info.used_guest_addr) + } else { + None + }; + + self.vu + .set_vring_addr(i, &config_data) + .map_err(Error::VhostUserSetVringAddr)?; + } + } + + Ok(()) + } + + pub fn start_dirty_log(&mut self, last_ram_addr: u64) -> Result<()> { + if !self.supports_migration { + return Err(Error::MigrationNotSupported); + } + + // Set the shm log region + self.update_log_base(last_ram_addr)?; + + // Enable VHOST_F_LOG_ALL feature + let features = self.acked_features | (1 << VHOST_F_LOG_ALL); + self.vu + .set_features(features) + .map_err(Error::VhostUserSetFeatures)?; + + // Enable dirty page logging of used ring for all queues + self.set_vring_logging(true) + } + + pub fn stop_dirty_log(&mut self) -> Result<()> { + if !self.supports_migration { + return Err(Error::MigrationNotSupported); + } + + // Disable dirty page logging of used ring for all queues + self.set_vring_logging(false)?; + + // Disable VHOST_F_LOG_ALL feature + self.vu + .set_features(self.acked_features) + .map_err(Error::VhostUserSetFeatures)?; + + // This is important here since the log region goes out of scope, + // invoking the Drop trait, hence unmapping the memory. + self.shm_log = None; + + Ok(()) + } + + pub fn dirty_log(&mut self, last_ram_addr: u64) -> Result { + // The log region is updated by creating a new region that is sent to + // the backend. This ensures the backend stops logging to the previous + // region. The previous region is returned and processed to create the + // bitmap representing the dirty pages. + if let Some(region) = self.update_log_base(last_ram_addr)? { + // Be careful with the size, as it was based on u8, meaning we must + // divide it by 8. + let len = region.size() / 8; + let bitmap = unsafe { + // Cast the pointer to u64 + let ptr = region.as_ptr() as *const u64; + std::slice::from_raw_parts(ptr, len).to_vec() + }; + Ok(MemoryRangeTable::from_bitmap(bitmap, 0, 4096)) + } else { + Err(Error::MissingShmLogRegion) + } + } +} + +fn memfd_create(name: &ffi::CStr, flags: u32) -> std::result::Result { + let res = unsafe { libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags) }; + + if res < 0 { + Err(std::io::Error::last_os_error()) + } else { + Ok(res as RawFd) + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/connection.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/connection.rs new file mode 100644 index 0000000..71c0916 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/connection.rs @@ -0,0 +1,1254 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +/// The main job of `VsockConnection` is to forward data traffic, back and forth, between a +/// guest-side AF_VSOCK socket and a host-side generic `Read + Write + AsRawFd` stream, while +/// also managing its internal state. +/// To that end, `VsockConnection` implements: +/// - `VsockChannel` for: +/// - moving data from the host stream to a guest-provided RX buffer, via `recv_pkt()`; and +/// - moving data from a guest-provided TX buffer to the host stream, via `send_pkt()`; and +/// - updating its internal state, by absorbing control packets (anything other than +/// VSOCK_OP_RW). +/// - `VsockEpollListener` for getting notified about the availability of data or free buffer +/// space at the host stream. +/// +/// Note: there is a certain asymmetry to the RX and TX data flows: +/// - RX transfers do not need any data buffering, since data is read straight from the +/// host stream and into the guest-provided RX buffer; +/// - TX transfers may require some data to be buffered by `VsockConnection`, if the host +/// peer can't keep up with reading the data that we're writing. This is because, once +/// the guest driver provides some data in a virtio TX buffer, the vsock device must +/// consume it. If that data can't be forwarded straight to the host stream, we'll +/// have to store it in a buffer (and flush it at a later time). Vsock flow control +/// ensures that our TX buffer doesn't overflow. +/// +// The code in this file is best read with a fresh memory of the vsock protocol inner-workings. +// To help with that, here is a +// +// Short primer on the vsock protocol +// ---------------------------------- +// +// 1. Establishing a connection +// A vsock connection is considered established after a two-way handshake: +// - the initiating peer sends a connection request packet (`hdr.op` == VSOCK_OP_REQUEST); +// then +// - the listening peer sends back a connection response packet (`hdr.op` == +// VSOCK_OP_RESPONSE). +// +// 2. Terminating a connection +// When a peer wants to shut down an established connection, it sends a VSOCK_OP_SHUTDOWN +// packet. Two header flags are used with VSOCK_OP_SHUTDOWN, indicating the sender's +// intention: +// - VSOCK_FLAGS_SHUTDOWN_RCV: the sender will receive no more data for this connection; and +// - VSOCK_FLAGS_SHUTDOWN_SEND: the sender will send no more data for this connection. +// After a shutdown packet, the receiving peer will have some protocol-undefined time to +// flush its buffers, and then forcefully terminate the connection by sending back an RST +// packet. If the shutdown-initiating peer doesn't receive this RST packet during a timeout +// period, it will send one itself, thus terminating the connection. +// Note: a peer can send more than one VSOCK_OP_SHUTDOWN packets. However, read/write +// indications cannot be undone. E.g. once a "no-more-sending" promise was made, it +// cannot be taken back. That is, `hdr.flags` will be ORed between subsequent +// VSOCK_OP_SHUTDOWN packets. +// +// 3. Flow control +// Before sending a data packet (VSOCK_OP_RW), the sender must make sure that the receiver +// has enough free buffer space to store that data. If this condition is not respected, the +// receiving peer's behavior is undefined. In this implementation, we forcefully terminate +// the connection by sending back a VSOCK_OP_RST packet. +// Note: all buffer space information is computed and stored on a per-connection basis. +// Peers keep each other informed about the free buffer space they have by filling in two +// packet header members with each packet they send: +// - `hdr.buf_alloc`: the total buffer space the peer has allocated for receiving data; and +// - `hdr.fwd_cnt`: the total number of bytes the peer has successfully flushed out of its +// buffer. +// One can figure out how much space its peer has available in its buffer by inspecting the +// difference between how much it has sent to the peer and how much the peer has flushed out +// (i.e. "forwarded", in the vsock spec terminology): +// `peer_free = peer_buf_alloc - (total_bytes_sent_to_peer - peer_fwd_cnt)`. +// Note: the above requires that peers constantly keep each other informed on their buffer +// space situation. However, since there are no receipt acknowledgement packets +// defined for the vsock protocol, packet flow can often be unidirectional (just one +// peer sending data to another), so the sender's information about the receiver's +// buffer space can get quickly outdated. The vsock protocol defines two solutions to +// this problem: +// 1. The sender can explicitly ask for a buffer space (i.e. "credit") update from its +// peer, via a VSOCK_OP_CREDIT_REQUEST packet, to which it will get a +// VSOCK_OP_CREDIT_UPDATE response (or any response will do, really, since credit +// information must be included in any packet); +// 2. The receiver can be proactive, and send VSOCK_OP_CREDIT_UPDATE packet, whenever +// it thinks its peer's information is out of date. +// Our implementation uses the proactive approach. +// +use std::io::{ErrorKind, Read, Write}; +use std::num::Wrapping; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::time::{Duration, Instant}; + +use super::super::defs::uapi; +use super::super::packet::VsockPacket; +use super::super::{Result as VsockResult, VsockChannel, VsockEpollListener, VsockError}; +use super::defs; +use super::txbuf::TxBuf; +use super::{ConnState, Error, PendingRx, PendingRxSet, Result}; + +/// A self-managing connection object, that handles communication between a guest-side AF_VSOCK +/// socket and a host-side `Read + Write + AsRawFd` stream. +/// +pub struct VsockConnection { + /// The current connection state. + state: ConnState, + /// The local CID. Most of the time this will be the constant `2` (the vsock host CID). + local_cid: u64, + /// The peer (guest) CID. + peer_cid: u64, + /// The local (host) port. + local_port: u32, + /// The peer (guest) port. + peer_port: u32, + /// The (connected) host-side stream. + stream: S, + /// The TX buffer for this connection. + tx_buf: TxBuf, + /// Total number of bytes that have been successfully written to `self.stream`, either + /// directly, or flushed from `self.tx_buf`. + fwd_cnt: Wrapping, + /// The amount of buffer space that the peer (guest) has allocated for this connection. + peer_buf_alloc: u32, + /// The total number of bytes that the peer has forwarded away. + peer_fwd_cnt: Wrapping, + /// The total number of bytes sent to the peer (guest vsock driver) + rx_cnt: Wrapping, + /// Our `self.fwd_cnt`, as last sent to the peer. This is used to provide proactive credit + /// updates, and let the peer know it's OK to send more data. + last_fwd_cnt_to_peer: Wrapping, + /// The set of pending RX packet indications that `recv_pkt()` will use to fill in a + /// packet for the peer (guest). + pending_rx: PendingRxSet, + /// Instant when this connection should be scheduled for immediate termination, due to some + /// timeout condition having been fulfilled. + expiry: Option, +} + +impl VsockChannel for VsockConnection +where + S: Read + Write + AsRawFd, +{ + /// Fill in a vsock packet, to be delivered to our peer (the guest driver). + /// + /// As per the `VsockChannel` trait, this should only be called when there is data to be + /// fetched from the channel (i.e. `has_pending_rx()` is true). Otherwise, it will error + /// out with `VsockError::NoData`. + /// Pending RX indications are set by other mutable actions performed on the channel. For + /// instance, `send_pkt()` could set an Rst indication, if called with a VSOCK_OP_SHUTDOWN + /// packet, or `notify()` could set a Rw indication (a data packet can be fetched from the + /// channel), if data was ready to be read from the host stream. + /// + /// Returns: + /// - `Ok(())`: the packet has been successfully filled in and is ready for delivery; + /// - `Err(VsockError::NoData)`: there was no data available with which to fill in the + /// packet; + /// - `Err(VsockError::PktBufMissing)`: the packet would've been filled in with data, but + /// it is missing the data buffer. + /// + fn recv_pkt(&mut self, pkt: &mut VsockPacket) -> VsockResult<()> { + // Perform some generic initialization that is the same for any packet operation (e.g. + // source, destination, credit, etc). + self.init_pkt(pkt); + + // If forceful termination is pending, there's no point in checking for anything else. + // It's dead, Jim. + if self.pending_rx.remove(PendingRx::Rst) { + pkt.set_op(uapi::VSOCK_OP_RST); + return Ok(()); + } + + // Next up: if we're due a connection confirmation, that's all we need to know to fill + // in this packet. + if self.pending_rx.remove(PendingRx::Response) { + self.state = ConnState::Established; + pkt.set_op(uapi::VSOCK_OP_RESPONSE); + return Ok(()); + } + + // Same thing goes for locally-initiated connections that need to yield a connection + // request. + if self.pending_rx.remove(PendingRx::Request) { + self.expiry = + Some(Instant::now() + Duration::from_millis(defs::CONN_REQUEST_TIMEOUT_MS)); + pkt.set_op(uapi::VSOCK_OP_REQUEST); + return Ok(()); + } + + if self.pending_rx.remove(PendingRx::Rw) { + // We're due to produce a data packet, by reading the data from the host-side + // Unix socket. + + match self.state { + // A data packet is only valid for established connections, and connections for + // which our peer has initiated a graceful shutdown, but can still receive data. + ConnState::Established | ConnState::PeerClosed(false, _) => (), + _ => { + // Any other connection state is invalid at this point, and we need to kill it + // with fire. + pkt.set_op(uapi::VSOCK_OP_RST); + return Ok(()); + } + } + + // Oh wait, before we start bringing in the big data, can our peer handle receiving so + // much bytes goodness? + if self.need_credit_update_from_peer() { + self.last_fwd_cnt_to_peer = self.fwd_cnt; + pkt.set_op(uapi::VSOCK_OP_CREDIT_REQUEST); + return Ok(()); + } + + let buf = pkt.buf_mut().ok_or(VsockError::PktBufMissing)?; + + // The maximum amount of data we can read in is limited by both the RX buffer size and + // the peer available buffer space. + let max_len = std::cmp::min(buf.len(), self.peer_avail_credit()); + + // Read data from the stream straight to the RX buffer, for maximum throughput. + match self.stream.read(&mut buf[..max_len]) { + Ok(read_cnt) => { + if read_cnt == 0 { + // A 0-length read means the host stream was closed down. In that case, + // we'll ask our peer to shut down the connection. We can neither send nor + // receive any more data. + self.state = ConnState::LocalClosed; + self.expiry = Some( + Instant::now() + Duration::from_millis(defs::CONN_SHUTDOWN_TIMEOUT_MS), + ); + pkt.set_op(uapi::VSOCK_OP_SHUTDOWN) + .set_flag(uapi::VSOCK_FLAGS_SHUTDOWN_RCV) + .set_flag(uapi::VSOCK_FLAGS_SHUTDOWN_SEND); + } else { + // On a successful data read, we fill in the packet with the RW op, and + // length of the read data. + pkt.set_op(uapi::VSOCK_OP_RW).set_len(read_cnt as u32); + } + self.rx_cnt += Wrapping(pkt.len()); + self.last_fwd_cnt_to_peer = self.fwd_cnt; + return Ok(()); + } + Err(err) if err.kind() == ErrorKind::WouldBlock => { + // This shouldn't actually happen (receiving EWOULDBLOCK after EPOLLIN), but + // apparently it does, so we need to handle it gracefully. + warn!( + "vsock: unexpected EWOULDBLOCK while reading from backing stream: \ + lp={}, pp={}, err={:?}", + self.local_port, self.peer_port, err + ); + } + Err(err) => { + // We are not expecting any other errors when reading from the underlying + // stream. If any show up, we'll immediately kill this connection. + error!( + "vsock: error reading from backing stream: lp={}, pp={}, err={:?}", + self.local_port, self.peer_port, err + ); + pkt.set_op(uapi::VSOCK_OP_RST); + self.last_fwd_cnt_to_peer = self.fwd_cnt; + return Ok(()); + } + }; + } + + // A credit update is basically a no-op, so we should only waste a perfectly fine RX + // buffer on it if we really have nothing else to say, hence we check for this RX + // indication last. + if self.pending_rx.remove(PendingRx::CreditUpdate) && !self.has_pending_rx() { + pkt.set_op(uapi::VSOCK_OP_CREDIT_UPDATE); + self.last_fwd_cnt_to_peer = self.fwd_cnt; + return Ok(()); + } + + // We've already checked for all conditions that would have produced a packet, so + // if we got to here, we don't know how to yield one. + Err(VsockError::NoData) + } + + /// Deliver a guest-generated packet to this connection. + /// + /// This forwards the data in RW packets to the host stream, and absorbs control packets, + /// using them to manage the internal connection state. + /// + /// Returns: + /// always `Ok(())`: the packet has been consumed; + /// + fn send_pkt(&mut self, pkt: &VsockPacket) -> VsockResult<()> { + // Update the peer credit information. + self.peer_buf_alloc = pkt.buf_alloc(); + self.peer_fwd_cnt = Wrapping(pkt.fwd_cnt()); + + match self.state { + // Most frequent case: this is an established connection that needs to forward some + // data to the host stream. Also works for a connection that has begun shutting + // down, but the peer still has some data to send. + ConnState::Established | ConnState::PeerClosed(_, false) + if pkt.op() == uapi::VSOCK_OP_RW => + { + if pkt.buf().is_none() { + info!( + "vsock: dropping empty data packet from guest (lp={}, pp={}", + self.local_port, self.peer_port + ); + return Ok(()); + } + + // Unwrapping here is safe, since we just checked `pkt.buf()` above. + let buf_slice = &pkt.buf().unwrap()[..(pkt.len() as usize)]; + if let Err(err) = self.send_bytes(buf_slice) { + // If we can't write to the host stream, that's an unrecoverable error, so + // we'll terminate this connection. + warn!( + "vsock: error writing to local stream (lp={}, pp={}): {:?}", + self.local_port, self.peer_port, err + ); + self.kill(); + return Ok(()); + } + + // We might've just consumed some data. If that's the case, we might need to + // update the peer on our buffer space situation, so that it can keep sending + // data packets our way. + if self.peer_needs_credit_update() { + self.pending_rx.insert(PendingRx::CreditUpdate); + } + } + + // Next up: receiving a response / confirmation for a host-initiated connection. + // We'll move to an Established state, and pass on the good news through the host + // stream. + ConnState::LocalInit if pkt.op() == uapi::VSOCK_OP_RESPONSE => { + self.expiry = None; + self.state = ConnState::Established; + } + + // The peer wants to shut down an established connection. If they have nothing + // more to send nor receive, and we don't have to wait to drain our TX buffer, we + // can schedule an RST packet (to terminate the connection on the next recv call). + // Otherwise, we'll arm the kill timer. + ConnState::Established if pkt.op() == uapi::VSOCK_OP_SHUTDOWN => { + let recv_off = pkt.flags() & uapi::VSOCK_FLAGS_SHUTDOWN_RCV != 0; + let send_off = pkt.flags() & uapi::VSOCK_FLAGS_SHUTDOWN_SEND != 0; + self.state = ConnState::PeerClosed(recv_off, send_off); + if recv_off && send_off { + if self.tx_buf.is_empty() { + self.pending_rx.insert(PendingRx::Rst); + } else { + self.expiry = Some( + Instant::now() + Duration::from_millis(defs::CONN_SHUTDOWN_TIMEOUT_MS), + ); + } + } + } + + // The peer wants to update a shutdown request, with more receive/send indications. + // The same logic as above applies. + ConnState::PeerClosed(ref mut recv_off, ref mut send_off) + if pkt.op() == uapi::VSOCK_OP_SHUTDOWN => + { + *recv_off = *recv_off || (pkt.flags() & uapi::VSOCK_FLAGS_SHUTDOWN_RCV != 0); + *send_off = *send_off || (pkt.flags() & uapi::VSOCK_FLAGS_SHUTDOWN_SEND != 0); + if *recv_off && *send_off && self.tx_buf.is_empty() { + self.pending_rx.insert(PendingRx::Rst); + } + } + + // A credit update from our peer is valid only in a state which allows data + // transfer towards the peer. + ConnState::Established | ConnState::PeerInit | ConnState::PeerClosed(false, _) + if pkt.op() == uapi::VSOCK_OP_CREDIT_UPDATE => + { + // Nothing to do here; we've already updated peer credit. + } + + // A credit request from our peer is valid only in a state which allows data + // transfer from the peer. We'll respond with a credit update packet. + ConnState::Established | ConnState::PeerInit | ConnState::PeerClosed(_, false) + if pkt.op() == uapi::VSOCK_OP_CREDIT_REQUEST => + { + self.pending_rx.insert(PendingRx::CreditUpdate); + } + + _ => { + debug!( + "vsock: dropping invalid TX pkt for connection: state={:?}, pkt.hdr={:?}", + self.state, + pkt.hdr() + ); + } + }; + + Ok(()) + } + + /// Check if the connection has any pending packet addressed to the peer. + /// + fn has_pending_rx(&self) -> bool { + !self.pending_rx.is_empty() + } +} + +impl VsockEpollListener for VsockConnection +where + S: Read + Write + AsRawFd, +{ + /// Get the file descriptor that this connection wants polled. + /// + /// The connection is interested in being notified about EPOLLIN / EPOLLOUT events on the + /// host stream. + /// + fn get_polled_fd(&self) -> RawFd { + self.stream.as_raw_fd() + } + + /// Get the event set that this connection is interested in. + /// + /// A connection will want to be notified when: + /// - data is available to be read from the host stream, so that it can store an RW pending + /// RX indication; and + /// - data can be written to the host stream, and the TX buffer needs to be flushed. + /// + fn get_polled_evset(&self) -> epoll::Events { + let mut evset = epoll::Events::empty(); + if !self.tx_buf.is_empty() { + // There's data waiting in the TX buffer, so we are interested in being notified + // when writing to the host stream wouldn't block. + evset.insert(epoll::Events::EPOLLOUT); + } + // We're generally interested in being notified when data can be read from the host + // stream, unless we're in a state which doesn't allow moving data from host to guest. + match self.state { + ConnState::Killed | ConnState::LocalClosed | ConnState::PeerClosed(true, _) => (), + _ if self.need_credit_update_from_peer() => (), + _ => evset.insert(epoll::Events::EPOLLIN), + } + evset + } + + /// Notify the connection about an event (or set of events) that it was interested in. + /// + fn notify(&mut self, evset: epoll::Events) { + if evset.contains(epoll::Events::EPOLLIN) { + // Data can be read from the host stream. Setting a Rw pending indication, so that + // the muxer will know to call `recv_pkt()` later. + self.pending_rx.insert(PendingRx::Rw); + } + + if evset.contains(epoll::Events::EPOLLOUT) { + // Data can be written to the host stream. Time to flush out the TX buffer. + // + if self.tx_buf.is_empty() { + info!("vsock: connection received unexpected EPOLLOUT event"); + return; + } + let flushed = self + .tx_buf + .flush_to(&mut self.stream) + .unwrap_or_else(|err| match err { + Error::TxBufFlush(inner) if inner.kind() == ErrorKind::WouldBlock => { + debug!( + "vsock: unexpected EWOULDBLOCK while flushing TX buf (lp={}, pp={})", + self.local_port, self.peer_port + ); + 0 + } + _ => { + warn!( + "vsock: error flushing TX buf for (lp={}, pp={}): {:?}", + self.local_port, self.peer_port, err + ); + self.kill(); + 0 + } + }); + + self.fwd_cnt += Wrapping(flushed as u32); + + // If this connection was shutting down, but is waiting to drain the TX buffer + // before forceful termination, the wait might be over. + if self.state == ConnState::PeerClosed(true, true) && self.tx_buf.is_empty() { + self.pending_rx.insert(PendingRx::Rst); + } else if self.peer_needs_credit_update() { + // If we've freed up some more buffer space, we may need to let the peer know it + // can safely send more data our way. + self.pending_rx.insert(PendingRx::CreditUpdate); + } + } + } +} + +impl VsockConnection +where + S: Read + Write + AsRawFd, +{ + /// Create a new guest-initiated connection object. + /// + pub fn new_peer_init( + stream: S, + local_cid: u64, + peer_cid: u64, + local_port: u32, + peer_port: u32, + peer_buf_alloc: u32, + ) -> Self { + Self { + local_cid, + peer_cid, + local_port, + peer_port, + stream, + state: ConnState::PeerInit, + tx_buf: TxBuf::new(), + fwd_cnt: Wrapping(0), + peer_buf_alloc, + peer_fwd_cnt: Wrapping(0), + rx_cnt: Wrapping(0), + last_fwd_cnt_to_peer: Wrapping(0), + pending_rx: PendingRxSet::from(PendingRx::Response), + expiry: None, + } + } + + /// Create a new host-initiated connection object. + /// + pub fn new_local_init( + stream: S, + local_cid: u64, + peer_cid: u64, + local_port: u32, + peer_port: u32, + ) -> Self { + Self { + local_cid, + peer_cid, + local_port, + peer_port, + stream, + state: ConnState::LocalInit, + tx_buf: TxBuf::new(), + fwd_cnt: Wrapping(0), + peer_buf_alloc: 0, + peer_fwd_cnt: Wrapping(0), + rx_cnt: Wrapping(0), + last_fwd_cnt_to_peer: Wrapping(0), + pending_rx: PendingRxSet::from(PendingRx::Request), + expiry: None, + } + } + + /// Check if there is an expiry (kill) timer set for this connection, sometime in the + /// future. + /// + pub fn will_expire(&self) -> bool { + match self.expiry { + None => false, + Some(t) => t > Instant::now(), + } + } + + /// Check if this connection needs to be scheduled for forceful termination, due to its + /// kill timer having expired. + /// + pub fn has_expired(&self) -> bool { + match self.expiry { + None => false, + Some(t) => t <= Instant::now(), + } + } + + /// Get the kill timer value, if one is set. + /// + pub fn expiry(&self) -> Option { + self.expiry + } + + /// Schedule the connection to be forcefully terminated ASAP (i.e. the next time the + /// connection is asked to yield a packet, via `recv_pkt()`). + /// + pub fn kill(&mut self) { + self.state = ConnState::Killed; + self.pending_rx.insert(PendingRx::Rst); + } + + /// Return the connections state. + /// + pub fn state(&self) -> ConnState { + self.state + } + + /// Get a mutable reference to the underlying stream. + pub fn stream_mut(&mut self) -> &mut S { + &mut self.stream + } + + /// Send some raw data (a byte-slice) to the host stream. + /// + /// Raw data can either be sent straight to the host stream, or to our TX buffer, if the + /// former fails. + /// + fn send_bytes(&mut self, buf: &[u8]) -> Result<()> { + // If there is data in the TX buffer, that means we're already registered for EPOLLOUT + // events on the underlying stream. Therefore, there's no point in attempting a write + // at this point. `self.notify()` will get called when EPOLLOUT arrives, and it will + // attempt to drain the TX buffer then. + if !self.tx_buf.is_empty() { + return self.tx_buf.push(buf); + } + + // The TX buffer is empty, so we can try to write straight to the host stream. + let written = match self.stream.write(buf) { + Ok(cnt) => cnt, + Err(e) => { + // For WouldBlock error, we should put data into buffer + if e.kind() == ErrorKind::WouldBlock { + debug!( + "vsock: stream write would block (lp={}, pp={}), buffering data", + self.local_port, self.peer_port + ); + return self.tx_buf.push(buf); + } else { + // We don't know how to handle any other write error, so we'll send it up + // the call chain. + return Err(Error::StreamWrite(e)); + } + } + }; + // Move the "forwarded bytes" counter ahead by how much we were able to send out. + self.fwd_cnt += Wrapping(written as u32); + + // If we couldn't write the whole slice, we'll need to push the remaining data to our + // buffer. + if written < buf.len() { + self.tx_buf.push(&buf[written..])?; + } + + Ok(()) + } + + /// Check if the credit information the peer has last received from us is outdated. + /// + fn peer_needs_credit_update(&self) -> bool { + let peer_seen_free_buf = + Wrapping(defs::CONN_TX_BUF_SIZE) - (self.fwd_cnt - self.last_fwd_cnt_to_peer); + peer_seen_free_buf < Wrapping(defs::CONN_CREDIT_UPDATE_THRESHOLD) + } + + /// Check if we need to ask the peer for a credit update before sending any more data its + /// way. + /// + fn need_credit_update_from_peer(&self) -> bool { + self.peer_avail_credit() == 0 + } + + /// Get the maximum number of bytes that we can send to our peer, without overflowing its + /// buffer. + /// + fn peer_avail_credit(&self) -> usize { + (Wrapping(self.peer_buf_alloc) - (self.rx_cnt - self.peer_fwd_cnt)).0 as usize + } + + /// Prepare a packet header for transmission to our peer. + /// + fn init_pkt<'a>(&self, pkt: &'a mut VsockPacket) -> &'a mut VsockPacket { + // Make sure the header is zeroed-out first. + // This looks sub-optimal, but it is actually optimized-out in the compiled code to be + // faster than a memset(). + for b in pkt.hdr_mut() { + *b = 0; + } + + pkt.set_src_cid(self.local_cid) + .set_dst_cid(self.peer_cid) + .set_src_port(self.local_port) + .set_dst_port(self.peer_port) + .set_type(uapi::VSOCK_TYPE_STREAM) + .set_buf_alloc(defs::CONN_TX_BUF_SIZE) + .set_fwd_cnt(self.fwd_cnt.0) + } +} + +#[cfg(test)] +mod tests { + use std::io::{Error as IoError, ErrorKind, Read, Result as IoResult, Write}; + use std::os::unix::io::RawFd; + use std::time::{Duration, Instant}; + + use libc::EFD_NONBLOCK; + use virtio_queue::QueueOwnedT; + use vmm_sys_util::eventfd::EventFd; + + use super::super::super::defs::uapi; + use super::super::super::tests::TestContext; + use super::super::defs as csm_defs; + use super::*; + + const LOCAL_CID: u64 = 2; + const PEER_CID: u64 = 3; + const LOCAL_PORT: u32 = 1002; + const PEER_PORT: u32 = 1003; + const PEER_BUF_ALLOC: u32 = 64 * 1024; + + enum StreamState { + Closed, + Error(ErrorKind), + Ready, + WouldBlock, + } + + struct TestStream { + fd: EventFd, + read_buf: Vec, + read_state: StreamState, + write_buf: Vec, + write_state: StreamState, + } + impl TestStream { + fn new() -> Self { + Self { + fd: EventFd::new(EFD_NONBLOCK).unwrap(), + read_state: StreamState::Ready, + write_state: StreamState::Ready, + read_buf: Vec::new(), + write_buf: Vec::new(), + } + } + fn new_with_read_buf(buf: &[u8]) -> Self { + let mut stream = Self::new(); + stream.read_buf = buf.to_vec(); + stream + } + } + + impl AsRawFd for TestStream { + fn as_raw_fd(&self) -> RawFd { + self.fd.as_raw_fd() + } + } + + impl Read for TestStream { + fn read(&mut self, data: &mut [u8]) -> IoResult { + match self.read_state { + StreamState::Closed => Ok(0), + StreamState::Error(kind) => Err(IoError::new(kind, "whatevs")), + StreamState::Ready => { + if self.read_buf.is_empty() { + return Err(IoError::new(ErrorKind::WouldBlock, "EAGAIN")); + } + let len = std::cmp::min(data.len(), self.read_buf.len()); + assert_ne!(len, 0); + data[..len].copy_from_slice(&self.read_buf[..len]); + self.read_buf = self.read_buf.split_off(len); + Ok(len) + } + StreamState::WouldBlock => Err(IoError::new(ErrorKind::WouldBlock, "EAGAIN")), + } + } + } + + impl Write for TestStream { + fn write(&mut self, data: &[u8]) -> IoResult { + match self.write_state { + StreamState::Closed => Err(IoError::new(ErrorKind::BrokenPipe, "EPIPE")), + StreamState::Error(kind) => Err(IoError::new(kind, "whatevs")), + StreamState::Ready => { + self.write_buf.extend_from_slice(data); + Ok(data.len()) + } + StreamState::WouldBlock => Err(IoError::new(ErrorKind::WouldBlock, "EAGAIN")), + } + } + fn flush(&mut self) -> IoResult<()> { + Ok(()) + } + } + + impl VsockConnection + where + S: Read + Write + AsRawFd, + { + /// Get the fwd_cnt value from the connection. + pub(crate) fn fwd_cnt(&self) -> Wrapping { + self.fwd_cnt + } + + /// Forcefully insert a credit update flag. + pub(crate) fn insert_credit_update(&mut self) { + self.pending_rx.insert(PendingRx::CreditUpdate); + } + } + + fn init_pkt(pkt: &mut VsockPacket, op: u16, len: u32) -> &mut VsockPacket { + for b in pkt.hdr_mut() { + *b = 0; + } + pkt.set_src_cid(PEER_CID) + .set_dst_cid(LOCAL_CID) + .set_src_port(PEER_PORT) + .set_dst_port(LOCAL_PORT) + .set_type(uapi::VSOCK_TYPE_STREAM) + .set_buf_alloc(PEER_BUF_ALLOC) + .set_op(op) + .set_len(len) + } + + // This is the connection state machine test context: a helper struct to provide CSM testing + // primitives. A single `VsockPacket` object will be enough for our testing needs. We'll be + // using it for simulating both packet sends and packet receives. We need to keep the vsock + // testing context alive, since `VsockPacket` is just a pointer-wrapper over some data that + // resides in guest memory. The vsock test context owns the `GuestMemory` object, so we'll make + // it a member here, in order to make sure that guest memory outlives our testing packet. A + // single `VsockConnection` object will also suffice for our testing needs. We'll be using a + // specially crafted `Read + Write + AsRawFd` object as a backing stream, so that we can + // control the various error conditions that might arise. + struct CsmTestContext { + _vsock_test_ctx: TestContext, + pkt: VsockPacket, + conn: VsockConnection, + } + + impl CsmTestContext { + fn new_established() -> Self { + Self::new(ConnState::Established) + } + + fn new(conn_state: ConnState) -> Self { + let vsock_test_ctx = TestContext::new(); + let mut handler_ctx = vsock_test_ctx.create_epoll_handler_context(); + let stream = TestStream::new(); + let mut pkt = VsockPacket::from_rx_virtq_head( + &mut handler_ctx.handler.queues[0] + .iter(&vsock_test_ctx.mem) + .unwrap() + .next() + .unwrap(), + None, + ) + .unwrap(); + let conn = match conn_state { + ConnState::PeerInit => VsockConnection::::new_peer_init( + stream, + LOCAL_CID, + PEER_CID, + LOCAL_PORT, + PEER_PORT, + PEER_BUF_ALLOC, + ), + ConnState::LocalInit => VsockConnection::::new_local_init( + stream, LOCAL_CID, PEER_CID, LOCAL_PORT, PEER_PORT, + ), + ConnState::Established => { + let mut conn = VsockConnection::::new_peer_init( + stream, + LOCAL_CID, + PEER_CID, + LOCAL_PORT, + PEER_PORT, + PEER_BUF_ALLOC, + ); + assert!(conn.has_pending_rx()); + conn.recv_pkt(&mut pkt).unwrap(); + assert_eq!(pkt.op(), uapi::VSOCK_OP_RESPONSE); + conn + } + other => panic!("invalid ctx state: {:?}", other), + }; + assert_eq!(conn.state, conn_state); + Self { + _vsock_test_ctx: vsock_test_ctx, + pkt, + conn, + } + } + + fn set_stream(&mut self, stream: TestStream) { + self.conn.stream = stream; + } + + fn set_peer_credit(&mut self, credit: u32) { + assert!(credit < self.conn.peer_buf_alloc); + self.conn.peer_fwd_cnt = Wrapping(0); + self.conn.rx_cnt = Wrapping(self.conn.peer_buf_alloc - credit); + assert_eq!(self.conn.peer_avail_credit(), credit as usize); + } + + fn send(&mut self) { + self.conn.send_pkt(&self.pkt).unwrap(); + } + + fn recv(&mut self) { + self.conn.recv_pkt(&mut self.pkt).unwrap(); + } + + fn notify_epollin(&mut self) { + self.conn.notify(epoll::Events::EPOLLIN); + assert!(self.conn.has_pending_rx()); + } + + fn notify_epollout(&mut self) { + self.conn.notify(epoll::Events::EPOLLOUT); + } + + fn init_pkt(&mut self, op: u16, len: u32) -> &mut VsockPacket { + init_pkt(&mut self.pkt, op, len) + } + + fn init_data_pkt(&mut self, data: &[u8]) -> &VsockPacket { + assert!(data.len() <= self.pkt.buf().unwrap().len()); + self.init_pkt(uapi::VSOCK_OP_RW, data.len() as u32); + self.pkt.buf_mut().unwrap()[..data.len()].copy_from_slice(data); + &self.pkt + } + } + + #[test] + fn test_peer_request() { + let mut ctx = CsmTestContext::new(ConnState::PeerInit); + assert!(ctx.conn.has_pending_rx()); + ctx.recv(); + // For peer-initiated requests, our connection should always yield a vsock response packet, + // in order to establish the connection. + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RESPONSE); + assert_eq!(ctx.pkt.src_cid(), LOCAL_CID); + assert_eq!(ctx.pkt.dst_cid(), PEER_CID); + assert_eq!(ctx.pkt.src_port(), LOCAL_PORT); + assert_eq!(ctx.pkt.dst_port(), PEER_PORT); + assert_eq!(ctx.pkt.type_(), uapi::VSOCK_TYPE_STREAM); + assert_eq!(ctx.pkt.len(), 0); + // After yielding the response packet, the connection should have transitioned to the + // established state. + assert_eq!(ctx.conn.state, ConnState::Established); + } + + #[test] + fn test_local_request() { + let mut ctx = CsmTestContext::new(ConnState::LocalInit); + // Host-initiated connections should first yield a connection request packet. + assert!(ctx.conn.has_pending_rx()); + // Before yielding the connection request packet, the timeout kill timer shouldn't be + // armed. + assert!(!ctx.conn.will_expire()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_REQUEST); + // Since the request might time-out, the kill timer should now be armed. + assert!(ctx.conn.will_expire()); + assert!(!ctx.conn.has_expired()); + ctx.init_pkt(uapi::VSOCK_OP_RESPONSE, 0); + ctx.send(); + // Upon receiving a connection response, the connection should have transitioned to the + // established state, and the kill timer should've been disarmed. + assert_eq!(ctx.conn.state, ConnState::Established); + assert!(!ctx.conn.will_expire()); + } + + #[test] + fn test_local_request_timeout() { + let mut ctx = CsmTestContext::new(ConnState::LocalInit); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_REQUEST); + assert!(ctx.conn.will_expire()); + assert!(!ctx.conn.has_expired()); + std::thread::sleep(std::time::Duration::from_millis( + defs::CONN_REQUEST_TIMEOUT_MS, + )); + assert!(ctx.conn.has_expired()); + } + + #[test] + fn test_rx_data() { + let mut ctx = CsmTestContext::new_established(); + let data = &[1, 2, 3, 4]; + ctx.set_stream(TestStream::new_with_read_buf(data)); + assert_eq!(ctx.conn.get_polled_fd(), ctx.conn.stream.as_raw_fd()); + ctx.notify_epollin(); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RW); + assert_eq!(ctx.pkt.len() as usize, data.len()); + assert_eq!(ctx.pkt.buf().unwrap()[..ctx.pkt.len() as usize], *data); + + // There's no more data in the stream, so `recv_pkt` should yield `VsockError::NoData`. + match ctx.conn.recv_pkt(&mut ctx.pkt) { + Err(VsockError::NoData) => (), + other => panic!("{:?}", other), + } + + // A recv attempt in an invalid state should yield an instant reset packet. + ctx.conn.state = ConnState::LocalClosed; + ctx.notify_epollin(); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + } + + #[test] + fn test_local_close() { + let mut ctx = CsmTestContext::new_established(); + let mut stream = TestStream::new(); + stream.read_state = StreamState::Closed; + ctx.set_stream(stream); + ctx.notify_epollin(); + ctx.recv(); + // When the host-side stream is closed, we can neither send not receive any more data. + // Therefore, the vsock shutdown packet that we'll deliver to the guest must contain both + // the no-more-send and the no-more-recv indications. + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_SHUTDOWN); + assert_ne!(ctx.pkt.flags() & uapi::VSOCK_FLAGS_SHUTDOWN_SEND, 0); + assert_ne!(ctx.pkt.flags() & uapi::VSOCK_FLAGS_SHUTDOWN_RCV, 0); + + // The kill timer should now be armed. + assert!(ctx.conn.will_expire()); + assert!( + ctx.conn.expiry().unwrap() + < Instant::now() + Duration::from_millis(defs::CONN_SHUTDOWN_TIMEOUT_MS) + ); + } + + #[test] + fn test_peer_close() { + // Test that send/recv shutdown indications are handled correctly. + // I.e. once set, an indication cannot be reset. + { + let mut ctx = CsmTestContext::new_established(); + + ctx.init_pkt(uapi::VSOCK_OP_SHUTDOWN, 0) + .set_flags(uapi::VSOCK_FLAGS_SHUTDOWN_RCV); + ctx.send(); + assert_eq!(ctx.conn.state, ConnState::PeerClosed(true, false)); + + // Attempting to reset the no-more-recv indication should not work + // (we are only setting the no-more-send indication here). + ctx.pkt.set_flags(uapi::VSOCK_FLAGS_SHUTDOWN_SEND); + ctx.send(); + assert_eq!(ctx.conn.state, ConnState::PeerClosed(true, true)); + } + + // Test case: + // - reading data from a no-more-send connection should work; and + // - writing data should have no effect. + { + let data = &[1, 2, 3, 4]; + let mut ctx = CsmTestContext::new_established(); + ctx.set_stream(TestStream::new_with_read_buf(data)); + ctx.init_pkt(uapi::VSOCK_OP_SHUTDOWN, 0) + .set_flags(uapi::VSOCK_FLAGS_SHUTDOWN_SEND); + ctx.send(); + ctx.notify_epollin(); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RW); + assert_eq!(&ctx.pkt.buf().unwrap()[..ctx.pkt.len() as usize], data); + + ctx.init_data_pkt(data); + ctx.send(); + assert_eq!(ctx.conn.stream.write_buf.len(), 0); + assert!(ctx.conn.tx_buf.is_empty()); + } + + // Test case: + // - writing data to a no-more-recv connection should work; and + // - attempting to read data from it should yield an RST packet. + { + let mut ctx = CsmTestContext::new_established(); + ctx.init_pkt(uapi::VSOCK_OP_SHUTDOWN, 0) + .set_flags(uapi::VSOCK_FLAGS_SHUTDOWN_RCV); + ctx.send(); + let data = &[1, 2, 3, 4]; + ctx.init_data_pkt(data); + ctx.send(); + assert_eq!(ctx.conn.stream.write_buf, data.to_vec()); + + ctx.notify_epollin(); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + } + + // Test case: setting both no-more-send and no-more-recv indications should have the + // connection confirm termination (i.e. yield an RST). + { + let mut ctx = CsmTestContext::new_established(); + ctx.init_pkt(uapi::VSOCK_OP_SHUTDOWN, 0) + .set_flags(uapi::VSOCK_FLAGS_SHUTDOWN_RCV | uapi::VSOCK_FLAGS_SHUTDOWN_SEND); + ctx.send(); + assert!(ctx.conn.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + } + } + + #[test] + fn test_local_read_error() { + let mut ctx = CsmTestContext::new_established(); + let mut stream = TestStream::new(); + stream.read_state = StreamState::Error(ErrorKind::PermissionDenied); + ctx.set_stream(stream); + ctx.notify_epollin(); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + } + + #[test] + fn test_credit_request_to_peer() { + let mut ctx = CsmTestContext::new_established(); + ctx.set_peer_credit(0); + ctx.notify_epollin(); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_CREDIT_REQUEST); + } + + #[test] + fn test_credit_request_from_peer() { + let mut ctx = CsmTestContext::new_established(); + ctx.init_pkt(uapi::VSOCK_OP_CREDIT_REQUEST, 0); + ctx.send(); + assert!(ctx.conn.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_CREDIT_UPDATE); + assert_eq!(ctx.pkt.buf_alloc(), csm_defs::CONN_TX_BUF_SIZE); + assert_eq!(ctx.pkt.fwd_cnt(), ctx.conn.fwd_cnt.0); + } + + #[test] + fn test_credit_update_to_peer() { + let mut ctx = CsmTestContext::new_established(); + + // Force a stale state, where the peer hasn't been updated on our credit situation. + ctx.conn.last_fwd_cnt_to_peer = Wrapping(0); + + // Since a credit update token is sent when the fwd_cnt value exceeds + // CONN_TX_BUF_SIZE - CONN_CREDIT_UPDATE_THRESHOLD, we initialize + // fwd_cnt at 6 bytes below the threshold. + let initial_fwd_cnt = + csm_defs::CONN_TX_BUF_SIZE - csm_defs::CONN_CREDIT_UPDATE_THRESHOLD - 6; + ctx.conn.fwd_cnt = Wrapping(initial_fwd_cnt); + + // Use a 4-byte packet for triggering the credit update threshold. + let data = &[1, 2, 3, 4]; + + // Check that there is no pending RX. + ctx.init_data_pkt(data); + ctx.send(); + assert!(!ctx.conn.has_pending_rx()); + + // Send a packet again. + ctx.init_data_pkt(data); + ctx.send(); + + // The CSM should now have a credit update available for the peer. + assert!(ctx.conn.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_CREDIT_UPDATE); + assert_eq!(ctx.pkt.fwd_cnt(), initial_fwd_cnt + data.len() as u32 * 2); + assert_eq!(ctx.conn.fwd_cnt, ctx.conn.last_fwd_cnt_to_peer); + } + + #[test] + fn test_tx_buffering() { + // Test case: + // - when writing to the backing stream would block, TX data should end up in the TX buf + // - when the CSM is notified that it can write to the backing stream, it should flush + // the TX buf. + { + let mut ctx = CsmTestContext::new_established(); + + let mut stream = TestStream::new(); + stream.write_state = StreamState::WouldBlock; + ctx.set_stream(stream); + + // Send some data through the connection. The backing stream is set to reject writes, + // so the data should end up in the TX buffer. + let data = &[1, 2, 3, 4]; + ctx.init_data_pkt(data); + ctx.send(); + + // When there's data in the TX buffer, the connection should ask to be notified when it + // can write to its backing stream. + assert!(ctx + .conn + .get_polled_evset() + .contains(epoll::Events::EPOLLOUT)); + assert_eq!(ctx.conn.tx_buf.len(), data.len()); + + // Unlock the write stream and notify the connection it can now write its buffered + // data. + ctx.set_stream(TestStream::new()); + ctx.conn.notify(epoll::Events::EPOLLOUT); + assert!(ctx.conn.tx_buf.is_empty()); + assert_eq!(ctx.conn.stream.write_buf, data); + } + } + + #[test] + fn test_stream_write_error() { + // Test case: sending a data packet to a broken / closed backing stream should kill it. + { + let mut ctx = CsmTestContext::new_established(); + let mut stream = TestStream::new(); + stream.write_state = StreamState::Closed; + ctx.set_stream(stream); + + let data = &[1, 2, 3, 4]; + ctx.init_data_pkt(data); + ctx.send(); + + assert_eq!(ctx.conn.state, ConnState::Killed); + assert!(ctx.conn.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + } + + // Test case: notifying a connection that it can flush its TX buffer to a broken stream + // should kill the connection. + { + let mut ctx = CsmTestContext::new_established(); + + let mut stream = TestStream::new(); + stream.write_state = StreamState::WouldBlock; + ctx.set_stream(stream); + + // Send some data through the connection. The backing stream is set to reject writes, + // so the data should end up in the TX buffer. + let data = &[1, 2, 3, 4]; + ctx.init_data_pkt(data); + ctx.send(); + + // Set the backing stream to error out on write. + let mut stream = TestStream::new(); + stream.write_state = StreamState::Closed; + ctx.set_stream(stream); + + assert!(ctx + .conn + .get_polled_evset() + .contains(epoll::Events::EPOLLOUT)); + ctx.notify_epollout(); + assert_eq!(ctx.conn.state, ConnState::Killed); + } + } + + #[test] + fn test_peer_credit_misbehavior() { + let mut ctx = CsmTestContext::new_established(); + + let mut stream = TestStream::new(); + stream.write_state = StreamState::WouldBlock; + ctx.set_stream(stream); + + // Fill up the TX buffer. + let data = vec![0u8; ctx.pkt.buf().unwrap().len()]; + ctx.init_data_pkt(data.as_slice()); + for _i in 0..(csm_defs::CONN_TX_BUF_SIZE / data.len() as u32) { + ctx.send(); + } + + // Then try to send more data. + ctx.send(); + + // The connection should've committed suicide. + assert_eq!(ctx.conn.state, ConnState::Killed); + assert!(ctx.conn.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/mod.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/mod.rs new file mode 100644 index 0000000..d61920f --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/mod.rs @@ -0,0 +1,129 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +/// This module implements our vsock connection state machine. The heavy lifting is done by +/// `connection::VsockConnection`, while this file only defines some constants and helper structs. +/// +mod connection; +mod txbuf; + +pub use connection::VsockConnection; + +pub mod defs { + /// Vsock connection TX buffer capacity. + pub const CONN_TX_BUF_SIZE: u32 = 64 * 1024; + + /// When the guest thinks we have less than this amount of free buffer space, + /// we will send them a credit update packet. + pub const CONN_CREDIT_UPDATE_THRESHOLD: u32 = 4 * 1024; + + /// Connection request timeout, in millis. + pub const CONN_REQUEST_TIMEOUT_MS: u64 = 2000; + + /// Connection graceful shutdown timeout, in millis. + pub const CONN_SHUTDOWN_TIMEOUT_MS: u64 = 2000; +} + +#[derive(Debug)] +pub enum Error { + /// Attempted to push data to a full TX buffer. + TxBufFull, + /// An I/O error occurred, when attempting to flush the connection TX buffer. + TxBufFlush(std::io::Error), + /// An I/O error occurred, when attempting to write data to the host-side stream. + StreamWrite(std::io::Error), +} + +type Result = std::result::Result; + +/// A vsock connection state. +/// +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ConnState { + /// The connection has been initiated by the host end, but is yet to be confirmed by the guest. + LocalInit, + /// The connection has been initiated by the guest, but we are yet to confirm it, by sending + /// a response packet (VSOCK_OP_RESPONSE). + PeerInit, + /// The connection handshake has been performed successfully, and data can now be exchanged. + Established, + /// The host (AF_UNIX) socket was closed. + LocalClosed, + /// A VSOCK_OP_SHUTDOWN packet was received from the guest. The tuple represents the guest R/W + /// indication: (will_not_recv_anymore_data, will_not_send_anymore_data). + PeerClosed(bool, bool), + /// The connection is scheduled to be forcefully terminated as soon as possible. + Killed, +} + +/// An RX indication, used by `VsockConnection` to schedule future `recv_pkt()` responses. +/// For instance, after being notified that there is available data to be read from the host stream +/// (via `notify()`), the connection will store a `PendingRx::Rw` to be later inspected by +/// `recv_pkt()`. +/// +#[derive(Clone, Copy, PartialEq, Eq)] +enum PendingRx { + /// We need to yield a connection request packet (VSOCK_OP_REQUEST). + Request = 0, + /// We need to yield a connection response packet (VSOCK_OP_RESPONSE). + Response = 1, + /// We need to yield a forceful connection termination packet (VSOCK_OP_RST). + Rst = 2, + /// We need to yield a data packet (VSOCK_OP_RW), by reading from the AF_UNIX socket. + Rw = 3, + /// We need to yield a credit update packet (VSOCK_OP_CREDIT_UPDATE). + CreditUpdate = 4, +} +impl PendingRx { + /// Transform the enum value into a bitmask, that can be used for set operations. + /// + fn into_mask(self) -> u16 { + 1u16 << (self as u16) + } +} + +/// A set of RX indications (`PendingRx` items). +/// +struct PendingRxSet { + data: u16, +} + +impl PendingRxSet { + /// Insert an item into the set. + /// + fn insert(&mut self, it: PendingRx) { + self.data |= it.into_mask(); + } + + /// Remove an item from the set and return: + /// - true, if the item was in the set; or + /// - false, if the item wasn't in the set. + /// + fn remove(&mut self, it: PendingRx) -> bool { + let ret = self.contains(it); + self.data &= !it.into_mask(); + ret + } + + /// Check if an item is present in this set. + /// + fn contains(&self, it: PendingRx) -> bool { + self.data & it.into_mask() != 0 + } + + /// Check if the set is empty. + /// + fn is_empty(&self) -> bool { + self.data == 0 + } +} + +/// Create a set containing only one item. +/// +impl From for PendingRxSet { + fn from(it: PendingRx) -> Self { + Self { + data: it.into_mask(), + } + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/txbuf.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/txbuf.rs new file mode 100644 index 0000000..cb3711f --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/txbuf.rs @@ -0,0 +1,272 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// + +use std::io::Write; +use std::num::Wrapping; + +use super::defs; +use super::{Error, Result}; + +/// A simple ring-buffer implementation, used by vsock connections to buffer TX (guest -> host) +/// data. Memory for this buffer is allocated lazily, since buffering will only be needed when +/// the host can't read fast enough. +/// +pub struct TxBuf { + /// The actual u8 buffer - only allocated after the first push. + data: Option>, + /// Ring-buffer head offset - where new data is pushed to. + head: Wrapping, + /// Ring-buffer tail offset - where data is flushed from. + tail: Wrapping, +} + +impl TxBuf { + /// Total buffer size, in bytes. + /// + const SIZE: usize = defs::CONN_TX_BUF_SIZE as usize; + + /// Ring-buffer constructor. + /// + pub fn new() -> Self { + Self { + data: None, + head: Wrapping(0), + tail: Wrapping(0), + } + } + + /// Get the used length of this buffer - number of bytes that have been pushed in, but not + /// yet flushed out. + /// + pub fn len(&self) -> usize { + (self.head - self.tail).0 as usize + } + + /// Push a byte slice onto the ring-buffer. + /// + /// Either the entire source slice will be pushed to the ring-buffer, or none of it, if + /// there isn't enough room, in which case `Err(Error::TxBufFull)` is returned. + /// + pub fn push(&mut self, src: &[u8]) -> Result<()> { + // Error out if there's no room to push the entire slice. + if self.len() + src.len() > Self::SIZE { + return Err(Error::TxBufFull); + } + + let data = self + .data + .get_or_insert_with(|| vec![0u8; Self::SIZE].into_boxed_slice()); + + // Buffer head, as an offset into the data slice. + let head_ofs = self.head.0 as usize % Self::SIZE; + + // Pushing a slice to this buffer can take either one or two slice copies: - one copy, + // if the slice fits between `head_ofs` and `Self::SIZE`; or - two copies, if the + // ring-buffer head wraps around. + + // First copy length: we can only go from the head offset up to the total buffer size. + let len = std::cmp::min(Self::SIZE - head_ofs, src.len()); + data[head_ofs..(head_ofs + len)].copy_from_slice(&src[..len]); + + // If the slice didn't fit, the buffer head will wrap around, and pushing continues + // from the start of the buffer (`&self.data[0]`). + if len < src.len() { + data[..(src.len() - len)].copy_from_slice(&src[len..]); + } + + // Either way, we've just pushed exactly `src.len()` bytes, so that's the amount by + // which the (wrapping) buffer head needs to move forward. + self.head += Wrapping(src.len() as u32); + + Ok(()) + } + + /// Flush the contents of the ring-buffer to a writable stream. + /// + /// Return the number of bytes that have been transferred out of the ring-buffer and into + /// the writable stream. + /// + pub fn flush_to(&mut self, sink: &mut W) -> Result + where + W: Write, + { + // Nothing to do, if this buffer holds no data. + if self.is_empty() { + return Ok(0); + } + + // Buffer tail, as an offset into the buffer data slice. + let tail_ofs = self.tail.0 as usize % Self::SIZE; + + // Flushing the buffer can take either one or two writes: + // - one write, if the tail doesn't need to wrap around to reach the head; or + // - two writes, if the tail would wrap around: tail to slice end, then slice end to + // head. + + // First write length: the lesser of tail to slice end, or tail to head. + let len_to_write = std::cmp::min(Self::SIZE - tail_ofs, self.len()); + + // It's safe to unwrap here, since we've already checked if the buffer was empty. + let data = self.data.as_ref().unwrap(); + + // Issue the first write and absorb any `WouldBlock` error (we can just try again + // later). + let written = sink + .write(&data[tail_ofs..(tail_ofs + len_to_write)]) + .map_err(Error::TxBufFlush)?; + + // Move the buffer tail ahead by the amount (of bytes) we were able to flush out. + self.tail += Wrapping(written as u32); + + // If we weren't able to flush out as much as we tried, there's no point in attempting + // our second write. + if written < len_to_write { + return Ok(written); + } + + // Attempt our second write. This will return immediately if a second write isn't + // needed, since checking for an empty buffer is the first thing we do in this + // function. + // + // Interesting corner case: if we've already written some data in the first pass, + // and then the second write fails, we will consider the flush action a success + // and return the number of bytes written in the first pass. + Ok(written + self.flush_to(sink).unwrap_or(0)) + } + + /// Check if the buffer holds any data that hasn't yet been flushed out. + /// + pub fn is_empty(&self) -> bool { + self.len() == 0 + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Error as IoError; + use std::io::Result as IoResult; + use std::io::{ErrorKind, Write}; + + struct TestSink { + data: Vec, + err: Option, + capacity: usize, + } + + impl TestSink { + const DEFAULT_CAPACITY: usize = 2 * TxBuf::SIZE; + fn new() -> Self { + Self { + data: Vec::with_capacity(Self::DEFAULT_CAPACITY), + err: None, + capacity: Self::DEFAULT_CAPACITY, + } + } + } + + impl Write for TestSink { + fn write(&mut self, src: &[u8]) -> IoResult { + if self.err.is_some() { + return Err(self.err.take().unwrap()); + } + let len_to_push = std::cmp::min(self.capacity - self.data.len(), src.len()); + self.data.extend_from_slice(&src[..len_to_push]); + Ok(len_to_push) + } + fn flush(&mut self) -> IoResult<()> { + Ok(()) + } + } + + impl TestSink { + fn clear(&mut self) { + self.data = Vec::with_capacity(self.capacity); + self.err = None; + } + fn set_err(&mut self, err: IoError) { + self.err = Some(err); + } + fn set_capacity(&mut self, capacity: usize) { + self.capacity = capacity; + if self.data.len() > self.capacity { + self.data.resize(self.capacity, 0); + } + } + } + + #[test] + fn test_push_nowrap() { + let mut txbuf = TxBuf::new(); + let mut sink = TestSink::new(); + assert!(txbuf.is_empty()); + + assert!(txbuf.data.is_none()); + txbuf.push(&[1, 2, 3, 4]).unwrap(); + txbuf.push(&[5, 6, 7, 8]).unwrap(); + txbuf.flush_to(&mut sink).unwrap(); + assert_eq!(sink.data, [1, 2, 3, 4, 5, 6, 7, 8]); + } + + #[test] + fn test_push_wrap() { + let mut txbuf = TxBuf::new(); + let mut sink = TestSink::new(); + let tmp: Vec = vec![0; TxBuf::SIZE - 2]; + + txbuf.push(tmp.as_slice()).unwrap(); + txbuf.flush_to(&mut sink).unwrap(); + sink.clear(); + + txbuf.push(&[1, 2, 3, 4]).unwrap(); + assert_eq!(txbuf.flush_to(&mut sink).unwrap(), 4); + assert_eq!(sink.data, [1, 2, 3, 4]); + } + + #[test] + fn test_push_error() { + let mut txbuf = TxBuf::new(); + let mut tmp = Vec::with_capacity(TxBuf::SIZE); + + tmp.resize(TxBuf::SIZE - 1, 0); + txbuf.push(tmp.as_slice()).unwrap(); + match txbuf.push(&[1, 2]) { + Err(Error::TxBufFull) => (), + other => panic!("Unexpected result: {:?}", other), + } + } + + #[test] + fn test_incomplete_flush() { + let mut txbuf = TxBuf::new(); + let mut sink = TestSink::new(); + + sink.set_capacity(2); + txbuf.push(&[1, 2, 3, 4]).unwrap(); + assert_eq!(txbuf.flush_to(&mut sink).unwrap(), 2); + assert_eq!(txbuf.len(), 2); + assert_eq!(sink.data, [1, 2]); + + sink.set_capacity(4); + assert_eq!(txbuf.flush_to(&mut sink).unwrap(), 2); + assert!(txbuf.is_empty()); + assert_eq!(sink.data, [1, 2, 3, 4]); + } + + #[test] + fn test_flush_error() { + const EACCESS: i32 = 13; + + let mut txbuf = TxBuf::new(); + let mut sink = TestSink::new(); + + txbuf.push(&[1, 2, 3, 4]).unwrap(); + let io_err = IoError::from_raw_os_error(EACCESS); + sink.set_err(io_err); + match txbuf.flush_to(&mut sink) { + Err(Error::TxBufFlush(ref err)) if err.kind() == ErrorKind::PermissionDenied => (), + other => panic!("Unexpected result: {:?}", other), + } + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/device.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/device.rs new file mode 100644 index 0000000..27fde63 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/device.rs @@ -0,0 +1,927 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the THIRD-PARTY file. + +/// This is the `VirtioDevice` implementation for our vsock device. It handles the virtio-level +/// device logic: feature negotiation, device configuration, and device activation. +/// The run-time device logic (i.e. event-driven data handling) is implemented by +/// `super::epoll_handler::EpollHandler`. +/// +/// We aim to conform to the VirtIO v1.1 spec: +/// https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.html +/// +/// The vsock device has two input parameters: a CID to identify the device, and a `VsockBackend` +/// to use for offloading vsock traffic. +/// +/// Upon its activation, the vsock device creates its `EpollHandler`, passes it the event-interested +/// file descriptors, and registers these descriptors with the VMM `EpollContext`. Going forward, +/// the `EpollHandler` will get notified whenever an event occurs on the just-registered FDs: +/// - an RX queue FD; +/// - a TX queue FD; +/// - an event queue FD; and +/// - a backend FD. +/// +use super::{VsockBackend, VsockPacket}; +use crate::seccomp_filters::Thread; +use crate::Error as DeviceError; +use crate::GuestMemoryMmap; +use crate::VirtioInterrupt; +use crate::{ + thread_helper::spawn_virtio_thread, ActivateResult, EpollHelper, EpollHelperError, + EpollHelperHandler, VirtioCommon, VirtioDevice, VirtioDeviceType, VirtioInterruptType, + EPOLL_HELPER_EVENT_LAST, VIRTIO_F_IN_ORDER, VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_VERSION_1, +}; +use anyhow::anyhow; +use byteorder::{ByteOrder, LittleEndian}; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::io; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::path::PathBuf; +use std::result; +use std::sync::atomic::AtomicBool; +use std::sync::{Arc, Barrier, RwLock}; +use virtio_queue::Queue; +use virtio_queue::QueueOwnedT; +use virtio_queue::QueueT; +use vm_memory::GuestAddressSpace; +use vm_memory::GuestMemoryAtomic; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vm_virtio::AccessPlatform; +use vmm_sys_util::eventfd::EventFd; + +const QUEUE_SIZE: u16 = 256; +const NUM_QUEUES: usize = 3; +const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE; NUM_QUEUES]; + +// New descriptors are pending on the rx queue. +pub const RX_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; +// New descriptors are pending on the tx queue. +pub const TX_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2; +// New descriptors are pending on the event queue. +pub const EVT_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 3; +// Notification coming from the backend. +pub const BACKEND_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 4; +// Host sock no-nested event +pub const MUXER_EPOLL_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 20; + +/// The `VsockEpollHandler` implements the runtime logic of our vsock device: +/// 1. Respond to TX queue events by wrapping virtio buffers into `VsockPacket`s, then sending those +/// packets to the `VsockBackend`; +/// 2. Forward backend FD event notifications to the `VsockBackend`; +/// 3. Fetch incoming packets from the `VsockBackend` and place them into the virtio RX queue; +/// 4. Whenever we have processed some virtio buffers (either TX or RX), let the driver know by +/// raising our assigned IRQ. +/// +/// In a nutshell, the `VsockEpollHandler` logic looks like this: +/// - on TX queue event: +/// - fetch all packets from the TX queue and send them to the backend; then +/// - if the backend has queued up any incoming packets, fetch them into any available RX buffers. +/// - on RX queue event: +/// - fetch any incoming packets, queued up by the backend, into newly available RX buffers. +/// - on backend event: +/// - forward the event to the backend; then +/// - again, attempt to fetch any incoming packets queued by the backend into virtio RX buffers. +/// +pub struct VsockEpollHandler { + pub mem: GuestMemoryAtomic, + pub queues: Vec, + pub queue_evts: Vec, + pub kill_evt: EventFd, + pub pause_evt: EventFd, + pub interrupt_cb: Arc, + pub backend: Arc>, + pub access_platform: Option>, +} + +impl VsockEpollHandler +where + B: VsockBackend, +{ + /// Signal the guest driver that we've used some virtio buffers that it had previously made + /// available. + /// + fn signal_used_queue(&self, queue_index: u16) -> result::Result<(), DeviceError> { + debug!("vsock: raising IRQ"); + + self.interrupt_cb + .trigger(VirtioInterruptType::Queue(queue_index)) + .map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + /// Walk the driver-provided RX queue buffers and attempt to fill them up with any data that we + /// have pending. + /// + fn process_rx(&mut self) -> result::Result<(), DeviceError> { + debug!("vsock: epoll_handler::process_rx()"); + + let mut used_descs = false; + + while let Some(mut desc_chain) = self.queues[0].pop_descriptor_chain(self.mem.memory()) { + let used_len = match VsockPacket::from_rx_virtq_head( + &mut desc_chain, + self.access_platform.as_ref(), + ) { + Ok(mut pkt) => { + if self.backend.write().unwrap().recv_pkt(&mut pkt).is_ok() { + pkt.hdr().len() as u32 + pkt.len() + } else { + // We are using a consuming iterator over the virtio buffers, so, if we can't + // fill in this buffer, we'll need to undo the last iterator step. + self.queues[0].go_to_previous_position(); + break; + } + } + Err(e) => { + warn!("vsock: RX queue error: {:?}", e); + 0 + } + }; + + self.queues[0] + .add_used(desc_chain.memory(), desc_chain.head_index(), used_len) + .map_err(DeviceError::QueueAddUsed)?; + used_descs = true; + } + + if used_descs { + self.signal_used_queue(0) + } else { + Ok(()) + } + } + + /// Walk the driver-provided TX queue buffers, package them up as vsock packets, and send them to + /// the backend for processing. + /// + fn process_tx(&mut self) -> result::Result<(), DeviceError> { + debug!("vsock: epoll_handler::process_tx()"); + + let mut used_descs = false; + + while let Some(mut desc_chain) = self.queues[1].pop_descriptor_chain(self.mem.memory()) { + let pkt = match VsockPacket::from_tx_virtq_head( + &mut desc_chain, + self.access_platform.as_ref(), + ) { + Ok(pkt) => pkt, + Err(e) => { + error!("vsock: error reading TX packet: {:?}", e); + self.queues[1] + .add_used(desc_chain.memory(), desc_chain.head_index(), 0) + .map_err(DeviceError::QueueAddUsed)?; + used_descs = true; + continue; + } + }; + + if self.backend.write().unwrap().send_pkt(&pkt).is_err() { + self.queues[1].go_to_previous_position(); + break; + } + + self.queues[1] + .add_used(desc_chain.memory(), desc_chain.head_index(), 0) + .map_err(DeviceError::QueueAddUsed)?; + used_descs = true; + } + + if used_descs { + self.signal_used_queue(1) + } else { + Ok(()) + } + } + + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.queue_evts[0].as_raw_fd(), RX_QUEUE_EVENT)?; + helper.add_event(self.queue_evts[1].as_raw_fd(), TX_QUEUE_EVENT)?; + helper.add_event(self.queue_evts[2].as_raw_fd(), EVT_QUEUE_EVENT)?; + + let muxer_epoll_nested = self.backend.read().unwrap().muxer_epoll_nested(); + if muxer_epoll_nested { + helper.add_event(self.backend.read().unwrap().get_polled_fd(), BACKEND_EVENT)?; + } else { + self.backend + .write() + .unwrap() + .set_epoll_helper_fd(helper.as_raw_fd()); + info!( + "add host sock to eventpoll fd {} to listener...", + helper.as_raw_fd() + ); + self.backend.write().unwrap().add_host_sock(); + } + + helper.run(paused, paused_sync, self)?; + + Ok(()) + } +} + +impl EpollHelperHandler for VsockEpollHandler +where + B: VsockBackend, +{ + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let evset = match epoll::Events::from_bits(event.events) { + Some(evset) => evset, + None => { + let evbits = event.events; + warn!("epoll: ignoring unknown event set: 0x{:x}", evbits); + return Ok(()); + } + }; + + let ev_type = event.data as u16; + match ev_type { + RX_QUEUE_EVENT => { + debug!("vsock: RX queue event"); + self.queue_evts[0].read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get RX queue event: {:?}", e)) + })?; + if self.backend.read().unwrap().has_pending_rx() { + self.process_rx().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process RX queue: {:?}", + e + )) + })?; + } + } + TX_QUEUE_EVENT => { + debug!("vsock: TX queue event"); + self.queue_evts[1].read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get TX queue event: {:?}", e)) + })?; + + self.process_tx().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to process TX queue: {:?}", e)) + })?; + + // The backend may have queued up responses to the packets we sent during TX queue + // processing. If that happened, we need to fetch those responses and place them + // into RX buffers. + if self.backend.read().unwrap().has_pending_rx() { + self.process_rx().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process RX queue: {:?}", + e + )) + })?; + } + } + EVT_QUEUE_EVENT => { + debug!("vsock: EVT queue event"); + self.queue_evts[2].read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get EVT queue event: {:?}", e)) + })?; + } + BACKEND_EVENT => { + debug!("vsock: backend event"); + self.backend.write().unwrap().notify(evset); + // After the backend has been kicked, it might've freed up some resources, so we + // can attempt to send it more data to process. + // In particular, if `self.backend.send_pkt()` halted the TX queue processing (by + // returning an error) at some point in the past, now is the time to try walking the + // TX queue again. + self.process_tx().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to process TX queue: {:?}", e)) + })?; + if self.backend.read().unwrap().has_pending_rx() { + self.process_rx().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process RX queue: {:?}", + e + )) + })?; + } + } + MUXER_EPOLL_EVENT => { + debug!("vsock: nested event"); + let fd = (event.data >> 32) as RawFd; + self.backend + .write() + .unwrap() + .dispatch_muxer_event(fd, evset); + + self.process_tx().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to process TX queue: {:?}", e)) + })?; + if self.backend.read().unwrap().has_pending_rx() { + self.process_rx().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process RX queue: {:?}", + e + )) + })?; + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unknown event for virtio-vsock {}", + ev_type + ))); + } + } + + Ok(()) + } +} + +/// Virtio device exposing virtual socket to the guest. +pub struct Vsock { + common: VirtioCommon, + id: String, + cid: u64, + backend: Arc>, + path: PathBuf, + seccomp_action: SeccompAction, + exit_evt: EventFd, +} + +#[derive(Serialize, Deserialize)] +pub struct VsockState { + pub avail_features: u64, + pub acked_features: u64, + #[serde(default)] + pub connections: Vec<(u32, u32)>, +} + +impl Vsock +where + B: VsockBackend, +{ + /// Create a new virtio-vsock device with the given VM CID and vsock + /// backend. + #[allow(clippy::too_many_arguments)] + pub fn new( + id: String, + cid: u64, + path: PathBuf, + mut backend: B, + iommu: bool, + seccomp_action: SeccompAction, + exit_evt: EventFd, + state: Option, + ) -> io::Result> { + let (avail_features, acked_features) = if let Some(state) = state { + debug!("Restoring virtio-vsock {}", id); + // Instead of letting the guest connection hang/timeout, proactively let + // the guest know the connection is gone. + backend.queue_rst_for_connections(state.connections.clone()); + (state.avail_features, state.acked_features) + } else { + let mut avail_features = 1u64 << VIRTIO_F_VERSION_1 | 1u64 << VIRTIO_F_IN_ORDER; + + if iommu { + avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM; + } + (avail_features, 0) + }; + + Ok(Vsock { + common: VirtioCommon { + device_type: VirtioDeviceType::Vsock as u32, + avail_features, + acked_features, + paused_sync: Some(Arc::new(Barrier::new(2))), + queue_sizes: QUEUE_SIZES.to_vec(), + min_queues: NUM_QUEUES as u16, + ..Default::default() + }, + id, + cid, + backend: Arc::new(RwLock::new(backend)), + path, + seccomp_action, + exit_evt, + }) + } + + fn state(&self) -> VsockState { + VsockState { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + connections: self.backend.read().unwrap().connections(), + } + } +} + +impl Drop for Vsock +where + B: VsockBackend, +{ + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +impl VirtioDevice for Vsock +where + B: VsockBackend + Sync + 'static, +{ + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn read_config(&self, offset: u64, data: &mut [u8]) { + match offset { + 0 if data.len() == 8 => LittleEndian::write_u64(data, self.cid), + 0 if data.len() == 4 => LittleEndian::write_u32(data, (self.cid & 0xffff_ffff) as u32), + 4 if data.len() == 4 => { + LittleEndian::write_u32(data, ((self.cid >> 32) & 0xffff_ffff) as u32) + } + _ => warn!( + "vsock: virtio-vsock received invalid read request of {} bytes at offset {}", + data.len(), + offset + ), + } + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let mut virtqueues = Vec::new(); + let mut queue_evts = Vec::new(); + for (_, queue, queue_evt) in queues { + virtqueues.push(queue); + queue_evts.push(queue_evt); + } + + let mut handler = VsockEpollHandler { + mem, + queues: virtqueues, + queue_evts, + kill_evt, + pause_evt, + interrupt_cb, + backend: self.backend.clone(), + access_platform: self.common.access_platform.clone(), + }; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + let mut epoll_threads = Vec::new(); + + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioVsock, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + + self.common.epoll_threads = Some(epoll_threads); + + event!("virtio-device", "activated", "id", &self.id); + Ok(()) + } + + fn reset(&mut self) -> Option> { + let result = self.common.reset(); + event!("virtio-device", "reset", "id", &self.id); + result + } + + fn shutdown(&mut self) { + std::fs::remove_file(&self.path).ok(); + } + + fn set_access_platform(&mut self, access_platform: Arc) { + self.common.set_access_platform(access_platform) + } +} + +impl Pausable for Vsock +where + B: VsockBackend + Sync + 'static, +{ + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume() + } +} + +impl Snapshottable for Vsock +where + B: VsockBackend + Sync + 'static, +{ + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + let mut snapshot = Snapshot::new_from_state(&self.id, &self.state())?; + snapshot.add_snapshot(self.backend.write().unwrap().snapshot()?); + Ok(snapshot) + } +} +impl Transportable for Vsock where B: VsockBackend + Sync + 'static {} +impl Migratable for Vsock where B: VsockBackend + Sync + 'static {} + +#[cfg(test)] +mod tests { + use super::super::tests::{NoopVirtioInterrupt, TestContext}; + use super::super::*; + use super::*; + use crate::vsock::device::{BACKEND_EVENT, EVT_QUEUE_EVENT, RX_QUEUE_EVENT, TX_QUEUE_EVENT}; + use crate::ActivateError; + use libc::EFD_NONBLOCK; + + #[test] + fn test_virtio_device() { + let mut ctx = TestContext::new(); + let avail_features = 1u64 << VIRTIO_F_VERSION_1 | 1u64 << VIRTIO_F_IN_ORDER; + let device_features = avail_features; + let driver_features: u64 = avail_features | 1 | (1 << 32); + let device_pages = [ + (device_features & 0xffff_ffff) as u32, + (device_features >> 32) as u32, + ]; + let driver_pages = [ + (driver_features & 0xffff_ffff) as u32, + (driver_features >> 32) as u32, + ]; + assert_eq!(ctx.device.device_type(), VirtioDeviceType::Vsock as u32); + assert_eq!(ctx.device.queue_max_sizes(), QUEUE_SIZES); + assert_eq!(ctx.device.features() as u32, device_pages[0]); + assert_eq!((ctx.device.features() >> 32) as u32, device_pages[1]); + + // Ack device features, page 0. + ctx.device.ack_features(u64::from(driver_pages[0])); + // Ack device features, page 1. + ctx.device.ack_features(u64::from(driver_pages[1]) << 32); + // Check that no side effect are present, and that the acked features are exactly the same + // as the device features. + assert_eq!( + ctx.device.common.acked_features, + device_features & driver_features + ); + + // Test reading 32-bit chunks. + let mut data = [0u8; 8]; + ctx.device.read_config(0, &mut data[..4]); + assert_eq!( + u64::from(LittleEndian::read_u32(&data)), + ctx.cid & 0xffff_ffff + ); + ctx.device.read_config(4, &mut data[4..]); + assert_eq!( + u64::from(LittleEndian::read_u32(&data[4..])), + (ctx.cid >> 32) & 0xffff_ffff + ); + + // Test reading 64-bit. + let mut data = [0u8; 8]; + ctx.device.read_config(0, &mut data); + assert_eq!(LittleEndian::read_u64(&data), ctx.cid); + + // Check that out-of-bounds reading doesn't mutate the destination buffer. + let mut data = [0u8, 1, 2, 3, 4, 5, 6, 7]; + ctx.device.read_config(2, &mut data); + assert_eq!(data, [0u8, 1, 2, 3, 4, 5, 6, 7]); + + // Just covering lines here, since the vsock device has no writable config. + // A warning is, however, logged, if the guest driver attempts to write any config data. + ctx.device.write_config(0, &data[..4]); + + let memory = GuestMemoryAtomic::new(ctx.mem.clone()); + + // Test a bad activation. + let bad_activate = + ctx.device + .activate(memory.clone(), Arc::new(NoopVirtioInterrupt {}), Vec::new()); + match bad_activate { + Err(ActivateError::BadActivate) => (), + other => panic!("{:?}", other), + } + + // Test a correct activation. + ctx.device + .activate( + memory, + Arc::new(NoopVirtioInterrupt {}), + vec![ + ( + 0, + Queue::new(256).unwrap(), + EventFd::new(EFD_NONBLOCK).unwrap(), + ), + ( + 1, + Queue::new(256).unwrap(), + EventFd::new(EFD_NONBLOCK).unwrap(), + ), + ( + 2, + Queue::new(256).unwrap(), + EventFd::new(EFD_NONBLOCK).unwrap(), + ), + ], + ) + .unwrap(); + } + + #[test] + fn test_irq() { + // Test case: successful IRQ signaling. + { + let test_ctx = TestContext::new(); + let ctx = test_ctx.create_epoll_handler_context(); + + let _queue: Queue = Queue::new(256).unwrap(); + assert!(ctx.handler.signal_used_queue(0).is_ok()); + } + } + + #[test] + fn test_txq_event() { + // Test case: + // - the driver has something to send (there's data in the TX queue); and + // - the backend has no pending RX data. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + ctx.handler.backend.write().unwrap().set_pending_rx(false); + ctx.signal_txq_event(); + + // The available TX descriptor should have been used. + assert_eq!(ctx.guest_txvq.used.idx.get(), 1); + // The available RX descriptor should be untouched. + assert_eq!(ctx.guest_rxvq.used.idx.get(), 0); + } + + // Test case: + // - the driver has something to send (there's data in the TX queue); and + // - the backend also has some pending RX data. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + ctx.handler.backend.write().unwrap().set_pending_rx(true); + ctx.signal_txq_event(); + + // Both available RX and TX descriptors should have been used. + assert_eq!(ctx.guest_txvq.used.idx.get(), 1); + assert_eq!(ctx.guest_rxvq.used.idx.get(), 1); + } + + // Test case: + // - the driver has something to send (there's data in the TX queue); and + // - the backend errors out and cannot process the TX queue. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + ctx.handler.backend.write().unwrap().set_pending_rx(false); + ctx.handler + .backend + .write() + .unwrap() + .set_tx_err(Some(VsockError::NoData)); + ctx.signal_txq_event(); + + // Both RX and TX queues should be untouched. + assert_eq!(ctx.guest_txvq.used.idx.get(), 0); + assert_eq!(ctx.guest_rxvq.used.idx.get(), 0); + } + + // Test case: + // - the driver supplied a malformed TX buffer. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + // Invalidate the packet header descriptor, by setting its length to 0. + ctx.guest_txvq.dtable[0].len.set(0); + ctx.signal_txq_event(); + + // The available descriptor should have been consumed, but no packet should have + // reached the backend. + assert_eq!(ctx.guest_txvq.used.idx.get(), 1); + assert_eq!(ctx.handler.backend.read().unwrap().tx_ok_cnt, 0); + } + + // Test case: spurious TXQ_EVENT. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + let events = epoll::Events::EPOLLIN; + let event = epoll::Event::new(events, TX_QUEUE_EVENT as u64); + let mut epoll_helper = + EpollHelper::new(&ctx.handler.kill_evt, &ctx.handler.pause_evt).unwrap(); + + assert!( + ctx.handler.handle_event(&mut epoll_helper, &event).is_err(), + "handle_event() should have failed" + ); + } + } + + #[test] + fn test_rxq_event() { + // Test case: + // - there is pending RX data in the backend; and + // - the driver makes RX buffers available; and + // - the backend successfully places its RX data into the queue. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + ctx.handler.backend.write().unwrap().set_pending_rx(true); + ctx.handler + .backend + .write() + .unwrap() + .set_rx_err(Some(VsockError::NoData)); + ctx.signal_rxq_event(); + + // The available RX buffer should've been left untouched. + assert_eq!(ctx.guest_rxvq.used.idx.get(), 0); + } + + // Test case: + // - there is pending RX data in the backend; and + // - the driver makes RX buffers available; and + // - the backend errors out, when attempting to receive data. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + ctx.handler.backend.write().unwrap().set_pending_rx(true); + ctx.signal_rxq_event(); + + // The available RX buffer should have been used. + assert_eq!(ctx.guest_rxvq.used.idx.get(), 1); + } + + // Test case: the driver provided a malformed RX descriptor chain. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + // Invalidate the packet header descriptor, by setting its length to 0. + ctx.guest_rxvq.dtable[0].len.set(0); + + // The chain should've been processed, without employing the backend. + assert!(ctx.handler.process_rx().is_ok()); + assert_eq!(ctx.guest_rxvq.used.idx.get(), 1); + assert_eq!(ctx.handler.backend.read().unwrap().rx_ok_cnt, 0); + } + + // Test case: spurious RXQ_EVENT. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + ctx.handler.backend.write().unwrap().set_pending_rx(false); + + let events = epoll::Events::EPOLLIN; + let event = epoll::Event::new(events, RX_QUEUE_EVENT as u64); + let mut epoll_helper = + EpollHelper::new(&ctx.handler.kill_evt, &ctx.handler.pause_evt).unwrap(); + + assert!( + ctx.handler.handle_event(&mut epoll_helper, &event).is_err(), + "handle_event() should have failed" + ); + } + } + + #[test] + fn test_evq_event() { + // Test case: spurious EVQ_EVENT. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + ctx.handler.backend.write().unwrap().set_pending_rx(false); + + let events = epoll::Events::EPOLLIN; + let event = epoll::Event::new(events, EVT_QUEUE_EVENT as u64); + let mut epoll_helper = + EpollHelper::new(&ctx.handler.kill_evt, &ctx.handler.pause_evt).unwrap(); + + assert!( + ctx.handler.handle_event(&mut epoll_helper, &event).is_err(), + "handle_event() should have failed" + ); + } + } + + #[test] + fn test_backend_event() { + // Test case: + // - a backend event is received; and + // - the backend has pending RX data. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + ctx.handler.backend.write().unwrap().set_pending_rx(true); + + let events = epoll::Events::EPOLLIN; + let event = epoll::Event::new(events, BACKEND_EVENT as u64); + let mut epoll_helper = + EpollHelper::new(&ctx.handler.kill_evt, &ctx.handler.pause_evt).unwrap(); + assert!(ctx.handler.handle_event(&mut epoll_helper, &event).is_ok()); + + // The backend should've received this event. + assert_eq!( + ctx.handler.backend.read().unwrap().evset, + Some(epoll::Events::EPOLLIN) + ); + // TX queue processing should've been triggered. + assert_eq!(ctx.guest_txvq.used.idx.get(), 1); + // RX queue processing should've been triggered. + assert_eq!(ctx.guest_rxvq.used.idx.get(), 1); + } + + // Test case: + // - a backend event is received; and + // - the backend doesn't have any pending RX data. + { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + ctx.handler.backend.write().unwrap().set_pending_rx(false); + + let events = epoll::Events::EPOLLIN; + let event = epoll::Event::new(events, BACKEND_EVENT as u64); + let mut epoll_helper = + EpollHelper::new(&ctx.handler.kill_evt, &ctx.handler.pause_evt).unwrap(); + assert!(ctx.handler.handle_event(&mut epoll_helper, &event).is_ok()); + + // The backend should've received this event. + assert_eq!( + ctx.handler.backend.read().unwrap().evset, + Some(epoll::Events::EPOLLIN) + ); + // TX queue processing should've been triggered. + assert_eq!(ctx.guest_txvq.used.idx.get(), 1); + // The RX queue should've been left untouched. + assert_eq!(ctx.guest_rxvq.used.idx.get(), 0); + } + } + + #[test] + fn test_unknown_event() { + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_epoll_handler_context(); + + let events = epoll::Events::EPOLLIN; + let event = epoll::Event::new(events, 0xff); + let mut epoll_helper = + EpollHelper::new(&ctx.handler.kill_evt, &ctx.handler.pause_evt).unwrap(); + + assert!( + ctx.handler.handle_event(&mut epoll_helper, &event).is_err(), + "handle_event() should have failed" + ); + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/mod.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/mod.rs new file mode 100644 index 0000000..b3cde96 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/mod.rs @@ -0,0 +1,396 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the THIRD-PARTY file. + +mod csm; +mod device; +mod packet; +mod unix; + +pub use self::device::Vsock; +pub use self::unix::VsockUnixBackend; +pub use self::unix::VsockUnixError; + +pub use packet::VsockPacket; +use std::os::unix::io::RawFd; +use vm_migration::Snapshottable; + +mod defs { + + /// Max vsock packet data/buffer size. + pub const MAX_PKT_BUF_SIZE: usize = 64 * 1024; + + pub mod uapi { + + /// Vsock packet operation IDs. + /// Defined in `/include/uapi/linux/virtio_vsock.h`. + /// + /// Connection invalid. + pub const VSOCK_OP_INVALID: u16 = 0; + /// Connection request. + pub const VSOCK_OP_REQUEST: u16 = 1; + /// Connection response. + pub const VSOCK_OP_RESPONSE: u16 = 2; + /// Connection reset. + pub const VSOCK_OP_RST: u16 = 3; + /// Connection clean shutdown. + pub const VSOCK_OP_SHUTDOWN: u16 = 4; + /// Connection data (read/write). + pub const VSOCK_OP_RW: u16 = 5; + /// Flow control credit update. + pub const VSOCK_OP_CREDIT_UPDATE: u16 = 6; + /// Flow control credit update request. + pub const VSOCK_OP_CREDIT_REQUEST: u16 = 7; + + /// Vsock packet flags. + /// Defined in `/include/uapi/linux/virtio_vsock.h`. + /// + /// Valid with a VSOCK_OP_SHUTDOWN packet: the packet sender will receive no more data. + pub const VSOCK_FLAGS_SHUTDOWN_RCV: u32 = 1; + /// Valid with a VSOCK_OP_SHUTDOWN packet: the packet sender will send no more data. + pub const VSOCK_FLAGS_SHUTDOWN_SEND: u32 = 2; + + /// Vsock packet type. + /// Defined in `/include/uapi/linux/virtio_vsock.h`. + /// + /// Stream / connection-oriented packet (the only currently valid type). + pub const VSOCK_TYPE_STREAM: u16 = 1; + + pub const VSOCK_TYPE_SEQPACKET: u16 = 2; + + pub const VSOCK_HOST_CID: u64 = 2; + } +} + +#[derive(Debug)] +pub enum VsockError { + /// The vsock data/buffer virtio descriptor length is smaller than expected. + BufDescTooSmall, + /// The vsock data/buffer virtio descriptor is expected, but missing. + BufDescMissing, + /// Chained GuestMemory error. + GuestMemory, + /// Bounds check failed on guest memory pointer. + GuestMemoryBounds, + /// The vsock header descriptor length is too small. + HdrDescTooSmall(u32), + /// The vsock header descriptor is expected, but missing. + HdrDescMissing, + /// The vsock header `len` field holds an invalid value. + InvalidPktLen(u32), + /// A data fetch was attempted when no data was available. + NoData, + /// A data buffer was expected for the provided packet, but it is missing. + PktBufMissing, + /// Encountered an unexpected write-only virtio descriptor. + UnreadableDescriptor, + /// Encountered an unexpected read-only virtio descriptor. + UnwritableDescriptor, +} +type Result = std::result::Result; + +#[derive(Debug)] +pub enum VsockEpollHandlerError { + /// The vsock data/buffer virtio descriptor length is smaller than expected. + BufDescTooSmall, + /// The vsock data/buffer virtio descriptor is expected, but missing. + BufDescMissing, + /// Chained GuestMemory error. + GuestMemory, + /// Bounds check failed on guest memory pointer. + GuestMemoryBounds, + /// The vsock header descriptor length is too small. + HdrDescTooSmall(u32), + /// The vsock header `len` field holds an invalid value. + InvalidPktLen(u32), + /// A data fetch was attempted when no data was available. + NoData, + /// A data buffer was expected for the provided packet, but it is missing. + PktBufMissing, + /// Encountered an unexpected write-only virtio descriptor. + UnreadableDescriptor, + /// Encountered an unexpected read-only virtio descriptor. + UnwritableDescriptor, +} + +/// A passive, event-driven object, that needs to be notified whenever an epoll-able event occurs. +/// An event-polling control loop will use `get_polled_fd()` and `get_polled_evset()` to query +/// the listener for the file descriptor and the set of events it's interested in. When such an +/// event occurs, the control loop will route the event to the listener via `notify()`. +/// +pub trait VsockEpollListener { + /// Get the file descriptor the listener needs polled. + fn get_polled_fd(&self) -> RawFd; + + /// Get the set of events for which the listener wants to be notified. + fn get_polled_evset(&self) -> epoll::Events; + + /// Notify the listener that one ore more events have occurred. + fn notify(&mut self, evset: epoll::Events); + + /// If muxer epoll is nested. + fn muxer_epoll_nested(&self) -> bool { + true + } + + /// Set epoll helper fd to muxer + fn set_epoll_helper_fd(&mut self, _epfd: RawFd) {} + + /// add host_sock fd to listener map + fn add_host_sock(&mut self) {} + + /// Handle host socket event + fn dispatch_muxer_event(&mut self, _fd: RawFd, _event_set: epoll::Events) {} +} + +/// Any channel that handles vsock packet traffic: sending and receiving packets. Since we're +/// implementing the device model here, our responsibility is to always process the sending of +/// packets (i.e. the TX queue). So, any locally generated data, addressed to the driver (e.g. +/// a connection response or RST), will have to be queued, until we get to processing the RX queue. +/// +/// Note: `recv_pkt()` and `send_pkt()` are named analogous to `Read::read()` and `Write::write()`, +/// respectively. I.e. +/// - `recv_pkt(&mut pkt)` will read data from the channel, and place it into `pkt`; and +/// - `send_pkt(&pkt)` will fetch data from `pkt`, and place it into the channel. +pub trait VsockChannel { + /// Read/receive an incoming packet from the channel. + fn recv_pkt(&mut self, pkt: &mut VsockPacket) -> Result<()>; + + /// Write/send a packet through the channel. + fn send_pkt(&mut self, pkt: &VsockPacket) -> Result<()>; + + /// Checks whether there is pending incoming data inside the channel, meaning that a subsequent + /// call to `recv_pkt()` won't fail. + fn has_pending_rx(&self) -> bool; +} + +/// The vsock backend, which is basically an epoll-event-driven vsock channel, that needs to be +/// sendable through a mpsc channel (the latter due to how `vmm::EpollContext` works). +/// Currently, the only implementation we have is `crate::virtio::unix::muxer::VsockMuxer`, which +/// translates guest-side vsock connections to host-side Unix domain socket connections. +pub trait VsockBackend: VsockChannel + Snapshottable + VsockEpollListener + Send { + fn connections(&self) -> Vec<(u32, u32)> { + Vec::new() + } + fn queue_rst_for_connections(&mut self, _conns: Vec<(u32, u32)>) {} +} + +#[cfg(test)] +mod tests { + use super::device::{VsockEpollHandler, RX_QUEUE_EVENT, TX_QUEUE_EVENT}; + use super::packet::VSOCK_PKT_HDR_SIZE; + use super::*; + use crate::device::{VirtioInterrupt, VirtioInterruptType}; + use crate::epoll_helper::EpollHelperHandler; + use crate::EpollHelper; + use crate::GuestMemoryMmap; + use libc::EFD_NONBLOCK; + use std::os::unix::io::AsRawFd; + use std::path::PathBuf; + use std::sync::{Arc, RwLock}; + use virtio_bindings::bindings::virtio_ring::{VRING_DESC_F_NEXT, VRING_DESC_F_WRITE}; + use vm_memory::{GuestAddress, GuestMemoryAtomic}; + use vm_migration::Pausable; + use vm_virtio::queue::testing::VirtQueue as GuestQ; + use vmm_sys_util::eventfd::EventFd; + + pub struct NoopVirtioInterrupt {} + + impl VirtioInterrupt for NoopVirtioInterrupt { + fn trigger( + &self, + _int_type: VirtioInterruptType, + ) -> std::result::Result<(), std::io::Error> { + Ok(()) + } + } + + pub struct TestBackend { + pub evfd: EventFd, + pub rx_err: Option, + pub tx_err: Option, + pub pending_rx: bool, + pub rx_ok_cnt: usize, + pub tx_ok_cnt: usize, + pub evset: Option, + } + impl TestBackend { + pub fn new() -> Self { + Self { + evfd: EventFd::new(EFD_NONBLOCK).unwrap(), + rx_err: None, + tx_err: None, + pending_rx: false, + rx_ok_cnt: 0, + tx_ok_cnt: 0, + evset: None, + } + } + pub fn set_rx_err(&mut self, err: Option) { + self.rx_err = err; + } + pub fn set_tx_err(&mut self, err: Option) { + self.tx_err = err; + } + pub fn set_pending_rx(&mut self, prx: bool) { + self.pending_rx = prx; + } + } + impl VsockChannel for TestBackend { + fn recv_pkt(&mut self, _pkt: &mut VsockPacket) -> Result<()> { + match self.rx_err.take() { + None => { + self.rx_ok_cnt += 1; + Ok(()) + } + Some(e) => Err(e), + } + } + fn send_pkt(&mut self, _pkt: &VsockPacket) -> Result<()> { + match self.tx_err.take() { + None => { + self.tx_ok_cnt += 1; + Ok(()) + } + Some(e) => Err(e), + } + } + fn has_pending_rx(&self) -> bool { + self.pending_rx + } + } + impl VsockEpollListener for TestBackend { + fn get_polled_fd(&self) -> RawFd { + self.evfd.as_raw_fd() + } + fn get_polled_evset(&self) -> epoll::Events { + epoll::Events::EPOLLIN + } + fn notify(&mut self, evset: epoll::Events) { + self.evset = Some(evset); + } + } + + impl Pausable for TestBackend {} + impl Snapshottable for TestBackend {} + impl VsockBackend for TestBackend {} + + pub struct TestContext { + pub cid: u64, + pub mem: GuestMemoryMmap, + pub mem_size: usize, + pub device: Vsock, + } + + impl TestContext { + pub fn new() -> Self { + const CID: u64 = 52; + const MEM_SIZE: usize = 1024 * 1024 * 128; + Self { + cid: CID, + mem: GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE)]).unwrap(), + mem_size: MEM_SIZE, + device: Vsock::new( + String::from("vsock"), + CID, + PathBuf::from("/test/sock"), + TestBackend::new(), + false, + seccompiler::SeccompAction::Trap, + EventFd::new(EFD_NONBLOCK).unwrap(), + None, + ) + .unwrap(), + } + } + + pub fn create_epoll_handler_context(&self) -> EpollHandlerContext { + const QSIZE: u16 = 2; + + let guest_rxvq = GuestQ::new(GuestAddress(0x0010_0000), &self.mem, QSIZE); + let guest_txvq = GuestQ::new(GuestAddress(0x0020_0000), &self.mem, QSIZE); + let guest_evvq = GuestQ::new(GuestAddress(0x0030_0000), &self.mem, QSIZE); + let rxvq = guest_rxvq.create_queue(); + let txvq = guest_txvq.create_queue(); + let evvq = guest_evvq.create_queue(); + + // Set up one available descriptor in the RX queue. + guest_rxvq.dtable[0].set( + 0x0040_0000, + VSOCK_PKT_HDR_SIZE as u32, + (VRING_DESC_F_WRITE | VRING_DESC_F_NEXT).try_into().unwrap(), + 1, + ); + guest_rxvq.dtable[1].set(0x0040_1000, 4096, VRING_DESC_F_WRITE.try_into().unwrap(), 0); + guest_rxvq.avail.ring[0].set(0); + guest_rxvq.avail.idx.set(1); + + // Set up one available descriptor in the TX queue. + guest_txvq.dtable[0].set( + 0x0050_0000, + VSOCK_PKT_HDR_SIZE as u32, + VRING_DESC_F_NEXT.try_into().unwrap(), + 1, + ); + guest_txvq.dtable[1].set(0x0050_1000, 4096, 0, 0); + guest_txvq.avail.ring[0].set(0); + guest_txvq.avail.idx.set(1); + + let queues = vec![rxvq, txvq, evvq]; + let queue_evts = vec![ + EventFd::new(EFD_NONBLOCK).unwrap(), + EventFd::new(EFD_NONBLOCK).unwrap(), + EventFd::new(EFD_NONBLOCK).unwrap(), + ]; + let interrupt_cb = Arc::new(NoopVirtioInterrupt {}); + + EpollHandlerContext { + guest_rxvq, + guest_txvq, + guest_evvq, + handler: VsockEpollHandler { + mem: GuestMemoryAtomic::new(self.mem.clone()), + queues, + queue_evts, + kill_evt: EventFd::new(EFD_NONBLOCK).unwrap(), + pause_evt: EventFd::new(EFD_NONBLOCK).unwrap(), + interrupt_cb, + backend: Arc::new(RwLock::new(TestBackend::new())), + access_platform: None, + }, + } + } + } + + pub struct EpollHandlerContext<'a> { + pub handler: VsockEpollHandler, + pub guest_rxvq: GuestQ<'a>, + pub guest_txvq: GuestQ<'a>, + pub guest_evvq: GuestQ<'a>, + } + + impl<'a> EpollHandlerContext<'a> { + pub fn signal_txq_event(&mut self) { + self.handler.queue_evts[1].write(1).unwrap(); + let events = epoll::Events::EPOLLIN; + let event = epoll::Event::new(events, TX_QUEUE_EVENT as u64); + let mut epoll_helper = + EpollHelper::new(&self.handler.kill_evt, &self.handler.pause_evt).unwrap(); + self.handler.handle_event(&mut epoll_helper, &event).ok(); + } + pub fn signal_rxq_event(&mut self) { + self.handler.queue_evts[0].write(1).unwrap(); + let events = epoll::Events::EPOLLIN; + let event = epoll::Event::new(events, RX_QUEUE_EVENT as u64); + let mut epoll_helper = + EpollHelper::new(&self.handler.kill_evt, &self.handler.pause_evt).unwrap(); + self.handler.handle_event(&mut epoll_helper, &event).ok(); + } + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/packet.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/packet.rs new file mode 100644 index 0000000..2be48fc --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/packet.rs @@ -0,0 +1,803 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// + +/// `VsockPacket` provides a thin wrapper over the buffers exchanged via virtio queues. +/// There are two components to a vsock packet, each using its own descriptor in a +/// virtio queue: +/// - the packet header; and +/// - the packet data/buffer. +/// There is a 1:1 relation between descriptor chains and packets: the first (chain head) holds +/// the header, and an optional second descriptor holds the data. The second descriptor is only +/// present for data packets (VSOCK_OP_RW). +/// +/// `VsockPacket` wraps these two buffers and provides direct access to the data stored +/// in guest memory. This is done to avoid unnecessarily copying data from guest memory +/// to temporary buffers, before passing it on to the vsock backend. +/// +use byteorder::{ByteOrder, LittleEndian}; +use std::fmt::{Debug, Error, Formatter}; +use std::ops::Deref; +use std::result; +use std::sync::Arc; + +use super::defs; +use super::{Result, VsockError}; +use crate::get_host_address_range; +use crate::vsock::defs::uapi; +use virtio_queue::DescriptorChain; +use vm_memory::{Address, GuestMemory}; +use vm_virtio::{AccessPlatform, Translatable}; +// The vsock packet header is defined by the C struct: +// +// ```C +// struct virtio_vsock_hdr { +// le64 src_cid; +// le64 dst_cid; +// le32 src_port; +// le32 dst_port; +// le32 len; +// le16 type; +// le16 op; +// le32 flags; +// le32 buf_alloc; +// le32 fwd_cnt; +// }; +// ``` +// +// This structed will occupy the buffer pointed to by the head descriptor. We'll be accessing it +// as a byte slice. To that end, we define below the offsets for each field struct, as well as the +// packed struct size, as a bunch of `usize` consts. +// Note that these offsets are only used privately by the `VsockPacket` struct, the public interface +// consisting of getter and setter methods, for each struct field, that will also handle the correct +// endianess. + +/// The vsock packet header struct size (when packed). +pub const VSOCK_PKT_HDR_SIZE: usize = 44; + +// Source CID. +const HDROFF_SRC_CID: usize = 0; + +// Destination CID. +const HDROFF_DST_CID: usize = 8; + +// Source port. +const HDROFF_SRC_PORT: usize = 16; + +// Destination port. +const HDROFF_DST_PORT: usize = 20; + +// Data length (in bytes) - may be 0, if there is no data buffer. +const HDROFF_LEN: usize = 24; + +// Socket type. Currently, only connection-oriented streams are defined by the vsock protocol. +const HDROFF_TYPE: usize = 28; + +// Operation ID - one of the VSOCK_OP_* values; e.g. +// - VSOCK_OP_RW: a data packet; +// - VSOCK_OP_REQUEST: connection request; +// - VSOCK_OP_RST: forceful connection termination; +// etc (see `super::defs::uapi` for the full list). +const HDROFF_OP: usize = 30; + +// Additional options (flags) associated with the current operation (`op`). +// Currently, only used with shutdown requests (VSOCK_OP_SHUTDOWN). +const HDROFF_FLAGS: usize = 32; + +// Size (in bytes) of the packet sender receive buffer (for the connection to which this packet +// belongs). +const HDROFF_BUF_ALLOC: usize = 36; + +// Number of bytes the sender has received and consumed (for the connection to which this packet +// belongs). For instance, for our Unix backend, this counter would be the total number of bytes +// we have successfully written to a backing Unix socket. +const HDROFF_FWD_CNT: usize = 40; + +#[allow(dead_code)] +#[derive(Clone, Copy)] +#[repr(C)] +pub struct VirtioVsockHdr { + src_cid: u64, + dst_cid: u64, + src_port: u32, + dst_port: u32, + len: u32, + type_field: u16, + op: u16, + flags: u32, + buf_alloc: u32, + fwd_cnt: u32, +} + +impl VirtioVsockHdr { + pub fn from_slice(data: &[u8]) -> Option { + let raw_hdr: &VirtioVsockHdr; + // SAFETY: data is a slice of the packet header + unsafe { + let ptr = data.as_ptr() as *const VirtioVsockHdr; + raw_hdr = &*ptr; + } + + let hdr = Self { + src_cid: u64::from_le(raw_hdr.src_cid), + dst_cid: u64::from_le(raw_hdr.dst_cid), + src_port: u32::from_le(raw_hdr.src_port), + dst_port: u32::from_le(raw_hdr.dst_port), + len: u32::from_le(raw_hdr.len), + type_field: u16::from_le(raw_hdr.type_field), + op: u16::from_le(raw_hdr.op), + flags: u32::from_le(raw_hdr.flags), + buf_alloc: u32::from_le(raw_hdr.buf_alloc), + fwd_cnt: u32::from_le(raw_hdr.fwd_cnt), + }; + Some(hdr) + } +} + +impl Debug for VirtioVsockHdr { + fn fmt(&self, f: &mut Formatter<'_>) -> result::Result<(), Error> { + let type_field = match self.type_field { + uapi::VSOCK_TYPE_STREAM => "VSOCK_TYPE_STREAM", + uapi::VSOCK_TYPE_SEQPACKET => "VSOCK_TYPE_SEQPACKET", + _ => "UNKNOWN_TYPE", + }; + let op = match self.op { + uapi::VSOCK_OP_INVALID => "VIRTIO_VSOCK_OP_INVALID", + uapi::VSOCK_OP_REQUEST => "VIRTIO_VSOCK_OP_REQUEST", + uapi::VSOCK_OP_RESPONSE => "VIRTIO_VSOCK_OP_RESPONSE", + uapi::VSOCK_OP_RST => "VIRTIO_VSOCK_OP_RST", + uapi::VSOCK_OP_SHUTDOWN => "VIRTIO_VSOCK_OP_SHUTDOWN", + uapi::VSOCK_OP_RW => "VIRTIO_VSOCK_OP_RW", + uapi::VSOCK_OP_CREDIT_UPDATE => "VIRTIO_VSOCK_OP_CREDIT_UPDATE", + uapi::VSOCK_OP_CREDIT_REQUEST => "VIRTIO_VSOCK_OP_CREDIT_REQUEST", + _ => "UNKNOWN_OP", + }; + f.debug_struct("VirtioVsockHdr") + .field("src_cid", &self.src_cid) + .field("dst_cid", &self.dst_cid) + .field("src_port", &self.src_port) + .field("dst_port", &self.dst_port) + .field("len", &self.len) + .field("type_field", &type_field) + .field("op", &op) + .field("flags", &self.flags) + .field("buf_alloc", &self.buf_alloc) + .field("fwd_cnt", &self.fwd_cnt) + .finish() + } +} + +/// The vsock packet, implemented as a wrapper over a virtq descriptor chain: +/// - the chain head, holding the packet header; and +/// - (an optional) data/buffer descriptor, only present for data packets (VSOCK_OP_RW). +/// +pub struct VsockPacket { + hdr: *mut u8, + buf: Option<*mut u8>, + buf_size: usize, +} + +impl VsockPacket { + /// Create the packet wrapper from a TX virtq chain head. + /// + /// The chain head is expected to hold valid packet header data. A following packet buffer + /// descriptor can optionally end the chain. Bounds and pointer checks are performed when + /// creating the wrapper. + /// + pub fn from_tx_virtq_head( + desc_chain: &mut DescriptorChain, + access_platform: Option<&Arc>, + ) -> Result + where + M: Clone + Deref, + M::Target: GuestMemory, + { + let head = desc_chain.next().ok_or(VsockError::HdrDescMissing)?; + + // All buffers in the TX queue must be readable. + // + if head.is_write_only() { + return Err(VsockError::UnreadableDescriptor); + } + + // The packet header should fit inside the head descriptor. + if head.len() < VSOCK_PKT_HDR_SIZE as u32 { + return Err(VsockError::HdrDescTooSmall(head.len())); + } + + let mut pkt = Self { + hdr: get_host_address_range( + desc_chain.memory(), + head.addr() + .translate_gva(access_platform, VSOCK_PKT_HDR_SIZE), + VSOCK_PKT_HDR_SIZE, + ) + .ok_or(VsockError::GuestMemory)?, + buf: None, + buf_size: 0, + }; + + // No point looking for a data/buffer descriptor, if the packet is zero-lengthed. + if pkt.is_empty() { + return Ok(pkt); + } + + // Reject weirdly-sized packets. + // + if pkt.len() > defs::MAX_PKT_BUF_SIZE as u32 { + return Err(VsockError::InvalidPktLen(pkt.len())); + } + + // Prior to Linux v6.3 there are two descriptors + if head.has_next() { + let buf_desc = desc_chain.next().ok_or(VsockError::BufDescMissing)?; + + // TX data should be read-only. + if buf_desc.is_write_only() { + return Err(VsockError::UnreadableDescriptor); + } + + // The data buffer should be large enough to fit the size of the data, as described by + // the header descriptor. + if buf_desc.len() < pkt.len() { + return Err(VsockError::BufDescTooSmall); + } + let buf_size = buf_desc.len() as usize; + pkt.buf_size = buf_size; + pkt.buf = Some( + get_host_address_range( + desc_chain.memory(), + buf_desc.addr().translate_gva(access_platform, buf_size), + pkt.buf_size, + ) + .ok_or(VsockError::GuestMemory)?, + ); + } else { + let buf_size: usize = head.len() as usize - VSOCK_PKT_HDR_SIZE; + pkt.buf_size = buf_size; + pkt.buf = Some( + get_host_address_range( + desc_chain.memory(), + head.addr() + .checked_add(VSOCK_PKT_HDR_SIZE as u64) + .unwrap() + .translate_gva(access_platform, buf_size), + buf_size, + ) + .ok_or(VsockError::GuestMemory)?, + ); + } + + Ok(pkt) + } + + /// Create the packet wrapper from an RX virtq chain head. + /// + /// There must be two descriptors in the chain, both writable: a header descriptor and a data + /// descriptor. Bounds and pointer checks are performed when creating the wrapper. + /// + pub fn from_rx_virtq_head( + desc_chain: &mut DescriptorChain, + access_platform: Option<&Arc>, + ) -> Result + where + M: Clone + Deref, + M::Target: GuestMemory, + { + let head = desc_chain.next().ok_or(VsockError::HdrDescMissing)?; + + // All RX buffers must be writable. + // + if !head.is_write_only() { + return Err(VsockError::UnwritableDescriptor); + } + + // The packet header should fit inside the head descriptor. + if head.len() < VSOCK_PKT_HDR_SIZE as u32 { + return Err(VsockError::HdrDescTooSmall(head.len())); + } + + // Prior to Linux v6.3 there are two descriptors + if head.has_next() { + let buf_desc = desc_chain.next().ok_or(VsockError::BufDescMissing)?; + let buf_size = buf_desc.len() as usize; + + Ok(Self { + hdr: get_host_address_range( + desc_chain.memory(), + head.addr() + .translate_gva(access_platform, VSOCK_PKT_HDR_SIZE), + VSOCK_PKT_HDR_SIZE, + ) + .ok_or(VsockError::GuestMemory)?, + buf: Some( + get_host_address_range( + desc_chain.memory(), + buf_desc.addr().translate_gva(access_platform, buf_size), + buf_size, + ) + .ok_or(VsockError::GuestMemory)?, + ), + buf_size, + }) + } else { + let buf_size: usize = head.len() as usize - VSOCK_PKT_HDR_SIZE; + Ok(Self { + hdr: get_host_address_range( + desc_chain.memory(), + head.addr() + .translate_gva(access_platform, VSOCK_PKT_HDR_SIZE), + VSOCK_PKT_HDR_SIZE, + ) + .ok_or(VsockError::GuestMemory)?, + buf: Some( + get_host_address_range( + desc_chain.memory(), + head.addr() + .checked_add(VSOCK_PKT_HDR_SIZE as u64) + .unwrap() + .translate_gva(access_platform, buf_size), + buf_size, + ) + .ok_or(VsockError::GuestMemory)?, + ), + buf_size, + }) + } + } + + /// Provides in-place, byte-slice, access to the vsock packet header. + /// + pub fn hdr(&self) -> &[u8] { + // This is safe since bound checks have already been performed when creating the packet + // from the virtq descriptor. + unsafe { std::slice::from_raw_parts(self.hdr as *const u8, VSOCK_PKT_HDR_SIZE) } + } + + /// Provides in-place, byte-slice, mutable access to the vsock packet header. + /// + pub fn hdr_mut(&mut self) -> &mut [u8] { + // This is safe since bound checks have already been performed when creating the packet + // from the virtq descriptor. + unsafe { std::slice::from_raw_parts_mut(self.hdr, VSOCK_PKT_HDR_SIZE) } + } + + /// Provides in-place, byte-slice access to the vsock packet data buffer. + /// + /// Note: control packets (e.g. connection request or reset) have no data buffer associated. + /// For those packets, this method will return `None`. + /// Also note: calling `len()` on the returned slice will yield the buffer size, which may be + /// (and often is) larger than the length of the packet data. The packet data length + /// is stored in the packet header, and accessible via `VsockPacket::len()`. + pub fn buf(&self) -> Option<&[u8]> { + self.buf.map(|ptr| { + // This is safe since bound checks have already been performed when creating the packet + // from the virtq descriptor. + unsafe { std::slice::from_raw_parts(ptr as *const u8, self.buf_size) } + }) + } + + /// Provides in-place, byte-slice, mutable access to the vsock packet data buffer. + /// + /// Note: control packets (e.g. connection request or reset) have no data buffer associated. + /// For those packets, this method will return `None`. + /// Also note: calling `len()` on the returned slice will yield the buffer size, which may be + /// (and often is) larger than the length of the packet data. The packet data length + /// is stored in the packet header, and accessible via `VsockPacket::len()`. + pub fn buf_mut(&mut self) -> Option<&mut [u8]> { + self.buf.map(|ptr| { + // This is safe since bound checks have already been performed when creating the packet + // from the virtq descriptor. + unsafe { std::slice::from_raw_parts_mut(ptr, self.buf_size) } + }) + } + + pub fn src_cid(&self) -> u64 { + LittleEndian::read_u64(&self.hdr()[HDROFF_SRC_CID..]) + } + + pub fn set_src_cid(&mut self, cid: u64) -> &mut Self { + LittleEndian::write_u64(&mut self.hdr_mut()[HDROFF_SRC_CID..], cid); + self + } + + pub fn dst_cid(&self) -> u64 { + LittleEndian::read_u64(&self.hdr()[HDROFF_DST_CID..]) + } + + pub fn set_dst_cid(&mut self, cid: u64) -> &mut Self { + LittleEndian::write_u64(&mut self.hdr_mut()[HDROFF_DST_CID..], cid); + self + } + + pub fn src_port(&self) -> u32 { + LittleEndian::read_u32(&self.hdr()[HDROFF_SRC_PORT..]) + } + + pub fn set_src_port(&mut self, port: u32) -> &mut Self { + LittleEndian::write_u32(&mut self.hdr_mut()[HDROFF_SRC_PORT..], port); + self + } + + pub fn dst_port(&self) -> u32 { + LittleEndian::read_u32(&self.hdr()[HDROFF_DST_PORT..]) + } + + pub fn set_dst_port(&mut self, port: u32) -> &mut Self { + LittleEndian::write_u32(&mut self.hdr_mut()[HDROFF_DST_PORT..], port); + self + } + + pub fn len(&self) -> u32 { + LittleEndian::read_u32(&self.hdr()[HDROFF_LEN..]) + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn set_len(&mut self, len: u32) -> &mut Self { + LittleEndian::write_u32(&mut self.hdr_mut()[HDROFF_LEN..], len); + self + } + + pub fn type_(&self) -> u16 { + LittleEndian::read_u16(&self.hdr()[HDROFF_TYPE..]) + } + + pub fn set_type(&mut self, type_: u16) -> &mut Self { + LittleEndian::write_u16(&mut self.hdr_mut()[HDROFF_TYPE..], type_); + self + } + + pub fn op(&self) -> u16 { + LittleEndian::read_u16(&self.hdr()[HDROFF_OP..]) + } + + pub fn set_op(&mut self, op: u16) -> &mut Self { + LittleEndian::write_u16(&mut self.hdr_mut()[HDROFF_OP..], op); + self + } + + pub fn flags(&self) -> u32 { + LittleEndian::read_u32(&self.hdr()[HDROFF_FLAGS..]) + } + + pub fn set_flags(&mut self, flags: u32) -> &mut Self { + LittleEndian::write_u32(&mut self.hdr_mut()[HDROFF_FLAGS..], flags); + self + } + + pub fn set_flag(&mut self, flag: u32) -> &mut Self { + self.set_flags(self.flags() | flag); + self + } + + pub fn buf_alloc(&self) -> u32 { + LittleEndian::read_u32(&self.hdr()[HDROFF_BUF_ALLOC..]) + } + + pub fn set_buf_alloc(&mut self, buf_alloc: u32) -> &mut Self { + LittleEndian::write_u32(&mut self.hdr_mut()[HDROFF_BUF_ALLOC..], buf_alloc); + self + } + + pub fn fwd_cnt(&self) -> u32 { + LittleEndian::read_u32(&self.hdr()[HDROFF_FWD_CNT..]) + } + + pub fn set_fwd_cnt(&mut self, fwd_cnt: u32) -> &mut Self { + LittleEndian::write_u32(&mut self.hdr_mut()[HDROFF_FWD_CNT..], fwd_cnt); + self + } +} + +#[cfg(test)] +mod tests { + use super::super::tests::TestContext; + use super::*; + use crate::vsock::defs::MAX_PKT_BUF_SIZE; + use crate::GuestMemoryMmap; + use virtio_bindings::bindings::virtio_ring::VRING_DESC_F_WRITE; + use virtio_queue::QueueOwnedT; + use vm_memory::GuestAddress; + use vm_virtio::queue::testing::VirtqDesc as GuestQDesc; + + macro_rules! create_context { + ($test_ctx:ident, $handler_ctx:ident) => { + let $test_ctx = TestContext::new(); + let mut $handler_ctx = $test_ctx.create_epoll_handler_context(); + // For TX packets, hdr.len should be set to a valid value. + set_pkt_len(1024, &$handler_ctx.guest_txvq.dtable[0], &$test_ctx.mem); + }; + } + + macro_rules! expect_asm_error { + (tx, $test_ctx:expr, $handler_ctx:expr, $err:pat) => { + expect_asm_error!($test_ctx, $handler_ctx, $err, from_tx_virtq_head, 1); + }; + (rx, $test_ctx:expr, $handler_ctx:expr, $err:pat) => { + expect_asm_error!($test_ctx, $handler_ctx, $err, from_rx_virtq_head, 0); + }; + ($test_ctx:expr, $handler_ctx:expr, $err:pat, $ctor:ident, $vq:expr) => { + match VsockPacket::$ctor( + &mut $handler_ctx.handler.queues[$vq] + .iter(&$test_ctx.mem) + .unwrap() + .next() + .unwrap(), + None, + ) { + Err($err) => (), + Ok(_) => panic!("Packet assembly should've failed!"), + Err(other) => panic!("Packet assembly failed with: {:?}", other), + } + }; + } + + fn set_pkt_len(len: u32, guest_desc: &GuestQDesc, mem: &GuestMemoryMmap) { + let hdr_gpa = guest_desc.addr.get(); + let hdr_ptr = + get_host_address_range(mem, GuestAddress(hdr_gpa), VSOCK_PKT_HDR_SIZE).unwrap(); + let len_ptr = unsafe { hdr_ptr.add(HDROFF_LEN) }; + + LittleEndian::write_u32(unsafe { std::slice::from_raw_parts_mut(len_ptr, 4) }, len); + } + + #[test] + #[allow(clippy::cognitive_complexity)] + fn test_tx_packet_assembly() { + // Test case: successful TX packet assembly. + { + create_context!(test_ctx, handler_ctx); + + let pkt = VsockPacket::from_tx_virtq_head( + &mut handler_ctx.handler.queues[1] + .iter(&test_ctx.mem) + .unwrap() + .next() + .unwrap(), + None, + ) + .unwrap(); + assert_eq!(pkt.hdr().len(), VSOCK_PKT_HDR_SIZE); + assert_eq!( + pkt.buf().unwrap().len(), + handler_ctx.guest_txvq.dtable[1].len.get() as usize + ); + } + + // Test case: error on write-only hdr descriptor. + { + create_context!(test_ctx, handler_ctx); + handler_ctx.guest_txvq.dtable[0] + .flags + .set(VRING_DESC_F_WRITE.try_into().unwrap()); + expect_asm_error!(tx, test_ctx, handler_ctx, VsockError::UnreadableDescriptor); + } + + // Test case: header descriptor has insufficient space to hold the packet header. + { + create_context!(test_ctx, handler_ctx); + handler_ctx.guest_txvq.dtable[0] + .len + .set(VSOCK_PKT_HDR_SIZE as u32 - 1); + expect_asm_error!(tx, test_ctx, handler_ctx, VsockError::HdrDescTooSmall(_)); + } + + // Test case: zero-length TX packet. + { + create_context!(test_ctx, handler_ctx); + set_pkt_len(0, &handler_ctx.guest_txvq.dtable[0], &test_ctx.mem); + let mut pkt = VsockPacket::from_tx_virtq_head( + &mut handler_ctx.handler.queues[1] + .iter(&test_ctx.mem) + .unwrap() + .next() + .unwrap(), + None, + ) + .unwrap(); + assert!(pkt.buf().is_none()); + assert!(pkt.buf_mut().is_none()); + } + + // Test case: TX packet has more data than we can handle. + { + create_context!(test_ctx, handler_ctx); + set_pkt_len( + MAX_PKT_BUF_SIZE as u32 + 1, + &handler_ctx.guest_txvq.dtable[0], + &test_ctx.mem, + ); + expect_asm_error!(tx, test_ctx, handler_ctx, VsockError::InvalidPktLen(_)); + } + + // Test case: error on write-only buf descriptor. + { + create_context!(test_ctx, handler_ctx); + handler_ctx.guest_txvq.dtable[1] + .flags + .set(VRING_DESC_F_WRITE.try_into().unwrap()); + expect_asm_error!(tx, test_ctx, handler_ctx, VsockError::UnreadableDescriptor); + } + + // Test case: the buffer descriptor cannot fit all the data advertised by the + // packet header `len` field. + { + create_context!(test_ctx, handler_ctx); + set_pkt_len(8 * 1024, &handler_ctx.guest_txvq.dtable[0], &test_ctx.mem); + handler_ctx.guest_txvq.dtable[1].len.set(4 * 1024); + expect_asm_error!(tx, test_ctx, handler_ctx, VsockError::BufDescTooSmall); + } + } + + #[test] + fn test_rx_packet_assembly() { + // Test case: successful RX packet assembly. + { + create_context!(test_ctx, handler_ctx); + let pkt = VsockPacket::from_rx_virtq_head( + &mut handler_ctx.handler.queues[0] + .iter(&test_ctx.mem) + .unwrap() + .next() + .unwrap(), + None, + ) + .unwrap(); + assert_eq!(pkt.hdr().len(), VSOCK_PKT_HDR_SIZE); + assert_eq!( + pkt.buf().unwrap().len(), + handler_ctx.guest_rxvq.dtable[1].len.get() as usize + ); + } + + // Test case: read-only RX packet header. + { + create_context!(test_ctx, handler_ctx); + handler_ctx.guest_rxvq.dtable[0].flags.set(0); + expect_asm_error!(rx, test_ctx, handler_ctx, VsockError::UnwritableDescriptor); + } + + // Test case: RX descriptor head cannot fit the entire packet header. + { + create_context!(test_ctx, handler_ctx); + handler_ctx.guest_rxvq.dtable[0] + .len + .set(VSOCK_PKT_HDR_SIZE as u32 - 1); + expect_asm_error!(rx, test_ctx, handler_ctx, VsockError::HdrDescTooSmall(_)); + } + } + + #[test] + #[allow(clippy::cognitive_complexity)] + fn test_packet_hdr_accessors() { + const SRC_CID: u64 = 1; + const DST_CID: u64 = 2; + const SRC_PORT: u32 = 3; + const DST_PORT: u32 = 4; + const LEN: u32 = 5; + const TYPE: u16 = 6; + const OP: u16 = 7; + const FLAGS: u32 = 8; + const BUF_ALLOC: u32 = 9; + const FWD_CNT: u32 = 10; + + create_context!(test_ctx, handler_ctx); + let mut pkt = VsockPacket::from_rx_virtq_head( + &mut handler_ctx.handler.queues[0] + .iter(&test_ctx.mem) + .unwrap() + .next() + .unwrap(), + None, + ) + .unwrap(); + + // Test field accessors. + pkt.set_src_cid(SRC_CID) + .set_dst_cid(DST_CID) + .set_src_port(SRC_PORT) + .set_dst_port(DST_PORT) + .set_len(LEN) + .set_type(TYPE) + .set_op(OP) + .set_flags(FLAGS) + .set_buf_alloc(BUF_ALLOC) + .set_fwd_cnt(FWD_CNT); + + assert_eq!(pkt.src_cid(), SRC_CID); + assert_eq!(pkt.dst_cid(), DST_CID); + assert_eq!(pkt.src_port(), SRC_PORT); + assert_eq!(pkt.dst_port(), DST_PORT); + assert_eq!(pkt.len(), LEN); + assert_eq!(pkt.type_(), TYPE); + assert_eq!(pkt.op(), OP); + assert_eq!(pkt.flags(), FLAGS); + assert_eq!(pkt.buf_alloc(), BUF_ALLOC); + assert_eq!(pkt.fwd_cnt(), FWD_CNT); + + // Test individual flag setting. + let flags = pkt.flags() | 0b1000; + pkt.set_flag(0b1000); + assert_eq!(pkt.flags(), flags); + + // Test packet header as-slice access. + // + + assert_eq!(pkt.hdr().len(), VSOCK_PKT_HDR_SIZE); + + assert_eq!( + SRC_CID, + LittleEndian::read_u64(&pkt.hdr()[HDROFF_SRC_CID..]) + ); + assert_eq!( + DST_CID, + LittleEndian::read_u64(&pkt.hdr()[HDROFF_DST_CID..]) + ); + assert_eq!( + SRC_PORT, + LittleEndian::read_u32(&pkt.hdr()[HDROFF_SRC_PORT..]) + ); + assert_eq!( + DST_PORT, + LittleEndian::read_u32(&pkt.hdr()[HDROFF_DST_PORT..]) + ); + assert_eq!(LEN, LittleEndian::read_u32(&pkt.hdr()[HDROFF_LEN..])); + assert_eq!(TYPE, LittleEndian::read_u16(&pkt.hdr()[HDROFF_TYPE..])); + assert_eq!(OP, LittleEndian::read_u16(&pkt.hdr()[HDROFF_OP..])); + assert_eq!(FLAGS, LittleEndian::read_u32(&pkt.hdr()[HDROFF_FLAGS..])); + assert_eq!( + BUF_ALLOC, + LittleEndian::read_u32(&pkt.hdr()[HDROFF_BUF_ALLOC..]) + ); + assert_eq!( + FWD_CNT, + LittleEndian::read_u32(&pkt.hdr()[HDROFF_FWD_CNT..]) + ); + + assert_eq!(pkt.hdr_mut().len(), VSOCK_PKT_HDR_SIZE); + for b in pkt.hdr_mut() { + *b = 0; + } + assert_eq!(pkt.src_cid(), 0); + assert_eq!(pkt.dst_cid(), 0); + assert_eq!(pkt.src_port(), 0); + assert_eq!(pkt.dst_port(), 0); + assert_eq!(pkt.len(), 0); + assert_eq!(pkt.type_(), 0); + assert_eq!(pkt.op(), 0); + assert_eq!(pkt.flags(), 0); + assert_eq!(pkt.buf_alloc(), 0); + assert_eq!(pkt.fwd_cnt(), 0); + } + + #[test] + fn test_packet_buf() { + create_context!(test_ctx, handler_ctx); + let mut pkt = VsockPacket::from_rx_virtq_head( + &mut handler_ctx.handler.queues[0] + .iter(&test_ctx.mem) + .unwrap() + .next() + .unwrap(), + None, + ) + .unwrap(); + + assert_eq!( + pkt.buf().unwrap().len(), + handler_ctx.guest_rxvq.dtable[1].len.get() as usize + ); + assert_eq!( + pkt.buf_mut().unwrap().len(), + handler_ctx.guest_rxvq.dtable[1].len.get() as usize + ); + + for i in 0..pkt.buf().unwrap().len() { + pkt.buf_mut().unwrap()[i] = (i % 0x100) as u8; + assert_eq!(pkt.buf().unwrap()[i], (i % 0x100) as u8); + } + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/mod.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/mod.rs new file mode 100644 index 0000000..c6ba349 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/mod.rs @@ -0,0 +1,61 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// + +/// This module implements the Unix Domain Sockets backend for vsock - a mediator between +/// guest-side AF_VSOCK sockets and host-side AF_UNIX sockets. The heavy lifting is performed by +/// `muxer::VsockMuxer`, a connection multiplexer that uses `super::csm::VsockConnection` for +/// handling vsock connection states. +/// Check out `muxer.rs` for a more detailed explanation of the inner workings of this backend. +/// +mod muxer; +mod muxer_killq; +mod muxer_rxq; +mod stream; + +pub use muxer::VsockMuxer as VsockUnixBackend; +pub use stream::{PassFdStream, VsockBackendStream}; +pub use Error as VsockUnixError; + +mod defs { + /// Maximum number of established connections that we can handle. + pub const MAX_CONNECTIONS: usize = 1023; + + /// Size of the muxer RX packet queue. + pub const MUXER_RXQ_SIZE: usize = 256; + + /// Size of the muxer connection kill queue. + pub const MUXER_KILLQ_SIZE: usize = 128; +} + +#[derive(Debug)] +pub enum Error { + /// Error converting from UTF-8 + ConvertFromUtf8(std::str::Utf8Error), + /// Error registering a new epoll-listening FD. + EpollAdd(std::io::Error), + /// Error creating an epoll FD. + EpollFdCreate(std::io::Error), + /// The host made an invalid vsock port connection request. + InvalidPortRequest, + /// Error parsing integer. + ParseInteger(std::num::ParseIntError), + /// Error reading stream port. + ReadStreamPort(Box), + /// Error accepting a new connection from the host-side Unix socket. + UnixAccept(std::io::Error), + /// Error binding to the host-side Unix socket. + UnixBind(std::io::Error), + /// Error connecting to a host-side Unix socket. + UnixConnect(std::io::Error), + /// Error reading from host-side Unix socket. + UnixRead(std::io::Error), + /// Muxer connection limit reached. + TooManyConnections, + /// Error parsing vsock dbg config. + ParseDbgConf, +} + +type Result = std::result::Result; + +type MuxerConnection = super::csm::VsockConnection; diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer.rs new file mode 100644 index 0000000..a87aedc --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer.rs @@ -0,0 +1,2126 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// + +use std::collections::{HashMap, HashSet}; +use std::fs::File; +use std::io; +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; +use std::os::unix::net::{UnixListener, UnixStream}; + +use sendfd::RecvWithFd; +use serde::{Deserialize, Serialize}; +use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable}; + +use super::super::csm::ConnState; +use super::super::defs::uapi; +use super::super::device::MUXER_EPOLL_EVENT; +use super::super::packet::VsockPacket; +use super::super::{ + Result as VsockResult, VsockBackend, VsockChannel, VsockEpollListener, VsockError, +}; +use super::defs; +use super::muxer_killq::MuxerKillQ; +use super::muxer_rxq::MuxerRxQ; +use super::stream::PassFdLabel; +use super::MuxerConnection; +use super::{Error, Result}; +use super::{PassFdStream, VsockBackendStream}; +use crate::vsock::packet::VirtioVsockHdr; + +enum LocalStreamCommand { + Connect(u32), + PassFds(Vec), +} + +struct PassFdRequest { + label: PassFdLabel, + port: u32, + fd: RawFd, +} + +impl Drop for PassFdRequest { + fn drop(&mut self) { + if self.fd >= 0 { + unsafe { + libc::close(self.fd); + } + self.fd = -1; + } + } +} + +impl PassFdRequest { + fn take_fd(&mut self) -> RawFd { + let fd = self.fd; + self.fd = -1; + fd + } +} + +struct LocalPassFd { + label: PassFdLabel, + port: u32, + local_port: u32, + fd: RawFd, +} + +impl Drop for LocalPassFd { + fn drop(&mut self) { + if self.fd >= 0 { + unsafe { + libc::close(self.fd); + } + self.fd = -1; + } + } +} + +impl LocalPassFd { + fn take_fd(&mut self) -> RawFd { + let fd = self.fd; + self.fd = -1; + fd + } +} + +/// `VsockMuxer` is the device-facing component of the Unix domain sockets vsock backend. I.e. +/// by implementing the `VsockBackend` trait, it abstracts away the gory details of translating +/// between AF_VSOCK and AF_UNIX, and presents a clean interface to the rest of the vsock +/// device model. +/// +/// The vsock muxer has two main roles: +/// 1. Vsock connection multiplexer: +/// It's the muxer's job to create, manage, and terminate `VsockConnection` objects. The +/// muxer also routes packets to their owning connections. It does so via a connection +/// `HashMap`, keyed by what is basically a (host_port, guest_port) tuple. +/// Vsock packet traffic needs to be inspected, in order to detect connection request +/// packets (leading to the creation of a new connection), and connection reset packets +/// (leading to the termination of an existing connection). All other packets, though, must +/// belong to an existing connection and, as such, the muxer simply forwards them. +/// 2. Event dispatcher +/// There are three event categories that the vsock backend is interested it: +/// 1. A new host-initiated connection is ready to be accepted from the listening host Unix +/// socket; +/// 2. Data is available for reading from a newly-accepted host-initiated connection (i.e. +/// the host is ready to issue a vsock connection request, informing us of the +/// destination port to which it wants to connect); +/// 3. Some event was triggered for a connected Unix socket, that belongs to a +/// `VsockConnection`. +/// The muxer gets notified about all of these events, because, as a `VsockEpollListener` +/// implementor, it gets to register a nested epoll FD into the main VMM epolling loop. All +/// other pollable FDs are then registered under this nested epoll FD. +/// To route all these events to their handlers, the muxer uses another `HashMap` object, +/// mapping `RawFd`s to `EpollListener`s. +/// + +/// A unique identifier of a `MuxerConnection` object. Connections are stored in a hash map, +/// keyed by a `ConnMapKey` object. +/// +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +pub struct ConnMapKey { + local_port: u32, + peer_port: u32, +} + +/// A muxer RX queue item. +/// +#[derive(Clone, Copy, Debug)] +pub enum MuxerRx { + /// The packet must be fetched from the connection identified by `ConnMapKey`. + ConnRx(ConnMapKey), + /// The muxer must produce an RST packet. + RstPkt { local_port: u32, peer_port: u32 }, +} + +/// An epoll listener, registered under the muxer's nested epoll FD. +/// +enum EpollListener { + /// The listener is a `MuxerConnection`, identified by `key`, and interested in the events + /// in `evset`. Since `MuxerConnection` implements `VsockEpollListener`, notifications will + /// be forwarded to the listener via `VsockEpollListener::notify()`. + Connection { + key: ConnMapKey, + evset: epoll::Events, + }, + /// A listener interested in new host-initiated connections. + HostSock, + /// A listener interested in reading host "connect " commands from a freshly + /// connected host socket. + LocalStream(UnixStream), +} + +pub struct ConnectionInfo { + /// timestamp accept host vsock app unix socket + pub unix_accept_time: std::time::Instant, + /// timestamp host vsock app send CONNECT + pub send_connect_time: std::time::Instant, +} + +/// The vsock connection multiplexer. +/// +pub struct VsockMuxer { + /// The Vsock Muxer ID + id: String, + /// Guest CID. + cid: u64, + /// A hash map used to store the active connections. + conn_map: HashMap, + /// A hash map used to store epoll event listeners / handlers. + listener_map: HashMap, + /// The RX queue. Items in this queue are consumed by `VsockMuxer::recv_pkt()`, and + /// produced + /// - by `VsockMuxer::send_pkt()` (e.g. RST in response to a connection request packet); + /// and + /// - in response to EPOLLIN events (e.g. data available to be read from an AF_UNIX + /// socket). + rxq: MuxerRxQ, + /// A queue used for terminating connections that are taking too long to shut down. + killq: MuxerKillQ, + /// The Unix socket, through which host-initiated connections are accepted. + host_sock: UnixListener, + /// The file system path of the host-side Unix socket. This is used to figure out the path + /// to Unix sockets listening on specific ports. I.e. "_". + host_sock_path: String, + /// The nested epoll File, used to register epoll listeners. + epoll_file: File, + /// A hash set used to keep track of used host-side (local) ports, in order to assign local + /// ports to host-initiated connections. + local_port_set: HashSet, + /// The last used host-side port. + local_port_last: u32, + /// epoll helper fd + helper_fd: RawFd, + epoll_nested: bool, + /// cube dbg vsock conf + cube_dbg_conf: Vec, + /// vsock host app connection info + conn_info: HashMap, +} + +#[derive(Serialize, Deserialize)] +pub struct VsockMuxerState { + local_port_set: HashSet, +} + +impl VsockChannel for VsockMuxer { + /// Deliver a vsock packet to the guest vsock driver. + /// + /// Returns: + /// - `Ok(())`: `pkt` has been successfully filled in; or + /// - `Err(VsockError::NoData)`: there was no available data with which to fill in the + /// packet. + /// + fn recv_pkt(&mut self, pkt: &mut VsockPacket) -> VsockResult<()> { + // We'll look for instructions on how to build the RX packet in the RX queue. If the + // queue is empty, that doesn't necessarily mean we don't have any pending RX, since + // the queue might be out-of-sync. If that's the case, we'll attempt to sync it first, + // and then try to pop something out again. + if self.rxq.is_empty() && !self.rxq.is_synced() { + self.rxq = MuxerRxQ::from_conn_map(&self.conn_map); + } + + while let Some(rx) = self.rxq.peek() { + let res = match rx { + // We need to build an RST packet, going from `local_port` to `peer_port`. + MuxerRx::RstPkt { + local_port, + peer_port, + } => { + pkt.set_op(uapi::VSOCK_OP_RST) + .set_src_cid(uapi::VSOCK_HOST_CID) + .set_dst_cid(self.cid) + .set_src_port(local_port) + .set_dst_port(peer_port) + .set_len(0) + .set_type(uapi::VSOCK_TYPE_STREAM) + .set_flags(0) + .set_buf_alloc(0) + .set_fwd_cnt(0); + self.rxq.pop().unwrap(); + return Ok(()); + } + + // We'll defer building the packet to this connection, since it has something + // to say. + MuxerRx::ConnRx(key) => { + let mut conn_res = Err(VsockError::NoData); + let mut do_pop = true; + self.apply_conn_mutation(key, |conn| { + conn_res = conn.recv_pkt(pkt); + do_pop = !conn.has_pending_rx(); + }); + if do_pop { + self.rxq.pop().unwrap(); + } + conn_res + } + }; + + if res.is_ok() { + // Inspect traffic, looking for RST packets, since that means we have to + // terminate and remove this connection from the active connection pool. + // + if pkt.op() == uapi::VSOCK_OP_RST { + self.remove_connection(ConnMapKey { + local_port: pkt.src_port(), + peer_port: pkt.dst_port(), + }); + } + + debug!( + "vsock muxer: RX pkt: {:?}", + VirtioVsockHdr::from_slice(pkt.hdr()) + ); + return Ok(()); + } + } + + Err(VsockError::NoData) + } + + /// Deliver a guest-generated packet to its destination in the vsock backend. + /// + /// This absorbs unexpected packets, handles RSTs (by dropping connections), and forwards + /// all the rest to their owning `MuxerConnection`. + /// + /// Returns: + /// always `Ok(())` - the packet has been consumed, and its virtio TX buffers can be + /// returned to the guest vsock driver. + /// + fn send_pkt(&mut self, pkt: &VsockPacket) -> VsockResult<()> { + let conn_key = ConnMapKey { + local_port: pkt.dst_port(), + peer_port: pkt.src_port(), + }; + + debug!( + "vsock: muxer.send[rxq.len={}]: {:?}", + self.rxq.len(), + VirtioVsockHdr::from_slice(pkt.hdr()) + ); + + // If this packet has an unsupported type (!=stream), we must send back an RST. + // + if pkt.type_() != uapi::VSOCK_TYPE_STREAM { + self.enq_rst(pkt.dst_port(), pkt.src_port()); + return Ok(()); + } + + // We don't know how to handle packets addressed to other CIDs. We only handle the host + // part of the guest - host communication here. + if pkt.dst_cid() != uapi::VSOCK_HOST_CID { + info!( + "vsock: dropping guest packet for unknown CID: {:?}", + VirtioVsockHdr::from_slice(pkt.hdr()) + ); + return Ok(()); + } + + if !self.conn_map.contains_key(&conn_key) { + // This packet can't be routed to any active connection (based on its src and dst + // ports). The only orphan / unroutable packets we know how to handle are + // connection requests. + if pkt.op() == uapi::VSOCK_OP_REQUEST { + // Oh, this is a connection request! + self.handle_peer_request_pkt(pkt); + } else { + // Send back an RST, to let the drive know we weren't expecting this packet. + self.enq_rst(pkt.dst_port(), pkt.src_port()); + } + return Ok(()); + } + + // Right, we know where to send this packet, then (to `conn_key`). + // However, if this is an RST, we have to forcefully terminate the connection, so + // there's no point in forwarding it the packet. + if pkt.op() == uapi::VSOCK_OP_RST { + self.remove_connection(conn_key); + return Ok(()); + } + + // Alright, everything looks in order - forward this packet to its owning connection. + let mut res: VsockResult<()> = Ok(()); + self.apply_conn_mutation(conn_key, |conn| { + res = conn.send_pkt(pkt); + }); + + res + } + + /// Check if the muxer has any pending RX data, with which to fill a guest-provided RX + /// buffer. + /// + fn has_pending_rx(&self) -> bool { + !self.rxq.is_empty() || !self.rxq.is_synced() + } +} + +impl VsockEpollListener for VsockMuxer { + /// Get the FD to be registered for polling upstream (in the main VMM epoll loop, in this + /// case). + /// + /// This will be the muxer's nested epoll FD. + /// + fn get_polled_fd(&self) -> RawFd { + self.epoll_file.as_raw_fd() + } + + fn muxer_epoll_nested(&self) -> bool { + self.epoll_nested + } + + /// Get the epoll events to be polled upstream. + /// + /// Since the polled FD is a nested epoll FD, we're only interested in EPOLLIN events (i.e. + /// some event occurred on one of the FDs registered under our epoll FD). + /// + fn get_polled_evset(&self) -> epoll::Events { + epoll::Events::EPOLLIN + } + + /// Notify the muxer about a pending event having occurred under its nested epoll FD. + /// + fn notify(&mut self, _: epoll::Events) { + debug!("vsock: muxer received kick"); + + let mut epoll_events = vec![epoll::Event::new(epoll::Events::empty(), 0); 32]; + 'epoll: loop { + match epoll::wait(self.epoll_file.as_raw_fd(), 0, epoll_events.as_mut_slice()) { + Ok(ev_cnt) => { + for evt in epoll_events.iter().take(ev_cnt) { + self.handle_event( + evt.data as RawFd, + // It's ok to unwrap here, since the `evt.events` is filled + // in by `epoll::wait()`, and therefore contains only valid epoll + // flags. + epoll::Events::from_bits(evt.events).unwrap(), + ); + } + } + Err(e) => { + if e.kind() == io::ErrorKind::Interrupted { + // It's well defined from the epoll_wait() syscall + // documentation that the epoll loop can be interrupted + // before any of the requested events occurred or the + // timeout expired. In both those cases, epoll_wait() + // returns an error of type EINTR, but this should not + // be considered as a regular error. Instead it is more + // appropriate to retry, by calling into epoll_wait(). + continue; + } + warn!("vsock: failed to consume muxer epoll event: {}", e); + } + } + break 'epoll; + } + } + fn set_epoll_helper_fd(&mut self, epfd: RawFd) { + self.helper_fd = epfd; + } + + fn add_host_sock(&mut self) { + self.add_host_sock_to_listener(); + } + + fn dispatch_muxer_event(&mut self, fd: RawFd, event_set: epoll::Events) { + self.handle_host_sock_event(fd, event_set); + } +} + +impl Pausable for VsockMuxer {} + +impl Snapshottable for VsockMuxer { + fn id(&self) -> String { + self.id.clone() + } + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } +} + +impl VsockBackend for VsockMuxer { + fn connections(&self) -> Vec<(u32, u32)> { + self.conn_map + .keys() + .map(|k| (k.local_port, k.peer_port)) + .collect() + } + + fn queue_rst_for_connections(&mut self, conns: Vec<(u32, u32)>) { + for (local_port, peer_port) in conns { + self.rxq.push(MuxerRx::RstPkt { + local_port, + peer_port, + }); + } + } +} + +#[derive(Debug, Default, Deserialize, Serialize)] +pub struct CubeVsockDbgConf { + #[serde(default)] + pub file: String, + #[serde(default)] + pub port: u32, +} + +pub fn cube_get_vsock_dbg_conf() -> Vec { + let file = "/etc/cube-hypervisor/vsock_conf.json"; + if let Ok(_) = std::fs::metadata(&file) { + debug!("vsock dbg json_path {:?}", file); + if let Ok(conf) = serde_json::from_str(&std::fs::read_to_string(file).unwrap()) { + return conf; + } else { + Vec::new() + } + } else { + debug!("vsock no dbg conf file"); + Vec::new() + } +} + +impl VsockMuxer { + fn close_fds_safely(fds: &mut [RawFd], count: usize) { + for fd in fds.iter_mut().take(count) { + if *fd >= 0 { + unsafe { + libc::close(*fd); + } + *fd = -1; + } + } + } + + fn validate_passfd(recv_fd: RawFd) -> bool { + if unsafe { libc::fcntl(recv_fd, libc::F_GETFD) } < 0 { + info!("vsock: passfd invalid fd: {}", recv_fd); + return false; + } + + let mut stat: libc::stat = unsafe { std::mem::zeroed() }; + if unsafe { libc::fstat(recv_fd, &mut stat) } < 0 { + info!( + "vsock: passfd fstat failed: fd={}, error={:?}", + recv_fd, + io::Error::last_os_error() + ); + return false; + } + + match stat.st_mode & libc::S_IFMT { + libc::S_IFIFO | libc::S_IFSOCK => true, + file_type => { + info!( + "vsock: passfd unsupported fd type: fd={}, file_type={:#o}", + recv_fd, file_type + ); + false + } + } + } + + fn set_passfd_nonblocking(recv_fd: RawFd) -> bool { + let flags = unsafe { libc::fcntl(recv_fd, libc::F_GETFL) }; + if flags < 0 { + info!( + "vsock: passfd F_GETFL failed: fd={}, error={:?}", + recv_fd, + io::Error::last_os_error() + ); + return false; + } + + if unsafe { libc::fcntl(recv_fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 { + info!( + "vsock: passfd F_SETFL O_NONBLOCK failed: fd={}, error={:?}", + recv_fd, + io::Error::last_os_error() + ); + return false; + } + true + } + + fn validate_local_peer(stream: &UnixStream) -> io::Result<()> { + let mut cred: libc::ucred = unsafe { std::mem::zeroed() }; + let mut len = std::mem::size_of::() as libc::socklen_t; + let ret = unsafe { + libc::getsockopt( + stream.as_raw_fd(), + libc::SOL_SOCKET, + libc::SO_PEERCRED, + &mut cred as *mut _ as *mut libc::c_void, + &mut len, + ) + }; + if ret < 0 { + return Err(io::Error::last_os_error()); + } + + let expected_uid = unsafe { libc::geteuid() }; + if cred.uid != expected_uid { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!( + "unexpected peer uid {}, expected {}", + cred.uid, expected_uid + ), + )); + } + + Ok(()) + } + + fn add_local_connect_connection(&mut self, stream: UnixStream, peer_port: u32) { + let local_port = self.allocate_local_port(); + info!( + "vsock: local-init connection: local_port={}, peer_port={}", + local_port, peer_port + ); + let backend = VsockBackendStream::Unix(stream); + if let Err(err) = self.add_connection( + ConnMapKey { + local_port, + peer_port, + }, + MuxerConnection::new_local_init( + backend, + uapi::VSOCK_HOST_CID, + self.cid, + local_port, + peer_port, + ), + ) { + info!("vsock: error adding local-init connection: {:?}", err); + self.free_local_port(local_port); + } + } + + fn prepare_local_passfds( + &mut self, + requests: &mut [PassFdRequest], + ) -> Option> { + // A passfd command is one batch: the shim waits for one response per + // requested label. Do not establish valid siblings when any received + // fd is unusable, otherwise those connections have no ports in the + // eventual RPC and remain unclaimed in the guest. + if requests.iter().any(|req| !Self::validate_passfd(req.fd)) { + return None; + } + if requests + .iter() + .any(|req| !Self::set_passfd_nonblocking(req.fd)) + { + return None; + } + + let mut passfds = Vec::with_capacity(requests.len()); + for req in requests.iter_mut() { + passfds.push(LocalPassFd { + label: req.label.clone(), + port: req.port, + local_port: self.allocate_local_port(), + fd: req.take_fd(), + }); + } + Some(passfds) + } + + fn add_local_passfd_connections(&mut self, stream: &UnixStream, mut passfds: Vec) { + for passfd in passfds.iter_mut() { + info!( + "vsock: local-init passfd connection: label={}, local_port={}, peer_port={}", + passfd.label, passfd.local_port, passfd.port + ); + let control = match stream.try_clone() { + Ok(control) => control, + Err(err) => { + info!("vsock: error cloning passfd control stream: {:?}", err); + self.free_local_port(passfd.local_port); + continue; + } + }; + let file = unsafe { std::fs::File::from_raw_fd(passfd.take_fd()) }; + let backend = + VsockBackendStream::PassFd(PassFdStream::new(file, control, passfd.label.clone())); + if let Err(err) = self.add_connection( + ConnMapKey { + local_port: passfd.local_port, + peer_port: passfd.port, + }, + MuxerConnection::new_local_init( + backend, + uapi::VSOCK_HOST_CID, + self.cid, + passfd.local_port, + passfd.port, + ), + ) { + info!( + "vsock: error adding local-init passfd connection: {:?}", + err + ); + self.free_local_port(passfd.local_port); + } + } + } + + /// Muxer constructor. + /// + pub fn new( + id: String, + cid: u64, + host_sock_path: String, + epoll_nested: bool, + state: Option, + ) -> Result { + // Create the local port set. + let local_port_set = if let Some(state) = state { + state.local_port_set + } else { + HashSet::with_capacity(defs::MAX_CONNECTIONS) + }; + + // Create the nested epoll FD. This FD will be added to the VMM `EpollContext`, at + // device activation time. + let epoll_fd = epoll::create(true).map_err(Error::EpollFdCreate)?; + // Use 'File' to enforce closing on 'epoll_fd' + let epoll_file = unsafe { File::from_raw_fd(epoll_fd) }; + + // Open/bind/listen on the host Unix socket, so we can accept host-initiated + // connections. + let host_sock = UnixListener::bind(&host_sock_path) + .and_then(|sock| sock.set_nonblocking(true).map(|_| sock)) + .map_err(Error::UnixBind)?; + let cube_dbg_conf = cube_get_vsock_dbg_conf(); + debug!("vsock: cube dbg conf {:?}", cube_dbg_conf); + + let mut muxer = Self { + id, + cid, + host_sock, + host_sock_path, + epoll_file, + rxq: MuxerRxQ::new(), + conn_map: HashMap::with_capacity(defs::MAX_CONNECTIONS), + listener_map: HashMap::with_capacity(defs::MAX_CONNECTIONS + 1), + killq: MuxerKillQ::new(), + local_port_last: (1u32 << 30) - 1, + local_port_set, + helper_fd: epoll_fd, + epoll_nested, + cube_dbg_conf, + conn_info: HashMap::with_capacity(defs::MAX_CONNECTIONS), + }; + + if epoll_nested { + muxer.add_listener(muxer.host_sock.as_raw_fd(), EpollListener::HostSock)?; + } + Ok(muxer) + } + + fn add_host_sock_to_listener(&mut self) { + let _ = self.add_listener(self.host_sock.as_raw_fd(), EpollListener::HostSock); + } + + fn is_cube_vsock_dbg_port(&self, port: u32) -> Option { + for conf in self.cube_dbg_conf.iter() { + if port == conf.port { + return Some(conf.file.clone()); + } + } + None + } + + fn dispatch_event( + &mut self, + fd: RawFd, + event_set: epoll::Events, + record_conn_info: bool, + log_label: &str, + ) { + debug!( + "vsock: muxer processing {} event: fd={}, event_set={:?}", + log_label, fd, event_set + ); + + match self.listener_map.get_mut(&fd) { + // This event needs to be forwarded to a `MuxerConnection` that is listening for + // it. + // + Some(EpollListener::Connection { key, evset: _ }) => { + let key_copy = *key; + // The handling of this event will most probably mutate the state of the + // receiving connection. We'll need to check for new pending RX, event set + // mutation, and all that, so we're wrapping the event delivery inside those + // checks. + self.apply_conn_mutation(key_copy, |conn| { + conn.notify(event_set); + }); + } + + // A new host-initiated connection is ready to be accepted. + // + Some(EpollListener::HostSock) => { + if self.conn_map.len() == defs::MAX_CONNECTIONS { + // If we're already maxed-out on connections, we'll just accept and + // immediately discard this potentially new one. + warn!("vsock: connection limit reached; refusing new host connection"); + self.host_sock.accept().map(|_| 0).unwrap_or(0); + return; + } + + let tm = std::time::Instant::now(); + let conn_info = ConnectionInfo { + unix_accept_time: tm, + send_connect_time: tm, + }; + + self.host_sock + .accept() + .map_err(Error::UnixAccept) + .and_then(|(stream, _)| { + Self::validate_local_peer(&stream).map_err(Error::UnixAccept)?; + Ok(stream) + }) + .and_then(|stream| { + stream + .set_nonblocking(true) + .map(|_| stream) + .map_err(Error::UnixAccept) + }) + .and_then(|stream| { + // Before forwarding this connection to a listening AF_VSOCK socket on + // the guest side, we need to know the destination port. We'll read + // that port from a "connect" command received on this socket, so the + // next step is to ask to be notified the moment we can read from it. + if record_conn_info { + self.conn_info.insert(stream.as_raw_fd(), conn_info); + debug!("vsock unix accept {:?}", tm); + } + self.add_listener(stream.as_raw_fd(), EpollListener::LocalStream(stream)) + }) + .unwrap_or_else(|err| { + warn!("vsock: unable to accept local connection: {:?}", err); + }); + } + + // Data is ready to be read from a host-initiated connection. That would be the + // "connect" command that we're expecting. + Some(EpollListener::LocalStream(_)) => { + if let Some(EpollListener::LocalStream(mut stream)) = self.remove_listener(fd) { + match Self::read_local_stream_port(self, &mut stream) { + Ok(LocalStreamCommand::Connect(peer_port)) => { + self.add_local_connect_connection(stream, peer_port); + } + Ok(LocalStreamCommand::PassFds(mut requests)) => { + if let Some(passfds) = self.prepare_local_passfds(&mut requests) { + self.add_local_passfd_connections(&stream, passfds); + } else { + info!("vsock: rejecting invalid passfd batch"); + } + } + Err(err) => { + info!("vsock: error adding local-init connection: {:?}", err); + } + } + } + } + + _ => { + info!( + "vsock: unexpected event: fd={:?}, event_set={:?}", + fd, event_set + ); + } + } + } + + /// Handle host sock event. + fn handle_host_sock_event(&mut self, fd: RawFd, event_set: epoll::Events) { + self.dispatch_event(fd, event_set, true, "host sock"); + } + + /// Handle/dispatch an epoll event to its listener. + /// + fn handle_event(&mut self, fd: RawFd, event_set: epoll::Events) { + self.dispatch_event(fd, event_set, false, "nested"); + } + + /// Parse a host "connect" or "passfd" command, and extract the destination vsock port. + /// + fn read_local_stream_port(&mut self, stream: &mut UnixStream) -> Result { + let mut buf = [0u8; 96]; + let mut fds = [-1; 3]; + + // Locate the command terminator without consuming the stream. CONNECT + // clients may write their first payload in the same send, so the real + // read must stop exactly at the newline and leave that payload queued. + let peek_len = unsafe { + libc::recv( + stream.as_raw_fd(), + buf.as_mut_ptr() as *mut libc::c_void, + buf.len(), + libc::MSG_PEEK, + ) + }; + if peek_len == 0 { + return Err(Error::InvalidPortRequest); + } + if peek_len < 0 { + return Err(Error::UnixRead(io::Error::last_os_error())); + } + let peek_len = peek_len as usize; + let cmd_len = buf[..peek_len] + .iter() + .position(|byte| *byte == b'\n') + .map(|pos| pos + 1) + .ok_or(Error::InvalidPortRequest)?; + + // Consume the complete command with recvmsg so PASSFD receives the + // SCM_RIGHTS control message attached to the stream data. + let (data_len, fd_len) = stream + .recv_with_fd(&mut buf[..cmd_len], &mut fds) + .map_err(Error::UnixRead)?; + + if data_len != cmd_len { + Self::close_fds_safely(&mut fds, fd_len); + return Err(Error::InvalidPortRequest); + } + // recvmsg transferred ownership of every SCM_RIGHTS descriptor to us. + // Guard them before parsing so all malformed-command paths close them. + let mut received_fds = fds + .iter_mut() + .take(fd_len) + .map(|fd| { + let owned = unsafe { OwnedFd::from_raw_fd(*fd) }; + *fd = -1; + Some(owned) + }) + .collect::>(); + + let mut word_iter = std::str::from_utf8(&buf[..data_len]) + .map_err(Error::ConvertFromUtf8)? + .split_whitespace(); + + let cmd = word_iter.next().ok_or(Error::InvalidPortRequest)?; + let port_str = word_iter.next().ok_or(Error::InvalidPortRequest)?; + let port = port_str.parse::().map_err(Error::ParseInteger)?; + + if cmd.to_lowercase() == "connect" { + if let Some(conn_info) = self.conn_info.get_mut(&stream.as_raw_fd()) { + conn_info.send_connect_time = std::time::Instant::now(); + } + Ok(LocalStreamCommand::Connect(port)) + } else if cmd.to_lowercase() == "passfd" { + let labels = word_iter.collect::>(); + // Keep the original single-stream command compatible. Batched + // passfd requests must label every fd so responses can be matched. + let labels = if labels.is_empty() && fd_len == 1 { + vec!["stream"] + } else { + labels + }; + if fd_len != labels.len() || labels.len() > fds.len() { + return Err(Error::InvalidPortRequest); + } + let mut requests = Vec::with_capacity(labels.len()); + for (idx, label) in labels.into_iter().enumerate() { + requests.push(PassFdRequest { + label: PassFdLabel::from(label), + port, + fd: received_fds[idx].take().unwrap().into_raw_fd(), + }); + } + Ok(LocalStreamCommand::PassFds(requests)) + } else { + Err(Error::InvalidPortRequest) + } + } + + /// Add a new connection to the active connection pool. + /// + fn add_connection(&mut self, key: ConnMapKey, conn: MuxerConnection) -> Result<()> { + // We might need to make room for this new connection, so let's sweep the kill queue + // first. It's fine to do this here because: + // - unless the kill queue is out of sync, this is a pretty inexpensive operation; and + // - we are under no pressure to respect any accurate timing for connection + // termination. + self.sweep_killq(); + + if self.conn_map.len() >= defs::MAX_CONNECTIONS { + info!( + "vsock: muxer connection limit reached ({})", + defs::MAX_CONNECTIONS + ); + return Err(Error::TooManyConnections); + } + + self.add_listener( + conn.get_polled_fd(), + EpollListener::Connection { + key, + evset: conn.get_polled_evset(), + }, + ) + .map(|_| { + if conn.has_pending_rx() { + // We can safely ignore any error in adding a connection RX indication. Worst + // case scenario, the RX queue will get desynchronized, but we'll handle that + // the next time we need to yield an RX packet. + self.rxq.push(MuxerRx::ConnRx(key)); + } + self.conn_map.insert(key, conn); + }) + } + + /// Remove a connection from the active connection poll. + /// + fn remove_connection(&mut self, key: ConnMapKey) { + let tm1 = std::time::Instant::now(); + let mut fd = -1; + if let Some(conn) = self.conn_map.remove(&key) { + fd = conn.get_polled_fd(); + self.remove_listener(conn.get_polled_fd()); + } + self.free_local_port(key.local_port); + + if let Some(conn_info) = self.conn_info.remove(&fd) { + let unix_accept_time = conn_info.unix_accept_time; + let send_connect_time = conn_info.send_connect_time; + let tm2 = std::time::Instant::now(); + let unix_accept_cost = send_connect_time + .duration_since(unix_accept_time) + .as_micros(); + let rst_cost = tm1.duration_since(send_connect_time).as_micros(); + let free_cost = tm2.duration_since(tm1).as_micros(); + if (unix_accept_cost + rst_cost + free_cost) > 20_000 { + info!( + "vsock remove connection unix accept cost {} us, rst {} us, free_cost {} us", + unix_accept_cost, rst_cost, free_cost + ); + } + } + } + + /// Schedule a connection for immediate termination. + /// I.e. as soon as we can also let our peer know we're dropping the connection, by sending + /// it an RST packet. + /// + fn kill_connection(&mut self, key: ConnMapKey) { + let mut had_rx = false; + self.conn_map.entry(key).and_modify(|conn| { + had_rx = conn.has_pending_rx(); + conn.kill(); + }); + // This connection will now have an RST packet to yield, so we need to add it to the RX + // queue. However, there's no point in doing that if it was already in the queue. + if !had_rx { + // We can safely ignore any error in adding a connection RX indication. Worst case + // scenario, the RX queue will get desynchronized, but we'll handle that the next + // time we need to yield an RX packet. + self.rxq.push(MuxerRx::ConnRx(key)); + } + } + + /// Register a new epoll listener under the muxer's nested epoll FD. + /// + fn add_listener(&mut self, fd: RawFd, listener: EpollListener) -> Result<()> { + let evset = match listener { + EpollListener::Connection { evset, .. } => evset, + EpollListener::LocalStream(_) => epoll::Events::EPOLLIN, + EpollListener::HostSock => epoll::Events::EPOLLIN, + }; + + let mut epfd = self.epoll_file.as_raw_fd(); + let mut event_data = fd as u64; + if !self.epoll_nested { + epfd = self.helper_fd; + event_data = ((fd as u64) << 32) | (MUXER_EPOLL_EVENT as u64); + } + + debug!( + "vsock epoll_nested {} epoll add fd {} data {}", + self.epoll_nested, fd, event_data + ); + epoll::ctl( + // self.epoll_file.as_raw_fd(), + epfd, + epoll::ControlOptions::EPOLL_CTL_ADD, + fd, + // epoll::Event::new(evset, fd as u64), + epoll::Event::new(evset, event_data), + ) + .map(|_| { + debug!("vsock epoll added"); + self.listener_map.insert(fd, listener); + }) + .map_err(Error::EpollAdd)?; + + Ok(()) + } + + /// Remove (and return) a previously registered epoll listener. + /// + fn remove_listener(&mut self, fd: RawFd) -> Option { + let maybe_listener = self.listener_map.remove(&fd); + + let mut epfd = self.epoll_file.as_raw_fd(); + if !self.epoll_nested { + epfd = self.helper_fd; + } + + if maybe_listener.is_some() { + epoll::ctl( + epfd, + epoll::ControlOptions::EPOLL_CTL_DEL, + fd, + epoll::Event::new(epoll::Events::empty(), 0), + ) + .unwrap_or_else(|err| { + warn!( + "vosck muxer: error removing epoll listener for fd {:?}: {:?}", + fd, err + ); + }); + } + + maybe_listener + } + + /// Allocate a host-side port to be assigned to a new host-initiated connection. + /// + /// + fn allocate_local_port(&mut self) -> u32 { + // TODO: this doesn't seem very space-efficient. + // Maybe rewrite this to limit port range and use a bitmap? + // + + loop { + self.local_port_last = (self.local_port_last + 1) & !(1 << 31) | (1 << 30); + if self.local_port_set.insert(self.local_port_last) { + break; + } + } + self.local_port_last + } + + /// Mark a previously used host-side port as free. + /// + fn free_local_port(&mut self, port: u32) { + self.local_port_set.remove(&port); + } + + /// Handle a new connection request coming from our peer (the guest vsock driver). + /// + /// This will attempt to connect to a host-side Unix socket, expected to be listening at + /// the file system path corresponding to the destination port. If successful, a new + /// connection object will be created and added to the connection pool. On failure, a new + /// RST packet will be scheduled for delivery to the guest. + /// + fn handle_peer_request_pkt(&mut self, pkt: &VsockPacket) { + let mut port_path = format!("{}_{}", self.host_sock_path, pkt.dst_port()); + + if let Some(path) = self.is_cube_vsock_dbg_port(pkt.dst_port()) { + port_path = path; + } + + debug!("vsock: port_path {}", port_path); + + UnixStream::connect(port_path) + .and_then(|stream| stream.set_nonblocking(true).map(|_| stream)) + .map_err(Error::UnixConnect) + .and_then(|stream| { + self.add_connection( + ConnMapKey { + local_port: pkt.dst_port(), + peer_port: pkt.src_port(), + }, + MuxerConnection::new_peer_init( + VsockBackendStream::Unix(stream), + uapi::VSOCK_HOST_CID, + self.cid, + pkt.dst_port(), + pkt.src_port(), + pkt.buf_alloc(), + ), + ) + }) + .unwrap_or_else(|_| self.enq_rst(pkt.dst_port(), pkt.src_port())); + } + + /// Perform an action that might mutate a connection's state. + /// + /// This is used as shorthand for repetitive tasks that need to be performed after a + /// connection object mutates. E.g. + /// - update the connection's epoll listener; + /// - schedule the connection to be queried for RX data; + /// - kill the connection if an unrecoverable error occurs. + /// + fn apply_conn_mutation(&mut self, key: ConnMapKey, mut_fn: F) + where + F: FnOnce(&mut MuxerConnection), + { + if let Some(conn) = self.conn_map.get_mut(&key) { + let had_rx = conn.has_pending_rx(); + let was_expiring = conn.will_expire(); + let prev_state = conn.state(); + + mut_fn(conn); + + // If this is a host-initiated connection that has just become established, we'll have + // to send an ack message to the host end. + if prev_state == ConnState::LocalInit && conn.state() == ConnState::Established { + let fd = conn.get_polled_fd(); + if let Some(conn_info) = self.conn_info.get_mut(&fd) { + let unix_accept_time = conn_info.unix_accept_time; + let send_connect_time = conn_info.send_connect_time; + let unix_accept_cost = send_connect_time + .duration_since(unix_accept_time) + .as_micros(); + let conn_cost = std::time::Instant::now() + .duration_since(send_connect_time) + .as_micros(); + info!("vsock unix accept cost {} us, established {} us, timestamp: {:?}-{:?}-{:?}", + unix_accept_cost, conn_cost, unix_accept_time, send_connect_time, std::time::Instant::now()); + } + + match conn.stream_mut().send_connect_ack(key.local_port) { + Ok(()) => (), + Err(err) => { + conn.kill(); + warn!("vsock: unable to ack host connection: {:?}", err); + } + }; + } + + // If the connection wasn't previously scheduled for RX, add it to our RX queue. + if !had_rx && conn.has_pending_rx() { + self.rxq.push(MuxerRx::ConnRx(key)); + } + + // If the connection wasn't previously scheduled for termination, add it to the + // kill queue. + if !was_expiring && conn.will_expire() { + // It's safe to unwrap here, since `conn.will_expire()` already guaranteed that + // an `conn.expiry` is available. + self.killq.push(key, conn.expiry().unwrap()); + } + + let fd = conn.get_polled_fd(); + let new_evset = conn.get_polled_evset(); + if new_evset.is_empty() { + // If the connection no longer needs epoll notifications, remove its listener + // from our list. + self.remove_listener(fd); + return; + } + if let Some(EpollListener::Connection { evset, .. }) = self.listener_map.get_mut(&fd) { + if *evset != new_evset { + // If the set of events that the connection is interested in has changed, + // we need to update its epoll listener. + debug!( + "vsock: updating listener for (lp={}, pp={}): old={:?}, new={:?}", + key.local_port, key.peer_port, *evset, new_evset + ); + + *evset = new_evset; + + let mut epfd = self.epoll_file.as_raw_fd(); + let mut event_data = fd as u64; + if !self.epoll_nested { + epfd = self.helper_fd; + event_data = ((fd as u64) << 32) | (MUXER_EPOLL_EVENT as u64); + } + // Update epoll listener to handle possible errors + if let Err(e) = epoll::ctl( + epfd, + epoll::ControlOptions::EPOLL_CTL_MOD, + fd, + epoll::Event::new(*evset, event_data), + ) { + if e.kind() == std::io::ErrorKind::NotFound { + debug!( + "vsock: removing invalid fd connection (lp={}, pp={})", + key.local_port, key.peer_port + ); + self.conn_map.remove(&key); + self.listener_map.remove(&fd); + } else if e.kind() == std::io::ErrorKind::Interrupted + || e.kind() == std::io::ErrorKind::WouldBlock + { + warn!( + "vsock: recoverable error updating epoll listener for (lp={}, pp={}): {:?}", + key.local_port, key.peer_port, e); + } else { + warn!( + "vsock: fatal error updating epoll listener for (lp={}, pp={}): {:?}", + key.local_port, key.peer_port, e); + self.kill_connection(key); + } + } + } + } else { + // The connection had previously asked to be removed from the listener map (by + // returning an empty event set via `get_polled_fd()`), but now wants back in. + self.add_listener( + fd, + EpollListener::Connection { + key, + evset: new_evset, + }, + ) + .unwrap_or_else(|err| { + self.kill_connection(key); + error!( + "vsock: error updating epoll listener for (lp={}, pp={}): {:?}", + key.local_port, key.peer_port, err + ); + }); + } + } + } + + /// Check if any connections have timed out, and if so, schedule them for immediate + /// termination. + /// + fn sweep_killq(&mut self) { + while let Some(key) = self.killq.pop() { + // Connections don't get removed from the kill queue when their kill timer is + // disarmed, since that would be a costly operation. This means we must check if + // the connection has indeed expired, prior to killing it. + let mut kill = false; + self.conn_map + .entry(key) + .and_modify(|conn| kill = conn.has_expired()); + if kill { + self.kill_connection(key); + } + } + + if self.killq.is_empty() && !self.killq.is_synced() { + self.killq = MuxerKillQ::from_conn_map(&self.conn_map); + // If we've just re-created the kill queue, we can sweep it again; maybe there's + // more to kill. + self.sweep_killq(); + } + } + + /// Enqueue an RST packet into `self.rxq`. + /// + /// Enqueue errors aren't propagated up the call chain, since there is nothing we can do to + /// handle them. We do, however, log a warning, since not being able to enqueue an RST + /// packet means we have to drop it, which is not normal operation. + /// + fn enq_rst(&mut self, local_port: u32, peer_port: u32) { + debug!("vsock: send VSOCK_OP_RST to guest"); + let pushed = self.rxq.push(MuxerRx::RstPkt { + local_port, + peer_port, + }); + if !pushed { + warn!( + "vsock: muxer.rxq full; dropping RST packet for lp={}, pp={}", + local_port, peer_port + ); + } + } + + fn state(&self) -> VsockMuxerState { + VsockMuxerState { + local_port_set: self.local_port_set.clone(), + } + } +} + +#[cfg(test)] +mod tests { + use std::io::{Read, Write}; + use std::ops::Drop; + use std::os::unix::io::{AsRawFd, FromRawFd}; + use std::os::unix::net::{UnixListener, UnixStream}; + use std::path::{Path, PathBuf}; + + use sendfd::SendWithFd; + use virtio_queue::QueueOwnedT; + + use super::super::super::csm::defs as csm_defs; + use super::super::super::tests::TestContext as VsockTestContext; + use super::*; + + const PEER_CID: u64 = 3; + const PEER_BUF_ALLOC: u32 = 64 * 1024; + + struct MuxerTestContext { + _vsock_test_ctx: VsockTestContext, + pkt: VsockPacket, + muxer: VsockMuxer, + } + + impl Drop for MuxerTestContext { + fn drop(&mut self) { + std::fs::remove_file(self.muxer.host_sock_path.as_str()).unwrap(); + } + } + + impl MuxerTestContext { + fn new(name: &str) -> Self { + let vsock_test_ctx = VsockTestContext::new(); + let mut handler_ctx = vsock_test_ctx.create_epoll_handler_context(); + let pkt = VsockPacket::from_rx_virtq_head( + &mut handler_ctx.handler.queues[0] + .iter(&vsock_test_ctx.mem) + .unwrap() + .next() + .unwrap(), + None, + ) + .unwrap(); + let uds_path = format!("test_vsock_{}.sock", name); + let id = format!("test_vsock_{name}"); + let muxer = VsockMuxer::new(id, PEER_CID, uds_path, true, None).unwrap(); + + Self { + _vsock_test_ctx: vsock_test_ctx, + pkt, + muxer, + } + } + + fn init_pkt(&mut self, local_port: u32, peer_port: u32, op: u16) -> &mut VsockPacket { + for b in self.pkt.hdr_mut() { + *b = 0; + } + self.pkt + .set_type(uapi::VSOCK_TYPE_STREAM) + .set_src_cid(PEER_CID) + .set_dst_cid(uapi::VSOCK_HOST_CID) + .set_src_port(peer_port) + .set_dst_port(local_port) + .set_op(op) + .set_buf_alloc(PEER_BUF_ALLOC) + } + + fn init_data_pkt( + &mut self, + local_port: u32, + peer_port: u32, + data: &[u8], + ) -> &mut VsockPacket { + assert!(data.len() <= self.pkt.buf().unwrap().len()); + self.init_pkt(local_port, peer_port, uapi::VSOCK_OP_RW) + .set_len(data.len() as u32); + self.pkt.buf_mut().unwrap()[..data.len()].copy_from_slice(data); + &mut self.pkt + } + + fn send(&mut self) { + self.muxer.send_pkt(&self.pkt).unwrap(); + } + + fn recv(&mut self) { + self.muxer.recv_pkt(&mut self.pkt).unwrap(); + } + + fn notify_muxer(&mut self) { + self.muxer.notify(epoll::Events::EPOLLIN); + } + + fn count_epoll_listeners(&self) -> (usize, usize) { + let mut local_lsn_count = 0usize; + let mut conn_lsn_count = 0usize; + for key in self.muxer.listener_map.values() { + match key { + EpollListener::LocalStream(_) => local_lsn_count += 1, + EpollListener::Connection { .. } => conn_lsn_count += 1, + _ => (), + }; + } + (local_lsn_count, conn_lsn_count) + } + + fn create_local_listener(&self, port: u32) -> LocalListener { + LocalListener::new(format!("{}_{}", self.muxer.host_sock_path, port)) + } + + fn local_connect(&mut self, peer_port: u32) -> (UnixStream, u32) { + let (init_local_lsn_count, init_conn_lsn_count) = self.count_epoll_listeners(); + + let mut stream = UnixStream::connect(self.muxer.host_sock_path.clone()).unwrap(); + stream.set_nonblocking(true).unwrap(); + // The muxer would now get notified of a new connection having arrived at its Unix + // socket, so it can accept it. + self.notify_muxer(); + + // Just after having accepted a new local connection, the muxer should've added a new + // `LocalStream` listener to its `listener_map`. + let (local_lsn_count, _) = self.count_epoll_listeners(); + assert_eq!(local_lsn_count, init_local_lsn_count + 1); + + let buf = format!("CONNECT {}\n", peer_port); + stream.write_all(buf.as_bytes()).unwrap(); + // The muxer would now get notified that data is available for reading from the locally + // initiated connection. + self.notify_muxer(); + + // Successfully reading and parsing the connection request should have removed the + // LocalStream epoll listener and added a Connection epoll listener. + let (local_lsn_count, conn_lsn_count) = self.count_epoll_listeners(); + assert_eq!(local_lsn_count, init_local_lsn_count); + assert_eq!(conn_lsn_count, init_conn_lsn_count + 1); + + // A LocalInit connection should've been added to the muxer connection map. A new + // local port should also have been allocated for the new LocalInit connection. + let local_port = self.muxer.local_port_last; + let key = ConnMapKey { + local_port, + peer_port, + }; + assert!(self.muxer.conn_map.contains_key(&key)); + assert!(self.muxer.local_port_set.contains(&local_port)); + + // A connection request for the peer should now be available from the muxer. + assert!(self.muxer.has_pending_rx()); + self.recv(); + assert_eq!(self.pkt.op(), uapi::VSOCK_OP_REQUEST); + assert_eq!(self.pkt.dst_port(), peer_port); + assert_eq!(self.pkt.src_port(), local_port); + + self.init_pkt(local_port, peer_port, uapi::VSOCK_OP_RESPONSE); + self.send(); + + let mut buf = vec![0u8; 32]; + let len = stream.read(&mut buf[..]).unwrap(); + assert_eq!(&buf[..len], format!("OK {}\n", local_port).as_bytes()); + + (stream, local_port) + } + } + + struct LocalListener { + path: PathBuf, + sock: UnixListener, + } + impl LocalListener { + fn new + Clone>(path: P) -> Self { + let path_buf = path.as_ref().to_path_buf(); + let sock = UnixListener::bind(path).unwrap(); + sock.set_nonblocking(true).unwrap(); + Self { + path: path_buf, + sock, + } + } + fn accept(&mut self) -> UnixStream { + let (stream, _) = self.sock.accept().unwrap(); + stream.set_nonblocking(true).unwrap(); + stream + } + } + impl Drop for LocalListener { + fn drop(&mut self) { + std::fs::remove_file(&self.path).unwrap(); + } + } + + #[test] + fn test_muxer_epoll_listener() { + let ctx = MuxerTestContext::new("muxer_epoll_listener"); + assert_eq!(ctx.muxer.get_polled_fd(), ctx.muxer.epoll_file.as_raw_fd()); + assert_eq!(ctx.muxer.get_polled_evset(), epoll::Events::EPOLLIN); + } + + fn temp_file() -> std::fs::File { + let mut path = std::env::temp_dir(); + path.push(format!( + "muxer-passfd-test-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + + let file = std::fs::OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .open(&path) + .unwrap(); + std::fs::remove_file(path).unwrap(); + file + } + + fn fd_identity(fd: RawFd) -> Option<(libc::dev_t, libc::ino_t)> { + let mut stat: libc::stat = unsafe { std::mem::zeroed() }; + if unsafe { libc::fstat(fd, &mut stat) } == 0 { + Some((stat.st_dev, stat.st_ino)) + } else { + None + } + } + + #[test] + fn test_passfd_validation_accepts_supported_fd_types() { + let file = temp_file(); + // Regular files cannot be registered with epoll (EPERM), so they are + // intentionally not supported by the passfd backend. + assert!(!VsockMuxer::validate_passfd(file.as_raw_fd())); + + let (socket, _peer) = UnixStream::pair().unwrap(); + assert!(VsockMuxer::validate_passfd(socket.as_raw_fd())); + + let mut pipe_fds = [0; 2]; + assert_eq!(unsafe { libc::pipe(pipe_fds.as_mut_ptr()) }, 0); + let pipe_read = unsafe { std::fs::File::from_raw_fd(pipe_fds[0]) }; + let _pipe_write = unsafe { std::fs::File::from_raw_fd(pipe_fds[1]) }; + assert!(VsockMuxer::validate_passfd(pipe_read.as_raw_fd())); + } + + #[test] + fn test_passfd_validation_rejects_unsupported_fd_types() { + let dir = std::fs::File::open(std::env::temp_dir()).unwrap(); + + assert!(!VsockMuxer::validate_passfd(dir.as_raw_fd())); + assert!(!VsockMuxer::validate_passfd(-1)); + } + + #[test] + fn test_prepare_local_passfds_rejects_mixed_batch_atomically() { + let mut ctx = MuxerTestContext::new("passfd_mixed_batch"); + let (valid, _peer) = UnixStream::pair().unwrap(); + let invalid = temp_file(); + let valid_fd = unsafe { libc::dup(valid.as_raw_fd()) }; + let invalid_fd = unsafe { libc::dup(invalid.as_raw_fd()) }; + assert!(valid_fd >= 0); + assert!(invalid_fd >= 0); + let valid_identity = fd_identity(valid_fd).unwrap(); + let invalid_identity = fd_identity(invalid_fd).unwrap(); + + let initial_port = ctx.muxer.local_port_last; + let initial_ports = ctx.muxer.local_port_set.len(); + let initial_connections = ctx.muxer.conn_map.len(); + { + let mut requests = vec![ + PassFdRequest { + label: PassFdLabel::Stdin, + port: 1027, + fd: valid_fd, + }, + PassFdRequest { + label: PassFdLabel::Stdout, + port: 1027, + fd: invalid_fd, + }, + ]; + + assert!(ctx.muxer.prepare_local_passfds(&mut requests).is_none()); + assert_eq!(ctx.muxer.local_port_last, initial_port); + assert_eq!(ctx.muxer.local_port_set.len(), initial_ports); + assert_eq!(ctx.muxer.conn_map.len(), initial_connections); + } + + // The fd numbers can be reused immediately by another parallel test. + // Verify that they no longer refer to the received file descriptions. + assert_ne!(fd_identity(valid_fd), Some(valid_identity)); + assert_ne!(fd_identity(invalid_fd), Some(invalid_identity)); + } + + #[test] + fn test_set_passfd_nonblocking_sets_flag() { + let (stream, _peer) = UnixStream::pair().unwrap(); + let fd = stream.as_raw_fd(); + + assert!(VsockMuxer::set_passfd_nonblocking(fd)); + + let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) }; + assert!(flags >= 0); + assert_ne!(flags & libc::O_NONBLOCK, 0); + assert!(!VsockMuxer::set_passfd_nonblocking(-1)); + } + + #[test] + fn test_bad_peer_pkt() { + const LOCAL_PORT: u32 = 1026; + const PEER_PORT: u32 = 1025; + const SOCK_DGRAM: u16 = 2; + + let mut ctx = MuxerTestContext::new("bad_peer_pkt"); + ctx.init_pkt(LOCAL_PORT, PEER_PORT, uapi::VSOCK_OP_REQUEST) + .set_type(SOCK_DGRAM); + ctx.send(); + + // The guest sent a SOCK_DGRAM packet. Per the vsock spec, we need to reply with an RST + // packet, since vsock only supports stream sockets. + assert!(ctx.muxer.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + assert_eq!(ctx.pkt.src_cid(), uapi::VSOCK_HOST_CID); + assert_eq!(ctx.pkt.dst_cid(), PEER_CID); + assert_eq!(ctx.pkt.src_port(), LOCAL_PORT); + assert_eq!(ctx.pkt.dst_port(), PEER_PORT); + + // Any orphan (i.e. without a connection), non-RST packet, should be replied to with an + // RST. + let bad_ops = [ + uapi::VSOCK_OP_RESPONSE, + uapi::VSOCK_OP_CREDIT_REQUEST, + uapi::VSOCK_OP_CREDIT_UPDATE, + uapi::VSOCK_OP_SHUTDOWN, + uapi::VSOCK_OP_RW, + ]; + for op in bad_ops.iter() { + ctx.init_pkt(LOCAL_PORT, PEER_PORT, *op); + ctx.send(); + assert!(ctx.muxer.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + assert_eq!(ctx.pkt.src_port(), LOCAL_PORT); + assert_eq!(ctx.pkt.dst_port(), PEER_PORT); + } + + // Any packet addressed to anything other than VSOCK_VHOST_CID should get dropped. + assert!(!ctx.muxer.has_pending_rx()); + ctx.init_pkt(LOCAL_PORT, PEER_PORT, uapi::VSOCK_OP_REQUEST) + .set_dst_cid(uapi::VSOCK_HOST_CID + 1); + ctx.send(); + assert!(!ctx.muxer.has_pending_rx()); + } + + #[test] + fn test_peer_connection() { + const LOCAL_PORT: u32 = 1026; + const PEER_PORT: u32 = 1025; + + let mut ctx = MuxerTestContext::new("peer_connection"); + + // Test peer connection refused. + ctx.init_pkt(LOCAL_PORT, PEER_PORT, uapi::VSOCK_OP_REQUEST); + ctx.send(); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + assert_eq!(ctx.pkt.len(), 0); + assert_eq!(ctx.pkt.src_cid(), uapi::VSOCK_HOST_CID); + assert_eq!(ctx.pkt.dst_cid(), PEER_CID); + assert_eq!(ctx.pkt.src_port(), LOCAL_PORT); + assert_eq!(ctx.pkt.dst_port(), PEER_PORT); + + // Test peer connection accepted. + let mut listener = ctx.create_local_listener(LOCAL_PORT); + ctx.init_pkt(LOCAL_PORT, PEER_PORT, uapi::VSOCK_OP_REQUEST); + ctx.send(); + assert_eq!(ctx.muxer.conn_map.len(), 1); + let mut stream = listener.accept(); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RESPONSE); + assert_eq!(ctx.pkt.len(), 0); + assert_eq!(ctx.pkt.src_cid(), uapi::VSOCK_HOST_CID); + assert_eq!(ctx.pkt.dst_cid(), PEER_CID); + assert_eq!(ctx.pkt.src_port(), LOCAL_PORT); + assert_eq!(ctx.pkt.dst_port(), PEER_PORT); + let key = ConnMapKey { + local_port: LOCAL_PORT, + peer_port: PEER_PORT, + }; + assert!(ctx.muxer.conn_map.contains_key(&key)); + + // Test guest -> host data flow. + let data = [1, 2, 3, 4]; + ctx.init_data_pkt(LOCAL_PORT, PEER_PORT, &data); + ctx.send(); + let mut buf = vec![0; data.len()]; + stream.read_exact(buf.as_mut_slice()).unwrap(); + assert_eq!(buf.as_slice(), data); + + // Test host -> guest data flow. + let data = [5u8, 6, 7, 8]; + stream.write_all(&data).unwrap(); + + // When data is available on the local stream, an EPOLLIN event would normally be delivered + // to the muxer's nested epoll FD. For testing only, we can fake that event notification + // here. + ctx.notify_muxer(); + // After being notified, the muxer should've figured out that RX data was available for one + // of its connections, so it should now be reporting that it can fill in an RX packet. + assert!(ctx.muxer.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RW); + assert_eq!(ctx.pkt.buf().unwrap()[..data.len()], data); + assert_eq!(ctx.pkt.src_port(), LOCAL_PORT); + assert_eq!(ctx.pkt.dst_port(), PEER_PORT); + + assert!(!ctx.muxer.has_pending_rx()); + } + + #[test] + fn test_local_connection() { + let mut ctx = MuxerTestContext::new("local_connection"); + let peer_port = 1025; + let (mut stream, local_port) = ctx.local_connect(peer_port); + + // Test guest -> host data flow. + let data = [1, 2, 3, 4]; + ctx.init_data_pkt(local_port, peer_port, &data); + ctx.send(); + + let mut buf = vec![0u8; data.len()]; + stream.read_exact(buf.as_mut_slice()).unwrap(); + assert_eq!(buf.as_slice(), &data); + + // Test host -> guest data flow. + let data = [5, 6, 7, 8]; + stream.write_all(&data).unwrap(); + ctx.notify_muxer(); + + assert!(ctx.muxer.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RW); + assert_eq!(ctx.pkt.src_port(), local_port); + assert_eq!(ctx.pkt.dst_port(), peer_port); + assert_eq!(ctx.pkt.buf().unwrap()[..data.len()], data); + } + + #[test] + fn test_local_close() { + let peer_port = 1025; + let mut ctx = MuxerTestContext::new("local_close"); + let local_port; + { + let (_stream, local_port_) = ctx.local_connect(peer_port); + local_port = local_port_; + } + // Local var `_stream` was now dropped, thus closing the local stream. After the muxer gets + // notified via EPOLLIN, it should attempt to gracefully shutdown the connection, issuing a + // VSOCK_OP_SHUTDOWN with both no-more-send and no-more-recv indications set. + ctx.notify_muxer(); + assert!(ctx.muxer.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_SHUTDOWN); + assert_ne!(ctx.pkt.flags() & uapi::VSOCK_FLAGS_SHUTDOWN_SEND, 0); + assert_ne!(ctx.pkt.flags() & uapi::VSOCK_FLAGS_SHUTDOWN_RCV, 0); + assert_eq!(ctx.pkt.src_port(), local_port); + assert_eq!(ctx.pkt.dst_port(), peer_port); + + // The connection should get removed (and its local port freed), after the peer replies + // with an RST. + ctx.init_pkt(local_port, peer_port, uapi::VSOCK_OP_RST); + ctx.send(); + let key = ConnMapKey { + local_port, + peer_port, + }; + assert!(!ctx.muxer.conn_map.contains_key(&key)); + assert!(!ctx.muxer.local_port_set.contains(&local_port)); + } + + #[test] + fn test_peer_close() { + let peer_port = 1025; + let local_port = 1026; + let mut ctx = MuxerTestContext::new("peer_close"); + + let mut sock = ctx.create_local_listener(local_port); + ctx.init_pkt(local_port, peer_port, uapi::VSOCK_OP_REQUEST); + ctx.send(); + let mut stream = sock.accept(); + + assert!(ctx.muxer.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RESPONSE); + assert_eq!(ctx.pkt.src_port(), local_port); + assert_eq!(ctx.pkt.dst_port(), peer_port); + let key = ConnMapKey { + local_port, + peer_port, + }; + assert!(ctx.muxer.conn_map.contains_key(&key)); + + // Emulate a full shutdown from the peer (no-more-send + no-more-recv). + ctx.init_pkt(local_port, peer_port, uapi::VSOCK_OP_SHUTDOWN) + .set_flag(uapi::VSOCK_FLAGS_SHUTDOWN_SEND) + .set_flag(uapi::VSOCK_FLAGS_SHUTDOWN_RCV); + ctx.send(); + + // Now, the muxer should remove the connection from its map, and reply with an RST. + assert!(ctx.muxer.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + assert_eq!(ctx.pkt.src_port(), local_port); + assert_eq!(ctx.pkt.dst_port(), peer_port); + let key = ConnMapKey { + local_port, + peer_port, + }; + assert!(!ctx.muxer.conn_map.contains_key(&key)); + + // The muxer should also drop / close the local Unix socket for this connection. + let mut buf = vec![0u8; 16]; + assert_eq!(stream.read(buf.as_mut_slice()).unwrap(), 0); + } + + #[test] + fn test_muxer_rxq() { + let mut ctx = MuxerTestContext::new("muxer_rxq"); + let local_port = 1026; + let peer_port_first = 1025; + let mut listener = ctx.create_local_listener(local_port); + let mut streams: Vec = Vec::new(); + + for peer_port in peer_port_first..peer_port_first + defs::MUXER_RXQ_SIZE { + ctx.init_pkt(local_port, peer_port as u32, uapi::VSOCK_OP_REQUEST); + ctx.send(); + streams.push(listener.accept()); + } + + // The muxer RX queue should now be full (with connection responses), but still + // synchronized. + assert!(ctx.muxer.rxq.is_synced()); + + // One more queued reply should desync the RX queue. + ctx.init_pkt( + local_port, + (peer_port_first + defs::MUXER_RXQ_SIZE) as u32, + uapi::VSOCK_OP_REQUEST, + ); + ctx.send(); + assert!(!ctx.muxer.rxq.is_synced()); + + // With an out-of-sync queue, an RST should evict any non-RST packet from the queue, and + // take its place. We'll check that by making sure that the last packet popped from the + // queue is an RST. + ctx.init_pkt( + local_port + 1, + peer_port_first as u32, + uapi::VSOCK_OP_REQUEST, + ); + ctx.send(); + + for peer_port in peer_port_first..peer_port_first + defs::MUXER_RXQ_SIZE - 1 { + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RESPONSE); + // The response order should hold. The evicted response should have been the last + // enqueued. + assert_eq!(ctx.pkt.dst_port(), peer_port as u32); + } + // There should be one more packet in the queue: the RST. + assert_eq!(ctx.muxer.rxq.len(), 1); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + + // The queue should now be empty, but out-of-sync, so the muxer should report it has some + // pending RX. + assert!(ctx.muxer.rxq.is_empty()); + assert!(!ctx.muxer.rxq.is_synced()); + assert!(ctx.muxer.has_pending_rx()); + + // The next recv should sync the queue back up. It should also yield one of the two + // responses that are still left: + // - the one that desynchronized the queue; and + // - the one that got evicted by the RST. + ctx.recv(); + assert!(ctx.muxer.rxq.is_synced()); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RESPONSE); + + assert!(ctx.muxer.has_pending_rx()); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RESPONSE); + } + + #[test] + fn test_muxer_killq() { + let mut ctx = MuxerTestContext::new("muxer_killq"); + let local_port = 1026; + let peer_port_first = 1025; + let peer_port_last = peer_port_first + defs::MUXER_KILLQ_SIZE; + let mut listener = ctx.create_local_listener(local_port); + + for peer_port in peer_port_first..=peer_port_last { + ctx.init_pkt(local_port, peer_port as u32, uapi::VSOCK_OP_REQUEST); + ctx.send(); + ctx.notify_muxer(); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RESPONSE); + assert_eq!(ctx.pkt.src_port(), local_port); + assert_eq!(ctx.pkt.dst_port(), peer_port as u32); + { + let _stream = listener.accept(); + } + ctx.notify_muxer(); + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_SHUTDOWN); + assert_eq!(ctx.pkt.src_port(), local_port); + assert_eq!(ctx.pkt.dst_port(), peer_port as u32); + // The kill queue should be synchronized, up until the `defs::MUXER_KILLQ_SIZE`th + // connection we schedule for termination. + assert_eq!( + ctx.muxer.killq.is_synced(), + peer_port < peer_port_first + defs::MUXER_KILLQ_SIZE + ); + } + + assert!(!ctx.muxer.killq.is_synced()); + assert!(!ctx.muxer.has_pending_rx()); + + // Wait for the kill timers to expire. + std::thread::sleep(std::time::Duration::from_millis( + csm_defs::CONN_SHUTDOWN_TIMEOUT_MS, + )); + + // Trigger a kill queue sweep, by requesting a new connection. + ctx.init_pkt( + local_port, + peer_port_last as u32 + 1, + uapi::VSOCK_OP_REQUEST, + ); + ctx.send(); + + // After sweeping the kill queue, it should now be synced (assuming the RX queue is larger + // than the kill queue, since an RST packet will be queued for each killed connection). + assert!(ctx.muxer.killq.is_synced()); + assert!(ctx.muxer.has_pending_rx()); + // There should be `defs::MUXER_KILLQ_SIZE` RSTs in the RX queue, from terminating the + // dying connections in the recent killq sweep. + for _p in peer_port_first..peer_port_last { + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RST); + assert_eq!(ctx.pkt.src_port(), local_port); + } + + // There should be one more packet in the RX queue: the connection response our request + // that triggered the kill queue sweep. + ctx.recv(); + assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_RESPONSE); + assert_eq!(ctx.pkt.dst_port(), peer_port_last as u32 + 1); + + assert!(!ctx.muxer.has_pending_rx()); + } + + #[test] + fn test_regression_handshake() { + // Address one of the issues found while fixing the following issue: + // https://github.com/firecracker-microvm/firecracker/issues/1751 + // This test checks that the handshake message is not accounted for + let mut ctx = MuxerTestContext::new("regression_handshake"); + let peer_port = 1025; + + // Create a local connection. + let (_, local_port) = ctx.local_connect(peer_port); + + // Get the connection from the connection map. + let key = ConnMapKey { + local_port, + peer_port, + }; + let conn = ctx.muxer.conn_map.get_mut(&key).unwrap(); + + // Check that fwd_cnt is 0 - "OK ..." was not accounted for. + assert_eq!(conn.fwd_cnt().0, 0); + } + + #[test] + fn test_regression_rxq_pop() { + // Address one of the issues found while fixing the following issue: + // https://github.com/firecracker-microvm/firecracker/issues/1751 + // This test checks that a connection is not popped out of the muxer + // rxq when multiple flags are set + let mut ctx = MuxerTestContext::new("regression_rxq_pop"); + let peer_port = 1025; + let (mut stream, local_port) = ctx.local_connect(peer_port); + + // Send some data. + let data = [5u8, 6, 7, 8]; + stream.write_all(&data).unwrap(); + ctx.notify_muxer(); + + // Get the connection from the connection map. + let key = ConnMapKey { + local_port, + peer_port, + }; + let conn = ctx.muxer.conn_map.get_mut(&key).unwrap(); + + // Forcefully insert another flag. + conn.insert_credit_update(); + + // Call recv twice in order to check that the connection is still + // in the rxq. + assert!(ctx.muxer.has_pending_rx()); + ctx.recv(); + assert!(ctx.muxer.has_pending_rx()); + ctx.recv(); + + // Since initially the connection had two flags set, now there should + // not be any pending RX in the muxer. + assert!(!ctx.muxer.has_pending_rx()); + } + + #[test] + fn test_read_local_stream_port_connect_and_passfd() { + let mut ctx = MuxerTestContext::new("read_local_stream_port"); + + // Test "connect" + let (mut s1, mut s2) = std::os::unix::net::UnixStream::pair().unwrap(); + s1.write_all(b"connect 5678\n").unwrap(); + + let res = ctx.muxer.read_local_stream_port(&mut s2); + match res { + Ok(LocalStreamCommand::Connect(port)) => { + assert_eq!(port, 5678); + } + _ => panic!("Expected Connect result, got {:?}", res.err()), + } + + // Test batched "passfd" + let (s3, mut s4) = std::os::unix::net::UnixStream::pair().unwrap(); + let req = b"passfd 1234 stdin stdout\n"; + let fds = [s3.as_raw_fd(), s3.as_raw_fd()]; + s3.send_with_fd(req, &fds).unwrap(); + + let res = ctx.muxer.read_local_stream_port(&mut s4); + match res { + Ok(LocalStreamCommand::PassFds(requests)) => { + assert_eq!(requests.len(), 2); + assert_eq!(requests[0].label, PassFdLabel::Stdin); + assert_eq!(requests[0].port, 1234); + assert!(requests[0].fd > 0); + assert_eq!(requests[1].label, PassFdLabel::Stdout); + assert_eq!(requests[1].port, 1234); + assert!(requests[1].fd > 0); + } + _ => panic!("Expected PassFds result, got {:?}", res.err()), + } + + // Legacy single-stream passfd commands did not carry a label. + let (s5, mut s6) = std::os::unix::net::UnixStream::pair().unwrap(); + s5.send_with_fd(b"passfd 1234\n", &[s5.as_raw_fd()]) + .unwrap(); + + let res = ctx.muxer.read_local_stream_port(&mut s6); + match res { + Ok(LocalStreamCommand::PassFds(requests)) => { + assert_eq!(requests.len(), 1); + assert_eq!(requests[0].label, PassFdLabel::Stream); + assert_eq!(requests[0].port, 1234); + assert!(requests[0].fd > 0); + } + _ => panic!("Expected legacy PassFds result, got {:?}", res.err()), + } + } + + #[test] + fn test_read_local_stream_port_closes_fds_on_parse_error() { + let mut ctx = MuxerTestContext::new("passfd_parse_error"); + let (control, mut receiver) = UnixStream::pair().unwrap(); + let (passed, mut observer) = UnixStream::pair().unwrap(); + control + .send_with_fd(b"passfd invalid stdout\n", &[passed.as_raw_fd()]) + .unwrap(); + + assert!(ctx.muxer.read_local_stream_port(&mut receiver).is_err()); + drop(passed); + + let mut byte = [0; 1]; + assert_eq!(observer.read(&mut byte).unwrap(), 0); + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_killq.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_killq.rs new file mode 100644 index 0000000..a537d3f --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_killq.rs @@ -0,0 +1,140 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// + +/// `MuxerKillQ` implements a helper object that `VsockMuxer` can use for scheduling forced +/// connection termination. I.e. after one peer issues a clean shutdown request +/// (VSOCK_OP_SHUTDOWN), the concerned connection is queued for termination (VSOCK_OP_RST) in +/// the near future (herein implemented via an expiring timer). +/// +/// Whenever the muxer needs to schedule a connection for termination, it pushes it (or rather +/// an identifier - the connection key) to this queue. A subsequent pop() operation will +/// succeed if and only if the first connection in the queue is ready to be terminated (i.e. +/// its kill timer expired). +/// +/// Without using this queue, the muxer would have to walk its entire connection pool +/// (hashmap), whenever it needs to check for expired kill timers. With this queue, both +/// scheduling and termination are performed in constant time. However, since we don't want to +/// waste space on a kill queue that's as big as the connection hashmap itself, it is possible +/// that this queue may become full at times. We call this kill queue "synchronized" if we are +/// certain that all connections that are awaiting termination are present in the queue. This +/// means a simple constant-time pop() operation is enough to check whether any connections +/// need to be terminated. When the kill queue becomes full, though, pushing fails, so +/// connections that should be terminated are left out. The queue is not synchronized anymore. +/// When that happens, the muxer will first drain the queue, and then replace it with a new +/// queue, created by walking the connection pool, looking for connections that will be +/// expiring in the future. +/// +use std::collections::{HashMap, VecDeque}; +use std::time::Instant; + +use super::defs; +use super::muxer::ConnMapKey; +use super::MuxerConnection; + +/// A kill queue item, holding the connection key and the scheduled time for termination. +/// +#[derive(Clone, Copy)] +struct MuxerKillQItem { + key: ConnMapKey, + kill_time: Instant, +} + +/// The connection kill queue: a FIFO structure, storing the connections that are scheduled for +/// termination. +/// +pub struct MuxerKillQ { + /// The kill queue contents. + q: VecDeque, + + /// The kill queue sync status: + /// - when true, all connections that are awaiting termination are guaranteed to be in this + /// queue; + /// - when false, some connections may have been left out. + /// + synced: bool, +} + +impl MuxerKillQ { + const SIZE: usize = defs::MUXER_KILLQ_SIZE; + + /// Trivial kill queue constructor. + /// + pub fn new() -> Self { + Self { + q: VecDeque::with_capacity(Self::SIZE), + synced: true, + } + } + + /// Create a kill queue by walking the connection pool, looking for connections that are + /// set to expire at some point in the future. + /// Note: if more than `Self::SIZE` connections are found, the queue will be created in an + /// out-of-sync state, and will be discarded after it is emptied. + /// + pub fn from_conn_map(conn_map: &HashMap) -> Self { + let mut q_buf: Vec = Vec::with_capacity(Self::SIZE); + let mut synced = true; + for (key, conn) in conn_map.iter() { + if !conn.will_expire() { + continue; + } + if q_buf.len() >= Self::SIZE { + synced = false; + break; + } + q_buf.push(MuxerKillQItem { + key: *key, + kill_time: conn.expiry().unwrap(), + }); + } + q_buf.sort_unstable_by_key(|it| it.kill_time); + Self { + q: q_buf.into(), + synced, + } + } + + /// Push a connection key to the queue, scheduling it for termination at + /// `CONN_SHUTDOWN_TIMEOUT_MS` from now (the push time). + /// + pub fn push(&mut self, key: ConnMapKey, kill_time: Instant) { + if !self.is_synced() || self.is_full() { + self.synced = false; + return; + } + self.q.push_back(MuxerKillQItem { key, kill_time }); + } + + /// Attempt to pop an expired connection from the kill queue. + /// + /// This will succeed and return a connection key, only if the connection at the front of + /// the queue has expired. Otherwise, `None` is returned. + /// + pub fn pop(&mut self) -> Option { + if let Some(item) = self.q.front() { + if Instant::now() > item.kill_time { + return Some(self.q.pop_front().unwrap().key); + } + } + None + } + + /// Check if the kill queue is synchronized with the connection pool. + /// + pub fn is_synced(&self) -> bool { + self.synced + } + + /// Check if the kill queue is empty, obviously. + /// + pub fn is_empty(&self) -> bool { + self.q.len() == 0 + } + + /// Check if the kill queue is full. + /// + pub fn is_full(&self) -> bool { + self.q.len() == Self::SIZE + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_rxq.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_rxq.rs new file mode 100644 index 0000000..3b02b54 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_rxq.rs @@ -0,0 +1,145 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// + +/// `MuxerRxQ` implements a helper object that `VsockMuxer` can use for queuing RX (host -> guest) +/// packets (or rather instructions on how to build said packets). +/// +/// Under ideal operation, every connection, that has pending RX data, will be present in the muxer +/// RX queue. However, since the RX queue is smaller than the connection pool, it may, under some +/// conditions, become full, meaning that it can no longer account for all the connections that can +/// yield RX data. When that happens, we say that it is no longer "synchronized" (i.e. with the +/// connection pool). A desynchronized RX queue still holds valid data, and the muxer will +/// continue to pop packets from it. However, when a desynchronized queue is drained, additional +/// data may still be available, so the muxer will have to perform a more costly walk of the entire +/// connection pool to find it. This walk is performed here, as part of building an RX queue from +/// the connection pool. When an out-of-sync is drained, the muxer will discard it, and attempt to +/// rebuild a synced one. +/// +use std::collections::{HashMap, VecDeque}; + +use super::super::VsockChannel; +use super::defs; +use super::muxer::{ConnMapKey, MuxerRx}; +use super::MuxerConnection; + +/// The muxer RX queue. +/// +pub struct MuxerRxQ { + /// The RX queue data. + q: VecDeque, + /// The RX queue sync status. + synced: bool, +} + +impl MuxerRxQ { + const SIZE: usize = defs::MUXER_RXQ_SIZE; + + /// Trivial RX queue constructor. + /// + pub fn new() -> Self { + Self { + q: VecDeque::with_capacity(Self::SIZE), + synced: true, + } + } + + /// Attempt to build an RX queue, that is synchronized to the connection pool. + /// Note: the resulting queue may still be desynchronized, if there are too many connections + /// that have pending RX data. In that case, the muxer will first drain this queue, and + /// then try again to build a synchronized one. + /// + pub fn from_conn_map(conn_map: &HashMap) -> Self { + let mut q = VecDeque::new(); + let mut synced = true; + + for (key, conn) in conn_map.iter() { + if !conn.has_pending_rx() { + continue; + } + if q.len() >= Self::SIZE { + synced = false; + break; + } + q.push_back(MuxerRx::ConnRx(*key)); + } + Self { q, synced } + } + + /// Push a new RX item to the queue. + /// + /// A push will fail when: + /// - trying to push a connection key onto an out-of-sync, or full queue; or + /// - trying to push an RST onto a queue already full of RSTs. + /// RSTs take precedence over connections, because connections can always be queried for + /// pending RX data later. Aside from this queue, there is no other storage for RSTs, so + /// failing to push one means that we have to drop the packet. + /// + /// Returns: + /// - `true` if the new item has been successfully queued; or + /// - `false` if there was no room left in the queue. + /// + pub fn push(&mut self, rx: MuxerRx) -> bool { + // Pushing to a non-full, synchronized queue will always succeed. + if self.is_synced() && !self.is_full() { + self.q.push_back(rx); + return true; + } + + match rx { + MuxerRx::RstPkt { .. } => { + // If we just failed to push an RST packet, we'll look through the queue, trying to + // find a connection key that we could evict. This way, the queue does lose sync, + // but we don't drop any packets. + for qi in self.q.iter_mut().rev() { + if let MuxerRx::ConnRx(_) = qi { + *qi = rx; + self.synced = false; + return true; + } + } + } + MuxerRx::ConnRx(_) => { + self.synced = false; + } + }; + + false + } + + /// Peek into the front of the queue. + /// + pub fn peek(&self) -> Option { + self.q.front().copied() + } + + /// Pop an RX item from the front of the queue. + /// + pub fn pop(&mut self) -> Option { + self.q.pop_front() + } + + /// Check if the RX queue is synchronized with the connection pool. + /// + pub fn is_synced(&self) -> bool { + self.synced + } + + /// Get the total number of items in the queue. + /// + pub fn len(&self) -> usize { + self.q.len() + } + + /// Check if the queue is empty. + /// + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Check if the queue is full. + /// + pub fn is_full(&self) -> bool { + self.len() == Self::SIZE + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/stream.rs b/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/stream.rs new file mode 100644 index 0000000..4cdbbf6 --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/stream.rs @@ -0,0 +1,222 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Stream backends for vsock host-side connections. +// +// `VsockBackendStream` unifies two concrete stream types behind a single +// `Read + Write + AsRawFd` enum so that `VsockConnection` +// can handle both transparently: +// +// - `Unix(UnixStream)`: the classic path: each vsock connection maps to a +// host-side Unix domain socket. +// - `PassFd(PassFdStream)`: the high-performance path: the Shim passes +// container stdio pipe FDs to the VMM via `SCM_RIGHTS`, and data flows +// directly through those pipes instead of traversing a Unix socket. +// + +use std::fs::File; +use std::io::{Read, Write}; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::os::unix::net::UnixStream; + +pub enum VsockBackendStream { + Unix(UnixStream), + PassFd(PassFdStream), +} + +impl Read for VsockBackendStream { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + match self { + Self::Unix(s) => s.read(buf), + Self::PassFd(s) => s.read(buf), + } + } +} + +impl Write for VsockBackendStream { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + match self { + Self::Unix(s) => s.write(buf), + Self::PassFd(s) => s.write(buf), + } + } + + fn flush(&mut self) -> std::io::Result<()> { + match self { + Self::Unix(s) => s.flush(), + Self::PassFd(s) => s.flush(), + } + } +} + +impl AsRawFd for VsockBackendStream { + fn as_raw_fd(&self) -> RawFd { + match self { + Self::Unix(s) => s.as_raw_fd(), + Self::PassFd(s) => s.as_raw_fd(), + } + } +} + +impl VsockBackendStream { + pub fn send_connect_ack(&mut self, local_port: u32) -> std::io::Result<()> { + match self { + Self::Unix(s) => s.write_all(format!("OK {}\n", local_port).as_bytes()), + Self::PassFd(s) => s.write_ack(local_port), + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum PassFdLabel { + Stdin, + Stdout, + Stderr, + Stream, + Other(String), +} + +impl PassFdLabel { + pub fn as_str(&self) -> &str { + match self { + Self::Stdin => "stdin", + Self::Stdout => "stdout", + Self::Stderr => "stderr", + Self::Stream => "stream", + Self::Other(s) => s.as_str(), + } + } +} + +impl std::fmt::Display for PassFdLabel { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.as_str()) + } +} + +impl From<&str> for PassFdLabel { + fn from(s: &str) -> Self { + match s { + "stdin" => Self::Stdin, + "stdout" => Self::Stdout, + "stderr" => Self::Stderr, + "stream" => Self::Stream, + other => Self::Other(other.to_string()), + } + } +} + +pub struct PassFdStream { + pub file: File, + pub control: UnixStream, + pub label: PassFdLabel, +} + +impl PassFdStream { + pub fn new(file: File, control: UnixStream, label: PassFdLabel) -> Self { + Self { + file, + control, + label, + } + } + + fn write_ack(&mut self, local_port: u32) -> std::io::Result<()> { + self.control + .write_all(format!("OK {} {}\n", self.label, local_port).as_bytes()) + } +} + +impl Read for PassFdStream { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + self.file.read(buf) + } +} + +impl Write for PassFdStream { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.file.write(buf) + } + + fn flush(&mut self) -> std::io::Result<()> { + self.file.flush() + } +} + +impl AsRawFd for PassFdStream { + fn as_raw_fd(&self) -> RawFd { + self.file.as_raw_fd() + } +} + +#[cfg(test)] +mod tests { + use std::fs::OpenOptions; + use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write}; + use std::os::unix::net::UnixStream; + + use super::{PassFdLabel, PassFdStream, VsockBackendStream}; + + fn temp_file() -> std::fs::File { + let mut path = std::env::temp_dir(); + path.push(format!( + "passfd-stream-test-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + + let file = OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .open(&path) + .unwrap(); + std::fs::remove_file(path).unwrap(); + file + } + + #[test] + fn unix_stream_sends_plain_connect_ack() { + let (stream, mut peer) = UnixStream::pair().unwrap(); + let mut backend = VsockBackendStream::Unix(stream); + + backend.send_connect_ack(1024).unwrap(); + + let mut ack = String::new(); + let mut reader = BufReader::new(peer); + reader.read_line(&mut ack).unwrap(); + assert_eq!(ack, "OK 1024\n"); + } + + #[test] + fn passfd_stream_sends_labeled_connect_ack() { + let (control, mut peer) = UnixStream::pair().unwrap(); + let file = temp_file(); + let mut backend = + VsockBackendStream::PassFd(PassFdStream::new(file, control, PassFdLabel::Stdout)); + + backend.send_connect_ack(1025).unwrap(); + + let mut ack = String::new(); + let mut reader = BufReader::new(peer); + reader.read_line(&mut ack).unwrap(); + assert_eq!(ack, "OK stdout 1025\n"); + } + + #[test] + fn passfd_stream_proxies_file_io() { + let (control, _peer) = UnixStream::pair().unwrap(); + let file = temp_file(); + let mut stream = PassFdStream::new(file, control, PassFdLabel::Stdin); + + stream.write_all(b"payload").unwrap(); + stream.flush().unwrap(); + stream.file.seek(SeekFrom::Start(0)).unwrap(); + + let mut buf = [0; 7]; + stream.read_exact(&mut buf).unwrap(); + assert_eq!(&buf, b"payload"); + } +} diff --git a/vendor/cube-hypervisor/virtio-devices/src/watchdog.rs b/vendor/cube-hypervisor/virtio-devices/src/watchdog.rs new file mode 100644 index 0000000..60b5c7c --- /dev/null +++ b/vendor/cube-hypervisor/virtio-devices/src/watchdog.rs @@ -0,0 +1,419 @@ +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause +// +// Copyright © 2020 Intel Corporation +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use super::Error as DeviceError; +use super::{ + ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, VirtioCommon, + VirtioDevice, VirtioDeviceType, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_VERSION_1, +}; +use crate::seccomp_filters::Thread; +use crate::thread_helper::spawn_virtio_thread; +use crate::GuestMemoryMmap; +use crate::{VirtioInterrupt, VirtioInterruptType}; +use anyhow::anyhow; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::fs::File; +use std::io::{self, Read}; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::result; +use std::sync::atomic::AtomicBool; +use std::sync::{Arc, Barrier, Mutex}; +use std::time::Instant; +use thiserror::Error; +use virtio_queue::{Queue, QueueT}; +use vm_memory::{Bytes, GuestAddressSpace, GuestMemoryAtomic}; +use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +use vmm_sys_util::eventfd::EventFd; + +const QUEUE_SIZE: u16 = 8; +const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE]; + +// New descriptors are pending on the virtio queue. +const QUEUE_AVAIL_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1; +// Timer expired +const TIMER_EXPIRED_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2; + +// Number of seconds to check to see if there has been a ping +// This needs to match what the driver is using. +const WATCHDOG_TIMER_INTERVAL: i64 = 15; + +// Number of seconds since last ping to trigger reboot +const WATCHDOG_TIMEOUT: u64 = WATCHDOG_TIMER_INTERVAL as u64 + 5; + +#[derive(Error, Debug)] +enum Error { + #[error("Error programming timer fd: {0}")] + TimerfdSetup(io::Error), + #[error("Descriptor chain too short")] + DescriptorChainTooShort, + #[error("Failed adding used index: {0}")] + QueueAddUsed(virtio_queue::Error), + #[error("Invalid descriptor")] + InvalidDescriptor, + #[error("Failed to write to guest memory: {0}")] + GuestMemoryWrite(vm_memory::guest_memory::Error), +} + +struct WatchdogEpollHandler { + mem: GuestMemoryAtomic, + queue: Queue, + interrupt_cb: Arc, + queue_evt: EventFd, + kill_evt: EventFd, + pause_evt: EventFd, + timer: File, + last_ping_time: Arc>>, + reset_evt: EventFd, +} + +impl WatchdogEpollHandler { + // The main queue is very simple - the driver "pings" the device by passing it a (write-only) + // descriptor. In response the device writes a 1 into the descriptor and returns it to the driver + fn process_queue(&mut self) -> result::Result { + let queue = &mut self.queue; + let mut used_descs = false; + while let Some(mut desc_chain) = queue.pop_descriptor_chain(self.mem.memory()) { + let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?; + + if !(desc.is_write_only() && desc.len() > 0) { + return Err(Error::InvalidDescriptor); + } + + desc_chain + .memory() + .write_obj(1u8, desc.addr()) + .map_err(Error::GuestMemoryWrite)?; + + // If this is the first "ping" then setup the timer + if self.last_ping_time.lock().unwrap().is_none() { + info!( + "First ping received. Starting timer (every {} seconds)", + WATCHDOG_TIMER_INTERVAL + ); + timerfd_setup(&self.timer, WATCHDOG_TIMER_INTERVAL).map_err(Error::TimerfdSetup)?; + } + self.last_ping_time.lock().unwrap().replace(Instant::now()); + + queue + .add_used(desc_chain.memory(), desc_chain.head_index(), desc.len()) + .map_err(Error::QueueAddUsed)?; + used_descs = true; + } + + Ok(used_descs) + } + + fn signal_used_queue(&self) -> result::Result<(), DeviceError> { + self.interrupt_cb + .trigger(VirtioInterruptType::Queue(0)) + .map_err(|e| { + error!("Failed to signal used queue: {:?}", e); + DeviceError::FailedSignalingUsedQueue(e) + }) + } + + fn run( + &mut self, + paused: Arc, + paused_sync: Arc, + ) -> result::Result<(), EpollHelperError> { + let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?; + helper.add_event(self.queue_evt.as_raw_fd(), QUEUE_AVAIL_EVENT)?; + helper.add_event(self.timer.as_raw_fd(), TIMER_EXPIRED_EVENT)?; + helper.run(paused, paused_sync, self)?; + + Ok(()) + } +} + +impl EpollHelperHandler for WatchdogEpollHandler { + fn handle_event( + &mut self, + _helper: &mut EpollHelper, + event: &epoll::Event, + ) -> result::Result<(), EpollHelperError> { + let ev_type = event.data as u16; + match ev_type { + QUEUE_AVAIL_EVENT => { + self.queue_evt.read().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) + })?; + + let needs_notification = self.process_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Failed to process queue : {:?}", e)) + })?; + if needs_notification { + self.signal_used_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", + e + )) + })?; + } + } + TIMER_EXPIRED_EVENT => { + // When reading from the timerfd you get 8 bytes indicating + // the number of times this event has elapsed since the last read. + let mut buf = vec![0; 8]; + self.timer.read_exact(&mut buf).map_err(|e| { + EpollHelperError::HandleEvent(anyhow!("Error reading from timer fd: {:}", e)) + })?; + + if let Some(last_ping_time) = self.last_ping_time.lock().unwrap().as_ref() { + let now = Instant::now(); + let gap = now.duration_since(*last_ping_time).as_secs(); + if gap > WATCHDOG_TIMEOUT { + error!("Watchdog triggered: {} seconds since last ping", gap); + self.reset_evt.write(1).ok(); + } + } + } + _ => { + return Err(EpollHelperError::HandleEvent(anyhow!( + "Unexpected event: {}", + ev_type + ))); + } + } + Ok(()) + } +} + +/// Virtio device for exposing a watchdog to the guest +pub struct Watchdog { + common: VirtioCommon, + id: String, + seccomp_action: SeccompAction, + reset_evt: EventFd, + last_ping_time: Arc>>, + timer: File, + exit_evt: EventFd, +} + +#[derive(Serialize, Deserialize)] +pub struct WatchdogState { + pub avail_features: u64, + pub acked_features: u64, + pub enabled: bool, +} + +impl Watchdog { + /// Create a new virtio watchdog device that will reboot VM if the guest hangs + pub fn new( + id: String, + reset_evt: EventFd, + seccomp_action: SeccompAction, + exit_evt: EventFd, + state: Option, + ) -> io::Result { + let mut last_ping_time = None; + let (avail_features, acked_features) = if let Some(state) = state { + info!("Restoring virtio-watchdog {}", id); + + // When restoring enable the watchdog if it was previously enabled. + // We reset the timer to ensure that we don't unnecessarily reboot + // due to the offline time. + if state.enabled { + last_ping_time = Some(Instant::now()); + } + + (state.avail_features, state.acked_features) + } else { + (1u64 << VIRTIO_F_VERSION_1, 0) + }; + + let timer_fd = timerfd_create().map_err(|e| { + error!("Failed to create timer fd {}", e); + e + })?; + let timer = unsafe { File::from_raw_fd(timer_fd) }; + + Ok(Watchdog { + common: VirtioCommon { + device_type: VirtioDeviceType::Watchdog as u32, + queue_sizes: QUEUE_SIZES.to_vec(), + paused_sync: Some(Arc::new(Barrier::new(2))), + avail_features, + acked_features, + min_queues: 1, + ..Default::default() + }, + id, + seccomp_action, + reset_evt, + last_ping_time: Arc::new(Mutex::new(last_ping_time)), + timer, + exit_evt, + }) + } + + fn state(&self) -> WatchdogState { + WatchdogState { + avail_features: self.common.avail_features, + acked_features: self.common.acked_features, + enabled: self.last_ping_time.lock().unwrap().is_some(), + } + } + + #[cfg(fuzzing)] + pub fn wait_for_epoll_threads(&mut self) { + self.common.wait_for_epoll_threads(); + } +} + +impl Drop for Watchdog { + fn drop(&mut self) { + if let Some(kill_evt) = self.common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + } +} + +fn timerfd_create() -> Result { + let res = unsafe { libc::timerfd_create(libc::CLOCK_MONOTONIC, 0) }; + if res < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(res as RawFd) + } +} + +fn timerfd_setup(timer: &File, secs: i64) -> Result<(), io::Error> { + let periodic = libc::itimerspec { + it_interval: libc::timespec { + tv_sec: secs, + tv_nsec: 0, + }, + it_value: libc::timespec { + tv_sec: secs, + tv_nsec: 0, + }, + }; + + let res = + unsafe { libc::timerfd_settime(timer.as_raw_fd(), 0, &periodic, std::ptr::null_mut()) }; + + if res < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(()) + } +} + +impl VirtioDevice for Watchdog { + fn device_type(&self) -> u32 { + self.common.device_type + } + + fn queue_max_sizes(&self) -> &[u16] { + &self.common.queue_sizes + } + + fn features(&self) -> u64 { + self.common.avail_features + } + + fn ack_features(&mut self, value: u64) { + self.common.ack_features(value) + } + + fn activate( + &mut self, + mem: GuestMemoryAtomic, + interrupt_cb: Arc, + mut queues: Vec<(usize, Queue, EventFd)>, + ) -> ActivateResult { + self.common.activate(&queues, &interrupt_cb)?; + let (kill_evt, pause_evt) = self.common.dup_eventfds(); + + let reset_evt = self.reset_evt.try_clone().map_err(|e| { + error!("Failed to clone reset_evt eventfd: {}", e); + ActivateError::BadActivate + })?; + + let timer = self.timer.try_clone().map_err(|e| { + error!("Failed to clone timer fd: {}", e); + ActivateError::BadActivate + })?; + + let (_, queue, queue_evt) = queues.remove(0); + + let mut handler = WatchdogEpollHandler { + mem, + queue, + interrupt_cb, + queue_evt, + kill_evt, + pause_evt, + timer, + last_ping_time: self.last_ping_time.clone(), + reset_evt, + }; + + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); + let mut epoll_threads = Vec::new(); + + spawn_virtio_thread( + &self.id, + &self.seccomp_action, + Thread::VirtioWatchdog, + &mut epoll_threads, + &self.exit_evt, + move || handler.run(paused, paused_sync.unwrap()), + )?; + + self.common.epoll_threads = Some(epoll_threads); + + event!("virtio-device", "activated", "id", &self.id); + Ok(()) + } + + fn reset(&mut self) -> Option> { + let result = self.common.reset(); + event!("virtio-device", "reset", "id", &self.id); + result + } +} + +impl Pausable for Watchdog { + fn pause(&mut self) -> result::Result<(), MigratableError> { + info!("Watchdog paused - disabling timer"); + timerfd_setup(&self.timer, 0) + .map_err(|e| MigratableError::Pause(anyhow!("Error clearing timer: {:?}", e)))?; + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + // Reset the timer on pause if it was previously used + if self.last_ping_time.lock().unwrap().is_some() { + info!( + "Watchdog resumed - enabling timer (every {} seconds)", + WATCHDOG_TIMER_INTERVAL + ); + self.last_ping_time.lock().unwrap().replace(Instant::now()); + timerfd_setup(&self.timer, WATCHDOG_TIMER_INTERVAL) + .map_err(|e| MigratableError::Resume(anyhow!("Error setting timer: {:?}", e)))?; + } + self.common.resume() + } +} + +impl Snapshottable for Watchdog { + fn id(&self) -> String { + self.id.clone() + } + + fn snapshot(&mut self) -> std::result::Result { + Snapshot::new_from_state(&self.id, &self.state()) + } +} + +impl Transportable for Watchdog {} +impl Migratable for Watchdog {} diff --git a/vendor/cube-hypervisor/virtiofsd/.gitignore b/vendor/cube-hypervisor/virtiofsd/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/vendor/cube-hypervisor/virtiofsd/.gitlab-ci.yml b/vendor/cube-hypervisor/virtiofsd/.gitlab-ci.yml new file mode 100644 index 0000000..838c112 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/.gitlab-ci.yml @@ -0,0 +1,48 @@ +workflow: + rules: + - if: $CI_MERGE_REQUEST_ID # Execute jobs in merge request context + - if: $CI_COMMIT_BRANCH == 'main' # Execute jobs when a new commit is pushed to main branch + +image: "rust:alpine" + +clippy: + before_script: + - apk add musl-dev + - rustup component add clippy + script: + - cargo clippy --verbose -- -Dwarnings + +fmt: + before_script: + - rustup component add rustfmt + script: + - cargo fmt -v -- --check + +test: + before_script: + - apk add libcap-ng-static libseccomp-static musl-dev + script: + - rustc --version && cargo --version # Print version info for debugging + - RUSTFLAGS='-C target-feature=+crt-static -C link-self-contained=yes' LIBSECCOMP_LINK_TYPE=static LIBSECCOMP_LIB_PATH=/usr/lib LIBCAPNG_LINK_TYPE=static LIBCAPNG_LIB_PATH=/usr/lib cargo build --verbose --target x86_64-unknown-linux-musl + - RUSTFLAGS='-C target-feature=+crt-static -C link-self-contained=yes' LIBSECCOMP_LINK_TYPE=static LIBSECCOMP_LIB_PATH=/usr/lib LIBCAPNG_LINK_TYPE=static LIBCAPNG_LIB_PATH=/usr/lib cargo test --verbose --target x86_64-unknown-linux-musl + except: + # There is an equivalent job ('publish') that will run when code lands + # on the 'main' branch, except it will upload a built binary. These are + # kept separate to avoid accidentally uploading binaries when it's not + # necessary. + - main@virtio-fs/virtiofsd + +# Build a statically linked and optimized binary for publishing. +# +# This only runs when code is merged to the 'main' branch. +publish: + before_script: + - apk add libcap-ng-static libseccomp-static musl-dev + script: + - RUSTFLAGS='-C target-feature=+crt-static -C link-self-contained=yes' LIBSECCOMP_LINK_TYPE=static LIBSECCOMP_LIB_PATH=/usr/lib LIBCAPNG_LINK_TYPE=static LIBCAPNG_LIB_PATH=/usr/lib cargo build --release --target x86_64-unknown-linux-musl + artifacts: + name: "virtiofsd-$CI_COMMIT_SHORT_SHA" + paths: + - target/x86_64-unknown-linux-musl/release/virtiofsd + only: + - main@virtio-fs/virtiofsd diff --git a/vendor/cube-hypervisor/virtiofsd/50-virtiofsd.json b/vendor/cube-hypervisor/virtiofsd/50-virtiofsd.json new file mode 100644 index 0000000..31043b4 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/50-virtiofsd.json @@ -0,0 +1,8 @@ +{ + "description": "virtiofsd vhost-user-fs", + "type": "fs", + "binary": "/usr/libexec/virtiofsd", + "features": [ + "migrate-precopy" + ] +} diff --git a/vendor/cube-hypervisor/virtiofsd/CONTRIBUTING.md b/vendor/cube-hypervisor/virtiofsd/CONTRIBUTING.md new file mode 100644 index 0000000..a7d6945 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/CONTRIBUTING.md @@ -0,0 +1,73 @@ +# Contributing to virtiofsd + +virtiofsd is an open source project licensed under the [Apache v2 License](https://opensource.org/licenses/Apache-2.0) and the [BSD 3 Clause](https://opensource.org/licenses/BSD-3-Clause) license. + +## Coding Style + +We follow the [Rust Style](https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/guide.md) +convention and enforce it through the Continuous Integration (CI) process calling into `rustfmt` +for each submitted Pull Request (PR). + +## Certificate of Origin + +In order to get a clear contribution chain of trust we use the [signed-off-by language](https://01.org/community/signed-process) +used by the Linux kernel project. + +## Patch format + +Beside the signed-off-by footer, we expect each patch to comply with the following format: + +``` +Change summary + +More detailed explanation of your changes: Why and how. +Wrap it to 72 characters. +See http://chris.beams.io/posts/git-commit/ +for some more good pieces of advice. + +Signed-off-by: +``` + +For example: + +``` +Implement support for optional sandboxing + +Implement support for setting up a sandbox for running the +service. The technique for this has been borrowed from virtiofsd, and +consists on switching to new PID, mount and network namespaces, and +then switching root to the directory to be shared. + +Future patches will implement additional hardening features like +dropping capabilities and seccomp filters. + +Signed-off-by: Sergio Lopez +``` + +## Pull requests + +virtiofsd uses the “fork-and-merge” development model. Follow these steps if +you want to merge your changes to `virtiofsd`: + +1. Fork the [virtiofsd](https://gitlab.com/virtio-fs/virtiofsd) project + into your GitLab organization. +2. Within your fork, create a branch for your contribution. +3. [Create a merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) + against the master branch of the virtiofsd repository. +4. Once the merge request is approved, one of the maintainers will merge it. + +## Issue tracking + +If you have a problem, please let us know. We recommend using +[gitlab issues](https://gitlab.com/virtio-fs/virtiofsd/-/issues/new) for formally +reporting and documenting them. + +You can also contact us via email through the [virtio-fs mailing list](https://www.redhat.com/mailman/listinfo/virtio-fs). + +## Closing issues + +You can either close issues manually by adding the fixing commit SHA1 to the issue +comments or by adding the `Fixes` keyword to your commit message. + +After the corresponding MR is merged, GitLab will automatically close that issue when parsing the +[commit message](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically). diff --git a/vendor/cube-hypervisor/virtiofsd/Cargo.toml b/vendor/cube-hypervisor/virtiofsd/Cargo.toml new file mode 100644 index 0000000..8171afa --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/Cargo.toml @@ -0,0 +1,41 @@ +[package] +name = "virtiofsd" +description = "A virtio-fs vhost-user device daemon" +version = "1.11.0" +authors = ["The Virtiofs Project Developers"] +edition = "2018" +homepage = "https://virtio-fs.gitlab.io/" +repository = "https://gitlab.com/virtio-fs/virtiofsd" +license = "Apache-2.0 AND BSD-3-Clause" +readme = "README.md" +autobins = false + +exclude = [".gitlab-ci.yml"] + +[features] +# Enabling Xen support will _disable_ QEMU/KVM support! +xen = ["vhost-user-backend/xen", "vhost/xen", "vm-memory/xen"] + +[dependencies] +anyhow = "1.0.66" +bitflags = "1.2" +capng = "0.2.2" +env_logger = "0.10.2" +futures = { version = "0.3", features = ["thread-pool"] } +libc = "0.2.155" +log = "0.4" +libseccomp-sys = "0.2" +clap = { version = "4", features = ["derive"] } +postcard = { version = "1.0", features = ["use-std"] } +serde = { version = "1.0", features = ["derive"] } +vhost-user-backend = "0.15.0" +vhost = "0.11.0" +virtio-bindings = "0.2.1" +vm-memory = { version = "0.16.1", features = ["backend-mmap", "backend-atomic", "backend-bitmap"] } +virtio-queue = "0.14.0" +vmm-sys-util = "0.12.1" +syslog = "6.1" +rate_limiter = { path = "../rate_limiter" } + +[profile.release] +lto = true diff --git a/vendor/cube-hypervisor/virtiofsd/LICENSE-APACHE b/vendor/cube-hypervisor/virtiofsd/LICENSE-APACHE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/LICENSE-APACHE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/cube-hypervisor/virtiofsd/LICENSE-BSD-3-Clause b/vendor/cube-hypervisor/virtiofsd/LICENSE-BSD-3-Clause new file mode 100644 index 0000000..8bafca3 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/LICENSE-BSD-3-Clause @@ -0,0 +1,27 @@ +// Copyright 2017 The Chromium OS Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/cube-hypervisor/virtiofsd/README.md b/vendor/cube-hypervisor/virtiofsd/README.md new file mode 100644 index 0000000..f652472 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/README.md @@ -0,0 +1,554 @@ +# virtiofsd + +A [virtio-fs](https://virtio-fs.gitlab.io/) vhost-user device daemon +written in Rust. + +## Building from sources + +### Requirements + +This project depends on +[libcap-ng](https://people.redhat.com/sgrubb/libcap-ng/) and +[libseccomp](https://github.com/seccomp/libseccomp). You can obtain +those dependencies by building them for their respective sources, or +by installing the correspondent development packages from your +distribution, if available: + +- Fedora/CentOS/RHEL +```shell +dnf install libcap-ng-devel libseccomp-devel +``` + +- Debian/Ubuntu +```shell +apt install libcap-ng-dev libseccomp-dev +``` + +### Compiling + +virtiofsd is written in Rust, so you will have to install [Rust](https://www.rust-lang.org/learn/get-started) +in order to compile it, and it uses [cargo](https://doc.rust-lang.org/cargo/) to manage the +project and its dependencies. +After installing Rust, you can compile it to a binary by running: + +```shell +cargo build --release +``` + +## CI-built binaries + +Every time new code is merged, the CI pipeline will upload a debug binary +of virtiofsd. It is intended to be an accessible way for anyone to +download and test virtiofsd without needing a Rust toolchain installed. + +The debug binary is built only for x86\_64 Linux-based systems. + +[Click here to download the latest build]( +https://gitlab.com/virtio-fs/virtiofsd/-/jobs/artifacts/main/download?job=publish) + +## Contributing +See [CONTRIBUTING.md](CONTRIBUTING.md) + +## Usage +This program must be run as the root user or as a "fake" root inside a +user namespace (see [Running as non-privileged user](#running-as-non-privileged-user)). + +The program drops privileges where possible during startup, +although it must be able to create and access files with any uid/gid: + +* The ability to invoke syscalls is limited using `seccomp(2)`. +* Linux `capabilities(7)` are dropped. virtiofsd only retains the following capabilities: +`CAP_CHOWN`, `CAP_DAC_OVERRIDE`, `CAP_FOWNER`, `CAP_FSETID`, `CAP_SETGID`, `CAP_SETUID`, +`CAP_MKNOD`, `CAP_SETFCAP` +(and `CAP_DAC_READ_SEARCH` if `--inode-file-handles` is used). + +```shell +virtiofsd [FLAGS] [OPTIONS] --fd |--socket-path --shared-dir +``` +#### Flags +```shell +-h, --help +``` +Prints help information. + +```shell +-V, --version +``` +Prints version information. + +```shell +--syslog +``` +Log to syslog. Default: stderr. + +```shell +--print-capabilities +``` +Print vhost-user.json backend program capabilities and exit. + +```shell +--allow-direct-io +``` +Honor the `O_DIRECT` flag passed down by guest applications. + +```shell +--announce-submounts +``` +Tell the guest which directories are mount points. +If multiple filesystems are mounted in the shared directory, +virtiofsd passes inode IDs directly to the guest, and because such IDs +are unique only on a single filesystem, it is possible that the guest +will encounter duplicates if multiple filesystems are mounted in the +shared directory. +`--announce-submounts` solves that problem because it reports a different +device number for every submount it encounters. + +In addition, when running with `--announce-submounts`, the client sends one +`SYNCFS` request per submount that is to be synced, so virtiofsd +will call `syncfs()` on each submount. +On the other hand, when running without `--announce-submounts`, +the client only sends a `SYNCFS` request for the root mount, +this may lead to data loss/corruption. + +```shell +--no-killpriv-v2 +``` +Disable `KILLPRIV V2` support. +This is required if the shared directory is an NFS file system. +`KILLPRIV V2` support is disabled by default. + +```shell +--killpriv-v2 +``` +Enable `KILLPRIV V2` support. It is disabled by default. + +```shell +--no-readdirplus +``` +Disable support for `READDIRPLUS` operations. + +```shell +--writeback +``` +Enable writeback cache. + +```shell +--xattr +``` +Enable support for extended attributes. + +```shell +--posix-acl +``` +Enable support for posix ACLs (implies --xattr). + +```shell +--security-label +``` +Enable support for security label (SELinux). + +```shell +--preserve-noatime +``` +Always preserve `O_NOATIME`. + +By default virtiofsd will implicitly clean up `O_NOATIME` to prevent potential +permission errors when it does not have the right capabilities to access all +the exported files (typically when running as unprivileged user and with +`--sandbox none`, that means it won't have the `CAP_FOWNER` capability set). + +The option `--preserve-noatime` can be used to override this behavior and +preserve the `O_NOATIME` flag specified by the client. + +#### Options +```shell +--shared-dir +``` +Shared directory path. + +```shell +--tag +``` +The tag that the virtio device advertises. + +Setting this option will enable advertising of VHOST_USER_PROTOCOL_F_CONFIG. +However, the vhost-user frontend of your hypervisor may not negotiate this +feature and (or) ignore this value. Notably, QEMU currently (as of 8.1) ignores +the CONFIG feature. QEMU versions from 7.1 to 8.0 will crash while attempting to +log a warning about not supporting the feature. + +```shell +--socket-group +``` +Name of group for the vhost-user socket. + +```shell +--socket-path +``` +vhost-user socket path. + +```shell +--fd +``` +File descriptor for the listening socket. + +```shell +--log-level +``` +Log level (error, warn, info, debug, trace, off). + +Default: info. + +```shell +--thread-pool-size +``` +Maximum thread pool size. A value of "0" disables the pool. + +Default: 0. + +```shell +--rlimit-nofile +``` +Set maximum number of file descriptors. +If the soft limit is greater than 1M or `--rlimit-nofile=0` is passed +as parameter, the maximum number of file descriptors is not changed. + +Default: min(1000000, `/proc/sys/fs/nr_open`). + +```shell +--modcaps= +``` +Modify the list of capabilities, e.g., `--modcaps=+sys_admin:-chown`. +Although it is not mandatory, it is recommended to always use the `=` sign, +in other case, this will fail `--modcaps -mknod`, because it will be +interpreted as two options, instead of the intended `--modcaps=-mknod`. + +```shell +--sandbox +``` +Sandbox mechanism to isolate the daemon process (namespace, chroot, none). + +- **namespace**: The program switches into a new file system +namespace (`namespaces(7)`) and invokes `pivot_root(2)` to make the shared directory +tree its root. A new mount (`mount_namespaces(7)`), pid (`pid_namespaces(7)`) and +net namespace (`network_namespaces(7)`) is also created to isolate the process. + +- **chroot**: The program invokes `chroot(2)` to make the shared +directory tree its root. This mode is intended for container environments where +the container runtime has already set up the namespaces and the program does +not have permission to create namespaces itself. + +- **none**: Do not isolate the daemon (not recommended). + +Both **namespace** and **chroot** sandbox modes prevent "file system escapes" +due to symlinks and other file system objects that might lead to files outside +the shared directory. + +Default: namespace. + +```shell +--seccomp +``` +Action to take when seccomp finds a not allowed syscall (none, kill, log, trap). + +Default: kill. + +```shell +--cache +``` +The caching policy the file system should use (auto, always, metadata, never). + +Default: auto. + +```shell +--allow-mmap +``` + +For shared directories with `--cache={metadata, never}`, allow files contained in the shared directory to be `mmap`'d. + +```shell +--inode-file-handles= +``` +When to use file handles to reference inodes instead of `O_PATH` file descriptors (never, prefer, mandatory). + +- **never**: Never use file handles, always use `O_PATH` file descriptors. + +- **prefer**: Attempt to generate file handles, but fall back to `O_PATH` file descriptors where the underlying + filesystem does not support file handles or `CAP_DAC_READ_SEARCH` is not available. + Useful when there are various different filesystems under the shared directory and some of them do not support file handles. + +- **mandatory**: Always use file handles. + It will fail if the underlying filesystem does not support file handles or `CAP_DAC_READ_SEARCH` is not available. + +Using file handles reduces the number of file descriptors virtiofsd keeps open, which is not only helpful +with resources, but may also be important in cases where virtiofsd should only have file descriptors open +for files that are open in the guest, e.g. to get around bad interactions with NFS's silly renaming +(see [NFS FAQ, Section D2: "What is a "silly rename"?"](http://nfs.sourceforge.net/)). + +Default: never. + +```shell +--xattrmap +``` +Add custom rules for translating extended attributes between host and guest (e.g., `:map::user.virtiofs.:`). +For additional details please see [Extended attribute mapping](doc/xattr-mapping.md). + +```shell +--uid-map=:namespace_uid:host_uid:count: +``` +When running virtiofsd as non-root, map a range of UIDs from host to namespace. +In order to use this option, the range of subordinate user IDs must have been set up via +`subuid(5)`. virtiofsd uses `newuidmap(1)` for non-trivial cases, that requires a valid subuid, +to do the mapping. If this option is not provided, virtiofsd will set up a 1-to-1 mapping for current uid. + +namespace_uid: Beginning of the range of UIDs inside the user namespace. +host_uid: Beginning of the range of UIDs outside the user namespace. +count: Length of the ranges (both inside and outside the user namespace). + +For instance, let's assume the invoking UID is 1000 and the content of /etc/subuid is: 1000:100000:65536, +which creates 65536 subuids starting at 100000, i.e. the (inclusive) range [100000, 165535], belonging to the actual UID 1000. +This range can be mapped to the UIDs [0, 65535] in virtiofsd’s user namespace (i.e. as seen in the guest) via --uid-map=:0:100000:65536:. +Alternatively, you can simply map your own UID to a single UID in the namespace: +For example, --uid-map=:0:1000:1: would map UID 1000 to root’s UID in the namespace (and thus the guest). + +```shell +--gid-map=:namespace_gid:host_gid:count: +``` +When running virtiofsd as non-root, map a range of GIDs from host to namespace. +In order to use this option, the range of subordinate group IDs must have been set up via +`subgid(5)`. virtiofsd uses `newgidmap(1)` for non-trivial cases, that requires a valid subgid, +to do the mapping. If this option is not provided, virtiofsd will set up a 1-to-1 mapping for current gid. + +namespace_gid: Beginning of the range of GIDs inside the user namespace. +host_gid: Beginning of the range of GIDs outside the user namespace. +count: Length of the ranges (both inside and outside the user namespace). + +For instance, let's assume the invoking GID is 1000 and the content of /etc/subgid is: 1000:100000:65536, +which creates 65536 subgids starting at 100000, i.e. the (inclusive) range [100000, 165535], belonging to the actual GID 1000. +This range can be mapped to the GIDs [0, 65535] in virtiofsd’s user namespace (i.e. as seen in the guest) via --gid-map=:0:100000:65536:. +Alternatively, you can simply map your own GID to a single GID in the namespace: +For example, --gid-map=:0:1000:1: would map GID 1000 to root’s GID in the namespace (and thus the guest). + +```shell +--migration-mode= +``` +Defines how to perform migration, i.e. how to represent the internal state to the destination +instance, and how to obtain that representation. Note that (when using QEMU) **QEMU version 8.2** +or newer is required to use virtio-fs migration. + +virtiofsd internally holds references to all inodes indexed or opened by the guest. During +migration, these references need to be transferred to the destination; how that is done is +determined with this switch: + +- **find-paths**: Iterate through the shared directory (exhaustive search) to find paths for all + inodes held by the source instance (relative to the shared directory), transfer these paths to the + destination, and let the destination instance open these paths. This allows migration without + requiring special privileges, and regardless of whether source and destination use the same shared + directory; but it is expensive in terms of I/O (DFS through the shared directory) and is + vulnerable to third parties changing metadata in the shared directory while migration is ongoing + (e.g. renaming, unlinking, removing permissions), which can potentially lead to data loss and/or + corruption. + +This parameter is ignored on the destination side of migration. + +```shell +--migration-on-error= +``` +Controls how to respond to errors during migration. + +During migration, some inodes that the guest has indexed or opened may turn out not to be +migrateable: Either the source instance cannot construct instructions on how the destination +instance may be able to find/open some inode, or the destination instance finds itself unable to +follow those instructions. In all cases, the destination instance is notified of these inodes, and +then decides what to do depending on the value of this parameter: + +- **abort**: Whenever the destination instance sees any such error, it returns a hard error to the + vhost-user front-end (e.g. QEMU), which aborts migration. Execution is to continue on the source + VM. + +- **guest-error**: Migration is allowed to finish, but all affected inodes are marked as invalid. + The guest will not be able to access any such inode, receiving only errors. + +Note that this parameter is to be used purely for the destination instance; its value is ignored on +the source side of migration. + +```shell +--migration-verify-handles +``` +Ensure that the migration destination opens the very same inodes as the source. This only works if +source and destination are to use the same shared directory on the same filesystem. + +On migration, the source instance informs the destination instance of all inodes the guest has +indexed or opened, and has the destination re-open them. This switch makes the source generate a +file handle for each such inode, and send it to the destination, allowing the destination to +re-generate the same file handle for the inode it has opened and verify that it is equal, proving it +is the same inode. + +(File handles are per-filesystem unique identifiers for inodes that, besides the inode ID, also +include a generation ID to protect against inode ID reuse.) + +Using this option protects against external parties renaming or replacing inodes while migration is +ongoing, which, without this option, can lead to data loss or corruption, so it should always be +used when other processes besides virtiofsd have write access to the shared directory. However, +again, it only works if both source and destination use the same shared directory; though in the +case of network filesystems, this does not require them to run on the same host. + +This parameter is ignored on the destination side of migration. + +```shell +--migration-confirm-paths +``` +Double-check the identity of inodes right before switching over to the destination, potentially +making migration more resilient when third parties have write access to the shared directory. + +When representing migrated inodes by their paths relative to the shared directory, double-check +during switch-over to the destination that each path still matches the respective inode. If a path +does not match, try to correct by consulting the respective symbolic link in */proc/self/fd*. + +Note that this option requires accessing each inode indexed or opened by the guest once during the +switch-over stage of migration, when both the source and destination VM are paused, so can prolong +that phase for an indeterminate amount of time. + +This parameter is ignored on the destination side of migration. + +### Examples +Export `/mnt` on vhost-user UNIX domain socket `/tmp/vfsd.sock`: + +```shell +host# virtiofsd --socket-path=/tmp/vfsd.sock --shared-dir /mnt \ + --announce-submounts --inode-file-handles=mandatory & + +host# qemu-system \ + -blockdev file,node-name=hdd,filename= \ + -device virtio-blk,drive=hdd \ + -chardev socket,id=char0,path=/tmp/vfsd.sock \ + -device vhost-user-fs-pci,queue-size=1024,chardev=char0,tag=myfs \ + -object memory-backend-memfd,id=mem,size=4G,share=on \ + -numa node,memdev=mem \ + -accel kvm -m 4G + +guest# mount -t virtiofs myfs /mnt +``` + +See [FAQ](#faq) for adding virtiofs config to an existing qemu command-line. + +### Running as non-privileged user +When run without root, virtiofsd requires a user namespace (see `user_namespaces(7)`) +to be able to switch between arbitrary user/group IDs within the guest. +virtiofsd will fail in a user namespace where UIDs/GIDs have not been mapped +(i.e., `uid_map` and `gid_map` files have not been written). +There are many options to run virtiofsd inside a user namespace. +For instance: + +Let's assume the invoking UID and GID is 1000 and the content of both `/etc/subuid` +and `/etc/subgid` are: +``` +1000:100000:65536 +``` + +Using `podman-unshare(1)` the user namespace will be configured so that the invoking user's UID +and primary GID (i.e., 1000) appear to be UID 0 and GID 0, respectively. +Any ranges which match that user and group in `/etc/subuid` and `/etc/subgid` are also +mapped in as themselves with the help of the `newuidmap(1)` and `newgidmap(1)` helpers: + +```shell +host$ podman unshare -- virtiofsd --socket-path=/tmp/vfsd.sock --shared-dir /mnt \ + --announce-submounts --sandbox chroot & +``` + +Using `lxc-usernsexec(1)`, we could leave the invoking user outside the mapping, having +the root user inside the user namespace mapped to the user and group 100000: + +```shell +host$ lxc-usernsexec -m b:0:100000:65536 -- virtiofsd --socket-path=/tmp/vfsd.sock \ + --shared-dir /mnt --announce-submounts --sandbox chroot & +``` + +In order to have the same behavior as `podman-unshare(1)`, we need to run + +```shell +host$ lxc-usernsexec -m b:0:1000:1 -m b:1:100000:65536 -- virtiofsd --socket-path=/tmp/vfsd.sock \ + --shared-dir /mnt --announce-submounts --sandbox chroot & +``` + +We could also select `--sandbox none` instead of `--sandbox chroot`. + +#### Limitations +- Within the guest, it is not possible to create block or char device nodes in the shared directory. + +- virtiofsd can't use file handles (`--inode-file-handles` requires `CAP_DAC_READ_SEARCH`), + so a large number of file descriptors is required. + Additionally, on NFS, not using file handles may result in a hidden file lingering after some file is deleted + (see [NFS FAQ, Section D2: "What is a "silly rename"?"](http://nfs.sourceforge.net/)). + +- virtiofsd will not be able to increase `RLIMIT_NOFILE`. + +## FAQ +- How to read-only-share a directory that cannot be modified within the guest? +To accomplish this you need to export a read-only mount point, for instance, +exporting `share`: + +```shell +mkdir ro-share +mount -o bind,ro share ro-share +virtiofsd --shared-dir ro-share ... +``` + +- How to share multiple directories with the same virtiofsd? +Currently, virtiofsd only supports sharing a single directory, +but it is possible to use submounts to achieve this, for instance, +exporting `share0`, `share1`: + +```shell +mkdir -p share/{sh0,sh1} +mount -o bind share0 share/sh0 +mount -o bind share1 share/sh1 +virtiofsd --announce-submounts --shared-dir share ... +``` +Note the use of `--announce-submounts` to prevent data loss/corruption. + +- How to add virtiofs devices to an existing qemu command-line: + + If `-object memory-backend-memfd,id=mem` and either `-numa node,memdev=mem` + or a `memory-backend=mem` property in the `-machine` option + have not already been added to the command, add them. + + If a different memory backend is already configured then it should be changed + to `memory-backend-memfd`. + + `-object memory-backend-memfd` **must** have the option `share=on` + and `size=` **must** match the memory size defined by `-m`. + + For each virtiofs device mount add a + `-chardev socket,id=${MATCHING_ID},path=${VIRTIOFSD_SOCKET_PATH}` and + `-device vhost-user-fs-pci,queue-size=1024,chardev=${MATCHING_ID},tag=${VIRTIOFS_TAG}` + substituting appropriate values for the shell-style variables. + +## SELinux Support +One can enable support for SELinux by running virtiofsd with option +"--security-label". But this will try to save guest's security context +in xattr security.selinux on host and it might fail if host's SELinux +policy does not permit virtiofsd to do this operation. + +Hence, it is recommended to remap guest's "security.selinux" xattr to say +"trusted.virtiofs.security.selinux" on host. Add following option to +command line. + +"--xattrmap=:map:security.selinux:trusted.virtiofs.:" + +This will make sure that guest and host's SELinux xattrs on same file +remain separate and not interfere with each other. And will allow both +host and guest to implement their own separate SELinux policies. + +Setting trusted xattr on host requires CAP_SYS_ADMIN. So one will need +add this capability to daemon. Add following option to command line. + +"--modcaps=+sys_admin" + +trusted xattrs are not namespaced. So virtiofsd needs to have CAP_SYS_ADMIN +in init_user_ns. IOW, one should not be using user namespaces and virtiofsd +should run with CAP_SYS_ADMIN. + +Giving CAP_SYS_ADMIN increases the risk on system. Now virtiofsd is more +powerful and if gets compromised, it can do lot of damage to host system. +So keep this trade-off in my mind while making a decision. diff --git a/vendor/cube-hypervisor/virtiofsd/doc/xattr-mapping.md b/vendor/cube-hypervisor/virtiofsd/doc/xattr-mapping.md new file mode 100644 index 0000000..12e72b3 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/doc/xattr-mapping.md @@ -0,0 +1,193 @@ +# Extended attribute (xattr) mapping + +By default, the name of xattrs used by the client are passed through to the server +file system. This can be a problem where either those xattr names are used +by something on the server (e.g. selinux client/server confusion) or if the +virtiofsd is running in a container with restricted privileges where it cannot +access some attributes. + +## Mapping syntax + +A mapping of xattr names can be made using `--xattrmap=` where the `` +string consists of a series of rules. + +When looking for a mapping, the first matching rule applies. +There *must* be a mapping for every xattr name in the list of rules, +for example by making the final rule a catch-all rule to match any remaining attributes. + +Each rule consists of a number of fields separated with a separator that is the +first non-white space character in the rule. This separator must then be used +for the whole rule. +White space may be added before and after each rule. + +Using `:` as the separator a rule is of the form: + +``` +:type:scope:key:prepend: +``` + +**scope** is one of: + +- `client`: Match **key** against an xattr name from the client for + setxattr/getxattr/removexattr +- `server`: Match **prepend** against an xattr name from the server + for listxattr +- `all`: Can be used to make a single rule where both the server + and client matches are triggered. + +**type** is one of: + +- `prefix`: Is designed to prepend and strip a prefix; the modified + attributes then being passed on to the client/server. + +- `ok`: Causes the rule set to be terminated when a match is found + while allowing matching xattrs through unchanged. + It is intended both as a way of explicitly terminating + the list of rules, and to allow some xattrs to skip following rules. + +- `bad`: If a client tries to use a name matching **key** it's + denied using `EPERM`; when the server passes an attribute + name matching **prepend** it's hidden. In many ways its use is very like + the `ok` type as either an explicit terminator or for special handling of certain + patterns. + +- `unsupported`: If a client tries to use a name matching **key** it's + denied using `ENOTSUP`; when the server passes an attribute + name matching **prepend** it's hidden. In many ways its use is very like + the `ok` type as either an explicit terminator or for special handling of certain + patterns. + +**key** is a string tested as a prefix on an attribute name originating +on the client. It may be empty in which case a `client` scoped rule +will always match on client names. + +**prepend** is a string tested as a prefix on an attribute name originating +on the server, and used as a new prefix. It may be empty +in which case a `server` scoped rule will always match on all names from +the server. + +e.g.: + +| Mapping rule | Description | +| ----------------------------------------- | ---------------------------------------------------------------------------------------------------- | +| `:prefix:client:trusted.:user.virtiofs.:` | will match `trusted.*` attributes in client calls and prefix them before passing them to the server. | +| `:prefix:server::user.virtiofs.:` | will strip `user.virtiofs.` from all server replies. | +| `:prefix:all:trusted.:user.virtiofs.:` | combines the previous two cases into a single rule. | +| `:ok:client:user.::` | will allow get/set xattr for `user.` xattrs. | +| `:ok:server::security.:` | will pass `security.` xattrs in listxattr from the server. | +| `:ok:all:::` | will terminate the rule search passing any remaining attributes in both directions. | +| `:bad:server::security.:` | would hide `security.` xattrs in listxattr from the server. | + +A simpler **map** type provides a shorter syntax for the common case: + +``` +:map:key:prepend: +``` + +The `map` type adds a number of separate rules to add **prepend** as a prefix +to the matched **key** (or all attributes if **key** is empty). +There may be at most one `map` rule, and it must be the last rule in the set. + +Please note that when the `security.capability` xattr is remapped, the daemon has to do +extra work to remove it during many operations, which the host kernel normally +does itself. + +## Security considerations + +Operating systems typically partition the xattr namespace using +well-defined name prefixes. Each partition may have different +access controls applied. For example, on Linux there are multiple +partitions + +- `system.*`: access varies depending on attribute and filesystem +- `security.*`: only processes with `CAP_SYS_ADMIN` +- `trusted.*`: only processes with `CAP_SYS_ADMIN` +- `user.*`: any process granted by file permissions / ownership + +While other OS such as FreeBSD have different name prefixes +and access control rules. + +When remapping attributes on the host, it is important to +ensure that the remapping does not allow a guest user to +evade the guest access control rules. + +Consider if `trusted.*` from the guest was remapped to +`user.virtiofs.trusted.*` in the host. An unprivileged +user in a Linux guest has the ability to write to xattrs +under `user.*`. Thus the user can evade the access +control restriction on `trusted.*` by instead writing +to `user.virtiofs.trusted.*`. + +As noted above, the partitions used and access controls +applied, will vary across guest OS, so it is not wise to +try to predict what the guest OS will use. + +The simplest way to avoid an insecure configuration is +to remap all xattrs at once, to a given fixed prefix. +This is shown in example (1) below. + +If selectively mapping only a subset of xattr prefixes, +then rules must be added to explicitly block direct +access to the target of the remapping. This is shown +in example (2) below. + +## Mapping examples + +1. Prefix all attributes with `user.virtiofs.` + +```shell +--xattrmap=":prefix:all::user.virtiofs.::bad:all:::" +``` + +This uses two rules, using : as the field separator; +the first rule prefixes and strips `user.virtiofs.`, +the second rule hides any non-prefixed attributes that +the host set. + +This is equivalent to the `map` rule: + +```shell +--xattrmap=":map::user.virtiofs.:" +``` + +2. Prefix `trusted.` attributes, allow others through + +```shell +--xattrmap="/prefix/all/trusted./user.virtiofs./ + /bad/server//trusted./ + /bad/client/user.virtiofs.// + /ok/all///" +``` +(each rule is on a single line just for the sake of clarity) + +Here there are four rules, using `/` as the field +separator, and also demonstrating that new lines can +be included between rules. +The first rule is the prefixing of `trusted.` and +stripping of `user.virtiofs.`. +The second rule hides unprefixed `trusted.` attributes +on the host. +The third rule stops a guest from explicitly setting +the `user.virtiofs.` path directly to prevent access +control bypass on the target of the earlier prefix +remapping. +Finally, the fourth rule lets all remaining attributes +through. + +This is equivalent to the `map` rule: + +```shell +--xattrmap="/map/trusted./user.virtiofs./" +``` + +3. Hide `security.` attributes, and allow everything else + +```shell +--xattrmap="/bad/all/security./security./ + /ok/all///" +``` + +The first rule combines what could be separate client and server +rules into a single `all` rule, matching `security.` in either +client arguments or lists returned from the host. This prevents +the client from seeing and/or setting any `security.` attributes on the server. \ No newline at end of file diff --git a/vendor/cube-hypervisor/virtiofsd/docs/preserialization-storeonly-design.md b/vendor/cube-hypervisor/virtiofsd/docs/preserialization-storeonly-design.md new file mode 100644 index 0000000..cce35b0 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/docs/preserialization-storeonly-design.md @@ -0,0 +1,388 @@ +# Virtiofsd Migration Preserialization Speed-up: `StoreOnlyReconstructor` Design + +> Author: Songqian Li <sionli@tencent.com> +> +> Module: `hypervisor/virtiofsd/src/passthrough/device_state/preserialization.rs` + +--- + +## 1. Background + +During live migration, virtiofsd must write an `InodeMigrationInfo` **for every inode in the inode store** on the source side: either `RootNode`, or `Path { parent, filename, fullname }`. The destination relies on this information to `openat(2)` the corresponding file in the same shared directory by path. + +```rust +pub(in crate::passthrough) enum InodeLocation { + RootNode, + Path { parent: StrongInodeReference, filename: String, fullname: String }, +} +``` + +`PathReconstructor` recurses top-down through the **entire shared directory tree** (or, when the filter list is non-empty, down from each filter directory), giving a complexity of **O(shared tree / filter subtree size)**. When the shared directory holds tens of millions of files but the guest has only ever touched a few hundred, the preserialization phase blows the migration SLA. + +**Goal**: bring the cost down from `O(on-disk tree)` to `O(inode store size)` while keeping the wire format and the destination-observable semantics identical to `PathReconstructor`. + +--- + +## 2. Core Idea + +Instead of digging down through the on-disk tree level by level, **iterate only the inode store itself**: + +1. Every inode already holds a `FileOrHandle`, so `readlink("/proc/self/fd/")` yields its **absolute path** (for `FileOrHandle::File` this is a pure procfs read; for `Handle` it first materializes a temporary FD via `open_by_handle_at(2)`, which still stays bounded by inode-store size rather than on-disk tree size). +2. Sorting the `(inode_id, abs_path)` set **by path byte length, ascending** guarantees that every parent is processed strictly before its children. +3. When processing a child, strip the last component off `abs_path` to get `parent_path`, and reverse-look up the parent inode_id in an **incrementally built** `HashMap, Inode>`. + +``` + ┌──────────────────┐ per inode ┌────────────────────┐ + │ inode store │ ───────────────▶ │ (id, abs_path) │ + │ (all inodes) │ readlink │ list │ + └──────────────────┘ /proc/self/fd └────────┬───────────┘ + │ sort by path.len() + ▼ + ┌────────────────────┐ + │ sorted list │ + └────────┬───────────┘ + │ stream through + + │ hash reverse lookup + ▼ + ┌────────────────────┐ + │ migration_info:: │ + │ Path │ + └────────────────────┘ +``` + +Complexity: **O(inode store size) + O(total depth of missing parent chains)**. + +--- + +## 3. Selecting a Reconstructor + +`StoreOnlyReconstructor` is the default; `PathReconstructor` is only used when the DFS switch is explicitly turned on: + +```rust +// hypervisor/virtiofsd/src/passthrough/device_state/mod.rs +let result = if self.cfg.migration_dfs_preserialization { + PathReconstructor::new(self, cancel).execute() +} else { + StoreOnlyReconstructor::new(self, cancel).execute() +}; +``` + +Configuration (ignored on the destination side): + +- CLI: `--migration-dfs-preserialization`, with the compatibility aliases `--migration-legacy-reconstructor` and `--migration-no-store-only` +- `Config.migration_dfs_preserialization`, default `false` + +One structural difference between the two: when the filter list is non-empty, `PathReconstructor` calls `recurse_from` on each filter **directory** and walks its whole subtree. `StoreOnlyReconstructor` does **not** perform that DFS — it only writes `Path { parent: root, filename, fullname }` for the filter inodes themselves, and lets descendants be filled in by the inode-store snapshot plus reverse lookup. + +--- + +## 4. `execute()` Pipeline + +``` + ┌─────────────────────────────────────────────────────────┐ + │ Phase 0 build_filter_resolved_map │ + │ filter_inode -> resolved_path map │ + ├─────────────────────────────────────────────────────────┤ + │ Phase 1 set_root_info root -> RootNode │ + ├─────────────────────────────────────────────────────────┤ + │ Phase 2 set_filter_infos filter -> Path(parent=root)│ + ├─────────────────────────────────────────────────────────┤ + │ Phase 3 inodes.map { get_path() } single-pass snapshot│ + ├─────────────────────────────────────────────────────────┤ + │ Phase 4 entries.sort_by(path.len()) │ + ├─────────────────────────────────────────────────────────┤ + │ Phase 5 seed path_to_inode (root + filter) │ + ├─────────────────────────────────────────────────────────┤ + │ Phase 6 main loop: stream reverse-lookup of parents │ + │ + ensure_ancestor_chain fills missing links │ + ├─────────────────────────────────────────────────────────┤ + │ Phase 7 drop(synthesized_refs) release holding pen │ + └─────────────────────────────────────────────────────────┘ +``` + +--- + +## 5. Mechanisms in Execution Order + +### 5.0 Two Data Structures That Span the Whole Flow + +**(1) `path_to_inode: HashMap, Inode>`** — the incrementally built "path to inode" reverse lookup table. Keys are `Vec` rather than `String` so that non-UTF-8 paths are not mangled. Seeded in phase 5; phase 6 both consumes it and inserts newly discovered children into it as it goes. + +**(2) `synthesized_refs: Vec`** — a `Vec` declared in the body of `execute()` whose lifetime **spans the entire main loop**, used to **keep alive the intermediate directory inodes that phase 6 materializes on demand**. + +The crux: every time `ensure_ancestor_chain` materializes a new intermediate directory it gets back a `StrongInodeReference` with `refcount = 1`, and **that reference must survive until the main loop reaches `inodes.get_strong(parent_id)` for that inode** — otherwise the intermediate directory is evicted from the store before any descendant can reference it. `synthesized_refs` is the container that holds those references. + +The helper `normalize_path_bytes` strips redundant trailing `/` from a path (except for a bare `/`). The parent slice computed in the phase 6 main loop, `abs_path[..rposition('/')]`, never carries a trailing slash, whereas seed sources (a filter `read_link` target, `get_path()` output) legitimately may. Without normalization the two sides never match, which **silently breaks the parent chain**. + +--- + +### 5.1 Phase 0: Build the Migration-Local Filter Resolution Map + +The filter table is typed as: + +```rust +filter: RwLock> +// filename, original_path / fullname +``` + +**What it does**: build a `HashMap>` mapping each filter inode to the absolute host path bytes usable for seeding `path_to_inode`. + +**Why it is needed**: in whitelist / host-dir setups, a filter entry's `original_path` is frequently an absolute path **outside the shared directory** (an NFS mount point, or a symlink pointing at a remote directory). Preserialization must obtain that host path (resolving the symlink target where applicable), otherwise descendants' parent slices land on the guest-visible path and fail to reverse-resolve. + +**Implementation** (`build_filter_resolved_map`): + +```rust +for (id, (_filename, original_path)) in filter_map.iter() { + match std::fs::read_link(original_path.as_str()) { + Ok(path_buf) => out.insert(*id, path_buf.into_os_string().into_vec()), + Err(_) => out.insert(*id, original_path.as_bytes().to_vec()), + } +} +``` + +- Filter entry is a symlink: `read_link` succeeds, and the target's raw OS bytes are used (no `to_string_lossy`, so non-UTF-8 targets are preserved). +- Not a symlink, broken link, or missing target: fall back to the `original_path` bytes, matching the "keep the original path on resolution failure" semantics. +- **Never written back into `fs.filter`**: the map lives only for the duration of this `execute()` call, so a cancelled or failed migration leaves no runtime residue. + +--- + +### 5.2 Phases 1 & 2: Mark the Root and Filter Inodes + +- Phase 1: `set_root_info` writes `InodeLocation::RootNode` on the root inode. +- Phase 2: `set_filter_infos` writes `InodeLocation::Path { parent: root, filename, fullname }` for every filter inode that can still be resolved via `get_strong`, where `fullname` preserves the filter entry's full guest-visible name. + +Both are wire-format obligations and must complete before phase 5, since they provide the base anchors for seeding. + +If `get_strong(*id)` fails, that filter is skipped and never enters `filter_inodes` — which is precisely why filter inodes must be pinned against guest FORGET; see section 6. + +--- + +### 5.3 Phase 3: Single-Pass Snapshot of the Inode Store + +Snapshot the absolute paths of **all inodes** in the store into a `Vec<(Inode, Vec)>` in one go: + +```rust +let snapshots = self.fs.inodes.map(|inode_data| { + if inode == ROOT || filter_inode_set.contains(inode) || FileOrHandle::Invalid { + return Ok((inode, Vec::new())); // empty paths are dropped afterwards + } + match inode_data.get_path(proc_self_fd) { + Ok(path) => Ok((inode_data.inode, path.into_bytes())), + Err(err) => { /* empty path -> marked Invalid at serialization */ } + } +}); +``` + +**Key design points**: + +1. **`inodes.map` takes the snapshot under a single read lock**, guaranteeing consistency: no inode is added or removed midway. +2. **Inodes that already have `migration_info` still participate in the snapshot** (their migration_info is not overwritten, but they must be seeded into `path_to_inode`). Otherwise their in-store descendants cannot find a parent in phase 6. +3. Entries unlinked on the host, for which procfs reports `"Inode deleted"`, are logged at `warn` level (so operators can correlate post-migration "missing inode" reports with on-host unlink activity); other `get_path` failures are logged at `debug`. Both become empty paths and are ultimately serialized as `Invalid`. +4. Cancellation is the only hard error this phase propagates upwards. + +--- + +### 5.4 Phase 4: Sort by Path Length + +`entries.sort_by(|a, b| a.1.len().cmp(&b.1.len()))`. + +**Proof of the parent-before-child invariant**: + +- Under POSIX, `child = parent + '/' + name` with a non-empty `name`; +- therefore `len(child) >= len(parent) + 2`; +- paths of equal length cannot be in an ancestor/descendant relationship, so ties may be ordered arbitrarily. + +A single sort turns "parents before children" into a deterministic order with no topological sort required. Should mount-namespace aliasing surfacing through `/proc/self/fd` ever break this invariant, the affected descendants simply fall through to the "parent not found" branch and degrade into on-demand synthesis or a skip. + +--- + +### 5.5 Phase 5: Seed `path_to_inode` + +Load the root and all filter inodes into `path_to_inode` via **two overlaid seeding channels**: + +1. The `filter_resolved_map` from phase 0 (the `read_link` result, or the `original_path` fallback); +2. **Additionally**, each filter inode's own `/proc/self/fd` path, as a fallback that covers cases where the "logical" path and the kernel's path disagree, e.g. bind mounts. This second channel uses `entry().or_insert`, so it never overwrites a key already written by the first. + +Every key is normalized through `normalize_path_bytes` first. + +--- + +### 5.6 Phase 6: Main Loop — Streaming Reverse Lookup With On-Demand Parent Synthesis + +The heart of the algorithm. + +#### 5.6.1 Main Loop Skeleton + +```rust +for (inode_id, abs_path) in entries.iter() { + let (parent_bytes, filename_bytes) = split_at_last_slash(abs_path); + let parent_id = match path_to_inode.get(parent_bytes) { + Some(id) => *id, + None => { + match ensure_ancestor_chain(parent_bytes, &mut path_to_inode, &mut synthesized_refs)? { + true => *path_to_inode.get(parent_bytes).unwrap(), + false => { path_to_inode.insert(abs_path.clone(), *inode_id); continue; } + } + } + }; + let parent_ref = self.fs.inodes.get_strong(parent_id)?; + // if migration_info already exists, do not overwrite it; just seed our own path + // otherwise write Path { parent, filename, fullname: "" } + path_to_inode.insert(abs_path.clone(), *inode_id); +} +``` + +Three things per iteration: **split the path**, **reverse-look up or synthesize the parent**, then **write migration_info and insert the current inode into the table** (it may itself become some descendant's parent later). + +Error handling matches `PathReconstructor`'s fail-hard semantics: + +- Non-UTF-8 filename: hard error under `migration_verify_handles`, otherwise `warn` + skip (serialized as `Invalid`). +- Inside `ensure_ancestor_chain`, a failed `openat` or a component containing NUL or invalid UTF-8: likewise a hard error under verify_handles, otherwise degraded to a skip. +- A non-directory encountered mid-path: abort that chain; the caller then marks the child as Invalid. + +#### 5.6.2 Why `ensure_ancestor_chain` Is Needed + +The guest may have forgotten a parent directory while still holding an fd on a file underneath it. In that case: + +- the child inode is still in the store and `readlink` returns its full absolute path; +- the parent inode has been evicted from the store and is absent from `path_to_inode`; +- `PathReconstructor` would create the intermediate directory inode on the spot during its DFS; +- simply skipping would serialize the child as `Invalid`, so **the destination would permanently lose that inode** — a functional regression. + +#### 5.6.3 The `ensure_ancestor_chain` Algorithm + +Strategy: **walk upwards to locate the nearest anchor, then walk downwards materializing each level with `openat(O_PATH | O_NOFOLLOW)`**. + +``` + Input: parent_bytes = "/share/a/b/c" (not in path_to_inode) + nearest known anchor in path_to_inode = "/share/a" + + ┌─ Step A: walk upwards to locate the nearest anchor ────┐ + │ try "/share/a/b/c" -> miss │ + │ try "/share/a/b" -> miss │ + │ try "/share/a" -> HIT (anchor) │ + │ collected missing = ["b", "c"] │ + └────────────────────────────────────────────────────────┘ + │ + ▼ + ┌─ Step B: walk downwards, materializing each component ─┐ + │ anchor_fd = open("/share/a", O_PATH|O_NOFOLLOW) │ + │ │ + │ for component in ["b", "c"]: │ + │ fd = openat(anchor_fd, component, O_PATH) │ + │ statx(fd) must be a directory │ + │ handle = name_to_handle_at(fd) │ + │ ┌─ claim_inode(handle, ids) ─┐ │ + │ │ hit -> reuse InodeData │ │ + │ │ miss -> get_or_insert( │ │ + │ │ full InodeData) │ │ + │ └─────────────┬──────────────┘ │ + │ ▼ │ + │ write migration_info { parent = previous level } │ + │ holding_pen.push(strong_ref) <- keeps it alive │ + │ path_to_inode.insert(next_path -> new_id) │ + │ anchor_fd = fd │ + └────────────────────────────────────────────────────────┘ + │ + ▼ + back in main loop: parent_bytes is now in path_to_inode +``` + +**Key design points**: + +1. **`claim_inode` first**: if the same physical inode is already in the store via another path, reuse it; never create a duplicate `InodeData`, or the destination would see two inodes referencing the same on-disk object. +2. **Complete `InodeData`**: a synthesized node carries `file_or_handle` + `ids` + `mode` + `migration_info`, so its wire format is identical to an intermediate node created on the fly by `PathReconstructor`. +3. **Idempotent**: if a path has already been populated by another chain or by the main loop, rebase onto the existing inode and continue downwards. +4. **Cost O(missing_chain_depth)**, far below the DFS cost of O(subtree size). + +#### 5.6.4 Why the Caller Must Own `synthesized_refs` + +This is the least obvious lifetime constraint in the design, and it follows from the inode store's refcount semantics. + +**Fact 1**: the `StrongInodeReference` returned by `claim_inode` / `get_or_insert` is the **only live strong reference** to that inode in the store — freshly materialized it has `refcount = 1`, so dropping it takes the refcount to zero and the inode is evicted immediately. + +**Fact 2**: for the main loop to use that synthesized inode as some child's parent, it must call `inodes.get_strong(parent_id)` to obtain **its own** strong reference and store it in the child's `migration_info.location.parent`. That step is what actually makes the intermediate directory referenced by the serialization output. + +**Counter-example (what happens without a holding pen)**: + +``` +inside ensure_ancestor_chain, for each component { + let new_ref = claim_or_insert(...)?; // refcount: 0 -> 1 + path_to_inode.insert(next_path, new_id); + // function returns, new_ref goes out of scope +} // <- drop(new_ref): refcount 1 -> 0 + // inode is evicted from the store +back in the main loop: +let parent_ref = inodes.get_strong(parent_id)?; // NotFound + // child skipped -> serialized as Invalid +``` + +**The fix**: park `new_ref` in the caller-provided `holding_pen: &mut Vec`: + +```rust +holding_pen.push(new_ref); // refcount stays at 1, but the lifetime moves to the caller +``` + +`synthesized_refs` is declared in the body of `execute()` with a scope that **strictly covers the entire main loop**, yielding this invariant: + +| Point in time | Sources of the synthesized inode's refcount | Main loop `get_strong(parent_id)` | +|---|---|---| +| When `ensure_ancestor_chain` returns | `synthesized_refs` × 1 | — | +| While the main loop visits its descendants | `synthesized_refs` × 1 + `migration_info.parent` × k from descendants created so far | **always succeeds** | +| After the main loop, once phase 7 has dropped the pen | only `migration_info.parent` × k (survives if it has descendants; evicted at zero otherwise) | — | + +**In one sentence**: `synthesized_refs` is a **temporary custody area** that holds synthesized inodes which have just been born but are not yet held by any serialization object, keeping them alive until the main loop finishes — so the main loop can always resolve them as parents, while phase 7 can flush in one go those that never gained a descendant. + +--- + +### 5.7 Phase 7: `drop(synthesized_refs)` + +Release the holding pen explicitly. **The timing is the other half of the design**: dropping too early makes `get_strong(parent_id)` fail, dropping too late wrongly serializes intermediate directories that have no descendants. + +``` +during the main loop synthesized_refs holds N strong refs to synthesized inodes + │ + ▼ +main loop ends each synthesized inode with descendants: + refcount = 1 (synthesized_refs) + + k (migration_info.parent of k descendants) + each synthesized inode without descendants: + refcount = 1 (synthesized_refs) only + │ + ▼ +drop(synthesized_refs) + │ + ▼ + each synthesized inode's refcount drops by 1: + has descendants -> still >= 1, stays in the store, gets serialized + no descendants -> reaches 0, evicted, excluded from the output +``` + +This way, at serialization time `StoreOnlyReconstructor` only sees synthesized intermediate directories that were **genuinely referenced by some descendant through `migration_info.location.parent`** — semantically equivalent to `PathReconstructor`, where an intermediate node not held by anything simply disappears as the DFS unwinds. + +--- + +## 6. Filter Inodes Must Be Pinned Against Guest FORGET + +Phases 2 and 5 of `StoreOnlyReconstructor` rely on one invariant: every inode id in `self.filter` is **also** still alive in `inode_store`. + +If a guest `FORGET` (including a bulk forget triggered by `drop_caches`) evicts a whitelisted directory or symlink from the store while `self.filter` retains the old id: + +1. the next lookup allocates a fresh inode number, so the filter table ends up with two ids pointing at the same on-disk object; +2. the phase 5 `path_to_inode` seed is overwritten by the stale id; +3. `get_strong` fails for descendants, the source marks them `Invalid`, the destination reports `Migration source has lost inode N`, and every open handle on those children is rejected. + +`PassthroughFs::forget` / `batch_forget` therefore **skip keys present in `self.filter` instead of forwarding them to `inodes.forget_one` / `forget_many`**. The whitelist is bounded (a few dozen entries at most) and each pin costs only an `O_PATH` fd plus one `InodeData`; guest semantics are unaffected because refcount bookkeeping is host-private anyway. + +--- + +## 7. Summary + +`StoreOnlyReconstructor` follows a single line of reasoning — **iterate only the inode store, let procfs report each path, sort and stream the reverse lookup, synthesize missing parents on demand** — to bring preserialization down from `O(shared directory tree)` to `O(inode store)`. + +The details most worth remembering: + +- **Strictly monotonic path length** is the foundation of the parent-before-child invariant. +- A **caller-owned holding pen** resolves the non-obvious lifetime constraint that strong references to on-demand synthesized inodes must survive across the main loop. +- **Phase 0 resolves filters locally for the migration only**, never writing back into the runtime filter table, so cancellation or failure leaves no residue. +- **Filter inodes must be pinned against FORGET**, or the `path_to_inode` seed gets poisoned by stale ids. diff --git a/vendor/cube-hypervisor/virtiofsd/rustfmt.toml b/vendor/cube-hypervisor/virtiofsd/rustfmt.toml new file mode 100644 index 0000000..eb2fbdf --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/rustfmt.toml @@ -0,0 +1,2 @@ +imports_granularity = "Module" +edition = "2018" diff --git a/vendor/cube-hypervisor/virtiofsd/src/descriptor_utils.rs b/vendor/cube-hypervisor/virtiofsd/src/descriptor_utils.rs new file mode 100644 index 0000000..ada67bb --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/descriptor_utils.rs @@ -0,0 +1,969 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use std::collections::VecDeque; +use std::fmt::{self, Display}; +use std::io::{self, Read, Write}; +use std::mem::{size_of, MaybeUninit}; +use std::ops::Deref; +use std::ptr::copy_nonoverlapping; +use std::{cmp, result}; + +use virtio_queue::DescriptorChain; +use vm_memory::bitmap::Bitmap; +use vm_memory::{ + Address, ByteValued, GuestMemory, GuestMemoryError, GuestMemoryRegion, Le16, Le32, Le64, + VolatileMemory, VolatileMemoryError, VolatileSlice, +}; + +use crate::file_traits::FileReadWriteAtVolatile; +use crate::{oslib, GuestMemoryMmap}; +use vm_memory::bitmap::{AtomicBitmap, RefSlice}; + +#[derive(Debug)] +pub enum Error { + DescriptorChainOverflow, + FindMemoryRegion, + GuestMemoryError(GuestMemoryError), + InvalidChain, + IoError(io::Error), + SplitOutOfBounds(usize), + VolatileMemoryError(VolatileMemoryError), +} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + + match self { + DescriptorChainOverflow => write!( + f, + "the combined length of all the buffers in a `DescriptorChain` would overflow" + ), + FindMemoryRegion => write!(f, "no memory region for this address range"), + GuestMemoryError(e) => write!(f, "descriptor guest memory error: {e}"), + InvalidChain => write!(f, "invalid descriptor chain"), + IoError(e) => write!(f, "descriptor I/O error: {e}"), + SplitOutOfBounds(off) => write!(f, "`DescriptorChain` split is out of bounds: {off}"), + VolatileMemoryError(e) => write!(f, "volatile memory error: {e}"), + } + } +} + +pub type Result = result::Result; + +impl std::error::Error for Error {} + +#[derive(Clone)] +struct DescriptorChainConsumer<'a> { + buffers: VecDeque>>, + bytes_consumed: usize, +} + +impl<'a> DescriptorChainConsumer<'a> { + fn available_bytes(&self) -> usize { + // This is guaranteed not to overflow because the total length of the chain + // is checked during all creations of `DescriptorChainConsumer` (see + // `Reader::new()` and `Writer::new()`). + self.buffers + .iter() + .fold(0usize, |count, vs| count + vs.len()) + } + + fn bytes_consumed(&self) -> usize { + self.bytes_consumed + } + + /// Consumes at most `count` bytes from the `DescriptorChain`. Callers must provide a function + /// that takes a `&[VolatileSlice]` and returns the total number of bytes consumed. This + /// function guarantees that the combined length of all the slices in the `&[VolatileSlice]` is + /// less than or equal to `count`. + /// + /// # Errors + /// + /// If the provided function returns any error then no bytes are consumed from the buffer and + /// the error is returned to the caller. + fn consume(&mut self, count: usize, f: F) -> io::Result + where + F: FnOnce(&[VolatileSlice>]) -> io::Result, + { + let mut buflen = 0; + let mut bufs = Vec::with_capacity(self.buffers.len()); + for &vs in &self.buffers { + if buflen >= count { + break; + } + + bufs.push(vs); + + let rem = count - buflen; + if rem < vs.len() { + buflen += rem; + } else { + buflen += vs.len(); + } + } + + if bufs.is_empty() { + return Ok(0); + } + + let bytes_consumed = f(&bufs)?; + + // This can happen if a driver tricks a device into reading/writing more data than + // fits in a `usize`. + let total_bytes_consumed = + self.bytes_consumed + .checked_add(bytes_consumed) + .ok_or_else(|| { + io::Error::new(io::ErrorKind::InvalidData, Error::DescriptorChainOverflow) + })?; + + let mut rem = bytes_consumed; + while let Some(vs) = self.buffers.pop_front() { + if rem < vs.len() { + // Split the slice and push the remainder back into the buffer list. Safe because we + // know that `rem` is not out of bounds due to the check and we checked the bounds + // on `vs` when we added it to the buffer list. + self.buffers.push_front(vs.offset(rem).unwrap()); + break; + } + + // No need for checked math because we know that `vs.size() <= rem`. + rem -= vs.len(); + } + + self.bytes_consumed = total_bytes_consumed; + + Ok(bytes_consumed) + } + + fn split_at(&mut self, offset: usize) -> Result> { + let mut rem = offset; + let pos = self.buffers.iter().position(|vs| { + if rem < vs.len() { + true + } else { + rem -= vs.len(); + false + } + }); + + if let Some(at) = pos { + let mut other = self.buffers.split_off(at); + + if rem > 0 { + // There must be at least one element in `other` because we checked + // its `size` value in the call to `position` above. + let front = other.pop_front().expect("empty VecDeque after split"); + self.buffers + .push_back(front.subslice(0, rem).map_err(Error::VolatileMemoryError)?); + other.push_front(front.offset(rem).map_err(Error::VolatileMemoryError)?); + } + + Ok(DescriptorChainConsumer { + buffers: other, + bytes_consumed: 0, + }) + } else if rem == 0 { + Ok(DescriptorChainConsumer { + buffers: VecDeque::new(), + bytes_consumed: 0, + }) + } else { + Err(Error::SplitOutOfBounds(offset)) + } + } +} + +/// Provides high-level interface over the sequence of memory regions +/// defined by readable descriptors in the descriptor chain. +/// +/// Note that virtio spec requires driver to place any device-writable +/// descriptors after any device-readable descriptors (2.6.4.2 in Virtio Spec v1.1). +/// Reader will skip iterating over descriptor chain when first writable +/// descriptor is encountered. +#[derive(Clone)] +pub struct Reader<'a> { + buffer: DescriptorChainConsumer<'a>, +} + +impl<'a> Reader<'a> { + /// Construct a new Reader wrapper over `desc_chain`. + pub fn new(mem: &'a GuestMemoryMmap, desc_chain: DescriptorChain) -> Result> + where + M: Deref, + M::Target: GuestMemory + Sized, + { + let mut total_len: usize = 0; + let buffers = desc_chain + .readable() + .map(|desc| { + // Verify that summing the descriptor sizes does not overflow. + // This can happen if a driver tricks a device into reading more data than + // fits in a `usize`. + total_len = total_len + .checked_add(desc.len() as usize) + .ok_or(Error::DescriptorChainOverflow)?; + + let region = mem + .find_region(desc.addr()) + .ok_or(Error::FindMemoryRegion)?; + let offset = desc + .addr() + .checked_sub(region.start_addr().raw_value()) + .unwrap(); + region + .deref() + .get_slice(offset.raw_value() as usize, desc.len() as usize) + .map_err(Error::VolatileMemoryError) + }) + .collect::>>>>()?; + Ok(Reader { + buffer: DescriptorChainConsumer { + buffers, + bytes_consumed: 0, + }, + }) + } + + /// Reads an object from the descriptor chain buffer. + pub fn read_obj(&mut self) -> io::Result { + let mut obj = MaybeUninit::::uninit(); + + // Safe because `MaybeUninit` guarantees that the pointer is valid for + // `size_of::()` bytes. + let buf = unsafe { + ::std::slice::from_raw_parts_mut(obj.as_mut_ptr() as *mut u8, size_of::()) + }; + + self.read_exact(buf)?; + + // Safe because any type that implements `ByteValued` can be considered initialized + // even if it is filled with random data. + Ok(unsafe { obj.assume_init() }) + } + + /// Reads data from the descriptor chain buffer into a File at offset `off`. + /// Returns the number of bytes read from the descriptor chain buffer. + /// The number of bytes read can be less than `count` if there isn't + /// enough data in the descriptor chain buffer. + pub fn read_to_at( + &mut self, + dst: F, + count: usize, + off: u64, + flags: Option, + ) -> io::Result { + self.buffer.consume(count, |bufs| { + dst.write_vectored_at_volatile(bufs, off, flags) + }) + } + + /// Returns number of bytes available for reading. May return an error if the combined + /// lengths of all the buffers in the DescriptorChain would cause an integer overflow. + pub fn available_bytes(&self) -> usize { + self.buffer.available_bytes() + } + + /// Returns number of bytes already read from the descriptor chain buffer. + pub fn bytes_read(&self) -> usize { + self.buffer.bytes_consumed() + } + + /// Splits this `Reader` into two at the given offset in the `DescriptorChain` buffer. + /// After the split, `self` will be able to read up to `offset` bytes while the returned + /// `Reader` can read up to `available_bytes() - offset` bytes. Returns an error if + /// `offset > self.available_bytes()`. + pub fn split_at(&mut self, offset: usize) -> Result> { + self.buffer.split_at(offset).map(|buffer| Reader { buffer }) + } +} + +impl<'a> io::Read for Reader<'a> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.buffer.consume(buf.len(), |bufs| { + let mut rem = buf; + let mut total = 0; + for vs in bufs { + let copy_len = cmp::min(rem.len(), vs.len()); + + // SAFETY: Safe because we verify that we do not read outside + // of the slice's bound. The slice guard will only get dropped + // after the function returns. This will keep the pointer valid + // while reads are happening. + unsafe { + copy_nonoverlapping(vs.ptr_guard().as_ptr(), rem.as_mut_ptr(), copy_len); + } + rem = &mut rem[copy_len..]; + total += copy_len; + } + Ok(total) + }) + } +} + +/// Provides high-level interface over the sequence of memory regions +/// defined by writable descriptors in the descriptor chain. +/// +/// Note that virtio spec requires driver to place any device-writable +/// descriptors after any device-readable descriptors (2.6.4.2 in Virtio Spec v1.1). +/// Writer will start iterating the descriptors from the first writable one and will +/// assume that all following descriptors are writable. +#[derive(Clone)] +pub struct Writer<'a> { + buffer: DescriptorChainConsumer<'a>, +} + +impl<'a> Writer<'a> { + /// Construct a new Writer wrapper over `desc_chain`. + pub fn new(mem: &'a GuestMemoryMmap, desc_chain: DescriptorChain) -> Result> + where + M: Deref, + M::Target: GuestMemory + Sized, + { + let mut total_len: usize = 0; + let buffers = desc_chain + .writable() + .map(|desc| { + // Verify that summing the descriptor sizes does not overflow. + // This can happen if a driver tricks a device into writing more data than + // fits in a `usize`. + total_len = total_len + .checked_add(desc.len() as usize) + .ok_or(Error::DescriptorChainOverflow)?; + + let region = mem + .find_region(desc.addr()) + .ok_or(Error::FindMemoryRegion)?; + let offset = desc + .addr() + .checked_sub(region.start_addr().raw_value()) + .unwrap(); + region + .deref() + .get_slice(offset.raw_value() as usize, desc.len() as usize) + .map_err(Error::VolatileMemoryError) + }) + .collect::>>>>()?; + + Ok(Writer { + buffer: DescriptorChainConsumer { + buffers, + bytes_consumed: 0, + }, + }) + } + + /// Writes an object to the descriptor chain buffer. + pub fn write_obj(&mut self, val: T) -> io::Result<()> { + self.write_all(val.as_slice()) + } + + /// Returns number of bytes available for writing. May return an error if the combined + /// lengths of all the buffers in the DescriptorChain would cause an overflow. + pub fn available_bytes(&self) -> usize { + self.buffer.available_bytes() + } + + /// Writes data to the descriptor chain buffer from a File at offset `off`. + /// Returns the number of bytes written to the descriptor chain buffer. + /// The number of bytes written can be less than `count` if + /// there isn't enough data in the descriptor chain buffer. + pub fn write_from_at( + &mut self, + src: F, + count: usize, + off: u64, + ) -> io::Result { + self.buffer + .consume(count, |bufs| src.read_vectored_at_volatile(bufs, off)) + } + + /// Returns number of bytes already written to the descriptor chain buffer. + pub fn bytes_written(&self) -> usize { + self.buffer.bytes_consumed() + } + + /// Splits this `Writer` into two at the given offset in the `DescriptorChain` buffer. + /// After the split, `self` will be able to write up to `offset` bytes while the returned + /// `Writer` can write up to `available_bytes() - offset` bytes. Returns an error if + /// `offset > self.available_bytes()`. + pub fn split_at(&mut self, offset: usize) -> Result> { + self.buffer.split_at(offset).map(|buffer| Writer { buffer }) + } +} + +impl<'a> io::Write for Writer<'a> { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.buffer.consume(buf.len(), |bufs| { + let mut rem = buf; + let mut total = 0; + for vs in bufs { + let copy_len = cmp::min(rem.len(), vs.len()); + + // SAFETY: Safe because we ensure that we do not write over the + // slice's bounds. The slice guard will only get dropped after + // the function returns. This will keep the pointer valid while + // writes are happening. + unsafe { + copy_nonoverlapping(rem.as_ptr(), vs.ptr_guard_mut().as_ptr(), copy_len); + } + vs.bitmap().mark_dirty(0, copy_len); + rem = &rem[copy_len..]; + total += copy_len; + } + Ok(total) + }) + } + + fn flush(&mut self) -> io::Result<()> { + // Nothing to flush since the writes go straight into the buffer. + Ok(()) + } +} + +#[derive(Copy, Clone, PartialEq, Eq)] +pub enum DescriptorType { + Readable, + Writable, +} + +#[allow(dead_code)] +#[derive(Copy, Clone, Debug, Default)] +#[repr(C)] +struct virtq_desc { + addr: Le64, + len: Le32, + flags: Le16, + next: Le16, +} + +// Safe because it only has data and has no implicit padding. +unsafe impl ByteValued for virtq_desc {} + +#[allow(dead_code)] +#[derive(Copy, Clone, Debug, Default)] +#[repr(C)] +struct virtq_avail { + flags: Le16, + idx: Le16, + ring: Le16, +} + +// Safe because it only has data and has no implicit padding. +unsafe impl ByteValued for virtq_avail {} + +#[cfg(test)] +mod tests { + use super::*; + use virtio_queue::{Queue, QueueOwnedT, QueueT}; + use vm_memory::{Bytes, GuestAddress}; + + const VIRTQ_DESC_F_NEXT: u16 = 0x1; + const VIRTQ_DESC_F_WRITE: u16 = 0x2; + const MAX_QUEUE_SIZE: u16 = 32768; + + /// Test utility function to create a descriptor chain in guest memory. + pub fn create_descriptor_chain( + memory: &GuestMemoryMmap, + descriptor_array_addr: GuestAddress, + mut buffers_start_addr: GuestAddress, + descriptors: Vec<(DescriptorType, u32)>, + spaces_between_regions: u32, + ) -> Result> { + let descriptors_len = descriptors.len(); + for (index, (type_, size)) in descriptors.into_iter().enumerate() { + let mut flags = 0; + if let DescriptorType::Writable = type_ { + flags |= VIRTQ_DESC_F_WRITE; + } + if index + 1 < descriptors_len { + flags |= VIRTQ_DESC_F_NEXT; + } + + let index = index as u16; + let desc = virtq_desc { + addr: buffers_start_addr.raw_value().into(), + len: size.into(), + flags: flags.into(), + next: (index + 1).into(), + }; + + let offset = size + spaces_between_regions; + buffers_start_addr = buffers_start_addr + .checked_add(u64::from(offset)) + .ok_or(Error::InvalidChain)?; + + let _ = memory.write_obj( + desc, + descriptor_array_addr + .checked_add(u64::from(index) * std::mem::size_of::() as u64) + .ok_or(Error::InvalidChain)?, + ); + } + + let avail_ring = descriptor_array_addr + .checked_add( + u64::from(descriptors_len as u16) * std::mem::size_of::() as u64, + ) + .ok_or(Error::InvalidChain)?; + let avail = virtq_avail { + flags: 0.into(), + idx: 1.into(), + ring: 0.into(), + }; + let _ = memory.write_obj(avail, avail_ring); + + let mut queue: Queue = Queue::new(MAX_QUEUE_SIZE).unwrap(); + queue + .try_set_desc_table_address(descriptor_array_addr) + .unwrap(); + queue.try_set_avail_ring_address(avail_ring).unwrap(); + queue.set_ready(true); + let desc = queue.iter(memory).unwrap().next().unwrap(); + Ok(desc.clone()) + } + + #[test] + fn reader_test_simple_chain() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![ + (Readable, 8), + (Readable, 16), + (Readable, 18), + (Readable, 64), + ], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut reader = Reader::new(&memory, chain).expect("failed to create Reader"); + assert_eq!(reader.available_bytes(), 106); + assert_eq!(reader.bytes_read(), 0); + + let mut buffer = [0 as u8; 64]; + if let Err(e) = reader.read_exact(&mut buffer) { + panic!("read_exact should not fail here: {:?}", e); + } + + assert_eq!(reader.available_bytes(), 42); + assert_eq!(reader.bytes_read(), 64); + + match reader.read(&mut buffer) { + Err(e) => panic!("read should not fail here: {:?}", e), + Ok(length) => assert_eq!(length, 42), + } + + assert_eq!(reader.available_bytes(), 0); + assert_eq!(reader.bytes_read(), 106); + } + + #[test] + fn writer_test_simple_chain() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![ + (Writable, 8), + (Writable, 16), + (Writable, 18), + (Writable, 64), + ], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut writer = Writer::new(&memory, chain).expect("failed to create Writer"); + assert_eq!(writer.available_bytes(), 106); + assert_eq!(writer.bytes_written(), 0); + + let buffer = [0 as u8; 64]; + if let Err(e) = writer.write_all(&buffer) { + panic!("write_all should not fail here: {:?}", e); + } + + assert_eq!(writer.available_bytes(), 42); + assert_eq!(writer.bytes_written(), 64); + + match writer.write(&buffer) { + Err(e) => panic!("write should not fail here {:?}", e), + Ok(length) => assert_eq!(length, 42), + } + + assert_eq!(writer.available_bytes(), 0); + assert_eq!(writer.bytes_written(), 106); + } + + #[test] + fn reader_test_incompatible_chain() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![(Writable, 8)], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut reader = Reader::new(&memory, chain).expect("failed to create Reader"); + assert_eq!(reader.available_bytes(), 0); + assert_eq!(reader.bytes_read(), 0); + + assert!(reader.read_obj::().is_err()); + + assert_eq!(reader.available_bytes(), 0); + assert_eq!(reader.bytes_read(), 0); + } + + #[test] + fn writer_test_incompatible_chain() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![(Readable, 8)], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut writer = Writer::new(&memory, chain).expect("failed to create Writer"); + assert_eq!(writer.available_bytes(), 0); + assert_eq!(writer.bytes_written(), 0); + + assert!(writer.write_obj(0u8).is_err()); + + assert_eq!(writer.available_bytes(), 0); + assert_eq!(writer.bytes_written(), 0); + } + + #[test] + fn reader_writer_shared_chain() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![ + (Readable, 16), + (Readable, 16), + (Readable, 96), + (Writable, 64), + (Writable, 1), + (Writable, 3), + ], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut reader = Reader::new(&memory, chain.clone()).expect("failed to create Reader"); + let mut writer = Writer::new(&memory, chain).expect("failed to create Writer"); + + assert_eq!(reader.bytes_read(), 0); + assert_eq!(writer.bytes_written(), 0); + + let mut buffer = Vec::with_capacity(200); + + assert_eq!( + reader + .read_to_end(&mut buffer) + .expect("read should not fail here"), + 128 + ); + + // The writable descriptors are only 68 bytes long. + writer + .write_all(&buffer[..68]) + .expect("write should not fail here"); + + assert_eq!(reader.available_bytes(), 0); + assert_eq!(reader.bytes_read(), 128); + assert_eq!(writer.available_bytes(), 0); + assert_eq!(writer.bytes_written(), 68); + } + + #[test] + fn reader_writer_shattered_object() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let secret: Le32 = 0x1234_5678.into(); + + // Create a descriptor chain with memory regions that are properly separated. + let chain_writer = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![(Writable, 1), (Writable, 1), (Writable, 1), (Writable, 1)], + 123, + ) + .expect("create_descriptor_chain failed"); + let mut writer = Writer::new(&memory, chain_writer).expect("failed to create Writer"); + if let Err(e) = writer.write_obj(secret) { + panic!("write_obj should not fail here: {:?}", e); + } + + // Now create new descriptor chain pointing to the same memory and try to read it. + let chain_reader = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![(Readable, 1), (Readable, 1), (Readable, 1), (Readable, 1)], + 123, + ) + .expect("create_descriptor_chain failed"); + let mut reader = Reader::new(&memory, chain_reader).expect("failed to create Reader"); + match reader.read_obj::() { + Err(e) => panic!("read_obj should not fail here: {:?}", e), + Ok(read_secret) => assert_eq!(read_secret, secret), + } + } + + #[test] + fn reader_unexpected_eof() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![(Readable, 256), (Readable, 256)], + 0, + ) + .expect("create_descriptor_chain failed"); + + let mut reader = Reader::new(&memory, chain).expect("failed to create Reader"); + + let mut buf = vec![0; 1024]; + + assert_eq!( + reader + .read_exact(&mut buf[..]) + .expect_err("read more bytes than available") + .kind(), + io::ErrorKind::UnexpectedEof + ); + } + + #[test] + fn split_border() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![ + (Readable, 16), + (Readable, 16), + (Readable, 96), + (Writable, 64), + (Writable, 1), + (Writable, 3), + ], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut reader = Reader::new(&memory, chain).expect("failed to create Reader"); + + let other = reader.split_at(32).expect("failed to split Reader"); + assert_eq!(reader.available_bytes(), 32); + assert_eq!(other.available_bytes(), 96); + } + + #[test] + fn split_middle() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![ + (Readable, 16), + (Readable, 16), + (Readable, 96), + (Writable, 64), + (Writable, 1), + (Writable, 3), + ], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut reader = Reader::new(&memory, chain).expect("failed to create Reader"); + + let other = reader.split_at(24).expect("failed to split Reader"); + assert_eq!(reader.available_bytes(), 24); + assert_eq!(other.available_bytes(), 104); + } + + #[test] + fn split_end() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![ + (Readable, 16), + (Readable, 16), + (Readable, 96), + (Writable, 64), + (Writable, 1), + (Writable, 3), + ], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut reader = Reader::new(&memory, chain).expect("failed to create Reader"); + + let other = reader.split_at(128).expect("failed to split Reader"); + assert_eq!(reader.available_bytes(), 128); + assert_eq!(other.available_bytes(), 0); + } + + #[test] + fn split_beginning() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![ + (Readable, 16), + (Readable, 16), + (Readable, 96), + (Writable, 64), + (Writable, 1), + (Writable, 3), + ], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut reader = Reader::new(&memory, chain).expect("failed to create Reader"); + + let other = reader.split_at(0).expect("failed to split Reader"); + assert_eq!(reader.available_bytes(), 0); + assert_eq!(other.available_bytes(), 128); + } + + #[test] + fn split_outofbounds() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![ + (Readable, 16), + (Readable, 16), + (Readable, 96), + (Writable, 64), + (Writable, 1), + (Writable, 3), + ], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut reader = Reader::new(&memory, chain).expect("failed to create Reader"); + + if reader.split_at(256).is_ok() { + panic!("successfully split Reader with out of bounds offset"); + } + } + + #[test] + fn read_full() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![(Readable, 16), (Readable, 16), (Readable, 16)], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut reader = Reader::new(&memory, chain).expect("failed to create Reader"); + + let mut buf = vec![0u8; 64]; + assert_eq!( + reader.read(&mut buf[..]).expect("failed to read to buffer"), + 48 + ); + } + + #[test] + fn write_full() { + use DescriptorType::*; + + let memory_start_addr = GuestAddress(0x0); + let memory = GuestMemoryMmap::from_ranges(&[(memory_start_addr, 0x10000)]).unwrap(); + + let chain = create_descriptor_chain( + &memory, + GuestAddress(0x0), + GuestAddress(0x100), + vec![(Writable, 16), (Writable, 16), (Writable, 16)], + 0, + ) + .expect("create_descriptor_chain failed"); + let mut writer = Writer::new(&memory, chain).expect("failed to create Writer"); + + let buf = vec![0xdeu8; 64]; + assert_eq!( + writer.write(&buf[..]).expect("failed to write from buffer"), + 48 + ); + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/file_traits.rs b/vendor/cube-hypervisor/virtiofsd/src/file_traits.rs new file mode 100644 index 0000000..e643296 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/file_traits.rs @@ -0,0 +1,161 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use std::convert::TryInto; +use std::fs::File; +use std::io::{Error, Result}; +use std::os::unix::io::{AsFd, AsRawFd}; + +use vm_memory::bitmap::{AtomicBitmap, Bitmap, RefSlice}; +use vm_memory::VolatileSlice; + +use crate::oslib; +use libc::{c_int, c_void, off64_t, preadv64, size_t}; + +/// A trait for setting the size of a file. +/// This is equivalent to File's `set_len` method, but +/// wrapped in a trait so that it can be implemented for +/// other types. +pub trait FileSetLen { + // Set the size of this file. + // This is the moral equivalent of `ftruncate()`. + fn set_len(&self, _len: u64) -> Result<()>; +} + +impl FileSetLen for File { + fn set_len(&self, len: u64) -> Result<()> { + File::set_len(self, len) + } +} + +/// A trait similar to the unix `ReadExt` and `WriteExt` traits, but for volatile memory. +pub trait FileReadWriteAtVolatile { + /// Reads bytes from this file at `offset` into the given slice of buffers, returning the number + /// of bytes read on success. Data is copied to fill each buffer in order, with the final buffer + /// written to possibly being only partially filled. + fn read_vectored_at_volatile( + &self, + bufs: &[VolatileSlice>], + offset: u64, + ) -> Result; + + /// Writes bytes to this file at `offset` from the given slice of buffers, returning the number + /// of bytes written on success. Data is copied from each buffer in order, with the final buffer + /// read from possibly being only partially consumed. + fn write_vectored_at_volatile( + &self, + bufs: &[VolatileSlice>], + offset: u64, + flags: Option, + ) -> Result; +} + +impl FileReadWriteAtVolatile for &T { + fn read_vectored_at_volatile( + &self, + bufs: &[VolatileSlice>], + offset: u64, + ) -> Result { + (**self).read_vectored_at_volatile(bufs, offset) + } + + fn write_vectored_at_volatile( + &self, + bufs: &[VolatileSlice>], + offset: u64, + flags: Option, + ) -> Result { + (**self).write_vectored_at_volatile(bufs, offset, flags) + } +} + +macro_rules! volatile_impl { + ($ty:ty) => { + impl FileReadWriteAtVolatile for $ty { + fn read_vectored_at_volatile( + &self, + bufs: &[VolatileSlice>], + offset: u64, + ) -> Result { + let slice_guards: Vec<_> = bufs.iter().map(|s| s.ptr_guard_mut()).collect(); + let iovecs: Vec = slice_guards + .iter() + .map(|s| libc::iovec { + iov_base: s.as_ptr() as *mut c_void, + iov_len: s.len() as size_t, + }) + .collect(); + + if iovecs.is_empty() { + return Ok(0); + } + + // SAFETY: Safe because only bytes inside the buffers are + // accessed and the kernel is expected to handle arbitrary + // memory for I/O. The pointers into the slice are valid since + // the slice_guards are still in scope. + let ret = unsafe { + preadv64( + self.as_raw_fd(), + &iovecs[0], + iovecs.len() as c_int, + offset as off64_t, + ) + }; + + if ret >= 0 { + let mut total = 0; + for vs in bufs { + // Each `VolatileSlice` has a "local" bitmap (i.e., the offset 0 in the + // bitmap corresponds to the beginning of the `VolatileSlice`) + vs.bitmap() + .mark_dirty(0, std::cmp::min(ret as usize - total, vs.len())); + total += vs.len(); + if total >= ret as usize { + break; + } + } + Ok(ret as usize) + } else { + Err(Error::last_os_error()) + } + } + + fn write_vectored_at_volatile( + &self, + bufs: &[VolatileSlice>], + offset: u64, + flags: Option, + ) -> Result { + let slice_guards: Vec<_> = bufs.iter().map(|s| s.ptr_guard()).collect(); + let iovecs: Vec = slice_guards + .iter() + .map(|s| libc::iovec { + iov_base: s.as_ptr() as *mut c_void, + iov_len: s.len() as size_t, + }) + .collect(); + + if iovecs.is_empty() { + return Ok(0); + } + + // SAFETY: Each `libc::iovec` element is created from a + // `VolatileSlice` of the guest memory. The pointers are valid + // because the slice guards are still in scope. We also ensure + // that we do not read over the slice bounds. + unsafe { + oslib::writev_at( + self.as_fd(), + iovecs.as_slice(), + offset.try_into().unwrap(), + flags, + ) + } + } + } + }; +} + +volatile_impl!(File); diff --git a/vendor/cube-hypervisor/virtiofsd/src/filesystem.rs b/vendor/cube-hypervisor/virtiofsd/src/filesystem.rs new file mode 100644 index 0000000..1963f06 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/filesystem.rs @@ -0,0 +1,1306 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use std::convert::TryInto; +use std::ffi::{CStr, CString}; +use std::fs::File; +use std::sync::atomic::AtomicBool; +use std::sync::Arc; +use std::time::Duration; +use std::{io, mem}; + +use crate::passthrough::stat::StatExt; +use crate::{fuse, oslib}; + +use super::fs_cache_req_handler::FsCacheReqHandler; +pub use fuse::{FsOptions, OpenOptions, RemovemappingOne, SetattrValid, SetxattrFlags, ROOT_ID}; + +/// Information about a path in the filesystem. +pub struct Entry { + /// An `Inode` that uniquely identifies this path. During `lookup`, setting this to `0` means a + /// negative entry. Returning `ENOENT` also means a negative entry but setting this to `0` + /// allows the kernel to cache the negative result for `entry_timeout`. The value should be + /// produced by converting a `FileSystem::Inode` into a `u64`. + pub inode: u64, + + /// The generation number for this `Entry`. Typically used for network file systems. An `inode` + /// / `generation` pair must be unique over the lifetime of the file system (rather than just + /// the lifetime of the mount). In other words, if a `FileSystem` implementation re-uses an + /// `Inode` after it has been deleted then it must assign a new, previously unused generation + /// number to the `Inode` at the same time. + pub generation: u64, + + /// Inode attributes. Even if `attr_timeout` is zero, `attr` must be correct. For example, for + /// `open()`, FUSE uses `attr.st_size` from `lookup()` to determine how many bytes to request. + /// If this value is not correct, incorrect data will be returned. + pub attr: libc::stat64, + + /// Flags for `fuse::Attr.flags`. + pub attr_flags: u32, + + /// How long the values in `attr` should be considered valid. If the attributes of the `Entry` + /// are only modified by the FUSE client, then this should be set to a very large value. + pub attr_timeout: Duration, + + /// How long the name associated with this `Entry` should be considered valid. If directory + /// entries are only changed or deleted by the FUSE client, then this should be set to a very + /// large value. + pub entry_timeout: Duration, +} + +impl From for fuse::EntryOut { + fn from(entry: Entry) -> fuse::EntryOut { + fuse::EntryOut { + nodeid: entry.inode, + generation: entry.generation, + entry_valid: entry.entry_timeout.as_secs(), + attr_valid: entry.attr_timeout.as_secs(), + entry_valid_nsec: entry.entry_timeout.subsec_nanos(), + attr_valid_nsec: entry.attr_timeout.subsec_nanos(), + attr: fuse::Attr::with_flags(entry.attr, entry.attr_flags), + } + } +} + +/// Represents information about an entry in a directory. +#[derive(Debug)] +pub struct DirEntry<'a> { + /// The inode number for this entry. This does NOT have to be the same as the `Inode` for this + /// directory entry. However, it must be the same as the `attr.st_ino` field of the `Entry` that + /// would be returned by a `lookup` request in the parent directory for `name`. + pub ino: libc::ino64_t, + + /// Any non-zero value that the kernel can use to identify the current point in the directory + /// entry stream. It does not need to be the actual physical position. A value of `0` is + /// reserved to mean "from the beginning" and should never be used. The `offset` value of the + /// first entry in a stream should point to the beginning of the second entry and so on. + pub offset: u64, + + /// The type of this directory entry. Valid values are any of the `libc::DT_*` constants. + pub type_: u32, + + /// The name of this directory entry. There are no requirements for the contents of this field + /// and any sequence of bytes is considered valid. + pub name: &'a CStr, +} + +/// Represents information about an entry in a directory. +#[derive(Debug)] +pub struct DirEntry2 { + /// The inode number for this entry. This does NOT have to be the same as the `Inode` for this + /// directory entry. However, it must be the same as the `attr.st_ino` field of the `Entry` that + /// would be returned by a `lookup` request in the parent directory for `name`. + pub ino: libc::ino64_t, + + /// Any non-zero value that the kernel can use to identify the current point in the directory + /// entry stream. It does not need to be the actual physical position. A value of `0` is + /// reserved to mean "from the beginning" and should never be used. The `offset` value of the + /// first entry in a stream should point to the beginning of the second entry and so on. + pub offset: u64, + + /// The type of this directory entry. Valid values are any of the `libc::DT_*` constants. + pub type_: u32, + + /// The name of this directory entry. There are no requirements for the contents of this field + /// and any sequence of bytes is considered valid. + pub name: String, +} + +/// A reply to a `getxattr` method call. +pub enum GetxattrReply { + /// The value of the requested extended attribute. This can be arbitrary textual or binary data + /// and does not need to be nul-terminated. + Value(Vec), + + /// The size of the buffer needed to hold the value of the requested extended attribute. Should + /// be returned when the `size` parameter is 0. Callers should note that it is still possible + /// for the size of the value to change in between `getxattr` calls and should not assume that a + /// subsequent call to `getxattr` with the returned count will always succeed. + Count(u32), +} + +/// A reply to a `listxattr` method call. +pub enum ListxattrReply { + /// A buffer containing a nul-separated list of the names of all the extended attributes + /// associated with this `Inode`. This list of names may be unordered and includes a namespace + /// prefix. There may be several disjoint namespaces associated with a single `Inode`. + Names(Vec), + + /// This size of the buffer needed to hold the full list of extended attribute names associated + /// with this `Inode`. Should be returned when the `size` parameter is 0. Callers should note + /// that it is still possible for the set of extended attributes to change between `listxattr` + /// calls and so should not assume that a subsequent call to `listxattr` with the returned count + /// will always succeed. + Count(u32), +} + +/// A trait for directly copying data from the fuse transport into a `File` without first storing it +/// in an intermediate buffer. +pub trait ZeroCopyReader { + /// Copies at most `count` bytes from `self` directly into `f` at offset `off` without storing + /// it in any intermediate buffers. If the return value is `Ok(n)` then it must be guaranteed + /// that `0 <= n <= count`. If `n` is `0`, then it can indicate one of 3 possibilities: + /// + /// 1. There is no more data left in `self`. + /// 2. There is no more space in `f`. + /// 3. `count` was `0`. + /// + /// # Errors + /// + /// If any error is returned then the implementation must guarantee that no bytes were copied + /// from `self`. If the underlying write to `f` returns `0` then the implementation must return + /// an error of the kind `io::ErrorKind::WriteZero`. + fn read_to( + &mut self, + f: &File, + count: usize, + off: u64, + flags: Option, + ) -> io::Result; + + /// Copies exactly `count` bytes of data from `self` into `f` at offset `off`. `off + count` + /// must be less than `u64::MAX`. + /// + /// # Errors + /// + /// If an error is returned then the number of bytes copied from `self` is unspecified but it + /// will never be more than `count`. + fn read_exact_to( + &mut self, + f: &mut File, + mut count: usize, + mut off: u64, + flags: Option, + ) -> io::Result<()> { + let c = count + .try_into() + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; + if off.checked_add(c).is_none() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "`off` + `count` must be less than u64::MAX", + )); + } + + while count > 0 { + match self.read_to(f, count, off, flags) { + Ok(0) => { + return Err(io::Error::new( + io::ErrorKind::WriteZero, + "failed to fill whole buffer", + )) + } + Ok(n) => { + count -= n; + off += n as u64; + } + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} + Err(e) => return Err(e), + } + } + + Ok(()) + } + + /// Copies all remaining bytes from `self` into `f` at offset `off`. Equivalent to repeatedly + /// calling `read_to` until it returns either `Ok(0)` or a non-`ErrorKind::Interrupted` error. + /// + /// # Errors + /// + /// If an error is returned then the number of bytes copied from `self` is unspecified. + fn copy_to_end( + &mut self, + f: &mut File, + mut off: u64, + flags: Option, + ) -> io::Result { + let mut out = 0; + loop { + match self.read_to(f, usize::MAX, off, flags) { + Ok(0) => return Ok(out), + Ok(n) => { + off = off.saturating_add(n as u64); + out += n; + } + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} + Err(e) => return Err(e), + } + } + } +} + +impl ZeroCopyReader for &mut R { + fn read_to( + &mut self, + f: &File, + count: usize, + off: u64, + flags: Option, + ) -> io::Result { + (**self).read_to(f, count, off, flags) + } + fn read_exact_to( + &mut self, + f: &mut File, + count: usize, + off: u64, + flags: Option, + ) -> io::Result<()> { + (**self).read_exact_to(f, count, off, flags) + } + fn copy_to_end( + &mut self, + f: &mut File, + off: u64, + flags: Option, + ) -> io::Result { + (**self).copy_to_end(f, off, flags) + } +} + +/// A trait for directly copying data from a `File` into the fuse transport without first storing +/// it in an intermediate buffer. +pub trait ZeroCopyWriter { + /// Copies at most `count` bytes from `f` at offset `off` directly into `self` without storing + /// it in any intermediate buffers. If the return value is `Ok(n)` then it must be guaranteed + /// that `0 <= n <= count`. If `n` is `0`, then it can indicate one of 3 possibilities: + /// + /// 1. There is no more data left in `f`. + /// 2. There is no more space in `self`. + /// 3. `count` was `0`. + /// + /// # Errors + /// + /// If any error is returned then the implementation must guarantee that no bytes were copied + /// from `f`. If the underlying read from `f` returns `0` then the implementation must return an + /// error of the kind `io::ErrorKind::UnexpectedEof`. + fn write_from(&mut self, f: &File, count: usize, off: u64) -> io::Result; + + /// Copies exactly `count` bytes of data from `f` at offset `off` into `self`. `off + count` + /// must be less than `u64::MAX`. + /// + /// # Errors + /// + /// If an error is returned then the number of bytes copied from `self` is unspecified but it + /// well never be more than `count`. + fn write_all_from(&mut self, f: &mut File, mut count: usize, mut off: u64) -> io::Result<()> { + let c = count + .try_into() + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; + if off.checked_add(c).is_none() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "`off` + `count` must be less than u64::MAX", + )); + } + + while count > 0 { + match self.write_from(f, count, off) { + Ok(0) => { + return Err(io::Error::new( + io::ErrorKind::UnexpectedEof, + "failed to write whole buffer", + )) + } + Ok(n) => { + // No need for checked math here because we verified that `off + count` will not + // overflow and `n` must be <= `count`. + count -= n; + off += n as u64; + } + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} + Err(e) => return Err(e), + } + } + + Ok(()) + } + + /// Copies all remaining bytes from `f` at offset `off` into `self`. Equivalent to repeatedly + /// calling `write_from` until it returns either `Ok(0)` or a non-`ErrorKind::Interrupted` + /// error. + /// + /// # Errors + /// + /// If an error is returned then the number of bytes copied from `f` is unspecified. + fn copy_to_end(&mut self, f: &mut File, mut off: u64) -> io::Result { + let mut out = 0; + loop { + match self.write_from(f, usize::MAX, off) { + Ok(0) => return Ok(out), + Ok(n) => { + off = off.saturating_add(n as u64); + out += n; + } + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} + Err(e) => return Err(e), + } + } + } +} + +impl ZeroCopyWriter for &mut W { + fn write_from(&mut self, f: &File, count: usize, off: u64) -> io::Result { + (**self).write_from(f, count, off) + } + fn write_all_from(&mut self, f: &mut File, count: usize, off: u64) -> io::Result<()> { + (**self).write_all_from(f, count, off) + } + fn copy_to_end(&mut self, f: &mut File, off: u64) -> io::Result { + (**self).copy_to_end(f, off) + } +} + +/// Additional context associated with requests. +#[derive(Clone, Copy, Debug)] +pub struct Context { + /// The user ID of the calling process. + pub uid: libc::uid_t, + + /// The group ID of the calling process. + pub gid: libc::gid_t, + + /// The thread group ID of the calling process. + pub pid: libc::pid_t, +} + +impl From for Context { + fn from(source: fuse::InHeader) -> Self { + Context { + uid: source.uid, + gid: source.gid, + pid: source.pid as i32, + } + } +} + +/// Request extensions +#[derive(Clone, Default, Debug)] +pub struct Extensions { + pub secctx: Option, + pub sup_gid: Option, +} + +/// Additional security context associated with requests. +#[derive(Clone, Debug, Default)] +pub struct SecContext { + /// Name of security context + pub name: CString, + + /// Actual security context + pub secctx: Vec, +} + +/// A trait for iterating over the contents of a directory. This trait is needed because rust +/// doesn't support generic associated types, which means that it's not possible to implement a +/// regular iterator that yields a `DirEntry` due to its generic lifetime parameter. +pub trait DirectoryIterator { + /// Returns the next entry in the directory or `None` if there are no more. + fn next(&'_ mut self) -> Option>; +} + +/// The main trait that connects a file system with a transport. +#[allow(unused_variables)] +pub trait FileSystem { + /// Represents a location in the filesystem tree and can be used to perform operations that act + /// on the metadata of a file/directory (e.g., `getattr` and `setattr`). Can also be used as the + /// starting point for looking up paths in the filesystem tree. An `Inode` may support operating + /// directly on the content of the path that to which it points. `FileSystem` implementations + /// that support this should set the `FsOptions::ZERO_MESSAGE_OPEN` option in the return value + /// of the `init` function. On linux based systems, an `Inode` is equivalent to opening a file + /// or directory with the `libc::O_PATH` flag. + /// + /// # Lookup Count + /// + /// The `FileSystem` implementation is required to keep a "lookup count" for every `Inode`. + /// Every time an `Entry` is returned by a `FileSystem` trait method, this lookup count should + /// increase by 1. The lookup count for an `Inode` decreases when the kernel sends a `forget` + /// request. `Inode`s with a non-zero lookup count may receive requests from the kernel even + /// after calls to `unlink`, `rmdir` or (when overwriting an existing file) `rename`. + /// `FileSystem` implementations must handle such requests properly and it is recommended to + /// defer removal of the `Inode` until the lookup count reaches zero. Calls to `unlink`, `rmdir` + /// or `rename` will be followed closely by `forget` unless the file or directory is open, in + /// which case the kernel issues `forget` only after the `release` or `releasedir` calls. + /// + /// Note that if a file system will be exported over NFS the `Inode`'s lifetime must extend even + /// beyond `forget`. See the `generation` field in `Entry`. + type Inode: From + Into; + + /// Represents a file or directory that is open for reading/writing. + type Handle: From + Into; + + /// An iterator over the entries of a directory. See the documentation for `readdir` for more + /// details. + type DirIter: DirectoryIterator; + + /// Initialize the file system. + /// + /// This method is called when a connection to the FUSE kernel module is first established. The + /// `capable` parameter indicates the features that are supported by the kernel module. The + /// implementation should return the options that it supports. Any options set in the returned + /// `FsOptions` that are not also set in `capable` are silently dropped. + fn init(&self, capable: FsOptions) -> io::Result { + Ok(FsOptions::empty()) + } + + /// Clean up the file system. + /// + /// Called when the filesystem exits. All open `Handle`s should be closed and the lookup count + /// for all open `Inode`s implicitly goes to zero. At this point the connection to the FUSE + /// kernel module may already be gone so implementations should not rely on being able to + /// communicate with the kernel. + fn destroy(&self) {} + + /// Look up a directory entry by name and get its attributes. + /// + /// If this call is successful then the lookup count of the `Inode` associated with the returned + /// `Entry` must be increased by 1. + fn lookup( + &self, + ctx: Context, + parent: Self::Inode, + name: &CStr, + f_info: Option<(String, StatExt)>, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Forget about an inode. + /// + /// Called when the kernel removes an inode from its internal caches. `count` indicates the + /// amount by which the lookup count for the inode should be decreased. If reducing the lookup + /// count by `count` causes it to go to zero, then the implementation may delete the `Inode`. + fn forget(&self, ctx: Context, inode: Self::Inode, count: u64) {} + + /// Forget about multiple inodes. + /// + /// `requests` is a vector of `(inode, count)` pairs. See the documentation for `forget` for + /// more information. + fn batch_forget(&self, ctx: Context, requests: Vec<(Self::Inode, u64)>) { + for (inode, count) in requests { + self.forget(ctx, inode, count) + } + } + + /// Get attributes for a file / directory. + /// + /// If `handle` is not `None`, then it contains the handle previously returned by the + /// implementation after a call to `open` or `opendir`. However, implementations should still + /// take care to verify the handle if they do not trust the client (e.g., virtio-fs). + /// + /// If writeback caching is enabled (`FsOptions::WRITEBACK_CACHE`), then the kernel module + /// likely has a better idea of the length of the file than the file system (for + /// example, if there was a write that extended the size of the file but has not yet been + /// flushed). In this case, the `st_size` field of the returned struct is ignored. + /// + /// The returned `Duration` indicates how long the returned attributes should be considered + /// valid by the client. If the attributes are only changed via the FUSE kernel module (i.e., + /// the kernel module has exclusive access), then this should be a very large value. + fn getattr( + &self, + ctx: Context, + inode: Self::Inode, + handle: Option, + ) -> io::Result<(libc::stat64, Duration)> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Set attributes for a file / directory. + /// + /// If `handle` is not `None`, then it contains the handle previously returned by the + /// implementation after a call to `open` or `opendir`. However, implementations should still + /// take care to verify the handle if they do not trust the client (e.g., virtio-fs). + /// + /// The `valid` parameter indicates the fields of `attr` that may be considered valid and should + /// be set by the file system. The content of all other fields in `attr` is undefined. + /// + /// If the `FsOptions::HANDLE_KILLPRIV_V2` was set during `init`, then the implementation is + /// expected to reset the setuid and setgid bits if the file size or owner is being changed. + /// + /// This method returns the new attributes after making the modifications requested by the + /// client. The returned `Duration` indicates how long the returned attributes should be + /// considered valid by the client. If the attributes are only changed via the FUSE kernel + /// module (i.e., the kernel module has exclusive access), then this should be a very large + /// value. + fn setattr( + &self, + ctx: Context, + inode: Self::Inode, + attr: libc::stat64, + handle: Option, + valid: SetattrValid, + ) -> io::Result<(libc::stat64, Duration)> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Read a symbolic link. + fn readlink(&self, ctx: Context, inode: Self::Inode) -> io::Result> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Create a symbolic link. + /// + /// The file system must create a symbolic link named `name` in the directory represented by + /// `parent`, which contains the string `linkname`. Returns an `Entry` for the newly created + /// symlink. + /// + /// If this call is successful then the lookup count of the `Inode` associated with the returned + /// `Entry` must be increased by 1. + fn symlink( + &self, + ctx: Context, + linkname: &CStr, + parent: Self::Inode, + name: &CStr, + extensions: Extensions, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Create a file node. + /// + /// Create a regular file, character device, block device, fifo, or socket node named `name` in + /// the directory represented by `inode`. Valid values for `mode` and `rdev` are the same as + /// those accepted by the `mknod(2)` system call. Returns an `Entry` for the newly created node. + /// + /// When the `FsOptions::DONT_MASK` feature is set, the file system is responsible for setting + /// the permissions of the created node to `mode & !umask`. + /// + /// If this call is successful then the lookup count of the `Inode` associated with the returned + /// `Entry` must be increased by 1. + #[allow(clippy::too_many_arguments)] + fn mknod( + &self, + ctx: Context, + inode: Self::Inode, + name: &CStr, + mode: u32, + rdev: u32, + umask: u32, + extensions: Extensions, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Create a directory. + /// + /// When the `FsOptions::DONT_MASK` feature is set, the file system is responsible for setting + /// the permissions of the created directory to `mode & !umask`. Returns an `Entry` for the + /// newly created directory. + /// + /// If this call is successful then the lookup count of the `Inode` associated with the returned + /// `Entry` must be increased by 1. + fn mkdir( + &self, + ctx: Context, + parent: Self::Inode, + name: &CStr, + mode: u32, + umask: u32, + extensions: Extensions, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Remove a file. + /// + /// If the file's inode lookup count is non-zero, then the file system is expected to delay + /// removal of the inode until the lookup count goes to zero. See the documentation of the + /// `forget` function for more information. + fn unlink(&self, ctx: Context, parent: Self::Inode, name: &CStr) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Remove a directory. + /// + /// If the directory's inode lookup count is non-zero, then the file system is expected to delay + /// removal of the inode until the lookup count goes to zero. See the documentation of the + /// `forget` function for more information. + fn rmdir(&self, ctx: Context, parent: Self::Inode, name: &CStr) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Rename a file / directory. + /// + /// If the destination exists, it should be atomically replaced. If the destination's inode + /// lookup count is non-zero, then the file system is expected to delay removal of the inode + /// until the lookup count goes to zero. See the documentation of the `forget` function for more + /// information. + /// + /// `flags` may be `libc::RENAME_EXCHANGE` or `libc::RENAME_NOREPLACE`. If + /// `libc::RENAME_NOREPLACE` is specified, the implementation must not overwrite `newname` if it + /// exists and must return an error instead. If `libc::RENAME_EXCHANGE` is specified, the + /// implementation must atomically exchange the two files, i.e., both must exist and neither may + /// be deleted. + fn rename( + &self, + ctx: Context, + olddir: Self::Inode, + oldname: &CStr, + newdir: Self::Inode, + newname: &CStr, + flags: u32, + ) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Create a hard link. + /// + /// Create a hard link from `inode` to `newname` in the directory represented by `newparent`. + /// + /// If this call is successful then the lookup count of the `Inode` associated with the returned + /// `Entry` must be increased by 1. + fn link( + &self, + ctx: Context, + inode: Self::Inode, + newparent: Self::Inode, + newname: &CStr, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Open a file. + /// + /// Open the file associated with `inode` for reading / writing. All values accepted by the + /// `open(2)` system call are valid values for `flags` and must be handled by the file system. + /// However, there are some additional rules: + /// + /// * Creation flags (`libc::O_CREAT`, `libc::O_EXCL`, `libc::O_NOCTTY`) will be filtered out + /// and handled by the kernel. + /// + /// * The file system should check the access modes (`libc::O_RDONLY`, `libc::O_WRONLY`, + /// `libc::O_RDWR`) to determine if the operation is permitted. If the file system was mounted + /// with the `-o default_permissions` mount option, then this check will also be carried out + /// by the kernel before sending the open request. + /// + /// * When writeback caching is enabled (`FsOptions::WRITEBACK_CACHE`) the kernel may send read + /// requests even for files opened with `libc::O_WRONLY`. The file system should be prepared + /// to handle this. + /// + /// * When writeback caching is enabled, the kernel will handle the `libc::O_APPEND` flag. + /// However, this will not work reliably unless the kernel has exclusive access to the file. + /// In this case the file system may either ignore the `libc::O_APPEND` flag or return an + /// error to indicate that reliable `libc::O_APPEND` handling is not available. + /// + /// * When writeback caching is disabled, the file system is expected to properly handle + /// `libc::O_APPEND` and ensure that each write is appended to the end of the file. + /// + /// The file system may choose to return a `Handle` to refer to the newly opened file. The + /// kernel will then use this `Handle` for all operations on the content of the file (`read`, + /// `write`, `flush`, `release`, `fsync`). If the file system does not return a + /// `Handle` then the kernel will use the `Inode` for the file to operate on its contents. In + /// this case the file system may wish to enable the `FsOptions::ZERO_MESSAGE_OPEN` feature if + /// it is supported by the kernel (see below). + /// + /// The returned `OpenOptions` allow the file system to change the way the opened file is + /// handled by the kernel. See the documentation of `OpenOptions` for more information. + /// + /// If `kill_priv` is true then it indicates that the file system is expected to clear the + /// setuid and setgid bits. + /// + /// If the `FsOptions::ZERO_MESSAGE_OPEN` feature is enabled by both the file system + /// implementation and the kernel, then the file system may return an error of `ENOSYS`. This + /// will be interpreted by the kernel as success and future calls to `open` and `release` will + /// be handled by the kernel without being passed on to the file system. + fn open( + &self, + ctx: Context, + inode: Self::Inode, + kill_priv: bool, + flags: u32, + ) -> io::Result<(Option, OpenOptions)> { + // Matches the behavior of libfuse. + Ok((None, OpenOptions::empty())) + } + + /// Create and open a file. + /// + /// If the file does not already exist, the file system should create it with the specified + /// `mode`. When the `FsOptions::DONT_MASK` feature is set, the file system is responsible for + /// setting the permissions of the created file to `mode & !umask`. + /// + /// If `kill_priv` is true then it indicates that the file system is expected to clear the + /// setuid and setgid bits. + /// + /// If the file system returns an `ENOSYS` error, then the kernel will treat this method as + /// unimplemented and all future calls to `create` will be handled by calling the `mknod` and + /// `open` methods instead. + /// + /// See the documentation for the `open` method for more information about opening the file. In + /// addition to the optional `Handle` and the `OpenOptions`, the file system must also return an + /// `Entry` for the file. This increases the lookup count for the `Inode` associated with the + /// file by 1. + #[allow(clippy::too_many_arguments)] + fn create( + &self, + ctx: Context, + parent: Self::Inode, + name: &CStr, + mode: u32, + kill_priv: bool, + flags: u32, + umask: u32, + extensions: Extensions, + ) -> io::Result<(Entry, Option, OpenOptions)> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Read data from a file. + /// + /// Returns `size` bytes of data starting from offset `off` from the file associated with + /// `inode` or `handle`. + /// + /// `flags` contains the flags used to open the file. Similarly, `handle` is the `Handle` + /// returned by the file system from the `open` method, if any. If the file system + /// implementation did not return a `Handle` from `open` then the contents of `handle` are + /// undefined. + /// + /// This method should return exactly the number of bytes requested by the kernel, except in the + /// case of error or EOF. Otherwise, the kernel will substitute the rest of the data with + /// zeroes. An exception to this rule is if the file was opened with the "direct I/O" option + /// (`libc::O_DIRECT`), in which case the kernel will forward the return code from this method + /// to the userspace application that made the system call. + #[allow(clippy::too_many_arguments)] + fn read( + &self, + ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + w: W, + size: u32, + offset: u64, + lock_owner: Option, + flags: u32, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Write data to a file. + /// + /// Writes `size` bytes of data starting from offset `off` to the file associated with `inode` + /// or `handle`. + /// + /// `flags` contains the flags used to open the file. Similarly, `handle` is the `Handle` + /// returned by the file system from the `open` method, if any. If the file system + /// implementation did not return a `Handle` from `open` then the contents of `handle` are + /// undefined. + /// + /// If `delayed_write` is true then it indicates that this is a write for buffered data. + /// + /// If `kill_priv` is true then it indicates that the file system is expected to clear the + /// setuid and setgid bits. + /// + /// This method should return exactly the number of bytes requested by the kernel, except in the + /// case of error. An exception to this rule is if the file was opened with the "direct I/O" + /// option (`libc::O_DIRECT`), in which case the kernel will forward the return code from this + /// method to the userspace application that made the system call. + #[allow(clippy::too_many_arguments)] + fn write( + &self, + ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + r: R, + size: u32, + offset: u64, + lock_owner: Option, + delayed_write: bool, + kill_priv: bool, + flags: u32, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Flush the contents of a file. + /// + /// This method is called on every `close()` of a file descriptor. Since it is possible to + /// duplicate file descriptors there may be many `flush` calls for one call to `open`. + /// + /// File systems should not make any assumptions about when `flush` will be + /// called or even if it will be called at all. + /// + /// `handle` is the `Handle` returned by the file system from the `open` method, if any. If the + /// file system did not return a `Handle` from `open` then the contents of `handle` are + /// undefined. + /// + /// Unlike `fsync`, the file system is not required to flush pending writes. One reason to flush + /// data is if the file system wants to return write errors during close. However, this is not + /// portable because POSIX does not require `close` to wait for delayed I/O to complete. + /// + /// If the `FsOptions::POSIX_LOCKS` feature is enabled, then the file system must remove all + /// locks belonging to `lock_owner`. + /// + /// If this method returns an `ENOSYS` error then the kernel will treat it as success and all + /// subsequent calls to `flush` will be handled by the kernel without being forwarded to the + /// file system. + fn flush( + &self, + ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + lock_owner: u64, + ) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Synchronize file contents. + /// + /// File systems must ensure that the file contents have been flushed to disk before returning + /// from this method. If `datasync` is true then only the file data (but not the metadata) needs + /// to be flushed. + /// + /// `handle` is the `Handle` returned by the file system from the `open` method, if any. If the + /// file system did not return a `Handle` from `open` then the contents of + /// `handle` are undefined. + /// + /// If this method returns an `ENOSYS` error then the kernel will treat it as success and all + /// subsequent calls to `fsync` will be handled by the kernel without being forwarded to the + /// file system. + fn fsync( + &self, + ctx: Context, + inode: Self::Inode, + datasync: bool, + handle: Self::Handle, + ) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Allocate requested space for file data. + /// + /// If this function returns success, then the file system must guarantee that it is possible to + /// write up to `length` bytes of data starting at `offset` without failing due to a lack of + /// free space on the disk. + /// + /// `handle` is the `Handle` returned by the file system from the `open` method, if any. If the + /// file system did not return a `Handle` from `open` then the contents of `handle` are + /// undefined. + /// + /// If this method returns an `ENOSYS` error then the kernel will treat that as a permanent + /// failure: all future calls to `fallocate` will fail with `EOPNOTSUPP` without being forwarded + /// to the file system. + fn fallocate( + &self, + ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + mode: u32, + offset: u64, + length: u64, + ) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Release an open file. + /// + /// This method is called when there are no more references to an open file: all file + /// descriptors are closed and all memory mappings are unmapped. + /// + /// For every `open` call there will be exactly one `release` call (unless the file system is + /// force-unmounted). + /// + /// The file system may reply with an error, but error values are not returned to the `close()` + /// or `munmap()` which triggered the release. + /// + /// `handle` is the `Handle` returned by the file system from the `open` method, if any. If the + /// file system did not return a `Handle` from `open` then the contents of + /// `handle` are undefined. + /// + /// If `flush` is `true` then the contents of the file should also be flushed to disk. + #[allow(clippy::too_many_arguments)] + fn release( + &self, + ctx: Context, + inode: Self::Inode, + flags: u32, + handle: Self::Handle, + flush: bool, + flock_release: bool, + lock_owner: Option, + ) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Get information about the file system. + fn statfs(&self, ctx: Context, inode: Self::Inode) -> io::Result { + // Safe because we are zero-initializing a struct with only POD fields. + let mut st: libc::statvfs64 = unsafe { mem::zeroed() }; + + // This matches the behavior of libfuse as it returns these values if the + // filesystem doesn't implement this method. + st.f_namemax = 255; + st.f_bsize = 512; + + Ok(st) + } + + /// Set an extended attribute. + /// + /// If this method fails with an `ENOSYS` error, then the kernel will treat that as a permanent + /// failure. The kernel will return `EOPNOTSUPP` for all future calls to `setxattr` without + /// forwarding them to the file system. + /// + /// Valid values for flags are the same as those accepted by the `setxattr(2)` system call and + /// have the same behavior. + fn setxattr( + &self, + ctx: Context, + inode: Self::Inode, + name: &CStr, + value: &[u8], + flags: u32, + extra_flags: SetxattrFlags, + ) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Get an extended attribute. + /// + /// If `size` is 0, then the file system should respond with `GetxattrReply::Count` and the + /// number of bytes needed to hold the value. If `size` is large enough to hold the value, then + /// the file system should reply with `GetxattrReply::Value` and the value of the extended + /// attribute. If `size` is not 0 but is also not large enough to hold the value, then the file + /// system should reply with an `ERANGE` error. + /// + /// If this method fails with an `ENOSYS` error, then the kernel will treat that as a permanent + /// failure. The kernel will return `EOPNOTSUPP` for all future calls to `getxattr` without + /// forwarding them to the file system. + fn getxattr( + &self, + ctx: Context, + inode: Self::Inode, + name: &CStr, + size: u32, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// List extended attribute names. + /// + /// If `size` is 0, then the file system should respond with `ListxattrReply::Count` and the + /// number of bytes needed to hold a `\0` byte separated list of the names of all the extended + /// attributes. If `size` is large enough to hold the `\0` byte separated list of the attribute + /// names, then the file system should reply with `ListxattrReply::Names` and the list. If + /// `size` is not 0 but is also not large enough to hold the list, then the file system should + /// reply with an `ERANGE` error. + /// + /// If this method fails with an `ENOSYS` error, then the kernel will treat that as a permanent + /// failure. The kernel will return `EOPNOTSUPP` for all future calls to `listxattr` without + /// forwarding them to the file system. + fn listxattr(&self, ctx: Context, inode: Self::Inode, size: u32) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Remove an extended attribute. + /// + /// If this method fails with an `ENOSYS` error, then the kernel will treat that as a permanent + /// failure. The kernel will return `EOPNOTSUPP` for all future calls to `removexattr` without + /// forwarding them to the file system. + fn removexattr(&self, ctx: Context, inode: Self::Inode, name: &CStr) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Open a directory for reading. + /// + /// The file system may choose to return a `Handle` to refer to the newly opened directory. The + /// kernel will then use this `Handle` for all operations on the content of the directory + /// (`readdir`, `readdirplus`, `fsyncdir`, `releasedir`). If the file system does not return a + /// `Handle` then the kernel will use the `Inode` for the directory to operate on its contents. + /// In this case the file system may wish to enable the `FsOptions::ZERO_MESSAGE_OPENDIR` + /// feature if it is supported by the kernel (see below). + /// + /// The returned `OpenOptions` allow the file system to change the way the opened directory is + /// handled by the kernel. See the documentation of `OpenOptions` for more information. + /// + /// If the `FsOptions::ZERO_MESSAGE_OPENDIR` feature is enabled by both the file system + /// implementation and the kernel, then the file system may return an error of `ENOSYS`. This + /// will be interpreted by the kernel as success and future calls to `opendir` and `releasedir` + /// will be handled by the kernel without being passed on to the file system. + fn opendir( + &self, + ctx: Context, + inode: Self::Inode, + flags: u32, + ) -> io::Result<(Option, OpenOptions)> { + // Matches the behavior of libfuse. + Ok((None, OpenOptions::empty())) + } + + /// Read a directory. + /// + /// `handle` is the `Handle` returned by the file system from the `opendir` method, if any. If + /// the file system did not return a `Handle` from `opendir` then the contents of `handle` are + /// undefined. + /// + /// `size` indicates the maximum number of bytes that should be returned by this method. + /// + /// If `offset` is non-zero then it corresponds to one of the `offset` values from a `DirEntry` + /// that was previously returned by a call to `readdir` for the same handle. In this case the + /// file system should skip over the entries before the position defined by the `offset` value. + /// If entries were added or removed while the `Handle` is open then the file system may still + /// include removed entries or skip newly created entries. However, adding or removing entries + /// should never cause the file system to skip over unrelated entries or include an entry more + /// than once. This means that `offset` cannot be a simple index and must include sufficient + /// information to uniquely determine the next entry in the list even when the set of entries is + /// being changed. + /// + /// The file system may return entries for the current directory (".") and parent directory + /// ("..") but is not required to do so. If the file system does not return these entries, then + /// they are implicitly added by the kernel. + /// + /// The lookup count for `Inode`s associated with the returned directory entries is **NOT** + /// affected by this method. + /// + fn readdir( + &self, + ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + size: u32, + offset: u64, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Synchronize the contents of a directory. + /// + /// File systems must ensure that the directory contents have been flushed to disk before + /// returning from this method. If `datasync` is true then only the directory data (but not the + /// metadata) needs to be flushed. + /// + /// `handle` is the `Handle` returned by the file system from the `opendir` method, if any. If + /// the file system did not return a `Handle` from `opendir` then the contents of + /// `handle` are undefined. + /// + /// If this method returns an `ENOSYS` error then the kernel will treat it as success and all + /// subsequent calls to `fsyncdir` will be handled by the kernel without being forwarded to the + /// file system. + fn fsyncdir( + &self, + ctx: Context, + inode: Self::Inode, + datasync: bool, + handle: Self::Handle, + ) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Release an open directory. + /// + /// For every `opendir` call there will be exactly one `releasedir` call (unless the file system + /// is force-unmounted). + /// + /// `handle` is the `Handle` returned by the file system from the `opendir` method, if any. If + /// the file system did not return a `Handle` from `opendir` then the contents of `handle` are + /// undefined. + /// + /// `flags` contains used the flags used to open the directory in `opendir`. + fn releasedir( + &self, + ctx: Context, + inode: Self::Inode, + flags: u32, + handle: Self::Handle, + ) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Setup a mapping so that guest can access files in DAX style. + #[allow(clippy::too_many_arguments)] + fn setupmapping( + &self, + _ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + foffset: u64, + len: u64, + flags: u64, + moffset: u64, + vu_req: &mut T, + ) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + fn removemapping( + &self, + _ctx: Context, + requests: Vec, + vu_req: &mut T, + ) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Check file access permissions. + /// + /// This method is called when a userspace process in the client makes an `access()` or + /// `chdir()` system call. If the file system was mounted with the `-o default_permissions` + /// mount option, then the kernel will perform these checks itself and this method will not be + /// called. + /// + /// If this method returns an `ENOSYS` error, then the kernel will treat it as a permanent + /// success: all future calls to `access` will return success without being forwarded to the + /// file system. + fn access(&self, ctx: Context, inode: Self::Inode, mask: u32) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Reposition read/write file offset. + fn lseek( + &self, + ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + offset: u64, + whence: u32, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + #[allow(clippy::too_many_arguments)] + fn copyfilerange( + &self, + ctx: Context, + inode_in: Self::Inode, + handle_in: Self::Handle, + offset_in: u64, + inode_out: Self::Inode, + handle_out: Self::Handle, + offset_out: u64, + len: u64, + flags: u64, + ) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Synchronize the filesystem containing the file referenced by `inode`. When running with + /// --announce-submounts, `syncfs` is called once per submount that is to be synced. When + /// running without--announce-submounts, `syncfs` is called on the root mount, but all submounts + /// need to be synced, too. + fn syncfs(&self, _ctx: Context, inode: Self::Inode) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// TODO: support this + fn getlk(&self) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// TODO: support this + fn setlk(&self) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// TODO: support this + fn setlkw(&self) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// TODO: support this + fn ioctl(&self) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// TODO: support this + fn bmap(&self) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// TODO: support this + fn poll(&self) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// TODO: support this + fn notify_reply(&self) -> io::Result<()> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// TODO: support this + fn tmpfile(&self) -> io::Result<(Entry, Option, OpenOptions)> { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + fn get_root_ino(&self) -> io::Result { + Err(io::Error::from_raw_os_error(libc::ENOSYS)) + } + + /// Whether the filesystem has `readdirplus` support enabled. + /// + /// When this returns `false`, the server should reject any `readdirplus` FUSE request + /// (returning an empty reply) to guard against guests that bypass the negotiated + /// capabilities and issue forged `readdirplus` requests directly. + fn readdirplus_enabled(&self) -> bool { + false + } +} + +/// Allow filesystem's state to be serialized for migration. +pub trait SerializableFileSystem { + /// Prepare serialization of the filesystem state. + /// + /// Called once migration is initiated. If serialization of the filesystem state takes time, + /// this allows starting serialization now, so that `serialize()` has less work to do and can + /// finish quickly. + /// + /// This function is generally run in a separate thread, so is allowed to block and take time + /// to complete. It should regularly check the value of the `cancel` bool, though, and if it + /// becomes set, cancel the preparation and return as soon as reasonably possible. + fn prepare_serialization(&self, _cancel: Arc) -> io::Result<()> { + Err(io::Error::new( + io::ErrorKind::Unsupported, + "State serialization not supported", + )) + } + + /// Serialize the filesystem state. + /// + /// Called during migration on the source side, when the vhost front-end requests this state in + /// order to put it into its migration stream and transfer it to the destination. + /// + /// This function should finish quickly and not perform complex tasks. If such tasks are + /// necessary, they should be started by `prepare_serialization()` so they can be done in time + /// for `serialize()`. + fn serialize(&self, _state_pipe: File) -> io::Result<()> { + Err(io::Error::new( + io::ErrorKind::Unsupported, + "State serialization not supported", + )) + } + + /// Deserialize the filesystem state, and apply it. + /// + /// Called during migration on the destination side, when the vhost front-end has received this + /// state from the source. We should deserialize and apply it, so we can resume where the + /// source has left off. + /// + /// This function should finish quickly. Any complex tasks should be deferred, if possible. + fn deserialize_and_apply(&self, _state_pipe: File) -> io::Result<()> { + Err(io::Error::new( + io::ErrorKind::Unsupported, + "State deserialization not supported", + )) + } + + fn serialize_data(&self) -> io::Result> { + Err(io::Error::new( + io::ErrorKind::Unsupported, + "State serialization not supported", + )) + } + + fn deserialize_and_apply_data(&self, _serialized: &Vec) -> io::Result<()> { + Err(io::Error::new( + io::ErrorKind::Unsupported, + "State deserialization data not supported", + )) + } + + /// Destination-only: map filter inode basename → host path to open at restore. + /// + /// `Server` fills this from the current `FilterList` before applying a + /// migration blob. Default is a no-op so backends that do not implement + /// filter restore ignore the table. + fn set_filter_path_remap(&self, _remap: std::collections::HashMap) {} +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/fs_cache_req_handler.rs b/vendor/cube-hypervisor/virtiofsd/src/fs_cache_req_handler.rs new file mode 100644 index 0000000..89b1cb9 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/fs_cache_req_handler.rs @@ -0,0 +1,62 @@ +use crate::fuse; +use std::io; +use std::os::unix::io::RawFd; +use vhost::vhost_user::message::{ + VhostUserFSBackendMsg, VhostUserFSBackendMsgFlags, VHOST_USER_FS_BACKEND_ENTRIES, +}; +use vhost::vhost_user::{Backend, VhostUserFrontendReqHandler}; + +/// Trait for virtio-fs cache requests operations. This is mainly used to hide +/// vhost-user details from virtio-fs's fuse part. +pub trait FsCacheReqHandler: Send + Sync + 'static { + /// Setup a dedicated mapping so that guest can access file data in DAX style. + fn map( + &mut self, + foffset: u64, + moffset: u64, + len: u64, + flags: u64, + fd: RawFd, + ) -> io::Result<()>; + + /// Remove those mappings that provide the access to file data. + fn unmap(&mut self, requests: Vec) -> io::Result<()>; +} + +impl FsCacheReqHandler for Backend { + fn map( + &mut self, + foffset: u64, + moffset: u64, + len: u64, + flags: u64, + fd: RawFd, + ) -> io::Result<()> { + let mut msg: VhostUserFSBackendMsg = Default::default(); + msg.fd_offset[0] = foffset; + msg.cache_offset[0] = moffset; + msg.len[0] = len; + msg.flags[0] = if (flags & fuse::SetupmappingFlags::WRITE.bits()) != 0 { + VhostUserFSBackendMsgFlags::MAP_W | VhostUserFSBackendMsgFlags::MAP_R + } else { + VhostUserFSBackendMsgFlags::MAP_R + }; + + self.fs_backend_map(&msg, &fd)?; + Ok(()) + } + + fn unmap(&mut self, requests: Vec) -> io::Result<()> { + for chunk in requests.chunks(VHOST_USER_FS_BACKEND_ENTRIES) { + let mut msg: VhostUserFSBackendMsg = Default::default(); + + for (ind, req) in chunk.iter().enumerate() { + msg.len[ind] = req.len; + msg.cache_offset[ind] = req.moffset; + } + + self.fs_backend_unmap(&msg)?; + } + Ok(()) + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/fuse.rs b/vendor/cube-hypervisor/virtiofsd/src/fuse.rs new file mode 100644 index 0000000..a8f65e9 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/fuse.rs @@ -0,0 +1,1418 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use std::convert::TryFrom; +use std::mem; + +use crate::macros::enum_value; +use bitflags::bitflags; +use vm_memory::ByteValued; + +/// Version number of this interface. +pub const KERNEL_VERSION: u32 = 7; + +/// Minor version number of this interface. +pub const KERNEL_MINOR_VERSION: u32 = 38; + +/// Minimum Minor version number supported. If client sends a minor +/// number lesser than this, we don't support it. +pub const MIN_KERNEL_MINOR_VERSION: u32 = 27; + +/// The ID of the inode corresponding to the root directory of the file system. +pub const ROOT_ID: u64 = 1; + +// Bitmasks for `fuse_setattr_in.valid`. +const FATTR_MODE: u32 = 1 << 0; +const FATTR_UID: u32 = 1 << 1; +const FATTR_GID: u32 = 1 << 2; +const FATTR_SIZE: u32 = 1 << 3; +const FATTR_ATIME: u32 = 1 << 4; +const FATTR_MTIME: u32 = 1 << 5; +pub const FATTR_FH: u32 = 1 << 6; +const FATTR_ATIME_NOW: u32 = 1 << 7; +const FATTR_MTIME_NOW: u32 = 1 << 8; +pub const FATTR_LOCKOWNER: u32 = 1 << 9; +const FATTR_CTIME: u32 = 1 << 10; +const FATTR_KILL_SUIDGID: u32 = 1 << 11; + +bitflags! { + pub struct SetattrValid: u32 { + const MODE = FATTR_MODE; + const UID = FATTR_UID; + const GID = FATTR_GID; + const SIZE = FATTR_SIZE; + const ATIME = FATTR_ATIME; + const MTIME = FATTR_MTIME; + const ATIME_NOW = FATTR_ATIME_NOW; + const MTIME_NOW = FATTR_MTIME_NOW; + const CTIME = FATTR_CTIME; + const KILL_SUIDGID = FATTR_KILL_SUIDGID; + } +} + +// Flags returned by the OPEN request. + +/// Bypass page cache for this open file. +const FOPEN_DIRECT_IO: u32 = 1 << 0; + +/// Don't invalidate the data cache on open. +const FOPEN_KEEP_CACHE: u32 = 1 << 1; + +/// The file is not seekable. +const FOPEN_NONSEEKABLE: u32 = 1 << 2; + +/// Allow caching this directory. +const FOPEN_CACHE_DIR: u32 = 1 << 3; + +/// The file is stream-like (no file position at all). +#[allow(dead_code)] +const FOPEN_STREAM: u32 = 1 << 4; + +/// Don't flush data cache on close (unless FUSE_WRITEBACK_CACHE) +const FOPEN_NOFLUSH: u32 = 1 << 5; + +/// Allow concurrent direct writes on the same inode +const FOPEN_PARALLEL_DIRECT_WRITES: u32 = 1 << 6; + +bitflags! { + /// Options controlling the behavior of files opened by the server in response + /// to an open or create request. + pub struct OpenOptions: u32 { + const DIRECT_IO = FOPEN_DIRECT_IO; + const KEEP_CACHE = FOPEN_KEEP_CACHE; + const NONSEEKABLE = FOPEN_NONSEEKABLE; + const CACHE_DIR = FOPEN_CACHE_DIR; + const STREAM = FOPEN_CACHE_DIR; + const NOFLUSH = FOPEN_NOFLUSH; + const PARALLEL_DIRECT_WRITES = FOPEN_PARALLEL_DIRECT_WRITES; + } +} + +// INIT request/reply flags. + +/// Asynchronous read requests. +const ASYNC_READ: u64 = 1 << 0; + +/// Remote locking for POSIX file locks. +const POSIX_LOCKS: u64 = 1 << 1; + +/// Kernel sends file handle for fstat, etc... (not yet supported). +const FILE_OPS: u64 = 1 << 2; + +/// Handles the O_TRUNC open flag in the filesystem. +const ATOMIC_O_TRUNC: u64 = 1 << 3; + +/// FileSystem handles lookups of "." and "..". +const EXPORT_SUPPORT: u64 = 1 << 4; + +/// FileSystem can handle write size larger than 4kB. +const BIG_WRITES: u64 = 1 << 5; + +/// Don't apply umask to file mode on create operations. +const DONT_MASK: u64 = 1 << 6; + +/// Kernel supports splice write on the device. +const SPLICE_WRITE: u64 = 1 << 7; + +/// Kernel supports splice move on the device. +const SPLICE_MOVE: u64 = 1 << 8; + +/// Kernel supports splice read on the device. +const SPLICE_READ: u64 = 1 << 9; + +/// Remote locking for BSD style file locks. +const FLOCK_LOCKS: u64 = 1 << 10; + +/// Kernel supports ioctl on directories. +const HAS_IOCTL_DIR: u64 = 1 << 11; + +/// Automatically invalidate cached pages. +const AUTO_INVAL_DATA: u64 = 1 << 12; + +/// Do READDIRPLUS (READDIR+LOOKUP in one). +const DO_READDIRPLUS: u64 = 1 << 13; + +/// Adaptive readdirplus. +const READDIRPLUS_AUTO: u64 = 1 << 14; + +/// Asynchronous direct I/O submission. +const ASYNC_DIO: u64 = 1 << 15; + +/// Use writeback cache for buffered writes. +const WRITEBACK_CACHE: u64 = 1 << 16; + +/// Kernel supports zero-message opens. +const NO_OPEN_SUPPORT: u64 = 1 << 17; + +/// Allow parallel lookups and readdir. +const PARALLEL_DIROPS: u64 = 1 << 18; + +/// Fs handles killing suid/sgid/cap on write/chown/trunc. +const HANDLE_KILLPRIV: u64 = 1 << 19; + +/// FileSystem supports posix acls. +const POSIX_ACL: u64 = 1 << 20; + +/// Reading the device after abort returns ECONNABORTED. +const ABORT_ERROR: u64 = 1 << 21; + +/// Init_out.max_pages contains the max number of req pages. +const MAX_PAGES: u64 = 1 << 22; + +/// Cache READLINK responses +const CACHE_SYMLINKS: u64 = 1 << 23; + +/// Kernel supports zero-message opendir +const NO_OPENDIR_SUPPORT: u64 = 1 << 24; + +/// Only invalidate cached pages on explicit request +const EXPLICIT_INVAL_DATA: u64 = 1 << 25; + +/// init_out.map_alignment contains log2(byte alignment) for +/// foffset and moffset fields in struct fuse_setupmapping_out and +/// fuse_removemapping_one +#[allow(dead_code)] +const MAP_ALIGNMENT: u64 = 1 << 26; + +/// Kernel supports auto-mounting directory submounts +const SUBMOUNTS: u64 = 1 << 27; + +/// Fs handles killing suid/sgid/cap on write/chown/trunc (v2). +const HANDLE_KILLPRIV_V2: u64 = 1 << 28; + +/// Server supports extended struct SetxattrIn +const SETXATTR_EXT: u64 = 1 << 29; + +/// Extended fuse_init_in request +const INIT_EXT: u64 = 1 << 30; + +/// Reserved. Do not use. +const INIT_RESERVED: u64 = 1 << 31; + +/// Add security context to create, mkdir, symlink, and mknod +const SECURITY_CTX: u64 = 1 << 32; + +/// Use per inode DAX +const HAS_INODE_DAX: u64 = 1 << 33; + +/// Add supplementary groups info to create, mkdir, symlink +/// and mknod (single group that matches parent) +const CREATE_SUPP_GROUP: u64 = 1 << 34; + +/// Relax restrictions in FOPEN_DIRECT_IO mode to allow shared mmap. +const DIRECT_IO_ALLOW_MMAP: u64 = 1 << 36; + +bitflags! { + /// A bitfield passed in as a parameter to and returned from the `init` method of the + /// `FileSystem` trait. + pub struct FsOptions: u64 { + /// Indicates that the filesystem supports asynchronous read requests. + /// + /// If this capability is not requested/available, the kernel will ensure that there is at + /// most one pending read request per file-handle at any time, and will attempt to order + /// read requests by increasing offset. + /// + /// This feature is enabled by default when supported by the kernel. + const ASYNC_READ = ASYNC_READ; + + /// Indicates that the filesystem supports "remote" locking. + /// + /// This feature is not enabled by default and should only be set if the filesystem + /// implements the `getlk` and `setlk` methods of the `FileSystem` trait. + const POSIX_LOCKS = POSIX_LOCKS; + + /// Kernel sends file handle for fstat, etc... (not yet supported). + const FILE_OPS = FILE_OPS; + + /// Indicates that the filesystem supports the `O_TRUNC` open flag. If disabled, and an + /// application specifies `O_TRUNC`, fuse first calls `setattr` to truncate the file and + /// then calls `open` with `O_TRUNC` filtered out. + /// + /// This feature is enabled by default when supported by the kernel. + const ATOMIC_O_TRUNC = ATOMIC_O_TRUNC; + + /// Indicates that the filesystem supports lookups of "." and "..". + /// + /// This feature is disabled by default. + const EXPORT_SUPPORT = EXPORT_SUPPORT; + + /// FileSystem can handle write size larger than 4kB. + const BIG_WRITES = BIG_WRITES; + + /// Indicates that the kernel should not apply the umask to the file mode on create + /// operations. + /// + /// This feature is disabled by default. + const DONT_MASK = DONT_MASK; + + /// Indicates that the server should try to use `splice(2)` when writing to the fuse device. + /// This may improve performance. + /// + /// This feature is not currently supported. + const SPLICE_WRITE = SPLICE_WRITE; + + /// Indicates that the server should try to move pages instead of copying when writing to / + /// reading from the fuse device. This may improve performance. + /// + /// This feature is not currently supported. + const SPLICE_MOVE = SPLICE_MOVE; + + /// Indicates that the server should try to use `splice(2)` when reading from the fuse + /// device. This may improve performance. + /// + /// This feature is not currently supported. + const SPLICE_READ = SPLICE_READ; + + /// If set, then calls to `flock` will be emulated using POSIX locks and must + /// then be handled by the filesystem's `setlock()` handler. + /// + /// If not set, `flock` calls will be handled by the FUSE kernel module internally (so any + /// access that does not go through the kernel cannot be taken into account). + /// + /// This feature is disabled by default. + const FLOCK_LOCKS = FLOCK_LOCKS; + + /// Indicates that the filesystem supports ioctl's on directories. + /// + /// This feature is enabled by default when supported by the kernel. + const HAS_IOCTL_DIR = HAS_IOCTL_DIR; + + /// Traditionally, while a file is open the FUSE kernel module only asks the filesystem for + /// an update of the file's attributes when a client attempts to read beyond EOF. This is + /// unsuitable for e.g. network filesystems, where the file contents may change without the + /// kernel knowing about it. + /// + /// If this flag is set, FUSE will check the validity of the attributes on every read. If + /// the attributes are no longer valid (i.e., if the *attribute* timeout has expired) then + /// FUSE will first send another `getattr` request. If the new mtime differs from the + /// previous value, any cached file *contents* will be invalidated as well. + /// + /// This flag should always be set when available. If all file changes go through the + /// kernel, *attribute* validity should be set to a very large number to avoid unnecessary + /// `getattr()` calls. + /// + /// This feature is enabled by default when supported by the kernel. + const AUTO_INVAL_DATA = AUTO_INVAL_DATA; + + /// Indicates that the filesystem supports readdirplus. + /// + /// The feature is not enabled by default and should only be set if the filesystem + /// implements the `readdirplus` method of the `FileSystem` trait. + const DO_READDIRPLUS = DO_READDIRPLUS; + + /// Indicates that the filesystem supports adaptive readdirplus. + /// + /// If `DO_READDIRPLUS` is not set, this flag has no effect. + /// + /// If `DO_READDIRPLUS` is set and this flag is not set, the kernel will always issue + /// `readdirplus()` requests to retrieve directory contents. + /// + /// If `DO_READDIRPLUS` is set and this flag is set, the kernel will issue both `readdir()` + /// and `readdirplus()` requests, depending on how much information is expected to be + /// required. + /// + /// This feature is not enabled by default and should only be set if the file system + /// implements both the `readdir` and `readdirplus` methods of the `FileSystem` trait. + const READDIRPLUS_AUTO = READDIRPLUS_AUTO; + + /// Indicates that the filesystem supports asynchronous direct I/O submission. + /// + /// If this capability is not requested/available, the kernel will ensure that there is at + /// most one pending read and one pending write request per direct I/O file-handle at any + /// time. + /// + /// This feature is enabled by default when supported by the kernel. + const ASYNC_DIO = ASYNC_DIO; + + /// Indicates that writeback caching should be enabled. This means that individual write + /// request may be buffered and merged in the kernel before they are sent to the file + /// system. + /// + /// This feature is disabled by default. + const WRITEBACK_CACHE = WRITEBACK_CACHE; + + /// Indicates support for zero-message opens. If this flag is set in the `capable` parameter + /// of the `init` trait method, then the file system may return `ENOSYS` from the open() handler + /// to indicate success. Further attempts to open files will be handled in the kernel. (If + /// this flag is not set, returning ENOSYS will be treated as an error and signaled to the + /// caller). + /// + /// Setting (or not setting) the field in the `FsOptions` returned from the `init` method + /// has no effect. + const ZERO_MESSAGE_OPEN = NO_OPEN_SUPPORT; + + /// Indicates support for parallel directory operations. If this flag is unset, the FUSE + /// kernel module will ensure that lookup() and readdir() requests are never issued + /// concurrently for the same directory. + /// + /// This feature is enabled by default when supported by the kernel. + const PARALLEL_DIROPS = PARALLEL_DIROPS; + + /// Indicates that the file system is responsible for unsetting setuid and setgid bits when a + /// file is written, truncated, or its owner is changed. + /// + /// This feature is not currently supported. + const HANDLE_KILLPRIV = HANDLE_KILLPRIV; + + /// Indicates support for POSIX ACLs. + /// + /// If this feature is enabled, the kernel will cache and have responsibility for enforcing + /// ACLs. ACL will be stored as xattrs and passed to userspace, which is responsible for + /// updating the ACLs in the filesystem, keeping the file mode in sync with the ACL, and + /// ensuring inheritance of default ACLs when new filesystem nodes are created. Note that + /// this requires that the file system is able to parse and interpret the xattr + /// representation of ACLs. + /// + /// Enabling this feature implicitly turns on the `default_permissions` mount option (even + /// if it was not passed to mount(2)). + /// + /// This feature is disabled by default. + const POSIX_ACL = POSIX_ACL; + + /// Indicates that if the connection is gone because of sysfs abort, reading from the device + /// will return -ECONNABORTED. + /// + /// This feature is not currently supported. + const ABORT_ERROR = ABORT_ERROR; + + /// Indicates support for negotiating the maximum number of pages supported. + /// + /// If this feature is enabled, we can tell the kernel the maximum number of pages that we + /// support to transfer in a single request. + /// + /// This feature is enabled by default if supported by the kernel. + const MAX_PAGES = MAX_PAGES; + + /// Indicates that the kernel supports caching READLINK responses. + /// + /// This feature is not currently supported. + const CACHE_SYMLINKS = CACHE_SYMLINKS; + + /// Indicates support for zero-message opens. If this flag is set in the `capable` parameter + /// of the `init` trait method, then the file system may return `ENOSYS` from the opendir() handler + /// to indicate success. Further attempts to open directories will be handled in the kernel. (If + /// this flag is not set, returning ENOSYS will be treated as an error and signaled to the + /// caller). + /// + /// Setting (or not setting) the field in the `FsOptions` returned from the `init` method + /// has no effect. + const ZERO_MESSAGE_OPENDIR = NO_OPENDIR_SUPPORT; + + /// Indicates support for explicit data invalidation. If this feature is enabled, the + /// server is fully responsible for data cache invalidation, and the kernel won't + /// invalidate files data cache on size change and only truncate that cache to new size + /// in case the size decreased. + /// + /// This feature is not currently supported. + const EXPLICIT_INVAL_DATA = EXPLICIT_INVAL_DATA; + + /// Indicates that the kernel supports the FUSE_ATTR_SUBMOUNT flag. + /// + /// Setting (or not setting) this flag in the `FsOptions` returned from the `init` method + /// has no effect. + const SUBMOUNTS = SUBMOUNTS; + + /// Indicates that the filesystem is responsible for clearing + /// security.capability xattr and clearing setuid and setgid bits. Following + /// are the rules. + /// - clear "security.capability" on write, truncate and chown unconditionally + /// - clear suid/sgid if following is true. Note, sgid is cleared only if + /// group executable bit is set. + /// o setattr has FATTR_SIZE and FATTR_KILL_SUIDGID set. + /// o setattr has FATTR_UID or FATTR_GID + /// o open has O_TRUNC and FUSE_OPEN_KILL_SUIDGID + /// o create has O_TRUNC and FUSE_OPEN_KILL_SUIDGID flag set. + /// o write has FUSE_WRITE_KILL_SUIDGID + /// + /// This feature is enabled by default if supported by the kernel. + const HANDLE_KILLPRIV_V2 = HANDLE_KILLPRIV_V2; + + /// Server supports extended struct SetxattrIn + const SETXATTR_EXT = SETXATTR_EXT; + + /// Indicates that fuse_init_in structure has been extended and + /// expect extended struct coming in from kernel. + const INIT_EXT = INIT_EXT; + + /// This bit is reserved. Don't use it. + const INIT_RESERVED = INIT_RESERVED; + + /// Indicates that kernel is capable of sending a security + /// context at file creation time (create, mkdir, symlink + /// and mknod). This is expected to be a SELinux security + /// context as of now. + const SECURITY_CTX = SECURITY_CTX; + + /// Indicates that kernel is capable of understanding + /// per inode dax flag sent in response to getattr + /// request. This will allow server to enable to + /// enable dax on selective files. + const HAS_INODE_DAX = HAS_INODE_DAX; + + /// Add supplementary groups info to create, mkdir, symlink + /// and mknod (single group that matches parent). + const CREATE_SUPP_GROUP = CREATE_SUPP_GROUP; + + /// Allow shared mmap'ing of files in DIRECT_IO. + const DIRECT_IO_ALLOW_MMAP = DIRECT_IO_ALLOW_MMAP; + } +} + +// Release flags. +pub const RELEASE_FLUSH: u32 = 1 << 0; +pub const RELEASE_FLOCK_UNLOCK: u32 = 1 << 1; + +// Getattr flags. +pub const GETATTR_FH: u32 = 1 << 0; + +// Lock flags. +pub const LK_FLOCK: u32 = 1 << 0; + +// Write flags. + +/// Delayed write from page cache, file handle is guessed. +pub const WRITE_CACHE: u32 = 1 << 0; + +/// `lock_owner` field is valid. +pub const WRITE_LOCKOWNER: u32 = 1 << 1; + +/// Kill suid and sgid bits +pub const WRITE_KILL_PRIV: u32 = 1 << 2; + +// Read flags. +pub const READ_LOCKOWNER: u32 = 1 << 1; + +// Ioctl flags. + +/// 32bit compat ioctl on 64bit machine +const IOCTL_COMPAT: u32 = 1 << 0; + +/// Not restricted to well-formed ioctls, retry allowed +const IOCTL_UNRESTRICTED: u32 = 1 << 1; + +/// Retry with new iovecs +const IOCTL_RETRY: u32 = 1 << 2; + +/// 32bit ioctl +const IOCTL_32BIT: u32 = 1 << 3; + +/// Is a directory +const IOCTL_DIR: u32 = 1 << 4; + +/// x32 compat ioctl on 64bit machine (64bit time_t) +const IOCTL_COMPAT_X32: u32 = 1 << 5; + +/// Maximum of in_iovecs + out_iovecs +const IOCTL_MAX_IOV: u32 = 256; + +bitflags! { + pub struct IoctlFlags: u32 { + /// 32bit compat ioctl on 64bit machine + const IOCTL_COMPAT = IOCTL_COMPAT; + + /// Not restricted to well-formed ioctls, retry allowed + const IOCTL_UNRESTRICTED = IOCTL_UNRESTRICTED; + + /// Retry with new iovecs + const IOCTL_RETRY = IOCTL_RETRY; + + /// 32bit ioctl + const IOCTL_32BIT = IOCTL_32BIT; + + /// Is a directory + const IOCTL_DIR = IOCTL_DIR; + + /// x32 compat ioctl on 64bit machine (64bit time_t) + const IOCTL_COMPAT_X32 = IOCTL_COMPAT_X32; + + /// Maximum of in_iovecs + out_iovecs + const IOCTL_MAX_IOV = IOCTL_MAX_IOV; + } +} + +/// Request poll notify. +pub const POLL_SCHEDULE_NOTIFY: u32 = 1 << 0; + +/// The read buffer is required to be at least 8k, but may be much larger. +pub const FUSE_MIN_READ_BUFFER: u32 = 8192; + +pub const FUSE_COMPAT_ENTRY_OUT_SIZE: u32 = 120; +pub const FUSE_COMPAT_ATTR_OUT_SIZE: u32 = 96; +pub const FUSE_COMPAT_MKNOD_IN_SIZE: u32 = 8; +pub const FUSE_COMPAT_WRITE_IN_SIZE: u32 = 24; +pub const FUSE_COMPAT_STATFS_SIZE: u32 = 48; +pub const FUSE_COMPAT_INIT_OUT_SIZE: u32 = 8; +pub const FUSE_COMPAT_22_INIT_OUT_SIZE: u32 = 24; +pub const FUSE_COMPAT_SETXATTR_IN_SIZE: u32 = 8; + +// Fsync flags +pub const FSYNC_FDATASYNC: u32 = 1 << 0; + +// Attr.flags flags. + +/// Object is a submount root +pub const ATTR_SUBMOUNT: u32 = 1 << 0; +/// Indicate to kernel to enable DAX for this file in per inode DAX mode +pub const ATTR_DAX: u32 = 1 << 1; + +// Open flags +/// Kill suid and sgid if executable +pub const OPEN_KILL_SUIDGID: u32 = 1 << 0; + +// setxattr flags +/// Clear SGID when system.posix_acl_access is set +const SETXATTR_ACL_KILL_SGID: u32 = 1 << 0; + +bitflags! { + pub struct SetxattrFlags: u32 { + /// Clear SGID when system.posix_acl_access is set + const SETXATTR_ACL_KILL_SGID = SETXATTR_ACL_KILL_SGID; + } +} + +// Message definitions follow. It is safe to implement ByteValued for all of these +// because they are POD types. + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Attr { + pub ino: u64, + pub size: u64, + pub blocks: u64, + pub atime: u64, + pub mtime: u64, + pub ctime: u64, + pub atimensec: u32, + pub mtimensec: u32, + pub ctimensec: u32, + pub mode: u32, + pub nlink: u32, + pub uid: u32, + pub gid: u32, + pub rdev: u32, + pub blksize: u32, + pub flags: u32, +} +unsafe impl ByteValued for Attr {} + +impl From for Attr { + fn from(st: libc::stat64) -> Attr { + Attr::with_flags(st, 0) + } +} + +impl Attr { + pub fn with_flags(st: libc::stat64, flags: u32) -> Attr { + Attr { + ino: st.st_ino, + size: st.st_size as u64, + blocks: st.st_blocks as u64, + atime: st.st_atime as u64, + mtime: st.st_mtime as u64, + ctime: st.st_ctime as u64, + atimensec: st.st_atime_nsec as u32, + mtimensec: st.st_mtime_nsec as u32, + ctimensec: st.st_ctime_nsec as u32, + mode: st.st_mode, + nlink: st.st_nlink as u32, + uid: st.st_uid, + gid: st.st_gid, + rdev: st.st_rdev as u32, + blksize: st.st_blksize as u32, + flags, + } + } +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Kstatfs { + pub blocks: u64, + pub bfree: u64, + pub bavail: u64, + pub files: u64, + pub ffree: u64, + pub bsize: u32, + pub namelen: u32, + pub frsize: u32, + pub padding: u32, + pub spare: [u32; 6], +} +unsafe impl ByteValued for Kstatfs {} + +impl From for Kstatfs { + fn from(st: libc::statvfs64) -> Self { + Kstatfs { + blocks: st.f_blocks, + bfree: st.f_bfree, + bavail: st.f_bavail, + files: st.f_files, + ffree: st.f_ffree, + bsize: st.f_bsize as u32, + namelen: st.f_namemax as u32, + frsize: st.f_frsize as u32, + ..Default::default() + } + } +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct FileLock { + pub start: u64, + pub end: u64, + pub type_: u32, + pub pid: u32, /* tgid */ +} +unsafe impl ByteValued for FileLock {} + +enum_value! { + #[derive(Debug, Copy, Clone)] + pub enum Opcode: u32 { + Lookup = 1, + Forget = 2, /* No Reply */ + Getattr = 3, + Setattr = 4, + Readlink = 5, + Symlink = 6, + Mknod = 8, + Mkdir = 9, + Unlink = 10, + Rmdir = 11, + Rename = 12, + Link = 13, + Open = 14, + Read = 15, + Write = 16, + Statfs = 17, + Release = 18, + Fsync = 20, + Setxattr = 21, + Getxattr = 22, + Listxattr = 23, + Removexattr = 24, + Flush = 25, + Init = 26, + Opendir = 27, + Readdir = 28, + Releasedir = 29, + Fsyncdir = 30, + Getlk = 31, + Setlk = 32, + Setlkw = 33, + Access = 34, + Create = 35, + Interrupt = 36, + Bmap = 37, + Destroy = 38, + Ioctl = 39, + Poll = 40, + NotifyReply = 41, + BatchForget = 42, + Fallocate = 43, + Readdirplus = 44, + Rename2 = 45, + Lseek = 46, + CopyFileRange = 47, + SetupMapping = 48, + RemoveMapping = 49, + Syncfs = 50, + TmpFile = 51, + } +} + +#[repr(u32)] +#[derive(Debug, Copy, Clone)] +pub enum NotifyOpcode { + Poll = 1, + InvalInode = 2, + InvalEntry = 3, + Store = 4, + Retrieve = 5, + Delete = 6, + CodeMax = 7, +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct EntryOut { + pub nodeid: u64, /* Inode ID */ + pub generation: u64, /* Inode generation: nodeid:gen must be unique for the fs's lifetime */ + pub entry_valid: u64, /* Cache timeout for the name */ + pub attr_valid: u64, /* Cache timeout for the attributes */ + pub entry_valid_nsec: u32, + pub attr_valid_nsec: u32, + pub attr: Attr, +} +unsafe impl ByteValued for EntryOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct ForgetIn { + pub nlookup: u64, +} +unsafe impl ByteValued for ForgetIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct ForgetOne { + pub nodeid: u64, + pub nlookup: u64, +} +unsafe impl ByteValued for ForgetOne {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct BatchForgetIn { + pub count: u32, + pub dummy: u32, +} +unsafe impl ByteValued for BatchForgetIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct GetattrIn { + pub flags: u32, + pub dummy: u32, + pub fh: u64, +} +unsafe impl ByteValued for GetattrIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct AttrOut { + pub attr_valid: u64, /* Cache timeout for the attributes */ + pub attr_valid_nsec: u32, + pub dummy: u32, + pub attr: Attr, +} +unsafe impl ByteValued for AttrOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct MknodIn { + pub mode: u32, + pub rdev: u32, + pub umask: u32, + pub padding: u32, +} +unsafe impl ByteValued for MknodIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct MkdirIn { + pub mode: u32, + pub umask: u32, +} +unsafe impl ByteValued for MkdirIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct RenameIn { + pub newdir: u64, +} +unsafe impl ByteValued for RenameIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Rename2In { + pub newdir: u64, + pub flags: u32, + pub padding: u32, +} +unsafe impl ByteValued for Rename2In {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct LinkIn { + pub oldnodeid: u64, +} +unsafe impl ByteValued for LinkIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct SetattrIn { + pub valid: u32, + pub padding: u32, + pub fh: u64, + pub size: u64, + pub lock_owner: u64, + pub atime: u64, + pub mtime: u64, + pub ctime: u64, + pub atimensec: u32, + pub mtimensec: u32, + pub ctimensec: u32, + pub mode: u32, + pub unused4: u32, + pub uid: u32, + pub gid: u32, + pub unused5: u32, +} +unsafe impl ByteValued for SetattrIn {} + +impl From for libc::stat64 { + fn from(sai: SetattrIn) -> libc::stat64 { + let mut out: libc::stat64 = unsafe { mem::zeroed() }; + out.st_mode = sai.mode; + out.st_uid = sai.uid; + out.st_gid = sai.gid; + out.st_size = sai.size as i64; + out.st_atime = sai.atime as i64; + out.st_mtime = sai.mtime as i64; + out.st_ctime = sai.ctime as i64; + out.st_atime_nsec = sai.atimensec.into(); + out.st_mtime_nsec = sai.mtimensec.into(); + out.st_ctime_nsec = sai.ctimensec.into(); + + out + } +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct OpenIn { + pub flags: u32, + pub open_flags: u32, +} +unsafe impl ByteValued for OpenIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct CreateIn { + pub flags: u32, + pub mode: u32, + pub umask: u32, + pub open_flags: u32, +} +unsafe impl ByteValued for CreateIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct OpenOut { + pub fh: u64, + pub open_flags: u32, + pub padding: u32, +} +unsafe impl ByteValued for OpenOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct ReleaseIn { + pub fh: u64, + pub flags: u32, + pub release_flags: u32, + pub lock_owner: u64, +} +unsafe impl ByteValued for ReleaseIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct FlushIn { + pub fh: u64, + pub unused: u32, + pub padding: u32, + pub lock_owner: u64, +} +unsafe impl ByteValued for FlushIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct ReadIn { + pub fh: u64, + pub offset: u64, + pub size: u32, + pub read_flags: u32, + pub lock_owner: u64, + pub flags: u32, + pub padding: u32, +} +unsafe impl ByteValued for ReadIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct WriteIn { + pub fh: u64, + pub offset: u64, + pub size: u32, + pub write_flags: u32, + pub lock_owner: u64, + pub flags: u32, + pub padding: u32, +} +unsafe impl ByteValued for WriteIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct WriteOut { + pub size: u32, + pub padding: u32, +} +unsafe impl ByteValued for WriteOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct StatfsOut { + pub st: Kstatfs, +} +unsafe impl ByteValued for StatfsOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct FsyncIn { + pub fh: u64, + pub fsync_flags: u32, + pub padding: u32, +} +unsafe impl ByteValued for FsyncIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct SetxattrIn { + pub size: u32, + pub flags: u32, + pub setxattr_flags: u32, + pub padding: u32, +} +unsafe impl ByteValued for SetxattrIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct SetxattrInCompat { + pub size: u32, + pub flags: u32, +} +unsafe impl ByteValued for SetxattrInCompat {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct GetxattrIn { + pub size: u32, + pub padding: u32, +} +unsafe impl ByteValued for GetxattrIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct GetxattrOut { + pub size: u32, + pub padding: u32, +} +unsafe impl ByteValued for GetxattrOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct LkIn { + pub fh: u64, + pub owner: u64, + pub lk: FileLock, + pub lk_flags: u32, + pub padding: u32, +} +unsafe impl ByteValued for LkIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct LkOut { + pub lk: FileLock, +} +unsafe impl ByteValued for LkOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct AccessIn { + pub mask: u32, + pub padding: u32, +} +unsafe impl ByteValued for AccessIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct InitInCompat { + pub major: u32, + pub minor: u32, + pub max_readahead: u32, + pub flags: u32, +} +unsafe impl ByteValued for InitInCompat {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct InitInExt { + pub flags2: u32, + pub unused: [u32; 11], +} +unsafe impl ByteValued for InitInExt {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct InitOut { + pub major: u32, + pub minor: u32, + pub max_readahead: u32, + pub flags: u32, + pub max_background: u16, + pub congestion_threshold: u16, + pub max_write: u32, + pub time_gran: u32, + pub max_pages: u16, + pub map_alignment: u16, + pub flags2: u32, + pub unused: [u32; 7], +} +unsafe impl ByteValued for InitOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct InterruptIn { + pub unique: u64, +} +unsafe impl ByteValued for InterruptIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct BmapIn { + pub block: u64, + pub blocksize: u32, + pub padding: u32, +} +unsafe impl ByteValued for BmapIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct BmapOut { + pub block: u64, +} +unsafe impl ByteValued for BmapOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct IoctlIn { + pub fh: u64, + pub flags: u32, + pub cmd: u32, + pub arg: u64, + pub in_size: u32, + pub out_size: u32, +} +unsafe impl ByteValued for IoctlIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct IoctlIovec { + pub base: u64, + pub len: u64, +} +unsafe impl ByteValued for IoctlIovec {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct IoctlOut { + pub result: i32, + pub flags: u32, + pub in_iovs: u32, + pub out_iovs: u32, +} +unsafe impl ByteValued for IoctlOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct PollIn { + pub fh: u64, + pub kh: u64, + pub flags: u32, + pub events: u32, +} +unsafe impl ByteValued for PollIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct PollOut { + pub revents: u32, + pub padding: u32, +} +unsafe impl ByteValued for PollOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct NotifyPollWakeupOut { + pub kh: u64, +} +unsafe impl ByteValued for NotifyPollWakeupOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct FallocateIn { + pub fh: u64, + pub offset: u64, + pub length: u64, + pub mode: u32, + pub padding: u32, +} +unsafe impl ByteValued for FallocateIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct InHeader { + pub len: u32, + pub opcode: u32, + pub unique: u64, + pub nodeid: u64, + pub uid: u32, + pub gid: u32, + pub pid: u32, + pub total_extlen: u16, // length of extensions in 8-byte units + pub padding: u16, +} +unsafe impl ByteValued for InHeader {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct OutHeader { + pub len: u32, + pub error: i32, + pub unique: u64, +} +unsafe impl ByteValued for OutHeader {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Dirent { + pub ino: u64, + pub off: u64, + pub namelen: u32, + pub type_: u32, + // char name[]; +} +unsafe impl ByteValued for Dirent {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Direntplus { + pub entry_out: EntryOut, + pub dirent: Dirent, +} +unsafe impl ByteValued for Direntplus {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct NotifyInvalInodeOut { + pub ino: u64, + pub off: i64, + pub len: i64, +} +unsafe impl ByteValued for NotifyInvalInodeOut {} + +const FUSE_EXPIRE_ONLY: u32 = 1 << 0; +bitflags! { + pub struct NotifyInvalEntryOutFlags: u32 { + const EXPIRE_ONLY = FUSE_EXPIRE_ONLY; + } +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct NotifyInvalEntryOut { + pub parent: u64, + pub namelen: u32, + pub flags: u32, +} +unsafe impl ByteValued for NotifyInvalEntryOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct NotifyDeleteOut { + pub parent: u64, + pub child: u64, + pub namelen: u32, + pub padding: u32, +} +unsafe impl ByteValued for NotifyDeleteOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct NotifyStoreOut { + pub nodeid: u64, + pub offset: u64, + pub size: u32, + pub padding: u32, +} +unsafe impl ByteValued for NotifyStoreOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Notify_Retrieve_Out { + pub notify_unique: u64, + pub nodeid: u64, + pub offset: u64, + pub size: u32, + pub padding: u32, +} +unsafe impl ByteValued for Notify_Retrieve_Out {} + +/* Matches the size of fuse_write_in */ +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct NotifyRetrieveIn { + pub dummy1: u64, + pub offset: u64, + pub size: u32, + pub dummy2: u32, + pub dummy3: u64, + pub dummy4: u64, +} +unsafe impl ByteValued for NotifyRetrieveIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct LseekIn { + pub fh: u64, + pub offset: u64, + pub whence: u32, + pub padding: u32, +} +unsafe impl ByteValued for LseekIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct LseekOut { + pub offset: u64, +} +unsafe impl ByteValued for LseekOut {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct CopyfilerangeIn { + pub fh_in: u64, + pub off_in: u64, + pub nodeid_out: u64, + pub fh_out: u64, + pub off_out: u64, + pub len: u64, + pub flags: u64, +} +unsafe impl ByteValued for CopyfilerangeIn {} + +const SETUPMAPPING_FLAG_WRITE: u64 = 1 << 0; +const SETUPMAPPING_FLAG_READ: u64 = 1 << 1; + +bitflags! { + pub struct SetupmappingFlags: u64 { + const WRITE = SETUPMAPPING_FLAG_WRITE; + const READ = SETUPMAPPING_FLAG_READ; + } +} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct SetupmappingIn { + pub fh: u64, + pub foffset: u64, + pub len: u64, + pub flags: u64, + pub moffset: u64, +} + +unsafe impl ByteValued for SetupmappingIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct RemovemappingIn { + pub count: u32, +} + +unsafe impl ByteValued for RemovemappingIn {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct RemovemappingOne { + pub moffset: u64, + pub len: u64, +} + +unsafe impl ByteValued for RemovemappingOne {} + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct SyncfsIn { + pub padding: u64, +} + +unsafe impl ByteValued for SyncfsIn {} + +/// Extension header +/// `size`: total size of this extension including this header +/// `ext_type`: type of extension +/// This is made compatible with `SecctxHeader` by using type values > `FUSE_MAX_NR_SECCTX` +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct ExtHeader { + pub size: u32, + pub ext_type: u32, +} + +/// Extension types +/// Types `0..MAX_NR_SECCTX` are reserved for `SecCtx` extension for backward compatibility. +const MAX_NR_SECCTX: u32 = 31; // Maximum value of `SecctxHeader::nr_secctx` +const EXT_SUP_GROUPS: u32 = 32; + +unsafe impl ByteValued for ExtHeader {} + +/// Extension type +#[derive(Debug, Copy, Clone)] +pub enum ExtType { + /// Security contexts + SecCtx(u32), + /// `Supplementary groups + SupGroups, +} + +impl TryFrom for ExtType { + type Error = (); + + fn try_from(value: u32) -> Result { + match value { + v if v <= MAX_NR_SECCTX => Ok(Self::SecCtx(value)), + v if v == EXT_SUP_GROUPS => Ok(Self::SupGroups), + _ => Err(()), + } + } +} + +/// For each security context, send `Secctx` with size of security context +/// `Secctx` will be followed by security context name and this in turn +/// will be followed by actual context label. +/// `Secctx`, name, context +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Secctx { + pub size: u32, + pub padding: u32, +} + +unsafe impl ByteValued for Secctx {} + +/// Contains the information about how many `Secctx` structures are being +/// sent and what's the total size of all security contexts (including +/// size of `SecctxHeader`). +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct SecctxHeader { + pub size: u32, + pub nr_secctx: u32, +} + +unsafe impl ByteValued for SecctxHeader {} + +/// Supplementary groups extension +/// `nr_groups`: number of supplementary groups +/// `groups`: flexible array of group IDs +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct SuppGroups { + pub nr_groups: u32, + // uint32_t groups[]; +} + +unsafe impl ByteValued for SuppGroups {} diff --git a/vendor/cube-hypervisor/virtiofsd/src/idmap.rs b/vendor/cube-hypervisor/virtiofsd/src/idmap.rs new file mode 100644 index 0000000..bb27aea --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/idmap.rs @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: BSD-3-Clause +use std::fmt; +use std::num::ParseIntError; +use std::str::FromStr; + +/// Expected error conditions with respect to parsing both UidMap and GidMap +#[derive(Debug, Eq, PartialEq)] +pub enum IdMapError { + /// A delimiter has been found that does not match the delimiter the map started with. + InvalidDelimiter, + /// The map is empty or incorrect number of values are provided. + IncompleteMap, + /// Wraps the cause of parsing an integer failing. + InvalidValue(ParseIntError), +} +impl std::error::Error for IdMapError {} + +impl fmt::Display for IdMapError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + IdMapError::InvalidDelimiter => write!( + f, + "A delimiter has been found that does not match the delimiter the map started with" + ), + IdMapError::IncompleteMap => write!( + f, + "The map is empty or incorrect number of values are provided" + ), + IdMapError::InvalidValue(err) => write!(f, "{}", err), + } + } +} + +impl From for IdMapError { + fn from(err: ParseIntError) -> Self { + IdMapError::InvalidValue(err) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct UidMap { + pub inside_uid: u32, + pub outside_uid: u32, + pub count: u32, +} + +impl FromStr for UidMap { + type Err = IdMapError; + + fn from_str(s: &str) -> Result { + let fields = parse_idmap(s, 3)?; + + Ok(UidMap { + inside_uid: fields[0], + outside_uid: fields[1], + count: fields[2], + }) + } +} + +impl fmt::Display for UidMap { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + ":{}:{}:{}:", + self.inside_uid, self.outside_uid, self.count + ) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct GidMap { + pub inside_gid: u32, + pub outside_gid: u32, + pub count: u32, +} + +impl FromStr for GidMap { + type Err = IdMapError; + + fn from_str(s: &str) -> Result { + let fields = parse_idmap(s, 3)?; + + Ok(GidMap { + inside_gid: fields[0], + outside_gid: fields[1], + count: fields[2], + }) + } +} + +impl fmt::Display for GidMap { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + ":{}:{}:{}:", + self.inside_gid, self.outside_gid, self.count + ) + } +} + +fn parse_idmap(s: &str, expected_len: usize) -> Result, IdMapError> { + let mut s = String::from(s); + let delimiter = s.pop().ok_or(IdMapError::IncompleteMap)?; + if delimiter.is_alphanumeric() { + return Err(IdMapError::InvalidDelimiter); + } + + let values: Vec<&str> = s + .strip_prefix(delimiter) + .ok_or(IdMapError::InvalidDelimiter)? + .split(delimiter) + .collect(); + + if values.len() != expected_len { + return Err(IdMapError::IncompleteMap); + } + + values + .into_iter() + .map(|v| v.parse().map_err(IdMapError::InvalidValue)) + .collect() +} + +#[derive(Debug, Eq, PartialEq)] +#[repr(u8)] +pub(crate) enum IdMapSetUpPipeMessage { + Request = 0x1, + Done = 0x2, +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/lib.rs b/vendor/cube-hypervisor/virtiofsd/src/lib.rs new file mode 100644 index 0000000..a154710 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/lib.rs @@ -0,0 +1,91 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +#[macro_use] +extern crate log; + +pub mod descriptor_utils; +pub mod file_traits; +pub mod filesystem; +pub mod fs_cache_req_handler; +pub mod fuse; +pub mod idmap; +pub mod limits; +pub mod macros; +pub mod oslib; +pub mod passthrough; +pub mod read_dir; +pub mod sandbox; +pub mod seccomp; +pub mod server; +pub mod util; + +use std::ffi::{FromBytesWithNulError, FromVecWithNulError}; +use std::{error, fmt, io}; + +use vm_memory::bitmap::AtomicBitmap; +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + +#[derive(Debug)] +pub enum Error { + /// Failed to decode protocol messages. + DecodeMessage(io::Error), + /// Failed to encode protocol messages. + EncodeMessage(io::Error), + /// Failed to flush protocol messages. + FlushMessage(io::Error), + /// One or more parameters are missing. + MissingParameter, + /// A C string parameter is invalid. + InvalidCString(FromBytesWithNulError), + /// A C string parameter is invalid. + InvalidCString2(FromVecWithNulError), + /// The `len` field of the header is too small. + InvalidHeaderLength, + /// The `size` field of the `SetxattrIn` message does not match the length + /// of the decoded value. + InvalidXattrSize((u32, usize)), + /// One or more extensions are missing. + MissingExtension, + /// ratelimiter reaches its limitation. + RateLimiterReached, + /// ratelimiter overconsumption. + RateLimiterOverConsumption, + /// filter by allow dir. + DisAllowed, + /// invalid filter list. + InvalidFilter(anyhow::Error), + /// internal error. + InternalError(io::Error), +} + +impl error::Error for Error {} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use Error::*; + match self { + DecodeMessage(err) => write!(f, "failed to decode fuse message: {err}"), + EncodeMessage(err) => write!(f, "failed to encode fuse message: {err}"), + FlushMessage(err) => write!(f, "failed to flush fuse message: {err}"), + MissingParameter => write!(f, "one or more parameters are missing"), + InvalidHeaderLength => write!(f, "the `len` field of the header is too small"), + InvalidCString(err) => write!(f, "a c string parameter is invalid: {err}"), + InvalidCString2(err) => write!(f, "a c string parameter is invalid: {err}"), + InvalidXattrSize((size, len)) => write!( + f, + "The `size` field of the `SetxattrIn` message does not match the length of the\ + decoded value: size = {size}, value.len() = {len}" + ), + MissingExtension => write!(f, "one or more extensions are missing"), + RateLimiterReached => write!(f, "bandwidth ratelimiter reaches its capacity"), + RateLimiterOverConsumption => write!(f, "bandwidth ratelimiter over consumed"), + DisAllowed => write!(f, "access is not allowed"), + InvalidFilter(err) => write!(f, "failed to init filter list: {err}"), + InternalError(err) => write!(f, "virtiofs server internal error: {err}"), + } + } +} + +pub type Result = ::std::result::Result; diff --git a/vendor/cube-hypervisor/virtiofsd/src/limits.rs b/vendor/cube-hypervisor/virtiofsd/src/limits.rs new file mode 100644 index 0000000..99ad1e2 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/limits.rs @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: BSD-3-Clause + +use libc::{getrlimit, rlim_t, rlimit, setrlimit, RLIMIT_NOFILE}; +use std::mem::MaybeUninit; +use std::{cmp, fs, io}; + +// Default number of open files (RLIMIT_NOFILE) +const DEFAULT_NOFILE: rlim_t = 1_000_000; + +/// Gets the maximum number of open files. +fn get_max_nofile() -> Result { + let path = "/proc/sys/fs/nr_open"; + let max_str = fs::read_to_string(path).map_err(|error| format!("Reading {path}: {error:?}"))?; + + max_str + .trim() + .parse() + .map_err(|error| format!("Parsing {path}: {error:?}")) +} + +/// Gets the hard limit of open files. +fn get_nofile_limits() -> Result { + let mut limits = MaybeUninit::::zeroed(); + let ret = unsafe { getrlimit(RLIMIT_NOFILE, limits.as_mut_ptr()) }; + if ret != 0 { + return Err(format!("getrlimit: {}", io::Error::last_os_error())); + } + + Ok(unsafe { limits.assume_init() }) +} + +/// Sets the limit of open files to the given value. +fn setup_rlimit_nofile_to(nofile: rlim_t) -> Result<(), String> { + let rlimit = rlimit { + rlim_cur: nofile, + rlim_max: nofile, + }; + let ret = unsafe { setrlimit(RLIMIT_NOFILE, &rlimit) }; + if ret < 0 { + Err(format!( + "Failed to increase the limit: {:?}", + io::Error::last_os_error() + )) + } else { + Ok(()) + } +} + +pub fn setup_rlimit_nofile(nofile: Option) -> Result<(), String> { + let max_nofile = get_max_nofile()?; + let rlimit { rlim_cur, rlim_max } = get_nofile_limits()?; + + let target_limit = if let Some(nofile) = nofile { + if nofile == 0 { + return Ok(()); // '--rlimit-nofile=0' leaves the resource limit unchanged + } + nofile + } else { + if DEFAULT_NOFILE <= rlim_cur { + return Ok(()); // the user has already setup the soft limit higher than the target + } + cmp::min(DEFAULT_NOFILE, max_nofile) + }; + + if target_limit > max_nofile { + return Err(format!("It cannot be increased above {max_nofile}")); + } + + if let Err(error) = setup_rlimit_nofile_to(target_limit) { + if nofile.is_some() { + // Error attempting to setup user-supplied value + return Err(error); + } else { + warn!( + "Failure when trying to set the limit to {}, \ + the hard limit ({}) of open file descriptors is used instead.", + target_limit, rlim_max + ); + setup_rlimit_nofile_to(rlim_max).map_err(|error| { + format!("Cannot increase the soft limit to the hard limit: {error}") + })? + } + } + + Ok(()) +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/macros.rs b/vendor/cube-hypervisor/virtiofsd/src/macros.rs new file mode 100644 index 0000000..f48e9e5 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/macros.rs @@ -0,0 +1,34 @@ +// Copyright 2022 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +macro_rules! enum_value { + ( + $(#[$meta:meta])* + $vis:vis enum $enum:ident: $T:tt { + $( + $(#[$variant_meta:meta])* + $variant:ident $(= $val:expr)?, + )* + } + ) => { + #[repr($T)] + $(#[$meta])* + $vis enum $enum { + $($(#[$variant_meta])* $variant $(= $val)?,)* + } + + impl std::convert::TryFrom<$T> for $enum { + type Error = (); + + fn try_from(v: $T) -> Result { + match v { + $(v if v == $enum::$variant as $T => Ok($enum::$variant),)* + _ => Err(()), + } + } + } + } +} + +pub(crate) use enum_value; diff --git a/vendor/cube-hypervisor/virtiofsd/src/main.rs b/vendor/cube-hypervisor/virtiofsd/src/main.rs new file mode 100644 index 0000000..f9c1d44 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/main.rs @@ -0,0 +1,1462 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// +// SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause) + +use futures::executor::{ThreadPool, ThreadPoolBuilder}; +use libc::EFD_NONBLOCK; +use log::*; +use passthrough::xattrmap::XattrMap; +use std::collections::HashSet; +use std::convert::{self, TryFrom, TryInto}; +use std::ffi::CString; +use std::fs::File; +use std::os::unix::io::{FromRawFd, RawFd}; +use std::path::Path; +use std::str::FromStr; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex, RwLock}; +use std::thread::{self, JoinHandle}; +use std::time::Duration; +use std::{env, error, fmt, io, process}; +use virtiofsd::idmap::{GidMap, UidMap}; + +use clap::{CommandFactory, Parser}; + +use vhost::vhost_user::message::*; +use vhost::vhost_user::Error::Disconnected; +use vhost::vhost_user::{Backend, Listener}; +use vhost_user_backend::bitmap::BitmapMmapRegion; +use vhost_user_backend::Error::HandleRequest; +use vhost_user_backend::{VhostUserBackend, VhostUserDaemon, VringMutex, VringState, VringT}; +use virtio_bindings::bindings::virtio_config::*; +use virtio_bindings::bindings::virtio_ring::{ + VIRTIO_RING_F_EVENT_IDX, VIRTIO_RING_F_INDIRECT_DESC, +}; +use virtio_queue::{DescriptorChain, QueueOwnedT}; +use virtiofsd::descriptor_utils::{Error as VufDescriptorError, Reader, Writer}; +use virtiofsd::filesystem::{FileSystem, SerializableFileSystem}; +use virtiofsd::passthrough::{ + self, CachePolicy, InodeFileHandlesMode, MigrationMode, MigrationOnError, PassthroughFs, +}; +use virtiofsd::sandbox::{Sandbox, SandboxMode}; +use virtiofsd::seccomp::{enable_seccomp, SeccompAction}; +use virtiofsd::server::Server; +use virtiofsd::util::{other_io_error, write_pid_file}; +use virtiofsd::{limits, oslib, Error as VhostUserFsError}; +use vm_memory::{ + ByteValued, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryLoadGuard, GuestMemoryMmap, Le32, +}; +use vmm_sys_util::epoll::EventSet; +use vmm_sys_util::eventfd::EventFd; + +const QUEUE_SIZE: usize = 32768; +// The spec allows for multiple request queues. We currently only support one. +const REQUEST_QUEUES: u32 = 1; +// In addition to the request queue there is one high-prio queue. +// Since VIRTIO_FS_F_NOTIFICATION is not advertised we do not have a +// notification queue. +const NUM_QUEUES: usize = REQUEST_QUEUES as usize + 1; + +type LoggedMemory = GuestMemoryMmap; +type LoggedMemoryAtomic = GuestMemoryAtomic; + +// The guest queued an available buffer for the high priority queue. +const HIPRIO_QUEUE_EVENT: u16 = 0; +// The guest queued an available buffer for the request queue. +const REQ_QUEUE_EVENT: u16 = 1; + +const MAX_TAG_LEN: usize = 36; + +type Result = std::result::Result; +type VhostUserBackendResult = std::result::Result; + +// The compiler warns that some wrapped values are never read, but they are in fact read by +// `::fmt()` via the derived `Debug`. +#[allow(dead_code)] +#[derive(Debug)] +enum Error { + /// Failed to create kill eventfd. + CreateKillEventFd(io::Error), + /// Failed to create thread pool. + CreateThreadPool(io::Error), + /// Failed to handle event other than input event. + HandleEventNotEpollIn, + /// Failed to handle unknown event. + HandleEventUnknownEvent, + /// Iterating through the queue failed. + IterateQueue, + /// No memory configured. + NoMemoryConfigured, + /// Processing queue failed. + ProcessQueue(VhostUserFsError), + /// Creating a queue reader failed. + QueueReader(VufDescriptorError), + /// Creating a queue writer failed. + QueueWriter(VufDescriptorError), + /// The unshare(CLONE_FS) call failed. + UnshareCloneFs(io::Error), + /// Invalid tag name + InvalidTag, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use self::Error::UnshareCloneFs; + match self { + UnshareCloneFs(error) => { + write!( + f, + "The unshare(CLONE_FS) syscall failed with '{error}'. \ + If running in a container please check that the container \ + runtime seccomp policy allows unshare." + ) + } + Self::InvalidTag => write!( + f, + "The tag may not be empty or longer than {MAX_TAG_LEN} bytes (encoded as UTF-8)." + ), + _ => write!(f, "{self:?}"), + } + } +} + +impl error::Error for Error {} + +impl convert::From for io::Error { + fn from(e: Error) -> Self { + other_io_error(e) + } +} + +struct VhostUserFsThread { + mem: Option, + kill_evt: EventFd, + server: Arc>, + // handle request from backend to frontend + vu_req: Option, + event_idx: bool, + pool: Option, +} + +impl Clone for VhostUserFsThread { + fn clone(&self) -> Self { + VhostUserFsThread { + mem: self.mem.clone(), + kill_evt: self.kill_evt.try_clone().unwrap(), + server: self.server.clone(), + vu_req: self.vu_req.clone(), + event_idx: self.event_idx, + pool: self.pool.clone(), + } + } +} + +impl VhostUserFsThread { + fn new(fs: F, thread_pool_size: usize) -> Result { + let pool = if thread_pool_size > 0 { + // Test that unshare(CLONE_FS) works, it will be called for each thread. + // It's an unprivileged system call but some Docker/Moby versions are + // known to reject it via seccomp when CAP_SYS_ADMIN is not given. + // + // Note that the program is single-threaded here so this syscall has no + // visible effect and is safe to make. + let ret = unsafe { libc::unshare(libc::CLONE_FS) }; + if ret == -1 { + return Err(Error::UnshareCloneFs(std::io::Error::last_os_error())); + } + + Some( + ThreadPoolBuilder::new() + .after_start(|_| { + // unshare FS for xattr operation + let ret = unsafe { libc::unshare(libc::CLONE_FS) }; + assert_eq!(ret, 0); // Should not fail + }) + .pool_size(thread_pool_size) + .create() + .map_err(Error::CreateThreadPool)?, + ) + } else { + None + }; + + Ok(VhostUserFsThread { + mem: None, + kill_evt: EventFd::new(EFD_NONBLOCK).map_err(Error::CreateKillEventFd)?, + server: Arc::new(Server::new(fs, &None).unwrap()), + vu_req: None, + event_idx: false, + pool, + }) + } + + fn return_descriptor( + vring_state: &mut VringState, + head_index: u16, + event_idx: bool, + len: usize, + ) { + let used_len: u32 = match len.try_into() { + Ok(l) => l, + Err(_) => panic!("Invalid used length, can't return used descritors to the ring"), + }; + + if vring_state.add_used(head_index, used_len).is_err() { + warn!("Couldn't return used descriptors to the ring"); + } + + if event_idx { + match vring_state.needs_notification() { + Err(_) => { + warn!("Couldn't check if queue needs to be notified"); + vring_state.signal_used_queue().unwrap(); + } + Ok(needs_notification) => { + if needs_notification { + vring_state.signal_used_queue().unwrap(); + } + } + } + } else { + vring_state.signal_used_queue().unwrap(); + } + } + + fn process_queue_pool(&self, vring: VringMutex) -> Result { + let mut used_any = false; + let atomic_mem = match &self.mem { + Some(m) => m, + None => return Err(Error::NoMemoryConfigured), + }; + + while let Some(avail_desc) = vring + .get_mut() + .get_queue_mut() + .iter(atomic_mem.memory()) + .map_err(|_| Error::IterateQueue)? + .next() + { + used_any = true; + + // Prepare a set of objects that can be moved to the worker thread. + let atomic_mem = atomic_mem.clone(); + let server = self.server.clone(); + let mut vu_req = self.vu_req.clone(); + let event_idx = self.event_idx; + let worker_vring = vring.clone(); + let worker_desc = avail_desc.clone(); + + self.pool.as_ref().unwrap().spawn_ok(async move { + let mem = atomic_mem.memory(); + let head_index = worker_desc.head_index(); + + let reader = Reader::new(&mem, worker_desc.clone()) + .map_err(Error::QueueReader) + .unwrap(); + let writer = Writer::new(&mem, worker_desc.clone()) + .map_err(Error::QueueWriter) + .unwrap(); + + let len = server + .handle_message(reader, writer, vu_req.as_mut()) + .map_err(Error::ProcessQueue) + .unwrap(); + + Self::return_descriptor(&mut worker_vring.get_mut(), head_index, event_idx, len); + }); + } + + Ok(used_any) + } + + fn process_queue_serial( + &self, + vring_state: &mut VringState, + ) -> Result { + let mut used_any = false; + let mem = match &self.mem { + Some(m) => m.memory(), + None => return Err(Error::NoMemoryConfigured), + }; + let mut vu_req = self.vu_req.clone(); + + let avail_chains: Vec>> = vring_state + .get_queue_mut() + .iter(mem.clone()) + .map_err(|_| Error::IterateQueue)? + .collect(); + + for chain in avail_chains { + used_any = true; + + let head_index = chain.head_index(); + + let reader = Reader::new(&mem, chain.clone()) + .map_err(Error::QueueReader) + .unwrap(); + let writer = Writer::new(&mem, chain.clone()) + .map_err(Error::QueueWriter) + .unwrap(); + + let len = self + .server + .handle_message(reader, writer, vu_req.as_mut()) + .map_err(Error::ProcessQueue) + .unwrap(); + + Self::return_descriptor(vring_state, head_index, self.event_idx, len); + } + + Ok(used_any) + } + + fn handle_event_pool( + &self, + device_event: u16, + vrings: &[VringMutex], + ) -> VhostUserBackendResult<()> { + let idx = match device_event { + HIPRIO_QUEUE_EVENT => { + debug!("HIPRIO_QUEUE_EVENT"); + 0 + } + REQ_QUEUE_EVENT => { + debug!("QUEUE_EVENT"); + 1 + } + _ => return Err(Error::HandleEventUnknownEvent.into()), + }; + + if self.event_idx { + // vm-virtio's Queue implementation only checks avail_index + // once, so to properly support EVENT_IDX we need to keep + // calling process_queue() until it stops finding new + // requests on the queue. + loop { + vrings[idx].disable_notification().unwrap(); + self.process_queue_pool(vrings[idx].clone())?; + if !vrings[idx].enable_notification().unwrap() { + break; + } + } + } else { + // Without EVENT_IDX, a single call is enough. + self.process_queue_pool(vrings[idx].clone())?; + } + + Ok(()) + } + + fn handle_event_serial( + &self, + device_event: u16, + vrings: &[VringMutex], + ) -> VhostUserBackendResult<()> { + let mut vring_state = match device_event { + HIPRIO_QUEUE_EVENT => { + debug!("HIPRIO_QUEUE_EVENT"); + vrings[0].get_mut() + } + REQ_QUEUE_EVENT => { + debug!("QUEUE_EVENT"); + vrings[1].get_mut() + } + _ => return Err(Error::HandleEventUnknownEvent.into()), + }; + + if self.event_idx { + // vm-virtio's Queue implementation only checks avail_index + // once, so to properly support EVENT_IDX we need to keep + // calling process_queue() until it stops finding new + // requests on the queue. + loop { + vring_state.disable_notification().unwrap(); + self.process_queue_serial(&mut vring_state)?; + if !vring_state.enable_notification().unwrap() { + break; + } + } + } else { + // Without EVENT_IDX, a single call is enough. + self.process_queue_serial(&mut vring_state)?; + } + + Ok(()) + } +} + +#[repr(C)] +#[derive(Clone, Copy)] +struct VirtioFsConfig { + tag: [u8; MAX_TAG_LEN], + num_request_queues: Le32, +} + +// vm-memory needs a Default implementation even though these values are never +// used anywhere... +impl Default for VirtioFsConfig { + fn default() -> Self { + Self { + tag: [0; MAX_TAG_LEN], + num_request_queues: Le32::default(), + } + } +} + +unsafe impl ByteValued for VirtioFsConfig {} + +struct PremigrationThread { + handle: JoinHandle>, + cancel: Arc, +} + +struct VhostUserFsBackend { + thread: RwLock>, + premigration_thread: Mutex>, + migration_thread: Mutex>>>, + tag: Option, +} + +impl VhostUserFsBackend { + fn new(fs: F, thread_pool_size: usize, tag: Option) -> Result { + let thread = RwLock::new(VhostUserFsThread::new(fs, thread_pool_size)?); + Ok(VhostUserFsBackend { + thread, + premigration_thread: None.into(), + migration_thread: None.into(), + tag, + }) + } +} + +impl VhostUserBackend + for VhostUserFsBackend +{ + type Bitmap = BitmapMmapRegion; + type Vring = VringMutex; + + fn num_queues(&self) -> usize { + NUM_QUEUES + } + + fn max_queue_size(&self) -> usize { + QUEUE_SIZE + } + + fn features(&self) -> u64 { + 1 << VIRTIO_F_VERSION_1 + | 1 << VIRTIO_RING_F_INDIRECT_DESC + | 1 << VIRTIO_RING_F_EVENT_IDX + | VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits() + | VhostUserVirtioFeatures::LOG_ALL.bits() + } + + fn protocol_features(&self) -> VhostUserProtocolFeatures { + let mut protocol_features = VhostUserProtocolFeatures::MQ + | VhostUserProtocolFeatures::BACKEND_REQ + | VhostUserProtocolFeatures::BACKEND_SEND_FD + | VhostUserProtocolFeatures::REPLY_ACK + | VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS + | VhostUserProtocolFeatures::LOG_SHMFD + | VhostUserProtocolFeatures::DEVICE_STATE; + + if self.tag.is_some() { + protocol_features |= VhostUserProtocolFeatures::CONFIG; + } + + protocol_features + } + + fn get_config(&self, offset: u32, size: u32) -> Vec { + // virtio spec 1.2, 5.11.4: + // The tag is encoded in UTF-8 and padded with NUL bytes if shorter than + // the available space. This field is not NUL-terminated if the encoded + // bytes take up the entire field. + // The length was already checked when parsing the arguments. Hence, we + // only assert that everything looks sane and pad with NUL bytes to the + // fixed length. + let tag = self.tag.as_ref().expect("Did not expect read of config if tag is not set. We do not advertise F_CONFIG in that case!"); + assert!(tag.len() <= MAX_TAG_LEN, "too long tag length"); + assert!(!tag.is_empty(), "tag should not be empty"); + let mut fixed_len_tag = [0; MAX_TAG_LEN]; + fixed_len_tag[0..tag.len()].copy_from_slice(tag.as_bytes()); + + let config = VirtioFsConfig { + tag: fixed_len_tag, + num_request_queues: Le32::from(REQUEST_QUEUES), + }; + + let offset = offset as usize; + let size = size as usize; + let mut result: Vec<_> = config + .as_slice() + .iter() + .skip(offset) + .take(size) + .copied() + .collect(); + // pad with 0s up to `size` + result.resize(size, 0); + result + } + + fn acked_features(&self, features: u64) { + if features & VhostUserVirtioFeatures::LOG_ALL.bits() != 0 { + // F_LOG_ALL set: Prepare for migration (unless we're already doing that) + let mut premigration_thread = self.premigration_thread.lock().unwrap(); + if premigration_thread.is_none() { + let cancel = Arc::new(AtomicBool::new(false)); + let cloned_server = Arc::clone(&self.thread.read().unwrap().server); + let cloned_cancel = Arc::clone(&cancel); + let handle = + thread::spawn(move || cloned_server.prepare_serialization(cloned_cancel)); + *premigration_thread = Some(PremigrationThread { handle, cancel }); + } + } else { + // F_LOG_ALL cleared: Migration cancelled, if any was ongoing + // (Note that this is our interpretation, and not said by the specification. The back + // end might clear this flag also on the source side once the VM has been stopped, even + // before we receive SET_DEVICE_STATE_FD. QEMU will clear F_LOG_ALL only when the VM + // is running, i.e. when the source resumes after a cancelled migration, which is + // exactly what we want, but it would be better if we had a more reliable way that is + // backed up by the spec. We could delay cancelling until we receive a guest request + // while F_LOG_ALL is cleared, but that can take an indefinite amount of time.) + if let Some(premigration_thread) = self.premigration_thread.lock().unwrap().take() { + premigration_thread.cancel.store(true, Ordering::Relaxed); + // Ignore the result, we are cancelling anyway + let _ = premigration_thread.handle.join(); + } + } + } + + fn set_event_idx(&self, enabled: bool) { + self.thread.write().unwrap().event_idx = enabled; + } + + fn update_memory(&self, mem: LoggedMemoryAtomic) -> VhostUserBackendResult<()> { + self.thread.write().unwrap().mem = Some(mem); + Ok(()) + } + + fn handle_event( + &self, + device_event: u16, + evset: EventSet, + vrings: &[VringMutex], + _thread_id: usize, + ) -> VhostUserBackendResult<()> { + if evset != EventSet::IN { + return Err(Error::HandleEventNotEpollIn.into()); + } + + let thread = self.thread.read().unwrap(); + + if thread.pool.is_some() { + thread.handle_event_pool(device_event, vrings) + } else { + thread.handle_event_serial(device_event, vrings) + } + } + + fn exit_event(&self, _thread_index: usize) -> Option { + Some(self.thread.read().unwrap().kill_evt.try_clone().unwrap()) + } + + fn set_backend_req_fd(&self, vu_req: Backend) { + self.thread.write().unwrap().vu_req = Some(vu_req); + } + + fn set_device_state_fd( + &self, + direction: VhostTransferStateDirection, + phase: VhostTransferStatePhase, + file: File, + ) -> io::Result> { + if phase != VhostTransferStatePhase::STOPPED { + return Err(io::Error::new( + io::ErrorKind::Unsupported, + format!("Transfer in phase {:?} is not supported", phase), + )); + } + + let server = Arc::clone(&self.thread.read().unwrap().server); + let join_handle = match direction { + VhostTransferStateDirection::SAVE => { + if let Some(premigration_thread) = self.premigration_thread.lock().unwrap().take() { + // It isn't great to have this vhost-user message block, but if the + // preparations are still ongoing, we have no choice + premigration_thread.handle.join().map_err(|_| { + other_io_error("Failed to finalize serialization preparation".to_string()) + })??; + } + + thread::spawn(move || { + server.serialize(file).map_err(|e| { + io::Error::new(e.kind(), format!("Failed to save state: {}", e)) + }) + }) + } + + VhostTransferStateDirection::LOAD => { + if let Some(premigration_thread) = self.premigration_thread.lock().unwrap().take() { + // Strange, but OK + premigration_thread.cancel.store(true, Ordering::Relaxed); + warn!("Cancelling serialization preparation because of incoming migration"); + let _ = premigration_thread.handle.join(); + } + + thread::spawn(move || { + server.deserialize_and_apply(file).map_err(|e| { + io::Error::new(e.kind(), format!("Failed to load state: {}", e)) + }) + }) + } + }; + + *self.migration_thread.lock().unwrap() = Some(join_handle); + + Ok(None) + } + + fn check_device_state(&self) -> io::Result<()> { + let result = if let Some(migration_thread) = self.migration_thread.lock().unwrap().take() { + // `Result::flatten()` is not stable yet, so no `.join().map_err(...).flatten()` + match migration_thread.join() { + Ok(x) => x, + Err(_) => Err(other_io_error("Failed to join the migration thread")), + } + } else { + // `check_device_state()` must follow a successful `set_device_state_fd()`, so this is + // a protocol violation + Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Front-end attempts to check migration state, but no migration has been done", + )) + }; + + // Note that just like any other vhost-user message implementation, the error object that + // we return is not forwarded to the front end (it only receives an error flag), so if we + // want users to see some diagnostics, we have to print them ourselves + if let Err(e) = &result { + error!("Migration failed: {e}"); + } + result + } +} + +fn parse_seccomp(src: &str) -> std::result::Result { + Ok(match src { + "none" => SeccompAction::Allow, // i.e. no seccomp + "kill" => SeccompAction::Kill, + "log" => SeccompAction::Log, + "trap" => SeccompAction::Trap, + _ => return Err("Matching variant not found"), + }) +} + +/// On the command line, we want to allow aliases for `InodeFileHandlesMode` values. This enum has +/// all values allowed on the command line, and with `From`/`Into`, it can be translated into the +/// internally used `InodeFileHandlesMode` enum. +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +enum InodeFileHandlesCommandLineMode { + /// `InodeFileHandlesMode::Never` + Never, + /// Alias for `InodeFileHandlesMode::Prefer` + Fallback, + /// `InodeFileHandlesMode::Prefer` + Prefer, + /// `InodeFileHandlesMode::Mandatory` + Mandatory, +} + +impl From for InodeFileHandlesMode { + fn from(clm: InodeFileHandlesCommandLineMode) -> Self { + match clm { + InodeFileHandlesCommandLineMode::Never => InodeFileHandlesMode::Never, + InodeFileHandlesCommandLineMode::Fallback => InodeFileHandlesMode::Prefer, + InodeFileHandlesCommandLineMode::Prefer => InodeFileHandlesMode::Prefer, + InodeFileHandlesCommandLineMode::Mandatory => InodeFileHandlesMode::Mandatory, + } + } +} + +impl FromStr for InodeFileHandlesCommandLineMode { + type Err = &'static str; + + fn from_str(s: &str) -> std::result::Result { + match s { + "never" => Ok(InodeFileHandlesCommandLineMode::Never), + "fallback" => Ok(InodeFileHandlesCommandLineMode::Fallback), + "prefer" => Ok(InodeFileHandlesCommandLineMode::Prefer), + "mandatory" => Ok(InodeFileHandlesCommandLineMode::Mandatory), + + _ => Err("invalid inode file handles mode"), + } + } +} + +fn parse_tag(tag: &str) -> Result { + if !tag.is_empty() && tag.len() <= MAX_TAG_LEN { + Ok(tag.into()) + } else { + Err(Error::InvalidTag) + } +} + +#[derive(Clone, Debug, Parser)] +#[command( + name = "virtiofsd", + about = "Launch a virtiofsd backend.", + version, + args_override_self = true +)] +struct Opt { + /// Shared directory path + #[arg(long, required_unless_present("compat_options"))] + shared_dir: Option, + + /// The tag that the virtio device advertises + /// + /// Setting this option will enable advertising of + /// VHOST_USER_PROTOCOL_F_CONFIG. However, the vhost-user frontend of your + /// hypervisor may not negotiate this feature and (or) ignore this value. + /// Notably, QEMU currently (as of 8.1) ignores the CONFIG feature. QEMU + /// versions from 7.1 to 8.0 will crash while attempting to log a warning + /// about not supporting the feature. + #[arg(long, value_parser = parse_tag)] + tag: Option, + + /// vhost-user socket path [deprecated] + #[arg(long, required_unless_present_any = &["fd", "socket_path", "print_capabilities"])] + socket: Option, + + /// vhost-user socket path + #[arg(long = "socket-path", required_unless_present_any = &["fd", "socket", "print_capabilities"])] + socket_path: Option, + + /// Name of group for the vhost-user socket + #[arg(long = "socket-group", conflicts_with_all = &["fd", "print_capabilities"])] + socket_group: Option, + + /// File descriptor for the listening socket + #[arg(long, required_unless_present_any = &["socket", "socket_path", "print_capabilities"], conflicts_with_all = &["socket_path", "socket"])] + fd: Option, + + /// Maximum thread pool size. A value of "0" disables the pool + #[arg(long, default_value = "0")] + thread_pool_size: usize, + + /// Enable support for extended attributes + #[arg(long)] + xattr: bool, + + /// Enable support for posix ACLs (implies --xattr) + #[arg(long)] + posix_acl: bool, + + /// Add custom rules for translating extended attributes between host and guest + /// (e.g. :map::user.virtiofs.:) + #[arg(long, value_parser = |s: &_| XattrMap::try_from(s))] + xattrmap: Option, + + /// Sandbox mechanism to isolate the daemon process (namespace, chroot, none) + #[arg(long, default_value = "namespace")] + sandbox: SandboxMode, + + /// Action to take when seccomp finds a not allowed syscall (none, kill, log, trap) + #[arg(long, value_parser = parse_seccomp, default_value = "kill")] + seccomp: SeccompAction, + + /// Tell the guest which directories are mount points [default] + #[arg(long)] + announce_submounts: bool, + + /// Do not tell the guest which directories are mount points + #[arg(long, overrides_with("announce_submounts"))] + no_announce_submounts: bool, + + /// When to use file handles to reference inodes instead of O_PATH file descriptors (never, + /// prefer, mandatory) + /// + /// - never: Never use file handles, always use O_PATH file descriptors. + /// + /// - prefer: Attempt to generate file handles, but fall back to O_PATH file descriptors where + /// the underlying filesystem does not support file handles. Useful when there are various + /// different filesystems under the shared directory and some of them do not support file + /// handles. ("fallback" is a deprecated alias for "prefer".) + /// + /// - mandatory: Always use file handles, never fall back to O_PATH file descriptors. + /// + /// Using file handles reduces the number of file descriptors virtiofsd keeps open, which is + /// not only helpful with resources, but may also be important in cases where virtiofsd should + /// only have file descriptors open for files that are open in the guest, e.g. to get around + /// bad interactions with NFS's silly renaming. + #[arg(long, require_equals = true, default_value = "never")] + inode_file_handles: InodeFileHandlesCommandLineMode, + + /// The caching policy the file system should use (auto, always, never, metadata) + #[arg(long, default_value = "auto")] + cache: CachePolicy, + + #[arg(long)] + allow_mmap: bool, + + /// Disable support for READDIRPLUS operations + #[arg(long)] + no_readdirplus: bool, + + /// Enable writeback cache + #[arg(long)] + writeback: bool, + + /// Honor the O_DIRECT flag passed down by guest applications + #[arg(long)] + allow_direct_io: bool, + + /// Print vhost-user.json backend program capabilities and exit + #[arg(long = "print-capabilities")] + print_capabilities: bool, + + /// Modify the list of capabilities, e.g., --modcaps=+sys_admin:-chown + #[arg(long)] + modcaps: Option, + + /// Log level (error, warn, info, debug, trace, off) + #[arg(long = "log-level", default_value = "info")] + log_level: LevelFilter, + + /// Log to syslog [default: stderr] + #[arg(long)] + syslog: bool, + + /// Set maximum number of file descriptors (0 leaves rlimit unchanged) + /// [default: min(1000000, '/proc/sys/fs/nr_open')] + #[arg(long = "rlimit-nofile")] + rlimit_nofile: Option, + + /// Options in a format compatible with the legacy implementation [deprecated] + #[arg(short = 'o')] + compat_options: Option>, + + /// Set log level to "debug" [deprecated] + #[arg(short = 'd')] + compat_debug: bool, + + /// Disable KILLPRIV V2 support [default] + #[arg(long)] + _no_killpriv_v2: bool, + + /// Enable KILLPRIV V2 support + #[arg(long, overrides_with("_no_killpriv_v2"))] + killpriv_v2: bool, + + /// Compatibility option that has no effect [deprecated] + #[arg(short = 'f')] + compat_foreground: bool, + + /// Enable security label support. Expects SELinux xattr on file creation + /// from client and stores it in the newly created file. + #[arg(long = "security-label")] + security_label: bool, + + /// Map a range of UIDs from the host into the namespace, given as + /// :namespace_uid:host_uid:count: + /// + /// For example, :0:100000:65536: will map the 65536 host UIDs [100000, 165535] + /// into the namespace as [0, 65535]. + /// + /// Provide this argument multiple times to map multiple UID ranges. + #[arg(long)] + uid_map: Vec, + + /// Map a range of GIDs from the host into the namespace, given as + /// :namespace_gid:host_gid:count: + /// + /// For example, :0:100000:65536: will map the 65536 host GIDs [100000, 165535] + /// into the namespace as [0, 65535]. + /// + /// Provide this argument multiple times to map multiple GID ranges. + #[arg(long)] + gid_map: Vec, + + /// Preserve O_NOATIME behavior, otherwise automatically clean up O_NOATIME flag to prevent + /// potential permission errors when running in unprivileged mode (e.g., when accessing files + /// without having ownership/capability to use O_NOATIME). + #[arg(long = "preserve-noatime")] + preserve_noatime: bool, + + /// Defines how to perform migration, i.e. how to represent the internal state to the + /// destination, and how to obtain that representation. + /// + /// - find-paths: Iterate through the shared directory (exhaustive search) to find paths for + /// all inodes indexed and opened by the guest, and transfer these paths to the destination. + /// + /// This parameter is ignored on the destination side. + #[arg(long = "migration-mode", default_value = "find-paths")] + migration_mode: MigrationMode, + + /// Controls how to respond to errors during migration. + /// + /// If any inode turns out not to be migrateable (either the source cannot serialize it, or the + /// destination cannot opened the serialized representation), the destination can react in + /// different ways: + /// + /// - abort: Whenever any error occurs, return a hard error to the vhost-user front-end (e.g. + /// QEMU), aborting migration. + /// + /// - guest-error: Let migration finish, but the guest will be unable to access any of the + /// affected inodes, receiving only errors. + /// + /// This parameter is ignored on the source side. + #[arg(long = "migration-on-error", default_value = "abort")] + migration_on_error: MigrationOnError, + + /// Ensure that the migration destination opens the very same inodes as the source (only works + /// if source and destination use the same shared directory on the same filesystem). + /// + /// This option makes the source attach the respective file handle to each inode transferred + /// during migration. Once the destination has (re-)opened the inode, it will generate the + /// file handle on its end, and compare, ensuring that it has opened the very same inode. + /// + /// (File handles are per-filesystem unique identifiers for inodes that, besides the inode ID, + /// also include a generation ID to protect against inode ID reuse.) + /// + /// Using this option protects against external parties renaming or replacing inodes + /// while migration is ongoing, which, without this option, can lead to data loss or + /// corruption, so it should always be used when other processes besides virtiofsd have write + /// access to the shared directory. However, again, it only works if both source and + /// destination use the same shared directory. + /// + /// This parameter is ignored on the destination side. + #[arg(long = "migration-verify-handles")] + migration_verify_handles: bool, + + /// Double-check the identity of inodes right before switching over to the destination, + /// potentially making migration more resilient when third parties have write access to the + /// shared directory. + /// + /// When representing migrated inodes using their paths relative to the shared directory, + /// double-check during switch-over to the destination that each path still matches the + /// respective inode, and on mismatch, try to correct it via the respective symlink in + /// /proc/self/fd. + /// + /// Because this option requires accessing each inode indexed or opened by the guest, it can + /// prolong the switch-over phase of migration (when both source and destination are paused) + /// for an indeterminate amount of time. + /// + /// This parameter is ignored on the destination side. + #[arg(long = "migration-confirm-paths")] + migration_confirm_paths: bool, + + /// Use the legacy full-tree DFS-based path reconstruction for the preserialization phase, + /// instead of the default store-only reconstructor. + /// + /// By default (when this flag is NOT given), preserialization only iterates the inode store + /// and reverse-looks up parent inodes via a path -> inode hash table built in a single store + /// pass. Any parent inodes that are not yet in the store are materialized on demand via + /// `openat(O_PATH | O_NOFOLLOW)` from the nearest known anchor, so the result is semantically + /// identical to the legacy DFS reconstructor. This is much faster when the shared directory + /// is large but only a small subset has been looked up by the guest, and especially when the + /// shared tree is on a slow backend (e.g. NFS). + /// + /// Set this flag to opt into the legacy directory-tree DFS reconstructor (e.g. as an escape + /// hatch if the store-only reconstructor is suspected of misbehaving in a specific setup). + /// + /// The deprecated aliases `--migration-legacy-reconstructor` and `--migration-no-store-only` + /// are still accepted for backward compatibility. + /// + /// This parameter is ignored on the destination side. + #[arg( + long = "migration-dfs-preserialization", + alias = "migration-legacy-reconstructor", + alias = "migration-no-store-only" + )] + migration_dfs_preserialization: bool, +} + +fn parse_compat(opt: Opt) -> Opt { + use clap::error::ErrorKind; + fn value_error(arg: &str, value: &str) -> ! { + ::command() + .error( + ErrorKind::InvalidValue, + format!("Invalid compat value '{value}' for '-o {arg}'"), + ) + .exit() + } + fn argument_error(arg: &str) -> ! { + ::command() + .error( + ErrorKind::UnknownArgument, + format!("Invalid compat argument '-o {arg}'"), + ) + .exit() + } + + fn parse_tuple(opt: &mut Opt, tuple: &str) { + match tuple.split('=').collect::>()[..] { + ["xattrmap", value] => { + opt.xattrmap = Some( + XattrMap::try_from(value).unwrap_or_else(|_| value_error("xattrmap", value)), + ) + } + ["cache", value] => match value { + "auto" => opt.cache = CachePolicy::Auto, + "always" => opt.cache = CachePolicy::Always, + "none" => opt.cache = CachePolicy::Never, + "metadata" => opt.cache = CachePolicy::Metadata, + _ => value_error("cache", value), + }, + ["loglevel", value] => match value { + "debug" => opt.log_level = LevelFilter::Debug, + "info" => opt.log_level = LevelFilter::Info, + "warn" => opt.log_level = LevelFilter::Warn, + "err" => opt.log_level = LevelFilter::Error, + _ => value_error("loglevel", value), + }, + ["sandbox", value] => match value { + "namespace" => opt.sandbox = SandboxMode::Namespace, + "chroot" => opt.sandbox = SandboxMode::Chroot, + _ => value_error("sandbox", value), + }, + ["source", value] => opt.shared_dir = Some(value.to_string()), + ["modcaps", value] => opt.modcaps = Some(value.to_string()), + _ => argument_error(tuple), + } + } + + fn parse_single(opt: &mut Opt, option: &str) { + match option { + "xattr" => opt.xattr = true, + "no_xattr" => opt.xattr = false, + "readdirplus" => opt.no_readdirplus = false, + "no_readdirplus" => opt.no_readdirplus = true, + "writeback" => opt.writeback = true, + "no_writeback" => opt.writeback = false, + "allow_direct_io" => opt.allow_direct_io = true, + "no_allow_direct_io" => opt.allow_direct_io = false, + "announce_submounts" => opt.announce_submounts = true, + "killpriv_v2" => opt.killpriv_v2 = true, + "no_killpriv_v2" => opt.killpriv_v2 = false, + "posix_acl" => opt.posix_acl = true, + "no_posix_acl" => opt.posix_acl = false, + "security_label" => opt.security_label = true, + "no_security_label" => opt.security_label = false, + "no_posix_lock" | "no_flock" => (), + _ => argument_error(option), + } + } + + let mut clean_opt = opt.clone(); + + if let Some(compat_options) = opt.compat_options.as_ref() { + for line in compat_options { + for option in line.split(',') { + if option.contains('=') { + parse_tuple(&mut clean_opt, option); + } else { + parse_single(&mut clean_opt, option); + } + } + } + } + + clean_opt +} + +fn print_capabilities() { + println!("{{"); + println!(" \"type\": \"fs\","); + println!(" \"features\": ["); + println!(" \"migrate-precopy\""); + println!(" ]"); + println!("}}"); +} + +fn set_default_logger(log_level: LevelFilter) { + if env::var("RUST_LOG").is_err() { + env::set_var("RUST_LOG", log_level.to_string()); + } + env_logger::init(); +} + +fn initialize_logging(opt: &Opt) { + let log_level = if opt.compat_debug { + LevelFilter::Debug + } else { + opt.log_level + }; + + if opt.syslog { + if let Err(e) = syslog::init(syslog::Facility::LOG_USER, log_level, None) { + set_default_logger(log_level); + warn!("can't enable syslog: {}", e); + } + } else { + set_default_logger(log_level); + } +} + +fn set_signal_handlers() { + use vmm_sys_util::signal; + + extern "C" fn handle_signal(_: libc::c_int, _: *mut libc::siginfo_t, _: *mut libc::c_void) { + unsafe { libc::_exit(1) }; + } + let signals = vec![libc::SIGHUP, libc::SIGTERM]; + for s in signals { + if let Err(e) = signal::register_signal_handler(s, handle_signal) { + error!("Setting signal handlers: {}", e); + process::exit(1); + } + } +} + +fn parse_modcaps( + default_caps: Vec<&str>, + modcaps: Option, +) -> (HashSet, HashSet) { + let mut required_caps: HashSet = default_caps.iter().map(|&s| s.into()).collect(); + let mut disabled_caps = HashSet::new(); + + if let Some(modcaps) = modcaps { + for modcap in modcaps.split(':').map(str::to_string) { + if modcap.is_empty() { + error!("empty modcap found: expected (+|-)capability:..."); + process::exit(1); + } + let (action, cap_name) = modcap.split_at(1); + let cap_name = cap_name.to_uppercase(); + if !matches!(action, "+" | "-") { + error!( + "invalid modcap action: expecting '+'|'-' but found '{}'", + action + ); + process::exit(1); + } + if let Err(error) = capng::name_to_capability(&cap_name) { + error!("invalid capability '{}': {}", &cap_name, error); + process::exit(1); + } + + match action { + "+" => { + disabled_caps.remove(&cap_name); + required_caps.insert(cap_name); + } + "-" => { + required_caps.remove(&cap_name); + disabled_caps.insert(cap_name); + } + _ => unreachable!(), + } + } + } + (required_caps, disabled_caps) +} + +fn drop_capabilities(inode_file_handles: InodeFileHandlesMode, modcaps: Option) { + let default_caps = vec![ + "CHOWN", + "DAC_OVERRIDE", + "FOWNER", + "FSETID", + "SETGID", + "SETUID", + "MKNOD", + "SETFCAP", + ]; + let (mut required_caps, disabled_caps) = parse_modcaps(default_caps, modcaps); + + if inode_file_handles != InodeFileHandlesMode::Never { + let required_cap = "DAC_READ_SEARCH".to_owned(); + if disabled_caps.contains(&required_cap) { + error!( + "can't disable {} when using --inode-file-handles={:?}", + &required_cap, inode_file_handles + ); + process::exit(1); + } + required_caps.insert(required_cap); + } + + capng::clear(capng::Set::BOTH); + // Configure the required set of capabilities for the child, and leave the + // parent with none. + if let Err(e) = capng::updatev( + capng::Action::ADD, + capng::Type::PERMITTED | capng::Type::EFFECTIVE, + required_caps.iter().map(String::as_str).collect(), + ) { + error!("can't set up the child capabilities: {}", e); + process::exit(1); + } + if let Err(e) = capng::apply(capng::Set::BOTH) { + error!("can't apply the child capabilities: {}", e); + process::exit(1); + } +} + +fn has_noatime_capability() -> bool { + // We may not have all permissions/capabilities to use O_NOATIME with all the exported files if + // we are running as unprivileged user and without any sandbox (e.g., --sandbox=none). + // + // Provide this helper function to check this particular case. + let uid = unsafe { libc::geteuid() }; + let cap = capng::name_to_capability("FOWNER").unwrap_or_else(|err| { + error!("could not get capability FOWNER: {}", err); + process::exit(1); + }); + + uid == 0 || capng::have_capability(capng::Type::EFFECTIVE, cap) +} + +fn main() { + let opt = parse_compat(Opt::parse()); + + // Enable killpriv_v2 only if user explicitly asked for it by using + // --killpriv-v2 or -o killpriv_v2. Otherwise disable it by default. + let killpriv_v2 = opt.killpriv_v2; + + // Disable announce submounts if the user asked for it + let announce_submounts = !opt.no_announce_submounts; + + if opt.print_capabilities { + print_capabilities(); + return; + } + + initialize_logging(&opt); + set_signal_handlers(); + + let shared_dir = match opt.shared_dir.as_ref() { + Some(s) => s, + None => { + error!("missing \"--shared-dir\" or \"-o source\" option"); + process::exit(1); + } + }; + + let shadir_path = Path::new(shared_dir); + if !shadir_path.is_dir() && !shadir_path.is_file() { + error!("{shared_dir} does not exist"); + process::exit(1); + } + + if opt.compat_foreground { + warn!("Use of deprecated flag '-f': This flag has no effect, please remove it"); + } + if opt.compat_debug { + warn!("Use of deprecated flag '-d': Please use the '--log-level debug' option instead"); + } + if opt.compat_options.is_some() { + warn!("Use of deprecated option format '-o': Please specify options without it (e.g., '--cache auto' instead of '-o cache=auto')"); + } + if opt.inode_file_handles == InodeFileHandlesCommandLineMode::Fallback { + warn!("Use of deprecated value 'fallback' for '--inode-file-handles': Please use 'prefer' instead"); + } + + let xattrmap = opt.xattrmap.clone(); + let xattr = xattrmap.is_some() || opt.posix_acl || opt.xattr; + let thread_pool_size = opt.thread_pool_size; + let readdirplus = match opt.cache { + CachePolicy::Never => false, + _ => !opt.no_readdirplus, + }; + + let timeout = match opt.cache { + CachePolicy::Never => Duration::from_secs(0), + CachePolicy::Metadata => Duration::from_secs(86400), + CachePolicy::Auto => Duration::from_secs(1), + CachePolicy::Always => Duration::from_secs(86400), + }; + + let umask = if opt.socket_group.is_some() { + libc::S_IROTH | libc::S_IWOTH | libc::S_IXOTH + } else { + libc::S_IRGRP + | libc::S_IWGRP + | libc::S_IXGRP + | libc::S_IROTH + | libc::S_IWOTH + | libc::S_IXOTH + }; + + // We need to keep _pid_file around because it maintains a lock on the pid file + // that prevents another daemon from using the same pid file. + let (listener, socket_path, _pid_file) = match opt.fd.as_ref() { + Some(fd) => unsafe { (Listener::from_raw_fd(*fd), None, None) }, + None => { + // Set umask to ensure the socket is created with the right permissions + let _umask_guard = oslib::ScopedUmask::new(umask); + + let socket = opt.socket_path.as_ref().unwrap_or_else(|| { + warn!("use of deprecated parameter '--socket': Please use the '--socket-path' option instead"); + opt.socket.as_ref().unwrap() // safe to unwrap because clap ensures either --socket or --socket-path are passed + }); + + let socket_parent_dir = Path::new(socket).parent().unwrap_or_else(|| { + error!("Invalid socket file name"); + process::exit(1); + }); + + if !socket_parent_dir.as_os_str().is_empty() && !socket_parent_dir.exists() { + error!( + "{} does not exist or is not a directory", + socket_parent_dir.to_string_lossy() + ); + process::exit(1); + } + + let pid_file_name = socket.to_owned() + ".pid"; + let pid_file_path = Path::new(pid_file_name.as_str()); + let pid_file = write_pid_file(pid_file_path).unwrap_or_else(|error| { + error!("Error creating pid file '{}': {}", pid_file_name, error); + process::exit(1); + }); + + let listener = Listener::new(socket, true).unwrap_or_else(|error| { + error!("Error creating listener: {}", error); + process::exit(1); + }); + + (listener, Some(socket.clone()), Some(pid_file)) + } + }; + + if let Some(group_name) = opt.socket_group { + let c_name = CString::new(group_name).expect("invalid group name"); + let group = unsafe { libc::getgrnam(c_name.as_ptr()) }; + if group.is_null() { + error!("Couldn't resolve the group name specified for the socket path"); + process::exit(1); + } + + // safe to unwrap because clap ensures --socket-group can't be specified alongside --fd + let c_socket_path = CString::new(socket_path.unwrap()).expect("invalid socket path"); + let ret = unsafe { libc::chown(c_socket_path.as_ptr(), u32::MAX, (*group).gr_gid) }; + if ret != 0 { + error!( + "Couldn't set up the group for the socket path: {}", + std::io::Error::last_os_error() + ); + process::exit(1); + } + } + + limits::setup_rlimit_nofile(opt.rlimit_nofile).unwrap_or_else(|error| { + error!("Error increasing number of open files: {}", error); + process::exit(1) + }); + + let mut sandbox = Sandbox::new( + shared_dir.to_string(), + opt.sandbox, + opt.uid_map, + opt.gid_map, + ) + .unwrap_or_else(|error| { + error!("Error creating sandbox: {}", error); + process::exit(1) + }); + + // Enter the sandbox, from this point the process will be isolated (or not) + // as chosen in '--sandbox'. + let listener = sandbox.enter(listener).unwrap_or_else(|error| { + error!("Error entering sandbox: {}", error); + process::exit(1) + }); + + let fs_cfg = passthrough::Config { + entry_timeout: timeout, + attr_timeout: timeout, + cache_policy: opt.cache, + root_dir: sandbox.get_root_dir(), + mountinfo_prefix: sandbox.get_mountinfo_prefix(), + xattr, + xattrmap, + proc_sfd_rawfd: sandbox.get_proc_self_fd(), + proc_mountinfo_rawfd: sandbox.get_mountinfo_fd(), + announce_submounts, + inode_file_handles: opt.inode_file_handles.into(), + readdirplus, + writeback: opt.writeback, + allow_direct_io: opt.allow_direct_io, + killpriv_v2, + security_label: opt.security_label, + posix_acl: opt.posix_acl, + clean_noatime: !opt.preserve_noatime && !has_noatime_capability(), + allow_mmap: opt.allow_mmap, + migration_on_error: opt.migration_on_error, + migration_verify_handles: opt.migration_verify_handles, + migration_confirm_paths: opt.migration_confirm_paths, + migration_mode: opt.migration_mode, + migration_dfs_preserialization: opt.migration_dfs_preserialization, + ..Default::default() + }; + + // Must happen before we start the thread pool + match opt.seccomp { + SeccompAction::Allow => {} + _ => enable_seccomp(opt.seccomp, opt.syslog).unwrap(), + } + + // We don't modify the capabilities if the user call us without + // any sandbox (i.e. --sandbox=none) as unprivileged user + let uid = unsafe { libc::geteuid() }; + if uid == 0 { + drop_capabilities(fs_cfg.inode_file_handles, opt.modcaps); + } + + let fs = match PassthroughFs::new(fs_cfg) { + Ok(fs) => fs, + Err(e) => { + error!( + "Failed to create internal filesystem representation: {:?}", + e + ); + process::exit(1); + } + }; + + let fs_backend = Arc::new( + VhostUserFsBackend::new(fs, thread_pool_size, opt.tag).unwrap_or_else(|error| { + error!("Error creating vhost-user backend: {}", error); + process::exit(1) + }), + ); + + let mut daemon = VhostUserDaemon::new( + String::from("virtiofsd-backend"), + fs_backend.clone(), + GuestMemoryAtomic::new(GuestMemoryMmap::new()), + ) + .unwrap(); + + info!("Waiting for vhost-user socket connection..."); + + if let Err(e) = daemon.start(listener) { + error!("Failed to start daemon: {:?}", e); + process::exit(1); + } + + info!("Client connected, servicing requests"); + + if let Err(e) = daemon.wait() { + match e { + HandleRequest(Disconnected) => info!("Client disconnected, shutting down"), + _ => error!("Waiting for daemon failed: {:?}", e), + } + } + + let kill_evt = fs_backend + .thread + .read() + .unwrap() + .kill_evt + .try_clone() + .unwrap(); + if let Err(e) = kill_evt.write(1) { + error!("Error shutting down worker thread: {:?}", e) + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/oslib.rs b/vendor/cube-hypervisor/virtiofsd/src/oslib.rs new file mode 100644 index 0000000..ffb2409 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/oslib.rs @@ -0,0 +1,479 @@ +// SPDX-License-Identifier: BSD-3-Clause + +use bitflags::bitflags; +use std::ffi::{CStr, CString}; +use std::fs::File; +use std::io::{self, Error, Result}; +use std::os::unix::io::{AsRawFd, BorrowedFd, RawFd}; +use std::os::unix::prelude::FromRawFd; + +// A helper function that check the return value of a C function call +// and wraps it in a `Result` type, returning the `errno` code as `Err`. +fn check_retval + PartialEq>(t: T) -> Result { + if t == T::from(-1_i8) { + Err(Error::last_os_error()) + } else { + Ok(t) + } +} + +/// Simple object to collect basic facts about the OS, +/// such as available syscalls. +pub struct OsFacts { + pub has_openat2: bool, +} + +#[allow(clippy::new_without_default)] +impl OsFacts { + /// This object should only be constructed using new. + #[must_use] + pub fn new() -> Self { + // Checking for `openat2()` since it first appeared in Linux 5.6. + // SAFETY: all-zero byte-pattern is a valid `libc::open_how` + let how: libc::open_how = unsafe { std::mem::zeroed() }; + let cwd = CString::new(".").unwrap(); + // SAFETY: `cwd.as_ptr()` points to a valid NUL-terminated string, + // and the `how` pointer is a valid pointer to an `open_how` struct. + let fd = unsafe { + libc::syscall( + libc::SYS_openat2, + libc::AT_FDCWD, + cwd.as_ptr(), + std::ptr::addr_of!(how), + std::mem::size_of::(), + ) + }; + + let has_openat2 = fd >= 0; + if has_openat2 { + // SAFETY: `fd` is an open file descriptor + unsafe { + libc::close(fd as libc::c_int); + } + } + + Self { has_openat2 } + } +} + +/// Safe wrapper for `mount(2)` +/// +/// # Errors +/// +/// Will return `Err(errno)` if `mount(2)` fails. +/// Each filesystem type may have its own special errors and its own special behavior, +/// see `mount(2)` and the linux source kernel for details. +/// +/// # Panics +/// +/// This function panics if the strings `source`, `target` or `fstype` contain an internal 0 byte. +pub fn mount(source: Option<&str>, target: &str, fstype: Option<&str>, flags: u64) -> Result<()> { + let source = CString::new(source.unwrap_or("")).unwrap(); + let source = source.as_ptr(); + + let target = CString::new(target).unwrap(); + let target = target.as_ptr(); + + let fstype = CString::new(fstype.unwrap_or("")).unwrap(); + let fstype = fstype.as_ptr(); + + // Safety: `source`, `target` or `fstype` are a valid C string pointers + check_retval(unsafe { libc::mount(source, target, fstype, flags, std::ptr::null()) })?; + Ok(()) +} + +/// Safe wrapper for `umount2(2)` +/// +/// # Errors +/// +/// Will return `Err(errno)` if `umount2(2)` fails. +/// Each filesystem type may have its own special errors and its own special behavior, +/// see `umount2(2)` and the linux source kernel for details. +/// +/// # Panics +/// +/// This function panics if the strings `target` contains an internal 0 byte. +pub fn umount2(target: &str, flags: i32) -> Result<()> { + let target = CString::new(target).unwrap(); + let target = target.as_ptr(); + + // Safety: `target` is a valid C string pointer + check_retval(unsafe { libc::umount2(target, flags) })?; + Ok(()) +} + +/// Safe wrapper for `fchdir(2)` +/// +/// # Errors +/// +/// Will return `Err(errno)` if `fchdir(2)` fails. +/// Each filesystem type may have its own special errors, see `fchdir(2)` for details. +pub fn fchdir(fd: RawFd) -> Result<()> { + check_retval(unsafe { libc::fchdir(fd) })?; + Ok(()) +} + +/// Safe wrapper for `umask(2)` +pub fn umask(mask: u32) -> u32 { + // SAFETY: this call doesn't modify any memory and there is no need + // to check the return value because this system call always succeeds. + unsafe { libc::umask(mask) } +} + +/// An RAII implementation of a scoped file mode creation mask (umask), it set the +/// new umask. When this structure is dropped (falls out of scope), it set the previous +/// value of the mask. +pub struct ScopedUmask { + umask: libc::mode_t, +} + +impl ScopedUmask { + pub fn new(new_umask: u32) -> Self { + Self { + umask: umask(new_umask), + } + } +} + +impl Drop for ScopedUmask { + fn drop(&mut self) { + umask(self.umask); + } +} + +/// Safe wrapper around `openat(2)`. +/// +/// # Errors +/// +/// Will return `Err(errno)` if `openat(2)` fails, +/// see `openat(2)` for details. +pub fn openat(dir: &impl AsRawFd, pathname: &CStr, flags: i32, mode: Option) -> Result { + let mode = u64::from(mode.unwrap_or(0)); + + // SAFETY: `pathname` points to a valid NUL-terminated string. + // However, the caller must ensure that `dir` can provide a valid file descriptor. + check_retval(unsafe { + libc::openat( + dir.as_raw_fd(), + pathname.as_ptr(), + flags as libc::c_int, + mode, + ) + }) +} + +/// An utility function that uses `openat2(2)` to restrict the how the provided pathname +/// is resolved. It uses the following flags: +/// - `RESOLVE_IN_ROOT`: Treat the directory referred to by dirfd as the root directory while +/// resolving pathname. This has the effect as though virtiofsd had used chroot(2) to modify its +/// root directory to dirfd. +/// - `RESOLVE_NO_MAGICLINKS`: Disallow all magic-link (i.e., proc(2) link-like files) resolution +/// during path resolution. +/// +/// Additionally, the flags `O_NOFOLLOW` and `O_CLOEXEC` are added. +/// +/// # Error +/// +/// Will return `Err(errno)` if `openat2(2)` fails, see the man page for details. +/// +/// # Safety +/// +/// The caller must ensure that dirfd is a valid file descriptor. +pub fn do_open_relative_to( + dir: &impl AsRawFd, + pathname: &CStr, + flags: i32, + mode: Option, +) -> Result { + // `openat2(2)` returns an error if `how.mode` contains bits other than those in range 07777, + // let's ignore the extra bits to be compatible with `openat(2)`. + let mode = u64::from(mode.unwrap_or(0)) & 0o7777; + + // SAFETY: all-zero byte-pattern represents a valid `libc::open_how` + let mut how: libc::open_how = unsafe { std::mem::zeroed() }; + how.resolve = libc::RESOLVE_IN_ROOT | libc::RESOLVE_NO_MAGICLINKS; + how.flags = flags as u64; + how.mode = mode; + + // SAFETY: `pathname` points to a valid NUL-terminated string, and the `how` pointer is a valid + // pointer to an `open_how` struct. However, the caller must ensure that `dir` can provide a + // valid file descriptor (this can be changed to BorrowedFd). + check_retval(unsafe { + libc::syscall( + libc::SYS_openat2, + dir.as_raw_fd(), + pathname.as_ptr(), + std::ptr::addr_of!(how), + std::mem::size_of::(), + ) + } as RawFd) +} + +mod filehandle { + const MAX_HANDLE_SZ: usize = 128; + + #[derive(Clone, PartialOrd, Ord, PartialEq, Eq)] + #[repr(C)] + pub struct CFileHandle { + handle_bytes: libc::c_uint, + handle_type: libc::c_int, + f_handle: [u8; MAX_HANDLE_SZ], + } + + impl Default for CFileHandle { + fn default() -> Self { + CFileHandle { + handle_bytes: MAX_HANDLE_SZ as libc::c_uint, + handle_type: 0, + f_handle: [0; MAX_HANDLE_SZ], + } + } + } + + impl CFileHandle { + pub fn as_bytes(&self) -> &[u8] { + &self.f_handle[..(self.handle_bytes as usize)] + } + + pub fn handle_type(&self) -> libc::c_int { + self.handle_type + } + } + + extern "C" { + pub fn name_to_handle_at( + dirfd: libc::c_int, + pathname: *const libc::c_char, + file_handle: *mut CFileHandle, + mount_id: *mut libc::c_int, + flags: libc::c_int, + ) -> libc::c_int; + + // Technically `file_handle` should be a `mut` pointer, but `open_by_handle_at()` is specified + // not to change it, so we can declare it `const`. + pub fn open_by_handle_at( + mount_fd: libc::c_int, + file_handle: *const CFileHandle, + flags: libc::c_int, + ) -> libc::c_int; + } +} +pub use filehandle::CFileHandle; + +pub fn name_to_handle_at( + dirfd: &impl AsRawFd, + pathname: &CStr, + file_handle: &mut CFileHandle, + mount_id: &mut libc::c_int, + flags: libc::c_int, +) -> Result<()> { + // SAFETY: `dirfd` is a valid file descriptor, `file_handle` + // is a valid reference to `CFileHandle`, and `mount_id` is + // valid reference to an `int` + check_retval(unsafe { + filehandle::name_to_handle_at( + dirfd.as_raw_fd(), + pathname.as_ptr(), + file_handle, + mount_id, + flags, + ) + })?; + Ok(()) +} + +pub fn open_by_handle_at( + mount_fd: &impl AsRawFd, + file_handle: &CFileHandle, + flags: libc::c_int, +) -> Result { + // SAFETY: `mount_fd` is a valid file descriptor and `file_handle` + // is a valid reference to `CFileHandle` + let fd = check_retval(unsafe { + filehandle::open_by_handle_at(mount_fd.as_raw_fd(), file_handle, flags) + })?; + + // SAFETY: `open_by_handle_at()` guarantees `fd` is a valid file descriptor + Ok(unsafe { File::from_raw_fd(fd) }) +} + +mod writev { + /// musl does not provide a wrapper for the `pwritev2(2)` system call, + /// we need to call it using `syscall(2)`. + + #[cfg(target_env = "gnu")] + pub use libc::pwritev2; + + #[cfg(target_env = "musl")] + pub unsafe fn pwritev2( + fd: libc::c_int, + iov: *const libc::iovec, + iovcnt: libc::c_int, + offset: libc::off_t, + flags: libc::c_int, + ) -> libc::ssize_t { + // The `pwritev2(2)` syscall expects to receive the 64-bit offset split in + // its high and low parts (see `syscall(2)`). On 64-bit architectures we + // set `lo_off=offset` and `hi_off=0` (glibc does it), since `hi_off` is cleared, + // so we need to make sure of not clear the higher 32 bits of `lo_off`, otherwise + // the offset will be 0 on 64-bit architectures. + let lo_off = offset as libc::c_long; // warn: do not clear the higher 32 bits + let hi_off = (offset as u64).checked_shr(libc::c_long::BITS).unwrap_or(0) as libc::c_long; + unsafe { + libc::syscall(libc::SYS_pwritev2, fd, iov, iovcnt, lo_off, hi_off, flags) + as libc::ssize_t + } + } +} + +// We cannot use libc::RWF_HIPRI, etc, because these constants are not defined in musl. +bitflags! { + /// A bitwise OR of zero or more flags passed in as a parameter to the + /// write vectored function `writev_at()`. + pub struct WritevFlags: i32 { + /// High priority write. Allows block-based filesystems to use polling of the device, which + /// provides lower latency, but may use additional resources. (Currently, this feature is + /// usable only on a file descriptor opened using the O_DIRECT flag.) + const RWF_HIPRI = 0x00000001; + + /// Provide a per-write equivalent of the O_DSYNC open(2) flag. Its effect applies + /// only to the data range written by the system call. + const RWF_DSYNC = 0x00000002; + + /// Provide a per-write equivalent of the O_SYNC open(2) flag. Its effect applies only + /// to the data range written by the system call. + const RWF_SYNC = 0x00000004; + + /// Provide a per-write equivalent of the O_APPEND open(2) flag. Its effect applies only + /// to the data range written by the system call. The offset argument does not affect the + /// write operation; the data is always appended to the end of the file. + /// However, if the offset argument is -1, the current file offset is updated. + const RWF_APPEND = 0x00000010; + } +} + +#[cfg(target_env = "gnu")] +mod writev_test { + // Lets make sure (at compile time) that the WritevFlags don't go out of sync with the libc + const _: () = assert!( + super::WritevFlags::RWF_HIPRI.bits() == libc::RWF_HIPRI, + "invalid RWF_HIPRI value" + ); + const _: () = assert!( + super::WritevFlags::RWF_DSYNC.bits() == libc::RWF_DSYNC, + "invalid RWF_DSYNC value" + ); + const _: () = assert!( + super::WritevFlags::RWF_SYNC.bits() == libc::RWF_SYNC, + "invalid RWF_SYNC value" + ); + const _: () = assert!( + super::WritevFlags::RWF_APPEND.bits() == libc::RWF_APPEND, + "invalid RWF_APPEND value" + ); +} + +/// Safe wrapper for `pwritev2(2)` +/// +/// This system call is similar `pwritev(2)`, but add a new argument, +/// flags, which modifies the behavior on a per-call basis. +/// Unlike `pwritev(2)`, if the offset argument is -1, then the current file offset +/// is used and updated. +/// +/// # Errors +/// +/// Will return `Err(errno)` if `pwritev2(2)` fails, see `pwritev2(2)` for details. +/// +/// # Safety +/// +/// The caller must ensure that each iovec element is valid (i.e., it has a valid `iov_base` +/// pointer and `iov_len`). +pub unsafe fn writev_at( + fd: BorrowedFd, + iovecs: &[libc::iovec], + offset: i64, + flags: Option, +) -> Result { + let flags = flags.unwrap_or(WritevFlags::empty()); + // SAFETY: `fd` is a valid filed descriptor, `iov` is a valid pointer + // to the iovec slice `ìovecs` of `iovcnt` elements. However, the caller + // must ensure that each iovec element has a valid `iov_base` pointer and `iov_len`. + let bytes_written = check_retval(unsafe { + writev::pwritev2( + fd.as_raw_fd(), + iovecs.as_ptr(), + iovecs.len() as libc::c_int, + offset, + flags.bits(), + ) + })?; + Ok(bytes_written as usize) +} + +pub struct PipeReader(File); + +impl io::Read for PipeReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.0.read(buf) + } +} + +pub struct PipeWriter(File); + +impl io::Write for PipeWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.0.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.0.flush() + } +} + +pub fn pipe() -> io::Result<(PipeReader, PipeWriter)> { + let mut fds: [RawFd; 2] = [-1, -1]; + let ret = unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) }; + if ret == -1 { + Err(io::Error::last_os_error()) + } else { + Ok(( + PipeReader(unsafe { File::from_raw_fd(fds[0]) }), + PipeWriter(unsafe { File::from_raw_fd(fds[1]) }), + )) + } +} + +// We want credential changes to be per-thread because otherwise +// we might interfere with operations being carried out on other +// threads with different uids/gids. However, posix requires that +// all threads in a process share the same credentials. To do this +// libc uses signals to ensure that when one thread changes its +// credentials the other threads do the same thing. +// +// So instead we invoke the syscall directly in order to get around +// this limitation. Another option is to use the setfsuid and +// setfsgid systems calls. However since those calls have no way to +// return an error, it's preferable to do this instead. +/// Set effective user ID +pub fn seteffuid(uid: libc::uid_t) -> io::Result<()> { + check_retval(unsafe { libc::syscall(libc::SYS_setresuid, -1, uid, -1) })?; + Ok(()) +} + +/// Set effective group ID +pub fn seteffgid(gid: libc::gid_t) -> io::Result<()> { + check_retval(unsafe { libc::syscall(libc::SYS_setresgid, -1, gid, -1) })?; + Ok(()) +} + +/// Set supplementary group +pub fn setsupgroup(gid: libc::gid_t) -> io::Result<()> { + check_retval(unsafe { libc::setgroups(1, &gid) })?; + Ok(()) +} + +/// Drop all supplementary groups +pub fn dropsupgroups() -> io::Result<()> { + check_retval(unsafe { libc::setgroups(0, std::ptr::null()) })?; + Ok(()) +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/credentials.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/credentials.rs new file mode 100644 index 0000000..f75b80f --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/credentials.rs @@ -0,0 +1,174 @@ +// SPDX-License-Identifier: BSD-3-Clause + +use crate::oslib; +use crate::passthrough::util::einval; +use std::io; + +pub struct UnixCredentials { + uid: libc::uid_t, + gid: libc::gid_t, + sup_gid: Option, + keep_capability: bool, +} + +impl UnixCredentials { + pub fn new(uid: libc::uid_t, gid: libc::gid_t) -> Self { + UnixCredentials { + uid, + gid, + sup_gid: None, + keep_capability: false, + } + } + + /// Set a supplementary group. Set `supported_extension` to `false` to signal that a + /// supplementary group maybe required, but the guest was not able to tell us which, + /// so we have to rely on keeping the DAC_OVERRIDE capability. + pub fn supplementary_gid(self, supported_extension: bool, sup_gid: Option) -> Self { + UnixCredentials { + uid: self.uid, + gid: self.gid, + sup_gid, + keep_capability: !supported_extension, + } + } + + /// Changes the effective uid/gid of the current thread to `val`. Changes + /// the thread's credentials back to root when the returned struct is dropped. + pub fn set(self) -> io::Result> { + let change_uid = self.uid != 0; + let change_gid = self.gid != 0; + + // We have to change the gid before we change the uid because if we + // change the uid first then we lose the capability to change the gid. + // However changing back can happen in any order. + if let Some(sup_gid) = self.sup_gid { + oslib::setsupgroup(sup_gid)?; + } + + if change_gid { + oslib::seteffgid(self.gid)?; + } + + if change_uid { + oslib::seteffuid(self.uid)?; + } + + if change_uid && self.keep_capability { + // Before kernel 6.3, we don't have access to process supplementary groups. + // To work around this we can set the `DAC_OVERRIDE` in the effective set. + // We are allowed to set the capability because we only change the effective + // user ID, so we still have the 'DAC_OVERRIDE' in the permitted set. + // After switching back to root the permitted set is copied to the effective set, + // so no additional steps are required. + if let Err(e) = crate::util::add_cap_to_eff("DAC_OVERRIDE") { + warn!("failed to add 'DAC_OVERRIDE' to the effective set of capabilities: {e}"); + } + } + + if !change_uid && !change_gid { + return Ok(None); + } + + Ok(Some(UnixCredentialsGuard { + reset_uid: change_uid, + reset_gid: change_gid, + drop_sup_gid: self.sup_gid.is_some(), + })) + } +} + +pub struct UnixCredentialsGuard { + reset_uid: bool, + reset_gid: bool, + drop_sup_gid: bool, +} + +impl Drop for UnixCredentialsGuard { + fn drop(&mut self) { + if self.reset_uid { + oslib::seteffuid(0).unwrap_or_else(|e| { + error!("failed to change uid back to root: {e}"); + }); + } + + if self.reset_gid { + oslib::seteffgid(0).unwrap_or_else(|e| { + error!("failed to change gid back to root: {e}"); + }); + } + + if self.drop_sup_gid { + oslib::dropsupgroups().unwrap_or_else(|e| { + error!("failed to drop supplementary groups: {e}"); + }); + } + } +} + +pub struct ScopedCaps { + cap: capng::Capability, +} + +impl ScopedCaps { + fn new(cap_name: &str) -> io::Result> { + use capng::{Action, CUpdate, Set, Type}; + + let cap = capng::name_to_capability(cap_name).map_err(|_| { + let err = io::Error::last_os_error(); + error!( + "couldn't get the capability id for name {}: {:?}", + cap_name, err + ); + err + })?; + + if capng::have_capability(Type::EFFECTIVE, cap) { + let req = vec![CUpdate { + action: Action::DROP, + cap_type: Type::EFFECTIVE, + capability: cap, + }]; + capng::update(req).map_err(|e| { + error!("couldn't drop {} capability: {:?}", cap, e); + einval() + })?; + capng::apply(Set::CAPS).map_err(|e| { + error!( + "couldn't apply capabilities after dropping {}: {:?}", + cap, e + ); + einval() + })?; + Ok(Some(Self { cap })) + } else { + Ok(None) + } + } +} + +impl Drop for ScopedCaps { + fn drop(&mut self) { + use capng::{Action, CUpdate, Set, Type}; + + let req = vec![CUpdate { + action: Action::ADD, + cap_type: Type::EFFECTIVE, + capability: self.cap, + }]; + + if let Err(e) = capng::update(req) { + panic!("couldn't restore {} capability: {:?}", self.cap, e); + } + if let Err(e) = capng::apply(Set::CAPS) { + panic!( + "couldn't apply capabilities after restoring {}: {:?}", + self.cap, e + ); + } + } +} + +pub fn drop_effective_cap(cap_name: &str) -> io::Result> { + ScopedCaps::new(cap_name) +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/deserialization.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/deserialization.rs new file mode 100644 index 0000000..399c77a --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/deserialization.rs @@ -0,0 +1,695 @@ +// Copyright 2024 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/// Deserialization functionality (i.e. what happens in +/// `SerializableFileSystem::deserialize_and_apply()`): Take a plain vector of bytes, deserialize +/// it into our serializable structs ('serialized' module), and then apply the information from +/// there to a `PassthroughFs`, restoring the state from the migration source. +use crate::fuse; +use crate::passthrough::device_state::preserialization::HandleMigrationInfo; +use crate::passthrough::device_state::serialized; +use crate::passthrough::file_handle::SerializableFileHandle; +use crate::passthrough::inode_store::{InodeData, InodeIds, StrongInodeReference}; +use crate::passthrough::stat::statx; +use crate::passthrough::util::{openat, printable_fd}; +use crate::passthrough::{ + FileOrHandle, HandleData, HandleDataFile, MigrationOnError, PassthroughFs, +}; +use crate::util::{other_io_error, ErrorContext}; +use std::collections::BTreeMap; +use std::convert::{TryFrom, TryInto}; +use std::io; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, RwLock}; + +impl TryFrom<&Vec> for serialized::PassthroughFs { + type Error = io::Error; + + /// Root of deserialization: Turn plain bytes into a structured `serialized::PassthroughFs` + fn try_from(serialized: &Vec) -> io::Result { + postcard::from_bytes(serialized).map_err(other_io_error) + } +} + +impl serialized::PassthroughFsV1 { + /// Apply the state represented in `self: PassthroughFsV1` to the given actual filesystem state + /// `fs: &PassthroughFs` (i.e. restore the inode store, open handles, etc.) + pub(super) fn apply(mut self, fs: &PassthroughFs) -> io::Result<()> { + debug!("deserialization apply start"); + + // Apply options as negotiated with the guest on the source + self.negotiated_opts.apply(fs)?; + + fs.inodes.clear(); + + // Some inodes may depend on other inodes being deserialized before them, so trying to + // deserialize them without their dependency being fulfilled will return `false` below, + // asking to be deferred. Therefore, it may take multiple iterations until we have + // successfully deserialized all inodes. + // (However serialized inodes are represented, it must be ensured that no loops occur in + // such dependencies.) + while !self.inodes.is_empty() { + let mut i = 0; + let mut processed_any = false; + while i < self.inodes.len() { + if self.inodes[i].deserialize_with_fs(fs)? { + // All good + self.inodes.swap_remove(i); + processed_any = true; + } else { + // Process this inode later (e.g. needs to resolve a reference to a parent node + // that has not yet been deserialized) + i += 1; + } + } + + if !processed_any { + return Err(other_io_error( + "Unresolved references between serialized inodes", + )); + } + } + + fs.next_inode.store(self.next_inode, Ordering::Relaxed); + + // Reconstruct handles (i.e., open those files) + *fs.handles.write().unwrap() = BTreeMap::new(); + for handle in self.handles { + handle.deserialize_with_fs(fs)?; + } + + fs.next_handle.store(self.next_handle, Ordering::Relaxed); + + debug!("deserialization apply finish"); + + Ok(()) + } +} + +impl serialized::NegotiatedOpts { + /// Apply the options negotiated with the guest on the source side to `fs`'s configuration + fn apply(self, fs: &PassthroughFs) -> io::Result<()> { + debug!("deserialization apply NegotiatedOpts"); + + if !fs.cfg.writeback && self.writeback { + return Err(other_io_error( + "Migration source wants writeback enabled, but it is disabled on the destination", + )); + } + // Note the case of `fs.cfg.writeback && !self.writeback`, i.e. the user asked for it to be + // enabled, but the migration source had it disabled: From a technical perspective, just + // disabling it here is fine, because that is what happens (and what we want to happen) + // when the guest does not support the flag (in which case there will already have been a + // warning on INIT). However, it is imaginable that the guest supports the flag, but it + // was user-disabled on the source (and is user-enabled now): We can't distinguish this + // case from the no-guest-support one, and disabling the flag is still the right thing to + // do, because we would need to re-negotiate through INIT first before we can enable it. + // Given that it would be strange for the user to use different configurations for source + // and destination, do not print a warning either. + fs.writeback.store(self.writeback, Ordering::Relaxed); + + if !fs.cfg.announce_submounts && self.announce_submounts { + return Err(other_io_error( + "Migration source wants announce-submounts enabled, but it is disabled on the \ + destination", + )); + } + // The comment from writeback applies here, too + fs.announce_submounts + .store(self.announce_submounts, Ordering::Relaxed); + + if !fs.cfg.posix_acl && self.posix_acl { + return Err(other_io_error( + "Migration source wants posix ACLs enabled, but it is disabled on the destination", + )); + } + // The comment from writeback applies here, too + fs.posix_acl.store(self.posix_acl, Ordering::Relaxed); + + fs.sup_group_extension + .store(self.sup_group_extension, Ordering::Relaxed); + + Ok(()) + } +} + +impl serialized::Inode { + /// Deserialize this inode into `fs`'s inode store. Return `Ok(true)` on success, `Err(_)` on + /// error, and `Ok(false)` when there is a dependency to another inode that has not yet been + /// deserialized, so deserialization should be re-attempted later. + fn deserialize_with_fs(&self, fs: &PassthroughFs) -> io::Result { + debug!( + "deserialization restore Inode, inode:{:?}, location:{:?}", + self.id, self.location + ); + + match &self.location { + serialized::InodeLocation::RootNode => { + if self.id != fuse::ROOT_ID { + return Err(other_io_error(format!( + "Node with non-root ID ({}) given as root node", + self.id + ))); + } + + // We open the root node ourselves (from the configuration the user gave us)... + fs.open_root_node()?; + // ...and only take the refcount from the source, ignoring filename and parent + // information. Note that we must not call `fs.open_root_node()` before we have + // the correct refcount, or deserializing child nodes (which drops one reference + // each) would quickly reduce the refcount below 0. + let root_data = fs.inodes.get(fuse::ROOT_ID).unwrap(); + root_data.refcount.store(self.refcount, Ordering::Relaxed); + + // For the root node, a non-matching file handle is always a hard error. We cannot + // deserialize the root node as an invalid node. + self.check_file_handle(&root_data)?; + + Ok(true) + } + + serialized::InodeLocation::Path { + parent, + filename, + fullname, + } => { + if self.id == fuse::ROOT_ID { + return Err(other_io_error( + "Refusing to use path given for root node".to_string(), + )); + } + + let parent_ref = match fs.inodes.get(*parent) { + None => { + // `parent` not found yet, defer deserialization until it is present + return Ok(false); + } + + Some(parent_data) => { + // Safe because the migration source guarantees that this reference is + // included in the parent node's refcount. Once we have deserialized this + // inode, we must drop that reference, and moving it into + // `deserialize_path()` will achieve that. + unsafe { StrongInodeReference::new_no_increment(parent_data, &fs.inodes) } + } + }; + + // Restore-time FilterList is authoritative for filter roots: same + // guest basename opens at the current allow_dir path. Children keep + // walking via parent fd and are not remapped here. + let (filename, is_filter) = + if !fullname.is_empty() && std::path::Path::new(fullname).is_absolute() { + (fs.remap_filter_fullname(self.id, fullname), true) + } else { + (filename.as_str(), false) + }; + + let inode_data = self + .deserialize_path(fs, parent_ref, filename) + .or_else(|err| self.deserialize_invalid_inode(fs, err))?; + + let inode_data = match self.check_file_handle(&inode_data) { + Ok(()) => inode_data, + Err(err) => self.deserialize_invalid_inode(fs, err)?, + }; + + fs.inodes.new_inode(inode_data)?; + if is_filter { + debug!( + "deserialization filter inode, id:{:?}, name:{:?}, fullname:{:?}", + self.id, filename, fullname + ); + // Write the remapped open path so the data plane follows + // the restore-time allow_dir, not the blob's stale fullname. + fs.filter + .write() + .unwrap() + .insert(self.id, (filename.to_string(), filename.to_string())); + } + + Ok(true) + } + + serialized::InodeLocation::Invalid => { + let err = io::Error::new( + io::ErrorKind::NotFound, + format!("Migration source has lost inode {}", self.id), + ); + let inode_data = self.deserialize_invalid_inode(fs, err)?; + fs.inodes.new_inode(inode_data)?; + Ok(true) + } + + serialized::InodeLocation::FullPath { filename } => { + if self.id == fuse::ROOT_ID { + return Err(other_io_error( + "Refusing to use path given for root node".to_string(), + )); + } + + let Ok(shared_dir) = fs.inodes.get_strong(fuse::ROOT_ID) else { + // No root node? Defer until we have it. + return Ok(false); + }; + + let inode_data = self + .deserialize_path(fs, shared_dir, filename) + .or_else(|err| self.deserialize_invalid_inode(fs, err))?; + + fs.inodes.new_inode(inode_data)?; + Ok(true) + } + } + } + + /// Helper function for `deserialize_with_fs()`: Try to locate an inode based on its parent + /// directory and its filename. + /// Takes ownership of the `parent` strong reference and drops it. + /// On success, returns `InodeData` to add to `fs.inodes`. + fn deserialize_path( + &self, + fs: &PassthroughFs, + parent: StrongInodeReference, + filename: &str, + ) -> io::Result { + debug!( + "deserialization restore Inode path, inode:{:?}, filename:{:?}", + self.id, filename + ); + + let parent_fd = parent.get().get_file()?; + let fd = openat( + &parent_fd, + filename, + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + ) + .map_err(|err| { + let pfd = printable_fd(&parent_fd, Some(&fs.proc_self_fd)); + io::Error::new( + err.kind(), + format!( + "Opening {}{}{}: {}", + pfd, + if pfd.ends_with('/') { "" } else { "/" }, + filename, + err + ), + ) + })?; + + let st = statx(&fd, None)?; + let handle = fs.get_file_handle_opt(&fd, &st)?; + + let file_or_handle = if let Some(h) = handle.as_ref() { + FileOrHandle::Handle(fs.make_file_handle_openable(h)?) + } else { + FileOrHandle::File(fd) + }; + + Ok(InodeData { + inode: self.id, + file_or_handle, + refcount: AtomicU64::new(self.refcount), + ids: InodeIds { + ino: st.st.st_ino, + dev: st.st.st_dev, + mnt_id: st.mnt_id, + }, + mode: st.st.st_mode, + migration_info: Mutex::new(None), + }) + } + + /// Helper function for `deserialize_with_fs()`: Handle invalid inodes, i.e. ones that cannot + /// be located. + /// Depending on the configuration, they either cause a hard error, or should be added as + /// explicitly invalid inodes to `fs.inodes` (in which case their `InodeData` is returned). + fn deserialize_invalid_inode( + &self, + fs: &PassthroughFs, + err: io::Error, + ) -> io::Result { + match fs.cfg.migration_on_error { + MigrationOnError::Abort => Err(err.context(format!("Inode {}", self.id))), + MigrationOnError::GuestError => { + warn!("Invalid inode {} indexed: {}", self.id, err); + Ok(InodeData { + inode: self.id, + file_or_handle: FileOrHandle::Invalid(Arc::new(err)), + refcount: AtomicU64::new(self.refcount), + ids: Default::default(), + mode: Default::default(), + migration_info: Default::default(), + }) + } + } + } + + /// If the source sent us a reference file handle, check it against `inode_data`'s file handle + fn check_file_handle(&self, inode_data: &InodeData) -> io::Result<()> { + let Some(ref_fh) = &self.file_handle else { + return Ok(()); + }; + + let is_fh: SerializableFileHandle = (&inode_data.file_or_handle).try_into()?; + // Disregard the mount ID, this may be a different host, so the mount ID may differ + is_fh.require_equal_without_mount_id(ref_fh).map_err(|err| { + other_io_error(format!( + "Inode {} is not the same inode as in the migration source: {}", + self.id, err + )) + }) + } +} + +impl serialized::Handle { + /// Deserialize this handle into `fs`'s handle map. + fn deserialize_with_fs(&self, fs: &PassthroughFs) -> io::Result<()> { + debug!( + "deserialization restore Handle, inode:{:?}, source:{:?}", + self.inode, self.source + ); + + let inode = fs + .inodes + .get(self.inode) + .ok_or_else(|| other_io_error(format!("Inode {} not found", self.inode)))?; + + let (file, migration_info) = match self.source { + serialized::HandleSource::OpenInode { flags } => { + let handle_data_file = match inode + .open_file(flags, &fs.proc_self_fd) + .and_then(|f| f.into_file()) + { + Ok(f) => HandleDataFile::File(RwLock::new(f)), + Err(err) => { + let error_msg = if let Ok(path) = inode.get_path(&fs.proc_self_fd) { + let p = path.as_c_str().to_string_lossy(); + format!( + "Opening inode {} ({}) as handle {}: {}", + self.inode, p, self.id, err + ) + } else { + format!( + "Opening inode {} as handle {}: {}", + self.inode, self.id, err + ) + }; + let err = io::Error::new(err.kind(), error_msg); + match fs.cfg.migration_on_error { + MigrationOnError::Abort => return Err(err), + MigrationOnError::GuestError => { + warn!("Invalid handle {} is open in guest: {}", self.id, err); + HandleDataFile::Invalid(Arc::new(err)) + } + } + } + }; + let migration_info = HandleMigrationInfo::OpenInode { flags }; + (handle_data_file, migration_info) + } + }; + + let handle_data = HandleData { + inode: self.inode, + file, + migration_info, + }; + fs.handles + .write() + .unwrap() + .insert(self.id, Arc::new(handle_data)); + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::filesystem::SerializableFileSystem; + use crate::fuse; + use crate::passthrough::device_state::serialized; + use crate::passthrough::Config; + use std::collections::HashMap; + use std::fs; + use std::path::{Path, PathBuf}; + use std::sync::atomic::{AtomicU64, Ordering}; + use std::time::{SystemTime, UNIX_EPOCH}; + + const FILTER_ID: u64 = 2; + const CHILD_ID: u64 = 3; + + static FIXTURE_SEQ: AtomicU64 = AtomicU64::new(0); + + struct TestDir { + path: PathBuf, + } + + impl TestDir { + fn new(test_name: &str) -> Self { + let pid = std::process::id(); + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or(0); + let seq = FIXTURE_SEQ.fetch_add(1, Ordering::Relaxed); + let base = std::env::temp_dir() + .join(format!("virtiofsd-remap-{pid}-{nanos}-{seq}-{test_name}")); + fs::create_dir_all(&base).unwrap(); + TestDir { + path: fs::canonicalize(&base).unwrap(), + } + } + + fn path(&self) -> &Path { + &self.path + } + + fn mkdir(&self, rel: &str) -> PathBuf { + let full = self.path.join(rel); + fs::create_dir_all(&full).unwrap(); + fs::canonicalize(&full).unwrap() + } + + fn touch(&self, rel: &str) { + let full = self.path.join(rel); + if let Some(parent) = full.parent() { + fs::create_dir_all(parent).unwrap(); + } + fs::File::create(&full).unwrap(); + } + } + + impl Drop for TestDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.path); + } + } + + fn mk_fs(root: &Path) -> PassthroughFs { + let mut cfg = Config::default(); + cfg.root_dir = root.to_str().unwrap().to_string(); + cfg.migration_verify_handles = false; + cfg.migration_on_error = MigrationOnError::Abort; + PassthroughFs::new(cfg).unwrap() + } + + fn apply_mnt_tree(fs: &PassthroughFs, mnt_fullname: &str) { + serialized::PassthroughFsV1 { + inodes: vec![ + serialized::Inode { + id: fuse::ROOT_ID, + // +1 for the filter inode's Path.parent strong ref + refcount: 2, + location: serialized::InodeLocation::RootNode, + file_handle: None, + }, + serialized::Inode { + id: FILTER_ID, + // +1 for the child inode's Path.parent strong ref + refcount: 2, + location: serialized::InodeLocation::Path { + parent: fuse::ROOT_ID, + filename: "mnt".into(), + fullname: mnt_fullname.to_string(), + }, + file_handle: None, + }, + serialized::Inode { + id: CHILD_ID, + refcount: 1, + location: serialized::InodeLocation::Path { + parent: FILTER_ID, + filename: "file".into(), + fullname: String::new(), + }, + file_handle: None, + }, + ], + next_inode: 4, + handles: Vec::new(), + next_handle: 0, + negotiated_opts: serialized::NegotiatedOpts { + writeback: false, + announce_submounts: false, + posix_acl: false, + sup_group_extension: false, + }, + } + .apply(fs) + .expect("apply failed"); + } + + fn host_path(fs: &PassthroughFs, inode: u64) -> PathBuf { + let data = fs.inodes.get(inode).expect("inode missing"); + let cpath = data.get_path(&fs.proc_self_fd).expect("get_path failed"); + PathBuf::from(cpath.to_str().unwrap()) + } + + fn filter_original_path(fs: &PassthroughFs, inode: u64) -> String { + fs.filter + .read() + .unwrap() + .get(&inode) + .map(|(_, original)| original.clone()) + .expect("filter entry missing") + } + + #[test] + fn remap_hit_opens_new_tree() { + let shared = TestDir::new("hit-shared"); + let old = TestDir::new("hit-old"); + let new = TestDir::new("hit-new"); + old.mkdir("mnt"); + old.touch("mnt/file"); + let new_mnt = new.mkdir("mnt"); + new.touch("mnt/file"); + + let fs = mk_fs(shared.path()); + let mut remap = HashMap::new(); + remap.insert("mnt".into(), new_mnt.to_str().unwrap().to_string()); + fs.set_filter_path_remap(remap); + + let old_mnt = old.path().join("mnt"); + apply_mnt_tree(&fs, old_mnt.to_str().unwrap()); + + assert_eq!( + filter_original_path(&fs, FILTER_ID), + new_mnt.to_str().unwrap() + ); + assert_eq!(host_path(&fs, FILTER_ID), new_mnt); + assert_eq!(host_path(&fs, CHILD_ID), new_mnt.join("file")); + } + + #[test] + fn remap_uses_fullname_basename_after_restore() { + let shared = TestDir::new("restored-shared"); + let old = TestDir::new("restored-old"); + let new = TestDir::new("restored-new"); + let old_mnt = old.mkdir("mnt"); + let new_mnt = new.mkdir("mnt"); + + let fs = mk_fs(shared.path()); + let mut remap = HashMap::new(); + remap.insert("mnt".into(), new_mnt.to_str().unwrap().to_string()); + fs.set_filter_path_remap(remap); + + assert_eq!( + fs.remap_filter_fullname(FILTER_ID, old_mnt.to_str().unwrap()), + new_mnt.to_str().unwrap() + ); + } + + #[test] + fn remap_miss_keeps_blob_fullname() { + let shared = TestDir::new("miss-shared"); + let old = TestDir::new("miss-old"); + old.mkdir("mnt"); + old.touch("mnt/file"); + let old_mnt = old.path().join("mnt"); + + let fs = mk_fs(shared.path()); + apply_mnt_tree(&fs, old_mnt.to_str().unwrap()); + + let old_mnt = fs::canonicalize(&old_mnt).unwrap(); + assert_eq!( + filter_original_path(&fs, FILTER_ID), + old_mnt.to_str().unwrap() + ); + assert_eq!(host_path(&fs, FILTER_ID), old_mnt); + assert_eq!(host_path(&fs, CHILD_ID), old_mnt.join("file")); + } + + #[test] + fn remap_identity_is_noop() { + let shared = TestDir::new("id-shared"); + let old = TestDir::new("id-old"); + old.mkdir("mnt"); + old.touch("mnt/file"); + let old_mnt = fs::canonicalize(old.path().join("mnt")).unwrap(); + + let fs = mk_fs(shared.path()); + let mut remap = HashMap::new(); + remap.insert("mnt".into(), old_mnt.to_str().unwrap().to_string()); + fs.set_filter_path_remap(remap); + apply_mnt_tree(&fs, old_mnt.to_str().unwrap()); + + assert_eq!( + filter_original_path(&fs, FILTER_ID), + old_mnt.to_str().unwrap() + ); + assert_eq!(host_path(&fs, FILTER_ID), old_mnt); + } + + #[test] + fn remap_ignores_non_filter_child() { + let shared = TestDir::new("child-shared"); + shared.touch("file"); + let decoy = TestDir::new("child-decoy"); + decoy.touch("file"); + + let fs = mk_fs(shared.path()); + let mut remap = HashMap::new(); + remap.insert( + "file".into(), + decoy.path().join("file").to_str().unwrap().to_string(), + ); + fs.set_filter_path_remap(remap); + + serialized::PassthroughFsV1 { + inodes: vec![ + serialized::Inode { + id: fuse::ROOT_ID, + refcount: 2, + location: serialized::InodeLocation::RootNode, + file_handle: None, + }, + serialized::Inode { + id: CHILD_ID, + refcount: 1, + location: serialized::InodeLocation::Path { + parent: fuse::ROOT_ID, + filename: "file".into(), + fullname: String::new(), + }, + file_handle: None, + }, + ], + next_inode: 4, + handles: Vec::new(), + next_handle: 0, + negotiated_opts: serialized::NegotiatedOpts { + writeback: false, + announce_submounts: false, + posix_acl: false, + sup_group_extension: false, + }, + } + .apply(&fs) + .unwrap(); + + assert_eq!(host_path(&fs, CHILD_ID), shared.path().join("file")); + assert!(fs.filter.read().unwrap().is_empty()); + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/mod.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/mod.rs new file mode 100644 index 0000000..bcf74ba --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/mod.rs @@ -0,0 +1,196 @@ +// Copyright 2024 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/** + * Module for migrating our internal FS state (i.e. serializing and deserializing it), with the + * following submodules: + * - serialized: Serialized data structures + * - preserialization: Structures and functionality for preparing for migration (serialization), + * i.e. define and construct the precursors to the eventually serialized + * information that are stored alongside the associated inodes and handles they + * describe + * - serialization: Functionality for serializing + * - deserialization: Functionality for deserializing + */ +mod deserialization; +pub(super) mod preserialization; +mod serialization; +mod serialized; + +use crate::filesystem::SerializableFileSystem; +use crate::passthrough::PassthroughFs; +use preserialization::{InodeMigrationInfoConstructor, PathReconstructor, StoreOnlyReconstructor}; +use std::convert::{TryFrom, TryInto}; +use std::fs::File; +use std::io::{self, Read, Write}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; + +/// Adds serialization (migration) capabilities to `PassthroughFs` +impl SerializableFileSystem for PassthroughFs { + fn prepare_serialization(&self, cancel: Arc) -> io::Result<()> { + self.inodes.clear_migration_info(); + + // Set this so the filesystem code knows that every node is supposed to have up-to-date + // migration information. For example, nodes that are created after they would have been + // visited by the reconstructor below will not get migration info, unless the general + // filesystem code makes an effort to set it (when the node is created). + self.track_migration_info.store(true, Ordering::Relaxed); + + // Pick a reconstructor. The new "store-only" implementation is far cheaper for big + // shared directories (and especially for slow backends like NFS); the legacy + // `PathReconstructor` (full-tree DFS) is kept as a fallback that can be re-enabled via + // configuration. + let result = if self.cfg.migration_dfs_preserialization { + let reconstructor = PathReconstructor::new(self, cancel); + reconstructor.execute() + } else { + let reconstructor = StoreOnlyReconstructor::new(self, cancel); + reconstructor.execute() + }; + if result.is_err() { + // Do not leave incomplete data behind (cancelling returns an error, too, landing here) + self.inodes.clear_migration_info(); + } + result + } + + fn serialize_data(&self) -> io::Result> { + self.track_migration_info.store(false, Ordering::Relaxed); + + let result = (|| { + let state = serialized::PassthroughFs::V1(self.try_into()?); + let serialized: Vec = state.try_into()?; + Ok(serialized) + })(); + + self.inodes.clear_migration_info(); + result + } + + fn serialize(&self, mut state_pipe: File) -> io::Result<()> { + let serialized: Vec = self.serialize_data()?; + state_pipe.write_all(&serialized)?; + Ok(()) + } + + fn deserialize_and_apply_data(&self, serialized: &Vec) -> io::Result<()> { + match serialized::PassthroughFs::try_from(serialized)? { + serialized::PassthroughFs::V1(state) => state.apply(self)?, + }; + Ok(()) + } + + fn deserialize_and_apply(&self, mut state_pipe: File) -> io::Result<()> { + let mut serialized: Vec = Vec::new(); + state_pipe.read_to_end(&mut serialized)?; + self.deserialize_and_apply_data(&serialized) + } + + fn set_filter_path_remap(&self, remap: std::collections::HashMap) { + let _ = self.restore_filter_remap.set(remap); + } +} + +#[cfg(test)] +mod tests { + use super::preserialization::{InodeLocation, InodeMigrationInfo}; + use super::serialized; + use crate::filesystem::SerializableFileSystem; + use crate::fuse; + use crate::passthrough::file_handle::FileOrHandle; + use crate::passthrough::inode_store::{InodeData, InodeIds}; + use crate::passthrough::{Config, PassthroughFs}; + use std::convert::TryFrom; + use std::fs::{self, File}; + use std::path::{Path, PathBuf}; + use std::sync::atomic::AtomicU64; + use std::sync::Mutex; + use std::time::{SystemTime, UNIX_EPOCH}; + + struct TestDir { + path: PathBuf, + } + + impl TestDir { + fn new(name: &str) -> Self { + let unique = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + let path = std::env::temp_dir().join(format!( + "virtiofsd-device-state-{name}-{}-{unique}", + std::process::id() + )); + fs::create_dir_all(&path).unwrap(); + Self { path } + } + + fn path(&self) -> &Path { + &self.path + } + } + + impl Drop for TestDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.path); + } + } + + fn new_passthrough_fs(root: &Path) -> PassthroughFs { + PassthroughFs::new(Config { + root_dir: root.to_string_lossy().into_owned(), + ..Default::default() + }) + .unwrap() + } + + fn set_root_migration_info(fs: &PassthroughFs) { + let root = fs.inodes.get(fuse::ROOT_ID).unwrap(); + *root.migration_info.lock().unwrap() = Some(InodeMigrationInfo { + location: InodeLocation::RootNode, + file_handle: None, + }); + } + + #[test] + fn serialize_data_marks_non_root_inode_invalid_when_location_is_missing() { + let test_dir = TestDir::new("missing-child-info"); + let child_path = test_dir.path().join("child"); + fs::write(&child_path, b"child").unwrap(); + + let fs = new_passthrough_fs(test_dir.path()); + fs.open_root_node().unwrap(); + set_root_migration_info(&fs); + fs.inodes + .new_inode(InodeData { + inode: 2, + file_or_handle: FileOrHandle::File(File::open(&child_path).unwrap()), + refcount: AtomicU64::new(1), + ids: InodeIds { + ino: 2, + dev: 3, + mnt_id: 4, + }, + mode: libc::S_IFREG as u32, + migration_info: Mutex::new(None), + }) + .unwrap(); + + let data = fs.serialize_data().unwrap(); + let serialized = serialized::PassthroughFs::try_from(&data).unwrap(); + let serialized::PassthroughFs::V1(state) = serialized; + + let child = state.inodes.iter().find(|inode| inode.id == 2).unwrap(); + assert!(matches!(child.location, serialized::InodeLocation::Invalid)); + let root = state + .inodes + .iter() + .find(|inode| inode.id == fuse::ROOT_ID) + .unwrap(); + assert!(matches!(root.location, serialized::InodeLocation::RootNode)); + let root = fs.inodes.get(fuse::ROOT_ID).unwrap(); + assert!(root.migration_info.lock().unwrap().is_none()); + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/preserialization.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/preserialization.rs new file mode 100644 index 0000000..ad151a5 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/preserialization.rs @@ -0,0 +1,1781 @@ +// Copyright 2024 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use crate::filesystem::DirectoryIterator; +use crate::fuse; +use crate::passthrough::file_handle::{FileHandle, SerializableFileHandle}; +use crate::passthrough::inode_store::{Inode, InodeData, InodeIds, StrongInodeReference}; +use crate::passthrough::stat::statx; +use crate::passthrough::{self, FileOrHandle, PassthroughFs}; +use crate::read_dir::ReadDir; +use crate::util::other_io_error; +use std::collections::HashMap; +use std::convert::TryInto; +use std::ffi::CStr; +use std::fs::File; +use std::io; +use std::os::unix::io::{AsRawFd, FromRawFd}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::{Arc, Mutex}; + +/// Precursor to `serialized::Inode` that is constructed while serialization is being prepared, and +/// will then be transformed into the latter at the time of serialization. To be stored in the +/// inode store, alongside each inode (i.e. in its `InodeData`). Constructing this is costly, so +/// should only be done when necessary, i.e. when actually preparing for migration. +pub(in crate::passthrough) struct InodeMigrationInfo { + /// Location of the inode (how the destination can find it) + pub(in crate::passthrough) location: InodeLocation, + + /// The inode's file handle. The destination is not supposed to open this handle, but instead + /// compare it against the one from the inode it has opened based on `location`. + pub(in crate::passthrough) file_handle: Option, +} + +pub(in crate::passthrough) enum InodeLocation { + /// The root node: No information is stored, the destination is supposed to find this on its + /// own (as configured by the user) + RootNode, + + /// Inode is represented by its parent directory and its filename therein, allowing the + /// destination to `openat(2)` it + Path { + parent: StrongInodeReference, + filename: String, + fullname: String, + }, +} + +/// Precursor to `SerializableHandleRepresentation` that is constructed while serialization is +/// being prepared, and will then be transformed into the latter at the time of serialization. +/// To be stored in the `handles` map, alongside each handle (i.e. in its `HandleData`). +/// Constructing this is cheap, so can be done whenever any handle is created. +pub(in crate::passthrough) enum HandleMigrationInfo { + /// Handle can be opened by opening its associated inode with the given `open(2)` flags + OpenInode { flags: i32 }, +} + +/// Stores state for constructing serializable data for inodes using the `InodeMigrationInfo::Path` +/// variant, in order to prepare for migration. +pub(super) struct PathReconstructor<'a> { + /// Reference to the filesystem for which to reconstruct inodes' paths. + fs: &'a PassthroughFs, + /// Set to true when we are supposed to cancel + cancel: Arc, +} + +/// Constructs `InodeMigrationInfo` data for every inode in the inode store. This may take a long +/// time, and is the core part of our preserialization phase. +/// Different implementations of this trait can create different variants of the +/// `InodeMigrationInfo` enum. +pub(super) trait InodeMigrationInfoConstructor { + /// Runs the constructor + fn execute(self) -> io::Result<()>; +} + +impl InodeMigrationInfo { + /// Create the migration info for an inode that is collected during the `prepare_serialization` + /// phase + pub(in crate::passthrough) fn new( + fs_cfg: &passthrough::Config, + parent_ref: StrongInodeReference, + filename: &CStr, + fullname: String, + file_or_handle: &FileOrHandle, + ) -> io::Result { + let utf8_name = filename.to_str().map_err(|err| { + other_io_error(format!( + "Cannot convert filename into UTF-8: {filename:?}: {err}" + )) + })?; + + Self::new_with_utf8_name(fs_cfg, parent_ref, utf8_name, fullname, file_or_handle) + } + + fn new_with_utf8_name( + fs_cfg: &passthrough::Config, + parent_ref: StrongInodeReference, + filename: &str, + fullname: String, + file_or_handle: &FileOrHandle, + ) -> io::Result { + let file_handle: Option = if fs_cfg.migration_verify_handles { + Some(file_or_handle.try_into()?) + } else { + None + }; + + Ok(InodeMigrationInfo { + location: InodeLocation::Path { + parent: parent_ref, + fullname, + filename: filename.to_string(), + }, + file_handle, + }) + } +} + +impl HandleMigrationInfo { + /// Create the migration info for a handle that will be required when serializing + pub(in crate::passthrough) fn new(flags: i32) -> Self { + HandleMigrationInfo::OpenInode { + // Remove flags that make sense when the file is first opened by the guest, but which + // we should not set when continuing to use the file after migration because they would + // e.g. modify the file + flags: flags & !(libc::O_CREAT | libc::O_EXCL | libc::O_TRUNC), + } + } +} + +/// The `PathReconstructor` is an `InodeMigrationInfoConstructor` that creates `InodeMigrationInfo` +/// of the `InodeMigrationInfo::Path` variant: It recurses through the filesystem (i.e. the shared +/// directory), matching up all inodes it finds with our inode store, and thus finds the parent +/// directory node and filename for every such inode. +impl<'a> PathReconstructor<'a> { + pub(super) fn new(fs: &'a PassthroughFs, cancel: Arc) -> Self { + PathReconstructor { fs, cancel } + } + + /// Recurse from the given directory inode + fn recurse_from(&self, root_ref: StrongInodeReference) -> io::Result<()> { + let mut dir_buf = vec![0u8; 1024]; + + // We don't actually use recursion (to not exhaust the stack), but keep a list of + // directories we still need to visit, and pop from it until it is empty and we're done + let mut remaining_dirs = vec![root_ref]; + while let Some(inode_ref) = remaining_dirs.pop() { + let dirfd = inode_ref.get().open_file( + libc::O_RDONLY | libc::O_NOFOLLOW | libc::O_CLOEXEC, + &self.fs.proc_self_fd, + )?; + + // Read all directory entries, check them for matches in our inode store, and add any + // directory to `remaining_dirs` + loop { + // Safe because we use nothing but this function on the FD + let mut entries = unsafe { ReadDir::new_no_seek(&dirfd, dir_buf.as_mut()) }?; + if entries.remaining() == 0 { + break; + } + + while let Some(entry) = entries.next() { + debug!("rescore discover {:?}", entry.name); + if self.cancel.load(Ordering::Relaxed) { + return Err(other_io_error("Cancelled serialization preparation")); + } + + debug!( + "preserialization discover inode, name:{:?} root:{:?}", + entry.name, + inode_ref.get().inode + ); + + if let Some(entry_inode) = self.discover(&inode_ref, &dirfd, entry.name)? { + // Add directories to visit to the list + remaining_dirs.push(entry_inode); + } + } + } + } + + Ok(()) + } + + /// Check the given directory entry (parent + name) for matches in our inode store. If we find + /// any corresponding `InodeData` there, its `.migration_info` is set accordingly. + /// For all directories (and directories only), return a strong reference to an inode in our + /// store that can be used to recurse further. + fn discover( + &self, + parent_reference: &StrongInodeReference, + parent_fd: &F, + name: &CStr, + ) -> io::Result> { + let utf8_name = name.to_str().map_err(|err| { + other_io_error(format!( + "Cannot convert filename into UTF-8: {name:?}: {err}", + )) + })?; + + // Ignore these + if utf8_name == "." || utf8_name == ".." { + return Ok(None); + } + + let path_fd = { + let fd = self + .fs + .open_relative_to(parent_fd, name, libc::O_PATH, None)?; + unsafe { File::from_raw_fd(fd) } + }; + let stat = statx(&path_fd, None)?; + let handle = self.fs.get_file_handle_opt(&path_fd, &stat)?; + + let ids = InodeIds { + ino: stat.st.st_ino, + dev: stat.st.st_dev, + mnt_id: stat.mnt_id, + }; + + let is_directory = stat.st.st_mode & libc::S_IFMT == libc::S_IFDIR; + + if let Ok(inode_ref) = self.fs.inodes.claim_inode(handle.as_ref(), &ids) { + let file_handle = if self.fs.cfg.migration_verify_handles { + Some(match &handle { + Some(h) => h.into(), + None => FileHandle::from_fd_fail_hard(&path_fd)?.into(), + }) + } else { + None + }; + + let mig_info = InodeMigrationInfo { + location: InodeLocation::Path { + parent: StrongInodeReference::clone(parent_reference), + filename: utf8_name.to_string(), + fullname: String::new(), + }, + file_handle, + }; + + *inode_ref.get().migration_info.lock().unwrap() = Some(mig_info); + + return Ok(is_directory.then_some(inode_ref)); + } + + // For readonly shared mount, we are done. Cause any live dentry will hold a reference to + // it's parent. + if self.fs.cfg.read_only { + debug!( + "preserialization discover child: name:{:?} is_dir:{:?} is done", + utf8_name, is_directory + ); + return Ok(None); + } + + // We did not find a matching entry in our inode store. In case of non-directories, we are + // done. + if !is_directory { + return Ok(None); + } + + // However, in case of directories, we must create an entry, so we can return it. + // (Our inode store may still have matching entries recursively downwards from this + // directory. Because every node is serialized referencing its parent, this directory + // inode may end up being recursively referenced this way, we don't know yet. + // In case there is no such entry, the refcount will eventually return to 0 before + // `Self::execute()` returns, dropping it from the inode store again, so it will not + // actually end up being serialized.) + + let file_or_handle = if let Some(h) = handle.as_ref() { + debug!("discover new handle for {:?}", utf8_name); + FileOrHandle::Handle(self.fs.make_file_handle_openable(h)?) + } else { + debug!("discover new file for {:?}", utf8_name); + FileOrHandle::File(path_fd) + }; + + let mig_info = InodeMigrationInfo::new_with_utf8_name( + &self.fs.cfg, + StrongInodeReference::clone(parent_reference), + utf8_name, + String::new(), + &file_or_handle, + )?; + + let new_inode = InodeData { + inode: self.fs.next_inode.fetch_add(1, Ordering::Relaxed), + file_or_handle, + refcount: AtomicU64::new(1), + ids, + mode: stat.st.st_mode, + migration_info: Mutex::new(Some(mig_info)), + }; + + Ok(Some(self.fs.inodes.get_or_insert(new_inode)?)) + } +} + +impl InodeMigrationInfoConstructor for PathReconstructor<'_> { + /// Recurse from the root directory (the shared directory) + fn execute(self) -> io::Result<()> { + if let Ok(root) = self.fs.inodes.get_strong(fuse::ROOT_ID) { + // Root node is special in that the destination gets no information on how to find it, + // because that is configured by the user + *root.get().migration_info.lock().unwrap() = Some(InodeMigrationInfo { + location: InodeLocation::RootNode, + file_handle: if self.fs.cfg.migration_verify_handles { + Some((&root.get().file_or_handle).try_into()?) + } else { + None + }, + }); + + if !self.fs.filter.read().unwrap().is_empty() { + for (id, (filename, fullname)) in self.fs.filter.read().unwrap().iter() { + debug!( + "preserialization filter inode, id:{:?}, name:{:?}, fullname:{:?}", + id, filename, fullname + ); + if let Ok(inode) = self.fs.inodes.get_strong(*id) { + *inode.get().migration_info.lock().unwrap() = Some(InodeMigrationInfo { + location: InodeLocation::Path { + parent: StrongInodeReference::clone(&root), + filename: filename.clone(), + fullname: fullname.clone(), + }, + file_handle: if self.fs.cfg.migration_verify_handles { + Some((&inode.get().file_or_handle).try_into()?) + } else { + None + }, + }); + if inode.get().mode & libc::S_IFMT == libc::S_IFDIR { + debug!( + "preserialization filter tree, id: {:?}, name: {:?}, fullname: {:?}", + id, filename, fullname + ); + self.recurse_from(inode)?; + } + } + } + Ok(()) + } else { + self.recurse_from(root) + } + } else { + // No root node? Then the filesystem is not mounted and we do not need to do anything. + Ok(()) + } + } +} + +/// The `StoreOnlyReconstructor` is an `InodeMigrationInfoConstructor` that creates +/// `InodeMigrationInfo` of the `InodeMigrationInfo::Path` variant *without* recursing through the +/// shared directory on disk. +/// +/// Instead, it: +/// 1. Iterates the inode store exactly once, calling `readlinkat("/proc/self/fd/")` for +/// each inode to obtain its absolute path. The cost of this step is not uniform: for inodes +/// whose `file_or_handle` is `FileOrHandle::File`, the readlink is a pure procfs read; for +/// `FileOrHandle::Handle` inodes, `InodeData::get_path()` first calls `Handle::open(O_PATH)` +/// via `open_by_handle_at(2)` to materialize a temporary FD on the underlying filesystem, +/// which does touch the backing FS (this is what kept it cheap relative to a full DFS on +/// slow backends like NFS: bounded by inode-store size, not on-disk tree size). +/// 2. Sorts the collected (inode_id, abs_path) list by path-byte-length so that any parent's +/// entry is processed before its children. This relies on the invariant that within a +/// single mount namespace an ancestor's absolute byte-path is strictly shorter than any +/// of its descendants' (no descendant can collapse to a shorter path under normal POSIX +/// semantics). Equal-length entries cannot have an ancestor/descendant relationship, so +/// any tie-breaking order is acceptable. +/// 3. Streams through the sorted list once more, building a `path -> inode_id` lookup table +/// incrementally and using it to resolve each inode's parent inode_id by string-splitting +/// its own absolute path. If a child's parent path is not yet in the lookup table (e.g. the +/// parent directory was forgotten by the guest while the child remained open), the parent +/// chain is materialized on demand via `openat(O_PATH | O_NOFOLLOW)` starting from the +/// nearest known anchor (root or filter inode) and walking strictly downwards. Each +/// synthesized parent is registered into the inode store via `inodes.get_or_insert()` with +/// a real `file_or_handle` and `ids`, so it is semantically identical to a parent inode +/// that would have been (re)discovered by `PathReconstructor`. This keeps the cost bounded +/// by the *depth* of the missing parent chain, not the breadth of the shared directory. +/// +/// Compared to `PathReconstructor`, the cost goes from O(size of shared directory tree) down to +/// O(size of inode store) + O(depth of any missing parent chains), which is typically orders of +/// magnitude smaller in production. +/// +/// Filter inodes are handled with the legacy semantics (their `fullname` is an absolute path that +/// is *not* under the shared directory, so the generic absolute-path-relative-to-shared-dir +/// reconstruction does not apply to them). +pub(super) struct StoreOnlyReconstructor<'a> { + fs: &'a PassthroughFs, + cancel: Arc, +} + +impl<'a> StoreOnlyReconstructor<'a> { + pub(super) fn new(fs: &'a PassthroughFs, cancel: Arc) -> Self { + StoreOnlyReconstructor { fs, cancel } + } + + /// Set the migration info for the root node. + fn set_root_info(&self, root: &StrongInodeReference) -> io::Result<()> { + *root.get().migration_info.lock().unwrap() = Some(InodeMigrationInfo { + location: InodeLocation::RootNode, + file_handle: if self.fs.cfg.migration_verify_handles { + Some((&root.get().file_or_handle).try_into()?) + } else { + None + }, + }); + Ok(()) + } + + /// Strip a single trailing '/' (if any) from a path, *unless* the path is exactly "/". + /// This keeps seed keys aligned with the parent slice computed in step 5 + /// (`abs_path[..rposition('/')]`), which never carries a trailing slash. Without this, + /// `readlink()`-derived paths like ".../sionli-test/" would fail to match per-inode + /// procfs-derived parent slices like ".../sionli-test". + fn normalize_path_bytes(mut bytes: Vec) -> Vec { + while bytes.len() > 1 && *bytes.last().unwrap() == b'/' { + bytes.pop(); + } + bytes + } + + /// Build a *migration-local* `filter_inode_id -> resolved_host_path` map. + /// + /// For every filter entry (`filename`, `original_path`) we perform a one-off + /// `readlink(original_path)` into this local map only. We *never* mutate `fs.filter`. + /// The resolved path is kept as `Vec` (raw OS bytes) and only used as a lookup + /// key in `path_to_inode`. Errors (broken symlink, non-symlink directory, target + /// missing, etc.) fall back to the original path bytes. + fn build_filter_resolved_map(&self) -> std::collections::HashMap> { + use std::os::unix::ffi::OsStringExt; + + let mut out: std::collections::HashMap> = std::collections::HashMap::new(); + let filter_map = self.fs.filter.read().unwrap(); + for (id, (_filename, original_path)) in filter_map.iter() { + match std::fs::read_link(original_path.as_str()) { + Ok(path_buf) => { + let bytes = path_buf.into_os_string().into_vec(); + debug!( + "store_only preserialization: locally resolved filter symlink, \ + inode:{:?}, original:{:?}, resolved_bytes_len:{}", + id, + original_path, + bytes.len() + ); + out.insert(*id, bytes); + } + Err(err) => { + // Match the legacy fallback: if readlink fails (or the entry isn't + // actually a symlink), use the original path as-is. The seed pass below + // is tolerant of this not matching any inode's procfs path -- it only + // means descendants of this filter can't be reverse-resolved, which is + // the same outcome as before. + debug!( + "store_only preserialization: readlink on filter entry failed, \ + inode:{:?}, original:{:?}, err:{}; falling back to original path", + id, original_path, err + ); + out.insert(*id, original_path.as_bytes().to_vec()); + } + } + } + out + } + + /// On-demand allocate inode store entries for every directory component along + /// `child_parent_path` that is not yet known in `path_to_inode`, by walking down from the + /// closest known ancestor with `openat(O_PATH | O_NOFOLLOW)` and registering each + /// intermediate directory with full `InodeData` + `migration_info`. + /// + /// This mirrors what the legacy `PathReconstructor::discover()` does when it walks into a + /// directory that is not yet in the inode store: it creates a real entry (with a real fd + /// or file handle) and recurses. The difference is that here we drive the creation + /// *bottom-up* from a child that already has a known absolute path, instead of recursing + /// top-down through every directory on disk. + /// + /// Returns `Ok(true)` if the chain was fully materialized (the caller's parent is now in + /// `path_to_inode`); returns `Ok(false)` if no known ancestor was found (cannot happen in + /// practice as the shared-dir root is always seeded, but we tolerate it defensively); and + /// returns `Err` on hard IO failures only when `migration_verify_handles` is enabled + /// (otherwise hard failures degrade gracefully into "parent not found" + skip, matching + /// the legacy reconstructor's behaviour on transient errors). + /// + /// The byte slice `child_parent_path` must be the absolute path of the parent we need to + /// materialize (i.e. the result of stripping the trailing `/` off the child's + /// own absolute path). It must already be normalized via `normalize_path_bytes` (no + /// trailing slash unless it is exactly `/`). + /// + /// `holding_pen` is a caller-owned vector into which this function pushes a strong + /// reference for every newly synthesized intermediate inode. The caller MUST keep this + /// vector alive for the entire main reverse-lookup loop in `execute()`. Without this + /// scaffolding, a freshly-inserted intermediate inode (returned by `claim_inode` or + /// `get_or_insert` with `refcount = 1`) would be dropped at the end of this function and + /// the inode store would forget it before the main loop has a chance to take its own + /// strong reference via `inodes.get_strong(parent_id)`. The destination would then lose + /// the descendant inode entirely. Once the main loop's `get_strong` succeeds and stores + /// the parent reference inside the descendant's `migration_info.location.parent`, the + /// holding-pen reference becomes redundant; dropping the vector after the main loop lets + /// any intermediate that ended up with no descendants fall back to refcount 0 and be + /// evicted, matching the legacy `PathReconstructor`'s contract. + fn ensure_ancestor_chain( + &self, + child_parent_path: &[u8], + path_to_inode: &mut HashMap, Inode>, + holding_pen: &mut Vec, + ) -> io::Result { + // Find the longest prefix of `child_parent_path` that already exists in + // `path_to_inode`. We scan from the longest possible prefix (the full path itself, + // which by definition is *not* there -- that is why we were called) downwards by + // truncating at successive '/' boundaries. + // + // We collect the path components we need to traverse on the way down into + // `missing_components`, ordered from closest-to-anchor first to deepest last. + let mut missing_components: Vec<&[u8]> = Vec::new(); + let mut anchor_path: Vec = Vec::new(); + let mut anchor_inode: Option = None; + + // Walk from `child_parent_path` upward, splitting at '/' boundaries. + let mut cursor: &[u8] = child_parent_path; + loop { + if let Some(&id) = path_to_inode.get(cursor) { + anchor_path = cursor.to_vec(); + anchor_inode = Some(id); + break; + } + // Could not find this prefix; descend to its parent by stripping the last + // component. If we cannot strip further (cursor is "/" or has no '/'), give up. + match cursor.iter().rposition(|&b| b == b'/') { + Some(0) => { + // The parent of `cursor` is "/" itself. Check if "/" is seeded. + if let Some(&id) = path_to_inode.get(&b"/"[..]) { + anchor_path = b"/".to_vec(); + anchor_inode = Some(id); + // The missing component is everything after the leading '/'. + missing_components.push(&cursor[1..]); + } + break; + } + Some(p) => { + // Component after the '/' is missing; record it and continue upward. + missing_components.push(&cursor[p + 1..]); + cursor = &cursor[..p]; + } + None => { + // Malformed (no '/' at all and not in the table); cannot anchor. + break; + } + } + } + + let anchor_inode = match anchor_inode { + Some(id) => id, + None => return Ok(false), + }; + + // Reverse so we walk from anchor downwards. + missing_components.reverse(); + + if missing_components.is_empty() { + // Already known; nothing to do. + return Ok(true); + } + + // Open the anchor's fd once and walk down component-by-component. + let anchor_data = match self.fs.inodes.get(anchor_inode) { + Some(d) => d, + None => { + debug!( + "store_only preserialization: anchor inode {} disappeared from store while \ + synthesizing parent chain for {:?}; skipping", + anchor_inode, + std::str::from_utf8(child_parent_path).unwrap_or(""), + ); + return Ok(false); + } + }; + // Open the anchor's fd once and walk down component-by-component. We keep + // `current_fd` as a plain `File` (rather than `InodeFile<'static>`) so we can freely + // move it across iterations and `try_clone()` it when we need a copy for the new + // inode's `FileOrHandle::File` variant. + let mut current_fd: File = anchor_data + .open_file( + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + &self.fs.proc_self_fd, + )? + .into_file()?; + let mut current_path = anchor_path; + let mut current_inode = anchor_inode; + + for component in missing_components { + if self.cancel.load(Ordering::Relaxed) { + return Err(other_io_error("Cancelled serialization preparation")); + } + + // Build the path of the next directory we are about to materialize. + let mut next_path = current_path.clone(); + if next_path.last() != Some(&b'/') { + next_path.push(b'/'); + } + next_path.extend_from_slice(component); + let next_path = Self::normalize_path_bytes(next_path); + + // Idempotency: if another `ensure_ancestor_chain` call (or the main loop) has + // already populated this exact path, just rebase onto it and continue. + if let Some(&existing_id) = path_to_inode.get(&next_path) { + let existing_data = match self.fs.inodes.get(existing_id) { + Some(d) => d, + None => return Ok(false), + }; + current_fd = existing_data + .open_file( + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + &self.fs.proc_self_fd, + )? + .into_file()?; + current_path = next_path; + current_inode = existing_id; + continue; + } + + // `open_relative_to` expects a `CStr`. The component cannot contain '/' (we split + // at '/' boundaries) and cannot contain NUL (paths from `readlink()` on /proc/self/fd + // never contain NUL since NUL is not allowed in filenames). Map non-UTF-8 conformantly + // to the legacy reconstructor's behaviour: a hard error under verify_handles, a + // degrade-to-skip otherwise. + let component_cstr = match std::ffi::CString::new(component.to_vec()) { + Ok(c) => c, + Err(err) => { + if self.fs.cfg.migration_verify_handles { + return Err(other_io_error(format!( + "store_only preserialization: NUL in path component {:?}: {}", + std::str::from_utf8(component).unwrap_or(""), + err + ))); + } + warn!( + "store_only preserialization: NUL in path component {:?}: {}; cannot \ + synthesize parent chain, skipping", + std::str::from_utf8(component).unwrap_or(""), + err + ); + return Ok(false); + } + }; + + // Open the next component as an O_PATH fd relative to the current dir fd. + let raw_fd = + match self + .fs + .open_relative_to(¤t_fd, &component_cstr, libc::O_PATH, None) + { + Ok(fd) => fd, + Err(err) => { + // Directory disappeared between snapshot and now, or permission issue. + // Under verify_handles, propagate; otherwise degrade. + if self.fs.cfg.migration_verify_handles { + return Err(other_io_error(format!( + "store_only preserialization: failed to open {:?} relative to \ + inode {}: {}", + std::str::from_utf8(component).unwrap_or(""), + current_inode, + err + ))); + } + warn!( + "store_only preserialization: failed to open {:?} relative to inode \ + {}: {}; skipping descendants under this path", + std::str::from_utf8(component).unwrap_or(""), + current_inode, + err + ); + return Ok(false); + } + }; + let new_dir_fd = unsafe { File::from_raw_fd(raw_fd) }; + let stat = statx(&new_dir_fd, None)?; + + // Only directories are valid intermediate components on the path to a child. Any + // non-directory in the middle would mean the path itself is malformed (e.g. the + // child went through a symlink we did not resolve). Refuse to register + // non-directories here -- the caller will then mark the child as Invalid. + if stat.st.st_mode & libc::S_IFMT != libc::S_IFDIR { + debug!( + "store_only preserialization: intermediate component {:?} of synthesized \ + parent chain is not a directory (mode={:o}); aborting chain", + std::str::from_utf8(component).unwrap_or(""), + stat.st.st_mode + ); + return Ok(false); + } + + let handle = self.fs.get_file_handle_opt(&new_dir_fd, &stat)?; + let ids = InodeIds { + ino: stat.st.st_ino, + dev: stat.st.st_dev, + mnt_id: stat.mnt_id, + }; + + // Build the inode's migration_info pointing to the *parent* we just came from. + let parent_ref = self.fs.inodes.get_strong(current_inode)?; + let utf8_component = match std::str::from_utf8(component) { + Ok(s) => s, + Err(err) => { + if self.fs.cfg.migration_verify_handles { + return Err(other_io_error(format!( + "store_only preserialization: non-UTF-8 path component: {err}" + ))); + } + warn!( + "store_only preserialization: non-UTF-8 path component: {}; cannot \ + synthesize parent chain, skipping", + err + ); + return Ok(false); + } + }; + + // First, try to claim an existing inode (with matching handle/ids). This handles + // the case where the same inode is reachable both through the store (lookup by + // its own fd) and through this synthesized chain (lookup as a parent of someone + // else): we must not create a duplicate `InodeData`, or the destination would see + // two inodes referencing the same on-disk object. + let new_ref = if let Ok(existing) = self.fs.inodes.claim_inode(handle.as_ref(), &ids) { + // Already in store. Set migration_info if it does not have one yet. + { + let mut mig_guard = existing.get().migration_info.lock().unwrap(); + if mig_guard.is_none() { + let file_handle = if self.fs.cfg.migration_verify_handles { + Some(match &handle { + Some(h) => h.into(), + None => FileHandle::from_fd_fail_hard(&new_dir_fd)?.into(), + }) + } else { + None + }; + *mig_guard = Some(InodeMigrationInfo { + location: InodeLocation::Path { + parent: parent_ref, + filename: utf8_component.to_string(), + fullname: String::new(), + }, + file_handle, + }); + } + } + existing + } else { + // Genuinely new intermediate inode: build a complete `InodeData` and insert. + let file_or_handle = if let Some(ref h) = handle { + FileOrHandle::Handle(self.fs.make_file_handle_openable(h)?) + } else { + FileOrHandle::File(new_dir_fd.try_clone()?) + }; + + let mig_info = InodeMigrationInfo::new_with_utf8_name( + &self.fs.cfg, + parent_ref, + utf8_component, + String::new(), + &file_or_handle, + )?; + + let new_inode = InodeData { + inode: self.fs.next_inode.fetch_add(1, Ordering::Relaxed), + file_or_handle, + refcount: AtomicU64::new(1), + ids, + mode: stat.st.st_mode, + migration_info: Mutex::new(Some(mig_info)), + }; + + self.fs.inodes.get_or_insert(new_inode)? + }; + + let new_inode_id = new_ref.get().inode; + path_to_inode.insert(next_path.clone(), new_inode_id); + + // Move down the chain. + current_fd = new_dir_fd; + current_path = next_path; + current_inode = new_inode_id; + // Park the strong reference in the caller-owned holding pen so the inode survives + // until the main reverse-lookup loop has had a chance to take its own strong ref + // via `get_strong(parent_id)` (and store it in the descendant's + // `migration_info.location.parent`). See the function-level doc comment for the + // full rationale; in short, dropping `new_ref` here would let an intermediate + // inode whose refcount has just been bumped to 1 fall straight back to 0 and be + // evicted from the store before any descendant can reference it. Intermediate + // dirs that end up with *no* descendant referencing them will still be dropped + // when the holding pen is dropped at the end of `execute()`, matching the legacy + // reconstructor's contract. + holding_pen.push(new_ref); + } + + Ok(true) + } + + /// Set the migration info for every filter inode (mirrors the legacy `PathReconstructor` + /// filter handling, but without recursing into filter sub-trees -- the per-inode reverse + /// lookup pass below will populate any descendants that are actually present in the store). + fn set_filter_infos( + &self, + root: &StrongInodeReference, + filter_inodes: &mut Vec, + ) -> io::Result<()> { + let filter_map = self.fs.filter.read().unwrap(); + for (id, (filename, fullname)) in filter_map.iter() { + if let Ok(inode) = self.fs.inodes.get_strong(*id) { + debug!( + "store_only preserialization filter inode, id:{:?}, name:{:?}, fullname:{:?}", + id, filename, fullname + ); + *inode.get().migration_info.lock().unwrap() = Some(InodeMigrationInfo { + location: InodeLocation::Path { + parent: StrongInodeReference::clone(root), + filename: filename.clone(), + fullname: fullname.clone(), + }, + file_handle: if self.fs.cfg.migration_verify_handles { + Some((&inode.get().file_or_handle).try_into()?) + } else { + None + }, + }); + filter_inodes.push(*id); + } + } + Ok(()) + } +} + +impl InodeMigrationInfoConstructor for StoreOnlyReconstructor<'_> { + fn execute(self) -> io::Result<()> { + let Ok(root) = self.fs.inodes.get_strong(fuse::ROOT_ID) else { + // No root node => filesystem not mounted, nothing to do. + return Ok(()); + }; + + // 0. Build a migration-only `filter_inode_id -> resolved_host_path_bytes` map. This + // does NOT mutate the runtime `fs.filter` cache, so a cancelled/failed migration + // leaves no residue. See `build_filter_resolved_map` doc for the rationale. + let filter_resolved_map = self.build_filter_resolved_map(); + + // 1. Root node + self.set_root_info(&root)?; + + // 2. Filter nodes (legacy semantics: fullname kept, parent = root). + let mut filter_inodes: Vec = Vec::new(); + self.set_filter_infos(&root, &mut filter_inodes)?; + + // 3. Single-pass collection: snapshot every store inode's absolute path via procfs. + // We deliberately keep this pass under one read-lock of the inode store (via + // `inodes.map`) so the snapshot is consistent. + let filter_inode_set: std::collections::HashSet = + filter_inodes.iter().copied().collect(); + let cancel = Arc::clone(&self.cancel); + let proc_self_fd = &self.fs.proc_self_fd; + + let snapshots: Vec)>> = self.fs.inodes.map(|inode_data| { + if cancel.load(Ordering::Relaxed) { + return Err(other_io_error("Cancelled serialization preparation")); + } + // Skip nodes we have already populated above (root + filter inodes). + if inode_data.inode == fuse::ROOT_ID || filter_inode_set.contains(&inode_data.inode) { + return Ok((inode_data.inode, Vec::new())); + } + // Skip invalid inodes (carry-overs from a previous failed in-migration). + if matches!(inode_data.file_or_handle, FileOrHandle::Invalid(_)) { + return Ok((inode_data.inode, Vec::new())); + } + // For inodes that already have migration_info set (e.g. populated by `do_lookup` + // while `track_migration_info` was already true), we must still snapshot their + // absolute path: their descendants in the store need to find them via + // `path_to_inode` in step 5. We just must not overwrite their migration_info later. + // The downstream loop checks `migration_info.is_some()` again before mutating it. + // + // Resolve the absolute path through /proc/self/fd. The cost of this depends on + // `file_or_handle`: for `FileOrHandle::File` this is a procfs-only readlink; for + // `FileOrHandle::Handle`, `InodeData::get_path()` first calls `Handle::open(O_PATH)` + // via `open_by_handle_at(2)` to materialize a temporary FD, which does touch the + // backing filesystem. Either way, the cost stays bounded by inode-store size rather + // than the on-disk tree size. + match inode_data.get_path(proc_self_fd) { + Ok(path) => Ok((inode_data.inode, path.into_bytes())), + Err(err) => { + // `get_path_by_fd` returns `"Inode deleted"` when the kernel reports the + // procfs link target with a trailing " (deleted)" suffix, i.e. the file + // was unlinked on the host while still being open in the guest. Such an + // inode cannot be migrated (the destination has nothing to open at the + // former path) and will be serialized as `Invalid`, dropping it from the + // destination's view. Surface this at warn level so operators can correlate + // post-migration "missing inode" reports with on-host unlink activity. + let is_deleted = err.to_string().contains("Inode deleted"); + if is_deleted { + warn!( + "store_only preserialization: inode {} (st_dev={}, mnt_id={}, \ + st_ino={}) was unlinked on host while still open; it will be \ + serialized as Invalid and dropped on the destination", + inode_data.inode, + inode_data.ids.dev, + inode_data.ids.mnt_id, + inode_data.ids.ino, + ); + } else { + debug!( + "store_only preserialization: failed to get path for inode {} \ + (st_dev={}, mnt_id={}, st_ino={}): {}; skipping (will be marked \ + Invalid at serialization)", + inode_data.inode, + inode_data.ids.dev, + inode_data.ids.mnt_id, + inode_data.ids.ino, + err + ); + } + Ok((inode_data.inode, Vec::new())) + } + } + }); + + // Propagate the (only) hard error: cancellation. + let mut entries: Vec<(Inode, Vec)> = Vec::with_capacity(snapshots.len()); + for snap in snapshots { + let (id, path) = snap?; + if !path.is_empty() { + entries.push((id, path)); + } + } + + // 4. Sort by path byte-length so any parent comes strictly before its children. The + // invariant being relied on: in POSIX, every descendant's absolute byte-path is + // strictly longer than its ancestor's (a child path is parent + '/' + name, and name + // is non-empty). Ties in length therefore cannot represent an ancestor/descendant + // relationship, so any order within a tie is fine. If this invariant is ever broken + // (e.g. by mount-namespace aliasing surfacing through /proc/self/fd), descendants + // may fail to find their parent and fall through to the "parent not found" debug + // path below. + entries.sort_by(|a, b| a.1.len().cmp(&b.1.len())); + + // Pre-seed the lookup table with root and filter inodes so descendants below them can be + // resolved correctly. Every key we insert here is normalized via `normalize_path_bytes` + // (i.e. no trailing '/'), because the parent slice computed in step 5 never carries a + // trailing slash. This is critical for symlinked filters whose `readlink()` target may + // legitimately end with '/', e.g. ".../sionli-test/". + let mut path_to_inode: HashMap, Inode> = HashMap::with_capacity(entries.len() + 8); + // Root: its absolute path is the shared_dir path on host. + if let Ok(root_path) = root.get().get_path(&self.fs.proc_self_fd) { + path_to_inode.insert( + Self::normalize_path_bytes(root_path.into_bytes()), + fuse::ROOT_ID, + ); + } + // Filter inodes: their absolute paths on host are typically *outside* the shared_dir + // (e.g. NFS mount points). Seed them too so any of their store-resident descendants can + // be reverse-looked up. We prefer the migration-local resolved map (which includes + // symlink targets we resolved just for this migration without touching `fs.filter`), + // and additionally fall back to each filter inode's own /proc/self/fd path. + for (id, resolved_bytes) in filter_resolved_map.iter() { + let bytes = Self::normalize_path_bytes(resolved_bytes.clone()); + if !bytes.is_empty() { + path_to_inode.insert(bytes, *id); + } + // Also try to seed via the inode's own /proc/self/fd path (covers cases where the + // resolved path differs from the actual host path, e.g. bind mounts). + if let Some(inode_data) = self.fs.inodes.get(*id) { + if let Ok(p) = inode_data.get_path(&self.fs.proc_self_fd) { + path_to_inode + .entry(Self::normalize_path_bytes(p.into_bytes())) + .or_insert(*id); + } + } + } + + // 5. Stream through the sorted list, resolving parent_id from the lookup table. + // + // `synthesized_refs` is a holding pen for strong references to inodes that + // `ensure_ancestor_chain` materializes on demand for missing parents (Blocking 1 + // scenario: a parent directory was forgotten by the guest while a descendant kept a + // handle alive). Each newly synthesized inode comes back from `claim_inode`/ + // `get_or_insert` with `refcount = 1`, and would otherwise be evicted from the + // inode store the moment that strong reference is dropped -- before the main loop + // below can take its own strong reference via `inodes.get_strong(parent_id)` and + // store it inside the descendant's `migration_info.location.parent`. Keeping these + // refs alive across the main loop guarantees that `get_strong(parent_id)` always + // succeeds for synthesized parents. After the main loop, dropping the vector lets + // intermediates that ended up with no real descendants fall back to refcount 0 and + // be evicted from the store, matching the legacy `PathReconstructor`'s contract. + let mut synthesized_refs: Vec = Vec::new(); + for (inode_id, abs_path) in entries.iter() { + if self.cancel.load(Ordering::Relaxed) { + return Err(other_io_error("Cancelled serialization preparation")); + } + + // Split off the filename and parent path. `abs_path` is a NUL-free byte vec coming + // from a `CString::into_bytes()`. + let (parent_bytes, filename_bytes) = match abs_path.iter().rposition(|&b| b == b'/') { + Some(0) => (&b"/"[..], &abs_path[1..]), + Some(p) => (&abs_path[..p], &abs_path[p + 1..]), + None => { + debug!( + "store_only preserialization: malformed absolute path for inode {}: {:?}; \ + skipping", + inode_id, abs_path + ); + continue; + } + }; + if filename_bytes.is_empty() { + debug!( + "store_only preserialization: empty filename for inode {} (path={:?}); \ + skipping", + inode_id, abs_path + ); + continue; + } + + // Non-UTF-8 filenames: the legacy `PathReconstructor::discover()` treats this as + // a hard error (it calls `name.to_str()?` and propagates). We match that semantics + // here: silently skipping would be a behavioural regression (the inode would be + // serialized as `Invalid` and the destination would lose it). We always log at + // warn! level so operators see it; under `migration_verify_handles` we additionally + // surface it as a hard error to keep parity with the legacy reconstructor's + // fail-hard guarantees. + let filename = match std::str::from_utf8(filename_bytes) { + Ok(s) => s.to_string(), + Err(err) => { + if self.fs.cfg.migration_verify_handles { + return Err(other_io_error(format!( + "store_only preserialization: non-UTF-8 filename for inode {inode_id}: {err}" + ))); + } + warn!( + "store_only preserialization: non-UTF-8 filename for inode {}: {}; \ + skipping (inode will be marked Invalid at serialization)", + inode_id, err + ); + continue; + } + }; + + // Reverse-lookup parent inode_id. If the parent is not yet in the table, try to + // synthesize the missing ancestor chain on demand: open every intermediate + // directory between the closest known ancestor and `parent_bytes` with O_PATH, + // register them as proper inode store entries with `migration_info`, and seed + // them into `path_to_inode`. This mirrors what the legacy `PathReconstructor` + // does when it walks into a directory that is not yet in the store -- the + // difference is that we drive the materialization bottom-up from the child's + // known absolute path rather than top-down from the shared-dir root, paying + // O(depth) syscalls per missing chain instead of O(tree size). + let parent_id = match path_to_inode.get(parent_bytes) { + Some(id) => *id, + None => match self.ensure_ancestor_chain( + parent_bytes, + &mut path_to_inode, + &mut synthesized_refs, + ) { + Ok(true) => match path_to_inode.get(parent_bytes) { + Some(id) => *id, + None => { + // `ensure_ancestor_chain` returned `true` but did not actually seed + // the path we asked for -- this would be an internal bug. + debug!( + "store_only preserialization: parent path {:?} still not in \ + lookup table after ensure_ancestor_chain succeeded; skipping \ + inode {}", + std::str::from_utf8(parent_bytes).unwrap_or(""), + inode_id + ); + path_to_inode.insert(abs_path.clone(), *inode_id); + continue; + } + }, + Ok(false) => { + debug!( + "store_only preserialization: parent path {:?} not found in lookup \ + table for inode {} (path={:?}) and could not be synthesized; \ + skipping (will be marked Invalid at serialization)", + std::str::from_utf8(parent_bytes).unwrap_or(""), + inode_id, + std::str::from_utf8(abs_path).unwrap_or(""), + ); + // Still register this inode in the table -- its descendants (if any) + // should not be wrongly remapped to a stale ancestor. + path_to_inode.insert(abs_path.clone(), *inode_id); + continue; + } + Err(err) => { + // Hard error from ensure_ancestor_chain (only possible under + // `migration_verify_handles` or on cancellation): propagate. + return Err(err); + } + }, + }; + + // Take a strong reference to the parent. Failure here is theoretically impossible + // because the inode store cannot lose entries while guest is paused, but defensively + // tolerate it. + let parent_ref = match self.fs.inodes.get_strong(parent_id) { + Ok(r) => r, + Err(err) => { + debug!( + "store_only preserialization: failed to take strong ref to parent inode \ + {} for child {}: {}; skipping", + parent_id, inode_id, err + ); + path_to_inode.insert(abs_path.clone(), *inode_id); + continue; + } + }; + + // Look up the child's InodeData and set its migration_info. + let inode_data = match self.fs.inodes.get(*inode_id) { + Some(d) => d, + None => { + debug!( + "store_only preserialization: inode {} disappeared from store between \ + snapshot and processing; skipping", + inode_id + ); + continue; + } + }; + + // When `migration_verify_handles` is enabled, the legacy `PathReconstructor` builds + // the file handle via `FileHandle::from_fd_fail_hard()` and propagates failure. Match + // that semantics here: a missing/failing file handle under verify_handles indicates + // that the destination would not be able to verify this inode, which is exactly the + // case the operator opted into verifying. Returning a hard error aborts the + // migration preparation, which is the safe default. + let file_handle = if self.fs.cfg.migration_verify_handles { + match (&inode_data.file_or_handle).try_into() { + Ok(h) => Some(h), + Err(err) => { + return Err(other_io_error(format!( + "store_only preserialization: failed to build file handle for inode \ + {inode_id}: {err}" + ))); + } + } + } else { + None + }; + + // Defensive: if migration_info was already populated (e.g. by `do_lookup` after our + // snapshot pass but before this mutation), do not overwrite it. The snapshot pass + // intentionally keeps such inodes in `entries` so they can be seeded into + // `path_to_inode` (done below), but their migration_info must remain authoritative. + { + let mut mig_info_guard = inode_data.migration_info.lock().unwrap(); + if mig_info_guard.is_some() { + drop(mig_info_guard); + path_to_inode.insert(abs_path.clone(), *inode_id); + continue; + } + *mig_info_guard = Some(InodeMigrationInfo { + location: InodeLocation::Path { + parent: parent_ref, + filename, + fullname: String::new(), + }, + file_handle, + }); + } + + // Insert into table so descendants can resolve us as their parent. + path_to_inode.insert(abs_path.clone(), *inode_id); + } + + // Now that the main loop is done, every descendant that genuinely needed one of the + // synthesized parents has captured its own strong reference inside its + // `migration_info.location.parent`. Releasing the holding pen here lets unreferenced + // intermediates fall back to refcount 0 and be evicted from the store, so the + // destination only sees inodes that are actually reachable from real descendants -- + // matching the legacy `PathReconstructor`'s contract for parent-chain materialization. + drop(synthesized_refs); + + info!( + "store_only preserialization finished: processed {} non-root/non-filter inodes", + entries.len() + ); + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + //! Behavioural tests for `StoreOnlyReconstructor::execute()`. + //! + //! These tests drive a real `PassthroughFs` against a private host temporary directory tree + //! and exercise the full preserialization flow end-to-end. They specifically cover the MR + //! !23 review integrity-critical scenarios: + //! + //! * Basic chain: a multi-level path can be looked up and reconstructed with correct + //! filename/parent linkage for every inode. + //! * Missing-parent recovery (Blocking 1): when a parent directory has been forgotten by + //! the guest but a descendant inode is still in the store, `StoreOnlyReconstructor` must + //! synthesize the parent chain on demand rather than skipping the descendant. + //! * Parity with `PathReconstructor`: for the same starting inode-store state, both + //! reconstructors must produce equivalent migration info (filename + ancestor path) for + //! every guest-looked-up inode. + //! * Root-only and empty-tree edge cases. + //! + //! Test layout: + //! * Each test creates a unique on-disk fixture under `$TMPDIR/virtiofsd-test-...`. + //! `TestDir::drop` recursively removes it. + //! * Tests are linux-only (the codebase is linux-only) and require a writable temp dir; + //! they do not need root. + //! * The tests run with `migration_verify_handles=false` to remain compatible with file + //! systems that do not support `name_to_handle_at()` (e.g. tmpfs). + //! * `cargo test` runs tests in parallel by default. Each test owns an independent tempdir + //! and an independent `PassthroughFs`; no shared state is touched. + //! + //! Caveat: the tests link against the production `PassthroughFs` and therefore inherit its + //! requirement on `/proc/self/fd`. They will fail to construct the fs on platforms where + //! procfs is unavailable, which is intentional (and matches production behaviour). + + use super::*; + use crate::passthrough::{Config, PassthroughFs}; + use std::ffi::CString; + use std::fs; + use std::path::{Path, PathBuf}; + use std::sync::atomic::{AtomicU64, Ordering as AOrdering}; + use std::sync::Arc; + use std::time::{SystemTime, UNIX_EPOCH}; + + // -- fixture helpers ------------------------------------------------------------------- + + /// Globally-unique counter to avoid collisions between tests running in parallel and to + /// avoid relying on `SystemTime::now()` having sufficient resolution. + static FIXTURE_SEQ: AtomicU64 = AtomicU64::new(0); + + /// Owns an on-disk temporary directory; recursively removes it on drop. + struct TestDir { + path: PathBuf, + } + + impl TestDir { + /// Create a fresh empty tempdir scoped to the given test name. + fn new(test_name: &str) -> Self { + let pid = std::process::id(); + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or(0); + let seq = FIXTURE_SEQ.fetch_add(1, AOrdering::Relaxed); + let base = std::env::temp_dir() + .join(format!("virtiofsd-test-{pid}-{nanos}-{seq}-{test_name}")); + fs::create_dir_all(&base).expect("failed to create test tempdir"); + // Canonicalize so that readlink-based path lookups inside the fs (which return + // host-absolute, symlink-resolved paths) match the path we hand to root_dir. + let canon = fs::canonicalize(&base).expect("failed to canonicalize tempdir"); + TestDir { path: canon } + } + + fn path(&self) -> &Path { + &self.path + } + + /// `mkdir -p ` inside the tempdir. + fn mkdir(&self, rel: &str) { + fs::create_dir_all(self.path.join(rel)).expect("mkdir -p failed"); + } + + /// `touch ` inside the tempdir; parent dirs are created if missing. + fn touch(&self, rel: &str) { + let full = self.path.join(rel); + if let Some(parent) = full.parent() { + fs::create_dir_all(parent).expect("mkdir for touch failed"); + } + fs::File::create(&full).expect("touch failed"); + } + + /// Create a symlink at `` pointing to `target` (literal value of the link). + /// Parent directories of `rel_link` are created if missing. The target is *not* + /// validated to exist, mirroring `ln -s` behaviour. + fn symlink(&self, target: &str, rel_link: &str) { + let full = self.path.join(rel_link); + if let Some(parent) = full.parent() { + fs::create_dir_all(parent).expect("mkdir for symlink failed"); + } + std::os::unix::fs::symlink(target, &full).expect("symlink failed"); + } + } + + impl Drop for TestDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.path); + } + } + + /// Construct a `PassthroughFs` rooted at the given path with test-friendly defaults, and + /// initialize its root inode. Returns the fs along with a never-set cancel flag suitable + /// for use with both reconstructors. + fn mk_fs(root: &Path) -> (PassthroughFs, Arc) { + let mut cfg = Config::default(); + cfg.root_dir = root + .to_str() + .expect("test tempdir must be UTF-8") + .to_string(); + // Keep handles disabled so the tests do not depend on the underlying FS supporting + // name_to_handle_at(); we exercise the FileOrHandle::File branch of get_path(). + cfg.migration_verify_handles = false; + cfg.migration_dfs_preserialization = false; + // No filters; tests covering filters seed them explicitly via `fs.filter`. + let fs = PassthroughFs::new(cfg).expect("PassthroughFs::new failed"); + fs.open_root_node().expect("open_root_node failed"); + (fs, Arc::new(AtomicBool::new(false))) + } + + /// Look up a chain of names starting from ROOT_ID and return the inode ID of each level + /// (in order, *not* including ROOT). All path components are resolved via the production + /// `do_lookup`, so the resulting inodes have real `file_or_handle` and `ids`. + fn lookup_chain(fs: &PassthroughFs, names: &[&str]) -> Vec { + let mut parent = fuse::ROOT_ID; + let mut chain = Vec::with_capacity(names.len()); + for name in names { + let cname = CString::new(*name).expect("name contains NUL"); + let entry = fs + .do_lookup(parent, &cname, None) + .unwrap_or_else(|err| panic!("do_lookup({}, {:?}) failed: {}", parent, name, err)); + chain.push(entry.inode); + parent = entry.inode; + } + chain + } + + /// Helper: clear all migration_info on the fs so a reconstructor can be re-run from a + /// clean slate. Used by the parity test. + fn clear_all_migration_info(fs: &PassthroughFs) { + fs.inodes.clear_migration_info(); + } + + /// Extract `(filename, parent_inode_id)` from an inode's `migration_info`, if it has been + /// populated with a `Path` location. Returns `None` for unset / `RootNode` / inode not in + /// store. + fn path_info(fs: &PassthroughFs, inode: Inode) -> Option<(String, Inode)> { + let data = fs.inodes.get(inode)?; + let guard = data.migration_info.lock().unwrap(); + let info = guard.as_ref()?; + match &info.location { + InodeLocation::Path { + filename, parent, .. + } => { + // SAFETY: we are not leaking the strong reference; `get_raw` is documented as + // safe to call while the strong reference exists. + let parent_id = unsafe { parent.get_raw() }; + Some((filename.clone(), parent_id)) + } + InodeLocation::RootNode => None, + } + } + + /// Returns `true` if the inode's `migration_info` is set to `RootNode`. + fn is_root_migration_info(fs: &PassthroughFs, inode: Inode) -> bool { + let Some(data) = fs.inodes.get(inode) else { + return false; + }; + let guard = data.migration_info.lock().unwrap(); + matches!( + guard.as_ref().map(|i| &i.location), + Some(InodeLocation::RootNode) + ) + } + + /// Build the full ancestor-filename chain (root-first, including the inode itself) by + /// walking `migration_info.parent` until a `RootNode` is reached. Returns `None` if any + /// step is missing migration_info, which signals "lost" in the reconstruction. Note that + /// the parent inode IDs themselves are *not* part of the chain: only filenames are, so + /// the chain is invariant under inode-id reallocation between runs (which is essential + /// for the parity test). + fn filename_chain_to_root(fs: &PassthroughFs, inode: Inode) -> Option> { + let mut out: Vec = Vec::new(); + let mut cur = inode; + // Safety budget against accidental cycles. + for _ in 0..256 { + if cur == fuse::ROOT_ID { + return Some({ + out.reverse(); + out + }); + } + let data = fs.inodes.get(cur)?; + let guard = data.migration_info.lock().unwrap(); + let info = guard.as_ref()?; + match &info.location { + InodeLocation::Path { + filename, parent, .. + } => { + out.push(filename.clone()); + cur = unsafe { parent.get_raw() }; + } + InodeLocation::RootNode => { + return Some({ + out.reverse(); + out + }); + } + } + } + panic!("filename_chain_to_root: walked >256 hops, suspected cycle"); + } + + // -- t1: basic chain ------------------------------------------------------------------ + + /// A straight three-level chain `a/b/c.txt` should be fully reconstructed: each non-root + /// inode gets `InodeLocation::Path` with the correct filename, and walking parent + /// references upwards yields the original chain. + #[test] + fn storeonly_reconstructs_basic_chain() { + let dir = TestDir::new("basic_chain"); + dir.mkdir("a/b"); + dir.touch("a/b/c.txt"); + + let (fs, cancel) = mk_fs(dir.path()); + let chain = lookup_chain(&fs, &["a", "b", "c.txt"]); + assert_eq!(chain.len(), 3, "expected three inodes in chain"); + + StoreOnlyReconstructor::new(&fs, Arc::clone(&cancel)) + .execute() + .expect("StoreOnlyReconstructor::execute failed"); + + // Root must be marked as RootNode. + assert!( + is_root_migration_info(&fs, fuse::ROOT_ID), + "root inode must have RootNode migration_info" + ); + + // Each chain element must have a Path location with the correct filename. + let (a_name, a_parent) = path_info(&fs, chain[0]).expect("a missing path info"); + assert_eq!(a_name, "a"); + assert_eq!(a_parent, fuse::ROOT_ID); + + let (b_name, b_parent) = path_info(&fs, chain[1]).expect("b missing path info"); + assert_eq!(b_name, "b"); + assert_eq!(b_parent, chain[0]); + + let (c_name, c_parent) = path_info(&fs, chain[2]).expect("c.txt missing path info"); + assert_eq!(c_name, "c.txt"); + assert_eq!(c_parent, chain[1]); + + // And the convenience chain helper agrees end-to-end. + let names = filename_chain_to_root(&fs, chain[2]).expect("chain reconstruction failed"); + assert_eq!(names, vec!["a", "b", "c.txt"]); + } + + // -- t2: missing-parent recovery (Blocking 1) ------------------------------------------ + + /// Reproduce the central Blocking 1 scenario from MR !23: a child inode survives in the + /// store while its parent directory has been forgotten by the guest. The old + /// `StoreOnlyReconstructor` would skip such a child (resulting in `Invalid` at + /// serialization); the fixed version must synthesize the missing parent chain on demand. + #[test] + fn storeonly_recovers_forgotten_parent_directory() { + let dir = TestDir::new("forgotten_parent"); + dir.mkdir("p"); + dir.touch("p/child.txt"); + + let (fs, cancel) = mk_fs(dir.path()); + let chain = lookup_chain(&fs, &["p", "child.txt"]); + let p_inode = chain[0]; + let child_inode = chain[1]; + + // Simulate the guest forgetting `p` while still holding `child.txt`. `do_lookup` + // incremented `p`'s refcount to 1 (after the implicit +1 from try_lookup), so a + // single forget brings it to 0 and removes it from the store. `child.txt` does not + // hold a strong reference to its parent (migration_info is not yet set), so the + // forget is unobstructed. + fs.inodes.forget_one(p_inode, 1); + assert!( + fs.inodes.get(p_inode).is_none(), + "parent `p` should have been removed from store after forget" + ); + assert!( + fs.inodes.get(child_inode).is_some(), + "child inode must still be in store" + ); + + StoreOnlyReconstructor::new(&fs, Arc::clone(&cancel)) + .execute() + .expect("StoreOnlyReconstructor::execute failed on forgotten-parent case"); + + // The child must now have valid Path migration_info, and the chain back to root must + // be exactly ["p", "child.txt"] -- proving that the missing `p` was synthesized. + let names = filename_chain_to_root(&fs, child_inode).unwrap_or_else(|| { + panic!( + "child {} has no migration_info after StoreOnly reconstruction; \ + Blocking 1 fix did not take effect", + child_inode + ) + }); + assert_eq!( + names, + vec!["p", "child.txt"], + "synthesized parent chain has wrong filename(s)" + ); + + // The synthesized parent must have been registered into the inode store with a real + // InodeData (not just a phantom path table entry), so we can read its file/handle. + let (child_name, synth_parent_id) = + path_info(&fs, child_inode).expect("child path_info missing"); + assert_eq!(child_name, "child.txt"); + let synth_parent_data = fs + .inodes + .get(synth_parent_id) + .expect("synthesized parent must be in the inode store with a real InodeData"); + // The synthesized parent's own migration_info must point to root with filename `p`. + let (synth_name, synth_grandparent) = path_info(&fs, synth_parent_id) + .expect("synthesized parent has no migration_info -- must point at root"); + assert_eq!(synth_name, "p"); + assert_eq!(synth_grandparent, fuse::ROOT_ID); + // And its on-disk identity must match the directory we created. + let stat = crate::passthrough::stat::statx(&synth_parent_data.get_file().unwrap(), None) + .expect("statx on synthesized parent"); + assert!( + stat.st.st_mode & libc::S_IFDIR != 0, + "synthesized parent must be a directory" + ); + } + + // -- t3: empty / root-only ------------------------------------------------------------ + + /// With nothing but the root inode in the store, the reconstructor must run cleanly and + /// produce `RootNode` migration_info on the root. This pins down the cheapest path + /// through `execute()`. + #[test] + fn storeonly_root_only_is_a_no_op() { + let dir = TestDir::new("root_only"); + let (fs, cancel) = mk_fs(dir.path()); + + StoreOnlyReconstructor::new(&fs, Arc::clone(&cancel)) + .execute() + .expect("StoreOnlyReconstructor::execute failed on root-only store"); + + assert!(is_root_migration_info(&fs, fuse::ROOT_ID)); + } + + // -- t4: parity with PathReconstructor ------------------------------------------------- + + /// For the same starting inode-store state, both reconstructors must produce the same + /// filename-chain back to root for every guest-looked-up inode. This is the + /// `alexyonghe`-requested parity check. + /// + /// We do *not* compare inode IDs directly across runs, because `PathReconstructor` may + /// auto-create intermediate directory inodes during its DFS (e.g. when their children + /// are in the store but the directory itself was forgotten), and the new inode IDs are + /// not stable across runs. Filename chains are inode-id-invariant and are what the + /// destination actually reconstructs from. + #[test] + fn parity_with_path_reconstructor_on_mixed_tree() { + let dir = TestDir::new("parity"); + dir.mkdir("a/b"); + dir.touch("a/b/leaf.txt"); + dir.mkdir("a/sibling"); + dir.touch("a/sibling/other.txt"); + dir.touch("a/top.txt"); + + // Inodes we will assert about (all looked up explicitly so they live in the store + // independently of the reconstructor's recursion). + let to_check: &[&[&str]] = &[ + &["a"], + &["a", "b"], + &["a", "b", "leaf.txt"], + &["a", "sibling"], + &["a", "sibling", "other.txt"], + &["a", "top.txt"], + ]; + + // ----- run #1: PathReconstructor -------------------------------------------------- + let (fs1, cancel1) = mk_fs(dir.path()); + let chains1: Vec = to_check + .iter() + .map(|names| *lookup_chain(&fs1, names).last().unwrap()) + .collect(); + PathReconstructor::new(&fs1, Arc::clone(&cancel1)) + .execute() + .expect("PathReconstructor execute failed"); + let names1: Vec> = chains1 + .iter() + .map(|&id| { + filename_chain_to_root(&fs1, id).unwrap_or_else(|| { + panic!("PathReconstructor: inode {} has no migration_info", id) + }) + }) + .collect(); + + // ----- run #2: StoreOnlyReconstructor on a fresh fs ------------------------------- + let (fs2, cancel2) = mk_fs(dir.path()); + let chains2: Vec = to_check + .iter() + .map(|names| *lookup_chain(&fs2, names).last().unwrap()) + .collect(); + StoreOnlyReconstructor::new(&fs2, Arc::clone(&cancel2)) + .execute() + .expect("StoreOnlyReconstructor execute failed"); + let names2: Vec> = chains2 + .iter() + .map(|&id| { + filename_chain_to_root(&fs2, id).unwrap_or_else(|| { + panic!("StoreOnlyReconstructor: inode {} has no migration_info", id) + }) + }) + .collect(); + + // The filename chains must be identical for every queried inode -- this is the parity + // contract that the destination relies on for path-based migration to work the same + // way regardless of which reconstructor produced the data. + for ((q, p), s) in to_check.iter().zip(names1.iter()).zip(names2.iter()) { + let expected: Vec = q.iter().map(|s| (*s).to_string()).collect(); + assert_eq!( + p, &expected, + "PathReconstructor chain for {q:?} disagrees with the path itself" + ); + assert_eq!( + s, &expected, + "StoreOnlyReconstructor chain for {q:?} disagrees with the path itself" + ); + assert_eq!( + p, s, + "Parity violation: PathReconstructor and StoreOnlyReconstructor produced \ + different filename chains for {q:?}" + ); + } + + // And root in both runs must be RootNode. + assert!(is_root_migration_info(&fs1, fuse::ROOT_ID)); + assert!(is_root_migration_info(&fs2, fuse::ROOT_ID)); + + // Defensive: clearing migration_info on the same fs and re-running StoreOnly must + // produce the same chains again (idempotency-ish under reset). This catches the + // class of bug where a second run depends on state lingering from the first. + clear_all_migration_info(&fs2); + StoreOnlyReconstructor::new(&fs2, Arc::clone(&cancel2)) + .execute() + .expect("StoreOnlyReconstructor re-execute failed"); + for (q, expected) in to_check.iter().zip(names2.iter()) { + // Note: chains2 was filled before the clear, so the inode IDs are still valid + // (clear_migration_info does not remove inodes from the store, only drops the + // migration_info field). + let id = *lookup_chain(&fs2, q).last().unwrap(); + let again = filename_chain_to_root(&fs2, id).unwrap_or_else(|| { + panic!( + "StoreOnlyReconstructor re-run: inode {} missing migration_info", + id + ) + }); + assert_eq!( + &again, expected, + "Re-run of StoreOnlyReconstructor yielded a different chain for {q:?}" + ); + } + } + + /// `StoreOnlyReconstructor` does *not* silently re-resolve symlinks during + /// preserialization: production `do_lookup` opens entries with + /// `O_PATH | O_NOFOLLOW`, so a symlink becomes its own inode in the store with an + /// `O_PATH` fd that *refers to the symlink itself*, not its target. `/proc/self/fd/` + /// for such an fd returns the symlink's own path, so the reconstructed migration_info + /// chain records the symlink filename (`link.txt`), not the target's filename + /// (`target.txt`). + /// + /// This is the property the author claimed in review feedback #1 ("没有实际 inode + /// 时不会解析"): only inodes that are actually present in the store are walked, and + /// the procfs path of an `O_PATH | O_NOFOLLOW` symlink fd is the symlink itself. + #[test] + fn storeonly_does_not_follow_symlink_file() { + let dir = TestDir::new("symlink_file"); + dir.mkdir("a"); + dir.touch("a/target.txt"); + // a/link.txt -> target.txt (relative symlink, sibling of target) + dir.symlink("target.txt", "a/link.txt"); + + let (fs, cancel) = mk_fs(dir.path()); + + // Looking up `a/link.txt` returns the symlink inode (because production lookup + // uses O_NOFOLLOW). Looking up `a/target.txt` returns the target inode. They + // must be distinct inodes -- this is the contract `StoreOnlyReconstructor` + // relies on to keep symlink and target separate in the chain. + let link_inode = *lookup_chain(&fs, &["a", "link.txt"]).last().unwrap(); + let target_inode = *lookup_chain(&fs, &["a", "target.txt"]).last().unwrap(); + assert_ne!( + link_inode, target_inode, + "production do_lookup uses O_NOFOLLOW, so the symlink and its target must be \ + two different inodes in the store" + ); + + StoreOnlyReconstructor::new(&fs, Arc::clone(&cancel)) + .execute() + .expect("StoreOnlyReconstructor execute failed"); + + // The symlink's own chain reads the symlink's filename, not the target's. + let link_chain = filename_chain_to_root(&fs, link_inode) + .expect("symlink inode must have migration_info"); + assert_eq!( + link_chain, + vec!["a".to_string(), "link.txt".to_string()], + "StoreOnlyReconstructor must record the symlink's own filename, not the \ + target it points at -- /proc/self/fd of an O_PATH|O_NOFOLLOW fd resolves \ + back to the symlink itself" + ); + + // The target's chain is unaffected. + let target_chain = filename_chain_to_root(&fs, target_inode) + .expect("target inode must have migration_info"); + assert_eq!( + target_chain, + vec!["a".to_string(), "target.txt".to_string()], + ); + + // Parity check: PathReconstructor on a fresh fs over the same fixture yields + // the same chains for both inodes. + let (fs_p, cancel_p) = mk_fs(dir.path()); + let link_p = *lookup_chain(&fs_p, &["a", "link.txt"]).last().unwrap(); + let target_p = *lookup_chain(&fs_p, &["a", "target.txt"]).last().unwrap(); + PathReconstructor::new(&fs_p, Arc::clone(&cancel_p)) + .execute() + .expect("PathReconstructor execute failed"); + let link_chain_p = filename_chain_to_root(&fs_p, link_p) + .expect("PathReconstructor: symlink inode must have migration_info"); + let target_chain_p = filename_chain_to_root(&fs_p, target_p) + .expect("PathReconstructor: target inode must have migration_info"); + assert_eq!( + link_chain_p, link_chain, + "Parity violation on symlink inode chain" + ); + assert_eq!( + target_chain_p, target_chain, + "Parity violation on target inode chain" + ); + } + + /// A symlink that points at a directory is an `S_IFLNK` inode in the store with an + /// `O_PATH | O_NOFOLLOW` fd; it has no children in the inode store (the guest + /// cannot `lookup` through it because production `do_lookup` would `openat` with + /// `O_NOFOLLOW` and refuse to traverse). The reconstructor must therefore handle + /// this isolated symlink inode without crashing and record its own filename in the + /// migration chain. + #[test] + fn storeonly_handles_symlink_to_directory_isolated() { + let dir = TestDir::new("symlink_dir"); + dir.mkdir("a/realdir"); + dir.touch("a/realdir/leaf.txt"); + // a/symdir -> realdir + dir.symlink("realdir", "a/symdir"); + + let (fs, cancel) = mk_fs(dir.path()); + // Look up the symlink-to-directory itself; production rejects walking *through* + // it, but the symlink inode itself is a valid lookup target. + let symdir_inode = *lookup_chain(&fs, &["a", "symdir"]).last().unwrap(); + let real_leaf = *lookup_chain(&fs, &["a", "realdir", "leaf.txt"]) + .last() + .unwrap(); + + StoreOnlyReconstructor::new(&fs, Arc::clone(&cancel)) + .execute() + .expect("StoreOnlyReconstructor execute failed"); + + // Symlink-to-dir gets its own chain ending in `symdir`. + let symdir_chain = filename_chain_to_root(&fs, symdir_inode) + .expect("symdir inode must have migration_info"); + assert_eq!( + symdir_chain, + vec!["a".to_string(), "symdir".to_string()], + "Symlink-to-directory must be recorded by its own filename" + ); + + // The real leaf, reached via the real directory, has the real chain. + let leaf_chain = + filename_chain_to_root(&fs, real_leaf).expect("leaf inode must have migration_info"); + assert_eq!( + leaf_chain, + vec![ + "a".to_string(), + "realdir".to_string(), + "leaf.txt".to_string(), + ], + ); + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialization.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialization.rs new file mode 100644 index 0000000..43eea49 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialization.rs @@ -0,0 +1,357 @@ +// Copyright 2024 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/// Serialization functionality (i.e. what happens in `SerializableFileSystem::serialize()`): Take +/// information that we have collected during preserialization and turn it into actually +/// serializable structs ('serialized' module), which are then turned into a plain vector of bytes. +use crate::fuse; +use crate::passthrough::device_state::preserialization::{ + self, HandleMigrationInfo, InodeMigrationInfo, +}; +use crate::passthrough::device_state::serialized; +use crate::passthrough::file_handle::{FileHandle, SerializableFileHandle}; +use crate::passthrough::inode_store::InodeData; +use crate::passthrough::stat::statx; +use crate::passthrough::util::relative_path; +use crate::passthrough::{Handle, HandleData, PassthroughFs}; +use crate::util::{other_io_error, ResultErrorContext}; +use std::convert::TryFrom; +use std::ffi::CString; +use std::io; +use std::sync::atomic::Ordering; + +impl TryFrom for Vec { + type Error = io::Error; + + /// Root of serialization: Turn the final `serialized::PassthroughFs` struct into plain bytes + fn try_from(state: serialized::PassthroughFs) -> io::Result { + postcard::to_stdvec(&state).map_err(other_io_error) + } +} + +impl TryFrom<&PassthroughFs> for serialized::PassthroughFsV1 { + type Error = io::Error; + + /// Serialize `fs`, assuming it has been prepared for serialization (i.e. all inodes must have + /// their migration info set) + fn try_from(fs: &PassthroughFs) -> io::Result { + let handles_map = fs.handles.read().unwrap(); + + debug!("serialization PassthroughFs start"); + + let inodes = if let Some(shared_dir) = fs.inodes.get(fuse::ROOT_ID) { + let shared_dir_path = shared_dir.get_path(&fs.proc_self_fd); + fs.inodes.map(|inode| { + inode + .as_ref() + .as_serialized(fs, &shared_dir, &shared_dir_path) + .unwrap_or_else(|err| { + warn!( + "Failed to serialize inode {} (st_dev={}, mnt_id={}, st_ino={}): {}; marking as invalid", + inode.inode, inode.ids.dev, inode.ids.mnt_id, inode.ids.ino, err + ); + serialized::Inode { + id: inode.inode, + refcount: inode.refcount.load(Ordering::Relaxed), + location: serialized::InodeLocation::Invalid, + file_handle: None, + } + }) + }) + } else { + // When unmounted, we will not have a root node, that's OK. But there should not be + // any other nodes either then. + if !fs.inodes.is_empty() { + return Err(other_io_error( + "Root node (shared directory) not in inode store".to_string(), + )); + }; + Vec::new() + }; + + let handles: Vec<_> = handles_map + .iter() + .map(|(handle, data)| (*handle, data.as_ref()).into()) + .collect(); + + info!( + "serialization PassthroughFs finish, total inodes: {:?}, total handles: {:?}", + inodes.len(), + handles.len() + ); + + Ok(serialized::PassthroughFsV1 { + inodes, + next_inode: fs.next_inode.load(Ordering::Relaxed), + + handles, + next_handle: fs.next_handle.load(Ordering::Relaxed), + + negotiated_opts: fs.into(), + }) + } +} + +impl From<&PassthroughFs> for serialized::NegotiatedOpts { + /// Serialize the options we have negotiated with the guest + fn from(fs: &PassthroughFs) -> Self { + debug!("serialization NegotiatedOpts"); + + serialized::NegotiatedOpts { + writeback: fs.writeback.load(Ordering::Relaxed), + announce_submounts: fs.announce_submounts.load(Ordering::Relaxed), + posix_acl: fs.posix_acl.load(Ordering::Relaxed), + sup_group_extension: fs.sup_group_extension.load(Ordering::Relaxed), + } + } +} + +impl InodeData { + /// Serialize an inode, which requires that its `migration_info` is set + fn as_serialized( + &self, + fs: &PassthroughFs, + shared_dir: &InodeData, + shared_dir_path: &io::Result, + ) -> io::Result { + let id = self.inode; + let refcount = self.refcount.load(Ordering::Relaxed); + + debug!( + "serialization Inode, inode:{:?} root inode:{:?}", + id, shared_dir.inode + ); + + // Note that we do not special-case invalid inodes here (`self.file_or_handle == + // FileOrHandle::Invalid(_)`), i.e. inodes that this instance failed to find on a prior + // incoming migration. We do not expect them to have migration info (we could not open + // them, so we should not know where to find them), but if we do, there must be a reason + // for it, so we might as well forward it to our destination. + + let migration_info_locked = self.migration_info.lock().unwrap(); + let migration_info = migration_info_locked + .as_ref() + .ok_or_else(|| other_io_error("Failed to reconstruct inode location"))?; + + // The root node (and only the root node) must have its special kind of placeholder info + assert_eq!( + (id == fuse::ROOT_ID), + matches!( + migration_info.location, + preserialization::InodeLocation::RootNode + ) + ); + + // Serialize the information that tells the destination how to find this inode + let location = migration_info.as_serialized(self, fs, shared_dir, shared_dir_path)?; + + let file_handle = if fs.cfg.migration_verify_handles { + // We could construct the file handle now, but we don't want to do I/O here. It should + // have been prepared in the preserialization phase. If it is not, that's an internal + // programming error. + let handle = migration_info + .file_handle + .as_ref() + .ok_or_else(|| other_io_error("No prepared file handle found"))?; + Some(handle.clone()) + } else { + None + }; + + Ok(serialized::Inode { + id, + refcount, + location, + file_handle, + }) + } +} + +impl InodeMigrationInfo { + /// Helper for serializing inodes: Turn their prepared `migration_info` into a + /// `serialized::InodeLocation` + fn as_serialized( + &self, + inode_data: &InodeData, + fs: &PassthroughFs, + shared_dir: &InodeData, + shared_dir_path: &io::Result, + ) -> io::Result { + debug!( + "serialization InodeMigrationInfo, inode:{:?}", + inode_data.inode + ); + + Ok(match &self.location { + preserialization::InodeLocation::RootNode => serialized::InodeLocation::RootNode, + + preserialization::InodeLocation::Path { + parent, + filename, + fullname, + } => { + if fs.cfg.migration_confirm_paths { + if let Err(err) = self.check_presence(inode_data, parent.get(), filename) { + warn!( + "Lost inode {} (former location: {}): {}; looking it up through /proc/self/fd", + inode_data.inode, filename, err + ); + // Inode is gone (or replaced), look for it in /proc/self/fd + let path_in_shared_dir = self + .path_from_proc_self_fd(inode_data, fs, shared_dir, shared_dir_path) + .err_context(|| "Failed to get path from /proc/self/fd".to_string())?; + info!("Found inode {}: {}", inode_data.inode, path_in_shared_dir); + return Ok(serialized::InodeLocation::FullPath { + filename: path_in_shared_dir, + }); + } + } + + debug!("serialization InodeMigrationInfo, name: {:?}, fullname: {:?}, shared_dir_path: {:?}, parent id: {:?}", + filename, fullname, shared_dir_path, parent.get().inode); + + // Safe: We serialize everything before we will drop the serialized state (the + // inode store), so the strong refcount in there will outlive this weak reference + // (which means that the ID we get will remain valid until everything is + // serialized, i.e. that parent node will be part of the serialized state) + let parent = unsafe { parent.get_raw() }; + let filename = filename.clone(); + let fullname = fullname.clone(); + + serialized::InodeLocation::Path { + parent, + filename, + fullname, + } + } + }) + } + + /// Check whether the given `inode_data` from our inode store can be found at the given location + /// (i.e. `filename` under parent directory `parent`) + fn check_presence( + &self, + inode_data: &InodeData, + parent: &InodeData, + filename: &str, + ) -> io::Result<()> { + let filename = CString::new(filename)?; + let parent_fd = parent.get_file()?; + let st = statx(&parent_fd, Some(&filename))?; + + if st.st.st_dev != inode_data.ids.dev { + return Err(other_io_error(format!( + "Device ID differs: Expected {}, found {}", + inode_data.ids.dev, st.st.st_dev + ))); + } + + // Try to take a file handle from `self.file_handle`; if none is there, try to generate it + // (but ignore errors, falling back to checking the inode ID). We do really want to check + // the file handle if possible, though, to detect inode ID reuse. + let (fh, fh_ref) = if let Some(fh_ref) = self.file_handle.as_ref() { + (None, Some(fh_ref)) + } else if let Ok(fh) = SerializableFileHandle::try_from(&inode_data.file_or_handle) { + (Some(fh), None) + } else { + (None, None) + }; + if let Some(fh) = fh_ref.or(fh.as_ref()) { + // If we got a file handle for `inode_data`, failing to get it for `filename` probably + // means it is a different inode. Be cautious and return an error then. + let actual_fh = FileHandle::from_name_at_fail_hard(&parent_fd, &filename) + .err_context(|| "Failed to generate file handle")?; + // Ignore mount ID: A file handle can be in two different mount IDs, but as long as it + // is on the same device, it is still the same mount ID; and we have already checked + // the device ID. + fh.require_equal_without_mount_id(&actual_fh.into()) + .map_err(other_io_error) + } else { + // Cannot generate file handle? Fall back to just the inode ID. + if st.st.st_ino != inode_data.ids.ino { + return Err(other_io_error(format!( + "Inode ID differs: Expected {}, found {}", + inode_data.ids.ino, st.st.st_ino + ))); + } + Ok(()) + } + } + + /// Retrieve the inode's path relative to the shared directory from /proc/self/fd + fn path_from_proc_self_fd( + &self, + inode_data: &InodeData, + fs: &PassthroughFs, + shared_dir: &InodeData, + shared_dir_path: &io::Result, + ) -> io::Result { + let path = inode_data.get_path(&fs.proc_self_fd)?; + + // Kernel will report nodes beyond our root as having path / -- but only the root node (the + // shared directory) can actually have that path, so we can cut the rest short and spare + // the user the more cryptic error generated by `check_presence()` + if path.as_bytes() == b"/" && inode_data.inode != fuse::ROOT_ID { + return Err(other_io_error( + "Got empty path for non-root node, so it is outside the shared directory" + .to_string(), + )); + } + + let shared_dir_path = shared_dir_path.as_ref().map_err(|err| { + io::Error::new(err.kind(), format!("Shared directory path unknown: {err}")) + })?; + + let relative_path = relative_path(&path, shared_dir_path)? + .to_str() + .map_err(|err| other_io_error(format!("Path {path:?} is not a UTF-8 string: {err}")))? + .to_string(); + + self.check_presence(inode_data, shared_dir, &relative_path) + .map_err(|err| { + io::Error::new(err.kind(), format!("Inode not found at {path:?}: {err}")) + })?; + + Ok(relative_path) + } +} + +impl From<(Handle, &HandleData)> for serialized::Handle { + /// Serialize a handle + fn from(handle: (Handle, &HandleData)) -> Self { + // Note that we will happily process invalid handles here (`handle.1.file == + // HandleDataFile::Invalid(_)`), i.e. handles that this instance failed to open on a prior + // incoming migration. A handle is identified by the inode to which it belongs, and + // instructions on how to open that inode (e.g. `open()` flags). If this instance failed + // to open the inode in this way (on in-migration), that does not prevent us from + // forwarding the same information to the next destination (on out-migration), and thus + // allow it to re-try. + + debug!( + "serialization Handle, handle:{:?} inode:{:?})", + handle.0, handle.1.inode + ); + + let source = (&handle.1.migration_info).into(); + serialized::Handle { + id: handle.0, + inode: handle.1.inode, + source, + } + } +} + +impl From<&HandleMigrationInfo> for serialized::HandleSource { + /// Helper for serializing handles: Turn their prepared `migration_info` into a + /// `serialized::HandleSource` + fn from(repr: &HandleMigrationInfo) -> Self { + debug!("serialization HandleMigrationInfo"); + + match repr { + HandleMigrationInfo::OpenInode { flags } => { + serialized::HandleSource::OpenInode { flags: *flags } + } + } + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialized.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialized.rs new file mode 100644 index 0000000..3500709 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialized.rs @@ -0,0 +1,121 @@ +// Copyright 2024 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/// Structs and enums that constitute our serialized state "on the wire". Turning them into/from +/// plain bytes still needs to be done with some serde implementation. +use crate::passthrough::file_handle::SerializableFileHandle; +use crate::passthrough::inode_store::Inode as InodeId; +use crate::passthrough::Handle as HandleId; +use serde::{Deserialize, Serialize}; + +/// Full serialized device state (for `PassthroughFs`). This is an enum so in case incompatible +/// changes have to be made, new version variants can be added while still being able to migrate +/// from older versions. +#[derive(Debug, Deserialize, Serialize)] +pub(super) enum PassthroughFs { + /// Initial version + V1(PassthroughFsV1), +} + +#[derive(Debug, Deserialize, Serialize)] +pub(super) struct PassthroughFsV1 { + /// List of all looked up inodes + pub(super) inodes: Vec, + /// Next free index for inode IDs + pub(super) next_inode: u64, + + /// List of all open files (handles) + pub(super) handles: Vec, + /// Next free index for handle IDs + pub(super) next_handle: u64, + + /// Remember which options have been negotiated during INIT + pub(super) negotiated_opts: NegotiatedOpts, +} + +/// Options that can be negotiated during INIT, i.e. ones for which we must remember whether we +/// have enabled them after negotiating with the guest +#[derive(Debug, Deserialize, Serialize)] +pub(super) struct NegotiatedOpts { + pub(super) writeback: bool, + pub(super) announce_submounts: bool, + pub(super) posix_acl: bool, + pub(super) sup_group_extension: bool, +} + +/// Serializable data for an inode that has been looked up +#[derive(Debug, Deserialize, Serialize)] +pub(super) struct Inode { + /// Own inode ID + pub(super) id: InodeId, + + /// Current refcount + pub(super) refcount: u64, + + /// Description of this inode that allows the destination to find it + pub(super) location: InodeLocation, + + /// Inode file handle. If present, the destination is not supposed to open this file handle, + /// but instead compare it against the one of the inode it has opened based on `location`. + pub(super) file_handle: Option, +} + +/// Serializable description of some inode that allows the destination to find it +#[derive(Debug, Deserialize, Serialize)] +pub(super) enum InodeLocation { + /// The root node is not given a serialized location; the destination is supposed to find it on + /// its own + RootNode, + + /// Described by its path: The destination will have to open the given filename + Path { + /// ID of the parent inode + parent: InodeId, + + /// A filename relative to the parent that allows opening this inode. Note that using + /// `String` restricts us to paths that can be represented as UTF-8, which is not + /// necessarily a restriction that all operating systems have. However, we need to use + /// some common encoding (i.e., cannot use `OsString`), or otherwise we could not migrate + /// between operating systems using different string representations. + filename: String, + fullname: String, + }, + + /// Source has deemed that this inode can no longer be found. The destination needs to decide + /// how to proceed (e.g. whether to abort migration or simply remember that this inode is + /// invalid and tell the guest so). + Invalid, + + /// Described by its path: The destination will have to open the given filename relative to the + /// shared directory (the root node). In contrast to `Path`, there is no strong reference to + /// the shared directory node. + FullPath { + /// Filename relative to the shared directory root node. Stored in UTF-8, just like + /// `Path.filename`. + filename: String, + }, +} + +/// Serializable representation of an open file (a handle) +#[derive(Debug, Deserialize, Serialize)] +pub(super) struct Handle { + /// Own handle ID + pub(super) id: HandleId, + + /// Inode to which this handle refers + pub(super) inode: InodeId, + + /// Describes where this handle comes from, so the destination can open it + pub(super) source: HandleSource, +} + +/// Serializable description of some handle that allows the destination to open it +#[derive(Debug, Deserialize, Serialize)] +pub(super) enum HandleSource { + /// Handle should be opened by opening `Handle.inode` with the `open(2)` flags given here + OpenInode { + /// Flags passed to `openat(2)` + flags: i32, + }, +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/file_handle.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/file_handle.rs new file mode 100644 index 0000000..b106446 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/file_handle.rs @@ -0,0 +1,199 @@ +// Copyright 2021 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use crate::oslib; +use crate::passthrough::mount_fd::{MPRResult, MountFd, MountFds}; +use crate::passthrough::stat::MountId; +use serde::{Deserialize, Serialize}; +use std::convert::{TryFrom, TryInto}; +use std::ffi::CStr; +use std::fs::File; +use std::io; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::sync::Arc; + +const EMPTY_CSTR: &[u8] = b"\0"; + +#[derive(Clone, PartialOrd, Ord, PartialEq, Eq)] +pub struct FileHandle { + mnt_id: MountId, + handle: oslib::CFileHandle, +} + +pub struct OpenableFileHandle { + handle: FileHandle, + mount_fd: Arc, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct SerializableFileHandle { + mnt_id: u64, + handle_type: i32, + handle: Vec, +} + +pub enum FileOrHandle { + File(File), + Handle(OpenableFileHandle), + // `io::Error` does not implement `Clone`, so without wrapping it in `Arc`, returning the error + // anywhere would be impossible without consuming it + Invalid(Arc), +} + +impl FileHandle { + /// Try to create a file handle for the given file. In contrast to `from_name_at()`, this will + /// always return a file handle or an error. + pub fn from_name_at_fail_hard(dir: &impl AsRawFd, path: &CStr) -> io::Result { + let mut mount_id: libc::c_int = 0; + let mut c_fh = oslib::CFileHandle::default(); + + oslib::name_to_handle_at(dir, path, &mut c_fh, &mut mount_id, libc::AT_EMPTY_PATH)?; + Ok(FileHandle { + mnt_id: mount_id as MountId, + handle: c_fh, + }) + } + + /// Create a file handle for the given file. + /// + /// Return `Ok(None)` if no file handle can be generated for this file: Either because the + /// filesystem does not support it, or because it would require a larger file handle than we + /// can store. These are not intermittent failures, i.e. if this function returns `Ok(None)` + /// for a specific file, it will always return `Ok(None)` for it. Conversely, if this function + /// returns `Ok(Some)` at some point, it will never return `Ok(None)` later. + /// + /// Return an `io::Error` for all other errors. + pub fn from_name_at(dir: &impl AsRawFd, path: &CStr) -> io::Result> { + match Self::from_name_at_fail_hard(dir, path) { + Ok(fh) => Ok(Some(fh)), + Err(err) => match err.raw_os_error() { + // Filesystem does not support file handles + Some(libc::EOPNOTSUPP) => Ok(None), + // Handle would need more bytes than `MAX_HANDLE_SZ` + Some(libc::EOVERFLOW) => Ok(None), + // Other error + _ => Err(err), + }, + } + } + + /// Try to create a file handle for `fd`. In contrast to `from_fd()`, this will always return + /// a file handle or an error. + pub fn from_fd_fail_hard(fd: &impl AsRawFd) -> io::Result { + // Safe because this is a constant value and a valid C string. + let empty_path = unsafe { CStr::from_bytes_with_nul_unchecked(EMPTY_CSTR) }; + Self::from_name_at_fail_hard(fd, empty_path) + } + + /// Create a file handle for `fd`. + /// This is a wrapper around `from_name_at()` and so has the same interface. + pub fn from_fd(fd: &impl AsRawFd) -> io::Result> { + // Safe because this is a constant value and a valid C string. + let empty_path = unsafe { CStr::from_bytes_with_nul_unchecked(EMPTY_CSTR) }; + Self::from_name_at(fd, empty_path) + } + + /** + * Return an openable copy of the file handle by ensuring that `mount_fds` contains a valid fd + * for the mount the file handle is for. + * + * `reopen_fd` will be invoked to duplicate an `O_PATH` fd with custom `libc::open()` flags. + */ + pub fn to_openable( + &self, + mount_fds: &MountFds, + reopen_fd: F, + ) -> MPRResult + where + F: FnOnce(RawFd, libc::c_int) -> io::Result, + { + Ok(OpenableFileHandle { + handle: self.clone(), + mount_fd: mount_fds.get(self.mnt_id, reopen_fd)?, + }) + } +} + +impl OpenableFileHandle { + pub fn inner(&self) -> &FileHandle { + &self.handle + } + + /** + * Open a file handle, using our mount FDs hash map. + */ + pub fn open(&self, flags: libc::c_int) -> io::Result { + oslib::open_by_handle_at(self.mount_fd.file(), &self.handle.handle, flags) + } +} + +impl SerializableFileHandle { + /// Compare `self` against `other`, disregarding the mount ID. Return a more or less + /// descriptive error if both handles are not equal. + pub fn require_equal_without_mount_id(&self, other: &Self) -> Result<(), String> { + if self.handle_type != other.handle_type { + Err(format!( + "File handle type differs: 0x{:x} != 0x{:x}", + self.handle_type, other.handle_type + )) + } else if self.handle != other.handle { + use std::fmt::Write; + let mut description = "File handle differs:".to_string(); + for b in self.handle.iter() { + let _ = write!(&mut description, " {b:02x}"); + } + description += " !="; + for b in other.handle.iter() { + let _ = write!(&mut description, " {b:02x}"); + } + Err(description) + } else { + Ok(()) + } + } + + /// Compare `self` against `other`. Return a more or less descriptive error if both handles + /// are not equal. + pub fn require_equal(&self, other: &Self) -> Result<(), String> { + if self.mnt_id != other.mnt_id { + Err(format!( + "File handle mount ID differs: {} != {}", + self.mnt_id, other.mnt_id + )) + } else { + self.require_equal_without_mount_id(other) + } + } +} + +impl From<&FileHandle> for SerializableFileHandle { + fn from(fh: &FileHandle) -> SerializableFileHandle { + SerializableFileHandle { + mnt_id: fh.mnt_id, + #[allow(clippy::useless_conversion)] + handle_type: fh.handle.handle_type().try_into().unwrap(), + handle: fh.handle.as_bytes().into(), + } + } +} + +impl From for SerializableFileHandle { + fn from(fh: FileHandle) -> SerializableFileHandle { + (&fh).into() + } +} + +impl TryFrom<&FileOrHandle> for SerializableFileHandle { + type Error = io::Error; + + fn try_from(file_or_handle: &FileOrHandle) -> io::Result { + match file_or_handle { + FileOrHandle::Handle(handle) => Ok(handle.inner().into()), + FileOrHandle::File(file) => { + FileHandle::from_fd_fail_hard(file).map(SerializableFileHandle::from) + } + FileOrHandle::Invalid(err) => Err(io::Error::new(err.kind(), Arc::clone(err))), + } + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/inode_store.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/inode_store.rs new file mode 100644 index 0000000..e65f240 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/inode_store.rs @@ -0,0 +1,599 @@ +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use crate::fuse; +use crate::passthrough::device_state::preserialization::{InodeLocation, InodeMigrationInfo}; +use crate::passthrough::file_handle::{FileHandle, FileOrHandle}; +use crate::passthrough::stat::MountId; +use crate::passthrough::util::{ebadf, get_path_by_fd, is_safe_inode, reopen_fd_through_proc}; +use crate::util::other_io_error; +use std::collections::BTreeMap; +use std::ffi::CString; +use std::fs::File; +use std::io; +use std::ops::Deref; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, RwLock}; + +pub type Inode = u64; + +#[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)] +pub struct InodeIds { + pub ino: libc::ino64_t, + pub dev: libc::dev_t, + pub mnt_id: MountId, +} + +/// Strong reference to some inode in our inode store, which is counted against the +/// `InodeData.refcount` field. Dropping this object will thus decrement that refcount, and +/// potentially remove the inode from the store (when the refcount reaches 0). +/// Note that dropping this object locks its inode store, so care must be taken not to drop strong +/// references while the inode store is locked, or to use `StrongInodeReference::drop_unlocked()`. +pub struct StrongInodeReference { + /// Referenced inode's data. + /// Is only `None` after the inode has been leaked, which cannot occur outside of `leak()` and + /// `drop()`, because `leak()` consumes the object. + inode_data: Option>, + + /// Inode store that holds the referenced inode. + inode_store: Arc>, +} + +pub struct InodeData { + pub inode: Inode, + // Most of these aren't actually files but ¯\_(ツ)_/¯. + pub file_or_handle: FileOrHandle, + pub refcount: AtomicU64, + + // Used as key in the `InodeStoreInner::by_ids` map. + pub ids: InodeIds, + + // File type and mode + pub mode: u32, + + // Constructed in the `prepare_serialization` phase of migration, and must be set on all inodes + // when we are actually going to serialize our internal state to send it to the migration + // destination. + // Because this may contain a strong inode reference, which must not be dropped while the inode + // store is locked, this info must in turn not be dropped while the store is locked. + // To ensure this, locking of the store is only done here in this file, and here we ensure that + // while the store is locked, `InodeMigrationInfo` (e.g. as part of an `InodeData`) is dropped + // only by using `drop_unlocked()` for a potentially contained strong reference. + pub(super) migration_info: Mutex>, +} + +/** + * Represents the file associated with an inode (`InodeData`). + * + * When obtaining such a file, it may either be a new file (the `Owned` variant), in which case the + * object's lifetime is static, or it may reference `InodeData.file` (the `Ref` variant), in which + * case the object's lifetime is that of the respective `InodeData` object. + */ +pub enum InodeFile<'inode_lifetime> { + Owned(File), + Ref(&'inode_lifetime File), +} + +#[derive(Default)] +struct InodeStoreInner { + data: BTreeMap>, + by_ids: BTreeMap, + by_handle: BTreeMap, +} + +#[derive(Default)] +pub struct InodeStore { + inner: Arc>, +} + +impl<'a> InodeData { + /// Get an `O_PATH` file for this inode + pub fn get_file(&'a self) -> io::Result> { + match &self.file_or_handle { + FileOrHandle::File(f) => Ok(InodeFile::Ref(f)), + FileOrHandle::Handle(h) => { + let file = h.open(libc::O_PATH)?; + Ok(InodeFile::Owned(file)) + } + FileOrHandle::Invalid(err) => Err(io::Error::new(err.kind(), Arc::clone(err))), + } + } + + /// Try to obtain this inode's path through /proc/self/fd + pub fn get_path(&self, proc_self_fd: &File) -> io::Result { + let path = get_path_by_fd(&self.get_file()?, proc_self_fd)?; + + // Kernel will report nodes beyond our root as having path / -- but only the root node (the + // shared directory) can actually have that path, so for others, it must be inaccurate + if path.as_bytes() == b"/" && self.inode != fuse::ROOT_ID { + return Err(other_io_error( + "Got empty path for non-root node, so it is outside the shared directory" + .to_string(), + )); + } + + Ok(path) + } + + /// Open this inode with the given flags + /// (always returns a new (i.e. `Owned`) file, hence the static lifetime) + pub fn open_file( + &self, + flags: libc::c_int, + proc_self_fd: &File, + ) -> io::Result> { + // Do not move the `is_safe_inode()` check up: It is always false for invalid inodes, so + // would hide their perfectly good error message + match &self.file_or_handle { + FileOrHandle::File(f) => { + if !is_safe_inode(self.mode) { + return Err(ebadf()); + } + let new_file = reopen_fd_through_proc(f, flags, proc_self_fd)?; + Ok(InodeFile::Owned(new_file)) + } + FileOrHandle::Handle(h) => { + if !is_safe_inode(self.mode) { + return Err(ebadf()); + } + let new_file = h.open(flags)?; + Ok(InodeFile::Owned(new_file)) + } + FileOrHandle::Invalid(err) => Err(io::Error::new(err.kind(), Arc::clone(err))), + } + } +} + +impl InodeFile<'_> { + /// Create a standalone `File` object + pub fn into_file(self) -> io::Result { + match self { + Self::Owned(file) => Ok(file), + Self::Ref(file_ref) => file_ref.try_clone(), + } + } +} + +impl AsRawFd for InodeFile<'_> { + /// Return a file descriptor for this file + /// Note: This fd is only valid as long as the `InodeFile` exists. + fn as_raw_fd(&self) -> RawFd { + match self { + Self::Owned(file) => file.as_raw_fd(), + Self::Ref(file_ref) => file_ref.as_raw_fd(), + } + } +} + +impl InodeStoreInner { + /// Insert a new entry into the inode store. Panics if the entry already existed. + /// (This guarantees that inserting a value will not drop an existing `InodeMigrationInfo` + /// object.) + fn insert_new(&mut self, data: Arc) { + // Overwriting something in `by_ids` or `by_handle` is not exactly what we want, but having + // the same physical inode under several different FUSE IDs is not catastrophic, so do not + // panic about that. + self.by_ids.insert(data.ids, data.inode); + if let FileOrHandle::Handle(handle) = &data.file_or_handle { + self.by_handle.insert(handle.inner().clone(), data.inode); + } + let existing = self.data.insert(data.inode, data); + assert!(existing.is_none()); + } + + /// Remove the given inode, and, if found, take care to drop any associated strong reference in + /// the migration info via `drop_unlocked()`. + fn remove(&mut self, inode: Inode) { + let data = self.data.remove(&inode); + if let Some(data) = data { + if let FileOrHandle::Handle(handle) = &data.file_or_handle { + self.by_handle.remove(handle.inner()); + } + self.by_ids.remove(&data.ids); + if let Some(mig_info) = data.migration_info.lock().unwrap().take() { + match mig_info.location { + InodeLocation::RootNode => (), + InodeLocation::Path { parent, .. } => parent.drop_unlocked(self), + } + } + } + } + + fn clear(&mut self) { + self.clear_migration_info(); + self.data.clear(); + self.by_handle.clear(); + self.by_ids.clear(); + } + + /// Clears all migration info, using `drop_unlocked()` to drop any strong references within. + fn clear_migration_info(&mut self) { + let mut strong_references = Vec::::new(); + for inode in self.data.values() { + if let Some(mig_info) = inode.migration_info.lock().unwrap().take() { + match mig_info.location { + InodeLocation::RootNode => (), + InodeLocation::Path { parent, .. } => strong_references.push(parent), + } + } + } + for strong_reference in strong_references { + strong_reference.drop_unlocked(self); + } + } + + fn get(&self, inode: Inode) -> Option<&Arc> { + self.data.get(&inode) + } + + fn get_by_ids(&self, ids: &InodeIds) -> Option<&Arc> { + self.inode_by_ids(ids).map(|inode| self.get(inode).unwrap()) + } + + fn get_by_handle(&self, handle: &FileHandle) -> Option<&Arc> { + self.inode_by_handle(handle) + .map(|inode| self.get(inode).unwrap()) + } + + fn contains(&self, inode: Inode) -> bool { + self.data.contains_key(&inode) + } + + fn inode_by_ids(&self, ids: &InodeIds) -> Option { + self.by_ids.get(ids).copied() + } + + fn inode_by_handle(&self, handle: &FileHandle) -> Option { + self.by_handle.get(handle).copied() + } + + fn is_empty(&self) -> bool { + self.data.is_empty() + } + + /// Decrement the refcount of the given `inode` ID, and remove it from the store when it + /// reaches 0 + fn forget_one(&mut self, inode: Inode, count: u64) { + if let Some(data) = self.get(inode) { + // Having a mutable reference on `self` prevents concurrent lookups from incrementing + // the refcount but there is the possibility that a previous lookup already acquired a + // reference to the inode data and is in the process of updating the refcount so we + // need to loop here until we can decrement successfully. + loop { + let refcount = data.refcount.load(Ordering::Relaxed); + + // Saturating sub because it doesn't make sense for a refcount to go below zero and + // we don't want misbehaving clients to cause integer overflow. + let new_count = refcount.saturating_sub(count); + + // We don't need any stronger ordering, because the refcount itself doesn't protect + // any data. + if data.refcount.compare_exchange( + refcount, + new_count, + Ordering::Relaxed, + Ordering::Relaxed, + ) == Ok(refcount) + { + if new_count == 0 { + // We just removed the last refcount for this inode. There's no need for an + // acquire fence here because we have a mutable reference on `self`. So + // there's is no other release store for us to synchronize with before + // deleting the entry. + self.remove(inode); + } + break; + } + } + } + } +} + +impl InodeStore { + pub fn get(&self, inode: Inode) -> Option> { + self.inner.read().unwrap().get(inode).cloned() + } + + pub fn get_by_ids(&self, ids: &InodeIds) -> Option> { + self.inner.read().unwrap().get_by_ids(ids).cloned() + } + + pub fn get_by_handle(&self, handle: &FileHandle) -> Option> { + self.inner.read().unwrap().get_by_handle(handle).cloned() + } + + pub fn inode_by_ids(&self, ids: &InodeIds) -> Option { + self.inner.read().unwrap().inode_by_ids(ids) + } + + pub fn inode_by_handle(&self, handle: &FileHandle) -> Option { + self.inner.read().unwrap().inode_by_handle(handle) + } + + /// Invoke `func()` on each inode, collect all results, and return them. Note that the inode + /// store is read-locked when `func()` is called. + pub fn map) -> V>(&self, func: F) -> Vec { + self.inner.read().unwrap().data.values().map(func).collect() + } + + /// Turn the weak reference `inode` into a strong one (increments its refcount) + pub fn get_strong(&self, inode: Inode) -> io::Result { + StrongInodeReference::new(inode, self) + } + + /// Attempt to get an inode from `inodes` and create a strong reference to it, i.e. increment + /// its refcount. Return that reference on success, and an error on failure. + /// Reasons for failure can be that the inode isn't in the map or that the refcount is zero. + /// This function will never increment a refcount that's already zero. + /// Note that dropping the returned strong reference will automatically decrement the refcount + /// again. + pub fn claim_inode( + &self, + handle: Option<&FileHandle>, + ids: &InodeIds, + ) -> io::Result { + self.do_claim_inode(&self.inner.read().unwrap(), handle, ids) + } + + fn do_claim_inode>( + &self, + inner: &I, + handle: Option<&FileHandle>, + ids: &InodeIds, + ) -> io::Result { + let data = handle + .and_then(|h| inner.get_by_handle(h)) + .or_else(|| { + inner.get_by_ids(ids).filter(|data| { + // When we have to fall back to looking up an inode by its inode ID, ensure + // that we hit an entry that has a valid file descriptor. Having an FD open + // means that the inode cannot really be deleted until the FD is closed, so + // that the inode ID remains valid until we evict the `InodeData`. With no FD + // open (and just a file handle), the inode can be deleted while we still have + // our `InodeData`, and so the inode ID may be reused by a completely different + // new inode. Such inodes must be looked up by file handle, because this + // handle contains a generation ID to differentiate between the old and the new + // inode. + matches!(data.file_or_handle, FileOrHandle::File(_)) + }) + }) + .ok_or_else(|| { + io::Error::new( + io::ErrorKind::NotFound, + "Cannot take strong reference to inode by handle or IDs, not found".to_string(), + ) + })?; + + StrongInodeReference::new_with_data(Arc::clone(data), self) + } + + /// Check whether a matching inode is already present (see `claim_inode`), and if so, return + /// that inode and drop `inode_data`. + /// Otherwise, insert `inode_data`, and return a strong reference to it. `inode_data.refcount` + /// is ignored; the returned strong reference is the only one that can exist, so the refcount + /// is hard-set to 1. + pub fn get_or_insert(&self, mut inode_data: InodeData) -> io::Result { + let mut inner = self.inner.write().unwrap(); + let handle = match &inode_data.file_or_handle { + FileOrHandle::File(_) => None, + FileOrHandle::Handle(handle) => Some(handle.inner()), + FileOrHandle::Invalid(_) => None, + }; + if let Ok(inode) = self.do_claim_inode(&inner, handle, &inode_data.ids) { + // `InodeData`s should not be dropped while the inode store is locked, so drop the lock + // before `inode_data` + drop(inner); + return Ok(inode); + } + if inner.contains(inode_data.inode) { + // `InodeData`s should not be dropped while the inode store is locked, so drop the lock + // before `inode_data` + drop(inner); + return Err(other_io_error(format!( + "Double-use of FUSE inode ID {}", + inode_data.inode + ))); + } + + // Safe because we have the only reference + inode_data.refcount = AtomicU64::new(1); + let inode_data = Arc::new(inode_data); + inner.insert_new(Arc::clone(&inode_data)); + + // We just set the reference to 1 to account for this + Ok(unsafe { StrongInodeReference::new_no_increment(inode_data, self) }) + } + + /// Insert `inode_data` into the inode store regardless of whether a matching inode already + /// exists. However, if the given inode ID already exists, return an error and drop + /// `inode_data.` + pub fn new_inode(&self, inode_data: InodeData) -> io::Result<()> { + let mut inner = self.inner.write().unwrap(); + if inner.contains(inode_data.inode) { + // `InodeData`s should not be dropped while the inode store is locked, so drop the lock + // before `inode_data` + drop(inner); + return Err(other_io_error(format!( + "Double-use of FUSE inode ID {}", + inode_data.inode + ))); + } + inner.insert_new(Arc::new(inode_data)); + Ok(()) + } + + pub fn remove(&self, inode: Inode) { + self.inner.write().unwrap().remove(inode); + } + + pub fn forget_one(&self, inode: Inode, count: u64) { + self.inner.write().unwrap().forget_one(inode, count); + } + + pub fn forget_many>(&self, inodes: I) { + let mut inner = self.inner.write().unwrap(); + for (inode, count) in inodes { + inner.forget_one(inode, count); + } + } + + pub fn clear(&self) { + self.inner.write().unwrap().clear(); + } + + pub fn clear_migration_info(&self) { + self.inner.write().unwrap().clear_migration_info(); + } + + pub fn is_empty(&self) -> bool { + self.inner.read().unwrap().is_empty() + } +} + +impl StrongInodeReference { + /// Create a new strong reference to the given inode in the given inode store, incrementing the + /// refcount appropriately. + pub fn new(inode: Inode, inode_store: &InodeStore) -> io::Result { + let inode_data = inode_store.get(inode).ok_or_else(|| { + io::Error::new( + io::ErrorKind::NotFound, + format!("Cannot take strong reference to inode {inode}: Not found"), + ) + })?; + + Self::new_with_data(inode_data, inode_store) + } + + /// Create a new strong reference to an inode with the given data from the given inode store, + /// incrementing the refcount appropriately. + pub fn new_with_data(inode_data: Arc, inode_store: &InodeStore) -> io::Result { + Self::increment_refcount_for(&inode_data)?; + + // Safe because we have just incremented the refcount + Ok(unsafe { StrongInodeReference::new_no_increment(inode_data, inode_store) }) + } + + /// Create a new strong reference to an inode with the given data from the given inode store, + /// but do not increment the inode's refcount, and instead assume that the caller has already + /// done it. + /// + /// # Safety + /// Caller ensures the inode's refcount is incremented by 1 to account for this strong + /// reference. + pub unsafe fn new_no_increment(inode_data: Arc, inode_store: &InodeStore) -> Self { + StrongInodeReference { + inode_data: Some(inode_data), + inode_store: Arc::clone(&inode_store.inner), + } + } + + /// Tries to increment the refcount in the given `inode_data`, but will refuse to increment a + /// refcount that is 0 (because in this case, the inode is already in the process of being + /// removed from the store, so continuing to use it would not be safe). + fn increment_refcount_for(inode_data: &InodeData) -> io::Result<()> { + // Use `.fetch_update()` instead of `.fetch_add()` to ensure we never increment the + // refcount from zero to one. + match inode_data + .refcount + .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |rc| { + (rc > 0).then_some(rc + 1) + }) { + Ok(_old_rc) => Ok(()), + Err(_old_rc) => Err(io::Error::new( + io::ErrorKind::NotFound, + format!( + "Cannot take strong reference to inode {}: Is already deleted", + inode_data.inode + ), + )), + } + } + + /// Consume this strong reference, yield the underlying inode ID, without decrementing the + /// inode's refcount. + /// + /// # Safety + /// Caller must guarantee that the refcount is tracked somehow still, i.e. that forget_one() + /// will eventually be called. Otherwise, this inode will be truly leaked, which generally is + /// not good. + pub unsafe fn leak(mut self) -> Inode { + // Unwrapping is safe: Every initializer sets this to `Some(_)`, and every function that + // `take()`s the value (`leak()`, `drop_unlocked()`, `drop()`) also consumes `self`, so + // outside of them, this must always be `None`. + self.inode_data.take().unwrap().inode + } + + /// Yield the underlying inode ID. + /// + /// # Safety + /// The inode ID is technically a form of a weak reference. To ensure safety, the caller may + /// not assume that it is valid beyond the lifetime of the corresponding strong reference. + pub unsafe fn get_raw(&self) -> Inode { + // Unwrapping is safe: Every initializer sets this to `Some(_)`, and every function that + // `take()`s the value (`leak()`, `drop_unlocked()`, `drop()`) also consumes `self`, so + // outside of them, this must always be `None`. + self.inode_data.as_ref().unwrap().inode + } + + /// Get the associated inode data. + pub fn get(&self) -> &InodeData { + // Unwrapping is safe: Every initializer sets this to `Some(_)`, and every function that + // `take()`s the value (`leak()`, `drop_unlocked()`, `drop()`) also consumes `self`, so + // outside of them, this must always be `None`. + self.inode_data.as_ref().unwrap() + } + + /// This function allows dropping a `StrongInodeReference` while the inode store is locked, but + /// the caller must have mutable access to the inode store. + fn drop_unlocked(mut self, inodes: &mut InodeStoreInner) { + if let Some(inode_data) = self.inode_data.take() { + inodes.forget_one(inode_data.inode, 1); + } + } +} + +impl Clone for StrongInodeReference { + /// Create an additional strong reference. + fn clone(&self) -> Self { + // Unwrapping is safe: Every initializer sets this to `Some(_)`, and every function that + // `take()`s the value (`leak()`, `drop_unlocked()`, `drop()`) also consumes `self`, so + // outside of them, this must always be `None`. + let cloned_data = Arc::clone(self.inode_data.as_ref().unwrap()); + let cloned_store = Arc::clone(&self.inode_store); + + // Unwrapping is safe, because this can only fail if the refcount became 0, which is + // impossible because `self` is a strong reference + Self::increment_refcount_for(&cloned_data).unwrap(); + + StrongInodeReference { + inode_data: Some(cloned_data), + inode_store: cloned_store, + } + } +} + +impl Drop for StrongInodeReference { + /// Decrement the refcount on the referenced inode, removing it from the store when the + /// refcount reaches 0. + /// Note that this function locks `self.inode_store`, so a `StrongInodeReference` must not be + /// dropped while that inode store is locked. In such a case, + /// `StrongInodeReference::drop_unlocked()` must be used. + fn drop(&mut self) { + if let Some(inode_data) = self.inode_data.take() { + self.inode_store + .write() + .unwrap() + .forget_one(inode_data.inode, 1); + } + } +} + +impl Drop for InodeStore { + /// Explicitly clear the inner inode store on drop, because there may be circular references + /// within (in the migration info's strong references) that may otherwise prevent the + /// `InodeStoreInner` from being dropped. + fn drop(&mut self) { + self.inner.write().unwrap().clear(); + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/mod.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/mod.rs new file mode 100644 index 0000000..16e5b5a --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/mod.rs @@ -0,0 +1,2698 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +pub mod credentials; +pub mod device_state; +pub mod file_handle; +pub mod inode_store; +pub mod mount_fd; +pub mod read_only; +pub mod stat; +pub mod util; +pub mod xattrmap; + +use super::fs_cache_req_handler::FsCacheReqHandler; +use crate::filesystem::{ + Context, Entry, Extensions, FileSystem, FsOptions, GetxattrReply, ListxattrReply, OpenOptions, + SecContext, SetattrValid, SetxattrFlags, ZeroCopyReader, ZeroCopyWriter, +}; +use crate::passthrough::credentials::{drop_effective_cap, UnixCredentials}; +use crate::passthrough::device_state::preserialization::{HandleMigrationInfo, InodeMigrationInfo}; +use crate::passthrough::inode_store::{ + Inode, InodeData, InodeFile, InodeIds, InodeStore, StrongInodeReference, +}; +use crate::passthrough::util::{ + ebadf, is_safe_inode, openat, reopen_fd_through_proc, validate_dentry_name, +}; +use crate::read_dir::ReadDir; +use crate::{fuse, oslib}; +use file_handle::{FileHandle, FileOrHandle, OpenableFileHandle}; +use mount_fd::{MPRError, MountFds}; +use stat::{statx, StatExt}; +use std::borrow::Cow; +use std::collections::{btree_map, BTreeMap, HashMap}; +use std::ffi::{CStr, CString}; +use std::fs::File; +use std::io; +use std::io::ErrorKind; +use std::mem::MaybeUninit; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::str::FromStr; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, OnceLock, RwLock}; +use std::time::Duration; +use xattrmap::{AppliedRule, XattrMap}; + +const EMPTY_CSTR: &[u8] = b"\0"; + +type Handle = u64; + +enum HandleDataFile { + File(RwLock), + // `io::Error` does not implement `Clone`, so without wrapping it in `Arc`, returning the error + // anywhere would be impossible without consuming it + Invalid(Arc), +} + +struct HandleData { + inode: Inode, + file: HandleDataFile, + + // On migration, must be set when we serialize our internal state to send it to the + // destination. As long as `HandleMigrationInfo::new()` is cheap, we may as well + // keep it always set. + migration_info: HandleMigrationInfo, +} + +struct ScopedWorkingDirectory { + back_to: RawFd, +} + +impl ScopedWorkingDirectory { + fn new(new_wd: RawFd, old_wd: RawFd) -> ScopedWorkingDirectory { + oslib::fchdir(new_wd).expect("the working directory should be changed"); + ScopedWorkingDirectory { back_to: old_wd } + } +} + +impl Drop for ScopedWorkingDirectory { + fn drop(&mut self) { + oslib::fchdir(self.back_to).expect("the working directory should be changed"); + } +} + +fn set_working_directory(new_wd: RawFd, old_wd: RawFd) -> ScopedWorkingDirectory { + ScopedWorkingDirectory::new(new_wd, old_wd) +} + +/// The caching policy that the file system should report to the FUSE client. By default the FUSE +/// protocol uses close-to-open consistency. This means that any cached contents of the file are +/// invalidated the next time that file is opened. +#[derive(Default, Debug, Clone)] +pub enum CachePolicy { + /// The client should never cache file data and all I/O should be directly forwarded to the + /// server. This policy must be selected when file contents may change without the knowledge of + /// the FUSE client (i.e., the file system does not have exclusive access to the directory). + Never, + + /// This is almost same as Never, but it allows page cache of directories, dentries and attr + /// cache in guest. In other words, it acts like cache=never for normal files, and like + /// cache=always for directories, besides, metadata like dentries and attrs are kept as well. + /// This policy can be used if: + /// 1. the client wants to use Never policy but it's performance in I/O is not good enough + /// 2. the file system has exclusive access to the directory + /// 3. cache directory content and other fs metadata can make a difference on performance. + Metadata, + + /// The client is free to choose when and how to cache file data. This is the default policy and + /// uses close-to-open consistency as described in the enum documentation. + #[default] + Auto, + + /// The client should always cache file data. This means that the FUSE client will not + /// invalidate any cached data that was returned by the file system the last time the file was + /// opened. This policy should only be selected when the file system has exclusive access to the + /// directory. + Always, + + /// No cache policy. The client should never cache file data or directory data. + /// All I/O for both files and directories should be directly forwarded to the server. + /// This is the most conservative policy and should be used when maximum consistency is required + /// for both files and directories. + None, +} + +impl FromStr for CachePolicy { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + match &s.to_lowercase()[..] { + "never" => Ok(CachePolicy::Never), + "metadata" => Ok(CachePolicy::Metadata), + "auto" => Ok(CachePolicy::Auto), + "always" => Ok(CachePolicy::Always), + "none" => Ok(CachePolicy::None), + _ => Err("invalid cache policy"), + } + } +} + +/// When to use file handles to reference inodes instead of `O_PATH` file descriptors. +#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)] +pub enum InodeFileHandlesMode { + /// Never use file handles, always use `O_PATH` file descriptors. + #[default] + Never, + + /// Attempt to generate file handles, but fall back to `O_PATH` file descriptors where the + /// underlying filesystem does not support file handles. + Prefer, + + /// Always use file handles, never fall back to `O_PATH` file descriptors. + Mandatory, +} + +/// What to do when an error occurs during migration (checked on the migration destination) +#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)] +pub enum MigrationOnError { + /// Whenever any failure occurs, return a hard error to the vhost-user front-end (e.g. QEMU), + /// aborting migration. + #[default] + Abort, + + /// Let migration finish, but the guest will be unable to access any of the files that were + /// failed to be found/opened, receiving only errors. + GuestError, +} + +impl FromStr for MigrationOnError { + type Err = &'static str; + + fn from_str(s: &str) -> std::result::Result { + match s { + "abort" => Ok(MigrationOnError::Abort), + "guest-error" => Ok(MigrationOnError::GuestError), + + _ => Err("invalid migration-on-error value"), + } + } +} + +/// How to migrate our internal state to the destination instance +#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)] +pub enum MigrationMode { + /// Iterate through the shared directory to find paths for all inodes indexed and opened by the + /// guest, and transfer these paths to the destination. + #[default] + FindPaths, +} + +impl FromStr for MigrationMode { + type Err = &'static str; + + fn from_str(s: &str) -> std::result::Result { + match s { + "find-paths" => Ok(MigrationMode::FindPaths), + + _ => Err("invalid migration-mode value"), + } + } +} + +/// Options that configure the behavior of the file system. +#[derive(Debug)] +pub struct Config { + /// How long the FUSE client should consider directory entries to be valid. If the contents of a + /// directory can only be modified by the FUSE client (i.e., the file system has exclusive + /// access), then this should be a large value. + /// + /// The default value for this option is 5 seconds. + pub entry_timeout: Duration, + + /// How long the FUSE client should consider file and directory attributes to be valid. If the + /// attributes of a file or directory can only be modified by the FUSE client (i.e., the file + /// system has exclusive access), then this should be set to a large value. + /// + /// The default value for this option is 5 seconds. + pub attr_timeout: Duration, + + /// The caching policy the file system should use. See the documentation of `CachePolicy` for + /// more details. + pub cache_policy: CachePolicy, + + /// Whether the file system should enabled writeback caching. This can improve performance as it + /// allows the FUSE client to cache and coalesce multiple writes before sending them to the file + /// system. However, enabling this option can increase the risk of data corruption if the file + /// contents can change without the knowledge of the FUSE client (i.e., the server does **NOT** + /// have exclusive access). Additionally, the file system should have read access to all files + /// in the directory it is serving as the FUSE client may send read requests even for files + /// opened with `O_WRONLY`. + /// + /// Therefore callers should only enable this option when they can guarantee that: 1) the file + /// system has exclusive access to the directory and 2) the file system has read permissions for + /// all files in that directory. + /// + /// The default value for this option is `false`. + pub writeback: bool, + + /// The path of the root directory. + /// + /// The default is `/`. + pub root_dir: String, + + /// A prefix to strip from the mount points listed in /proc/self/mountinfo. + /// + /// The default is `None`. + pub mountinfo_prefix: Option, + + /// Whether the file system should support Extended Attributes (xattr). Enabling this feature may + /// have a significant impact on performance, especially on write parallelism. This is the result + /// of FUSE attempting to remove the special file privileges after each write request. + /// + /// The default value for this options is `false`. + pub xattr: bool, + + /// An optional translation layer for host<->guest Extended Attribute (xattr) names. + pub xattrmap: Option, + + /// The xattr name that "security.capability" is remapped to, if the client remapped it at all. + /// If the client's xattrmap did not remap "security.capability", this will be `None`. + pub xattr_security_capability: Option, + + /// Optional `File` object for /proc/self/fd. Callers can open a `File` and pass it here, so + /// there's no need to open it in PassthroughFs::new(). This is specially useful for + /// sandboxing. + /// + /// The default is `None`. + pub proc_sfd_rawfd: Option, + + /// Optional `File` object for /proc/self/mountinfo. Callers can open a `File` and pass it + /// here, so there is no need to open it in PassthroughFs::new(). This is especially useful + /// for sandboxing. + /// + /// The default is `None`. + pub proc_mountinfo_rawfd: Option, + + /// Whether the file system should announce submounts to the guest. Not doing so means that + /// the FUSE client may see st_ino collisions: This stat field is passed through, so if the + /// shared directory encompasses multiple mounts, some inodes (in different file systems) may + /// have the same st_ino value. If the FUSE client does not know these inodes are in different + /// file systems, then it will be oblivious to this collision. + /// By announcing submount points, the FUSE client can create virtual submounts with distinct + /// st_dev values where necessary, so that the combination of st_dev and st_ino will stay + /// unique. + /// On the other hand, it may be undesirable to let the client know the shared directory's + /// submount structure. The user needs to decide which drawback weighs heavier for them, which + /// is why this is a configurable option. + /// + /// The default is `false`. + pub announce_submounts: bool, + + /// Whether to use file handles to reference inodes. We need to be able to open file + /// descriptors for arbitrary inodes, and by default that is done by storing an `O_PATH` FD in + /// `InodeData`. Not least because there is a maximum number of FDs a process can have open + /// users may find it preferable to store a file handle instead, which we can use to open an FD + /// when necessary. + /// So this switch allows to choose between the alternatives: When set to `Never`, `InodeData` + /// will store `O_PATH` FDs. Otherwise, we will attempt to generate and store a file handle + /// instead. With `Prefer`, errors that are inherent to file handles (like no support from the + /// underlying filesystem) lead to falling back to `O_PATH` FDs, and only generic errors (like + /// `ENOENT` or `ENOMEM`) are passed to the guest. `Mandatory` enforces the use of file + /// handles, returning all errors to the guest. + /// + /// The default is `Never`. + pub inode_file_handles: InodeFileHandlesMode, + + /// Whether the file system should support READDIRPLUS (READDIR+LOOKUP) operations. + /// + /// The default is `false`. + pub readdirplus: bool, + + /// Whether the file system should honor the O_DIRECT flag. If this option is disabled (which + /// is the default value), that flag will be filtered out at `open_inode`. + /// + /// The default is `false`. + pub allow_direct_io: bool, + + /// If `killpriv_v2` is true then it indicates that the file system is expected to clear the + /// setuid and setgid bits. + pub killpriv_v2: bool, + + /// Enable support for posix ACLs + /// + /// The default is `false`. + pub posix_acl: bool, + + /// If `security_label` is true, then server will indicate to client + /// to send any security context associated with file during file + /// creation and set that security context on newly created file. + /// This security context is expected to be security.selinux. + /// + /// The default is `false`. + pub security_label: bool, + + /// If `clean_noatime` is true automatically clean up O_NOATIME flag to prevent potential + /// permission errors. + pub clean_noatime: bool, + + /// If `allow_mmap` is true, then server will allow shared mmap'ing of files opened/created + /// with DIRECT_IO. + /// + /// The default is `false`. + pub allow_mmap: bool, + + /// A hit to access share path by read only, so that we could do some optimization on migration. + /// The correct way to implement a readonly passthroughfs should be like "5eab594b4e2a"("Add + /// PassthroughFsRo as read-only variant"), let's optmize this in next rebase from upstream. + /// + /// The default is 'false'. + pub read_only: bool, + + /// Whether to disable host page cache by adding O_DIRECT flag when opening files. + /// When enabled, all file I/O will bypass the host's page cache, which can be useful + /// for ensuring data consistency and reducing memory usage on the host. + /// + /// The default is `false`. + pub force_direct: bool, + + /// Defines what happens when restoring our internal state on the destination fails. + /// + /// The default is `Abort`. + pub migration_on_error: MigrationOnError, + + /// Whether to store a file handle for each inode in the migration stream, alongside the + /// information on how to find the inode. The destination must generate the file handle for + /// the inode it has opened and verify they match. + /// + /// The default is `false`. + pub migration_verify_handles: bool, + + /// Whether to confirm (for path-based migration) at serialization (during switch-over) whether + /// the paths still match the inodes they are supposed to represent, and if they do not, try to + /// correct the path via the respective symlink in /proc/self/fd. + /// + /// The default is `false`. + pub migration_confirm_paths: bool, + + /// Defines how to migrate our internal state to the destination instance. + /// + /// The default is `FindPaths`. + pub migration_mode: MigrationMode, + + /// Whether to use the legacy DFS-based `PathReconstructor` during the preserialization + /// phase of migration instead of the optimized "store-only" reconstructor. + /// + /// When `false` (default), preserialization only iterates the inode store (and reverse-looks + /// up parents via a path -> inode hash table built in a single store pass), avoiding any + /// directory tree traversal. Any parent inodes that are not yet in the store are + /// materialized on demand via `openat(O_PATH | O_NOFOLLOW)` from the nearest known anchor, + /// so the result is semantically identical to the legacy DFS reconstructor. This is much + /// faster when the shared directory is large but only a small subset has been looked up by + /// the guest, and especially when the shared tree is on a slow backend (e.g. NFS). + /// + /// When `true`, fall back to the original full-tree DFS-based `PathReconstructor` (e.g. as + /// an escape hatch if the store-only reconstructor is suspected of misbehaving in a + /// specific setup). + pub migration_dfs_preserialization: bool, +} + +impl Default for Config { + fn default() -> Self { + Config { + entry_timeout: Duration::from_secs(5), + attr_timeout: Duration::from_secs(5), + cache_policy: Default::default(), + writeback: false, + root_dir: String::from("/"), + mountinfo_prefix: None, + xattr: false, + xattrmap: None, + xattr_security_capability: None, + proc_sfd_rawfd: None, + proc_mountinfo_rawfd: None, + announce_submounts: false, + inode_file_handles: Default::default(), + readdirplus: false, + allow_direct_io: false, + killpriv_v2: false, + posix_acl: false, + security_label: false, + clean_noatime: true, + allow_mmap: false, + read_only: false, + force_direct: false, + migration_on_error: MigrationOnError::Abort, + migration_verify_handles: false, + migration_confirm_paths: false, + migration_mode: MigrationMode::FindPaths, + migration_dfs_preserialization: false, + } + } +} + +/// A file system that simply "passes through" all requests it receives to the underlying file +/// system. To keep the implementation simple it servers the contents of its root directory. Users +/// that wish to serve only a specific directory should set up the environment so that that +/// directory ends up as the root of the file system process. One way to accomplish this is via a +/// combination of mount namespaces and the pivot_root system call. +pub struct PassthroughFs { + // File descriptors for various points in the file system tree. These fds are always opened with + // the `O_PATH` option so they cannot be used for reading or writing any data. See the + // documentation of the `O_PATH` flag in `open(2)` for more details on what one can and cannot + // do with an fd opened with this flag. + inodes: InodeStore, + next_inode: AtomicU64, + + // File descriptors for open files and directories. Unlike the fds in `inodes`, these _can_ be + // used for reading and writing data. + handles: RwLock>>, + next_handle: AtomicU64, + + // Maps mount IDs to an open FD on the respective ID for the purpose of open_by_handle_at(). + // This is set when inode_file_handles is not never, since in the 'never' case, + // open_by_handle_at() is not called. + mount_fds: Option, + + // File descriptor pointing to the `/proc/self/fd` directory. This is used to convert an fd from + // `inodes` into one that can go into `handles`. This is accomplished by reading the + // `/proc/self/fd/{}` symlink. We keep an open fd here in case the file system tree that we are + // meant to be serving doesn't have access to `/proc/self/fd`. + proc_self_fd: File, + + // File descriptor pointing to the `/` directory. + root_fd: File, + + // Whether writeback caching is enabled for this directory. This will only be true when + // `cfg.writeback` is true and `init` was called with `FsOptions::WRITEBACK_CACHE`. + writeback: AtomicBool, + + // Whether to announce submounts (i.e., whether the guest supports them and whether they are + // enabled in the configuration) + announce_submounts: AtomicBool, + + // Whether posix ACLs is enabled. + posix_acl: AtomicBool, + + // Basic facts about the OS + os_facts: oslib::OsFacts, + + // Whether the guest kernel supports the supplementary group extension. + sup_group_extension: AtomicBool, + + // Whether we are preparing for migration and need to track changes to inodes like renames. We + // should then also make sure newly created inodes immediately have their migration info set. + track_migration_info: AtomicBool, + + // White list filter entries, used to record accessed filter entries, so that we could + // serialization each subtree of filter entries. + filter: RwLock>, + + // basename → destination absolute path, used only while applying a + // migration blob so filter inodes open at the restore-time allow_dir. + // OnceLock so remap_filter_fullname can return &str borrowed from self. + restore_filter_remap: OnceLock>, + + cfg: Config, +} + +use crate::fuse::ROOT_ID; + +impl PassthroughFs { + pub fn new(mut cfg: Config) -> io::Result { + let proc_self_fd = if let Some(fd) = cfg.proc_sfd_rawfd.take() { + fd + } else { + openat( + &libc::AT_FDCWD, + "/proc/self/fd", + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + )? + }; + + let root_fd = openat( + &libc::AT_FDCWD, + "/", + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + )?; + + let timeout = match cfg.cache_policy { + // We using mode 'Never' in cube product, to reudce + // file pagecache inside guest kernel. + // But we want to keep directory entry cache to speed + // up getdents/getdents64 in guest kernel, to avoid + // request to backend virtiofsd, so here we set timeout + // value as mode 'Always'. + CachePolicy::Never => Duration::from_secs(86400), + CachePolicy::Auto => Duration::from_secs(1), + CachePolicy::Always => Duration::from_secs(86400), + CachePolicy::Metadata => Duration::from_secs(86400), + CachePolicy::None => Duration::from_secs(0), + }; + cfg.entry_timeout = timeout; + cfg.attr_timeout = timeout; + + let mut fs = PassthroughFs { + inodes: Default::default(), + next_inode: AtomicU64::new(fuse::ROOT_ID + 1), + handles: RwLock::new(BTreeMap::new()), + next_handle: AtomicU64::new(0), + mount_fds: None, + proc_self_fd, + root_fd, + writeback: AtomicBool::new(false), + announce_submounts: AtomicBool::new(false), + posix_acl: AtomicBool::new(false), + sup_group_extension: AtomicBool::new(false), + os_facts: oslib::OsFacts::new(), + track_migration_info: AtomicBool::new(false), + filter: RwLock::new(BTreeMap::new()), + restore_filter_remap: OnceLock::new(), + cfg, + }; + + // Check to see if the client remapped "security.capability", if so, + // stash its mapping since the daemon will have to enforce semantics + // that the host kernel otherwise would if the xattrname was not mapped. + let sec_xattr = unsafe { CStr::from_bytes_with_nul_unchecked(b"security.capability\0") }; + fs.cfg.xattr_security_capability = fs + .map_client_xattrname(sec_xattr) + .ok() + .filter(|n| !sec_xattr.eq(n)) + .map(CString::from); + + fs.check_working_file_handles()?; + + // We need to clear the umask here because we want the client to be + // able to set all the bits in the mode. + oslib::umask(0o000); + + Ok(fs) + } + + pub fn keep_fds(&self) -> Vec { + vec![self.proc_self_fd.as_raw_fd()] + } + + fn open_relative_to( + &self, + dir: &impl AsRawFd, + pathname: &CStr, + flags: i32, + mode: Option, + ) -> io::Result { + let flags = libc::O_NOFOLLOW | libc::O_CLOEXEC | flags; + + if self.os_facts.has_openat2 { + oslib::do_open_relative_to(dir, pathname, flags, mode) + } else { + oslib::openat(dir, pathname, flags, mode) + } + } + + fn find_handle(&self, handle: Handle, inode: Inode) -> io::Result> { + self.handles + .read() + .unwrap() + .get(&handle) + .filter(|hd| hd.inode == inode) + .cloned() + .ok_or_else(ebadf) + } + + /// If restore remapped this filter basename to a new host path, return that + /// path; otherwise keep `fullname` from the migration blob. + fn remap_filter_fullname<'a>(&'a self, inode: Inode, fullname: &'a str) -> &'a str { + match self + .restore_filter_remap + .get() + .and_then(|remap| { + std::path::Path::new(fullname) + .file_name() + .and_then(|name| name.to_str()) + .and_then(|name| remap.get(name)) + }) + .map(String::as_str) + { + Some(new_path) if new_path != fullname => { + info!( + "remap filter inode {}: {:?} -> {:?}", + inode, fullname, new_path + ); + new_path + } + _ => fullname, + } + } + + fn open_inode(&self, inode: Inode, mut flags: i32) -> io::Result { + let data = self.inodes.get(inode).ok_or_else(ebadf)?; + + // When writeback caching is enabled, the kernel may send read requests even if the + // userspace program opened the file write-only. So we need to ensure that we have opened + // the file for reading as well as writing. + let writeback = self.writeback.load(Ordering::Relaxed); + if writeback && flags & libc::O_ACCMODE == libc::O_WRONLY { + flags &= !libc::O_ACCMODE; + flags |= libc::O_RDWR; + } + + // When writeback caching is enabled the kernel is responsible for handling `O_APPEND`. + // However, this breaks atomicity as the file may have changed on disk, invalidating the + // cached copy of the data in the kernel and the offset that the kernel thinks is the end of + // the file. Just allow this for now as it is the user's responsibility to enable writeback + // caching only for directories that are not shared. It also means that we need to clear the + // `O_APPEND` flag. + if writeback && flags & libc::O_APPEND != 0 { + flags &= !libc::O_APPEND; + } + + if !self.cfg.allow_direct_io && flags & libc::O_DIRECT != 0 { + flags &= !libc::O_DIRECT; + } + + // Add O_DIRECT flag to disable host page cache if configured + if self.cfg.force_direct { + flags |= libc::O_DIRECT; + } + + data.open_file(flags | libc::O_CLOEXEC, &self.proc_self_fd)? + .into_file() + } + + /// Generate a file handle for `fd` using `FileHandle::from_fd()`. `st` is `fd`'s stat + /// information (we may need the mount ID for errors/warnings). + /// + /// These are the possible return values: + /// - `Ok(Some(_))`: Success, caller should use this file handle. + /// - `Ok(None)`: No error, but no file handle is available. The caller should fall back to + /// using an `O_PATH` FD. + /// - `Err(_)`: An error occurred, the caller should return this to the guest. + /// + /// This function takes the chosen `self.cfg.inode_file_handles` mode into account: + /// - `Never`: Always return `Ok(None)`. + /// - `Prefer`: Return `Ok(None)` when file handles are not supported by this filesystem. + /// Otherwise, return either `Ok(Some(_))` or `Err(_)`, depending on whether a file + /// handle could be generated or not. + /// - `Mandatory`: Never return `Ok(None)`. When the filesystem does not support file handles, + /// return an `Err(_)`. + /// + /// When the filesystem does not support file handles, this is logged (as a warning in + /// `Prefer` mode, and as an error in `Mandatory` mode) one time per filesystem. + fn get_file_handle_opt( + &self, + fd: &impl AsRawFd, + st: &StatExt, + ) -> io::Result> { + let handle = match self.cfg.inode_file_handles { + InodeFileHandlesMode::Never => { + // Let's make this quick, so we can skip this case below + return Ok(None); + } + + InodeFileHandlesMode::Prefer | InodeFileHandlesMode::Mandatory => { + FileHandle::from_fd(fd)? + } + }; + + if handle.is_none() { + // No error, but no handle (because of EOPNOTSUPP/EOVERFLOW)? Log it. + let io_err = io::Error::from_raw_os_error(libc::EOPNOTSUPP); + + let desc = match self.cfg.inode_file_handles { + InodeFileHandlesMode::Never => unreachable!(), + InodeFileHandlesMode::Prefer => { + "Filesystem does not support file handles, falling back to O_PATH FDs" + } + InodeFileHandlesMode::Mandatory => "Filesystem does not support file handles", + }; + + // Use the MPRError object, because (with a mount ID obtained through statx()) + // `self.mount_fds.error_for()` will attempt to add a prefix to the error description + // that describes the offending filesystem by mount point and mount ID, and will also + // suppress the message if we have already logged any error concerning file handles for + // the respective filesystem (so we only log errors/warnings once). + let err: MPRError = if st.mnt_id > 0 { + // Valid mount ID + // self.mount_fds won't be None if we enter here. + self.mount_fds + .as_ref() + .unwrap() + .error_for(st.mnt_id, io_err) + } else { + // No valid mount ID, return error object not bound to a filesystem + io_err.into() + } + .set_desc(desc.to_string()); + + // In `Prefer` mode, warn; in `Mandatory` mode, log and return an error. + // (Suppress logging if the error is silenced, which means that we have already logged + // a warning/error for this filesystem.) + match self.cfg.inode_file_handles { + InodeFileHandlesMode::Never => unreachable!(), + InodeFileHandlesMode::Prefer => { + if !err.silent() { + warn!("{}", err); + } + } + InodeFileHandlesMode::Mandatory => { + if !err.silent() { + error!("{}", err); + } + return Err(err.into_inner()); + } + } + } + + Ok(handle) + } + + fn make_file_handle_openable(&self, fh: &FileHandle) -> io::Result { + // self.mount_fds won't be None if we enter here. + fh.to_openable(self.mount_fds.as_ref().unwrap(), |fd, flags| { + reopen_fd_through_proc(&fd, flags, &self.proc_self_fd) + }) + .map_err(|e| { + if !e.silent() { + error!("{}", e); + } + e.into_inner() + }) + } + + fn check_working_file_handles(&mut self) -> io::Result<()> { + if self.cfg.inode_file_handles == InodeFileHandlesMode::Never { + // No need to check anything + return Ok(()); + } + + // Try to open the root directory, turn it into a file handle, then try to open that file + // handle to see whether file handles do indeed work + // (Note that we pass through all I/O errors to the caller, because `PassthroughFs::init()` + // will do these calls (`openat()`, `stat()`, etc.) anyway, so if they do not work now, + // they probably are not going to work later either. Better to report errors early then.) + let root_dir = openat( + &libc::AT_FDCWD, + self.cfg.root_dir.as_str(), + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + )?; + + let st = statx(&root_dir, None)?; + if let Some(h) = self.get_file_handle_opt(&root_dir, &st)? { + // Got an openable file handle, try opening it + match self.make_file_handle_openable(&h)?.open(libc::O_PATH) { + Ok(_) => (), + Err(e) => match self.cfg.inode_file_handles { + InodeFileHandlesMode::Never => unreachable!(), + InodeFileHandlesMode::Prefer => { + warn!("Failed to open file handle for the root node: {}", e); + warn!("File handles do not appear safe to use, disabling file handles altogether"); + self.cfg.inode_file_handles = InodeFileHandlesMode::Never; + } + InodeFileHandlesMode::Mandatory => { + error!("Failed to open file handle for the root node: {}", e); + error!("Refusing to use (mandatory) file handles, as they do not appear safe to use"); + return Err(e); + } + }, + } + } else { + // Did not get an openable file handle (nor an error), so we cannot be in `mandatory` + // mode. We also cannot be in `never` mode, because that is sorted out at the very + // beginning of this function. Still, use `match` so the compiler could warn us if we + // were to forget some (future?) variant. + match self.cfg.inode_file_handles { + InodeFileHandlesMode::Never => unreachable!(), + InodeFileHandlesMode::Prefer => { + warn!("Failed to generate a file handle for the root node, disabling file handles altogether"); + self.cfg.inode_file_handles = InodeFileHandlesMode::Never; + } + InodeFileHandlesMode::Mandatory => unreachable!(), + } + } + + Ok(()) + } + + /// Try to look up an inode by its `name` relative to the given `parent` inode. If the inode + /// is registered in our inode store (`self.inodes`), return a strong reference to it. + /// Otherwise, return `None` instead. + /// Along with the inode (or `None`), return information gathered along the way: An `O_PATH` + /// file to the inode, stat information, and optionally a file handle if virtiofsd has been + /// configured to use file handles. + /// Return an error if the parent node cannot be opened, the given inode cannot be found on the + /// filesystem, or generating the stat information or file handle fails. + fn try_lookup_implementation( + &self, + parent_data: &InodeData, + name: &CStr, + ) -> io::Result<( + Option, + File, + StatExt, + Option, + )> { + let p_file = parent_data.get_file()?; + + let path_fd = { + let fd = self.open_relative_to(&p_file, name, libc::O_PATH, None)?; + // Safe because we just opened this fd. + unsafe { File::from_raw_fd(fd) } + }; + + let st = statx(&path_fd, None)?; + + // Note that this will always be `None` if `cfg.inode_file_handles` is `Never`, but we only + // really need the handle when we do not have an `O_PATH` fd open for every inode. So if + // `cfg.inode_file_handles` is `Never`, we do not need it anyway. + let handle = self.get_file_handle_opt(&path_fd, &st)?; + + let ids = InodeIds { + ino: st.st.st_ino, + dev: st.st.st_dev, + mnt_id: st.mnt_id, + }; + + Ok(( + self.inodes.claim_inode(handle.as_ref(), &ids).ok(), + path_fd, + st, + handle, + )) + } + + /// Try to look up an inode by its `name` relative to the parent inode given by its + /// `parent_data`. If the inode is registered in our inode store (`self.inodes`), return a + /// strong reference to it. Otherwise, return `None`. + /// Return an error if the parent node cannot be opened, the given inode cannot be found on the + /// filesystem, or generating the Stat information or file handle fails. + fn try_lookup( + &self, + parent_data: &InodeData, + name: &CStr, + ) -> io::Result> { + self.try_lookup_implementation(parent_data, name) + .map(|result| result.0) + } + + fn do_lookup( + &self, + parent: Inode, + name: &CStr, + f_info: Option<(String, StatExt)>, + ) -> io::Result { + let st; + let path_fd; + let p = self.inodes.get(parent).ok_or_else(ebadf)?; + let mut fullname = String::new(); + let mut is_filter = false; + + debug!( + "lookup parent {:?} f_info {:?} name {:?}", + parent, + f_info.is_some(), + name + ); + let (existing_inode, handle) = if parent == ROOT_ID && f_info.is_some() { + if let Some((_dir, _st)) = f_info { + st = _st; + fullname = _dir.clone(); + path_fd = openat( + &libc::AT_FDCWD, + &_dir, + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + )?; + } else { + warn!("Lookup {:?} failed, f_info is None", name); + return Err(io::Error::from_raw_os_error(libc::EINVAL)); + }; + + let ids = InodeIds { + ino: st.st.st_ino, + dev: st.st.st_dev, + mnt_id: st.mnt_id, + }; + + let inode = self.inodes.claim_inode(None, &ids).ok(); + is_filter = true; + + (inode, None) + } else { + let (inode, fd, _st, handle) = self.try_lookup_implementation(&p, name)?; + + st = _st; + path_fd = fd; + + (inode, handle) + }; + + let mut attr_flags: u32 = 0; + + if st.st.st_mode & libc::S_IFMT == libc::S_IFDIR + && self.announce_submounts.load(Ordering::Relaxed) + && (st.st.st_dev != p.ids.dev || st.mnt_id != p.ids.mnt_id) + { + attr_flags |= fuse::ATTR_SUBMOUNT; + } + + let inode = if let Some(inode) = existing_inode { + inode + } else { + let file_or_handle = if let Some(h) = handle.as_ref() { + FileOrHandle::Handle(self.make_file_handle_openable(h)?) + } else { + FileOrHandle::File(path_fd) + }; + + let mig_info = if self.track_migration_info.load(Ordering::Relaxed) { + let parent_strong_ref = StrongInodeReference::new_with_data(p, &self.inodes)?; + Some(InodeMigrationInfo::new( + &self.cfg, + parent_strong_ref, + name, + fullname.clone(), + &file_or_handle, + )?) + } else { + None + }; + + let inode_data = InodeData { + inode: self.next_inode.fetch_add(1, Ordering::Relaxed), + file_or_handle, + refcount: AtomicU64::new(1), + ids: InodeIds { + ino: st.st.st_ino, + dev: st.st.st_dev, + mnt_id: st.mnt_id, + }, + mode: st.st.st_mode, + migration_info: Mutex::new(mig_info), + }; + + debug!( + "lookup inode {:?} name {:?} fullname {:?} is_filter {:?}", + inode_data.inode, name, fullname, is_filter, + ); + + if is_filter { + self.filter.write().unwrap().insert( + inode_data.inode, + (name.to_str().unwrap().to_string(), fullname), + ); + } + + self.inodes.get_or_insert(inode_data)? + }; + + Ok(Entry { + // By leaking, we transfer ownership of this refcount to the guest. That is safe, + // because the guest is expected to explicitly release its reference and decrement the + // refcount via `FORGET` later. + inode: unsafe { inode.leak() }, + generation: 0, + attr: st.st, + attr_flags, + attr_timeout: self.cfg.attr_timeout, + entry_timeout: self.cfg.entry_timeout, + }) + } + + fn do_open( + &self, + inode: Inode, + kill_priv: bool, + flags: u32, + ) -> io::Result<(Option, OpenOptions)> { + // We need to clean the `O_APPEND` flag in case the file is mem mapped or if the flag + // is later modified in the guest using `fcntl(F_SETFL)`. We do a per-write `O_APPEND` + // check setting `RWF_APPEND` for non-mmapped writes, if necessary. + let mut flags = flags & !(libc::O_APPEND as u32); + + // Clean O_NOATIME (unless specified otherwise with --preserve-noatime) to prevent + // potential permission errors when running in unprivileged mode. + if self.cfg.clean_noatime { + flags &= !(libc::O_NOATIME as u32) + } + + let file = { + let _killpriv_guard = if self.cfg.killpriv_v2 && kill_priv { + drop_effective_cap("FSETID")? + } else { + None + }; + self.open_inode(inode, flags as i32)? + }; + + if flags & (libc::O_TRUNC as u32) != 0 { + self.clear_file_capabilities(file.as_raw_fd(), false)?; + } + + let handle = self.next_handle.fetch_add(1, Ordering::Relaxed); + let data = HandleData { + inode, + file: file.into(), + migration_info: HandleMigrationInfo::new(flags as i32), + }; + + self.handles.write().unwrap().insert(handle, Arc::new(data)); + + let mut opts = OpenOptions::empty(); + match self.cfg.cache_policy { + // We only set the direct I/O option on files. + CachePolicy::Never => { + opts.set( + OpenOptions::DIRECT_IO, + flags & (libc::O_DIRECTORY as u32) == 0, + ); + // Force enable directory cache, we could speed up + // getdents/getdents64 in guest without request to + // backend everytime. Because we are using virtiofs + // for readonly access in guest. + if flags & (libc::O_DIRECTORY as u32) != 0 { + opts |= OpenOptions::KEEP_CACHE; + // Disable cache for root directory. We are going + // to support inject docker/POD into VM, which will + // add new root entry in backend root directory. + if inode != 1 { + opts |= OpenOptions::CACHE_DIR; + } + } + } + CachePolicy::Metadata => { + if flags & (libc::O_DIRECTORY as u32) == 0 { + opts |= OpenOptions::DIRECT_IO; + } else { + opts |= OpenOptions::CACHE_DIR | OpenOptions::KEEP_CACHE; + } + } + CachePolicy::Always => { + opts |= OpenOptions::KEEP_CACHE; + if flags & (libc::O_DIRECTORY as u32) != 0 { + opts |= OpenOptions::CACHE_DIR; + } + } + CachePolicy::None => { + // We only set the direct I/O option on files. + opts.set( + OpenOptions::DIRECT_IO, + flags & (libc::O_DIRECTORY as u32) == 0, + ); + } + _ => {} + }; + + Ok((Some(handle), opts)) + } + + fn do_release(&self, inode: Inode, handle: Handle) -> io::Result<()> { + let mut handles = self.handles.write().unwrap(); + + if let btree_map::Entry::Occupied(e) = handles.entry(handle) { + if e.get().inode == inode { + // We don't need to close the file here because that will happen automatically when + // the last `Arc` is dropped. + e.remove(); + return Ok(()); + } + } + + Err(ebadf()) + } + + fn do_getattr(&self, inode: Inode) -> io::Result<(libc::stat64, Duration)> { + let data = self.inodes.get(inode).ok_or_else(ebadf)?; + let inode_file = data.get_file()?; + let st = statx(&inode_file, None)?.st; + + Ok((st, self.cfg.attr_timeout)) + } + + fn do_unlink(&self, parent: Inode, name: &CStr, flags: libc::c_int) -> io::Result<()> { + let data = self.inodes.get(parent).ok_or_else(ebadf)?; + let parent_file = data.get_file()?; + + // Safe because this doesn't modify any memory and we check the return value. + let res = unsafe { libc::unlinkat(parent_file.as_raw_fd(), name.as_ptr(), flags) }; + if res == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + } + } + + fn block_xattr(&self, name: &[u8]) -> bool { + // Currently we only filter out posix acl xattrs. + // If acls are enabled, there is nothing to filter. + if self.posix_acl.load(Ordering::Relaxed) { + return false; + } + + let acl_access = "system.posix_acl_access".as_bytes(); + let acl_default = "system.posix_acl_default".as_bytes(); + acl_access.starts_with(name) || acl_default.starts_with(name) + } + + fn map_client_xattrname<'a>(&self, name: &'a CStr) -> std::io::Result> { + if self.block_xattr(name.to_bytes()) { + return Err(io::Error::from_raw_os_error(libc::ENOTSUP)); + } + + match &self.cfg.xattrmap { + Some(map) => match map.map_client_xattr(name).expect("unterminated mapping") { + AppliedRule::Deny => Err(io::Error::from_raw_os_error(libc::EPERM)), + AppliedRule::Unsupported => Err(io::Error::from_raw_os_error(libc::ENOTSUP)), + AppliedRule::Pass(new_name) => Ok(new_name), + }, + None => Ok(Cow::Borrowed(name)), + } + } + + fn map_server_xattrlist(&self, xattr_names: Vec) -> Vec { + let all_xattrs = match &self.cfg.xattrmap { + Some(map) => map + .map_server_xattrlist(xattr_names) + .expect("unterminated mapping"), + None => xattr_names, + }; + + // filter out the blocked xattrs + let mut filtered = Vec::with_capacity(all_xattrs.len()); + let all_xattrs = all_xattrs.split(|b| *b == 0).filter(|bs| !bs.is_empty()); + + for xattr in all_xattrs { + if !self.block_xattr(xattr) { + filtered.extend_from_slice(xattr); + filtered.push(0); + } + } + + filtered.shrink_to_fit(); + + filtered + } + + /// Clears file capabilities + /// + /// * `fd` - A file descriptor + /// * `o_path` - Must be `true` if the file referred to by `fd` was opened with the `O_PATH` flag + /// + /// If it is not clear whether `fd` was opened with `O_PATH` it is safe to set `o_path` + /// to `true`. + fn clear_file_capabilities(&self, fd: RawFd, o_path: bool) -> io::Result<()> { + match self.cfg.xattr_security_capability.as_ref() { + // Unmapped, let the kernel take care of this. + None => Ok(()), + // Otherwise we have to uphold the same semantics the kernel + // would; which is to drop the "security.capability" xattr + // on write + Some(xattrname) => { + let res = if o_path { + let proc_file_name = CString::new(format!("{fd}")) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + let _working_dir_guard = set_working_directory( + self.proc_self_fd.as_raw_fd(), + self.root_fd.as_raw_fd(), + ); + unsafe { libc::removexattr(proc_file_name.as_ptr(), xattrname.as_ptr()) } + } else { + unsafe { libc::fremovexattr(fd, xattrname.as_ptr()) } + }; + + if res == 0 { + Ok(()) + } else { + let eno = io::Error::last_os_error(); + match eno.raw_os_error().unwrap() { + libc::ENODATA | libc::ENOTSUP => Ok(()), + _ => Err(eno), + } + } + } + } + } + + #[allow(clippy::too_many_arguments)] + fn do_create( + &self, + ctx: &Context, + parent_file: &InodeFile, + name: &CStr, + mode: u32, + flags: u32, + umask: u32, + extensions: Extensions, + ) -> io::Result { + let fd = { + let _credentials_guard = UnixCredentials::new(ctx.uid, ctx.gid) + .supplementary_gid( + self.sup_group_extension.load(Ordering::Relaxed), + extensions.sup_gid, + ) + .set()?; + let _umask_guard = self + .posix_acl + .load(Ordering::Relaxed) + .then(|| oslib::ScopedUmask::new(umask)); + + // Add libc:O_EXCL to ensure we're not accidentally opening a file the guest wouldn't + // be allowed to access otherwise. + self.open_relative_to( + parent_file, + name, + flags as i32 | libc::O_CREAT | libc::O_EXCL, + mode.into(), + )? + }; + + // Set security context + if let Some(secctx) = extensions.secctx { + // Remap security xattr name. + let xattr_name = match self.map_client_xattrname(&secctx.name) { + Ok(xattr_name) => xattr_name, + Err(e) => { + unsafe { + libc::unlinkat(parent_file.as_raw_fd(), name.as_ptr(), 0); + } + return Err(e); + } + }; + + let ret = unsafe { + libc::fsetxattr( + fd, + xattr_name.as_ptr(), + secctx.secctx.as_ptr() as *const libc::c_void, + secctx.secctx.len(), + 0, + ) + }; + + if ret != 0 { + unsafe { + libc::unlinkat(parent_file.as_raw_fd(), name.as_ptr(), 0); + } + return Err(io::Error::last_os_error()); + } + } + Ok(fd) + } + + fn do_mknod_mkdir_symlink_secctx( + &self, + parent_file: &InodeFile, + name: &CStr, + secctx: &SecContext, + ) -> io::Result<()> { + // Remap security xattr name. + let xattr_name = self.map_client_xattrname(&secctx.name)?; + + // Set security context on newly created node. It could be + // device node as well, so it is not safe to open the node + // and call fsetxattr(). Instead, use the fchdir(proc_fd) + // and call setxattr(o_path_fd). We use this trick while + // setting xattr as well. + + // Open O_PATH fd for dir/symlink/special node just created. + let path_fd = self.open_relative_to(parent_file, name, libc::O_PATH, None)?; + + let procname = CString::new(format!("{path_fd}")) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)); + + let procname = match procname { + Ok(name) => name, + Err(error) => { + return Err(error); + } + }; + + let _working_dir_guard = + set_working_directory(self.proc_self_fd.as_raw_fd(), self.root_fd.as_raw_fd()); + + let res = unsafe { + libc::setxattr( + procname.as_ptr(), + xattr_name.as_ptr(), + secctx.secctx.as_ptr() as *const libc::c_void, + secctx.secctx.len(), + 0, + ) + }; + + let res_err = io::Error::last_os_error(); + + if res == 0 { + Ok(()) + } else { + Err(res_err) + } + } + + pub fn open_root_node(&self) -> io::Result<()> { + // We use `O_PATH` because we just want this for traversing the directory tree + // and not for actually reading the contents. We don't use `open_relative_to()` + // here because we are not opening a guest-provided pathname. Also, `self.cfg.root_dir` + // is an absolute pathname, thus not relative to CWD, so we will not be able to open it + // if "/" didn't change (e.g., chroot or pivot_root) + let path_fd = openat( + &libc::AT_FDCWD, + self.cfg.root_dir.as_str(), + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + )?; + + let st = statx(&path_fd, None)?; + let handle = self.get_file_handle_opt(&path_fd, &st)?; + + let file_or_handle = if let Some(h) = handle.as_ref() { + FileOrHandle::Handle(self.make_file_handle_openable(h)?) + } else { + FileOrHandle::File(path_fd) + }; + + // Not sure why the root inode gets a refcount of 2 but that's what libfuse does. + let inode = InodeData { + inode: fuse::ROOT_ID, + file_or_handle, + refcount: AtomicU64::new(2), + ids: InodeIds { + ino: st.st.st_ino, + dev: st.st.st_dev, + mnt_id: st.mnt_id, + }, + mode: st.st.st_mode, + migration_info: Mutex::new(None), + }; + self.inodes.new_inode(inode)?; + Ok(()) + } + + /// After renaming an inode while preparing for migration, update its migration info if + /// necessary. For example, when representing inodes through their filename and parent + /// directory node, these must be updated to match the new name and location. + /// `parent` and `filename` are the inode's new location. + fn update_inode_migration_info( + &self, + parent_data: Arc, + filename: &CStr, + ) -> io::Result<()> { + // We only need to update the node's migration info if we have it in our store + if let Some(inode) = self.try_lookup(&parent_data, filename)? { + let inode_data = inode.get(); + let parent_strong_ref = StrongInodeReference::new_with_data(parent_data, &self.inodes)?; + let mut info_locked = inode_data.migration_info.lock().unwrap(); + // Unconditionally clear any potentially existing path, because it will be outdated + info_locked.take(); + *info_locked = Some(InodeMigrationInfo::new( + &self.cfg, + parent_strong_ref, + filename, + String::new(), + &inode_data.file_or_handle, + )?); + } + + Ok(()) + } +} + +impl FileSystem for PassthroughFs { + type Inode = Inode; + type Handle = Handle; + type DirIter = ReadDir>; + + fn get_root_ino(&self) -> io::Result { + let path_fd = openat( + &libc::AT_FDCWD, + self.cfg.root_dir.as_str(), + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + )?; + + let st = statx(&path_fd, None)?; + + Ok(st.st.st_ino) + } + + fn readdirplus_enabled(&self) -> bool { + self.cfg.readdirplus + } + + fn init(&self, capable: FsOptions) -> io::Result { + self.open_root_node()?; + + // Note: On migration, all options negotiated here with the guest must be sent to the + // destination in the `device_state::serialized::NegotiatedOpts` structure. So when adding + // a new option here, don't forget to add it there, too, and handle it both in + // `>::from()` and + // `serialized::NegotiatedOpts::apply()`. + + let mut opts = if self.cfg.readdirplus { + FsOptions::DO_READDIRPLUS | FsOptions::READDIRPLUS_AUTO + } else { + FsOptions::empty() + }; + if self.cfg.writeback && capable.contains(FsOptions::WRITEBACK_CACHE) { + opts |= FsOptions::WRITEBACK_CACHE; + self.writeback.store(true, Ordering::Relaxed); + } + if self.cfg.announce_submounts { + if capable.contains(FsOptions::SUBMOUNTS) { + self.announce_submounts.store(true, Ordering::Relaxed); + } else { + eprintln!("Warning: Cannot announce submounts, client does not support it"); + } + } + if self.cfg.killpriv_v2 { + if capable.contains(FsOptions::HANDLE_KILLPRIV_V2) { + opts |= FsOptions::HANDLE_KILLPRIV_V2; + } else { + warn!("Cannot enable KILLPRIV_V2, client does not support it"); + } + } + if self.cfg.posix_acl { + let acl_required_flags = + FsOptions::POSIX_ACL | FsOptions::DONT_MASK | FsOptions::SETXATTR_EXT; + if capable.contains(acl_required_flags) { + opts |= acl_required_flags; + self.posix_acl.store(true, Ordering::Relaxed); + debug!("init: enabling posix acl"); + } else { + error!("Cannot enable posix ACLs, client does not support it"); + return Err(io::Error::from_raw_os_error(libc::EPROTO)); + } + } + + if self.cfg.security_label { + if capable.contains(FsOptions::SECURITY_CTX) { + opts |= FsOptions::SECURITY_CTX; + } else { + error!("Cannot enable security label. kernel does not support FUSE_SECURITY_CTX capability"); + return Err(io::Error::from_raw_os_error(libc::EPROTO)); + } + } + + if self.cfg.allow_mmap { + opts |= FsOptions::DIRECT_IO_ALLOW_MMAP; + } + + if capable.contains(FsOptions::CREATE_SUPP_GROUP) { + self.sup_group_extension.store(true, Ordering::Relaxed); + } + + // Force enable DIRECT_IO_ALLOW_MMAP when cache is disabled. + match self.cfg.cache_policy { + CachePolicy::Never => opts |= FsOptions::DIRECT_IO_ALLOW_MMAP, + CachePolicy::None => opts |= FsOptions::DIRECT_IO_ALLOW_MMAP, + _ => {} + }; + + Ok(opts) + } + + fn destroy(&self) { + self.handles.write().unwrap().clear(); + self.inodes.clear(); + self.writeback.store(false, Ordering::Relaxed); + self.announce_submounts.store(false, Ordering::Relaxed); + self.posix_acl.store(false, Ordering::Relaxed); + self.sup_group_extension.store(false, Ordering::Relaxed); + } + + fn statfs(&self, _ctx: Context, inode: Inode) -> io::Result { + let data = self.inodes.get(inode).ok_or_else(ebadf)?; + let inode_file = data.get_file()?; + let mut out = MaybeUninit::::zeroed(); + + // Safe because this will only modify `out` and we check the return value. + let res = unsafe { libc::fstatvfs64(inode_file.as_raw_fd(), out.as_mut_ptr()) }; + if res == 0 { + // Safe because the kernel guarantees that `out` has been initialized. + Ok(unsafe { out.assume_init() }) + } else { + Err(io::Error::last_os_error()) + } + } + + fn lookup( + &self, + _ctx: Context, + parent: Inode, + name: &CStr, + f_info: Option<(String, StatExt)>, + ) -> io::Result { + validate_dentry_name(name)?; + self.do_lookup(parent, name, f_info) + } + + fn forget(&self, _ctx: Context, inode: Inode, count: u64) { + // Pin filter inodes: guest FORGETs must never evict a filter (whitelist) + // entry from `self.inodes`, otherwise `self.filter` and `inode_store` can + // desynchronize. A stale filter id left in `self.filter` will poison the + // preserialization `path_to_inode` seed (see `build_filter_resolved_map`) + // and cause children of that filter to be marked Invalid during migration. + if self.filter.read().unwrap().contains_key(&inode) { + debug!( + "forget: skipping filter-pinned inode {} (count={})", + inode, count + ); + return; + } + self.inodes.forget_one(inode, count) + } + + fn batch_forget(&self, _ctx: Context, requests: Vec<(Inode, u64)>) { + // Same rationale as `forget`: strip filter-pinned inodes from the batch + // so they are never evicted from `self.inodes`. + let filter = self.filter.read().unwrap(); + let filtered: Vec<(Inode, u64)> = requests + .into_iter() + .filter(|(inode, count)| { + if filter.contains_key(inode) { + debug!( + "batch_forget: skipping filter-pinned inode {} (count={})", + inode, count + ); + false + } else { + true + } + }) + .collect(); + drop(filter); + self.inodes.forget_many(filtered) + } + + fn opendir( + &self, + _ctx: Context, + inode: Inode, + flags: u32, + ) -> io::Result<(Option, OpenOptions)> { + self.do_open(inode, false, flags | (libc::O_DIRECTORY as u32)) + } + + fn releasedir( + &self, + _ctx: Context, + inode: Inode, + _flags: u32, + handle: Handle, + ) -> io::Result<()> { + self.do_release(inode, handle) + } + + fn mkdir( + &self, + ctx: Context, + parent: Inode, + name: &CStr, + mode: u32, + umask: u32, + extensions: Extensions, + ) -> io::Result { + validate_dentry_name(name)?; + let data = self.inodes.get(parent).ok_or_else(ebadf)?; + let parent_file = data.get_file()?; + + let res = { + let _credentials_guard = UnixCredentials::new(ctx.uid, ctx.gid) + .supplementary_gid( + self.sup_group_extension.load(Ordering::Relaxed), + extensions.sup_gid, + ) + .set()?; + let _umask_guard = self + .posix_acl + .load(Ordering::Relaxed) + .then(|| oslib::ScopedUmask::new(umask)); + + // Safe because this doesn't modify any memory and we check the return value. + unsafe { libc::mkdirat(parent_file.as_raw_fd(), name.as_ptr(), mode) } + }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + + // Set security context on dir. + if let Some(secctx) = extensions.secctx { + if let Err(e) = self.do_mknod_mkdir_symlink_secctx(&parent_file, name, &secctx) { + unsafe { + libc::unlinkat(parent_file.as_raw_fd(), name.as_ptr(), libc::AT_REMOVEDIR); + }; + return Err(e); + } + } + + self.do_lookup(parent, name, None) + } + + fn rmdir(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<()> { + validate_dentry_name(name)?; + self.do_unlink(parent, name, libc::AT_REMOVEDIR) + } + + fn readdir( + &self, + _ctx: Context, + inode: Inode, + handle: Handle, + size: u32, + offset: u64, + ) -> io::Result { + if size == 0 { + return Ok(ReadDir::default()); + } + let data = self.find_handle(handle, inode)?; + + let buf = vec![0; size as usize]; + + // Since we are going to work with the kernel offset, we have to acquire the file + // lock for both the `lseek64` and `getdents64` syscalls to ensure that no other + // thread changes the kernel offset while we are using it. + #[allow(clippy::readonly_write_lock)] + let dir = data.file.get()?.write().unwrap(); + + ReadDir::new(&*dir, offset as libc::off64_t, buf) + } + + fn open( + &self, + _ctx: Context, + inode: Inode, + kill_priv: bool, + flags: u32, + ) -> io::Result<(Option, OpenOptions)> { + self.do_open(inode, kill_priv, flags) + } + + fn release( + &self, + _ctx: Context, + inode: Inode, + _flags: u32, + handle: Handle, + _flush: bool, + _flock_release: bool, + _lock_owner: Option, + ) -> io::Result<()> { + self.do_release(inode, handle) + } + + fn create( + &self, + ctx: Context, + parent: Inode, + name: &CStr, + mode: u32, + kill_priv: bool, + flags: u32, + umask: u32, + extensions: Extensions, + ) -> io::Result<(Entry, Option, OpenOptions)> { + validate_dentry_name(name)?; + let data = self.inodes.get(parent).ok_or_else(ebadf)?; + let parent_file = data.get_file()?; + + // We need to clean the `O_APPEND` flag in case the file is mem mapped or if the flag + // is later modified in the guest using `fcntl(F_SETFL)`. We do a per-write `O_APPEND` + // check setting `RWF_APPEND` for non-mmapped writes, if necessary. + let create_flags = flags & !(libc::O_APPEND as u32); + let fd = self.do_create( + &ctx, + &parent_file, + name, + mode, + create_flags, + umask, + extensions, + ); + + let (entry, handle) = match fd { + Err(last_error) => { + // Ignore the error if the file exists and O_EXCL is not present in `flags` + match last_error.kind() { + io::ErrorKind::AlreadyExists => { + if (flags as i32 & libc::O_EXCL) != 0 { + return Err(last_error); + } + } + _ => return Err(last_error), + } + + let entry = self.do_lookup(parent, name, None)?; + let (handle, _) = self.do_open(entry.inode, kill_priv, flags)?; + let handle = handle.ok_or_else(ebadf)?; + + (entry, handle) + } + Ok(fd) => { + // Safe because we just opened this fd. + let file = unsafe { File::from_raw_fd(fd) }; + + let entry = self.do_lookup(parent, name, None)?; + + let handle = self.next_handle.fetch_add(1, Ordering::Relaxed); + let data = HandleData { + inode: entry.inode, + file: file.into(), + migration_info: HandleMigrationInfo::new(flags as i32), + }; + + self.handles.write().unwrap().insert(handle, Arc::new(data)); + + (entry, handle) + } + }; + + let mut opts = OpenOptions::empty(); + match self.cfg.cache_policy { + CachePolicy::Never => opts |= OpenOptions::DIRECT_IO, + CachePolicy::Metadata => opts |= OpenOptions::DIRECT_IO, + CachePolicy::Always => opts |= OpenOptions::KEEP_CACHE, + CachePolicy::None => opts |= OpenOptions::DIRECT_IO, + _ => {} + }; + + Ok((entry, Some(handle), opts)) + } + + fn unlink(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<()> { + validate_dentry_name(name)?; + self.do_unlink(parent, name, 0) + } + + fn setupmapping( + &self, + _ctx: Context, + inode: Inode, + _handle: Handle, + foffset: u64, + len: u64, + flags: u64, + moffset: u64, + vu_req: &mut T, + ) -> io::Result<()> { + debug!( + "setupmapping: ino {:?} foffset {} len {} flags {} moffset {}", + inode, foffset, len, flags, moffset + ); + + let open_flags = if (flags & fuse::SetupmappingFlags::WRITE.bits()) != 0 { + libc::O_RDWR + } else { + libc::O_RDONLY + }; + + let file = self.open_inode(inode, open_flags)?; + (*vu_req).map(foffset, moffset, len, flags, file.as_raw_fd()) + } + + fn removemapping( + &self, + _ctx: Context, + requests: Vec, + vu_req: &mut T, + ) -> io::Result<()> { + (*vu_req).unmap(requests) + } + + fn read( + &self, + _ctx: Context, + inode: Inode, + handle: Handle, + mut w: W, + size: u32, + offset: u64, + _lock_owner: Option, + _flags: u32, + ) -> io::Result { + let data = self.find_handle(handle, inode)?; + + // This is safe because write_from uses preadv64, so the underlying file descriptor + // offset is not affected by this operation. + let f = data.file.get()?.read().unwrap(); + w.write_from(&f, size as usize, offset) + } + + fn write( + &self, + _ctx: Context, + inode: Inode, + handle: Handle, + mut r: R, + size: u32, + offset: u64, + _lock_owner: Option, + delayed_write: bool, + kill_priv: bool, + flags: u32, + ) -> io::Result { + let data = self.find_handle(handle, inode)?; + + // This is safe because read_to uses `pwritev2(2)`, so the underlying file descriptor + // offset is not affected by this operation. + let f = data.file.get()?.read().unwrap(); + + { + let _killpriv_guard = if self.cfg.killpriv_v2 && kill_priv { + // We need to drop FSETID during a write so that the kernel will remove setuid + // or setgid bits from the file if it was written to by someone other than the + // owner. + drop_effective_cap("FSETID")? + } else { + None + }; + + self.clear_file_capabilities(f.as_raw_fd(), false)?; + + // We don't set the `RWF_APPEND` (i.e., equivalent to `O_APPEND`) flag, if it's a + // delayed write (i.e., using writeback mode or a mem mapped file) even if the file + // was open in append mode, since the guest kernel sends the correct offset. + // For non-delayed writes, we set the append mode, if necessary, to correctly handle + // writes on a file shared among VMs. This case can only be handled correctly if the + // write on the underlying file is performed in append mode. + let is_append = flags & libc::O_APPEND as u32 != 0; + let flags = (!delayed_write && is_append).then_some(oslib::WritevFlags::RWF_APPEND); + r.read_to(&f, size as usize, offset, flags) + } + } + + fn getattr( + &self, + _ctx: Context, + inode: Inode, + _handle: Option, + ) -> io::Result<(libc::stat64, Duration)> { + self.do_getattr(inode) + } + + fn setattr( + &self, + _ctx: Context, + inode: Inode, + attr: libc::stat64, + handle: Option, + valid: SetattrValid, + ) -> io::Result<(libc::stat64, Duration)> { + let inode_data = self.inodes.get(inode).ok_or_else(ebadf)?; + + // In this case, we need to open a new O_RDWR FD + let rdwr_inode_file = handle.is_none() && valid.intersects(SetattrValid::SIZE); + let inode_file = if rdwr_inode_file { + inode_data.open_file(libc::O_NONBLOCK | libc::O_RDWR, &self.proc_self_fd)? + } else { + inode_data.get_file()? + }; + + // `HandleData` is never read, but we need to keep a reference so its file is not dropped + #[allow(dead_code)] + enum Data { + Handle(Arc, RawFd), + ProcPath(CString), + } + + // If we have a handle then use it otherwise get a new fd from the inode. + let data = if let Some(handle) = handle { + let hd = self.find_handle(handle, inode)?; + + let fd = hd.file.get()?.write().unwrap().as_raw_fd(); + Data::Handle(hd, fd) + } else { + let pathname = CString::new(format!("{}", inode_file.as_raw_fd())) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + Data::ProcPath(pathname) + }; + + if valid.contains(SetattrValid::MODE) { + // Safe because this doesn't modify any memory and we check the return value. + let res = unsafe { + match data { + Data::Handle(_, fd) => libc::fchmod(fd, attr.st_mode), + Data::ProcPath(ref p) => { + libc::fchmodat(self.proc_self_fd.as_raw_fd(), p.as_ptr(), attr.st_mode, 0) + } + } + }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + } + + if valid.intersects(SetattrValid::UID | SetattrValid::GID) { + let uid = if valid.contains(SetattrValid::UID) { + attr.st_uid + } else { + // Cannot use -1 here because these are unsigned values. + u32::MAX + }; + let gid = if valid.contains(SetattrValid::GID) { + attr.st_gid + } else { + // Cannot use -1 here because these are unsigned values. + u32::MAX + }; + + self.clear_file_capabilities(inode_file.as_raw_fd(), true)?; + + // Safe because this is a constant value and a valid C string. + let empty = unsafe { CStr::from_bytes_with_nul_unchecked(EMPTY_CSTR) }; + + // Safe because this doesn't modify any memory and we check the return value. + let res = unsafe { + libc::fchownat( + inode_file.as_raw_fd(), + empty.as_ptr(), + uid, + gid, + libc::AT_EMPTY_PATH | libc::AT_SYMLINK_NOFOLLOW, + ) + }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + } + + if valid.contains(SetattrValid::SIZE) { + let fd = match data { + Data::Handle(_, fd) => fd, + _ => { + // Should have opened an O_RDWR inode_file above + assert!(rdwr_inode_file); + inode_file.as_raw_fd() + } + }; + + let _killpriv_guard = + if self.cfg.killpriv_v2 && valid.contains(SetattrValid::KILL_SUIDGID) { + drop_effective_cap("FSETID")? + } else { + None + }; + + // Safe because this doesn't modify any memory and we check the return value. + let res = self + .clear_file_capabilities(fd, false) + .map(|_| unsafe { libc::ftruncate(fd, attr.st_size) })?; + if res < 0 { + return Err(io::Error::last_os_error()); + } + } + + if valid.intersects(SetattrValid::ATIME | SetattrValid::MTIME) { + let mut tvs = [ + libc::timespec { + tv_sec: 0, + tv_nsec: libc::UTIME_OMIT, + }, + libc::timespec { + tv_sec: 0, + tv_nsec: libc::UTIME_OMIT, + }, + ]; + + if valid.contains(SetattrValid::ATIME_NOW) { + tvs[0].tv_nsec = libc::UTIME_NOW; + } else if valid.contains(SetattrValid::ATIME) { + tvs[0].tv_sec = attr.st_atime; + tvs[0].tv_nsec = attr.st_atime_nsec; + } + + if valid.contains(SetattrValid::MTIME_NOW) { + tvs[1].tv_nsec = libc::UTIME_NOW; + } else if valid.contains(SetattrValid::MTIME) { + tvs[1].tv_sec = attr.st_mtime; + tvs[1].tv_nsec = attr.st_mtime_nsec; + } + + // Safe because this doesn't modify any memory and we check the return value. + let res = match data { + Data::Handle(_, fd) => unsafe { libc::futimens(fd, tvs.as_ptr()) }, + Data::ProcPath(ref p) => unsafe { + libc::utimensat(self.proc_self_fd.as_raw_fd(), p.as_ptr(), tvs.as_ptr(), 0) + }, + }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + } + + self.do_getattr(inode) + } + + fn rename( + &self, + _ctx: Context, + olddir: Inode, + oldname: &CStr, + newdir: Inode, + newname: &CStr, + flags: u32, + ) -> io::Result<()> { + validate_dentry_name(oldname)?; + validate_dentry_name(newname)?; + let old_inode = self.inodes.get(olddir).ok_or_else(ebadf)?; + let new_inode = self.inodes.get(newdir).ok_or_else(ebadf)?; + + let old_file = old_inode.get_file()?; + let new_file = new_inode.get_file()?; + + // Safe because this doesn't modify any memory and we check the return value. + // TODO: Switch to libc::renameat2 once https://github.com/rust-lang/libc/pull/1508 lands + // and we have glibc 2.28. + let res = unsafe { + libc::syscall( + libc::SYS_renameat2, + old_file.as_raw_fd(), + oldname.as_ptr(), + new_file.as_raw_fd(), + newname.as_ptr(), + flags, + ) + }; + if res != 0 { + return Err(io::Error::last_os_error()); + } + + if self.track_migration_info.load(Ordering::Relaxed) { + // When preparing for migration, we need to tell the migration code that this node has + // been renamed, which might need to be reflected in the migration info + if let Err(err) = self.update_inode_migration_info(new_inode, newname) { + warn!( + "Failed to update renamed file's ({oldname:?} -> {newname:?}) migration info, \ + the migration destination may be unable to find it: {err}", + ); + } + } + + Ok(()) + } + + fn mknod( + &self, + ctx: Context, + parent: Inode, + name: &CStr, + mode: u32, + rdev: u32, + umask: u32, + extensions: Extensions, + ) -> io::Result { + validate_dentry_name(name)?; + let data = self.inodes.get(parent).ok_or_else(ebadf)?; + let parent_file = data.get_file()?; + + let res = { + let _credentials_guard = UnixCredentials::new(ctx.uid, ctx.gid) + .supplementary_gid( + self.sup_group_extension.load(Ordering::Relaxed), + extensions.sup_gid, + ) + .set()?; + let _umask_guard = self + .posix_acl + .load(Ordering::Relaxed) + .then(|| oslib::ScopedUmask::new(umask)); + + // Safe because this doesn't modify any memory and we check the return value. + unsafe { + libc::mknodat( + parent_file.as_raw_fd(), + name.as_ptr(), + mode as libc::mode_t, + u64::from(rdev), + ) + } + }; + + if res < 0 { + return Err(io::Error::last_os_error()); + } + + // Set security context on node. + if let Some(secctx) = extensions.secctx { + if let Err(e) = self.do_mknod_mkdir_symlink_secctx(&parent_file, name, &secctx) { + unsafe { + libc::unlinkat(parent_file.as_raw_fd(), name.as_ptr(), 0); + }; + return Err(e); + } + } + self.do_lookup(parent, name, None) + } + + fn link( + &self, + _ctx: Context, + inode: Inode, + newparent: Inode, + newname: &CStr, + ) -> io::Result { + validate_dentry_name(newname)?; + let data = self.inodes.get(inode).ok_or_else(ebadf)?; + let new_inode = self.inodes.get(newparent).ok_or_else(ebadf)?; + + let inode_file = data.get_file()?; + let newparent_file = new_inode.get_file()?; + + let procname = CString::new(format!("{}", inode_file.as_raw_fd())) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + + // Safe because this doesn't modify any memory and we check the return value. + let res = unsafe { + libc::linkat( + self.proc_self_fd.as_raw_fd(), + procname.as_ptr(), + newparent_file.as_raw_fd(), + newname.as_ptr(), + libc::AT_SYMLINK_FOLLOW, + ) + }; + if res == 0 { + self.do_lookup(newparent, newname, None) + } else { + Err(io::Error::last_os_error()) + } + } + + fn symlink( + &self, + ctx: Context, + linkname: &CStr, + parent: Inode, + name: &CStr, + extensions: Extensions, + ) -> io::Result { + // The link *name* (the entry being created inside `parent`) must be a + // single dentry component -- multi-component paths like `"../foo"` + // would let a forged FUSE request escape the parent directory. + validate_dentry_name(name)?; + let data = self.inodes.get(parent).ok_or_else(ebadf)?; + let parent_file = data.get_file()?; + + let res = { + let _credentials_guard = UnixCredentials::new(ctx.uid, ctx.gid) + .supplementary_gid( + self.sup_group_extension.load(Ordering::Relaxed), + extensions.sup_gid, + ) + .set()?; + + // Safe because this doesn't modify any memory and we check the return value. + unsafe { libc::symlinkat(linkname.as_ptr(), parent_file.as_raw_fd(), name.as_ptr()) } + }; + + if res < 0 { + return Err(io::Error::last_os_error()); + } + + // Set security context on symlink. + if let Some(secctx) = extensions.secctx { + if let Err(e) = self.do_mknod_mkdir_symlink_secctx(&parent_file, name, &secctx) { + unsafe { + libc::unlinkat(parent_file.as_raw_fd(), name.as_ptr(), 0); + }; + return Err(e); + } + } + + self.do_lookup(parent, name, None) + } + + fn readlink(&self, _ctx: Context, inode: Inode) -> io::Result> { + let data = self.inodes.get(inode).ok_or_else(ebadf)?; + let inode_file = data.get_file()?; + + let mut buf = vec![0; libc::PATH_MAX as usize]; + + // Safe because this is a constant value and a valid C string. + let empty = unsafe { CStr::from_bytes_with_nul_unchecked(EMPTY_CSTR) }; + + // Safe because this will only modify the contents of `buf` and we check the return value. + let res = unsafe { + libc::readlinkat( + inode_file.as_raw_fd(), + empty.as_ptr(), + buf.as_mut_ptr() as *mut libc::c_char, + buf.len(), + ) + }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + + buf.resize(res as usize, 0); + Ok(buf) + } + + fn flush( + &self, + _ctx: Context, + inode: Inode, + handle: Handle, + _lock_owner: u64, + ) -> io::Result<()> { + let data = self.find_handle(handle, inode)?; + + // Since this method is called whenever an fd is closed in the client, we can emulate that + // behavior by doing the same thing (dup-ing the fd and then immediately closing it). Safe + // because this doesn't modify any memory and we check the return values. + unsafe { + let newfd = libc::dup(data.file.get()?.write().unwrap().as_raw_fd()); + if newfd < 0 { + return Err(io::Error::last_os_error()); + } + + if libc::close(newfd) < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(()) + } + } + } + + fn fsync(&self, _ctx: Context, inode: Inode, datasync: bool, handle: Handle) -> io::Result<()> { + let data = self.find_handle(handle, inode)?; + + let fd = data.file.get()?.write().unwrap().as_raw_fd(); + + // Safe because this doesn't modify any memory and we check the return value. + let res = unsafe { + if datasync { + libc::fdatasync(fd) + } else { + libc::fsync(fd) + } + }; + + if res == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + } + } + + fn fsyncdir( + &self, + ctx: Context, + inode: Inode, + datasync: bool, + handle: Handle, + ) -> io::Result<()> { + self.fsync(ctx, inode, datasync, handle) + } + + fn access(&self, ctx: Context, inode: Inode, mask: u32) -> io::Result<()> { + let data = self.inodes.get(inode).ok_or_else(ebadf)?; + let inode_file = data.get_file()?; + let st = statx(&inode_file, None)?.st; + let mode = mask as i32 & (libc::R_OK | libc::W_OK | libc::X_OK); + + if mode == libc::F_OK { + // The file exists since we were able to call `stat(2)` on it. + return Ok(()); + } + + if (mode & libc::R_OK) != 0 + && ctx.uid != 0 + && (st.st_uid != ctx.uid || st.st_mode & 0o400 == 0) + && (st.st_gid != ctx.gid || st.st_mode & 0o040 == 0) + && st.st_mode & 0o004 == 0 + { + return Err(io::Error::from_raw_os_error(libc::EACCES)); + } + + if (mode & libc::W_OK) != 0 + && ctx.uid != 0 + && (st.st_uid != ctx.uid || st.st_mode & 0o200 == 0) + && (st.st_gid != ctx.gid || st.st_mode & 0o020 == 0) + && st.st_mode & 0o002 == 0 + { + return Err(io::Error::from_raw_os_error(libc::EACCES)); + } + + // root can only execute something if it is executable by one of the owner, the group, or + // everyone. + if (mode & libc::X_OK) != 0 + && (ctx.uid != 0 || st.st_mode & 0o111 == 0) + && (st.st_uid != ctx.uid || st.st_mode & 0o100 == 0) + && (st.st_gid != ctx.gid || st.st_mode & 0o010 == 0) + && st.st_mode & 0o001 == 0 + { + return Err(io::Error::from_raw_os_error(libc::EACCES)); + } + + Ok(()) + } + + fn setxattr( + &self, + ctx: Context, + inode: Inode, + name: &CStr, + value: &[u8], + flags: u32, + extra_flags: SetxattrFlags, + ) -> io::Result<()> { + if !self.cfg.xattr { + return Err(io::Error::from_raw_os_error(libc::ENOSYS)); + } + + let data = self.inodes.get(inode).ok_or_else(ebadf)?; + let name = self.map_client_xattrname(name)?; + + // If we are setting posix access acl and if SGID needs to be + // cleared, then switch to caller's gid and drop CAP_FSETID + // and that should make sure host kernel clears SGID. + // + // This probably will not work when we support idmapped mounts. + // In that case we will need to find a non-root gid and switch + // to it. (Instead of gid in request). Fix it when we support + // idmapped mounts. + let xattr_name = name.as_ref().to_str().unwrap(); + let _clear_sgid_guard = if self.posix_acl.load(Ordering::Relaxed) + && extra_flags.contains(SetxattrFlags::SETXATTR_ACL_KILL_SGID) + && xattr_name.eq("system.posix_acl_access") + { + let cap_guard = drop_effective_cap("FSETID")?; + let credentials_guard = UnixCredentials::new(ctx.uid, ctx.gid).set()?; + + // If `UnixCredentials::set()` changes the effective user ID to non-zero, then the + // effective set is cleared from all capabilities. When switching back to root the + // permitted set is copied to the effective set. We need to keep `DAC_READ_SEARCH` + // to use file handles. + if self.cfg.inode_file_handles != InodeFileHandlesMode::Never { + if let Err(e) = crate::util::add_cap_to_eff("DAC_READ_SEARCH") { + warn!( + "failed to add 'DAC_READ_SEARCH' to the effective set of capabilities: {}", + e + ); + } + } + + (cap_guard, credentials_guard) + } else { + (None, None) + }; + + let res = if is_safe_inode(data.mode) { + // The f{set,get,remove,list}xattr functions don't work on an fd opened with `O_PATH` so we + // need to get a new fd. + let file = self.open_inode(inode, libc::O_RDONLY | libc::O_NONBLOCK)?; + + self.clear_file_capabilities(file.as_raw_fd(), false)?; + + // Safe because this doesn't modify any memory and we check the return value. + unsafe { + libc::fsetxattr( + file.as_raw_fd(), + name.as_ptr(), + value.as_ptr() as *const libc::c_void, + value.len(), + flags as libc::c_int, + ) + } + } else { + let file = data.get_file()?; + + self.clear_file_capabilities(file.as_raw_fd(), true)?; + + let procname = CString::new(format!("{}", file.as_raw_fd())) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + + let _working_dir_guard = + set_working_directory(self.proc_self_fd.as_raw_fd(), self.root_fd.as_raw_fd()); + + // Safe because this doesn't modify any memory and we check the return value. + unsafe { + libc::setxattr( + procname.as_ptr(), + name.as_ptr(), + value.as_ptr() as *const libc::c_void, + value.len(), + flags as libc::c_int, + ) + } + }; + if res == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + } + } + + fn getxattr( + &self, + _ctx: Context, + inode: Inode, + name: &CStr, + size: u32, + ) -> io::Result { + if !self.cfg.xattr { + return Err(io::Error::from_raw_os_error(libc::ENOSYS)); + } + + let mut buf = vec![0; size as usize]; + + let name = self.map_client_xattrname(name).map_err(|e| { + if e.kind() == ErrorKind::PermissionDenied { + io::Error::from_raw_os_error(libc::ENODATA) + } else { + e + } + })?; + + let data = self.inodes.get(inode).ok_or_else(ebadf)?; + + let res = if is_safe_inode(data.mode) { + // The f{set,get,remove,list}xattr functions don't work on an fd opened with `O_PATH` so we + // need to get a new fd. + let file = self.open_inode(inode, libc::O_RDONLY | libc::O_NONBLOCK)?; + + // Safe because this will only modify the contents of `buf`. + unsafe { + libc::fgetxattr( + file.as_raw_fd(), + name.as_ptr(), + buf.as_mut_ptr() as *mut libc::c_void, + size as libc::size_t, + ) + } + } else { + let file = data.get_file()?; + + let procname = CString::new(format!("{}", file.as_raw_fd())) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + + let _working_dir_guard = + set_working_directory(self.proc_self_fd.as_raw_fd(), self.root_fd.as_raw_fd()); + + // Safe because this will only modify the contents of `buf`. + unsafe { + libc::getxattr( + procname.as_ptr(), + name.as_ptr(), + buf.as_mut_ptr() as *mut libc::c_void, + size as libc::size_t, + ) + } + }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + + if size == 0 { + Ok(GetxattrReply::Count(res as u32)) + } else { + buf.resize(res as usize, 0); + Ok(GetxattrReply::Value(buf)) + } + } + + fn listxattr(&self, _ctx: Context, inode: Inode, size: u32) -> io::Result { + if !self.cfg.xattr { + return Err(io::Error::from_raw_os_error(libc::ENOSYS)); + } + + let data = self.inodes.get(inode).ok_or_else(ebadf)?; + let mut buf = vec![0; size as usize]; + + let res = if is_safe_inode(data.mode) { + // The f{set,get,remove,list}xattr functions don't work on an fd opened with `O_PATH` so we + // need to get a new fd. + let file = self.open_inode(inode, libc::O_RDONLY | libc::O_NONBLOCK)?; + + // Safe because this will only modify the contents of `buf`. + unsafe { + libc::flistxattr( + file.as_raw_fd(), + buf.as_mut_ptr() as *mut libc::c_char, + size as libc::size_t, + ) + } + } else { + let file = data.get_file()?; + + let procname = CString::new(format!("{}", file.as_raw_fd())) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + + let _working_dir_guard = + set_working_directory(self.proc_self_fd.as_raw_fd(), self.root_fd.as_raw_fd()); + + // Safe because this will only modify the contents of `buf`. + unsafe { + libc::listxattr( + procname.as_ptr(), + buf.as_mut_ptr() as *mut libc::c_char, + size as libc::size_t, + ) + } + }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + + if size == 0 { + Ok(ListxattrReply::Count(res as u32)) + } else { + buf.resize(res as usize, 0); + let buf = self.map_server_xattrlist(buf); + Ok(ListxattrReply::Names(buf)) + } + } + + fn removexattr(&self, _ctx: Context, inode: Inode, name: &CStr) -> io::Result<()> { + if !self.cfg.xattr { + return Err(io::Error::from_raw_os_error(libc::ENOSYS)); + } + + let data = self.inodes.get(inode).ok_or_else(ebadf)?; + let name = self.map_client_xattrname(name)?; + + let res = if is_safe_inode(data.mode) { + // The f{set,get,remove,list}xattr functions don't work on an fd opened with `O_PATH` so we + // need to get a new fd. + let file = self.open_inode(inode, libc::O_RDONLY | libc::O_NONBLOCK)?; + + // Safe because this doesn't modify any memory and we check the return value. + unsafe { libc::fremovexattr(file.as_raw_fd(), name.as_ptr()) } + } else { + let file = data.get_file()?; + + let procname = CString::new(format!("{}", file.as_raw_fd())) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + + let _working_dir_guard = + set_working_directory(self.proc_self_fd.as_raw_fd(), self.root_fd.as_raw_fd()); + + // Safe because this doesn't modify any memory and we check the return value. + unsafe { libc::removexattr(procname.as_ptr(), name.as_ptr()) } + }; + + if res == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + } + } + + fn fallocate( + &self, + _ctx: Context, + inode: Inode, + handle: Handle, + mode: u32, + offset: u64, + length: u64, + ) -> io::Result<()> { + let data = self.find_handle(handle, inode)?; + + let fd = data.file.get()?.write().unwrap().as_raw_fd(); + // Safe because this doesn't modify any memory and we check the return value. + let res = unsafe { + libc::fallocate64( + fd, + mode as libc::c_int, + offset as libc::off64_t, + length as libc::off64_t, + ) + }; + if res == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + } + } + + fn lseek( + &self, + _ctx: Context, + inode: Inode, + handle: Handle, + offset: u64, + whence: u32, + ) -> io::Result { + let data = self.find_handle(handle, inode)?; + + let fd = data.file.get()?.write().unwrap().as_raw_fd(); + + // Safe because this doesn't modify any memory and we check the return value. + let res = unsafe { libc::lseek(fd, offset as libc::off64_t, whence as libc::c_int) }; + if res < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(res as u64) + } + } + + fn copyfilerange( + &self, + _ctx: Context, + inode_in: Inode, + handle_in: Handle, + offset_in: u64, + inode_out: Inode, + handle_out: Handle, + offset_out: u64, + len: u64, + flags: u64, + ) -> io::Result { + let data_in = self.find_handle(handle_in, inode_in)?; + + // Take just a read lock as we're not going to alter the file descriptor offset. + let fd_in = data_in.file.get()?.read().unwrap().as_raw_fd(); + + let data_out = self.find_handle(handle_out, inode_out)?; + + // Take just a read lock as we're not going to alter the file descriptor offset. + let fd_out = data_out.file.get()?.read().unwrap().as_raw_fd(); + + // Safe because this will only modify `offset_in` and `offset_out` and we check + // the return value. + let res = unsafe { + libc::syscall( + libc::SYS_copy_file_range, + fd_in, + &mut (offset_in as i64) as &mut _ as *mut _, + fd_out, + &mut (offset_out as i64) as &mut _ as *mut _, + len, + flags, + ) + }; + if res < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(res as usize) + } + } + + fn syncfs(&self, _ctx: Context, inode: Inode) -> io::Result<()> { + // TODO: Branch here depending on whether virtiofsd announces submounts or not. + + let file = self.open_inode(inode, libc::O_RDONLY | libc::O_NOFOLLOW)?; + let raw_fd = file.as_raw_fd(); + debug!("syncfs: inode={}, mount_fd={}", inode, raw_fd); + let ret = unsafe { libc::syncfs(raw_fd) }; + if ret != 0 { + // Thread-safe, because errno is stored in thread-local storage. + Err(io::Error::last_os_error()) + } else { + Ok(()) + } + } +} + +impl HandleDataFile { + fn get(&self) -> io::Result<&'_ RwLock> { + match self { + HandleDataFile::File(file) => Ok(file), + HandleDataFile::Invalid(err) => Err(io::Error::new(err.kind(), Arc::clone(err))), + } + } +} + +impl From for HandleDataFile { + fn from(file: File) -> Self { + HandleDataFile::File(RwLock::new(file)) + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/mount_fd.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/mount_fd.rs new file mode 100644 index 0000000..e458e8c --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/mount_fd.rs @@ -0,0 +1,414 @@ +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use crate::passthrough::stat::{statx, MountId}; +use std::collections::{HashMap, HashSet}; +use std::ffi::CString; +use std::fs::File; +use std::io::{self, Read, Seek}; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::sync::{Arc, Mutex, RwLock, Weak}; + +pub struct MountFd { + map: Weak>>>, + mount_id: MountId, + file: File, +} + +/// This type maintains a map where each entry maps a mount ID to an open FD on that mount. Other +/// code can request an `Arc` for any mount ID. A key gets added to the map, when the +/// first `Arc` for that mount ID is requested. A key gets removed from the map, when the +/// last `Arc` for that mount ID is dropped. That is, map entries are reference-counted +/// and other code can keep an entry in the map by holding on to an `Arc`. +/// +/// We currently have one use case for `MountFds`: +/// +/// 1. Creating a file handle only returns a mount ID, but opening a file handle requires an open FD +/// on the respective mount. So we look that up in the map. +pub struct MountFds { + map: Arc>>>, + + /// /proc/self/mountinfo + mountinfo: Mutex, + + /// An optional prefix to strip from all mount points in mountinfo + mountprefix: Option, + + /// Set of filesystems for which we have already logged file handle errors + error_logged: Arc>>, +} + +impl MountFd { + pub fn file(&self) -> &File { + &self.file + } +} + +/** + * Error object (to be used as `Result`) for mount-point-related errors (hence MPR). + * Includes a description (that is auto-generated from the `io::Error` at first), which can be + * overridden with `MPRError::set_desc()`, or given a prefix with `MPRError::prefix()`. + * + * The full description can be retrieved through the `Display` trait implementation (or the + * auto-derived `ToString`). + * + * `MPRError` objects should generally be logged at some point, because they may indicate an error + * in the user's configuration or a bug in virtiofsd. However, we only want to log them once per + * filesystem, and so they can be silenced (setting `silent` to true if we know that we have + * already logged an error for the respective filesystem) and then should not be logged. + * + * Naturally, a "mount-point-related" error should be associated with some mount point, which is + * reflected in `fs_mount_id` and `fs_mount_root`. Setting these values will improve the error + * description, because the `Display` implementation will prepend these values to the returned + * string. + * + * To achieve this association, `MPRError` objects should be created through + * `MountFds::error_for()`, which obtains the mount root path for the given mount ID, and will thus + * try to not only set `fs_mount_id`, but `fs_mount_root` also. `MountFds::error_for()` will also + * take care to set `silent` as appropriate. + * + * (Sometimes, though, we know an error is associated with a mount point, but we do not know with + * which one. That is why the `fs_mount_id` field is optional.) + */ +#[derive(Debug)] +pub struct MPRError { + io: io::Error, + description: String, + silent: bool, + + fs_mount_id: Option, + fs_mount_root: Option, +} + +/// Type alias for convenience +pub type MPRResult = Result; + +impl Drop for MountFd { + fn drop(&mut self) { + debug!( + "Dropping MountFd: mount_id={}, mount_fd={}", + self.mount_id, + self.file.as_raw_fd(), + ); + + // If `self.map.upgrade()` fails, then the `MountFds` structure was dropped while there was + // still an `Arc` alive. In this case, we don't need to remove it from the map, + // because the map doesn't exist anymore. + if let Some(map) = self.map.upgrade() { + let mut map = map.write().unwrap(); + // After the refcount reaches zero and before we lock the map, there's a window where + // the value can be concurrently replaced by a `Weak` pointer to a new `MountFd`. + // Therefore, only remove the value if the refcount in the map is zero, too. + if let Some(0) = map.get(&self.mount_id).map(Weak::strong_count) { + map.remove(&self.mount_id); + } + } + } +} + +impl> From for MPRError { + /// Convert any stringifyable error object that can be converted to an `io::Error` to an + /// `MPRError`. Note that `fs_mount_id` and `fs_mount_root` are not set, so this `MPRError` + /// object is not associated with any mount point. + /// The initial description is taken from the original error object. + fn from(err: E) -> Self { + let description = err.to_string(); + MPRError { + io: err.into(), + description, + silent: false, + + fs_mount_id: None, + fs_mount_root: None, + } + } +} + +impl MPRError { + /// Override the current description + #[must_use] + pub fn set_desc(mut self, s: String) -> Self { + self.description = s; + self + } + + /// Add a prefix to the description + #[must_use] + pub fn prefix(self, s: String) -> Self { + let new_desc = format!("{}: {}", s, self.description); + self.set_desc(new_desc) + } + + /// To give additional information to the user (when this error is logged), add the mount ID of + /// the filesystem associated with this error + #[must_use] + fn set_mount_id(mut self, mount_id: MountId) -> Self { + self.fs_mount_id = Some(mount_id); + self + } + + /// To give additional information to the user (when this error is logged), add the mount root + /// path for the filesystem associated with this error + #[must_use] + fn set_mount_root(mut self, mount_root: String) -> Self { + self.fs_mount_root = Some(mount_root); + self + } + + /// Mark this error as silent (i.e. not to be logged) + #[must_use] + fn silence(mut self) -> Self { + self.silent = true; + self + } + + /// Return whether this error is silent (i.e. should not be logged) + pub fn silent(&self) -> bool { + self.silent + } + + /// Return the `io::Error` from an `MPRError` and drop the rest + pub fn into_inner(self) -> io::Error { + self.io + } +} + +impl std::fmt::Display for MPRError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match (self.fs_mount_id, &self.fs_mount_root) { + (None, None) => write!(f, "{}", self.description), + + (Some(id), None) => write!(f, "Filesystem with mount ID {}: {}", id, self.description), + + (None, Some(root)) => write!( + f, + "Filesystem mounted on \"{}\": {}", + root, self.description + ), + + (Some(id), Some(root)) => write!( + f, + "Filesystem mounted on \"{}\" (mount ID: {}): {}", + root, id, self.description + ), + } + } +} + +impl std::error::Error for MPRError {} + +impl MountFds { + pub fn new(mountinfo: File, mountprefix: Option) -> Self { + MountFds { + map: Default::default(), + mountinfo: Mutex::new(mountinfo), + mountprefix, + error_logged: Default::default(), + } + } + + pub fn get(&self, mount_id: MountId, reopen_fd: F) -> MPRResult> + where + F: FnOnce(RawFd, libc::c_int) -> io::Result, + { + let existing_mount_fd = self + .map + // The `else` branch below (where `existing_mount_fd` matches `None`) takes a write lock + // to insert a new mount FD into the hash map. This doesn't deadlock, because the read + // lock taken here doesn't have its lifetime extended beyond the statement, because + // `Weak::upgrade` returns a new pointer and not a reference into the read lock. + .read() + .unwrap() + .get(&mount_id) + // We treat a failed upgrade just like a non-existent key, because it means that all + // strong references to the `MountFd` have disappeared, so it's in the process of being + // dropped, but `MountFd::drop()` just did not yet get to remove it from the map. + .and_then(Weak::upgrade); + + let mount_fd = if let Some(mount_fd) = existing_mount_fd { + mount_fd + } else { + // `open_by_handle_at()` needs a non-`O_PATH` fd, which we will need to open here. We + // are going to open the filesystem's mount point, but we do not know whether that is a + // special file[1], and we must not open special files with anything but `O_PATH`, so + // we have to get some `O_PATH` fd first that we can stat to find out whether it is + // safe to open. + // [1] While mount points are commonly directories, it is entirely possible for a + // filesystem's root inode to be a regular or even special file. + let mount_point = self.get_mount_root(mount_id)?; + + // Clone `mount_point` so we can still use it in error messages + let c_mount_point = CString::new(mount_point.clone()).map_err(|e| { + self.error_for(mount_id, e) + .prefix(format!("Failed to convert \"{mount_point}\" to a CString")) + })?; + + let mount_point_fd = unsafe { libc::open(c_mount_point.as_ptr(), libc::O_PATH) }; + if mount_point_fd < 0 { + return Err(self + .error_for(mount_id, io::Error::last_os_error()) + .prefix(format!("Failed to open mount point \"{mount_point}\""))); + } + + // Safe because we have just opened this FD + let mount_point_path = unsafe { File::from_raw_fd(mount_point_fd) }; + + // Ensure that `mount_point_path` refers to an inode with the mount ID we need + let stx = statx(&mount_point_path, None).map_err(|e| { + self.error_for(mount_id, e) + .prefix(format!("Failed to stat mount point \"{mount_point}\"")) + })?; + + if stx.mnt_id != mount_id { + return Err(self + .error_for(mount_id, io::Error::from_raw_os_error(libc::EIO)) + .set_desc(format!( + "Mount point's ({}) mount ID ({}) does not match expected value ({})", + mount_point, stx.mnt_id, mount_id + ))); + } + + // Ensure that we can safely reopen `mount_point_path` with `O_RDONLY` + let file_type = stx.st.st_mode & libc::S_IFMT; + if file_type != libc::S_IFREG && file_type != libc::S_IFDIR { + return Err(self + .error_for(mount_id, io::Error::from_raw_os_error(libc::EIO)) + .set_desc(format!( + "Mount point \"{mount_point}\" is not a regular file or directory" + ))); + } + + // Now that we know that this is a regular file or directory, really open it + let file = reopen_fd( + mount_point_path.as_raw_fd(), + libc::O_RDONLY | libc::O_NOFOLLOW | libc::O_CLOEXEC, + ) + .map_err(|e| { + self.error_for(mount_id, e).prefix(format!( + "Failed to reopen mount point \"{mount_point}\" for reading" + )) + })?; + + let mut mount_fds_locked = self.map.write().unwrap(); + + // As above: by calling `and_then(Weak::upgrade)`, we treat a failed upgrade just like a + // non-existent key. If the key exists but upgrade fails, then `HashMap::insert()` + // below will update the value. `MountFd::drop()` takes care to only remove a `MountFd` + // without strong references from the map, and hence will not touch the updated one. + if let Some(mount_fd) = mount_fds_locked.get(&mount_id).and_then(Weak::upgrade) { + // A mount FD was added concurrently while we did not hold a lock on + // `mount_fds.map` -- use that entry (`file` will be dropped). + mount_fd + } else { + debug!( + "Creating MountFd: mount_id={}, mount_fd={}", + mount_id, + file.as_raw_fd(), + ); + let mount_fd = Arc::new(MountFd { + map: Arc::downgrade(&self.map), + mount_id, + file, + }); + mount_fds_locked.insert(mount_id, Arc::downgrade(&mount_fd)); + mount_fd + } + }; + + Ok(mount_fd) + } + + /// Given a mount ID, return the mount root path (by reading `/proc/self/mountinfo`) + fn get_mount_root(&self, mount_id: MountId) -> MPRResult { + let mountinfo = { + let mountinfo_file = &mut *self.mountinfo.lock().unwrap(); + + mountinfo_file.rewind().map_err(|e| { + self.error_for_nolookup(mount_id, e) + .prefix("Failed to access /proc/self/mountinfo".into()) + })?; + + let mut mountinfo = String::new(); + mountinfo_file.read_to_string(&mut mountinfo).map_err(|e| { + self.error_for_nolookup(mount_id, e) + .prefix("Failed to read /proc/self/mountinfo".into()) + })?; + + mountinfo + }; + + let path = mountinfo.split('\n').find_map(|line| { + let mut columns = line.split(char::is_whitespace); + + if columns.next()?.parse::().ok()? != mount_id { + return None; + } + + // Skip parent mount ID, major:minor device ID, and the root within the filesystem + // (to get to the mount path) + columns.nth(3) + }); + + match path { + Some(p) => { + let p = String::from(p); + if let Some(prefix) = self.mountprefix.as_ref() { + if let Some(suffix) = p.strip_prefix(prefix).filter(|s| !s.is_empty()) { + Ok(suffix.into()) + } else { + // The shared directory is the mount point (strip_prefix() returned "") or + // mount is outside the shared directory, so it must be the mount the root + // directory is on + Ok("/".into()) + } + } else { + Ok(p) + } + } + + None => Err(self + .error_for_nolookup(mount_id, io::Error::from_raw_os_error(libc::EINVAL)) + .set_desc(format!("Failed to find mount root for mount ID {mount_id}"))), + } + } + + /// Generate an `MPRError` object for the given `mount_id`, and silence it if we have already + /// generated such an object for that `mount_id`. + /// (Called `..._nolookup`, because in contrast to `MountFds::error_for()`, this method will + /// not try to look up the respective mount root path, and so is safe to call when such a + /// lookup would be unwise.) + fn error_for_nolookup>( + &self, + mount_id: MountId, + err: E, + ) -> MPRError { + let err = MPRError::from(err).set_mount_id(mount_id); + + if self.error_logged.read().unwrap().contains(&mount_id) { + err.silence() + } else { + self.error_logged.write().unwrap().insert(mount_id); + err + } + } + + /// Call `self.error_for_nolookup()`, and if the `MPRError` object is not silenced, try to + /// obtain the mount root path for the given `mount_id` and add it to the error object. + /// (Note: DO NOT call this method from `MountFds::get_mount_root()`, because that may lead to + /// an infinite loop.) + pub fn error_for>(&self, mount_id: MountId, err: E) -> MPRError { + let err = self.error_for_nolookup(mount_id, err); + + if err.silent() { + // No need to add more information + err + } else { + // This just adds some information, so ignore errors + if let Ok(mount_root) = self.get_mount_root(mount_id) { + err.set_mount_root(mount_root) + } else { + err + } + } + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/read_only.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/read_only.rs new file mode 100644 index 0000000..9812d7c --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/read_only.rs @@ -0,0 +1,475 @@ +// Copyright 2024 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +//! Implementation of a read-only variant of [`PassthroughFs`]. +//! +//! Implements a wrapper around [`PassthroughFs`] ([`PassthroughFsRo`]) that prohibits all +//! operations that would modify anything within the shared directory. This wrapper implements the +//! [`FileSystem`] and [`SerializableFileSystem`] traits, so can be used as a virtiofsd filesystem +//! driver. + +use super::util::{einval, erofs, openat}; +use super::PassthroughFs; +use crate::filesystem::{ + Context, Entry, Extensions, FileSystem, FsOptions, GetxattrReply, ListxattrReply, OpenOptions, + SerializableFileSystem, SetattrValid, SetxattrFlags, ZeroCopyReader, ZeroCopyWriter, +}; +use crate::passthrough::stat::{statx, StatExt}; + +use std::collections::HashMap; +use std::convert::TryInto; +use std::ffi::CStr; +use std::fs::File; +use std::io; +use std::sync::atomic::AtomicBool; +use std::sync::Arc; +use std::time::Duration; + +/// Wrapper around `PassthroughFs`, prohibiting modifications. +/// +/// Prevent any operation that would modify the underlying filesystem. +pub struct PassthroughFsRo(PassthroughFs); + +impl PassthroughFsRo { + /// Create a `PassthroughFsRo` filesystem. + /// + /// Internally creates a `PassthroughFs` filesystem using the `cfg` configuration, then wraps + /// it in the `PassthroughFsRo` type. + pub fn new(cfg: super::Config) -> io::Result { + let inner = PassthroughFs::new(cfg)?; + Ok(PassthroughFsRo(inner)) + } + + /// Internal: Run an `open()`-like function without allowing modifications or write access. + /// + /// That means: + /// - Prevent access modes other than `O_RDONLY` and the following flags: + /// - O_EXCL: We filter out `O_CREAT`, and then, its behavior will be undefined (except for + /// block devices, which don’t really work with virtio-fs anyway). In any case, on a + /// read-only filesystem, `O_CREAT | O_EXCL` will always give an error. + /// - O_TMPFILE: Not allowed with `O_RDONLY`. + /// - O_TRUNC: Undefined behavior with `O_RDONLY`, might truncate anyway. + /// - Filter out `O_CREAT`, and return `EROFS` if the path does not exist yet + /// + /// `open_fn` runs the underlying open function, taking the potentially modified flags as an + /// argument. + fn rofs_open io::Result>(flags: u32, open_fn: F) -> io::Result { + let cflags: libc::c_int = flags + .try_into() + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; + + // `O_PATH` ignores all flags but `O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW`, just allow it + // wholesale + if cflags & libc::O_PATH != 0 { + return open_fn(flags); + } + + if cflags & libc::O_ACCMODE != libc::O_RDONLY { + return Err(erofs()); + } + + // Problem: We would like to have an allowlist, not a denylist; `O_LARGEFILE` would be in + // that allowlist (and is indeed set by guests), but `libc::O_LARGEFILE == 0`. The actual + // value (in C) can vary between systems (and it seems that it does indeed vary), so we + // cannot use an allowlist, and have to live with a denylist only. + + // For what it’s worth, here is what the allowlist would be (for comparison against the + // denylist below): + // - O_ACCMODE + // -- already checked to be O_RDONLY + // - O_APPEND | O_DSYNC | O_SYNC + // -- only affect writes, so useless here, but not harmful + // - O_CREAT + // -- special-case below + // - O_NOATIME + // -- perfectly OK; in fact, we would rather force-set it, but cannot (see + // --preserve-noatime) + // - O_ASYNC | O_CLOEXEC | O_DIRECT | O_DIRECTORY | O_LARGEFILE | O_NOCTTY | O_NOFOLLOW | + // O_NONBLOCK + // -- Don’t have anything to do with writing in particular. + + // Note that at least `O_TMPFILE` occupies multiple bits, so we need to check exactly. Do + // it for the other flags, too, why not. + if cflags & libc::O_EXCL == libc::O_EXCL { + // O_EXCL is undefined without O_CREAT (which we filter out below). Then again, on a + // read-only filesystem, O_CREAT | O_EXCL will always return an error, so we can just + // do that here. Maybe we should check whether to return EROFS or EEXIST, depending on + // the case, but then again, if someone tries to create a new file on a read-only + // filesystem, we can just tell them it’s EROFS. + // (And it’s also weird to use O_CREAT | O_EXCL | O_RDONLY.) + return Err(erofs()); + } + if cflags & libc::O_TMPFILE == libc::O_TMPFILE { + // O_TMPFILE | O_RDONLY should have already resulted in EINVAL in the guest, but better + // safeguard it explicitly. + return Err(einval()); + } + if cflags & libc::O_TRUNC == libc::O_TRUNC { + // Undefined with O_RDONLY, so we can do what we want. We need to error out, though, + // lest passing it to the host will truncate the file even with O_RDONLY. + return Err(einval()); + } + + if cflags & libc::O_CREAT == 0 { + open_fn(flags) + } else { + // Try to open without CREAT, if that fails, return EROFS + open_fn(flags & !(libc::O_CREAT as u32)).map_err(|err| { + if err.kind() == io::ErrorKind::NotFound { + erofs() + } else { + err + } + }) + } + } +} + +/// Create function definitions that always fall through to the corresponding function on `self.0` +macro_rules! ops_allow { + { + $( + fn $name:ident$(<$($gen_name:ident: $gen_trait:path),*>)?( + &self + $(, $($par_name:ident: $par_type:ty),*)? + $(,)? + )$( -> $ret:ty)?; + )* + } => { + $( + fn $name$(<$($gen_name: $gen_trait),*>)?( + &self + $(, $($par_name: $par_type),*)? + )$( -> $ret)? { + self.0.$name($($($par_name),*)?) + } + )* + } +} + +/// Create function definitions that always return `Err(erofs())` +macro_rules! ops_forbid { + { + $( + fn $name:ident$(<$($gen_name:ident: $gen_trait:path),*>)?( + &self + $(, $($par_name:ident: $par_type:ty),*)? + $(,)? + ) -> io::Result<$ret_ok:ty>; + )* + } => { + $( + fn $name$(<$($gen_name: $gen_trait),*>)?( + &self + $(, $($par_name: $par_type),*)? + ) -> io::Result<$ret_ok> { + Err(erofs()) + } + )* + } +} + +impl FileSystem for PassthroughFsRo { + type Inode = ::Inode; + type Handle = ::Handle; + type DirIter = ::DirIter; + + // Execute these functions without restrictions + ops_allow! { + fn init(&self, capable: FsOptions) -> io::Result; + fn destroy(&self); + fn lookup(&self, ctx: Context, parent: Self::Inode, name: &CStr, + f_info: Option<(String, StatExt)>, + ) -> io::Result; + fn forget(&self, ctx: Context, inode: Self::Inode, count: u64); + fn batch_forget(&self, ctx: Context, requests: Vec<(Self::Inode, u64)>); + fn getattr(&self, + ctx: Context, + inode: Self::Inode, + handle: Option, + ) -> io::Result<(libc::stat64, Duration)>; + fn readlink(&self, ctx: Context, inode: Self::Inode) -> io::Result>; + fn read( + &self, + ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + w: W, + size: u32, + offset: u64, + lock_owner: Option, + flags: u32, + ) -> io::Result; + fn flush( + &self, + ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + lock_owner: u64, + ) -> io::Result<()>; + fn fsync( + &self, + ctx: Context, + inode: Self::Inode, + datasync: bool, + handle: Self::Handle, + ) -> io::Result<()>; + fn release( + &self, + ctx: Context, + inode: Self::Inode, + flags: u32, + handle: Self::Handle, + flush: bool, + flock_release: bool, + lock_owner: Option, + ) -> io::Result<()>; + fn statfs(&self, ctx: Context, inode: Self::Inode) -> io::Result; + fn getxattr( + &self, + ctx: Context, + inode: Self::Inode, + name: &CStr, + size: u32, + ) -> io::Result; + fn listxattr( + &self, + ctx: Context, + inode: Self::Inode, + size: u32, + ) -> io::Result; + fn readdir( + &self, + ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + size: u32, + offset: u64, + ) -> io::Result; + fn fsyncdir( + &self, + ctx: Context, + inode: Self::Inode, + datasync: bool, + handle: Self::Handle, + ) -> io::Result<()>; + fn releasedir( + &self, + ctx: Context, + inode: Self::Inode, + flags: u32, + handle: Self::Handle, + ) -> io::Result<()>; + fn lseek( + &self, + ctx: Context, + inode: Self::Inode, + handle: Self::Handle, + offset: u64, + whence: u32, + ) -> io::Result; + fn syncfs(&self, ctx: Context, inode: Self::Inode) -> io::Result<()>; + } + + // Refuse to run these functions, always returning EROFS. + // Note: We assume that these functions must always fail on a read-only filesystem, so failing + // without further checks should be safe and reasonable. However, the Linux kernel treats + // EROFS more like a final barrier, i.e. something that is returned only if the operation would + // succeed on a writable filesystem. For example, on an -o ro filesystem, `mkdir()` will not + // return EROFS immediately, but first check whether the path already exists, and if so, return + // EEXIST instead. That would be complicated though (and might introduce TOCTTOU problems), so + // unconditionally returning EROFS seems like a more viable option for us. + // (FWIW, the FUSE kernel driver does not seem to special-case EEXIST.) + ops_forbid! { + fn setattr( + &self, + _ctx: Context, + _inode: Self::Inode, + _attr: libc::stat64, + _handle: Option, + _valid: SetattrValid, + ) -> io::Result<(libc::stat64, Duration)>; + fn symlink( + &self, + _ctx: Context, + _linkname: &CStr, + _parent: Self::Inode, + _name: &CStr, + _extensions: Extensions, + ) -> io::Result; + fn mknod( + &self, + _ctx: Context, + _parent: Self::Inode, + _name: &CStr, + _mode: u32, + _rdev: u32, + _umask: u32, + _extensions: Extensions, + ) -> io::Result; + fn mkdir( + &self, + _ctx: Context, + _parent: Self::Inode, + _name: &CStr, + _mode: u32, + _umask: u32, + _extensions: Extensions, + ) -> io::Result; + fn unlink(&self, _ctx: Context, _parent: Self::Inode, _name: &CStr) -> io::Result<()>; + fn rmdir(&self, _ctx: Context, _parent: Self::Inode, _name: &CStr) -> io::Result<()>; + fn rename( + &self, + _ctx: Context, + _olddir: Self::Inode, + _oldname: &CStr, + _newdir: Self::Inode, + _newname: &CStr, + _flags: u32, + ) -> io::Result<()>; + fn link( + &self, + _ctx: Context, + _inode: Self::Inode, + _newparent: Self::Inode, + _newname: &CStr, + ) -> io::Result; + fn write( + &self, + _ctx: Context, + _inode: Self::Inode, + _handle: Self::Handle, + _r: R, + _size: u32, + _offset: u64, + _lock_owner: Option, + _delayed_write: bool, + _kill_priv: bool, + _flags: u32, + ) -> io::Result; + fn fallocate( + &self, + _ctx: Context, + _inode: Self::Inode, + _handle: Self::Handle, + _mode: u32, + _offset: u64, + _length: u64, + ) -> io::Result<()>; + fn setxattr( + &self, + _ctx: Context, + _inode: Self::Inode, + _name: &CStr, + _value: &[u8], + _flags: u32, + _extra_flags: SetxattrFlags, + ) -> io::Result<()>; + fn removexattr(&self, _ctx: Context, _inode: Self::Inode, _name: &CStr) -> io::Result<()>; + fn copyfilerange( + &self, + _ctx: Context, + _inode_in: Self::Inode, + _handle_in: Self::Handle, + _offset_in: u64, + _inode_out: Self::Inode, + _handle_out: Self::Handle, + _offset_out: u64, + _len: u64, + _flags: u64, + ) -> io::Result; + } + + fn open( + &self, + ctx: Context, + inode: Self::Inode, + kill_priv: bool, + flags: u32, + ) -> io::Result<(Option, OpenOptions)> { + Self::rofs_open(flags, |flags| self.0.open(ctx, inode, kill_priv, flags)) + } + + fn create( + &self, + ctx: Context, + parent: Self::Inode, + name: &CStr, + _mode: u32, + kill_priv: bool, + flags: u32, + _umask: u32, + _extensions: Extensions, + ) -> io::Result<(Entry, Option, OpenOptions)> { + // We never want to create, but we should allow opening existing files + let entry = self.lookup(ctx, parent, name, None).map_err(|err| { + if err.kind() == io::ErrorKind::NotFound { + erofs() + } else { + err + } + })?; + let (handle, opts) = self.open(ctx, entry.inode, kill_priv, flags)?; + Ok((entry, handle, opts)) + } + + fn opendir( + &self, + ctx: Context, + inode: Self::Inode, + flags: u32, + ) -> io::Result<(Option, OpenOptions)> { + Self::rofs_open(flags, |flags| self.0.opendir(ctx, inode, flags)) + } + + fn access(&self, ctx: Context, inode: Self::Inode, mask: u32) -> io::Result<()> { + if mask & libc::W_OK as u32 != 0 { + Err(erofs()) + } else { + self.0.access(ctx, inode, mask) + } + } + + fn get_root_ino(&self) -> io::Result { + let path_fd = openat( + &libc::AT_FDCWD, + self.0.cfg.root_dir.as_str(), + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + )?; + + let st = statx(&path_fd, None)?; + + Ok(st.st.st_ino) + } + + fn readdirplus_enabled(&self) -> bool { + self.0.readdirplus_enabled() + } +} + +impl SerializableFileSystem for PassthroughFsRo { + fn set_filter_path_remap(&self, remap: HashMap) { + self.0.set_filter_path_remap(remap) + } + + fn prepare_serialization(&self, cancel: Arc) -> io::Result<()> { + self.0.prepare_serialization(cancel) + } + + fn serialize(&self, state_pipe: File) -> io::Result<()> { + self.0.serialize(state_pipe) + } + + fn deserialize_and_apply(&self, state_pipe: File) -> io::Result<()> { + self.0.deserialize_and_apply(state_pipe) + } + + fn serialize_data(&self) -> io::Result> { + self.0.serialize_data() + } + + fn deserialize_and_apply_data(&self, serialized: &Vec) -> io::Result<()> { + self.0.deserialize_and_apply_data(serialized) + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/stat.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/stat.rs new file mode 100644 index 0000000..ea16b6a --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/stat.rs @@ -0,0 +1,152 @@ +// Copyright 2021 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use std::ffi::CStr; +use std::io; +use std::mem::MaybeUninit; +use std::os::unix::io::AsRawFd; + +mod file_status; +use crate::oslib; +use file_status::{statx_st, STATX_BASIC_STATS, STATX_MNT_ID}; + +const EMPTY_CSTR: &[u8] = b"\0"; + +pub type MountId = u64; + +#[derive(Clone)] +pub struct StatExt { + pub st: libc::stat64, + pub mnt_id: MountId, +} + +/* + * Fields in libc::statx are only valid if their respective flag in + * .stx_mask is set. This trait provides functions that allow safe + * access to the libc::statx components we are interested in. + * + * (The implementations of these functions need to check whether the + * associated flag is set, and then extract the respective information + * to return it.) + */ +trait SafeStatXAccess { + fn stat64(&self) -> Option; + fn mount_id(&self) -> Option; +} + +impl SafeStatXAccess for statx_st { + fn stat64(&self) -> Option { + fn makedev(maj: libc::c_uint, min: libc::c_uint) -> libc::dev_t { + libc::makedev(maj, min) + } + + if self.stx_mask & STATX_BASIC_STATS != 0 { + /* + * Unfortunately, we cannot use an initializer to create the + * stat64 object, because it may contain padding and reserved + * fields (depending on the architecture), and it does not + * implement the Default trait. + * So we take a zeroed struct and set what we can. + * (Zero in all fields is wrong, but safe.) + */ + let mut st = unsafe { MaybeUninit::::zeroed().assume_init() }; + + st.st_dev = makedev(self.stx_dev_major, self.stx_dev_minor); + st.st_ino = self.stx_ino; + st.st_mode = self.stx_mode as _; + st.st_nlink = self.stx_nlink as _; + st.st_uid = self.stx_uid; + st.st_gid = self.stx_gid; + st.st_rdev = makedev(self.stx_rdev_major, self.stx_rdev_minor); + st.st_size = self.stx_size as _; + st.st_blksize = self.stx_blksize as _; + st.st_blocks = self.stx_blocks as _; + st.st_atime = self.stx_atime.tv_sec; + st.st_atime_nsec = self.stx_atime.tv_nsec as _; + st.st_mtime = self.stx_mtime.tv_sec; + st.st_mtime_nsec = self.stx_mtime.tv_nsec as _; + st.st_ctime = self.stx_ctime.tv_sec; + st.st_ctime_nsec = self.stx_ctime.tv_nsec as _; + + Some(st) + } else { + None + } + } + + fn mount_id(&self) -> Option { + if self.stx_mask & STATX_MNT_ID != 0 { + Some(self.stx_mnt_id) + } else { + None + } + } +} + +fn get_mount_id(dir: &impl AsRawFd, path: &CStr) -> Option { + let mut mount_id: libc::c_int = 0; + let mut c_fh = oslib::CFileHandle::default(); + + oslib::name_to_handle_at(dir, path, &mut c_fh, &mut mount_id, libc::AT_EMPTY_PATH) + .ok() + .and(Some(mount_id as MountId)) +} + +// Only works on Linux, and libc::SYS_statx is only defined for these +// environments +/// Performs a statx() syscall. libc provides libc::statx() that does +/// the same, however, the system's libc may not have a statx() wrapper +/// (e.g. glibc before 2.28), so linking to it may fail. +/// libc::syscall() and libc::SYS_statx are always present, though, so +/// we can safely rely on them. +unsafe fn do_statx( + dirfd: libc::c_int, + pathname: *const libc::c_char, + flags: libc::c_int, + mask: libc::c_uint, + statxbuf: *mut statx_st, +) -> libc::c_int { + libc::syscall(libc::SYS_statx, dirfd, pathname, flags, mask, statxbuf) as libc::c_int +} + +// Real statx() that depends on do_statx() +pub fn statx(dir: &impl AsRawFd, path: Option<&CStr>) -> io::Result { + let mut stx_ui = MaybeUninit::::zeroed(); + + // Safe because this is a constant value and a valid C string. + let path = path.unwrap_or_else(|| unsafe { CStr::from_bytes_with_nul_unchecked(EMPTY_CSTR) }); + + // Safe because the kernel will only write data in `stx_ui` and we + // check the return value. + let res = unsafe { + do_statx( + dir.as_raw_fd(), + path.as_ptr(), + libc::AT_EMPTY_PATH | libc::AT_SYMLINK_NOFOLLOW, + STATX_BASIC_STATS | STATX_MNT_ID, + stx_ui.as_mut_ptr(), + ) + }; + if res >= 0 { + // Safe because we are only going to use the SafeStatXAccess + // trait methods + let stx = unsafe { stx_ui.assume_init() }; + + // if `statx()` doesn't provide the mount id (before kernel 5.8), + // let's try `name_to_handle_at()`, if everything fails just use 0 + let mnt_id = stx + .mount_id() + .or_else(|| get_mount_id(dir, path)) + .unwrap_or(0); + + Ok(StatExt { + st: stx + .stat64() + .ok_or_else(|| io::Error::from_raw_os_error(libc::ENOSYS))?, + mnt_id, + }) + } else { + Err(io::Error::last_os_error()) + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/stat/file_status.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/stat/file_status.rs new file mode 100644 index 0000000..8867a6a --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/stat/file_status.rs @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: BSD-3-Clause + +#[cfg(target_env = "gnu")] +pub use libc::statx as statx_st; + +#[cfg(target_env = "gnu")] +pub use libc::{STATX_BASIC_STATS, STATX_MNT_ID}; + +// musl provides the 'struct statx', but without stx_mnt_id. +// However, the libc crate does not provide libc::statx +// if musl is used. So we add just the required struct and +// constants to make it works. + +#[cfg(not(target_env = "gnu"))] +#[repr(C)] +pub struct statx_st_timestamp { + pub tv_sec: i64, + pub tv_nsec: u32, + pub __statx_timestamp_pad1: [i32; 1], +} + +#[cfg(not(target_env = "gnu"))] +#[repr(C)] +pub struct statx_st { + pub stx_mask: u32, + pub stx_blksize: u32, + pub stx_attributes: u64, + pub stx_nlink: u32, + pub stx_uid: u32, + pub stx_gid: u32, + pub stx_mode: u16, + __statx_pad1: [u16; 1], + pub stx_ino: u64, + pub stx_size: u64, + pub stx_blocks: u64, + pub stx_attributes_mask: u64, + pub stx_atime: statx_st_timestamp, + pub stx_btime: statx_st_timestamp, + pub stx_ctime: statx_st_timestamp, + pub stx_mtime: statx_st_timestamp, + pub stx_rdev_major: u32, + pub stx_rdev_minor: u32, + pub stx_dev_major: u32, + pub stx_dev_minor: u32, + pub stx_mnt_id: u64, + __statx_pad2: u64, + __statx_pad3: [u64; 12], +} + +#[cfg(not(target_env = "gnu"))] +pub const STATX_BASIC_STATS: libc::c_uint = 0x07ff; + +#[cfg(not(target_env = "gnu"))] +pub const STATX_MNT_ID: libc::c_uint = 0x1000; diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/util.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/util.rs new file mode 100644 index 0000000..5195ac0 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/util.rs @@ -0,0 +1,180 @@ +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +use crate::util::other_io_error; +use std::ffi::{CStr, CString}; +use std::fs::File; +use std::io; +use std::os::unix::io::{AsRawFd, FromRawFd}; + +/// Safe wrapper around libc::openat(). +pub fn openat(dir_fd: &impl AsRawFd, path: &str, flags: libc::c_int) -> io::Result { + let path_cstr = + CString::new(path).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + + // Safe because: + // - CString::new() has returned success and thus guarantees `path_cstr` is a valid + // NUL-terminated string + // - this does not modify any memory + // - we check the return value + // We do not check `flags` because if the kernel cannot handle poorly specified flags then we + // have much bigger problems. + let fd = unsafe { libc::openat(dir_fd.as_raw_fd(), path_cstr.as_ptr(), flags) }; + if fd >= 0 { + // Safe because we just opened this fd + Ok(unsafe { File::from_raw_fd(fd) }) + } else { + Err(io::Error::last_os_error()) + } +} + +/// Open `/proc/self/fd/{fd}` with the given flags to effectively duplicate the given `fd` with new +/// flags (e.g. to turn an `O_PATH` file descriptor into one that can be used for I/O). +pub fn reopen_fd_through_proc( + fd: &impl AsRawFd, + flags: libc::c_int, + proc_self_fd: &File, +) -> io::Result { + // Clear the `O_NOFOLLOW` flag if it is set since we need to follow the `/proc/self/fd` symlink + // to get the file. + openat( + proc_self_fd, + format!("{}", fd.as_raw_fd()).as_str(), + flags & !libc::O_NOFOLLOW, + ) +} + +/// Returns true if it's safe to open this inode without O_PATH. +pub fn is_safe_inode(mode: u32) -> bool { + // Only regular files and directories are considered safe to be opened from the file + // server without O_PATH. + matches!(mode & libc::S_IFMT, libc::S_IFREG | libc::S_IFDIR) +} + +pub fn ebadf() -> io::Error { + io::Error::from_raw_os_error(libc::EBADF) +} + +pub fn einval() -> io::Error { + io::Error::from_raw_os_error(libc::EINVAL) +} + +pub fn erofs() -> io::Error { + io::Error::from_raw_os_error(libc::EROFS) +} + +/// Looks up an FD's path through /proc/self/fd +pub fn get_path_by_fd(fd: &impl AsRawFd, proc_self_fd: &impl AsRawFd) -> io::Result { + let fname = format!("{}\0", fd.as_raw_fd()); + let fname_cstr = CStr::from_bytes_with_nul(fname.as_bytes()).unwrap(); + + let max_len = libc::PATH_MAX as usize; // does not include final NUL byte + let mut link_target = vec![0u8; max_len + 1]; // make space for NUL byte + + let ret = unsafe { + libc::readlinkat( + proc_self_fd.as_raw_fd(), + fname_cstr.as_ptr(), + link_target.as_mut_ptr().cast::(), + max_len, + ) + }; + if ret < 0 { + return Err(io::Error::last_os_error()); + } else if ret as usize == max_len { + return Err(other_io_error("Path too long".to_string())); + } + + link_target.truncate(ret as usize + 1); + let link_target_cstring = CString::from_vec_with_nul(link_target).map_err(other_io_error)?; + let link_target_str = link_target_cstring.to_string_lossy(); + + let pre_slash = link_target_str.split('/').next().unwrap(); + if pre_slash.contains(':') { + return Err(other_io_error("Not a file".to_string())); + } + + if link_target_str.ends_with(" (deleted)") { + return Err(other_io_error("Inode deleted".to_string())); + } + + Ok(link_target_cstring) +} + +/// Debugging helper function: Turn the given file descriptor into a string representation we can +/// show the user. If `proc_self_fd` is given, try to obtain the actual path through the symlink +/// in /proc/self/fd; otherwise (or on error), just print the integer representation (as +/// "{fd:%i}"). +pub fn printable_fd(fd: &impl AsRawFd, proc_self_fd: Option<&impl AsRawFd>) -> String { + if let Some(Ok(path)) = proc_self_fd.map(|psf| get_path_by_fd(fd, psf)) { + match path.into_string() { + Ok(s) => s, + Err(err) => err.into_cstring().to_string_lossy().into_owned(), + } + } else { + format!("{{fd:{}}}", fd.as_raw_fd()) + } +} + +/// Verify that a dentry name coming from a FUSE request is syntactically a +/// single path component, as any legitimate Linux VFS request would already +/// guarantee before dispatching to a FUSE server. +/// +/// The VFS resolves the leading portion of the user-supplied pathname inside +/// the guest kernel (see `fs/namei.c::filename_create()` and friends) and only +/// hands FUSE the final path component; therefore the following properties +/// must hold on the wire: +/// +/// * name is non-empty +/// * name does not contain `/` +/// * name is neither `.` nor `..` +/// * name does not contain an interior NUL byte +/// +/// A compromised guest userspace could nevertheless *forge* a FUSE request +/// where `name` is a multi-component path such as `"../etc/passwd"`. If we +/// then blindly hand that string to `openat(2)` / `symlinkat(2)` / +/// `mkdirat(2)` / `linkat(2)` / `renameat2(2)` etc., the kernel would happily +/// walk it and let the write escape the intended parent directory. This +/// validator closes that gap while remaining zero-cost for legitimate VFS +/// traffic (which never violates any of the above). +pub fn validate_dentry_name(name: &CStr) -> io::Result<()> { + let bytes = name.to_bytes(); + // An empty name matches the kernel's own errno for e.g. `openat(fd, "")`. + if bytes.is_empty() { + return Err(io::Error::from_raw_os_error(libc::ENOENT)); + } + // Reject `.`, `..` and any embedded `/`. A legitimate VFS request cannot + // hit any of these: the guest kernel resolves the pathname down to the + // final component before dispatching to FUSE. A forged FUSE request that + // does contain them would otherwise let the syscall walk outside the + // intended parent directory (`../etc/passwd`) or violate the + // "single new child" contract (`.`, `foo/bar`). + // + // Note: interior NUL bytes are already ruled out by the `&CStr` type + // invariant -- `server.rs` performs `CStr::from_bytes_with_nul` before + // we ever get here, so we don't need to re-check that. + if bytes == b"." || bytes == b".." || bytes.contains(&b'/') { + return Err(einval()); + } + Ok(()) +} + +pub fn relative_path<'a>(path: &'a CStr, prefix: &CStr) -> io::Result<&'a CStr> { + let mut relative_path = path + .to_bytes_with_nul() + .strip_prefix(prefix.to_bytes()) + .ok_or_else(|| { + other_io_error(format!( + "Path {path:?} is outside the directory ({prefix:?})" + )) + })?; + + // Remove leading / if left + while let Some(prefixless) = relative_path.strip_prefix(b"/") { + relative_path = prefixless; + } + + // Must succeed: Was a `CStr` before, converted to `&[u8]` via `to_bytes_with_nul()`, so must + // still contain exactly one NUL byte at the end of the slice + Ok(CStr::from_bytes_with_nul(relative_path).unwrap()) +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/xattrmap.rs b/vendor/cube-hypervisor/virtiofsd/src/passthrough/xattrmap.rs new file mode 100644 index 0000000..b7a6250 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/passthrough/xattrmap.rs @@ -0,0 +1,854 @@ +//! The `xattrmap` module is used to translate extended attribute operations +//! between the server (virtiofsd) and the client (the virtio-fs guest kernel +//! module). +//! +//! Here's a non-exhaustive list of use-cases in which it may be beneficial to +//! install an extended attribute mapping: +//! +//! * The guest VM process is executing at a privilege level where it can't +//! actually modify the extended attribute on the host. In this case, one +//! may choose to map those guest's extended attributes to a "user." +//! namespace on the host. +//! +//! * An extended attribute mapping can partition a host's extended attributes +//! from a guest's to prevent the guest from clobbering extended attributes +//! that the host has set and depends on. +//! +//! ## Rules +//! +//! The entity that launches virtiofsd may provide an "extended attributes +//! mapping" (or "xattrmap") that defines how extended attributes should be +//! translated. An xattrmap is really just a series of rules with a specific +//! syntax. When translating an xattr, the xattrmap rules are traversed in the +//! order that the mappings were originally written. The traversal is terminated +//! on the first rule that matches the xattr. +//! +//! The xattrmap _must_ have a terminating rule. +//! +//! ### Reference +//! +//! There are two ways of expressing an xattrmap rule: +//! +//! 1. `:type:scope:key:prepend:` +//! 2. `:map:key:prepend:` - this is just syntactic sugar for expressing a common +//! rule. It is equivalent to `:prefix:all:key:prepend`. +//! +//! An xattrmap is just a series of these rules separated by whitespace. Each rule +//! can have its own delimiter. The colon (`:`) was just used here as an arbitary +//! example. Use a delimiter that you find readable. +//! +//! Let's dissect the xattrmap rule syntax: `:type:scope:key:prepend:`. +//! +//! | type | description | +//! | - | - | +//! | prefix | The value of `key` is prepended to xattrs originating from the client (i.e., `{get,set,remove}xattr()`). The value of `prepend` is stripped from the server's reply to `listxattr()`. | +//! | ok | If the xattr originating from the client is prefixed with `key`, or if an xattr in a server reply is prefixed with `prepend` it passes through unchanged. | +//! | bad | If the xattr originating from the client is prefixed with `key` it is denied with `EPERM`. If the xattr in a server reply is prefixed with `prepend` it is hidden from the client and not included in the reply. | +//! | unsupported | If a client tries to use a name matching 'key' it's denied using ENOTSUP; when the server passes an attribute name matching 'prepend' it's hidden. In many ways its use is very like 'ok' as either an explicit terminator or for special handling of certain patterns. | +//! +//! `ok` and `bad` can both be used as simple terminators for an xattrmap to +//! satisfy the expectation that every xattrmap has a terminator. For example, +//! `:ok:all:::`, will vacuously terminate all mappings. Placing a rule like +//! this at the end of the xattrmap rules is a common way of providing a +//! terminator. +//! +//! | scope | description | +//! | - | - | +//! | server | Match on xattrnames in the server reply that are prefixed with `prepend`. | +//! | client | Match on xattrnames from the client that are prefixed with `key`. | +//! | all | Matches on both server replies and client requests as described for `server` and `client` scopes. | +//! +//! ### Examples +//! +//! These have been taken almost verbatim from the original virtiofsd +//! documentation in the QEMU source code. +//! +//! #### Example 1 +//! +//! ```text +//! :prefix:all::user.virtiofs.: +//! :bad:all::: +//! ``` +//! +//! There are two rules in this xattrmap. The first rule prefixes and strips +//! `user.virtiofs.` from client requests and server replies respectively. +//! +//! The second rule hides any non-prefixed extended attributes that the host +//! set. +//! +//! #### Example 2 +//! +//! ```text +//! /prefix/all/trusted./user.virtiofs./ +//! /bad/server//trusted./ +//! /bad/client/user.virtiofs.// +//! /ok/all/// +//! ``` +//! +//! The first rule prefixes client xattrnames with `trusted.` and strips +//! `user.virtiofs.` from xattrnames included in the server reply. +//! +//! The second rule hides unprefixed `trusted.` attributes on the host. +//! +//! The third rule prevents the guest from manipulating the `user.virtiofs.` +//! namespace directly. +//! +//! The final rule is the terminator and allows all remaining attributes +//! through unchanged. + +#![deny(missing_docs)] + +use std::borrow::Cow; +use std::convert::TryFrom; +use std::ffi::{CStr, CString}; +use std::fmt; +use std::iter::Peekable; + +/// Expected error conditions with respect to parsing an XattrMap or +/// attempting to match on a rule. +#[derive(Debug, Eq, PartialEq)] +pub enum ErrorKind { + /// Scope is not one of: "all", "server", "client". + InvalidScope { + /// The unexpected value parsed from the input stream. + got: String, + + /// A list of the expected values. + expected: String, + }, + + /// Type is not one of "prefix", "ok", "bad", or "map". + InvalidType { + /// The unexpected value parsed from the input stream. + got: String, + + /// A list of the expected values. + expected: String, + }, + + /// A delimiter has been found that does not match the delimiter + /// the rule started with. + InvalidDelimiter, + + /// The rule is missing fields. + IncompleteRule, + + /// There may only be one `map` rule and it must be the final + /// rule; if this error is returned, then multiple map rules + /// exist or one exists and it is not the final rule. + MapRuleViolation, + + /// The input stream doesn't contain any rules. + NoRulesProvided, + + /// None of the rules matched on the input. + UnterminatedMapping, +} + +impl std::error::Error for ErrorKind {} + +impl fmt::Display for ErrorKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{self:?}") + } +} + +/// Errors specific to XattrMap operations. +#[derive(Debug, Eq, PartialEq)] +pub struct Error { + /// The specific error condition that was detected. + pub cause: ErrorKind, + + /// The culpable rule, if any. + pub rule: Option, +} + +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(&self.cause) + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{self:?}") + } +} + +impl From for Error { + fn from(ek: ErrorKind) -> Self { + Self { + cause: ek, + rule: None, + } + } +} + +bitflags::bitflags! { + struct Scope: u8 { + const CLIENT = 0b01; + const SERVER = 0b10; + } +} + +impl Scope { + fn from_bytes>(bytes: B) -> Result { + let bytes = bytes.as_ref(); + + Ok(match bytes { + b"all" => Scope::CLIENT | Scope::SERVER, + b"client" => Scope::CLIENT, + b"server" => Scope::SERVER, + _ => { + return Err(ErrorKind::InvalidScope { + got: String::from_utf8_lossy(bytes).into(), + expected: ["all", "client", "server"].join(", "), + }) + } + }) + } +} + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +enum Type { + Prefix, + Okay, + Bad, + Unsupported, + Map, +} + +impl Type { + fn from_bytes>(bytes: B) -> Result { + let bytes = bytes.as_ref(); + + Ok(match bytes { + b"prefix" => Type::Prefix, + b"ok" => Type::Okay, + b"bad" => Type::Bad, + b"unsupported" => Type::Unsupported, + b"map" => Type::Map, + _ => { + return Err(ErrorKind::InvalidType { + got: String::from_utf8_lossy(bytes).into(), + expected: ["prefix", "ok", "bad", "map"].join(", "), + }) + } + }) + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] +struct Rule { + scope: Scope, + type_: Type, + key: CString, + prepend: CString, +} + +impl Rule { + fn matches(&self, scope: Scope, xattr_name: &[u8]) -> bool { + if !self.scope.contains(scope) { + return false; + } + + match scope { + Scope::CLIENT => xattr_name.starts_with(self.key.to_bytes()), + Scope::SERVER => xattr_name.starts_with(self.prepend.to_bytes()), + _ => panic!("ambiguous scope"), + } + } + + fn from_tokens(tokens: &mut Peekable) -> Result + where + I: Iterator, + { + // The caller has already trimmed the whitespace leading up to here, + // so the next element should be a rule delimiter. + let delim = tokens.next().ok_or(ErrorKind::InvalidDelimiter)?; + + // This exists instead of using take_while() because take_while() will + // consume the delimiter (if it exists) and it won't complain if it doesn't + // exist. That means that we wouldn't be able to check for an unterminated + // rule error like this: + // :prefix:all:trusted.:user.vm. + // ^ missing ':' + let mut next_token = || { + let mut bytes = vec![]; + loop { + if let Some(ch) = tokens.peek() { + if !ch.eq(&delim) { + bytes.push(*ch as u8); + let _ = tokens.next(); + } else { + // advance past delimiter + let _ = tokens.next(); + break; + } + } else { + // Ran out of tokens without finding a terminating delimiter + return Err(ErrorKind::IncompleteRule); + } + } + + Ok(bytes) + }; + + let type_ = Type::from_bytes(next_token()?)?; + + Ok(match type_ { + Type::Map => Rule { + type_, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new(next_token()?).unwrap(), + prepend: CString::new(next_token()?).unwrap(), + }, + Type::Prefix | Type::Okay | Type::Bad | Type::Unsupported => { + let scope = Scope::from_bytes(next_token()?)?; + + Rule { + type_, + scope, + key: CString::new(next_token()?).unwrap(), + prepend: CString::new(next_token()?).unwrap(), + } + } + }) + } + + fn expand_map_type(rule: Self) -> Vec { + assert_eq!(rule.type_, Type::Map); + + // 1st: Prefix matches/everything + let mut rules = vec![Rule { + type_: Type::Prefix, + scope: Scope::CLIENT | Scope::SERVER, + key: rule.key.clone(), + prepend: rule.prepend.clone(), + }]; + + let last_rule_type = if !rule.key.as_bytes().is_empty() { + // 2nd: Hide non-prefixed but matching entries on the host, and + // stop the client accessing prefixed attributes directly + rules.push(Rule { + type_: Type::Bad, + scope: Scope::CLIENT | Scope::SERVER, + key: rule.prepend, + prepend: rule.key, + }); + + Type::Okay + } else { + Type::Bad + }; + + // Last: Everything else + rules.push(Rule { + type_: last_rule_type, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("").unwrap(), + }); + + rules + } +} + +/// A return value that indicates the xattr name input has passed through +/// the XattrMap where a rule was successfully matched and applied to the +/// xattrname. +#[derive(Debug, Eq, PartialEq)] +pub enum AppliedRule<'a> { + /// Server should the interior value onward through to the requested operation. + Pass(Cow<'a, CStr>), + + /// Server should return EPERM (i.e., this matched on a `bad` rule). + Deny, + + /// Server should return ENOTSUP (i.e., this matched on a `unsupported` rule). + Unsupported, +} + +/// A collection of well-formed xattr translation rules. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct XattrMap { + rules: Vec, +} + +impl XattrMap { + /// Applies xattrmap rules to a single extended attribute name. + /// + /// This should be called *before* any other extended attribute + /// operation is performed on the host file system. + /// + /// Client request -> this method -> {get,set,remove}xattr() -> server response + /// + /// See also: getxattr(2), setxattr(2), removexattr(2) + pub fn map_client_xattr<'a>(&self, xattr_name: &'a CStr) -> Result, Error> { + let rule = self.find_rule(Scope::CLIENT, xattr_name.to_bytes())?; + + Ok(match rule.type_ { + Type::Okay => AppliedRule::Pass(Cow::Borrowed(xattr_name)), + Type::Bad => AppliedRule::Deny, + Type::Unsupported => AppliedRule::Unsupported, + Type::Prefix => { + let mut concat = rule.prepend.as_bytes().to_vec(); + concat.extend_from_slice(xattr_name.to_bytes()); + AppliedRule::Pass(Cow::Owned(CString::new(concat).unwrap())) + } + Type::Map => panic!("Unexpanded MAP rule was found."), + }) + } + + /// Applies xattrmap rules to a list of extended attribute names. + /// + /// This should be called *before* replying to the client with the list + /// of extended attribute names. + /// + /// Client request -> listxattr() -> this method -> server response + /// + /// See also: listxattr(2) + pub fn map_server_xattrlist(&self, xattr_names: Vec) -> Result, Error> { + let mut filtered = Vec::with_capacity(xattr_names.len()); + let unprocessed = xattr_names.split(|b| *b == 0).filter(|bs| !bs.is_empty()); + + for xattr_name in unprocessed { + let rule = self.find_rule(Scope::SERVER, xattr_name)?; + + let processed = match rule.type_ { + Type::Bad | Type::Unsupported => continue, // hide this from the client + Type::Okay => xattr_name, + Type::Prefix => &xattr_name[rule.prepend.as_bytes().len()..], // strip prefix + Type::Map => panic!("Unexpanded MAP rule was found."), + }; + + filtered.extend_from_slice(processed); + filtered.push(0); + } + + if filtered.is_empty() { + filtered.push(0); + } + + filtered.shrink_to_fit(); + + Ok(filtered) + } + + fn find_rule(&self, scope: Scope, xattr_name: &[u8]) -> Result<&Rule, Error> { + let rule = self + .rules + .iter() + .find(|r| r.matches(scope, xattr_name)) + .ok_or(ErrorKind::UnterminatedMapping) + .map_err(|e| Error { + cause: e, + rule: None, + })?; + + Ok(rule) + } +} + +impl TryFrom<&str> for XattrMap { + type Error = Error; + + fn try_from(input: &str) -> Result { + let trimmed = input.trim(); + let mut unparsed = trimmed.chars().peekable(); + let mut rules: Vec = vec![]; + + while unparsed.peek().is_some() { + // Skip any whitespace between rules + if let Some(ch) = unparsed.peek() { + if ch.is_ascii_whitespace() { + let _ = unparsed.next(); + continue; + } + } + + let rule = Rule::from_tokens(&mut unparsed).map_err(|e| Error { + cause: e, + rule: Some(rules.len() + 1), + })?; + + if rule.type_ == Type::Map { + // There may only be one 'map' rule and it must be the final rule + if unparsed.peek().is_some() { + return Err(Error { + rule: Some(rules.len() + 1), + cause: ErrorKind::MapRuleViolation, + }); + } + rules.append(&mut Rule::expand_map_type(rule)); + } else { + rules.push(rule); + }; + } + + if rules.is_empty() { + return Err(ErrorKind::NoRulesProvided.into()); + } + + Ok(Self { rules }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parser_can_parse_single_rule() { + let input = ":prefix:client:trusted.:user.virtiofs.:"; + let actual = XattrMap::try_from(input).unwrap(); + let expected = XattrMap { + rules: vec![Rule { + type_: Type::Prefix, + scope: Scope::CLIENT, + key: CString::new("trusted.").unwrap(), + prepend: CString::new("user.virtiofs.").unwrap(), + }], + }; + + assert_eq!(actual, expected); + } + + #[test] + fn test_parser_can_parse_multiple_valid_rules() { + let input = ":prefix:all::user.virtiofs.::bad:all:::"; + let actual = XattrMap::try_from(input).unwrap(); + let expected = XattrMap { + rules: vec![ + Rule { + type_: Type::Prefix, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("user.virtiofs.").unwrap(), + }, + Rule { + type_: Type::Bad, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("").unwrap(), + }, + ], + }; + + assert_eq!(actual, expected); + } + + #[test] + fn test_parser_can_parse_rules_separated_by_whitespace() { + let input = r#" + /prefix/all/trusted./user.virtiofs./ + /bad/server//trusted./ + /bad/client/user.virtiofs.// + /ok/all/// + "#; + + let actual = XattrMap::try_from(input).unwrap(); + let expected = XattrMap { + rules: vec![ + Rule { + type_: Type::Prefix, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("trusted.").unwrap(), + prepend: CString::new("user.virtiofs.").unwrap(), + }, + Rule { + type_: Type::Bad, + scope: Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("trusted.").unwrap(), + }, + Rule { + type_: Type::Bad, + scope: Scope::CLIENT, + key: CString::new("user.virtiofs.").unwrap(), + prepend: CString::new("").unwrap(), + }, + Rule { + type_: Type::Okay, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("").unwrap(), + }, + ], + }; + + assert_eq!(actual, expected); + } + + #[test] + fn test_parser_emits_incomplete_rule_error() { + let input = ":prefix:client:hi"; + let actual = XattrMap::try_from(input).unwrap_err(); + let expected = Error { + rule: Some(1), + cause: ErrorKind::IncompleteRule, + }; + assert_eq!(actual, expected); + } + + #[test] + fn test_parser_emits_error_when_multiple_map_rules_exist() { + let input = ":map:trusted.:virtiofs.user.::map:trusted.:virtiofs.user.:"; + let actual = XattrMap::try_from(input).unwrap_err(); + let expected = Error { + rule: Some(1), + cause: ErrorKind::MapRuleViolation, + }; + assert_eq!(actual, expected); + } + + #[test] + fn test_parser_expands_map_rule_with_empty_key() { + let input = ":map::user.virtiofs.:"; + let actual = XattrMap::try_from(input).unwrap(); + let expected = XattrMap { + rules: vec![ + Rule { + type_: Type::Prefix, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("user.virtiofs.").unwrap(), + }, + Rule { + type_: Type::Bad, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("").unwrap(), + }, + ], + }; + assert_eq!(actual, expected); + } + + #[test] + fn test_parser_expands_map_rule_with_key_and_prepend() { + let input = ":map:trusted.:user.virtiofs.:"; + let actual = XattrMap::try_from(input).unwrap(); + let expected = XattrMap { + rules: vec![ + Rule { + type_: Type::Prefix, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("trusted.").unwrap(), + prepend: CString::new("user.virtiofs.").unwrap(), + }, + Rule { + type_: Type::Bad, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("user.virtiofs.").unwrap(), + prepend: CString::new("trusted.").unwrap(), + }, + Rule { + type_: Type::Okay, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("").unwrap(), + }, + ], + }; + assert_eq!(actual, expected); + } + + #[test] + fn test_parser_emits_error_when_invalid_type_is_used() { + let input = ":TOMATOPIRATE:trusted.:virtiofs.user.:"; + assert!(XattrMap::try_from(input).is_err()); + } + + #[test] + fn test_parser_emits_error_when_invalid_scope_is_used() { + let input = "/prefix/helloworld///"; + assert!(XattrMap::try_from(input).is_err()); + } + + #[test] + fn test_parser_emits_error_when_no_rules_are_provided() { + let input = " "; + let actual = XattrMap::try_from(input).unwrap_err(); + let expected = Error { + rule: None, + cause: ErrorKind::NoRulesProvided, + }; + assert_eq!(actual, expected); + } + + #[test] + fn test_parser_can_parse_rules_with_different_delimiters() { + let input = ":prefix:all:trusted.:user.virtiofs.: /prefix/all/trusted./user.virtiofs./"; + let expected_rule = Rule { + type_: Type::Prefix, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("trusted.").unwrap(), + prepend: CString::new("user.virtiofs.").unwrap(), + }; + let expected = XattrMap { + rules: vec![expected_rule.clone(), expected_rule], + }; + + let actual = XattrMap::try_from(input).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn test_rule_ok_all() { + let map = XattrMap { + rules: vec![Rule { + type_: Type::Okay, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("").unwrap(), + }], + }; + let input = CString::new("user.virtiofs.potato").unwrap(); + let actual = map.map_client_xattr(&input).unwrap(); + let expected = AppliedRule::Pass(CString::new("user.virtiofs.potato").unwrap().into()); + assert_eq!(actual, expected); + } + + #[test] + fn test_rule_bad_hides_xattr_names_from_client() { + let input = b"security.secret\x00boring_attr".to_vec(); + let map = XattrMap { + rules: vec![ + Rule { + type_: Type::Bad, + scope: Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("security.").unwrap(), + }, + Rule { + type_: Type::Okay, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("").unwrap(), + }, + ], + }; + + let actual = map.map_server_xattrlist(input).unwrap(); + let expected = b"boring_attr\x00"; + assert_eq!(actual.as_slice(), expected); + } + + #[test] + fn test_rule_unsupported_hides_xattr_names_from_client() { + let input = b"security.secret\x00boring_attr".to_vec(); + let map = XattrMap { + rules: vec![ + Rule { + type_: Type::Unsupported, + scope: Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("security.").unwrap(), + }, + Rule { + type_: Type::Okay, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("").unwrap(), + }, + ], + }; + + let actual = map.map_server_xattrlist(input).unwrap(); + let expected = b"boring_attr\x00"; + assert_eq!(actual.as_slice(), expected); + } + + #[test] + fn test_rule_bad_denies_the_client_request() { + let map = XattrMap { + rules: vec![Rule { + type_: Type::Bad, + scope: Scope::CLIENT, + key: CString::new("").unwrap(), + prepend: CString::new("").unwrap(), + }], + }; + + let input = CString::new("virtiofs.").unwrap(); + let actual = map.map_client_xattr(&input).unwrap(); + let expected = AppliedRule::Deny; + assert_eq!(actual, expected); + } + + #[test] + fn test_rule_unsupported_not_support_the_client_request() { + let map = XattrMap { + rules: vec![Rule { + type_: Type::Unsupported, + scope: Scope::CLIENT, + key: CString::new("").unwrap(), + prepend: CString::new("").unwrap(), + }], + }; + + let input = CString::new("virtiofs.").unwrap(); + let actual = map.map_client_xattr(&input).unwrap(); + let expected = AppliedRule::Unsupported; + assert_eq!(actual, expected); + } + + #[test] + fn test_rule_prefix_prepends_xattr_names_from_client() { + let map = XattrMap { + rules: vec![Rule { + type_: Type::Prefix, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("trusted.").unwrap(), + prepend: CString::new("virtiofs.user.").unwrap(), + }], + }; + + let input = CString::new("trusted.secret_thing").unwrap(); + let actual = map.map_client_xattr(&input).unwrap(); + let expected = AppliedRule::Pass(Cow::Owned( + CString::new("virtiofs.user.trusted.secret_thing").unwrap(), + )); + assert_eq!(actual, expected); + } + + #[test] + fn test_rule_prefix_strips_prefixes_from_server() { + let map = XattrMap { + rules: vec![Rule { + type_: Type::Prefix, + scope: Scope::SERVER, + key: CString::new("").unwrap(), + prepend: CString::new("virtiofs.user.").unwrap(), + }], + }; + + let list = b"virtiofs.user.x".to_vec(); + let actual = map.map_server_xattrlist(list).unwrap(); + let expected = b"x\x00".to_vec(); + assert_eq!(actual, expected); + } + + #[test] + fn test_rule_ok_allows_xattr_names_to_pass_through_unchanged() { + let map = XattrMap { + rules: vec![Rule { + type_: Type::Okay, + scope: Scope::CLIENT | Scope::SERVER, + key: CString::new("allow.").unwrap(), + prepend: CString::new("allow.").unwrap(), + }], + }; + + let input = CString::new("allow.y").unwrap(); + let actual = map.map_client_xattr(&input).unwrap(); + let expected = AppliedRule::Pass(Cow::Owned(CString::new("allow.y").unwrap())); + assert_eq!(actual, expected); + + let list = b"allow.y\x00".to_vec(); + let expected = list.clone(); + let actual = map.map_server_xattrlist(list).unwrap(); + assert_eq!(actual, expected); + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/read_dir.rs b/vendor/cube-hypervisor/virtiofsd/src/read_dir.rs new file mode 100644 index 0000000..339f98a --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/read_dir.rs @@ -0,0 +1,158 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +use crate::filesystem::{DirEntry, DirectoryIterator}; + +use std::ffi::CStr; +use std::io; +use std::mem::size_of; +use std::ops::{Deref, DerefMut}; +use std::os::unix::io::AsRawFd; + +use vm_memory::ByteValued; + +#[repr(C, packed)] +#[derive(Default, Clone, Copy)] +struct LinuxDirent64 { + d_ino: libc::ino64_t, + d_off: libc::off64_t, + d_reclen: libc::c_ushort, + d_ty: libc::c_uchar, +} +unsafe impl ByteValued for LinuxDirent64 {} + +#[derive(Default)] +pub struct ReadDir

{ + buf: P, + current: usize, + end: usize, +} + +impl> ReadDir

{ + pub fn new(dir: &D, offset: libc::off64_t, buf: P) -> io::Result { + // Safe because this doesn't modify any memory and we check the return value. + let res = unsafe { libc::lseek64(dir.as_raw_fd(), offset, libc::SEEK_SET) }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + + // Safe because we used lseek() to get to the correct position + unsafe { Self::new_no_seek(dir, buf) } + } + + /// Continue reading from the current position in the directory without seeking. + /// + /// # Safety + /// Caller must ensure the current position is valid, for example, by exclusively using this + /// function on a given FD, potentially repeatedly. + pub unsafe fn new_no_seek(dir: &D, mut buf: P) -> io::Result { + // Safe because the kernel guarantees that it will only write to `buf` and we check the + // return value. + let res = unsafe { + libc::syscall( + libc::SYS_getdents64, + dir.as_raw_fd(), + buf.as_mut_ptr() as *mut LinuxDirent64, + buf.len() as libc::c_int, + ) + }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + + Ok(ReadDir { + buf, + current: 0, + end: res as usize, + }) + } +} + +impl

ReadDir

{ + /// Returns the number of bytes from the internal buffer that have not yet been consumed. + pub fn remaining(&self) -> usize { + self.end.saturating_sub(self.current) + } +} + +impl> DirectoryIterator for ReadDir

{ + fn next(&'_ mut self) -> Option> { + let rem = &self.buf[self.current..self.end]; + if rem.is_empty() { + return None; + } + + // We only use debug asserts here because these values are coming from the kernel and we + // trust them implicitly. + debug_assert!( + rem.len() >= size_of::(), + "not enough space left in `rem`" + ); + + let (front, back) = rem.split_at(size_of::()); + + let dirent64 = + LinuxDirent64::from_slice(front).expect("unable to get LinuxDirent64 from slice"); + + let namelen = dirent64.d_reclen as usize - size_of::(); + debug_assert!(namelen <= back.len(), "back is smaller than `namelen`"); + + // The kernel will pad the name with additional nul bytes until it is 8-byte aligned so + // we need to strip those off here. + let name = strip_padding(&back[..namelen]); + let entry = DirEntry { + ino: dirent64.d_ino, + offset: dirent64.d_off as u64, + type_: dirent64.d_ty as u32, + name, + }; + + debug_assert!( + rem.len() >= dirent64.d_reclen as usize, + "rem is smaller than `d_reclen`" + ); + self.current += dirent64.d_reclen as usize; + + Some(entry) + } +} + +// Like `CStr::from_bytes_with_nul` but strips any bytes after the first '\0'-byte. Panics if `b` +// doesn't contain any '\0' bytes. +fn strip_padding(b: &[u8]) -> &CStr { + // It would be nice if we could use memchr here but that's locked behind an unstable gate. + let pos = b + .iter() + .position(|&c| c == 0) + .expect("`b` doesn't contain any nul bytes"); + + // Safe because we are creating this string with the first nul-byte we found so we can + // guarantee that it is nul-terminated and doesn't contain any interior nuls. + unsafe { CStr::from_bytes_with_nul_unchecked(&b[..=pos]) } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn padded_cstrings() { + assert_eq!(strip_padding(b".\0\0\0\0\0\0\0").to_bytes(), b"."); + assert_eq!(strip_padding(b"..\0\0\0\0\0\0").to_bytes(), b".."); + assert_eq!( + strip_padding(b"normal cstring\0").to_bytes(), + b"normal cstring" + ); + assert_eq!(strip_padding(b"\0\0\0\0").to_bytes(), b""); + assert_eq!( + strip_padding(b"interior\0nul bytes\0\0\0").to_bytes(), + b"interior" + ); + } + + #[test] + #[should_panic(expected = "`b` doesn't contain any nul bytes")] + fn no_nul_byte() { + strip_padding(b"no nul bytes in string"); + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/sandbox.rs b/vendor/cube-hypervisor/virtiofsd/src/sandbox.rs new file mode 100644 index 0000000..e100383 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/sandbox.rs @@ -0,0 +1,676 @@ +// Copyright 2020 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use crate::{idmap, oslib, util}; +use idmap::{GidMap, IdMapSetUpPipeMessage, UidMap}; +use std::ffi::CString; +use std::fs::{self, File}; +use std::io::{Read, Write}; +use std::os::fd::OwnedFd; +use std::os::unix::io::{AsRawFd, FromRawFd}; +use std::path::Path; +use std::process::{self, Command}; +use std::str::FromStr; +use std::{error, fmt, io}; +use vhost::vhost_user::Listener; + +#[derive(Debug)] +pub enum Error { + /// Failed to bind mount `/proc/self/fd` into a temporary directory. + BindMountProcSelfFd(io::Error), + /// Failed to bind mount shared directory. + BindMountSharedDir(io::Error), + /// Failed to change to the old root directory. + ChdirOldRoot(io::Error), + /// Failed to change to the new root directory. + ChdirNewRoot(io::Error), + /// Call to libc::chroot returned an error. + Chroot(io::Error), + /// Failed to change to the root directory after the chroot call. + ChrootChdir(io::Error), + /// Failed to clean the properties of the mount point. + CleanMount(io::Error), + /// Failed to create a temporary directory. + CreateTempDir(io::Error), + /// Failed to drop supplemental groups. + DropSupplementalGroups(io::Error), + /// Call to libc::fork returned an error. + Fork(io::Error), + /// Failed to get the number of supplemental groups. + GetSupplementalGroups(io::Error), + /// Error bind-mounting a directory. + MountBind(io::Error), + /// Failed to mount old root. + MountOldRoot(io::Error), + /// Error mounting proc. + MountProc(io::Error), + /// Failed to mount new root. + MountNewRoot(io::Error), + /// Error mounting target directory. + MountTarget(io::Error), + /// Failed to open `/proc/self/mountinfo`. + OpenMountinfo(io::Error), + /// Failed to open new root. + OpenNewRoot(io::Error), + /// Failed to open old root. + OpenOldRoot(io::Error), + /// Failed to open `/proc/self`. + OpenProcSelf(io::Error), + /// Failed to open `/proc/self/fd`. + OpenProcSelfFd(io::Error), + /// Error switching root directory. + PivotRoot(io::Error), + /// Failed to remove temporary directory. + RmdirTempDir(io::Error), + /// Failed to lazily unmount old root. + UmountOldRoot(io::Error), + /// Failed to lazily unmount temporary directory. + UmountTempDir(io::Error), + /// Call to libc::unshare returned an error. + Unshare(io::Error), + /// Failed to execute `newgidmap(1)`. + WriteGidMap(String), + /// Failed to write to `/proc/self/setgroups`. + WriteSetGroups(io::Error), + /// Failed to execute `newuidmap(1)`. + WriteUidMap(String), + /// Sandbox mode unavailable for non-privileged users + SandboxModeInvalidUID, + /// Setting uid_map is only allowed inside a namespace for non-privileged users + SandboxModeInvalidUidMap, + /// Setting gid_map is only allowed inside a namespace for non-privileged users + SandboxModeInvalidGidMap, +} + +impl error::Error for Error {} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use self::Error::{ + SandboxModeInvalidGidMap, SandboxModeInvalidUID, SandboxModeInvalidUidMap, WriteGidMap, + WriteUidMap, + }; + match self { + SandboxModeInvalidUID => { + write!( + f, + "sandbox mode 'chroot' can only be used by \ + root (Use '--sandbox namespace' instead)" + ) + } + SandboxModeInvalidUidMap => { + write!( + f, + "uid_map can only be used by unprivileged user where sandbox mod is namespace \ + (Use '--sandbox namespace' instead)" + ) + } + SandboxModeInvalidGidMap => { + write!( + f, + "gid_map can only be used by unprivileged user where sandbox mod is namespace \ + (Use '--sandbox namespace' instead)" + ) + } + WriteUidMap(msg) => write!(f, "write to uid map failed: {msg}"), + WriteGidMap(msg) => write!(f, "write to gid map failed: {msg}"), + _ => write!(f, "{self:?}"), + } + } +} + +/// Mechanism to be used for setting up the sandbox. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum SandboxMode { + /// Create the sandbox using Linux namespaces. + Namespace, + /// Create the sandbox using chroot. + Chroot, + /// Don't attempt to isolate the process inside a sandbox. + None, +} + +impl FromStr for SandboxMode { + type Err = &'static str; + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "namespace" => Ok(SandboxMode::Namespace), + "chroot" => Ok(SandboxMode::Chroot), + "none" => Ok(SandboxMode::None), + _ => Err("Unknown sandbox mode"), + } + } +} + +/// A helper for creating a sandbox for isolating the service. +pub struct Sandbox { + /// The directory that is going to be shared with the VM. The sandbox will be constructed on top + /// of this directory. + shared_dir: String, + /// A `File` object for `/proc/self/fd` obtained from the sandboxed context. + proc_self_fd: Option, + /// A `File` object for `/proc/self/mountinfo` obtained from the sandboxed context. + mountinfo_fd: Option, + /// Mechanism to be used for setting up the sandbox. + sandbox_mode: SandboxMode, + /// UidMap to be used for `newuidmap(1)` command line arguments + uid_map: Vec, + /// GidMap to be used for `newgidmap(1)` command line arguments + gid_map: Vec, +} + +impl Sandbox { + pub fn new( + shared_dir: String, + sandbox_mode: SandboxMode, + uid_map: Vec, + gid_map: Vec, + ) -> io::Result { + let shared_dir_rp = fs::canonicalize(shared_dir)?; + let shared_dir_rp_str = shared_dir_rp + .to_str() + .ok_or_else(|| io::Error::from_raw_os_error(libc::EINVAL))?; + + Ok(Sandbox { + shared_dir: shared_dir_rp_str.into(), + proc_self_fd: None, + mountinfo_fd: None, + sandbox_mode, + uid_map, + gid_map, + }) + } + + // Make `self.shared_dir` our root directory, and get isolated file descriptors for + // `/proc/self/fd` and '/proc/self/mountinfo`. + // + // This is based on virtiofsd's setup_namespaces() and setup_mounts(), and it's very similar to + // the strategy used in containers. Consists on a careful sequence of mounts and bind-mounts to + // ensure it's not possible to escape the sandbox through `self.shared_dir` nor the file + // descriptor obtained for `/proc/self/fd`. + // + // It's ugly, but it's the only way until Linux implements a proper containerization API. + fn setup_mounts(&mut self) -> Result<(), Error> { + // Open an FD to `/proc/self` so we can later open `/proc/self/mountinfo`. + // (If we opened `/proc/self/mountinfo` now, it would appear empty by the end of this + // function, which is why we need to defer opening it until then.) + let c_proc_self = CString::new("/proc/self").unwrap(); + let proc_self_raw = unsafe { libc::open(c_proc_self.as_ptr(), libc::O_PATH) }; + if proc_self_raw < 0 { + return Err(Error::OpenProcSelf(std::io::Error::last_os_error())); + } + + // Encapsulate the `/proc/self` FD in a `File` object so it is closed when this function + // returns + let proc_self = unsafe { File::from_raw_fd(proc_self_raw) }; + + // Ensure our mount changes don't affect the parent mount namespace. + + oslib::mount(None, "/", None, libc::MS_SLAVE | libc::MS_REC).map_err(Error::CleanMount)?; + + // Mount `/proc` in this context. + oslib::mount( + "proc".into(), + "/proc", + "proc".into(), + libc::MS_NODEV | libc::MS_NOEXEC | libc::MS_NOSUID | libc::MS_RELATIME, + ) + .map_err(Error::MountProc)?; + + // Bind-mount `/proc/self/fd` onto /proc preventing access to ancestor + // directories. + oslib::mount("/proc/self/fd".into(), "/proc", None, libc::MS_BIND) + .map_err(Error::BindMountProcSelfFd)?; + + // Obtain a file descriptor to /proc/self/fd/ by opening bind-mounted /proc directory. + let c_proc_dir = CString::new("/proc").unwrap(); + let proc_self_fd = unsafe { libc::open(c_proc_dir.as_ptr(), libc::O_PATH) }; + if proc_self_fd < 0 { + return Err(Error::OpenProcSelfFd(std::io::Error::last_os_error())); + } + // Safe because we just opened this fd. + self.proc_self_fd = Some(unsafe { File::from_raw_fd(proc_self_fd) }); + + // Bind-mount `self.shared_dir` on itself so we can use as new root on `pivot_root` syscall. + oslib::mount( + self.shared_dir.as_str().into(), + self.shared_dir.as_str(), + None, + libc::MS_BIND | libc::MS_REC, + ) + .map_err(Error::BindMountSharedDir)?; + + // Get a file descriptor to our old root so we can reference it after switching root. + let c_root_dir = CString::new("/").unwrap(); + let oldroot_fd = unsafe { + libc::open( + c_root_dir.as_ptr(), + libc::O_DIRECTORY | libc::O_RDONLY | libc::O_CLOEXEC, + ) + }; + if oldroot_fd < 0 { + return Err(Error::OpenOldRoot(std::io::Error::last_os_error())); + } + + // Get a file descriptor to the new root so we can reference it after switching root. + let c_shared_dir = CString::new(self.shared_dir.clone()).unwrap(); + let newroot_fd = unsafe { + libc::open( + c_shared_dir.as_ptr(), + libc::O_DIRECTORY | libc::O_RDONLY | libc::O_CLOEXEC, + ) + }; + if newroot_fd < 0 { + return Err(Error::OpenNewRoot(std::io::Error::last_os_error())); + } + + // Change to new root directory to prepare for `pivot_root` syscall. + oslib::fchdir(newroot_fd).map_err(Error::ChdirNewRoot)?; + + // Call to `pivot_root` using `.` as both new and old root. + let c_current_dir = CString::new(".").unwrap(); + let ret = unsafe { + libc::syscall( + libc::SYS_pivot_root, + c_current_dir.as_ptr(), + c_current_dir.as_ptr(), + ) + }; + if ret < 0 { + return Err(Error::PivotRoot(std::io::Error::last_os_error())); + } + + // Change to old root directory to prepare for cleaning up and unmounting it. + oslib::fchdir(oldroot_fd).map_err(Error::ChdirOldRoot)?; + + // Clean up old root to avoid mount namespace propagation. + oslib::mount(None, ".", None, libc::MS_SLAVE | libc::MS_REC).map_err(Error::CleanMount)?; + + // Lazily unmount old root. + oslib::umount2(".", libc::MNT_DETACH).map_err(Error::UmountOldRoot)?; + + // Change to new root. + oslib::fchdir(newroot_fd).map_err(Error::ChdirNewRoot)?; + + // We no longer need these file descriptors, so close them. + unsafe { libc::close(newroot_fd) }; + unsafe { libc::close(oldroot_fd) }; + + // Open `/proc/self/mountinfo` now + let c_mountinfo = CString::new("mountinfo").unwrap(); + let mountinfo_fd = + unsafe { libc::openat(proc_self.as_raw_fd(), c_mountinfo.as_ptr(), libc::O_RDONLY) }; + if mountinfo_fd < 0 { + return Err(Error::OpenMountinfo(std::io::Error::last_os_error())); + } + // Safe because we just opened this fd. + self.mountinfo_fd = Some(unsafe { File::from_raw_fd(mountinfo_fd) }); + + Ok(()) + } + + /// Sets mappings for the given uid and gid. + fn setup_id_mappings( + &self, + uid_map: &[UidMap], + gid_map: &[GidMap], + pid: i32, + ) -> Result<(), Error> { + let current_uid = unsafe { libc::geteuid() }; + let current_gid = unsafe { libc::getegid() }; + + // Take uid map or set up a 1-to-1 mapping for our current euid. + let default_uid_map = vec![UidMap { + outside_uid: current_uid, + inside_uid: current_uid, + count: 1, + }]; + let uid_map = if uid_map.is_empty() { + &default_uid_map + } else { + uid_map + }; + + // Take gid map or set up a 1-to-1 mapping for our current gid. + let default_gid_map = vec![GidMap { + outside_gid: current_gid, + inside_gid: current_gid, + count: 1, + }]; + let gid_map = if gid_map.is_empty() { + &default_gid_map + } else { + gid_map + }; + + // Unprivileged user can not set any mapping without any restriction. + // Therefore, newuidmap/newgidmap is used instead of writing directly + // into proc/[pid]/{uid,gid}_map if a potentially privileged action is + // requested (outside {u,g}id != e{u,g}id or count > 1). + if uid_map.len() != 1 || uid_map[0].outside_uid != current_uid || uid_map[0].count > 1 { + let mut newuidmap = Command::new("newuidmap"); + newuidmap.arg(pid.to_string()); + for entry in uid_map.iter() { + newuidmap.arg(entry.inside_uid.to_string()); + newuidmap.arg(entry.outside_uid.to_string()); + newuidmap.arg(entry.count.to_string()); + } + let output = newuidmap.output().map_err(|_| { + Error::WriteUidMap(format!( + "failed to execute newuidmap: {}", + io::Error::last_os_error() + )) + })?; + if !output.status.success() { + return Err(Error::WriteUidMap( + String::from_utf8_lossy(&output.stderr).to_string(), + )); + } + } else { + // Unprivileged part, we can driectly write to /proc/[pid]/uid_map. + std::fs::write( + format!("/proc/{pid}/uid_map"), + format!("{} {} 1", uid_map[0].inside_uid, uid_map[0].outside_uid), + ) + .map_err(|e| Error::WriteUidMap(e.to_string()))?; + } + + if gid_map.len() != 1 || gid_map[0].outside_gid != current_gid || gid_map[0].count > 1 { + let mut newgidmap = Command::new("newgidmap"); + newgidmap.arg(pid.to_string()); + for entry in gid_map.iter() { + newgidmap.arg(entry.inside_gid.to_string()); + newgidmap.arg(entry.outside_gid.to_string()); + newgidmap.arg(entry.count.to_string()); + } + let output = newgidmap.output().map_err(|_| { + Error::WriteGidMap(format!( + "failed to execute newgidmap: {}", + io::Error::last_os_error() + )) + })?; + if !output.status.success() { + return Err(Error::WriteGidMap( + String::from_utf8_lossy(&output.stderr).to_string(), + )); + } + } else { + // Unprivileged part, we can driectly write to /proc/[pid]/gid_map. + std::fs::write(format!("/proc/{pid}/setgroups"), b"deny") + .map_err(|e| Error::WriteGidMap(e.to_string()))?; + std::fs::write( + format!("/proc/{pid}/gid_map"), + format!("{} {} 1", gid_map[0].inside_gid, gid_map[0].outside_gid), + ) + .map_err(|e| Error::WriteGidMap(e.to_string()))?; + } + Ok(()) + } + + pub fn enter_namespace(&mut self, listener: Listener) -> Result { + let uid = unsafe { libc::geteuid() }; + + let flags = if uid == 0 { + libc::CLONE_NEWPID | libc::CLONE_NEWNS | libc::CLONE_NEWNET + } else { + // If running as an unprivileged user, rely on user_namespaces(7) for isolation. + libc::CLONE_NEWPID | libc::CLONE_NEWNS | libc::CLONE_NEWNET | libc::CLONE_NEWUSER + }; + + let (mut x_reader, mut x_writer) = oslib::pipe().unwrap(); + let (mut y_reader, mut y_writer) = oslib::pipe().unwrap(); + + let pid = util::sfork().map_err(Error::Fork)?; + let mut output = [0]; + + // First child is only responsible to setup id mapping + // from outside of the main thread's namespace. + // Pipe is used for synchronization between the main thread and the first child. + // That will guarantee the mapping is done before the main thread gets running. + if pid == 0 { + // First child + // Dropping the other end of the pipes + drop(x_writer); + drop(y_reader); + + // This is waiting until unshare() returns + x_reader.read_exact(&mut output).unwrap(); + assert_eq!(output[0], IdMapSetUpPipeMessage::Request as u8); + + // Setup uid/gid mappings + if uid != 0 { + let ppid = unsafe { libc::getppid() }; + if let Err(error) = self.setup_id_mappings(&self.uid_map, &self.gid_map, ppid) { + // We don't really need to close the pipes here, since the OS will close the FDs + // after the process exits. But let's do it explicitly to signal an error to the + // other end of the pipe. + drop(x_reader); + drop(y_writer); + error!("sandbox: couldn't setup id mappings: {}", error); + process::exit(1); + }; + } + + // Signal that mapping is done + y_writer + .write_all(&[IdMapSetUpPipeMessage::Done as u8]) + .unwrap_or_else(|_| process::exit(1)); + + // Terminate this child + process::exit(0); + } else { + // This is the parent + let ret = unsafe { libc::unshare(flags) }; + if ret != 0 { + return Err(Error::Unshare(std::io::Error::last_os_error())); + } + + // Dropping the other end of the pipes + drop(x_reader); + drop(y_writer); + + // Signal the first child to go ahead and setup the id mappings + x_writer + .write_all(&[IdMapSetUpPipeMessage::Request as u8]) + .unwrap(); + + // Receive the signal that mapping is done. If the child process exits + // before setting up the mapping, closing the pipe before sending the + // message, `read_exact()` will fail with `UnexpectedEof`. + y_reader + .read_exact(&mut output) + .unwrap_or_else(|_| process::exit(1)); + assert_eq!(output[0], IdMapSetUpPipeMessage::Done as u8); + + let mut status = 0_i32; + let _ = unsafe { libc::waitpid(pid, &mut status, 0) }; + + // Set the process inside the user namespace as root + let mut ret = unsafe { libc::setresuid(0, 0, 0) }; + if ret != 0 { + warn!("Couldn't set the process uid as root: {}", ret); + } + ret = unsafe { libc::setresgid(0, 0, 0) }; + if ret != 0 { + warn!("Couldn't set the process gid as root: {}", ret); + } + + let child = util::sfork().map_err(Error::Fork)?; + if child == 0 { + // Second child + self.setup_mounts()?; + Ok(listener) + } else { + // This is the parent + + // The child process drops the `vhost::Listener` after the first + // `accept()`. However, since the parent just waits until the child + // ends, keeping all the FDs open, as well as the socket's FD in a + // listen state. This is problematic because nothing prevents a + // miss-configured VMM to try to connect twice to the same socket + // leaving the VMM waiting forever. So, let's close the listener + // before waiting for the child. + let fd = listener.as_raw_fd(); + + // `vhost::Listener` beside closing the FD, it will remove the socket, if dropped + std::mem::forget(listener); + + // Let's close the FD without removing the socket file + // SAFETY: `fd` is open and nobody owns it + let fd = unsafe { OwnedFd::from_raw_fd(fd) }; + drop(fd); + + util::wait_for_child(child); // This never returns. + } + } + } + + pub fn enter_chroot(&mut self) -> Result<(), Error> { + let c_proc_self_fd = CString::new("/proc/self/fd").unwrap(); + let proc_self_fd = unsafe { libc::open(c_proc_self_fd.as_ptr(), libc::O_PATH) }; + if proc_self_fd < 0 { + return Err(Error::OpenProcSelfFd(std::io::Error::last_os_error())); + } + // Safe because we just opened this fd. + self.proc_self_fd = Some(unsafe { File::from_raw_fd(proc_self_fd) }); + + let c_mountinfo = CString::new("/proc/self/mountinfo").unwrap(); + let mountinfo_fd = unsafe { libc::open(c_mountinfo.as_ptr(), libc::O_RDONLY) }; + if mountinfo_fd < 0 { + return Err(Error::OpenMountinfo(std::io::Error::last_os_error())); + } + // Safe because we just opened this fd. + self.mountinfo_fd = Some(unsafe { File::from_raw_fd(mountinfo_fd) }); + + let c_shared_dir = CString::new(self.shared_dir.clone()).unwrap(); + let ret = unsafe { libc::chroot(c_shared_dir.as_ptr()) }; + if ret != 0 { + return Err(Error::Chroot(std::io::Error::last_os_error())); + } + + let c_root_dir = CString::new("/").unwrap(); + let ret = unsafe { libc::chdir(c_root_dir.as_ptr()) }; + if ret != 0 { + return Err(Error::ChrootChdir(std::io::Error::last_os_error())); + } + + Ok(()) + } + + fn must_drop_supplemental_groups(&self) -> Result { + let uid = unsafe { libc::geteuid() }; + if uid != 0 { + return Ok(false); + } + + // If we are running as root and the system does not support user namespaces, + // we must drop supplemental groups. + if !Path::new("/proc/self/ns/user").exists() { + return Ok(true); + } + + let uid_mmap_data = + fs::read_to_string("/proc/self/uid_map").map_err(Error::DropSupplementalGroups)?; + let uid_map: Vec<_> = uid_mmap_data.split_whitespace().collect(); + + let gid_map_data = + fs::read_to_string("/proc/self/gid_map").map_err(Error::DropSupplementalGroups)?; + let gid_map: Vec<_> = gid_map_data.split_whitespace().collect(); + + let setgroups = + fs::read_to_string("/proc/self/setgroups").map_err(Error::DropSupplementalGroups)?; + + // A single line mapping only has 3 fields, and the 'count' field should + // be 1. + let single_uid_mapping = uid_map.len() == 3 && uid_map[2] == "1"; + let single_gid_mapping = gid_map.len() == 3 && gid_map[2] == "1"; + + Ok(setgroups.trim() != "deny" || !single_uid_mapping || !single_gid_mapping) + } + + fn drop_supplemental_groups(&self) -> Result<(), Error> { + let ngroups = unsafe { libc::getgroups(0, std::ptr::null_mut()) }; + if ngroups < 0 { + return Err(Error::GetSupplementalGroups(std::io::Error::last_os_error())); + } else if ngroups != 0 { + let ret = unsafe { libc::setgroups(0, std::ptr::null()) }; + if ret != 0 { + return Err(Error::DropSupplementalGroups( + std::io::Error::last_os_error(), + )); + } + } + + Ok(()) + } + + /// Set up sandbox, + pub fn enter(&mut self, listener: Listener) -> Result { + let uid = unsafe { libc::geteuid() }; + if uid != 0 && self.sandbox_mode == SandboxMode::Chroot { + return Err(Error::SandboxModeInvalidUID); + } + + if !self.uid_map.is_empty() && (uid == 0 || self.sandbox_mode != SandboxMode::Namespace) { + return Err(Error::SandboxModeInvalidUidMap); + } + + if !self.gid_map.is_empty() && (uid == 0 || self.sandbox_mode != SandboxMode::Namespace) { + return Err(Error::SandboxModeInvalidGidMap); + } + + // We must drop supplemental groups membership if we support switching + // between arbitrary uids/gids, unless the following conditions are met: + // we're not running as root or we are inside a user namespace with only + // one uid and gid mapping and '/proc/self/setgroups' is equal to + // "deny". In both of these cases, no arbitrary uid/gid switching is + // possible and thus there's no need to drop supplemental groups. In + // both of these scenarios calling setgroups() is also not allowed so we + // avoid calling it since we know it will return a privilege error. + let must_drop_supplemental_groups = match self.must_drop_supplemental_groups() { + Ok(must_drop) => must_drop, + Err(error) => { + warn!( + "Failed to determine whether supplemental groups must be dropped: {error}; \ + defaulting to trying to drop supplemental groups" + ); + true + } + }; + + if must_drop_supplemental_groups { + self.drop_supplemental_groups()?; + } + + match self.sandbox_mode { + SandboxMode::Namespace => self.enter_namespace(listener), + SandboxMode::Chroot => self.enter_chroot().and(Ok(listener)), + SandboxMode::None => Ok(listener), + } + } + + pub fn get_proc_self_fd(&mut self) -> Option { + self.proc_self_fd.take() + } + + pub fn get_mountinfo_fd(&mut self) -> Option { + self.mountinfo_fd.take() + } + + pub fn get_root_dir(&self) -> String { + match self.sandbox_mode { + SandboxMode::Namespace | SandboxMode::Chroot => "/".to_string(), + SandboxMode::None => self.shared_dir.clone(), + } + } + + /// Return the prefix to strip from /proc/self/mountinfo entries to get paths that are actually + /// accessible in our sandbox + pub fn get_mountinfo_prefix(&self) -> Option { + match self.sandbox_mode { + SandboxMode::Namespace | SandboxMode::None => None, + SandboxMode::Chroot => Some(self.shared_dir.clone()), + } + } +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/seccomp.rs b/vendor/cube-hypervisor/virtiofsd/src/seccomp.rs new file mode 100644 index 0000000..ce31412 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/seccomp.rs @@ -0,0 +1,215 @@ +// Copyright 2020 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use libseccomp_sys::{ + seccomp_init, seccomp_load, seccomp_release, seccomp_rule_add, SCMP_ACT_ALLOW, + SCMP_ACT_KILL_PROCESS, SCMP_ACT_LOG, SCMP_ACT_TRAP, +}; +use std::convert::TryInto; +use std::{error, fmt}; + +#[derive(Debug)] +pub enum Error { + /// Error allowing a syscall + AllowSeccompSyscall(i32), + + /// Cannot load seccomp filter + LoadSeccompFilter, + + /// Cannot initialize seccomp context + InitSeccompContext, +} + +impl error::Error for Error {} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "virtiofsd_seccomp_error: {self:?}") + } +} + +#[derive(Copy, Clone, Debug)] +pub enum SeccompAction { + Allow, + Kill, + Log, + Trap, +} + +impl From for u32 { + fn from(action: SeccompAction) -> u32 { + match action { + SeccompAction::Allow => SCMP_ACT_ALLOW, + SeccompAction::Kill => SCMP_ACT_KILL_PROCESS, + SeccompAction::Log => SCMP_ACT_LOG, + SeccompAction::Trap => SCMP_ACT_TRAP, + } + } +} + +macro_rules! allow_syscall { + ($ctx:ident, $syscall:expr) => { + let syscall_nr: i32 = $syscall.try_into().unwrap(); + let ret = unsafe { seccomp_rule_add($ctx, SCMP_ACT_ALLOW, syscall_nr, 0) }; + if ret != 0 { + return Err(Error::AllowSeccompSyscall(syscall_nr)); + } + }; +} + +pub fn enable_seccomp(action: SeccompAction, allow_remote_logging: bool) -> Result<(), Error> { + let ctx = unsafe { seccomp_init(action.into()) }; + if ctx.is_null() { + return Err(Error::InitSeccompContext); + } + + allow_syscall!(ctx, libc::SYS_accept4); + allow_syscall!(ctx, libc::SYS_brk); + allow_syscall!(ctx, libc::SYS_capget); // For CAP_FSETID + allow_syscall!(ctx, libc::SYS_capset); + allow_syscall!(ctx, libc::SYS_clock_gettime); + allow_syscall!(ctx, libc::SYS_clone); + allow_syscall!(ctx, libc::SYS_clone3); + allow_syscall!(ctx, libc::SYS_close); + allow_syscall!(ctx, libc::SYS_copy_file_range); + allow_syscall!(ctx, libc::SYS_dup); + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64" + ))] + allow_syscall!(ctx, libc::SYS_epoll_create); + allow_syscall!(ctx, libc::SYS_epoll_create1); + allow_syscall!(ctx, libc::SYS_epoll_ctl); + allow_syscall!(ctx, libc::SYS_epoll_pwait); + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64" + ))] + allow_syscall!(ctx, libc::SYS_epoll_wait); + allow_syscall!(ctx, libc::SYS_eventfd2); + allow_syscall!(ctx, libc::SYS_exit); + allow_syscall!(ctx, libc::SYS_exit_group); + allow_syscall!(ctx, libc::SYS_fallocate); + allow_syscall!(ctx, libc::SYS_fchdir); + allow_syscall!(ctx, libc::SYS_fchmod); + allow_syscall!(ctx, libc::SYS_fchmodat); + allow_syscall!(ctx, libc::SYS_fchownat); + allow_syscall!(ctx, libc::SYS_fcntl); + allow_syscall!(ctx, libc::SYS_fdatasync); + allow_syscall!(ctx, libc::SYS_fgetxattr); + allow_syscall!(ctx, libc::SYS_flistxattr); + allow_syscall!(ctx, libc::SYS_flock); + allow_syscall!(ctx, libc::SYS_fremovexattr); + allow_syscall!(ctx, libc::SYS_fsetxattr); + #[cfg(not(target_arch = "loongarch64"))] + allow_syscall!(ctx, libc::SYS_fstat); + #[cfg(any(target_arch = "s390x", target_arch = "powerpc64"))] + allow_syscall!(ctx, libc::SYS_fstatfs64); + allow_syscall!(ctx, libc::SYS_fstatfs); + allow_syscall!(ctx, libc::SYS_fsync); + allow_syscall!(ctx, libc::SYS_ftruncate); + allow_syscall!(ctx, libc::SYS_futex); + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64" + ))] + allow_syscall!(ctx, libc::SYS_getdents); + allow_syscall!(ctx, libc::SYS_getdents64); + allow_syscall!(ctx, libc::SYS_getegid); + allow_syscall!(ctx, libc::SYS_geteuid); + allow_syscall!(ctx, libc::SYS_getpid); + allow_syscall!(ctx, libc::SYS_getrandom); + allow_syscall!(ctx, libc::SYS_gettid); + allow_syscall!(ctx, libc::SYS_gettimeofday); + allow_syscall!(ctx, libc::SYS_getxattr); + allow_syscall!(ctx, libc::SYS_linkat); + allow_syscall!(ctx, libc::SYS_listxattr); + allow_syscall!(ctx, libc::SYS_lseek); + allow_syscall!(ctx, libc::SYS_madvise); + allow_syscall!(ctx, libc::SYS_membarrier); + allow_syscall!(ctx, libc::SYS_mkdirat); + allow_syscall!(ctx, libc::SYS_mknodat); + allow_syscall!(ctx, libc::SYS_mmap); + allow_syscall!(ctx, libc::SYS_mprotect); + allow_syscall!(ctx, libc::SYS_mremap); + allow_syscall!(ctx, libc::SYS_munmap); + allow_syscall!(ctx, libc::SYS_name_to_handle_at); + #[cfg(not(target_arch = "loongarch64"))] + allow_syscall!(ctx, libc::SYS_newfstatat); + #[cfg(target_arch = "powerpc64")] + allow_syscall!(ctx, libc::SYS__llseek); + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64" + ))] + allow_syscall!(ctx, libc::SYS_open); + allow_syscall!(ctx, libc::SYS_openat); + allow_syscall!(ctx, libc::SYS_openat2); + allow_syscall!(ctx, libc::SYS_open_by_handle_at); + allow_syscall!(ctx, libc::SYS_prctl); // TODO restrict to just PR_SET_NAME? + allow_syscall!(ctx, libc::SYS_preadv); + allow_syscall!(ctx, libc::SYS_pread64); + allow_syscall!(ctx, libc::SYS_pwritev2); + allow_syscall!(ctx, libc::SYS_pwrite64); + allow_syscall!(ctx, libc::SYS_read); + allow_syscall!(ctx, libc::SYS_readlinkat); + allow_syscall!(ctx, libc::SYS_recvmsg); + #[cfg(not(any(target_arch = "loongarch64", target_arch = "riscv64")))] + allow_syscall!(ctx, libc::SYS_renameat); + allow_syscall!(ctx, libc::SYS_renameat2); + allow_syscall!(ctx, libc::SYS_removexattr); + #[cfg(target_env = "gnu")] + allow_syscall!(ctx, libc::SYS_rseq); + allow_syscall!(ctx, libc::SYS_rt_sigaction); + allow_syscall!(ctx, libc::SYS_rt_sigprocmask); + allow_syscall!(ctx, libc::SYS_rt_sigreturn); + allow_syscall!(ctx, libc::SYS_sched_getaffinity); // used by thread_pool + allow_syscall!(ctx, libc::SYS_sched_yield); + allow_syscall!(ctx, libc::SYS_sendmsg); + allow_syscall!(ctx, libc::SYS_setgroups); + allow_syscall!(ctx, libc::SYS_setresgid); + allow_syscall!(ctx, libc::SYS_setresuid); + //allow_syscall!(ctx, libc::SYS_setresgid32); Needed on some platforms, + //allow_syscall!(ctx, libc::SYS_setresuid32); Needed on some platforms + allow_syscall!(ctx, libc::SYS_set_robust_list); + allow_syscall!(ctx, libc::SYS_setxattr); + allow_syscall!(ctx, libc::SYS_sigaltstack); + #[cfg(target_arch = "s390x")] + allow_syscall!(ctx, libc::SYS_sigreturn); + allow_syscall!(ctx, libc::SYS_statx); + allow_syscall!(ctx, libc::SYS_symlinkat); + allow_syscall!(ctx, libc::SYS_syncfs); + #[cfg(target_arch = "x86_64")] + allow_syscall!(ctx, libc::SYS_time); // Rarely needed, except on static builds + allow_syscall!(ctx, libc::SYS_tgkill); + allow_syscall!(ctx, libc::SYS_umask); + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64" + ))] + allow_syscall!(ctx, libc::SYS_unlink); + allow_syscall!(ctx, libc::SYS_unlinkat); + allow_syscall!(ctx, libc::SYS_unshare); + allow_syscall!(ctx, libc::SYS_utimensat); + allow_syscall!(ctx, libc::SYS_write); + allow_syscall!(ctx, libc::SYS_writev); + + if allow_remote_logging { + allow_syscall!(ctx, libc::SYS_sendto); // Required by syslog + } + + let ret = unsafe { seccomp_load(ctx) }; + if ret != 0 { + return Err(Error::LoadSeccompFilter); + } + + unsafe { seccomp_release(ctx) }; + + Ok(()) +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/server.rs b/vendor/cube-hypervisor/virtiofsd/src/server.rs new file mode 100644 index 0000000..89a9d96 --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/server.rs @@ -0,0 +1,2211 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use super::fs_cache_req_handler::FsCacheReqHandler; +use crate::descriptor_utils::{Reader, Writer}; +use crate::filesystem::{ + Context, DirEntry, DirEntry2, DirectoryIterator, Entry, Extensions, FileSystem, GetxattrReply, + ListxattrReply, SecContext, SerializableFileSystem, ZeroCopyReader, ZeroCopyWriter, +}; +use crate::fuse::*; +use crate::passthrough::util::einval; +use crate::{oslib, Error, Result}; +use anyhow::anyhow; +use rate_limiter::{BucketReduction, RateLimiter, TokenType}; +use std::convert::{TryFrom, TryInto}; +use std::ffi::{CStr, CString}; +use std::fs::File; +use std::io::{self, Read, Write}; +use std::mem::{size_of, MaybeUninit}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::Duration; +use vm_memory::ByteValued; + +const FUSE_BUFFER_HEADER_SIZE: u32 = 0x1000; +const MAX_BUFFER_SIZE: u32 = 1 << 20; +const DIRENT_PADDING: [u8; 8] = [0; 8]; + +const CURRENT_DIR_CSTR: &[u8] = b"."; +const PARENT_DIR_CSTR: &[u8] = b".."; + +struct ZcReader<'a>(Reader<'a>); + +impl<'a> ZeroCopyReader for ZcReader<'a> { + fn read_to( + &mut self, + f: &File, + count: usize, + off: u64, + flags: Option, + ) -> io::Result { + self.0.read_to_at(f, count, off, flags) + } +} + +struct ZcWriter<'a>(Writer<'a>); + +impl<'a> ZeroCopyWriter for ZcWriter<'a> { + fn write_from(&mut self, f: &File, count: usize, off: u64) -> io::Result { + self.0.write_from_at(f, count, off) + } +} + +use std::collections::HashMap; +use std::io::ErrorKind; + +use crate::passthrough::stat::{self, statx}; +use crate::passthrough::util::openat; +use std::ffi::OsStr; +use std::path::Path; + +pub fn get_basename_from_path(path: &str) -> Option<&str> { + let path_obj = Path::new(path); + path_obj.file_name().and_then(OsStr::to_str) +} + +pub struct FilterList { + root_ino: u64, + allowed_dirs: HashMap, + root_dirents: Vec, +} + +impl FilterList { + // Init filter base whitelist. + fn init(&mut self, whitelist: &Option>) -> Result<()> { + let mut dic = HashMap::new(); + let mut dirents = Vec::::new(); + let mut index = 2; + + dirents.push(DirEntry2 { + ino: self.root_ino, + offset: 1, + type_: libc::DT_DIR as u32, + name: ".".to_string(), + }); + dirents.push(DirEntry2 { + ino: 1001, + offset: 2, + type_: libc::DT_DIR as u32, + name: "..".to_string(), + }); + // dir should be absolute path + match whitelist { + Some(dirs) => { + // Sort and Dedup dirs. + let mut dirs = dirs.clone(); + dirs.sort(); + dirs.dedup(); + + for dir in dirs { + if dir.is_empty() { + continue; + } + let dir_name = match get_basename_from_path(&dir) { + Some(path) => path, + _ => { + warn!("Invalid path name: {:?}", dir); + continue; + } + }; + let path_fd = openat( + &libc::AT_FDCWD, + &dir, + libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC, + ) + .map_err(|e| { + Error::InvalidFilter(anyhow!("Failed to openat file {:?}: {:?}", dir, e)) + })?; + let st = statx(&path_fd, None).map_err(|e| { + Error::InvalidFilter(anyhow!( + "Failed to statx file {:?} fd {:?}: {:?}", + dir, + path_fd, + e + )) + })?; + let ino = st.st.st_ino; + dic.insert(dir_name.to_string(), (dir.clone(), st)); + index += 1; + dirents.push(DirEntry2 { + ino, + offset: index, + type_: libc::DT_DIR as u32, + name: dir_name.to_string(), + }); + } + } + None => { + info!("allowed_dirs is None"); + } + } + + self.allowed_dirs = dic; + self.root_dirents = dirents; + + Ok(()) + } + + pub fn new(root_ino: u64, whitelist: &Option>) -> Result { + let mut filter = FilterList { + root_ino, + allowed_dirs: HashMap::new(), + root_dirents: Vec::::new(), + }; + + filter.init(whitelist)?; + + Ok(filter) + } + + // update filter with new whitelist. + pub fn update(&mut self, whitelist: &Option>) -> Result<()> { + self.init(whitelist)?; + + Ok(()) + } + + // whether allowd_dirs is empty. + pub fn is_empty(&self) -> bool { + self.allowed_dirs.is_empty() + } + + fn basename_paths(&self) -> HashMap { + self.allowed_dirs + .iter() + .map(|(name, (path, _))| (name.clone(), path.clone())) + .collect() + } + + // return allow dire entry. + pub fn get_allow_dir(&self, key: String) -> Result<(String, stat::StatExt)> { + if !self.allowed_dirs.contains_key(&key) { + return Err(Error::DisAllowed); + } + + Ok(self.allowed_dirs[&key].clone()) + } + + // return root dirent. + pub fn get_root_dirents(&self) -> &Vec { + &self.root_dirents + } +} + +pub struct Server { + fs: F, + options: AtomicU64, + root_filter: Arc>, + remap_filter_enabled: AtomicBool, +} + +impl Server { + pub fn new(fs: F, whitelist: &Option>) -> Result> { + let root_ino = fs.get_root_ino().map_err(Error::InternalError)?; + let filter = FilterList::new(root_ino, whitelist)?; + + Ok(Server { + fs, + options: AtomicU64::new(FsOptions::empty().bits()), + root_filter: Arc::new(Mutex::new(filter)), + remap_filter_enabled: AtomicBool::new(false), + }) + } + + /// Enable or disable the filter-path remap feature used during migration + /// restore. When disabled (the default), the backend will keep the + /// legacy behaviour and skip populating the remap map before deserializing + /// device state. + pub fn set_remap_filter_enabled(&self, enabled: bool) { + self.remap_filter_enabled.store(enabled, Ordering::Relaxed); + } + + pub fn is_remap_filter_enabled(&self) -> bool { + self.remap_filter_enabled.load(Ordering::Relaxed) + } + + pub fn update_filter(&self, whitelist: &Option>) -> Result<()> { + self.root_filter + .as_ref() + .lock() + .unwrap() + .update(whitelist)?; + + Ok(()) + } + + #[allow(clippy::cognitive_complexity)] + pub fn handle_message( + &self, + mut r: Reader, + w: Writer, + vu_req: Option<&mut T>, + rl: &mut Option, + rlret: &mut BucketReduction, + rltyp: &mut TokenType, + ) -> Result { + let in_header: InHeader = r.read_obj().map_err(Error::DecodeMessage)?; + + if in_header.len > (MAX_BUFFER_SIZE + FUSE_BUFFER_HEADER_SIZE) { + return reply_error( + io::Error::from_raw_os_error(libc::ENOMEM), + in_header.unique, + w, + ); + } + + if let Ok(opcode) = Opcode::try_from(in_header.opcode) { + debug!( + "Received request: opcode={:?} ({}), inode={}, unique={}, pid={}", + opcode, in_header.opcode, in_header.nodeid, in_header.unique, in_header.pid + ); + match opcode { + Opcode::Lookup => self.lookup(in_header, r, w), + Opcode::Forget => self.forget(in_header, r), // No reply. + Opcode::Getattr => self.getattr(in_header, r, w), + Opcode::Setattr => self.setattr(in_header, r, w), + Opcode::Readlink => self.readlink(in_header, w), + Opcode::Symlink => self.symlink(in_header, r, w), + Opcode::Mknod => self.mknod(in_header, r, w), + Opcode::Mkdir => self.mkdir(in_header, r, w), + Opcode::Unlink => self.unlink(in_header, r, w), + Opcode::Rmdir => self.rmdir(in_header, r, w), + Opcode::Rename => self.rename(in_header, r, w), + Opcode::Link => self.link(in_header, r, w), + Opcode::Open => self.open(in_header, r, w), + Opcode::Read => self.read(in_header, r, w, rl, rlret, rltyp), + Opcode::Write => self.write(in_header, r, w), + Opcode::Statfs => self.statfs(in_header, w), + Opcode::Release => self.release(in_header, r, w), + Opcode::Fsync => self.fsync(in_header, r, w), + Opcode::Setxattr => self.setxattr(in_header, r, w), + Opcode::Getxattr => self.getxattr(in_header, r, w), + Opcode::Listxattr => self.listxattr(in_header, r, w), + Opcode::Removexattr => self.removexattr(in_header, r, w), + Opcode::Flush => self.flush(in_header, r, w), + Opcode::Init => self.init(in_header, r, w), + Opcode::Opendir => self.opendir(in_header, r, w), + Opcode::Readdir => self.readdir(in_header, r, w), + Opcode::Releasedir => self.releasedir(in_header, r, w), + Opcode::Fsyncdir => self.fsyncdir(in_header, r, w), + Opcode::Getlk => self.getlk(in_header, r, w), + Opcode::Setlk => self.setlk(in_header, r, w), + Opcode::Setlkw => self.setlkw(in_header, r, w), + Opcode::Access => self.access(in_header, r, w), + Opcode::Create => self.create(in_header, r, w), + Opcode::Interrupt => Ok(self.interrupt(in_header)), + Opcode::Bmap => self.bmap(in_header, r, w), + Opcode::Destroy => Ok(self.destroy()), + Opcode::Ioctl => self.ioctl(in_header, r, w), + Opcode::Poll => self.poll(in_header, r, w), + Opcode::NotifyReply => self.notify_reply(in_header, r, w), + Opcode::BatchForget => self.batch_forget(in_header, r, w), + Opcode::Fallocate => self.fallocate(in_header, r, w), + Opcode::Readdirplus => self.readdirplus(in_header, r, w), + Opcode::Rename2 => self.rename2(in_header, r, w), + Opcode::Lseek => self.lseek(in_header, r, w), + Opcode::CopyFileRange => self.copyfilerange(in_header, r, w), + Opcode::SetupMapping => self.setupmapping(in_header, r, w, vu_req), + Opcode::RemoveMapping => self.removemapping(in_header, r, w, vu_req), + Opcode::Syncfs => self.syncfs(in_header, w), + Opcode::TmpFile => self.tmpfile(in_header, r, w), + } + } else { + debug!( + "Received unknown request: opcode={}, inode={}", + in_header.opcode, in_header.nodeid + ); + reply_error( + io::Error::from_raw_os_error(libc::ENOSYS), + in_header.unique, + w, + ) + } + } + + fn setupmapping( + &self, + in_header: InHeader, + mut r: Reader, + w: Writer, + vu_req: Option<&mut T>, + ) -> Result { + if let Some(req) = vu_req { + let SetupmappingIn { + fh, + foffset, + len, + flags, + moffset, + } = r.read_obj().map_err(Error::DecodeMessage)?; + + match self.fs.setupmapping( + Context::from(in_header), + in_header.nodeid.into(), + fh.into(), + foffset, + len, + flags, + moffset, + req, + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } else { + reply_error( + io::Error::from_raw_os_error(libc::EINVAL), + in_header.unique, + w, + ) + } + } + + fn removemapping( + &self, + in_header: InHeader, + mut r: Reader, + w: Writer, + vu_req: Option<&mut T>, + ) -> Result { + if let Some(req) = vu_req { + let RemovemappingIn { count } = r.read_obj().map_err(Error::DecodeMessage)?; + + if let Some(size) = (count as usize).checked_mul(size_of::()) { + if size > MAX_BUFFER_SIZE as usize { + return reply_error( + io::Error::from_raw_os_error(libc::ENOMEM), + in_header.unique, + w, + ); + } + } else { + return reply_error( + io::Error::from_raw_os_error(libc::EOVERFLOW), + in_header.unique, + w, + ); + } + + let mut requests = Vec::with_capacity(count as usize); + for _ in 0..count { + requests.push( + r.read_obj::() + .map_err(Error::DecodeMessage)?, + ); + } + + match self + .fs + .removemapping(Context::from(in_header), requests, req) + { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } else { + reply_error( + io::Error::from_raw_os_error(libc::EINVAL), + in_header.unique, + w, + ) + } + } + + // When an allow-dir whitelist is configured, the FUSE root is a synthetic, filtered directory: + // the guest may only traverse into whitelisted entries (LOOKUP) and list them (READDIR/ + // READDIRPLUS). It must never create, delete, rename, link, or change metadata directly on the + // real backend root. Returns true when `nodeid` is that filtered root so callers reject the op. + fn is_filtered_root(&self, nodeid: u64) -> bool { + nodeid == ROOT_ID && !self.root_filter.as_ref().lock().unwrap().is_empty() + } + + fn lookup(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let namelen = (in_header.len as usize) + .checked_sub(size_of::()) + .ok_or(Error::InvalidHeaderLength)?; + + let mut buf = vec![0u8; namelen]; + + r.read_exact(&mut buf).map_err(Error::DecodeMessage)?; + + let name = bytes_to_cstr(buf.as_ref())?; + + let par_id = in_header.nodeid; + let mut f_info = None; + if par_id == ROOT_ID { + let filter = self.root_filter.as_ref().lock().unwrap(); + if !filter.is_empty() { + match filter.get_allow_dir(name.to_str().unwrap().to_string()) { + Ok(entry) => f_info = Some(entry), + Err(_) => { + return reply_error(ErrorKind::NotFound.into(), in_header.unique, w); + } + } + } + } + + match self.fs.lookup( + Context::from(in_header), + in_header.nodeid.into(), + name, + f_info, + ) { + Ok(entry) => { + let out = EntryOut::from(entry); + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn forget(&self, in_header: InHeader, mut r: Reader) -> Result { + let ForgetIn { nlookup } = r.read_obj().map_err(Error::DecodeMessage)?; + + self.fs + .forget(Context::from(in_header), in_header.nodeid.into(), nlookup); + + // There is no reply for forget messages. + Ok(0) + } + + fn getattr(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let GetattrIn { flags, fh, .. } = r.read_obj().map_err(Error::DecodeMessage)?; + + let handle = if (flags & GETATTR_FH) != 0 { + Some(fh.into()) + } else { + None + }; + + match self + .fs + .getattr(Context::from(in_header), in_header.nodeid.into(), handle) + { + Ok((st, timeout)) => { + let out = AttrOut { + attr_valid: timeout.as_secs(), + attr_valid_nsec: timeout.subsec_nanos(), + dummy: 0, + attr: st.into(), + }; + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn setattr(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + if self.is_filtered_root(in_header.nodeid) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + let setattr_in: SetattrIn = r.read_obj().map_err(Error::DecodeMessage)?; + + let handle = if setattr_in.valid & FATTR_FH != 0 { + Some(setattr_in.fh.into()) + } else { + None + }; + + let valid = SetattrValid::from_bits_truncate(setattr_in.valid); + + let st: libc::stat64 = setattr_in.into(); + + match self.fs.setattr( + Context::from(in_header), + in_header.nodeid.into(), + st, + handle, + valid, + ) { + Ok((st, timeout)) => { + let out = AttrOut { + attr_valid: timeout.as_secs(), + attr_valid_nsec: timeout.subsec_nanos(), + dummy: 0, + attr: st.into(), + }; + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn readlink(&self, in_header: InHeader, w: Writer) -> Result { + match self + .fs + .readlink(Context::from(in_header), in_header.nodeid.into()) + { + Ok(linkname) => { + // We need to disambiguate the option type here even though it is `None`. + reply_ok(None::, Some(&linkname), in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn symlink(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + if self.is_filtered_root(in_header.nodeid) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + // Unfortunately the name and linkname are encoded one after another and + // separated by a nul character. + let len = (in_header.len as usize) + .checked_sub(size_of::()) + .ok_or(Error::InvalidHeaderLength)?; + let mut buf = vec![0; len]; + + r.read_exact(&mut buf).map_err(Error::DecodeMessage)?; + + let mut components = buf.split_inclusive(|c| *c == b'\0'); + + let name = components.next().ok_or(Error::MissingParameter)?; + let linkname = components.next().ok_or(Error::MissingParameter)?; + + let options = FsOptions::from_bits_truncate(self.options.load(Ordering::Relaxed)); + + let extensions = get_extensions(options, name.len() + linkname.len(), buf.as_slice())?; + + match self.fs.symlink( + Context::from(in_header), + bytes_to_cstr(linkname)?, + in_header.nodeid.into(), + bytes_to_cstr(name)?, + extensions, + ) { + Ok(entry) => { + let out = EntryOut::from(entry); + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn mknod(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + if self.is_filtered_root(in_header.nodeid) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + let MknodIn { + mode, rdev, umask, .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + + let remaining_len = (in_header.len as usize) + .checked_sub(size_of::()) + .and_then(|l| l.checked_sub(size_of::())) + .ok_or(Error::InvalidHeaderLength)?; + let mut buf = vec![0; remaining_len]; + + r.read_exact(&mut buf).map_err(Error::DecodeMessage)?; + let mut components = buf.split_inclusive(|c| *c == b'\0'); + let name = components.next().ok_or(Error::MissingParameter)?; + + let options = FsOptions::from_bits_truncate(self.options.load(Ordering::Relaxed)); + + let extensions = get_extensions(options, name.len(), buf.as_slice())?; + + match self.fs.mknod( + Context::from(in_header), + in_header.nodeid.into(), + bytes_to_cstr(name)?, + mode, + rdev, + umask, + extensions, + ) { + Ok(entry) => { + let out = EntryOut::from(entry); + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn mkdir(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + if self.is_filtered_root(in_header.nodeid) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + let MkdirIn { mode, umask } = r.read_obj().map_err(Error::DecodeMessage)?; + + let remaining_len = (in_header.len as usize) + .checked_sub(size_of::()) + .and_then(|l| l.checked_sub(size_of::())) + .ok_or(Error::InvalidHeaderLength)?; + let mut buf = vec![0; remaining_len]; + + r.read_exact(&mut buf).map_err(Error::DecodeMessage)?; + let mut components = buf.split_inclusive(|c| *c == b'\0'); + let name = components.next().ok_or(Error::MissingParameter)?; + + let options = FsOptions::from_bits_truncate(self.options.load(Ordering::Relaxed)); + + let extensions = get_extensions(options, name.len(), buf.as_slice())?; + + match self.fs.mkdir( + Context::from(in_header), + in_header.nodeid.into(), + bytes_to_cstr(name)?, + mode, + umask, + extensions, + ) { + Ok(entry) => { + let out = EntryOut::from(entry); + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn unlink(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + if self.is_filtered_root(in_header.nodeid) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + let namelen = (in_header.len as usize) + .checked_sub(size_of::()) + .ok_or(Error::InvalidHeaderLength)?; + let mut name = vec![0; namelen]; + + r.read_exact(&mut name).map_err(Error::DecodeMessage)?; + + match self.fs.unlink( + Context::from(in_header), + in_header.nodeid.into(), + bytes_to_cstr(&name)?, + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn rmdir(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + if self.is_filtered_root(in_header.nodeid) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + let namelen = (in_header.len as usize) + .checked_sub(size_of::()) + .ok_or(Error::InvalidHeaderLength)?; + let mut name = vec![0; namelen]; + + r.read_exact(&mut name).map_err(Error::DecodeMessage)?; + + match self.fs.rmdir( + Context::from(in_header), + in_header.nodeid.into(), + bytes_to_cstr(&name)?, + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn do_rename( + &self, + in_header: InHeader, + msg_size: usize, + newdir: u64, + flags: u32, + mut r: Reader, + w: Writer, + ) -> Result { + // Reject renames whose source (`in_header.nodeid`) or destination (`newdir`) parent is the + // filtered root, so the guest can neither remove from nor create in the real backend root. + if self.is_filtered_root(in_header.nodeid) || self.is_filtered_root(newdir) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + let buflen = (in_header.len as usize) + .checked_sub(size_of::()) + .and_then(|l| l.checked_sub(msg_size)) + .ok_or(Error::InvalidHeaderLength)?; + let mut buf = vec![0; buflen]; + + r.read_exact(&mut buf).map_err(Error::DecodeMessage)?; + + // We want to include the '\0' byte in the first slice. + let split_pos = buf + .iter() + .position(|c| *c == b'\0') + .map(|p| p + 1) + .ok_or(Error::MissingParameter)?; + + let (oldname, newname) = buf.split_at(split_pos); + + match self.fs.rename( + Context::from(in_header), + in_header.nodeid.into(), + bytes_to_cstr(oldname)?, + newdir.into(), + bytes_to_cstr(newname)?, + flags, + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn rename(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let RenameIn { newdir } = r.read_obj().map_err(Error::DecodeMessage)?; + + self.do_rename(in_header, size_of::(), newdir, 0, r, w) + } + + fn rename2(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let Rename2In { newdir, flags, .. } = r.read_obj().map_err(Error::DecodeMessage)?; + + let flags = + flags & (libc::RENAME_EXCHANGE | libc::RENAME_NOREPLACE | libc::RENAME_WHITEOUT); + + self.do_rename(in_header, size_of::(), newdir, flags, r, w) + } + + fn link(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + // `in_header.nodeid` is the destination parent; deny hardlinks into the filtered root. + if self.is_filtered_root(in_header.nodeid) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + let LinkIn { oldnodeid } = r.read_obj().map_err(Error::DecodeMessage)?; + + let namelen = (in_header.len as usize) + .checked_sub(size_of::()) + .and_then(|l| l.checked_sub(size_of::())) + .ok_or(Error::InvalidHeaderLength)?; + let mut name = vec![0; namelen]; + + r.read_exact(&mut name).map_err(Error::DecodeMessage)?; + + match self.fs.link( + Context::from(in_header), + oldnodeid.into(), + in_header.nodeid.into(), + bytes_to_cstr(&name)?, + ) { + Ok(entry) => { + let out = EntryOut::from(entry); + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn open(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let OpenIn { + flags, open_flags, .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + + let kill_priv = open_flags & OPEN_KILL_SUIDGID != 0; + + match self.fs.open( + Context::from(in_header), + in_header.nodeid.into(), + kill_priv, + flags, + ) { + Ok((handle, opts)) => { + let out = OpenOut { + fh: handle.map(Into::into).unwrap_or(0), + open_flags: opts.bits(), + ..Default::default() + }; + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn read( + &self, + in_header: InHeader, + mut r: Reader, + mut w: Writer, + rl: &mut Option, + rlret: &mut BucketReduction, + rltyp: &mut TokenType, + ) -> Result { + let ReadIn { + fh, + offset, + size, + read_flags, + lock_owner, + flags, + .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + + let _max_size = size_of::() + MAX_BUFFER_SIZE as usize; + if let Some(rate_limiter) = rl { + // If limiter.consume() fails it means there is no more TokenType::Ops + // budget and rate limiting is in effect. + *rlret = rate_limiter.consume(1, TokenType::Ops); + match rlret { + BucketReduction::Success => {} + _ => { + *rltyp = TokenType::Ops; + return Ok(0); + } + } + + *rlret = rate_limiter.consume(size as u64, TokenType::Bytes); + match rlret { + BucketReduction::Failure => { + rate_limiter.manual_replenish(1, TokenType::Ops); + *rltyp = TokenType::Bytes; + return Ok(0); + } + _ => {} + } + } + + let owner = if read_flags & READ_LOCKOWNER != 0 { + Some(lock_owner) + } else { + None + }; + + // Split the writer into 2 pieces: one for the `OutHeader` and the rest for the data. + let data_writer = ZcWriter(w.split_at(size_of::()).unwrap()); + + match self.fs.read( + Context::from(in_header), + in_header.nodeid.into(), + fh.into(), + data_writer, + size, + offset, + owner, + flags, + ) { + Ok(count) => { + // Don't use `reply_ok` because we need to set a custom size length for the + // header. + let out = OutHeader { + len: (size_of::() + count) as u32, + error: 0, + unique: in_header.unique, + }; + + debug!("Replying OK, header: {:?}", out); + w.write_all(out.as_slice()).map_err(Error::EncodeMessage)?; + Ok(out.len as usize) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn write(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let WriteIn { + fh, + offset, + size, + write_flags, + lock_owner, + flags, + .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + + let owner = if write_flags & WRITE_LOCKOWNER != 0 { + Some(lock_owner) + } else { + None + }; + + let delayed_write = write_flags & WRITE_CACHE != 0; + let kill_priv = write_flags & WRITE_KILL_PRIV != 0; + + let data_reader = ZcReader(r); + + match self.fs.write( + Context::from(in_header), + in_header.nodeid.into(), + fh.into(), + data_reader, + size, + offset, + owner, + delayed_write, + kill_priv, + flags, + ) { + Ok(count) => { + let out = WriteOut { + size: count as u32, + ..Default::default() + }; + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn statfs(&self, in_header: InHeader, w: Writer) -> Result { + match self + .fs + .statfs(Context::from(in_header), in_header.nodeid.into()) + { + Ok(st) => reply_ok(Some(Kstatfs::from(st)), None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn release(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let ReleaseIn { + fh, + flags, + release_flags, + lock_owner, + } = r.read_obj().map_err(Error::DecodeMessage)?; + + let flush = release_flags & RELEASE_FLUSH != 0; + let flock_release = release_flags & RELEASE_FLOCK_UNLOCK != 0; + let lock_owner = if flush || flock_release { + Some(lock_owner) + } else { + None + }; + + match self.fs.release( + Context::from(in_header), + in_header.nodeid.into(), + flags, + fh.into(), + flush, + flock_release, + lock_owner, + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn fsync(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let FsyncIn { + fh, fsync_flags, .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + let datasync = fsync_flags & 0x1 != 0; + + match self.fs.fsync( + Context::from(in_header), + in_header.nodeid.into(), + datasync, + fh.into(), + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn setxattr(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + if self.is_filtered_root(in_header.nodeid) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + let options = FsOptions::from_bits_truncate(self.options.load(Ordering::Relaxed)); + let ( + SetxattrIn { + size, + flags, + setxattr_flags, + .. + }, + setxattrin_size, + ) = if options.contains(FsOptions::SETXATTR_EXT) { + ( + r.read_obj().map_err(Error::DecodeMessage)?, + size_of::(), + ) + } else { + let SetxattrInCompat { size, flags } = r.read_obj().map_err(Error::DecodeMessage)?; + ( + SetxattrIn { + size, + flags, + setxattr_flags: 0, + padding: 0, + }, + size_of::(), + ) + }; + + // The name and value and encoded one after another and separated by a '\0' character. + let len = (in_header.len as usize) + .checked_sub(size_of::()) + .and_then(|l| l.checked_sub(setxattrin_size)) + .ok_or(Error::InvalidHeaderLength)?; + let mut buf = vec![0; len]; + + r.read_exact(&mut buf).map_err(Error::DecodeMessage)?; + + // We want to include the '\0' byte in the first slice. + let split_pos = buf + .iter() + .position(|c| *c == b'\0') + .map(|p| p + 1) + .ok_or(Error::MissingParameter)?; + + let (name, value) = buf.split_at(split_pos); + + if size != value.len() as u32 { + return Err(Error::InvalidXattrSize((size, value.len()))); + } + + match self.fs.setxattr( + Context::from(in_header), + in_header.nodeid.into(), + bytes_to_cstr(name)?, + value, + flags, + SetxattrFlags::from_bits_truncate(setxattr_flags), + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn getxattr(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let GetxattrIn { size, .. } = r.read_obj().map_err(Error::DecodeMessage)?; + + let namelen = (in_header.len as usize) + .checked_sub(size_of::()) + .and_then(|l| l.checked_sub(size_of::())) + .ok_or(Error::InvalidHeaderLength)?; + let mut name = vec![0; namelen]; + + r.read_exact(&mut name).map_err(Error::DecodeMessage)?; + + if size > MAX_BUFFER_SIZE { + return reply_error( + io::Error::from_raw_os_error(libc::ENOMEM), + in_header.unique, + w, + ); + } + + match self.fs.getxattr( + Context::from(in_header), + in_header.nodeid.into(), + bytes_to_cstr(&name)?, + size, + ) { + Ok(GetxattrReply::Value(val)) => reply_ok(None::, Some(&val), in_header.unique, w), + Ok(GetxattrReply::Count(count)) => { + let out = GetxattrOut { + size: count, + ..Default::default() + }; + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn listxattr(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let GetxattrIn { size, .. } = r.read_obj().map_err(Error::DecodeMessage)?; + + if size > MAX_BUFFER_SIZE { + return reply_error( + io::Error::from_raw_os_error(libc::ENOMEM), + in_header.unique, + w, + ); + } + + match self + .fs + .listxattr(Context::from(in_header), in_header.nodeid.into(), size) + { + Ok(ListxattrReply::Names(val)) => reply_ok(None::, Some(&val), in_header.unique, w), + Ok(ListxattrReply::Count(count)) => { + let out = GetxattrOut { + size: count, + ..Default::default() + }; + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn removexattr(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + if self.is_filtered_root(in_header.nodeid) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + let namelen = (in_header.len as usize) + .checked_sub(size_of::()) + .ok_or(Error::InvalidHeaderLength)?; + + let mut buf = vec![0; namelen]; + + r.read_exact(&mut buf).map_err(Error::DecodeMessage)?; + + let name = bytes_to_cstr(&buf)?; + + match self + .fs + .removexattr(Context::from(in_header), in_header.nodeid.into(), name) + { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn flush(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let FlushIn { fh, lock_owner, .. } = r.read_obj().map_err(Error::DecodeMessage)?; + + match self.fs.flush( + Context::from(in_header), + in_header.nodeid.into(), + fh.into(), + lock_owner, + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn init(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let InitInCompat { + major, + minor, + max_readahead, + flags, + } = r.read_obj().map_err(Error::DecodeMessage)?; + + let options = FsOptions::from_bits_truncate(flags as u64); + + let InitInExt { flags2, .. } = if options.contains(FsOptions::INIT_EXT) { + r.read_obj().map_err(Error::DecodeMessage)? + } else { + InitInExt::default() + }; + + if major < KERNEL_VERSION { + error!("Unsupported fuse protocol version: {}.{}", major, minor); + return reply_error( + io::Error::from_raw_os_error(libc::EPROTO), + in_header.unique, + w, + ); + } + + if major > KERNEL_VERSION { + // Wait for the kernel to reply back with a 7.X version. + let out = InitOut { + major: KERNEL_VERSION, + minor: KERNEL_MINOR_VERSION, + ..Default::default() + }; + + return reply_ok(Some(out), None, in_header.unique, w); + } + + if minor < MIN_KERNEL_MINOR_VERSION { + error!( + "Unsupported fuse protocol minor version: {}.{}", + major, minor + ); + return reply_error( + io::Error::from_raw_os_error(libc::EPROTO), + in_header.unique, + w, + ); + } + + // These fuse features are supported by this server by default. + let supported = FsOptions::ASYNC_READ + | FsOptions::PARALLEL_DIROPS + | FsOptions::BIG_WRITES + | FsOptions::AUTO_INVAL_DATA + | FsOptions::ASYNC_DIO + | FsOptions::HAS_IOCTL_DIR + | FsOptions::ATOMIC_O_TRUNC + | FsOptions::MAX_PAGES + | FsOptions::SUBMOUNTS + | FsOptions::INIT_EXT + | FsOptions::CREATE_SUPP_GROUP; + + let flags_64 = ((flags2 as u64) << 32) | (flags as u64); + let capable = FsOptions::from_bits_truncate(flags_64); + + let page_size: u32 = unsafe { libc::sysconf(libc::_SC_PAGESIZE).try_into().unwrap() }; + let max_pages = ((MAX_BUFFER_SIZE - 1) / page_size) + 1; + + match self.fs.init(capable) { + Ok(want) => { + let enabled = (capable & (want | supported)).bits(); + self.options.store(enabled, Ordering::Relaxed); + + let mut out = InitOut { + major: KERNEL_VERSION, + minor: KERNEL_MINOR_VERSION, + max_readahead, + flags: enabled as u32, + max_background: u16::MAX, + congestion_threshold: (u16::MAX / 4) * 3, + max_write: MAX_BUFFER_SIZE, + time_gran: 1, // nanoseconds + max_pages: max_pages.try_into().unwrap(), + map_alignment: 0, + flags2: (enabled >> 32) as u32, + ..Default::default() + }; + + // Enable DIRECT_IO_ALLOW_MMAP when cache is disabled, + // send then enabled feature with standard FUSE + // protocol. + if want.contains(FsOptions::DIRECT_IO_ALLOW_MMAP) { + out.flags2 = (FsOptions::DIRECT_IO_ALLOW_MMAP.bits() >> 32) as u32; + } + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn opendir(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let OpenIn { flags, .. } = r.read_obj().map_err(Error::DecodeMessage)?; + + match self + .fs + .opendir(Context::from(in_header), in_header.nodeid.into(), flags) + { + Ok((handle, opts)) => { + let out = OpenOut { + fh: handle.map(Into::into).unwrap_or(0), + open_flags: opts.bits(), + ..Default::default() + }; + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn mock_readdir(&self, size: u32, offset: u64, cursor: &mut Writer) -> io::Result { + let mut total_written = 0; + let mut err = None; + let filter = self.root_filter.as_ref().lock().unwrap(); + let root_dirents = filter.get_root_dirents(); + + for i in (offset as usize)..root_dirents.len() { + let dirent2 = &root_dirents[i]; + let dirent2_name = CString::new(dirent2.name.clone().into_bytes()).unwrap(); + let dirent = DirEntry { + ino: dirent2.ino, + offset: dirent2.offset, + type_: dirent2.type_, + name: dirent2_name.as_c_str(), + }; + // info!("dirent: {:?}", dirent); + let remaining = (size as usize).saturating_sub(total_written); + match add_dirent(cursor, remaining, dirent, None) { + // No more space left in the buffer. + Ok(0) => break, + Ok(bytes_written) => { + total_written += bytes_written; + } + Err(e) => { + err = Some(e); + break; + } + } + } + + if let Some(err) = err { + Err(err) + } else { + Ok(total_written) + } + } + + fn readdir(&self, in_header: InHeader, mut r: Reader, mut w: Writer) -> Result { + let ReadIn { + fh, offset, size, .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + + if size > MAX_BUFFER_SIZE { + return reply_error( + io::Error::from_raw_os_error(libc::ENOMEM), + in_header.unique, + w, + ); + } + + let available_bytes = w.available_bytes(); + if available_bytes < size as usize { + return reply_error( + io::Error::from_raw_os_error(libc::ENOMEM), + in_header.unique, + w, + ); + } + + // Skip over enough bytes for the header. + let unique = in_header.unique; + let mut cursor = w.split_at(size_of::()).unwrap(); + + let result = if in_header.nodeid == ROOT_ID + && !self.root_filter.as_ref().lock().unwrap().is_empty() + { + self.mock_readdir(size, offset, &mut cursor) + } else { + match self.fs.readdir( + Context::from(in_header), + in_header.nodeid.into(), + fh.into(), + size, + offset, + ) { + Ok(mut entries) => { + let mut total_written = 0; + let mut err = None; + while let Some(dirent) = entries.next() { + let remaining = (size as usize).saturating_sub(total_written); + match add_dirent(&mut cursor, remaining, dirent, None) { + // No more space left in the buffer. + Ok(0) => break, + Ok(bytes_written) => { + total_written += bytes_written; + } + Err(e) => { + err = Some(e); + break; + } + } + } + if let Some(err) = err { + Err(err) + } else { + Ok(total_written) + } + } + Err(e) => Err(e), + } + }; + + match result { + Ok(total_written) => reply_readdir(total_written, unique, w), + Err(e) => reply_error(e, unique, w), + } + } + + fn handle_dirent<'d>( + &self, + in_header: &InHeader, + dir_entry: DirEntry<'d>, + ) -> io::Result<(DirEntry<'d>, Entry)> { + let parent = in_header.nodeid.into(); + let name = dir_entry.name.to_bytes(); + let entry = if name == CURRENT_DIR_CSTR || name == PARENT_DIR_CSTR { + // Don't do lookups on the current directory or the parent directory. Safe because + // this only contains integer fields and any value is valid. + let mut attr = unsafe { MaybeUninit::::zeroed().assume_init() }; + attr.st_ino = dir_entry.ino; + attr.st_mode = dir_entry.type_ << 12; + + // We use 0 for the inode value to indicate a negative entry. + Entry { + inode: 0, + generation: 0, + attr, + attr_flags: 0, + attr_timeout: Duration::from_secs(0), + entry_timeout: Duration::from_secs(0), + } + } else { + self.fs + .lookup(Context::from(*in_header), parent, dir_entry.name, None)? + }; + + Ok((dir_entry, entry)) + } + + fn readdirplus(&self, in_header: InHeader, mut r: Reader, mut w: Writer) -> Result { + let ReadIn { + fh, offset, size, .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + + if size > MAX_BUFFER_SIZE { + return reply_error( + io::Error::from_raw_os_error(libc::ENOMEM), + in_header.unique, + w, + ); + } + + let available_bytes = w.available_bytes(); + if available_bytes < size as usize { + return reply_error( + io::Error::from_raw_os_error(libc::ENOMEM), + in_header.unique, + w, + ); + } + + // If `readdirplus` is not enabled in the filesystem configuration, reject any + // `readdirplus` FUSE request by returning an empty reply. This prevents a + // malicious guest from bypassing the negotiated capabilities and issuing forged + // `readdirplus` requests directly. + if !self.fs.readdirplus_enabled() { + return reply_readdir(0, in_header.unique, w); + } + + // Skip over enough bytes for the header. + let unique = in_header.unique; + let mut cursor = w.split_at(size_of::()).unwrap(); + let result = match self.fs.readdir( + Context::from(in_header), + in_header.nodeid.into(), + fh.into(), + size, + offset, + ) { + Ok(mut entries) => { + let mut total_written = 0; + let mut err = None; + while let Some(dirent) = entries.next() { + let mut entry_inode = None; + let bytes_written = + self.handle_dirent(&in_header, dirent).and_then(|(d, e)| { + entry_inode = Some(e.inode); + let remaining = (size as usize).saturating_sub(total_written); + add_dirent(&mut cursor, remaining, d, Some(e)) + }); + match bytes_written { + Ok(0) => { + // No more space left in the buffer but we need to undo the lookup + // that created the Entry or we will end up with mismatched lookup + // counts. + if let Some(inode) = entry_inode { + self.fs.forget(Context::from(in_header), inode.into(), 1); + } + break; + } + Ok(bytes_written) => { + total_written += bytes_written; + } + Err(e) => { + if let Some(inode) = entry_inode { + self.fs.forget(Context::from(in_header), inode.into(), 1); + } + + if total_written == 0 { + // We haven't filled any entries yet so we can just propagate + // the error. + err = Some(e); + } + + // We already filled in some entries. Returning an error now will + // cause lookup count mismatches for those entries so just return + // whatever we already have. + break; + } + } + } + if let Some(err) = err { + Err(err) + } else { + Ok(total_written) + } + } + Err(e) => Err(e), + }; + + match result { + Ok(total_written) => reply_readdir(total_written, unique, w), + Err(e) => reply_error(e, unique, w), + } + } + + fn releasedir(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let ReleaseIn { fh, flags, .. } = r.read_obj().map_err(Error::DecodeMessage)?; + + match self.fs.releasedir( + Context::from(in_header), + in_header.nodeid.into(), + flags, + fh.into(), + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn fsyncdir(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let FsyncIn { + fh, fsync_flags, .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + let datasync = fsync_flags & 0x1 != 0; + + match self.fs.fsyncdir( + Context::from(in_header), + in_header.nodeid.into(), + datasync, + fh.into(), + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn getlk(&self, in_header: InHeader, mut _r: Reader, w: Writer) -> Result { + if let Err(e) = self.fs.getlk() { + reply_error(e, in_header.unique, w) + } else { + Ok(0) + } + } + + fn setlk(&self, in_header: InHeader, mut _r: Reader, w: Writer) -> Result { + if let Err(e) = self.fs.setlk() { + reply_error(e, in_header.unique, w) + } else { + Ok(0) + } + } + + fn setlkw(&self, in_header: InHeader, mut _r: Reader, w: Writer) -> Result { + if let Err(e) = self.fs.setlkw() { + reply_error(e, in_header.unique, w) + } else { + Ok(0) + } + } + + fn access(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let AccessIn { mask, .. } = r.read_obj().map_err(Error::DecodeMessage)?; + + match self + .fs + .access(Context::from(in_header), in_header.nodeid.into(), mask) + { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn create(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + if self.is_filtered_root(in_header.nodeid) { + return reply_error( + io::Error::from_raw_os_error(libc::EACCES), + in_header.unique, + w, + ); + } + + let CreateIn { + flags, + mode, + umask, + open_flags, + .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + + let remaining_len = (in_header.len as usize) + .checked_sub(size_of::()) + .and_then(|l| l.checked_sub(size_of::())) + .ok_or(Error::InvalidHeaderLength)?; + + let mut buf = vec![0; remaining_len]; + + r.read_exact(&mut buf).map_err(Error::DecodeMessage)?; + let mut components = buf.split_inclusive(|c| *c == b'\0'); + let name = components.next().ok_or(Error::MissingParameter)?; + + let options = FsOptions::from_bits_truncate(self.options.load(Ordering::Relaxed)); + + let extensions = get_extensions(options, name.len(), buf.as_slice())?; + + let kill_priv = open_flags & OPEN_KILL_SUIDGID != 0; + + match self.fs.create( + Context::from(in_header), + in_header.nodeid.into(), + bytes_to_cstr(name)?, + mode, + kill_priv, + flags, + umask, + extensions, + ) { + Ok((entry, handle, opts)) => { + let entry_out = EntryOut { + nodeid: entry.inode, + generation: entry.generation, + entry_valid: entry.entry_timeout.as_secs(), + attr_valid: entry.attr_timeout.as_secs(), + entry_valid_nsec: entry.entry_timeout.subsec_nanos(), + attr_valid_nsec: entry.attr_timeout.subsec_nanos(), + attr: Attr::with_flags(entry.attr, entry.attr_flags), + }; + let open_out = OpenOut { + fh: handle.map(Into::into).unwrap_or(0), + open_flags: opts.bits(), + ..Default::default() + }; + + // Kind of a hack to write both structs. + reply_ok( + Some(entry_out), + Some(open_out.as_slice()), + in_header.unique, + w, + ) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn interrupt(&self, _in_header: InHeader) -> usize { + 0 + } + + fn bmap(&self, in_header: InHeader, mut _r: Reader, w: Writer) -> Result { + if let Err(e) = self.fs.bmap() { + reply_error(e, in_header.unique, w) + } else { + Ok(0) + } + } + + fn destroy(&self) -> usize { + // No reply to this function. + self.fs.destroy(); + self.options + .store(FsOptions::empty().bits(), Ordering::Relaxed); + + 0 + } + + fn ioctl(&self, in_header: InHeader, _r: Reader, w: Writer) -> Result { + if let Err(e) = self.fs.ioctl() { + reply_error(e, in_header.unique, w) + } else { + Ok(0) + } + } + + fn poll(&self, in_header: InHeader, mut _r: Reader, w: Writer) -> Result { + if let Err(e) = self.fs.poll() { + reply_error(e, in_header.unique, w) + } else { + Ok(0) + } + } + + fn notify_reply(&self, in_header: InHeader, mut _r: Reader, w: Writer) -> Result { + if let Err(e) = self.fs.notify_reply() { + reply_error(e, in_header.unique, w) + } else { + Ok(0) + } + } + + fn batch_forget(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let BatchForgetIn { count, .. } = r.read_obj().map_err(Error::DecodeMessage)?; + + if let Some(size) = (count as usize).checked_mul(size_of::()) { + if size > MAX_BUFFER_SIZE as usize { + return reply_error( + io::Error::from_raw_os_error(libc::ENOMEM), + in_header.unique, + w, + ); + } + } else { + return reply_error( + io::Error::from_raw_os_error(libc::EOVERFLOW), + in_header.unique, + w, + ); + } + + let mut requests = Vec::with_capacity(count as usize); + for _ in 0..count { + requests.push( + r.read_obj::() + .map(|f| (f.nodeid.into(), f.nlookup)) + .map_err(Error::DecodeMessage)?, + ); + } + + self.fs.batch_forget(Context::from(in_header), requests); + + // No reply for forget messages. + Ok(0) + } + + fn fallocate(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let FallocateIn { + fh, + offset, + length, + mode, + .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + + match self.fs.fallocate( + Context::from(in_header), + in_header.nodeid.into(), + fh.into(), + mode, + offset, + length, + ) { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn lseek(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let LseekIn { + fh, offset, whence, .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + + match self.fs.lseek( + Context::from(in_header), + in_header.nodeid.into(), + fh.into(), + offset, + whence, + ) { + Ok(offset) => { + let out = LseekOut { offset }; + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn copyfilerange(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result { + let CopyfilerangeIn { + fh_in, + off_in, + nodeid_out, + fh_out, + off_out, + len, + flags, + .. + } = r.read_obj().map_err(Error::DecodeMessage)?; + + match self.fs.copyfilerange( + Context::from(in_header), + in_header.nodeid.into(), + fh_in.into(), + off_in, + nodeid_out.into(), + fh_out.into(), + off_out, + len, + flags, + ) { + Ok(count) => { + let out = WriteOut { + size: count as u32, + ..Default::default() + }; + + reply_ok(Some(out), None, in_header.unique, w) + } + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn syncfs(&self, in_header: InHeader, w: Writer) -> Result { + match self + .fs + .syncfs(Context::from(in_header), in_header.nodeid.into()) + { + Ok(()) => reply_ok(None::, None, in_header.unique, w), + Err(e) => reply_error(e, in_header.unique, w), + } + } + + fn tmpfile(&self, in_header: InHeader, _r: Reader, w: Writer) -> Result { + let e = self + .fs + .tmpfile() + .err() + .unwrap_or_else(|| panic!("unsupported operation")); + reply_error(e, in_header.unique, w) + } +} + +impl SerializableFileSystem for Server { + fn prepare_serialization(&self, cancel: Arc) -> io::Result<()> { + self.fs.prepare_serialization(cancel) + } + + fn serialize(&self, state_pipe: File) -> io::Result<()> { + self.fs.serialize(state_pipe) + } + + fn deserialize_and_apply(&self, state_pipe: File) -> io::Result<()> { + self.inject_filter_path_remap(); + self.fs.deserialize_and_apply(state_pipe) + } + + fn serialize_data(&self) -> io::Result> { + self.fs.serialize_data() + } + + fn deserialize_and_apply_data(&self, serialized: &Vec) -> io::Result<()> { + self.inject_filter_path_remap(); + self.fs.deserialize_and_apply_data(serialized) + } +} + +impl Server { + fn inject_filter_path_remap(&self) { + if !self.is_remap_filter_enabled() { + return; + } + let remap = self.root_filter.lock().unwrap().basename_paths(); + if !remap.is_empty() { + self.fs.set_filter_path_remap(remap); + } + } +} + +fn reply_readdir(len: usize, unique: u64, mut w: Writer) -> Result { + let out = OutHeader { + len: (size_of::() + len) as u32, + error: 0, + unique, + }; + + debug!("Replying OK, header: {:?}", out); + w.write_all(out.as_slice()).map_err(Error::EncodeMessage)?; + w.flush().map_err(Error::FlushMessage)?; + Ok(out.len as usize) +} + +fn reply_ok( + out: Option, + data: Option<&[u8]>, + unique: u64, + mut w: Writer, +) -> Result { + let mut len = size_of::(); + + if out.is_some() { + len += size_of::(); + } + + if let Some(data) = data { + len += data.len(); + } + + let header = OutHeader { + len: len as u32, + error: 0, + unique, + }; + + debug!("Replying OK, header: {:?}", header); + + w.write_all(header.as_slice()) + .map_err(Error::EncodeMessage)?; + + if let Some(out) = out { + w.write_all(out.as_slice()).map_err(Error::EncodeMessage)?; + } + + if let Some(data) = data { + w.write_all(data).map_err(Error::EncodeMessage)?; + } + + debug_assert_eq!(len, w.bytes_written()); + Ok(w.bytes_written()) +} + +fn strerror(error: i32) -> String { + let mut err_desc: Vec = vec![0; 256]; + let buf_ptr = err_desc.as_mut_ptr() as *mut libc::c_char; + + // Safe because libc::strerror_r writes in err_desc at most err_desc.len() bytes + unsafe { + // We ignore the returned value since the two possible error values are: + // EINVAL and ERANGE, in the former err_desc will be "Unknown error #" + // and in the latter the message will be truncated to fit err_desc + libc::strerror_r(error, buf_ptr, err_desc.len()); + } + let err_desc = err_desc.split(|c| *c == b'\0').next().unwrap(); + String::from_utf8(err_desc.to_vec()).unwrap_or_else(|_| "".to_owned()) +} + +fn reply_error(e: io::Error, unique: u64, mut w: Writer) -> Result { + let header = OutHeader { + len: size_of::() as u32, + error: -e.raw_os_error().unwrap_or(libc::EIO), + unique, + }; + + debug!( + "Replying ERROR, header: OutHeader {{ error: {} ({}), unique: {}, len: {} }}", + header.error, + strerror(-header.error), + header.unique, + header.len + ); + + w.write_all(header.as_slice()) + .map_err(Error::EncodeMessage)?; + + debug_assert_eq!(header.len as usize, w.bytes_written()); + Ok(w.bytes_written()) +} + +fn bytes_to_cstr(buf: &[u8]) -> Result<&CStr> { + // Convert to a `CStr` first so that we can drop the '\0' byte at the end + // and make sure there are no interior '\0' bytes. + CStr::from_bytes_with_nul(buf).map_err(Error::InvalidCString) +} + +fn add_dirent( + cursor: &mut Writer, + max: usize, + d: DirEntry, + entry: Option, +) -> io::Result { + // Strip the trailing '\0'. + let name = d.name.to_bytes(); + if name.len() > u32::MAX as usize { + return Err(io::Error::from_raw_os_error(libc::EOVERFLOW)); + } + + let dirent_len = size_of::() + .checked_add(name.len()) + .ok_or_else(|| io::Error::from_raw_os_error(libc::EOVERFLOW))?; + + // Directory entries must be padded to 8-byte alignment. If adding 7 causes + // an overflow then this dirent cannot be properly padded. + let padded_dirent_len = dirent_len + .checked_add(7) + .map(|l| l & !7) + .ok_or_else(|| io::Error::from_raw_os_error(libc::EOVERFLOW))?; + + let total_len = if entry.is_some() { + padded_dirent_len + .checked_add(size_of::()) + .ok_or_else(|| io::Error::from_raw_os_error(libc::EOVERFLOW))? + } else { + padded_dirent_len + }; + + if max < total_len { + Ok(0) + } else { + if let Some(entry) = entry { + cursor.write_all(EntryOut::from(entry).as_slice())?; + } + + let dirent = Dirent { + ino: d.ino, + off: d.offset, + namelen: name.len() as u32, + type_: d.type_, + }; + + cursor.write_all(dirent.as_slice())?; + cursor.write_all(name)?; + + // We know that `dirent_len` <= `padded_dirent_len` due to the check above + // so there's no need for checked arithmetic. + let padding = padded_dirent_len - dirent_len; + if padding > 0 { + cursor.write_all(&DIRENT_PADDING[..padding])?; + } + + Ok(total_len) + } +} + +fn take_object(data: &[u8]) -> Result<(T, &[u8])> { + if data.len() < size_of::() { + return Err(Error::DecodeMessage(einval())); + } + + let (object_bytes, remaining_bytes) = data.split_at(size_of::()); + // SAFETY: `T` implements `ByteValued` that guarantees that it is safe to instantiate + // `T` with random data. + let object: T = unsafe { std::ptr::read_unaligned(object_bytes.as_ptr() as *const T) }; + Ok((object, remaining_bytes)) +} + +fn parse_security_context(nr_secctx: u32, data: &[u8]) -> Result> { + // Although the FUSE security context extension allows sending several security contexts, + // currently the guest kernel only sends one. + if nr_secctx > 1 { + return Err(Error::DecodeMessage(einval())); + } else if nr_secctx == 0 { + // No security context sent. May be no LSM supports it. + return Ok(None); + } + + let (secctx, data) = take_object::(data)?; + + if secctx.size == 0 { + return Err(Error::DecodeMessage(einval())); + } + + let mut components = data.split_inclusive(|c| *c == b'\0'); + let secctx_name = components.next().ok_or(Error::MissingParameter)?; + let (_, data) = data.split_at(secctx_name.len()); + + if data.len() < secctx.size as usize { + return Err(Error::DecodeMessage(einval())); + } + + // Fuse client aligns the whole security context block to 64 byte + // boundary. So it is possible that after actual security context + // of secctx.size, there are some null padding bytes left. If + // we ever parse more data after secctx, we will have to take those + // null bytes into account. Total size (including null bytes) is + // available in SecctxHeader->size. + let (remaining, _) = data.split_at(secctx.size as usize); + + let fuse_secctx = SecContext { + name: CString::from_vec_with_nul(secctx_name.to_vec()).map_err(Error::InvalidCString2)?, + secctx: remaining.to_vec(), + }; + + Ok(Some(fuse_secctx)) +} + +fn parse_sup_groups(data: &[u8]) -> Result { + let (group_header, group_id_bytes) = take_object::(data)?; + + // The FUSE extension allows sending several group IDs, but currently the guest + // kernel only sends one. + if group_header.nr_groups != 1 { + return Err(Error::DecodeMessage(einval())); + } + + let (gid, _) = take_object::(group_id_bytes)?; + Ok(gid) +} + +fn get_extensions(options: FsOptions, skip: usize, request_bytes: &[u8]) -> Result { + let mut extensions = Extensions::default(); + + if !(options.contains(FsOptions::SECURITY_CTX) + || options.contains(FsOptions::CREATE_SUPP_GROUP)) + { + return Ok(extensions); + } + + // It's not guaranty to receive an extension even if it's supported by the guest kernel + if request_bytes.len() < skip { + return Err(Error::DecodeMessage(einval())); + } + + // We need to track if a SecCtx was received, because it's valid + // for the guest to send an empty SecCtx (i.e, nr_secctx == 0) + let mut secctx_received = false; + + let mut buf = &request_bytes[skip..]; + while !buf.is_empty() { + let (extension_header, remaining_bytes) = take_object::(buf)?; + + let extension_size = (extension_header.size as usize) + .checked_sub(size_of::()) + .ok_or(Error::InvalidHeaderLength)?; + + let (current_extension_bytes, next_extension_bytes) = + remaining_bytes.split_at(extension_size); + + let ext_type = ExtType::try_from(extension_header.ext_type) + .map_err(|_| Error::DecodeMessage(einval()))?; + + match ext_type { + ExtType::SecCtx(nr_secctx) => { + if !options.contains(FsOptions::SECURITY_CTX) || secctx_received { + return Err(Error::DecodeMessage(einval())); + } + + secctx_received = true; + extensions.secctx = parse_security_context(nr_secctx, current_extension_bytes)?; + debug!("Extension received: {} SecCtx", nr_secctx); + } + ExtType::SupGroups => { + if !options.contains(FsOptions::CREATE_SUPP_GROUP) || extensions.sup_gid.is_some() { + return Err(Error::DecodeMessage(einval())); + } + + extensions.sup_gid = parse_sup_groups(current_extension_bytes)?.into(); + debug!("Extension received: SupGroups({:?})", extensions.sup_gid); + } + } + + // Let's process the next extension + buf = next_extension_bytes; + } + + // The SupGroup extension can be missing, since it is only sent if needed. + // A SecCtx is always sent in create/synlink/mknod/mkdir if supported. + if options.contains(FsOptions::SECURITY_CTX) && !secctx_received { + return Err(Error::MissingExtension); + } + + Ok(extensions) +} diff --git a/vendor/cube-hypervisor/virtiofsd/src/util.rs b/vendor/cube-hypervisor/virtiofsd/src/util.rs new file mode 100644 index 0000000..89c63ca --- /dev/null +++ b/vendor/cube-hypervisor/virtiofsd/src/util.rs @@ -0,0 +1,190 @@ +// Copyright 2022 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use std::fs::{File, OpenOptions}; +use std::io::{Error, Write}; +use std::os::unix::fs::{MetadataExt, OpenOptionsExt}; +use std::os::unix::io::{AsRawFd, FromRawFd}; +use std::path::Path; +use std::{fs, io, process}; + +fn try_lock_file(file: &File) -> Result<(), Error> { + // Safe because 'file' must exist and we check the return value. + let file_fd = file.as_raw_fd(); + let ret = unsafe { libc::flock(file_fd, libc::LOCK_EX | libc::LOCK_NB) }; + if ret == -1 { + return Err(Error::last_os_error()); + } + Ok(()) +} + +pub fn write_pid_file(pid_file_name: &Path) -> Result { + let mut pid_file = loop { + let file = OpenOptions::new() + .mode(libc::S_IRUSR | libc::S_IWUSR) + .custom_flags(libc::O_CLOEXEC) + .write(true) + .create(true) + .open(pid_file_name)?; + + try_lock_file(&file)?; + + // Let's make sure the file we locked still exists in the filesystem. + let locked = file.metadata()?.ino(); + let current = match fs::metadata(pid_file_name) { + Ok(stat) => stat.ino(), + _ => continue, // the pid file got removed or some error happened, try again. + }; + + if locked == current { + break file; // lock successfully acquired. + } + // the file changed, other process is racing with us, so try again. + }; + + let pid = format!("{}\n", process::id()); + pid_file.write_all(pid.as_bytes())?; + + Ok(pid_file) +} + +unsafe fn pidfd_open(pid: libc::pid_t, flags: libc::c_uint) -> libc::c_int { + libc::syscall(libc::SYS_pidfd_open, pid, flags) as libc::c_int +} + +/// Helper function to create a process and sets the parent process +/// death signal SIGTERM +pub fn sfork() -> io::Result { + let cur_pid = unsafe { libc::getpid() }; + + // We use pidfd_open(2) to check the parent's pid because if the + // child is created inside a pid namespace, getppid(2) will always + // return 0 + let parent_pidfd = unsafe { pidfd_open(cur_pid, 0) }; + if parent_pidfd == -1 { + return Err(Error::last_os_error()); + } + + // We wrap the parent PID file descriptor in a File object to ensure that is + // auto-closed when it goes out of scope. But, since nothing can be read, using read(2), + // from a PID file descriptor returned by pidfd_open(2) (it fails with EINVAL), we + // use a new type PidFd to prevent using the File's methods directly, and in the hope + // that whoever wants to do so will read this first. + // This is a temporary solution until OwnedFd is stabilized. + #[allow(dead_code)] + struct PidFd(File); + let _pidfd = unsafe { PidFd(File::from_raw_fd(parent_pidfd)) }; + + let child_pid = unsafe { libc::fork() }; + if child_pid == -1 { + return Err(Error::last_os_error()); + } + + if child_pid == 0 { + // Request to receive SIGTERM on parent's death. + let ret = unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) }; + assert_eq!(ret, 0); // This shouldn't fail because libc::SIGTERM is a valid signal number + + // Check if the original parent died before libc::prctl() was called + let mut pollfds = libc::pollfd { + fd: parent_pidfd, + events: libc::POLLIN, + revents: 0, + }; + let num_fds = unsafe { libc::poll(&mut pollfds, 1, 0) }; + if num_fds == -1 { + return Err(io::Error::last_os_error()); + } + if num_fds != 0 { + // The original parent died + return Err(other_io_error("Parent process died unexpectedly")); + } + } + Ok(child_pid) +} + +pub fn wait_for_child(pid: i32) -> ! { + // Drop all capabilities, since the parent doesn't require any + // capabilities, as it'd be just waiting for the child to exit. + capng::clear(capng::Set::BOTH); + if let Err(e) = capng::apply(capng::Set::BOTH) { + // Don't exit the process here since we already have a child. + error!("warning: can't apply the parent capabilities: {}", e); + } + + let mut status = 0; + // On success, `libc::waitpid()` returns the PID of the child. + if unsafe { libc::waitpid(pid, &mut status, 0) } != pid { + error!("Error during waitpid()"); + process::exit(1); + } + + let exit_code = if libc::WIFEXITED(status) { + libc::WEXITSTATUS(status) + } else if libc::WIFSIGNALED(status) { + let signal = libc::WTERMSIG(status); + error!("Child process terminated by signal {}", signal); + -signal + } else { + error!("Unexpected waitpid status: {:#X}", status); + libc::EXIT_FAILURE + }; + + process::exit(exit_code); +} + +/// Add a capability to the effective set +/// # Errors +/// An error variant will be returned: +/// - if the input string does not match the name, without the 'CAP_' prefix, +/// of any of the capability defined in `linux/capabiliy.h`. +/// - if `capng::get_caps_process()` cannot get the capabilities and bounding set of the process. +/// - if `capng::update()` fails to update the internal posix capabilities settings. +/// - if `capng::apply()` fails to transfer the specified internal posix capabilities +/// settings to the kernel. +pub fn add_cap_to_eff(cap_name: &str) -> capng::Result<()> { + use capng::{Action, CUpdate, Set, Type}; + let cap = capng::name_to_capability(cap_name)?; + capng::get_caps_process()?; + + let req = vec![CUpdate { + action: Action::ADD, + cap_type: Type::EFFECTIVE, + capability: cap, + }]; + + capng::update(req)?; + capng::apply(Set::CAPS)?; + + Ok(()) +} + +/// Same as `io::Error::other()`, but the respective io_error_other feature has only been +/// stabilized in Rust 1.74.0, which is too new for our intended targets. +pub fn other_io_error>>(err: E) -> io::Error { + io::Error::other(err) +} + +/// Trait for `Error` object that allows prepending the error message by something that gives +/// context +pub trait ErrorContext { + fn context(self, context: C) -> Self; +} + +impl ErrorContext for io::Error { + fn context(self, context: C) -> Self { + io::Error::new(self.kind(), format!("{context}: {self}")) + } +} + +/// Lifts the `ErrorContext` trait to `Result` types +pub trait ResultErrorContext { + fn err_context C>(self, context: F) -> Self; +} + +impl ResultErrorContext for Result { + fn err_context C>(self, context: F) -> Self { + self.map_err(|err| err.context(context())) + } +} diff --git a/vendor/cube-hypervisor/vm-allocator/Cargo.toml b/vendor/cube-hypervisor/vm-allocator/Cargo.toml new file mode 100644 index 0000000..00e7881 --- /dev/null +++ b/vendor/cube-hypervisor/vm-allocator/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "vm-allocator" +version = "0.1.0" +authors = ["The Chromium OS Authors"] +edition = "2021" + +[dependencies] +libc = "0.2.137" +vm-memory = { workspace = true } + +[target.'cfg(target_arch = "aarch64")'.dependencies] +arch = { path = "../arch" } diff --git a/vendor/cube-hypervisor/vm-allocator/src/address.rs b/vendor/cube-hypervisor/vm-allocator/src/address.rs new file mode 100644 index 0000000..73c1b6c --- /dev/null +++ b/vendor/cube-hypervisor/vm-allocator/src/address.rs @@ -0,0 +1,403 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Copyright © 2019 Intel Corporation +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use std::collections::btree_map::BTreeMap; +use std::result; +use vm_memory::{Address, GuestAddress, GuestUsize}; + +#[derive(Debug)] +pub enum Error { + Overflow, + Overlap, + UnalignedAddress, +} + +pub type Result = result::Result; + +/// Manages allocating address ranges. +/// Use `AddressAllocator` whenever an address range needs to be allocated to different users. +/// +/// # Examples +/// +/// ``` +/// # use vm_allocator::AddressAllocator; +/// # use vm_memory::{Address, GuestAddress, GuestUsize}; +/// AddressAllocator::new(GuestAddress(0x1000), 0x10000).map(|mut pool| { +/// assert_eq!(pool.allocate(None, 0x110, Some(0x100)), Some(GuestAddress(0x10e00))); +/// assert_eq!(pool.allocate(None, 0x100, Some(0x100)), Some(GuestAddress(0x10d00))); +/// }); +/// ``` +#[derive(Debug, Eq, PartialEq)] +pub struct AddressAllocator { + base: GuestAddress, + end: GuestAddress, + ranges: BTreeMap, +} + +impl AddressAllocator { + /// Creates a new `AddressAllocator` for managing a range of addresses. + /// Can return `None` if `base` + `size` overflows a u64. + /// + /// * `base` - The starting address of the range to manage. + /// * `size` - The size of the address range in bytes. + pub fn new(base: GuestAddress, size: GuestUsize) -> Option { + if size == 0 { + return None; + } + + let end = base.checked_add(size - 1)?; + + let mut allocator = AddressAllocator { + base, + end, + ranges: BTreeMap::new(), + }; + + // Insert the last address as a zero size range. + // This is our end of address space marker. + allocator.ranges.insert(base.checked_add(size)?, 0); + + Some(allocator) + } + + fn align_address(&self, address: GuestAddress, alignment: GuestUsize) -> GuestAddress { + let align_adjust = if address.raw_value() % alignment != 0 { + alignment - (address.raw_value() % alignment) + } else { + 0 + }; + + address.unchecked_add(align_adjust) + } + + fn available_range( + &self, + req_address: GuestAddress, + req_size: GuestUsize, + alignment: GuestUsize, + ) -> Result { + let aligned_address = self.align_address(req_address, alignment); + + // The requested address should be aligned. + if aligned_address != req_address { + return Err(Error::UnalignedAddress); + } + + // The aligned address should be within the address space range. + if aligned_address >= self.end || aligned_address < self.base { + return Err(Error::Overflow); + } + + let mut prev_end_address = self.base; + for (address, size) in self.ranges.iter() { + if aligned_address <= *address { + // Do we overlap with the previous range? + if prev_end_address > aligned_address { + return Err(Error::Overlap); + } + + // Do we have enough space? + if address + .unchecked_sub(aligned_address.raw_value()) + .raw_value() + < req_size + { + return Err(Error::Overlap); + } + + return Ok(aligned_address); + } + + prev_end_address = address.unchecked_add(*size); + } + + // We have not found a range that starts after the requested address, + // despite having a marker at the end of our range. + Err(Error::Overflow) + } + + fn first_available_range( + &self, + req_size: GuestUsize, + alignment: GuestUsize, + ) -> Option { + let reversed_ranges: Vec<(&GuestAddress, &GuestUsize)> = self.ranges.iter().rev().collect(); + + for (idx, (address, _size)) in reversed_ranges.iter().enumerate() { + let next_range_idx = idx + 1; + let prev_end_address = if next_range_idx >= reversed_ranges.len() { + self.base + } else { + reversed_ranges[next_range_idx] + .0 + .unchecked_add(*(reversed_ranges[next_range_idx].1)) + }; + + // If we have enough space between this range and the previous one, + // we return the start of this range minus the requested size. + // As each new range is allocated at the end of the available address space, + // we will tend to always allocate new ranges there as well. In other words, + // ranges accumulate at the end of the address space. + if let Some(size_delta) = + address.checked_sub(self.align_address(prev_end_address, alignment).raw_value()) + { + let adjust = if alignment > 1 { alignment - 1 } else { 0 }; + if size_delta.raw_value() >= req_size { + return Some( + self.align_address(address.unchecked_sub(req_size + adjust), alignment), + ); + } + } + } + + None + } + + /// Allocates a range of addresses from the managed region. Returns `Some(allocated_address)` + /// when successful, or `None` if an area of `size` can't be allocated or if alignment isn't + /// a power of two. + pub fn allocate( + &mut self, + address: Option, + size: GuestUsize, + align_size: Option, + ) -> Option { + if size == 0 { + return None; + } + + let alignment = align_size.unwrap_or(4); + if !alignment.is_power_of_two() || alignment == 0 { + return None; + } + + let new_addr = match address { + Some(req_address) => match self.available_range(req_address, size, alignment) { + Ok(addr) => addr, + Err(_) => { + return None; + } + }, + None => self.first_available_range(size, alignment)?, + }; + + self.ranges.insert(new_addr, size); + + Some(new_addr) + } + + /// Free an already allocated address range. + /// We can only free a range if it matches exactly an already allocated range. + pub fn free(&mut self, address: GuestAddress, size: GuestUsize) { + if let Some(&range_size) = self.ranges.get(&address) { + if size == range_size { + self.ranges.remove(&address); + } + } + } + + /// Start address of the allocator + pub fn base(&self) -> GuestAddress { + self.base + } + + /// Last address of the allocator + pub fn end(&self) -> GuestAddress { + self.end + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn new_fails_overflow() { + assert_eq!( + AddressAllocator::new(GuestAddress(u64::max_value()), 0x100), + None + ); + } + + #[test] + fn new_fails_size_zero() { + assert_eq!(AddressAllocator::new(GuestAddress(0x1000), 0), None); + } + + #[test] + fn allocate_fails_alignment_zero() { + let mut pool = AddressAllocator::new(GuestAddress(0x1000), 0x10000).unwrap(); + assert_eq!( + pool.allocate(Some(GuestAddress(0x1000)), 0x100, Some(0)), + None + ); + } + + #[test] + fn allocate_fails_alignment_non_power_of_two() { + let mut pool = AddressAllocator::new(GuestAddress(0x1000), 0x10000).unwrap(); + assert_eq!( + pool.allocate(Some(GuestAddress(0x1000)), 0x100, Some(200)), + None + ); + } + + #[test] + fn allocate_fails_not_enough_space() { + let mut pool = AddressAllocator::new(GuestAddress(0x1000), 0x1000).unwrap(); + assert_eq!( + pool.allocate(None, 0x800, Some(0x100)), + Some(GuestAddress(0x1800)) + ); + assert_eq!(pool.allocate(None, 0x900, Some(0x100)), None); + assert_eq!( + pool.allocate(None, 0x400, Some(0x100)), + Some(GuestAddress(0x1400)) + ); + } + + #[test] + fn allocate_alignment() { + let mut pool = AddressAllocator::new(GuestAddress(0x1000), 0x10000).unwrap(); + assert_eq!( + pool.allocate(None, 0x110, Some(0x100)), + Some(GuestAddress(0x10e00)) + ); + assert_eq!( + pool.allocate(None, 0x100, Some(0x100)), + Some(GuestAddress(0x10d00)) + ); + assert_eq!( + pool.allocate(None, 0x10, Some(0x100)), + Some(GuestAddress(0x10c00)) + ); + } + + #[test] + fn allocate_address() { + let mut pool = AddressAllocator::new(GuestAddress(0x1000), 0x1000).unwrap(); + assert_eq!( + pool.allocate(Some(GuestAddress(0x1200)), 0x800, None), + Some(GuestAddress(0x1200)) + ); + + assert_eq!( + pool.allocate(Some(GuestAddress(0x1a00)), 0x100, None), + Some(GuestAddress(0x1a00)) + ); + } + + #[test] + fn allocate_address_alignment() { + let mut pool = AddressAllocator::new(GuestAddress(0x1000), 0x1000).unwrap(); + assert_eq!( + pool.allocate(Some(GuestAddress(0x1200)), 0x800, Some(0x100)), + Some(GuestAddress(0x1200)) + ); + + // Unaligned request + assert_eq!( + pool.allocate(Some(GuestAddress(0x1210)), 0x800, Some(0x100)), + None + ); + + // Aligned request + assert_eq!( + pool.allocate(Some(GuestAddress(0x1b00)), 0x100, Some(0x100)), + Some(GuestAddress(0x1b00)) + ); + } + + #[test] + fn allocate_address_not_enough_space() { + let mut pool = AddressAllocator::new(GuestAddress(0x1000), 0x1000).unwrap(); + + // First range is [0x1200:0x1a00] + assert_eq!( + pool.allocate(Some(GuestAddress(0x1200)), 0x800, Some(0x100)), + Some(GuestAddress(0x1200)) + ); + + // Second range is [0x1c00:0x1e00] + assert_eq!( + pool.allocate(Some(GuestAddress(0x1c00)), 0x200, Some(0x100)), + Some(GuestAddress(0x1c00)) + ); + + // There is 0x200 between the first 2 ranges. + // We ask for an available address but the range is too big + assert_eq!( + pool.allocate(Some(GuestAddress(0x1b00)), 0x800, Some(0x100)), + None + ); + + // We ask for an available address, with a small enough range + assert_eq!( + pool.allocate(Some(GuestAddress(0x1b00)), 0x100, Some(0x100)), + Some(GuestAddress(0x1b00)) + ); + } + + #[test] + fn allocate_address_free_and_realloc() { + let mut pool = AddressAllocator::new(GuestAddress(0x1000), 0x1000).unwrap(); + + // First range is [0x1200:0x1a00] + assert_eq!( + pool.allocate(Some(GuestAddress(0x1200)), 0x800, Some(0x100)), + Some(GuestAddress(0x1200)) + ); + + pool.free(GuestAddress(0x1200), 0x800); + + assert_eq!( + pool.allocate(Some(GuestAddress(0x1200)), 0x800, Some(0x100)), + Some(GuestAddress(0x1200)) + ); + } + + #[test] + fn allocate_address_free_fail_and_realloc() { + let mut pool = AddressAllocator::new(GuestAddress(0x1000), 0x1000).unwrap(); + + // First range is [0x1200:0x1a00] + assert_eq!( + pool.allocate(Some(GuestAddress(0x1200)), 0x800, Some(0x100)), + Some(GuestAddress(0x1200)) + ); + + // We try to free a range smaller than the allocated one. + pool.free(GuestAddress(0x1200), 0x100); + + assert_eq!( + pool.allocate(Some(GuestAddress(0x1200)), 0x800, Some(0x100)), + None + ); + } + + #[test] + fn allocate_address_fail_free_and_realloc() { + let mut pool = AddressAllocator::new(GuestAddress(0x1000), 0x1000).unwrap(); + + // First allocation fails + assert_eq!( + pool.allocate(Some(GuestAddress(0x1200)), 0x2000, Some(0x100)), + None + ); + + // We try to free a range that was not allocated. + pool.free(GuestAddress(0x1200), 0x2000); + + // Now we try an allocation that should succeed. + assert_eq!( + pool.allocate(Some(GuestAddress(0x1200)), 0x800, Some(0x100)), + Some(GuestAddress(0x1200)) + ); + } +} diff --git a/vendor/cube-hypervisor/vm-allocator/src/gsi.rs b/vendor/cube-hypervisor/vm-allocator/src/gsi.rs new file mode 100644 index 0000000..ed01bbf --- /dev/null +++ b/vendor/cube-hypervisor/vm-allocator/src/gsi.rs @@ -0,0 +1,108 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause + +#[cfg(target_arch = "x86_64")] +use std::collections::btree_map::BTreeMap; +use std::result; + +#[derive(Debug)] +pub enum Error { + Overflow, +} + +pub type Result = result::Result; + +/// GsiApic +#[cfg(target_arch = "x86_64")] +#[derive(Copy, Clone)] +pub struct GsiApic { + base: u32, + irqs: u32, +} + +#[cfg(target_arch = "x86_64")] +impl GsiApic { + /// New GSI APIC + pub fn new(base: u32, irqs: u32) -> Self { + GsiApic { base, irqs } + } +} + +/// GsiAllocator +pub struct GsiAllocator { + #[cfg(target_arch = "x86_64")] + apics: BTreeMap, + next_irq: u32, + next_gsi: u32, +} + +impl GsiAllocator { + #[cfg(target_arch = "x86_64")] + /// New GSI allocator + pub fn new(apics: Vec) -> Self { + let mut allocator = GsiAllocator { + apics: BTreeMap::new(), + next_irq: 0xffff_ffff, + next_gsi: 0, + }; + + for apic in &apics { + if apic.base < allocator.next_irq { + allocator.next_irq = apic.base; + } + + if apic.base + apic.irqs > allocator.next_gsi { + allocator.next_gsi = apic.base + apic.irqs; + } + + allocator.apics.insert(apic.base, apic.irqs); + } + + allocator + } + + #[cfg(target_arch = "aarch64")] + #[allow(clippy::new_without_default)] + /// New GSI allocator + pub fn new() -> Self { + GsiAllocator { + next_irq: arch::IRQ_BASE, + next_gsi: arch::IRQ_BASE, + } + } + + /// Allocate a GSI + pub fn allocate_gsi(&mut self) -> Result { + let gsi = self.next_gsi; + self.next_gsi = self.next_gsi.checked_add(1).ok_or(Error::Overflow)?; + Ok(gsi) + } + + #[cfg(target_arch = "x86_64")] + /// Allocate an IRQ + pub fn allocate_irq(&mut self) -> Result { + let mut irq: u32 = 0; + for (base, irqs) in self.apics.iter() { + // HACKHACK - This only works with 1 single IOAPIC... + if self.next_irq >= *base && self.next_irq < *base + *irqs { + irq = self.next_irq; + self.next_irq += 1; + } + } + + if irq == 0 { + return Err(Error::Overflow); + } + + Ok(irq) + } + + #[cfg(target_arch = "aarch64")] + /// Allocate an IRQ + pub fn allocate_irq(&mut self) -> Result { + let irq = self.next_irq; + self.next_irq = self.next_irq.checked_add(1).ok_or(Error::Overflow)?; + Ok(irq) + } +} diff --git a/vendor/cube-hypervisor/vm-allocator/src/lib.rs b/vendor/cube-hypervisor/vm-allocator/src/lib.rs new file mode 100644 index 0000000..f7bf5ff --- /dev/null +++ b/vendor/cube-hypervisor/vm-allocator/src/lib.rs @@ -0,0 +1,21 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Copyright © 2019 Intel Corporation +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause +#![deny(missing_docs)] + +//! Manages system resources that can be allocated to VMs and their devices. + +mod address; +mod gsi; +mod system; + +pub use crate::address::AddressAllocator; +pub use crate::gsi::GsiAllocator; +#[cfg(target_arch = "x86_64")] +pub use crate::gsi::GsiApic; +pub use crate::system::SystemAllocator; diff --git a/vendor/cube-hypervisor/vm-allocator/src/system.rs b/vendor/cube-hypervisor/vm-allocator/src/system.rs new file mode 100644 index 0000000..e879ce1 --- /dev/null +++ b/vendor/cube-hypervisor/vm-allocator/src/system.rs @@ -0,0 +1,165 @@ +// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Copyright © 2019 Intel Corporation +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use vm_memory::{GuestAddress, GuestUsize}; + +use crate::address::AddressAllocator; +use crate::gsi::GsiAllocator; +#[cfg(target_arch = "x86_64")] +use crate::gsi::GsiApic; + +use libc::{sysconf, _SC_PAGESIZE}; + +/// Safe wrapper for `sysconf(_SC_PAGESIZE)`. +#[inline(always)] +fn pagesize() -> usize { + // Trivially safe + unsafe { sysconf(_SC_PAGESIZE) as usize } +} + +/// Manages allocating system resources such as address space and interrupt numbers. +/// +/// # Example - Use the `SystemAddress` builder. +/// +/// ``` +/// # #[cfg(target_arch = "x86_64")] +/// # use vm_allocator::{GsiApic, SystemAllocator}; +/// # #[cfg(target_arch = "aarch64")] +/// # use vm_allocator::SystemAllocator; +/// # use vm_memory::{Address, GuestAddress, GuestUsize}; +/// let mut allocator = SystemAllocator::new( +/// #[cfg(target_arch = "x86_64")] GuestAddress(0x1000), +/// #[cfg(target_arch = "x86_64")] 0x10000, +/// GuestAddress(0x10000000), 0x10000000, +/// GuestAddress(0x20000000), 0x100000, +/// #[cfg(target_arch = "x86_64")] vec![GsiApic::new(5, 19)]).unwrap(); +/// #[cfg(target_arch = "x86_64")] +/// assert_eq!(allocator.allocate_irq(), Some(5)); +/// #[cfg(target_arch = "aarch64")] +/// assert_eq!(allocator.allocate_irq(), Some(32)); +/// #[cfg(target_arch = "x86_64")] +/// assert_eq!(allocator.allocate_irq(), Some(6)); +/// #[cfg(target_arch = "aarch64")] +/// assert_eq!(allocator.allocate_irq(), Some(33)); +/// assert_eq!(allocator.allocate_platform_mmio_addresses(None, 0x1000, Some(0x1000)), Some(GuestAddress(0x1fff_f000))); +/// +/// ``` +pub struct SystemAllocator { + #[cfg(target_arch = "x86_64")] + io_address_space: AddressAllocator, + platform_mmio_address_space: AddressAllocator, + mmio_hole_address_space: AddressAllocator, + gsi_allocator: GsiAllocator, +} + +impl SystemAllocator { + /// Creates a new `SystemAllocator` for managing addresses and irq numvers. + /// Can return `None` if `base` + `size` overflows a u64 + /// + /// * `io_base` - (X86) The starting address of IO memory. + /// * `io_size` - (X86) The size of IO memory. + /// * `platform_mmio_base` - The starting address of platform MMIO memory. + /// * `platform_mmio_size` - The size of platform MMIO memory. + /// * `mmio_hole_base` - The starting address of MMIO memory in 32-bit address space. + /// * `mmio_hole_size` - The size of MMIO memory in 32-bit address space. + /// * `apics` - (X86) Vector of APIC's. + /// + pub fn new( + #[cfg(target_arch = "x86_64")] io_base: GuestAddress, + #[cfg(target_arch = "x86_64")] io_size: GuestUsize, + platform_mmio_base: GuestAddress, + platform_mmio_size: GuestUsize, + mmio_hole_base: GuestAddress, + mmio_hole_size: GuestUsize, + #[cfg(target_arch = "x86_64")] apics: Vec, + ) -> Option { + Some(SystemAllocator { + #[cfg(target_arch = "x86_64")] + io_address_space: AddressAllocator::new(io_base, io_size)?, + platform_mmio_address_space: AddressAllocator::new( + platform_mmio_base, + platform_mmio_size, + )?, + mmio_hole_address_space: AddressAllocator::new(mmio_hole_base, mmio_hole_size)?, + #[cfg(target_arch = "x86_64")] + gsi_allocator: GsiAllocator::new(apics), + #[cfg(target_arch = "aarch64")] + gsi_allocator: GsiAllocator::new(), + }) + } + + /// Reserves the next available system irq number. + pub fn allocate_irq(&mut self) -> Option { + self.gsi_allocator.allocate_irq().ok() + } + + /// Reserves the next available GSI. + pub fn allocate_gsi(&mut self) -> Option { + self.gsi_allocator.allocate_gsi().ok() + } + + #[cfg(target_arch = "x86_64")] + /// Reserves a section of `size` bytes of IO address space. + pub fn allocate_io_addresses( + &mut self, + address: Option, + size: GuestUsize, + align_size: Option, + ) -> Option { + self.io_address_space + .allocate(address, size, Some(align_size.unwrap_or(0x1))) + } + + /// Reserves a section of `size` bytes of platform MMIO address space. + pub fn allocate_platform_mmio_addresses( + &mut self, + address: Option, + size: GuestUsize, + align_size: Option, + ) -> Option { + self.platform_mmio_address_space.allocate( + address, + size, + Some(align_size.unwrap_or(pagesize() as u64)), + ) + } + + /// Reserves a section of `size` bytes of MMIO address space. + pub fn allocate_mmio_hole_addresses( + &mut self, + address: Option, + size: GuestUsize, + align_size: Option, + ) -> Option { + self.mmio_hole_address_space.allocate( + address, + size, + Some(align_size.unwrap_or(pagesize() as u64)), + ) + } + + #[cfg(target_arch = "x86_64")] + /// Free an IO address range. + /// We can only free a range if it matches exactly an already allocated range. + pub fn free_io_addresses(&mut self, address: GuestAddress, size: GuestUsize) { + self.io_address_space.free(address, size) + } + + /// Free a platform MMIO address range. + /// We can only free a range if it matches exactly an already allocated range. + pub fn free_platform_mmio_addresses(&mut self, address: GuestAddress, size: GuestUsize) { + self.platform_mmio_address_space.free(address, size) + } + + /// Free an MMIO address range from the 32 bits hole. + /// We can only free a range if it matches exactly an already allocated range. + pub fn free_mmio_hole_addresses(&mut self, address: GuestAddress, size: GuestUsize) { + self.mmio_hole_address_space.free(address, size) + } +} diff --git a/vendor/cube-hypervisor/vm-device/Cargo.toml b/vendor/cube-hypervisor/vm-device/Cargo.toml new file mode 100644 index 0000000..5704f29 --- /dev/null +++ b/vendor/cube-hypervisor/vm-device/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "vm-device" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[features] +default = [] +kvm = ["vfio-ioctls/kvm"] +mshv = ["vfio-ioctls/mshv"] + +[dependencies] +anyhow = "1.0.86" +hypervisor = { path = "../hypervisor" } +thiserror = "1.0.37" +serde = { version = "1.0.147", features = ["rc", "derive"] } +vfio-ioctls = { git = "https://github.com/rust-vmm/vfio", rev = "64171f3", default-features = false } +vm-memory = { workspace = true, features = ["backend-mmap"] } +vmm-sys-util = "0.12.1" + diff --git a/vendor/cube-hypervisor/vm-device/src/bus.rs b/vendor/cube-hypervisor/vm-device/src/bus.rs new file mode 100644 index 0000000..9f6d1eb --- /dev/null +++ b/vendor/cube-hypervisor/vm-device/src/bus.rs @@ -0,0 +1,359 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + +//! Handles routing to devices in an address space. + +use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}; +use std::collections::btree_map::BTreeMap; +use std::sync::{Arc, Barrier, Mutex, RwLock, Weak}; +use std::{convert, error, fmt, io, result}; + +/// Trait for devices that respond to reads or writes in an arbitrary address space. +/// +/// The device does not care where it exists in address space as each method is only given an offset +/// into its allocated portion of address space. +#[allow(unused_variables)] +pub trait BusDevice: Send { + /// Reads at `offset` from this device + fn read(&mut self, base: u64, offset: u64, data: &mut [u8]) {} + /// Writes at `offset` into this device + fn write(&mut self, base: u64, offset: u64, data: &[u8]) -> Option> { + None + } +} + +#[derive(Debug)] +pub enum Error { + /// The insertion failed because the new device overlapped with an old device. + Overlap, + /// Failed to operate on zero sized range. + ZeroSizedRange, + /// Failed to find address range. + MissingAddressRange, +} + +pub type Result = result::Result; + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "bus_error: {:?}", self) + } +} + +impl error::Error for Error {} + +impl convert::From for io::Error { + fn from(e: Error) -> Self { + io::Error::new(io::ErrorKind::Other, e) + } +} + +/// Holds a base and length representing the address space occupied by a `BusDevice`. +/// +/// * base - The address at which the range start. +/// * len - The length of the range in bytes. +#[derive(Debug, Copy, Clone)] +pub struct BusRange { + pub base: u64, + pub len: u64, +} + +impl BusRange { + /// Returns true if there is overlap with the given range. + pub fn overlaps(&self, base: u64, len: u64) -> bool { + self.base < (base + len) && base < self.base + self.len + } +} + +impl Eq for BusRange {} + +impl PartialEq for BusRange { + fn eq(&self, other: &BusRange) -> bool { + self.base == other.base + } +} + +impl Ord for BusRange { + fn cmp(&self, other: &BusRange) -> Ordering { + self.base.cmp(&other.base) + } +} + +impl PartialOrd for BusRange { + fn partial_cmp(&self, other: &BusRange) -> Option { + Some(self.cmp(other)) + } +} + +/// A device container for routing reads and writes over some address space. +/// +/// This doesn't have any restrictions on what kind of device or address space this applies to. The +/// only restriction is that no two devices can overlap in this address space. +#[derive(Default)] +pub struct Bus { + devices: RwLock>>>, +} + +impl Bus { + /// Constructs an a bus with an empty address space. + pub fn new() -> Bus { + Bus { + devices: RwLock::new(BTreeMap::new()), + } + } + + fn first_before(&self, addr: u64) -> Option<(BusRange, Arc>)> { + let devices = self.devices.read().unwrap(); + let (range, dev) = devices + .range(..=BusRange { base: addr, len: 1 }) + .next_back()?; + dev.upgrade().map(|d| (*range, d.clone())) + } + + #[allow(clippy::type_complexity)] + pub fn resolve(&self, addr: u64) -> Option<(u64, u64, Arc>)> { + if let Some((range, dev)) = self.first_before(addr) { + let offset = addr - range.base; + if offset < range.len { + return Some((range.base, offset, dev)); + } + } + None + } + + /// Puts the given device at the given address space. + pub fn insert(&self, device: Arc>, base: u64, len: u64) -> Result<()> { + if len == 0 { + return Err(Error::ZeroSizedRange); + } + + // Reject all cases where the new device's range overlaps with an existing device. + if self + .devices + .read() + .unwrap() + .iter() + .any(|(range, _dev)| range.overlaps(base, len)) + { + return Err(Error::Overlap); + } + + if self + .devices + .write() + .unwrap() + .insert(BusRange { base, len }, Arc::downgrade(&device)) + .is_some() + { + return Err(Error::Overlap); + } + + Ok(()) + } + + /// Removes the device at the given address space range. + pub fn remove(&self, base: u64, len: u64) -> Result<()> { + if len == 0 { + return Err(Error::ZeroSizedRange); + } + + let bus_range = BusRange { base, len }; + + if self.devices.write().unwrap().remove(&bus_range).is_none() { + return Err(Error::MissingAddressRange); + } + + Ok(()) + } + + /// Removes all entries referencing the given device. + pub fn remove_by_device(&self, device: &Arc>) -> Result<()> { + let mut device_list = self.devices.write().unwrap(); + let mut remove_key_list = Vec::new(); + + for (key, value) in device_list.iter() { + if Arc::ptr_eq(&value.upgrade().unwrap(), device) { + remove_key_list.push(*key); + } + } + + for key in remove_key_list.iter() { + device_list.remove(key); + } + + Ok(()) + } + + /// Updates the address range for an existing device. + pub fn update_range( + &self, + old_base: u64, + old_len: u64, + new_base: u64, + new_len: u64, + ) -> Result<()> { + // Retrieve the device corresponding to the range + let device = if let Some((_, _, dev)) = self.resolve(old_base) { + dev.clone() + } else { + return Err(Error::MissingAddressRange); + }; + + // Remove the old address range + self.remove(old_base, old_len)?; + + // Insert the new address range + self.insert(device, new_base, new_len) + } + + /// Reads data from the device that owns the range containing `addr` and puts it into `data`. + /// + /// Returns true on success, otherwise `data` is untouched. + pub fn read(&self, addr: u64, data: &mut [u8]) -> Result<()> { + if let Some((base, offset, dev)) = self.resolve(addr) { + // OK to unwrap as lock() failing is a serious error condition and should panic. + dev.lock() + .expect("Failed to acquire device lock") + .read(base, offset, data); + Ok(()) + } else { + Err(Error::MissingAddressRange) + } + } + + /// Writes `data` to the device that owns the range containing `addr`. + /// + /// Returns true on success, otherwise `data` is untouched. + pub fn write(&self, addr: u64, data: &[u8]) -> Result>> { + if let Some((base, offset, dev)) = self.resolve(addr) { + // OK to unwrap as lock() failing is a serious error condition and should panic. + Ok(dev + .lock() + .expect("Failed to acquire device lock") + .write(base, offset, data)) + } else { + Err(Error::MissingAddressRange) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + struct DummyDevice; + impl BusDevice for DummyDevice {} + + struct ConstantDevice; + impl BusDevice for ConstantDevice { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + for (i, v) in data.iter_mut().enumerate() { + *v = (offset as u8) + (i as u8); + } + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + for (i, v) in data.iter().enumerate() { + assert_eq!(*v, (offset as u8) + (i as u8)) + } + + None + } + } + + #[test] + fn bus_insert() { + let bus = Bus::new(); + let dummy = Arc::new(Mutex::new(DummyDevice)); + assert!(bus.insert(dummy.clone(), 0x10, 0).is_err()); + assert!(bus.insert(dummy.clone(), 0x10, 0x10).is_ok()); + + let result = bus.insert(dummy.clone(), 0x0f, 0x10); + assert!(result.is_err()); + assert_eq!(format!("{:?}", result), "Err(Overlap)"); + + assert!(bus.insert(dummy.clone(), 0x10, 0x10).is_err()); + assert!(bus.insert(dummy.clone(), 0x10, 0x15).is_err()); + assert!(bus.insert(dummy.clone(), 0x12, 0x15).is_err()); + assert!(bus.insert(dummy.clone(), 0x12, 0x01).is_err()); + assert!(bus.insert(dummy.clone(), 0x0, 0x20).is_err()); + assert!(bus.insert(dummy.clone(), 0x20, 0x05).is_ok()); + assert!(bus.insert(dummy.clone(), 0x25, 0x05).is_ok()); + assert!(bus.insert(dummy, 0x0, 0x10).is_ok()); + } + + #[test] + #[allow(clippy::redundant_clone)] + fn bus_read_write() { + let bus = Bus::new(); + let dummy = Arc::new(Mutex::new(DummyDevice)); + assert!(bus.insert(dummy.clone(), 0x10, 0x10).is_ok()); + assert!(bus.read(0x10, &mut [0, 0, 0, 0]).is_ok()); + assert!(bus.write(0x10, &[0, 0, 0, 0]).is_ok()); + assert!(bus.read(0x11, &mut [0, 0, 0, 0]).is_ok()); + assert!(bus.write(0x11, &[0, 0, 0, 0]).is_ok()); + assert!(bus.read(0x16, &mut [0, 0, 0, 0]).is_ok()); + assert!(bus.write(0x16, &[0, 0, 0, 0]).is_ok()); + assert!(bus.read(0x20, &mut [0, 0, 0, 0]).is_err()); + assert!(bus.write(0x20, &[0, 0, 0, 0]).is_err()); + assert!(bus.read(0x06, &mut [0, 0, 0, 0]).is_err()); + assert!(bus.write(0x06, &[0, 0, 0, 0]).is_err()); + } + + #[test] + #[allow(clippy::redundant_clone)] + fn bus_read_write_values() { + let bus = Bus::new(); + let dummy = Arc::new(Mutex::new(ConstantDevice)); + assert!(bus.insert(dummy.clone(), 0x10, 0x10).is_ok()); + + let mut values = [0, 1, 2, 3]; + assert!(bus.read(0x10, &mut values).is_ok()); + assert_eq!(values, [0, 1, 2, 3]); + assert!(bus.write(0x10, &values).is_ok()); + assert!(bus.read(0x15, &mut values).is_ok()); + assert_eq!(values, [5, 6, 7, 8]); + assert!(bus.write(0x15, &values).is_ok()); + } + + #[test] + #[allow(clippy::redundant_clone)] + fn busrange_cmp() { + let range = BusRange { base: 0x10, len: 2 }; + assert_eq!(range, BusRange { base: 0x10, len: 3 }); + assert_eq!(range, BusRange { base: 0x10, len: 2 }); + + assert!(range < BusRange { base: 0x12, len: 1 }); + assert!(range < BusRange { base: 0x12, len: 3 }); + + assert_eq!(range, range.clone()); + + let bus = Bus::new(); + let mut data = [1, 2, 3, 4]; + let device = Arc::new(Mutex::new(DummyDevice)); + assert!(bus.insert(device.clone(), 0x10, 0x10).is_ok()); + assert!(bus.write(0x10, &data).is_ok()); + assert!(bus.read(0x10, &mut data).is_ok()); + assert_eq!(data, [1, 2, 3, 4]); + } + + #[test] + fn bus_range_overlap() { + let a = BusRange { + base: 0x1000, + len: 0x400, + }; + assert!(a.overlaps(0x1000, 0x400)); + assert!(a.overlaps(0xf00, 0x400)); + assert!(a.overlaps(0x1000, 0x01)); + assert!(a.overlaps(0xfff, 0x02)); + assert!(a.overlaps(0x1100, 0x100)); + assert!(a.overlaps(0x13ff, 0x100)); + assert!(!a.overlaps(0x1400, 0x100)); + assert!(!a.overlaps(0xf00, 0x100)); + } +} diff --git a/vendor/cube-hypervisor/vm-device/src/dma_mapping/mod.rs b/vendor/cube-hypervisor/vm-device/src/dma_mapping/mod.rs new file mode 100644 index 0000000..62b5ceb --- /dev/null +++ b/vendor/cube-hypervisor/vm-device/src/dma_mapping/mod.rs @@ -0,0 +1,17 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause + +pub mod vfio; + +/// Trait meant for triggering the DMA mapping update related to an external +/// device not managed fully through virtio. It is dedicated to virtio-iommu +/// in order to trigger the map update anytime the mapping is updated from the +/// guest. +pub trait ExternalDmaMapping: Send + Sync { + /// Map a memory range + fn map(&self, iova: u64, gpa: u64, size: u64) -> std::result::Result<(), std::io::Error>; + + /// Unmap a memory range + fn unmap(&self, iova: u64, size: u64) -> std::result::Result<(), std::io::Error>; +} diff --git a/vendor/cube-hypervisor/vm-device/src/dma_mapping/vfio.rs b/vendor/cube-hypervisor/vm-device/src/dma_mapping/vfio.rs new file mode 100644 index 0000000..5ed7f51 --- /dev/null +++ b/vendor/cube-hypervisor/vm-device/src/dma_mapping/vfio.rs @@ -0,0 +1,73 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause + +use crate::dma_mapping::ExternalDmaMapping; +use std::io; +use std::sync::Arc; +use vfio_ioctls::VfioContainer; +use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemory}; + +/// This structure implements the ExternalDmaMapping trait. It is meant to +/// be used when the caller tries to provide a way to update the mappings +/// associated with a specific VFIO container. +pub struct VfioDmaMapping { + container: Arc, + memory: Arc, +} + +impl VfioDmaMapping { + /// Create a DmaMapping object. + /// + /// # Parameters + /// * `container`: VFIO container object. + /// * `memory·: guest memory to mmap. + pub fn new(container: Arc, memory: Arc) -> Self { + VfioDmaMapping { container, memory } + } +} + +impl ExternalDmaMapping for VfioDmaMapping { + fn map(&self, iova: u64, gpa: u64, size: u64) -> std::result::Result<(), io::Error> { + let mem = self.memory.memory(); + let guest_addr = GuestAddress(gpa); + let user_addr = if mem.check_range(guest_addr, size as usize) { + mem.get_host_address(guest_addr).unwrap() as u64 + } else { + return Err(io::Error::new( + io::ErrorKind::Other, + format!( + "failed to convert guest address 0x{:x} into \ + host user virtual address", + gpa + ), + )); + }; + + self.container + .vfio_dma_map(iova, size, user_addr) + .map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!( + "failed to map memory for VFIO container, \ + iova 0x{:x}, gpa 0x{:x}, size 0x{:x}: {:?}", + iova, gpa, size, e + ), + ) + }) + } + + fn unmap(&self, iova: u64, size: u64) -> std::result::Result<(), io::Error> { + self.container.vfio_dma_unmap(iova, size).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!( + "failed to unmap memory for VFIO container, \ + iova 0x{:x}, size 0x{:x}: {:?}", + iova, size, e + ), + ) + }) + } +} diff --git a/vendor/cube-hypervisor/vm-device/src/interrupt/mod.rs b/vendor/cube-hypervisor/vm-device/src/interrupt/mod.rs new file mode 100644 index 0000000..f4f7ebe --- /dev/null +++ b/vendor/cube-hypervisor/vm-device/src/interrupt/mod.rs @@ -0,0 +1,156 @@ +// Copyright (C) 2019 Alibaba Cloud. All rights reserved. +// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause + +//! Traits and Structs to manage interrupt sources for devices. +//! +//! In system programming, an interrupt is a signal to the processor emitted by hardware or +//! software indicating an event that needs immediate attention. An interrupt alerts the processor +//! to a high-priority condition requiring the interruption of the current code the processor is +//! executing. The processor responds by suspending its current activities, saving its state, and +//! executing a function called an interrupt handler (or an interrupt service routine, ISR) to deal +//! with the event. This interruption is temporary, and, after the interrupt handler finishes, +//! unless handling the interrupt has emitted a fatal error, the processor resumes normal +//! activities. +//! +//! Hardware interrupts are used by devices to communicate that they require attention from the +//! operating system, or a bare-metal program running on the CPU if there are no OSes. The act of +//! initiating a hardware interrupt is referred to as an interrupt request (IRQ). Different devices +//! are usually associated with different interrupts using a unique value associated with each +//! interrupt. This makes it possible to know which hardware device caused which interrupts. +//! These interrupt values are often called IRQ lines, or just interrupt lines. +//! +//! Nowadays, IRQ lines is not the only mechanism to deliver device interrupts to processors. +//! MSI [(Message Signaled Interrupt)](https://en.wikipedia.org/wiki/Message_Signaled_Interrupts) +//! is another commonly used alternative in-band method of signaling an interrupt, using special +//! in-band messages to replace traditional out-of-band assertion of dedicated interrupt lines. +//! While more complex to implement in a device, message signaled interrupts have some significant +//! advantages over pin-based out-of-band interrupt signaling. Message signaled interrupts are +//! supported in PCI bus since its version 2.2, and in later available PCI Express bus. Some +//! non-PCI architectures also use message signaled interrupts. +//! +//! While IRQ is a term commonly used by Operating Systems when dealing with hardware +//! interrupts, the IRQ numbers managed by OSes are independent of the ones managed by VMM. +//! For simplicity sake, the term `Interrupt Source` is used instead of IRQ to represent both +//! pin-based interrupts and MSI interrupts. +//! +//! A device may support multiple types of interrupts, and each type of interrupt may support one +//! or multiple interrupt sources. For example, a PCI device may support: +//! * Legacy Irq: exactly one interrupt source. +//! * PCI MSI Irq: 1,2,4,8,16,32 interrupt sources. +//! * PCI MSIx Irq: 2^n(n=0-11) interrupt sources. +//! +//! A distinct Interrupt Source Identifier (ISID) will be assigned to each interrupt source. +//! An ID allocator will be used to allocate and free Interrupt Source Identifiers for devices. +//! To decouple the vm-device crate from the ID allocator, the vm-device crate doesn't take the +//! responsibility to allocate/free Interrupt Source IDs but only makes use of assigned IDs. +//! +//! The overall flow to deal with interrupts is: +//! * The VMM creates an interrupt manager +//! * The VMM creates a device manager, passing on an reference to the interrupt manager +//! * The device manager passes on an reference to the interrupt manager to all registered devices +//! * The guest kernel loads drivers for virtual devices +//! * The guest device driver determines the type and number of interrupts needed, and update the +//! device configuration +//! * The virtual device backend requests the interrupt manager to create an interrupt group +//! according to guest configuration information + +pub use hypervisor::{InterruptSourceConfig, LegacyIrqSourceConfig, MsiIrqSourceConfig}; +use std::sync::Arc; +use vmm_sys_util::eventfd::EventFd; + +/// Reuse std::io::Result to simplify interoperability among crates. +pub type Result = std::io::Result; + +/// Data type to store an interrupt source identifier. +pub type InterruptIndex = u32; + +/// Configuration data for legacy, pin based interrupt groups. +/// +/// A legacy interrupt group only takes one irq number as its configuration. +#[derive(Copy, Clone, Debug)] +pub struct LegacyIrqGroupConfig { + /// Legacy irq number. + pub irq: InterruptIndex, +} + +/// Configuration data for MSI/MSI-X interrupt groups +/// +/// MSI/MSI-X interrupt groups are basically a set of vectors. +#[derive(Copy, Clone, Debug)] +pub struct MsiIrqGroupConfig { + /// First index of the MSI/MSI-X interrupt vectors + pub base: InterruptIndex, + /// Number of vectors in the MSI/MSI-X group. + pub count: InterruptIndex, +} + +/// Trait to manage interrupt sources for virtual device backends. +/// +/// The InterruptManager implementations should protect itself from concurrent accesses internally, +/// so it could be invoked from multi-threaded context. +pub trait InterruptManager: Send + Sync { + type GroupConfig; + + /// Create an [InterruptSourceGroup](trait.InterruptSourceGroup.html) object to manage + /// interrupt sources for a virtual device + /// + /// An [InterruptSourceGroup](trait.InterruptSourceGroup.html) object manages all interrupt + /// sources of the same type for a virtual device. + /// + /// # Arguments + /// * interrupt_type: type of interrupt source. + /// * base: base Interrupt Source ID to be managed by the group object. + /// * count: number of Interrupt Sources to be managed by the group object. + fn create_group(&self, config: Self::GroupConfig) -> Result>; + + /// Destroy an [InterruptSourceGroup](trait.InterruptSourceGroup.html) object created by + /// [create_group()](trait.InterruptManager.html#tymethod.create_group). + /// + /// Assume the caller takes the responsibility to disable all interrupt sources of the group + /// before calling destroy_group(). This assumption helps to simplify InterruptSourceGroup + /// implementations. + fn destroy_group(&self, group: Arc) -> Result<()>; +} + +pub trait InterruptSourceGroup: Send + Sync { + /// Enable the interrupt sources in the group to generate interrupts. + fn enable(&self) -> Result<()> { + // Not all interrupt sources can be enabled. + // To accommodate this, we can have a no-op here. + Ok(()) + } + + /// Disable the interrupt sources in the group to generate interrupts. + fn disable(&self) -> Result<()> { + // Not all interrupt sources can be disabled. + // To accommodate this, we can have a no-op here. + Ok(()) + } + + /// Inject an interrupt from this interrupt source into the guest. + fn trigger(&self, index: InterruptIndex) -> Result<()>; + + /// Returns an interrupt notifier from this interrupt. + /// + /// An interrupt notifier allows for external components and processes + /// to inject interrupts into a guest, by writing to the file returned + /// by this method. + #[allow(unused_variables)] + fn notifier(&self, index: InterruptIndex) -> Option; + + /// Update the interrupt source group configuration. + /// + /// # Arguments + /// * index: sub-index into the group. + /// * config: configuration data for the interrupt source. + /// * masked: if the interrupt is masked + fn update( + &self, + index: InterruptIndex, + config: InterruptSourceConfig, + masked: bool, + ) -> Result<()>; +} diff --git a/vendor/cube-hypervisor/vm-device/src/lib.rs b/vendor/cube-hypervisor/vm-device/src/lib.rs new file mode 100644 index 0000000..326285c --- /dev/null +++ b/vendor/cube-hypervisor/vm-device/src/lib.rs @@ -0,0 +1,62 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#![allow(clippy::significant_drop_in_scrutinee)] + +use serde::{Deserialize, Serialize}; + +mod bus; +pub mod dma_mapping; +pub mod interrupt; + +pub use self::bus::{Bus, BusDevice, Error as BusError}; + +/// Type of Message Signalled Interrupt +#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum MsiIrqType { + /// PCI MSI IRQ numbers. + PciMsi, + /// PCI MSIx IRQ numbers. + PciMsix, + /// Generic MSI IRQ numbers. + GenericMsi, +} + +#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] +pub enum PciBarType { + Io, + Mmio32, + Mmio64, +} + +/// Enumeration for device resources. +#[allow(missing_docs)] +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum Resource { + /// IO Port address range. + PioAddressRange { base: u16, size: u16 }, + /// Memory Mapped IO address range. + MmioAddressRange { base: u64, size: u64 }, + /// PCI BAR + PciBar { + index: usize, + base: u64, + size: u64, + type_: PciBarType, + prefetchable: bool, + }, + /// Legacy IRQ number. + LegacyIrq(u32), + /// Message Signaled Interrupt + MsiIrq { + ty: MsiIrqType, + base: u32, + size: u32, + }, + /// Network Interface Card MAC address. + MacAddress(String), + /// KVM memslot index. + KvmMemSlot(u32), +} diff --git a/vendor/cube-hypervisor/vm-migration/Cargo.toml b/vendor/cube-hypervisor/vm-migration/Cargo.toml new file mode 100644 index 0000000..6f46921 --- /dev/null +++ b/vendor/cube-hypervisor/vm-migration/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "vm-migration" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[dependencies] +anyhow = "1.0.86" +thiserror = "1.0.37" +serde = { workspace = true, features = ["rc", "derive"] } +serde_json = "1.0.87" +vm-memory = { workspace = true, features = ["backend-mmap", "backend-atomic"] } diff --git a/vendor/cube-hypervisor/vm-migration/src/lib.rs b/vendor/cube-hypervisor/vm-migration/src/lib.rs new file mode 100644 index 0000000..5c77bc6 --- /dev/null +++ b/vendor/cube-hypervisor/vm-migration/src/lib.rs @@ -0,0 +1,337 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause +// + +use crate::protocol::MemoryRangeTable; +use anyhow::anyhow; +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +pub mod protocol; + +#[derive(Error, Debug)] +pub enum MigratableError { + #[error("Failed to pause migratable component: {0}")] + Pause(#[source] anyhow::Error), + + #[error("Failed to resume migratable component: {0}")] + Resume(#[source] anyhow::Error), + + #[error("Failed to snapshot migratable component: {0}")] + Snapshot(#[source] anyhow::Error), + + #[error("Failed to restore migratable component: {0}")] + Restore(#[source] anyhow::Error), + + #[error("Failed to send migratable component snapshot: {0}")] + MigrateSend(#[source] anyhow::Error), + + #[error("Failed to receive migratable component snapshot: {0}")] + MigrateReceive(#[source] anyhow::Error), + + #[error("Socket error: {0}")] + MigrateSocket(#[source] std::io::Error), + + #[error("Failed to start migration for migratable component: {0}")] + StartDirtyLog(#[source] anyhow::Error), + + #[error("Failed to stop migration for migratable component: {0}")] + StopDirtyLog(#[source] anyhow::Error), + + #[error("Failed to retrieve dirty ranges for migratable component: {0}")] + DirtyLog(#[source] anyhow::Error), + + #[error("Failed to start migration for migratable component: {0}")] + StartMigration(#[source] anyhow::Error), + + #[error("Failed to complete migration for migratable component: {0}")] + CompleteMigration(#[source] anyhow::Error), +} + +/// A Pausable component can be paused and resumed. +pub trait Pausable { + /// Pause the component. + fn pause(&mut self) -> std::result::Result<(), MigratableError> { + Ok(()) + } + + /// Resume the component. + fn resume(&mut self) -> std::result::Result<(), MigratableError> { + Ok(()) + } +} + +/// A Snapshottable component snapshot section. +/// Migratable component can split their migration snapshot into +/// separate sections. +/// Splitting a component migration data into different sections +/// allows for easier and forward compatible extensions. +#[derive(Clone, Default, Deserialize, Serialize)] +pub struct SnapshotDataSection { + /// The section id. + pub id: String, + + /// The section serialized snapshot. + pub snapshot: String, +} + +impl SnapshotDataSection { + /// Generate the state data from the snapshot data + pub fn to_state<'a, T>(&'a self) -> Result + where + T: Deserialize<'a>, + { + serde_json::from_str(&self.snapshot).map_err(|e| { + MigratableError::Restore(anyhow!("Error deserialising: {} {}", self.id, e)) + }) + } + + /// Create from state that can be serialized + pub fn new_from_state(id: &str, state: &T) -> Result + where + T: Serialize, + { + let snapshot = serde_json::to_string(state) + .map_err(|e| MigratableError::Snapshot(anyhow!("Error serialising: {} {}", id, e)))?; + + let snapshot_data = SnapshotDataSection { + id: format!("{}-section", id), + snapshot, + }; + + Ok(snapshot_data) + } +} + +/// The type of snapshot to create +#[derive(Clone, Copy, Deserialize, Serialize, Debug, PartialEq, Eq, Default)] +#[serde(rename_all = "lowercase")] +pub enum SnapshotType { + /// Full snapshot - saves complete VM memory + #[default] + Full, + /// Incremental snapshot - only saves CoW anonymous pages, classified from + /// pagemap bit 61 (`PM_FILE`) + Incremental, + /// Soft-dirty snapshot - only saves pages written since the previous + /// soft-dirty snapshot (true delta), via /proc/self/clear_refs + + /// /proc/self/pagemap bit 55. Requires CONFIG_MEM_SOFT_DIRTY=y; on + /// kernels without this support the runtime silently falls back to the + /// `Incremental` (pagemap_anon) path. + #[serde(rename = "soft-dirty")] + SoftDirty, +} + +impl std::fmt::Display for SnapshotType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + SnapshotType::Full => write!(f, "full"), + SnapshotType::Incremental => write!(f, "incremental"), + SnapshotType::SoftDirty => write!(f, "soft-dirty"), + } + } +} + +impl std::str::FromStr for SnapshotType { + type Err = String; + + fn from_str(s: &str) -> std::result::Result { + match s.to_lowercase().as_str() { + "full" => Ok(SnapshotType::Full), + "incremental" => Ok(SnapshotType::Incremental), + "soft-dirty" => Ok(SnapshotType::SoftDirty), + _ => Err(format!( + "Invalid snapshot type: '{}'. Valid values are 'full', 'incremental' or 'soft-dirty'", + s + )), + } + } +} + +/// Metadata associated with a snapshot +#[derive(Clone, Default, Deserialize, Serialize)] +pub struct SnapshotMetadata { + /// The type of snapshot + #[serde(default)] + pub snapshot_type: SnapshotType, +} + +/// Configuration for creating a snapshot +#[derive(Clone, Default, serde::Deserialize, serde::Serialize, Debug)] +pub struct SnapshotConfig { + /// The snapshot destination URL + pub destination_url: String, + /// The type of snapshot to create (full or incremental) + #[serde(default)] + pub snapshot_type: SnapshotType, + /// Optional existing memory blob path for storing memory range data on a + /// separate volume. + /// Accepts either an absolute path like `/dev/vdb` or a `file:///dev/vdb` + /// URL. Other JSON data (config, state) is still saved to destination_url. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub memory_vol_url: Option, +} + +/// A Snapshottable component's snapshot is a tree of snapshots, where leafs +/// contain the snapshot data. Nodes of this tree track all their children +/// through the snapshots field, which is basically their sub-components. +/// Leaves will typically have an empty snapshots map, while nodes usually +/// carry an empty snapshot_data. +/// +/// For example, a device manager snapshot is the composition of all its +/// devices snapshots. The device manager Snapshot would have no snapshot_data +/// but one Snapshot child per tracked device. Then each device's Snapshot +/// would carry an empty snapshots map but a map of SnapshotDataSection, i.e. +/// the actual device snapshot data. +#[derive(Clone, Default, Deserialize, Serialize)] +pub struct Snapshot { + /// The Snapshottable component id. + pub id: String, + + /// The Snapshottable component snapshots. + pub snapshots: std::collections::BTreeMap>, + + /// The Snapshottable component's snapshot data. + /// A map of snapshot sections, indexed by the section ids. + pub snapshot_data: std::collections::HashMap, + + /// Optional metadata about the snapshot (e.g. snapshot type). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub metadata: Option, +} + +impl Snapshot { + /// Create an empty Snapshot. + pub fn new(id: &str) -> Self { + Snapshot { + id: id.to_string(), + ..Default::default() + } + } + + /// Create from state that can be serialized + pub fn new_from_state(id: &str, state: &T) -> Result + where + T: Serialize, + { + let mut snapshot_data = Snapshot::new(id); + snapshot_data.add_data_section(SnapshotDataSection::new_from_state(id, state)?); + + Ok(snapshot_data) + } + + /// Add a sub-component's Snapshot to the Snapshot. + pub fn add_snapshot(&mut self, snapshot: Snapshot) { + self.snapshots + .insert(snapshot.id.clone(), Box::new(snapshot)); + } + + /// Add a SnapshotDatasection to the component snapshot data. + pub fn add_data_section(&mut self, section: SnapshotDataSection) { + self.snapshot_data.insert(section.id.clone(), section); + } + + /// Generate the state data from the snapshot + pub fn to_state<'a, T>(&'a self, id: &str) -> Result + where + T: Deserialize<'a>, + { + self.snapshot_data + .get(&format!("{}-section", id)) + .ok_or_else(|| MigratableError::Restore(anyhow!("Missing section for {}", id)))? + .to_state() + } +} + +pub fn snapshot_from_id(snapshot: Option<&Snapshot>, id: &str) -> Option { + snapshot.and_then(|s| s.snapshots.get(id).map(|s| *s.clone())) +} + +pub fn state_from_id<'a, T>(s: Option<&'a Snapshot>, id: &str) -> Result, MigratableError> +where + T: Deserialize<'a>, +{ + if let Some(s) = s.as_ref() { + s.snapshots.get(id).map(|s| s.to_state(id)).transpose() + } else { + Ok(None) + } +} + +/// A snapshottable component can be snapshotted. +pub trait Snapshottable: Pausable { + /// The snapshottable component id. + fn id(&self) -> String { + String::new() + } + + /// Take a component snapshot. + fn snapshot(&mut self) -> std::result::Result { + Ok(Snapshot::new("")) + } + + /// Restore a component from its snapshot. + fn restore(&mut self, _snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + Ok(()) + } +} + +/// A transportable component can be sent or receive to a specific URL. +/// +/// This trait is meant to be used for component that have custom +/// transport handlers. +pub trait Transportable: Pausable + Snapshottable { + /// Send a component snapshot. + /// + /// # Arguments + /// + /// * `snapshot` - The migratable component snapshot to send. + /// * `config` - The snapshot configuration containing destination URL and snapshot type. + fn send( + &self, + _snapshot: &Snapshot, + _config: &SnapshotConfig, + ) -> std::result::Result<(), MigratableError> { + Ok(()) + } + + /// Receive a component snapshot. + /// + /// # Arguments + /// + /// * `source_url` - The source URL to fetch the snapshot from. This could be an HTTP + /// endpoint, a TCP address or a local file. + fn recv(&self, _source_url: &str) -> std::result::Result { + Ok(Snapshot::new("")) + } +} + +/// Trait to be implemented by any component (device, CPU, RAM, etc) that +/// can be migrated. +/// All migratable components are paused before being snapshotted, and then +/// eventually resumed. Thus any Migratable component must be both Pausable +/// and Snapshottable. +/// Moreover a migratable component can be transported to a remote or local +/// destination and thus must be Transportable. +pub trait Migratable: Send + Pausable + Snapshottable + Transportable { + fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + Ok(()) + } + + fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + Ok(()) + } + + fn dirty_log(&mut self) -> std::result::Result { + Ok(MemoryRangeTable::default()) + } + + fn start_migration(&mut self) -> std::result::Result<(), MigratableError> { + Ok(()) + } + + fn complete_migration(&mut self) -> std::result::Result<(), MigratableError> { + Ok(()) + } +} diff --git a/vendor/cube-hypervisor/vm-migration/src/protocol.rs b/vendor/cube-hypervisor/vm-migration/src/protocol.rs new file mode 100644 index 0000000..075e1de --- /dev/null +++ b/vendor/cube-hypervisor/vm-migration/src/protocol.rs @@ -0,0 +1,293 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::MigratableError; +use serde::{Deserialize, Serialize}; +use std::io::{Read, Write}; +use vm_memory::ByteValued; + +// Migration protocol +// 1: Source establishes communication with destination (file socket or TCP connection.) +// (The establishment is out of scope.) +// 2: Source -> Dest : send "start command" +// 3: Dest -> Source : sends "ok response" when read to accept state data +// 4: Source -> Dest : sends "config command" followed by config data, length +// in command is length of config data +// 5: Dest -> Source : sends "ok response" when ready to accept memory data +// 6: Source -> Dest : send "memory command" followed by table of u64 pairs (GPA, size) +// followed by the memory described in those pairs. +// !! length is size of table i.e. 16 * number of ranges !! +// 7: Dest -> Source : sends "ok response" when ready to accept more memory data +// 8..(n-4): Repeat steps 6 and 7 until source has no more memory to send +// (n-3): Source -> Dest : sends "state command" followed by state data, length +// in command is length of config data +// (n-2): Dest -> Source : sends "ok response" +// (n-1): Source -> Dest : send "complete command" +// n: Dest -> Source: sends "ok response" +// +// "Local version": (Handing FDs across socket for memory) +// 1: Source establishes communication with destination (file socket or TCP connection.) +// (The establishment is out of scope.) +// 2: Source -> Dest : send "start command" +// 3: Dest -> Source : sends "ok response" when read to accept state data +// 4: Source -> Dest : sends "config command" followed by config data, length +// in command is length of config data +// 5: Dest -> Source : sends "ok response" when ready to accept memory data +// 6: Source -> Dest : send "memory fd command" followed by u16 slot ID and FD for memory +// 7: Dest -> Source : sends "ok response" when received +// 8..(n-4): Repeat steps 6 and 7 until source has no more memory to send +// (n-3): Source -> Dest : sends "state command" followed by state data, length +// in command is length of config data +// (n-2): Dest -> Source : sends "ok response" +// (n-1): Source -> Dest : send "complete command" +// n: Dest -> Source: sends "ok response" +// +// The destination can at any time send an "error response" to cancel +// The source can at any time send an "abandon request" to cancel + +#[repr(u16)] +#[derive(Copy, Clone)] +pub enum Command { + Invalid, + Start, + Config, + State, + Memory, + Complete, + Abandon, + MemoryFd, +} + +impl Default for Command { + fn default() -> Self { + Self::Invalid + } +} + +#[repr(C)] +#[derive(Default, Copy, Clone)] +pub struct Request { + command: Command, + padding: [u8; 6], + length: u64, // Length of payload for command excluding the Request struct +} + +// SAFETY: Request contains a series of integers with no implicit padding +unsafe impl ByteValued for Request {} + +impl Request { + pub fn new(command: Command, length: u64) -> Self { + Self { + command, + length, + ..Default::default() + } + } + + pub fn start() -> Self { + Self::new(Command::Start, 0) + } + + pub fn state(length: u64) -> Self { + Self::new(Command::State, length) + } + + pub fn config(length: u64) -> Self { + Self::new(Command::Config, length) + } + + pub fn memory(length: u64) -> Self { + Self::new(Command::Memory, length) + } + + pub fn memory_fd(length: u64) -> Self { + Self::new(Command::MemoryFd, length) + } + + pub fn complete() -> Self { + Self::new(Command::Complete, 0) + } + + pub fn abandon() -> Self { + Self::new(Command::Abandon, 0) + } + + pub fn command(&self) -> Command { + self.command + } + + pub fn length(&self) -> u64 { + self.length + } + + pub fn read_from(fd: &mut dyn Read) -> Result { + let mut request = Request::default(); + fd.read_exact(Self::as_mut_slice(&mut request)) + .map_err(MigratableError::MigrateSocket)?; + + Ok(request) + } + + pub fn write_to(&self, fd: &mut dyn Write) -> Result<(), MigratableError> { + fd.write_all(Self::as_slice(self)) + .map_err(MigratableError::MigrateSocket) + } +} + +#[repr(u16)] +#[derive(Copy, Clone, PartialEq, Eq)] +pub enum Status { + Invalid, + Ok, + Error, +} + +impl Default for Status { + fn default() -> Self { + Self::Invalid + } +} + +#[repr(C)] +#[derive(Default, Copy, Clone)] +pub struct Response { + status: Status, + padding: [u8; 6], + length: u64, // Length of payload for command excluding the Response struct +} + +// SAFETY: Response contains a series of integers with no implicit padding +unsafe impl ByteValued for Response {} + +impl Response { + pub fn new(status: Status, length: u64) -> Self { + Self { + status, + length, + ..Default::default() + } + } + + pub fn ok() -> Self { + Self::new(Status::Ok, 0) + } + + pub fn error() -> Self { + Self::new(Status::Error, 0) + } + + pub fn status(&self) -> Status { + self.status + } + + pub fn read_from(fd: &mut dyn Read) -> Result { + let mut response = Response::default(); + fd.read_exact(Self::as_mut_slice(&mut response)) + .map_err(MigratableError::MigrateSocket)?; + + Ok(response) + } + + pub fn write_to(&self, fd: &mut dyn Write) -> Result<(), MigratableError> { + fd.write_all(Self::as_slice(self)) + .map_err(MigratableError::MigrateSocket) + } +} + +#[repr(C)] +#[derive(Clone, Default, Serialize, Deserialize)] +pub struct MemoryRange { + pub gpa: u64, + pub length: u64, +} + +#[derive(Clone, Default, Serialize, Deserialize)] +pub struct MemoryRangeTable { + data: Vec, +} + +impl MemoryRangeTable { + pub fn from_bitmap(bitmap: Vec, start_addr: u64, page_size: u64) -> Self { + let mut table = MemoryRangeTable::default(); + let mut entry: Option = None; + for (i, block) in bitmap.iter().enumerate() { + for j in 0..64 { + let is_page_dirty = ((block >> j) & 1u64) != 0u64; + let page_offset = ((i * 64) + j) as u64 * page_size; + if is_page_dirty { + if let Some(entry) = &mut entry { + entry.length += page_size; + } else { + entry = Some(MemoryRange { + gpa: start_addr + page_offset, + length: page_size, + }); + } + } else if let Some(entry) = entry.take() { + table.push(entry); + } + } + } + if let Some(entry) = entry.take() { + table.push(entry); + } + + table + } + + pub fn regions(&self) -> &[MemoryRange] { + &self.data + } + + pub fn push(&mut self, range: MemoryRange) { + self.data.push(range) + } + + pub fn read_from(fd: &mut dyn Read, length: u64) -> Result { + assert!(length as usize % std::mem::size_of::() == 0); + + let mut data: Vec = Vec::new(); + data.resize_with( + length as usize / (std::mem::size_of::()), + Default::default, + ); + fd.read_exact(unsafe { + std::slice::from_raw_parts_mut( + data.as_ptr() as *mut MemoryRange as *mut u8, + length as usize, + ) + }) + .map_err(MigratableError::MigrateSocket)?; + + Ok(Self { data }) + } + + pub fn length(&self) -> u64 { + (std::mem::size_of::() * self.data.len()) as u64 + } + + pub fn write_to(&self, fd: &mut dyn Write) -> Result<(), MigratableError> { + fd.write_all(unsafe { + std::slice::from_raw_parts(self.data.as_ptr() as *const u8, self.length() as usize) + }) + .map_err(MigratableError::MigrateSocket) + } + + pub fn is_empty(&self) -> bool { + self.data.is_empty() + } + + pub fn extend(&mut self, table: Self) { + self.data.extend(table.data) + } + + pub fn new_from_tables(tables: Vec) -> Self { + let mut data = Vec::new(); + for table in tables { + data.extend(table.data); + } + Self { data } + } +} diff --git a/vendor/cube-hypervisor/vm-virtio/Cargo.toml b/vendor/cube-hypervisor/vm-virtio/Cargo.toml new file mode 100644 index 0000000..741aa10 --- /dev/null +++ b/vendor/cube-hypervisor/vm-virtio/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "vm-virtio" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[features] +default = [] + +[dependencies] +log = "0.4.17" +virtio-queue = { workspace = true } +vm-memory = { workspace = true, features = ["backend-mmap", "backend-atomic", "backend-bitmap"] } diff --git a/vendor/cube-hypervisor/vm-virtio/src/checked_descriptor.rs b/vendor/cube-hypervisor/vm-virtio/src/checked_descriptor.rs new file mode 100644 index 0000000..abcb39c --- /dev/null +++ b/vendor/cube-hypervisor/vm-virtio/src/checked_descriptor.rs @@ -0,0 +1,185 @@ +// Copyright (c) 2026 Cloud Hypervisor Authors +// +// SPDX-License-Identifier: Apache-2.0 + +//! Validates virtio descriptor buffers before a device accesses guest memory. + +use std::ops::Deref; + +use log::warn; +use virtio_queue::{Descriptor, DescriptorChain}; +use vm_memory::{GuestAddress, GuestMemory}; + +use crate::AccessPlatform; + +/// A descriptor whose complete buffer range has been validated. +#[derive(Debug)] +pub struct CheckedDescriptor { + inner: Descriptor, + addr: GuestAddress, +} + +impl CheckedDescriptor { + pub fn addr(&self) -> GuestAddress { + self.addr + } + + pub fn len(&self) -> u32 { + self.inner.len() + } + + pub fn is_write_only(&self) -> bool { + self.inner.is_write_only() + } +} + +/// Iterator adapter that stops at the first descriptor whose translated +/// buffer is not fully backed by guest memory. +pub struct CheckedDescriptorIter<'a, M> { + chain: &'a mut DescriptorChain, + access_platform: Option<&'a dyn AccessPlatform>, + done: bool, +} + +impl<'a, M> CheckedDescriptorIter<'a, M> +where + M: Deref, + M::Target: GuestMemory, +{ + fn new( + chain: &'a mut DescriptorChain, + access_platform: Option<&'a dyn AccessPlatform>, + ) -> Self { + Self { + chain, + access_platform, + done: false, + } + } +} + +impl Iterator for CheckedDescriptorIter<'_, M> +where + M: Deref, + M::Target: GuestMemory, +{ + type Item = Result; + + fn next(&mut self) -> Option { + if self.done { + return None; + } + + let desc = self.chain.next()?; + if desc.len() == 0 { + return Some(Ok(CheckedDescriptor { + addr: desc.addr(), + inner: desc, + })); + } + + let desc_addr = desc.addr(); + let desc_len = desc.len() as usize; + let translated = match self.access_platform { + Some(access_platform) => access_platform + .translate_gva(desc_addr.0, desc_len as u64) + .map(GuestAddress), + None => Ok(desc_addr), + }; + let result = translated.map_err(|_| desc_addr).and_then(|addr| { + if self.chain.memory().check_range(addr, desc_len) { + Ok(CheckedDescriptor { inner: desc, addr }) + } else { + Err(addr) + } + }); + + if let Err(addr) = result { + warn!( + "Rejecting virtio descriptor buffer at 0x{:x} with length {}", + addr.0, desc_len + ); + self.done = true; + } + Some(result) + } +} + +/// Extension trait providing checked descriptor iteration. +pub trait DescriptorChainExt { + fn checked_iter<'a>( + &'a mut self, + access_platform: Option<&'a dyn AccessPlatform>, + ) -> CheckedDescriptorIter<'a, M>; + + fn next_checked( + &mut self, + access_platform: Option<&dyn AccessPlatform>, + ) -> Result, GuestAddress>; +} + +impl DescriptorChainExt for DescriptorChain +where + M: Deref, + M::Target: GuestMemory, +{ + fn checked_iter<'a>( + &'a mut self, + access_platform: Option<&'a dyn AccessPlatform>, + ) -> CheckedDescriptorIter<'a, M> { + CheckedDescriptorIter::new(self, access_platform) + } + + fn next_checked( + &mut self, + access_platform: Option<&dyn AccessPlatform>, + ) -> Result, GuestAddress> { + match self.checked_iter(access_platform).next() { + Some(Ok(desc)) => Ok(Some(desc)), + Some(Err(addr)) => Err(addr), + None => Ok(None), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::queue::testing::VirtQueue; + use virtio_queue::{Queue, QueueT}; + use vm_memory::bitmap::AtomicBitmap; + use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap}; + + type TestMemory = GuestMemoryMmap; + + fn queue_for( + mem_size: usize, + addr: u64, + len: u32, + ) -> (TestMemory, GuestMemoryAtomic, Queue) { + let memory = TestMemory::from_ranges(&[(GuestAddress(0), mem_size)]).unwrap(); + let queue_memory = memory.clone(); + let guest_queue = VirtQueue::new(GuestAddress(0x1_0000), &queue_memory, 2); + guest_queue.dtable[0].set(addr, len, 0, 0); + guest_queue.avail.ring[0].set(0); + guest_queue.avail.idx.set(1); + let atomic = GuestMemoryAtomic::new(memory.clone()); + (memory, atomic, guest_queue.create_queue()) + } + + #[test] + fn rejects_descriptor_that_crosses_guest_memory_end() { + let (_memory, atomic, mut queue) = queue_for(0x20_000, 0x1_ff00, 0x200); + let mut chain = queue.pop_descriptor_chain(atomic.memory()).unwrap(); + assert!(chain.next_checked(None).is_err()); + } + + #[test] + fn accepts_descriptor_at_guest_memory_end() { + let (_memory, atomic, mut queue) = queue_for(0x20_000, 0x1_ff00, 0x100); + let mut chain = queue.pop_descriptor_chain(atomic.memory()).unwrap(); + let descriptor = chain.next_checked(None).unwrap().unwrap(); + assert_eq!(descriptor.addr(), GuestAddress(0x1_ff00)); + assert_eq!(descriptor.len(), 0x100); + } +} diff --git a/vendor/cube-hypervisor/vm-virtio/src/lib.rs b/vendor/cube-hypervisor/vm-virtio/src/lib.rs new file mode 100644 index 0000000..d4fa27c --- /dev/null +++ b/vendor/cube-hypervisor/vm-virtio/src/lib.rs @@ -0,0 +1,152 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +//! Implements virtio queues + +use std::fmt::{self, Debug}; +use std::sync::Arc; +use virtio_queue::{Queue, QueueT}; +use vm_memory::GuestAddress; + +pub mod checked_descriptor; +pub mod queue; +pub use queue::*; + +pub const VIRTIO_MSI_NO_VECTOR: u16 = 0xffff; + +// Types taken from linux/virtio_ids.h +#[derive(Copy, Clone, Debug)] +#[allow(dead_code)] +#[allow(non_camel_case_types)] +#[repr(C)] +pub enum VirtioDeviceType { + Net = 1, + Block = 2, + Console = 3, + Rng = 4, + Balloon = 5, + Fs9P = 9, + Gpu = 16, + Input = 18, + Vsock = 19, + Iommu = 23, + Mem = 24, + Fs = 26, + Pmem = 27, + Watchdog = 35, // Temporary until official number allocated + Unknown = 0xFF, +} + +impl From for VirtioDeviceType { + fn from(t: u32) -> Self { + match t { + 1 => VirtioDeviceType::Net, + 2 => VirtioDeviceType::Block, + 3 => VirtioDeviceType::Console, + 4 => VirtioDeviceType::Rng, + 5 => VirtioDeviceType::Balloon, + 9 => VirtioDeviceType::Fs9P, + 16 => VirtioDeviceType::Gpu, + 18 => VirtioDeviceType::Input, + 19 => VirtioDeviceType::Vsock, + 23 => VirtioDeviceType::Iommu, + 24 => VirtioDeviceType::Mem, + 26 => VirtioDeviceType::Fs, + 27 => VirtioDeviceType::Pmem, + 35 => VirtioDeviceType::Watchdog, + _ => VirtioDeviceType::Unknown, + } + } +} + +// In order to use the `{}` marker, the trait `fmt::Display` must be implemented +// manually for the type VirtioDeviceType. +impl fmt::Display for VirtioDeviceType { + // This trait requires `fmt` with this exact signature. + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let output = match *self { + VirtioDeviceType::Net => "net", + VirtioDeviceType::Block => "block", + VirtioDeviceType::Console => "console", + VirtioDeviceType::Rng => "rng", + VirtioDeviceType::Balloon => "balloon", + VirtioDeviceType::Gpu => "gpu", + VirtioDeviceType::Fs9P => "9p", + VirtioDeviceType::Input => "input", + VirtioDeviceType::Vsock => "vsock", + VirtioDeviceType::Iommu => "iommu", + VirtioDeviceType::Mem => "mem", + VirtioDeviceType::Fs => "fs", + VirtioDeviceType::Pmem => "pmem", + VirtioDeviceType::Watchdog => "watchdog", + VirtioDeviceType::Unknown => "UNKNOWN", + }; + write!(f, "{}", output) + } +} + +/// Trait for devices with access to data in memory being limited and/or +/// translated. +pub trait AccessPlatform: Send + Sync + Debug { + /// Provide a way to translate GVA address ranges into GPAs. + fn translate_gva(&self, base: u64, size: u64) -> std::result::Result; + /// Provide a way to translate GPA address ranges into GVAs. + fn translate_gpa(&self, base: u64, size: u64) -> std::result::Result; +} + +pub trait Translatable { + fn translate_gva(&self, access_platform: Option<&Arc>, len: usize) -> Self; + fn translate_gpa(&self, access_platform: Option<&Arc>, len: usize) -> Self; +} + +impl Translatable for GuestAddress { + fn translate_gva(&self, access_platform: Option<&Arc>, len: usize) -> Self { + GuestAddress(self.0.translate_gva(access_platform, len)) + } + fn translate_gpa(&self, access_platform: Option<&Arc>, len: usize) -> Self { + GuestAddress(self.0.translate_gpa(access_platform, len)) + } +} + +impl Translatable for u64 { + fn translate_gva(&self, access_platform: Option<&Arc>, len: usize) -> Self { + if let Some(access_platform) = access_platform { + access_platform.translate_gva(*self, len as u64).unwrap() + } else { + *self + } + } + fn translate_gpa(&self, access_platform: Option<&Arc>, len: usize) -> Self { + if let Some(access_platform) = access_platform { + access_platform.translate_gpa(*self, len as u64).unwrap() + } else { + *self + } + } +} + +/// Helper for cloning a Queue since QueueState doesn't derive Clone +pub fn clone_queue(queue: &Queue) -> Queue { + let mut q = Queue::new(queue.max_size()).unwrap(); + + q.set_next_avail(queue.next_avail()); + q.set_next_used(queue.next_used()); + q.set_event_idx(queue.event_idx_enabled()); + q.set_size(queue.size()); + q.set_ready(queue.ready()); + q.try_set_desc_table_address(GuestAddress(queue.desc_table())) + .unwrap(); + q.try_set_avail_ring_address(GuestAddress(queue.avail_ring())) + .unwrap(); + q.try_set_used_ring_address(GuestAddress(queue.used_ring())) + .unwrap(); + + q +} diff --git a/vendor/cube-hypervisor/vm-virtio/src/queue.rs b/vendor/cube-hypervisor/vm-virtio/src/queue.rs new file mode 100644 index 0000000..7bcc579 --- /dev/null +++ b/vendor/cube-hypervisor/vm-virtio/src/queue.rs @@ -0,0 +1,247 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +pub mod testing { + use std::marker::PhantomData; + use std::mem; + use virtio_queue::{Queue, QueueT, VirtqUsedElem}; + use vm_memory::{bitmap::AtomicBitmap, Address, Bytes, GuestAddress, GuestUsize}; + + type GuestMemoryMmap = vm_memory::GuestMemoryMmap; + + // Represents a location in GuestMemoryMmap which holds a given type. + pub struct SomeplaceInMemory<'a, T> { + pub location: GuestAddress, + mem: &'a GuestMemoryMmap, + phantom: PhantomData<*const T>, + } + + // The ByteValued trait is required to use mem.read_obj and write_obj. + impl<'a, T> SomeplaceInMemory<'a, T> + where + T: vm_memory::ByteValued, + { + fn new(location: GuestAddress, mem: &'a GuestMemoryMmap) -> Self { + SomeplaceInMemory { + location, + mem, + phantom: PhantomData, + } + } + + // Reads from the actual memory location. + pub fn get(&self) -> T { + self.mem.read_obj(self.location).unwrap() + } + + // Writes to the actual memory location. + pub fn set(&self, val: T) { + self.mem.write_obj(val, self.location).unwrap() + } + + // This function returns a place in memory which holds a value of type U, and starts + // offset bytes after the current location. + fn map_offset(&self, offset: GuestUsize) -> SomeplaceInMemory<'a, U> { + SomeplaceInMemory { + location: self.location.checked_add(offset).unwrap(), + mem: self.mem, + phantom: PhantomData, + } + } + + // This function returns a place in memory which holds a value of type U, and starts + // immediately after the end of self (which is location + sizeof(T)). + fn next_place(&self) -> SomeplaceInMemory<'a, U> { + self.map_offset::(mem::size_of::() as u64) + } + + fn end(&self) -> GuestAddress { + self.location + .checked_add(mem::size_of::() as u64) + .unwrap() + } + } + + // Represents a virtio descriptor in guest memory. + pub struct VirtqDesc<'a> { + pub addr: SomeplaceInMemory<'a, u64>, + pub len: SomeplaceInMemory<'a, u32>, + pub flags: SomeplaceInMemory<'a, u16>, + pub next: SomeplaceInMemory<'a, u16>, + } + + impl<'a> VirtqDesc<'a> { + pub fn new(start: GuestAddress, mem: &'a GuestMemoryMmap) -> Self { + assert_eq!(start.0 & 0xf, 0); + + let addr = SomeplaceInMemory::new(start, mem); + let len = addr.next_place(); + let flags = len.next_place(); + let next = flags.next_place(); + + VirtqDesc { + addr, + len, + flags, + next, + } + } + + fn start(&self) -> GuestAddress { + self.addr.location + } + + fn end(&self) -> GuestAddress { + self.next.end() + } + + pub fn set(&self, addr: u64, len: u32, flags: u16, next: u16) { + self.addr.set(addr); + self.len.set(len); + self.flags.set(flags); + self.next.set(next); + } + } + + // Represents a virtio queue ring. The only difference between the used and available rings, + // is the ring element type. + pub struct VirtqRing<'a, T> { + pub flags: SomeplaceInMemory<'a, u16>, + pub idx: SomeplaceInMemory<'a, u16>, + pub ring: Vec>, + pub event: SomeplaceInMemory<'a, u16>, + } + + impl<'a, T> VirtqRing<'a, T> + where + T: vm_memory::ByteValued, + { + fn new( + start: GuestAddress, + mem: &'a GuestMemoryMmap, + qsize: u16, + alignment: GuestUsize, + ) -> Self { + assert_eq!(start.0 & (alignment - 1), 0); + + let flags = SomeplaceInMemory::new(start, mem); + let idx = flags.next_place(); + + let mut ring = Vec::with_capacity(qsize as usize); + + ring.push(idx.next_place()); + + for _ in 1..qsize as usize { + let x = ring.last().unwrap().next_place(); + ring.push(x) + } + + let event = ring.last().unwrap().next_place(); + + flags.set(0); + idx.set(0); + event.set(0); + + VirtqRing { + flags, + idx, + ring, + event, + } + } + + pub fn end(&self) -> GuestAddress { + self.event.end() + } + } + + pub type VirtqAvail<'a> = VirtqRing<'a, u16>; + pub type VirtqUsed<'a> = VirtqRing<'a, VirtqUsedElem>; + + pub struct VirtQueue<'a> { + pub dtable: Vec>, + pub avail: VirtqAvail<'a>, + pub used: VirtqUsed<'a>, + pub mem: &'a GuestMemoryMmap, + } + + impl<'a> VirtQueue<'a> { + // We try to make sure things are aligned properly :-s + pub fn new(start: GuestAddress, mem: &'a GuestMemoryMmap, qsize: u16) -> Self { + // power of 2? + assert!(qsize > 0 && qsize & (qsize - 1) == 0); + + let mut dtable = Vec::with_capacity(qsize as usize); + + let mut end = start; + + for _ in 0..qsize { + let d = VirtqDesc::new(end, mem); + end = d.end(); + dtable.push(d); + } + + const AVAIL_ALIGN: u64 = 2; + + let avail = VirtqAvail::new(end, mem, qsize, AVAIL_ALIGN); + + const USED_ALIGN: u64 = 4; + + let mut x = avail.end().0; + x = (x + USED_ALIGN - 1) & !(USED_ALIGN - 1); + + let used = VirtqUsed::new(GuestAddress(x), mem, qsize, USED_ALIGN); + + VirtQueue { + dtable, + avail, + used, + mem, + } + } + + fn size(&self) -> u16 { + self.dtable.len() as u16 + } + + pub fn dtable_start(&self) -> GuestAddress { + self.dtable.first().unwrap().start() + } + + pub fn avail_start(&self) -> GuestAddress { + self.avail.flags.location + } + + pub fn used_start(&self) -> GuestAddress { + self.used.flags.location + } + + // Creates a new Queue, using the underlying memory regions represented by the VirtQueue. + pub fn create_queue(&self) -> Queue { + let mut q = Queue::new(self.size()).unwrap(); + + q.set_size(self.size()); + q.set_ready(true); + q.try_set_desc_table_address(self.dtable_start()).unwrap(); + q.try_set_avail_ring_address(self.avail_start()).unwrap(); + q.try_set_used_ring_address(self.used_start()).unwrap(); + + q + } + + pub fn start(&self) -> GuestAddress { + self.dtable_start() + } + + pub fn end(&self) -> GuestAddress { + self.used.end() + } + } +} diff --git a/vendor/cube-hypervisor/vmm/Cargo.toml b/vendor/cube-hypervisor/vmm/Cargo.toml new file mode 100644 index 0000000..f4ef359 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/Cargo.toml @@ -0,0 +1,65 @@ +[package] +name = "vmm" +version = "0.1.0" +authors = ["The Cloud Hypervisor Authors"] +edition = "2021" + +[features] +default = [] +guest_debug = ["kvm", "gdbstub", "gdbstub_arch"] +kvm = ["hypervisor/kvm", "vfio-ioctls/kvm", "vm-device/kvm", "pci/kvm"] +mshv = ["hypervisor/mshv", "vfio-ioctls/mshv", "vm-device/mshv", "pci/mshv"] +tdx = ["arch/tdx", "hypervisor/tdx"] +tracing = ["tracer/tracing"] +lib_support = ["devices/lib_support"] + +[dependencies] +acpi_tables = { path = "../acpi_tables" } +anyhow = "1.0.86" +arc-swap = "1.5.1" +async-trait = "0.1.88" +arch = { path = "../arch" } +bitflags = "2.6.0" +block_util = { path = "../block_util" } +chrono = "0.4.0" +clap = "4.4.7" +devices = { path = "../devices" } +epoll = "4.3.1" +event_monitor = { path = "../event_monitor" } +event_notifier = { path = "../event_notifier" } +gdbstub = { version = "0.6.3", optional = true } +gdbstub_arch = { version = "0.2.4", optional = true } +hypervisor = { path = "../hypervisor" } +lazy_static = "1.2" +libc = "0.2.137" +linux-loader = { workspace = true, features = ["elf", "bzimage", "pe"] } +logging = { path = "../logging" } +log = "0.4.17" +micro_http = { git = "https://github.com/firecracker-microvm/micro-http", branch = "main" } +net_util = { path = "../net_util" } +once_cell = "1.16.0" +option_parser = { path = "../option_parser" } +pci = { path = "../pci" } +qcow = { path = "../qcow" } +seccompiler = { workspace = true } +serde = { workspace = true, features = ["rc", "derive"] } +serde_json = "1.0.87" +serial_buffer = { path = "../serial_buffer" } +signal-hook = "0.3.14" +thiserror = "1.0.37" +tokio = { version = "=1.40.0", features = ["full"] } +tracer = { path = "../tracer" } +uuid = "1.2.1" +vfio-ioctls = { git = "https://github.com/rust-vmm/vfio", rev = "64171f3", default-features = false } +vfio_user = { path = "../vfio_user" } +vhdx = { path = "../vhdx" } +virtio-devices = { path = "../virtio-devices" } +virtio-queue = "0.11.0" +vm-allocator = { path = "../vm-allocator" } +vm-device = { path = "../vm-device" } +vm-memory = { workspace = true, features = ["backend-mmap", "backend-atomic", "backend-bitmap"] } +vm-migration = { path = "../vm-migration" } +vm-virtio = { path = "../vm-virtio" } +vmm-sys-util = { version = "0.12.1", features = ["with-serde"] } +virtiofsd = { workspace = true } +zerocopy = "0.6.1" diff --git a/vendor/cube-hypervisor/vmm/src/acpi.rs b/vendor/cube-hypervisor/vmm/src/acpi.rs new file mode 100644 index 0000000..eb71b05 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/acpi.rs @@ -0,0 +1,868 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// +use crate::cpu::CpuManager; +use crate::device_manager::DeviceManager; +use crate::memory_manager::MemoryManager; +use crate::pci_segment::PciSegment; +use crate::{GuestMemoryMmap, GuestRegionMmap}; +#[cfg(target_arch = "aarch64")] +use acpi_tables::sdt::GenericAddress; +use acpi_tables::{aml::Aml, rsdp::Rsdp, sdt::Sdt}; +#[cfg(target_arch = "aarch64")] +use arch::aarch64::DeviceInfoForFdt; +#[cfg(target_arch = "aarch64")] +use arch::DeviceType; +use arch::NumaNodes; +use bitflags::bitflags; +use pci::PciBdf; +use std::sync::{Arc, Mutex}; +use std::time::Instant; +use tracer::trace_scoped; +use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemoryRegion}; +use zerocopy::AsBytes; + +/* Values for Type in APIC sub-headers */ +#[cfg(target_arch = "x86_64")] +pub const ACPI_APIC_PROCESSOR: u8 = 0; +#[cfg(target_arch = "x86_64")] +pub const ACPI_APIC_IO: u8 = 1; +#[cfg(target_arch = "x86_64")] +pub const ACPI_APIC_XRUPT_OVERRIDE: u8 = 2; +#[cfg(target_arch = "aarch64")] +pub const ACPI_APIC_GENERIC_CPU_INTERFACE: u8 = 11; +#[cfg(target_arch = "aarch64")] +pub const ACPI_APIC_GENERIC_DISTRIBUTOR: u8 = 12; +#[cfg(target_arch = "aarch64")] +pub const ACPI_APIC_GENERIC_REDISTRIBUTOR: u8 = 14; +#[cfg(target_arch = "aarch64")] +pub const ACPI_APIC_GENERIC_TRANSLATOR: u8 = 15; + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Default, AsBytes)] +struct PciRangeEntry { + pub base_address: u64, + pub segment: u16, + pub start: u8, + pub end: u8, + _reserved: u32, +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Default, AsBytes)] +struct MemoryAffinity { + pub type_: u8, + pub length: u8, + pub proximity_domain: u32, + _reserved1: u16, + pub base_addr_lo: u32, + pub base_addr_hi: u32, + pub length_lo: u32, + pub length_hi: u32, + _reserved2: u32, + pub flags: u32, + _reserved3: u64, +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Default, AsBytes)] +struct ProcessorLocalX2ApicAffinity { + pub type_: u8, + pub length: u8, + _reserved1: u16, + pub proximity_domain: u32, + pub x2apic_id: u32, + pub flags: u32, + pub clock_domain: u32, + _reserved2: u32, +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Default, AsBytes)] +struct ProcessorGiccAffinity { + pub type_: u8, + pub length: u8, + pub proximity_domain: u32, + pub acpi_processor_uid: u32, + pub flags: u32, + pub clock_domain: u32, +} + +bitflags! { + pub struct MemAffinityFlags: u32 { + const NOFLAGS = 0; + const ENABLE = 0b1; + const HOTPLUGGABLE = 0b10; + const NON_VOLATILE = 0b100; + } +} + +impl MemoryAffinity { + fn from_region( + region: &Arc, + proximity_domain: u32, + flags: MemAffinityFlags, + ) -> Self { + Self::from_range( + region.start_addr().raw_value(), + region.len(), + proximity_domain, + flags, + ) + } + + fn from_range( + base_addr: u64, + size: u64, + proximity_domain: u32, + flags: MemAffinityFlags, + ) -> Self { + let base_addr_lo = (base_addr & 0xffff_ffff) as u32; + let base_addr_hi = (base_addr >> 32) as u32; + let length_lo = (size & 0xffff_ffff) as u32; + let length_hi = (size >> 32) as u32; + + MemoryAffinity { + type_: 1, + length: 40, + proximity_domain, + base_addr_lo, + base_addr_hi, + length_lo, + length_hi, + flags: flags.bits(), + ..Default::default() + } + } +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Default, AsBytes)] +struct ViotVirtioPciNode { + pub type_: u8, + _reserved: u8, + pub length: u16, + pub pci_segment: u16, + pub pci_bdf_number: u16, + _reserved2: [u8; 8], +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Default, AsBytes)] +struct ViotPciRangeNode { + pub type_: u8, + _reserved: u8, + pub length: u16, + pub endpoint_start: u32, + pub pci_segment_start: u16, + pub pci_segment_end: u16, + pub pci_bdf_start: u16, + pub pci_bdf_end: u16, + pub output_node: u16, + _reserved2: [u8; 6], +} + +pub fn create_dsdt_table( + device_manager: &Arc>, + cpu_manager: &Arc>, + memory_manager: &Arc>, +) -> Sdt { + trace_scoped!("create_dsdt_table"); + // DSDT + let mut dsdt = Sdt::new(*b"DSDT", 36, 6, *b"CLOUDH", *b"CHDSDT ", 1); + + let mut bytes = Vec::new(); + + device_manager.lock().unwrap().append_aml_bytes(&mut bytes); + cpu_manager.lock().unwrap().append_aml_bytes(&mut bytes); + memory_manager.lock().unwrap().append_aml_bytes(&mut bytes); + dsdt.append_slice(&bytes); + + dsdt +} + +fn create_facp_table(dsdt_offset: GuestAddress, device_manager: &Arc>) -> Sdt { + trace_scoped!("create_facp_table"); + + // Revision 6 of the ACPI FADT table is 276 bytes long + let mut facp = Sdt::new(*b"FACP", 276, 6, *b"CLOUDH", *b"CHFACP ", 1); + + { + let device_manager = device_manager.lock().unwrap(); + if let Some(address) = device_manager.acpi_platform_addresses().reset_reg_address { + // RESET_REG + facp.write(116, address); + // RESET_VALUE + facp.write(128, 1u8); + } + + if let Some(address) = device_manager + .acpi_platform_addresses() + .sleep_control_reg_address + { + // SLEEP_CONTROL_REG + facp.write(244, address); + } + + if let Some(address) = device_manager + .acpi_platform_addresses() + .sleep_status_reg_address + { + // SLEEP_STATUS_REG + facp.write(256, address); + } + + if let Some(address) = device_manager.acpi_platform_addresses().pm_timer_address { + // X_PM_TMR_BLK + facp.write(208, address); + } + } + + // aarch64 specific fields + #[cfg(target_arch = "aarch64")] + // ARM_BOOT_ARCH: enable PSCI with HVC enable-method + facp.write(129, 3u16); + + // Architecture common fields + // HW_REDUCED_ACPI, RESET_REG_SUP, TMR_VAL_EXT + let fadt_flags: u32 = 1 << 20 | 1 << 10 | 1 << 8; + facp.write(112, fadt_flags); + // FADT minor version + facp.write(131, 3u8); + // X_DSDT + facp.write(140, dsdt_offset.0); + // Hypervisor Vendor Identity + facp.write_bytes(268, b"CLOUDHYP"); + + facp.update_checksum(); + + facp +} + +fn create_mcfg_table(pci_segments: &[PciSegment]) -> Sdt { + let mut mcfg = Sdt::new(*b"MCFG", 36, 1, *b"CLOUDH", *b"CHMCFG ", 1); + + // MCFG reserved 8 bytes + mcfg.append(0u64); + + for segment in pci_segments { + // 32-bit PCI enhanced configuration mechanism + mcfg.append(PciRangeEntry { + base_address: segment.mmio_config_address, + segment: segment.id, + start: 0, + end: 0, + ..Default::default() + }); + } + mcfg +} + +fn create_tpm2_table() -> Sdt { + let mut tpm = Sdt::new(*b"TPM2", 52, 3, *b"CLOUDH", *b"CHTPM2 ", 1); + + tpm.write(36, 0_u16); //Platform Class + tpm.write(38, 0_u16); // Reserved Space + tpm.write(40, 0xfed4_0040_u64); // Address of Control Area + tpm.write(48, 7_u32); //Start Method + + tpm.update_checksum(); + tpm +} + +fn create_srat_table(numa_nodes: &NumaNodes) -> Sdt { + let mut srat = Sdt::new(*b"SRAT", 36, 3, *b"CLOUDH", *b"CHSRAT ", 1); + // SRAT reserved 12 bytes + srat.append_slice(&[0u8; 12]); + + // Check the MemoryAffinity structure is the right size as expected by + // the ACPI specification. + assert_eq!(std::mem::size_of::(), 40); + + for (node_id, node) in numa_nodes.iter() { + let proximity_domain = *node_id; + + for region in &node.memory_regions { + srat.append(MemoryAffinity::from_region( + region, + proximity_domain, + MemAffinityFlags::ENABLE, + )) + } + + for region in &node.hotplug_regions { + srat.append(MemoryAffinity::from_region( + region, + proximity_domain, + MemAffinityFlags::ENABLE | MemAffinityFlags::HOTPLUGGABLE, + )) + } + + #[cfg(target_arch = "x86_64")] + for section in &node.sgx_epc_sections { + srat.append(MemoryAffinity::from_range( + section.start().raw_value(), + section.size(), + proximity_domain, + MemAffinityFlags::ENABLE, + )) + } + + for cpu in &node.cpus { + let x2apic_id = *cpu as u32; + + // Flags + // - Enabled = 1 (bit 0) + // - Reserved bits 1-31 + let flags = 1; + + #[cfg(target_arch = "x86_64")] + srat.append(ProcessorLocalX2ApicAffinity { + type_: 2, + length: 24, + proximity_domain, + x2apic_id, + flags, + clock_domain: 0, + ..Default::default() + }); + #[cfg(target_arch = "aarch64")] + srat.append(ProcessorGiccAffinity { + type_: 3, + length: 18, + proximity_domain, + acpi_processor_uid: x2apic_id, + flags, + clock_domain: 0, + }); + } + } + srat +} + +fn create_slit_table(numa_nodes: &NumaNodes) -> Sdt { + let mut slit = Sdt::new(*b"SLIT", 36, 1, *b"CLOUDH", *b"CHSLIT ", 1); + // Number of System Localities on 8 bytes. + slit.append(numa_nodes.len() as u64); + + let existing_nodes: Vec = numa_nodes.keys().cloned().collect(); + for (node_id, node) in numa_nodes.iter() { + let distances = &node.distances; + for i in existing_nodes.iter() { + let dist: u8 = if *node_id == *i { + 10 + } else if let Some(distance) = distances.get(i) { + *distance + } else { + 20 + }; + + slit.append(dist); + } + } + slit +} + +#[cfg(target_arch = "aarch64")] +fn create_gtdt_table() -> Sdt { + const ARCH_TIMER_NS_EL2_IRQ: u32 = 10; + const ARCH_TIMER_VIRT_IRQ: u32 = 11; + const ARCH_TIMER_S_EL1_IRQ: u32 = 13; + const ARCH_TIMER_NS_EL1_IRQ: u32 = 14; + const ACPI_GTDT_INTERRUPT_MODE_LEVEL: u32 = 0; + const ACPI_GTDT_CAP_ALWAYS_ON: u32 = 1 << 2; + + let irqflags: u32 = ACPI_GTDT_INTERRUPT_MODE_LEVEL; + // GTDT + let mut gtdt = Sdt::new(*b"GTDT", 104, 2, *b"CLOUDH", *b"CHGTDT ", 1); + // Secure EL1 Timer GSIV + gtdt.write(48, (ARCH_TIMER_S_EL1_IRQ + 16) as u32); + // Secure EL1 Timer Flags + gtdt.write(52, irqflags); + // Non-Secure EL1 Timer GSIV + gtdt.write(56, (ARCH_TIMER_NS_EL1_IRQ + 16) as u32); + // Non-Secure EL1 Timer Flags + gtdt.write(60, (irqflags | ACPI_GTDT_CAP_ALWAYS_ON) as u32); + // Virtual EL1 Timer GSIV + gtdt.write(64, (ARCH_TIMER_VIRT_IRQ + 16) as u32); + // Virtual EL1 Timer Flags + gtdt.write(68, irqflags); + // EL2 Timer GSIV + gtdt.write(72, (ARCH_TIMER_NS_EL2_IRQ + 16) as u32); + // EL2 Timer Flags + gtdt.write(76, irqflags); + + gtdt.update_checksum(); + + gtdt +} + +#[cfg(target_arch = "aarch64")] +fn create_spcr_table(base_address: u64, gsi: u32) -> Sdt { + // SPCR + let mut spcr = Sdt::new(*b"SPCR", 80, 2, *b"CLOUDH", *b"CHSPCR ", 1); + // Interface Type + spcr.write(36, 3u8); + // Base Address in format ACPI Generic Address Structure + spcr.write(40, GenericAddress::mmio_address::(base_address)); + // Interrupt Type: Bit[3] ARMH GIC interrupt + spcr.write(52, (1 << 3) as u8); + // Global System Interrupt used by the UART + spcr.write(54, (gsi as u32).to_le()); + // Baud Rate: 3 = 9600 + spcr.write(58, 3u8); + // Stop Bits: 1 Stop bit + spcr.write(60, 1u8); + // Flow Control: Bit[1] = RTS/CTS hardware flow control + spcr.write(61, (1 << 1) as u8); + // PCI Device ID: Not a PCI device + spcr.write(64, 0xffff_u16); + // PCI Vendor ID: Not a PCI device + spcr.write(66, 0xffff_u16); + + spcr.update_checksum(); + + spcr +} + +#[cfg(target_arch = "aarch64")] +fn create_dbg2_table(base_address: u64) -> Sdt { + let namespace = "_SB_.COM1"; + let debug_device_info_offset = 44usize; + let debug_device_info_len: u16 = 22 /* BaseAddressRegisterOffset */ + + 12 /* BaseAddressRegister */ + + 4 /* AddressSize */ + + namespace.len() as u16 + 1 /* zero-terminated */; + let tbl_len: u32 = debug_device_info_offset as u32 + debug_device_info_len as u32; + let mut dbg2 = Sdt::new(*b"DBG2", tbl_len, 0, *b"CLOUDH", *b"CHDBG2 ", 1); + + /* OffsetDbgDeviceInfo */ + dbg2.write_u32(36, 44); + /* NumberDbgDeviceInfo */ + dbg2.write_u32(40, 1); + + /* Debug Device Information structure */ + /* Offsets are calculated from the start of this structure. */ + let namespace_offset = 38u16; + let base_address_register_offset = 22u16; + let address_size_offset = 34u16; + /* Revision */ + dbg2.write_u8(debug_device_info_offset, 0); + /* Length */ + dbg2.write_u16(debug_device_info_offset + 1, debug_device_info_len); + /* NumberofGenericAddressRegisters */ + dbg2.write_u8(debug_device_info_offset + 3, 1); + /* NameSpaceStringLength */ + dbg2.write_u16(debug_device_info_offset + 4, namespace.len() as u16 + 1); + /* NameSpaceStringOffset */ + dbg2.write_u16(debug_device_info_offset + 6, namespace_offset); + /* OemDataLength */ + dbg2.write_u16(debug_device_info_offset + 8, 0); + /* OemDataOffset */ + dbg2.write_u16(debug_device_info_offset + 10, 0); + /* Port Type */ + dbg2.write_u16(debug_device_info_offset + 12, 0x8000); + /* Port Subtype */ + dbg2.write_u16(debug_device_info_offset + 14, 0x0003); + /* Reserved */ + dbg2.write_u16(debug_device_info_offset + 16, 0); + /* BaseAddressRegisterOffset */ + dbg2.write_u16(debug_device_info_offset + 18, base_address_register_offset); + /* AddressSizeOffset */ + dbg2.write_u16(debug_device_info_offset + 20, address_size_offset); + /* BaseAddressRegister */ + dbg2.write( + debug_device_info_offset + base_address_register_offset as usize, + GenericAddress::mmio_address::(base_address), + ); + /* AddressSize */ + dbg2.write_u32( + debug_device_info_offset + address_size_offset as usize, + 0x1000, + ); + /* NamespaceString, zero-terminated ASCII */ + for (k, c) in namespace.chars().enumerate() { + dbg2.write_u8( + debug_device_info_offset + namespace_offset as usize + k, + c as u8, + ); + } + dbg2.write_u8( + debug_device_info_offset + namespace_offset as usize + namespace.len(), + 0, + ); + + dbg2.update_checksum(); + + dbg2 +} + +#[cfg(target_arch = "aarch64")] +fn create_iort_table(pci_segments: &[PciSegment]) -> Sdt { + const ACPI_IORT_NODE_ITS_GROUP: u8 = 0x00; + const ACPI_IORT_NODE_PCI_ROOT_COMPLEX: u8 = 0x02; + const ACPI_IORT_NODE_ROOT_COMPLEX_OFFSET: usize = 72; + const ACPI_IORT_NODE_ROOT_COMPLEX_SIZE: usize = 60; + + // The IORT table containes: + // - Header (size = 40) + // - 1 x ITS Group Node (size = 24) + // - N x Root Complex Node (N = number of pci segments, size = 60 x N) + let iort_table_size: u32 = (ACPI_IORT_NODE_ROOT_COMPLEX_OFFSET + + ACPI_IORT_NODE_ROOT_COMPLEX_SIZE * pci_segments.len()) + as u32; + let mut iort = Sdt::new(*b"IORT", iort_table_size, 2, *b"CLOUDH", *b"CHIORT ", 1); + iort.write(36, ((1 + pci_segments.len()) as u32).to_le()); + iort.write(40, (48u32).to_le()); + + // ITS group node + iort.write(48, ACPI_IORT_NODE_ITS_GROUP as u8); + // Length of the ITS group node in bytes + iort.write(49, (24u16).to_le()); + // ITS counts + iort.write(64, (1u32).to_le()); + + // Root Complex Nodes + for (i, segment) in pci_segments.iter().enumerate() { + let node_offset: usize = + ACPI_IORT_NODE_ROOT_COMPLEX_OFFSET + i * ACPI_IORT_NODE_ROOT_COMPLEX_SIZE; + iort.write(node_offset, ACPI_IORT_NODE_PCI_ROOT_COMPLEX as u8); + // Length of the root complex node in bytes + iort.write( + node_offset + 1, + (ACPI_IORT_NODE_ROOT_COMPLEX_SIZE as u16).to_le(), + ); + // Revision + iort.write(node_offset + 3, (3u8).to_le()); + // Node ID + iort.write(node_offset + 4, (segment.id as u32).to_le()); + // Mapping counts + iort.write(node_offset + 8, (1u32).to_le()); + // Offset from the start of the RC node to the start of its Array of ID mappings + iort.write(node_offset + 12, (36u32).to_le()); + // Fully coherent device + iort.write(node_offset + 16, (1u32).to_le()); + // CCA = CPM = DCAS = 1 + iort.write(node_offset + 24, 3u8); + // PCI segment number + iort.write(node_offset + 28, (segment.id as u32).to_le()); + // Memory address size limit + iort.write(node_offset + 32, (64u8).to_le()); + + // From offset 32 onward is the space for ID mappings Array. + // Now we have only one mapping. + let mapping_offset: usize = node_offset + 36; + // The lowest value in the input range + iort.write(mapping_offset, (0u32).to_le()); + // The number of IDs in the range minus one: + // This should cover all the devices of a segment: + // 1 (bus) x 32 (devices) x 8 (functions) = 256 + // Note: Currently only 1 bus is supported in a segment. + iort.write(mapping_offset + 4, (255_u32).to_le()); + // The lowest value in the output range + iort.write(mapping_offset + 8, ((256 * segment.id) as u32).to_le()); + // id_mapping_array_output_reference should be + // the ITS group node (the first node) if no SMMU + iort.write(mapping_offset + 12, (48u32).to_le()); + // Flags + iort.write(mapping_offset + 16, (0u32).to_le()); + } + + iort.update_checksum(); + + iort +} + +fn create_viot_table(iommu_bdf: &PciBdf, devices_bdf: &[PciBdf]) -> Sdt { + // VIOT + let mut viot = Sdt::new(*b"VIOT", 36, 0, *b"CLOUDH", *b"CHVIOT ", 0); + // Node count + viot.append((devices_bdf.len() + 1) as u16); + // Node offset + viot.append(48u16); + // VIOT reserved 8 bytes + viot.append_slice(&[0u8; 8]); + + // Virtio-iommu based on virtio-pci node + viot.append(ViotVirtioPciNode { + type_: 3, + length: 16, + pci_segment: iommu_bdf.segment(), + pci_bdf_number: iommu_bdf.into(), + ..Default::default() + }); + + for device_bdf in devices_bdf { + viot.append(ViotPciRangeNode { + type_: 1, + length: 24, + endpoint_start: device_bdf.into(), + pci_segment_start: device_bdf.segment(), + pci_segment_end: device_bdf.segment(), + pci_bdf_start: device_bdf.into(), + pci_bdf_end: device_bdf.into(), + output_node: 48, + ..Default::default() + }); + } + + viot +} + +pub fn create_acpi_tables( + guest_mem: &GuestMemoryMmap, + device_manager: &Arc>, + cpu_manager: &Arc>, + memory_manager: &Arc>, + numa_nodes: &NumaNodes, + tpm_enabled: bool, +) -> GuestAddress { + trace_scoped!("create_acpi_tables"); + + let start_time = Instant::now(); + let rsdp_offset = arch::layout::RSDP_POINTER; + let mut tables: Vec = Vec::new(); + + // DSDT + let dsdt = create_dsdt_table(device_manager, cpu_manager, memory_manager); + let dsdt_offset = rsdp_offset.checked_add(Rsdp::len() as u64).unwrap(); + guest_mem + .write_slice(dsdt.as_slice(), dsdt_offset) + .expect("Error writing DSDT table"); + + // FACP aka FADT + let facp = create_facp_table(dsdt_offset, device_manager); + let facp_offset = dsdt_offset.checked_add(dsdt.len() as u64).unwrap(); + guest_mem + .write_slice(facp.as_slice(), facp_offset) + .expect("Error writing FACP table"); + tables.push(facp_offset.0); + + // MADT + let madt = cpu_manager.lock().unwrap().create_madt(); + let madt_offset = facp_offset.checked_add(facp.len() as u64).unwrap(); + guest_mem + .write_slice(madt.as_slice(), madt_offset) + .expect("Error writing MADT table"); + tables.push(madt_offset.0); + let mut prev_tbl_len = madt.len() as u64; + let mut prev_tbl_off = madt_offset; + + // PPTT + #[cfg(target_arch = "aarch64")] + { + let pptt = cpu_manager.lock().unwrap().create_pptt(); + let pptt_offset = prev_tbl_off.checked_add(prev_tbl_len).unwrap(); + guest_mem + .write_slice(pptt.as_slice(), pptt_offset) + .expect("Error writing PPTT table"); + tables.push(pptt_offset.0); + prev_tbl_len = pptt.len() as u64; + prev_tbl_off = pptt_offset; + } + + // GTDT + #[cfg(target_arch = "aarch64")] + { + let gtdt = create_gtdt_table(); + let gtdt_offset = prev_tbl_off.checked_add(prev_tbl_len).unwrap(); + guest_mem + .write_slice(gtdt.as_slice(), gtdt_offset) + .expect("Error writing GTDT table"); + tables.push(gtdt_offset.0); + prev_tbl_len = gtdt.len() as u64; + prev_tbl_off = gtdt_offset; + } + + // MCFG + let mcfg = create_mcfg_table(device_manager.lock().unwrap().pci_segments()); + let mcfg_offset = prev_tbl_off.checked_add(prev_tbl_len).unwrap(); + guest_mem + .write_slice(mcfg.as_slice(), mcfg_offset) + .expect("Error writing MCFG table"); + tables.push(mcfg_offset.0); + prev_tbl_len = mcfg.len() as u64; + prev_tbl_off = mcfg_offset; + + // SPCR and DBG2 + #[cfg(target_arch = "aarch64")] + { + let is_serial_on = device_manager + .lock() + .unwrap() + .get_device_info() + .clone() + .get(&(DeviceType::Serial, DeviceType::Serial.to_string())) + .is_some(); + let serial_device_addr = arch::layout::LEGACY_SERIAL_MAPPED_IO_START.raw_value(); + let serial_device_irq = if is_serial_on { + device_manager + .lock() + .unwrap() + .get_device_info() + .clone() + .get(&(DeviceType::Serial, DeviceType::Serial.to_string())) + .unwrap() + .irq() + } else { + // If serial is turned off, add a fake device with invalid irq. + 31 + }; + + // SPCR + let spcr = create_spcr_table(serial_device_addr, serial_device_irq); + let spcr_offset = prev_tbl_off.checked_add(prev_tbl_len).unwrap(); + guest_mem + .write_slice(spcr.as_slice(), spcr_offset) + .expect("Error writing SPCR table"); + tables.push(spcr_offset.0); + prev_tbl_len = spcr.len() as u64; + prev_tbl_off = spcr_offset; + + // DBG2 + let dbg2 = create_dbg2_table(serial_device_addr); + let dbg2_offset = prev_tbl_off.checked_add(prev_tbl_len).unwrap(); + guest_mem + .write_slice(dbg2.as_slice(), dbg2_offset) + .expect("Error writing DBG2 table"); + tables.push(dbg2_offset.0); + prev_tbl_len = dbg2.len() as u64; + prev_tbl_off = dbg2_offset; + } + + if tpm_enabled { + // TPM2 Table + let tpm2 = create_tpm2_table(); + let tpm2_offset = prev_tbl_off.checked_add(prev_tbl_len).unwrap(); + guest_mem + .write_slice(tpm2.as_slice(), tpm2_offset) + .expect("Error writing TPM2 table"); + tables.push(tpm2_offset.0); + + prev_tbl_len = tpm2.len() as u64; + prev_tbl_off = tpm2_offset; + } + // SRAT and SLIT + // Only created if the NUMA nodes list is not empty. + if !numa_nodes.is_empty() { + // SRAT + let srat = create_srat_table(numa_nodes); + let srat_offset = prev_tbl_off.checked_add(prev_tbl_len).unwrap(); + guest_mem + .write_slice(srat.as_slice(), srat_offset) + .expect("Error writing SRAT table"); + tables.push(srat_offset.0); + + // SLIT + let slit = create_slit_table(numa_nodes); + let slit_offset = srat_offset.checked_add(srat.len() as u64).unwrap(); + guest_mem + .write_slice(slit.as_slice(), slit_offset) + .expect("Error writing SRAT table"); + tables.push(slit_offset.0); + + prev_tbl_len = slit.len() as u64; + prev_tbl_off = slit_offset; + }; + + #[cfg(target_arch = "aarch64")] + { + let iort = create_iort_table(device_manager.lock().unwrap().pci_segments()); + let iort_offset = prev_tbl_off.checked_add(prev_tbl_len).unwrap(); + guest_mem + .write_slice(iort.as_slice(), iort_offset) + .expect("Error writing IORT table"); + tables.push(iort_offset.0); + prev_tbl_len = iort.len() as u64; + prev_tbl_off = iort_offset; + } + + // VIOT + if let Some((iommu_bdf, devices_bdf)) = device_manager.lock().unwrap().iommu_attached_devices() + { + let viot = create_viot_table(iommu_bdf, devices_bdf); + + let viot_offset = prev_tbl_off.checked_add(prev_tbl_len).unwrap(); + guest_mem + .write_slice(viot.as_slice(), viot_offset) + .expect("Error writing VIOT table"); + tables.push(viot_offset.0); + prev_tbl_len = viot.len() as u64; + prev_tbl_off = viot_offset; + } + + // XSDT + let mut xsdt = Sdt::new(*b"XSDT", 36, 1, *b"CLOUDH", *b"CHXSDT ", 1); + for table in tables { + xsdt.append(table); + } + xsdt.update_checksum(); + let xsdt_offset = prev_tbl_off.checked_add(prev_tbl_len).unwrap(); + guest_mem + .write_slice(xsdt.as_slice(), xsdt_offset) + .expect("Error writing XSDT table"); + + // RSDP + let rsdp = Rsdp::new(*b"CLOUDH", xsdt_offset.0); + guest_mem + .write_slice(rsdp.as_slice(), rsdp_offset) + .expect("Error writing RSDP"); + + info!( + "Generated ACPI tables: took {}µs size = {}", + Instant::now().duration_since(start_time).as_micros(), + xsdt_offset.0 + xsdt.len() as u64 - rsdp_offset.0 + ); + rsdp_offset +} + +#[cfg(feature = "tdx")] +pub fn create_acpi_tables_tdx( + device_manager: &Arc>, + cpu_manager: &Arc>, + memory_manager: &Arc>, + numa_nodes: &NumaNodes, +) -> Vec { + // DSDT + let mut tables = vec![create_dsdt_table( + device_manager, + cpu_manager, + memory_manager, + )]; + + // FACP aka FADT + tables.push(create_facp_table(GuestAddress(0), device_manager)); + + // MADT + tables.push(cpu_manager.lock().unwrap().create_madt()); + + // MCFG + tables.push(create_mcfg_table( + device_manager.lock().unwrap().pci_segments(), + )); + + // SRAT and SLIT + // Only created if the NUMA nodes list is not empty. + if !numa_nodes.is_empty() { + // SRAT + tables.push(create_srat_table(numa_nodes)); + + // SLIT + tables.push(create_slit_table(numa_nodes)); + }; + + // VIOT + if let Some((iommu_bdf, devices_bdf)) = device_manager.lock().unwrap().iommu_attached_devices() + { + tables.push(create_viot_table(iommu_bdf, devices_bdf)); + } + + tables +} diff --git a/vendor/cube-hypervisor/vmm/src/api/http/http_endpoint.rs b/vendor/cube-hypervisor/vmm/src/api/http/http_endpoint.rs new file mode 100644 index 0000000..b3a3543 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/api/http/http_endpoint.rs @@ -0,0 +1,220 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::api::http::{error_response, EndpointHandler, HttpError}; +#[cfg(feature = "guest_debug")] +use crate::api::vm_coredump; +use crate::api::{ + vm_add_device, vm_add_disk, vm_add_fs, vm_add_net, vm_add_pmem, vm_add_user_device, + vm_add_vdpa, vm_add_vsock, vm_boot, vm_counters, vm_create, vm_delete, vm_info, vm_pause, + vm_power_button, vm_reboot, vm_receive_migration, vm_remove_device, vm_resize, vm_resize_zone, + vm_restore, vm_resume, vm_send_migration, vm_shutdown, vm_snapshot, vm_wait_start, vmm_ping, + vmm_shutdown, VmAction, VmConfig, +}; +use crate::api::{vm_pause2snapshot, vm_resume_from_snapshot, vm_set_fs}; +use crate::vm_config::NetConfig; +use micro_http::{Body, Method, Request, Response, StatusCode, Version}; +use std::fs::File; +use std::os::unix::io::IntoRawFd; +use std::sync::Arc; + +// /api/v1/vm.create handler +pub struct VmCreate {} + +impl EndpointHandler for VmCreate { + fn handle_request(&self, req: &Request) -> Response { + match req.method() { + Method::Put => { + match &req.body { + Some(body) => { + // Deserialize into a VmConfig + let vm_config: Box = match serde_json::from_slice(body.raw()) + .map_err(HttpError::SerdeJsonDeserialize) + { + Ok(config) => config, + Err(e) => return error_response(e, StatusCode::BadRequest), + }; + + // Call vm_create() + match vm_create(vm_config).map_err(HttpError::ApiError) { + Ok(_) => Response::new(Version::Http11, StatusCode::NoContent), + Err(e) => error_response(e, StatusCode::InternalServerError), + } + } + + None => Response::new(Version::Http11, StatusCode::BadRequest), + } + } + + _ => error_response(HttpError::BadRequest, StatusCode::BadRequest), + } + } +} + +// Common handler for boot, shutdown and reboot +pub struct VmActionHandler { + action: VmAction, +} + +impl VmActionHandler { + pub fn new(action: VmAction) -> Self { + VmActionHandler { action } + } +} + +impl EndpointHandler for VmActionHandler { + fn put_handler( + &self, + body: &Option, + mut files: Vec, + ) -> std::result::Result, HttpError> { + use VmAction::*; + if let Some(body) = body { + match self.action { + AddDevice(_) => vm_add_device(Arc::new(serde_json::from_slice(body.raw())?)), + AddDisk(_) => vm_add_disk(Arc::new(serde_json::from_slice(body.raw())?)), + AddFs(_) => vm_add_fs(Arc::new(serde_json::from_slice(body.raw())?)), + SetFs(_) => vm_set_fs(Arc::new(serde_json::from_slice(body.raw())?)), + AddPmem(_) => vm_add_pmem(Arc::new(serde_json::from_slice(body.raw())?)), + AddNet(_) => { + let mut net_cfg: NetConfig = serde_json::from_slice(body.raw())?; + // Update network config with optional files that might have + // been sent through control message. + if !files.is_empty() { + let fds = files.drain(..).map(|f| f.into_raw_fd()).collect(); + net_cfg.fds = Some(fds); + } + vm_add_net(Arc::new(net_cfg)) + } + AddVdpa(_) => vm_add_vdpa(Arc::new(serde_json::from_slice(body.raw())?)), + AddVsock(_) => vm_add_vsock(Arc::new(serde_json::from_slice(body.raw())?)), + AddUserDevice(_) => { + vm_add_user_device(Arc::new(serde_json::from_slice(body.raw())?)) + } + RemoveDevice(_) => vm_remove_device(Arc::new(serde_json::from_slice(body.raw())?)), + Resize(_) => vm_resize(Arc::new(serde_json::from_slice(body.raw())?)), + ResizeZone(_) => vm_resize_zone(Arc::new(serde_json::from_slice(body.raw())?)), + Restore(_) => vm_restore(Arc::new(serde_json::from_slice(body.raw())?)), + Snapshot(_) => vm_snapshot(Arc::new(serde_json::from_slice(body.raw())?)), + PauseToSnapshot(_) => { + vm_pause2snapshot(Arc::new(serde_json::from_slice(body.raw())?)) + } + ResumeFromSnapshot(_) => { + vm_resume_from_snapshot(Arc::new(serde_json::from_slice(body.raw())?)) + } + #[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] + Coredump(_) => vm_coredump(Arc::new(serde_json::from_slice(body.raw())?)), + ReceiveMigration(_) => { + vm_receive_migration(Arc::new(serde_json::from_slice(body.raw())?)) + } + SendMigration(_) => { + vm_send_migration(Arc::new(serde_json::from_slice(body.raw())?)) + } + + _ => return Err(HttpError::BadRequest), + } + } else { + match self.action { + Boot => vm_boot(), + Delete => vm_delete(), + Shutdown => vm_shutdown(), + Reboot => vm_reboot(), + Pause => vm_pause(), + Resume => vm_resume(), + PowerButton => vm_power_button(), + _ => return Err(HttpError::BadRequest), + } + } + .map_err(HttpError::ApiError) + } + + fn get_handler(&self, _body: &Option) -> std::result::Result, HttpError> { + use VmAction::*; + match self.action { + Counters => vm_counters().map_err(HttpError::ApiError), + _ => Err(HttpError::BadRequest), + } + } +} + +// /api/v1/vm.info handler +pub struct VmInfo {} + +impl EndpointHandler for VmInfo { + fn handle_request(&self, req: &Request) -> Response { + match req.method() { + Method::Get => match vm_info().map_err(HttpError::ApiError) { + Ok(info) => { + let mut response = Response::new(Version::Http11, StatusCode::OK); + let info_serialized = serde_json::to_string(&info).unwrap(); + + response.set_body(Body::new(info_serialized)); + response + } + Err(e) => error_response(e, StatusCode::InternalServerError), + }, + _ => error_response(HttpError::BadRequest, StatusCode::BadRequest), + } + } +} + +// /api/v1/vmm.info handler +pub struct VmmPing {} + +impl EndpointHandler for VmmPing { + fn handle_request(&self, req: &Request) -> Response { + match req.method() { + Method::Get => match vmm_ping().map_err(HttpError::ApiError) { + Ok(pong) => { + let mut response = Response::new(Version::Http11, StatusCode::OK); + let info_serialized = serde_json::to_string(&pong).unwrap(); + + response.set_body(Body::new(info_serialized)); + response + } + Err(e) => error_response(e, StatusCode::InternalServerError), + }, + + _ => error_response(HttpError::BadRequest, StatusCode::BadRequest), + } + } +} + +// /api/v1/vmm.shutdown handler +pub struct VmmShutdown {} + +impl EndpointHandler for VmmShutdown { + fn handle_request(&self, req: &Request) -> Response { + match req.method() { + Method::Put => match vmm_shutdown().map_err(HttpError::ApiError) { + Ok(_) => Response::new(Version::Http11, StatusCode::OK), + Err(e) => error_response(e, StatusCode::InternalServerError), + }, + _ => error_response(HttpError::BadRequest, StatusCode::BadRequest), + } + } +} + +// /api/v1/vm.waitstart handler +pub struct VmWaitStart {} + +impl EndpointHandler for VmWaitStart { + fn handle_request(&self, req: &Request) -> Response { + match req.method() { + Method::Get => match vm_wait_start().map_err(HttpError::ApiError) { + Ok(wait) => { + let mut response = Response::new(Version::Http11, StatusCode::OK); + let info_serialized = serde_json::to_string(&wait).unwrap(); + + response.set_body(Body::new(info_serialized)); + response + } + Err(e) => error_response(e, StatusCode::InternalServerError), + }, + + _ => error_response(HttpError::BadRequest, StatusCode::BadRequest), + } + } +} diff --git a/vendor/cube-hypervisor/vmm/src/api/http/mod.rs b/vendor/cube-hypervisor/vmm/src/api/http/mod.rs new file mode 100644 index 0000000..41e31f3 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/api/http/mod.rs @@ -0,0 +1,369 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use self::http_endpoint::{VmActionHandler, VmCreate, VmInfo, VmWaitStart, VmmPing, VmmShutdown}; +use crate::api::{ApiError, VmAction}; +use crate::seccomp_filters::{get_seccomp_filter, Thread}; +use crate::{Error as VmmError, Result}; +use core::fmt; +use hypervisor::HypervisorType; +use micro_http::{Body, HttpServer, MediaType, Method, Request, Response, StatusCode, Version}; +use once_cell::sync::Lazy; +use seccompiler::{apply_filter, SeccompAction}; +use serde_json::Error as SerdeError; +use std::collections::BTreeMap; +use std::fmt::Display; +use std::fs::File; +use std::os::unix::io::{IntoRawFd, RawFd}; +use std::os::unix::net::UnixListener; +use std::panic::AssertUnwindSafe; +use std::path::PathBuf; +use std::sync::Arc; +use std::thread; +use vmm_sys_util::eventfd::EventFd; + +pub mod http_endpoint; + +/// Errors associated with VMM management +#[derive(Debug)] +pub enum HttpError { + /// API request receive error + SerdeJsonDeserialize(SerdeError), + + /// Attempt to access unsupported HTTP method + BadRequest, + + /// Undefined endpoints + NotFound, + + /// Internal Server Error + InternalServerError, + + /// Error from internal API + ApiError(ApiError), +} + +impl Display for HttpError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::HttpError::*; + match self { + BadRequest => write!(f, "Bad Request"), + NotFound => write!(f, "Not Found"), + InternalServerError => write!(f, "Internal Server Error"), + SerdeJsonDeserialize(serde_error) => write!(f, "{}", serde_error), + ApiError(api_error) => write!(f, "{}", api_error), + } + } +} + +impl From for HttpError { + fn from(e: serde_json::Error) -> Self { + HttpError::SerdeJsonDeserialize(e) + } +} + +const HTTP_ROOT: &str = "/api/v1"; + +pub fn error_response(error: HttpError, status: StatusCode) -> Response { + let mut response = Response::new(Version::Http11, status); + response.set_body(Body::new(format!("{error}"))); + + response +} + +/// An HTTP endpoint handler interface +pub trait EndpointHandler { + /// Handles an HTTP request. + /// After parsing the request, the handler could decide to send an + /// associated API request down to the VMM API server to e.g. create + /// or start a VM. The request will block waiting for an answer from the + /// API server and translate that into an HTTP response. + fn handle_request(&self, req: &Request) -> Response { + // Cloning the files here is very important as it dup() the file + // descriptors, leaving open the one that was received. This way, + // rebooting the VM will work since the VM will be created from the + // original file descriptors. + let files = req.files.iter().map(|f| f.try_clone().unwrap()).collect(); + let res = match req.method() { + Method::Put => self.put_handler(&req.body, files), + Method::Get => self.get_handler(&req.body), + _ => return Response::new(Version::Http11, StatusCode::BadRequest), + }; + + match res { + Ok(response_body) => { + if let Some(body) = response_body { + let mut response = Response::new(Version::Http11, StatusCode::OK); + response.set_body(body); + response + } else { + Response::new(Version::Http11, StatusCode::NoContent) + } + } + Err(e @ HttpError::BadRequest) => error_response(e, StatusCode::BadRequest), + Err(e @ HttpError::SerdeJsonDeserialize(_)) => { + error_response(e, StatusCode::BadRequest) + } + Err(e) => error_response(e, StatusCode::InternalServerError), + } + } + + fn put_handler( + &self, + _body: &Option, + _files: Vec, + ) -> std::result::Result, HttpError> { + Err(HttpError::BadRequest) + } + + fn get_handler(&self, _body: &Option) -> std::result::Result, HttpError> { + Err(HttpError::BadRequest) + } +} + +/// An HTTP routes structure. +pub struct HttpRoutes { + /// routes is a hash table mapping endpoint URIs to their endpoint handlers. + pub routes: BTreeMap>, +} + +macro_rules! endpoint { + ($path:expr) => { + format!("{}{}", HTTP_ROOT, $path) + }; +} + +/// HTTP_ROUTES contain all the cloud-hypervisor HTTP routes. +pub static HTTP_ROUTES: Lazy = Lazy::new(|| { + let mut r = HttpRoutes { + routes: BTreeMap::new(), + }; + + r.routes.insert( + endpoint!("/vm.add-device"), + Box::new(VmActionHandler::new(VmAction::AddDevice(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-user-device"), + Box::new(VmActionHandler::new( + VmAction::AddUserDevice(Arc::default()), + )), + ); + r.routes.insert( + endpoint!("/vm.add-disk"), + Box::new(VmActionHandler::new(VmAction::AddDisk(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-fs"), + Box::new(VmActionHandler::new(VmAction::AddFs(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.set-fs"), + Box::new(VmActionHandler::new(VmAction::SetFs(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-net"), + Box::new(VmActionHandler::new(VmAction::AddNet(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-pmem"), + Box::new(VmActionHandler::new(VmAction::AddPmem(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-vdpa"), + Box::new(VmActionHandler::new(VmAction::AddVdpa(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-vsock"), + Box::new(VmActionHandler::new(VmAction::AddVsock(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.boot"), + Box::new(VmActionHandler::new(VmAction::Boot)), + ); + r.routes.insert( + endpoint!("/vm.counters"), + Box::new(VmActionHandler::new(VmAction::Counters)), + ); + r.routes + .insert(endpoint!("/vm.create"), Box::new(VmCreate {})); + r.routes.insert( + endpoint!("/vm.delete"), + Box::new(VmActionHandler::new(VmAction::Delete)), + ); + r.routes.insert(endpoint!("/vm.info"), Box::new(VmInfo {})); + r.routes.insert( + endpoint!("/vm.pause"), + Box::new(VmActionHandler::new(VmAction::Pause)), + ); + r.routes.insert( + endpoint!("/vm.pause2snapshot"), + Box::new(VmActionHandler::new(VmAction::PauseToSnapshot( + Arc::default(), + ))), + ); + r.routes.insert( + endpoint!("/vm.power-button"), + Box::new(VmActionHandler::new(VmAction::PowerButton)), + ); + r.routes.insert( + endpoint!("/vm.reboot"), + Box::new(VmActionHandler::new(VmAction::Reboot)), + ); + r.routes.insert( + endpoint!("/vm.receive-migration"), + Box::new(VmActionHandler::new(VmAction::ReceiveMigration( + Arc::default(), + ))), + ); + r.routes.insert( + endpoint!("/vm.remove-device"), + Box::new(VmActionHandler::new(VmAction::RemoveDevice(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.resize"), + Box::new(VmActionHandler::new(VmAction::Resize(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.resize-zone"), + Box::new(VmActionHandler::new(VmAction::ResizeZone(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.restore"), + Box::new(VmActionHandler::new(VmAction::Restore(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.resume"), + Box::new(VmActionHandler::new(VmAction::Resume)), + ); + r.routes.insert( + endpoint!("/vm.resume-from-snapshot"), + Box::new(VmActionHandler::new(VmAction::ResumeFromSnapshot( + Arc::default(), + ))), + ); + r.routes.insert( + endpoint!("/vm.send-migration"), + Box::new(VmActionHandler::new( + VmAction::SendMigration(Arc::default()), + )), + ); + r.routes.insert( + endpoint!("/vm.shutdown"), + Box::new(VmActionHandler::new(VmAction::Shutdown)), + ); + r.routes.insert( + endpoint!("/vm.snapshot"), + Box::new(VmActionHandler::new(VmAction::Snapshot(Arc::default()))), + ); + #[cfg(feature = "guest_debug")] + r.routes.insert( + endpoint!("/vm.coredump"), + Box::new(VmActionHandler::new(VmAction::Coredump(Arc::default()))), + ); + r.routes + .insert(endpoint!("/vmm.ping"), Box::new(VmmPing {})); + r.routes + .insert(endpoint!("/vmm.shutdown"), Box::new(VmmShutdown {})); + r.routes + .insert(endpoint!("/vm.waitstart"), Box::new(VmWaitStart {})); + + r +}); + +fn handle_http_request(request: &Request) -> Response { + let path = request.uri().get_abs_path().to_string(); + let mut response = match HTTP_ROUTES.routes.get(&path) { + Some(route) => route.handle_request(request), + None => error_response(HttpError::NotFound, StatusCode::NotFound), + }; + + response.set_server("Cloud Hypervisor API"); + response.set_content_type(MediaType::ApplicationJson); + response +} + +fn start_http_thread( + mut server: HttpServer, + seccomp_action: &SeccompAction, + exit_evt: EventFd, + hypervisor_type: HypervisorType, +) -> Result>> { + // Retrieve seccomp filter for API thread + let api_seccomp_filter = get_seccomp_filter(seccomp_action, Thread::Api, hypervisor_type) + .map_err(VmmError::CreateSeccompFilter)?; + + thread::Builder::new() + .name("http-server".to_string()) + .spawn(move || { + // Apply seccomp filter for API thread. + if !api_seccomp_filter.is_empty() { + apply_filter(&api_seccomp_filter) + .map_err(VmmError::ApplySeccompFilter) + .map_err(|e| { + error!("Error applying seccomp filter: {:?}", e); + exit_evt.write(1).ok(); + e + })?; + } + + std::panic::catch_unwind(AssertUnwindSafe(move || { + server.start_server().unwrap(); + loop { + match server.requests() { + Ok(request_vec) => { + for server_request in request_vec { + if let Err(e) = + server.respond(server_request.process(handle_http_request)) + { + error!("HTTP server error on response: {}", e); + } + } + } + Err(e) => { + error!( + "HTTP server error on retrieving incoming request. Error: {}", + e + ); + } + } + } + })) + .map_err(|_| { + error!("http-server thread panicked"); + exit_evt.write(1).ok() + }) + .ok(); + + Ok(()) + }) + .map_err(VmmError::HttpThreadSpawn) +} + +pub fn start_http_path_thread( + path: &str, + + seccomp_action: &SeccompAction, + exit_evt: EventFd, + hypervisor_type: HypervisorType, +) -> Result>> { + let socket_path = PathBuf::from(path); + let socket_fd = UnixListener::bind(socket_path).map_err(VmmError::CreateApiServerSocket)?; + // SAFETY: Valid FD just opened + let server = unsafe { HttpServer::new_from_fd(socket_fd.into_raw_fd()) } + .map_err(VmmError::CreateApiServer)?; + start_http_thread(server, seccomp_action, exit_evt, hypervisor_type) +} + +pub fn start_http_fd_thread( + fd: RawFd, + seccomp_action: &SeccompAction, + exit_evt: EventFd, + hypervisor_type: HypervisorType, +) -> Result>> { + // SAFETY: Valid FD + let server = unsafe { HttpServer::new_from_fd(fd) }.map_err(VmmError::CreateApiServer)?; + start_http_thread(server, seccomp_action, exit_evt, hypervisor_type) +} diff --git a/vendor/cube-hypervisor/vmm/src/api/mod.rs b/vendor/cube-hypervisor/vmm/src/api/mod.rs new file mode 100644 index 0000000..bd4529e --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/api/mod.rs @@ -0,0 +1,710 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +//! The internal VMM API for Cloud Hypervisor. +//! +//! This API is a synchronous, [mpsc](https://doc.rust-lang.org/std/sync/mpsc/) +//! based IPC for sending commands to the VMM thread, from other +//! Cloud Hypervisor threads. The IPC follows a command-response protocol, i.e. +//! each command will receive a response back. +//! +//! The main Cloud Hypervisor thread creates an API event file descriptor +//! to notify the VMM thread about pending API commands, together with an +//! API mpsc channel. The former is the IPC control plane, the latter is the +//! IPC data plane. +//! In order to use the IPC, a Cloud Hypervisor thread needs to have a clone +//! of both the API event file descriptor and the channel Sender. Then it must +//! go through the following steps: +//! +//! 1. The thread creates an mpsc channel for receiving the command response. +//! 2. The thread sends an ApiRequest to the Sender endpoint. The ApiRequest +//! contains the response channel Sender, for the VMM API server to be able +//! to send the response back. +//! 3. The thread writes to the API event file descriptor to notify the VMM +//! API server about a pending command. +//! 4. The thread reads the response back from the VMM API server, from the +//! response channel Receiver. +//! 5. The thread handles the response and forwards potential errors. + +pub use self::http::start_http_fd_thread; +pub use self::http::start_http_path_thread; +use core::fmt; +use std::fmt::Display; + +pub mod http; +pub mod service; + +use crate::api::service::VMM_SERVICE; +use crate::config::RestoreConfig; +use crate::device_tree::DeviceTree; +use crate::vm::{Error as VmError, VmState}; +use crate::vm_config::{ + DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, UserDeviceConfig, VdpaConfig, + VmConfig, VsockConfig, +}; +use micro_http::Body; +use serde::{Deserialize, Serialize}; +use service::Error as VmmServiceError; +use std::sync::{Arc, Mutex}; +use std::time::Duration; +use vm_migration::{MigratableError, SnapshotConfig}; + +pub const SNAPSHOT_VERSION: &str = "1.0.3"; + +/// API errors are sent back from the VMM API server through the ApiResponse. +#[derive(Debug)] +pub enum ApiError { + /// Wrong response payload type + ResponsePayloadType, + + /// The VM could not boot. + VmBoot(VmError), + + /// The VM could not be created. + VmCreate(VmError), + + /// The VM could not be deleted. + VmDelete(VmError), + + /// The VM info is not available. + VmInfo(VmError), + + /// The VM could not be paused. + VmPause(VmError), + + /// The VM could not be paused or snapshot or deleted. + VmPauseToSnapshot(VmError), + + /// The VM could not resume. + VmResume(VmError), + + /// The VM could not be restored or resumed. + VmResumeFromSnapshot(VmError), + + /// The VM is not booted. + VmNotBooted, + + /// The VM is not created. + VmNotCreated, + + /// The VM could not shutdown. + VmShutdown(VmError), + + /// The VM could not reboot. + VmReboot(VmError), + + /// The VM could not be snapshotted. + VmSnapshot(VmError), + + /// The VM could not restored. + VmRestore(VmError), + + /// The VM could not be coredumped. + VmCoredump(VmError), + + /// The VMM could not shutdown. + VmmShutdown(VmError), + + /// The VM could not be resized + VmResize(VmError), + + /// The memory zone could not be resized. + VmResizeZone(VmError), + + /// The device could not be added to the VM. + VmAddDevice(VmError), + + /// The user device could not be added to the VM. + VmAddUserDevice(VmError), + + /// The device could not be removed from the VM. + VmRemoveDevice(VmError), + + /// Cannot create seccomp filter + CreateSeccompFilter(seccompiler::Error), + + /// Cannot apply seccomp filter + ApplySeccompFilter(seccompiler::Error), + + /// The disk could not be added to the VM. + VmAddDisk(VmError), + + /// The fs could not be added to the VM. + VmAddFs(VmError), + + /// The fs could not be added to the VM. + VmSetFs(VmError), + + /// The pmem device could not be added to the VM. + VmAddPmem(VmError), + + /// The network device could not be added to the VM. + VmAddNet(VmError), + + /// The vDPA device could not be added to the VM. + VmAddVdpa(VmError), + + /// The vsock device could not be added to the VM. + VmAddVsock(VmError), + + /// Error starting migration receiever + VmReceiveMigration(MigratableError), + + /// Error starting migration sender + VmSendMigration(MigratableError), + + /// Error triggering power button + VmPowerButton(VmError), + Service(VmmServiceError), +} +pub type ApiResult = std::result::Result; + +impl Display for ApiError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::ApiError::*; + match self { + ResponsePayloadType => write!(f, "Wrong response payload type"), + VmBoot(vm_error) => write!(f, "{}", vm_error), + VmCreate(vm_error) => write!(f, "{}", vm_error), + VmDelete(vm_error) => write!(f, "{}", vm_error), + VmInfo(vm_error) => write!(f, "{}", vm_error), + VmPause(vm_error) => write!(f, "{}", vm_error), + VmPauseToSnapshot(vm_error) => write!(f, "{}", vm_error), + VmResume(vm_error) => write!(f, "{}", vm_error), + VmResumeFromSnapshot(vm_error) => write!(f, "{}", vm_error), + VmNotBooted => write!(f, "VM is not booted"), + VmNotCreated => write!(f, "VM is not created"), + VmShutdown(vm_error) => write!(f, "{}", vm_error), + VmReboot(vm_error) => write!(f, "{}", vm_error), + VmSnapshot(vm_error) => write!(f, "{}", vm_error), + VmRestore(vm_error) => write!(f, "{}", vm_error), + VmCoredump(vm_error) => write!(f, "{}", vm_error), + VmmShutdown(vm_error) => write!(f, "{}", vm_error), + VmResize(vm_error) => write!(f, "{}", vm_error), + VmResizeZone(vm_error) => write!(f, "{}", vm_error), + VmAddDevice(vm_error) => write!(f, "{}", vm_error), + VmAddUserDevice(vm_error) => write!(f, "{}", vm_error), + VmRemoveDevice(vm_error) => write!(f, "{}", vm_error), + CreateSeccompFilter(seccomp_error) => write!(f, "{}", seccomp_error), + ApplySeccompFilter(seccomp_error) => write!(f, "{}", seccomp_error), + VmAddDisk(vm_error) => write!(f, "{}", vm_error), + VmAddFs(vm_error) => write!(f, "{}", vm_error), + VmAddPmem(vm_error) => write!(f, "{}", vm_error), + VmAddNet(vm_error) => write!(f, "{}", vm_error), + VmAddVdpa(vm_error) => write!(f, "{}", vm_error), + VmAddVsock(vm_error) => write!(f, "{}", vm_error), + VmReceiveMigration(migratable_error) => write!(f, "{}", migratable_error), + VmSendMigration(migratable_error) => write!(f, "{}", migratable_error), + VmPowerButton(vm_error) => write!(f, "{}", vm_error), + VmSetFs(vm_error) => write!(f, "{}", vm_error), + Service(service_error) => write!(f, "{}", service_error), + } + } +} + +#[derive(Clone, Deserialize, Serialize)] +pub struct VmInfo { + pub config: Arc>, + pub state: VmState, + pub memory_actual_size: u64, + pub device_tree: Option>>, +} + +#[derive(Clone, Deserialize, Serialize)] +pub struct VmmPingResponse { + pub version: String, +} + +#[derive(Clone, Deserialize, Serialize)] +pub struct VmWaitStartResponse { + pub started: bool, +} + +#[derive(Clone, Deserialize, Serialize, Default, Debug)] +pub struct VmResizeData { + pub desired_vcpus: Option, + pub desired_ram: Option, + pub desired_balloon: Option, +} + +#[derive(Clone, Deserialize, Serialize, Default, Debug)] +pub struct VmResizeZoneData { + pub id: String, + pub desired_ram: u64, +} + +#[derive(Clone, Deserialize, Serialize, Default, Debug)] +pub struct VmRemoveDeviceData { + pub id: String, +} + +#[derive(Clone, Deserialize, Serialize, Default, Debug)] +pub struct VmCoredumpData { + /// The coredump destination file + pub destination_url: String, +} + +#[derive(Clone, Deserialize, Serialize, Default, Debug)] +pub struct VmReceiveMigrationData { + /// URL for the reception of migration state + pub receiver_url: String, +} + +#[derive(Clone, Deserialize, Serialize, Default, Debug)] +pub struct VmSendMigrationData { + /// URL to migrate the VM to + pub destination_url: String, + /// Send memory across socket without copying + #[serde(default)] + pub local: bool, +} + +pub enum ApiResponsePayload { + /// No data is sent on the channel. + Empty, + + /// Virtual machine information + VmInfo(VmInfo), + + /// Vmm ping response + VmmPing(VmmPingResponse), + + /// Vmm wait response + VmWaitStart(VmWaitStartResponse), + + /// Vm action response + VmAction(Option>), +} + +/// This is the response sent by the VMM API server through the mpsc channel. +pub type ApiResponse = std::result::Result; + +#[allow(clippy::large_enum_variant)] +#[derive(Debug)] +pub enum ApiRequest { + /// Create the virtual machine. This request payload is a VM configuration + /// (VmConfig). + /// If the VMM API server could not create the VM, it will send a VmCreate + /// error back. + VmCreate(Box), + + /// Boot the previously created virtual machine. + /// If the VM was not previously created, the VMM API server will send a + /// VmBoot error back. + VmBoot, + + /// Delete the previously created virtual machine. + /// If the VM was not previously created, the VMM API server will send a + /// VmDelete error back. + /// If the VM is booted, we shut it down first. + VmDelete, + + /// Request the VM information. + VmInfo, + + /// Request the VMM API server status + VmmPing, + + /// Pause a VM. + VmPause, + + /// Pause a VM to snapshot + VmPauseToSnapshot(Arc), + + /// Resume a VM from snapshot + VmResumeFromSnapshot(Arc), + + /// Resume a VM. + VmResume, + + /// Wait vm started. + VmWaitStart, + + /// Get counters for a VM. + VmCounters, + + /// Shut the previously booted virtual machine down. + /// If the VM was not previously booted or created, the VMM API server + /// will send a VmShutdown error back. + VmShutdown, + + /// Reboot the previously booted virtual machine. + /// If the VM was not previously booted or created, the VMM API server + /// will send a VmReboot error back. + VmReboot, + + /// Shut the VMM down. + /// This will shutdown and delete the current VM, if any, and then exit the + /// VMM process. + VmmShutdown, + + /// Resize the VM. + VmResize(Arc), + + /// Resize the memory zone. + VmResizeZone(Arc), + + /// Add a device to the VM. + VmAddDevice(Arc), + + /// Add a user device to the VM. + VmAddUserDevice(Arc), + + /// Remove a device from the VM. + VmRemoveDevice(Arc), + + /// Add a disk to the VM. + VmAddDisk(Arc), + + /// Add a fs to the VM. + VmAddFs(Arc), + + /// Add a vsock device to the VM. + VmSetFs(Arc), + + /// Add a pmem device to the VM. + VmAddPmem(Arc), + + /// Add a network device to the VM. + VmAddNet(Arc), + + /// Add a vDPA device to the VM. + VmAddVdpa(Arc), + + /// Add a vsock device to the VM. + VmAddVsock(Arc), + + /// Take a VM snapshot + VmSnapshot(Arc), + + /// Restore from a VM snapshot + VmRestore(Arc), + + /// Take a VM coredump + #[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] + VmCoredump(Arc), + + /// Incoming migration + VmReceiveMigration(Arc), + + /// Outgoing migration + VmSendMigration(Arc), + + // Trigger power button + VmPowerButton, +} + +pub fn vm_create(config: Box) -> ApiResult<()> { + // Send the VM creation request. + VMM_SERVICE + .lock() + .unwrap() + .send_request(ApiRequest::VmCreate(config)) + .map_err(ApiError::Service)??; + + Ok(()) +} + +/// Represents a VM related action. +/// This is mostly used to factorize code between VM routines +/// that only differ by the IPC command they send. +pub enum VmAction { + /// Boot a VM + Boot, + + /// Delete a VM + Delete, + + /// Shut a VM down + Shutdown, + + /// Reboot a VM + Reboot, + + /// Pause a VM + Pause, + + /// Resume a VM + Resume, + + /// Snapshot VM + PauseToSnapshot(Arc), + + /// Restore VM + ResumeFromSnapshot(Arc), + + /// Return VM counters + Counters, + + /// Add VFIO device + AddDevice(Arc), + + /// Add disk + AddDisk(Arc), + + /// Add filesystem + AddFs(Arc), + + /// Add filesystem + SetFs(Arc), + + /// Add pmem + AddPmem(Arc), + + /// Add network + AddNet(Arc), + + /// Add vdpa + AddVdpa(Arc), + + /// Add vsock + AddVsock(Arc), + + /// Add user device + AddUserDevice(Arc), + + /// Remove VFIO device + RemoveDevice(Arc), + + /// Resize VM + Resize(Arc), + + /// Resize memory zone + ResizeZone(Arc), + + /// Restore VM + Restore(Arc), + + /// Snapshot VM + Snapshot(Arc), + + /// Coredump VM + #[cfg(feature = "guest_debug")] + Coredump(Arc), + + /// Incoming migration + ReceiveMigration(Arc), + + /// Outgoing migration + SendMigration(Arc), + + /// Power Button for clean shutdown + PowerButton, +} + +fn vm_action(action: VmAction) -> ApiResult> { + use VmAction::*; + let request = match action { + Boot => ApiRequest::VmBoot, + Delete => ApiRequest::VmDelete, + Shutdown => ApiRequest::VmShutdown, + Reboot => ApiRequest::VmReboot, + Pause => ApiRequest::VmPause, + Resume => ApiRequest::VmResume, + PauseToSnapshot(v) => ApiRequest::VmPauseToSnapshot(v), + ResumeFromSnapshot(v) => ApiRequest::VmResumeFromSnapshot(v), + Counters => ApiRequest::VmCounters, + AddDevice(v) => ApiRequest::VmAddDevice(v), + AddDisk(v) => ApiRequest::VmAddDisk(v), + AddFs(v) => ApiRequest::VmAddFs(v), + SetFs(v) => ApiRequest::VmSetFs(v), + AddPmem(v) => ApiRequest::VmAddPmem(v), + AddNet(v) => ApiRequest::VmAddNet(v), + AddVdpa(v) => ApiRequest::VmAddVdpa(v), + AddVsock(v) => ApiRequest::VmAddVsock(v), + AddUserDevice(v) => ApiRequest::VmAddUserDevice(v), + RemoveDevice(v) => ApiRequest::VmRemoveDevice(v), + Resize(v) => ApiRequest::VmResize(v), + ResizeZone(v) => ApiRequest::VmResizeZone(v), + Restore(v) => ApiRequest::VmRestore(v), + Snapshot(v) => ApiRequest::VmSnapshot(v), + #[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] + Coredump(v) => ApiRequest::VmCoredump(v), + ReceiveMigration(v) => ApiRequest::VmReceiveMigration(v), + SendMigration(v) => ApiRequest::VmSendMigration(v), + PowerButton => ApiRequest::VmPowerButton, + }; + + let body = match VMM_SERVICE + .lock() + .unwrap() + .send_request(request) + .map_err(ApiError::Service)?? + { + ApiResponsePayload::VmAction(response) => response.map(Body::new), + ApiResponsePayload::Empty => None, + _ => return Err(ApiError::ResponsePayloadType), + }; + + Ok(body) +} + +pub fn vm_boot() -> ApiResult> { + vm_action(VmAction::Boot) +} + +pub fn vm_delete() -> ApiResult> { + vm_action(VmAction::Delete) +} + +pub fn vm_shutdown() -> ApiResult> { + vm_action(VmAction::Shutdown) +} + +pub fn vm_reboot() -> ApiResult> { + vm_action(VmAction::Reboot) +} + +pub fn vm_pause() -> ApiResult> { + vm_action(VmAction::Pause) +} + +pub fn vm_resume() -> ApiResult> { + vm_action(VmAction::Resume) +} + +pub fn vm_pause2snapshot(data: Arc) -> ApiResult> { + vm_action(VmAction::PauseToSnapshot(data)) +} + +pub fn vm_resume_from_snapshot(data: Arc) -> ApiResult> { + vm_action(VmAction::ResumeFromSnapshot(data)) +} + +pub fn vm_counters() -> ApiResult> { + vm_action(VmAction::Counters) +} + +pub fn vm_power_button() -> ApiResult> { + vm_action(VmAction::PowerButton) +} + +pub fn vm_receive_migration(data: Arc) -> ApiResult> { + vm_action(VmAction::ReceiveMigration(data)) +} + +pub fn vm_send_migration(data: Arc) -> ApiResult> { + vm_action(VmAction::SendMigration(data)) +} + +pub fn vm_snapshot(data: Arc) -> ApiResult> { + vm_action(VmAction::Snapshot(data)) +} + +pub fn vm_restore(data: Arc) -> ApiResult> { + vm_action(VmAction::Restore(data)) +} + +#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] +pub fn vm_coredump(data: Arc) -> ApiResult> { + vm_action(VmAction::Coredump(data)) +} + +pub fn vm_info() -> ApiResult { + // Send the VM request. + let vm_info = VMM_SERVICE + .lock() + .unwrap() + .send_request(ApiRequest::VmInfo) + .map_err(ApiError::Service)??; + + match vm_info { + ApiResponsePayload::VmInfo(info) => Ok(info), + _ => Err(ApiError::ResponsePayloadType), + } +} + +pub fn vmm_ping() -> ApiResult { + let vmm_pong = VMM_SERVICE + .lock() + .unwrap() + .send_request(ApiRequest::VmmPing) + .map_err(ApiError::Service)??; + + match vmm_pong { + ApiResponsePayload::VmmPing(pong) => Ok(pong), + _ => Err(ApiError::ResponsePayloadType), + } +} + +pub fn vmm_shutdown() -> ApiResult<()> { + // Send the VMM shutdown request. + VMM_SERVICE + .lock() + .unwrap() + .send_request(ApiRequest::VmmShutdown) + .map_err(ApiError::Service)??; + Ok(()) +} + +pub fn vm_wait_start() -> ApiResult { + loop { + let vmm_wait = VMM_SERVICE + .lock() + .unwrap() + .send_request(ApiRequest::VmWaitStart) + .map_err(ApiError::Service)??; + + match vmm_wait { + ApiResponsePayload::VmWaitStart(wait) => { + if wait.started { + return Ok(wait); + } else { + std::thread::sleep(Duration::from_millis(100)); + continue; + } + } + _ => return Err(ApiError::ResponsePayloadType), + } + } +} + +pub fn vm_resize(data: Arc) -> ApiResult> { + vm_action(VmAction::Resize(data)) +} + +pub fn vm_resize_zone(data: Arc) -> ApiResult> { + vm_action(VmAction::ResizeZone(data)) +} + +pub fn vm_add_device(data: Arc) -> ApiResult> { + vm_action(VmAction::AddDevice(data)) +} + +pub fn vm_add_user_device(data: Arc) -> ApiResult> { + vm_action(VmAction::AddUserDevice(data)) +} + +pub fn vm_remove_device(data: Arc) -> ApiResult> { + vm_action(VmAction::RemoveDevice(data)) +} + +pub fn vm_add_disk(data: Arc) -> ApiResult> { + vm_action(VmAction::AddDisk(data)) +} + +pub fn vm_add_fs(data: Arc) -> ApiResult> { + vm_action(VmAction::AddFs(data)) +} + +pub fn vm_set_fs(data: Arc) -> ApiResult> { + vm_action(VmAction::SetFs(data)) +} + +pub fn vm_add_pmem(data: Arc) -> ApiResult> { + vm_action(VmAction::AddPmem(data)) +} + +pub fn vm_add_net(data: Arc) -> ApiResult> { + vm_action(VmAction::AddNet(data)) +} + +pub fn vm_add_vdpa(data: Arc) -> ApiResult> { + vm_action(VmAction::AddVdpa(data)) +} + +pub fn vm_add_vsock(data: Arc) -> ApiResult> { + vm_action(VmAction::AddVsock(data)) +} diff --git a/vendor/cube-hypervisor/vmm/src/api/openapi/cloud-hypervisor.yaml b/vendor/cube-hypervisor/vmm/src/api/openapi/cloud-hypervisor.yaml new file mode 100644 index 0000000..681af56 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/api/openapi/cloud-hypervisor.yaml @@ -0,0 +1,1197 @@ +openapi: 3.0.1 +info: + title: Cloud Hypervisor API + description: Local HTTP based API for managing and inspecting a cloud-hypervisor virtual machine. + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + version: 0.3.0 + +servers: + - url: http://localhost/api/v1 + +paths: + /vmm.ping: + get: + summary: Ping the VMM to check for API server availability + responses: + 200: + description: The VMM information + content: + application/json: + schema: + $ref: "#/components/schemas/VmmPingResponse" + + /vmm.shutdown: + put: + summary: Shuts the cloud-hypervisor VMM. + operationId: shutdownVMM + responses: + 204: + description: The VMM successfully shutdown. + + /vm.info: + get: + summary: Returns general information about the cloud-hypervisor Virtual Machine (VM) instance. + responses: + 200: + description: The VM information + content: + application/json: + schema: + $ref: "#/components/schemas/VmInfo" + + /vm.counters: + get: + summary: Get counters from the VM + responses: + 200: + description: The VM counters + content: + application/json: + schema: + $ref: "#/components/schemas/VmCounters" + + /vm.create: + put: + summary: Create the cloud-hypervisor Virtual Machine (VM) instance. The instance is not booted, only created. + operationId: createVM + requestBody: + description: The VM configuration + content: + application/json: + schema: + $ref: "#/components/schemas/VmConfig" + required: true + responses: + 204: + description: The VM instance was successfully created. + + /vm.delete: + put: + summary: Delete the cloud-hypervisor Virtual Machine (VM) instance. + operationId: deleteVM + responses: + 204: + description: The VM instance was successfully deleted. + + /vm.boot: + put: + summary: Boot the previously created VM instance. + operationId: bootVM + responses: + 204: + description: The VM instance successfully booted. + 404: + description: The VM instance could not boot because it is not created yet + + /vm.pause: + put: + summary: Pause a previously booted VM instance. + operationId: pauseVM + responses: + 204: + description: The VM instance successfully paused. + 404: + description: The VM instance could not pause because it is not created yet + 405: + description: The VM instance could not pause because it is not booted. + + /vm.resume: + put: + summary: Resume a previously paused VM instance. + operationId: resumeVM + responses: + 204: + description: The VM instance successfully paused. + 404: + description: The VM instance could not resume because it is not booted yet + 405: + description: The VM instance could not resume because it is not paused. + + /vm.pause2snapshot: + put: + summary: Pause VM and returns a VM snapshot. + requestBody: + description: The snapshot configuration + content: + application/json: + schema: + $ref: "#/components/schemas/VmSnapshotConfig" + required: true + responses: + 204: + description: The VM instance was successfully paused and snapshotted. + 404: + description: The VM instance could not be paused and snapshotted because it is not created. + 405: + description: The VM instance could not be paused and snapshotted because it is not booted. + + /vm.resume-from-snapshot: + put: + summary: Resume a VM from a snapshot. + requestBody: + description: The restore configuration + content: + application/json: + schema: + $ref: "#/components/schemas/RestoreConfig" + required: true + responses: + 204: + description: The VM instance was successfully restored and resumed. + 404: + description: The VM instance could not be restored because it is already created. + + /vm.shutdown: + put: + summary: Shut the VM instance down. + operationId: shutdownVM + responses: + 204: + description: The VM instance successfully shut down. + 404: + description: The VM instance could not shut down because is not created. + 405: + description: The VM instance could not shut down because it is not started. + + /vm.reboot: + put: + summary: Reboot the VM instance. + operationId: rebootVM + responses: + 204: + description: The VM instance successfully rebooted. + 404: + description: The VM instance could not reboot because it is not created. + 405: + description: The VM instance could not reboot because it is not booted. + + /vm.power-button: + put: + summary: Trigger a power button in the VM + operationId: power-buttonVM + responses: + 204: + description: Power button successfully triggered in the VM + 404: + description: The button could not be triggered because it is not created yet + 405: + description: The button could not be triggered because it is not booted. + + /vm.resize: + put: + summary: Resize the VM + requestBody: + description: The target size for the VM + content: + application/json: + schema: + $ref: "#/components/schemas/VmResize" + required: true + responses: + 204: + description: The VM instance was successfully resized. + 404: + description: The VM instance could not be resized because it is not created. + + /vm.resize-zone: + put: + summary: Resize a memory zone + requestBody: + description: The target size for the memory zone + content: + application/json: + schema: + $ref: "#/components/schemas/VmResizeZone" + required: true + responses: + 204: + description: The memory zone was successfully resized. + 500: + description: The memory zone could not be resized. + + /vm.add-device: + put: + summary: Add a new device to the VM + requestBody: + description: The path of the new device + content: + application/json: + schema: + $ref: "#/components/schemas/DeviceConfig" + required: true + responses: + 200: + description: The new device was successfully added to the VM instance. + content: + application/json: + schema: + $ref: "#/components/schemas/PciDeviceInfo" + 204: + description: The new device was successfully (cold) added to the VM instance. + 404: + description: The new device could not be added to the VM instance. + + /vm.remove-device: + put: + summary: Remove a device from the VM + requestBody: + description: The identifier of the device + content: + application/json: + schema: + $ref: "#/components/schemas/VmRemoveDevice" + required: true + responses: + 204: + description: The device was successfully removed from the VM instance. + 404: + description: The device could not be removed from the VM instance. + + /vm.add-disk: + put: + summary: Add a new disk to the VM + requestBody: + description: The details of the new disk + content: + application/json: + schema: + $ref: "#/components/schemas/DiskConfig" + required: true + responses: + 200: + description: The new disk was successfully added to the VM instance. + content: + application/json: + schema: + $ref: "#/components/schemas/PciDeviceInfo" + 204: + description: The new disk was successfully (cold) added to the VM instance. + 500: + description: The new disk could not be added to the VM instance. + + /vm.add-fs: + put: + summary: Add a new virtio-fs device to the VM + requestBody: + description: The details of the new virtio-fs + content: + application/json: + schema: + $ref: "#/components/schemas/FsConfig" + required: true + responses: + 200: + description: The new device was successfully added to the VM instance. + content: + application/json: + schema: + $ref: "#/components/schemas/PciDeviceInfo" + 204: + description: The new device was successfully (cold) added to the VM instance. + 500: + description: The new device could not be added to the VM instance. + + /vm.add-pmem: + put: + summary: Add a new pmem device to the VM + requestBody: + description: The details of the new pmem device + content: + application/json: + schema: + $ref: "#/components/schemas/PmemConfig" + required: true + responses: + 200: + description: The new device was successfully added to the VM instance. + content: + application/json: + schema: + $ref: "#/components/schemas/PciDeviceInfo" + 204: + description: The new device was successfully (cold) added to the VM instance. + 500: + description: The new device could not be added to the VM instance. + + /vm.add-net: + put: + summary: Add a new network device to the VM + requestBody: + description: The details of the new network device + content: + application/json: + schema: + $ref: "#/components/schemas/NetConfig" + required: true + responses: + 200: + description: The new device was successfully added to the VM instance. + content: + application/json: + schema: + $ref: "#/components/schemas/PciDeviceInfo" + 204: + description: The new device was successfully (cold) added to the VM instance. + 500: + description: The new device could not be added to the VM instance. + + /vm.add-vsock: + put: + summary: Add a new vsock device to the VM + requestBody: + description: The details of the new vsock device + content: + application/json: + schema: + $ref: "#/components/schemas/VsockConfig" + required: true + responses: + 200: + description: The new device was successfully added to the VM instance. + content: + application/json: + schema: + $ref: "#/components/schemas/PciDeviceInfo" + 204: + description: The new device was successfully (cold) added to the VM instance. + 500: + description: The new device could not be added to the VM instance. + + /vm.add-vdpa: + put: + summary: Add a new vDPA device to the VM + requestBody: + description: The details of the new vDPA device + content: + application/json: + schema: + $ref: "#/components/schemas/VdpaConfig" + required: true + responses: + 200: + description: The new vDPA device was successfully added to the VM instance. + content: + application/json: + schema: + $ref: "#/components/schemas/PciDeviceInfo" + 204: + description: The new vDPA device was successfully (cold) added to the VM instance. + 500: + description: The new vDPA device could not be added to the VM instance. + + /vm.snapshot: + put: + summary: Returns a VM snapshot. + requestBody: + description: The snapshot configuration + content: + application/json: + schema: + $ref: "#/components/schemas/VmSnapshotConfig" + required: true + responses: + 204: + description: The VM instance was successfully snapshotted. + 404: + description: The VM instance could not be snapshotted because it is not created. + 405: + description: The VM instance could not be snapshotted because it is not booted. + + /vm.coredump: + put: + summary: Takes a VM coredump. + requestBody: + description: The coredump configuration + content: + application/json: + schema: + $ref: "#/components/schemas/VmCoredumpData" + required: true + responses: + 204: + description: The VM instance was successfully coredumped. + 404: + description: The VM instance could not be coredumped because it is not created. + 405: + description: The VM instance could not be coredumped because it is not booted. + + /vm.restore: + put: + summary: Restore a VM from a snapshot. + requestBody: + description: The restore configuration + content: + application/json: + schema: + $ref: "#/components/schemas/RestoreConfig" + required: true + responses: + 204: + description: The VM instance was successfully restored. + 404: + description: The VM instance could not be restored because it is already created. + + /vm.receive-migration: + put: + summary: Receive a VM migration from URL + requestBody: + description: The URL for the reception of migration state + content: + application/json: + schema: + $ref: "#/components/schemas/ReceiveMigrationData" + required: true + responses: + 204: + description: The VM migration was successfully received. + 500: + description: The VM migration could not be received. + + /vm.send-migration: + put: + summary: Send a VM migration to URL + requestBody: + description: The URL for sending the migration state + content: + application/json: + schema: + $ref: "#/components/schemas/SendMigrationData" + required: true + responses: + 204: + description: The VM migration was successfully sent. + 500: + description: The VM migration could not be sent. + +components: + schemas: + VmmPingResponse: + required: + - version + type: object + properties: + version: + type: string + description: Virtual Machine Monitor information + + VmInfo: + required: + - config + - state + type: object + properties: + config: + $ref: "#/components/schemas/VmConfig" + state: + type: string + enum: [Created, Running, Shutdown, Paused] + memory_actual_size: + type: integer + format: int64 + device_tree: + type: object + additionalProperties: + $ref: "#/components/schemas/DeviceNode" + description: Virtual Machine information + + DeviceNode: + type: object + properties: + id: + type: string + resources: + type: array + items: + # Rust enum type (with data) which can't be better represented here + type: object + children: + type: array + items: + type: string + pci_bdf: + type: string + + VmCounters: + type: object + additionalProperties: + type: object + additionalProperties: + type: integer + format: int64 + + PciDeviceInfo: + required: + - id + - bdf + type: object + properties: + id: + type: string + bdf: + type: string + description: Information about a PCI device + + PayloadConfig: + type: object + properties: + firmware: + type: string + kernel: + type: string + cmdline: + type: string + initramfs: + type: string + description: Payloads to boot in guest + + VmConfig: + required: + - payload + type: object + properties: + cpus: + $ref: "#/components/schemas/CpusConfig" + memory: + $ref: "#/components/schemas/MemoryConfig" + payload: + $ref: "#/components/schemas/PayloadConfig" + disks: + type: array + items: + $ref: "#/components/schemas/DiskConfig" + net: + type: array + items: + $ref: "#/components/schemas/NetConfig" + rng: + $ref: "#/components/schemas/RngConfig" + balloon: + $ref: "#/components/schemas/BalloonConfig" + fs: + type: array + items: + $ref: "#/components/schemas/FsConfig" + pmem: + type: array + items: + $ref: "#/components/schemas/PmemConfig" + serial: + $ref: "#/components/schemas/ConsoleConfig" + console: + $ref: "#/components/schemas/ConsoleConfig" + devices: + type: array + items: + $ref: "#/components/schemas/DeviceConfig" + vdpa: + type: array + items: + $ref: "#/components/schemas/VdpaConfig" + vsock: + $ref: "#/components/schemas/VsockConfig" + sgx_epc: + type: array + items: + $ref: "#/components/schemas/SgxEpcConfig" + numa: + type: array + items: + $ref: "#/components/schemas/NumaConfig" + iommu: + type: boolean + default: false + watchdog: + type: boolean + default: false + platform: + $ref: "#/components/schemas/PlatformConfig" + tpm: + $ref: "#/components/schemas/TpmConfig" + description: Virtual machine configuration + + CpuAffinity: + type: object + properties: + vcpu: + type: integer + host_cpus: + type: array + items: + type: integer + + CpuFeatures: + type: object + properties: + amx: + type: boolean + + CpuTopology: + type: object + properties: + threads_per_core: + type: integer + cores_per_die: + type: integer + dies_per_package: + type: integer + packages: + type: integer + + CpusConfig: + required: + - boot_vcpus + - max_vcpus + type: object + properties: + boot_vcpus: + minimum: 1 + default: 1 + type: integer + max_vcpus: + minimum: 1 + default: 1 + type: integer + topology: + $ref: "#/components/schemas/CpuTopology" + kvm_hyperv: + type: boolean + default: false + max_phys_bits: + type: integer + affinity: + type: array + items: + $ref: "#/components/schemas/CpuAffinity" + features: + $ref: "#/components/schemas/CpuFeatures" + + PlatformConfig: + type: object + properties: + num_pci_segments: + type: integer + format: int16 + iommu_segments: + type: array + items: + type: integer + format: int16 + serial_number: + type: string + uuid: + type: string + oem_strings: + type: array + items: + type: string + tdx: + type: boolean + default: false + + MemoryZoneConfig: + required: + - id + - size + type: object + properties: + id: + type: string + size: + type: integer + format: int64 + default: 512 MB + file: + type: string + mergeable: + type: boolean + default: false + shared: + type: boolean + default: false + hugepages: + type: boolean + default: false + hugepage_size: + type: integer + format: int64 + host_numa_node: + type: integer + format: int32 + hotplug_size: + type: integer + format: int64 + hotplugged_size: + type: integer + format: int64 + prefault: + type: boolean + default: false + + MemoryConfig: + required: + - size + type: object + properties: + size: + type: integer + format: int64 + default: 512 MB + hotplug_size: + type: integer + format: int64 + hotplugged_size: + type: integer + format: int64 + mergeable: + type: boolean + default: false + hotplug_method: + type: string + default: "Acpi" + shared: + type: boolean + default: false + hugepages: + type: boolean + default: false + hugepage_size: + type: integer + format: int64 + prefault: + type: boolean + default: false + thp: + type: boolean + default: true + dirty_log: + type: boolean + default: false + zones: + type: array + items: + $ref: "#/components/schemas/MemoryZoneConfig" + + TokenBucket: + required: + - size + - refill_time + type: object + properties: + size: + type: integer + format: int64 + minimum: 0 + description: The total number of tokens this bucket can hold. + one_time_burst: + type: integer + format: int64 + minimum: 0 + description: The initial size of a token bucket. + refill_time: + type: integer + format: int64 + minimum: 0 + description: The amount of milliseconds it takes for the bucket to refill. + description: + Defines a token bucket with a maximum capacity (_size_), an initial burst size + (_one_time_burst_) and an interval for refilling purposes (_refill_time_). + The refill-rate is derived from _size_ and _refill_time_, and it is the constant + rate at which the tokens replenish. The refill process only starts happening after + the initial burst budget is consumed. + Consumption from the token bucket is unbounded in speed which allows for bursts + bound in size by the amount of tokens available. + Once the token bucket is empty, consumption speed is bound by the refill-rate. + + RateLimiterConfig: + type: object + properties: + bandwidth: + $ref: "#/components/schemas/TokenBucket" + ops: + $ref: "#/components/schemas/TokenBucket" + description: + Defines an IO rate limiter with independent bytes/s and ops/s limits. + Limits are defined by configuring each of the _bandwidth_ and _ops_ token buckets. + + DiskConfig: + required: + - path + type: object + properties: + path: + type: string + readonly: + type: boolean + default: false + direct: + type: boolean + default: false + iommu: + type: boolean + default: false + num_queues: + type: integer + default: 1 + queue_size: + type: integer + default: 128 + vhost_user: + type: boolean + default: false + vhost_socket: + type: string + rate_limiter_config: + $ref: "#/components/schemas/RateLimiterConfig" + pci_segment: + type: integer + format: int16 + id: + type: string + + NetConfig: + type: object + properties: + tap: + type: string + ip: + type: string + default: "192.168.249.1" + mask: + type: string + default: "255.255.255.0" + mac: + type: string + host_mac: + type: string + mtu: + type: integer + iommu: + type: boolean + default: false + num_queues: + type: integer + default: 2 + queue_size: + type: integer + default: 256 + vhost_user: + type: boolean + default: false + vhost_socket: + type: string + vhost_mode: + type: string + default: "Client" + id: + type: string + pci_segment: + type: integer + format: int16 + rate_limiter_config: + $ref: "#/components/schemas/RateLimiterConfig" + + RngConfig: + required: + - src + type: object + properties: + src: + type: string + default: "/dev/urandom" + iommu: + type: boolean + default: false + + BalloonConfig: + required: + - size + type: object + properties: + size: + type: integer + format: int64 + deflate_on_oom: + type: boolean + default: false + description: Deflate balloon when the guest is under memory pressure. + free_page_reporting: + type: boolean + default: false + description: Enable guest to report free pages. + + FsConfig: + required: + - num_queues + - queue_size + - socket + - tag + type: object + properties: + tag: + type: string + socket: + type: string + num_queues: + type: integer + default: 1 + queue_size: + type: integer + default: 1024 + pci_segment: + type: integer + format: int16 + id: + type: string + + PmemConfig: + required: + - file + type: object + properties: + file: + type: string + size: + type: integer + format: int64 + iommu: + type: boolean + default: false + discard_writes: + type: boolean + default: false + pci_segment: + type: integer + format: int16 + id: + type: string + + ConsoleConfig: + required: + - mode + type: object + properties: + file: + type: string + mode: + type: string + enum: [Off, Pty, Tty, File, Null] + iommu: + type: boolean + default: false + + DeviceConfig: + required: + - path + type: object + properties: + path: + type: string + iommu: + type: boolean + default: false + pci_segment: + type: integer + format: int16 + id: + type: string + + TpmConfig: + required: + - socket + type: object + properties: + socket: + type: string + + VdpaConfig: + required: + - path + - num_queues + type: object + properties: + path: + type: string + num_queues: + type: integer + default: 1 + iommu: + type: boolean + default: false + pci_segment: + type: integer + format: int16 + id: + type: string + + VsockConfig: + required: + - cid + - socket + type: object + properties: + cid: + type: integer + format: int64 + minimum: 3 + description: Guest Vsock CID + socket: + type: string + description: Path to UNIX domain socket, used to proxy vsock connections. + iommu: + type: boolean + default: false + pci_segment: + type: integer + format: int16 + id: + type: string + + SgxEpcConfig: + required: + - id + - size + type: object + properties: + id: + type: string + size: + type: integer + format: int64 + prefault: + type: boolean + default: false + + NumaDistance: + required: + - destination + - distance + type: object + properties: + destination: + type: integer + format: int32 + distance: + type: integer + format: int32 + + NumaConfig: + required: + - guest_numa_id + type: object + properties: + guest_numa_id: + type: integer + format: int32 + cpus: + type: array + items: + type: integer + format: int32 + distances: + type: array + items: + $ref: "#/components/schemas/NumaDistance" + memory_zones: + type: array + items: + type: string + sgx_epc_sections: + type: array + items: + type: string + + VmResize: + type: object + properties: + desired_vcpus: + minimum: 1 + type: integer + desired_ram: + description: desired memory ram in bytes + type: integer + format: int64 + desired_balloon: + description: desired balloon size in bytes + type: integer + format: int64 + + VmResizeZone: + type: object + properties: + id: + type: string + desired_ram: + description: desired memory zone size in bytes + type: integer + format: int64 + + VmRemoveDevice: + type: object + properties: + id: + type: string + + VmSnapshotConfig: + type: object + properties: + destination_url: + type: string + memory_vol_url: + type: string + description: Optional existing memory blob path for storing memory range data on a separate volume. Accepts /dev/... and file:///dev/... forms. + + VmCoredumpData: + type: object + properties: + destination_url: + type: string + + RestoreConfig: + required: + - source_url + type: object + properties: + source_url: + type: string + prefault: + type: boolean + disks: + type: array + items: + $ref: "#/components/schemas/DiskConfig" + net: + type: array + items: + $ref: "#/components/schemas/NetConfig" + fs: + type: array + items: + $ref: "#/components/schemas/FsConfig" + vsock: + $ref: "#/components/schemas/VsockConfig" + dirty_log: + type: boolean + default: false + memory_vol_url: + type: string + description: Optional existing memory blob path for reading memory range data from a separate volume. Accepts /dev/... and file:///dev/... forms. + + ReceiveMigrationData: + required: + - receiver_url + type: object + properties: + receiver_url: + type: string + + SendMigrationData: + required: + - destination_url + type: object + properties: + destination_url: + type: string + local: + type: boolean diff --git a/vendor/cube-hypervisor/vmm/src/api/service.rs b/vendor/cube-hypervisor/vmm/src/api/service.rs new file mode 100644 index 0000000..744267e --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/api/service.rs @@ -0,0 +1,90 @@ +// Copyright © 2023 Tencent Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::api::{ApiRequest, ApiResponse}; +use lazy_static::lazy_static; +use std::io; +use std::sync::mpsc::{Receiver, RecvError, SendError, Sender}; +use std::sync::Mutex; +use thiserror::Error; +use vmm_sys_util::eventfd::EventFd; + +lazy_static! { + pub static ref VMM_SERVICE: Mutex = Mutex::new(VmmService::new()); +} + +#[derive(Error, Debug)] +pub enum Error { + #[error("Cannot write to EventFd: {0}")] + EventFdWrite(#[source] io::Error), + #[error("API request send error: {0}")] + RequestSend(#[source] SendError), + #[error("API response receive error: {0}")] + ResponseRecv(#[source] RecvError), + /// Vmm Service not init + #[error("Vmm Service not init")] + NotInit, +} + +/// Vmm Api Service +/// A global static Vmm Api Sender to Vmm thread +pub struct VmmService { + inner: Option, +} + +impl VmmService { + pub fn new() -> Self { + Self { inner: None } + } + + pub fn init( + &mut self, + api_sender: Sender, + api_evt: EventFd, + res_receiver: Receiver, + ) -> Result<(), Error> { + self.inner = Some(VmmServiceInner::new(api_sender, api_evt, res_receiver)); + Ok(()) + } + + pub fn send_request(&self, request: ApiRequest) -> Result { + if let Some(instance) = self.inner.as_ref() { + return instance.send_request(request); + } + Err(Error::NotInit) + } +} + +impl Default for VmmService { + fn default() -> Self { + Self::new() + } +} + +struct VmmServiceInner { + api_sender: Sender, + api_evt: EventFd, + res_receiver: Receiver, +} + +impl VmmServiceInner { + pub fn new( + api_sender: Sender, + api_evt: EventFd, + res_receiver: Receiver, + ) -> Self { + Self { + api_sender, + api_evt, + res_receiver, + } + } + + pub fn send_request(&self, request: ApiRequest) -> Result { + self.api_sender.send(request).map_err(Error::RequestSend)?; + self.api_evt.write(1).map_err(Error::EventFdWrite)?; + self.res_receiver.recv().map_err(Error::ResponseRecv) + } +} diff --git a/vendor/cube-hypervisor/vmm/src/clone3.rs b/vendor/cube-hypervisor/vmm/src/clone3.rs new file mode 100644 index 0000000..f08e5ad --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/clone3.rs @@ -0,0 +1,27 @@ +// Copyright 2021 Alyssa Ross +// SPDX-License-Identifier: Apache-2.0 + +use libc::{c_long, size_t, syscall, SYS_clone3}; + +pub const CLONE_CLEAR_SIGHAND: u64 = 0x100000000; + +#[repr(C)] +#[derive(Default)] +#[allow(non_camel_case_types)] +pub struct clone_args { + pub flags: u64, + pub pidfd: u64, + pub child_tid: u64, + pub parent_tid: u64, + pub exit_signal: u64, + pub stack: u64, + pub stack_size: u64, + pub tls: u64, + pub set_tid: u64, + pub set_tid_size: u64, + pub cgroup: u64, +} + +pub unsafe fn clone3(args: &mut clone_args, size: size_t) -> c_long { + syscall(SYS_clone3, args, size) +} diff --git a/vendor/cube-hypervisor/vmm/src/config.rs b/vendor/cube-hypervisor/vmm/src/config.rs new file mode 100644 index 0000000..7ce2c51 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/config.rs @@ -0,0 +1,3975 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +pub use virtio_devices::fs::{ + default_fsconfig_cache, default_fsconfig_read_only, default_fsconfig_rlimit_nofile, + default_fsconfig_thread_pool_size, default_fsconfig_xattr, BackendFsConfig, + BACKEND_FS_CACHE_ALWAYS, BACKEND_FS_CACHE_AUTO, BACKEND_FS_CACHE_NEVER, BACKEND_FS_CACHE_NONE, +}; +pub use virtio_devices::{RateLimiterConfig, TokenBucketConfig}; + +use crate::vm_config::*; +use clap::ArgMatches; +use option_parser::{ + ByteSized, IntegerList, OptionParser, OptionParserError, StringList, Toggle, Tuple, +}; +use serde::{Deserialize, Serialize}; +use std::collections::{BTreeSet, HashMap}; +use std::convert::From; +use std::convert::TryFrom; +use std::path::PathBuf; +use std::result; +use std::str::FromStr; +use std::{fmt, fs}; +use thiserror::Error; +use virtio_devices::block::MINIMUM_BLOCK_QUEUE_SIZE; +use virtiofsd::passthrough::xattrmap::XattrMap; + +const MAX_NUM_PCI_SEGMENTS: u16 = 16; + +const UPDATE_FS_SHARED_DIR_FIELD: &str = "shared_dir"; +const UPDATE_FS_ALLOWED_DIRS_FIELD: &str = "allowed_dirs"; +const UPDATE_FS_CACHE_FIELD: &str = "cache"; +const UPDATE_FS_REMAP_FILTER_FIELD: &str = "remap_filter"; + +/// Errors associated with VM configuration parameters. +#[derive(Debug, Error)] +pub enum Error { + /// Filesystem tag is missing + ParseFsTagMissing, + /// Filesystem socket is missing + ParseFsSockMissing, + /// Missing persistent memory file parameter. + ParsePmemFileMissing, + /// Missing vsock socket path parameter. + ParseVsockSockMissing, + /// Missing vsock cid parameter. + ParseVsockCidMissing, + /// Missing restore source_url parameter. + ParseRestoreSourceUrlMissing, + /// Missing restore device id parameter. + ParseRestoreDeviceIdMissing, + /// Missing restore device path parameter. + ParseRestoreDevicePathMissing, + /// Error parsing CPU options + ParseCpus(OptionParserError), + /// Invalid CPU features + InvalidCpuFeatures(String), + /// Invalid Ivshmem backend file size + InvalidIvshmemSize(u64), + /// Invalid Ivshmem backend file path + InvalidIvshmemPath(std::io::Error), + /// Error parsing memory options + ParseMemory(OptionParserError), + /// Error parsing memory zone options + ParseMemoryZone(OptionParserError), + /// Missing 'id' from memory zone + ParseMemoryZoneIdMissing, + /// Error parsing disk options + ParseDisk(OptionParserError), + /// Error parsing network options + ParseNetwork(OptionParserError), + /// Error parsing RNG options + ParseRng(OptionParserError), + /// Error parsing balloon options + ParseBalloon(OptionParserError), + /// Error parsing filesystem parameters + ParseFileSystem(OptionParserError), + /// Error parsing persistent memory parameters + ParsePersistentMemory(OptionParserError), + /// Failed parsing console + ParseConsole(OptionParserError), + /// No mode given for console + ParseConsoleInvalidModeGiven, + /// Failed parsing device parameters + ParseDevice(OptionParserError), + /// Missing path from device, + ParseDevicePathMissing, + /// Failed parsing vsock parameters + ParseVsock(OptionParserError), + /// Failed parsing restore parameters + ParseRestore(OptionParserError), + /// Failed parsing restore device parameters + ParseRestoreDevice(OptionParserError), + /// Failed parsing SGX EPC parameters + #[cfg(target_arch = "x86_64")] + ParseSgxEpc(OptionParserError), + /// Missing 'id' from SGX EPC section + #[cfg(target_arch = "x86_64")] + ParseSgxEpcIdMissing, + /// Failed parsing NUMA parameters + ParseNuma(OptionParserError), + /// Failed validating configuration + Validation(ValidationError), + #[cfg(feature = "tdx")] + /// Failed parsing TDX config + ParseTdx(OptionParserError), + #[cfg(feature = "tdx")] + /// No TDX firmware + FirmwarePathMissing, + /// Failed parsing userspace device + ParseUserDevice(OptionParserError), + /// Missing socket for userspace device + ParseUserDeviceSocketMissing, + /// Failed parsing platform parameters + ParsePlatform(OptionParserError), + /// Failed parsing vDPA device + ParseVdpa(OptionParserError), + /// Missing path for vDPA device + ParseVdpaPathMissing, + /// Failed parsing TPM device + ParseTpm(OptionParserError), + /// Failed parsing ivsmem device + ParseIvshmem(OptionParserError), + /// Missing path for TPM device + ParseTpmPathMissing, + /// Filesystem shared_dir is missing + ParseFsSharedDirMissing, + /// Missing path for ivsmem device + ParseIvshmemPathMissing, +} + +#[derive(Debug, PartialEq, Eq, Error)] +pub enum ValidationError { + /// No kernel specified + KernelMissing, + /// Missing file value for console + ConsoleFileMissing, + /// Max is less than boot + CpusMaxLowerThanBoot, + /// Both socket and path specified + DiskSocketAndPath, + /// Using vhost user requires shared memory + VhostUserRequiresSharedMemory, + /// No socket provided for vhost_use + VhostUserMissingSocket, + /// Trying to use IOMMU without PCI + IommuUnsupported, + /// Trying to use VFIO without PCI + VfioUnsupported, + /// CPU topology count doesn't match max + CpuTopologyCount, + /// One part of the CPU topology was zero + CpuTopologyZeroPart, + #[cfg(target_arch = "aarch64")] + /// Dies per package must be 1 + CpuTopologyDiesPerPackage, + /// Virtio needs a min of 2 queues + VnetQueueLowerThan2, + /// The input queue number for virtio_net must match the number of input fds + VnetQueueFdMismatch, + /// Using reserved fd + VnetReservedFd, + /// Hugepages not turned on + HugePageSizeWithoutHugePages, + /// Huge page size is not power of 2 + InvalidHugePageSize(u64), + /// CPU Hotplug is not permitted with TDX + #[cfg(feature = "tdx")] + TdxNoCpuHotplug, + /// Missing firmware for TDX + #[cfg(feature = "tdx")] + TdxFirmwareMissing, + /// Insuffient vCPUs for queues + TooManyQueues, + /// Invalid queue size + InvalidQueueSize(u16), + /// Need shared memory for vfio-user + UserDevicesRequireSharedMemory, + /// Memory zone is reused across NUMA nodes + MemoryZoneReused(String, u32, u32), + /// Invalid number of PCI segments + InvalidNumPciSegments(u16), + /// Invalid PCI segment id + InvalidPciSegment(u16), + /// Balloon too big + BalloonLargerThanRam(u64, u64), + /// On a IOMMU segment but not behind IOMMU + OnIommuSegment(u16), + // On a IOMMU segment but IOMMU not suported + IommuNotSupportedOnSegment(u16), + // Identifier is not unique + IdentifierNotUnique(String), + /// Invalid identifier + InvalidIdentifier(String), + /// Placing the device behind a virtual IOMMU is not supported + IommuNotSupported, + /// Duplicated device path (device added twice) + DuplicateDevicePath(String), + /// Provided MTU is lower than what the VIRTIO specification expects + InvalidMtu(u16), + /// native virtio-fs shouldn't have socket argument + NativeVirtioFsSocket, +} + +type ValidationResult = std::result::Result; + +impl fmt::Display for ValidationError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::ValidationError::*; + match self { + KernelMissing => write!(f, "No kernel specified"), + ConsoleFileMissing => write!(f, "Path missing when using file console mode"), + CpusMaxLowerThanBoot => write!(f, "Max CPUs lower than boot CPUs"), + DiskSocketAndPath => write!(f, "Disk path and vhost socket both provided"), + VhostUserRequiresSharedMemory => { + write!( + f, + "Using vhost-user requires using shared memory or huge pages" + ) + } + VhostUserMissingSocket => write!(f, "No socket provided when using vhost-user"), + IommuUnsupported => write!(f, "Using an IOMMU without PCI support is unsupported"), + VfioUnsupported => write!(f, "Using VFIO without PCI support is unsupported"), + CpuTopologyZeroPart => write!(f, "No part of the CPU topology can be zero"), + CpuTopologyCount => write!( + f, + "Product of CPU topology parts does not match maximum vCPUs" + ), + #[cfg(target_arch = "aarch64")] + CpuTopologyDiesPerPackage => write!(f, "Dies per package must be 1"), + VnetQueueLowerThan2 => write!(f, "Number of queues to virtio_net less than 2"), + VnetQueueFdMismatch => write!( + f, + "Number of queues to virtio_net does not match the number of input FDs" + ), + VnetReservedFd => write!(f, "Reserved fd number (<= 2)"), + HugePageSizeWithoutHugePages => { + write!(f, "Huge page size specified but huge pages not enabled") + } + InvalidHugePageSize(s) => { + write!(f, "Huge page size is not power of 2: {}", s) + } + #[cfg(feature = "tdx")] + TdxNoCpuHotplug => { + write!(f, "CPU hotplug is not permitted with TDX") + } + #[cfg(feature = "tdx")] + TdxFirmwareMissing => { + write!(f, "No TDX firmware specified") + } + TooManyQueues => { + write!(f, "Number of vCPUs is insufficient for number of queues") + } + InvalidQueueSize(s) => { + write!( + f, + "Queue size is smaller than {MINIMUM_BLOCK_QUEUE_SIZE}: {s}" + ) + } + UserDevicesRequireSharedMemory => { + write!( + f, + "Using user devices requires using shared memory or huge pages" + ) + } + MemoryZoneReused(s, u1, u2) => { + write!( + f, + "Memory zone: {} belongs to multiple NUMA nodes {} and {}", + s, u1, u2 + ) + } + InvalidNumPciSegments(n) => { + write!( + f, + "Number of PCI segments ({}) not in range of 1 to {}", + n, MAX_NUM_PCI_SEGMENTS + ) + } + InvalidPciSegment(pci_segment) => { + write!(f, "Invalid PCI segment id: {}", pci_segment) + } + BalloonLargerThanRam(balloon_size, ram_size) => { + write!( + f, + "Ballon size ({}) greater than RAM ({})", + balloon_size, ram_size + ) + } + OnIommuSegment(pci_segment) => { + write!( + f, + "Device is on an IOMMU PCI segment ({}) but not placed behind IOMMU", + pci_segment + ) + } + IommuNotSupportedOnSegment(pci_segment) => { + write!( + f, + "Device is on an IOMMU PCI segment ({}) but does not support being placed behind IOMMU", + pci_segment + ) + } + IdentifierNotUnique(s) => { + write!(f, "Identifier {} is not unique", s) + } + InvalidIdentifier(s) => { + write!(f, "Identifier {} is invalid", s) + } + IommuNotSupported => { + write!(f, "Device does not support being placed behind IOMMU") + } + DuplicateDevicePath(p) => write!(f, "Duplicated device path: {}", p), + NativeVirtioFsSocket => { + write!(f, "Native virtio-fs shouldn't have socket argument") + } + &InvalidMtu(mtu) => { + write!( + f, + "Provided MTU {} is lower than 1280 (expected by VIRTIO specification)", + mtu + ) + } + } + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + match self { + ParseConsole(o) => write!(f, "Error parsing --console: {}", o), + ParseConsoleInvalidModeGiven => { + write!(f, "Error parsing --console: invalid console mode given") + } + ParseCpus(o) => write!(f, "Error parsing --cpus: {}", o), + InvalidCpuFeatures(o) => write!(f, "Invalid feature in --cpus features list: {}", o), + InvalidIvshmemSize(o) => write!(f, "Invalid ivshmem backend file size: {}", o), + InvalidIvshmemPath(o) => write!(f, "Invalid ivshmem backend file path: {}", o), + ParseDevice(o) => write!(f, "Error parsing --device: {}", o), + ParseDevicePathMissing => write!(f, "Error parsing --device: path missing"), + ParseFileSystem(o) => write!(f, "Error parsing --fs: {}", o), + ParseFsSockMissing => write!(f, "Error parsing --fs: socket missing"), + ParseFsTagMissing => write!(f, "Error parsing --fs: tag missing"), + ParsePersistentMemory(o) => write!(f, "Error parsing --pmem: {}", o), + ParsePmemFileMissing => write!(f, "Error parsing --pmem: file missing"), + ParseVsock(o) => write!(f, "Error parsing --vsock: {}", o), + ParseVsockCidMissing => write!(f, "Error parsing --vsock: cid missing"), + ParseVsockSockMissing => write!(f, "Error parsing --vsock: socket missing"), + ParseMemory(o) => write!(f, "Error parsing --memory: {}", o), + ParseMemoryZone(o) => write!(f, "Error parsing --memory-zone: {}", o), + ParseMemoryZoneIdMissing => write!(f, "Error parsing --memory-zone: id missing"), + ParseNetwork(o) => write!(f, "Error parsing --net: {}", o), + ParseDisk(o) => write!(f, "Error parsing --disk: {}", o), + ParseRng(o) => write!(f, "Error parsing --rng: {}", o), + ParseBalloon(o) => write!(f, "Error parsing --balloon: {}", o), + ParseRestore(o) => write!(f, "Error parsing --restore: {}", o), + ParseRestoreDevice(o) => write!(f, "Error parsing --restore-device: {}", o), + #[cfg(target_arch = "x86_64")] + ParseSgxEpc(o) => write!(f, "Error parsing --sgx-epc: {}", o), + #[cfg(target_arch = "x86_64")] + ParseSgxEpcIdMissing => write!(f, "Error parsing --sgx-epc: id missing"), + ParseNuma(o) => write!(f, "Error parsing --numa: {}", o), + ParseRestoreSourceUrlMissing => { + write!(f, "Error parsing --restore: source_url missing") + } + ParseRestoreDeviceIdMissing => { + write!(f, "Error parsing --restore-device: id missing") + } + ParseRestoreDevicePathMissing => { + write!(f, "Error parsing --restore-device: path missing") + } + ParseUserDeviceSocketMissing => { + write!(f, "Error parsing --user-device: socket missing") + } + ParseUserDevice(o) => write!(f, "Error parsing --user-device: {}", o), + Validation(v) => write!(f, "Error validating configuration: {}", v), + #[cfg(feature = "tdx")] + ParseTdx(o) => write!(f, "Error parsing --tdx: {}", o), + #[cfg(feature = "tdx")] + FirmwarePathMissing => write!(f, "TDX firmware missing"), + ParsePlatform(o) => write!(f, "Error parsing --platform: {}", o), + ParseVdpa(o) => write!(f, "Error parsing --vdpa: {}", o), + ParseVdpaPathMissing => write!(f, "Error parsing --vdpa: path missing"), + ParseTpm(o) => write!(f, "Error parsing --tpm: {}", o), + ParseIvshmem(o) => write!(f, "Error parsing --ivshmem: {}", o), + ParseTpmPathMissing => write!(f, "Error parsing --tpm: path missing"), + ParseFsSharedDirMissing => write!(f, "Error parsing --shared_dir: path missing"), + ParseIvshmemPathMissing => write!(f, "Error parsing --ivshmem: path missing"), + } + } +} + +pub fn add_to_config(items: &mut Option>, item: T) { + if let Some(items) = items { + items.push(item); + } else { + *items = Some(vec![item]); + } +} + +pub type Result = result::Result; + +pub struct VmParams<'a> { + pub cpus: &'a str, + pub memory: &'a str, + pub memory_zones: Option>, + pub firmware: Option<&'a str>, + pub kernel: Option<&'a str>, + pub initramfs: Option<&'a str>, + pub cmdline: Option<&'a str>, + pub disks: Option>, + pub net: Option>, + pub rng: &'a str, + pub balloon: Option<&'a str>, + pub fs: Option>, + pub pmem: Option>, + pub serial: &'a str, + pub console: &'a str, + pub devices: Option>, + pub user_devices: Option>, + pub vdpa: Option>, + pub vsock: Option<&'a str>, + pub pvpanic: bool, + #[cfg(target_arch = "x86_64")] + pub sgx_epc: Option>, + pub numa: Option>, + pub watchdog: bool, + #[cfg(feature = "guest_debug")] + pub gdb: bool, + pub platform: Option<&'a str>, + pub tpm: Option<&'a str>, + pub sys_ctrl: bool, + pub ivshmem: Option<&'a str>, +} + +impl<'a> VmParams<'a> { + pub fn from_arg_matches(args: &'a ArgMatches) -> Self { + // These .unwrap()s cannot fail as there is a default value defined + let cpus = args.get_one::("cpus").unwrap(); + let memory = args.get_one::("memory").unwrap(); + let memory_zones: Option> = args + .get_many::("memory-zone") + .map(|x| x.map(|y| y as &str).collect()); + let rng = args.get_one::("rng").unwrap(); + let serial = args.get_one::("serial").unwrap(); + let firmware = args.get_one::("firmware").map(|x| x as &str); + let kernel = args.get_one::("kernel").map(|x| x as &str); + let initramfs = args.get_one::("initramfs").map(|x| x as &str); + let cmdline = args.get_one::("cmdline").map(|x| x as &str); + let disks: Option> = args + .get_many::("disk") + .map(|x| x.map(|y| y as &str).collect()); + let net: Option> = args + .get_many::("net") + .map(|x| x.map(|y| y as &str).collect()); + let console = args.get_one::("console").unwrap(); + let balloon = args.get_one::("balloon").map(|x| x as &str); + let fs: Option> = args + .get_many::("fs") + .map(|x| x.map(|y| y as &str).collect()); + let pmem: Option> = args + .get_many::("pmem") + .map(|x| x.map(|y| y as &str).collect()); + let devices: Option> = args + .get_many::("device") + .map(|x| x.map(|y| y as &str).collect()); + let user_devices: Option> = args + .get_many::("user-device") + .map(|x| x.map(|y| y as &str).collect()); + let vdpa: Option> = args + .get_many::("vdpa") + .map(|x| x.map(|y| y as &str).collect()); + let vsock: Option<&str> = args.get_one::("vsock").map(|x| x as &str); + #[cfg(target_arch = "x86_64")] + let sgx_epc: Option> = args + .get_many::("sgx-epc") + .map(|x| x.map(|y| y as &str).collect()); + let numa: Option> = args + .get_many::("numa") + .map(|x| x.map(|y| y as &str).collect()); + let watchdog = args.get_flag("watchdog"); + let platform = args.get_one::("platform").map(|x| x as &str); + #[cfg(feature = "guest_debug")] + let gdb = args.contains_id("gdb"); + let tpm: Option<&str> = args.get_one::("tpm").map(|x| x as &str); + let sys_ctrl = args.get_flag("sys-ctrl"); + let ivshmem: Option<&str> = args.get_one::("ivshmem").map(|x| x as &str); + let pvpanic = args.get_flag("pvpanic"); + VmParams { + cpus, + memory, + memory_zones, + firmware, + kernel, + initramfs, + cmdline, + disks, + net, + rng, + balloon, + fs, + pmem, + serial, + console, + devices, + user_devices, + vdpa, + vsock, + pvpanic, + #[cfg(target_arch = "x86_64")] + sgx_epc, + numa, + watchdog, + #[cfg(feature = "guest_debug")] + gdb, + platform, + tpm, + sys_ctrl, + ivshmem, + } + } +} + +#[derive(Debug)] +pub enum ParseHotplugMethodError { + InvalidValue(String), +} + +impl FromStr for HotplugMethod { + type Err = ParseHotplugMethodError; + + fn from_str(s: &str) -> std::result::Result { + match s.to_lowercase().as_str() { + "acpi" => Ok(HotplugMethod::Acpi), + "virtio-mem" => Ok(HotplugMethod::VirtioMem), + _ => Err(ParseHotplugMethodError::InvalidValue(s.to_owned())), + } + } +} + +pub enum CpuTopologyParseError { + InvalidValue(String), +} + +impl FromStr for CpuTopology { + type Err = CpuTopologyParseError; + + fn from_str(s: &str) -> std::result::Result { + let parts: Vec<&str> = s.split(':').collect(); + + if parts.len() != 4 { + return Err(Self::Err::InvalidValue(s.to_owned())); + } + + let t = CpuTopology { + threads_per_core: parts[0] + .parse() + .map_err(|_| Self::Err::InvalidValue(s.to_owned()))?, + cores_per_die: parts[1] + .parse() + .map_err(|_| Self::Err::InvalidValue(s.to_owned()))?, + dies_per_package: parts[2] + .parse() + .map_err(|_| Self::Err::InvalidValue(s.to_owned()))?, + packages: parts[3] + .parse() + .map_err(|_| Self::Err::InvalidValue(s.to_owned()))?, + }; + + Ok(t) + } +} + +#[derive(Debug)] +pub enum ParseCompatibleModeError { + InvalidValue(String), +} + +impl FromStr for CompatibleMode { + type Err = ParseCompatibleModeError; + + fn from_str(s: &str) -> std::result::Result { + match s.to_lowercase().as_str() { + "vendor" => Ok(CompatibleMode::Vendor), + "max" => Ok(CompatibleMode::Max), + "ignore" => Ok(CompatibleMode::Ignore), + _ => Err(ParseCompatibleModeError::InvalidValue(s.to_owned())), + } + } +} + +impl CpusConfig { + pub fn parse(cpus: &str) -> Result { + let mut parser = OptionParser::new(); + parser + .add("boot") + .add("max") + .add("topology") + .add("kvm_hyperv") + .add("max_phys_bits") + .add("affinity") + .add("features") + .add("compatible"); + parser.parse(cpus).map_err(Error::ParseCpus)?; + + let boot_vcpus: u8 = parser + .convert("boot") + .map_err(Error::ParseCpus)? + .unwrap_or(DEFAULT_VCPUS); + let max_vcpus: u8 = parser + .convert("max") + .map_err(Error::ParseCpus)? + .unwrap_or(boot_vcpus); + let topology = parser.convert("topology").map_err(Error::ParseCpus)?; + let kvm_hyperv = parser + .convert::("kvm_hyperv") + .map_err(Error::ParseCpus)? + .unwrap_or(Toggle(false)) + .0; + let max_phys_bits = parser + .convert::("max_phys_bits") + .map_err(Error::ParseCpus)? + .unwrap_or(DEFAULT_MAX_PHYS_BITS); + let affinity = parser + .convert::>>("affinity") + .map_err(Error::ParseCpus)? + .map(|v| { + v.0.iter() + .map(|(e1, e2)| CpuAffinity { + vcpu: *e1, + host_cpus: e2.clone(), + }) + .collect() + }); + let features_list = parser + .convert::("features") + .map_err(Error::ParseCpus)? + .unwrap_or_default(); + let compatible = parser + .convert("compatible") + .map_err(Error::ParseCpus)? + .unwrap_or_default(); + + // Some ugliness here as the features being checked might be disabled + // at compile time causing the below allow and the need to specify the + // ref type in the match. + // The issue will go away once kvm_hyperv is moved under the features + // list as it will always be checked for. + #[allow(unused_mut)] + let mut features = CpuFeatures::default(); + for s in features_list.0 { + match >::as_ref(&s) { + #[cfg(target_arch = "x86_64")] + "amx" => { + features.amx = true; + Ok(()) + } + _ => Err(Error::InvalidCpuFeatures(s)), + }?; + } + + Ok(CpusConfig { + boot_vcpus, + max_vcpus, + topology, + kvm_hyperv, + max_phys_bits, + affinity, + features, + compatible, + }) + } +} + +impl PlatformConfig { + pub fn parse(platform: &str) -> Result { + let mut parser = OptionParser::new(); + parser + .add("num_pci_segments") + .add("iommu_segments") + .add("serial_number") + .add("uuid") + .add("oem_strings"); + #[cfg(feature = "tdx")] + parser.add("tdx"); + parser.parse(platform).map_err(Error::ParsePlatform)?; + + let num_pci_segments: u16 = parser + .convert("num_pci_segments") + .map_err(Error::ParsePlatform)? + .unwrap_or(DEFAULT_NUM_PCI_SEGMENTS); + let iommu_segments = parser + .convert::("iommu_segments") + .map_err(Error::ParsePlatform)? + .map(|v| v.0.iter().map(|e| *e as u16).collect()); + let serial_number = parser + .convert("serial_number") + .map_err(Error::ParsePlatform)?; + let uuid = parser.convert("uuid").map_err(Error::ParsePlatform)?; + let oem_strings = parser + .convert::("oem_strings") + .map_err(Error::ParsePlatform)? + .map(|v| v.0); + #[cfg(feature = "tdx")] + let tdx = parser + .convert::("tdx") + .map_err(Error::ParsePlatform)? + .unwrap_or(Toggle(false)) + .0; + Ok(PlatformConfig { + num_pci_segments, + iommu_segments, + serial_number, + uuid, + oem_strings, + #[cfg(feature = "tdx")] + tdx, + }) + } + + pub fn validate(&self) -> ValidationResult<()> { + if self.num_pci_segments == 0 || self.num_pci_segments > MAX_NUM_PCI_SEGMENTS { + return Err(ValidationError::InvalidNumPciSegments( + self.num_pci_segments, + )); + } + + if let Some(iommu_segments) = &self.iommu_segments { + for segment in iommu_segments { + if *segment >= self.num_pci_segments { + return Err(ValidationError::InvalidPciSegment(*segment)); + } + } + } + + Ok(()) + } +} + +impl MemoryConfig { + pub fn parse(memory: &str, memory_zones: Option>) -> Result { + let mut parser = OptionParser::new(); + parser + .add("size") + .add("file") + .add("mergeable") + .add("hotplug_method") + .add("hotplug_size") + .add("hotplugged_size") + .add("shared") + .add("hugepages") + .add("hugepage_size") + .add("prefault") + .add("thp") + .add("dirty_log"); + parser.parse(memory).map_err(Error::ParseMemory)?; + + let size = parser + .convert::("size") + .map_err(Error::ParseMemory)? + .unwrap_or(ByteSized(DEFAULT_MEMORY_MB << 20)) + .0; + let mergeable = parser + .convert::("mergeable") + .map_err(Error::ParseMemory)? + .unwrap_or(Toggle(false)) + .0; + let hotplug_method = parser + .convert("hotplug_method") + .map_err(Error::ParseMemory)? + .unwrap_or_default(); + let hotplug_size = parser + .convert::("hotplug_size") + .map_err(Error::ParseMemory)? + .map(|v| v.0); + let hotplugged_size = parser + .convert::("hotplugged_size") + .map_err(Error::ParseMemory)? + .map(|v| v.0); + let shared = parser + .convert::("shared") + .map_err(Error::ParseMemory)? + .unwrap_or(Toggle(false)) + .0; + let hugepages = parser + .convert::("hugepages") + .map_err(Error::ParseMemory)? + .unwrap_or(Toggle(false)) + .0; + let hugepage_size = parser + .convert::("hugepage_size") + .map_err(Error::ParseMemory)? + .map(|v| v.0); + let prefault = parser + .convert::("prefault") + .map_err(Error::ParseMemory)? + .unwrap_or(Toggle(false)) + .0; + let thp = parser + .convert::("thp") + .map_err(Error::ParseMemory)? + .unwrap_or(Toggle(true)) + .0; + let dirty_log = parser + .convert::("dirty_log") + .map_err(Error::ParseMemory)? + .unwrap_or(Toggle(false)) + .0; + + let zones: Option> = if let Some(memory_zones) = &memory_zones { + let mut zones = Vec::new(); + for memory_zone in memory_zones.iter() { + let mut parser = OptionParser::new(); + parser + .add("id") + .add("size") + .add("file") + .add("shared") + .add("hugepages") + .add("hugepage_size") + .add("host_numa_node") + .add("hotplug_size") + .add("hotplugged_size") + .add("prefault"); + parser.parse(memory_zone).map_err(Error::ParseMemoryZone)?; + + let id = parser.get("id").ok_or(Error::ParseMemoryZoneIdMissing)?; + let size = parser + .convert::("size") + .map_err(Error::ParseMemoryZone)? + .unwrap_or(ByteSized(DEFAULT_MEMORY_MB << 20)) + .0; + let file = parser.get("file").map(PathBuf::from); + let shared = parser + .convert::("shared") + .map_err(Error::ParseMemoryZone)? + .unwrap_or(Toggle(false)) + .0; + let hugepages = parser + .convert::("hugepages") + .map_err(Error::ParseMemoryZone)? + .unwrap_or(Toggle(false)) + .0; + let hugepage_size = parser + .convert::("hugepage_size") + .map_err(Error::ParseMemoryZone)? + .map(|v| v.0); + + let host_numa_node = parser + .convert::("host_numa_node") + .map_err(Error::ParseMemoryZone)?; + let hotplug_size = parser + .convert::("hotplug_size") + .map_err(Error::ParseMemoryZone)? + .map(|v| v.0); + let hotplugged_size = parser + .convert::("hotplugged_size") + .map_err(Error::ParseMemoryZone)? + .map(|v| v.0); + let prefault = parser + .convert::("prefault") + .map_err(Error::ParseMemoryZone)? + .unwrap_or(Toggle(false)) + .0; + + zones.push(MemoryZoneConfig { + id, + size, + file, + shared, + hugepages, + hugepage_size, + host_numa_node, + hotplug_size, + hotplugged_size, + prefault, + }); + } + Some(zones) + } else { + None + }; + + Ok(MemoryConfig { + size, + mergeable, + hotplug_method, + hotplug_size, + hotplugged_size, + shared, + hugepages, + hugepage_size, + prefault, + zones, + thp, + dirty_log, + }) + } + + pub fn total_size(&self) -> u64 { + let mut size = self.size; + if let Some(hotplugged_size) = self.hotplugged_size { + size += hotplugged_size; + } + + if let Some(zones) = &self.zones { + for zone in zones.iter() { + size += zone.size; + if let Some(hotplugged_size) = zone.hotplugged_size { + size += hotplugged_size; + } + } + } + + size + } + + pub fn exist_shared(&self) -> bool { + if self.shared { + return true; + } + + if let Some(zones) = &self.zones { + for zone in zones.iter() { + if zone.shared { + return true; + } + } + } + + false + } + + pub fn has_hotplug_virtio_mem(&self) -> bool { + self.hotplug_method == HotplugMethod::VirtioMem + && self.hotplugged_size.is_some() + && self.hotplugged_size.unwrap() > 0 + } +} + +impl DiskConfig { + pub const SYNTAX: &'static str = "Disk parameters \ + \"path=,readonly=on|off,direct=on|off,iommu=on|off,\ + num_queues=,queue_size=,\ + vhost_user=on|off,socket=,\ + bw_size=,bw_one_time_burst=,bw_refill_time=,\ + ops_size=,ops_one_time_burst=,ops_refill_time=,\ + id=,pci_segment=\""; + + pub fn parse(disk: &str) -> Result { + let mut parser = OptionParser::new(); + parser + .add("path") + .add("readonly") + .add("direct") + .add("iommu") + .add("queue_size") + .add("num_queues") + .add("vhost_user") + .add("socket") + .add("bw_size") + .add("bw_one_time_burst") + .add("bw_refill_time") + .add("ops_size") + .add("ops_one_time_burst") + .add("ops_refill_time") + .add("id") + .add("_disable_io_uring") + .add("pci_segment"); + parser.parse(disk).map_err(Error::ParseDisk)?; + + let path = parser.get("path").map(PathBuf::from); + let readonly = parser + .convert::("readonly") + .map_err(Error::ParseDisk)? + .unwrap_or(Toggle(false)) + .0; + let direct = parser + .convert::("direct") + .map_err(Error::ParseDisk)? + .unwrap_or(Toggle(false)) + .0; + let iommu = parser + .convert::("iommu") + .map_err(Error::ParseDisk)? + .unwrap_or(Toggle(false)) + .0; + let queue_size = parser + .convert("queue_size") + .map_err(Error::ParseDisk)? + .unwrap_or_else(default_diskconfig_queue_size); + let num_queues = parser + .convert("num_queues") + .map_err(Error::ParseDisk)? + .unwrap_or_else(default_diskconfig_num_queues); + let vhost_user = parser + .convert::("vhost_user") + .map_err(Error::ParseDisk)? + .unwrap_or(Toggle(false)) + .0; + let vhost_socket = parser.get("socket"); + let id = parser.get("id"); + let disable_io_uring = parser + .convert::("_disable_io_uring") + .map_err(Error::ParseDisk)? + .unwrap_or(Toggle(false)) + .0; + let pci_segment = parser + .convert("pci_segment") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let bw_size = parser + .convert("bw_size") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let bw_one_time_burst = parser + .convert("bw_one_time_burst") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let bw_refill_time = parser + .convert("bw_refill_time") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let ops_size = parser + .convert("ops_size") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let ops_one_time_burst = parser + .convert("ops_one_time_burst") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let ops_refill_time = parser + .convert("ops_refill_time") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let bw_tb_config = if bw_size != 0 && bw_refill_time != 0 { + Some(TokenBucketConfig { + size: bw_size, + one_time_burst: Some(bw_one_time_burst), + refill_time: bw_refill_time, + }) + } else { + None + }; + let ops_tb_config = if ops_size != 0 && ops_refill_time != 0 { + Some(TokenBucketConfig { + size: ops_size, + one_time_burst: Some(ops_one_time_burst), + refill_time: ops_refill_time, + }) + } else { + None + }; + let rate_limiter_config = if bw_tb_config.is_some() || ops_tb_config.is_some() { + Some(RateLimiterConfig { + bandwidth: bw_tb_config, + ops: ops_tb_config, + }) + } else { + None + }; + + Ok(DiskConfig { + path, + readonly, + direct, + iommu, + num_queues, + queue_size, + vhost_user, + vhost_socket, + rate_limiter_config, + id, + disable_io_uring, + pci_segment, + }) + } + + pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> { + if self.num_queues > vm_config.cpus.boot_vcpus as usize { + return Err(ValidationError::TooManyQueues); + } + + if self.queue_size <= MINIMUM_BLOCK_QUEUE_SIZE { + return Err(ValidationError::InvalidQueueSize(self.queue_size)); + } + + if self.vhost_user && self.iommu { + return Err(ValidationError::IommuNotSupported); + } + + if let Some(platform_config) = vm_config.platform.as_ref() { + if self.pci_segment >= platform_config.num_pci_segments { + return Err(ValidationError::InvalidPciSegment(self.pci_segment)); + } + + if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() { + if iommu_segments.contains(&self.pci_segment) && !self.iommu { + return Err(ValidationError::OnIommuSegment(self.pci_segment)); + } + } + } + + Ok(()) + } +} + +#[derive(Debug)] +pub enum ParseVhostModeError { + InvalidValue(String), +} + +impl FromStr for VhostMode { + type Err = ParseVhostModeError; + + fn from_str(s: &str) -> std::result::Result { + match s.to_lowercase().as_str() { + "client" => Ok(VhostMode::Client), + "server" => Ok(VhostMode::Server), + _ => Err(ParseVhostModeError::InvalidValue(s.to_owned())), + } + } +} + +impl NetConfig { + pub const SYNTAX: &'static str = "Network parameters \ + \"tap=,ip=,mask=,mac=,fd=,iommu=on|off,\ + num_queues=,queue_size=,id=,\ + vhost_user=,socket=,vhost_mode=client|server,\ + bw_size=,bw_one_time_burst=,bw_refill_time=,\ + ops_size=,ops_one_time_burst=,ops_refill_time=,pci_segment=\""; + + pub fn parse(net: &str) -> Result { + let mut parser = OptionParser::new(); + + parser + .add("tap") + .add("ip") + .add("mask") + .add("mac") + .add("host_mac") + .add("mtu") + .add("iommu") + .add("queue_size") + .add("num_queues") + .add("vhost_user") + .add("socket") + .add("vhost_mode") + .add("id") + .add("fd") + .add("bw_size") + .add("bw_one_time_burst") + .add("bw_refill_time") + .add("ops_size") + .add("ops_one_time_burst") + .add("ops_refill_time") + .add("pci_segment"); + parser.parse(net).map_err(Error::ParseNetwork)?; + + let tap = parser.get("tap"); + let ip = parser + .convert("ip") + .map_err(Error::ParseNetwork)? + .unwrap_or_else(default_netconfig_ip); + let mask = parser + .convert("mask") + .map_err(Error::ParseNetwork)? + .unwrap_or_else(default_netconfig_mask); + let mac = parser + .convert("mac") + .map_err(Error::ParseNetwork)? + .unwrap_or_else(default_netconfig_mac); + let host_mac = parser.convert("host_mac").map_err(Error::ParseNetwork)?; + let mtu = parser.convert("mtu").map_err(Error::ParseNetwork)?; + let iommu = parser + .convert::("iommu") + .map_err(Error::ParseNetwork)? + .unwrap_or(Toggle(false)) + .0; + let queue_size = parser + .convert("queue_size") + .map_err(Error::ParseNetwork)? + .unwrap_or_else(default_netconfig_queue_size); + let num_queues = parser + .convert("num_queues") + .map_err(Error::ParseNetwork)? + .unwrap_or_else(default_netconfig_num_queues); + let vhost_user = parser + .convert::("vhost_user") + .map_err(Error::ParseNetwork)? + .unwrap_or(Toggle(false)) + .0; + let vhost_socket = parser.get("socket"); + let vhost_mode = parser + .convert("vhost_mode") + .map_err(Error::ParseNetwork)? + .unwrap_or_default(); + let id = parser.get("id"); + let fds = parser + .convert::("fd") + .map_err(Error::ParseNetwork)? + .map(|v| v.0.iter().map(|e| *e as i32).collect()); + let pci_segment = parser + .convert("pci_segment") + .map_err(Error::ParseNetwork)? + .unwrap_or_default(); + let bw_size = parser + .convert("bw_size") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let bw_one_time_burst = parser + .convert("bw_one_time_burst") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let bw_refill_time = parser + .convert("bw_refill_time") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let ops_size = parser + .convert("ops_size") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let ops_one_time_burst = parser + .convert("ops_one_time_burst") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let ops_refill_time = parser + .convert("ops_refill_time") + .map_err(Error::ParseDisk)? + .unwrap_or_default(); + let bw_tb_config = if bw_size != 0 && bw_refill_time != 0 { + Some(TokenBucketConfig { + size: bw_size, + one_time_burst: Some(bw_one_time_burst), + refill_time: bw_refill_time, + }) + } else { + None + }; + let ops_tb_config = if ops_size != 0 && ops_refill_time != 0 { + Some(TokenBucketConfig { + size: ops_size, + one_time_burst: Some(ops_one_time_burst), + refill_time: ops_refill_time, + }) + } else { + None + }; + let rate_limiter_config = if bw_tb_config.is_some() || ops_tb_config.is_some() { + Some(RateLimiterConfig { + bandwidth: bw_tb_config, + ops: ops_tb_config, + }) + } else { + None + }; + + let config = NetConfig { + tap, + ip, + mask, + mac, + host_mac, + mtu, + iommu, + num_queues, + queue_size, + vhost_user, + vhost_socket, + vhost_mode, + id, + fds, + rate_limiter_config, + pci_segment, + }; + Ok(config) + } + + pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> { + if self.num_queues < 2 { + return Err(ValidationError::VnetQueueLowerThan2); + } + + if self.fds.is_some() && self.fds.as_ref().unwrap().len() * 2 != self.num_queues { + return Err(ValidationError::VnetQueueFdMismatch); + } + + if let Some(fds) = self.fds.as_ref() { + for fd in fds { + if *fd <= 2 { + return Err(ValidationError::VnetReservedFd); + } + } + } + + if (self.num_queues / 2) > vm_config.cpus.boot_vcpus as usize { + return Err(ValidationError::TooManyQueues); + } + + if self.vhost_user && self.iommu { + return Err(ValidationError::IommuNotSupported); + } + + if let Some(platform_config) = vm_config.platform.as_ref() { + if self.pci_segment >= platform_config.num_pci_segments { + return Err(ValidationError::InvalidPciSegment(self.pci_segment)); + } + + if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() { + if iommu_segments.contains(&self.pci_segment) && !self.iommu { + return Err(ValidationError::OnIommuSegment(self.pci_segment)); + } + } + } + + if let Some(mtu) = self.mtu { + if mtu < virtio_devices::net::MIN_MTU { + return Err(ValidationError::InvalidMtu(mtu)); + } + } + + Ok(()) + } +} + +impl RngConfig { + pub fn parse(rng: &str) -> Result { + let mut parser = OptionParser::new(); + parser.add("src").add("iommu"); + parser.parse(rng).map_err(Error::ParseRng)?; + + let src = PathBuf::from( + parser + .get("src") + .unwrap_or_else(|| DEFAULT_RNG_SOURCE.to_owned()), + ); + let iommu = parser + .convert::("iommu") + .map_err(Error::ParseRng)? + .unwrap_or(Toggle(false)) + .0; + + Ok(RngConfig { src, iommu }) + } +} + +impl BalloonConfig { + pub const SYNTAX: &'static str = + "Balloon parameters \"size=,deflate_on_oom=on|off,\ + free_page_reporting=on|off\""; + + pub fn parse(balloon: &str) -> Result { + let mut parser = OptionParser::new(); + parser.add("size"); + parser.add("deflate_on_oom"); + parser.add("free_page_reporting"); + parser.parse(balloon).map_err(Error::ParseBalloon)?; + + let size = parser + .convert::("size") + .map_err(Error::ParseBalloon)? + .map(|v| v.0) + .unwrap_or(0); + + let deflate_on_oom = parser + .convert::("deflate_on_oom") + .map_err(Error::ParseBalloon)? + .unwrap_or(Toggle(false)) + .0; + + let free_page_reporting = parser + .convert::("free_page_reporting") + .map_err(Error::ParseBalloon)? + .unwrap_or(Toggle(false)) + .0; + + Ok(BalloonConfig { + size, + deflate_on_oom, + free_page_reporting, + }) + } +} + +impl FsConfig { + pub const SYNTAX: &'static str = "virtio-fs parameters \ + \"tag=,socket=,num_queues=,\ + queue_size=,id=,pci_segment=,\ + native=,shared_dir=,xattr=,\ + posix_acl=,xattrmap=,announce_submounts=\ + cache=,no_readdirplus=,writeback=,\ + allow_direct_io=,read_only=,rlimit_nofile=,\ + killpriv_v2=,security_label=,\ + thread_pool_size=,ops_size=,\ + ops_one_time_burst=,ops_refill_time=,\ + bw_size=,bw_one_time_burst=,bw_refill_time=,\ + allowed_dirs=,remap_filter=\""; + + fn add_frontend_args(parser: &mut OptionParser) { + parser + .add("tag") + .add("queue_size") + .add("num_queues") + .add("socket") + .add("id") + .add("pci_segment") + .add("native"); + } + + fn add_backend_args(parser: &mut OptionParser) { + parser + .add("shared_dir") + .add("thread_pool_size") + .add("xattr") + .add("posix_acl") + .add("xattrmap") + .add("announce_submounts") + .add("cache") + .add("no_readdirplus") + .add("writeback") + .add("allow_direct_io") + .add("read_only") + .add("rlimit_nofile") + .add("killpriv_v2") + .add("security_label") + .add("allowed_dirs") + .add("remap_filter"); + } + + fn add_ratelimiter_args(parser: &mut OptionParser) { + parser + .add("ops_size") + .add("ops_one_time_burst") + .add("ops_refill_time") + .add("bw_size") + .add("bw_one_time_burst") + .add("bw_refill_time"); + } + + fn parse_backendfs(parser: &OptionParser) -> Result { + let shared_dir = parser + .get("shared_dir") + .ok_or(Error::ParseFsSharedDirMissing)?; + let thread_pool_size = parser + .convert("thread_pool_size") + .map_err(Error::ParseFileSystem)? + .unwrap_or_else(default_fsconfig_thread_pool_size); + let xattr = parser + .convert("xattr") + .map_err(Error::ParseFileSystem)? + .unwrap_or_else(default_fsconfig_xattr); + let posix_acl = parser + .convert::("posix_acl") + .map_err(Error::ParseFileSystem)? + .unwrap_or(Toggle(false)) + .0; + let xattrmap = parser.get("xattrmap"); + if let Some(ref map_str) = xattrmap { + XattrMap::try_from(map_str.as_str()).map_err(|_| { + Error::ParseFileSystem(OptionParserError::Conversion( + "xattrmap".to_string(), + map_str.to_string(), + )) + })?; + } + let announce_submounts = parser + .convert::("announce_submounts") + .map_err(Error::ParseFileSystem)? + .unwrap_or(Toggle(false)) + .0; + let no_readdirplus = parser + .convert::("no_readdirplus") + .map_err(Error::ParseFileSystem)? + .unwrap_or(Toggle(false)) + .0; + let writeback = parser + .convert::("writeback") + .map_err(Error::ParseFileSystem)? + .unwrap_or(Toggle(false)) + .0; + let allow_direct_io = parser + .convert::("allow_direct_io") + .map_err(Error::ParseFileSystem)? + .unwrap_or(Toggle(false)) + .0; + let read_only = parser + .convert("read_only") + .map_err(Error::ParseFileSystem)? + .unwrap_or_else(default_fsconfig_read_only); + let killpriv_v2 = parser + .convert::("killpriv_v2") + .map_err(Error::ParseFileSystem)? + .unwrap_or(Toggle(false)) + .0; + let security_label = parser + .convert::("security_label") + .map_err(Error::ParseFileSystem)? + .unwrap_or(Toggle(false)) + .0; + let cache; + if let Some(cache_string) = parser.get("cache") { + cache = match cache_string.as_str() { + "auto" => BACKEND_FS_CACHE_AUTO, + "always" => BACKEND_FS_CACHE_ALWAYS, + "never" => BACKEND_FS_CACHE_NEVER, + "none" => BACKEND_FS_CACHE_NONE, + cache_str => { + return Err(Error::ParseFileSystem(OptionParserError::Conversion( + "cache".to_string(), + cache_str.to_string(), + ))) + } + } + } else { + cache = default_fsconfig_cache(); + } + let rlimit_nofile = parser + .convert("rlimit_nofile") + .map_err(Error::ParseFileSystem)? + .unwrap_or_else(default_fsconfig_rlimit_nofile); + let allowed_dirs = parser + .convert::("allowed_dirs") + .map_err(Error::ParseFileSystem)? + .map(|v| v.0); + let remap_filter = parser + .convert::("remap_filter") + .map_err(Error::ParseFileSystem)? + .unwrap_or(Toggle(false)) + .0; + + Ok(BackendFsConfig { + shared_dir, + thread_pool_size, + xattr, + posix_acl, + xattrmap, + announce_submounts, + cache, + no_readdirplus, + writeback, + read_only, + allow_direct_io, + rlimit_nofile, + killpriv_v2, + security_label, + allowed_dirs, + remap_filter, + }) + } + + pub fn parse(fs: &str) -> Result { + let mut parser = OptionParser::new(); + Self::add_frontend_args(&mut parser); + Self::add_backend_args(&mut parser); + Self::add_ratelimiter_args(&mut parser); + + parser.parse(fs).map_err(Error::ParseFileSystem)?; + + let native = parser + .convert::("native") + .map_err(Error::ParseFileSystem)? + .unwrap_or(Toggle(false)) + .0; + + let tag = parser.get("tag").ok_or(Error::ParseFsTagMissing)?; + let mut socket = PathBuf::new(); + if !native { + socket = PathBuf::from(parser.get("socket").ok_or(Error::ParseFsSockMissing)?); + } + + let queue_size = parser + .convert("queue_size") + .map_err(Error::ParseFileSystem)? + .unwrap_or_else(default_fsconfig_queue_size); + let num_queues = parser + .convert("num_queues") + .map_err(Error::ParseFileSystem)? + .unwrap_or_else(default_fsconfig_num_queues); + + let id = parser.get("id"); + + let pci_segment = parser + .convert("pci_segment") + .map_err(Error::ParseFileSystem)? + .unwrap_or_default(); + + let mut backendfs_config = None; + // backend args are ignored if native is false + let rate_limiter_config = if native { + backendfs_config = Some(Self::parse_backendfs(&parser)?); + + let ops_size = parser + .convert("ops_size") + .map_err(Error::ParseFileSystem)? + .unwrap_or_default(); + let ops_one_time_burst = parser + .convert("ops_one_time_burst") + .map_err(Error::ParseFileSystem)? + .unwrap_or_default(); + let ops_refill_time = parser + .convert("ops_refill_time") + .map_err(Error::ParseFileSystem)? + .unwrap_or_default(); + let ops_tb_config = if ops_size != 0 && ops_refill_time != 0 { + Some(TokenBucketConfig { + size: ops_size, + one_time_burst: Some(ops_one_time_burst), + refill_time: ops_refill_time, + }) + } else { + None + }; + + let bw_size = parser + .convert("bw_size") + .map_err(Error::ParseFileSystem)? + .unwrap_or_default(); + let bw_one_time_burst = parser + .convert("bw_one_time_burst") + .map_err(Error::ParseFileSystem)? + .unwrap_or_default(); + let bw_refill_time = parser + .convert("bw_refill_time") + .map_err(Error::ParseFileSystem)? + .unwrap_or_default(); + let bw_tb_config = if bw_size != 0 && bw_refill_time != 0 { + Some(TokenBucketConfig { + size: bw_size, + one_time_burst: Some(bw_one_time_burst), + refill_time: bw_refill_time, + }) + } else { + None + }; + + if ops_tb_config.is_some() || bw_tb_config.is_some() { + Some(RateLimiterConfig { + bandwidth: bw_tb_config, + ops: ops_tb_config, + }) + } else { + None + } + } else { + None + }; + + Ok(FsConfig { + tag, + socket, + num_queues, + queue_size, + id, + pci_segment, + backendfs_config, + rate_limiter_config, + }) + } + + pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> { + if self.num_queues > vm_config.cpus.boot_vcpus as usize { + return Err(ValidationError::TooManyQueues); + } + + if let Some(platform_config) = vm_config.platform.as_ref() { + if self.pci_segment >= platform_config.num_pci_segments { + return Err(ValidationError::InvalidPciSegment(self.pci_segment)); + } + + if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() { + if iommu_segments.contains(&self.pci_segment) { + return Err(ValidationError::IommuNotSupportedOnSegment( + self.pci_segment, + )); + } + } + } + + if self.backendfs_config.is_some() { + if self.socket.to_str() != Some("") { + return Err(ValidationError::NativeVirtioFsSocket); + } + } else { + if !vm_config.backed_by_shared_memory() { + return Err(ValidationError::VhostUserRequiresSharedMemory); + } + if self.socket.to_str() == Some("") { + return Err(ValidationError::VhostUserMissingSocket); + } + } + + Ok(()) + } +} + +impl PmemConfig { + pub const SYNTAX: &'static str = "Persistent memory parameters \ + \"file=,size=,iommu=on|off,\ + discard_writes=on|off,id=,pci_segment=\""; + pub fn parse(pmem: &str) -> Result { + let mut parser = OptionParser::new(); + parser + .add("size") + .add("file") + .add("iommu") + .add("discard_writes") + .add("id") + .add("pci_segment"); + parser.parse(pmem).map_err(Error::ParsePersistentMemory)?; + + let file = PathBuf::from(parser.get("file").ok_or(Error::ParsePmemFileMissing)?); + let size = parser + .convert::("size") + .map_err(Error::ParsePersistentMemory)? + .map(|v| v.0); + let iommu = parser + .convert::("iommu") + .map_err(Error::ParsePersistentMemory)? + .unwrap_or(Toggle(false)) + .0; + let discard_writes = parser + .convert::("discard_writes") + .map_err(Error::ParsePersistentMemory)? + .unwrap_or(Toggle(false)) + .0; + let id = parser.get("id"); + let pci_segment = parser + .convert("pci_segment") + .map_err(Error::ParsePersistentMemory)? + .unwrap_or_default(); + + Ok(PmemConfig { + file, + size, + iommu, + discard_writes, + id, + pci_segment, + }) + } + + pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> { + if let Some(platform_config) = vm_config.platform.as_ref() { + if self.pci_segment >= platform_config.num_pci_segments { + return Err(ValidationError::InvalidPciSegment(self.pci_segment)); + } + + if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() { + if iommu_segments.contains(&self.pci_segment) && !self.iommu { + return Err(ValidationError::OnIommuSegment(self.pci_segment)); + } + } + } + + Ok(()) + } +} + +impl ConsoleConfig { + pub fn parse(console: &str) -> Result { + let mut parser = OptionParser::new(); + parser + .add_valueless("off") + .add_valueless("pty") + .add_valueless("tty") + .add_valueless("null") + .add("file") + .add("sigwinch") + .add("iommu"); + parser.parse(console).map_err(Error::ParseConsole)?; + + let mut file: Option = default_consoleconfig_file(); + let mut mode: ConsoleOutputMode = ConsoleOutputMode::Off; + + if parser.is_set("off") { + } else if parser.is_set("pty") { + mode = ConsoleOutputMode::Pty + } else if parser.is_set("tty") { + mode = ConsoleOutputMode::Tty + } else if parser.is_set("null") { + mode = ConsoleOutputMode::Null + } else if parser.is_set("file") { + mode = ConsoleOutputMode::File; + file = + Some(PathBuf::from(parser.get("file").ok_or( + Error::Validation(ValidationError::ConsoleFileMissing), + )?)); + } else { + return Err(Error::ParseConsoleInvalidModeGiven); + } + let iommu = parser + .convert::("iommu") + .map_err(Error::ParseConsole)? + .unwrap_or(Toggle(false)) + .0; + let sigwinch = parser + .convert::("sigwinch") + .map_err(Error::ParseConsole)? + .unwrap_or(Toggle(false)) + .0; + + Ok(Self { + file, + mode, + iommu, + sigwinch, + }) + } +} + +impl DeviceConfig { + pub const SYNTAX: &'static str = + "Direct device assignment parameters \"path=,iommu=on|off,id=,pci_segment=\""; + pub fn parse(device: &str) -> Result { + let mut parser = OptionParser::new(); + parser.add("path").add("id").add("iommu").add("pci_segment"); + parser.parse(device).map_err(Error::ParseDevice)?; + + let path = parser + .get("path") + .map(PathBuf::from) + .ok_or(Error::ParseDevicePathMissing)?; + let iommu = parser + .convert::("iommu") + .map_err(Error::ParseDevice)? + .unwrap_or(Toggle(false)) + .0; + let id = parser.get("id"); + let pci_segment = parser + .convert::("pci_segment") + .map_err(Error::ParseDevice)? + .unwrap_or_default(); + + Ok(DeviceConfig { + path, + iommu, + id, + pci_segment, + }) + } + + pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> { + if let Some(platform_config) = vm_config.platform.as_ref() { + if self.pci_segment >= platform_config.num_pci_segments { + return Err(ValidationError::InvalidPciSegment(self.pci_segment)); + } + + if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() { + if iommu_segments.contains(&self.pci_segment) && !self.iommu { + return Err(ValidationError::OnIommuSegment(self.pci_segment)); + } + } + } + + Ok(()) + } +} + +impl UserDeviceConfig { + pub const SYNTAX: &'static str = + "Userspace device socket=,id=,pci_segment=\""; + pub fn parse(user_device: &str) -> Result { + let mut parser = OptionParser::new(); + parser.add("socket").add("id").add("pci_segment"); + parser.parse(user_device).map_err(Error::ParseUserDevice)?; + + let socket = parser + .get("socket") + .map(PathBuf::from) + .ok_or(Error::ParseUserDeviceSocketMissing)?; + let id = parser.get("id"); + let pci_segment = parser + .convert::("pci_segment") + .map_err(Error::ParseUserDevice)? + .unwrap_or_default(); + + Ok(UserDeviceConfig { + socket, + id, + pci_segment, + }) + } + + pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> { + if let Some(platform_config) = vm_config.platform.as_ref() { + if self.pci_segment >= platform_config.num_pci_segments { + return Err(ValidationError::InvalidPciSegment(self.pci_segment)); + } + + if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() { + if iommu_segments.contains(&self.pci_segment) { + return Err(ValidationError::IommuNotSupportedOnSegment( + self.pci_segment, + )); + } + } + } + + Ok(()) + } +} + +impl VdpaConfig { + pub const SYNTAX: &'static str = "vDPA device \ + \"path=,num_queues=,iommu=on|off,\ + id=,pci_segment=\""; + pub fn parse(vdpa: &str) -> Result { + let mut parser = OptionParser::new(); + parser + .add("path") + .add("num_queues") + .add("iommu") + .add("id") + .add("pci_segment"); + parser.parse(vdpa).map_err(Error::ParseVdpa)?; + + let path = parser + .get("path") + .map(PathBuf::from) + .ok_or(Error::ParseVdpaPathMissing)?; + let num_queues = parser + .convert("num_queues") + .map_err(Error::ParseVdpa)? + .unwrap_or_else(default_vdpaconfig_num_queues); + let iommu = parser + .convert::("iommu") + .map_err(Error::ParseVdpa)? + .unwrap_or(Toggle(false)) + .0; + let id = parser.get("id"); + let pci_segment = parser + .convert("pci_segment") + .map_err(Error::ParseVdpa)? + .unwrap_or_default(); + + Ok(VdpaConfig { + path, + num_queues, + iommu, + id, + pci_segment, + }) + } + + pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> { + if let Some(platform_config) = vm_config.platform.as_ref() { + if self.pci_segment >= platform_config.num_pci_segments { + return Err(ValidationError::InvalidPciSegment(self.pci_segment)); + } + + if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() { + if iommu_segments.contains(&self.pci_segment) && !self.iommu { + return Err(ValidationError::OnIommuSegment(self.pci_segment)); + } + } + } + + Ok(()) + } +} + +impl VsockConfig { + pub const SYNTAX: &'static str = "Virtio VSOCK parameters \ + \"cid=,socket=,iommu=on|off,id=,pci_segment=,muxer_epoll_nested=on|off\""; + pub fn parse(vsock: &str) -> Result { + let mut parser = OptionParser::new(); + parser + .add("socket") + .add("cid") + .add("iommu") + .add("id") + .add("pci_segment") + .add("muxer_epoll_nested"); + parser.parse(vsock).map_err(Error::ParseVsock)?; + + let socket = parser + .get("socket") + .map(PathBuf::from) + .ok_or(Error::ParseVsockSockMissing)?; + let iommu = parser + .convert::("iommu") + .map_err(Error::ParseVsock)? + .unwrap_or(Toggle(false)) + .0; + let cid = parser + .convert("cid") + .map_err(Error::ParseVsock)? + .ok_or(Error::ParseVsockCidMissing)?; + let id = parser.get("id"); + let pci_segment = parser + .convert("pci_segment") + .map_err(Error::ParseVsock)? + .unwrap_or_default(); + let muxer_epoll_nested = parser + .convert::("muxer_epoll_nested") + .map_err(Error::ParseVsock)? + .unwrap_or(Toggle(false)) + .0; + + Ok(VsockConfig { + cid, + socket, + iommu, + id, + pci_segment, + muxer_epoll_nested, + }) + } + + pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> { + if let Some(platform_config) = vm_config.platform.as_ref() { + if self.pci_segment >= platform_config.num_pci_segments { + return Err(ValidationError::InvalidPciSegment(self.pci_segment)); + } + + if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() { + if iommu_segments.contains(&self.pci_segment) && !self.iommu { + return Err(ValidationError::OnIommuSegment(self.pci_segment)); + } + } + } + + Ok(()) + } +} + +#[cfg(target_arch = "x86_64")] +impl SgxEpcConfig { + pub const SYNTAX: &'static str = "SGX EPC parameters \ + \"id=,size=,prefault=on|off\""; + pub fn parse(sgx_epc: &str) -> Result { + let mut parser = OptionParser::new(); + parser.add("id").add("size").add("prefault"); + parser.parse(sgx_epc).map_err(Error::ParseSgxEpc)?; + + let id = parser.get("id").ok_or(Error::ParseSgxEpcIdMissing)?; + let size = parser + .convert::("size") + .map_err(Error::ParseSgxEpc)? + .unwrap_or(ByteSized(0)) + .0; + let prefault = parser + .convert::("prefault") + .map_err(Error::ParseSgxEpc)? + .unwrap_or(Toggle(false)) + .0; + + Ok(SgxEpcConfig { id, size, prefault }) + } +} + +impl NumaConfig { + pub const SYNTAX: &'static str = "Settings related to a given NUMA node \ + \"guest_numa_id=,cpus=,distances=,\ + memory_zones=,sgx_epc_sections=\""; + pub fn parse(numa: &str) -> Result { + let mut parser = OptionParser::new(); + parser + .add("guest_numa_id") + .add("cpus") + .add("distances") + .add("memory_zones") + .add("sgx_epc_sections"); + parser.parse(numa).map_err(Error::ParseNuma)?; + + let guest_numa_id = parser + .convert::("guest_numa_id") + .map_err(Error::ParseNuma)? + .unwrap_or(0); + let cpus = parser + .convert::("cpus") + .map_err(Error::ParseNuma)? + .map(|v| v.0.iter().map(|e| *e as u8).collect()); + let distances = parser + .convert::>("distances") + .map_err(Error::ParseNuma)? + .map(|v| { + v.0.iter() + .map(|(e1, e2)| NumaDistance { + destination: *e1 as u32, + distance: *e2 as u8, + }) + .collect() + }); + let memory_zones = parser + .convert::("memory_zones") + .map_err(Error::ParseNuma)? + .map(|v| v.0); + #[cfg(target_arch = "x86_64")] + let sgx_epc_sections = parser + .convert::("sgx_epc_sections") + .map_err(Error::ParseNuma)? + .map(|v| v.0); + + Ok(NumaConfig { + guest_numa_id, + cpus, + distances, + memory_zones, + #[cfg(target_arch = "x86_64")] + sgx_epc_sections, + }) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct RestoreDevice { + pub id: String, + pub path: String, +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +pub struct RestoreConfig { + pub source_url: PathBuf, + #[serde(default)] + pub prefault: bool, + pub disks: Option>, + pub net: Option>, + pub fs: Option>, + pub vsock: Option, + pub pmem: Option>, + #[serde(default)] + pub dirty_log: bool, + /// Optional existing memory blob path for reading memory range data from a + /// separate volume. + /// Accepts either an absolute path like `/dev/vdb` or a `file:///dev/vdb` + /// URL. When set, memory snapshots are read from this path instead of + /// source_url/. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub memory_vol_url: Option, + /// Optional ivshmem shared memory device configuration. + /// When present, the VM will have an ivshmem device for host-guest communication. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ivshmem: Option, +} + +impl RestoreConfig { + pub const SYNTAX: &'static str = "Restore from a VM snapshot. \ + \nRestore parameters \"source_url=,prefault=on|off,memory_vol_url=\" \ + \n`source_url` should be a valid URL (e.g file:///foo/bar or tcp://192.168.1.10/foo) \ + \n`prefault` brings memory pages in when enabled (disabled by default) \ + \n`memory_vol_url` optional existing memory blob path (for example /dev/vdb or file:///dev/vdb) \ + \nfor reading memory range data from a separate volume"; + pub fn parse(restore: &str) -> Result { + let mut parser = OptionParser::new(); + parser + .add("source_url") + .add("prefault") + .add("memory_vol_url"); + parser.parse(restore).map_err(Error::ParseRestore)?; + + let source_url = parser + .get("source_url") + .map(PathBuf::from) + .ok_or(Error::ParseRestoreSourceUrlMissing)?; + let prefault = parser + .convert::("prefault") + .map_err(Error::ParseRestore)? + .unwrap_or(Toggle(false)) + .0; + let dirty_log = parser + .convert::("dirty_log") + .map_err(Error::ParseRestore)? + .unwrap_or(Toggle(false)) + .0; + + let memory_vol_url = parser.get("memory_vol_url"); + + Ok(RestoreConfig { + source_url, + prefault, + disks: None, + net: None, + fs: None, + vsock: None, + pmem: None, + dirty_log, + memory_vol_url, + ivshmem: None, + }) + } +} + +impl TpmConfig { + pub const SYNTAX: &'static str = "TPM device \ + \"(UNIX Domain Socket from swtpm) socket=\""; + pub fn parse(tpm: &str) -> Result { + let mut parser = OptionParser::new(); + parser.add("socket"); + parser.parse(tpm).map_err(Error::ParseTpm)?; + let socket = parser + .get("socket") + .map(PathBuf::from) + .ok_or(Error::ParseTpmPathMissing)?; + Ok(TpmConfig { socket }) + } +} + +impl IvshmemConfig { + pub const SYNTAX: &'static str = "Ivshmem device \ + \"(backend file) path=,size=\""; + pub fn parse(ivshmem: &str) -> Result { + let mut parser = OptionParser::new(); + parser.add("path").add("size"); + parser.parse(ivshmem).map_err(Error::ParseIvshmem)?; + let path = parser + .get("path") + .map(PathBuf::from) + .ok_or(Error::ParseIvshmemPathMissing)?; + + let size = parser + .convert::("size") + .map_err(Error::ParseIvshmem)? + .unwrap_or(ByteSized((DEFAULT_IVSHMEM_SIZE << 20) as u64)) + .0; + + // size must = 2^n + if size == 0 || (size & (size - 1)) != 0 { + return Err(Error::InvalidIvshmemSize(size)); + } + let metadata = fs::metadata(path.to_str().unwrap()).map_err(Error::InvalidIvshmemPath)?; + if metadata.len() < size { + return Err(Error::InvalidIvshmemSize(size)); + } + + Ok(IvshmemConfig { + path, + size: size as usize, + }) + } +} + +enum DeviceValue { + Str(String), + Arr(Vec), + Value(u64), + Bool(bool), +} + +impl VmConfig { + fn validate_identifier( + id_list: &mut BTreeSet, + id: &Option, + ) -> ValidationResult<()> { + if let Some(id) = id.as_ref() { + if id.starts_with("__") { + return Err(ValidationError::InvalidIdentifier(id.clone())); + } + + if !id_list.insert(id.clone()) { + return Err(ValidationError::IdentifierNotUnique(id.clone())); + } + } + + Ok(()) + } + + pub fn backed_by_shared_memory(&self) -> bool { + if self.memory.shared || self.memory.hugepages { + return true; + } + + if self.memory.size == 0 { + for zone in self.memory.zones.as_ref().unwrap() { + if !zone.shared && !zone.hugepages { + return false; + } + } + true + } else { + false + } + } + + pub fn update_disks(&mut self, disk_cfgs: &Vec) { + if let Some(disks) = &mut self.disks { + let mut devices: HashMap = HashMap::default(); + let mut rate_limiter: HashMap = HashMap::default(); + for disk_cfg in disk_cfgs.iter() { + if disk_cfg.id.is_none() { + continue; + } + if disk_cfg.path.is_some() { + devices.insert( + disk_cfg.id.as_ref().unwrap().clone(), + disk_cfg.path.as_ref().unwrap().clone(), + ); + } + if disk_cfg.rate_limiter_config.is_some() { + rate_limiter.insert( + disk_cfg.id.as_ref().unwrap().clone(), + disk_cfg.rate_limiter_config.as_ref().unwrap().clone(), + ); + } + } + + for disk in disks.iter_mut() { + if let Some(id) = &disk.id { + if let Some(path) = devices.get(&id.clone()) { + disk.path = Some(PathBuf::from(path)); + } + if let Some(rate_limit) = rate_limiter.get(&id.clone()) { + disk.rate_limiter_config = Some(*rate_limit); + } + } + } + } + } + + pub fn update_nets(&mut self, net_cfgs: &Vec) { + if let Some(nets) = &mut self.net { + let mut devices: HashMap = HashMap::default(); + let mut rate_limiter: HashMap = HashMap::default(); + for net_cfg in net_cfgs.iter() { + if net_cfg.id.is_none() { + continue; + } + if net_cfg.tap.is_some() { + devices.insert( + net_cfg.id.as_ref().unwrap().clone(), + net_cfg.tap.as_ref().unwrap().clone(), + ); + } + if net_cfg.rate_limiter_config.is_some() { + rate_limiter.insert( + net_cfg.id.as_ref().unwrap().clone(), + net_cfg.rate_limiter_config.as_ref().unwrap().clone(), + ); + } + } + + for net in nets.iter_mut() { + if let Some(id) = &net.id { + if let Some(tap) = devices.get(&id.clone()) { + net.tap = Some(tap.clone()); + } + if let Some(rate_limit) = rate_limiter.get(&id.clone()) { + net.rate_limiter_config = Some(*rate_limit); + } + } + } + } + } + + pub fn update_fses(&mut self, fs_cfgs: &Vec) { + if let Some(fses) = &mut self.fs { + let mut devices = HashMap::new(); + let mut rate_limiter: HashMap = HashMap::default(); + + for fs_cfg in fs_cfgs.iter() { + if fs_cfg.id.is_none() { + continue; + } + if let Some(backend) = &fs_cfg.backendfs_config { + let mut device = HashMap::new(); + + device.insert( + UPDATE_FS_SHARED_DIR_FIELD, + DeviceValue::Str(backend.shared_dir.clone()), + ); + if backend.allowed_dirs.is_some() { + device.insert( + UPDATE_FS_ALLOWED_DIRS_FIELD, + DeviceValue::Arr(backend.allowed_dirs.clone().unwrap().clone()), + ); + } + device.insert( + UPDATE_FS_CACHE_FIELD, + DeviceValue::Value(backend.cache as u64), + ); + device.insert( + UPDATE_FS_REMAP_FILTER_FIELD, + DeviceValue::Bool(backend.remap_filter), + ); + devices.insert(fs_cfg.id.as_ref().unwrap().clone(), device); + } + if fs_cfg.rate_limiter_config.is_some() { + rate_limiter.insert( + fs_cfg.id.as_ref().unwrap().clone(), + fs_cfg.rate_limiter_config.as_ref().unwrap().clone(), + ); + } + } + + for fs in fses.iter_mut() { + if let Some(id) = &fs.id { + if let Some(backend) = &mut fs.backendfs_config { + if let Some(device) = devices.get(id) { + if let Some(DeviceValue::Str(shared_dir)) = + device.get(UPDATE_FS_SHARED_DIR_FIELD) + { + backend.shared_dir = shared_dir.clone(); + } + if let Some(DeviceValue::Arr(allowed_dirs)) = + device.get(UPDATE_FS_ALLOWED_DIRS_FIELD) + { + backend.allowed_dirs = Some(allowed_dirs.clone()); + } + if let Some(DeviceValue::Value(cache)) = + device.get(UPDATE_FS_CACHE_FIELD) + { + backend.cache = *cache as u8; + } + if let Some(DeviceValue::Bool(remap_filter)) = + device.get(UPDATE_FS_REMAP_FILTER_FIELD) + { + backend.remap_filter = *remap_filter; + } + } + } + if let Some(rate_limit) = rate_limiter.get(id) { + fs.rate_limiter_config = Some(*rate_limit); + } + } + } + } + } + + pub fn update_vsock(&mut self, vsock_cfg: &VsockConfig) { + if let Some(vsock) = &mut self.vsock { + if vsock.id.is_none() { + return; + } + if vsock_cfg.id.is_none() { + return; + } + vsock.socket = vsock_cfg.socket.clone(); + } + } + + pub fn update_ivshmem(&mut self, ivshmem_cfg: &IvshmemConfig) { + if let Some(ivshmem) = &mut self.ivshmem { + ivshmem.path = ivshmem_cfg.path.clone(); + ivshmem.size = ivshmem_cfg.size; + } + } + + pub fn update_pmem(&mut self, pmem_cfgs: &[PmemConfig]) { + if let Some(pmems) = &mut self.pmem { + let mut new_cfgs: HashMap = HashMap::default(); + + for cfg in pmem_cfgs.iter() { + if let Some(id) = &cfg.id { + new_cfgs.insert(id.to_string(), cfg.file.clone()); + } + } + + for pmem in pmems.iter_mut() { + if let Some(id) = &pmem.id { + if let Some(file) = new_cfgs.get(&id.clone()) { + pmem.file = file.to_path_buf(); + } + } + } + } + } + + // Also enables virtio-iommu if the config needs it + // Returns the list of unique identifiers provided through the + // configuration. + pub fn validate(&mut self) -> ValidationResult> { + let mut id_list = BTreeSet::new(); + + self.payload + .as_ref() + .ok_or(ValidationError::KernelMissing)?; + + #[cfg(feature = "tdx")] + { + let tdx_enabled = self.platform.as_ref().map(|p| p.tdx).unwrap_or(false); + // At this point we know payload isn't None. + if tdx_enabled && self.payload.as_ref().unwrap().firmware.is_none() { + return Err(ValidationError::TdxFirmwareMissing); + } + if tdx_enabled && (self.cpus.max_vcpus != self.cpus.boot_vcpus) { + return Err(ValidationError::TdxNoCpuHotplug); + } + } + + // The 'conflict' check is introduced in commit 24438e0390d3 + // (vm-virtio: Enable the vmm support for virtio-console). + // + // Allow simultaneously set serial and console as TTY mode, for + // someone want to use virtio console for better performance, and + // want to keep legacy serial to catch boot stage logs for debug. + // Using such double tty mode, you need to configure the kernel + // properly, such as: + // "console=hvc0 earlyprintk=ttyS0" + if self.console.mode == ConsoleOutputMode::Tty && self.serial.mode == ConsoleOutputMode::Tty + { + warn!("Using TTY output for both virtio-console and serial port"); + } + + if self.console.mode == ConsoleOutputMode::File && self.console.file.is_none() { + return Err(ValidationError::ConsoleFileMissing); + } + + if self.serial.mode == ConsoleOutputMode::File && self.serial.file.is_none() { + return Err(ValidationError::ConsoleFileMissing); + } + + if self.cpus.max_vcpus < self.cpus.boot_vcpus { + return Err(ValidationError::CpusMaxLowerThanBoot); + } + + if let Some(disks) = &self.disks { + for disk in disks { + if disk.vhost_socket.as_ref().and(disk.path.as_ref()).is_some() { + return Err(ValidationError::DiskSocketAndPath); + } + if disk.vhost_user && !self.backed_by_shared_memory() { + return Err(ValidationError::VhostUserRequiresSharedMemory); + } + if disk.vhost_user && disk.vhost_socket.is_none() { + return Err(ValidationError::VhostUserMissingSocket); + } + disk.validate(self)?; + self.iommu |= disk.iommu; + + Self::validate_identifier(&mut id_list, &disk.id)?; + } + } + + if let Some(nets) = &self.net { + for net in nets { + if net.vhost_user && !self.backed_by_shared_memory() { + return Err(ValidationError::VhostUserRequiresSharedMemory); + } + net.validate(self)?; + self.iommu |= net.iommu; + + Self::validate_identifier(&mut id_list, &net.id)?; + } + } + + if let Some(fses) = &self.fs { + for fs in fses { + fs.validate(self)?; + + Self::validate_identifier(&mut id_list, &fs.id)?; + } + } + + if let Some(pmems) = &self.pmem { + for pmem in pmems { + pmem.validate(self)?; + self.iommu |= pmem.iommu; + + Self::validate_identifier(&mut id_list, &pmem.id)?; + } + } + + self.iommu |= self.rng.iommu; + self.iommu |= self.console.iommu; + + if let Some(t) = &self.cpus.topology { + if t.threads_per_core == 0 + || t.cores_per_die == 0 + || t.dies_per_package == 0 + || t.packages == 0 + { + return Err(ValidationError::CpuTopologyZeroPart); + } + + // The setting of dies doesen't apply on AArch64. + // Only '1' value is accepted, so its impact on the vcpu topology + // setting can be ignored. + #[cfg(target_arch = "aarch64")] + if t.dies_per_package != 1 { + return Err(ValidationError::CpuTopologyDiesPerPackage); + } + + let total = t.threads_per_core * t.cores_per_die * t.dies_per_package * t.packages; + if total != self.cpus.max_vcpus { + return Err(ValidationError::CpuTopologyCount); + } + } + + if let Some(hugepage_size) = &self.memory.hugepage_size { + if !self.memory.hugepages { + return Err(ValidationError::HugePageSizeWithoutHugePages); + } + if !hugepage_size.is_power_of_two() { + return Err(ValidationError::InvalidHugePageSize(*hugepage_size)); + } + } + + if let Some(user_devices) = &self.user_devices { + if !user_devices.is_empty() && !self.backed_by_shared_memory() { + return Err(ValidationError::UserDevicesRequireSharedMemory); + } + + for user_device in user_devices { + user_device.validate(self)?; + + Self::validate_identifier(&mut id_list, &user_device.id)?; + } + } + + if let Some(vdpa_devices) = &self.vdpa { + for vdpa_device in vdpa_devices { + vdpa_device.validate(self)?; + self.iommu |= vdpa_device.iommu; + + Self::validate_identifier(&mut id_list, &vdpa_device.id)?; + } + } + + if let Some(balloon) = &self.balloon { + let mut ram_size = self.memory.size; + + if let Some(hotplugged_size) = &self.memory.hotplugged_size { + ram_size += hotplugged_size; + } + + if let Some(zones) = &self.memory.zones { + for zone in zones { + ram_size += zone.size; + } + } + + if balloon.size >= ram_size { + return Err(ValidationError::BalloonLargerThanRam( + balloon.size, + ram_size, + )); + } + } + + if let Some(devices) = &self.devices { + let mut device_paths = BTreeSet::new(); + for device in devices { + if !device_paths.insert(device.path.to_string_lossy()) { + return Err(ValidationError::DuplicateDevicePath( + device.path.to_string_lossy().to_string(), + )); + } + + device.validate(self)?; + self.iommu |= device.iommu; + + Self::validate_identifier(&mut id_list, &device.id)?; + } + } + + if let Some(vsock) = &self.vsock { + vsock.validate(self)?; + self.iommu |= vsock.iommu; + + Self::validate_identifier(&mut id_list, &vsock.id)?; + } + + if let Some(numa) = &self.numa { + let mut used_numa_node_memory_zones = HashMap::new(); + for numa_node in numa.iter() { + for memory_zone in numa_node.memory_zones.clone().unwrap().iter() { + if !used_numa_node_memory_zones.contains_key(memory_zone) { + used_numa_node_memory_zones + .insert(memory_zone.to_string(), numa_node.guest_numa_id); + } else { + return Err(ValidationError::MemoryZoneReused( + memory_zone.to_string(), + *used_numa_node_memory_zones.get(memory_zone).unwrap(), + numa_node.guest_numa_id, + )); + } + } + } + } + + if let Some(zones) = &self.memory.zones { + for zone in zones.iter() { + let id = zone.id.clone(); + Self::validate_identifier(&mut id_list, &Some(id))?; + } + } + + #[cfg(target_arch = "x86_64")] + if let Some(sgx_epcs) = &self.sgx_epc { + for sgx_epc in sgx_epcs.iter() { + let id = sgx_epc.id.clone(); + Self::validate_identifier(&mut id_list, &Some(id))?; + } + } + + self.platform.as_ref().map(|p| p.validate()).transpose()?; + self.iommu |= self + .platform + .as_ref() + .map(|p| p.iommu_segments.is_some()) + .unwrap_or_default(); + + Ok(id_list) + } + + pub fn parse(vm_params: VmParams) -> Result { + let mut disks: Option> = None; + if let Some(disk_list) = &vm_params.disks { + let mut disk_config_list = Vec::new(); + for item in disk_list.iter() { + let disk_config = DiskConfig::parse(item)?; + disk_config_list.push(disk_config); + } + disks = Some(disk_config_list); + } + + let mut net: Option> = None; + if let Some(net_list) = &vm_params.net { + let mut net_config_list = Vec::new(); + for item in net_list.iter() { + let net_config = NetConfig::parse(item)?; + net_config_list.push(net_config); + } + net = Some(net_config_list); + } + + let rng = RngConfig::parse(vm_params.rng)?; + + let mut balloon: Option = None; + if let Some(balloon_params) = &vm_params.balloon { + balloon = Some(BalloonConfig::parse(balloon_params)?); + } + + let mut fs: Option> = None; + if let Some(fs_list) = &vm_params.fs { + let mut fs_config_list = Vec::new(); + for item in fs_list.iter() { + fs_config_list.push(FsConfig::parse(item)?); + } + fs = Some(fs_config_list); + } + + let mut pmem: Option> = None; + if let Some(pmem_list) = &vm_params.pmem { + let mut pmem_config_list = Vec::new(); + for item in pmem_list.iter() { + let pmem_config = PmemConfig::parse(item)?; + pmem_config_list.push(pmem_config); + } + pmem = Some(pmem_config_list); + } + + let console = ConsoleConfig::parse(vm_params.console)?; + let serial = ConsoleConfig::parse(vm_params.serial)?; + + let mut devices: Option> = None; + if let Some(device_list) = &vm_params.devices { + let mut device_config_list = Vec::new(); + for item in device_list.iter() { + let device_config = DeviceConfig::parse(item)?; + device_config_list.push(device_config); + } + devices = Some(device_config_list); + } + + let mut user_devices: Option> = None; + if let Some(user_device_list) = &vm_params.user_devices { + let mut user_device_config_list = Vec::new(); + for item in user_device_list.iter() { + let user_device_config = UserDeviceConfig::parse(item)?; + user_device_config_list.push(user_device_config); + } + user_devices = Some(user_device_config_list); + } + + let mut vdpa: Option> = None; + if let Some(vdpa_list) = &vm_params.vdpa { + let mut vdpa_config_list = Vec::new(); + for item in vdpa_list.iter() { + let vdpa_config = VdpaConfig::parse(item)?; + vdpa_config_list.push(vdpa_config); + } + vdpa = Some(vdpa_config_list); + } + + let mut vsock: Option = None; + if let Some(vs) = &vm_params.vsock { + let vsock_config = VsockConfig::parse(vs)?; + vsock = Some(vsock_config); + } + + let platform = vm_params.platform.map(PlatformConfig::parse).transpose()?; + + #[cfg(target_arch = "x86_64")] + let mut sgx_epc: Option> = None; + #[cfg(target_arch = "x86_64")] + { + if let Some(sgx_epc_list) = &vm_params.sgx_epc { + let mut sgx_epc_config_list = Vec::new(); + for item in sgx_epc_list.iter() { + let sgx_epc_config = SgxEpcConfig::parse(item)?; + sgx_epc_config_list.push(sgx_epc_config); + } + sgx_epc = Some(sgx_epc_config_list); + } + } + + let mut numa: Option> = None; + if let Some(numa_list) = &vm_params.numa { + let mut numa_config_list = Vec::new(); + for item in numa_list.iter() { + let numa_config = NumaConfig::parse(item)?; + numa_config_list.push(numa_config); + } + numa = Some(numa_config_list); + } + + let payload = if vm_params.kernel.is_some() || vm_params.firmware.is_some() { + Some(PayloadConfig { + kernel: vm_params.kernel.map(PathBuf::from), + initramfs: vm_params.initramfs.map(PathBuf::from), + cmdline: vm_params.cmdline.map(|s| s.to_string()), + firmware: vm_params.firmware.map(PathBuf::from), + }) + } else { + None + }; + + let mut tpm: Option = None; + if let Some(tc) = vm_params.tpm { + let tpm_conf = TpmConfig::parse(tc)?; + tpm = Some(TpmConfig { + socket: tpm_conf.socket, + }); + } + + let mut ivshmem: Option = None; + if let Some(iv) = vm_params.ivshmem { + let ivshmem_conf = IvshmemConfig::parse(iv)?; + ivshmem = Some(ivshmem_conf); + } + + #[cfg(feature = "guest_debug")] + let gdb = vm_params.gdb; + + let mut config = VmConfig { + cpus: CpusConfig::parse(vm_params.cpus)?, + memory: MemoryConfig::parse(vm_params.memory, vm_params.memory_zones)?, + payload, + disks, + net, + rng, + balloon, + fs, + pmem, + serial, + console, + devices, + user_devices, + vdpa, + vsock, + pvpanic: vm_params.pvpanic, + iommu: false, // updated in VmConfig::validate() + #[cfg(target_arch = "x86_64")] + sgx_epc, + numa, + watchdog: vm_params.watchdog, + #[cfg(feature = "guest_debug")] + gdb, + platform, + tpm, + sys_ctrl: vm_params.sys_ctrl, + ivshmem, + }; + config.validate().map_err(Error::Validation)?; + Ok(config) + } + + #[cfg(feature = "tdx")] + pub fn is_tdx_enabled(&self) -> bool { + self.platform.as_ref().map(|p| p.tdx).unwrap_or(false) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use net_util::MacAddr; + + #[test] + fn update_fses_preserves_restore_remap_filter() { + let mut config = VmConfig { + fs: Some(vec![FsConfig { + id: Some("virtio_rw".to_string()), + backendfs_config: Some(BackendFsConfig::default()), + ..Default::default() + }]), + ..Default::default() + }; + let restore_fses = vec![FsConfig { + id: Some("virtio_rw".to_string()), + backendfs_config: Some(BackendFsConfig { + remap_filter: true, + ..Default::default() + }), + ..Default::default() + }]; + + config.update_fses(&restore_fses); + + assert!( + config.fs.unwrap()[0] + .backendfs_config + .as_ref() + .unwrap() + .remap_filter + ); + } + + #[test] + fn test_cpu_parsing() -> Result<()> { + assert_eq!(CpusConfig::parse("")?, CpusConfig::default()); + + assert_eq!( + CpusConfig::parse("boot=1")?, + CpusConfig { + boot_vcpus: 1, + max_vcpus: 1, + ..Default::default() + } + ); + assert_eq!( + CpusConfig::parse("boot=1,max=2")?, + CpusConfig { + boot_vcpus: 1, + max_vcpus: 2, + ..Default::default() + } + ); + assert_eq!( + CpusConfig::parse("boot=8,topology=2:2:1:2")?, + CpusConfig { + boot_vcpus: 8, + max_vcpus: 8, + topology: Some(CpuTopology { + threads_per_core: 2, + cores_per_die: 2, + dies_per_package: 1, + packages: 2 + }), + ..Default::default() + } + ); + + assert!(CpusConfig::parse("boot=8,topology=2:2:1").is_err()); + assert!(CpusConfig::parse("boot=8,topology=2:2:1:x").is_err()); + assert_eq!( + CpusConfig::parse("boot=1,kvm_hyperv=on")?, + CpusConfig { + boot_vcpus: 1, + max_vcpus: 1, + kvm_hyperv: true, + ..Default::default() + } + ); + assert_eq!( + CpusConfig::parse("boot=2,affinity=[0@[0,2],1@[1,3]]")?, + CpusConfig { + boot_vcpus: 2, + max_vcpus: 2, + affinity: Some(vec![ + CpuAffinity { + vcpu: 0, + host_cpus: vec![0, 2], + }, + CpuAffinity { + vcpu: 1, + host_cpus: vec![1, 3], + } + ]), + ..Default::default() + }, + ); + + Ok(()) + } + + #[test] + fn test_mem_parsing() -> Result<()> { + assert_eq!(MemoryConfig::parse("", None)?, MemoryConfig::default()); + // Default string + assert_eq!( + MemoryConfig::parse("size=512M", None)?, + MemoryConfig::default() + ); + assert_eq!( + MemoryConfig::parse("size=512M,mergeable=on", None)?, + MemoryConfig { + size: 512 << 20, + mergeable: true, + ..Default::default() + } + ); + assert_eq!( + MemoryConfig::parse("mergeable=on", None)?, + MemoryConfig { + mergeable: true, + ..Default::default() + } + ); + assert_eq!( + MemoryConfig::parse("size=1G,mergeable=off", None)?, + MemoryConfig { + size: 1 << 30, + mergeable: false, + ..Default::default() + } + ); + assert_eq!( + MemoryConfig::parse("hotplug_method=acpi", None)?, + MemoryConfig { + ..Default::default() + } + ); + assert_eq!( + MemoryConfig::parse("hotplug_method=acpi,hotplug_size=512M", None)?, + MemoryConfig { + hotplug_size: Some(512 << 20), + ..Default::default() + } + ); + assert_eq!( + MemoryConfig::parse("hotplug_method=virtio-mem,hotplug_size=512M", None)?, + MemoryConfig { + hotplug_size: Some(512 << 20), + hotplug_method: HotplugMethod::VirtioMem, + ..Default::default() + } + ); + assert_eq!( + MemoryConfig::parse("hugepages=on,size=1G,hugepage_size=2M", None)?, + MemoryConfig { + hugepage_size: Some(2 << 20), + size: 1 << 30, + hugepages: true, + ..Default::default() + } + ); + Ok(()) + } + + #[test] + fn test_disk_parsing() -> Result<()> { + assert_eq!( + DiskConfig::parse("path=/path/to_file")?, + DiskConfig { + path: Some(PathBuf::from("/path/to_file")), + ..Default::default() + } + ); + assert_eq!( + DiskConfig::parse("path=/path/to_file,id=mydisk0")?, + DiskConfig { + path: Some(PathBuf::from("/path/to_file")), + id: Some("mydisk0".to_owned()), + ..Default::default() + } + ); + assert_eq!( + DiskConfig::parse("vhost_user=true,socket=/tmp/sock")?, + DiskConfig { + vhost_socket: Some(String::from("/tmp/sock")), + vhost_user: true, + ..Default::default() + } + ); + assert_eq!( + DiskConfig::parse("path=/path/to_file,iommu=on")?, + DiskConfig { + path: Some(PathBuf::from("/path/to_file")), + iommu: true, + ..Default::default() + } + ); + assert_eq!( + DiskConfig::parse("path=/path/to_file,iommu=on,queue_size=256")?, + DiskConfig { + path: Some(PathBuf::from("/path/to_file")), + iommu: true, + queue_size: 256, + ..Default::default() + } + ); + assert_eq!( + DiskConfig::parse("path=/path/to_file,iommu=on,queue_size=256,num_queues=4")?, + DiskConfig { + path: Some(PathBuf::from("/path/to_file")), + iommu: true, + queue_size: 256, + num_queues: 4, + ..Default::default() + } + ); + assert_eq!( + DiskConfig::parse("path=/path/to_file,direct=on")?, + DiskConfig { + path: Some(PathBuf::from("/path/to_file")), + direct: true, + ..Default::default() + } + ); + assert_eq!( + DiskConfig::parse("path=/path/to_file")?, + DiskConfig { + path: Some(PathBuf::from("/path/to_file")), + ..Default::default() + } + ); + assert_eq!( + DiskConfig::parse("path=/path/to_file")?, + DiskConfig { + path: Some(PathBuf::from("/path/to_file")), + ..Default::default() + } + ); + + Ok(()) + } + + #[test] + fn test_net_parsing() -> Result<()> { + // mac address is random + assert_eq!( + NetConfig::parse("mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef")?, + NetConfig { + mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(), + host_mac: Some(MacAddr::parse_str("12:34:de:ad:be:ef").unwrap()), + ..Default::default() + } + ); + + assert_eq!( + NetConfig::parse("mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,id=mynet0")?, + NetConfig { + mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(), + host_mac: Some(MacAddr::parse_str("12:34:de:ad:be:ef").unwrap()), + id: Some("mynet0".to_owned()), + ..Default::default() + } + ); + + assert_eq!( + NetConfig::parse( + "mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,tap=tap0,ip=192.168.100.1,mask=255.255.255.128" + )?, + NetConfig { + mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(), + host_mac: Some(MacAddr::parse_str("12:34:de:ad:be:ef").unwrap()), + tap: Some("tap0".to_owned()), + ip: "192.168.100.1".parse().unwrap(), + mask: "255.255.255.128".parse().unwrap(), + ..Default::default() + } + ); + + assert_eq!( + NetConfig::parse( + "mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,vhost_user=true,socket=/tmp/sock" + )?, + NetConfig { + mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(), + host_mac: Some(MacAddr::parse_str("12:34:de:ad:be:ef").unwrap()), + vhost_user: true, + vhost_socket: Some("/tmp/sock".to_owned()), + ..Default::default() + } + ); + + assert_eq!( + NetConfig::parse("mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,num_queues=4,queue_size=1024,iommu=on")?, + NetConfig { + mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(), + host_mac: Some(MacAddr::parse_str("12:34:de:ad:be:ef").unwrap()), + num_queues: 4, + queue_size: 1024, + iommu: true, + ..Default::default() + } + ); + + assert_eq!( + NetConfig::parse("mac=de:ad:be:ef:12:34,fd=[3,7],num_queues=4")?, + NetConfig { + mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(), + fds: Some(vec![3, 7]), + num_queues: 4, + ..Default::default() + } + ); + + Ok(()) + } + + #[test] + fn test_parse_rng() -> Result<()> { + assert_eq!(RngConfig::parse("")?, RngConfig::default()); + assert_eq!( + RngConfig::parse("src=/dev/random")?, + RngConfig { + src: PathBuf::from("/dev/random"), + ..Default::default() + } + ); + assert_eq!( + RngConfig::parse("src=/dev/random,iommu=on")?, + RngConfig { + src: PathBuf::from("/dev/random"), + iommu: true, + } + ); + assert_eq!( + RngConfig::parse("iommu=on")?, + RngConfig { + iommu: true, + ..Default::default() + } + ); + Ok(()) + } + + #[test] + fn test_parse_fs() -> Result<()> { + // "tag" and "socket" must be supplied + assert!(FsConfig::parse("").is_err()); + assert!(FsConfig::parse("tag=mytag").is_err()); + assert!(FsConfig::parse("socket=/tmp/sock").is_err()); + assert_eq!( + FsConfig::parse("tag=mytag,socket=/tmp/sock")?, + FsConfig { + socket: PathBuf::from("/tmp/sock"), + tag: "mytag".to_owned(), + ..Default::default() + } + ); + assert_eq!( + FsConfig::parse("tag=mytag,socket=/tmp/sock")?, + FsConfig { + socket: PathBuf::from("/tmp/sock"), + tag: "mytag".to_owned(), + ..Default::default() + } + ); + assert_eq!( + FsConfig::parse("tag=mytag,socket=/tmp/sock,num_queues=4,queue_size=1024")?, + FsConfig { + socket: PathBuf::from("/tmp/sock"), + tag: "mytag".to_owned(), + num_queues: 4, + queue_size: 1024, + ..Default::default() + } + ); + + Ok(()) + } + + #[test] + fn test_pmem_parsing() -> Result<()> { + // Must always give a file and size + assert!(PmemConfig::parse("").is_err()); + assert!(PmemConfig::parse("size=128M").is_err()); + assert_eq!( + PmemConfig::parse("file=/tmp/pmem,size=128M")?, + PmemConfig { + file: PathBuf::from("/tmp/pmem"), + size: Some(128 << 20), + ..Default::default() + } + ); + assert_eq!( + PmemConfig::parse("file=/tmp/pmem,size=128M,id=mypmem0")?, + PmemConfig { + file: PathBuf::from("/tmp/pmem"), + size: Some(128 << 20), + id: Some("mypmem0".to_owned()), + ..Default::default() + } + ); + assert_eq!( + PmemConfig::parse("file=/tmp/pmem,size=128M,iommu=on,discard_writes=on")?, + PmemConfig { + file: PathBuf::from("/tmp/pmem"), + size: Some(128 << 20), + discard_writes: true, + iommu: true, + ..Default::default() + } + ); + + Ok(()) + } + + #[test] + fn test_console_parsing() -> Result<()> { + assert!(ConsoleConfig::parse("").is_err()); + assert!(ConsoleConfig::parse("badmode").is_err()); + assert_eq!( + ConsoleConfig::parse("off")?, + ConsoleConfig { + mode: ConsoleOutputMode::Off, + iommu: false, + sigwinch: false, + file: None, + } + ); + assert_eq!( + ConsoleConfig::parse("pty")?, + ConsoleConfig { + mode: ConsoleOutputMode::Pty, + iommu: false, + sigwinch: false, + file: None, + } + ); + assert_eq!( + ConsoleConfig::parse("tty")?, + ConsoleConfig { + mode: ConsoleOutputMode::Tty, + iommu: false, + sigwinch: false, + file: None, + } + ); + assert_eq!( + ConsoleConfig::parse("null")?, + ConsoleConfig { + mode: ConsoleOutputMode::Null, + iommu: false, + sigwinch: false, + file: None, + } + ); + assert_eq!( + ConsoleConfig::parse("file=/tmp/console")?, + ConsoleConfig { + mode: ConsoleOutputMode::File, + iommu: false, + sigwinch: false, + file: Some(PathBuf::from("/tmp/console")) + } + ); + assert_eq!( + ConsoleConfig::parse("null,iommu=on")?, + ConsoleConfig { + mode: ConsoleOutputMode::Null, + iommu: true, + sigwinch: false, + file: None, + } + ); + assert_eq!( + ConsoleConfig::parse("file=/tmp/console,iommu=on")?, + ConsoleConfig { + mode: ConsoleOutputMode::File, + iommu: true, + sigwinch: false, + file: Some(PathBuf::from("/tmp/console")) + } + ); + Ok(()) + } + + #[test] + fn test_device_parsing() -> Result<()> { + // Device must have a path provided + assert!(DeviceConfig::parse("").is_err()); + assert_eq!( + DeviceConfig::parse("path=/path/to/device")?, + DeviceConfig { + path: PathBuf::from("/path/to/device"), + id: None, + iommu: false, + ..Default::default() + } + ); + + assert_eq!( + DeviceConfig::parse("path=/path/to/device,iommu=on")?, + DeviceConfig { + path: PathBuf::from("/path/to/device"), + id: None, + iommu: true, + ..Default::default() + } + ); + + assert_eq!( + DeviceConfig::parse("path=/path/to/device,iommu=on,id=mydevice0")?, + DeviceConfig { + path: PathBuf::from("/path/to/device"), + id: Some("mydevice0".to_owned()), + iommu: true, + ..Default::default() + } + ); + + Ok(()) + } + + #[test] + fn test_vdpa_parsing() -> Result<()> { + // path is required + assert!(VdpaConfig::parse("").is_err()); + assert_eq!( + VdpaConfig::parse("path=/dev/vhost-vdpa")?, + VdpaConfig { + path: PathBuf::from("/dev/vhost-vdpa"), + num_queues: 1, + id: None, + ..Default::default() + } + ); + assert_eq!( + VdpaConfig::parse("path=/dev/vhost-vdpa,num_queues=2,id=my_vdpa")?, + VdpaConfig { + path: PathBuf::from("/dev/vhost-vdpa"), + num_queues: 2, + id: Some("my_vdpa".to_owned()), + ..Default::default() + } + ); + Ok(()) + } + + #[test] + fn test_tpm_parsing() -> Result<()> { + // path is required + assert!(TpmConfig::parse("").is_err()); + assert_eq!( + TpmConfig::parse("socket=/var/run/tpm.sock")?, + TpmConfig { + socket: PathBuf::from("/var/run/tpm.sock"), + } + ); + Ok(()) + } + + #[test] + fn test_vsock_parsing() -> Result<()> { + // socket and cid is required + assert!(VsockConfig::parse("").is_err()); + assert_eq!( + VsockConfig::parse("socket=/tmp/sock,cid=1")?, + VsockConfig { + cid: 1, + socket: PathBuf::from("/tmp/sock"), + iommu: false, + id: None, + ..Default::default() + } + ); + assert_eq!( + VsockConfig::parse("socket=/tmp/sock,cid=1,iommu=on")?, + VsockConfig { + cid: 1, + socket: PathBuf::from("/tmp/sock"), + iommu: true, + id: None, + ..Default::default() + } + ); + Ok(()) + } + + #[test] + fn test_update_ivshmem_ignores_missing_snapshot_device() { + let mut vm_config = VmConfig { + ivshmem: None, + ..Default::default() + }; + let restore_ivshmem = IvshmemConfig { + path: PathBuf::from("/dev/shm/ivshmem-sandbox"), + size: 1024 * 1024, + }; + + vm_config.update_ivshmem(&restore_ivshmem); + + assert_eq!(vm_config.ivshmem, None); + } + + #[test] + fn test_update_ivshmem_updates_existing_backend() { + let mut vm_config = VmConfig { + ivshmem: Some(IvshmemConfig { + path: PathBuf::from("/dev/shm/ivshmem-template"), + size: 512 * 1024, + }), + ..Default::default() + }; + let restore_ivshmem = IvshmemConfig { + path: PathBuf::from("/dev/shm/ivshmem-sandbox"), + size: 1024 * 1024, + }; + + vm_config.update_ivshmem(&restore_ivshmem); + + assert_eq!(vm_config.ivshmem, Some(restore_ivshmem)); + } + + #[test] + fn test_config_validation() { + let mut valid_config = VmConfig { + cpus: CpusConfig { + boot_vcpus: 1, + max_vcpus: 1, + ..Default::default() + }, + memory: MemoryConfig { + size: 536_870_912, + mergeable: false, + hotplug_method: HotplugMethod::Acpi, + hotplug_size: None, + hotplugged_size: None, + shared: false, + hugepages: false, + hugepage_size: None, + prefault: false, + zones: None, + thp: true, + dirty_log: false, + }, + payload: Some(PayloadConfig { + kernel: Some(PathBuf::from("/path/to/kernel")), + ..Default::default() + }), + disks: None, + net: None, + rng: RngConfig { + src: PathBuf::from("/dev/urandom"), + iommu: false, + }, + balloon: None, + fs: None, + pmem: None, + serial: ConsoleConfig { + file: None, + mode: ConsoleOutputMode::Null, + sigwinch: false, + iommu: false, + }, + console: ConsoleConfig { + file: None, + mode: ConsoleOutputMode::Tty, + sigwinch: false, + iommu: false, + }, + devices: None, + user_devices: None, + vdpa: None, + vsock: None, + pvpanic: false, + iommu: false, + #[cfg(target_arch = "x86_64")] + sgx_epc: None, + numa: None, + watchdog: false, + #[cfg(feature = "guest_debug")] + gdb: false, + platform: None, + tpm: None, + sys_ctrl: false, + ivshmem: None, + }; + + assert!(valid_config.validate().is_ok()); + + let mut invalid_config = valid_config.clone(); + invalid_config.serial.mode = ConsoleOutputMode::Tty; + invalid_config.console.mode = ConsoleOutputMode::Tty; + assert!(valid_config.validate().is_ok()); + + let mut invalid_config = valid_config.clone(); + invalid_config.payload = None; + assert_eq!( + invalid_config.validate(), + Err(ValidationError::KernelMissing) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.serial.mode = ConsoleOutputMode::File; + invalid_config.serial.file = None; + assert_eq!( + invalid_config.validate(), + Err(ValidationError::ConsoleFileMissing) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.cpus.max_vcpus = 16; + invalid_config.cpus.boot_vcpus = 32; + assert_eq!( + invalid_config.validate(), + Err(ValidationError::CpusMaxLowerThanBoot) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.cpus.max_vcpus = 16; + invalid_config.cpus.boot_vcpus = 16; + invalid_config.cpus.topology = Some(CpuTopology { + threads_per_core: 2, + cores_per_die: 8, + dies_per_package: 1, + packages: 2, + }); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::CpuTopologyCount) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.disks = Some(vec![DiskConfig { + vhost_socket: Some("/path/to/sock".to_owned()), + path: Some(PathBuf::from("/path/to/image")), + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::DiskSocketAndPath) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.memory.shared = true; + invalid_config.disks = Some(vec![DiskConfig { + vhost_user: true, + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::VhostUserMissingSocket) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.disks = Some(vec![DiskConfig { + vhost_user: true, + vhost_socket: Some("/path/to/sock".to_owned()), + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::VhostUserRequiresSharedMemory) + ); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.disks = Some(vec![DiskConfig { + vhost_user: true, + vhost_socket: Some("/path/to/sock".to_owned()), + ..Default::default() + }]); + still_valid_config.memory.shared = true; + assert!(still_valid_config.validate().is_ok()); + + let mut invalid_config = valid_config.clone(); + invalid_config.net = Some(vec![NetConfig { + vhost_user: true, + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::VhostUserRequiresSharedMemory) + ); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.net = Some(vec![NetConfig { + vhost_user: true, + vhost_socket: Some("/path/to/sock".to_owned()), + ..Default::default() + }]); + still_valid_config.memory.shared = true; + assert!(still_valid_config.validate().is_ok()); + + let mut invalid_config = valid_config.clone(); + invalid_config.net = Some(vec![NetConfig { + fds: Some(vec![0]), + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::VnetReservedFd) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.fs = Some(vec![FsConfig { + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::VhostUserRequiresSharedMemory) + ); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.memory.shared = true; + assert!(still_valid_config.validate().is_ok()); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.memory.hugepages = true; + assert!(still_valid_config.validate().is_ok()); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.memory.hugepages = true; + still_valid_config.memory.hugepage_size = Some(2 << 20); + assert!(still_valid_config.validate().is_ok()); + + let mut invalid_config = valid_config.clone(); + invalid_config.memory.hugepages = false; + invalid_config.memory.hugepage_size = Some(2 << 20); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::HugePageSizeWithoutHugePages) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.memory.hugepages = true; + invalid_config.memory.hugepage_size = Some(3 << 20); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::InvalidHugePageSize(3 << 20)) + ); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + ..Default::default() + }); + assert!(still_valid_config.validate().is_ok()); + + let mut invalid_config = valid_config.clone(); + invalid_config.platform = Some(PlatformConfig { + num_pci_segments: 17, + ..Default::default() + }); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::InvalidNumPciSegments(17)) + ); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + assert!(still_valid_config.validate().is_ok()); + + let mut invalid_config = valid_config.clone(); + invalid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![17, 18]), + ..Default::default() + }); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::InvalidPciSegment(17)) + ); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + still_valid_config.disks = Some(vec![DiskConfig { + iommu: true, + pci_segment: 1, + ..Default::default() + }]); + assert!(still_valid_config.validate().is_ok()); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + still_valid_config.net = Some(vec![NetConfig { + iommu: true, + pci_segment: 1, + ..Default::default() + }]); + assert!(still_valid_config.validate().is_ok()); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + still_valid_config.pmem = Some(vec![PmemConfig { + iommu: true, + pci_segment: 1, + ..Default::default() + }]); + assert!(still_valid_config.validate().is_ok()); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + still_valid_config.devices = Some(vec![DeviceConfig { + iommu: true, + pci_segment: 1, + ..Default::default() + }]); + assert!(still_valid_config.validate().is_ok()); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + still_valid_config.vsock = Some(VsockConfig { + iommu: true, + pci_segment: 1, + ..Default::default() + }); + assert!(still_valid_config.validate().is_ok()); + + let mut invalid_config = valid_config.clone(); + invalid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + invalid_config.disks = Some(vec![DiskConfig { + iommu: false, + pci_segment: 1, + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::OnIommuSegment(1)) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + invalid_config.net = Some(vec![NetConfig { + iommu: false, + pci_segment: 1, + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::OnIommuSegment(1)) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + invalid_config.pmem = Some(vec![PmemConfig { + iommu: false, + pci_segment: 1, + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::OnIommuSegment(1)) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + invalid_config.devices = Some(vec![DeviceConfig { + iommu: false, + pci_segment: 1, + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::OnIommuSegment(1)) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + invalid_config.vsock = Some(VsockConfig { + iommu: false, + pci_segment: 1, + ..Default::default() + }); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::OnIommuSegment(1)) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.memory.shared = true; + invalid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + invalid_config.user_devices = Some(vec![UserDeviceConfig { + pci_segment: 1, + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::IommuNotSupportedOnSegment(1)) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + invalid_config.vdpa = Some(vec![VdpaConfig { + pci_segment: 1, + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::OnIommuSegment(1)) + ); + + let mut invalid_config = valid_config.clone(); + invalid_config.memory.shared = true; + invalid_config.platform = Some(PlatformConfig { + num_pci_segments: 16, + iommu_segments: Some(vec![1, 2, 3]), + ..Default::default() + }); + invalid_config.fs = Some(vec![FsConfig { + pci_segment: 1, + ..Default::default() + }]); + assert_eq!( + invalid_config.validate(), + Err(ValidationError::IommuNotSupportedOnSegment(1)) + ); + + let mut still_valid_config = valid_config.clone(); + still_valid_config.devices = Some(vec![ + DeviceConfig { + path: "/device1".into(), + ..Default::default() + }, + DeviceConfig { + path: "/device2".into(), + ..Default::default() + }, + ]); + assert!(still_valid_config.validate().is_ok()); + + let mut invalid_config = valid_config; + invalid_config.devices = Some(vec![ + DeviceConfig { + path: "/device1".into(), + ..Default::default() + }, + DeviceConfig { + path: "/device1".into(), + ..Default::default() + }, + ]); + assert!(invalid_config.validate().is_err()); + } +} diff --git a/vendor/cube-hypervisor/vmm/src/coredump.rs b/vendor/cube-hypervisor/vmm/src/coredump.rs new file mode 100644 index 0000000..99647c1 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/coredump.rs @@ -0,0 +1,346 @@ +// Copyright © 2022 ZTE Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#[cfg(target_arch = "x86_64")] +use hypervisor::arch::x86::{DescriptorTable, SegmentRegister}; +use linux_loader::elf; +use std::fs::File; +use std::io::Write; +use vm_memory::ByteValued; + +#[derive(Clone)] +pub struct CoredumpMemoryRegion { + pub mem_offset_in_elf: u64, + pub mem_size: u64, +} + +#[derive(Clone)] +pub struct CoredumpMemoryRegions { + pub ram_maps: std::collections::BTreeMap, +} + +/// Platform information +#[derive(Default)] +pub struct DumpState { + pub elf_note_size: isize, + pub elf_phdr_num: u16, + pub elf_sh_info: u32, + pub mem_offset: u64, + pub mem_info: Option, + pub file: Option, +} + +#[derive(Debug)] +pub enum GuestDebuggableError { + Coredump(anyhow::Error), + CoredumpFile(std::io::Error), +} + +pub trait GuestDebuggable: vm_migration::Pausable { + fn coredump( + &mut self, + _destination_url: &str, + ) -> std::result::Result<(), GuestDebuggableError> { + Ok(()) + } +} + +#[macro_export] +macro_rules! div_round_up { + ($n:expr,$d:expr) => { + ($n + $d - 1) / $d + }; +} + +#[repr(C)] +#[derive(Default, Copy, Clone)] +pub struct X86_64UserRegs { + /// r15, r14, r13, r12, rbp, rbx, r11, r10; + pub regs1: [u64; 8], + /// r9, r8, rax, rcx, rdx, rsi, rdi, orig_rax; + pub regs2: [u64; 8], + pub rip: u64, + pub cs: u64, + pub eflags: u64, + pub rsp: u64, + pub ss: u64, + pub fs_base: u64, + pub gs_base: u64, + pub ds: u64, + pub es: u64, + pub fs: u64, + pub gs: u64, +} + +unsafe impl ByteValued for X86_64UserRegs {} + +#[repr(C)] +pub struct X86_64ElfPrStatus { + pub pad1: [u8; 32], + pub pid: u32, + pub pads2: [u8; 76], + pub regs: X86_64UserRegs, + pub pad3: [u8; 8], +} + +#[repr(C)] +#[derive(Default, Copy, Clone)] +pub struct CpuSegment { + pub selector: u32, + pub limit: u32, + pub flags: u32, + pub pad: u32, + pub base: u64, +} + +const DESC_TYPE_SHIFT: u32 = 8; +const DESC_S_SHIFT: u32 = 12; +const DESC_DPL_SHIFT: u32 = 13; +const DESC_P_SHIFT: u32 = 15; +const DESC_P_MASK: u32 = 1 << DESC_P_SHIFT; +const DESC_AVL_SHIFT: u32 = 20; +const DESC_AVL_MASK: u32 = 1 << DESC_AVL_SHIFT; +const DESC_L_SHIFT: u32 = 21; +const DESC_B_SHIFT: u32 = 22; +const DESC_S_MASK: u32 = 1 << DESC_S_SHIFT; +const DESC_G_SHIFT: u32 = 23; +const DESC_G_MASK: u32 = 1 << DESC_G_SHIFT; + +impl CpuSegment { + pub fn new(reg: SegmentRegister) -> Self { + let p_mask = if (reg.present > 0) && (reg.unusable == 0) { + DESC_P_MASK + } else { + 0 + }; + let flags = ((reg.type_ as u32) << DESC_TYPE_SHIFT) + | p_mask + | ((reg.dpl as u32) << DESC_DPL_SHIFT) + | ((reg.db as u32) << DESC_B_SHIFT) + | ((reg.s as u32) * DESC_S_MASK) + | ((reg.l as u32) << DESC_L_SHIFT) + | ((reg.g as u32) * DESC_G_MASK) + | ((reg.avl as u32) * DESC_AVL_MASK); + + CpuSegment { + selector: reg.selector as u32, + limit: reg.limit, + flags, + pad: 0, + base: reg.base, + } + } + + pub fn new_from_table(reg: DescriptorTable) -> Self { + CpuSegment { + selector: 0, + limit: reg.limit as u32, + flags: 0, + pad: 0, + base: reg.base, + } + } +} + +#[repr(C)] +#[derive(Default, Copy, Clone)] +pub struct CpuState { + pub version: u32, + pub size: u32, + /// rax, rbx, rcx, rdx, rsi, rdi, rsp, rbp + pub regs1: [u64; 8], + /// r8, r9, r10, r11, r12, r13, r14, r15 + pub regs2: [u64; 8], + pub rip: u64, + pub rflags: u64, + pub cs: CpuSegment, + pub ds: CpuSegment, + pub es: CpuSegment, + pub fs: CpuSegment, + pub gs: CpuSegment, + pub ss: CpuSegment, + pub ldt: CpuSegment, + pub tr: CpuSegment, + pub gdt: CpuSegment, + pub idt: CpuSegment, + pub cr: [u64; 5], + pub kernel_gs_base: u64, +} + +unsafe impl ByteValued for CpuState {} + +pub enum NoteDescType { + Elf = 0, + Vmm = 1, + ElfAndVmm = 2, +} + +// "CORE" or "QEMU" +pub const COREDUMP_NAME_SIZE: u32 = 5; +pub const NT_PRSTATUS: u32 = 1; + +/// Core file. +const ET_CORE: u16 = 4; +/// 64-bit object. +const ELFCLASS64: u8 = 2; +/// Current ELF version. +const EV_CURRENT: u8 = 1; +/// AMD x86-64 architecture +const EM_X86_64: u16 = 62; + +pub trait Elf64Writable { + fn write_header( + &mut self, + dump_state: &DumpState, + ) -> std::result::Result<(), GuestDebuggableError> { + let e_ident = [ + elf::ELFMAG0 as u8, // magic + elf::ELFMAG1, + elf::ELFMAG2, + elf::ELFMAG3, + ELFCLASS64, // class + elf::ELFDATA2LSB as u8, //data + EV_CURRENT, // version + 0, // os_abi + 0, // abi_version + 0, // padding + 0, + 0, + 0, + 0, + 0, + 0, + ]; + let elf64_ehdr_size = std::mem::size_of::(); + let elf64_phdr_size = std::mem::size_of::(); + let mut elf64_ehdr = elf::Elf64_Ehdr { + e_ident, + e_type: ET_CORE, + e_machine: EM_X86_64, + e_version: EV_CURRENT as u32, + e_entry: 0, + e_phoff: elf64_ehdr_size as u64, + e_shoff: 0, + e_flags: 0, + e_ehsize: 0, + e_phentsize: elf64_phdr_size as u16, + e_phnum: dump_state.elf_phdr_num, + e_shentsize: 0, + e_shnum: 0, + e_shstrndx: 0, + }; + elf64_ehdr.e_ehsize = std::mem::size_of_val(&elf64_ehdr) as u16; + + let mut coredump_file = dump_state.file.as_ref().unwrap(); + let bytes: &[u8] = elf64_ehdr.as_slice(); + coredump_file + .write(bytes) + .map_err(GuestDebuggableError::CoredumpFile)?; + + Ok(()) + } + + fn write_note( + &mut self, + dump_state: &DumpState, + ) -> std::result::Result<(), GuestDebuggableError> { + let begin = dump_state.mem_offset - dump_state.elf_note_size as u64; + let elf64_phdr = elf::Elf64_Phdr { + p_type: elf::PT_NOTE, + p_flags: 0, + p_offset: begin, + p_vaddr: 0, + p_paddr: 0, + p_filesz: dump_state.elf_note_size as u64, + p_memsz: dump_state.elf_note_size as u64, + p_align: 0, + }; + + let mut coredump_file = dump_state.file.as_ref().unwrap(); + let bytes: &[u8] = elf64_phdr.as_slice(); + coredump_file + .write(bytes) + .map_err(GuestDebuggableError::CoredumpFile)?; + + Ok(()) + } + + fn write_load( + &mut self, + offset: u64, + phys_addr: u64, + length: u64, + virt_addr: u64, + dump_state: &DumpState, + ) -> std::result::Result<(), GuestDebuggableError> { + let elf64_load = elf::Elf64_Phdr { + p_type: elf::PT_LOAD, + p_flags: 0, + p_offset: offset, + p_vaddr: virt_addr, + p_paddr: phys_addr, + p_filesz: length, + p_memsz: length, + p_align: 0, + }; + + let mut coredump_file = dump_state.file.as_ref().unwrap(); + let bytes: &[u8] = elf64_load.as_slice(); + coredump_file + .write(bytes) + .map_err(GuestDebuggableError::CoredumpFile)?; + + Ok(()) + } + + fn write_loads( + &mut self, + dump_state: &DumpState, + ) -> std::result::Result<(), GuestDebuggableError> { + let mem_info = dump_state.mem_info.as_ref().unwrap(); + + for (gpa, load) in &mem_info.ram_maps { + self.write_load(load.mem_offset_in_elf, *gpa, load.mem_size, 0, dump_state)?; + } + + Ok(()) + } + + fn elf_note_size(&self, hdr_size: u32, name_size: u32, desc_size: u32) -> u32 { + (div_round_up!(hdr_size, 4) + div_round_up!(name_size, 4) + div_round_up!(desc_size, 4)) * 4 + } + + fn get_note_size(&self, desc_type: NoteDescType, nr_cpus: u32) -> u32 { + let note_head_size = std::mem::size_of::() as u32; + let elf_desc_size = std::mem::size_of::() as u32; + let cpu_state_desc_size = std::mem::size_of::() as u32; + + let elf_note_size = self.elf_note_size(note_head_size, COREDUMP_NAME_SIZE, elf_desc_size); + let vmm_note_size = + self.elf_note_size(note_head_size, COREDUMP_NAME_SIZE, cpu_state_desc_size); + + match desc_type { + NoteDescType::Elf => elf_note_size * nr_cpus, + NoteDescType::Vmm => vmm_note_size * nr_cpus, + NoteDescType::ElfAndVmm => (elf_note_size + vmm_note_size) * nr_cpus, + } + } +} + +pub trait CpuElf64Writable { + fn cpu_write_elf64_note( + &mut self, + _dump_state: &DumpState, + ) -> std::result::Result<(), GuestDebuggableError> { + Ok(()) + } + + fn cpu_write_vmm_note( + &mut self, + _dump_state: &DumpState, + ) -> std::result::Result<(), GuestDebuggableError> { + Ok(()) + } +} diff --git a/vendor/cube-hypervisor/vmm/src/cpu.rs b/vendor/cube-hypervisor/vmm/src/cpu.rs new file mode 100644 index 0000000..0082fb9 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/cpu.rs @@ -0,0 +1,2902 @@ +// Copyright © 2020, Oracle and/or its affiliates. +// +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause +// + +#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] +use crate::coredump::{ + CpuElf64Writable, CpuSegment, CpuState as DumpCpusState, DumpState, Elf64Writable, + GuestDebuggableError, NoteDescType, X86_64ElfPrStatus, X86_64UserRegs, COREDUMP_NAME_SIZE, + NT_PRSTATUS, +}; +use crate::device_manager::DeviceManager; +#[cfg(feature = "guest_debug")] +use crate::gdb::{get_raw_tid, Debuggable, DebuggableError}; +use crate::memory_manager::MemoryManager; +use crate::seccomp_filters::{get_seccomp_filter, Thread}; +#[cfg(target_arch = "x86_64")] +use crate::vm::physical_bits; +#[cfg(target_arch = "x86_64")] +use crate::vm_config::CompatibleMode; +use crate::vm_config::CpusConfig; +use crate::GuestMemoryMmap; +use crate::CPU_MANAGER_SNAPSHOT_ID; +use acpi_tables::{aml, aml::Aml, sdt::Sdt}; +use anyhow::anyhow; +#[cfg(all(target_arch = "aarch64", feature = "guest_debug"))] +use arch::aarch64::regs; +use arch::EntryPoint; +use arch::NumaNodes; +#[cfg(target_arch = "aarch64")] +use devices::gic::Gic; +use devices::interrupt_controller::InterruptController; +#[cfg(all(target_arch = "aarch64", feature = "guest_debug"))] +use gdbstub_arch::aarch64::reg::AArch64CoreRegs as CoreRegs; +#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] +use gdbstub_arch::x86::reg::{X86SegmentRegs, X86_64CoreRegs as CoreRegs}; +#[cfg(all(target_arch = "aarch64", feature = "guest_debug"))] +use hypervisor::aarch64::StandardRegisters; +#[allow(unused_imports)] +#[cfg(target_arch = "x86_64")] +use hypervisor::arch::x86::msr_index; +#[cfg(target_arch = "x86_64")] +use hypervisor::arch::x86::CpuIdEntry; +#[cfg(target_arch = "x86_64")] +use hypervisor::arch::x86::MsrEntry; +#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] +use hypervisor::arch::x86::{SpecialRegisters, StandardRegisters}; +#[cfg(target_arch = "aarch64")] +use hypervisor::kvm::kvm_bindings; +#[cfg(feature = "tdx")] +use hypervisor::kvm::{TdxExitDetails, TdxExitStatus}; +use hypervisor::{CpuState, HypervisorCpuError, HypervisorType, VmExit, VmOps}; +use libc::{c_void, siginfo_t}; +#[cfg(feature = "guest_debug")] +use linux_loader::elf::Elf64_Nhdr; +use seccompiler::{apply_filter, SeccompAction}; +use std::collections::BTreeMap; +#[cfg(feature = "guest_debug")] +use std::io::Write; +#[cfg(feature = "guest_debug")] +use std::mem::size_of; +use std::os::unix::thread::JoinHandleExt; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Barrier, Mutex}; +use std::{cmp, io, result, thread}; +use thiserror::Error; +use tracer::trace_scoped; +use vm_device::BusDevice; +#[cfg(feature = "guest_debug")] +use vm_memory::ByteValued; +#[cfg(feature = "guest_debug")] +use vm_memory::{Bytes, GuestAddressSpace}; +use vm_memory::{GuestAddress, GuestMemoryAtomic}; +use vm_migration::{ + Migratable, MigratableError, Pausable, Snapshot, SnapshotDataSection, Snapshottable, + Transportable, +}; +use vmm_sys_util::eventfd::EventFd; +use vmm_sys_util::signal::{register_signal_handler, SIGRTMIN}; +use zerocopy::AsBytes; + +#[cfg(all(target_arch = "aarch64", feature = "guest_debug"))] +/// Extract the specified bits of a 64-bit integer. +/// For example, to extrace 2 bits from offset 1 (zero based) of `6u64`, +/// following expression should return 3 (`0b11`): +/// `extract_bits_64!(0b0000_0110u64, 1, 2)` +/// +macro_rules! extract_bits_64 { + ($value: tt, $offset: tt, $length: tt) => { + ($value >> $offset) & (!0u64 >> (64 - $length)) + }; +} + +pub const CPU_MANAGER_ACPI_SIZE: usize = 0xc; + +#[derive(Debug, Error)] +pub enum Error { + #[error("Error creating vCPU: {0}")] + VcpuCreate(#[source] anyhow::Error), + + #[error("Error running bCPU: {0}")] + VcpuRun(#[source] anyhow::Error), + + #[error("Error spawning vCPU thread: {0}")] + VcpuSpawn(#[source] io::Error), + + #[error("Error generating common CPUID: {0}")] + CommonCpuId(#[source] arch::Error), + + #[error("Error configuring vCPU: {0}")] + VcpuConfiguration(#[source] arch::Error), + + #[cfg(target_arch = "aarch64")] + #[error("Error fetching preferred target: {0}")] + VcpuArmPreferredTarget(#[source] hypervisor::HypervisorVmError), + + #[cfg(target_arch = "aarch64")] + #[error("Error initialising vCPU: {0}")] + VcpuArmInit(#[source] hypervisor::HypervisorCpuError), + + #[error("Failed to join on vCPU threads: {0:?}")] + ThreadCleanup(std::boxed::Box), + + #[error("Error adding CpuManager to MMIO bus: {0}")] + BusError(#[source] vm_device::BusError), + + #[error("Requested vCPUs exceed maximum")] + DesiredVCpuCountExceedsMax, + + #[error("Cannot create seccomp filter: {0}")] + CreateSeccompFilter(#[source] seccompiler::Error), + + #[error("Cannot apply seccomp filter: {0}")] + ApplySeccompFilter(#[source] seccompiler::Error), + + #[error("Error starting vCPU after restore: {0}")] + StartRestoreVcpu(#[source] anyhow::Error), + + #[error("Unexpected VmExit")] + UnexpectedVmExit, + + #[error("Failed to allocate MMIO address for CpuManager")] + AllocateMmmioAddress, + + #[cfg(feature = "tdx")] + #[error("Error initializing TDX: {0}")] + InitializeTdx(#[source] hypervisor::HypervisorCpuError), + + #[cfg(target_arch = "aarch64")] + #[error("Error initializing PMU: {0}")] + InitPmu(#[source] hypervisor::HypervisorCpuError), + + #[cfg(feature = "guest_debug")] + #[error("Error during CPU debug: {0}")] + CpuDebug(#[source] hypervisor::HypervisorCpuError), + + #[cfg(feature = "guest_debug")] + #[error("Error translating virtual address: {0}")] + TranslateVirtualAddress(#[source] anyhow::Error), + + #[cfg(target_arch = "x86_64")] + #[error("Error setting up AMX: {0}")] + AmxEnable(#[source] anyhow::Error), +} +pub type Result = result::Result; + +#[cfg(target_arch = "x86_64")] +#[allow(dead_code)] +#[repr(packed)] +#[derive(AsBytes)] +struct LocalApic { + pub r#type: u8, + pub length: u8, + pub processor_id: u8, + pub apic_id: u8, + pub flags: u32, +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Default, AsBytes)] +struct Ioapic { + pub r#type: u8, + pub length: u8, + pub ioapic_id: u8, + _reserved: u8, + pub apic_address: u32, + pub gsi_base: u32, +} + +#[cfg(target_arch = "aarch64")] +#[allow(dead_code)] +#[repr(packed)] +#[derive(AsBytes)] +struct GicC { + pub r#type: u8, + pub length: u8, + pub reserved0: u16, + pub cpu_interface_number: u32, + pub uid: u32, + pub flags: u32, + pub parking_version: u32, + pub performance_interrupt: u32, + pub parked_address: u64, + pub base_address: u64, + pub gicv_base_address: u64, + pub gich_base_address: u64, + pub vgic_interrupt: u32, + pub gicr_base_address: u64, + pub mpidr: u64, + pub proc_power_effi_class: u8, + pub reserved1: u8, + pub spe_overflow_interrupt: u16, +} + +#[cfg(target_arch = "aarch64")] +#[allow(dead_code)] +#[repr(packed)] +#[derive(AsBytes)] +struct GicD { + pub r#type: u8, + pub length: u8, + pub reserved0: u16, + pub gic_id: u32, + pub base_address: u64, + pub global_irq_base: u32, + pub version: u8, + pub reserved1: [u8; 3], +} + +#[cfg(target_arch = "aarch64")] +#[allow(dead_code)] +#[repr(packed)] +#[derive(AsBytes)] +struct GicR { + pub r#type: u8, + pub length: u8, + pub reserved: u16, + pub base_address: u64, + pub range_length: u32, +} + +#[cfg(target_arch = "aarch64")] +#[allow(dead_code)] +#[repr(packed)] +#[derive(AsBytes)] +struct GicIts { + pub r#type: u8, + pub length: u8, + pub reserved0: u16, + pub translation_id: u32, + pub base_address: u64, + pub reserved1: u32, +} + +#[cfg(target_arch = "aarch64")] +#[allow(dead_code)] +#[repr(packed)] +#[derive(AsBytes)] +struct ProcessorHierarchyNode { + pub r#type: u8, + pub length: u8, + pub reserved: u16, + pub flags: u32, + pub parent: u32, + pub acpi_processor_id: u32, + pub num_private_resources: u32, +} + +#[allow(dead_code)] +#[repr(packed)] +#[derive(Default, AsBytes)] +struct InterruptSourceOverride { + pub r#type: u8, + pub length: u8, + pub bus: u8, + pub source: u8, + pub gsi: u32, + pub flags: u16, +} + +#[cfg(feature = "guest_debug")] +macro_rules! round_up { + ($n:expr,$d:expr) => { + (($n / ($d + 1)) + 1) * $d + }; +} + +/// Recover the raw errno from a `vcpu_init` failure so `Vcpu::init` can tell +/// "feature unsupported" (EINVAL) apart from genuine errors. The kvm-ioctls +/// error is a `vmm_sys_util::errno::Error` wrapped in anyhow inside +/// `HypervisorCpuError::VcpuInit`; returns `None` if it is any other variant or +/// the errno cannot be recovered. +#[cfg(target_arch = "aarch64")] +fn vcpu_init_errno(e: &hypervisor::HypervisorCpuError) -> Option { + let hypervisor::HypervisorCpuError::VcpuInit(source) = e else { + return None; + }; + source + .downcast_ref::() + .map(|err| err.errno()) +} + +/// Decide whether a `vcpu_init` failure warrants retrying without a PMU. Only +/// EINVAL means "feature unsupported"; any other errno -- or an error whose +/// errno cannot be recovered -- is a genuine failure that clearing the PMU bit +/// would not fix, so it must be propagated rather than masked with a retry. +/// Split out from `Vcpu::init` so the decision is unit-testable without a KVM +/// vcpu to inject failures into (see test_vcpu_init_retry_decision). +#[cfg(target_arch = "aarch64")] +fn should_retry_without_pmu(e: &hypervisor::HypervisorCpuError) -> bool { + vcpu_init_errno(e) == Some(libc::EINVAL) +} + +/// A wrapper around creating and using a kvm-based VCPU. +pub struct Vcpu { + // The hypervisor abstracted CPU. + vcpu: Arc, + id: u8, + #[cfg(target_arch = "aarch64")] + mpidr: u64, + saved_state: Option, + #[cfg(target_arch = "x86_64")] + tsc_msrs: Vec, +} + +impl Vcpu { + /// Constructs a new VCPU for `vm`. + /// + /// # Arguments + /// + /// * `id` - Represents the CPU number between [0, max vcpus). + /// * `vm` - The virtual machine this vcpu will get attached to. + /// * `vm_ops` - Optional object for exit handling. + pub fn new( + id: u8, + vm: &Arc, + vm_ops: Option>, + ) -> Result { + let vcpu = vm + .create_vcpu(id, vm_ops) + .map_err(|e| Error::VcpuCreate(e.into()))?; + // Initially the cpuid per vCPU is the one supported by this VM. + Ok(Vcpu { + vcpu, + id, + #[cfg(target_arch = "aarch64")] + mpidr: 0, + saved_state: None, + #[cfg(target_arch = "x86_64")] + tsc_msrs: Vec::new(), + }) + } + + /// Configures a vcpu and should be called once per vcpu when created. + /// + /// # Arguments + /// + /// * `kernel_entry_point` - Kernel entry point address in guest memory and boot protocol used. + /// * `vm_memory` - Guest memory. + /// * `cpuid` - (x86_64) CpuId, wrapper over the `kvm_cpuid2` structure. + pub fn configure( + &mut self, + #[cfg(target_arch = "aarch64")] vm: &Arc, + kernel_entry_point: Option, + #[cfg(target_arch = "x86_64")] vm_memory: &GuestMemoryAtomic, + #[cfg(target_arch = "x86_64")] cpuid: Vec, + #[cfg(target_arch = "x86_64")] kvm_hyperv: bool, + ) -> Result<()> { + #[cfg(target_arch = "aarch64")] + { + self.init(vm)?; + self.mpidr = arch::configure_vcpu(&self.vcpu, self.id, kernel_entry_point) + .map_err(Error::VcpuConfiguration)?; + } + info!("Configuring vCPU: cpu_id = {}", self.id); + #[cfg(target_arch = "x86_64")] + arch::configure_vcpu( + &self.vcpu, + self.id, + kernel_entry_point, + vm_memory, + cpuid, + kvm_hyperv, + ) + .map_err(Error::VcpuConfiguration)?; + + Ok(()) + } + + /// Gets the MPIDR register value. + #[cfg(target_arch = "aarch64")] + pub fn get_mpidr(&self) -> u64 { + self.mpidr + } + + /// Gets the saved vCPU state. + #[cfg(target_arch = "aarch64")] + pub fn get_saved_state(&self) -> Option { + self.saved_state.clone() + } + + /// Initializes an aarch64 specific vcpu for booting Linux. + /// + /// A guest PMU is requested when the host KVM supports it. On hosts that do + /// not advertise PMUv3 for the guest -- older kernels, some nested-virt + /// setups, a few ARM cores -- the vcpu is initialized without a PMU rather + /// than failing to boot, since nothing in the guest depends on a hardware + /// PMU being exposed. + #[cfg(target_arch = "aarch64")] + pub fn init(&self, vm: &Arc) -> Result<()> { + let mut kvi: kvm_bindings::kvm_vcpu_init = kvm_bindings::kvm_vcpu_init::default(); + + // This reads back the kernel's preferred target type. + vm.get_preferred_target(&mut kvi) + .map_err(Error::VcpuArmPreferredTarget)?; + kvi.features[0] |= 1 << kvm_bindings::KVM_ARM_VCPU_PSCI_0_2; + // Non-boot cpus are powered off initially. + if self.id > 0 { + kvi.features[0] |= 1 << kvm_bindings::KVM_ARM_VCPU_POWER_OFF; + } + + // Try with a PMU first; on hosts without PMUv3, vcpu_init reports EINVAL, + // so clear the bit and retry once. The guest FDT PMU node is gated + // separately by has_pmu_support(), so a vcpu armed without a PMU here is + // never advertised one it does not have. + // + // PMU_V3 must remain the last feature flag set before vcpu_init: the + // retry below recovers solely by clearing this bit, so adding another + // flag after this line would let an EINVAL from that flag be silently + // "recovered" by dropping the wrong feature. + kvi.features[0] |= 1 << kvm_bindings::KVM_ARM_VCPU_PMU_V3; + if let Err(e) = self.vcpu.vcpu_init(&kvi) { + if !should_retry_without_pmu(&e) { + return Err(Error::VcpuArmInit(e)); + } + warn!( + "vcpu_init with KVM_ARM_VCPU_PMU_V3 failed for vcpu {}: {:?}, \ + retrying without PMU support", + self.id, e + ); + kvi.features[0] &= !(1 << kvm_bindings::KVM_ARM_VCPU_PMU_V3); + self.vcpu.vcpu_init(&kvi).map_err(Error::VcpuArmInit)?; + } + + Ok(()) + } + + /// Runs the VCPU until it exits, returning the reason. + /// + /// Note that the state of the VCPU and associated VM must be setup first for this to do + /// anything useful. + pub fn run(&self) -> std::result::Result { + self.vcpu.run() + } +} + +const VCPU_SNAPSHOT_ID: &str = "vcpu"; +impl Pausable for Vcpu {} +impl Snapshottable for Vcpu { + fn id(&self) -> String { + VCPU_SNAPSHOT_ID.to_string() + } + + fn snapshot(&mut self) -> std::result::Result { + let saved_state = self + .vcpu + .state() + .map_err(|e| MigratableError::Pause(anyhow!("Could not get vCPU state {:?}", e)))?; + + let mut vcpu_snapshot = Snapshot::new(&format!("{:03}", self.id)); + vcpu_snapshot.add_data_section(SnapshotDataSection::new_from_state( + VCPU_SNAPSHOT_ID, + &saved_state, + )?); + + self.saved_state = Some(saved_state); + + Ok(vcpu_snapshot) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + let saved_state: CpuState = snapshot.to_state(VCPU_SNAPSHOT_ID)?; + + self.vcpu + .set_state(&saved_state) + .map_err(|e| MigratableError::Pause(anyhow!("Could not set the vCPU state {:?}", e)))?; + + // Parse msrs from state, save related msrs in tsc_msrs. + #[cfg(target_arch = "x86_64")] + { + let tsc_msrs: Vec = match &saved_state { + #[cfg(feature = "kvm")] + hypervisor::CpuState::Kvm(inner) => inner + .msrs + .iter() + .filter(|msr| msr.index == msr_index::MSR_IA32_TSC) + .cloned() + .collect(), + #[cfg(feature = "mshv")] + _ => Vec::new(), + }; + self.tsc_msrs = tsc_msrs; + } + + self.saved_state = Some(saved_state); + + Ok(()) + } +} + +pub struct CpuManager { + hypervisor_type: HypervisorType, + config: CpusConfig, + #[cfg_attr(target_arch = "aarch64", allow(dead_code))] + interrupt_controller: Option>>, + #[cfg_attr(target_arch = "aarch64", allow(dead_code))] + vm_memory: GuestMemoryAtomic, + #[cfg(target_arch = "x86_64")] + cpuid: Vec, + #[cfg_attr(target_arch = "aarch64", allow(dead_code))] + vm: Arc, + vcpus_kill_signalled: Arc, + vcpus_pause_signalled: Arc, + exit_evt: EventFd, + #[allow(dead_code)] + reset_evt: EventFd, + #[cfg(feature = "guest_debug")] + vm_debug_evt: EventFd, + vcpu_states: Vec, + selected_cpu: u8, + vcpus: Vec>>, + seccomp_action: SeccompAction, + vm_ops: Arc, + #[cfg_attr(target_arch = "aarch64", allow(dead_code))] + acpi_address: Option, + proximity_domain_per_cpu: BTreeMap, + affinity: BTreeMap>, + dynamic: bool, + vcpu_started: Arc, +} + +const CPU_ENABLE_FLAG: usize = 0; +const CPU_INSERTING_FLAG: usize = 1; +const CPU_REMOVING_FLAG: usize = 2; +const CPU_EJECT_FLAG: usize = 3; + +const CPU_STATUS_OFFSET: u64 = 4; +const CPU_SELECTION_OFFSET: u64 = 0; + +impl BusDevice for CpuManager { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + // The Linux kernel, quite reasonably, doesn't zero the memory it gives us. + data.fill(0); + + match offset { + CPU_SELECTION_OFFSET => { + data[0] = self.selected_cpu; + } + CPU_STATUS_OFFSET => { + if self.selected_cpu < self.max_vcpus() { + let state = &self.vcpu_states[usize::from(self.selected_cpu)]; + if state.active() { + data[0] |= 1 << CPU_ENABLE_FLAG; + } + if state.inserting { + data[0] |= 1 << CPU_INSERTING_FLAG; + } + if state.removing { + data[0] |= 1 << CPU_REMOVING_FLAG; + } + } else { + warn!("Out of range vCPU id: {}", self.selected_cpu); + } + } + _ => { + warn!( + "Unexpected offset for accessing CPU manager device: {:#}", + offset + ); + } + } + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + match offset { + CPU_SELECTION_OFFSET => { + self.selected_cpu = data[0]; + } + CPU_STATUS_OFFSET => { + if self.selected_cpu < self.max_vcpus() { + let state = &mut self.vcpu_states[usize::from(self.selected_cpu)]; + // The ACPI code writes back a 1 to acknowledge the insertion + if (data[0] & (1 << CPU_INSERTING_FLAG) == 1 << CPU_INSERTING_FLAG) + && state.inserting + { + state.inserting = false; + } + // Ditto for removal + if (data[0] & (1 << CPU_REMOVING_FLAG) == 1 << CPU_REMOVING_FLAG) + && state.removing + { + state.removing = false; + } + // Trigger removal of vCPU + if data[0] & (1 << CPU_EJECT_FLAG) == 1 << CPU_EJECT_FLAG { + if let Err(e) = self.remove_vcpu(self.selected_cpu) { + error!("Error removing vCPU: {:?}", e); + } + } + } else { + warn!("Out of range vCPU id: {}", self.selected_cpu); + } + } + _ => { + warn!( + "Unexpected offset for accessing CPU manager device: {:#}", + offset + ); + } + } + None + } +} + +#[derive(Default)] +struct VcpuState { + inserting: bool, + removing: bool, + handle: Option>, + kill: Arc, + vcpu_run_interrupted: Arc, +} + +impl VcpuState { + fn active(&self) -> bool { + self.handle.is_some() + } + + fn signal_thread(&self) { + if let Some(handle) = self.handle.as_ref() { + loop { + unsafe { + libc::pthread_kill(handle.as_pthread_t() as _, SIGRTMIN()); + } + if self.vcpu_run_interrupted.load(Ordering::SeqCst) { + break; + } else { + // This is more effective than thread::yield_now() at + // avoiding a priority inversion with the vCPU thread + thread::sleep(std::time::Duration::from_millis(1)); + } + } + } + } + + fn join_thread(&mut self) -> Result<()> { + if let Some(handle) = self.handle.take() { + handle.join().map_err(Error::ThreadCleanup)? + } + + Ok(()) + } + + fn unpark_thread(&self) { + if let Some(handle) = self.handle.as_ref() { + handle.thread().unpark() + } + } +} + +impl CpuManager { + #[allow(unused_variables)] + #[allow(clippy::too_many_arguments)] + pub fn new( + config: &CpusConfig, + device_manager: &Arc>, + memory_manager: &Arc>, + vm: Arc, + exit_evt: EventFd, + reset_evt: EventFd, + #[cfg(feature = "guest_debug")] vm_debug_evt: EventFd, + hypervisor: Arc, + seccomp_action: SeccompAction, + vm_ops: Arc, + #[cfg(feature = "tdx")] tdx_enabled: bool, + numa_nodes: &NumaNodes, + vcpu_started: Arc, + ) -> Result>> { + let guest_memory = memory_manager.lock().unwrap().guest_memory(); + let mut vcpu_states = Vec::with_capacity(usize::from(config.max_vcpus)); + vcpu_states.resize_with(usize::from(config.max_vcpus), VcpuState::default); + let hypervisor_type = hypervisor.hypervisor_type(); + + #[cfg(target_arch = "x86_64")] + let sgx_epc_sections = memory_manager + .lock() + .unwrap() + .sgx_epc_region() + .as_ref() + .map(|sgx_epc_region| sgx_epc_region.epc_sections().values().cloned().collect()); + #[cfg(target_arch = "x86_64")] + let cpuid = { + let phys_bits = physical_bits(config.max_phys_bits, hypervisor.hypervisor_type()); + + let (vendor_compatible, arch_compatible) = match config.compatible { + CompatibleMode::Vendor => (true, false), + CompatibleMode::Max => (true, true), + CompatibleMode::Ignore => (false, false), + }; + + arch::generate_common_cpuid( + hypervisor, + config + .topology + .clone() + .map(|t| (t.threads_per_core, t.cores_per_die, t.dies_per_package)), + sgx_epc_sections, + phys_bits, + config.kvm_hyperv, + #[cfg(feature = "tdx")] + tdx_enabled, + vendor_compatible, + arch_compatible, + ) + .map_err(Error::CommonCpuId)? + }; + #[cfg(target_arch = "x86_64")] + if config.features.amx { + const ARCH_GET_XCOMP_GUEST_PERM: usize = 0x1024; + const ARCH_REQ_XCOMP_GUEST_PERM: usize = 0x1025; + const XFEATURE_XTILEDATA: usize = 18; + const XFEATURE_XTILEDATA_MASK: usize = 1 << XFEATURE_XTILEDATA; + + // This is safe as the syscall is only modifing kernel internal + // data structures that the kernel is itself expected to safeguard. + let amx_tile = unsafe { + libc::syscall( + libc::SYS_arch_prctl, + ARCH_REQ_XCOMP_GUEST_PERM, + XFEATURE_XTILEDATA, + ) + }; + + if amx_tile != 0 { + return Err(Error::AmxEnable(anyhow!("Guest AMX usage not supported"))); + } else { + // This is safe as the mask being modified (not marked mutable as it is + // modified in unsafe only which is permitted) isn't in use elsewhere. + let mask: usize = 0; + let result = unsafe { + libc::syscall(libc::SYS_arch_prctl, ARCH_GET_XCOMP_GUEST_PERM, &mask) + }; + if result != 0 || (mask & XFEATURE_XTILEDATA_MASK) != XFEATURE_XTILEDATA_MASK { + return Err(Error::AmxEnable(anyhow!("Guest AMX usage not supported"))); + } + } + } + + let device_manager = device_manager.lock().unwrap(); + + let proximity_domain_per_cpu: BTreeMap = { + let mut cpu_list = Vec::new(); + for (proximity_domain, numa_node) in numa_nodes.iter() { + for cpu in numa_node.cpus.iter() { + cpu_list.push((*cpu, *proximity_domain)) + } + } + cpu_list + } + .into_iter() + .collect(); + + let affinity = if let Some(cpu_affinity) = config.affinity.as_ref() { + cpu_affinity + .iter() + .map(|a| (a.vcpu, a.host_cpus.clone())) + .collect() + } else { + BTreeMap::new() + }; + + #[cfg(feature = "tdx")] + let dynamic = !tdx_enabled; + #[cfg(not(feature = "tdx"))] + let dynamic = true; + + let acpi_address = if dynamic { + Some( + device_manager + .allocator() + .lock() + .unwrap() + .allocate_platform_mmio_addresses(None, CPU_MANAGER_ACPI_SIZE as u64, None) + .ok_or(Error::AllocateMmmioAddress)?, + ) + } else { + None + }; + + let cpu_manager = Arc::new(Mutex::new(CpuManager { + hypervisor_type, + config: config.clone(), + interrupt_controller: device_manager.interrupt_controller().clone(), + vm_memory: guest_memory, + #[cfg(target_arch = "x86_64")] + cpuid, + vm, + vcpus_kill_signalled: Arc::new(AtomicBool::new(false)), + vcpus_pause_signalled: Arc::new(AtomicBool::new(false)), + vcpu_states, + exit_evt, + reset_evt, + #[cfg(feature = "guest_debug")] + vm_debug_evt, + selected_cpu: 0, + vcpus: Vec::with_capacity(usize::from(config.max_vcpus)), + seccomp_action, + vm_ops, + acpi_address, + proximity_domain_per_cpu, + affinity, + dynamic, + vcpu_started, + })); + + if let Some(acpi_address) = acpi_address { + device_manager + .mmio_bus() + .insert( + cpu_manager.clone(), + acpi_address.0, + CPU_MANAGER_ACPI_SIZE as u64, + ) + .map_err(Error::BusError)?; + } + + Ok(cpu_manager) + } + + fn create_vcpu( + &mut self, + cpu_id: u8, + entry_point: Option, + snapshot: Option, + ) -> Result<()> { + info!("Creating vCPU: cpu_id = {}", cpu_id); + + let mut vcpu = Vcpu::new(cpu_id, &self.vm, Some(self.vm_ops.clone()))?; + + if let Some(snapshot) = snapshot { + // AArch64 vCPUs should be initialized after created. + #[cfg(target_arch = "aarch64")] + vcpu.init(&self.vm)?; + + vcpu.restore(snapshot).expect("Failed to restore vCPU"); + } else { + #[cfg(target_arch = "x86_64")] + vcpu.configure( + entry_point, + &self.vm_memory, + self.cpuid.clone(), + self.config.kvm_hyperv, + )?; + + #[cfg(target_arch = "aarch64")] + vcpu.configure(&self.vm, entry_point)?; + } + + // Adding vCPU to the CpuManager's vCPU list. + let vcpu = Arc::new(Mutex::new(vcpu)); + self.vcpus.push(vcpu); + + Ok(()) + } + + /// Only create new vCPUs if there aren't any inactive ones to reuse + fn create_vcpus(&mut self, desired_vcpus: u8, entry_point: Option) -> Result<()> { + info!( + "Request to create new vCPUs: desired = {}, max = {}, allocated = {}, present = {}", + desired_vcpus, + self.config.max_vcpus, + self.vcpus.len(), + self.present_vcpus() + ); + + if desired_vcpus > self.config.max_vcpus { + return Err(Error::DesiredVCpuCountExceedsMax); + } + + // Only create vCPUs in excess of all the allocated vCPUs. + for cpu_id in self.vcpus.len() as u8..desired_vcpus { + self.create_vcpu(cpu_id, entry_point, None)?; + } + + Ok(()) + } + + #[cfg(target_arch = "aarch64")] + pub fn init_pmu(&self, irq: u32) -> Result { + for cpu in self.vcpus.iter() { + let cpu = cpu.lock().unwrap(); + // Check if PMU attr is available, if not, log the information. + if cpu.vcpu.has_pmu_support() { + cpu.vcpu.init_pmu(irq).map_err(Error::InitPmu)?; + } else { + debug!( + "PMU attribute is not supported in vCPU{}, skip PMU init!", + cpu.id + ); + return Ok(false); + } + } + + Ok(true) + } + + fn start_vcpu( + &mut self, + vcpu: Arc>, + vcpu_id: u8, + vcpu_thread_barrier: Arc, + inserting: bool, + ) -> Result<()> { + // WORKAROUND: force reset event into shutdown. + let reset_evt = self.exit_evt.try_clone().unwrap(); + let exit_evt = self.exit_evt.try_clone().unwrap(); + #[cfg(feature = "guest_debug")] + let vm_debug_evt = self.vm_debug_evt.try_clone().unwrap(); + let panic_exit_evt = self.exit_evt.try_clone().unwrap(); + let vcpu_kill_signalled = self.vcpus_kill_signalled.clone(); + let vcpu_pause_signalled = self.vcpus_pause_signalled.clone(); + + let vcpu_kill = self.vcpu_states[usize::from(vcpu_id)].kill.clone(); + let vcpu_run_interrupted = self.vcpu_states[usize::from(vcpu_id)] + .vcpu_run_interrupted + .clone(); + let panic_vcpu_run_interrupted = vcpu_run_interrupted.clone(); + + // Prepare the CPU set the current vCPU is expected to run onto. + let cpuset = self.affinity.get(&vcpu_id).map(|host_cpus| { + let mut cpuset: libc::cpu_set_t = unsafe { std::mem::zeroed() }; + unsafe { libc::CPU_ZERO(&mut cpuset) }; + for host_cpu in host_cpus { + unsafe { libc::CPU_SET(*host_cpu as usize, &mut cpuset) }; + } + cpuset + }); + + // Retrieve seccomp filter for vcpu thread + let vcpu_seccomp_filter = + get_seccomp_filter(&self.seccomp_action, Thread::Vcpu, self.hypervisor_type) + .map_err(Error::CreateSeccompFilter)?; + + #[cfg(target_arch = "x86_64")] + let interrupt_controller_clone = self.interrupt_controller.as_ref().cloned(); + + info!("Starting vCPU: cpu_id = {}", vcpu_id); + + let handle = Some( + thread::Builder::new() + .name(format!("vcpu{}", vcpu_id)) + .spawn(move || { + // Schedule the thread to run on the expected CPU set + if let Some(cpuset) = cpuset.as_ref() { + let ret = unsafe { + libc::sched_setaffinity( + 0, + std::mem::size_of::(), + cpuset as *const libc::cpu_set_t, + ) + }; + + if ret != 0 { + error!( + "Failed scheduling the vCPU {} on the expected CPU set: {}", + vcpu_id, + io::Error::last_os_error() + ); + return; + } + } + + // Apply seccomp filter for vcpu thread. + if !vcpu_seccomp_filter.is_empty() { + if let Err(e) = + apply_filter(&vcpu_seccomp_filter).map_err(Error::ApplySeccompFilter) + { + error!("Error applying seccomp filter: {:?}", e); + return; + } + } + extern "C" fn handle_signal(_: i32, _: *mut siginfo_t, _: *mut c_void) {} + // This uses an async signal safe handler to kill the vcpu handles. + register_signal_handler(SIGRTMIN(), handle_signal) + .expect("Failed to register vcpu signal handler"); + // Block until all CPUs are ready. + vcpu_thread_barrier.wait(); + + std::panic::catch_unwind(move || { + loop { + // If we are being told to pause, we park the thread + // until the pause boolean is toggled. + // The resume operation is responsible for toggling + // the boolean and unpark the thread. + // We enter a loop because park() could spuriously + // return. We will then park() again unless the + // pause boolean has been toggled. + + // Need to use Ordering::SeqCst as we have multiple + // loads and stores to different atomics and we need + // to see them in a consistent order in all threads + + if vcpu_pause_signalled.load(Ordering::SeqCst) { + // As a pause can be caused by PIO & MMIO exits then we need to ensure they are + // completed by returning to KVM_RUN. From the kernel docs: + // + // For KVM_EXIT_IO, KVM_EXIT_MMIO, KVM_EXIT_OSI, KVM_EXIT_PAPR, KVM_EXIT_XEN, + // KVM_EXIT_EPR, KVM_EXIT_X86_RDMSR and KVM_EXIT_X86_WRMSR the corresponding + // operations are complete (and guest state is consistent) only after userspace + // has re-entered the kernel with KVM_RUN. The kernel side will first finish + // incomplete operations and then check for pending signals. + // The pending state of the operation is not preserved in state which is + // visible to userspace, thus userspace should ensure that the operation is + // completed before performing a live migration. Userspace can re-enter the + // guest with an unmasked signal pending or with the immediate_exit field set + // to complete pending operations without allowing any further instructions + // to be executed. + + #[cfg(feature = "kvm")] + { + vcpu.lock().as_ref().unwrap().vcpu.set_immediate_exit(true); + if !matches!(vcpu.lock().unwrap().run(), Ok(VmExit::Ignore)) { + error!("Unexpected VM exit on \"immediate_exit\" run"); + break; + } + vcpu.lock().as_ref().unwrap().vcpu.set_immediate_exit(false); + } + + vcpu_run_interrupted.store(true, Ordering::SeqCst); + while vcpu_pause_signalled.load(Ordering::SeqCst) { + thread::park(); + } + vcpu_run_interrupted.store(false, Ordering::SeqCst); + } + + // We've been told to terminate + if vcpu_kill_signalled.load(Ordering::SeqCst) + || vcpu_kill.load(Ordering::SeqCst) + { + vcpu_run_interrupted.store(true, Ordering::SeqCst); + break; + } + + #[cfg(feature = "tdx")] + let mut vcpu = vcpu.lock().unwrap(); + #[cfg(not(feature = "tdx"))] + let vcpu = vcpu.lock().unwrap(); + // vcpu.run() returns false on a triple-fault so trigger a reset + match vcpu.run() { + Ok(run) => match run { + #[cfg(feature = "kvm")] + VmExit::Debug => { + info!("VmExit::Debug"); + #[cfg(feature = "guest_debug")] + { + vcpu_pause_signalled.store(true, Ordering::SeqCst); + let raw_tid = get_raw_tid(vcpu_id as usize); + vm_debug_evt.write(raw_tid as u64).unwrap(); + } + } + #[cfg(target_arch = "x86_64")] + VmExit::IoapicEoi(vector) => { + if let Some(interrupt_controller) = + &interrupt_controller_clone + { + interrupt_controller + .lock() + .unwrap() + .end_of_interrupt(vector); + } + } + VmExit::Ignore => {} + VmExit::Hyperv => {} + VmExit::Reset => { + error!("Shutdown because VmExit::Reset"); + vcpu_run_interrupted.store(true, Ordering::SeqCst); + reset_evt.write(1).unwrap(); + break; + } + VmExit::Shutdown => { + info!("VmExit::Shutdown"); + vcpu_run_interrupted.store(true, Ordering::SeqCst); + exit_evt.write(1).unwrap(); + break; + } + #[cfg(feature = "tdx")] + VmExit::Tdx => { + if let Some(vcpu) = Arc::get_mut(&mut vcpu.vcpu) { + match vcpu.get_tdx_exit_details() { + Ok(details) => match details { + TdxExitDetails::GetQuote => warn!("TDG_VP_VMCALL_GET_QUOTE not supported"), + TdxExitDetails::SetupEventNotifyInterrupt => { + warn!("TDG_VP_VMCALL_SETUP_EVENT_NOTIFY_INTERRUPT not supported") + } + }, + Err(e) => error!("Unexpected TDX VMCALL: {}", e), + } + vcpu.set_tdx_status(TdxExitStatus::InvalidOperand); + } else { + // We should never reach this code as + // this means the design from the code + // is wrong. + unreachable!("Couldn't get a mutable reference from Arc as there are multiple instances"); + } + } + }, + + Err(e) => { + error!("VCPU generated error: {:?}", Error::VcpuRun(e.into())); + break; + } + } + + // We've been told to terminate + if vcpu_kill_signalled.load(Ordering::SeqCst) + || vcpu_kill.load(Ordering::SeqCst) + { + vcpu_run_interrupted.store(true, Ordering::SeqCst); + break; + } + } + }) + .or_else(|_| { + panic_vcpu_run_interrupted.store(true, Ordering::SeqCst); + error!("vCPU thread panicked"); + panic_exit_evt.write(1) + }) + .ok(); + }) + .map_err(Error::VcpuSpawn)?, + ); + + // On hot plug calls into this function entry_point is None. It is for + // those hotplug CPU additions that we need to set the inserting flag. + self.vcpu_states[usize::from(vcpu_id)].handle = handle; + self.vcpu_states[usize::from(vcpu_id)].inserting = inserting; + + Ok(()) + } + + /// Start up as many vCPUs threads as needed to reach `desired_vcpus` + fn activate_vcpus( + &mut self, + desired_vcpus: u8, + inserting: bool, + paused: Option, + ) -> Result<()> { + if desired_vcpus > self.config.max_vcpus { + return Err(Error::DesiredVCpuCountExceedsMax); + } + + let vcpu_thread_barrier = Arc::new(Barrier::new( + (desired_vcpus - self.present_vcpus() + 1) as usize, + )); + + if let Some(paused) = paused { + self.vcpus_pause_signalled.store(paused, Ordering::SeqCst); + } + + info!( + "Starting vCPUs: desired = {}, allocated = {}, present = {}, paused = {}", + desired_vcpus, + self.vcpus.len(), + self.present_vcpus(), + self.vcpus_pause_signalled.load(Ordering::SeqCst) + ); + + // This reuses any inactive vCPUs as well as any that were newly created + for vcpu_id in self.present_vcpus()..desired_vcpus { + let vcpu = Arc::clone(&self.vcpus[vcpu_id as usize]); + self.start_vcpu(vcpu, vcpu_id, vcpu_thread_barrier.clone(), inserting)?; + } + + self.vcpu_started.store(true, Ordering::SeqCst); + + // Unblock all CPU threads. + vcpu_thread_barrier.wait(); + Ok(()) + } + + fn mark_vcpus_for_removal(&mut self, desired_vcpus: u8) { + // Mark vCPUs for removal, actual removal happens on ejection + for cpu_id in desired_vcpus..self.present_vcpus() { + self.vcpu_states[usize::from(cpu_id)].removing = true; + } + } + + fn remove_vcpu(&mut self, cpu_id: u8) -> Result<()> { + info!("Removing vCPU: cpu_id = {}", cpu_id); + let state = &mut self.vcpu_states[usize::from(cpu_id)]; + state.kill.store(true, Ordering::SeqCst); + state.signal_thread(); + state.join_thread()?; + state.handle = None; + + // Once the thread has exited, clear the "kill" so that it can reused + state.kill.store(false, Ordering::SeqCst); + + Ok(()) + } + + pub fn create_boot_vcpus(&mut self, entry_point: Option) -> Result<()> { + trace_scoped!("create_boot_vcpus"); + + self.create_vcpus(self.boot_vcpus(), entry_point) + } + + // Starts all the vCPUs that the VM is booting with. Blocks until all vCPUs are running. + pub fn start_boot_vcpus(&mut self, paused: bool) -> Result<()> { + self.activate_vcpus(self.boot_vcpus(), false, Some(paused)) + } + + pub fn start_restored_vcpus(&mut self) -> Result<()> { + self.activate_vcpus(self.vcpus.len() as u8, false, Some(true)) + .map_err(|e| { + Error::StartRestoreVcpu(anyhow!("Failed to start restored vCPUs: {:#?}", e)) + })?; + + Ok(()) + } + + pub fn resize(&mut self, desired_vcpus: u8) -> Result { + if desired_vcpus.cmp(&self.present_vcpus()) == cmp::Ordering::Equal { + return Ok(false); + } + + if !self.dynamic { + return Ok(false); + } + + match desired_vcpus.cmp(&self.present_vcpus()) { + cmp::Ordering::Greater => { + self.create_vcpus(desired_vcpus, None)?; + self.activate_vcpus(desired_vcpus, true, None)?; + Ok(true) + } + cmp::Ordering::Less => { + self.mark_vcpus_for_removal(desired_vcpus); + Ok(true) + } + _ => Ok(false), + } + } + + pub fn shutdown(&mut self) -> Result<()> { + // Tell the vCPUs to stop themselves next time they go through the loop + self.vcpus_kill_signalled.store(true, Ordering::SeqCst); + + // Toggle the vCPUs pause boolean + self.vcpus_pause_signalled.store(false, Ordering::SeqCst); + + // Unpark all the VCPU threads. + for state in self.vcpu_states.iter() { + state.unpark_thread(); + } + + // Signal to the spawned threads (vCPUs and console signal handler). For the vCPU threads + // this will interrupt the KVM_RUN ioctl() allowing the loop to check the boolean set + // above. + for state in self.vcpu_states.iter() { + state.signal_thread(); + } + + // Wait for all the threads to finish. This removes the state from the vector. + for mut state in self.vcpu_states.drain(..) { + state.join_thread()?; + } + + Ok(()) + } + + #[cfg(feature = "tdx")] + pub fn initialize_tdx(&self, hob_address: u64) -> Result<()> { + for vcpu in &self.vcpus { + vcpu.lock() + .unwrap() + .vcpu + .tdx_init(hob_address) + .map_err(Error::InitializeTdx)?; + } + Ok(()) + } + + pub fn boot_vcpus(&self) -> u8 { + self.config.boot_vcpus + } + + pub fn max_vcpus(&self) -> u8 { + self.config.max_vcpus + } + + #[cfg(target_arch = "x86_64")] + pub fn common_cpuid(&self) -> Vec { + self.cpuid.clone() + } + + fn present_vcpus(&self) -> u8 { + self.vcpu_states + .iter() + .fold(0, |acc, state| acc + state.active() as u8) + } + + #[cfg(target_arch = "aarch64")] + pub fn get_mpidrs(&self) -> Vec { + self.vcpus + .iter() + .map(|cpu| cpu.lock().unwrap().get_mpidr()) + .collect() + } + + #[cfg(target_arch = "aarch64")] + pub fn get_saved_states(&self) -> Vec { + self.vcpus + .iter() + .map(|cpu| cpu.lock().unwrap().get_saved_state().unwrap()) + .collect() + } + + #[cfg(target_arch = "aarch64")] + pub fn get_vcpu_topology(&self) -> Option<(u8, u8, u8)> { + self.config + .topology + .clone() + .map(|t| (t.threads_per_core, t.cores_per_die, t.packages)) + } + + pub fn create_madt(&self) -> Sdt { + use crate::acpi; + // This is also checked in the commandline parsing. + assert!(self.config.boot_vcpus <= self.config.max_vcpus); + + let mut madt = Sdt::new(*b"APIC", 44, 5, *b"CLOUDH", *b"CHMADT ", 1); + #[cfg(target_arch = "x86_64")] + { + madt.write(36, arch::layout::APIC_START.0); + + for cpu in 0..self.config.max_vcpus { + let lapic = LocalApic { + r#type: acpi::ACPI_APIC_PROCESSOR, + length: 8, + processor_id: cpu, + apic_id: cpu, + flags: if cpu < self.config.boot_vcpus { + 1 << MADT_CPU_ENABLE_FLAG + } else { + 0 + } | 1 << MADT_CPU_ONLINE_CAPABLE_FLAG, + }; + madt.append(lapic); + } + + madt.append(Ioapic { + r#type: acpi::ACPI_APIC_IO, + length: 12, + ioapic_id: 0, + apic_address: arch::layout::IOAPIC_START.0 as u32, + gsi_base: 0, + ..Default::default() + }); + + madt.append(InterruptSourceOverride { + r#type: acpi::ACPI_APIC_XRUPT_OVERRIDE, + length: 10, + bus: 0, + source: 4, + gsi: 4, + flags: 0, + }); + } + + #[cfg(target_arch = "aarch64")] + { + /* Notes: + * Ignore Local Interrupt Controller Address at byte offset 36 of MADT table. + */ + + // See section 5.2.12.14 GIC CPU Interface (GICC) Structure in ACPI spec. + for cpu in 0..self.config.boot_vcpus { + let vcpu = &self.vcpus[cpu as usize]; + let mpidr = vcpu.lock().unwrap().get_mpidr(); + /* ARMv8 MPIDR format: + Bits [63:40] Must be zero + Bits [39:32] Aff3 : Match Aff3 of target processor MPIDR + Bits [31:24] Must be zero + Bits [23:16] Aff2 : Match Aff2 of target processor MPIDR + Bits [15:8] Aff1 : Match Aff1 of target processor MPIDR + Bits [7:0] Aff0 : Match Aff0 of target processor MPIDR + */ + let mpidr_mask = 0xff_00ff_ffff; + let gicc = GicC { + r#type: acpi::ACPI_APIC_GENERIC_CPU_INTERFACE, + length: 80, + reserved0: 0, + cpu_interface_number: cpu as u32, + uid: cpu as u32, + flags: 1, + parking_version: 0, + performance_interrupt: 0, + parked_address: 0, + base_address: 0, + gicv_base_address: 0, + gich_base_address: 0, + vgic_interrupt: 0, + gicr_base_address: 0, + mpidr: mpidr & mpidr_mask, + proc_power_effi_class: 0, + reserved1: 0, + spe_overflow_interrupt: 0, + }; + + madt.append(gicc); + } + let vgic_config = Gic::create_default_config(self.config.boot_vcpus.into()); + + // GIC Distributor structure. See section 5.2.12.15 in ACPI spec. + let gicd = GicD { + r#type: acpi::ACPI_APIC_GENERIC_DISTRIBUTOR, + length: 24, + reserved0: 0, + gic_id: 0, + base_address: vgic_config.dist_addr, + global_irq_base: 0, + version: 3, + reserved1: [0; 3], + }; + madt.append(gicd); + + // See 5.2.12.17 GIC Redistributor (GICR) Structure in ACPI spec. + let gicr = GicR { + r#type: acpi::ACPI_APIC_GENERIC_REDISTRIBUTOR, + length: 16, + reserved: 0, + base_address: vgic_config.redists_addr, + range_length: vgic_config.redists_size as u32, + }; + madt.append(gicr); + + // See 5.2.12.18 GIC Interrupt Translation Service (ITS) Structure in ACPI spec. + let gicits = GicIts { + r#type: acpi::ACPI_APIC_GENERIC_TRANSLATOR, + length: 20, + reserved0: 0, + translation_id: 0, + base_address: vgic_config.msi_addr, + reserved1: 0, + }; + madt.append(gicits); + + madt.update_checksum(); + } + + madt + } + + #[cfg(target_arch = "aarch64")] + pub fn create_pptt(&self) -> Sdt { + let pptt_start = 0; + let mut cpus = 0; + let mut uid = 0; + // If topology is not specified, the default setting is: + // 1 package, multiple cores, 1 thread per core + // This is also the behavior when PPTT is missing. + let (threads_per_core, cores_per_package, packages) = + self.get_vcpu_topology().unwrap_or((1, self.max_vcpus(), 1)); + + let mut pptt = Sdt::new(*b"PPTT", 36, 2, *b"CLOUDH", *b"CHPPTT ", 1); + + for cluster_idx in 0..packages { + if cpus < self.config.boot_vcpus as usize { + let cluster_offset = pptt.len() - pptt_start; + let cluster_hierarchy_node = ProcessorHierarchyNode { + r#type: 0, + length: 20, + reserved: 0, + flags: 0x2, + parent: 0, + acpi_processor_id: cluster_idx as u32, + num_private_resources: 0, + }; + pptt.append(cluster_hierarchy_node); + + for core_idx in 0..cores_per_package { + let core_offset = pptt.len() - pptt_start; + + if threads_per_core > 1 { + let core_hierarchy_node = ProcessorHierarchyNode { + r#type: 0, + length: 20, + reserved: 0, + flags: 0x2, + parent: cluster_offset as u32, + acpi_processor_id: core_idx as u32, + num_private_resources: 0, + }; + pptt.append(core_hierarchy_node); + + for _thread_idx in 0..threads_per_core { + let thread_hierarchy_node = ProcessorHierarchyNode { + r#type: 0, + length: 20, + reserved: 0, + flags: 0xE, + parent: core_offset as u32, + acpi_processor_id: uid as u32, + num_private_resources: 0, + }; + pptt.append(thread_hierarchy_node); + uid += 1; + } + } else { + let thread_hierarchy_node = ProcessorHierarchyNode { + r#type: 0, + length: 20, + reserved: 0, + flags: 0xA, + parent: cluster_offset as u32, + acpi_processor_id: uid as u32, + num_private_resources: 0, + }; + pptt.append(thread_hierarchy_node); + uid += 1; + } + } + cpus += (cores_per_package * threads_per_core) as usize; + } + } + + pptt.update_checksum(); + pptt + } + + #[cfg(feature = "guest_debug")] + fn get_regs(&self, cpu_id: u8) -> Result { + self.vcpus[usize::from(cpu_id)] + .lock() + .unwrap() + .vcpu + .get_regs() + .map_err(Error::CpuDebug) + } + + #[cfg(feature = "guest_debug")] + fn set_regs(&self, cpu_id: u8, regs: &StandardRegisters) -> Result<()> { + self.vcpus[usize::from(cpu_id)] + .lock() + .unwrap() + .vcpu + .set_regs(regs) + .map_err(Error::CpuDebug) + } + + #[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] + fn get_sregs(&self, cpu_id: u8) -> Result { + self.vcpus[usize::from(cpu_id)] + .lock() + .unwrap() + .vcpu + .get_sregs() + .map_err(Error::CpuDebug) + } + + #[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] + fn set_sregs(&self, cpu_id: u8, sregs: &SpecialRegisters) -> Result<()> { + self.vcpus[usize::from(cpu_id)] + .lock() + .unwrap() + .vcpu + .set_sregs(sregs) + .map_err(Error::CpuDebug) + } + + #[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] + fn translate_gva(&self, cpu_id: u8, gva: u64) -> Result { + let (gpa, _) = self.vcpus[usize::from(cpu_id)] + .lock() + .unwrap() + .vcpu + .translate_gva(gva, /* flags: unused */ 0) + .map_err(|e| Error::TranslateVirtualAddress(e.into()))?; + Ok(gpa) + } + + /// + /// On AArch64, `translate_gva` API is not provided by KVM. We implemented + /// it in VMM by walking through translation tables. + /// + /// Address translation is big topic, here we only focus the scenario that + /// happens in VMM while debugging kernel. This `translate_gva` + /// implementation is restricted to: + /// - Exception Level 1 + /// - Translate high address range only (kernel space) + /// + /// This implementation supports following Arm-v8a features related to + /// address translation: + /// - FEAT_LPA + /// - FEAT_LVA + /// - FEAT_LPA2 + /// + #[cfg(all(target_arch = "aarch64", feature = "guest_debug"))] + fn translate_gva(&self, cpu_id: u8, gva: u64) -> Result { + let tcr_el1: u64 = self.vcpus[usize::from(cpu_id)] + .lock() + .unwrap() + .vcpu + .get_sys_reg(regs::TCR_EL1) + .map_err(|e| Error::TranslateVirtualAddress(e.into()))?; + let ttbr1_el1: u64 = self.vcpus[usize::from(cpu_id)] + .lock() + .unwrap() + .vcpu + .get_sys_reg(regs::TTBR1_EL1) + .map_err(|e| Error::TranslateVirtualAddress(e.into()))?; + let id_aa64mmfr0_el1: u64 = self.vcpus[usize::from(cpu_id)] + .lock() + .unwrap() + .vcpu + .get_sys_reg(regs::ID_AA64MMFR0_EL1) + .map_err(|e| Error::TranslateVirtualAddress(e.into()))?; + + // Bit 55 of the VA determines the range, high (0xFFFxxx...) + // or low (0x000xxx...). + let high_range = extract_bits_64!(gva, 55, 1); + if high_range == 0 { + info!("VA (0x{:x}) range is not supported!", gva); + return Ok(gva); + } + + // High range size offset + let tsz = extract_bits_64!(tcr_el1, 16, 6); + // Granule size + let tg = extract_bits_64!(tcr_el1, 30, 2); + // Indication of 48-bits (0) or 52-bits (1) for FEAT_LPA2 + let ds = extract_bits_64!(tcr_el1, 59, 1); + + if tsz == 0 { + info!("VA translation is not ready!"); + return Ok(gva); + } + + // VA size is determined by TCR_BL1.T1SZ + let va_size = 64 - tsz; + // Number of bits in VA consumed in each level of translation + let stride = match tg { + 3 => 13, // 64KB granule size + 1 => 11, // 16KB granule size + _ => 9, // 4KB, default + }; + // Starting level of walking + let mut level = 4 - (va_size - 4) / stride; + + // PA or IPA size is determined + let tcr_ips = extract_bits_64!(tcr_el1, 32, 3); + #[allow(clippy::identity_op)] + let pa_range = extract_bits_64!(id_aa64mmfr0_el1, 0, 4); + // The IPA size in TCR_BL1 and PA Range in ID_AA64MMFR0_EL1 should match. + // To be safe, we use the minimum value if they are different. + let pa_range = std::cmp::min(tcr_ips, pa_range); + // PA size in bits + let pa_size = match pa_range { + 0 => 32, + 1 => 36, + 2 => 40, + 3 => 42, + 4 => 44, + 5 => 48, + 6 => 52, + _ => { + return Err(Error::TranslateVirtualAddress(anyhow!(format!( + "PA range not supported {}", + pa_range + )))) + } + }; + + let indexmask_grainsize = (!0u64) >> (64 - (stride + 3)); + let mut indexmask = (!0u64) >> (64 - (va_size - (stride * (4 - level)))); + // If FEAT_LPA2 is present, the translation table descriptor holds + // 50 bits of the table address of next level. + // Otherwise, it is 48 bits. + let descaddrmask = if ds == 1 { + !0u64 >> (64 - 50) // mask with 50 least significant bits + } else { + !0u64 >> (64 - 48) // mask with 48 least significant bits + }; + let descaddrmask = descaddrmask & !indexmask_grainsize; + + // Translation table base address + #[allow(clippy::identity_op)] + let mut descaddr: u64 = extract_bits_64!(ttbr1_el1, 0, 48); + // In the case of FEAT_LPA and FEAT_LPA2, the initial translation table + // addresss bits [48:51] comes from TTBR1_EL1 bits [2:5]. + if pa_size == 52 { + descaddr |= extract_bits_64!(ttbr1_el1, 2, 4) << 48; + } + + // Loop through tables of each level + loop { + // Table offset for current level + let table_offset: u64 = (gva >> (stride * (4 - level))) & indexmask; + descaddr |= table_offset; + descaddr &= !7u64; + + let mut buf = [0; 8]; + self.vm_memory + .memory() + .read(&mut buf, GuestAddress(descaddr)) + .map_err(|e| Error::TranslateVirtualAddress(e.into()))?; + let descriptor = u64::from_le_bytes(buf); + + descaddr = descriptor & descaddrmask; + // In the case of FEAT_LPA, the next-level translation table address + // bits [48:51] comes from bits [12:15] of the current descriptor. + // For FEAT_LPA2, the next-level translation table address + // bits [50:51] comes from bits [8:9] of the current descriptor, + // bits [48:49] comes from bits [48:49] of the descriptor which was + // handled previously. + if pa_size == 52 { + if ds == 1 { + // FEAT_LPA2 + descaddr |= extract_bits_64!(descriptor, 8, 2) << 50; + } else { + // FEAT_LPA + descaddr |= extract_bits_64!(descriptor, 12, 4) << 48; + } + } + + if (descriptor & 2) != 0 && (level < 3) { + // This is a table entry. Go down to next level. + level += 1; + indexmask = indexmask_grainsize; + continue; + } + + break; + } + + // We have reached either: + // - a page entry at level 3 or + // - a block entry at level 1 or 2 + let page_size = 1u64 << ((stride * (4 - level)) + 3); + descaddr &= !(page_size - 1); + descaddr |= gva & (page_size - 1); + + Ok(descaddr) + } +} + +struct Cpu { + cpu_id: u8, + proximity_domain: u32, + dynamic: bool, +} + +#[cfg(target_arch = "x86_64")] +const MADT_CPU_ENABLE_FLAG: usize = 0; + +#[cfg(target_arch = "x86_64")] +const MADT_CPU_ONLINE_CAPABLE_FLAG: usize = 1; + +impl Cpu { + #[cfg(target_arch = "x86_64")] + fn generate_mat(&self) -> Vec { + let lapic = LocalApic { + r#type: 0, + length: 8, + processor_id: self.cpu_id, + apic_id: self.cpu_id, + flags: 1 << MADT_CPU_ENABLE_FLAG, + }; + + let mut mat_data: Vec = vec![0; std::mem::size_of_val(&lapic)]; + // SAFETY: mat_data is large enough to hold lapic + unsafe { *(mat_data.as_mut_ptr() as *mut LocalApic) = lapic }; + + mat_data + } +} + +impl Aml for Cpu { + fn append_aml_bytes(&self, bytes: &mut Vec) { + #[cfg(target_arch = "x86_64")] + let mat_data: Vec = self.generate_mat(); + #[allow(clippy::if_same_then_else)] + if self.dynamic { + aml::Device::new( + format!("C{:03}", self.cpu_id).as_str().into(), + vec![ + &aml::Name::new("_HID".into(), &"ACPI0007"), + &aml::Name::new("_UID".into(), &self.cpu_id), + // Currently, AArch64 cannot support following fields. + /* + _STA return value: + Bit [0] – Set if the device is present. + Bit [1] – Set if the device is enabled and decoding its resources. + Bit [2] – Set if the device should be shown in the UI. + Bit [3] – Set if the device is functioning properly (cleared if device failed its diagnostics). + Bit [4] – Set if the battery is present. + Bits [31:5] – Reserved (must be cleared). + */ + #[cfg(target_arch = "x86_64")] + &aml::Method::new( + "_STA".into(), + 0, + false, + // Call into CSTA method which will interrogate device + vec![&aml::Return::new(&aml::MethodCall::new( + "CSTA".into(), + vec![&self.cpu_id], + ))], + ), + &aml::Method::new( + "_PXM".into(), + 0, + false, + vec![&aml::Return::new(&self.proximity_domain)], + ), + // The Linux kernel expects every CPU device to have a _MAT entry + // containing the LAPIC for this processor with the enabled bit set + // even it if is disabled in the MADT (non-boot CPU) + #[cfg(target_arch = "x86_64")] + &aml::Name::new("_MAT".into(), &aml::Buffer::new(mat_data)), + // Trigger CPU ejection + #[cfg(target_arch = "x86_64")] + &aml::Method::new( + "_EJ0".into(), + 1, + false, + // Call into CEJ0 method which will actually eject device + vec![&aml::MethodCall::new("CEJ0".into(), vec![&self.cpu_id])], + ), + ], + ) + .append_aml_bytes(bytes); + } else { + aml::Device::new( + format!("C{:03}", self.cpu_id).as_str().into(), + vec![ + &aml::Name::new("_HID".into(), &"ACPI0007"), + &aml::Name::new("_UID".into(), &self.cpu_id), + #[cfg(target_arch = "x86_64")] + &aml::Method::new( + "_STA".into(), + 0, + false, + // Mark CPU present see CSTA implementation + vec![&aml::Return::new(&0xfu8)], + ), + &aml::Method::new( + "_PXM".into(), + 0, + false, + vec![&aml::Return::new(&self.proximity_domain)], + ), + // The Linux kernel expects every CPU device to have a _MAT entry + // containing the LAPIC for this processor with the enabled bit set + // even it if is disabled in the MADT (non-boot CPU) + #[cfg(target_arch = "x86_64")] + &aml::Name::new("_MAT".into(), &aml::Buffer::new(mat_data)), + ], + ) + .append_aml_bytes(bytes); + } + } +} + +struct CpuNotify { + cpu_id: u8, +} + +impl Aml for CpuNotify { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let object = aml::Path::new(&format!("C{:03}", self.cpu_id)); + aml::If::new( + &aml::Equal::new(&aml::Arg(0), &self.cpu_id), + vec![&aml::Notify::new(&object, &aml::Arg(1))], + ) + .append_aml_bytes(bytes) + } +} + +struct CpuMethods { + max_vcpus: u8, + dynamic: bool, +} + +impl Aml for CpuMethods { + fn append_aml_bytes(&self, bytes: &mut Vec) { + if self.dynamic { + // CPU status method + aml::Method::new( + "CSTA".into(), + 1, + true, + vec![ + // Take lock defined above + &aml::Acquire::new("\\_SB_.PRES.CPLK".into(), 0xffff), + // Write CPU number (in first argument) to I/O port via field + &aml::Store::new(&aml::Path::new("\\_SB_.PRES.CSEL"), &aml::Arg(0)), + &aml::Store::new(&aml::Local(0), &aml::ZERO), + // Check if CPEN bit is set, if so make the local variable 0xf (see _STA for details of meaning) + &aml::If::new( + &aml::Equal::new(&aml::Path::new("\\_SB_.PRES.CPEN"), &aml::ONE), + vec![&aml::Store::new(&aml::Local(0), &0xfu8)], + ), + // Release lock + &aml::Release::new("\\_SB_.PRES.CPLK".into()), + // Return 0 or 0xf + &aml::Return::new(&aml::Local(0)), + ], + ) + .append_aml_bytes(bytes); + + let mut cpu_notifies = Vec::new(); + for cpu_id in 0..self.max_vcpus { + cpu_notifies.push(CpuNotify { cpu_id }); + } + + let mut cpu_notifies_refs: Vec<&dyn aml::Aml> = Vec::new(); + for cpu_id in 0..self.max_vcpus { + cpu_notifies_refs.push(&cpu_notifies[usize::from(cpu_id)]); + } + + aml::Method::new("CTFY".into(), 2, true, cpu_notifies_refs).append_aml_bytes(bytes); + + aml::Method::new( + "CEJ0".into(), + 1, + true, + vec![ + &aml::Acquire::new("\\_SB_.PRES.CPLK".into(), 0xffff), + // Write CPU number (in first argument) to I/O port via field + &aml::Store::new(&aml::Path::new("\\_SB_.PRES.CSEL"), &aml::Arg(0)), + // Set CEJ0 bit + &aml::Store::new(&aml::Path::new("\\_SB_.PRES.CEJ0"), &aml::ONE), + &aml::Release::new("\\_SB_.PRES.CPLK".into()), + ], + ) + .append_aml_bytes(bytes); + + aml::Method::new( + "CSCN".into(), + 0, + true, + vec![ + // Take lock defined above + &aml::Acquire::new("\\_SB_.PRES.CPLK".into(), 0xffff), + &aml::Store::new(&aml::Local(0), &aml::ZERO), + &aml::While::new( + &aml::LessThan::new(&aml::Local(0), &self.max_vcpus), + vec![ + // Write CPU number (in first argument) to I/O port via field + &aml::Store::new(&aml::Path::new("\\_SB_.PRES.CSEL"), &aml::Local(0)), + // Check if CINS bit is set + &aml::If::new( + &aml::Equal::new(&aml::Path::new("\\_SB_.PRES.CINS"), &aml::ONE), + // Notify device if it is + vec![ + &aml::MethodCall::new( + "CTFY".into(), + vec![&aml::Local(0), &aml::ONE], + ), + // Reset CINS bit + &aml::Store::new( + &aml::Path::new("\\_SB_.PRES.CINS"), + &aml::ONE, + ), + ], + ), + // Check if CRMV bit is set + &aml::If::new( + &aml::Equal::new(&aml::Path::new("\\_SB_.PRES.CRMV"), &aml::ONE), + // Notify device if it is (with the eject constant 0x3) + vec![ + &aml::MethodCall::new( + "CTFY".into(), + vec![&aml::Local(0), &3u8], + ), + // Reset CRMV bit + &aml::Store::new( + &aml::Path::new("\\_SB_.PRES.CRMV"), + &aml::ONE, + ), + ], + ), + &aml::Add::new(&aml::Local(0), &aml::Local(0), &aml::ONE), + ], + ), + // Release lock + &aml::Release::new("\\_SB_.PRES.CPLK".into()), + ], + ) + .append_aml_bytes(bytes) + } else { + aml::Method::new("CSCN".into(), 0, true, vec![]).append_aml_bytes(bytes) + } + } +} + +impl Aml for CpuManager { + fn append_aml_bytes(&self, bytes: &mut Vec) { + #[cfg(target_arch = "x86_64")] + if let Some(acpi_address) = self.acpi_address { + // CPU hotplug controller + aml::Device::new( + "_SB_.PRES".into(), + vec![ + &aml::Name::new("_HID".into(), &aml::EisaName::new("PNP0A06")), + &aml::Name::new("_UID".into(), &"CPU Hotplug Controller"), + // Mutex to protect concurrent access as we write to choose CPU and then read back status + &aml::Mutex::new("CPLK".into(), 0), + &aml::Name::new( + "_CRS".into(), + &aml::ResourceTemplate::new(vec![&aml::AddressSpace::new_memory( + aml::AddressSpaceCachable::NotCacheable, + true, + acpi_address.0, + acpi_address.0 + CPU_MANAGER_ACPI_SIZE as u64 - 1, + )]), + ), + // OpRegion and Fields map MMIO range into individual field values + &aml::OpRegion::new( + "PRST".into(), + aml::OpRegionSpace::SystemMemory, + acpi_address.0 as usize, + CPU_MANAGER_ACPI_SIZE, + ), + &aml::Field::new( + "PRST".into(), + aml::FieldAccessType::Byte, + aml::FieldUpdateRule::WriteAsZeroes, + vec![ + aml::FieldEntry::Reserved(32), + aml::FieldEntry::Named(*b"CPEN", 1), + aml::FieldEntry::Named(*b"CINS", 1), + aml::FieldEntry::Named(*b"CRMV", 1), + aml::FieldEntry::Named(*b"CEJ0", 1), + aml::FieldEntry::Reserved(4), + aml::FieldEntry::Named(*b"CCMD", 8), + ], + ), + &aml::Field::new( + "PRST".into(), + aml::FieldAccessType::DWord, + aml::FieldUpdateRule::Preserve, + vec![ + aml::FieldEntry::Named(*b"CSEL", 32), + aml::FieldEntry::Reserved(32), + aml::FieldEntry::Named(*b"CDAT", 32), + ], + ), + ], + ) + .append_aml_bytes(bytes); + } + + // CPU devices + let hid = aml::Name::new("_HID".into(), &"ACPI0010"); + let uid = aml::Name::new("_CID".into(), &aml::EisaName::new("PNP0A05")); + // Bundle methods together under a common object + let methods = CpuMethods { + max_vcpus: self.config.max_vcpus, + dynamic: self.dynamic, + }; + let mut cpu_data_inner: Vec<&dyn aml::Aml> = vec![&hid, &uid, &methods]; + + let mut cpu_devices = Vec::new(); + for cpu_id in 0..self.config.max_vcpus { + let proximity_domain = *self.proximity_domain_per_cpu.get(&cpu_id).unwrap_or(&0); + let cpu_device = Cpu { + cpu_id, + proximity_domain, + dynamic: self.dynamic, + }; + + cpu_devices.push(cpu_device); + } + + for cpu_device in cpu_devices.iter() { + cpu_data_inner.push(cpu_device); + } + + aml::Device::new("_SB_.CPUS".into(), cpu_data_inner).append_aml_bytes(bytes) + } +} + +impl Pausable for CpuManager { + fn pause(&mut self) -> std::result::Result<(), MigratableError> { + // Tell the vCPUs to pause themselves next time they exit + self.vcpus_pause_signalled.store(true, Ordering::SeqCst); + + // Signal to the spawned threads (vCPUs and console signal handler). For the vCPU threads + // this will interrupt the KVM_RUN ioctl() allowing the loop to check the boolean set + // above. + for state in self.vcpu_states.iter() { + state.signal_thread(); + } + + for vcpu in self.vcpus.iter() { + let mut vcpu = vcpu.lock().unwrap(); + vcpu.pause()?; + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + if !self.config.kvm_hyperv { + vcpu.vcpu.notify_guest_clock_paused().map_err(|e| { + MigratableError::Pause(anyhow!( + "Could not notify guest it has been paused {:?}", + e + )) + })?; + } + } + + Ok(()) + } + + fn resume(&mut self) -> std::result::Result<(), MigratableError> { + for vcpu in self.vcpus.iter() { + vcpu.lock().unwrap().resume()?; + } + + // Toggle the vCPUs pause boolean + self.vcpus_pause_signalled.store(false, Ordering::SeqCst); + + // Unpark all the VCPU threads. + // Once unparked, the next thing they will do is checking for the pause + // boolean. Since it'll be set to false, they will exit their pause loop + // and go back to vmx root. + for state in self.vcpu_states.iter() { + state.unpark_thread(); + } + Ok(()) + } +} + +impl Snapshottable for CpuManager { + fn id(&self) -> String { + CPU_MANAGER_SNAPSHOT_ID.to_string() + } + + fn snapshot(&mut self) -> std::result::Result { + let mut cpu_manager_snapshot = Snapshot::new(CPU_MANAGER_SNAPSHOT_ID); + + // The CpuManager snapshot is a collection of all vCPUs snapshots. + for vcpu in &self.vcpus { + let cpu_snapshot = vcpu.lock().unwrap().snapshot()?; + cpu_manager_snapshot.add_snapshot(cpu_snapshot); + } + + Ok(cpu_manager_snapshot) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + for (cpu_id, snapshot) in snapshot.snapshots.iter() { + info!("Restoring VCPU {}", cpu_id); + self.create_vcpu(cpu_id.parse::().unwrap(), None, Some(*snapshot.clone())) + .map_err(|e| MigratableError::Restore(anyhow!("Could not create vCPU {:?}", e)))?; + } + + // Reset tsc msrs for all vcpu, so that KVM could synchronize TSC + // for restored VM, before vcpu run. + #[cfg(target_arch = "x86_64")] + { + for vcpu in &self.vcpus { + let vcpu = vcpu.lock().unwrap(); + let _ = vcpu.vcpu.set_msrs(&vcpu.tsc_msrs); + } + } + + Ok(()) + } +} + +impl Transportable for CpuManager {} +impl Migratable for CpuManager {} + +#[cfg(feature = "guest_debug")] +impl Debuggable for CpuManager { + #[cfg(feature = "kvm")] + fn set_guest_debug( + &self, + cpu_id: usize, + addrs: &[GuestAddress], + singlestep: bool, + ) -> std::result::Result<(), DebuggableError> { + self.vcpus[cpu_id] + .lock() + .unwrap() + .vcpu + .set_guest_debug(addrs, singlestep) + .map_err(DebuggableError::SetDebug) + } + + fn debug_pause(&mut self) -> std::result::Result<(), DebuggableError> { + Ok(()) + } + + fn debug_resume(&mut self) -> std::result::Result<(), DebuggableError> { + Ok(()) + } + + #[cfg(target_arch = "x86_64")] + fn read_regs(&self, cpu_id: usize) -> std::result::Result { + // General registers: RAX, RBX, RCX, RDX, RSI, RDI, RBP, RSP, r8-r15 + let gregs = self + .get_regs(cpu_id as u8) + .map_err(DebuggableError::ReadRegs)?; + let regs = [ + gregs.rax, gregs.rbx, gregs.rcx, gregs.rdx, gregs.rsi, gregs.rdi, gregs.rbp, gregs.rsp, + gregs.r8, gregs.r9, gregs.r10, gregs.r11, gregs.r12, gregs.r13, gregs.r14, gregs.r15, + ]; + + // GDB exposes 32-bit eflags instead of 64-bit rflags. + // https://github.com/bminor/binutils-gdb/blob/master/gdb/features/i386/64bit-core.xml + let eflags = gregs.rflags as u32; + let rip = gregs.rip; + + // Segment registers: CS, SS, DS, ES, FS, GS + let sregs = self + .get_sregs(cpu_id as u8) + .map_err(DebuggableError::ReadRegs)?; + let segments = X86SegmentRegs { + cs: sregs.cs.selector as u32, + ss: sregs.ss.selector as u32, + ds: sregs.ds.selector as u32, + es: sregs.es.selector as u32, + fs: sregs.fs.selector as u32, + gs: sregs.gs.selector as u32, + }; + + // TODO: Add other registers + + Ok(CoreRegs { + regs, + eflags, + rip, + segments, + ..Default::default() + }) + } + + #[cfg(target_arch = "aarch64")] + fn read_regs(&self, cpu_id: usize) -> std::result::Result { + let gregs = self + .get_regs(cpu_id as u8) + .map_err(DebuggableError::ReadRegs)?; + Ok(CoreRegs { + x: gregs.regs.regs, + sp: gregs.regs.sp, + pc: gregs.regs.pc, + ..Default::default() + }) + } + + #[cfg(target_arch = "x86_64")] + fn write_regs( + &self, + cpu_id: usize, + regs: &CoreRegs, + ) -> std::result::Result<(), DebuggableError> { + let orig_gregs = self + .get_regs(cpu_id as u8) + .map_err(DebuggableError::ReadRegs)?; + let gregs = StandardRegisters { + rax: regs.regs[0], + rbx: regs.regs[1], + rcx: regs.regs[2], + rdx: regs.regs[3], + rsi: regs.regs[4], + rdi: regs.regs[5], + rbp: regs.regs[6], + rsp: regs.regs[7], + r8: regs.regs[8], + r9: regs.regs[9], + r10: regs.regs[10], + r11: regs.regs[11], + r12: regs.regs[12], + r13: regs.regs[13], + r14: regs.regs[14], + r15: regs.regs[15], + rip: regs.rip, + // Update the lower 32-bit of rflags. + rflags: (orig_gregs.rflags & !(u32::MAX as u64)) | (regs.eflags as u64), + }; + + self.set_regs(cpu_id as u8, &gregs) + .map_err(DebuggableError::WriteRegs)?; + + // Segment registers: CS, SS, DS, ES, FS, GS + // Since GDB care only selectors, we call get_sregs() first. + let mut sregs = self + .get_sregs(cpu_id as u8) + .map_err(DebuggableError::ReadRegs)?; + sregs.cs.selector = regs.segments.cs as u16; + sregs.ss.selector = regs.segments.ss as u16; + sregs.ds.selector = regs.segments.ds as u16; + sregs.es.selector = regs.segments.es as u16; + sregs.fs.selector = regs.segments.fs as u16; + sregs.gs.selector = regs.segments.gs as u16; + + self.set_sregs(cpu_id as u8, &sregs) + .map_err(DebuggableError::WriteRegs)?; + + // TODO: Add other registers + + Ok(()) + } + + #[cfg(target_arch = "aarch64")] + fn write_regs( + &self, + cpu_id: usize, + regs: &CoreRegs, + ) -> std::result::Result<(), DebuggableError> { + let mut gregs = self + .get_regs(cpu_id as u8) + .map_err(DebuggableError::ReadRegs)?; + + gregs.regs.regs = regs.x; + gregs.regs.sp = regs.sp; + gregs.regs.pc = regs.pc; + + self.set_regs(cpu_id as u8, &gregs) + .map_err(DebuggableError::WriteRegs)?; + + Ok(()) + } + + fn read_mem( + &self, + cpu_id: usize, + vaddr: GuestAddress, + len: usize, + ) -> std::result::Result, DebuggableError> { + let mut buf = vec![0; len]; + let mut total_read = 0_u64; + + while total_read < len as u64 { + let gaddr = vaddr.0 + total_read; + let paddr = match self.translate_gva(cpu_id as u8, gaddr) { + Ok(paddr) => paddr, + Err(_) if gaddr == u64::MIN => gaddr, // Silently return GVA as GPA if GVA == 0. + Err(e) => return Err(DebuggableError::TranslateGva(e)), + }; + let psize = arch::PAGE_SIZE as u64; + let read_len = std::cmp::min(len as u64 - total_read, psize - (paddr & (psize - 1))); + self.vm_memory + .memory() + .read( + &mut buf[total_read as usize..total_read as usize + read_len as usize], + GuestAddress(paddr), + ) + .map_err(DebuggableError::ReadMem)?; + total_read += read_len; + } + Ok(buf) + } + + fn write_mem( + &self, + cpu_id: usize, + vaddr: &GuestAddress, + data: &[u8], + ) -> std::result::Result<(), DebuggableError> { + let mut total_written = 0_u64; + + while total_written < data.len() as u64 { + let gaddr = vaddr.0 + total_written; + let paddr = match self.translate_gva(cpu_id as u8, gaddr) { + Ok(paddr) => paddr, + Err(_) if gaddr == u64::MIN => gaddr, // Silently return GVA as GPA if GVA == 0. + Err(e) => return Err(DebuggableError::TranslateGva(e)), + }; + let psize = arch::PAGE_SIZE as u64; + let write_len = std::cmp::min( + data.len() as u64 - total_written, + psize - (paddr & (psize - 1)), + ); + self.vm_memory + .memory() + .write( + &data[total_written as usize..total_written as usize + write_len as usize], + GuestAddress(paddr), + ) + .map_err(DebuggableError::WriteMem)?; + total_written += write_len; + } + Ok(()) + } + + fn active_vcpus(&self) -> usize { + self.present_vcpus() as usize + } +} + +#[cfg(feature = "guest_debug")] +impl Elf64Writable for CpuManager {} + +#[cfg(feature = "guest_debug")] +impl CpuElf64Writable for CpuManager { + fn cpu_write_elf64_note( + &mut self, + dump_state: &DumpState, + ) -> std::result::Result<(), GuestDebuggableError> { + let mut coredump_file = dump_state.file.as_ref().unwrap(); + for vcpu in &self.vcpus { + let note_size = self.get_note_size(NoteDescType::Elf, 1); + let mut pos: usize = 0; + let mut buf = vec![0; note_size as usize]; + let descsz = size_of::(); + let vcpu_id = vcpu.lock().unwrap().id; + + let note = Elf64_Nhdr { + n_namesz: COREDUMP_NAME_SIZE, + n_descsz: descsz as u32, + n_type: NT_PRSTATUS, + }; + + let bytes: &[u8] = note.as_slice(); + buf.splice(0.., bytes.to_vec()); + pos += round_up!(size_of::(), 4); + buf.resize(pos + 4, 0); + buf.splice(pos.., "CORE".to_string().into_bytes()); + + pos += round_up!(COREDUMP_NAME_SIZE as usize, 4); + buf.resize(pos + 32 + 4, 0); + let pid = vcpu_id as u64; + let bytes: &[u8] = pid.as_slice(); + buf.splice(pos + 32.., bytes.to_vec()); /* pr_pid */ + + pos += descsz - size_of::() - size_of::(); + + let orig_rax: u64 = 0; + let gregs = self.vcpus[usize::from(vcpu_id)] + .lock() + .unwrap() + .vcpu + .get_regs() + .map_err(|_e| GuestDebuggableError::Coredump(anyhow!("get regs failed")))?; + + let regs1 = [ + gregs.r15, gregs.r14, gregs.r13, gregs.r12, gregs.rbp, gregs.rbx, gregs.r11, + gregs.r10, + ]; + let regs2 = [ + gregs.r9, gregs.r8, gregs.rax, gregs.rcx, gregs.rdx, gregs.rsi, gregs.rdi, orig_rax, + ]; + + let sregs = self.vcpus[usize::from(vcpu_id)] + .lock() + .unwrap() + .vcpu + .get_sregs() + .map_err(|_e| GuestDebuggableError::Coredump(anyhow!("get sregs failed")))?; + + debug!( + "rip 0x{:x} rsp 0x{:x} gs 0x{:x} cs 0x{:x} ss 0x{:x} ds 0x{:x}", + gregs.rip, + gregs.rsp, + sregs.gs.base, + sregs.cs.selector, + sregs.ss.selector, + sregs.ds.selector, + ); + + let regs = X86_64UserRegs { + regs1, + regs2, + rip: gregs.rip, + cs: sregs.cs.selector as u64, + eflags: gregs.rflags, + rsp: gregs.rsp, + ss: sregs.ss.selector as u64, + fs_base: sregs.fs.base, + gs_base: sregs.gs.base, + ds: sregs.ds.selector as u64, + es: sregs.es.selector as u64, + fs: sregs.fs.selector as u64, + gs: sregs.gs.selector as u64, + }; + + // let bytes: &[u8] = unsafe { any_as_u8_slice(®s) }; + let bytes: &[u8] = regs.as_slice(); + buf.resize(note_size as usize, 0); + buf.splice(pos.., bytes.to_vec()); + buf.resize(note_size as usize, 0); + + coredump_file + .write(&buf) + .map_err(GuestDebuggableError::CoredumpFile)?; + } + + Ok(()) + } + + fn cpu_write_vmm_note( + &mut self, + dump_state: &DumpState, + ) -> std::result::Result<(), GuestDebuggableError> { + let mut coredump_file = dump_state.file.as_ref().unwrap(); + for vcpu in &self.vcpus { + let note_size = self.get_note_size(NoteDescType::Vmm, 1); + let mut pos: usize = 0; + let mut buf = vec![0; note_size as usize]; + let descsz = size_of::(); + let vcpu_id = vcpu.lock().unwrap().id; + + let note = Elf64_Nhdr { + n_namesz: COREDUMP_NAME_SIZE, + n_descsz: descsz as u32, + n_type: 0, + }; + + let bytes: &[u8] = note.as_slice(); + buf.splice(0.., bytes.to_vec()); + pos += round_up!(size_of::(), 4); + + buf.resize(pos + 4, 0); + buf.splice(pos.., "QEMU".to_string().into_bytes()); + + pos += round_up!(COREDUMP_NAME_SIZE as usize, 4); + + let gregs = self.vcpus[usize::from(vcpu_id)] + .lock() + .unwrap() + .vcpu + .get_regs() + .map_err(|_e| GuestDebuggableError::Coredump(anyhow!("get regs failed")))?; + + let regs1 = [ + gregs.rax, gregs.rbx, gregs.rcx, gregs.rdx, gregs.rsi, gregs.rdi, gregs.rsp, + gregs.rbp, + ]; + + let regs2 = [ + gregs.r8, gregs.r9, gregs.r10, gregs.r11, gregs.r12, gregs.r13, gregs.r14, + gregs.r15, + ]; + + let sregs = self.vcpus[usize::from(vcpu_id)] + .lock() + .unwrap() + .vcpu + .get_sregs() + .map_err(|_e| GuestDebuggableError::Coredump(anyhow!("get sregs failed")))?; + + let mut msrs = vec![MsrEntry { + index: msr_index::MSR_KERNEL_GS_BASE, + ..Default::default() + }]; + + self.vcpus[vcpu_id as usize] + .lock() + .unwrap() + .vcpu + .get_msrs(&mut msrs) + .map_err(|_e| GuestDebuggableError::Coredump(anyhow!("get msr failed")))?; + let kernel_gs_base = msrs[0].data; + + let cs = CpuSegment::new(sregs.cs); + let ds = CpuSegment::new(sregs.ds); + let es = CpuSegment::new(sregs.es); + let fs = CpuSegment::new(sregs.fs); + let gs = CpuSegment::new(sregs.gs); + let ss = CpuSegment::new(sregs.ss); + let ldt = CpuSegment::new(sregs.ldt); + let tr = CpuSegment::new(sregs.tr); + let gdt = CpuSegment::new_from_table(sregs.gdt); + let idt = CpuSegment::new_from_table(sregs.idt); + let cr = [sregs.cr0, sregs.cr8, sregs.cr2, sregs.cr3, sregs.cr4]; + let regs = DumpCpusState { + version: 1, + size: size_of::() as u32, + regs1, + regs2, + rip: gregs.rip, + rflags: gregs.rflags, + cs, + ds, + es, + fs, + gs, + ss, + ldt, + tr, + gdt, + idt, + cr, + kernel_gs_base, + }; + + let bytes: &[u8] = regs.as_slice(); + buf.resize(note_size as usize, 0); + buf.splice(pos.., bytes.to_vec()); + buf.resize(note_size as usize, 0); + + coredump_file + .write(&buf) + .map_err(GuestDebuggableError::CoredumpFile)?; + } + + Ok(()) + } +} + +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +#[cfg(test)] +mod tests { + use arch::x86_64::interrupts::*; + use arch::x86_64::regs::*; + use hypervisor::arch::x86::{FpuState, LapicState, StandardRegisters}; + + #[test] + fn test_setlint() { + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().expect("new VM fd creation failed"); + assert!(hv.check_required_extensions().is_ok()); + // Calling get_lapic will fail if there is no irqchip before hand. + assert!(vm.create_irq_chip().is_ok()); + let vcpu = vm.create_vcpu(0, None).unwrap(); + let klapic_before: LapicState = vcpu.get_lapic().unwrap(); + + // Compute the value that is expected to represent LVT0 and LVT1. + let lint0 = klapic_before.get_klapic_reg(APIC_LVT0); + let lint1 = klapic_before.get_klapic_reg(APIC_LVT1); + let lint0_mode_expected = set_apic_delivery_mode(lint0, APIC_MODE_EXTINT); + let lint1_mode_expected = set_apic_delivery_mode(lint1, APIC_MODE_NMI); + + set_lint(&vcpu).unwrap(); + + // Compute the value that represents LVT0 and LVT1 after set_lint. + let klapic_actual: LapicState = vcpu.get_lapic().unwrap(); + let lint0_mode_actual = klapic_actual.get_klapic_reg(APIC_LVT0); + let lint1_mode_actual = klapic_actual.get_klapic_reg(APIC_LVT1); + assert_eq!(lint0_mode_expected, lint0_mode_actual); + assert_eq!(lint1_mode_expected, lint1_mode_actual); + } + + #[test] + fn test_setup_fpu() { + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().expect("new VM fd creation failed"); + let vcpu = vm.create_vcpu(0, None).unwrap(); + setup_fpu(&vcpu).unwrap(); + + let expected_fpu: FpuState = FpuState { + fcw: 0x37f, + mxcsr: 0x1f80, + ..Default::default() + }; + let actual_fpu: FpuState = vcpu.get_fpu().unwrap(); + // TODO: auto-generate kvm related structures with PartialEq on. + assert_eq!(expected_fpu.fcw, actual_fpu.fcw); + // Setting the mxcsr register from FpuState inside setup_fpu does not influence anything. + // See 'kvm_arch_vcpu_ioctl_set_fpu' from arch/x86/kvm/x86.c. + // The mxcsr will stay 0 and the assert below fails. Decide whether or not we should + // remove it at all. + // assert!(expected_fpu.mxcsr == actual_fpu.mxcsr); + } + + #[test] + fn test_setup_msrs() { + use hypervisor::arch::x86::{msr_index, MsrEntry}; + + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().expect("new VM fd creation failed"); + let vcpu = vm.create_vcpu(0, None).unwrap(); + setup_msrs(&vcpu).unwrap(); + + // This test will check against the last MSR entry configured (the tenth one). + // See create_msr_entries for details. + let mut msrs = vec![MsrEntry { + index: msr_index::MSR_IA32_MISC_ENABLE, + ..Default::default() + }]; + + // get_msrs returns the number of msrs that it succeed in reading. We only want to read 1 + // in this test case scenario. + let read_msrs = vcpu.get_msrs(&mut msrs).unwrap(); + assert_eq!(read_msrs, 1); + + // Official entries that were setup when we did setup_msrs. We need to assert that the + // tenth one (i.e the one with index msr_index::MSR_IA32_MISC_ENABLE has the data we + // expect. + let entry_vec = vcpu.boot_msr_entries(); + assert_eq!(entry_vec.as_slice()[9], msrs.as_slice()[0]); + } + + #[test] + fn test_setup_regs() { + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().expect("new VM fd creation failed"); + let vcpu = vm.create_vcpu(0, None).unwrap(); + + let expected_regs: StandardRegisters = StandardRegisters { + rflags: 0x0000000000000002u64, + rbx: arch::layout::PVH_INFO_START.0, + rip: 1, + ..Default::default() + }; + + setup_regs(&vcpu, expected_regs.rip).unwrap(); + + let actual_regs: StandardRegisters = vcpu.get_regs().unwrap(); + assert_eq!(actual_regs, expected_regs); + } +} + +#[cfg(target_arch = "aarch64")] +#[cfg(test)] +mod tests { + use super::Vcpu; + use arch::{aarch64::regs, layout}; + use hypervisor::kvm::aarch64::is_system_register; + use hypervisor::kvm::kvm_bindings::{ + kvm_regs, kvm_vcpu_init, user_pt_regs, KVM_REG_ARM64, KVM_REG_ARM64_SYSREG, + KVM_REG_ARM_CORE, KVM_REG_SIZE_U64, + }; + use hypervisor::{arm64_core_reg_id, offset__of}; + use std::mem; + + #[test] + fn test_setup_regs() { + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().unwrap(); + let vcpu = vm.create_vcpu(0, None).unwrap(); + + let res = vcpu.setup_regs(0, 0x0, layout::FDT_START.0); + // Must fail when vcpu is not initialized yet. + assert!(res.is_err()); + + let mut kvi: kvm_vcpu_init = kvm_vcpu_init::default(); + vm.get_preferred_target(&mut kvi).unwrap(); + vcpu.vcpu_init(&kvi).unwrap(); + + assert!(vcpu.setup_regs(0, 0x0, layout::FDT_START.0).is_ok()); + } + + #[test] + fn test_read_mpidr() { + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().unwrap(); + let vcpu = vm.create_vcpu(0, None).unwrap(); + let mut kvi: kvm_vcpu_init = kvm_vcpu_init::default(); + vm.get_preferred_target(&mut kvi).unwrap(); + + // Must fail when vcpu is not initialized yet. + assert!(vcpu.get_sys_reg(regs::MPIDR_EL1).is_err()); + + vcpu.vcpu_init(&kvi).unwrap(); + assert_eq!(vcpu.get_sys_reg(regs::MPIDR_EL1).unwrap(), 0x80000000); + } + + #[test] + fn test_is_system_register() { + let offset = offset__of!(user_pt_regs, pc); + let regid = arm64_core_reg_id!(KVM_REG_SIZE_U64, offset); + assert!(!is_system_register(regid)); + let regid = KVM_REG_ARM64 as u64 | KVM_REG_SIZE_U64 as u64 | KVM_REG_ARM64_SYSREG as u64; + assert!(is_system_register(regid)); + } + + #[test] + fn test_save_restore_core_regs() { + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().unwrap(); + let vcpu = vm.create_vcpu(0, None).unwrap(); + let mut kvi: kvm_vcpu_init = kvm_vcpu_init::default(); + vm.get_preferred_target(&mut kvi).unwrap(); + + // Must fail when vcpu is not initialized yet. + let res = vcpu.get_regs(); + assert!(res.is_err()); + assert_eq!( + format!("{}", res.unwrap_err()), + "Failed to get core register: Exec format error (os error 8)" + ); + + let mut state = kvm_regs::default(); + let res = vcpu.set_regs(&state); + assert!(res.is_err()); + assert_eq!( + format!("{}", res.unwrap_err()), + "Failed to set core register: Exec format error (os error 8)" + ); + + vcpu.vcpu_init(&kvi).unwrap(); + let res = vcpu.get_regs(); + assert!(res.is_ok()); + state = res.unwrap(); + assert_eq!(state.regs.pstate, 0x3C5); + + assert!(vcpu.set_regs(&state).is_ok()); + } + + #[test] + fn test_get_set_mpstate() { + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().unwrap(); + let vcpu = vm.create_vcpu(0, None).unwrap(); + let mut kvi: kvm_vcpu_init = kvm_vcpu_init::default(); + vm.get_preferred_target(&mut kvi).unwrap(); + + let res = vcpu.get_mp_state(); + assert!(res.is_ok()); + assert!(vcpu.set_mp_state(res.unwrap()).is_ok()); + } + + #[test] + fn test_vcpu_init_pmu_fallback() { + // Vcpu::init() must succeed both on PMU-capable hosts (PMU-enabled + // first attempt) and on hosts without PMUv3 (fallback to a PMU-less + // init). Exercising it here guards against reintroducing the boot + // failure the fallback fixes; the branch actually taken depends on the + // host running the test, so this only has discriminating power on a + // PMU-less host. The deterministic guarantee comes from + // test_vcpu_init_errno_classification below, which locks in the errno + // decoding that decides whether the fallback fires. + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().unwrap(); + + let vcpu = Vcpu::new(0, &vm, None).unwrap(); + vcpu.init(&vm).expect("boot vcpu init failed"); + + // Non-boot vcpus (id > 0) take the POWER_OFF path; that too must init + // regardless of PMU availability. + let secondary = Vcpu::new(1, &vm, None).unwrap(); + secondary.init(&vm).expect("secondary vcpu init failed"); + } + + #[test] + fn test_vcpu_init_errno_classification() { + use super::vcpu_init_errno; + + // EINVAL wrapped exactly as vcpu_init produces it (errno::Error -> + // anyhow -> HypervisorCpuError::VcpuInit) must decode back to EINVAL: + // this is the value the fallback keys on. + let einval = hypervisor::HypervisorCpuError::VcpuInit( + vmm_sys_util::errno::Error::new(libc::EINVAL).into(), + ); + assert_eq!(vcpu_init_errno(&einval), Some(libc::EINVAL)); + + // A different errno must be reported as-is so the fallback does not fire. + let enomem = hypervisor::HypervisorCpuError::VcpuInit( + vmm_sys_util::errno::Error::new(libc::ENOMEM).into(), + ); + assert_eq!(vcpu_init_errno(&enomem), Some(libc::ENOMEM)); + + // VcpuInit carrying a non-errno source cannot be classified, so the + // fallback conservatively treats it as a genuine failure (None). + let opaque = + hypervisor::HypervisorCpuError::VcpuInit(anyhow::anyhow!("not an errno error")); + assert_eq!(vcpu_init_errno(&opaque), None); + } + + #[test] + fn test_vcpu_init_retry_decision() { + use super::should_retry_without_pmu; + + // EINVAL is the only errno that means "PMU unsupported", so it is the + // only case that retries without a PMU. + let einval = hypervisor::HypervisorCpuError::VcpuInit( + vmm_sys_util::errno::Error::new(libc::EINVAL).into(), + ); + assert!(should_retry_without_pmu(&einval)); + + // A non-EINVAL errno is a genuine failure and must be propagated. This + // covers the propagation branch of Vcpu::init that cannot be triggered + // on real KVM without injecting failures. + let enomem = hypervisor::HypervisorCpuError::VcpuInit( + vmm_sys_util::errno::Error::new(libc::ENOMEM).into(), + ); + assert!(!should_retry_without_pmu(&enomem)); + + // An unclassifiable error is conservatively treated as genuine. + let opaque = + hypervisor::HypervisorCpuError::VcpuInit(anyhow::anyhow!("not an errno error")); + assert!(!should_retry_without_pmu(&opaque)); + + // A different HypervisorCpuError variant is not a vcpu_init failure at + // all, so it must never retry. + let other = hypervisor::HypervisorCpuError::RunVcpu(anyhow::anyhow!("unrelated")); + assert!(!should_retry_without_pmu(&other)); + } +} diff --git a/vendor/cube-hypervisor/vmm/src/device_manager.rs b/vendor/cube-hypervisor/vmm/src/device_manager.rs new file mode 100644 index 0000000..2e2edae --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/device_manager.rs @@ -0,0 +1,5098 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause +// + +use crate::device_tree::{DeviceNode, DeviceTree}; +use crate::interrupt::LegacyUserspaceInterruptManager; +use crate::interrupt::MsiInterruptManager; +use crate::memory_manager::{Error as MemoryManagerError, MemoryManager, MEMORY_MANAGER_ACPI_SIZE}; +use crate::pci_segment::PciSegment; +use crate::seccomp_filters::{get_seccomp_filter, Thread}; +use crate::serial_manager::{Error as SerialManagerError, SerialManager}; +use crate::sigwinch_listener::start_sigwinch_listener; +use crate::vm_config::{ + ConsoleOutputMode, DeviceConfig, DiskConfig, FsConfig, IvshmemConfig, NetConfig, PmemConfig, + UserDeviceConfig, VdpaConfig, VhostMode, VmConfig, VsockConfig, +}; +use crate::GuestRegionMmap; +use crate::PciDeviceInfo; +use crate::{device_node, DEVICE_MANAGER_SNAPSHOT_ID}; +use acpi_tables::sdt::GenericAddress; +use acpi_tables::{aml, aml::Aml}; +use anyhow::anyhow; +use arch::layout; +#[cfg(target_arch = "x86_64")] +use arch::layout::{APIC_START, IOAPIC_SIZE, IOAPIC_START}; +use arch::NumaNodes; +#[cfg(target_arch = "aarch64")] +use arch::{DeviceType, MmioDeviceInfo}; +use block_util::{ + async_io::DiskFile, block_io_uring_is_supported, detect_image_type, + fixed_vhd_async::FixedVhdDiskAsync, fixed_vhd_sync::FixedVhdDiskSync, qcow_sync::QcowDiskSync, + raw_async::RawFileDisk, raw_sync::RawFileDiskSync, vhdx_sync::VhdxDiskSync, ImageType, +}; +#[cfg(target_arch = "aarch64")] +use devices::gic; +#[cfg(target_arch = "x86_64")] +use devices::ioapic; +#[cfg(target_arch = "aarch64")] +use devices::legacy::Pl011; +#[cfg(target_arch = "x86_64")] +use devices::legacy::Serial; +use devices::{ + interrupt_controller, interrupt_controller::InterruptController, AcpiNotificationFlags, +}; +use hypervisor::{HypervisorType, IoEventAddress}; +use libc::EFD_NONBLOCK; +use libc::{ + cfmakeraw, isatty, tcgetattr, tcsetattr, termios, MAP_NORESERVE, MAP_PRIVATE, MAP_SHARED, + O_TMPFILE, PROT_READ, PROT_WRITE, TCSANOW, +}; +#[cfg(target_arch = "x86_64")] +use pci::PciConfigIo; +use pci::{ + DeviceRelocation, PciBarRegionType, PciBdf, PciDevice, VfioPciDevice, VfioUserDmaMapping, + VfioUserPciDevice, VfioUserPciDeviceError, +}; +use seccompiler::SeccompAction; +use serde::{Deserialize, Serialize}; +use std::collections::{BTreeSet, HashMap}; +use std::convert::TryInto; +use std::fs::{read_link, File, OpenOptions}; +use std::io::{self, stdout, Seek, SeekFrom}; +use std::mem::zeroed; +use std::num::Wrapping; +use std::os::unix::fs::OpenOptionsExt; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::path::PathBuf; +use std::result; +use std::sync::{Arc, Mutex}; +use std::time::Instant; +use tokio::task::JoinHandle; +use tracer::trace_scoped; +use vfio_ioctls::{VfioContainer, VfioDevice, VfioDeviceFd}; +use virtio_devices::fs::{BackendFsConfig, FsEvent}; +use virtio_devices::transport::VirtioTransport; +use virtio_devices::transport::{VirtioPciDevice, VirtioPciDeviceActivator}; +use virtio_devices::vhost_user::VhostUserConfig; +use virtio_devices::{ + AccessPlatformMapping, ActivateError, VdpaDmaMapping, VirtioMemMappingSource, +}; +use virtio_devices::{Endpoint, IommuMapping}; +use vm_allocator::{AddressAllocator, SystemAllocator}; +use vm_device::dma_mapping::vfio::VfioDmaMapping; +use vm_device::dma_mapping::ExternalDmaMapping; +use vm_device::interrupt::{ + InterruptIndex, InterruptManager, LegacyIrqGroupConfig, MsiIrqGroupConfig, +}; +use vm_device::{Bus, BusDevice, Resource}; +use vm_memory::guest_memory::FileOffset; +use vm_memory::GuestMemoryRegion; +use vm_memory::{Address, GuestAddress, GuestUsize, MmapRegion}; +#[cfg(target_arch = "x86_64")] +use vm_memory::{GuestAddressSpace, GuestMemory}; +use vm_migration::{ + protocol::MemoryRangeTable, snapshot_from_id, state_from_id, Migratable, MigratableError, + Pausable, Snapshot, SnapshotDataSection, Snapshottable, Transportable, +}; +use vm_virtio::AccessPlatform; +use vm_virtio::VirtioDeviceType; +use vmm_sys_util::eventfd::EventFd; + +#[cfg(target_arch = "aarch64")] +const MMIO_LEN: u64 = 0x1000; + +// Singleton devices / devices the user cannot name +#[cfg(target_arch = "x86_64")] +const IOAPIC_DEVICE_NAME: &str = "__ioapic"; +const SERIAL_DEVICE_NAME: &str = "__serial"; +#[cfg(target_arch = "aarch64")] +const GPIO_DEVICE_NAME: &str = "__gpio"; +const RNG_DEVICE_NAME: &str = "__rng"; +const IOMMU_DEVICE_NAME: &str = "__iommu"; +const BALLOON_DEVICE_NAME: &str = "__balloon"; +const CONSOLE_DEVICE_NAME: &str = "__console"; +const PVPANIC_DEVICE_NAME: &str = "__pvpanic"; +const IVSHMEM_DEVICE_NAME: &str = "__ivshmem"; +const SYS_CTRL_DEVICE_NAME: &str = "__sys_ctrl"; + +// Devices that the user may name and for which we generate +// identifiers if the user doesn't give one +const DISK_DEVICE_NAME_PREFIX: &str = "_disk"; +const FS_DEVICE_NAME_PREFIX: &str = "_fs"; +const NET_DEVICE_NAME_PREFIX: &str = "_net"; +const PMEM_DEVICE_NAME_PREFIX: &str = "_pmem"; +const VDPA_DEVICE_NAME_PREFIX: &str = "_vdpa"; +const VSOCK_DEVICE_NAME_PREFIX: &str = "_vsock"; +const WATCHDOG_DEVICE_NAME: &str = "__watchdog"; +const VFIO_DEVICE_NAME_PREFIX: &str = "_vfio"; +const VFIO_USER_DEVICE_NAME_PREFIX: &str = "_vfio_user"; +const VIRTIO_PCI_DEVICE_NAME_PREFIX: &str = "_virtio-pci"; +const MAX_WORKER_THREADS: usize = 5; + +/// Errors associated with device manager +#[derive(Debug)] +pub enum DeviceManagerError { + /// Cannot create EventFd. + EventFd(io::Error), + + /// Cannot open disk path + Disk(io::Error), + + /// Cannot create vhost-user-net device + CreateVhostUserNet(virtio_devices::vhost_user::Error), + + /// Cannot create virtio-blk device + CreateVirtioBlock(io::Error), + + /// Cannot create virtio-net device + CreateVirtioNet(virtio_devices::net::Error), + + /// Cannot create virtio-console device + CreateVirtioConsole(io::Error), + + /// Cannot create virtio-rng device + CreateVirtioRng(io::Error), + + /// Cannot create virtio-fs device + CreateVirtioFs(virtio_devices::vhost_user::Error), + + /// Cannot create native virtio-fs device + CreateNativeVirtioFs(io::Error), + + /// Virtio-fs device was created without a socket. + NoVirtioFsSock, + + /// Cannot create vhost-user-blk device + CreateVhostUserBlk(virtio_devices::vhost_user::Error), + + /// Cannot create virtio-pmem device + CreateVirtioPmem(io::Error), + + /// Cannot create vDPA device + CreateVdpa(virtio_devices::vdpa::Error), + + /// Cannot create virtio-vsock device + CreateVirtioVsock(io::Error), + + /// Cannot create tpm device + CreateTpmDevice(anyhow::Error), + + /// Failed to convert Path to &str for the vDPA device. + CreateVdpaConvertPath, + + /// Failed to convert Path to &str for the virtio-vsock device. + CreateVsockConvertPath, + + /// Cannot create virtio-vsock backend + CreateVsockBackend(virtio_devices::vsock::VsockUnixError), + + /// Cannot create virtio-iommu device + CreateVirtioIommu(io::Error), + + /// Cannot create virtio-balloon device + CreateVirtioBalloon(io::Error), + + /// Cannot create virtio-watchdog device + CreateVirtioWatchdog(io::Error), + + /// Failed to parse disk image format + DetectImageType(io::Error), + + /// Cannot open qcow disk path + QcowDeviceCreate(qcow::Error), + + /// Cannot create serial manager + CreateSerialManager(SerialManagerError), + + /// Cannot spawn the serial manager thread + SpawnSerialManager(SerialManagerError), + + /// Cannot open tap interface + OpenTap(net_util::TapError), + + /// Cannot allocate IRQ. + AllocateIrq, + + /// Cannot configure the IRQ. + Irq(vmm_sys_util::errno::Error), + + /// Cannot allocate PCI BARs + AllocateBars(pci::PciDeviceError), + + /// Could not free the BARs associated with a PCI device. + FreePciBars(pci::PciDeviceError), + + /// Cannot register ioevent. + RegisterIoevent(anyhow::Error), + + /// Cannot unregister ioevent. + UnRegisterIoevent(anyhow::Error), + + /// Cannot create virtio device + VirtioDevice(vmm_sys_util::errno::Error), + + /// Cannot add PCI device + AddPciDevice(pci::PciRootError), + + /// Cannot open persistent memory file + PmemFileOpen(io::Error), + + /// Cannot set persistent memory file size + PmemFileSetLen(io::Error), + + /// Cannot find a memory range for persistent memory + PmemRangeAllocation, + + /// Cannot find a memory range for virtio-fs + FsRangeAllocation, + + /// Error creating serial output file + SerialOutputFileOpen(io::Error), + + /// Error creating console output file + ConsoleOutputFileOpen(io::Error), + + /// Error creating serial pty + SerialPtyOpen(io::Error), + + /// Error creating console pty + ConsolePtyOpen(io::Error), + + /// Error setting pty raw mode + SetPtyRaw(vmm_sys_util::errno::Error), + + /// Error getting pty peer + GetPtyPeer(vmm_sys_util::errno::Error), + + /// Cannot create a VFIO device + VfioCreate(vfio_ioctls::VfioError), + + /// Cannot create a VFIO PCI device + VfioPciCreate(pci::VfioPciError), + + /// Failed to map VFIO MMIO region. + VfioMapRegion(pci::VfioPciError), + + /// Failed to DMA map VFIO device. + VfioDmaMap(vfio_ioctls::VfioError), + + /// Failed to DMA unmap VFIO device. + VfioDmaUnmap(pci::VfioPciError), + + /// Failed to create the passthrough device. + CreatePassthroughDevice(anyhow::Error), + + /// Failed to memory map. + Mmap(io::Error), + + /// Cannot add legacy device to Bus. + BusError(vm_device::BusError), + + /// Failed to allocate IO port + AllocateIoPort, + + /// Failed to allocate MMIO address + AllocateMmioAddress, + + /// Failed to make hotplug notification + HotPlugNotification(io::Error), + + /// Error from a memory manager operation + MemoryManager(MemoryManagerError), + + /// Failed to create new interrupt source group. + CreateInterruptGroup(io::Error), + + /// Failed to update interrupt source group. + UpdateInterruptGroup(io::Error), + + /// Failed to create interrupt controller. + CreateInterruptController(interrupt_controller::Error), + + /// Failed to create a new MmapRegion instance. + NewMmapRegion(vm_memory::mmap::MmapRegionError), + + /// Failed to create a new GuestRegionMmap. + NewGuestRegion(vm_memory::Error), + + /// Failed to clone a File. + CloneFile(io::Error), + + /// Failed to create socket file + CreateSocketFile(io::Error), + + /// Failed to spawn the network backend + SpawnNetBackend(io::Error), + + /// Failed to spawn the block backend + SpawnBlockBackend(io::Error), + + /// Missing PCI bus. + NoPciBus, + + /// Could not find an available device name. + NoAvailableDeviceName, + + /// Missing PCI device. + MissingPciDevice, + + /// Failed to remove a PCI device from the PCI bus. + RemoveDeviceFromPciBus(pci::PciRootError), + + /// Failed to remove a bus device from the IO bus. + RemoveDeviceFromIoBus(vm_device::BusError), + + /// Failed to remove a bus device from the MMIO bus. + RemoveDeviceFromMmioBus(vm_device::BusError), + + /// Failed to find the device corresponding to a specific PCI b/d/f. + UnknownPciBdf(u32), + + /// Not allowed to remove this type of device from the VM. + RemovalNotAllowed(vm_virtio::VirtioDeviceType), + + /// Failed to find device corresponding to the given identifier. + UnknownDeviceId(String), + + /// Failed to find an available PCI device ID. + NextPciDeviceId(pci::PciRootError), + + /// Could not reserve the PCI device ID. + GetPciDeviceId(pci::PciRootError), + + /// Could not give the PCI device ID back. + PutPciDeviceId(pci::PciRootError), + + /// No disk path was specified when one was expected + NoDiskPath, + + /// Failed to update guest memory for virtio device. + UpdateMemoryForVirtioDevice(virtio_devices::Error), + + /// Cannot create virtio-mem device + CreateVirtioMem(io::Error), + + /// Cannot find a memory range for virtio-mem memory + VirtioMemRangeAllocation, + + /// Failed to update guest memory for VFIO PCI device. + UpdateMemoryForVfioPciDevice(vfio_ioctls::VfioError), + + /// Trying to use a directory for pmem but no size specified + PmemWithDirectorySizeMissing, + + /// Trying to use a size that is not multiple of 2MiB + PmemSizeNotAligned, + + /// Could not find the node in the device tree. + MissingNode, + + /// Resource was already found. + ResourceAlreadyExists, + + /// Expected resources for virtio-pmem could not be found. + MissingVirtioPmemResources, + + /// Missing PCI b/d/f from the DeviceNode. + MissingDeviceNodePciBdf, + + /// No support for device passthrough + NoDevicePassthroughSupport, + + /// Failed to resize virtio-balloon + VirtioBalloonResize(virtio_devices::balloon::Error), + + /// Missing virtio-balloon, can't proceed as expected. + MissingVirtioBalloon, + + /// Missing virtual IOMMU device + MissingVirtualIommu, + + /// Failed to do power button notification + PowerButtonNotification(io::Error), + + /// Failed to do AArch64 GPIO power button notification + #[cfg(target_arch = "aarch64")] + AArch64PowerButtonNotification(devices::legacy::GpioDeviceError), + + /// Failed to set O_DIRECT flag to file descriptor + SetDirectIo, + + /// Failed to create FixedVhdDiskAsync + CreateFixedVhdDiskAsync(io::Error), + + /// Failed to create FixedVhdDiskSync + CreateFixedVhdDiskSync(io::Error), + + /// Failed to create QcowDiskSync + CreateQcowDiskSync(qcow::Error), + + /// Failed to create FixedVhdxDiskSync + CreateFixedVhdxDiskSync(vhdx::vhdx::VhdxError), + + /// Failed to add DMA mapping handler to virtio-mem device. + AddDmaMappingHandlerVirtioMem(virtio_devices::mem::Error), + + /// Failed to remove DMA mapping handler from virtio-mem device. + RemoveDmaMappingHandlerVirtioMem(virtio_devices::mem::Error), + + /// Failed to create vfio-user client + VfioUserCreateClient(vfio_user::Error), + + /// Failed to create VFIO user device + VfioUserCreate(VfioUserPciDeviceError), + + /// Failed to map region from VFIO user device into guest + VfioUserMapRegion(VfioUserPciDeviceError), + + /// Failed to DMA map VFIO user device. + VfioUserDmaMap(VfioUserPciDeviceError), + + /// Failed to DMA unmap VFIO user device. + VfioUserDmaUnmap(VfioUserPciDeviceError), + + /// Failed to update memory mappings for VFIO user device + UpdateMemoryForVfioUserPciDevice(VfioUserPciDeviceError), + + /// Cannot duplicate file descriptor + DupFd(vmm_sys_util::errno::Error), + + /// Failed to DMA map virtio device. + VirtioDmaMap(std::io::Error), + + /// Failed to DMA unmap virtio device. + VirtioDmaUnmap(std::io::Error), + + /// Cannot hotplug device behind vIOMMU + InvalidIommuHotplug, + + /// Invalid identifier as it is not unique. + IdentifierNotUnique(String), + + /// Invalid identifier + InvalidIdentifier(String), + + /// Error activating virtio device + VirtioActivate(ActivateError), + + /// Failed retrieving device state from snapshot + RestoreGetState(MigratableError), + + /// Cannot create a PvPanic device + PvPanicCreate(devices::pvpanic::PvPanicError), + /// Channle recv error. + VirtioFsServerRecv(std::sync::mpsc::RecvError), + + /// Virtio-Fs server update filter failed. + VirtioFsServerUpdate(String), +} +pub type DeviceManagerResult = result::Result; + +const DEVICE_MANAGER_ACPI_SIZE: usize = 0x10; + +const TIOCSPTLCK: libc::c_int = 0x4004_5431; +const TIOCGTPEER: libc::c_int = 0x5441; + +// const IVSHMEM_START_ADDR: u64 = 0x8000_0000; + +pub fn create_pty() -> io::Result<(File, File, PathBuf)> { + // Try to use /dev/pts/ptmx first then fall back to /dev/ptmx + // This is done to try and use the devpts filesystem that + // could be available for use in the process's namespace first. + // Ideally these are all the same file though but different + // kernels could have things setup differently. + // See https://www.kernel.org/doc/Documentation/filesystems/devpts.txt + // for further details. + + let custom_flags = libc::O_NONBLOCK; + let main = match OpenOptions::new() + .read(true) + .write(true) + .custom_flags(custom_flags) + .open("/dev/pts/ptmx") + { + Ok(f) => f, + _ => OpenOptions::new() + .read(true) + .write(true) + .custom_flags(custom_flags) + .open("/dev/ptmx")?, + }; + let mut unlock: libc::c_ulong = 0; + // SAFETY: FFI call into libc, trivially safe + unsafe { + libc::ioctl( + main.as_raw_fd(), + TIOCSPTLCK.try_into().unwrap(), + &mut unlock, + ) + }; + + // SAFETY: FFI call into libc, trivally safe + let sub_fd = unsafe { + libc::ioctl( + main.as_raw_fd(), + TIOCGTPEER.try_into().unwrap(), + libc::O_NOCTTY | libc::O_RDWR, + ) + }; + if sub_fd == -1 { + return vmm_sys_util::errno::errno_result().map_err(|e| e.into()); + } + + let proc_path = PathBuf::from(format!("/proc/self/fd/{}", sub_fd)); + let path = read_link(proc_path)?; + + // SAFETY: sub_fd is checked to be valid before being wrapped in File + Ok((main, unsafe { File::from_raw_fd(sub_fd) }, path)) +} + +#[derive(Default)] +pub struct Console { + console_resizer: Option>, +} + +impl Console { + pub fn need_resize(&self) -> bool { + if let Some(_resizer) = self.console_resizer.as_ref() { + return true; + } + + false + } + + pub fn update_console_size(&self) { + if let Some(resizer) = self.console_resizer.as_ref() { + resizer.update_console_size() + } + } +} + +pub(crate) struct AddressManager { + pub(crate) allocator: Arc>, + #[cfg(target_arch = "x86_64")] + pub(crate) io_bus: Arc, + pub(crate) mmio_bus: Arc, + vm: Arc, + device_tree: Arc>, + pci_mmio_allocators: Vec>>, +} + +impl DeviceRelocation for AddressManager { + fn move_bar( + &self, + old_base: u64, + new_base: u64, + len: u64, + pci_dev: &mut dyn PciDevice, + region_type: PciBarRegionType, + ) -> std::result::Result<(), std::io::Error> { + match region_type { + PciBarRegionType::IoRegion => { + #[cfg(target_arch = "x86_64")] + { + // Update system allocator + self.allocator + .lock() + .unwrap() + .free_io_addresses(GuestAddress(old_base), len as GuestUsize); + + self.allocator + .lock() + .unwrap() + .allocate_io_addresses( + Some(GuestAddress(new_base)), + len as GuestUsize, + None, + ) + .ok_or_else(|| { + io::Error::new(io::ErrorKind::Other, "failed allocating new IO range") + })?; + + // Update PIO bus + self.io_bus + .update_range(old_base, len, new_base, len) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + } + #[cfg(target_arch = "aarch64")] + error!("I/O region is not supported"); + } + PciBarRegionType::Memory32BitRegion | PciBarRegionType::Memory64BitRegion => { + // Update system allocator + if region_type == PciBarRegionType::Memory32BitRegion { + self.allocator + .lock() + .unwrap() + .free_mmio_hole_addresses(GuestAddress(old_base), len as GuestUsize); + + self.allocator + .lock() + .unwrap() + .allocate_mmio_hole_addresses( + Some(GuestAddress(new_base)), + len as GuestUsize, + Some(len), + ) + .ok_or_else(|| { + io::Error::new( + io::ErrorKind::Other, + "failed allocating new 32 bits MMIO range", + ) + })?; + } else { + // Find the specific allocator that this BAR was allocated from and use it for new one + for allocator in &self.pci_mmio_allocators { + let allocator_base = allocator.lock().unwrap().base(); + let allocator_end = allocator.lock().unwrap().end(); + + if old_base >= allocator_base.0 && old_base <= allocator_end.0 { + allocator + .lock() + .unwrap() + .free(GuestAddress(old_base), len as GuestUsize); + + allocator + .lock() + .unwrap() + .allocate( + Some(GuestAddress(new_base)), + len as GuestUsize, + Some(len), + ) + .ok_or_else(|| { + io::Error::new( + io::ErrorKind::Other, + "failed allocating new 64 bits MMIO range", + ) + })?; + + break; + } + } + } + + // Update MMIO bus + self.mmio_bus + .update_range(old_base, len, new_base, len) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + } + } + + // Update the device_tree resources associated with the device + if let Some(id) = pci_dev.id() { + if let Some(node) = self.device_tree.lock().unwrap().get_mut(&id) { + let mut resource_updated = false; + for resource in node.resources.iter_mut() { + if let Resource::PciBar { base, type_, .. } = resource { + if PciBarRegionType::from(*type_) == region_type && *base == old_base { + *base = new_base; + resource_updated = true; + break; + } + } + } + + if !resource_updated { + return Err(io::Error::new( + io::ErrorKind::Other, + format!( + "Couldn't find a resource with base 0x{:x} for device {}", + old_base, id + ), + )); + } + } else { + return Err(io::Error::new( + io::ErrorKind::Other, + format!("Couldn't find device {} from device tree", id), + )); + } + } + + let any_dev = pci_dev.as_any(); + if let Some(virtio_pci_dev) = any_dev.downcast_ref::() { + let bar_addr = virtio_pci_dev.config_bar_addr(); + if bar_addr == new_base { + for (event, addr) in virtio_pci_dev.ioeventfds(old_base) { + let io_addr = IoEventAddress::Mmio(addr); + self.vm.unregister_ioevent(event, &io_addr).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("failed to unregister ioevent: {:?}", e), + ) + })?; + } + for (event, addr) in virtio_pci_dev.ioeventfds(new_base) { + let io_addr = IoEventAddress::Mmio(addr); + self.vm + .register_ioevent(event, &io_addr, None) + .map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("failed to register ioevent: {:?}", e), + ) + })?; + } + } else { + let virtio_dev = virtio_pci_dev.virtio_device(); + let mut virtio_dev = virtio_dev.lock().unwrap(); + if let Some(mut shm_regions) = virtio_dev.get_shm_regions() { + if shm_regions.addr.raw_value() == old_base { + let mem_region = self.vm.make_user_memory_region( + shm_regions.mem_slot, + old_base, + shm_regions.len, + shm_regions.host_addr, + false, + false, + ); + + self.vm.remove_user_memory_region(mem_region).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("failed to remove user memory region: {:?}", e), + ) + })?; + + // Create new mapping by inserting new region to KVM. + let mem_region = self.vm.make_user_memory_region( + shm_regions.mem_slot, + new_base, + shm_regions.len, + shm_regions.host_addr, + false, + false, + ); + + self.vm.create_user_memory_region(mem_region).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("failed to create user memory regions: {:?}", e), + ) + })?; + + // Update shared memory regions to reflect the new mapping. + shm_regions.addr = GuestAddress(new_base); + virtio_dev.set_shm_regions(shm_regions).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("failed to update shared memory regions: {:?}", e), + ) + })?; + } + } + } + } + + pci_dev.move_bar(old_base, new_base) + } +} + +#[derive(Serialize, Deserialize)] +struct DeviceManagerState { + device_tree: DeviceTree, + device_id_cnt: Wrapping, +} + +#[derive(Debug)] +pub struct PtyPair { + pub main: File, + pub path: PathBuf, +} + +impl Clone for PtyPair { + fn clone(&self) -> Self { + PtyPair { + main: self.main.try_clone().unwrap(), + path: self.path.clone(), + } + } +} + +#[derive(Clone)] +pub enum PciDeviceHandle { + Vfio(Arc>), + Virtio(Arc>), + VfioUser(Arc>), +} + +#[derive(Clone)] +struct MetaVirtioDevice { + virtio_device: Arc>, + iommu: bool, + id: String, + pci_segment: u16, + dma_handler: Option>, +} + +#[derive(Default)] +pub struct AcpiPlatformAddresses { + pub pm_timer_address: Option, + pub reset_reg_address: Option, + pub sleep_control_reg_address: Option, + pub sleep_status_reg_address: Option, +} + +pub struct DeviceManager { + // The underlying hypervisor + hypervisor_type: HypervisorType, + + // Manage address space related to devices + address_manager: Arc, + + // Console abstraction + console: Arc, + + // console PTY + console_pty: Option>>, + + // serial PTY + serial_pty: Option>>, + + // Serial Manager + serial_manager: Option>, + + // pty foreground status, + console_resize_pipe: Option>, + + // Interrupt controller + #[cfg(target_arch = "x86_64")] + interrupt_controller: Option>>, + #[cfg(target_arch = "aarch64")] + interrupt_controller: Option>>, + + // Things to be added to the commandline (e.g. aarch64 early console) + #[cfg(target_arch = "aarch64")] + cmdline_additions: Vec, + + // ACPI GED notification device + ged_notification_device: Option>>, + + // VM configuration + config: Arc>, + + // Memory Manager + memory_manager: Arc>, + + // The virtio devices on the system + virtio_devices: Vec, + + // List of bus devices + // Let the DeviceManager keep strong references to the BusDevice devices. + // This allows the IO and MMIO buses to be provided with Weak references, + // which prevents cyclic dependencies. + bus_devices: Vec>>, + + // Counter to keep track of the consumed device IDs. + device_id_cnt: Wrapping, + + pci_segments: Vec, + + #[cfg_attr(target_arch = "aarch64", allow(dead_code))] + // MSI Interrupt Manager + msi_interrupt_manager: Arc>, + + #[cfg_attr(feature = "mshv", allow(dead_code))] + // Legacy Interrupt Manager + legacy_interrupt_manager: Option>>, + + // Passthrough device handle + passthrough_device: Option, + + // VFIO container + // Only one container can be created, therefore it is stored as part of the + // DeviceManager to be reused. + vfio_container: Option>, + + // Paravirtualized IOMMU + iommu_device: Option>>, + iommu_mapping: Option>, + + // PCI information about devices attached to the paravirtualized IOMMU + // It contains the virtual IOMMU PCI BDF along with the list of PCI BDF + // representing the devices attached to the virtual IOMMU. This is useful + // information for filling the ACPI VIOT table. + iommu_attached_devices: Option<(PciBdf, Vec)>, + + // Tree of devices, representing the dependencies between devices. + // Useful for introspection, snapshot and restore. + device_tree: Arc>, + + // Exit event + exit_evt: EventFd, + reset_evt: EventFd, + + #[cfg(target_arch = "aarch64")] + id_to_dev_info: HashMap<(DeviceType, String), MmioDeviceInfo>, + + // seccomp action + seccomp_action: SeccompAction, + + // List of guest NUMA nodes. + numa_nodes: NumaNodes, + + // Possible handle to the virtio-balloon device + balloon: Option>>, + + // Virtio Device activation EventFd to allow the VMM thread to trigger device + // activation and thus start the threads from the VMM thread + activate_evt: EventFd, + + acpi_address: GuestAddress, + + selected_segment: usize, + + // Possible handle to the virtio-mem device + virtio_mem_devices: Vec>>, + + #[cfg(target_arch = "aarch64")] + // GPIO device for AArch64 + gpio_device: Option>>, + + // pvpanic device + pvpanic_device: Option>>, + + // Flag to force setting the iommu on virtio devices + force_iommu: bool, + + // Helps identify if the VM is currently being restored + restoring: bool, + + // io_uring availability if detected + io_uring_supported: Option, + + // List of unique identifiers provided at boot through the configuration. + boot_id_list: BTreeSet, + + // Start time of the VM + timestamp: Instant, + + // Pending activations + pending_activations: Arc>>, + + // Addresses for ACPI platform devices e.g. ACPI PM timer, sleep/reset registers + acpi_platform_addresses: AcpiPlatformAddresses, + + snapshot: Option, + + sandbox_id: String, + + sys_ctrl: Option>>, + + // ivshmem device + ivshmem_device: Option>>, +} + +impl DeviceManager { + #[allow(clippy::too_many_arguments)] + pub fn new( + hypervisor_type: HypervisorType, + vm: Arc, + config: Arc>, + memory_manager: Arc>, + exit_evt: &EventFd, + reset_evt: &EventFd, + seccomp_action: SeccompAction, + numa_nodes: NumaNodes, + activate_evt: &EventFd, + force_iommu: bool, + restoring: bool, + boot_id_list: BTreeSet, + timestamp: Instant, + snapshot: Option, + sandbox_id: String, + ) -> DeviceManagerResult>> { + trace_scoped!("DeviceManager::new"); + + let device_tree = Arc::new(Mutex::new(DeviceTree::new())); + + let num_pci_segments = + if let Some(platform_config) = config.lock().unwrap().platform.as_ref() { + platform_config.num_pci_segments + } else { + 1 + }; + + let start_of_device_area = memory_manager.lock().unwrap().start_of_device_area().0; + let end_of_device_area = memory_manager.lock().unwrap().end_of_device_area().0; + + // Start each PCI segment range on a 4GiB boundary + let pci_segment_size = (end_of_device_area - start_of_device_area + 1) + / ((4 << 30) * num_pci_segments as u64) + * (4 << 30); + + let mut pci_mmio_allocators = vec![]; + for i in 0..num_pci_segments as u64 { + let mmio_start = start_of_device_area + i * pci_segment_size; + let allocator = Arc::new(Mutex::new( + AddressAllocator::new(GuestAddress(mmio_start), pci_segment_size).unwrap(), + )); + pci_mmio_allocators.push(allocator) + } + + let address_manager = Arc::new(AddressManager { + allocator: memory_manager.lock().unwrap().allocator(), + #[cfg(target_arch = "x86_64")] + io_bus: Arc::new(Bus::new()), + mmio_bus: Arc::new(Bus::new()), + vm: vm.clone(), + device_tree: Arc::clone(&device_tree), + pci_mmio_allocators, + }); + + // First we create the MSI interrupt manager, the legacy one is created + // later, after the IOAPIC device creation. + // The reason we create the MSI one first is because the IOAPIC needs it, + // and then the legacy interrupt manager needs an IOAPIC. So we're + // handling a linear dependency chain: + // msi_interrupt_manager <- IOAPIC <- legacy_interrupt_manager. + let msi_interrupt_manager: Arc> = + Arc::new(MsiInterruptManager::new( + Arc::clone(&address_manager.allocator), + vm, + )); + + let acpi_address = address_manager + .allocator + .lock() + .unwrap() + .allocate_platform_mmio_addresses(None, DEVICE_MANAGER_ACPI_SIZE as u64, None) + .ok_or(DeviceManagerError::AllocateIoPort)?; + + let mut pci_irq_slots = [0; 32]; + PciSegment::reserve_legacy_interrupts_for_pci_devices( + &address_manager, + &mut pci_irq_slots, + )?; + + let mut pci_segments = vec![PciSegment::new_default_segment( + &address_manager, + Arc::clone(&address_manager.pci_mmio_allocators[0]), + &pci_irq_slots, + )?]; + + for i in 1..num_pci_segments as usize { + pci_segments.push(PciSegment::new( + i as u16, + &address_manager, + Arc::clone(&address_manager.pci_mmio_allocators[i]), + &pci_irq_slots, + )?); + } + + let device_manager = DeviceManager { + hypervisor_type, + address_manager: Arc::clone(&address_manager), + console: Arc::new(Console::default()), + interrupt_controller: None, + #[cfg(target_arch = "aarch64")] + cmdline_additions: Vec::new(), + ged_notification_device: None, + config, + memory_manager, + virtio_devices: Vec::new(), + bus_devices: Vec::new(), + device_id_cnt: Wrapping(0), + msi_interrupt_manager, + legacy_interrupt_manager: None, + passthrough_device: None, + vfio_container: None, + iommu_device: None, + iommu_mapping: None, + iommu_attached_devices: None, + pci_segments, + device_tree, + exit_evt: exit_evt.try_clone().map_err(DeviceManagerError::EventFd)?, + reset_evt: reset_evt.try_clone().map_err(DeviceManagerError::EventFd)?, + #[cfg(target_arch = "aarch64")] + id_to_dev_info: HashMap::new(), + seccomp_action, + numa_nodes, + balloon: None, + activate_evt: activate_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + acpi_address, + selected_segment: 0, + serial_pty: None, + serial_manager: None, + console_pty: None, + console_resize_pipe: None, + virtio_mem_devices: Vec::new(), + #[cfg(target_arch = "aarch64")] + gpio_device: None, + sys_ctrl: None, + pvpanic_device: None, + force_iommu, + restoring, + io_uring_supported: None, + boot_id_list, + timestamp, + pending_activations: Arc::new(Mutex::new(Vec::default())), + acpi_platform_addresses: AcpiPlatformAddresses::default(), + snapshot, + sandbox_id, + ivshmem_device: None, + }; + + let device_manager = Arc::new(Mutex::new(device_manager)); + + address_manager + .mmio_bus + .insert( + Arc::clone(&device_manager) as Arc>, + acpi_address.0, + DEVICE_MANAGER_ACPI_SIZE as u64, + ) + .map_err(DeviceManagerError::BusError)?; + + Ok(device_manager) + } + + pub fn serial_pty(&self) -> Option { + self.serial_pty + .as_ref() + .map(|pty| pty.lock().unwrap().clone()) + } + + pub fn console_pty(&self) -> Option { + self.console_pty + .as_ref() + .map(|pty| pty.lock().unwrap().clone()) + } + + pub fn console_resize_pipe(&self) -> Option> { + self.console_resize_pipe.as_ref().map(Arc::clone) + } + + pub fn fs_device_update(&self, fs_cfg: FsConfig) -> DeviceManagerResult<()> { + if let Some(id) = fs_cfg.id { + let device_tree = self.device_tree.lock().unwrap(); + let node = device_tree + .get(&id) + .ok_or(DeviceManagerError::UnknownDeviceId(id))?; + let mut channel_wait = false; + let (tx, rx): ( + std::sync::mpsc::Sender, + std::sync::mpsc::Receiver, + ) = std::sync::mpsc::channel(); + if node.dev_fd.is_some() + && node.pending_dev_message.is_some() + && fs_cfg.backendfs_config.is_some() + { + let mut pending_dev_message = + node.pending_dev_message.as_ref().unwrap().lock().unwrap(); + let event = FsEvent { + tx, + backendfs_config: fs_cfg.backendfs_config.unwrap(), + }; + pending_dev_message.push(event); + // SAFETY: FFI call to dup. Trivially safe. + let dev_fd = unsafe { libc::dup(node.dev_fd.unwrap()) }; + // SAFETY: dev_fd is valid and owned solely by us. + let dev_evt = unsafe { EventFd::from_raw_fd(dev_fd) }; + dev_evt.write(1).unwrap(); + channel_wait = true; + } + if channel_wait { + debug!("Waiting on channel"); + let success = rx.recv().map_err(DeviceManagerError::VirtioFsServerRecv)?; + if !success { + return Err(DeviceManagerError::VirtioFsServerUpdate( + "failed to update filter list".into(), + )); + } + debug!("Waiting finished with {:?}", success); + } + } + Ok(()) + } + + pub fn create_devices( + &mut self, + serial_pty: Option, + console_pty: Option, + console_resize_pipe: Option, + ) -> DeviceManagerResult<()> { + trace_scoped!("create_devices"); + + let mut virtio_devices: Vec = Vec::new(); + + let interrupt_controller = self.add_interrupt_controller()?; + + // Now we can create the legacy interrupt manager, which needs the freshly + // formed IOAPIC device. + let legacy_interrupt_manager: Arc< + dyn InterruptManager, + > = Arc::new(LegacyUserspaceInterruptManager::new(Arc::clone( + &interrupt_controller, + ))); + + { + if let Some(acpi_address) = self.memory_manager.lock().unwrap().acpi_address() { + self.address_manager + .mmio_bus + .insert( + Arc::clone(&self.memory_manager) as Arc>, + acpi_address.0, + MEMORY_MANAGER_ACPI_SIZE as u64, + ) + .map_err(DeviceManagerError::BusError)?; + } + } + + #[cfg(target_arch = "x86_64")] + // WORKAROUND: force reset event into shutdown. + self.add_legacy_devices( + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + )?; + + #[cfg(target_arch = "aarch64")] + self.add_legacy_devices(&legacy_interrupt_manager)?; + + { + // WORKAROUND: force reset event into shutdown. + self.ged_notification_device = self.add_acpi_devices( + &legacy_interrupt_manager, + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + )?; + } + + self.console = self.add_console_device( + &legacy_interrupt_manager, + &mut virtio_devices, + serial_pty, + console_pty, + console_resize_pipe, + )?; + + if let Some(tpm) = self.config.clone().lock().unwrap().tpm.as_ref() { + let tpm_dev = self.add_tpm_device(tpm.socket.clone())?; + self.bus_devices + .push(Arc::clone(&tpm_dev) as Arc>) + } + self.legacy_interrupt_manager = Some(legacy_interrupt_manager); + + virtio_devices.append(&mut self.make_virtio_devices()?); + + self.add_pci_devices(virtio_devices.clone())?; + + self.virtio_devices = virtio_devices; + + if self.config.clone().lock().unwrap().pvpanic { + self.pvpanic_device = self.add_pvpanic_device()?; + } + + if let Some(ivshmem) = self.config.clone().lock().unwrap().ivshmem.as_ref() { + self.ivshmem_device = self.add_ivshmem_device(ivshmem)?; + } + + Ok(()) + } + + fn state(&self) -> DeviceManagerState { + DeviceManagerState { + device_tree: self.device_tree.lock().unwrap().clone(), + device_id_cnt: self.device_id_cnt, + } + } + + fn set_state(&mut self, state: &DeviceManagerState) { + *self.device_tree.lock().unwrap() = state.device_tree.clone(); + self.device_id_cnt = state.device_id_cnt; + } + + fn get_msi_iova_space(&mut self) -> (u64, u64) { + #[cfg(target_arch = "aarch64")] + { + let vcpus = self.config.lock().unwrap().cpus.boot_vcpus; + let vgic_config = gic::Gic::create_default_config(vcpus.into()); + ( + vgic_config.msi_addr, + vgic_config.msi_addr + vgic_config.msi_size - 1, + ) + } + #[cfg(target_arch = "x86_64")] + (0xfee0_0000, 0xfeef_ffff) + } + + #[cfg(target_arch = "aarch64")] + /// Gets the information of the devices registered up to some point in time. + pub fn get_device_info(&self) -> &HashMap<(DeviceType, String), MmioDeviceInfo> { + &self.id_to_dev_info + } + + #[allow(unused_variables)] + fn add_pci_devices( + &mut self, + virtio_devices: Vec, + ) -> DeviceManagerResult<()> { + let iommu_id = String::from(IOMMU_DEVICE_NAME); + + let iommu_device = if self.config.lock().unwrap().iommu { + let (device, mapping) = virtio_devices::Iommu::new( + iommu_id.clone(), + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + self.get_msi_iova_space(), + state_from_id(self.snapshot.as_ref(), iommu_id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVirtioIommu)?; + let device = Arc::new(Mutex::new(device)); + self.iommu_device = Some(Arc::clone(&device)); + self.iommu_mapping = Some(mapping); + + // Fill the device tree with a new node. In case of restore, we + // know there is nothing to do, so we can simply override the + // existing entry. + self.device_tree + .lock() + .unwrap() + .insert(iommu_id.clone(), device_node!(iommu_id, device)); + + Some(device) + } else { + None + }; + + let mut iommu_attached_devices = Vec::new(); + { + for handle in virtio_devices { + let mapping: Option> = if handle.iommu { + self.iommu_mapping.clone() + } else { + None + }; + + let dev_id = self.add_virtio_pci_device( + handle.virtio_device, + &mapping, + handle.id, + handle.pci_segment, + handle.dma_handler, + )?; + + if handle.iommu { + iommu_attached_devices.push(dev_id); + } + } + + let mut vfio_iommu_device_ids = self.add_vfio_devices()?; + iommu_attached_devices.append(&mut vfio_iommu_device_ids); + + let mut vfio_user_iommu_device_ids = self.add_user_devices()?; + iommu_attached_devices.append(&mut vfio_user_iommu_device_ids); + + // Add all devices from forced iommu segments + if let Some(platform_config) = self.config.lock().unwrap().platform.as_ref() { + if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() { + for segment in iommu_segments { + for device in 0..32 { + let bdf = PciBdf::new(*segment, 0, device, 0); + if !iommu_attached_devices.contains(&bdf) { + iommu_attached_devices.push(bdf); + } + } + } + } + } + + if let Some(iommu_device) = iommu_device { + let dev_id = self.add_virtio_pci_device(iommu_device, &None, iommu_id, 0, None)?; + self.iommu_attached_devices = Some((dev_id, iommu_attached_devices)); + } + } + + for segment in &self.pci_segments { + #[cfg(target_arch = "x86_64")] + if let Some(pci_config_io) = segment.pci_config_io.as_ref() { + self.bus_devices + .push(Arc::clone(pci_config_io) as Arc>); + } + + self.bus_devices + .push(Arc::clone(&segment.pci_config_mmio) as Arc>); + } + + Ok(()) + } + + #[cfg(target_arch = "aarch64")] + fn add_interrupt_controller( + &mut self, + ) -> DeviceManagerResult>> { + let interrupt_controller: Arc> = Arc::new(Mutex::new( + gic::Gic::new( + self.config.lock().unwrap().cpus.boot_vcpus, + Arc::clone(&self.msi_interrupt_manager), + ) + .map_err(DeviceManagerError::CreateInterruptController)?, + )); + + self.interrupt_controller = Some(interrupt_controller.clone()); + + // Unlike x86_64, the "interrupt_controller" here for AArch64 is only + // a `Gic` object that implements the `InterruptController` to provide + // interrupt delivery service. This is not the real GIC device so that + // we do not need to insert it to the device tree. + + Ok(interrupt_controller) + } + + #[cfg(target_arch = "aarch64")] + pub fn get_interrupt_controller(&mut self) -> Option<&Arc>> { + self.interrupt_controller.as_ref() + } + + #[cfg(target_arch = "x86_64")] + fn add_interrupt_controller( + &mut self, + ) -> DeviceManagerResult>> { + let id = String::from(IOAPIC_DEVICE_NAME); + + // Create IOAPIC + let interrupt_controller = Arc::new(Mutex::new( + ioapic::Ioapic::new( + id.clone(), + APIC_START, + Arc::clone(&self.msi_interrupt_manager), + ) + .map_err(DeviceManagerError::CreateInterruptController)?, + )); + + self.interrupt_controller = Some(interrupt_controller.clone()); + + self.address_manager + .mmio_bus + .insert(interrupt_controller.clone(), IOAPIC_START.0, IOAPIC_SIZE) + .map_err(DeviceManagerError::BusError)?; + + self.bus_devices + .push(Arc::clone(&interrupt_controller) as Arc>); + + // Fill the device tree with a new node. In case of restore, we + // know there is nothing to do, so we can simply override the + // existing entry. + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, interrupt_controller)); + + Ok(interrupt_controller) + } + + fn add_acpi_devices( + &mut self, + interrupt_manager: &Arc>, + reset_evt: EventFd, + exit_evt: EventFd, + ) -> DeviceManagerResult>>> { + let shutdown_device = Arc::new(Mutex::new(devices::AcpiShutdownDevice::new( + exit_evt, reset_evt, + ))); + + self.bus_devices + .push(Arc::clone(&shutdown_device) as Arc>); + + #[cfg(target_arch = "x86_64")] + { + let shutdown_pio_address: u16 = 0x600; + + self.address_manager + .allocator + .lock() + .unwrap() + .allocate_io_addresses(Some(GuestAddress(shutdown_pio_address.into())), 0x8, None) + .ok_or(DeviceManagerError::AllocateIoPort)?; + + self.address_manager + .io_bus + .insert(shutdown_device, shutdown_pio_address.into(), 0x4) + .map_err(DeviceManagerError::BusError)?; + + self.acpi_platform_addresses.sleep_control_reg_address = + Some(GenericAddress::io_port_address::(shutdown_pio_address)); + self.acpi_platform_addresses.sleep_status_reg_address = + Some(GenericAddress::io_port_address::(shutdown_pio_address)); + self.acpi_platform_addresses.reset_reg_address = + Some(GenericAddress::io_port_address::(shutdown_pio_address)); + } + + let ged_irq = self + .address_manager + .allocator + .lock() + .unwrap() + .allocate_irq() + .unwrap(); + let interrupt_group = interrupt_manager + .create_group(LegacyIrqGroupConfig { + irq: ged_irq as InterruptIndex, + }) + .map_err(DeviceManagerError::CreateInterruptGroup)?; + let ged_address = self + .address_manager + .allocator + .lock() + .unwrap() + .allocate_platform_mmio_addresses( + None, + devices::acpi::GED_DEVICE_ACPI_SIZE as u64, + None, + ) + .ok_or(DeviceManagerError::AllocateMmioAddress)?; + let ged_device = Arc::new(Mutex::new(devices::AcpiGedDevice::new( + interrupt_group, + ged_irq, + ged_address, + ))); + self.address_manager + .mmio_bus + .insert( + ged_device.clone(), + ged_address.0, + devices::acpi::GED_DEVICE_ACPI_SIZE as u64, + ) + .map_err(DeviceManagerError::BusError)?; + self.bus_devices + .push(Arc::clone(&ged_device) as Arc>); + + let pm_timer_device = Arc::new(Mutex::new(devices::AcpiPmTimerDevice::new())); + + self.bus_devices + .push(Arc::clone(&pm_timer_device) as Arc>); + + #[cfg(target_arch = "x86_64")] + { + let pm_timer_pio_address: u16 = 0x608; + + self.address_manager + .allocator + .lock() + .unwrap() + .allocate_io_addresses(Some(GuestAddress(pm_timer_pio_address.into())), 0x4, None) + .ok_or(DeviceManagerError::AllocateIoPort)?; + + self.address_manager + .io_bus + .insert(pm_timer_device, pm_timer_pio_address.into(), 0x4) + .map_err(DeviceManagerError::BusError)?; + + self.acpi_platform_addresses.pm_timer_address = + Some(GenericAddress::io_port_address::(pm_timer_pio_address)); + } + + Ok(Some(ged_device)) + } + + #[cfg(target_arch = "x86_64")] + fn add_legacy_devices(&mut self, reset_evt: EventFd) -> DeviceManagerResult<()> { + // Add a shutdown device (i8042) + let i8042 = Arc::new(Mutex::new(devices::legacy::I8042Device::new( + reset_evt.try_clone().unwrap(), + ))); + + self.bus_devices + .push(Arc::clone(&i8042) as Arc>); + + self.address_manager + .io_bus + .insert(i8042, 0x61, 0x4) + .map_err(DeviceManagerError::BusError)?; + { + // Add a CMOS emulated device + let mem_size = self + .memory_manager + .lock() + .unwrap() + .guest_memory() + .memory() + .last_addr() + .0 + + 1; + let mem_below_4g = std::cmp::min(arch::layout::MEM_32BIT_RESERVED_START.0, mem_size); + let mem_above_4g = mem_size.saturating_sub(arch::layout::RAM_64BIT_START.0); + + let cmos = Arc::new(Mutex::new(devices::legacy::Cmos::new( + mem_below_4g, + mem_above_4g, + reset_evt, + ))); + + self.bus_devices + .push(Arc::clone(&cmos) as Arc>); + + self.address_manager + .io_bus + .insert(cmos, 0x70, 0x2) + .map_err(DeviceManagerError::BusError)?; + + let fwdebug = Arc::new(Mutex::new(devices::legacy::FwDebugDevice::new())); + + self.bus_devices + .push(Arc::clone(&fwdebug) as Arc>); + + self.address_manager + .io_bus + .insert(fwdebug, 0x402, 0x1) + .map_err(DeviceManagerError::BusError)?; + } + + // 0x80 debug port + let debug_port = Arc::new(Mutex::new(devices::legacy::DebugPort::new(self.timestamp))); + self.bus_devices + .push(Arc::clone(&debug_port) as Arc>); + self.address_manager + .io_bus + .insert(debug_port, 0x80, 0x1) + .map_err(DeviceManagerError::BusError)?; + + // 0x0680 system control port + #[cfg(target_arch = "x86_64")] + if self.config.lock().unwrap().sys_ctrl { + let id = String::from(SYS_CTRL_DEVICE_NAME); + let sys_ctrl = Arc::new(Mutex::new(devices::legacy::SysCtrl::new( + id.clone(), + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ))); + self.sys_ctrl = Some(Arc::clone(&sys_ctrl)); + self.bus_devices + .push(Arc::clone(&sys_ctrl) as Arc>); + self.address_manager + .io_bus + .insert( + Arc::clone(&sys_ctrl) as Arc>, + 0x680, + 0x1, + ) + .map_err(DeviceManagerError::BusError)?; + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, sys_ctrl)); + } + + Ok(()) + } + + #[cfg(target_arch = "aarch64")] + fn add_legacy_devices( + &mut self, + interrupt_manager: &Arc>, + ) -> DeviceManagerResult<()> { + // Add a RTC device + let rtc_irq = self + .address_manager + .allocator + .lock() + .unwrap() + .allocate_irq() + .unwrap(); + + let interrupt_group = interrupt_manager + .create_group(LegacyIrqGroupConfig { + irq: rtc_irq as InterruptIndex, + }) + .map_err(DeviceManagerError::CreateInterruptGroup)?; + + let rtc_device = Arc::new(Mutex::new(devices::legacy::Rtc::new(interrupt_group))); + + self.bus_devices + .push(Arc::clone(&rtc_device) as Arc>); + + let addr = arch::layout::LEGACY_RTC_MAPPED_IO_START; + + self.address_manager + .mmio_bus + .insert(rtc_device, addr.0, MMIO_LEN) + .map_err(DeviceManagerError::BusError)?; + + self.id_to_dev_info.insert( + (DeviceType::Rtc, "rtc".to_string()), + MmioDeviceInfo { + addr: addr.0, + len: MMIO_LEN, + irq: rtc_irq, + }, + ); + + // Add a GPIO device + let id = String::from(GPIO_DEVICE_NAME); + let gpio_irq = self + .address_manager + .allocator + .lock() + .unwrap() + .allocate_irq() + .unwrap(); + + let interrupt_group = interrupt_manager + .create_group(LegacyIrqGroupConfig { + irq: gpio_irq as InterruptIndex, + }) + .map_err(DeviceManagerError::CreateInterruptGroup)?; + + let gpio_device = Arc::new(Mutex::new(devices::legacy::Gpio::new( + id.clone(), + interrupt_group, + ))); + + self.bus_devices + .push(Arc::clone(&gpio_device) as Arc>); + + let addr = arch::layout::LEGACY_GPIO_MAPPED_IO_START; + + self.address_manager + .mmio_bus + .insert(gpio_device.clone(), addr.0, MMIO_LEN) + .map_err(DeviceManagerError::BusError)?; + + self.gpio_device = Some(gpio_device.clone()); + + self.id_to_dev_info.insert( + (DeviceType::Gpio, "gpio".to_string()), + MmioDeviceInfo { + addr: addr.0, + len: MMIO_LEN, + irq: gpio_irq, + }, + ); + + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, gpio_device)); + + // Add a system control device for guest-to-shim notifications. + // AArch64 has no PIO port space, so expose the same SysCtrl device via MMIO. + if self.config.lock().unwrap().sys_ctrl { + let id = String::from(SYS_CTRL_DEVICE_NAME); + let sys_ctrl = Arc::new(Mutex::new(devices::legacy::SysCtrl::new( + id.clone(), + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ))); + self.sys_ctrl = Some(Arc::clone(&sys_ctrl)); + self.bus_devices + .push(Arc::clone(&sys_ctrl) as Arc>); + + let addr = arch::layout::LEGACY_SYS_CTRL_MAPPED_IO_START; + self.address_manager + .mmio_bus + .insert( + Arc::clone(&sys_ctrl) as Arc>, + addr.0, + MMIO_LEN, + ) + .map_err(DeviceManagerError::BusError)?; + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, sys_ctrl)); + } + + Ok(()) + } + + #[cfg(target_arch = "x86_64")] + fn add_serial_device( + &mut self, + interrupt_manager: &Arc>, + serial_writer: Option>, + ) -> DeviceManagerResult>> { + // Serial is tied to IRQ #4 + let serial_irq = 4; + + let id = String::from(SERIAL_DEVICE_NAME); + + let interrupt_group = interrupt_manager + .create_group(LegacyIrqGroupConfig { + irq: serial_irq as InterruptIndex, + }) + .map_err(DeviceManagerError::CreateInterruptGroup)?; + + let serial = Arc::new(Mutex::new(Serial::new( + id.clone(), + interrupt_group, + serial_writer, + ))); + + self.bus_devices + .push(Arc::clone(&serial) as Arc>); + + self.address_manager + .allocator + .lock() + .unwrap() + .allocate_io_addresses(Some(GuestAddress(0x3f8)), 0x8, None) + .ok_or(DeviceManagerError::AllocateIoPort)?; + + self.address_manager + .io_bus + .insert(serial.clone(), 0x3f8, 0x8) + .map_err(DeviceManagerError::BusError)?; + + // Fill the device tree with a new node. In case of restore, we + // know there is nothing to do, so we can simply override the + // existing entry. + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, serial)); + + Ok(serial) + } + + #[cfg(target_arch = "aarch64")] + fn add_serial_device( + &mut self, + interrupt_manager: &Arc>, + serial_writer: Option>, + ) -> DeviceManagerResult>> { + let id = String::from(SERIAL_DEVICE_NAME); + + let serial_irq = self + .address_manager + .allocator + .lock() + .unwrap() + .allocate_irq() + .unwrap(); + + let interrupt_group = interrupt_manager + .create_group(LegacyIrqGroupConfig { + irq: serial_irq as InterruptIndex, + }) + .map_err(DeviceManagerError::CreateInterruptGroup)?; + + let serial = Arc::new(Mutex::new(devices::legacy::Pl011::new( + id.clone(), + interrupt_group, + serial_writer, + self.timestamp, + ))); + + self.bus_devices + .push(Arc::clone(&serial) as Arc>); + + let addr = arch::layout::LEGACY_SERIAL_MAPPED_IO_START; + + self.address_manager + .mmio_bus + .insert(serial.clone(), addr.0, MMIO_LEN) + .map_err(DeviceManagerError::BusError)?; + + self.id_to_dev_info.insert( + (DeviceType::Serial, DeviceType::Serial.to_string()), + MmioDeviceInfo { + addr: addr.0, + len: MMIO_LEN, + irq: serial_irq, + }, + ); + + self.cmdline_additions + .push(format!("earlycon=pl011,mmio,0x{:08x}", addr.0)); + + // Fill the device tree with a new node. In case of restore, we + // know there is nothing to do, so we can simply override the + // existing entry. + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, serial)); + + Ok(serial) + } + + fn modify_mode( + &self, + fd: RawFd, + f: F, + ) -> vmm_sys_util::errno::Result<()> { + // SAFETY: safe because we check the return value of isatty. + if unsafe { isatty(fd) } != 1 { + return Ok(()); + } + + // SAFETY: The following pair are safe because termios gets totally overwritten by tcgetattr + // and we check the return result. + let mut termios: termios = unsafe { zeroed() }; + let ret = unsafe { tcgetattr(fd, &mut termios as *mut _) }; + if ret < 0 { + return vmm_sys_util::errno::errno_result(); + } + f(&mut termios); + // SAFETY: Safe because the syscall will only read the extent of termios and we check + // the return result. + let ret = unsafe { tcsetattr(fd, TCSANOW, &termios as *const _) }; + if ret < 0 { + return vmm_sys_util::errno::errno_result(); + } + + Ok(()) + } + + fn set_raw_mode(&self, f: &mut File) -> vmm_sys_util::errno::Result<()> { + // SAFETY: FFI call. Variable t is guaranteed to be a valid termios from modify_mode. + self.modify_mode(f.as_raw_fd(), |t| unsafe { cfmakeraw(t) }) + } + + fn listen_for_sigwinch_on_tty(&mut self, pty_main: File, pty_sub: File) -> std::io::Result<()> { + let seccomp_filter = get_seccomp_filter( + &self.seccomp_action, + Thread::PtyForeground, + self.hypervisor_type, + ) + .unwrap(); + + match start_sigwinch_listener(seccomp_filter, pty_main, pty_sub) { + Ok(pipe) => { + self.console_resize_pipe = Some(Arc::new(pipe)); + } + Err(e) => { + warn!("Ignoring error from setting up SIGWINCH listener: {}", e) + } + } + + Ok(()) + } + + fn add_virtio_console_device( + &mut self, + virtio_devices: &mut Vec, + console_pty: Option, + resize_pipe: Option, + ) -> DeviceManagerResult>> { + let console_config = self.config.lock().unwrap().console.clone(); + let endpoint = match console_config.mode { + ConsoleOutputMode::File => { + let file = File::create(console_config.file.as_ref().unwrap()) + .map_err(DeviceManagerError::ConsoleOutputFileOpen)?; + Endpoint::File(file) + } + ConsoleOutputMode::Pty => { + if let Some(pty) = console_pty { + self.config.lock().unwrap().console.file = Some(pty.path.clone()); + let file = pty.main.try_clone().unwrap(); + self.console_pty = Some(Arc::new(Mutex::new(pty))); + self.console_resize_pipe = resize_pipe.map(Arc::new); + Endpoint::PtyPair(file.try_clone().unwrap(), file) + } else { + let (main, mut sub, path) = + create_pty().map_err(DeviceManagerError::ConsolePtyOpen)?; + self.set_raw_mode(&mut sub) + .map_err(DeviceManagerError::SetPtyRaw)?; + self.config.lock().unwrap().console.file = Some(path.clone()); + let file = main.try_clone().unwrap(); + assert!(resize_pipe.is_none()); + self.listen_for_sigwinch_on_tty(main.try_clone().unwrap(), sub) + .unwrap(); + self.console_pty = Some(Arc::new(Mutex::new(PtyPair { main, path }))); + Endpoint::PtyPair(file.try_clone().unwrap(), file) + } + } + ConsoleOutputMode::Tty => { + // Duplicating the file descriptors like this is needed as otherwise + // they will be closed on a reboot and the numbers reused + + // SAFETY: FFI call to dup. Trivially safe. + let stdout = unsafe { libc::dup(libc::STDOUT_FILENO) }; + if stdout == -1 { + return vmm_sys_util::errno::errno_result().map_err(DeviceManagerError::DupFd); + } + // SAFETY: stdout is valid and owned solely by us. + let stdout = unsafe { File::from_raw_fd(stdout) }; + + // If an interactive TTY then we can accept input + // SAFETY: FFI call. Trivially safe. + if unsafe { libc::isatty(libc::STDIN_FILENO) == 1 } { + // SAFETY: FFI call to dup. Trivially safe. + let stdin = unsafe { libc::dup(libc::STDIN_FILENO) }; + if stdin == -1 { + return vmm_sys_util::errno::errno_result() + .map_err(DeviceManagerError::DupFd); + } + // SAFETY: stdin is valid and owned solely by us. + let stdin = unsafe { File::from_raw_fd(stdin) }; + + Endpoint::FilePair(stdout, stdin) + } else { + Endpoint::File(stdout) + } + } + ConsoleOutputMode::Null => Endpoint::Null, + ConsoleOutputMode::Off => return Ok(None), + }; + let id = String::from(CONSOLE_DEVICE_NAME); + + let (virtio_console_device, console_resizer) = virtio_devices::Console::new( + id.clone(), + endpoint, + self.console_resize_pipe + .as_ref() + .map(|p| p.try_clone().unwrap()), + self.force_iommu | console_config.iommu, + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVirtioConsole)?; + let virtio_console_device = Arc::new(Mutex::new(virtio_console_device)); + virtio_devices.push(MetaVirtioDevice { + virtio_device: Arc::clone(&virtio_console_device) + as Arc>, + iommu: console_config.iommu, + id: id.clone(), + pci_segment: 0, + dma_handler: None, + }); + + // Fill the device tree with a new node. In case of restore, we + // know there is nothing to do, so we can simply override the + // existing entry. + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, virtio_console_device)); + + // Only provide a resizer (for SIGWINCH handling) if the console is attached to the TTY + Ok( + if matches!(console_config.mode, ConsoleOutputMode::Tty) && console_config.sigwinch { + Some(console_resizer) + } else { + None + }, + ) + } + + fn add_console_device( + &mut self, + interrupt_manager: &Arc>, + virtio_devices: &mut Vec, + serial_pty: Option, + console_pty: Option, + console_resize_pipe: Option, + ) -> DeviceManagerResult> { + let serial_config = self.config.lock().unwrap().serial.clone(); + let serial_writer: Option> = match serial_config.mode { + ConsoleOutputMode::File => Some(Box::new( + File::create(serial_config.file.as_ref().unwrap()) + .map_err(DeviceManagerError::SerialOutputFileOpen)?, + )), + ConsoleOutputMode::Pty => { + if let Some(pty) = serial_pty { + self.config.lock().unwrap().serial.file = Some(pty.path.clone()); + self.serial_pty = Some(Arc::new(Mutex::new(pty))); + } else { + let (main, mut sub, path) = + create_pty().map_err(DeviceManagerError::SerialPtyOpen)?; + self.set_raw_mode(&mut sub) + .map_err(DeviceManagerError::SetPtyRaw)?; + self.config.lock().unwrap().serial.file = Some(path.clone()); + self.serial_pty = Some(Arc::new(Mutex::new(PtyPair { main, path }))); + } + None + } + ConsoleOutputMode::Tty => Some(Box::new(stdout())), + ConsoleOutputMode::Off | ConsoleOutputMode::Null => None, + }; + if serial_config.mode != ConsoleOutputMode::Off { + let serial = self.add_serial_device(interrupt_manager, serial_writer)?; + self.serial_manager = match serial_config.mode { + ConsoleOutputMode::Pty | ConsoleOutputMode::Tty => { + let serial_manager = + SerialManager::new(serial, self.serial_pty.clone(), serial_config.mode) + .map_err(DeviceManagerError::CreateSerialManager)?; + if let Some(mut serial_manager) = serial_manager { + serial_manager + .start_thread( + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + ) + .map_err(DeviceManagerError::SpawnSerialManager)?; + Some(Arc::new(serial_manager)) + } else { + None + } + } + _ => None, + }; + } + + let console_resizer = + self.add_virtio_console_device(virtio_devices, console_pty, console_resize_pipe)?; + + Ok(Arc::new(Console { console_resizer })) + } + + fn add_tpm_device( + &mut self, + tpm_path: PathBuf, + ) -> DeviceManagerResult>> { + // Create TPM Device + let tpm = devices::tpm::Tpm::new(tpm_path.to_str().unwrap().to_string()).map_err(|e| { + DeviceManagerError::CreateTpmDevice(anyhow!("Failed to create TPM Device : {:?}", e)) + })?; + let tpm = Arc::new(Mutex::new(tpm)); + + // Add TPM Device to mmio + self.address_manager + .mmio_bus + .insert( + tpm.clone(), + arch::layout::TPM_START.0, + arch::layout::TPM_SIZE, + ) + .map_err(DeviceManagerError::BusError)?; + + Ok(tpm) + } + + fn make_virtio_devices(&mut self) -> DeviceManagerResult> { + let mut devices: Vec = Vec::new(); + + // Create "standard" virtio devices (net/block/rng) + devices.append(&mut self.make_virtio_block_devices()?); + devices.append(&mut self.make_virtio_net_devices()?); + devices.append(&mut self.make_virtio_rng_devices()?); + + // Add virtio-fs if required + devices.append(&mut self.make_virtio_fs_devices()?); + + // Add virtio-pmem if required + devices.append(&mut self.make_virtio_pmem_devices()?); + + // Add virtio-vsock if required + devices.append(&mut self.make_virtio_vsock_devices()?); + + devices.append(&mut self.make_virtio_mem_devices()?); + + // Add virtio-balloon if required + devices.append(&mut self.make_virtio_balloon_devices()?); + + // Add virtio-watchdog device + devices.append(&mut self.make_virtio_watchdog_devices()?); + + // Add vDPA devices if required + devices.append(&mut self.make_vdpa_devices()?); + + Ok(devices) + } + + // Cache whether io_uring is supported to avoid probing for very block device + fn io_uring_is_supported(&mut self) -> bool { + if let Some(supported) = self.io_uring_supported { + return supported; + } + + let supported = block_io_uring_is_supported(); + self.io_uring_supported = Some(supported); + supported + } + + fn make_virtio_block_device( + &mut self, + disk_cfg: &mut DiskConfig, + ) -> DeviceManagerResult { + let id = if let Some(id) = &disk_cfg.id { + id.clone() + } else { + let id = self.next_device_name(DISK_DEVICE_NAME_PREFIX)?; + disk_cfg.id = Some(id.clone()); + id + }; + + info!("Creating virtio-block device: {:?}", disk_cfg); + + let (virtio_device, migratable_device) = if disk_cfg.vhost_user { + let socket = disk_cfg.vhost_socket.as_ref().unwrap().clone(); + let vu_cfg = VhostUserConfig { + socket, + num_queues: disk_cfg.num_queues, + queue_size: disk_cfg.queue_size, + }; + let vhost_user_block = Arc::new(Mutex::new( + match virtio_devices::vhost_user::Blk::new( + id.clone(), + vu_cfg, + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + self.force_iommu, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) { + Ok(vub_device) => vub_device, + Err(e) => { + return Err(DeviceManagerError::CreateVhostUserBlk(e)); + } + }, + )); + + ( + Arc::clone(&vhost_user_block) as Arc>, + vhost_user_block as Arc>, + ) + } else { + let mut options = OpenOptions::new(); + options.read(true); + options.write(!disk_cfg.readonly); + if disk_cfg.direct { + options.custom_flags(libc::O_DIRECT); + } + // Open block device path + let mut file: File = options + .open( + disk_cfg + .path + .as_ref() + .ok_or(DeviceManagerError::NoDiskPath)? + .clone(), + ) + .map_err(DeviceManagerError::Disk)?; + let image_type = + detect_image_type(&mut file).map_err(DeviceManagerError::DetectImageType)?; + + let image = match image_type { + ImageType::FixedVhd => { + // Use asynchronous backend relying on io_uring if the + // syscalls are supported. + if !disk_cfg.disable_io_uring && self.io_uring_is_supported() { + info!("Using asynchronous fixed VHD disk file (io_uring)"); + Box::new( + FixedVhdDiskAsync::new(file) + .map_err(DeviceManagerError::CreateFixedVhdDiskAsync)?, + ) as Box + } else { + info!("Using synchronous fixed VHD disk file"); + Box::new( + FixedVhdDiskSync::new(file) + .map_err(DeviceManagerError::CreateFixedVhdDiskSync)?, + ) as Box + } + } + ImageType::Raw => { + // Use asynchronous backend relying on io_uring if the + // syscalls are supported. + if !disk_cfg.disable_io_uring && self.io_uring_is_supported() { + info!("Using asynchronous RAW disk file (io_uring)"); + Box::new(RawFileDisk::new(file)) as Box + } else { + info!("Using synchronous RAW disk file"); + Box::new(RawFileDiskSync::new(file)) as Box + } + } + ImageType::Qcow2 => { + info!("Using synchronous QCOW disk file"); + Box::new( + QcowDiskSync::new(file, disk_cfg.direct) + .map_err(DeviceManagerError::CreateQcowDiskSync)?, + ) as Box + } + ImageType::Vhdx => { + info!("Using synchronous VHDX disk file"); + Box::new( + VhdxDiskSync::new(file) + .map_err(DeviceManagerError::CreateFixedVhdxDiskSync)?, + ) as Box + } + }; + + let virtio_block = Arc::new(Mutex::new( + virtio_devices::Block::new( + id.clone(), + image, + disk_cfg + .path + .as_ref() + .ok_or(DeviceManagerError::NoDiskPath)? + .clone(), + disk_cfg.readonly, + self.force_iommu | disk_cfg.iommu, + disk_cfg.num_queues, + disk_cfg.queue_size, + self.seccomp_action.clone(), + disk_cfg.rate_limiter_config, + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVirtioBlock)?, + )); + + ( + Arc::clone(&virtio_block) as Arc>, + virtio_block as Arc>, + ) + }; + + // Fill the device tree with a new node. In case of restore, we + // know there is nothing to do, so we can simply override the + // existing entry. + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, migratable_device)); + + Ok(MetaVirtioDevice { + virtio_device, + iommu: disk_cfg.iommu, + id, + pci_segment: disk_cfg.pci_segment, + dma_handler: None, + }) + } + + fn make_virtio_block_devices(&mut self) -> DeviceManagerResult> { + let mut devices = Vec::new(); + + let mut block_devices = self.config.lock().unwrap().disks.clone(); + if let Some(disk_list_cfg) = &mut block_devices { + for disk_cfg in disk_list_cfg.iter_mut() { + devices.push(self.make_virtio_block_device(disk_cfg)?); + } + } + self.config.lock().unwrap().disks = block_devices; + + Ok(devices) + } + + fn make_virtio_net_device( + &mut self, + net_cfg: &mut NetConfig, + ) -> DeviceManagerResult { + let id = if let Some(id) = &net_cfg.id { + id.clone() + } else { + let id = self.next_device_name(NET_DEVICE_NAME_PREFIX)?; + net_cfg.id = Some(id.clone()); + id + }; + info!("Creating virtio-net device: {:?}", net_cfg); + + let (virtio_device, migratable_device) = if net_cfg.vhost_user { + let socket = net_cfg.vhost_socket.as_ref().unwrap().clone(); + let vu_cfg = VhostUserConfig { + socket, + num_queues: net_cfg.num_queues, + queue_size: net_cfg.queue_size, + }; + let server = match net_cfg.vhost_mode { + VhostMode::Client => false, + VhostMode::Server => true, + }; + let vhost_user_net = Arc::new(Mutex::new( + match virtio_devices::vhost_user::Net::new( + id.clone(), + net_cfg.mac, + net_cfg.mtu, + vu_cfg, + server, + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + self.force_iommu, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) { + Ok(vun_device) => vun_device, + Err(e) => { + return Err(DeviceManagerError::CreateVhostUserNet(e)); + } + }, + )); + + ( + Arc::clone(&vhost_user_net) as Arc>, + vhost_user_net as Arc>, + ) + } else { + let state = state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?; + + let virtio_net = if let Some(ref tap_if_name) = net_cfg.tap { + Arc::new(Mutex::new( + virtio_devices::Net::new( + id.clone(), + Some(tap_if_name), + Some(net_cfg.ip), + Some(net_cfg.mask), + Some(net_cfg.mac), + &mut net_cfg.host_mac, + net_cfg.mtu, + self.force_iommu | net_cfg.iommu, + net_cfg.num_queues, + net_cfg.queue_size, + self.seccomp_action.clone(), + net_cfg.rate_limiter_config, + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state, + Some(self.sandbox_id.clone()), + ) + .map_err(DeviceManagerError::CreateVirtioNet)?, + )) + } else if let Some(fds) = &net_cfg.fds { + Arc::new(Mutex::new( + virtio_devices::Net::from_tap_fds( + id.clone(), + fds, + Some(net_cfg.mac), + net_cfg.mtu, + self.force_iommu | net_cfg.iommu, + net_cfg.queue_size, + self.seccomp_action.clone(), + net_cfg.rate_limiter_config, + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state, + ) + .map_err(DeviceManagerError::CreateVirtioNet)?, + )) + } else { + Arc::new(Mutex::new( + virtio_devices::Net::new( + id.clone(), + None, + Some(net_cfg.ip), + Some(net_cfg.mask), + Some(net_cfg.mac), + &mut net_cfg.host_mac, + net_cfg.mtu, + self.force_iommu | net_cfg.iommu, + net_cfg.num_queues, + net_cfg.queue_size, + self.seccomp_action.clone(), + net_cfg.rate_limiter_config, + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state, + None, + ) + .map_err(DeviceManagerError::CreateVirtioNet)?, + )) + }; + + ( + Arc::clone(&virtio_net) as Arc>, + virtio_net as Arc>, + ) + }; + + // Fill the device tree with a new node. In case of restore, we + // know there is nothing to do, so we can simply override the + // existing entry. + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, migratable_device)); + + Ok(MetaVirtioDevice { + virtio_device, + iommu: net_cfg.iommu, + id, + pci_segment: net_cfg.pci_segment, + dma_handler: None, + }) + } + + /// Add virto-net and vhost-user-net devices + fn make_virtio_net_devices(&mut self) -> DeviceManagerResult> { + let mut devices = Vec::new(); + let mut net_devices = self.config.lock().unwrap().net.clone(); + if let Some(net_list_cfg) = &mut net_devices { + for net_cfg in net_list_cfg.iter_mut() { + devices.push(self.make_virtio_net_device(net_cfg)?); + } + } + self.config.lock().unwrap().net = net_devices; + + Ok(devices) + } + + fn make_virtio_rng_devices(&mut self) -> DeviceManagerResult> { + let mut devices = Vec::new(); + + // Add virtio-rng if required + let rng_config = self.config.lock().unwrap().rng.clone(); + if let Some(rng_path) = rng_config.src.to_str() { + info!("Creating virtio-rng device: {:?}", rng_config); + let id = String::from(RNG_DEVICE_NAME); + + let virtio_rng_device = Arc::new(Mutex::new( + virtio_devices::Rng::new( + id.clone(), + rng_path, + self.force_iommu | rng_config.iommu, + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVirtioRng)?, + )); + devices.push(MetaVirtioDevice { + virtio_device: Arc::clone(&virtio_rng_device) + as Arc>, + iommu: rng_config.iommu, + id: id.clone(), + pci_segment: 0, + dma_handler: None, + }); + + // Fill the device tree with a new node. In case of restore, we + // know there is nothing to do, so we can simply override the + // existing entry. + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, virtio_rng_device)); + } + + Ok(devices) + } + + fn make_vhost_virtio_fs_device( + &mut self, + id: String, + mut node: DeviceNode, + fs_cfg: &FsConfig, + ) -> DeviceManagerResult { + let virtio_fs_device; + if let Some(fs_socket) = fs_cfg.socket.to_str() { + virtio_fs_device = Arc::new(Mutex::new( + virtio_devices::vhost_user::Fs::new( + id.clone(), + fs_socket, + &fs_cfg.tag, + fs_cfg.num_queues, + fs_cfg.queue_size, + None, + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + self.force_iommu, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVirtioFs)?, + )); + } else { + return Err(DeviceManagerError::NoVirtioFsSock); + } + + // Update the device tree with the migratable device. + node.migratable = Some(Arc::clone(&virtio_fs_device) as Arc>); + self.device_tree.lock().unwrap().insert(id.clone(), node); + Ok(MetaVirtioDevice { + virtio_device: Arc::clone(&virtio_fs_device) + as Arc>, + iommu: false, + id, + pci_segment: fs_cfg.pci_segment, + dma_handler: None, + }) + } + + fn make_native_virtio_fs_device( + &mut self, + id: String, + mut node: DeviceNode, + fs_cfg: &FsConfig, + bfs_cfg: &BackendFsConfig, + ) -> DeviceManagerResult { + let dev_evt = EventFd::new(EFD_NONBLOCK).unwrap(); + let dev_fd = dev_evt.as_raw_fd(); + let pending_dev_message = Arc::new(Mutex::new(Vec::default())); + let virtio_fs_device = Arc::new(Mutex::new( + virtio_devices::Fs::new( + id.clone(), + &fs_cfg.tag, + fs_cfg.num_queues, + fs_cfg.queue_size, + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + self.force_iommu, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + bfs_cfg, + None, + fs_cfg.rate_limiter_config, + dev_evt, + pending_dev_message.clone(), + ) + .map_err(DeviceManagerError::CreateNativeVirtioFs)?, + )); + + // Update the device tree with the migratable device. + node.migratable = Some(Arc::clone(&virtio_fs_device) as Arc>); + node.dev_fd = Some(dev_fd); + node.pending_dev_message = Some(pending_dev_message); + self.device_tree.lock().unwrap().insert(id.clone(), node); + Ok(MetaVirtioDevice { + virtio_device: Arc::clone(&virtio_fs_device) + as Arc>, + iommu: false, + id, + pci_segment: fs_cfg.pci_segment, + dma_handler: None, + }) + } + + fn make_virtio_fs_device( + &mut self, + fs_cfg: &mut FsConfig, + ) -> DeviceManagerResult { + let id = if let Some(id) = &fs_cfg.id { + id.clone() + } else { + let id = self.next_device_name(FS_DEVICE_NAME_PREFIX)?; + fs_cfg.id = Some(id.clone()); + id + }; + + info!("Creating virtio-fs device: {:?}", fs_cfg); + + let node = device_node!(id); + + if let Some(bfs_cfg) = &fs_cfg.backendfs_config { + self.make_native_virtio_fs_device(id, node, fs_cfg, bfs_cfg) + } else { + self.make_vhost_virtio_fs_device(id, node, fs_cfg) + } + } + + fn make_virtio_fs_devices(&mut self) -> DeviceManagerResult> { + let mut devices = Vec::new(); + + let mut fs_devices = self.config.lock().unwrap().fs.clone(); + if let Some(fs_list_cfg) = &mut fs_devices { + for fs_cfg in fs_list_cfg.iter_mut() { + devices.push(self.make_virtio_fs_device(fs_cfg)?); + } + } + self.config.lock().unwrap().fs = fs_devices; + + Ok(devices) + } + + fn make_virtio_pmem_device( + &mut self, + pmem_cfg: &mut PmemConfig, + ) -> DeviceManagerResult { + let id = if let Some(id) = &pmem_cfg.id { + id.clone() + } else { + let id = self.next_device_name(PMEM_DEVICE_NAME_PREFIX)?; + pmem_cfg.id = Some(id.clone()); + id + }; + + info!("Creating virtio-pmem device: {:?}", pmem_cfg); + + let mut node = device_node!(id); + + // Look for the id in the device tree. If it can be found, that means + // the device is being restored, otherwise it's created from scratch. + let region_range = if let Some(node) = self.device_tree.lock().unwrap().get(&id) { + debug!("Restoring virtio-pmem {} resources", id); + + let mut region_range: Option<(u64, u64)> = None; + for resource in node.resources.iter() { + match resource { + Resource::MmioAddressRange { base, size } => { + if region_range.is_some() { + return Err(DeviceManagerError::ResourceAlreadyExists); + } + + region_range = Some((*base, *size)); + } + _ => { + error!("Unexpected resource {:?} for {}", resource, id); + } + } + } + + if region_range.is_none() { + return Err(DeviceManagerError::MissingVirtioPmemResources); + } + + region_range + } else { + None + }; + + let (custom_flags, set_len) = if pmem_cfg.file.is_dir() { + if pmem_cfg.size.is_none() { + return Err(DeviceManagerError::PmemWithDirectorySizeMissing); + } + (O_TMPFILE, true) + } else { + (0, false) + }; + + let mut file = OpenOptions::new() + .read(true) + .write(!pmem_cfg.discard_writes) + .custom_flags(custom_flags) + .open(&pmem_cfg.file) + .map_err(DeviceManagerError::PmemFileOpen)?; + + let size = if let Some(size) = pmem_cfg.size { + if set_len { + file.set_len(size) + .map_err(DeviceManagerError::PmemFileSetLen)?; + } + size + } else { + file.seek(SeekFrom::End(0)) + .map_err(DeviceManagerError::PmemFileSetLen)? + }; + + if size % 0x20_0000 != 0 { + return Err(DeviceManagerError::PmemSizeNotAligned); + } + + let (region_base, region_size) = if let Some((base, size)) = region_range { + // The memory needs to be 2MiB aligned in order to support + // hugepages. + self.pci_segments[pmem_cfg.pci_segment as usize] + .allocator + .lock() + .unwrap() + .allocate( + Some(GuestAddress(base)), + size as GuestUsize, + Some(0x0020_0000), + ) + .ok_or(DeviceManagerError::PmemRangeAllocation)?; + + (base, size) + } else { + // The memory needs to be 2MiB aligned in order to support + // hugepages. + let base = self.pci_segments[pmem_cfg.pci_segment as usize] + .allocator + .lock() + .unwrap() + .allocate(None, size as GuestUsize, Some(0x0020_0000)) + .ok_or(DeviceManagerError::PmemRangeAllocation)?; + + (base.raw_value(), size) + }; + + let cloned_file = file.try_clone().map_err(DeviceManagerError::CloneFile)?; + let mmap_region = MmapRegion::build( + Some(FileOffset::new(cloned_file, 0)), + region_size as usize, + if pmem_cfg.discard_writes { + PROT_READ + } else { + PROT_READ | PROT_WRITE + }, + MAP_NORESERVE + | if pmem_cfg.discard_writes { + MAP_PRIVATE + } else { + MAP_SHARED + }, + ) + .map_err(DeviceManagerError::NewMmapRegion)?; + let host_addr: u64 = mmap_region.as_ptr() as u64; + + let mem_slot = self + .memory_manager + .lock() + .unwrap() + .create_userspace_mapping( + region_base, + region_size, + host_addr, + false, + pmem_cfg.discard_writes, + false, + ) + .map_err(DeviceManagerError::MemoryManager)?; + + // Wrap the MmapRegion into a GuestRegionMmap so it can be inserted + // into MemoryManager::device_memory. Ownership is shared via Arc: + // the same Arc is also handed to virtio_devices::Pmem so that + // munmap happens automatically when the device (and the device_memory + // entry) are dropped. + let pmem_guest_region = Arc::new( + GuestRegionMmap::new(mmap_region, GuestAddress(region_base)) + .map_err(DeviceManagerError::NewGuestRegion)?, + ); + self.memory_manager + .lock() + .unwrap() + .add_device_region(pmem_guest_region.clone()) + .map_err(DeviceManagerError::MemoryManager)?; + + let mapping = virtio_devices::UserspaceMapping { + host_addr, + mem_slot, + addr: GuestAddress(region_base), + len: region_size, + mergeable: false, + }; + + let virtio_pmem_device = Arc::new(Mutex::new( + virtio_devices::Pmem::new( + id.clone(), + file, + GuestAddress(region_base), + mapping, + pmem_guest_region, + self.force_iommu | pmem_cfg.iommu, + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVirtioPmem)?, + )); + + // Update the device tree with correct resource information and with + // the migratable device. + node.resources.push(Resource::MmioAddressRange { + base: region_base, + size: region_size, + }); + node.migratable = Some(Arc::clone(&virtio_pmem_device) as Arc>); + self.device_tree.lock().unwrap().insert(id.clone(), node); + + Ok(MetaVirtioDevice { + virtio_device: Arc::clone(&virtio_pmem_device) + as Arc>, + iommu: pmem_cfg.iommu, + id, + pci_segment: pmem_cfg.pci_segment, + dma_handler: None, + }) + } + + fn make_virtio_pmem_devices(&mut self) -> DeviceManagerResult> { + let mut devices = Vec::new(); + // Add virtio-pmem if required + let mut pmem_devices = self.config.lock().unwrap().pmem.clone(); + if let Some(pmem_list_cfg) = &mut pmem_devices { + for pmem_cfg in pmem_list_cfg.iter_mut() { + devices.push(self.make_virtio_pmem_device(pmem_cfg)?); + } + } + self.config.lock().unwrap().pmem = pmem_devices; + + Ok(devices) + } + + fn make_virtio_vsock_device( + &mut self, + vsock_cfg: &mut VsockConfig, + ) -> DeviceManagerResult { + let id = if let Some(id) = &vsock_cfg.id { + id.clone() + } else { + let id = self.next_device_name(VSOCK_DEVICE_NAME_PREFIX)?; + vsock_cfg.id = Some(id.clone()); + id + }; + let backend_id = format!("{id}-backend"); + + info!("Creating virtio-vsock device: {:?}", vsock_cfg); + + let socket_path = vsock_cfg + .socket + .to_str() + .ok_or(DeviceManagerError::CreateVsockConvertPath)?; + + let vsock_snapshot = snapshot_from_id(self.snapshot.as_ref(), id.as_str()); + let backend = virtio_devices::vsock::VsockUnixBackend::new( + backend_id.clone(), + vsock_cfg.cid, + socket_path.to_string(), + vsock_cfg.muxer_epoll_nested, + state_from_id(vsock_snapshot.as_ref(), backend_id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVsockBackend)?; + + let vsock_device = Arc::new(Mutex::new( + virtio_devices::Vsock::new( + id.clone(), + vsock_cfg.cid, + vsock_cfg.socket.clone(), + backend, + self.force_iommu | vsock_cfg.iommu, + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVirtioVsock)?, + )); + + // Fill the device tree with a new node. In case of restore, we + // know there is nothing to do, so we can simply override the + // existing entry. + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, vsock_device)); + + Ok(MetaVirtioDevice { + virtio_device: Arc::clone(&vsock_device) + as Arc>, + iommu: vsock_cfg.iommu, + id, + pci_segment: vsock_cfg.pci_segment, + dma_handler: None, + }) + } + + fn make_virtio_vsock_devices(&mut self) -> DeviceManagerResult> { + let mut devices = Vec::new(); + + let mut vsock = self.config.lock().unwrap().vsock.clone(); + if let Some(ref mut vsock_cfg) = &mut vsock { + devices.push(self.make_virtio_vsock_device(vsock_cfg)?); + } + self.config.lock().unwrap().vsock = vsock; + + Ok(devices) + } + + fn make_virtio_mem_devices(&mut self) -> DeviceManagerResult> { + let mut devices = Vec::new(); + + let mm = self.memory_manager.clone(); + let mut mm = mm.lock().unwrap(); + for (memory_zone_id, memory_zone) in mm.memory_zones_mut().iter_mut() { + if let Some(virtio_mem_zone) = memory_zone.virtio_mem_zone_mut() { + info!("Creating virtio-mem device: id = {}", memory_zone_id); + + let node_id = numa_node_id_from_memory_zone_id(&self.numa_nodes, memory_zone_id) + .map(|i| i as u16); + + let virtio_mem_device = Arc::new(Mutex::new( + virtio_devices::Mem::new( + memory_zone_id.clone(), + virtio_mem_zone.region(), + self.seccomp_action.clone(), + node_id, + virtio_mem_zone.hotplugged_size(), + virtio_mem_zone.hugepages(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + virtio_mem_zone.blocks_state().clone(), + state_from_id(self.snapshot.as_ref(), memory_zone_id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVirtioMem)?, + )); + + // Update the virtio-mem zone so that it has a handle onto the + // virtio-mem device, which will be used for triggering a resize + // if needed. + virtio_mem_zone.set_virtio_device(Arc::clone(&virtio_mem_device)); + + self.virtio_mem_devices.push(Arc::clone(&virtio_mem_device)); + + devices.push(MetaVirtioDevice { + virtio_device: Arc::clone(&virtio_mem_device) + as Arc>, + iommu: false, + id: memory_zone_id.clone(), + pci_segment: 0, + dma_handler: None, + }); + + // Fill the device tree with a new node. In case of restore, we + // know there is nothing to do, so we can simply override the + // existing entry. + self.device_tree.lock().unwrap().insert( + memory_zone_id.clone(), + device_node!(memory_zone_id, virtio_mem_device), + ); + } + } + + Ok(devices) + } + + fn make_virtio_balloon_devices(&mut self) -> DeviceManagerResult> { + let mut devices = Vec::new(); + + if let Some(balloon_config) = &self.config.lock().unwrap().balloon { + let id = String::from(BALLOON_DEVICE_NAME); + info!("Creating virtio-balloon device: id = {}", id); + + let virtio_balloon_device = Arc::new(Mutex::new( + virtio_devices::Balloon::new( + id.clone(), + balloon_config.size, + balloon_config.deflate_on_oom, + balloon_config.free_page_reporting, + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVirtioBalloon)?, + )); + + self.balloon = Some(virtio_balloon_device.clone()); + + devices.push(MetaVirtioDevice { + virtio_device: Arc::clone(&virtio_balloon_device) + as Arc>, + iommu: false, + id: id.clone(), + pci_segment: 0, + dma_handler: None, + }); + + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, virtio_balloon_device)); + } + + Ok(devices) + } + + fn make_virtio_watchdog_devices(&mut self) -> DeviceManagerResult> { + let mut devices = Vec::new(); + + if !self.config.lock().unwrap().watchdog { + return Ok(devices); + } + + let id = String::from(WATCHDOG_DEVICE_NAME); + info!("Creating virtio-watchdog device: id = {}", id); + + let virtio_watchdog_device = Arc::new(Mutex::new( + virtio_devices::Watchdog::new( + id.clone(), + self.reset_evt.try_clone().unwrap(), + self.seccomp_action.clone(), + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVirtioWatchdog)?, + )); + devices.push(MetaVirtioDevice { + virtio_device: Arc::clone(&virtio_watchdog_device) + as Arc>, + iommu: false, + id: id.clone(), + pci_segment: 0, + dma_handler: None, + }); + + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, virtio_watchdog_device)); + + Ok(devices) + } + + fn make_vdpa_device( + &mut self, + vdpa_cfg: &mut VdpaConfig, + ) -> DeviceManagerResult { + let id = if let Some(id) = &vdpa_cfg.id { + id.clone() + } else { + let id = self.next_device_name(VDPA_DEVICE_NAME_PREFIX)?; + vdpa_cfg.id = Some(id.clone()); + id + }; + + info!("Creating vDPA device: {:?}", vdpa_cfg); + + let device_path = vdpa_cfg + .path + .to_str() + .ok_or(DeviceManagerError::CreateVdpaConvertPath)?; + + let vdpa_device = Arc::new(Mutex::new( + virtio_devices::Vdpa::new( + id.clone(), + device_path, + self.memory_manager.lock().unwrap().device_memory(), + vdpa_cfg.num_queues as u16, + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::CreateVdpa)?, + )); + + // Create the DMA handler that is required by the vDPA device + let vdpa_mapping = Arc::new(VdpaDmaMapping::new( + Arc::clone(&vdpa_device), + Arc::new(self.memory_manager.lock().unwrap().device_memory()), + )); + + self.device_tree + .lock() + .unwrap() + .insert(id.clone(), device_node!(id, vdpa_device)); + + Ok(MetaVirtioDevice { + virtio_device: vdpa_device as Arc>, + iommu: vdpa_cfg.iommu, + id, + pci_segment: vdpa_cfg.pci_segment, + dma_handler: Some(vdpa_mapping), + }) + } + + fn make_vdpa_devices(&mut self) -> DeviceManagerResult> { + let mut devices = Vec::new(); + // Add vdpa if required + let mut vdpa_devices = self.config.lock().unwrap().vdpa.clone(); + if let Some(vdpa_list_cfg) = &mut vdpa_devices { + for vdpa_cfg in vdpa_list_cfg.iter_mut() { + devices.push(self.make_vdpa_device(vdpa_cfg)?); + } + } + self.config.lock().unwrap().vdpa = vdpa_devices; + + Ok(devices) + } + + fn next_device_name(&mut self, prefix: &str) -> DeviceManagerResult { + let start_id = self.device_id_cnt; + loop { + // Generate the temporary name. + let name = format!("{}{}", prefix, self.device_id_cnt); + // Increment the counter. + self.device_id_cnt += Wrapping(1); + // Check if the name is already in use. + if !self.boot_id_list.contains(&name) + && !self.device_tree.lock().unwrap().contains_key(&name) + { + return Ok(name); + } + + if self.device_id_cnt == start_id { + // We went through a full loop and there's nothing else we can + // do. + break; + } + } + Err(DeviceManagerError::NoAvailableDeviceName) + } + + fn add_passthrough_device( + &mut self, + device_cfg: &mut DeviceConfig, + ) -> DeviceManagerResult<(PciBdf, String)> { + // If the passthrough device has not been created yet, it is created + // here and stored in the DeviceManager structure for future needs. + if self.passthrough_device.is_none() { + self.passthrough_device = Some( + self.address_manager + .vm + .create_passthrough_device() + .map_err(|e| DeviceManagerError::CreatePassthroughDevice(e.into()))?, + ); + } + + self.add_vfio_device(device_cfg) + } + + fn create_vfio_container(&self) -> DeviceManagerResult> { + let passthrough_device = self + .passthrough_device + .as_ref() + .ok_or(DeviceManagerError::NoDevicePassthroughSupport)?; + + let dup = passthrough_device + .try_clone() + .map_err(DeviceManagerError::VfioCreate)?; + + Ok(Arc::new( + VfioContainer::new(Some(Arc::new(dup))).map_err(DeviceManagerError::VfioCreate)?, + )) + } + + fn add_vfio_device( + &mut self, + device_cfg: &mut DeviceConfig, + ) -> DeviceManagerResult<(PciBdf, String)> { + let vfio_name = if let Some(id) = &device_cfg.id { + id.clone() + } else { + let id = self.next_device_name(VFIO_DEVICE_NAME_PREFIX)?; + device_cfg.id = Some(id.clone()); + id + }; + + let (pci_segment_id, pci_device_bdf, resources) = + self.pci_resources(&vfio_name, device_cfg.pci_segment)?; + + let mut needs_dma_mapping = false; + + // Here we create a new VFIO container for two reasons. Either this is + // the first VFIO device, meaning we need a new VFIO container, which + // will be shared with other VFIO devices. Or the new VFIO device is + // attached to a vIOMMU, meaning we must create a dedicated VFIO + // container. In the vIOMMU use case, we can't let all devices under + // the same VFIO container since we couldn't map/unmap memory for each + // device. That's simply because the map/unmap operations happen at the + // VFIO container level. + let vfio_container = if device_cfg.iommu { + let vfio_container = self.create_vfio_container()?; + + let vfio_mapping = Arc::new(VfioDmaMapping::new( + Arc::clone(&vfio_container), + Arc::new(self.memory_manager.lock().unwrap().device_memory()), + )); + + if let Some(iommu) = &self.iommu_device { + iommu + .lock() + .unwrap() + .add_external_mapping(pci_device_bdf.into(), vfio_mapping); + } else { + return Err(DeviceManagerError::MissingVirtualIommu); + } + + vfio_container + } else if let Some(vfio_container) = &self.vfio_container { + Arc::clone(vfio_container) + } else { + let vfio_container = self.create_vfio_container()?; + needs_dma_mapping = true; + self.vfio_container = Some(Arc::clone(&vfio_container)); + + vfio_container + }; + + let vfio_device = VfioDevice::new(&device_cfg.path, Arc::clone(&vfio_container)) + .map_err(DeviceManagerError::VfioCreate)?; + + if needs_dma_mapping { + // Register DMA mapping in IOMMU. + // Do not register virtio-mem regions, as they are handled directly by + // virtio-mem device itself. + for (_, zone) in self.memory_manager.lock().unwrap().memory_zones().iter() { + for region in zone.regions() { + vfio_container + .vfio_dma_map( + region.start_addr().raw_value(), + region.len(), + region.as_ptr() as u64, + ) + .map_err(DeviceManagerError::VfioDmaMap)?; + } + } + + let vfio_mapping = Arc::new(VfioDmaMapping::new( + Arc::clone(&vfio_container), + Arc::new(self.memory_manager.lock().unwrap().device_memory()), + )); + + for virtio_mem_device in self.virtio_mem_devices.iter() { + virtio_mem_device + .lock() + .unwrap() + .add_dma_mapping_handler( + VirtioMemMappingSource::Container, + vfio_mapping.clone(), + ) + .map_err(DeviceManagerError::AddDmaMappingHandlerVirtioMem)?; + } + } + + let legacy_interrupt_group = + if let Some(legacy_interrupt_manager) = &self.legacy_interrupt_manager { + Some( + legacy_interrupt_manager + .create_group(LegacyIrqGroupConfig { + irq: self.pci_segments[pci_segment_id as usize].pci_irq_slots + [pci_device_bdf.device() as usize] + as InterruptIndex, + }) + .map_err(DeviceManagerError::CreateInterruptGroup)?, + ) + } else { + None + }; + + let memory_manager = self.memory_manager.clone(); + + let vfio_pci_device = VfioPciDevice::new( + vfio_name.clone(), + &self.address_manager.vm, + vfio_device, + vfio_container, + self.msi_interrupt_manager.clone(), + legacy_interrupt_group, + device_cfg.iommu, + pci_device_bdf, + self.restoring, + Arc::new(move || memory_manager.lock().unwrap().allocate_memory_slot()), + ) + .map_err(DeviceManagerError::VfioPciCreate)?; + + let vfio_pci_device = Arc::new(Mutex::new(vfio_pci_device)); + + let new_resources = self.add_pci_device( + vfio_pci_device.clone(), + vfio_pci_device.clone(), + pci_segment_id, + pci_device_bdf, + resources, + )?; + + // When restoring a VM, the restore codepath will take care of mapping + // the MMIO regions based on the information from the snapshot. + if !self.restoring { + vfio_pci_device + .lock() + .unwrap() + .map_mmio_regions() + .map_err(DeviceManagerError::VfioMapRegion)?; + } + + let mut node = device_node!(vfio_name, vfio_pci_device); + + // Update the device tree with correct resource information. + node.resources = new_resources; + node.pci_bdf = Some(pci_device_bdf); + node.pci_device_handle = Some(PciDeviceHandle::Vfio(vfio_pci_device)); + + self.device_tree + .lock() + .unwrap() + .insert(vfio_name.clone(), node); + + Ok((pci_device_bdf, vfio_name)) + } + + fn add_pci_device( + &mut self, + bus_device: Arc>, + pci_device: Arc>, + segment_id: u16, + bdf: PciBdf, + resources: Option>, + ) -> DeviceManagerResult> { + let bars = pci_device + .lock() + .unwrap() + .allocate_bars( + &self.address_manager.allocator, + &mut self.pci_segments[segment_id as usize] + .allocator + .lock() + .unwrap(), + resources, + ) + .map_err(DeviceManagerError::AllocateBars)?; + + let mut pci_bus = self.pci_segments[segment_id as usize] + .pci_bus + .lock() + .unwrap(); + + pci_bus + .add_device(bdf.device() as u32, pci_device) + .map_err(DeviceManagerError::AddPciDevice)?; + + self.bus_devices.push(Arc::clone(&bus_device)); + + pci_bus + .register_mapping( + bus_device, + #[cfg(target_arch = "x86_64")] + self.address_manager.io_bus.as_ref(), + self.address_manager.mmio_bus.as_ref(), + bars.clone(), + ) + .map_err(DeviceManagerError::AddPciDevice)?; + + let mut new_resources = Vec::new(); + for bar in bars { + new_resources.push(Resource::PciBar { + index: bar.idx(), + base: bar.addr(), + size: bar.size(), + type_: bar.region_type().into(), + prefetchable: bar.prefetchable().into(), + }); + } + + Ok(new_resources) + } + + fn add_vfio_devices(&mut self) -> DeviceManagerResult> { + let mut iommu_attached_device_ids = Vec::new(); + let mut devices = self.config.lock().unwrap().devices.clone(); + + if let Some(device_list_cfg) = &mut devices { + for device_cfg in device_list_cfg.iter_mut() { + let (device_id, _) = self.add_passthrough_device(device_cfg)?; + if device_cfg.iommu && self.iommu_device.is_some() { + iommu_attached_device_ids.push(device_id); + } + } + } + + // Update the list of devices + self.config.lock().unwrap().devices = devices; + + Ok(iommu_attached_device_ids) + } + + fn add_vfio_user_device( + &mut self, + device_cfg: &mut UserDeviceConfig, + ) -> DeviceManagerResult<(PciBdf, String)> { + let vfio_user_name = if let Some(id) = &device_cfg.id { + id.clone() + } else { + let id = self.next_device_name(VFIO_USER_DEVICE_NAME_PREFIX)?; + device_cfg.id = Some(id.clone()); + id + }; + + let (pci_segment_id, pci_device_bdf, resources) = + self.pci_resources(&vfio_user_name, device_cfg.pci_segment)?; + + let legacy_interrupt_group = + if let Some(legacy_interrupt_manager) = &self.legacy_interrupt_manager { + Some( + legacy_interrupt_manager + .create_group(LegacyIrqGroupConfig { + irq: self.pci_segments[pci_segment_id as usize].pci_irq_slots + [pci_device_bdf.device() as usize] + as InterruptIndex, + }) + .map_err(DeviceManagerError::CreateInterruptGroup)?, + ) + } else { + None + }; + + let client = Arc::new(Mutex::new( + vfio_user::Client::new(&device_cfg.socket) + .map_err(DeviceManagerError::VfioUserCreateClient)?, + )); + + let memory_manager = self.memory_manager.clone(); + + let mut vfio_user_pci_device = VfioUserPciDevice::new( + vfio_user_name.clone(), + &self.address_manager.vm, + client.clone(), + self.msi_interrupt_manager.clone(), + legacy_interrupt_group, + pci_device_bdf, + self.restoring, + Arc::new(move || memory_manager.lock().unwrap().allocate_memory_slot()), + ) + .map_err(DeviceManagerError::VfioUserCreate)?; + + let memory = self.memory_manager.lock().unwrap().device_memory(); + let vfio_user_mapping = Arc::new(VfioUserDmaMapping::new(client, Arc::new(memory))); + for virtio_mem_device in self.virtio_mem_devices.iter() { + virtio_mem_device + .lock() + .unwrap() + .add_dma_mapping_handler( + VirtioMemMappingSource::Device(pci_device_bdf.into()), + vfio_user_mapping.clone(), + ) + .map_err(DeviceManagerError::AddDmaMappingHandlerVirtioMem)?; + } + + for (_, zone) in self.memory_manager.lock().unwrap().memory_zones().iter() { + for region in zone.regions() { + vfio_user_pci_device + .dma_map(region) + .map_err(DeviceManagerError::VfioUserDmaMap)?; + } + } + + let vfio_user_pci_device = Arc::new(Mutex::new(vfio_user_pci_device)); + + let new_resources = self.add_pci_device( + vfio_user_pci_device.clone(), + vfio_user_pci_device.clone(), + pci_segment_id, + pci_device_bdf, + resources, + )?; + + // When restoring a VM, the restore codepath will take care of mapping + // the MMIO regions based on the information from the snapshot. + if !self.restoring { + // Note it is required to call 'add_pci_device()' in advance to have the list of + // mmio regions provisioned correctly + vfio_user_pci_device + .lock() + .unwrap() + .map_mmio_regions() + .map_err(DeviceManagerError::VfioUserMapRegion)?; + } + + let mut node = device_node!(vfio_user_name, vfio_user_pci_device); + + // Update the device tree with correct resource information. + node.resources = new_resources; + node.pci_bdf = Some(pci_device_bdf); + node.pci_device_handle = Some(PciDeviceHandle::VfioUser(vfio_user_pci_device)); + + self.device_tree + .lock() + .unwrap() + .insert(vfio_user_name.clone(), node); + + Ok((pci_device_bdf, vfio_user_name)) + } + + fn add_user_devices(&mut self) -> DeviceManagerResult> { + let mut user_devices = self.config.lock().unwrap().user_devices.clone(); + + if let Some(device_list_cfg) = &mut user_devices { + for device_cfg in device_list_cfg.iter_mut() { + let (_device_id, _id) = self.add_vfio_user_device(device_cfg)?; + } + } + + // Update the list of devices + self.config.lock().unwrap().user_devices = user_devices; + + Ok(vec![]) + } + + fn add_virtio_pci_device( + &mut self, + virtio_device: Arc>, + iommu_mapping: &Option>, + virtio_device_id: String, + pci_segment_id: u16, + dma_handler: Option>, + ) -> DeviceManagerResult { + let id = format!("{}-{}", VIRTIO_PCI_DEVICE_NAME_PREFIX, virtio_device_id); + + // Add the new virtio-pci node to the device tree. + let mut node = device_node!(id); + node.children = vec![virtio_device_id.clone()]; + + let (pci_segment_id, pci_device_bdf, resources) = + self.pci_resources(&id, pci_segment_id)?; + + // Update the existing virtio node by setting the parent. + if let Some(node) = self.device_tree.lock().unwrap().get_mut(&virtio_device_id) { + node.parent = Some(id.clone()); + } else { + return Err(DeviceManagerError::MissingNode); + } + + // Allows support for one MSI-X vector per queue. It also adds 1 + // as we need to take into account the dedicated vector to notify + // about a virtio config change. + let msix_num = (virtio_device.lock().unwrap().queue_max_sizes().len() + 1) as u16; + + // Create the AccessPlatform trait from the implementation IommuMapping. + // This will provide address translation for any virtio device sitting + // behind a vIOMMU. + let access_platform: Option> = if let Some(mapping) = iommu_mapping + { + Some(Arc::new(AccessPlatformMapping::new( + pci_device_bdf.into(), + mapping.clone(), + ))) + } else { + None + }; + + // NOTE: use device_memory so that virtio backends and DMA handlers can + // resolve GPAs that fall inside device-mapped host memory (e.g. PMEM). + // The guest_memory() RAM-only view is reserved for snapshot, migration, + // coredump and firmware-loading paths. + let memory = self.memory_manager.lock().unwrap().device_memory(); + + // Map DMA ranges if a DMA handler is available and if the device is + // not attached to a virtual IOMMU. + if let Some(dma_handler) = &dma_handler { + if iommu_mapping.is_some() { + if let Some(iommu) = &self.iommu_device { + iommu + .lock() + .unwrap() + .add_external_mapping(pci_device_bdf.into(), dma_handler.clone()); + } else { + return Err(DeviceManagerError::MissingVirtualIommu); + } + } else { + // Let every virtio-mem device handle the DMA map/unmap through the + // DMA handler provided. + for virtio_mem_device in self.virtio_mem_devices.iter() { + virtio_mem_device + .lock() + .unwrap() + .add_dma_mapping_handler( + VirtioMemMappingSource::Device(pci_device_bdf.into()), + dma_handler.clone(), + ) + .map_err(DeviceManagerError::AddDmaMappingHandlerVirtioMem)?; + } + + // Do not register virtio-mem regions, as they are handled directly by + // virtio-mem devices. + for (_, zone) in self.memory_manager.lock().unwrap().memory_zones().iter() { + for region in zone.regions() { + let gpa = region.start_addr().0; + let size = region.len(); + dma_handler + .map(gpa, gpa, size) + .map_err(DeviceManagerError::VirtioDmaMap)?; + } + } + } + } + + let device_type = virtio_device.lock().unwrap().device_type(); + let virtio_pci_device = Arc::new(Mutex::new( + VirtioPciDevice::new( + id.clone(), + memory, + virtio_device, + msix_num, + access_platform, + &self.msi_interrupt_manager, + pci_device_bdf.into(), + self.activate_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + // All device types *except* virtio block devices should be allocated a 64-bit bar + // The block devices should be given a 32-bit BAR so that they are easily accessible + // to firmware without requiring excessive identity mapping. + // The exception being if not on the default PCI segment. + pci_segment_id > 0 || device_type != VirtioDeviceType::Block as u32, + dma_handler, + self.pending_activations.clone(), + ) + .map_err(DeviceManagerError::VirtioDevice)?, + )); + + let new_resources = self.add_pci_device( + virtio_pci_device.clone(), + virtio_pci_device.clone(), + pci_segment_id, + pci_device_bdf, + resources, + )?; + + let bar_addr = virtio_pci_device.lock().unwrap().config_bar_addr(); + for (event, addr) in virtio_pci_device.lock().unwrap().ioeventfds(bar_addr) { + let io_addr = IoEventAddress::Mmio(addr); + self.address_manager + .vm + .register_ioevent(event, &io_addr, None) + .map_err(|e| DeviceManagerError::RegisterIoevent(e.into()))?; + } + + // Update the device tree with correct resource information. + node.resources = new_resources; + node.migratable = Some(Arc::clone(&virtio_pci_device) as Arc>); + node.pci_bdf = Some(pci_device_bdf); + node.pci_device_handle = Some(PciDeviceHandle::Virtio(virtio_pci_device)); + self.device_tree.lock().unwrap().insert(id, node); + + Ok(pci_device_bdf) + } + + fn add_pvpanic_device( + &mut self, + ) -> DeviceManagerResult>>> { + let id = String::from(PVPANIC_DEVICE_NAME); + let pci_segment_id = 0x0_u16; + + info!("Creating pvpanic device {}", id); + + let (pci_segment_id, pci_device_bdf, resources) = + self.pci_resources(&id, pci_segment_id)?; + + let pvpanic_device = devices::PvPanicDevice::new( + id.clone(), + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ) + .map_err(DeviceManagerError::PvPanicCreate)?; + + let pvpanic_device = Arc::new(Mutex::new(pvpanic_device)); + + let new_resources = self.add_pci_device( + pvpanic_device.clone(), + pvpanic_device.clone(), + pci_segment_id, + pci_device_bdf, + resources, + )?; + + let mut node = device_node!(id, pvpanic_device); + + node.resources = new_resources; + node.pci_bdf = Some(pci_device_bdf); + node.pci_device_handle = None; + + self.device_tree.lock().unwrap().insert(id, node); + + Ok(Some(pvpanic_device)) + } + + fn add_ivshmem_device( + &mut self, + ivshmem_cfg: &IvshmemConfig, + ) -> DeviceManagerResult>>> { + let id = String::from(IVSHMEM_DEVICE_NAME); + let pci_segment_id = 0x0_u16; + info!("Creating ivshmem device {}", id); + + let (pci_segment_id, pci_device_bdf, resources) = + self.pci_resources(&id, pci_segment_id)?; + + let ivshmem_device = Arc::new(Mutex::new(devices::IvshmemDevice::new( + id.clone(), + state_from_id(self.snapshot.as_ref(), id.as_str()) + .map_err(DeviceManagerError::RestoreGetState)?, + ivshmem_cfg.size as u64, + ))); + let new_resources = self.add_pci_device( + ivshmem_device.clone(), + ivshmem_device.clone(), + pci_segment_id, + pci_device_bdf, + resources, + )?; + + let start_addr = ivshmem_device.lock().unwrap().data_bar_addr(); + let region = MemoryManager::create_ram_region( + &Some(ivshmem_cfg.path.clone()), + 0, + GuestAddress(start_addr), + ivshmem_cfg.size, + false, + true, + false, + None, + None, + None, + None, + 0, + false, + ) + .map_err(DeviceManagerError::MemoryManager)?; + let mem_slot = self + .memory_manager + .lock() + .unwrap() + .create_userspace_mapping( + region.start_addr().0, + region.len(), + region.as_ptr() as u64, + false, + false, + false, + ) + .map_err(DeviceManagerError::MemoryManager)?; + // Make ivshmem/zshm BAR2 visible through device_memory so that virtio + // backends and DMA-mapping helpers can dereference GPAs that fall + // inside this BAR. + self.memory_manager + .lock() + .unwrap() + .add_device_region(Arc::clone(®ion)) + .map_err(DeviceManagerError::MemoryManager)?; + let _mapping = virtio_devices::UserspaceMapping { + host_addr: region.as_ptr() as u64, + mem_slot, + addr: GuestAddress(region.start_addr().0), + len: region.len(), + mergeable: false, + }; + ivshmem_device.lock().unwrap().assign_region(region); + + let mut node = device_node!(id, ivshmem_device); + node.resources = new_resources; + node.pci_bdf = Some(pci_device_bdf); + node.pci_device_handle = None; + self.device_tree.lock().unwrap().insert(id, node); + + Ok(Some(ivshmem_device)) + } + + fn pci_resources( + &self, + id: &str, + pci_segment_id: u16, + ) -> DeviceManagerResult<(u16, PciBdf, Option>)> { + // Look for the id in the device tree. If it can be found, that means + // the device is being restored, otherwise it's created from scratch. + Ok( + if let Some(node) = self.device_tree.lock().unwrap().get(id) { + debug!("Restoring virtio-pci {} resources", id); + let pci_device_bdf: PciBdf = node + .pci_bdf + .ok_or(DeviceManagerError::MissingDeviceNodePciBdf)?; + let pci_segment_id = pci_device_bdf.segment(); + + self.pci_segments[pci_segment_id as usize] + .pci_bus + .lock() + .unwrap() + .get_device_id(pci_device_bdf.device() as usize) + .map_err(DeviceManagerError::GetPciDeviceId)?; + + (pci_segment_id, pci_device_bdf, Some(node.resources.clone())) + } else { + let pci_device_bdf = + self.pci_segments[pci_segment_id as usize].next_device_bdf()?; + + (pci_segment_id, pci_device_bdf, None) + }, + ) + } + + #[cfg(target_arch = "x86_64")] + pub fn io_bus(&self) -> &Arc { + &self.address_manager.io_bus + } + + pub fn mmio_bus(&self) -> &Arc { + &self.address_manager.mmio_bus + } + + pub fn allocator(&self) -> &Arc> { + &self.address_manager.allocator + } + + pub fn interrupt_controller(&self) -> Option>> { + self.interrupt_controller + .as_ref() + .map(|ic| ic.clone() as Arc>) + } + + #[cfg(target_arch = "x86_64")] + // Used to provide a fast path for handling PIO exits + pub fn pci_config_io(&self) -> Arc> { + Arc::clone(self.pci_segments[0].pci_config_io.as_ref().unwrap()) + } + + pub(crate) fn pci_segments(&self) -> &Vec { + &self.pci_segments + } + + pub fn console(&self) -> &Arc { + &self.console + } + + #[cfg(target_arch = "aarch64")] + pub fn cmdline_additions(&self) -> &[String] { + self.cmdline_additions.as_slice() + } + + pub fn update_memory(&self, new_region: &Arc) -> DeviceManagerResult<()> { + for handle in self.virtio_devices.iter() { + handle + .virtio_device + .lock() + .unwrap() + .add_memory_region(new_region) + .map_err(DeviceManagerError::UpdateMemoryForVirtioDevice)?; + + if let Some(dma_handler) = &handle.dma_handler { + if !handle.iommu { + let gpa = new_region.start_addr().0; + let size = new_region.len(); + dma_handler + .map(gpa, gpa, size) + .map_err(DeviceManagerError::VirtioDmaMap)?; + } + } + } + + // Take care of updating the memory for VFIO PCI devices. + if let Some(vfio_container) = &self.vfio_container { + vfio_container + .vfio_dma_map( + new_region.start_addr().raw_value(), + new_region.len(), + new_region.as_ptr() as u64, + ) + .map_err(DeviceManagerError::UpdateMemoryForVfioPciDevice)?; + } + + // Take care of updating the memory for vfio-user devices. + { + let device_tree = self.device_tree.lock().unwrap(); + for pci_device_node in device_tree.pci_devices() { + if let PciDeviceHandle::VfioUser(vfio_user_pci_device) = pci_device_node + .pci_device_handle + .as_ref() + .ok_or(DeviceManagerError::MissingPciDevice)? + { + vfio_user_pci_device + .lock() + .unwrap() + .dma_map(new_region) + .map_err(DeviceManagerError::UpdateMemoryForVfioUserPciDevice)?; + } + } + } + + Ok(()) + } + + pub fn activate_virtio_devices(&self) -> DeviceManagerResult<()> { + for mut activator in self.pending_activations.lock().unwrap().drain(..) { + activator + .activate() + .map_err(DeviceManagerError::VirtioActivate)?; + } + Ok(()) + } + + pub fn notify_hotplug( + &self, + _notification_type: AcpiNotificationFlags, + ) -> DeviceManagerResult<()> { + return self + .ged_notification_device + .as_ref() + .unwrap() + .lock() + .unwrap() + .notify(_notification_type) + .map_err(DeviceManagerError::HotPlugNotification); + } + + pub fn add_device( + &mut self, + device_cfg: &mut DeviceConfig, + ) -> DeviceManagerResult { + self.validate_identifier(&device_cfg.id)?; + + if device_cfg.iommu && !self.is_iommu_segment(device_cfg.pci_segment) { + return Err(DeviceManagerError::InvalidIommuHotplug); + } + + let (bdf, device_name) = self.add_passthrough_device(device_cfg)?; + + // Update the PCIU bitmap + self.pci_segments[device_cfg.pci_segment as usize].pci_devices_up |= 1 << bdf.device(); + + Ok(PciDeviceInfo { + id: device_name, + bdf, + }) + } + + pub fn add_user_device( + &mut self, + device_cfg: &mut UserDeviceConfig, + ) -> DeviceManagerResult { + self.validate_identifier(&device_cfg.id)?; + + let (bdf, device_name) = self.add_vfio_user_device(device_cfg)?; + + // Update the PCIU bitmap + self.pci_segments[device_cfg.pci_segment as usize].pci_devices_up |= 1 << bdf.device(); + + Ok(PciDeviceInfo { + id: device_name, + bdf, + }) + } + + pub fn remove_device(&mut self, id: String) -> DeviceManagerResult<()> { + // The node can be directly a PCI node in case the 'id' refers to a + // VFIO device or a virtio-pci one. + // In case the 'id' refers to a virtio device, we must find the PCI + // node by looking at the parent. + let device_tree = self.device_tree.lock().unwrap(); + let node = device_tree + .get(&id) + .ok_or(DeviceManagerError::UnknownDeviceId(id))?; + + let pci_device_node = if node.pci_bdf.is_some() && node.pci_device_handle.is_some() { + node + } else { + let parent = node + .parent + .as_ref() + .ok_or(DeviceManagerError::MissingNode)?; + device_tree + .get(parent) + .ok_or(DeviceManagerError::MissingNode)? + }; + + let pci_device_bdf: PciBdf = pci_device_node + .pci_bdf + .ok_or(DeviceManagerError::MissingDeviceNodePciBdf)?; + let pci_segment_id = pci_device_bdf.segment(); + + let pci_device_handle = pci_device_node + .pci_device_handle + .as_ref() + .ok_or(DeviceManagerError::MissingPciDevice)?; + #[allow(irrefutable_let_patterns)] + if let PciDeviceHandle::Virtio(virtio_pci_device) = pci_device_handle { + let device_type = VirtioDeviceType::from( + virtio_pci_device + .lock() + .unwrap() + .virtio_device() + .lock() + .unwrap() + .device_type(), + ); + match device_type { + VirtioDeviceType::Net + | VirtioDeviceType::Block + | VirtioDeviceType::Pmem + | VirtioDeviceType::Fs + | VirtioDeviceType::Vsock => {} + _ => return Err(DeviceManagerError::RemovalNotAllowed(device_type)), + } + } + + // Update the PCID bitmap + self.pci_segments[pci_segment_id as usize].pci_devices_down |= 1 << pci_device_bdf.device(); + + Ok(()) + } + + pub fn eject_device(&mut self, pci_segment_id: u16, device_id: u8) -> DeviceManagerResult<()> { + info!( + "Ejecting device_id = {} on segment_id={}", + device_id, pci_segment_id + ); + + // Convert the device ID into the corresponding b/d/f. + let pci_device_bdf = PciBdf::new(pci_segment_id, 0, device_id, 0); + + // Give the PCI device ID back to the PCI bus. + self.pci_segments[pci_segment_id as usize] + .pci_bus + .lock() + .unwrap() + .put_device_id(device_id as usize) + .map_err(DeviceManagerError::PutPciDeviceId)?; + + // Remove the device from the device tree along with its children. + let mut device_tree = self.device_tree.lock().unwrap(); + let pci_device_node = device_tree + .remove_node_by_pci_bdf(pci_device_bdf) + .ok_or(DeviceManagerError::MissingPciDevice)?; + + // For VFIO and vfio-user the PCI device id is the id. + // For virtio we overwrite it later as we want the id of the + // underlying device. + let mut id = pci_device_node.id; + let pci_device_handle = pci_device_node + .pci_device_handle + .ok_or(DeviceManagerError::MissingPciDevice)?; + if matches!(pci_device_handle, PciDeviceHandle::Virtio(_)) { + // The virtio-pci device has a single child + if !pci_device_node.children.is_empty() { + assert_eq!(pci_device_node.children.len(), 1); + let child_id = &pci_device_node.children[0]; + id = child_id.clone(); + } + } + for child in pci_device_node.children.iter() { + device_tree.remove(child); + } + + let mut iommu_attached = false; + if let Some((_, iommu_attached_devices)) = &self.iommu_attached_devices { + if iommu_attached_devices.contains(&pci_device_bdf) { + iommu_attached = true; + } + } + + let (pci_device, bus_device, virtio_device, remove_dma_handler) = match pci_device_handle { + // No need to remove any virtio-mem mapping here as the container outlives all devices + PciDeviceHandle::Vfio(vfio_pci_device) => ( + Arc::clone(&vfio_pci_device) as Arc>, + Arc::clone(&vfio_pci_device) as Arc>, + None as Option>>, + false, + ), + PciDeviceHandle::Virtio(virtio_pci_device) => { + let dev = virtio_pci_device.lock().unwrap(); + let bar_addr = dev.config_bar_addr(); + for (event, addr) in dev.ioeventfds(bar_addr) { + let io_addr = IoEventAddress::Mmio(addr); + self.address_manager + .vm + .unregister_ioevent(event, &io_addr) + .map_err(|e| DeviceManagerError::UnRegisterIoevent(e.into()))?; + } + + if let Some(dma_handler) = dev.dma_handler() { + if !iommu_attached { + for (_, zone) in self.memory_manager.lock().unwrap().memory_zones().iter() { + for region in zone.regions() { + let iova = region.start_addr().0; + let size = region.len(); + dma_handler + .unmap(iova, size) + .map_err(DeviceManagerError::VirtioDmaUnmap)?; + } + } + } + } + + ( + Arc::clone(&virtio_pci_device) as Arc>, + Arc::clone(&virtio_pci_device) as Arc>, + Some(dev.virtio_device()), + dev.dma_handler().is_some() && !iommu_attached, + ) + } + PciDeviceHandle::VfioUser(vfio_user_pci_device) => { + let mut dev = vfio_user_pci_device.lock().unwrap(); + for (_, zone) in self.memory_manager.lock().unwrap().memory_zones().iter() { + for region in zone.regions() { + dev.dma_unmap(region) + .map_err(DeviceManagerError::VfioUserDmaUnmap)?; + } + } + + ( + Arc::clone(&vfio_user_pci_device) as Arc>, + Arc::clone(&vfio_user_pci_device) as Arc>, + None as Option>>, + true, + ) + } + }; + + if remove_dma_handler { + for virtio_mem_device in self.virtio_mem_devices.iter() { + virtio_mem_device + .lock() + .unwrap() + .remove_dma_mapping_handler(VirtioMemMappingSource::Device( + pci_device_bdf.into(), + )) + .map_err(DeviceManagerError::RemoveDmaMappingHandlerVirtioMem)?; + } + } + + // Free the allocated BARs + pci_device + .lock() + .unwrap() + .free_bars( + &mut self.address_manager.allocator.lock().unwrap(), + &mut self.pci_segments[pci_segment_id as usize] + .allocator + .lock() + .unwrap(), + ) + .map_err(DeviceManagerError::FreePciBars)?; + + // Remove the device from the PCI bus + self.pci_segments[pci_segment_id as usize] + .pci_bus + .lock() + .unwrap() + .remove_by_device(&pci_device) + .map_err(DeviceManagerError::RemoveDeviceFromPciBus)?; + + #[cfg(target_arch = "x86_64")] + // Remove the device from the IO bus + self.io_bus() + .remove_by_device(&bus_device) + .map_err(DeviceManagerError::RemoveDeviceFromIoBus)?; + + // Remove the device from the MMIO bus + self.mmio_bus() + .remove_by_device(&bus_device) + .map_err(DeviceManagerError::RemoveDeviceFromMmioBus)?; + + // Remove the device from the list of BusDevice held by the + // DeviceManager. + self.bus_devices + .retain(|dev| !Arc::ptr_eq(dev, &bus_device)); + + // Shutdown and remove the underlying virtio-device if present + if let Some(virtio_device) = virtio_device { + for mapping in virtio_device.lock().unwrap().userspace_mappings() { + self.memory_manager + .lock() + .unwrap() + .remove_userspace_mapping( + mapping.addr.raw_value(), + mapping.len, + mapping.host_addr, + mapping.mergeable, + mapping.mem_slot, + ) + .map_err(DeviceManagerError::MemoryManager)?; + } + + virtio_device.lock().unwrap().shutdown(); + + self.virtio_devices + .retain(|handler| !Arc::ptr_eq(&handler.virtio_device, &virtio_device)); + } + + event!( + "vm", + "device-removed", + "id", + &id, + "bdf", + pci_device_bdf.to_string() + ); + + // At this point, the device has been removed from all the list and + // buses where it was stored. At the end of this function, after + // any_device, bus_device and pci_device are released, the actual + // device will be dropped. + Ok(()) + } + + fn hotplug_virtio_pci_device( + &mut self, + handle: MetaVirtioDevice, + ) -> DeviceManagerResult { + // Add the virtio device to the device manager list. This is important + // as the list is used to notify virtio devices about memory updates + // for instance. + self.virtio_devices.push(handle.clone()); + + let mapping: Option> = if handle.iommu { + self.iommu_mapping.clone() + } else { + None + }; + + let bdf = self.add_virtio_pci_device( + handle.virtio_device, + &mapping, + handle.id.clone(), + handle.pci_segment, + handle.dma_handler, + )?; + + // Update the PCIU bitmap + self.pci_segments[handle.pci_segment as usize].pci_devices_up |= 1 << bdf.device(); + + Ok(PciDeviceInfo { id: handle.id, bdf }) + } + + fn is_iommu_segment(&self, pci_segment_id: u16) -> bool { + self.config + .lock() + .as_ref() + .unwrap() + .platform + .as_ref() + .map(|pc| { + pc.iommu_segments + .as_ref() + .map(|v| v.contains(&pci_segment_id)) + .unwrap_or_default() + }) + .unwrap_or_default() + } + + pub fn add_disk(&mut self, disk_cfg: &mut DiskConfig) -> DeviceManagerResult { + self.validate_identifier(&disk_cfg.id)?; + + if disk_cfg.iommu && !self.is_iommu_segment(disk_cfg.pci_segment) { + return Err(DeviceManagerError::InvalidIommuHotplug); + } + + let device = self.make_virtio_block_device(disk_cfg)?; + self.hotplug_virtio_pci_device(device) + } + + pub fn add_fs(&mut self, fs_cfg: &mut FsConfig) -> DeviceManagerResult { + self.validate_identifier(&fs_cfg.id)?; + + let device = self.make_virtio_fs_device(fs_cfg)?; + self.hotplug_virtio_pci_device(device) + } + + pub fn add_pmem(&mut self, pmem_cfg: &mut PmemConfig) -> DeviceManagerResult { + self.validate_identifier(&pmem_cfg.id)?; + + if pmem_cfg.iommu && !self.is_iommu_segment(pmem_cfg.pci_segment) { + return Err(DeviceManagerError::InvalidIommuHotplug); + } + + let device = self.make_virtio_pmem_device(pmem_cfg)?; + self.hotplug_virtio_pci_device(device) + } + + pub fn add_net(&mut self, net_cfg: &mut NetConfig) -> DeviceManagerResult { + self.validate_identifier(&net_cfg.id)?; + + if net_cfg.iommu && !self.is_iommu_segment(net_cfg.pci_segment) { + return Err(DeviceManagerError::InvalidIommuHotplug); + } + + let device = self.make_virtio_net_device(net_cfg)?; + self.hotplug_virtio_pci_device(device) + } + + pub fn add_vdpa(&mut self, vdpa_cfg: &mut VdpaConfig) -> DeviceManagerResult { + self.validate_identifier(&vdpa_cfg.id)?; + + if vdpa_cfg.iommu && !self.is_iommu_segment(vdpa_cfg.pci_segment) { + return Err(DeviceManagerError::InvalidIommuHotplug); + } + + let device = self.make_vdpa_device(vdpa_cfg)?; + self.hotplug_virtio_pci_device(device) + } + + pub fn add_vsock(&mut self, vsock_cfg: &mut VsockConfig) -> DeviceManagerResult { + self.validate_identifier(&vsock_cfg.id)?; + + if vsock_cfg.iommu && !self.is_iommu_segment(vsock_cfg.pci_segment) { + return Err(DeviceManagerError::InvalidIommuHotplug); + } + + let device = self.make_virtio_vsock_device(vsock_cfg)?; + self.hotplug_virtio_pci_device(device) + } + + pub fn counters(&self) -> HashMap>> { + let mut counters = HashMap::new(); + + for handle in &self.virtio_devices { + let virtio_device = handle.virtio_device.lock().unwrap(); + if let Some(device_counters) = virtio_device.counters() { + counters.insert(handle.id.clone(), device_counters.clone()); + } + } + + counters + } + + pub fn resize_balloon(&mut self, size: u64) -> DeviceManagerResult<()> { + if let Some(balloon) = &self.balloon { + return balloon + .lock() + .unwrap() + .resize(size) + .map_err(DeviceManagerError::VirtioBalloonResize); + } + + warn!("No balloon setup: Can't resize the balloon"); + Err(DeviceManagerError::MissingVirtioBalloon) + } + + pub fn balloon_size(&self) -> u64 { + if let Some(balloon) = &self.balloon { + return balloon.lock().unwrap().get_actual(); + } + + 0 + } + + pub fn device_tree(&self) -> Arc> { + self.device_tree.clone() + } + + pub fn sys_started(&self) -> bool { + if let Some(sys_ctrl) = &self.sys_ctrl { + return sys_ctrl.clone().lock().unwrap().sys_started(); + } + + true + } + + pub fn restore_device_node( + &mut self, + device_groups: (Vec, Vec), + snapshot: Snapshot, + ) -> std::result::Result<(), MigratableError> { + let total_nodes = device_groups.0.len() + device_groups.1.len(); + let work_thread_num = if total_nodes > MAX_WORKER_THREADS { + MAX_WORKER_THREADS + } else { + total_nodes + }; + + let rt = tokio::runtime::Builder::new_multi_thread() + .worker_threads(work_thread_num) + .enable_all() + .build() + .unwrap(); + + rt.block_on(async move { + // Process device groups in order to ensure correct restoration sequence + let groups = [device_groups.0, device_groups.1]; + for nodes in groups { + let mut thread_pool: Vec>> = vec![]; + + for node in nodes { + if let Some(migratable) = node.migratable { + debug!("Restoring {} from DeviceManager", node.id); + if let Some(snapshot) = snapshot.snapshots.get(&node.id).cloned() { + thread_pool.push(tokio::spawn(async move { + let mut guard = migratable.lock().unwrap(); + guard.pause()?; + guard.restore(*snapshot)?; + Ok(()) + })); + } else { + return Err(MigratableError::Restore(anyhow!( + "Missing device {}", + node.id + ))); + } + } + } + + // Wait for all devices in current group to complete before processing next group + for t in thread_pool { + t.await.unwrap()?; + } + } + Ok(()) + }) + } + + pub fn restore_devices( + &mut self, + snapshot: Snapshot, + ) -> std::result::Result<(), MigratableError> { + // Finally, restore all devices associated with the DeviceManager. + // It's important to restore devices in the right order, that's why + // the device tree is the right way to ensure we restore a child before + // its parent node. + let devices_children: Vec<_> = self + .device_tree + .lock() + .unwrap() + .breadth_first_traversal_children() + .rev() + .cloned() + .collect(); + + let devices_parent: Vec<_> = self + .device_tree + .lock() + .unwrap() + .breadth_first_traversal_parent() + .rev() + .cloned() + .collect(); + + let device_groups = (devices_children, devices_parent); + self.restore_device_node(device_groups, snapshot)?; + + // The devices have been fully restored, we can now update the + // restoring state of the DeviceManager. + self.restoring = false; + + Ok(()) + } + + #[cfg(target_arch = "x86_64")] + pub fn notify_power_button(&self) -> DeviceManagerResult<()> { + self.ged_notification_device + .as_ref() + .unwrap() + .lock() + .unwrap() + .notify(AcpiNotificationFlags::POWER_BUTTON_CHANGED) + .map_err(DeviceManagerError::PowerButtonNotification) + } + + #[cfg(target_arch = "aarch64")] + pub fn notify_power_button(&self) -> DeviceManagerResult<()> { + // There are two use cases: + // 1. Users will use direct kernel boot with device tree. + // 2. Users will use ACPI+UEFI boot. + + // Trigger a GPIO pin 3 event to satisify use case 1. + self.gpio_device + .as_ref() + .unwrap() + .lock() + .unwrap() + .trigger_key(3) + .map_err(DeviceManagerError::AArch64PowerButtonNotification)?; + // Trigger a GED power button event to satisify use case 2. + return self + .ged_notification_device + .as_ref() + .unwrap() + .lock() + .unwrap() + .notify(AcpiNotificationFlags::POWER_BUTTON_CHANGED) + .map_err(DeviceManagerError::PowerButtonNotification); + } + + pub fn iommu_attached_devices(&self) -> &Option<(PciBdf, Vec)> { + &self.iommu_attached_devices + } + + fn validate_identifier(&self, id: &Option) -> DeviceManagerResult<()> { + if let Some(id) = id { + if id.starts_with("__") { + return Err(DeviceManagerError::InvalidIdentifier(id.clone())); + } + + if self.device_tree.lock().unwrap().contains_key(id) { + return Err(DeviceManagerError::IdentifierNotUnique(id.clone())); + } + } + + Ok(()) + } + + pub(crate) fn acpi_platform_addresses(&self) -> &AcpiPlatformAddresses { + &self.acpi_platform_addresses + } +} + +fn numa_node_id_from_memory_zone_id(numa_nodes: &NumaNodes, memory_zone_id: &str) -> Option { + for (numa_node_id, numa_node) in numa_nodes.iter() { + if numa_node.memory_zones.contains(&memory_zone_id.to_owned()) { + return Some(*numa_node_id); + } + } + + None +} + +struct TpmDevice {} + +impl Aml for TpmDevice { + fn to_aml_bytes(&self) -> Vec { + aml::Device::new( + "TPM2".into(), + vec![ + &aml::Name::new("_HID".into(), &"MSFT0101"), + &aml::Name::new("_STA".into(), &(0xF_usize)), + &aml::Name::new( + "_CRS".into(), + &aml::ResourceTemplate::new(vec![&aml::Memory32Fixed::new( + true, + layout::TPM_START.0 as u32, + layout::TPM_SIZE as u32, + )]), + ), + ], + ) + .to_aml_bytes() + } +} + +impl Aml for DeviceManager { + fn append_aml_bytes(&self, bytes: &mut Vec) { + #[cfg(target_arch = "aarch64")] + use arch::aarch64::DeviceInfoForFdt; + + let mut pci_scan_methods = Vec::new(); + for i in 0..self.pci_segments.len() { + pci_scan_methods.push(aml::MethodCall::new( + format!("\\_SB_.PCI{:X}.PCNT", i).as_str().into(), + vec![], + )); + } + let mut pci_scan_inner: Vec<&dyn Aml> = Vec::new(); + for method in &pci_scan_methods { + pci_scan_inner.push(method) + } + + // PCI hotplug controller + aml::Device::new( + "_SB_.PHPR".into(), + vec![ + &aml::Name::new("_HID".into(), &aml::EisaName::new("PNP0A06")), + &aml::Name::new("_STA".into(), &0x0bu8), + &aml::Name::new("_UID".into(), &"PCI Hotplug Controller"), + &aml::Mutex::new("BLCK".into(), 0), + &aml::Name::new( + "_CRS".into(), + &aml::ResourceTemplate::new(vec![&aml::AddressSpace::new_memory( + aml::AddressSpaceCachable::NotCacheable, + true, + self.acpi_address.0, + self.acpi_address.0 + DEVICE_MANAGER_ACPI_SIZE as u64 - 1, + )]), + ), + // OpRegion and Fields map MMIO range into individual field values + &aml::OpRegion::new( + "PCST".into(), + aml::OpRegionSpace::SystemMemory, + self.acpi_address.0 as usize, + DEVICE_MANAGER_ACPI_SIZE, + ), + &aml::Field::new( + "PCST".into(), + aml::FieldAccessType::DWord, + aml::FieldUpdateRule::WriteAsZeroes, + vec![ + aml::FieldEntry::Named(*b"PCIU", 32), + aml::FieldEntry::Named(*b"PCID", 32), + aml::FieldEntry::Named(*b"B0EJ", 32), + aml::FieldEntry::Named(*b"PSEG", 32), + ], + ), + &aml::Method::new( + "PCEJ".into(), + 2, + true, + vec![ + // Take lock defined above + &aml::Acquire::new("BLCK".into(), 0xffff), + // Choose the current segment + &aml::Store::new(&aml::Path::new("PSEG"), &aml::Arg(1)), + // Write PCI bus number (in first argument) to I/O port via field + &aml::ShiftLeft::new(&aml::Path::new("B0EJ"), &aml::ONE, &aml::Arg(0)), + // Release lock + &aml::Release::new("BLCK".into()), + // Return 0 + &aml::Return::new(&aml::ZERO), + ], + ), + &aml::Method::new("PSCN".into(), 0, true, pci_scan_inner), + ], + ) + .append_aml_bytes(bytes); + + for segment in &self.pci_segments { + segment.append_aml_bytes(bytes); + } + + let mut mbrd_memory = Vec::new(); + + for segment in &self.pci_segments { + mbrd_memory.push(aml::Memory32Fixed::new( + true, + segment.mmio_config_address as u32, + layout::PCI_MMIO_CONFIG_SIZE_PER_SEGMENT as u32, + )) + } + + let mut mbrd_memory_refs = Vec::new(); + for mbrd_memory_ref in &mbrd_memory { + mbrd_memory_refs.push(mbrd_memory_ref as &dyn Aml); + } + + aml::Device::new( + "_SB_.MBRD".into(), + vec![ + &aml::Name::new("_HID".into(), &aml::EisaName::new("PNP0C02")), + &aml::Name::new("_UID".into(), &aml::ZERO), + &aml::Name::new("_CRS".into(), &aml::ResourceTemplate::new(mbrd_memory_refs)), + ], + ) + .append_aml_bytes(bytes); + + // Serial device + #[cfg(target_arch = "x86_64")] + let serial_irq = 4; + #[cfg(target_arch = "aarch64")] + let serial_irq = + if self.config.lock().unwrap().serial.clone().mode != ConsoleOutputMode::Off { + self.get_device_info() + .clone() + .get(&(DeviceType::Serial, DeviceType::Serial.to_string())) + .unwrap() + .irq() + } else { + // If serial is turned off, add a fake device with invalid irq. + 31 + }; + if self.config.lock().unwrap().serial.mode != ConsoleOutputMode::Off { + aml::Device::new( + "_SB_.COM1".into(), + vec![ + &aml::Name::new( + "_HID".into(), + #[cfg(target_arch = "x86_64")] + &aml::EisaName::new("PNP0501"), + #[cfg(target_arch = "aarch64")] + &"ARMH0011", + ), + &aml::Name::new("_UID".into(), &aml::ZERO), + &aml::Name::new("_DDN".into(), &"COM1"), + &aml::Name::new( + "_CRS".into(), + &aml::ResourceTemplate::new(vec![ + &aml::Interrupt::new(true, true, false, false, serial_irq), + #[cfg(target_arch = "x86_64")] + &aml::Io::new(0x3f8, 0x3f8, 0, 0x8), + #[cfg(target_arch = "aarch64")] + &aml::Memory32Fixed::new( + true, + arch::layout::LEGACY_SERIAL_MAPPED_IO_START.raw_value() as u32, + MMIO_LEN as u32, + ), + ]), + ), + ], + ) + .append_aml_bytes(bytes); + } + + aml::Name::new("_S5_".into(), &aml::Package::new(vec![&5u8])).append_aml_bytes(bytes); + + aml::Device::new( + "_SB_.PWRB".into(), + vec![ + &aml::Name::new("_HID".into(), &aml::EisaName::new("PNP0C0C")), + &aml::Name::new("_UID".into(), &aml::ZERO), + ], + ) + .append_aml_bytes(bytes); + + if self.config.lock().unwrap().tpm.is_some() { + // Add tpm device + let tpm_acpi = TpmDevice {}; + let tpm_dsdt_data = tpm_acpi.to_aml_bytes(); + bytes.extend_from_slice(tpm_dsdt_data.as_slice()); + } + + self.ged_notification_device + .as_ref() + .unwrap() + .lock() + .unwrap() + .append_aml_bytes(bytes); + } +} + +impl Pausable for DeviceManager { + fn pause(&mut self) -> result::Result<(), MigratableError> { + for (_, device_node) in self.device_tree.lock().unwrap().iter() { + if let Some(migratable) = &device_node.migratable { + migratable.lock().unwrap().pause()?; + } + } + // On AArch64, the pause of device manager needs to trigger + // a "pause" of GIC, which will flush the GIC pending tables + // and ITS tables to guest RAM. + #[cfg(target_arch = "aarch64")] + { + self.get_interrupt_controller() + .unwrap() + .lock() + .unwrap() + .pause()?; + }; + + Ok(()) + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + for (_, device_node) in self.device_tree.lock().unwrap().iter() { + if let Some(migratable) = &device_node.migratable { + migratable.lock().unwrap().resume()?; + } + } + + Ok(()) + } +} + +impl Snapshottable for DeviceManager { + fn id(&self) -> String { + DEVICE_MANAGER_SNAPSHOT_ID.to_string() + } + + fn snapshot(&mut self) -> std::result::Result { + // We aggregate all devices snapshots. + let devices: Vec<_> = self + .device_tree + .lock() + .unwrap() + .iter() + .map(|(_, node)| node.clone()) + .collect(); + + let work_thread_num = if devices.len() > MAX_WORKER_THREADS { + MAX_WORKER_THREADS + } else { + // always greater than 0 + devices.len() + }; + let rt = tokio::runtime::Builder::new_multi_thread() + .worker_threads(work_thread_num) + .enable_all() + .build() + .unwrap(); + + let mut snapshot = rt.block_on(async move { + let snapshot = Arc::new(tokio::sync::Mutex::new(Snapshot::new( + DEVICE_MANAGER_SNAPSHOT_ID, + ))); + let mut thread_pool = vec![]; + + for device_node in devices.into_iter() { + if let Some(migratable) = device_node.migratable { + let snapshot_arc = snapshot.clone(); + thread_pool.push(tokio::spawn(async move { + // sync::Mutex is safe since vm has been paused and vmm action is serial. + let device_snapshot = migratable.lock().unwrap().snapshot()?; + snapshot_arc.lock().await.add_snapshot(device_snapshot); + Ok(()) + })); + } + } + for t in thread_pool { + t.await.unwrap()?; + } + if let Ok(mutex) = Arc::try_unwrap(snapshot) { + Ok(mutex.into_inner()) + } else { + Err(MigratableError::Snapshot(anyhow!( + "Could not unwrap snapshot mutex" + ))) + } + })?; + + // Then we store the DeviceManager state. + snapshot.add_data_section(SnapshotDataSection::new_from_state( + DEVICE_MANAGER_SNAPSHOT_ID, + &self.state(), + )?); + + Ok(snapshot) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + // Let's first restore the DeviceManager. + + self.set_state(&snapshot.to_state(DEVICE_MANAGER_SNAPSHOT_ID)?); + + // Now that DeviceManager is updated with the right states, it's time + // to create the devices based on the configuration. + self.create_devices(None, None, None) + .map_err(|e| MigratableError::Restore(anyhow!("Could not create devices {:?}", e)))?; + + Ok(()) + } +} + +impl Transportable for DeviceManager {} + +impl Migratable for DeviceManager { + fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + for (_, device_node) in self.device_tree.lock().unwrap().iter() { + if let Some(migratable) = &device_node.migratable { + migratable.lock().unwrap().start_dirty_log()?; + } + } + Ok(()) + } + + fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + for (_, device_node) in self.device_tree.lock().unwrap().iter() { + if let Some(migratable) = &device_node.migratable { + migratable.lock().unwrap().stop_dirty_log()?; + } + } + Ok(()) + } + + fn dirty_log(&mut self) -> std::result::Result { + let mut tables = Vec::new(); + for (_, device_node) in self.device_tree.lock().unwrap().iter() { + if let Some(migratable) = &device_node.migratable { + tables.push(migratable.lock().unwrap().dirty_log()?); + } + } + Ok(MemoryRangeTable::new_from_tables(tables)) + } + + fn start_migration(&mut self) -> std::result::Result<(), MigratableError> { + for (_, device_node) in self.device_tree.lock().unwrap().iter() { + if let Some(migratable) = &device_node.migratable { + migratable.lock().unwrap().start_migration()?; + } + } + Ok(()) + } + + fn complete_migration(&mut self) -> std::result::Result<(), MigratableError> { + for (_, device_node) in self.device_tree.lock().unwrap().iter() { + if let Some(migratable) = &device_node.migratable { + migratable.lock().unwrap().complete_migration()?; + } + } + Ok(()) + } +} + +const PCIU_FIELD_OFFSET: u64 = 0; +const PCID_FIELD_OFFSET: u64 = 4; +const B0EJ_FIELD_OFFSET: u64 = 8; +const PSEG_FIELD_OFFSET: u64 = 12; +const PCIU_FIELD_SIZE: usize = 4; +const PCID_FIELD_SIZE: usize = 4; +const B0EJ_FIELD_SIZE: usize = 4; +const PSEG_FIELD_SIZE: usize = 4; + +impl BusDevice for DeviceManager { + fn read(&mut self, base: u64, offset: u64, data: &mut [u8]) { + match offset { + PCIU_FIELD_OFFSET => { + assert!(data.len() == PCIU_FIELD_SIZE); + data.copy_from_slice( + &self.pci_segments[self.selected_segment] + .pci_devices_up + .to_le_bytes(), + ); + // Clear the PCIU bitmap + self.pci_segments[self.selected_segment].pci_devices_up = 0; + } + PCID_FIELD_OFFSET => { + assert!(data.len() == PCID_FIELD_SIZE); + data.copy_from_slice( + &self.pci_segments[self.selected_segment] + .pci_devices_down + .to_le_bytes(), + ); + // Clear the PCID bitmap + self.pci_segments[self.selected_segment].pci_devices_down = 0; + } + B0EJ_FIELD_OFFSET => { + assert!(data.len() == B0EJ_FIELD_SIZE); + // Always return an empty bitmap since the eject is always + // taken care of right away during a write access. + data.fill(0); + } + PSEG_FIELD_OFFSET => { + assert_eq!(data.len(), PSEG_FIELD_SIZE); + data.copy_from_slice(&(self.selected_segment as u32).to_le_bytes()); + } + _ => error!( + "Accessing unknown location at base 0x{:x}, offset 0x{:x}", + base, offset + ), + } + + debug!( + "PCI_HP_REG_R: base 0x{:x}, offset 0x{:x}, data {:?}", + base, offset, data + ) + } + + fn write(&mut self, base: u64, offset: u64, data: &[u8]) -> Option> { + match offset { + B0EJ_FIELD_OFFSET => { + assert!(data.len() == B0EJ_FIELD_SIZE); + let mut data_array: [u8; 4] = [0, 0, 0, 0]; + data_array.copy_from_slice(data); + let mut slot_bitmap = u32::from_le_bytes(data_array); + + while slot_bitmap > 0 { + let slot_id = slot_bitmap.trailing_zeros(); + if let Err(e) = self.eject_device(self.selected_segment as u16, slot_id as u8) { + error!("Failed ejecting device {}: {:?}", slot_id, e); + } + slot_bitmap &= !(1 << slot_id); + } + } + PSEG_FIELD_OFFSET => { + assert_eq!(data.len(), PSEG_FIELD_SIZE); + let mut data_array: [u8; 4] = [0, 0, 0, 0]; + data_array.copy_from_slice(data); + let selected_segment = u32::from_le_bytes(data_array) as usize; + if selected_segment >= self.pci_segments.len() { + error!( + "Segment selection out of range: {} >= {}", + selected_segment, + self.pci_segments.len() + ); + return None; + } + self.selected_segment = selected_segment; + } + _ => error!( + "Accessing unknown location at base 0x{:x}, offset 0x{:x}", + base, offset + ), + } + + debug!( + "PCI_HP_REG_W: base 0x{:x}, offset 0x{:x}, data {:?}", + base, offset, data + ); + + None + } +} + +impl Drop for DeviceManager { + fn drop(&mut self) { + // Wake up the DeviceManager threads (mainly virtio device workers), + // to avoid deadlock on waiting for paused/parked worker threads. + if let Err(e) = self.resume() { + error!("Error resuming DeviceManager: {:?}", e); + } + + for handle in self.virtio_devices.drain(..) { + handle.virtio_device.lock().unwrap().shutdown(); + } + } +} diff --git a/vendor/cube-hypervisor/vmm/src/device_tree.rs b/vendor/cube-hypervisor/vmm/src/device_tree.rs new file mode 100644 index 0000000..b7fed19 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/device_tree.rs @@ -0,0 +1,363 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use crate::device_manager::PciDeviceHandle; +use pci::PciBdf; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::os::unix::io::RawFd; +use std::sync::{Arc, Mutex}; +use virtio_devices::fs::FsEvent; +use vm_device::Resource; +use vm_migration::Migratable; + +#[derive(Clone, Serialize, Deserialize)] +pub struct DeviceNode { + pub id: String, + pub resources: Vec, + pub parent: Option, + pub children: Vec, + #[serde(skip)] + pub migratable: Option>>, + pub pci_bdf: Option, + #[serde(skip)] + pub pci_device_handle: Option, + #[serde(skip)] + pub dev_fd: Option, + #[serde(skip)] + pub pending_dev_message: Option>>>, +} + +impl DeviceNode { + pub fn new( + id: String, + migratable: Option>>, + dev_fd: Option, + pending_dev_message: Option>>>, + ) -> Self { + DeviceNode { + id, + resources: Vec::new(), + parent: None, + children: Vec::new(), + migratable, + pci_bdf: None, + pci_device_handle: None, + dev_fd, + pending_dev_message, + } + } +} + +#[macro_export] +macro_rules! device_node { + ($id:ident) => { + DeviceNode::new($id.clone(), None, None, None) + }; + ($id:ident, $device:ident) => { + DeviceNode::new( + $id.clone(), + Some(Arc::clone(&$device) as Arc>), + None, + None, + ) + }; +} + +#[derive(Clone, Default, Serialize, Deserialize)] +pub struct DeviceTree(HashMap); + +impl DeviceTree { + pub fn new() -> Self { + DeviceTree(HashMap::new()) + } + pub fn contains_key(&self, k: &str) -> bool { + self.0.contains_key(k) + } + pub fn get(&self, k: &str) -> Option<&DeviceNode> { + self.0.get(k) + } + pub fn get_mut(&mut self, k: &str) -> Option<&mut DeviceNode> { + self.0.get_mut(k) + } + pub fn insert(&mut self, k: String, v: DeviceNode) -> Option { + self.0.insert(k, v) + } + pub fn remove(&mut self, k: &str) -> Option { + self.0.remove(k) + } + pub fn iter(&self) -> std::collections::hash_map::Iter { + self.0.iter() + } + pub fn breadth_first_traversal(&self) -> BftIter { + BftIter::new(&self.0) + } + pub fn breadth_first_traversal_parent(&self) -> BftIter { + BftIter::new_parent(&self.0) + } + pub fn breadth_first_traversal_children(&self) -> BftIter { + BftIter::new_children(&self.0) + } + pub fn pci_devices(&self) -> Vec<&DeviceNode> { + self.0 + .values() + .filter(|v| v.pci_bdf.is_some() && v.pci_device_handle.is_some()) + .collect() + } + + pub fn remove_node_by_pci_bdf(&mut self, pci_bdf: PciBdf) -> Option { + let mut id = None; + for (k, v) in self.0.iter() { + if v.pci_bdf == Some(pci_bdf) { + id = Some(k.clone()); + break; + } + } + + if let Some(id) = &id { + self.0.remove(id) + } else { + None + } + } +} + +// Breadth first traversal iterator. +pub struct BftIter<'a> { + nodes: Vec<&'a DeviceNode>, +} + +impl<'a> BftIter<'a> { + fn new(hash_map: &'a HashMap) -> Self { + let mut nodes = Vec::new(); + + for (_, node) in hash_map.iter() { + if node.parent.is_none() { + nodes.push(node); + } + } + + nodes.sort_by(|a, b| a.id.cmp(&b.id)); + + let mut node_layer = nodes.as_slice(); + loop { + let mut next_node_layer = Vec::new(); + + for node in node_layer.iter() { + for child_node_id in node.children.iter() { + if let Some(child_node) = hash_map.get(child_node_id) { + next_node_layer.push(child_node); + } + } + } + + if next_node_layer.is_empty() { + break; + } + + next_node_layer.sort_by(|a, b| a.id.cmp(&b.id)); + + let pos = nodes.len(); + nodes.extend(next_node_layer); + + node_layer = &nodes[pos..]; + } + + BftIter { nodes } + } + + fn new_parent(hash_map: &'a HashMap) -> Self { + let mut nodes = Vec::new(); + + for (_, node) in hash_map.iter() { + if node.parent.is_none() { + nodes.push(node); + } + } + + nodes.sort_by(|a, b| a.id.cmp(&b.id)); + + BftIter { nodes } + } + + fn new_children(hash_map: &'a HashMap) -> Self { + let mut nodes = Vec::new(); + let mut nodes_children = Vec::new(); + + for (_, node) in hash_map.iter() { + if node.parent.is_none() { + nodes.push(node); + } + } + + nodes.sort_by(|a, b| a.id.cmp(&b.id)); + + let mut node_layer = nodes.as_slice(); + loop { + let mut next_node_layer = Vec::new(); + + for node in node_layer.iter() { + for child_node_id in node.children.iter() { + if let Some(child_node) = hash_map.get(child_node_id) { + next_node_layer.push(child_node); + } + } + } + + if next_node_layer.is_empty() { + break; + } + + next_node_layer.sort_by(|a, b| a.id.cmp(&b.id)); + + let pos = nodes.len(); + nodes_children.extend(next_node_layer); + + node_layer = &nodes[pos..]; + } + + BftIter { + nodes: nodes_children, + } + } +} + +impl<'a> Iterator for BftIter<'a> { + type Item = &'a DeviceNode; + + fn next(&mut self) -> Option { + if self.nodes.is_empty() { + None + } else { + Some(self.nodes.remove(0)) + } + } +} + +impl<'a> DoubleEndedIterator for BftIter<'a> { + fn next_back(&mut self) -> Option { + self.nodes.pop() + } +} + +#[cfg(test)] +mod tests { + use super::{DeviceNode, DeviceTree}; + + #[test] + fn test_device_tree() { + // Check new() + let mut device_tree = DeviceTree::new(); + assert_eq!(device_tree.0.len(), 0); + + // Check insert() + let id = String::from("id1"); + device_tree.insert(id.clone(), DeviceNode::new(id.clone(), None, None, None)); + assert_eq!(device_tree.0.len(), 1); + let node = device_tree.0.get(&id); + assert!(node.is_some()); + let node = node.unwrap(); + assert_eq!(node.id, id); + + // Check get() + let id2 = String::from("id2"); + assert!(device_tree.get(&id).is_some()); + assert!(device_tree.get(&id2).is_none()); + + // Check get_mut() + let node = device_tree.get_mut(&id).unwrap(); + node.id = id2.clone(); + let node = device_tree.0.get(&id).unwrap(); + assert_eq!(node.id, id2); + + // Check remove() + let node = device_tree.remove(&id).unwrap(); + assert_eq!(node.id, id2); + assert_eq!(device_tree.0.len(), 0); + + // Check iter() + let disk_id = String::from("disk0"); + let net_id = String::from("net0"); + let rng_id = String::from("rng0"); + let device_list = vec![ + (disk_id.clone(), device_node!(disk_id)), + (net_id.clone(), device_node!(net_id)), + (rng_id.clone(), device_node!(rng_id)), + ]; + device_tree.0.extend(device_list); + for (id, node) in device_tree.iter() { + if id == &disk_id { + assert_eq!(node.id, disk_id); + } else if id == &net_id { + assert_eq!(node.id, net_id); + } else if id == &rng_id { + assert_eq!(node.id, rng_id); + } else { + unreachable!() + } + } + + // Check breadth_first_traversal() based on the following hierarchy + // + // 0 + // | \ + // 1 2 + // | | \ + // 3 4 5 + // + let mut device_tree = DeviceTree::new(); + let child_1_id = String::from("child1"); + let child_2_id = String::from("child2"); + let child_3_id = String::from("child3"); + let parent_1_id = String::from("parent1"); + let parent_2_id = String::from("parent2"); + let root_id = String::from("root"); + let mut child_1_node = device_node!(child_1_id); + let mut child_2_node = device_node!(child_2_id); + let mut child_3_node = device_node!(child_3_id); + let mut parent_1_node = device_node!(parent_1_id); + let mut parent_2_node = device_node!(parent_2_id); + let mut root_node = device_node!(root_id); + child_1_node.parent = Some(parent_1_id.clone()); + child_2_node.parent = Some(parent_2_id.clone()); + child_3_node.parent = Some(parent_2_id.clone()); + parent_1_node.children = vec![child_1_id.clone()]; + parent_1_node.parent = Some(root_id.clone()); + parent_2_node.children = vec![child_2_id.clone(), child_3_id.clone()]; + parent_2_node.parent = Some(root_id.clone()); + root_node.children = vec![parent_1_id.clone(), parent_2_id.clone()]; + let device_list = vec![ + (child_1_id.clone(), child_1_node), + (child_2_id.clone(), child_2_node), + (child_3_id.clone(), child_3_node), + (parent_1_id.clone(), parent_1_node), + (parent_2_id.clone(), parent_2_node), + (root_id.clone(), root_node), + ]; + device_tree.0.extend(device_list); + + let iter_vec = device_tree + .breadth_first_traversal() + .collect::>(); + assert_eq!(iter_vec.len(), 6); + assert_eq!(iter_vec[0].id, root_id); + assert_eq!(iter_vec[1].id, parent_1_id); + assert_eq!(iter_vec[2].id, parent_2_id); + assert_eq!(iter_vec[3].id, child_1_id); + assert_eq!(iter_vec[4].id, child_2_id); + assert_eq!(iter_vec[5].id, child_3_id); + + let iter_vec = device_tree + .breadth_first_traversal() + .rev() + .collect::>(); + assert_eq!(iter_vec.len(), 6); + assert_eq!(iter_vec[5].id, root_id); + assert_eq!(iter_vec[4].id, parent_1_id); + assert_eq!(iter_vec[3].id, parent_2_id); + assert_eq!(iter_vec[2].id, child_1_id); + assert_eq!(iter_vec[1].id, child_2_id); + assert_eq!(iter_vec[0].id, child_3_id); + } +} diff --git a/vendor/cube-hypervisor/vmm/src/gdb.rs b/vendor/cube-hypervisor/vmm/src/gdb.rs new file mode 100644 index 0000000..ee52e00 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/gdb.rs @@ -0,0 +1,544 @@ +// Copyright 2022 Akira Moroo. +// Portions Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// SPDX-License-Identifier: BSD-3-Clause + +use gdbstub::{ + arch::Arch, + common::{Signal, Tid}, + conn::{Connection, ConnectionExt}, + stub::{run_blocking, DisconnectReason, MultiThreadStopReason}, + target::{ + ext::{ + base::{ + multithread::{ + MultiThreadBase, MultiThreadResume, MultiThreadResumeOps, + MultiThreadSingleStep, MultiThreadSingleStepOps, + }, + BaseOps, + }, + breakpoints::{Breakpoints, BreakpointsOps, HwBreakpoint, HwBreakpointOps}, + }, + Target, TargetError, TargetResult, + }, +}; +#[cfg(target_arch = "aarch64")] +use gdbstub_arch::aarch64::reg::AArch64CoreRegs as CoreRegs; +#[cfg(target_arch = "aarch64")] +use gdbstub_arch::aarch64::AArch64 as GdbArch; +#[cfg(target_arch = "x86_64")] +use gdbstub_arch::x86::reg::X86_64CoreRegs as CoreRegs; +#[cfg(target_arch = "x86_64")] +use gdbstub_arch::x86::X86_64_SSE as GdbArch; +use std::os::unix::net::UnixListener; +use std::sync::mpsc::{channel, RecvError, RecvTimeoutError, Sender}; +use vm_memory::{GuestAddress, GuestMemoryError}; + +type ArchUsize = u64; + +#[derive(Debug)] +pub enum DebuggableError { + SetDebug(hypervisor::HypervisorCpuError), + Pause(vm_migration::MigratableError), + Resume(vm_migration::MigratableError), + ReadRegs(crate::cpu::Error), + WriteRegs(crate::cpu::Error), + ReadMem(GuestMemoryError), + WriteMem(GuestMemoryError), + TranslateGva(crate::cpu::Error), + PoisonedState, +} + +pub trait Debuggable: vm_migration::Pausable { + fn set_guest_debug( + &self, + cpu_id: usize, + addrs: &[GuestAddress], + singlestep: bool, + ) -> Result<(), DebuggableError>; + fn debug_pause(&mut self) -> std::result::Result<(), DebuggableError>; + fn debug_resume(&mut self) -> std::result::Result<(), DebuggableError>; + fn read_regs(&self, cpu_id: usize) -> std::result::Result; + fn write_regs( + &self, + cpu_id: usize, + regs: &CoreRegs, + ) -> std::result::Result<(), DebuggableError>; + fn read_mem( + &self, + cpu_id: usize, + vaddr: GuestAddress, + len: usize, + ) -> std::result::Result, DebuggableError>; + fn write_mem( + &self, + cpu_id: usize, + vaddr: &GuestAddress, + data: &[u8], + ) -> std::result::Result<(), DebuggableError>; + fn active_vcpus(&self) -> usize; +} + +#[derive(Debug)] +pub enum Error { + Vm(crate::vm::Error), + GdbRequest, + GdbResponseNotify(std::io::Error), + GdbResponse(RecvError), + GdbResponseTimeout(RecvTimeoutError), +} +type GdbResult = std::result::Result; + +#[derive(Debug)] +pub struct GdbRequest { + pub sender: Sender, + pub payload: GdbRequestPayload, + pub cpu_id: usize, +} + +#[derive(Debug)] +pub enum GdbRequestPayload { + ReadRegs, + WriteRegs(Box), + ReadMem(GuestAddress, usize), + WriteMem(GuestAddress, Vec), + Pause, + Resume, + SetSingleStep(bool), + SetHwBreakPoint(Vec), + ActiveVcpus, +} + +pub type GdbResponse = std::result::Result; + +#[derive(Debug)] +pub enum GdbResponsePayload { + CommandComplete, + RegValues(Box), + MemoryRegion(Vec), + ActiveVcpus(usize), +} + +pub struct GdbStub { + gdb_sender: Sender, + gdb_event: vmm_sys_util::eventfd::EventFd, + vm_event: vmm_sys_util::eventfd::EventFd, + hw_breakpoints: Vec, + single_step: bool, +} + +impl GdbStub { + pub fn new( + gdb_sender: Sender, + gdb_event: vmm_sys_util::eventfd::EventFd, + vm_event: vmm_sys_util::eventfd::EventFd, + hw_breakpoints: usize, + ) -> Self { + Self { + gdb_sender, + gdb_event, + vm_event, + hw_breakpoints: Vec::with_capacity(hw_breakpoints), + single_step: false, + } + } + + fn vm_request( + &self, + payload: GdbRequestPayload, + cpu_id: usize, + ) -> GdbResult { + let (response_sender, response_receiver) = channel(); + let request = GdbRequest { + sender: response_sender, + payload, + cpu_id, + }; + self.gdb_sender + .send(request) + .map_err(|_| Error::GdbRequest)?; + self.gdb_event.write(1).map_err(Error::GdbResponseNotify)?; + let res = response_receiver.recv().map_err(Error::GdbResponse)??; + Ok(res) + } +} + +impl Target for GdbStub { + type Arch = GdbArch; + type Error = String; + + #[inline(always)] + fn base_ops(&mut self) -> BaseOps { + BaseOps::MultiThread(self) + } + + #[inline(always)] + fn support_breakpoints(&mut self) -> Option> { + Some(self) + } + + #[inline(always)] + fn guard_rail_implicit_sw_breakpoints(&self) -> bool { + true + } +} + +fn tid_to_cpuid(tid: Tid) -> usize { + tid.get() - 1 +} + +fn cpuid_to_tid(cpu_id: usize) -> Tid { + Tid::new(get_raw_tid(cpu_id)).unwrap() +} + +pub fn get_raw_tid(cpu_id: usize) -> usize { + cpu_id + 1 +} + +impl MultiThreadBase for GdbStub { + fn read_registers( + &mut self, + regs: &mut ::Registers, + tid: Tid, + ) -> TargetResult<(), Self> { + match self.vm_request(GdbRequestPayload::ReadRegs, tid_to_cpuid(tid)) { + Ok(GdbResponsePayload::RegValues(r)) => { + *regs = *r; + Ok(()) + } + Ok(s) => { + error!("Unexpected response for ReadRegs: {:?}", s); + Err(TargetError::NonFatal) + } + Err(e) => { + error!("Failed to request ReadRegs: {:?}", e); + Err(TargetError::NonFatal) + } + } + } + + fn write_registers( + &mut self, + regs: &::Registers, + tid: Tid, + ) -> TargetResult<(), Self> { + match self.vm_request( + GdbRequestPayload::WriteRegs(Box::new(regs.clone())), + tid_to_cpuid(tid), + ) { + Ok(_) => Ok(()), + Err(e) => { + error!("Failed to request WriteRegs: {:?}", e); + Err(TargetError::NonFatal) + } + } + } + + fn read_addrs( + &mut self, + start_addr: ::Usize, + data: &mut [u8], + tid: Tid, + ) -> TargetResult<(), Self> { + match self.vm_request( + GdbRequestPayload::ReadMem(GuestAddress(start_addr), data.len()), + tid_to_cpuid(tid), + ) { + Ok(GdbResponsePayload::MemoryRegion(r)) => { + for (dst, v) in data.iter_mut().zip(r.iter()) { + *dst = *v; + } + Ok(()) + } + Ok(s) => { + error!("Unexpected response for ReadMem: {:?}", s); + Err(TargetError::NonFatal) + } + Err(e) => { + error!("Failed to request ReadMem: {:?}", e); + Err(TargetError::NonFatal) + } + } + } + + fn write_addrs( + &mut self, + start_addr: ::Usize, + data: &[u8], + tid: Tid, + ) -> TargetResult<(), Self> { + match self.vm_request( + GdbRequestPayload::WriteMem(GuestAddress(start_addr), data.to_owned()), + tid_to_cpuid(tid), + ) { + Ok(_) => Ok(()), + Err(e) => { + error!("Failed to request WriteMem: {:?}", e); + Err(TargetError::NonFatal) + } + } + } + + fn list_active_threads( + &mut self, + thread_is_active: &mut dyn FnMut(Tid), + ) -> Result<(), Self::Error> { + match self.vm_request(GdbRequestPayload::ActiveVcpus, 0) { + Ok(GdbResponsePayload::ActiveVcpus(active_vcpus)) => { + (0..active_vcpus).for_each(|cpu_id| { + thread_is_active(cpuid_to_tid(cpu_id)); + }); + Ok(()) + } + Ok(s) => Err(format!("Unexpected response for ActiveVcpus: {:?}", s)), + Err(e) => Err(format!("Failed to request ActiveVcpus: {:?}", e)), + } + } + + #[inline(always)] + fn support_resume(&mut self) -> Option> { + Some(self) + } +} + +impl MultiThreadResume for GdbStub { + fn resume(&mut self) -> Result<(), Self::Error> { + match self.vm_request(GdbRequestPayload::Resume, 0) { + Ok(_) => Ok(()), + Err(e) => Err(format!("Failed to resume the target: {:?}", e)), + } + } + + fn clear_resume_actions(&mut self) -> Result<(), Self::Error> { + if self.single_step { + match self.vm_request(GdbRequestPayload::SetSingleStep(false), 0) { + Ok(_) => { + self.single_step = false; + } + Err(e) => { + return Err(format!("Failed to request SetSingleStep: {:?}", e)); + } + } + } + Ok(()) + } + + fn set_resume_action_continue( + &mut self, + tid: Tid, + signal: Option, + ) -> Result<(), Self::Error> { + if signal.is_some() { + return Err("no support for continuing with signal".to_owned()); + } + match self.vm_request(GdbRequestPayload::Resume, tid_to_cpuid(tid)) { + Ok(_) => Ok(()), + Err(e) => Err(format!("Failed to resume the target: {:?}", e)), + } + } + + #[inline(always)] + fn support_single_step(&mut self) -> Option> { + Some(self) + } +} + +impl MultiThreadSingleStep for GdbStub { + fn set_resume_action_step( + &mut self, + tid: Tid, + signal: Option, + ) -> Result<(), Self::Error> { + if signal.is_some() { + return Err("no support for stepping with signal".to_owned()); + } + + if !self.single_step { + match self.vm_request(GdbRequestPayload::SetSingleStep(true), tid_to_cpuid(tid)) { + Ok(_) => { + self.single_step = true; + } + Err(e) => { + return Err(format!("Failed to request SetSingleStep: {:?}", e)); + } + } + } + match self.vm_request(GdbRequestPayload::Resume, tid_to_cpuid(tid)) { + Ok(_) => Ok(()), + Err(e) => Err(format!("Failed to resume the target: {:?}", e)), + } + } +} + +impl Breakpoints for GdbStub { + #[inline(always)] + fn support_hw_breakpoint(&mut self) -> Option> { + Some(self) + } +} + +impl HwBreakpoint for GdbStub { + fn add_hw_breakpoint( + &mut self, + addr: ::Usize, + _kind: ::BreakpointKind, + ) -> TargetResult { + // If the HW breakpoints reach the limit, no more can be added. + if self.hw_breakpoints.len() >= self.hw_breakpoints.capacity() { + error!( + "Not allowed to set more than {} HW breakpoints", + self.hw_breakpoints.capacity() + ); + return Ok(false); + } + + self.hw_breakpoints.push(GuestAddress(addr)); + + let payload = GdbRequestPayload::SetHwBreakPoint(self.hw_breakpoints.clone()); + match self.vm_request(payload, 0) { + Ok(_) => Ok(true), + Err(e) => { + error!("Failed to request SetHwBreakPoint: {:?}", e); + Err(TargetError::NonFatal) + } + } + } + fn remove_hw_breakpoint( + &mut self, + addr: ::Usize, + _kind: ::BreakpointKind, + ) -> TargetResult { + match self.hw_breakpoints.iter().position(|&b| b.0 == addr) { + None => return Ok(false), + Some(pos) => self.hw_breakpoints.remove(pos), + }; + + let payload = GdbRequestPayload::SetHwBreakPoint(self.hw_breakpoints.clone()); + match self.vm_request(payload, 0) { + Ok(_) => Ok(true), + Err(e) => { + error!("Failed to request SetHwBreakPoint: {:?}", e); + Err(TargetError::NonFatal) + } + } + } +} + +enum GdbEventLoop {} + +impl run_blocking::BlockingEventLoop for GdbEventLoop { + type Target = GdbStub; + type Connection = Box>; + type StopReason = MultiThreadStopReason; + + #[allow(clippy::type_complexity)] + fn wait_for_stop_reason( + target: &mut Self::Target, + conn: &mut Self::Connection, + ) -> Result< + run_blocking::Event, + run_blocking::WaitForStopReasonError< + ::Error, + ::Error, + >, + > { + // Polling + loop { + // This read is non-blocking. + match target.vm_event.read() { + Ok(tid) => { + target + .vm_request(GdbRequestPayload::Pause, 0) + .map_err(|_| { + run_blocking::WaitForStopReasonError::Target( + "Failed to pause VM".to_owned(), + ) + })?; + let stop_reason = if target.single_step { + MultiThreadStopReason::DoneStep + } else { + MultiThreadStopReason::HwBreak(Tid::new(tid as usize).unwrap()) + }; + return Ok(run_blocking::Event::TargetStopped(stop_reason)); + } + Err(e) => { + if e.kind() != std::io::ErrorKind::WouldBlock { + return Err(run_blocking::WaitForStopReasonError::Connection(e)); + } + } + } + + if conn.peek().map(|b| b.is_some()).unwrap_or(true) { + let byte = conn + .read() + .map_err(run_blocking::WaitForStopReasonError::Connection)?; + return Ok(run_blocking::Event::IncomingData(byte)); + } + } + } + + fn on_interrupt( + target: &mut Self::Target, + ) -> Result, ::Error> { + target + .vm_request(GdbRequestPayload::Pause, 0) + .map_err(|e| { + error!("Failed to pause the target: {:?}", e); + "Failed to pause the target" + })?; + Ok(Some(MultiThreadStopReason::Signal(Signal::SIGINT))) + } +} + +pub fn gdb_thread(mut gdbstub: GdbStub, path: &std::path::Path) { + let listener = match UnixListener::bind(path) { + Ok(s) => s, + Err(e) => { + error!("Failed to create a Unix domain socket listener: {}", e); + return; + } + }; + info!("Waiting for a GDB connection on {}...", path.display()); + + let (stream, addr) = match listener.accept() { + Ok(v) => v, + Err(e) => { + error!("Failed to accept a connection from GDB: {}", e); + return; + } + }; + info!("GDB connected from {:?}", addr); + + let connection: Box> = Box::new(stream); + let gdb = gdbstub::stub::GdbStub::new(connection); + + match gdb.run_blocking::(&mut gdbstub) { + Ok(disconnect_reason) => match disconnect_reason { + DisconnectReason::Disconnect => { + info!("GDB client has disconnected. Running..."); + + if let Err(e) = gdbstub.vm_request(GdbRequestPayload::SetSingleStep(false), 0) { + error!("Failed to disable single step: {:?}", e); + } + + if let Err(e) = + gdbstub.vm_request(GdbRequestPayload::SetHwBreakPoint(Vec::new()), 0) + { + error!("Failed to remove breakpoints: {:?}", e); + } + + if let Err(e) = gdbstub.vm_request(GdbRequestPayload::Resume, 0) { + error!("Failed to resume the VM: {:?}", e); + } + } + _ => { + error!("Target exited or terminated"); + } + }, + Err(e) => { + error!("error occurred in GDB session: {}", e); + } + } +} diff --git a/vendor/cube-hypervisor/vmm/src/interrupt.rs b/vendor/cube-hypervisor/vmm/src/interrupt.rs new file mode 100644 index 0000000..074392e --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/interrupt.rs @@ -0,0 +1,303 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause +// + +use devices::interrupt_controller::InterruptController; +use hypervisor::IrqRoutingEntry; +use std::collections::HashMap; +use std::io; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex}; +use vm_allocator::SystemAllocator; +use vm_device::interrupt::{ + InterruptIndex, InterruptManager, InterruptSourceConfig, InterruptSourceGroup, + LegacyIrqGroupConfig, MsiIrqGroupConfig, +}; +use vmm_sys_util::eventfd::EventFd; + +/// Reuse std::io::Result to simplify interoperability among crates. +pub type Result = std::io::Result; + +struct InterruptRoute { + gsi: u32, + irq_fd: EventFd, + registered: AtomicBool, +} + +impl InterruptRoute { + pub fn new(allocator: &mut SystemAllocator) -> Result { + let irq_fd = EventFd::new(libc::EFD_NONBLOCK)?; + let gsi = allocator + .allocate_gsi() + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Failed allocating new GSI"))?; + + Ok(InterruptRoute { + gsi, + irq_fd, + registered: AtomicBool::new(false), + }) + } + + pub fn enable(&self, vm: &Arc) -> Result<()> { + if !self.registered.load(Ordering::Acquire) { + vm.register_irqfd(&self.irq_fd, self.gsi).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("Failed registering irq_fd: {}", e), + ) + })?; + + // Update internals to track the irq_fd as "registered". + self.registered.store(true, Ordering::Release); + } + + Ok(()) + } + + pub fn disable(&self, vm: &Arc) -> Result<()> { + if self.registered.load(Ordering::Acquire) { + vm.unregister_irqfd(&self.irq_fd, self.gsi).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("Failed unregistering irq_fd: {}", e), + ) + })?; + + // Update internals to track the irq_fd as "unregistered". + self.registered.store(false, Ordering::Release); + } + + Ok(()) + } + + pub fn trigger(&self) -> Result<()> { + self.irq_fd.write(1) + } + + pub fn notifier(&self) -> Option { + Some( + self.irq_fd + .try_clone() + .expect("Failed cloning interrupt's EventFd"), + ) + } +} + +pub struct RoutingEntry { + route: IrqRoutingEntry, + masked: bool, +} + +pub struct MsiInterruptGroup { + vm: Arc, + gsi_msi_routes: Arc>>, + irq_routes: HashMap, +} + +impl MsiInterruptGroup { + fn set_gsi_routes(&self, routes: &HashMap) -> Result<()> { + let mut entry_vec: Vec = Vec::new(); + for (_, entry) in routes.iter() { + if entry.masked { + continue; + } + + entry_vec.push(entry.route); + } + + self.vm.set_gsi_routing(&entry_vec).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("Failed setting GSI routing: {}", e), + ) + }) + } +} + +impl MsiInterruptGroup { + fn new( + vm: Arc, + gsi_msi_routes: Arc>>, + irq_routes: HashMap, + ) -> Self { + MsiInterruptGroup { + vm, + gsi_msi_routes, + irq_routes, + } + } +} + +impl InterruptSourceGroup for MsiInterruptGroup { + fn enable(&self) -> Result<()> { + for (_, route) in self.irq_routes.iter() { + route.enable(&self.vm)?; + } + + Ok(()) + } + + fn disable(&self) -> Result<()> { + for (_, route) in self.irq_routes.iter() { + route.disable(&self.vm)?; + } + + Ok(()) + } + + fn trigger(&self, index: InterruptIndex) -> Result<()> { + if let Some(route) = self.irq_routes.get(&index) { + return route.trigger(); + } + + Err(io::Error::new( + io::ErrorKind::Other, + format!("trigger: Invalid interrupt index {}", index), + )) + } + + fn notifier(&self, index: InterruptIndex) -> Option { + if let Some(route) = self.irq_routes.get(&index) { + return route.notifier(); + } + + None + } + + fn update( + &self, + index: InterruptIndex, + config: InterruptSourceConfig, + masked: bool, + ) -> Result<()> { + if let Some(route) = self.irq_routes.get(&index) { + let entry = RoutingEntry { + route: self.vm.make_routing_entry(route.gsi, &config), + masked, + }; + if masked { + route.disable(&self.vm)?; + } else { + route.enable(&self.vm)?; + } + let mut routes = self.gsi_msi_routes.lock().unwrap(); + routes.insert(route.gsi, entry); + return self.set_gsi_routes(&routes); + } + + Err(io::Error::new( + io::ErrorKind::Other, + format!("update: Invalid interrupt index {}", index), + )) + } +} + +pub struct LegacyUserspaceInterruptGroup { + ioapic: Arc>, + irq: u32, +} + +impl LegacyUserspaceInterruptGroup { + fn new(ioapic: Arc>, irq: u32) -> Self { + LegacyUserspaceInterruptGroup { ioapic, irq } + } +} + +impl InterruptSourceGroup for LegacyUserspaceInterruptGroup { + fn trigger(&self, _index: InterruptIndex) -> Result<()> { + self.ioapic + .lock() + .unwrap() + .service_irq(self.irq as usize) + .map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("failed to inject IRQ #{}: {:?}", self.irq, e), + ) + }) + } + + fn update( + &self, + _index: InterruptIndex, + _config: InterruptSourceConfig, + _masked: bool, + ) -> Result<()> { + Ok(()) + } + + fn notifier(&self, _index: InterruptIndex) -> Option { + self.ioapic.lock().unwrap().notifier(self.irq as usize) + } +} + +pub struct LegacyUserspaceInterruptManager { + ioapic: Arc>, +} + +pub struct MsiInterruptManager { + allocator: Arc>, + vm: Arc, + gsi_msi_routes: Arc>>, +} + +impl LegacyUserspaceInterruptManager { + pub fn new(ioapic: Arc>) -> Self { + LegacyUserspaceInterruptManager { ioapic } + } +} + +impl MsiInterruptManager { + pub fn new(allocator: Arc>, vm: Arc) -> Self { + // Create a shared list of GSI that can be shared through all PCI + // devices. This way, we can maintain the full list of used GSI, + // preventing one device from overriding interrupts setting from + // another one. + let gsi_msi_routes = Arc::new(Mutex::new(HashMap::new())); + + MsiInterruptManager { + allocator, + vm, + gsi_msi_routes, + } + } +} + +impl InterruptManager for LegacyUserspaceInterruptManager { + type GroupConfig = LegacyIrqGroupConfig; + + fn create_group(&self, config: Self::GroupConfig) -> Result> { + Ok(Arc::new(LegacyUserspaceInterruptGroup::new( + self.ioapic.clone(), + config.irq, + ))) + } + + fn destroy_group(&self, _group: Arc) -> Result<()> { + Ok(()) + } +} + +impl InterruptManager for MsiInterruptManager { + type GroupConfig = MsiIrqGroupConfig; + + fn create_group(&self, config: Self::GroupConfig) -> Result> { + let mut allocator = self.allocator.lock().unwrap(); + let mut irq_routes: HashMap = + HashMap::with_capacity(config.count as usize); + for i in config.base..config.base + config.count { + irq_routes.insert(i, InterruptRoute::new(&mut allocator)?); + } + + Ok(Arc::new(MsiInterruptGroup::new( + self.vm.clone(), + self.gsi_msi_routes.clone(), + irq_routes, + ))) + } + + fn destroy_group(&self, _group: Arc) -> Result<()> { + Ok(()) + } +} diff --git a/vendor/cube-hypervisor/vmm/src/lib.rs b/vendor/cube-hypervisor/vmm/src/lib.rs new file mode 100644 index 0000000..2a27445 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/lib.rs @@ -0,0 +1,2839 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#![allow(clippy::significant_drop_in_scrutinee)] + +#[macro_use] +extern crate event_monitor; +#[macro_use] +extern crate log; + +use crate::api::{ + ApiError, ApiRequest, ApiResponse, ApiResponsePayload, VmInfo, VmReceiveMigrationData, + VmSendMigrationData, VmWaitStartResponse, VmmPingResponse, +}; +use crate::config::{add_to_config, RestoreConfig}; +#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] +use crate::coredump::GuestDebuggable; +use crate::memory_manager::MemoryManager; +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +use crate::migration::get_vm_snapshot; +use crate::migration::{recv_vm_config, recv_vm_state}; +use crate::seccomp_filters::{get_seccomp_filter, Thread}; +use crate::vm::{Error as VmError, Vm, VmState}; +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +use crate::vm_config::CompatibleMode; +use crate::vm_config::{ + DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, UserDeviceConfig, VdpaConfig, + VmConfig, VsockConfig, +}; +use anyhow::anyhow; +use event_notifier::{event_notify, NotifyEvent}; +use libc::{EFD_NONBLOCK, SIGINT, SIGTERM, TFD_NONBLOCK}; +use logging::LOG_CTRL_REOPEN; +use memory_manager::MemoryManagerSnapshotData; +use pci::PciBdf; +use seccompiler::{apply_filter, SeccompAction}; +use serde::ser::{SerializeStruct, Serializer}; +use serde::{Deserialize, Serialize}; +use signal_hook::iterator::{Handle, Signals}; +use std::collections::HashMap; +use std::fs::File; +use std::io; +use std::io::{Read, Write}; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::os::unix::net::UnixListener; +use std::os::unix::net::UnixStream; +use std::panic::AssertUnwindSafe; +use std::path::PathBuf; +use std::rc::Rc; +use std::sync::atomic::AtomicBool; +use std::sync::mpsc::{Receiver, RecvError, SendError, Sender}; +use std::sync::{Arc, Barrier, Mutex}; +use std::time::{Duration, Instant}; +use std::{result, thread}; +use thiserror::Error; +use tracer::trace_scoped; +use vm_memory::bitmap::AtomicBitmap; +use vm_memory::{ReadVolatile, WriteVolatile}; +use vm_migration::{protocol::*, Migratable}; +use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; +pub use vm_migration::{SnapshotConfig, SnapshotType}; +use vmm_sys_util::eventfd::EventFd; +use vmm_sys_util::signal::unblock_signal; +use vmm_sys_util::sock_ctrl_msg::ScmSocket; +use vmm_sys_util::terminal::Terminal; + +mod acpi; +pub mod api; +mod clone3; +pub mod config; +#[cfg(feature = "guest_debug")] +mod coredump; +pub mod cpu; +pub mod device_manager; +pub mod device_tree; +#[cfg(feature = "guest_debug")] +mod gdb; +pub mod interrupt; +pub mod memory_manager; +pub mod migration; +pub mod pagemap_anon; +mod pci_segment; +pub mod seccomp_filters; +mod serial_manager; +mod sigwinch_listener; +pub mod soft_dirty; +pub mod vm; +pub mod vm_config; + +type GuestMemoryMmap = vm_memory::GuestMemoryMmap; +type GuestRegionMmap = vm_memory::GuestRegionMmap; + +/// Errors associated with VMM management +#[derive(Debug, Error)] +#[allow(clippy::large_enum_variant)] +pub enum Error { + /// API request receive error + #[error("Error receiving API request: {0}")] + ApiRequestRecv(#[source] RecvError), + + /// API response send error + #[error("Error sending API request: {0}")] + ApiResponseSend(#[source] SendError), + + /// Cannot bind to the UNIX domain socket path + #[error("Error binding to UNIX domain socket: {0}")] + Bind(#[source] io::Error), + + /// Cannot clone EventFd. + #[error("Error cloning EventFd: {0}")] + EventFdClone(#[source] io::Error), + + /// Cannot create EventFd. + #[error("Error creating EventFd: {0}")] + EventFdCreate(#[source] io::Error), + + /// Cannot read from EventFd. + #[error("Error reading from EventFd: {0}")] + EventFdRead(#[source] io::Error), + + /// Cannot create the periodic log-reopen timer. + #[error("Error creating log-reopen timer: {0}")] + LogReopenTimerCreate(#[source] io::Error), + + /// Cannot consume the periodic log-reopen timer event. + #[error("Error reading log-reopen timer: {0}")] + LogReopenTimerRead(#[source] io::Error), + + /// Cannot create epoll context. + #[error("Error creating epoll context: {0}")] + Epoll(#[source] io::Error), + + /// Cannot create HTTP thread + #[error("Error spawning HTTP thread: {0}")] + HttpThreadSpawn(#[source] io::Error), + + /// Cannot handle the VM STDIN stream + #[error("Error handling VM stdin: {0:?}")] + Stdin(VmError), + + /// Cannot handle the VM pty stream + #[error("Error handling VM pty: {0:?}")] + Pty(VmError), + + /// Cannot reboot the VM + #[error("Error rebooting VM: {0:?}")] + VmReboot(VmError), + + /// Cannot create VMM thread + #[error("Error spawning VMM thread {0:?}")] + VmmThreadSpawn(#[source] io::Error), + + /// Cannot shut the VMM down + #[error("Error shutting down VMM: {0:?}")] + VmmShutdown(VmError), + + /// Cannot create seccomp filter + #[error("Error creating seccomp filter: {0}")] + CreateSeccompFilter(seccompiler::Error), + + /// Cannot apply seccomp filter + #[error("Error applying seccomp filter: {0}")] + ApplySeccompFilter(seccompiler::Error), + + /// Error activating virtio devices + #[error("Error activating virtio devices: {0:?}")] + ActivateVirtioDevices(VmError), + + /// Error creating API server + #[error("Error creating API server {0:?}")] + CreateApiServer(micro_http::ServerError), + + /// Error binding API server socket + #[error("Error creation API server's socket {0:?}")] + CreateApiServerSocket(#[source] io::Error), + + #[cfg(feature = "guest_debug")] + #[error("Failed to start the GDB thread: {0}")] + GdbThreadSpawn(io::Error), + + /// GDB request receive error + #[cfg(feature = "guest_debug")] + #[error("Error receiving GDB request: {0}")] + GdbRequestRecv(#[source] RecvError), + + /// GDB response send error + #[cfg(feature = "guest_debug")] + #[error("Error sending GDB request: {0}")] + GdbResponseSend(#[source] SendError), + + #[error("Cannot spawn a signal handler thread: {0}")] + SignalHandlerSpawn(#[source] io::Error), + + #[error("Failed to join on threads: {0:?}")] + ThreadCleanup(std::boxed::Box), +} +pub type Result = result::Result; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u64)] +pub enum EpollDispatch { + Exit = 0, + Reset = 1, + Api = 2, + ActivateVirtioDevices = 3, + Debug = 4, + LogReopen = 5, + Unknown, +} + +impl From for EpollDispatch { + fn from(v: u64) -> Self { + use EpollDispatch::*; + match v { + 0 => Exit, + 1 => Reset, + 2 => Api, + 3 => ActivateVirtioDevices, + 4 => Debug, + 5 => LogReopen, + _ => Unknown, + } + } +} + +const LOG_REOPEN_INTERVAL: Duration = Duration::from_secs(60 * 60); + +fn create_log_reopen_timer(interval: Duration) -> io::Result { + // This is called from the VMM control thread after its VMM-specific + // seccomp filter has been installed. The vCPU and API thread filters do + // not allow timerfd syscalls. + let timer_fd = + unsafe { libc::timerfd_create(libc::CLOCK_MONOTONIC, libc::TFD_CLOEXEC | TFD_NONBLOCK) }; + if timer_fd < 0 { + return Err(io::Error::last_os_error()); + } + + // SAFETY: timer_fd is a valid, newly-created file descriptor owned by us. + let timer = unsafe { File::from_raw_fd(timer_fd) }; + let interval = libc::itimerspec { + it_interval: libc::timespec { + tv_sec: interval.as_secs() as _, + tv_nsec: interval.subsec_nanos() as _, + }, + it_value: libc::timespec { + tv_sec: interval.as_secs() as _, + tv_nsec: interval.subsec_nanos() as _, + }, + }; + + let result = + unsafe { libc::timerfd_settime(timer.as_raw_fd(), 0, &interval, std::ptr::null_mut()) }; + if result < 0 { + return Err(io::Error::last_os_error()); + } + + Ok(timer) +} + +fn read_log_reopen_timer(reader: &mut R) -> io::Result { + let mut expirations = [0_u8; std::mem::size_of::()]; + match reader.read(&mut expirations) { + Ok(size) if size == expirations.len() => Ok(true), + Ok(size) => Err(io::Error::new( + io::ErrorKind::UnexpectedEof, + format!("short log-reopen timer read: {size} bytes"), + )), + Err(error) + if matches!( + error.kind(), + io::ErrorKind::Interrupted | io::ErrorKind::WouldBlock + ) => + { + Ok(false) + } + Err(error) => Err(error), + } +} + +pub struct EpollContext { + epoll_file: File, +} + +impl EpollContext { + pub fn new() -> result::Result { + let epoll_fd = epoll::create(true)?; + // Use 'File' to enforce closing on 'epoll_fd' + // SAFETY: the epoll_fd returned by epoll::create is valid and owned by us. + let epoll_file = unsafe { File::from_raw_fd(epoll_fd) }; + + Ok(EpollContext { epoll_file }) + } + + pub fn add_event(&mut self, fd: &T, token: EpollDispatch) -> result::Result<(), io::Error> + where + T: AsRawFd, + { + let dispatch_index = token as u64; + epoll::ctl( + self.epoll_file.as_raw_fd(), + epoll::ControlOptions::EPOLL_CTL_ADD, + fd.as_raw_fd(), + epoll::Event::new(epoll::Events::EPOLLIN, dispatch_index), + )?; + + Ok(()) + } +} + +impl AsRawFd for EpollContext { + fn as_raw_fd(&self) -> RawFd { + self.epoll_file.as_raw_fd() + } +} + +pub struct PciDeviceInfo { + pub id: String, + pub bdf: PciBdf, +} + +impl Serialize for PciDeviceInfo { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: Serializer, + { + let bdf_str = self.bdf.to_string(); + + // Serialize the structure. + let mut state = serializer.serialize_struct("PciDeviceInfo", 2)?; + state.serialize_field("id", &self.id)?; + state.serialize_field("bdf", &bdf_str)?; + state.end() + } +} + +#[allow(unused_variables)] +#[allow(clippy::too_many_arguments)] +pub fn start_vmm_thread( + vmm_version: String, + http_path: &Option, + http_fd: Option, + api_event: EventFd, + api_receiver: Receiver, + res_sender: Sender, + #[cfg(feature = "guest_debug")] debug_path: Option, + #[cfg(feature = "guest_debug")] debug_event: EventFd, + #[cfg(feature = "guest_debug")] vm_debug_event: EventFd, + seccomp_action: &SeccompAction, + hypervisor: Arc, + sandbox_id: String, + vcpu_started: Arc, + vmm_barrier: Option>, +) -> Result>> { + #[cfg(feature = "guest_debug")] + let gdb_hw_breakpoints = hypervisor.get_guest_debug_hw_bps(); + #[cfg(feature = "guest_debug")] + let (gdb_sender, gdb_receiver) = std::sync::mpsc::channel(); + #[cfg(feature = "guest_debug")] + let gdb_debug_event = debug_event.try_clone().map_err(Error::EventFdClone)?; + #[cfg(feature = "guest_debug")] + let gdb_vm_debug_event = vm_debug_event.try_clone().map_err(Error::EventFdClone)?; + + let hypervisor_type = hypervisor.hypervisor_type(); + + let seccomp_filter = get_seccomp_filter(seccomp_action, Thread::All, hypervisor_type) + .map_err(Error::CreateSeccompFilter)?; + // Apply seccomp filter for ALL thread. + if !seccomp_filter.is_empty() { + apply_filter(&seccomp_filter).map_err(Error::ApplySeccompFilter)?; + } + + // Retrieve seccomp filter + let vmm_seccomp_filter = get_seccomp_filter(seccomp_action, Thread::Vmm, hypervisor_type) + .map_err(Error::CreateSeccompFilter)?; + + let vmm_seccomp_action = seccomp_action.clone(); + let exit_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFdCreate)?; + let thread = { + let exit_evt = exit_evt.try_clone().map_err(Error::EventFdClone)?; + thread::Builder::new() + .name("vmm".to_string()) + .spawn(move || { + // Apply seccomp filter for VMM thread. + if !vmm_seccomp_filter.is_empty() { + apply_filter(&vmm_seccomp_filter).map_err(Error::ApplySeccompFilter)?; + } + + let mut vmm = Vmm::new( + vmm_version.to_string(), + api_event, + #[cfg(feature = "guest_debug")] + debug_event, + #[cfg(feature = "guest_debug")] + vm_debug_event, + vmm_seccomp_action, + hypervisor, + exit_evt, + sandbox_id, + vcpu_started, + )?; + + vmm.setup_signal_handler()?; + + vmm.control_loop( + Rc::new(api_receiver), + Rc::new(res_sender), + #[cfg(feature = "guest_debug")] + Rc::new(gdb_receiver), + vmm_barrier, + ) + }) + .map_err(Error::VmmThreadSpawn)? + }; + + // The VMM thread is started, we can start serving HTTP requests + if let Some(http_path) = http_path { + api::start_http_path_thread(http_path, seccomp_action, exit_evt, hypervisor_type)?; + } else if let Some(http_fd) = http_fd { + api::start_http_fd_thread(http_fd, seccomp_action, exit_evt, hypervisor_type)?; + } + + #[cfg(feature = "guest_debug")] + if let Some(debug_path) = debug_path { + let target = gdb::GdbStub::new( + gdb_sender, + gdb_debug_event, + gdb_vm_debug_event, + gdb_hw_breakpoints, + ); + thread::Builder::new() + .name("gdb".to_owned()) + .spawn(move || gdb::gdb_thread(target, &debug_path)) + .map_err(Error::GdbThreadSpawn)?; + } + + Ok(thread) +} + +#[derive(Clone, Deserialize, Serialize)] +struct VmMigrationConfig { + vm_config: Arc>, + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + common_cpuid: Vec, + memory_manager_data: MemoryManagerSnapshotData, +} + +pub struct Vmm { + epoll: EpollContext, + log_reopen_timer: File, + exit_evt: EventFd, + reset_evt: EventFd, + api_evt: EventFd, + #[cfg(feature = "guest_debug")] + debug_evt: EventFd, + #[cfg(feature = "guest_debug")] + vm_debug_evt: EventFd, + version: String, + vm: Option, + vm_config: Option>>, + seccomp_action: SeccompAction, + hypervisor: Arc, + activate_evt: EventFd, + signals: Option, + threads: Vec>, + pinglog: u32, + sandbox_id: String, + vcpu_started: Arc, +} + +impl Vmm { + pub const HANDLED_SIGNALS: [i32; 2] = [SIGTERM, SIGINT]; + + fn signal_handler(mut signals: Signals, on_tty: bool, exit_evt: &EventFd) { + for sig in &Self::HANDLED_SIGNALS { + unblock_signal(*sig).unwrap(); + } + + for signal in signals.forever() { + match signal { + SIGTERM | SIGINT => { + if exit_evt.write(1).is_err() { + // Resetting the terminal is usually done as the VMM exits + if on_tty { + io::stdin() + .lock() + .set_canon_mode() + .expect("failed to restore terminal mode"); + } + std::process::exit(1); + } + } + _ => (), + } + } + } + + fn setup_signal_handler(&mut self) -> Result<()> { + let signals = Signals::new(Self::HANDLED_SIGNALS); + // SAFETY: trivially safe + let on_tty = unsafe { libc::isatty(libc::STDIN_FILENO) } != 0; + + if !on_tty { + return Ok(()); + } + + match signals { + Ok(signals) => { + self.signals = Some(signals.handle()); + let exit_evt = self.exit_evt.try_clone().map_err(Error::EventFdClone)?; + + let signal_handler_seccomp_filter = get_seccomp_filter( + &self.seccomp_action, + Thread::SignalHandler, + self.hypervisor.hypervisor_type(), + ) + .map_err(Error::CreateSeccompFilter)?; + self.threads.push( + thread::Builder::new() + .name("vmm_signal_handler".to_string()) + .spawn(move || { + if !signal_handler_seccomp_filter.is_empty() { + if let Err(e) = apply_filter(&signal_handler_seccomp_filter) + .map_err(Error::ApplySeccompFilter) + { + error!("Error applying seccomp filter: {:?}", e); + exit_evt.write(1).ok(); + return; + } + } + std::panic::catch_unwind(AssertUnwindSafe(|| { + Vmm::signal_handler(signals, on_tty, &exit_evt); + })) + .map_err(|_| { + error!("signal_handler thead panicked"); + exit_evt.write(1).ok() + }) + .ok(); + }) + .map_err(Error::SignalHandlerSpawn)?, + ); + } + Err(e) => error!("Signal not found {}", e), + } + Ok(()) + } + + fn new( + vmm_version: String, + api_evt: EventFd, + #[cfg(feature = "guest_debug")] debug_evt: EventFd, + #[cfg(feature = "guest_debug")] vm_debug_evt: EventFd, + seccomp_action: SeccompAction, + hypervisor: Arc, + exit_evt: EventFd, + sandbox_id: String, + vcpu_started: Arc, + ) -> Result { + let mut epoll = EpollContext::new().map_err(Error::Epoll)?; + let log_reopen_timer = + create_log_reopen_timer(LOG_REOPEN_INTERVAL).map_err(Error::LogReopenTimerCreate)?; + let reset_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFdCreate)?; + let activate_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFdCreate)?; + + epoll + .add_event(&log_reopen_timer, EpollDispatch::LogReopen) + .map_err(Error::Epoll)?; + + epoll + .add_event(&exit_evt, EpollDispatch::Exit) + .map_err(Error::Epoll)?; + + epoll + .add_event(&reset_evt, EpollDispatch::Reset) + .map_err(Error::Epoll)?; + + epoll + .add_event(&activate_evt, EpollDispatch::ActivateVirtioDevices) + .map_err(Error::Epoll)?; + + epoll + .add_event(&api_evt, EpollDispatch::Api) + .map_err(Error::Epoll)?; + + #[cfg(feature = "guest_debug")] + epoll + .add_event(&debug_evt, EpollDispatch::Debug) + .map_err(Error::Epoll)?; + + Ok(Vmm { + epoll, + log_reopen_timer, + exit_evt, + reset_evt, + api_evt, + #[cfg(feature = "guest_debug")] + debug_evt, + #[cfg(feature = "guest_debug")] + vm_debug_evt, + version: vmm_version, + vm: None, + vm_config: None, + seccomp_action, + hypervisor, + activate_evt, + signals: None, + threads: vec![], + pinglog: 3, + sandbox_id, + vcpu_started, + }) + } + + fn vm_create(&mut self, config: Box) -> result::Result<(), VmError> { + // We only store the passed VM config. + // The VM will be created when being asked to boot it. + if self.vm_config.is_none() { + self.vm_config = Some(Arc::new(Mutex::new(*config))); + Ok(()) + } else { + Err(VmError::VmAlreadyCreated) + } + } + + fn vm_boot(&mut self) -> result::Result<(), VmError> { + tracer::start(); + let r = { + trace_scoped!("vm_boot"); + // If we don't have a config, we can not boot a VM. + if self.vm_config.is_none() { + return Err(VmError::VmMissingConfig); + }; + + // Create a new VM if we don't have one yet. + if self.vm.is_none() { + let exit_evt = self.exit_evt.try_clone().map_err(VmError::EventFdClone)?; + let reset_evt = self.reset_evt.try_clone().map_err(VmError::EventFdClone)?; + #[cfg(feature = "guest_debug")] + let vm_debug_evt = self + .vm_debug_evt + .try_clone() + .map_err(VmError::EventFdClone)?; + let activate_evt = self + .activate_evt + .try_clone() + .map_err(VmError::EventFdClone)?; + + if let Some(ref vm_config) = self.vm_config { + let vm = Vm::new( + Arc::clone(vm_config), + exit_evt, + reset_evt, + #[cfg(feature = "guest_debug")] + vm_debug_evt, + &self.seccomp_action, + self.hypervisor.clone(), + activate_evt, + None, + None, + None, + self.sandbox_id.clone(), + self.vcpu_started.clone(), + )?; + + self.vm = Some(vm); + } + } + + // Now we can boot the VM. + if let Some(ref mut vm) = self.vm { + vm.boot() + } else { + Err(VmError::VmNotCreated) + } + }; + tracer::end(); + r + } + + fn vm_pause(&mut self) -> result::Result<(), VmError> { + if let Some(ref mut vm) = self.vm { + vm.pause().map_err(VmError::Pause) + } else { + Err(VmError::VmNotRunning) + } + } + + fn vm_pause_to_snapshot( + &mut self, + snapshot_config: &SnapshotConfig, + ) -> result::Result<(), VmError> { + self.vm_pause()?; + self.vm_snapshot(snapshot_config)?; + // Stop phase, before the reply: the baseline teardown runs here + // in full -- workers woken (the resume kick may surface pending + // requests, as on the delete path), vcpus joined, then the drop + // kills the workers (only net joins them, the rest exit on + // their own), removes the vsock and vhost-user socket files, + // and closes the tap and disk fds and the queue eventfds. + let mut vm = self.vm.take().ok_or(VmError::VmNotRunning)?; + match vm.counters() { + Ok(info) => info!("counters details: {:?}", info), + Err(e) => info!("counter failed {}", e), + } + // The memory manager owns both the guest RAM and the hypervisor + // VM handle, so holding one reference defers the unmap and the + // KVM fd close past the reply. + let memory_manager = vm.memory_manager(); + vm.shutdown()?; + drop(vm); + // Cleared only on success: on failure the VM is already gone + // and only deleting the sandbox recovers. + self.vm_config = None; + + event!("vm", "deleted"); + // Release phase, after the reply: drop the held reference -- + // the guest RAM unmap and the KVM fd close happen when the + // last reference goes, normally right here. + if let Err(e) = std::thread::Builder::new() + .name("pause-release".to_string()) + .spawn(move || { + drop(memory_manager); + info!("pause release done"); + }) + { + // A failed spawn drops the closure and the reference with + // it: the release already ran inline. + error!("spawning pause release failed: {}", e); + } + Ok(()) + } + + fn vm_resume(&mut self) -> result::Result<(), VmError> { + if let Some(ref mut vm) = self.vm { + vm.resume().map_err(VmError::Resume) + } else { + Err(VmError::VmNotRunning) + } + } + + fn vm_resume_from_snapshot( + &mut self, + restore_cfg: RestoreConfig, + ) -> result::Result<(), VmError> { + self.vm_restore(restore_cfg) + } + + fn vm_snapshot(&mut self, snapshot_config: &SnapshotConfig) -> result::Result<(), VmError> { + if let Some(ref mut vm) = self.vm { + vm.snapshot() + .map_err(VmError::Snapshot) + .and_then(|snapshot| { + vm.send(&snapshot, snapshot_config) + .map_err(VmError::SnapshotSend) + }) + } else { + Err(VmError::VmNotRunning) + } + } + + fn vm_restore(&mut self, restore_cfg: RestoreConfig) -> result::Result<(), VmError> { + if self.vm.is_some() || self.vm_config.is_some() { + return Err(VmError::VmAlreadyCreated); + } + + let source_url = restore_cfg.source_url.as_path().to_str(); + if source_url.is_none() { + return Err(VmError::InvalidRestoreSourceUrl); + } + // Safe to unwrap as we checked it was Some(&str). + let source_url = source_url.unwrap(); + let vm_config = Arc::new(Mutex::new({ + let mut vm_config = recv_vm_config(source_url).map_err(VmError::Restore)?; + if let Some(disks) = &restore_cfg.disks { + vm_config.update_disks(disks); + } + if let Some(nets) = &restore_cfg.net { + vm_config.update_nets(nets); + } + if let Some(vsock) = &restore_cfg.vsock { + vm_config.update_vsock(vsock); + } + if let Some(fses) = &restore_cfg.fs { + vm_config.update_fses(fses); + } + if let Some(pmems) = &restore_cfg.pmem { + vm_config.update_pmem(pmems); + } + if let Some(ivshmem) = &restore_cfg.ivshmem { + vm_config.update_ivshmem(ivshmem); + } + vm_config.memory.dirty_log = restore_cfg.dirty_log; + + vm_config + })); + + info!("config {:?}", vm_config); + + let snapshot = recv_vm_state(source_url).map_err(VmError::Restore)?; + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + let vm_snapshot = get_vm_snapshot(&snapshot).map_err(VmError::Restore)?; + + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + self.vm_check_cpuid_compatibility(&vm_config, &vm_snapshot.common_cpuid) + .map_err(VmError::Restore)?; + + let exit_evt = self.exit_evt.try_clone().map_err(VmError::EventFdClone)?; + let reset_evt = self.reset_evt.try_clone().map_err(VmError::EventFdClone)?; + #[cfg(feature = "guest_debug")] + let debug_evt = self + .vm_debug_evt + .try_clone() + .map_err(VmError::EventFdClone)?; + let activate_evt = self + .activate_evt + .try_clone() + .map_err(VmError::EventFdClone)?; + + let mut vm = Vm::new_from_snapshot( + &snapshot, + vm_config.clone(), + exit_evt, + reset_evt, + #[cfg(feature = "guest_debug")] + debug_evt, + Some(source_url), + restore_cfg.prefault, + &self.seccomp_action, + self.hypervisor.clone(), + activate_evt, + self.sandbox_id.clone(), + self.vcpu_started.clone(), + restore_cfg.memory_vol_url.as_deref(), + )?; + + // Now we can restore the rest of the VM. + vm.restore(snapshot).map_err(VmError::Restore)?; + vm.resume().map_err(VmError::Resume)?; + + self.vm_config = Some(vm_config.clone()); + self.vm = Some(vm); + + Ok(()) + } + + #[cfg(feature = "guest_debug")] + fn vm_coredump(&mut self, destination_url: &str) -> result::Result<(), VmError> { + if let Some(ref mut vm) = self.vm { + vm.coredump(destination_url).map_err(VmError::Coredump) + } else { + Err(VmError::VmNotRunning) + } + } + + fn vm_shutdown(&mut self) -> result::Result<(), VmError> { + if let Some(ref mut vm) = self.vm.take() { + match vm.counters() { + Ok(info) => { + info!("counters details: {:?}", info); + } + Err(e) => { + info! {"counter failed {}", e}; + } + }; + vm.shutdown() + } else { + Err(VmError::VmNotRunning) + } + } + + fn vm_reboot(&mut self) -> result::Result<(), VmError> { + // First we stop the current VM + let (config, serial_pty, console_pty, console_resize_pipe) = + if let Some(mut vm) = self.vm.take() { + let config = vm.get_config(); + let serial_pty = vm.serial_pty(); + let console_pty = vm.console_pty(); + let console_resize_pipe = vm + .console_resize_pipe() + .as_ref() + .map(|pipe| pipe.try_clone().unwrap()); + vm.shutdown()?; + (config, serial_pty, console_pty, console_resize_pipe) + } else { + return Err(VmError::VmNotCreated); + }; + + let exit_evt = self.exit_evt.try_clone().map_err(VmError::EventFdClone)?; + let reset_evt = self.reset_evt.try_clone().map_err(VmError::EventFdClone)?; + #[cfg(feature = "guest_debug")] + let debug_evt = self + .vm_debug_evt + .try_clone() + .map_err(VmError::EventFdClone)?; + let activate_evt = self + .activate_evt + .try_clone() + .map_err(VmError::EventFdClone)?; + + // The Linux kernel fires off an i8042 reset after doing the ACPI reset so there may be + // an event sitting in the shared reset_evt. Without doing this we get very early reboots + // during the boot process. + if self.reset_evt.read().is_ok() { + warn!("Spurious second reset event received. Ignoring."); + } + + // Then we create the new VM + let mut vm = Vm::new( + config, + exit_evt, + reset_evt, + #[cfg(feature = "guest_debug")] + debug_evt, + &self.seccomp_action, + self.hypervisor.clone(), + activate_evt, + serial_pty, + console_pty, + console_resize_pipe, + self.sandbox_id.clone(), + self.vcpu_started.clone(), + )?; + + // And we boot it + vm.boot()?; + + self.vm = Some(vm); + + Ok(()) + } + + fn vm_info(&self) -> result::Result { + match &self.vm_config { + Some(config) => { + let state = match &self.vm { + Some(vm) => vm.get_state()?, + None => VmState::Created, + }; + + let config = Arc::clone(config); + + let mut memory_actual_size = config.lock().unwrap().memory.total_size(); + if let Some(vm) = &self.vm { + memory_actual_size -= vm.balloon_size(); + } + + let device_tree = self.vm.as_ref().map(|vm| vm.device_tree()); + + Ok(VmInfo { + config, + state, + memory_actual_size, + device_tree, + }) + } + None => Err(VmError::VmNotCreated), + } + } + + fn vmm_ping(&self) -> VmmPingResponse { + VmmPingResponse { + version: self.version.clone(), + } + } + + fn vm_delete(&mut self) -> result::Result<(), VmError> { + if self.vm_config.is_none() { + return Ok(()); + } + + // If a VM is booted, we first try to shut it down. + if self.vm.is_some() { + self.vm_shutdown()?; + } + + self.vm_config = None; + + event!("vm", "deleted"); + + Ok(()) + } + + fn vmm_shutdown(&mut self) -> result::Result<(), VmError> { + self.vm_delete()?; + event!("vmm", "shutdown"); + Ok(()) + } + + fn vm_wait_start(&mut self) -> VmWaitStartResponse { + let mut response = VmWaitStartResponse { started: true }; + if let Some(ref vm) = self.vm { + response.started = vm.sys_started(); + } + + response + } + + fn vm_resize( + &mut self, + desired_vcpus: Option, + desired_ram: Option, + desired_balloon: Option, + ) -> result::Result<(), VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + if let Some(ref mut vm) = self.vm { + if let Err(e) = vm.resize(desired_vcpus, desired_ram, desired_balloon) { + error!("Error when resizing VM: {:?}", e); + Err(e) + } else { + Ok(()) + } + } else { + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap(); + if let Some(desired_vcpus) = desired_vcpus { + config.cpus.boot_vcpus = desired_vcpus; + } + if let Some(desired_ram) = desired_ram { + config.memory.size = desired_ram; + } + if let Some(desired_balloon) = desired_balloon { + if let Some(balloon_config) = &mut config.balloon { + balloon_config.size = desired_balloon; + } + } + Ok(()) + } + } + + fn vm_resize_zone(&mut self, id: String, desired_ram: u64) -> result::Result<(), VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + if let Some(ref mut vm) = self.vm { + if let Err(e) = vm.resize_zone(id, desired_ram) { + error!("Error when resizing VM: {:?}", e); + Err(e) + } else { + Ok(()) + } + } else { + // Update VmConfig by setting the new desired ram. + let memory_config = &mut self.vm_config.as_ref().unwrap().lock().unwrap().memory; + + if let Some(zones) = &mut memory_config.zones { + for zone in zones.iter_mut() { + if zone.id == id { + zone.size = desired_ram; + return Ok(()); + } + } + } + + error!("Could not find the memory zone {} for the resize", id); + Err(VmError::ResizeZone) + } + } + + fn vm_add_device( + &mut self, + device_cfg: DeviceConfig, + ) -> result::Result>, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.devices, device_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + + if let Some(ref mut vm) = self.vm { + let info = vm.add_device(device_cfg).map_err(|e| { + error!("Error when adding new device to the VM: {:?}", e); + e + })?; + serde_json::to_vec(&info) + .map(Some) + .map_err(VmError::SerializeJson) + } else { + // Update VmConfig by adding the new device. + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap(); + add_to_config(&mut config.devices, device_cfg); + Ok(None) + } + } + + fn vm_add_user_device( + &mut self, + device_cfg: UserDeviceConfig, + ) -> result::Result>, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.user_devices, device_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + + if let Some(ref mut vm) = self.vm { + let info = vm.add_user_device(device_cfg).map_err(|e| { + error!("Error when adding new user device to the VM: {:?}", e); + e + })?; + serde_json::to_vec(&info) + .map(Some) + .map_err(VmError::SerializeJson) + } else { + // Update VmConfig by adding the new device. + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap(); + add_to_config(&mut config.user_devices, device_cfg); + Ok(None) + } + } + + fn vm_remove_device(&mut self, id: String) -> result::Result<(), VmError> { + if let Some(ref mut vm) = self.vm { + if let Err(e) = vm.remove_device(id) { + error!("Error when removing new device to the VM: {:?}", e); + Err(e) + } else { + Ok(()) + } + } else { + Err(VmError::VmNotRunning) + } + } + + fn vm_add_disk(&mut self, disk_cfg: DiskConfig) -> result::Result>, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.disks, disk_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + + if let Some(ref mut vm) = self.vm { + let info = vm.add_disk(disk_cfg).map_err(|e| { + error!("Error when adding new disk to the VM: {:?}", e); + e + })?; + serde_json::to_vec(&info) + .map(Some) + .map_err(VmError::SerializeJson) + } else { + // Update VmConfig by adding the new device. + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap(); + add_to_config(&mut config.disks, disk_cfg); + Ok(None) + } + } + + fn vm_add_fs(&mut self, fs_cfg: FsConfig) -> result::Result>, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.fs, fs_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + + if let Some(ref mut vm) = self.vm { + let info = vm.add_fs(fs_cfg).map_err(|e| { + error!("Error when adding new fs to the VM: {:?}", e); + e + })?; + serde_json::to_vec(&info) + .map(Some) + .map_err(VmError::SerializeJson) + } else { + // Update VmConfig by adding the new device. + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap(); + add_to_config(&mut config.fs, fs_cfg); + Ok(None) + } + } + + fn vm_set_fs(&mut self, fs_cfg: FsConfig) -> result::Result<(), VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + let fses = vec![fs_cfg.clone()]; + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap(); + config.update_fses(&fses); + + if let Some(ref mut vm) = self.vm { + vm.fs_device_update(fs_cfg) + } else { + Ok(()) + } + } + + fn vm_add_pmem(&mut self, pmem_cfg: PmemConfig) -> result::Result>, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.pmem, pmem_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + + if let Some(ref mut vm) = self.vm { + let info = vm.add_pmem(pmem_cfg).map_err(|e| { + error!("Error when adding new pmem device to the VM: {:?}", e); + e + })?; + serde_json::to_vec(&info) + .map(Some) + .map_err(VmError::SerializeJson) + } else { + // Update VmConfig by adding the new device. + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap(); + add_to_config(&mut config.pmem, pmem_cfg); + Ok(None) + } + } + + fn vm_add_net(&mut self, net_cfg: NetConfig) -> result::Result>, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.net, net_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + + if let Some(ref mut vm) = self.vm { + let info = vm.add_net(net_cfg).map_err(|e| { + error!("Error when adding new network device to the VM: {:?}", e); + e + })?; + serde_json::to_vec(&info) + .map(Some) + .map_err(VmError::SerializeJson) + } else { + // Update VmConfig by adding the new device. + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap(); + add_to_config(&mut config.net, net_cfg); + Ok(None) + } + } + + fn vm_add_vdpa(&mut self, vdpa_cfg: VdpaConfig) -> result::Result>, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.vdpa, vdpa_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + + if let Some(ref mut vm) = self.vm { + let info = vm.add_vdpa(vdpa_cfg).map_err(|e| { + error!("Error when adding new vDPA device to the VM: {:?}", e); + e + })?; + serde_json::to_vec(&info) + .map(Some) + .map_err(VmError::SerializeJson) + } else { + // Update VmConfig by adding the new device. + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap(); + add_to_config(&mut config.vdpa, vdpa_cfg); + Ok(None) + } + } + + fn vm_add_vsock(&mut self, vsock_cfg: VsockConfig) -> result::Result>, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + + if config.vsock.is_some() { + return Err(VmError::TooManyVsockDevices); + } + + config.vsock = Some(vsock_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + + if let Some(ref mut vm) = self.vm { + let info = vm.add_vsock(vsock_cfg).map_err(|e| { + error!("Error when adding new vsock device to the VM: {:?}", e); + e + })?; + serde_json::to_vec(&info) + .map(Some) + .map_err(VmError::SerializeJson) + } else { + // Update VmConfig by adding the new device. + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap(); + config.vsock = Some(vsock_cfg); + Ok(None) + } + } + + fn vm_counters(&mut self) -> result::Result>, VmError> { + if let Some(ref mut vm) = self.vm { + let info = vm.counters().map_err(|e| { + error!("Error when getting counters from the VM: {:?}", e); + e + })?; + serde_json::to_vec(&info) + .map(Some) + .map_err(VmError::SerializeJson) + } else { + Err(VmError::VmNotRunning) + } + } + + fn vm_power_button(&mut self) -> result::Result<(), VmError> { + if let Some(ref mut vm) = self.vm { + vm.power_button() + } else { + Err(VmError::VmNotRunning) + } + } + + fn vm_receive_config( + &mut self, + req: &Request, + socket: &mut T, + existing_memory_files: Option>, + ) -> std::result::Result>, MigratableError> + where + T: Read + Write, + { + // Read in config data along with memory manager data + let mut data: Vec = Vec::new(); + data.resize_with(req.length() as usize, Default::default); + socket + .read_exact(&mut data) + .map_err(MigratableError::MigrateSocket)?; + + let vm_migration_config: VmMigrationConfig = + serde_json::from_slice(&data).map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error deserialising config: {}", e)) + })?; + + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + self.vm_check_cpuid_compatibility( + &vm_migration_config.vm_config, + &vm_migration_config.common_cpuid, + )?; + + let config = vm_migration_config.vm_config.clone(); + self.vm_config = Some(vm_migration_config.vm_config); + + let vm = Vm::create_hypervisor_vm( + &self.hypervisor, + #[cfg(feature = "tdx")] + false, + ) + .map_err(|e| { + MigratableError::MigrateReceive(anyhow!( + "Error creating hypervisor VM from snapshot: {:?}", + e + )) + })?; + + let phys_bits = vm::physical_bits( + config.lock().unwrap().cpus.max_phys_bits, + self.hypervisor.hypervisor_type(), + ); + + let memory_manager = MemoryManager::new( + vm, + &config.lock().unwrap().memory.clone(), + None, + phys_bits, + #[cfg(feature = "tdx")] + false, + Some(&vm_migration_config.memory_manager_data), + existing_memory_files, + None, + #[cfg(target_arch = "x86_64")] + None, + ) + .map_err(|e| { + MigratableError::MigrateReceive(anyhow!( + "Error creating MemoryManager from snapshot: {:?}", + e + )) + })?; + + Response::ok().write_to(socket)?; + + Ok(memory_manager) + } + + fn vm_receive_state( + &mut self, + req: &Request, + socket: &mut T, + mm: Arc>, + ) -> std::result::Result<(), MigratableError> + where + T: Read + Write, + { + // Read in state data + let mut data: Vec = Vec::new(); + data.resize_with(req.length() as usize, Default::default); + socket + .read_exact(&mut data) + .map_err(MigratableError::MigrateSocket)?; + let snapshot: Snapshot = serde_json::from_slice(&data).map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error deserialising snapshot: {}", e)) + })?; + + let exit_evt = self.exit_evt.try_clone().map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error cloning exit EventFd: {}", e)) + })?; + let reset_evt = self.reset_evt.try_clone().map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error cloning reset EventFd: {}", e)) + })?; + #[cfg(feature = "guest_debug")] + let debug_evt = self.vm_debug_evt.try_clone().map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error cloning debug EventFd: {}", e)) + })?; + let activate_evt = self.activate_evt.try_clone().map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error cloning activate EventFd: {}", e)) + })?; + + let timestamp = Instant::now(); + let hypervisor_vm = mm.lock().unwrap().vm.clone(); + let mut vm = Vm::new_from_memory_manager( + self.vm_config.clone().unwrap(), + mm, + hypervisor_vm, + exit_evt, + reset_evt, + #[cfg(feature = "guest_debug")] + debug_evt, + &self.seccomp_action, + self.hypervisor.clone(), + activate_evt, + true, + timestamp, + Some(&snapshot), + self.sandbox_id.clone(), + self.vcpu_started.clone(), + ) + .map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error creating VM from snapshot: {:?}", e)) + })?; + + // Create VM + vm.restore(snapshot).map_err(|e| { + Response::error().write_to(socket).ok(); + e + })?; + self.vm = Some(vm); + + Response::ok().write_to(socket)?; + + Ok(()) + } + + fn vm_receive_memory( + &mut self, + req: &Request, + socket: &mut T, + memory_manager: &mut MemoryManager, + ) -> std::result::Result<(), MigratableError> + where + T: Read + ReadVolatile + Write, + { + // Read table + let table = MemoryRangeTable::read_from(socket, req.length())?; + + // And then read the memory itself + memory_manager + .receive_memory_regions(&table, socket) + .map_err(|e| { + Response::error().write_to(socket).ok(); + e + })?; + Response::ok().write_to(socket)?; + Ok(()) + } + + fn socket_url_to_path(url: &str) -> result::Result { + url.strip_prefix("unix:") + .ok_or_else(|| { + MigratableError::MigrateSend(anyhow!("Could not extract path from URL: {}", url)) + }) + .map(|s| s.into()) + } + + fn vm_receive_migration( + &mut self, + receive_data_migration: VmReceiveMigrationData, + ) -> result::Result<(), MigratableError> { + info!( + "Receiving migration: receiver_url = {}", + receive_data_migration.receiver_url + ); + + let path = Self::socket_url_to_path(&receive_data_migration.receiver_url)?; + let listener = UnixListener::bind(&path).map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error binding to UNIX socket: {}", e)) + })?; + let (mut socket, _addr) = listener.accept().map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error accepting on UNIX socket: {}", e)) + })?; + std::fs::remove_file(&path).map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error unlinking UNIX socket: {}", e)) + })?; + + let mut started = false; + let mut memory_manager: Option>> = None; + let mut existing_memory_files = None; + loop { + let req = Request::read_from(&mut socket)?; + match req.command() { + Command::Invalid => info!("Invalid Command Received"), + Command::Start => { + info!("Start Command Received"); + started = true; + + Response::ok().write_to(&mut socket)?; + } + Command::Config => { + info!("Config Command Received"); + + if !started { + warn!("Migration not started yet"); + Response::error().write_to(&mut socket)?; + continue; + } + memory_manager = Some(self.vm_receive_config( + &req, + &mut socket, + existing_memory_files.take(), + )?); + } + Command::State => { + info!("State Command Received"); + + if !started { + warn!("Migration not started yet"); + Response::error().write_to(&mut socket)?; + continue; + } + if let Some(mm) = memory_manager.take() { + self.vm_receive_state(&req, &mut socket, mm)?; + } else { + warn!("Configuration not sent yet"); + Response::error().write_to(&mut socket)?; + } + } + Command::Memory => { + info!("Memory Command Received"); + + if !started { + warn!("Migration not started yet"); + Response::error().write_to(&mut socket)?; + continue; + } + if let Some(mm) = memory_manager.as_ref() { + self.vm_receive_memory(&req, &mut socket, &mut mm.lock().unwrap())?; + } else { + warn!("Configuration not sent yet"); + Response::error().write_to(&mut socket)?; + } + } + Command::MemoryFd => { + info!("MemoryFd Command Received"); + + if !started { + warn!("Migration not started yet"); + Response::error().write_to(&mut socket)?; + continue; + } + + let mut buf = [0u8; 4]; + let (_, file) = socket.recv_with_fd(&mut buf).map_err(|e| { + MigratableError::MigrateReceive(anyhow!( + "Error receiving slot from socket: {}", + e + )) + })?; + + if existing_memory_files.is_none() { + existing_memory_files = Some(HashMap::default()) + } + + if let Some(ref mut existing_memory_files) = existing_memory_files { + let slot = u32::from_le_bytes(buf); + existing_memory_files.insert(slot, file.unwrap()); + } + + Response::ok().write_to(&mut socket)?; + } + Command::Complete => { + info!("Complete Command Received"); + if let Some(ref mut vm) = self.vm.as_mut() { + vm.resume()?; + event_notify!(NotifyEvent::MigrationComplete); + Response::ok().write_to(&mut socket)?; + } else { + warn!("VM not created yet"); + Response::error().write_to(&mut socket)?; + } + break; + } + Command::Abandon => { + info!("Abandon Command Received"); + self.vm = None; + self.vm_config = None; + Response::ok().write_to(&mut socket).ok(); + break; + } + } + } + + Ok(()) + } + + // Returns true if there were dirty pages to send + fn vm_maybe_send_dirty_pages( + vm: &mut Vm, + socket: &mut T, + ) -> result::Result + where + T: Read + Write + WriteVolatile, + { + // Send (dirty) memory table + let table = vm.dirty_log()?; + + // But if there are no regions go straight to pause + if table.regions().is_empty() { + return Ok(false); + } + + Request::memory(table.length()).write_to(socket).unwrap(); + table.write_to(socket)?; + // And then the memory itself + vm.send_memory_regions(&table, socket)?; + let res = Response::read_from(socket)?; + if res.status() != Status::Ok { + warn!("Error during dirty memory migration"); + Request::abandon().write_to(socket)?; + Response::read_from(socket).ok(); + return Err(MigratableError::MigrateSend(anyhow!( + "Error during dirty memory migration" + ))); + } + + Ok(true) + } + + fn send_migration( + vm: &mut Vm, + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] hypervisor: Arc< + dyn hypervisor::Hypervisor, + >, + send_data_migration: VmSendMigrationData, + ) -> result::Result<(), MigratableError> { + let path = Self::socket_url_to_path(&send_data_migration.destination_url)?; + let mut socket = UnixStream::connect(path).map_err(|e| { + MigratableError::MigrateSend(anyhow!("Error connecting to UNIX socket: {}", e)) + })?; + + // Start the migration + Request::start().write_to(&mut socket)?; + let res = Response::read_from(&mut socket)?; + if res.status() != Status::Ok { + warn!("Error starting migration"); + Request::abandon().write_to(&mut socket)?; + Response::read_from(&mut socket).ok(); + return Err(MigratableError::MigrateSend(anyhow!( + "Error starting migration" + ))); + } + + // Send config + let vm_config = vm.get_config(); + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + let common_cpuid = { + let phys_bits = vm::physical_bits( + vm_config.lock().unwrap().cpus.max_phys_bits, + hypervisor.hypervisor_type(), + ); + let cpu_config = &mut vm_config.lock().unwrap().cpus; + + let (vendor_compatible, arch_compatible) = match cpu_config.compatible { + CompatibleMode::Vendor => (true, false), + CompatibleMode::Max => (true, true), + CompatibleMode::Ignore => (false, false), + }; + + arch::generate_common_cpuid( + hypervisor, + None, + None, + phys_bits, + cpu_config.kvm_hyperv, + #[cfg(feature = "tdx")] + vm_config.lock().unwrap().is_tdx_enabled(), + vendor_compatible, + arch_compatible, + ) + .map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error generating common cpuid': {:?}", e)) + })? + }; + + if send_data_migration.local { + vm.send_memory_fds(&mut socket)?; + } + + let vm_migration_config = VmMigrationConfig { + vm_config, + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + common_cpuid, + memory_manager_data: vm.memory_manager_data(), + }; + let config_data = serde_json::to_vec(&vm_migration_config).unwrap(); + Request::config(config_data.len() as u64).write_to(&mut socket)?; + socket + .write_all(&config_data) + .map_err(MigratableError::MigrateSocket)?; + let res = Response::read_from(&mut socket)?; + if res.status() != Status::Ok { + warn!("Error during config migration"); + Request::abandon().write_to(&mut socket)?; + Response::read_from(&mut socket).ok(); + return Err(MigratableError::MigrateSend(anyhow!( + "Error during config migration" + ))); + } + + // Let every Migratable object know about the migration being started. + vm.start_migration()?; + + if send_data_migration.local { + // Now pause VM + vm.pause()?; + } else { + // Start logging dirty pages + vm.start_dirty_log()?; + + // Send memory table + let table = vm.memory_range_table()?; + Request::memory(table.length()) + .write_to(&mut socket) + .unwrap(); + table.write_to(&mut socket)?; + // And then the memory itself + vm.send_memory_regions(&table, &mut socket)?; + let res = Response::read_from(&mut socket)?; + if res.status() != Status::Ok { + warn!("Error during memory migration"); + Request::abandon().write_to(&mut socket)?; + Response::read_from(&mut socket).ok(); + return Err(MigratableError::MigrateSend(anyhow!( + "Error during memory migration" + ))); + } + + // Try at most 5 passes of dirty memory sending + const MAX_DIRTY_MIGRATIONS: usize = 5; + for i in 0..MAX_DIRTY_MIGRATIONS { + info!("Dirty memory migration {} of {}", i, MAX_DIRTY_MIGRATIONS); + if !Self::vm_maybe_send_dirty_pages(vm, &mut socket)? { + break; + } + } + + // Now pause VM + vm.pause()?; + + // Send last batch of dirty pages + Self::vm_maybe_send_dirty_pages(vm, &mut socket)?; + + // Stop logging dirty pages + vm.stop_dirty_log()?; + } + // Capture snapshot and send it + let vm_snapshot = vm.snapshot()?; + let snapshot_data = serde_json::to_vec(&vm_snapshot).unwrap(); + Request::state(snapshot_data.len() as u64).write_to(&mut socket)?; + socket + .write_all(&snapshot_data) + .map_err(MigratableError::MigrateSocket)?; + let res = Response::read_from(&mut socket)?; + if res.status() != Status::Ok { + warn!("Error during state migration"); + Request::abandon().write_to(&mut socket)?; + Response::read_from(&mut socket).ok(); + return Err(MigratableError::MigrateSend(anyhow!( + "Error during state migration" + ))); + } + + // Complete the migration + Request::complete().write_to(&mut socket)?; + let res = Response::read_from(&mut socket)?; + if res.status() != Status::Ok { + warn!("Error completing migration"); + Request::abandon().write_to(&mut socket)?; + Response::read_from(&mut socket).ok(); + return Err(MigratableError::MigrateSend(anyhow!( + "Error completing migration" + ))); + } + info!("Migration complete"); + + // Let every Migratable object know about the migration being complete + vm.complete_migration() + } + + fn vm_send_migration( + &mut self, + send_data_migration: VmSendMigrationData, + ) -> result::Result<(), MigratableError> { + info!( + "Sending migration: destination_url = {}, local = {}", + send_data_migration.destination_url, send_data_migration.local + ); + + if !self + .vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .backed_by_shared_memory() + && send_data_migration.local + { + return Err(MigratableError::MigrateSend(anyhow!( + "Local migration requires shared memory or hugepages enabled" + ))); + } + + if let Some(vm) = self.vm.as_mut() { + Self::send_migration( + vm, + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + self.hypervisor.clone(), + send_data_migration, + ) + .map_err(|migration_err| { + error!("Migration failed: {:?}", migration_err); + + event_notify!(NotifyEvent::MigrationFail); + + // Stop logging dirty pages + if let Err(e) = vm.stop_dirty_log() { + return e; + } + + if vm.get_state().unwrap() == VmState::Paused { + if let Err(e) = vm.resume() { + return e; + } + } + + migration_err + })?; + + event_notify!(NotifyEvent::MigrationComplete); + + // Shutdown the VM after the migration succeeded + self.exit_evt.write(1).map_err(|e| { + MigratableError::MigrateSend(anyhow!( + "Failed shutting down the VM after migration: {:?}", + e + )) + }) + } else { + Err(MigratableError::MigrateSend(anyhow!("VM is not running"))) + } + } + + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + fn vm_check_cpuid_compatibility( + &self, + src_vm_config: &Arc>, + src_vm_cpuid: &[hypervisor::arch::x86::CpuIdEntry], + ) -> result::Result<(), MigratableError> { + // We check the `CPUID` compatibility of between the source vm and destination, which is + // mostly about feature compatibility and "topology/sgx" leaves are not relevant. + let dest_cpuid = &{ + let vm_config = &src_vm_config.lock().unwrap(); + + let phys_bits = vm::physical_bits( + vm_config.cpus.max_phys_bits, + self.hypervisor.hypervisor_type(), + ); + + let (vendor_compatible, arch_compatible) = match vm_config.cpus.compatible { + CompatibleMode::Vendor => (true, false), + CompatibleMode::Max => (true, true), + CompatibleMode::Ignore => (false, false), + }; + + arch::generate_common_cpuid( + self.hypervisor.clone(), + None, + None, + phys_bits, + vm_config.cpus.kvm_hyperv, + #[cfg(feature = "tdx")] + vm_config.is_tdx_enabled(), + vendor_compatible, + arch_compatible, + ) + .map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error generating common cpuid: {:?}", e)) + })? + }; + arch::CpuidFeatureEntry::check_cpuid_compatibility(src_vm_cpuid, dest_cpuid).map_err(|e| { + MigratableError::MigrateReceive(anyhow!( + "Error checking cpu feature compatibility': {:?}", + e + )) + }) + } + + fn control_loop( + &mut self, + api_receiver: Rc>, + res_sender: Rc>, + #[cfg(feature = "guest_debug")] gdb_receiver: Rc>, + vmm_barrier: Option>, + ) -> Result<()> { + const EPOLL_EVENTS_LEN: usize = 100; + + let mut events = vec![epoll::Event::new(epoll::Events::empty(), 0); EPOLL_EVENTS_LEN]; + let epoll_fd = self.epoll.as_raw_fd(); + + if let Some(barrier) = vmm_barrier { + barrier.wait(); + } + info!("The control_loop is ready"); + 'outer: loop { + let num_events = match epoll::wait(epoll_fd, -1, &mut events[..]) { + Ok(res) => res, + Err(e) => { + if e.kind() == io::ErrorKind::Interrupted { + // It's well defined from the epoll_wait() syscall + // documentation that the epoll loop can be interrupted + // before any of the requested events occurred or the + // timeout expired. In both those cases, epoll_wait() + // returns an error of type EINTR, but this should not + // be considered as a regular error. Instead it is more + // appropriate to retry, by calling into epoll_wait(). + continue; + } + return Err(Error::Epoll(e)); + } + }; + + for event in events.iter().take(num_events) { + let dispatch_event: EpollDispatch = event.data.into(); + match dispatch_event { + EpollDispatch::Unknown => { + let event = event.data; + warn!("Unknown VMM loop event: {}", event); + } + EpollDispatch::Exit => { + info!("VM exit event"); + // Consume the event. + self.exit_evt.read().map_err(Error::EventFdRead)?; + self.vmm_shutdown().map_err(Error::VmmShutdown)?; + + break 'outer; + } + EpollDispatch::Reset => { + info!("VM reset event"); + // Consume the event. + self.reset_evt.read().map_err(Error::EventFdRead)?; + self.vm_reboot().map_err(Error::VmReboot)?; + } + EpollDispatch::ActivateVirtioDevices => { + if let Some(ref vm) = self.vm { + let count = self.activate_evt.read().map_err(Error::EventFdRead)?; + info!( + "Trying to activate pending virtio devices: count = {}", + count + ); + vm.activate_virtio_devices() + .map_err(Error::ActivateVirtioDevices)?; + } + } + EpollDispatch::LogReopen => { + if read_log_reopen_timer(&mut self.log_reopen_timer) + .map_err(Error::LogReopenTimerRead)? + { + // The default VMM verbosity is Warn. Keep the + // control record visible to the logger facade at + // that level; the file decorator consumes it + // without writing a warning line. + warn!(target: LOG_CTRL_REOPEN, "periodic log reopen"); + } + } + EpollDispatch::Api => { + // Consume the events. + for _ in 0..self.api_evt.read().map_err(Error::EventFdRead)? { + // Read from the API receiver channel + let api_request = api_receiver.recv().map_err(Error::ApiRequestRecv)?; + + match api_request { + ApiRequest::VmmPing => { + if self.pinglog != 0 { + self.pinglog -= 1; + info!("API request event: {:?}", api_request); + } + } + _ => { + info!("API request event: {:?}", api_request); + } + } + + match api_request { + ApiRequest::VmCreate(config) => { + let response = self + .vm_create(config) + .map_err(ApiError::VmCreate) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmDelete => { + let response = self + .vm_delete() + .map_err(ApiError::VmDelete) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmBoot => { + let response = self + .vm_boot() + .map_err(ApiError::VmBoot) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmShutdown => { + let response = self + .vm_shutdown() + .map_err(ApiError::VmShutdown) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmReboot => { + let response = self + .vm_reboot() + .map_err(ApiError::VmReboot) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmInfo => { + let response = self + .vm_info() + .map_err(ApiError::VmInfo) + .map(ApiResponsePayload::VmInfo); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmmPing => { + let response = ApiResponsePayload::VmmPing(self.vmm_ping()); + + res_sender + .send(Ok(response)) + .map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmPause => { + let response = self + .vm_pause() + .map_err(ApiError::VmPause) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmPauseToSnapshot(snapshot_data) => { + let response = self + .vm_pause_to_snapshot(&snapshot_data) + .map_err(ApiError::VmPauseToSnapshot) + .map(|_| ApiResponsePayload::Empty); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmResumeFromSnapshot(restore_data) => { + let response = self + .vm_resume_from_snapshot(restore_data.as_ref().clone()) + .map_err(ApiError::VmResumeFromSnapshot) + .map(|_| ApiResponsePayload::Empty); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmResume => { + let response = self + .vm_resume() + .map_err(ApiError::VmResume) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmSnapshot(snapshot_data) => { + let response = self + .vm_snapshot(&snapshot_data) + .map_err(ApiError::VmSnapshot) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmRestore(restore_data) => { + let response = self + .vm_restore(restore_data.as_ref().clone()) + .map_err(ApiError::VmRestore) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + #[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] + ApiRequest::VmCoredump(coredump_data) => { + let response = self + .vm_coredump(&coredump_data.destination_url) + .map_err(ApiError::VmCoredump) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmmShutdown => { + let response = self + .vmm_shutdown() + .map_err(ApiError::VmmShutdown) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + + break 'outer; + } + ApiRequest::VmWaitStart => { + let response = + ApiResponsePayload::VmWaitStart(self.vm_wait_start()); + + res_sender + .send(Ok(response)) + .map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmResize(resize_data) => { + let response = self + .vm_resize( + resize_data.desired_vcpus, + resize_data.desired_ram, + resize_data.desired_balloon, + ) + .map_err(ApiError::VmResize) + .map(|_| ApiResponsePayload::Empty); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmResizeZone(resize_zone_data) => { + let response = self + .vm_resize_zone( + resize_zone_data.id.clone(), + resize_zone_data.desired_ram, + ) + .map_err(ApiError::VmResizeZone) + .map(|_| ApiResponsePayload::Empty); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmAddDevice(add_device_data) => { + let response = self + .vm_add_device(add_device_data.as_ref().clone()) + .map_err(ApiError::VmAddDevice) + .map(ApiResponsePayload::VmAction); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmAddUserDevice(add_device_data) => { + let response = self + .vm_add_user_device(add_device_data.as_ref().clone()) + .map_err(ApiError::VmAddUserDevice) + .map(ApiResponsePayload::VmAction); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmRemoveDevice(remove_device_data) => { + let response = self + .vm_remove_device(remove_device_data.id.clone()) + .map_err(ApiError::VmRemoveDevice) + .map(|_| ApiResponsePayload::Empty); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmAddDisk(add_disk_data) => { + let response = self + .vm_add_disk(add_disk_data.as_ref().clone()) + .map_err(ApiError::VmAddDisk) + .map(ApiResponsePayload::VmAction); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmAddFs(add_fs_data) => { + let response = self + .vm_add_fs(add_fs_data.as_ref().clone()) + .map_err(ApiError::VmAddFs) + .map(ApiResponsePayload::VmAction); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmSetFs(set_fs_data) => { + let response = self + .vm_set_fs(set_fs_data.as_ref().clone()) + .map_err(ApiError::VmSetFs) + .map(|_| ApiResponsePayload::Empty); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmAddPmem(add_pmem_data) => { + let response = self + .vm_add_pmem(add_pmem_data.as_ref().clone()) + .map_err(ApiError::VmAddPmem) + .map(ApiResponsePayload::VmAction); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmAddNet(add_net_data) => { + let response = self + .vm_add_net(add_net_data.as_ref().clone()) + .map_err(ApiError::VmAddNet) + .map(ApiResponsePayload::VmAction); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmAddVdpa(add_vdpa_data) => { + let response = self + .vm_add_vdpa(add_vdpa_data.as_ref().clone()) + .map_err(ApiError::VmAddVdpa) + .map(ApiResponsePayload::VmAction); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmAddVsock(add_vsock_data) => { + let response = self + .vm_add_vsock(add_vsock_data.as_ref().clone()) + .map_err(ApiError::VmAddVsock) + .map(ApiResponsePayload::VmAction); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmCounters => { + let response = self + .vm_counters() + .map_err(ApiError::VmInfo) + .map(ApiResponsePayload::VmAction); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmReceiveMigration(receive_migration_data) => { + let response = self + .vm_receive_migration( + receive_migration_data.as_ref().clone(), + ) + .map_err(ApiError::VmReceiveMigration) + .map(|_| ApiResponsePayload::Empty); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmSendMigration(send_migration_data) => { + let response = self + .vm_send_migration(send_migration_data.as_ref().clone()) + .map_err(ApiError::VmSendMigration) + .map(|_| ApiResponsePayload::Empty); + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + ApiRequest::VmPowerButton => { + let response = self + .vm_power_button() + .map_err(ApiError::VmPowerButton) + .map(|_| ApiResponsePayload::Empty); + + res_sender.send(response).map_err(Error::ApiResponseSend)?; + } + } + } + } + #[cfg(feature = "guest_debug")] + EpollDispatch::Debug => { + // Consume the events. + for _ in 0..self.debug_evt.read().map_err(Error::EventFdRead)? { + // Read from the API receiver channel + let gdb_request = gdb_receiver.recv().map_err(Error::GdbRequestRecv)?; + + let response = if let Some(ref mut vm) = self.vm { + vm.debug_request(&gdb_request.payload, gdb_request.cpu_id) + } else { + Err(VmError::VmNotRunning) + } + .map_err(gdb::Error::Vm); + + gdb_request + .sender + .send(response) + .map_err(Error::GdbResponseSend)?; + } + } + #[cfg(not(feature = "guest_debug"))] + EpollDispatch::Debug => {} + } + } + } + + // Trigger the termination of the signal_handler thread + if let Some(signals) = self.signals.take() { + signals.close(); + } + + // Wait for all the threads to finish + for thread in self.threads.drain(..) { + thread.join().map_err(Error::ThreadCleanup)? + } + + Ok(()) + } +} + +const CPU_MANAGER_SNAPSHOT_ID: &str = "cpu-manager"; +const MEMORY_MANAGER_SNAPSHOT_ID: &str = "memory-manager"; +const DEVICE_MANAGER_SNAPSHOT_ID: &str = "device-manager"; + +#[cfg(test)] +mod unit_tests { + use super::*; + use crate::vm_config::{CompatibleMode, CpuFeatures}; + use vm_config::{ + ConsoleConfig, ConsoleOutputMode, CpusConfig, HotplugMethod, MemoryConfig, PayloadConfig, + RngConfig, VmConfig, + }; + + #[test] + fn log_reopen_timer_expires() { + let mut timer = create_log_reopen_timer(Duration::from_millis(1)).unwrap(); + for _ in 0..100 { + if read_log_reopen_timer(&mut timer).unwrap() { + return; + } + thread::sleep(Duration::from_millis(1)); + } + panic!("log-reopen timer did not expire"); + } + + #[test] + fn log_reopen_timer_handles_would_block() { + let mut timer = create_log_reopen_timer(Duration::from_secs(3600)).unwrap(); + + assert!(!read_log_reopen_timer(&mut timer).unwrap()); + } + + #[test] + fn log_reopen_timer_handles_interrupted_read() { + struct InterruptOnceReader { + interrupted: bool, + } + + impl Read for InterruptOnceReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + if !self.interrupted { + self.interrupted = true; + return Err(io::Error::from(io::ErrorKind::Interrupted)); + } + Ok(buf.len()) + } + } + + let mut reader = InterruptOnceReader { interrupted: false }; + assert!(!read_log_reopen_timer(&mut reader).unwrap()); + assert!(read_log_reopen_timer(&mut reader).unwrap()); + } + + fn create_dummy_vmm() -> Vmm { + Vmm::new( + "dummy".to_string(), + EventFd::new(EFD_NONBLOCK).unwrap(), + #[cfg(feature = "guest_debug")] + EventFd::new(EFD_NONBLOCK).unwrap(), + #[cfg(feature = "guest_debug")] + EventFd::new(EFD_NONBLOCK).unwrap(), + SeccompAction::Allow, + hypervisor::new().unwrap(), + EventFd::new(EFD_NONBLOCK).unwrap(), + "dummy".to_string(), + Arc::new(AtomicBool::new(false)), + ) + .unwrap() + } + + fn create_dummy_vm_config() -> Box { + Box::new(VmConfig { + cpus: CpusConfig { + boot_vcpus: 1, + max_vcpus: 1, + topology: None, + kvm_hyperv: false, + max_phys_bits: 46, + affinity: None, + features: CpuFeatures::default(), + compatible: CompatibleMode::Vendor, + }, + memory: MemoryConfig { + size: 536_870_912, + mergeable: false, + hotplug_method: HotplugMethod::Acpi, + hotplug_size: None, + hotplugged_size: None, + shared: true, + hugepages: false, + hugepage_size: None, + prefault: false, + zones: None, + thp: true, + dirty_log: false, + }, + payload: Some(PayloadConfig { + kernel: Some(PathBuf::from("/path/to/kernel")), + ..Default::default() + }), + disks: None, + net: None, + rng: RngConfig { + src: PathBuf::from("/dev/urandom"), + iommu: false, + }, + balloon: None, + fs: None, + pmem: None, + serial: ConsoleConfig { + file: None, + mode: ConsoleOutputMode::Null, + iommu: false, + sigwinch: false, + }, + console: ConsoleConfig { + file: None, + mode: ConsoleOutputMode::Tty, + iommu: false, + sigwinch: false, + }, + devices: None, + user_devices: None, + vdpa: None, + vsock: None, + pvpanic: false, + iommu: false, + #[cfg(target_arch = "x86_64")] + sgx_epc: None, + numa: None, + watchdog: false, + #[cfg(feature = "guest_debug")] + gdb: false, + platform: None, + tpm: None, + sys_ctrl: false, + ivshmem: None, + }) + } + + #[test] + fn test_vmm_vm_create() { + let mut vmm = create_dummy_vmm(); + let config = create_dummy_vm_config(); + + assert!(matches!(vmm.vm_create(config.clone()), Ok(()))); + assert!(matches!( + vmm.vm_create(config), + Err(VmError::VmAlreadyCreated) + )); + } + + #[test] + fn test_vmm_vm_cold_add_device() { + let mut vmm = create_dummy_vmm(); + let device_config = DeviceConfig::parse("path=/path/to/device").unwrap(); + + assert!(matches!( + vmm.vm_add_device(device_config.clone()), + Err(VmError::VmNotCreated) + )); + + let _ = vmm.vm_create(create_dummy_vm_config()); + assert!(vmm + .vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .devices + .is_none()); + + let result = vmm.vm_add_device(device_config.clone()); + assert!(result.is_ok()); + assert!(result.unwrap().is_none()); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .devices + .clone() + .unwrap() + .len(), + 1 + ); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .devices + .clone() + .unwrap()[0], + device_config + ); + } + + #[test] + fn test_vmm_vm_cold_add_user_device() { + let mut vmm = create_dummy_vmm(); + let user_device_config = + UserDeviceConfig::parse("socket=/path/to/socket,id=8,pci_segment=2").unwrap(); + + assert!(matches!( + vmm.vm_add_user_device(user_device_config.clone()), + Err(VmError::VmNotCreated) + )); + + let _ = vmm.vm_create(create_dummy_vm_config()); + assert!(vmm + .vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .user_devices + .is_none()); + + let result = vmm.vm_add_user_device(user_device_config.clone()); + assert!(result.is_ok()); + assert!(result.unwrap().is_none()); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .user_devices + .clone() + .unwrap() + .len(), + 1 + ); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .user_devices + .clone() + .unwrap()[0], + user_device_config + ); + } + + #[test] + fn test_vmm_vm_cold_add_disk() { + let mut vmm = create_dummy_vmm(); + let disk_config = DiskConfig::parse("path=/path/to_file").unwrap(); + + assert!(matches!( + vmm.vm_add_disk(disk_config.clone()), + Err(VmError::VmNotCreated) + )); + + let _ = vmm.vm_create(create_dummy_vm_config()); + assert!(vmm + .vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .disks + .is_none()); + + let result = vmm.vm_add_disk(disk_config.clone()); + assert!(result.is_ok()); + assert!(result.unwrap().is_none()); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .disks + .clone() + .unwrap() + .len(), + 1 + ); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .disks + .clone() + .unwrap()[0], + disk_config + ); + } + + #[test] + fn test_vmm_vm_cold_add_fs() { + let mut vmm = create_dummy_vmm(); + let fs_config = FsConfig::parse("tag=mytag,socket=/tmp/sock").unwrap(); + + assert!(matches!( + vmm.vm_add_fs(fs_config.clone()), + Err(VmError::VmNotCreated) + )); + + let _ = vmm.vm_create(create_dummy_vm_config()); + assert!(vmm.vm_config.as_ref().unwrap().lock().unwrap().fs.is_none()); + + let result = vmm.vm_add_fs(fs_config.clone()); + assert!(result.is_ok()); + assert!(result.unwrap().is_none()); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .fs + .clone() + .unwrap() + .len(), + 1 + ); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .fs + .clone() + .unwrap()[0], + fs_config + ); + } + + #[test] + fn test_vmm_vm_cold_add_pmem() { + let mut vmm = create_dummy_vmm(); + let pmem_config = PmemConfig::parse("file=/tmp/pmem,size=128M").unwrap(); + + assert!(matches!( + vmm.vm_add_pmem(pmem_config.clone()), + Err(VmError::VmNotCreated) + )); + + let _ = vmm.vm_create(create_dummy_vm_config()); + assert!(vmm + .vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .pmem + .is_none()); + + let result = vmm.vm_add_pmem(pmem_config.clone()); + assert!(result.is_ok()); + assert!(result.unwrap().is_none()); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .pmem + .clone() + .unwrap() + .len(), + 1 + ); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .pmem + .clone() + .unwrap()[0], + pmem_config + ); + } + + #[test] + fn test_vmm_vm_cold_add_net() { + let mut vmm = create_dummy_vmm(); + let net_config = NetConfig::parse( + "mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,vhost_user=true,socket=/tmp/sock", + ) + .unwrap(); + + assert!(matches!( + vmm.vm_add_net(net_config.clone()), + Err(VmError::VmNotCreated) + )); + + let _ = vmm.vm_create(create_dummy_vm_config()); + assert!(vmm + .vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .net + .is_none()); + + let result = vmm.vm_add_net(net_config.clone()); + assert!(result.is_ok()); + assert!(result.unwrap().is_none()); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .net + .clone() + .unwrap() + .len(), + 1 + ); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .net + .clone() + .unwrap()[0], + net_config + ); + } + + #[test] + fn test_vmm_vm_cold_add_vdpa() { + let mut vmm = create_dummy_vmm(); + let vdpa_config = VdpaConfig::parse("path=/dev/vhost-vdpa,num_queues=2").unwrap(); + + assert!(matches!( + vmm.vm_add_vdpa(vdpa_config.clone()), + Err(VmError::VmNotCreated) + )); + + let _ = vmm.vm_create(create_dummy_vm_config()); + assert!(vmm + .vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .vdpa + .is_none()); + + let result = vmm.vm_add_vdpa(vdpa_config.clone()); + assert!(result.is_ok()); + assert!(result.unwrap().is_none()); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .vdpa + .clone() + .unwrap() + .len(), + 1 + ); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .vdpa + .clone() + .unwrap()[0], + vdpa_config + ); + } + + #[test] + fn test_vmm_vm_cold_add_vsock() { + let mut vmm = create_dummy_vmm(); + let vsock_config = VsockConfig::parse("socket=/tmp/sock,cid=1,iommu=on").unwrap(); + + assert!(matches!( + vmm.vm_add_vsock(vsock_config.clone()), + Err(VmError::VmNotCreated) + )); + + let _ = vmm.vm_create(create_dummy_vm_config()); + assert!(vmm + .vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .vsock + .is_none()); + + let result = vmm.vm_add_vsock(vsock_config.clone()); + assert!(result.is_ok()); + assert!(result.unwrap().is_none()); + assert_eq!( + vmm.vm_config + .as_ref() + .unwrap() + .lock() + .unwrap() + .vsock + .clone() + .unwrap(), + vsock_config + ); + } +} diff --git a/vendor/cube-hypervisor/vmm/src/memory_manager.rs b/vendor/cube-hypervisor/vmm/src/memory_manager.rs new file mode 100644 index 0000000..57dd8c2 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/memory_manager.rs @@ -0,0 +1,3205 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// +#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] +use crate::coredump::{ + CoredumpMemoryRegion, CoredumpMemoryRegions, DumpState, GuestDebuggableError, +}; +use crate::migration::{memory_blob_to_path, url_to_path}; +use crate::pagemap_anon::{filter_memory_ranges_by_pagemap_anon, host_page_size}; +use crate::soft_dirty::{ + clear_soft_dirty, filter_memory_ranges_by_anon_and_soft_dirty, probe_soft_dirty_support, +}; +#[cfg(target_arch = "x86_64")] +use crate::vm_config::SgxEpcConfig; +use crate::vm_config::{HotplugMethod, MemoryConfig, MemoryZoneConfig}; +use crate::MEMORY_MANAGER_SNAPSHOT_ID; +use crate::{GuestMemoryMmap, GuestRegionMmap}; +use acpi_tables::{aml, aml::Aml}; +use anyhow::anyhow; +#[cfg(target_arch = "x86_64")] +use arch::x86_64::{SgxEpcRegion, SgxEpcSection}; +use arch::{layout, RegionType}; +#[cfg(target_arch = "x86_64")] +use devices::ioapic; +#[cfg(target_arch = "aarch64")] +use hypervisor::HypervisorVmError; +#[cfg(target_arch = "x86_64")] +use libc::{MAP_NORESERVE, MAP_POPULATE, MAP_SHARED, PROT_READ, PROT_WRITE}; +use serde::{Deserialize, Serialize}; +#[cfg(feature = "guest_debug")] +use std::collections::BTreeMap; +use std::collections::HashMap; +use std::convert::TryInto; +use std::ffi; +use std::fs::{File, OpenOptions}; +use std::io::{self, Seek, SeekFrom}; +use std::ops::Deref; +use std::os::fd::AsFd; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::path::{Path, PathBuf}; +use std::result; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Barrier, Mutex}; +use tracer::trace_scoped; +use virtio_devices::BlocksState; +#[cfg(target_arch = "x86_64")] +use vm_allocator::GsiApic; +use vm_allocator::{AddressAllocator, SystemAllocator}; +use vm_device::BusDevice; +use vm_memory::bitmap::AtomicBitmap; +use vm_memory::guest_memory::FileOffset; +use vm_memory::{ + mmap::MmapRegionError, Address, Error as MmapError, GuestAddress, GuestAddressSpace, + GuestMemory, GuestMemoryAtomic, GuestMemoryError, GuestMemoryRegion, GuestUsize, MmapRegion, + ReadVolatile, +}; +use vm_migration::{ + protocol::MemoryRange, protocol::MemoryRangeTable, Migratable, MigratableError, Pausable, + Snapshot, SnapshotConfig, SnapshotDataSection, SnapshotType, Snapshottable, Transportable, +}; + +pub const MEMORY_MANAGER_ACPI_SIZE: usize = 0x18; + +const DEFAULT_MEMORY_ZONE: &str = "mem0"; + +const SNAPSHOT_FILENAME: &str = "memory-ranges"; + +struct MemorySnapshotFile { + path: PathBuf, + external: bool, +} + +impl MemorySnapshotFile { + fn from_snapshot_url( + snapshot_url: &str, + memory_vol_url: Option<&str>, + ) -> result::Result { + if let Some(memory_vol_url) = memory_vol_url { + Ok(Self { + path: memory_blob_to_path(memory_vol_url)?, + external: true, + }) + } else { + let mut path = url_to_path(snapshot_url)?; + path.push(SNAPSHOT_FILENAME); + Ok(Self { + path, + external: false, + }) + } + } + + fn open_read(&self) -> io::Result { + OpenOptions::new().read(true).open(&self.path) + } + + fn path(&self) -> &Path { + &self.path + } + + fn is_external(&self) -> bool { + self.external + } + + fn open_read_write(&self) -> result::Result { + OpenOptions::new() + .read(true) + .write(true) + .open(&self.path) + .map_err(|e| MigratableError::MigrateSend(e.into())) + } + + fn open_for_fresh_write(&self) -> result::Result { + if self.external { + return self.open_read_write(); + } + + OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .truncate(true) + .open(&self.path) + .map_err(|e| MigratableError::MigrateSend(e.into())) + } +} + +#[cfg(test)] +mod memory_snapshot_file_tests { + use super::{MemorySnapshotFile, SNAPSHOT_FILENAME}; + use std::fs; + use std::path::PathBuf; + use std::time::{SystemTime, UNIX_EPOCH}; + + fn unique_temp_path(name: &str) -> PathBuf { + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + std::env::temp_dir().join(format!("ch-memory-snapshot-{}-{}", name, nanos)) + } + + #[test] + fn default_snapshot_target_uses_memory_ranges_file() { + let dir_path = unique_temp_path("dir"); + fs::create_dir_all(&dir_path).unwrap(); + let snapshot_url = format!("file://{}", dir_path.display()); + + let target = MemorySnapshotFile::from_snapshot_url(&snapshot_url, None).unwrap(); + assert!(!target.external); + assert_eq!(target.path, dir_path.join(SNAPSHOT_FILENAME)); + target.open_for_fresh_write().unwrap(); + assert!(target.path.exists()); + + fs::remove_dir_all(dir_path).unwrap(); + } + + #[test] + fn external_snapshot_target_keeps_existing_file_intact() { + let file_path = unique_temp_path("external"); + fs::write(&file_path, b"existing-memory").unwrap(); + let original_len = fs::metadata(&file_path).unwrap().len(); + + let target = MemorySnapshotFile::from_snapshot_url( + "file:///unused", + Some(file_path.to_str().unwrap()), + ) + .unwrap(); + assert!(target.external); + target.open_for_fresh_write().unwrap(); + + assert_eq!(fs::metadata(&file_path).unwrap().len(), original_len); + fs::remove_file(file_path).unwrap(); + } +} + +#[cfg(target_arch = "x86_64")] +const X86_64_IRQ_BASE: u32 = 5; + +#[cfg(target_arch = "x86_64")] +const SGX_PAGE_SIZE: u64 = 1 << 12; + +const HOTPLUG_COUNT: usize = 8; + +// Memory policy constants +const MPOL_BIND: u32 = 2; +const MPOL_MF_STRICT: u32 = 1; +const MPOL_MF_MOVE: u32 = 1 << 1; + +// Reserve 1 MiB for platform MMIO devices (e.g. ACPI control devices) +const PLATFORM_DEVICE_AREA_SIZE: u64 = 1 << 20; + +// KVM's dirty log and vm-memory's AtomicBitmap both size their bitmaps with one +// bit per host page (via sysconf(_SC_PAGESIZE)), so interpreting those bitmaps +// must use the same granularity: 4 KiB on x86_64, 64 KiB on some ARM64 hosts. +// This reuses `pagemap_anon::host_page_size()` (cached, probed once) so the +// whole crate shares a single source of truth for the host page size. + +#[derive(Clone, Default, Serialize, Deserialize)] +struct HotPlugState { + base: u64, + length: u64, + active: bool, + inserting: bool, + removing: bool, +} + +pub struct VirtioMemZone { + region: Arc, + virtio_device: Option>>, + hotplugged_size: u64, + hugepages: bool, + blocks_state: Arc>, +} + +impl VirtioMemZone { + pub fn region(&self) -> &Arc { + &self.region + } + pub fn set_virtio_device(&mut self, virtio_device: Arc>) { + self.virtio_device = Some(virtio_device); + } + pub fn hotplugged_size(&self) -> u64 { + self.hotplugged_size + } + pub fn hugepages(&self) -> bool { + self.hugepages + } + pub fn blocks_state(&self) -> &Arc> { + &self.blocks_state + } + pub fn plugged_ranges(&self) -> MemoryRangeTable { + self.blocks_state + .lock() + .unwrap() + .memory_ranges(self.region.start_addr().raw_value(), true) + } +} + +#[derive(Default)] +pub struct MemoryZone { + regions: Vec>, + virtio_mem_zone: Option, +} + +impl MemoryZone { + pub fn regions(&self) -> &Vec> { + &self.regions + } + pub fn virtio_mem_zone(&self) -> &Option { + &self.virtio_mem_zone + } + pub fn virtio_mem_zone_mut(&mut self) -> Option<&mut VirtioMemZone> { + self.virtio_mem_zone.as_mut() + } +} + +pub type MemoryZones = HashMap; + +#[derive(Clone, Serialize, Deserialize)] +struct GuestRamMapping { + slot: u32, + gpa: u64, + size: u64, + zone_id: String, + virtio_mem: bool, + file_offset: u64, +} + +#[derive(Clone, Serialize, Deserialize)] +struct ArchMemRegion { + base: u64, + size: usize, + r_type: RegionType, +} + +pub struct MemoryManager { + boot_guest_memory: GuestMemoryMmap, + guest_memory: GuestMemoryAtomic, + // device_memory is a superset of guest_memory: it contains all RAM regions + // plus device-backed regions (e.g. virtio-pmem, virtio-fs DAX window, + // ivshmem). Use this view for any virtio backend that may need to + // dereference a GPA pointing into device-mapped host memory; use + // guest_memory for snapshot, migration, coredump and firmware-loading + // paths. + device_memory: GuestMemoryAtomic, + next_memory_slot: u32, + start_of_device_area: GuestAddress, + end_of_device_area: GuestAddress, + end_of_ram_area: GuestAddress, + pub vm: Arc, + hotplug_slots: Vec, + selected_slot: usize, + mergeable: bool, + allocator: Arc>, + hotplug_method: HotplugMethod, + boot_ram: u64, + current_ram: u64, + next_hotplug_slot: usize, + shared: bool, + hugepages: bool, + hugepage_size: Option, + prefault: bool, + thp: bool, + #[cfg(target_arch = "x86_64")] + sgx_epc_region: Option, + user_provided_zones: bool, + snapshot_memory_ranges: MemoryRangeTable, + /// Whether the soft-dirty tracker is currently armed (i.e. the previous + /// snapshot cycle ended with a successful `clear_soft_dirty()`, so the + /// pagemap bit-55 set this cycle reflects only writes that happened + /// since then). + soft_dirty_armed: AtomicBool, + memory_zones: MemoryZones, + log_dirty: bool, // Enable dirty logging for created RAM regions + arch_mem_regions: Vec, + ram_allocator: AddressAllocator, + dynamic: bool, + dirty_log: bool, + + // Keep track of calls to create_userspace_mapping() for guest RAM. + // This is useful for getting the dirty pages as we need to know the + // slots that the mapping is created in. + guest_ram_mappings: Vec, + + pub acpi_address: Option, + #[cfg(target_arch = "aarch64")] + uefi_flash: Option>, +} + +#[derive(Debug)] +pub enum Error { + /// Failed to create shared file. + SharedFileCreate(io::Error), + + /// Failed to set shared file length. + SharedFileSetLen(io::Error), + + /// Mmap backed guest memory error + GuestMemory(MmapError), + + /// Failed to allocate a memory range. + MemoryRangeAllocation, + + /// Error from region creation + GuestMemoryRegion(MmapRegionError), + + /// No ACPI slot available + NoSlotAvailable, + + /// Not enough space in the hotplug RAM region + InsufficientHotplugRam, + + /// The requested hotplug memory addition is not a valid size + InvalidSize, + + /// Failed to create the user memory region. + CreateUserMemoryRegion(hypervisor::HypervisorVmError), + + /// Failed to remove the user memory region. + RemoveUserMemoryRegion(hypervisor::HypervisorVmError), + + /// Failed to EventFd. + EventFdFail(io::Error), + + /// Eventfd write error + EventfdError(io::Error), + + /// Failed to virtio-mem resize + VirtioMemResizeFail(virtio_devices::mem::Error), + + /// Cannot enable dirty log. + Dirtylog(MigratableError), + + /// Cannot restore VM + Restore(MigratableError), + + /// Cannot restore VM because source URL is missing + RestoreMissingSourceUrl, + + /// Cannot create the system allocator + CreateSystemAllocator, + + /// Invalid SGX EPC section size + #[cfg(target_arch = "x86_64")] + EpcSectionSizeInvalid, + + /// Failed allocating SGX EPC region + #[cfg(target_arch = "x86_64")] + SgxEpcRangeAllocation, + + /// Failed opening SGX virtual EPC device + #[cfg(target_arch = "x86_64")] + SgxVirtEpcOpen(io::Error), + + /// Failed setting the SGX virtual EPC section size + #[cfg(target_arch = "x86_64")] + SgxVirtEpcFileSetLen(io::Error), + + /// Failed opening SGX provisioning device + #[cfg(target_arch = "x86_64")] + SgxProvisionOpen(io::Error), + + /// Failed enabling SGX provisioning + #[cfg(target_arch = "x86_64")] + SgxEnableProvisioning(hypervisor::HypervisorVmError), + + /// Failed creating a new MmapRegion instance. + #[cfg(target_arch = "x86_64")] + NewMmapRegion(vm_memory::mmap::MmapRegionError), + + /// No memory zones found. + MissingMemoryZones, + + /// Memory configuration is not valid. + InvalidMemoryParameters, + + /// Forbidden operation. Impossible to resize guest memory if it is + /// backed by user defined memory regions. + InvalidResizeWithMemoryZones, + + /// It's invalid to try applying a NUMA policy to a memory zone that is + /// memory mapped with MAP_SHARED. + InvalidSharedMemoryZoneWithHostNuma, + + /// Failed applying NUMA memory policy. + ApplyNumaPolicy(io::Error), + + /// Memory zone identifier is not unique. + DuplicateZoneId, + + /// No virtio-mem resizing handler found. + MissingVirtioMemHandler, + + /// Unknown memory zone. + UnknownMemoryZone, + + /// Invalid size for resizing. Can be anything except 0. + InvalidHotplugSize, + + /// Invalid hotplug method associated with memory zones resizing capability. + InvalidHotplugMethodWithMemoryZones, + + /// Could not find specified memory zone identifier from hash map. + MissingZoneIdentifier, + + /// Resizing the memory zone failed. + ResizeZone, + + /// Guest address overflow + GuestAddressOverFlow, + + /// Error opening snapshot file + SnapshotOpen(io::Error), + + // Error copying snapshot into region + SnapshotCopy(GuestMemoryError), + + // Error copying snapshot into region + SnapshotDup(io::Error), + + /// Failed to allocate MMIO address + AllocateMmioAddress, + + #[cfg(target_arch = "aarch64")] + /// Failed to create UEFI flash + CreateUefiFlash(HypervisorVmError), +} + +const ENABLE_FLAG: usize = 0; +const INSERTING_FLAG: usize = 1; +const REMOVING_FLAG: usize = 2; +const EJECT_FLAG: usize = 3; + +const BASE_OFFSET_LOW: u64 = 0; +const BASE_OFFSET_HIGH: u64 = 0x4; +const LENGTH_OFFSET_LOW: u64 = 0x8; +const LENGTH_OFFSET_HIGH: u64 = 0xC; +const STATUS_OFFSET: u64 = 0x14; +const SELECTION_OFFSET: u64 = 0; + +// The MMIO address space size is subtracted with 64k. This is done for the +// following reasons: +// - Reduce the addressable space size by at least 4k to workaround a Linux +// bug when the VMM allocates devices at the end of the addressable space +// - Windows requires the addressable space size to be 64k aligned +fn mmio_address_space_size(phys_bits: u8) -> u64 { + (1 << phys_bits) - (1 << 16) +} + +impl BusDevice for MemoryManager { + fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) { + if self.selected_slot < self.hotplug_slots.len() { + let state = &self.hotplug_slots[self.selected_slot]; + match offset { + BASE_OFFSET_LOW => { + data.copy_from_slice(&state.base.to_le_bytes()[..4]); + } + BASE_OFFSET_HIGH => { + data.copy_from_slice(&state.base.to_le_bytes()[4..]); + } + LENGTH_OFFSET_LOW => { + data.copy_from_slice(&state.length.to_le_bytes()[..4]); + } + LENGTH_OFFSET_HIGH => { + data.copy_from_slice(&state.length.to_le_bytes()[4..]); + } + STATUS_OFFSET => { + // The Linux kernel, quite reasonably, doesn't zero the memory it gives us. + data.fill(0); + if state.active { + data[0] |= 1 << ENABLE_FLAG; + } + if state.inserting { + data[0] |= 1 << INSERTING_FLAG; + } + if state.removing { + data[0] |= 1 << REMOVING_FLAG; + } + } + _ => { + warn!( + "Unexpected offset for accessing memory manager device: {:#}", + offset + ); + } + } + } else { + warn!("Out of range memory slot: {}", self.selected_slot); + } + } + + fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option> { + match offset { + SELECTION_OFFSET => { + self.selected_slot = usize::from(data[0]); + } + STATUS_OFFSET => { + if self.selected_slot < self.hotplug_slots.len() { + let state = &mut self.hotplug_slots[self.selected_slot]; + // The ACPI code writes back a 1 to acknowledge the insertion + if (data[0] & (1 << INSERTING_FLAG) == 1 << INSERTING_FLAG) && state.inserting { + state.inserting = false; + } + // Ditto for removal + if (data[0] & (1 << REMOVING_FLAG) == 1 << REMOVING_FLAG) && state.removing { + state.removing = false; + } + // Trigger removal of "DIMM" + if data[0] & (1 << EJECT_FLAG) == 1 << EJECT_FLAG { + warn!("Ejection of memory not currently supported"); + } + } else { + warn!("Out of range memory slot: {}", self.selected_slot); + } + } + _ => { + warn!( + "Unexpected offset for accessing memory manager device: {:#}", + offset + ); + } + }; + None + } +} + +impl MemoryManager { + /// Creates all memory regions based on the available RAM ranges defined + /// by `ram_regions`, and based on the description of the memory zones. + /// In practice, this function can perform multiple memory mappings of the + /// same backing file if there's a hole in the address space between two + /// RAM ranges. + /// One example might be ram_regions containing 2 regions (0-3G and 4G-6G) + /// and zones containing two zones (size 1G and size 4G). + /// This function will create 3 resulting memory regions: + /// - First one mapping entirely the first memory zone on 0-1G range + /// - Second one mapping partially the second memory zone on 1G-3G range + /// - Third one mapping partially the second memory zone on 4G-6G range + fn create_memory_regions_from_zones( + ram_regions: &[(GuestAddress, usize)], + zones: &[MemoryZoneConfig], + prefault: Option, + thp: bool, + ) -> Result<(Vec>, MemoryZones), Error> { + let mut zones = zones.to_owned(); + let mut mem_regions = Vec::new(); + let mut zone = zones.remove(0); + let mut zone_offset = 0; + let mut memory_zones = HashMap::new(); + + // Add zone id to the list of memory zones. + memory_zones.insert(zone.id.clone(), MemoryZone::default()); + + for ram_region in ram_regions.iter() { + let mut ram_region_offset = 0; + let mut exit = false; + + loop { + let mut ram_region_consumed = false; + let mut pull_next_zone = false; + + let ram_region_sub_size = ram_region.1 - ram_region_offset; + let zone_sub_size = zone.size as usize - zone_offset; + + let file_offset = zone_offset as u64; + let region_start = ram_region + .0 + .checked_add(ram_region_offset as u64) + .ok_or(Error::GuestAddressOverFlow)?; + let region_size = if zone_sub_size <= ram_region_sub_size { + if zone_sub_size == ram_region_sub_size { + ram_region_consumed = true; + } + + ram_region_offset += zone_sub_size; + pull_next_zone = true; + + zone_sub_size + } else { + zone_offset += ram_region_sub_size; + ram_region_consumed = true; + + ram_region_sub_size + }; + + let region = MemoryManager::create_ram_region( + &zone.file, + file_offset, + region_start, + region_size, + match prefault { + Some(pf) => pf, + None => zone.prefault, + }, + zone.shared, + zone.hugepages, + zone.hugepage_size, + zone.host_numa_node, + None, + None, + 0, + thp, + )?; + + // Add region to the list of regions associated with the + // current memory zone. + if let Some(memory_zone) = memory_zones.get_mut(&zone.id) { + memory_zone.regions.push(region.clone()); + } + + mem_regions.push(region); + + if pull_next_zone { + // Get the next zone and reset the offset. + zone_offset = 0; + if zones.is_empty() { + exit = true; + break; + } + zone = zones.remove(0); + + // Check if zone id already exist. In case it does, throw + // an error as we need unique identifiers. Otherwise, add + // the new zone id to the list of memory zones. + if memory_zones.contains_key(&zone.id) { + error!( + "Memory zone identifier '{}' found more than once. \ + It must be unique", + zone.id, + ); + return Err(Error::DuplicateZoneId); + } + memory_zones.insert(zone.id.clone(), MemoryZone::default()); + } + + if ram_region_consumed { + break; + } + } + + if exit { + break; + } + } + + Ok((mem_regions, memory_zones)) + } + + // Restore both GuestMemory regions along with MemoryZone zones. + fn restore_memory_regions_and_zones( + guest_ram_mappings: &[GuestRamMapping], + saved_regions: &MemoryRangeTable, + zones_config: &[MemoryZoneConfig], + prefault: Option, + mut existing_memory_files: HashMap, + saved_file: Option, + thp: bool, + ) -> Result<(Vec>, MemoryZones), Error> { + let mut memory_regions = Vec::new(); + let mut memory_zones = HashMap::new(); + + for zone_config in zones_config { + memory_zones.insert(zone_config.id.clone(), MemoryZone::default()); + } + + for guest_ram_mapping in guest_ram_mappings { + for zone_config in zones_config { + if guest_ram_mapping.zone_id == zone_config.id { + let mut file = None; + let mut offset = 0; + if let Some(ref f) = saved_file { + for range in saved_regions.regions() { + if guest_ram_mapping.gpa == range.gpa + && guest_ram_mapping.size == range.length + { + file = Some(f.try_clone().map_err(Error::SnapshotDup)?); + break; + } + offset += range.length; + } + } + let region = MemoryManager::create_ram_region( + &zone_config.file, + guest_ram_mapping.file_offset, + GuestAddress(guest_ram_mapping.gpa), + guest_ram_mapping.size as usize, + match prefault { + Some(pf) => pf, + None => zone_config.prefault, + }, + zone_config.shared, + zone_config.hugepages, + zone_config.hugepage_size, + zone_config.host_numa_node, + existing_memory_files.remove(&guest_ram_mapping.slot), + file, + offset, + thp, + )?; + memory_regions.push(Arc::clone(®ion)); + if let Some(memory_zone) = memory_zones.get_mut(&guest_ram_mapping.zone_id) { + if guest_ram_mapping.virtio_mem { + let hotplugged_size = zone_config.hotplugged_size.unwrap_or(0); + let region_size = region.len(); + memory_zone.virtio_mem_zone = Some(VirtioMemZone { + region, + virtio_device: None, + hotplugged_size, + hugepages: zone_config.hugepages, + blocks_state: Arc::new(Mutex::new(BlocksState::new(region_size))), + }); + } else { + memory_zone.regions.push(region); + } + } + } + } + } + + memory_regions.sort_by_key(|x| x.start_addr()); + + Ok((memory_regions, memory_zones)) + } + + fn fill_saved_regions( + &mut self, + file_path: PathBuf, + saved_regions: MemoryRangeTable, + ) -> Result<(), Error> { + if saved_regions.is_empty() { + return Ok(()); + } + + // Open (read only) the snapshot file. + let mut memory_file = OpenOptions::new() + .read(true) + .open(file_path) + .map_err(Error::SnapshotOpen)?; + + let guest_memory = self.guest_memory.memory(); + for range in saved_regions.regions() { + let mut offset: u64 = 0; + // Here we are manually handling the retry in case we can't write + // the whole region at once because we can't use the implementation + // from vm-memory::GuestMemory of read_exact_from() as it is not + // following the correct behavior. For more info about this issue + // see: https://github.com/rust-vmm/vm-memory/issues/174 + loop { + let bytes_read = guest_memory + .read_volatile_from( + GuestAddress(range.gpa + offset), + &mut memory_file, + (range.length - offset) as usize, + ) + .map_err(Error::SnapshotCopy)?; + offset += bytes_read as u64; + + if offset == range.length { + break; + } + } + } + + Ok(()) + } + + fn validate_memory_config( + config: &MemoryConfig, + user_provided_zones: bool, + ) -> Result<(u64, Vec, bool), Error> { + let mut allow_mem_hotplug = false; + + if !user_provided_zones { + if config.zones.is_some() { + error!( + "User defined memory regions can't be provided if the \ + memory size is not 0" + ); + return Err(Error::InvalidMemoryParameters); + } + + if config.hotplug_size.is_some() { + allow_mem_hotplug = true; + } + + if let Some(hotplugged_size) = config.hotplugged_size { + if let Some(hotplug_size) = config.hotplug_size { + if hotplugged_size > hotplug_size { + error!( + "'hotplugged_size' {} can't be bigger than \ + 'hotplug_size' {}", + hotplugged_size, hotplug_size, + ); + return Err(Error::InvalidMemoryParameters); + } + } else { + error!( + "Invalid to define 'hotplugged_size' when there is\ + no 'hotplug_size'" + ); + return Err(Error::InvalidMemoryParameters); + } + if config.hotplug_method == HotplugMethod::Acpi { + error!( + "Invalid to define 'hotplugged_size' with hotplug \ + method 'acpi'" + ); + return Err(Error::InvalidMemoryParameters); + } + } + + // Create a single zone from the global memory config. This lets + // us reuse the codepath for user defined memory zones. + let zones = vec![MemoryZoneConfig { + id: String::from(DEFAULT_MEMORY_ZONE), + size: config.size, + file: None, + shared: config.shared, + hugepages: config.hugepages, + hugepage_size: config.hugepage_size, + host_numa_node: None, + hotplug_size: config.hotplug_size, + hotplugged_size: config.hotplugged_size, + prefault: config.prefault, + }]; + + Ok((config.size, zones, allow_mem_hotplug)) + } else { + if config.zones.is_none() { + error!( + "User defined memory regions must be provided if the \ + memory size is 0" + ); + return Err(Error::MissingMemoryZones); + } + + // Safe to unwrap as we checked right above there were some + // regions. + let zones = config.zones.clone().unwrap(); + if zones.is_empty() { + return Err(Error::MissingMemoryZones); + } + + let mut total_ram_size: u64 = 0; + for zone in zones.iter() { + total_ram_size += zone.size; + + if zone.shared && zone.file.is_some() && zone.host_numa_node.is_some() { + error!( + "Invalid to set host NUMA policy for a memory zone \ + backed by a regular file and mapped as 'shared'" + ); + return Err(Error::InvalidSharedMemoryZoneWithHostNuma); + } + + if zone.hotplug_size.is_some() && config.hotplug_method == HotplugMethod::Acpi { + error!("Invalid to set ACPI hotplug method for memory zones"); + return Err(Error::InvalidHotplugMethodWithMemoryZones); + } + + if let Some(hotplugged_size) = zone.hotplugged_size { + if let Some(hotplug_size) = zone.hotplug_size { + if hotplugged_size > hotplug_size { + error!( + "'hotplugged_size' {} can't be bigger than \ + 'hotplug_size' {}", + hotplugged_size, hotplug_size, + ); + return Err(Error::InvalidMemoryParameters); + } + } else { + error!( + "Invalid to define 'hotplugged_size' when there is\ + no 'hotplug_size' for a memory zone" + ); + return Err(Error::InvalidMemoryParameters); + } + if config.hotplug_method == HotplugMethod::Acpi { + error!( + "Invalid to define 'hotplugged_size' with hotplug \ + method 'acpi'" + ); + return Err(Error::InvalidMemoryParameters); + } + } + } + + Ok((total_ram_size, zones, allow_mem_hotplug)) + } + } + + fn allocate_address_space(&mut self) -> Result<(), Error> { + let mut list = Vec::new(); + + for (zone_id, memory_zone) in self.memory_zones.iter() { + let mut regions: Vec<(Arc>, bool)> = + memory_zone + .regions() + .iter() + .map(|r| (r.clone(), false)) + .collect(); + + if let Some(virtio_mem_zone) = memory_zone.virtio_mem_zone() { + regions.push((virtio_mem_zone.region().clone(), true)); + } + + list.push((zone_id.clone(), regions)); + } + + for (zone_id, regions) in list { + for (region, virtio_mem) in regions { + let slot = self.create_userspace_mapping( + region.start_addr().raw_value(), + region.len(), + region.as_ptr() as u64, + self.mergeable, + false, + self.log_dirty, + )?; + + let file_offset = if let Some(file_offset) = region.file_offset() { + file_offset.start() + } else { + 0 + }; + + self.guest_ram_mappings.push(GuestRamMapping { + gpa: region.start_addr().raw_value(), + size: region.len(), + slot, + zone_id: zone_id.clone(), + virtio_mem, + file_offset, + }); + self.ram_allocator + .allocate(Some(region.start_addr()), region.len(), None) + .ok_or(Error::MemoryRangeAllocation)?; + } + } + + // Allocate SubRegion and Reserved address ranges. + for region in self.arch_mem_regions.iter() { + if region.r_type == RegionType::Ram { + // Ignore the RAM type since ranges have already been allocated + // based on the GuestMemory regions. + continue; + } + self.ram_allocator + .allocate( + Some(GuestAddress(region.base)), + region.size as GuestUsize, + None, + ) + .ok_or(Error::MemoryRangeAllocation)?; + } + + Ok(()) + } + + #[cfg(target_arch = "aarch64")] + fn add_uefi_flash(&mut self) -> Result<(), Error> { + // On AArch64, the UEFI binary requires a flash device at address 0. + // 4 MiB memory is mapped to simulate the flash. + let uefi_mem_slot = self.allocate_memory_slot(); + let uefi_region = GuestRegionMmap::new( + MmapRegion::new(arch::layout::UEFI_SIZE as usize).unwrap(), + arch::layout::UEFI_START, + ) + .unwrap(); + let uefi_mem_region = self.vm.make_user_memory_region( + uefi_mem_slot, + uefi_region.start_addr().raw_value(), + uefi_region.len() as u64, + uefi_region.as_ptr() as u64, + false, + false, + ); + self.vm + .create_user_memory_region(uefi_mem_region) + .map_err(Error::CreateUefiFlash)?; + + let uefi_flash = + GuestMemoryAtomic::new(GuestMemoryMmap::from_regions(vec![uefi_region]).unwrap()); + + self.uefi_flash = Some(uefi_flash); + + Ok(()) + } + + #[allow(clippy::too_many_arguments)] + pub fn new( + vm: Arc, + config: &MemoryConfig, + prefault: Option, + phys_bits: u8, + #[cfg(feature = "tdx")] tdx_enabled: bool, + restore_data: Option<&MemoryManagerSnapshotData>, + existing_memory_files: Option>, + snap_file: Option, + #[cfg(target_arch = "x86_64")] sgx_epc_config: Option>, + ) -> Result>, Error> { + trace_scoped!("MemoryManager::new"); + + let user_provided_zones = config.size == 0; + + let mmio_address_space_size = mmio_address_space_size(phys_bits); + debug_assert_eq!( + (((mmio_address_space_size) >> 16) << 16), + mmio_address_space_size + ); + let start_of_platform_device_area = + GuestAddress(mmio_address_space_size - PLATFORM_DEVICE_AREA_SIZE); + let end_of_device_area = start_of_platform_device_area.unchecked_sub(1); + + let (ram_size, zones, allow_mem_hotplug) = + Self::validate_memory_config(config, user_provided_zones)?; + + let ( + start_of_device_area, + boot_ram, + current_ram, + arch_mem_regions, + memory_zones, + guest_memory, + boot_guest_memory, + hotplug_slots, + next_memory_slot, + selected_slot, + next_hotplug_slot, + ) = if let Some(data) = restore_data { + let (regions, memory_zones) = Self::restore_memory_regions_and_zones( + &data.guest_ram_mappings, + &data.memory_ranges, + &zones, + prefault, + existing_memory_files.unwrap_or_default(), + snap_file, + config.thp, + )?; + let guest_memory = + GuestMemoryMmap::from_arc_regions(regions).map_err(Error::GuestMemory)?; + let boot_guest_memory = guest_memory.clone(); + ( + GuestAddress(data.start_of_device_area), + data.boot_ram, + data.current_ram, + data.arch_mem_regions.clone(), + memory_zones, + guest_memory, + boot_guest_memory, + data.hotplug_slots.clone(), + data.next_memory_slot, + data.selected_slot, + data.next_hotplug_slot, + ) + } else { + // Init guest memory + let arch_mem_regions = arch::arch_memory_regions(ram_size); + + let ram_regions: Vec<(GuestAddress, usize)> = arch_mem_regions + .iter() + .filter(|r| r.2 == RegionType::Ram) + .map(|r| (r.0, r.1)) + .collect(); + + let arch_mem_regions: Vec = arch_mem_regions + .iter() + .map(|(a, b, c)| ArchMemRegion { + base: a.0, + size: *b, + r_type: *c, + }) + .collect(); + + let (mem_regions, mut memory_zones) = + Self::create_memory_regions_from_zones(&ram_regions, &zones, prefault, config.thp)?; + + let mut guest_memory = + GuestMemoryMmap::from_arc_regions(mem_regions).map_err(Error::GuestMemory)?; + + let boot_guest_memory = guest_memory.clone(); + + let mut start_of_device_area = + MemoryManager::start_addr(guest_memory.last_addr(), allow_mem_hotplug)?; + + // Update list of memory zones for resize. + for zone in zones.iter() { + if let Some(memory_zone) = memory_zones.get_mut(&zone.id) { + if let Some(hotplug_size) = zone.hotplug_size { + if hotplug_size == 0 { + error!("'hotplug_size' can't be 0"); + return Err(Error::InvalidHotplugSize); + } + + if !user_provided_zones && config.hotplug_method == HotplugMethod::Acpi { + start_of_device_area = start_of_device_area + .checked_add(hotplug_size) + .ok_or(Error::GuestAddressOverFlow)?; + } else { + // Alignment must be "natural" i.e. same as size of block + let start_addr = GuestAddress( + (start_of_device_area.0 + virtio_devices::VIRTIO_MEM_ALIGN_SIZE + - 1) + / virtio_devices::VIRTIO_MEM_ALIGN_SIZE + * virtio_devices::VIRTIO_MEM_ALIGN_SIZE, + ); + + // When `prefault` is set by vm_restore, memory manager + // will create ram region with `prefault` option in + // restore config rather than same option in zone + let region = MemoryManager::create_ram_region( + &None, + 0, + start_addr, + hotplug_size as usize, + match prefault { + Some(pf) => pf, + None => zone.prefault, + }, + zone.shared, + zone.hugepages, + zone.hugepage_size, + zone.host_numa_node, + None, + None, + 0, + config.thp, + )?; + + guest_memory = guest_memory + .insert_region(Arc::clone(®ion)) + .map_err(Error::GuestMemory)?; + + let hotplugged_size = zone.hotplugged_size.unwrap_or(0); + let region_size = region.len(); + memory_zone.virtio_mem_zone = Some(VirtioMemZone { + region, + virtio_device: None, + hotplugged_size, + hugepages: zone.hugepages, + blocks_state: Arc::new(Mutex::new(BlocksState::new(region_size))), + }); + + start_of_device_area = start_addr + .checked_add(hotplug_size) + .ok_or(Error::GuestAddressOverFlow)?; + } + } + } else { + return Err(Error::MissingZoneIdentifier); + } + } + + let mut hotplug_slots = Vec::with_capacity(HOTPLUG_COUNT); + hotplug_slots.resize_with(HOTPLUG_COUNT, HotPlugState::default); + + ( + start_of_device_area, + ram_size, + ram_size, + arch_mem_regions, + memory_zones, + guest_memory, + boot_guest_memory, + hotplug_slots, + 0, + 0, + 0, + ) + }; + + let guest_memory = GuestMemoryAtomic::new(guest_memory); + // device_memory starts as an independent container holding the same + // RAM regions as guest_memory. Region Arcs are shared (no extra mmap), + // but the two containers evolve independently afterwards: RAM changes + // are double-written via add_region(), while device-backed regions + // (PMEM/SHM/...) are inserted into device_memory only via + // add_device_region(). + let device_memory = GuestMemoryAtomic::new(guest_memory.memory().deref().clone()); + + // Both MMIO and PIO address spaces start at address 0. + let allocator = Arc::new(Mutex::new( + SystemAllocator::new( + #[cfg(target_arch = "x86_64")] + { + GuestAddress(0) + }, + #[cfg(target_arch = "x86_64")] + { + 1 << 16 + }, + start_of_platform_device_area, + PLATFORM_DEVICE_AREA_SIZE, + layout::MEM_32BIT_DEVICES_START, + layout::MEM_32BIT_DEVICES_SIZE, + #[cfg(target_arch = "x86_64")] + vec![GsiApic::new( + X86_64_IRQ_BASE, + ioapic::NUM_IOAPIC_PINS as u32 - X86_64_IRQ_BASE, + )], + ) + .ok_or(Error::CreateSystemAllocator)?, + )); + + #[cfg(not(feature = "tdx"))] + let dynamic = true; + #[cfg(feature = "tdx")] + let dynamic = !tdx_enabled; + + let acpi_address = if dynamic + && config.hotplug_method == HotplugMethod::Acpi + && (config.hotplug_size.unwrap_or_default() > 0) + { + Some( + allocator + .lock() + .unwrap() + .allocate_platform_mmio_addresses(None, MEMORY_MANAGER_ACPI_SIZE as u64, None) + .ok_or(Error::AllocateMmioAddress)?, + ) + } else { + None + }; + + // If running on SGX the start of device area and RAM area may diverge but + // at this point they are next to each other. + let end_of_ram_area = start_of_device_area.unchecked_sub(1); + let ram_allocator = AddressAllocator::new(GuestAddress(0), start_of_device_area.0).unwrap(); + + let mut memory_manager = MemoryManager { + boot_guest_memory, + guest_memory, + device_memory, + next_memory_slot, + start_of_device_area, + end_of_device_area, + end_of_ram_area, + vm, + hotplug_slots, + selected_slot, + mergeable: config.mergeable, + allocator, + hotplug_method: config.hotplug_method, + boot_ram, + current_ram, + next_hotplug_slot, + shared: config.shared, + hugepages: config.hugepages, + hugepage_size: config.hugepage_size, + prefault: config.prefault, + #[cfg(target_arch = "x86_64")] + sgx_epc_region: None, + user_provided_zones, + snapshot_memory_ranges: MemoryRangeTable::default(), + soft_dirty_armed: AtomicBool::new(false), + memory_zones, + guest_ram_mappings: Vec::new(), + acpi_address, + log_dirty: dynamic, // Cannot log dirty pages on a TD + arch_mem_regions, + ram_allocator, + dynamic, + dirty_log: config.dirty_log, + #[cfg(target_arch = "aarch64")] + uefi_flash: None, + thp: config.thp, + }; + + memory_manager.allocate_address_space()?; + + #[cfg(target_arch = "aarch64")] + memory_manager.add_uefi_flash()?; + + #[cfg(target_arch = "x86_64")] + if let Some(sgx_epc_config) = sgx_epc_config { + memory_manager.setup_sgx(sgx_epc_config)?; + } + + memory_manager.init_dirty_log().map_err(Error::Dirtylog)?; + + Ok(Arc::new(Mutex::new(memory_manager))) + } + + pub fn new_from_snapshot( + snapshot: &Snapshot, + vm: Arc, + config: &MemoryConfig, + source_url: Option<&str>, + prefault: bool, + phys_bits: u8, + memory_vol_url: Option<&str>, + ) -> Result>, Error> { + if let Some(source_url) = source_url { + let memory_file_target = + MemorySnapshotFile::from_snapshot_url(source_url, memory_vol_url) + .map_err(Error::Restore)?; + + let fast_restore = Self::support_fast_restore_check(config); + let memory_file = if fast_restore { + info!("restore non-shared map, speed up restore by share map memory file"); + Some( + memory_file_target + .open_read() + .map_err(Error::SnapshotOpen)?, + ) + } else { + None + }; + + let mem_snapshot: MemoryManagerSnapshotData = snapshot + .to_state(MEMORY_MANAGER_SNAPSHOT_ID) + .map_err(Error::Restore)?; + + let mm = MemoryManager::new( + vm, + config, + Some(prefault), + phys_bits, + #[cfg(feature = "tdx")] + false, + Some(&mem_snapshot), + None, + memory_file, + #[cfg(target_arch = "x86_64")] + None, + )?; + + if !fast_restore { + info!("restore shared map, fall back to slow restore"); + mm.lock() + .unwrap() + .fill_saved_regions(memory_file_target.path, mem_snapshot.memory_ranges)?; + } + + Ok(mm) + } else { + Err(Error::RestoreMissingSourceUrl) + } + } + + fn support_fast_restore_check(config: &MemoryConfig) -> bool { + !config.exist_shared() && !config.has_hotplug_virtio_mem() + } + + fn memfd_create(name: &ffi::CStr, flags: u32) -> Result { + let res = unsafe { libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags) }; + + if res < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(res as RawFd) + } + } + + fn mbind( + addr: *mut u8, + len: u64, + mode: u32, + nodemask: Vec, + maxnode: u64, + flags: u32, + ) -> Result<(), io::Error> { + let res = unsafe { + libc::syscall( + libc::SYS_mbind, + addr as *mut libc::c_void, + len, + mode, + nodemask.as_ptr(), + maxnode, + flags, + ) + }; + + if res < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(()) + } + } + + fn create_anonymous_file( + size: usize, + hugepages: bool, + hugepage_size: Option, + ) -> Result { + let fd = Self::memfd_create( + &ffi::CString::new("ch_ram").unwrap(), + libc::MFD_CLOEXEC + | if hugepages { + libc::MFD_HUGETLB + | if let Some(hugepage_size) = hugepage_size { + /* + * From the Linux kernel: + * Several system calls take a flag to request "hugetlb" huge pages. + * Without further specification, these system calls will use the + * system's default huge page size. If a system supports multiple + * huge page sizes, the desired huge page size can be specified in + * bits [26:31] of the flag arguments. The value in these 6 bits + * will encode the log2 of the huge page size. + */ + + hugepage_size.trailing_zeros() << 26 + } else { + // Use the system default huge page size + 0 + } + } else { + 0 + }, + ) + .map_err(Error::SharedFileCreate)?; + + let f = unsafe { File::from_raw_fd(fd) }; + f.set_len(size as u64).map_err(Error::SharedFileSetLen)?; + + Ok(FileOffset::new(f, 0)) + } + + fn open_backing_file( + backing_file: &PathBuf, + file_offset: u64, + size: usize, + ) -> Result { + if backing_file.is_dir() { + // Override file offset as it does not apply in this case. + info!( + "Ignoring file offset since the backing file is a \ + temporary file created from the specified directory." + ); + let fs_str = format!("{}{}", backing_file.display(), "/tmpfile_XXXXXX"); + let fs = ffi::CString::new(fs_str).unwrap(); + let mut path = fs.as_bytes_with_nul().to_owned(); + let path_ptr = path.as_mut_ptr() as *mut _; + let fd = unsafe { libc::mkstemp(path_ptr) }; + unsafe { libc::unlink(path_ptr) }; + let f = unsafe { File::from_raw_fd(fd) }; + f.set_len(size as u64).map_err(Error::SharedFileSetLen)?; + + Ok(FileOffset::new(f, 0)) + } else { + let f = OpenOptions::new() + .read(true) + .write(true) + .open(backing_file) + .map_err(Error::SharedFileCreate)?; + + Ok(FileOffset::new(f, file_offset)) + } + } + + #[allow(clippy::too_many_arguments)] + pub fn create_ram_region( + backing_file: &Option, + file_offset: u64, + start_addr: GuestAddress, + size: usize, + prefault: bool, + shared: bool, + hugepages: bool, + hugepage_size: Option, + host_numa_node: Option, + existing_memory_file: Option, + snap_file: Option, + snap_offset: u64, + thp: bool, + ) -> Result, Error> { + let mut mmap_flags = libc::MAP_NORESERVE; + + // The duplication of mmap_flags ORing here is unfortunate but it also makes + // the complexity of the handling clear. + let fo = if let Some(f) = snap_file { + mmap_flags |= libc::MAP_PRIVATE; + Some(FileOffset::new(f, snap_offset)) + } else if let Some(f) = existing_memory_file { + // It must be MAP_SHARED as we wouldn't already have an FD + mmap_flags |= libc::MAP_SHARED; + Some(FileOffset::new(f, file_offset)) + } else if let Some(backing_file) = backing_file { + if shared { + mmap_flags |= libc::MAP_SHARED; + } else { + mmap_flags |= libc::MAP_PRIVATE; + } + Some(Self::open_backing_file(backing_file, file_offset, size)?) + } else if shared || hugepages { + // For hugepages we must also MAP_SHARED otherwise we will trigger #4805 + // because the MAP_PRIVATE will trigger CoW against the backing file with + // the VFIO pinning + mmap_flags |= libc::MAP_SHARED; + Some(Self::create_anonymous_file(size, hugepages, hugepage_size)?) + } else { + mmap_flags |= libc::MAP_PRIVATE | libc::MAP_ANONYMOUS; + None + }; + + if prefault { + mmap_flags |= libc::MAP_POPULATE; + } + + let region = GuestRegionMmap::new( + MmapRegion::build(fo, size, libc::PROT_READ | libc::PROT_WRITE, mmap_flags) + .map_err(Error::GuestMemoryRegion)?, + start_addr, + ) + .map_err(Error::GuestMemory)?; + + if region.file_offset().is_none() && thp { + info!( + "Anonymous mapping at 0x{:x} (size = 0x{:x})", + region.as_ptr() as u64, + size + ); + let ret = unsafe { libc::madvise(region.as_ptr() as _, size, libc::MADV_HUGEPAGE) }; + if ret != 0 { + let e = io::Error::last_os_error(); + warn!("Failed to mark pages as THP eligible: {}", e); + } + } + + // Apply NUMA policy if needed. + if let Some(node) = host_numa_node { + let addr = region.deref().as_ptr(); + let len = region.deref().size() as u64; + let mode = MPOL_BIND; + let mut nodemask: Vec = Vec::new(); + let flags = MPOL_MF_STRICT | MPOL_MF_MOVE; + + // Linux is kind of buggy in the way it interprets maxnode as it + // will cut off the last node. That's why we have to add 1 to what + // we would consider as the proper maxnode value. + let maxnode = node as u64 + 1 + 1; + + // Allocate the right size for the vector. + nodemask.resize((node as usize / 64) + 1, 0); + + // Fill the global bitmask through the nodemask vector. + let idx = (node / 64) as usize; + let shift = node % 64; + nodemask[idx] |= 1u64 << shift; + + // Policies are enforced by using MPOL_MF_MOVE flag as it will + // force the kernel to move all pages that might have been already + // allocated to the proper set of NUMA nodes. MPOL_MF_STRICT is + // used to throw an error if MPOL_MF_MOVE didn't succeed. + // MPOL_BIND is the selected mode as it specifies a strict policy + // that restricts memory allocation to the nodes specified in the + // nodemask. + Self::mbind(addr, len, mode, nodemask, maxnode, flags) + .map_err(Error::ApplyNumaPolicy)?; + } + + Ok(Arc::new(region)) + } + + // Update the GuestMemoryMmap with the new range + // + // RAM changes must be double-written: first into device_memory so any + // virtio backend activation immediately sees the new region, then into + // guest_memory (the RAM-only view used by snapshot/coredump/migration). + // The two containers are independent (see constructor); region Arcs are + // shared, so this only clones a small Vec and bumps refcounts. + fn add_region(&mut self, region: Arc) -> Result<(), Error> { + // 1) Update device_memory first. + let new_device_memory = self + .device_memory + .memory() + .insert_region(Arc::clone(®ion)) + .map_err(Error::GuestMemory)?; + self.device_memory + .lock() + .unwrap() + .replace(new_device_memory); + + // 2) Update guest_memory (RAM view). + let new_guest_memory = self + .guest_memory + .memory() + .insert_region(region) + .map_err(Error::GuestMemory)?; + self.guest_memory.lock().unwrap().replace(new_guest_memory); + + Ok(()) + } + + /// Insert a device-backed region (e.g. virtio-pmem, virtio-fs DAX + /// window, ivshmem) into the device_memory view ONLY. The region will + /// NOT be visible through guest_memory(), so it does not affect + /// snapshot, migration, coredump, or firmware-loading paths. + pub fn add_device_region(&mut self, region: Arc) -> Result<(), Error> { + let new_device_memory = self + .device_memory + .memory() + .insert_region(region) + .map_err(Error::GuestMemory)?; + self.device_memory + .lock() + .unwrap() + .replace(new_device_memory); + Ok(()) + } + + /// Remove a previously inserted device-backed region from device_memory. + /// `start_addr`/`size` must match the region exactly (vm-memory requires + /// an exact-size match, otherwise it returns `InvalidGuestRegion`). + /// Use this in the unmap path of dynamically-mapped device segments + /// (e.g. ivshmem unmap_region) so device_memory does not retain a dead + /// reference to a region whose KVM slot has just been removed. + pub fn remove_device_region( + &mut self, + start_addr: GuestAddress, + size: GuestUsize, + ) -> Result<(), Error> { + let (new_device_memory, _removed) = self + .device_memory + .memory() + .remove_region(start_addr, size) + .map_err(Error::GuestMemory)?; + self.device_memory + .lock() + .unwrap() + .replace(new_device_memory); + Ok(()) + } + + // + // Calculate the start address of an area next to RAM. + // + // If memory hotplug is allowed, the start address needs to be aligned + // (rounded-up) to 128MiB boundary. + // If memory hotplug is not allowed, there is no alignment required. + // And it must also start at the 64bit start. + fn start_addr(mem_end: GuestAddress, allow_mem_hotplug: bool) -> Result { + let mut start_addr = if allow_mem_hotplug { + GuestAddress(mem_end.0 | ((128 << 20) - 1)) + } else { + mem_end + }; + + start_addr = start_addr + .checked_add(1) + .ok_or(Error::GuestAddressOverFlow)?; + + if mem_end < arch::layout::MEM_32BIT_RESERVED_START { + return Ok(arch::layout::RAM_64BIT_START); + } + + Ok(start_addr) + } + + pub fn add_ram_region( + &mut self, + start_addr: GuestAddress, + size: usize, + ) -> Result, Error> { + // Allocate memory for the region + let region = MemoryManager::create_ram_region( + &None, + 0, + start_addr, + size, + self.prefault, + self.shared, + self.hugepages, + self.hugepage_size, + None, + None, + None, + 0, + self.thp, + )?; + + // Map it into the guest + let slot = self.create_userspace_mapping( + region.start_addr().0, + region.len(), + region.as_ptr() as u64, + self.mergeable, + false, + self.log_dirty, + )?; + self.guest_ram_mappings.push(GuestRamMapping { + gpa: region.start_addr().raw_value(), + size: region.len(), + slot, + zone_id: DEFAULT_MEMORY_ZONE.to_string(), + virtio_mem: false, + file_offset: 0, + }); + + self.add_region(Arc::clone(®ion))?; + + Ok(region) + } + + fn hotplug_ram_region(&mut self, size: usize) -> Result, Error> { + info!("Hotplugging new RAM: {}", size); + + // Check that there is a free slot + if self.next_hotplug_slot >= HOTPLUG_COUNT { + return Err(Error::NoSlotAvailable); + } + + // "Inserted" DIMM must have a size that is a multiple of 128MiB + if size % (128 << 20) != 0 { + return Err(Error::InvalidSize); + } + + let start_addr = MemoryManager::start_addr(self.guest_memory.memory().last_addr(), true)?; + + if start_addr + .checked_add((size - 1).try_into().unwrap()) + .unwrap() + > self.end_of_ram_area + { + return Err(Error::InsufficientHotplugRam); + } + + let region = self.add_ram_region(start_addr, size)?; + + // Add region to the list of regions associated with the default + // memory zone. + if let Some(memory_zone) = self.memory_zones.get_mut(DEFAULT_MEMORY_ZONE) { + memory_zone.regions.push(Arc::clone(®ion)); + } + + // Tell the allocator + self.ram_allocator + .allocate(Some(start_addr), size as GuestUsize, None) + .ok_or(Error::MemoryRangeAllocation)?; + + // Update the slot so that it can be queried via the I/O port + let slot = &mut self.hotplug_slots[self.next_hotplug_slot]; + slot.active = true; + slot.inserting = true; + slot.base = region.start_addr().0; + slot.length = region.len(); + + self.next_hotplug_slot += 1; + + Ok(region) + } + + pub fn guest_memory(&self) -> GuestMemoryAtomic { + self.guest_memory.clone() + } + + /// Return the device-memory view: RAM + device-backed regions. + /// Use this for any virtio/DMA backend that may need to access GPAs + /// pointing into device-mapped host memory (e.g. PMEM). + pub fn device_memory(&self) -> GuestMemoryAtomic { + self.device_memory.clone() + } + + pub fn boot_guest_memory(&self) -> GuestMemoryMmap { + self.boot_guest_memory.clone() + } + + pub fn allocator(&self) -> Arc> { + self.allocator.clone() + } + + pub fn start_of_device_area(&self) -> GuestAddress { + self.start_of_device_area + } + + pub fn end_of_device_area(&self) -> GuestAddress { + self.end_of_device_area + } + + pub fn allocate_memory_slot(&mut self) -> u32 { + let slot_id = self.next_memory_slot; + self.next_memory_slot += 1; + slot_id + } + + pub fn create_userspace_mapping( + &mut self, + guest_phys_addr: u64, + memory_size: u64, + userspace_addr: u64, + mergeable: bool, + readonly: bool, + log_dirty: bool, + ) -> Result { + let slot = self.allocate_memory_slot(); + let mem_region = self.vm.make_user_memory_region( + slot, + guest_phys_addr, + memory_size, + userspace_addr, + readonly, + log_dirty, + ); + + info!( + "Creating userspace mapping: {:x} -> {:x} {:x}, slot {}", + guest_phys_addr, userspace_addr, memory_size, slot + ); + + self.vm + .create_user_memory_region(mem_region) + .map_err(Error::CreateUserMemoryRegion)?; + + // Mark the pages as mergeable if explicitly asked for. + if mergeable { + // Safe because the address and size are valid since the + // mmap succeeded. + let ret = unsafe { + libc::madvise( + userspace_addr as *mut libc::c_void, + memory_size as libc::size_t, + libc::MADV_MERGEABLE, + ) + }; + if ret != 0 { + let err = io::Error::last_os_error(); + // Safe to unwrap because the error is constructed with + // last_os_error(), which ensures the output will be Some(). + let errno = err.raw_os_error().unwrap(); + if errno == libc::EINVAL { + warn!("kernel not configured with CONFIG_KSM"); + } else { + warn!("madvise error: {}", err); + } + warn!("failed to mark pages as mergeable"); + } + } + + info!( + "Created userspace mapping: {:x} -> {:x} {:x}", + guest_phys_addr, userspace_addr, memory_size + ); + + Ok(slot) + } + + pub fn remove_userspace_mapping( + &mut self, + guest_phys_addr: u64, + memory_size: u64, + userspace_addr: u64, + mergeable: bool, + slot: u32, + ) -> Result<(), Error> { + let mem_region = self.vm.make_user_memory_region( + slot, + guest_phys_addr, + memory_size, + userspace_addr, + false, /* readonly -- don't care */ + false, /* log dirty */ + ); + + self.vm + .remove_user_memory_region(mem_region) + .map_err(Error::RemoveUserMemoryRegion)?; + + // Mark the pages as unmergeable if there were previously marked as + // mergeable. + if mergeable { + // Safe because the address and size are valid as the region was + // previously advised. + let ret = unsafe { + libc::madvise( + userspace_addr as *mut libc::c_void, + memory_size as libc::size_t, + libc::MADV_UNMERGEABLE, + ) + }; + if ret != 0 { + let err = io::Error::last_os_error(); + // Safe to unwrap because the error is constructed with + // last_os_error(), which ensures the output will be Some(). + let errno = err.raw_os_error().unwrap(); + if errno == libc::EINVAL { + warn!("kernel not configured with CONFIG_KSM"); + } else { + warn!("madvise error: {}", err); + } + warn!("failed to mark pages as unmergeable"); + } + } + + info!( + "Removed userspace mapping: {:x} -> {:x} {:x}", + guest_phys_addr, userspace_addr, memory_size + ); + + Ok(()) + } + + pub fn virtio_mem_resize(&mut self, id: &str, size: u64) -> Result<(), Error> { + if let Some(memory_zone) = self.memory_zones.get_mut(id) { + if let Some(virtio_mem_zone) = &mut memory_zone.virtio_mem_zone { + if let Some(virtio_mem_device) = virtio_mem_zone.virtio_device.as_ref() { + virtio_mem_device + .lock() + .unwrap() + .resize(size) + .map_err(Error::VirtioMemResizeFail)?; + } + + // Keep the hotplugged_size up to date. + virtio_mem_zone.hotplugged_size = size; + } else { + error!("Failed resizing virtio-mem region: No virtio-mem handler"); + return Err(Error::MissingVirtioMemHandler); + } + + return Ok(()); + } + + error!("Failed resizing virtio-mem region: Unknown memory zone"); + Err(Error::UnknownMemoryZone) + } + + /// In case this function resulted in adding a new memory region to the + /// guest memory, the new region is returned to the caller. The virtio-mem + /// use case never adds a new region as the whole hotpluggable memory has + /// already been allocated at boot time. + pub fn resize(&mut self, desired_ram: u64) -> Result>, Error> { + if self.user_provided_zones { + error!( + "Not allowed to resize guest memory when backed with user \ + defined memory zones." + ); + return Err(Error::InvalidResizeWithMemoryZones); + } + + let mut region: Option> = None; + match self.hotplug_method { + HotplugMethod::VirtioMem => { + if desired_ram >= self.boot_ram { + if !self.dynamic { + return Ok(region); + } + + self.virtio_mem_resize(DEFAULT_MEMORY_ZONE, desired_ram - self.boot_ram)?; + self.current_ram = desired_ram; + } + } + HotplugMethod::Acpi => { + if desired_ram > self.current_ram { + if !self.dynamic { + return Ok(region); + } + + region = + Some(self.hotplug_ram_region((desired_ram - self.current_ram) as usize)?); + self.current_ram = desired_ram; + } + } + } + Ok(region) + } + + pub fn resize_zone(&mut self, id: &str, virtio_mem_size: u64) -> Result<(), Error> { + if !self.user_provided_zones { + error!( + "Not allowed to resize guest memory zone when no zone is \ + defined." + ); + return Err(Error::ResizeZone); + } + + self.virtio_mem_resize(id, virtio_mem_size) + } + + #[cfg(target_arch = "x86_64")] + pub fn setup_sgx(&mut self, sgx_epc_config: Vec) -> Result<(), Error> { + let file = OpenOptions::new() + .read(true) + .open("/dev/sgx_provision") + .map_err(Error::SgxProvisionOpen)?; + self.vm + .enable_sgx_attribute(file) + .map_err(Error::SgxEnableProvisioning)?; + + // Go over each EPC section and verify its size is a 4k multiple. At + // the same time, calculate the total size needed for the contiguous + // EPC region. + let mut epc_region_size = 0; + for epc_section in sgx_epc_config.iter() { + if epc_section.size == 0 { + return Err(Error::EpcSectionSizeInvalid); + } + if epc_section.size & (SGX_PAGE_SIZE - 1) != 0 { + return Err(Error::EpcSectionSizeInvalid); + } + + epc_region_size += epc_section.size; + } + + // Place the SGX EPC region on a 4k boundary between the RAM and the device area + let epc_region_start = GuestAddress( + ((self.start_of_device_area.0 + SGX_PAGE_SIZE - 1) / SGX_PAGE_SIZE) * SGX_PAGE_SIZE, + ); + + self.start_of_device_area = epc_region_start + .checked_add(epc_region_size) + .ok_or(Error::GuestAddressOverFlow)?; + + let mut sgx_epc_region = SgxEpcRegion::new(epc_region_start, epc_region_size as GuestUsize); + info!( + "SGX EPC region: 0x{:x} (0x{:x})", + epc_region_start.0, epc_region_size + ); + + // Each section can be memory mapped into the allocated region. + let mut epc_section_start = epc_region_start.raw_value(); + for epc_section in sgx_epc_config.iter() { + let file = OpenOptions::new() + .read(true) + .write(true) + .open("/dev/sgx_vepc") + .map_err(Error::SgxVirtEpcOpen)?; + + let prot = PROT_READ | PROT_WRITE; + let mut flags = MAP_NORESERVE | MAP_SHARED; + if epc_section.prefault { + flags |= MAP_POPULATE; + } + + // We can't use the vm-memory crate to perform the memory mapping + // here as it would try to ensure the size of the backing file is + // matching the size of the expected mapping. The /dev/sgx_vepc + // device does not work that way, it provides a file descriptor + // which is not matching the mapping size, as it's a just a way to + // let KVM know that an EPC section is being created for the guest. + let host_addr = unsafe { + libc::mmap( + std::ptr::null_mut(), + epc_section.size as usize, + prot, + flags, + file.as_raw_fd(), + 0, + ) + } as u64; + + info!( + "Adding SGX EPC section: 0x{:x} (0x{:x})", + epc_section_start, epc_section.size + ); + + let _mem_slot = self.create_userspace_mapping( + epc_section_start, + epc_section.size, + host_addr, + false, + false, + false, + )?; + + sgx_epc_region.insert( + epc_section.id.clone(), + SgxEpcSection::new( + GuestAddress(epc_section_start), + epc_section.size as GuestUsize, + ), + ); + + epc_section_start += epc_section.size; + } + + self.sgx_epc_region = Some(sgx_epc_region); + + Ok(()) + } + + #[cfg(target_arch = "x86_64")] + pub fn sgx_epc_region(&self) -> &Option { + &self.sgx_epc_region + } + + pub fn is_hardlink(f: &File) -> bool { + let mut stat = std::mem::MaybeUninit::::uninit(); + let ret = unsafe { libc::fstat(f.as_raw_fd(), stat.as_mut_ptr()) }; + if ret != 0 { + error!("Couldn't fstat the backing file"); + return false; + } + + unsafe { (*stat.as_ptr()).st_nlink as usize > 0 } + } + + pub fn memory_zones(&self) -> &MemoryZones { + &self.memory_zones + } + + pub fn memory_zones_mut(&mut self) -> &mut MemoryZones { + &mut self.memory_zones + } + + pub fn memory_range_table( + &self, + snapshot: bool, + ) -> std::result::Result { + let mut table = MemoryRangeTable::default(); + + for memory_zone in self.memory_zones.values() { + if let Some(virtio_mem_zone) = memory_zone.virtio_mem_zone() { + table.extend(virtio_mem_zone.plugged_ranges()); + } + + for region in memory_zone.regions() { + if snapshot { + if let Some(file_offset) = region.file_offset() { + if (region.flags() & libc::MAP_SHARED == libc::MAP_SHARED) + && Self::is_hardlink(file_offset.file()) + { + // In this very specific case, we know the memory + // region is backed by a file on the host filesystem + // that can be accessed by the user, and additionally + // the mapping is shared, which means that modifications + // to the content are written to the actual file. + // When meeting these conditions, we can skip the + // copy of the memory content for this specific region, + // as we can assume the user will have it saved through + // the backing file already. + continue; + } + } + } + + table.push(MemoryRange { + gpa: region.start_addr().raw_value(), + length: region.len(), + }); + } + } + + Ok(table) + } + + pub fn snapshot_data(&self) -> MemoryManagerSnapshotData { + MemoryManagerSnapshotData { + memory_ranges: self.snapshot_memory_ranges.clone(), + guest_ram_mappings: self.guest_ram_mappings.clone(), + start_of_device_area: self.start_of_device_area.0, + boot_ram: self.boot_ram, + current_ram: self.current_ram, + arch_mem_regions: self.arch_mem_regions.clone(), + hotplug_slots: self.hotplug_slots.clone(), + next_memory_slot: self.next_memory_slot, + selected_slot: self.selected_slot, + next_hotplug_slot: self.next_hotplug_slot, + } + } + + pub fn memory_slot_fds(&self) -> HashMap { + let mut memory_slot_fds = HashMap::new(); + for guest_ram_mapping in &self.guest_ram_mappings { + let slot = guest_ram_mapping.slot; + let guest_memory = self.guest_memory.memory(); + let file = guest_memory + .find_region(GuestAddress(guest_ram_mapping.gpa)) + .unwrap() + .file_offset() + .unwrap() + .file(); + memory_slot_fds.insert(slot, file.as_raw_fd()); + } + memory_slot_fds + } + + pub fn acpi_address(&self) -> Option { + self.acpi_address + } + + pub fn num_guest_ram_mappings(&self) -> u32 { + self.guest_ram_mappings.len() as u32 + } + + #[cfg(target_arch = "aarch64")] + pub fn uefi_flash(&self) -> GuestMemoryAtomic { + self.uefi_flash.as_ref().unwrap().clone() + } + + #[cfg(feature = "guest_debug")] + pub fn coredump_memory_regions(&self, mem_offset: u64) -> CoredumpMemoryRegions { + let mut mapping_sorted_by_gpa = self.guest_ram_mappings.clone(); + mapping_sorted_by_gpa.sort_by_key(|m| m.gpa); + + let mut mem_offset_in_elf = mem_offset; + let mut ram_maps = BTreeMap::new(); + for mapping in mapping_sorted_by_gpa.iter() { + ram_maps.insert( + mapping.gpa, + CoredumpMemoryRegion { + mem_offset_in_elf, + mem_size: mapping.size, + }, + ); + mem_offset_in_elf += mapping.size; + } + + CoredumpMemoryRegions { ram_maps } + } + + #[cfg(feature = "guest_debug")] + pub fn coredump_iterate_save_mem( + &mut self, + dump_state: &DumpState, + ) -> std::result::Result<(), GuestDebuggableError> { + let snapshot_memory_ranges = self + .memory_range_table(false) + .map_err(|e| GuestDebuggableError::Coredump(e.into()))?; + + if snapshot_memory_ranges.is_empty() { + return Ok(()); + } + + let coredump_file = dump_state.file.as_ref().unwrap(); + + let guest_memory = self.guest_memory.memory(); + let mut total_bytes: u64 = 0; + + for range in snapshot_memory_ranges.regions() { + let mut offset: u64 = 0; + loop { + let bytes_written = guest_memory + .write_volatile_to( + GuestAddress(range.gpa + offset), + &mut coredump_file.as_fd(), + (range.length - offset) as usize, + ) + .map_err(|e| GuestDebuggableError::Coredump(e.into()))?; + offset += bytes_written as u64; + total_bytes += bytes_written as u64; + + if offset == range.length { + break; + } + } + } + + debug!("coredump total bytes {}", total_bytes); + Ok(()) + } + + pub fn receive_memory_regions( + &mut self, + ranges: &MemoryRangeTable, + fd: &mut F, + ) -> std::result::Result<(), MigratableError> + where + F: ReadVolatile, + { + let guest_memory = self.guest_memory(); + let mem = guest_memory.memory(); + + for range in ranges.regions() { + let mut offset: u64 = 0; + // Here we are manually handling the retry in case we can't the + // whole region at once because we can't use the implementation + // from vm-memory::GuestMemory of read_exact_from() as it is not + // following the correct behavior. For more info about this issue + // see: https://github.com/rust-vmm/vm-memory/issues/174 + loop { + let bytes_read = mem + .read_volatile_from( + GuestAddress(range.gpa + offset), + fd, + (range.length - offset) as usize, + ) + .map_err(|e| { + MigratableError::MigrateReceive(anyhow!( + "Error receiving memory from socket: {}", + e + )) + })?; + offset += bytes_read as u64; + + if offset == range.length { + break; + } + } + } + + Ok(()) + } + + pub fn init_dirty_log(&mut self) -> result::Result<(), MigratableError> { + if self.dirty_log { + self.start_dirty_log()?; + } + + Ok(()) + } + + pub fn save_range_to_file( + &self, + mut memory_file: &File, + range: &MemoryRange, + file_offset: u64, + ) -> result::Result<(), MigratableError> { + let guest_memory = self.guest_memory.memory(); + let mut offset: u64 = 0; + + if file_offset != 0 { + memory_file + .seek(SeekFrom::Start(file_offset)) + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + } + // Here we are manually handling the retry in case we can't read + // the whole region at once because we can't use the implementation + // from vm-memory::GuestMemory of write_all_to() as it is not + // following the correct behavior. For more info about this issue + // see: https://github.com/rust-vmm/vm-memory/issues/174 + loop { + let bytes_written = guest_memory + .write_volatile_to( + GuestAddress(range.gpa + offset), + &mut memory_file.as_fd(), + (range.length - offset) as usize, + ) + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + offset += bytes_written as u64; + + if offset == range.length { + break; + } + } + + Ok(()) + } + + /// Save a pagemap_anon incremental snapshot. + /// + /// This method creates a full snapshot by merging the base snapshot with + /// pagemap_anon-filtered anonymous pages (CoW pages) from current VM memory. + /// Only pages that Guest actually wrote to (triggering Copy-on-Write from + /// the MAP_PRIVATE mmap) are saved from guest memory; non-anonymous pages + /// When a base snapshot exists at the destination, the file is opened in + /// read-write mode and only the anonymous (CoW) pages are overwritten; + /// non-anonymous regions keep their original content from the base. + /// + /// When no base snapshot exists (cold start), a new file is created and + /// only anonymous pages are written; non-anonymous regions are left as + /// holes (zeros) in the pre-allocated file. + /// + /// The resulting snapshot file is in the same format as a regular full + /// snapshot and can be restored without any special handling. + fn send_pagemap_anon_memory( + &self, + destination_url: &str, + memory_vol_url: &Option, + ) -> result::Result<(), MigratableError> { + if self.snapshot_memory_ranges.is_empty() { + return Ok(()); + } + + let guest_memory = self.guest_memory.memory(); + + // Classify CoW anonymous pages from pagemap bit 61 (`PM_FILE`): a page + // is saved when it is swapped, or present without `PM_FILE`. + let (filtered_ranges, stats) = + filter_memory_ranges_by_pagemap_anon(&guest_memory, &self.snapshot_memory_ranges) + .map_err(|e| { + MigratableError::MigrateSend(anyhow!( + "Failed to filter memory with pagemap_anon: {}", + e + )) + })?; + + info!( + "PagemapAnon snapshot: total={} bytes ({} pages), anon={} bytes ({} pages), savings={:.1}%", + stats.total_bytes, + stats.total_pages, + stats.saved_bytes, + stats.anon_pages, + stats.savings_percentage() + ); + + let memory_file_target = + MemorySnapshotFile::from_snapshot_url(destination_url, memory_vol_url.as_deref())?; + if !memory_file_target.path().exists() { + return Err(MigratableError::MigrateSend(anyhow!( + "Base snapshot file not found at {:?}, incremental (pagemap_anon) snapshot requires an existing base file", + memory_file_target.path() + ))); + } + + info!( + "Base snapshot found at {:?}, overwriting anonymous pages in-place", + memory_file_target.path() + ); + let memory_file = memory_file_target.open_read_write()?; + + // Build a GPA-to-file-offset mapping for calculating offsets. + let mut gpa_to_file_offset: Vec<(u64, u64, u64)> = Vec::new(); + let mut offset = 0_u64; + for range in self.snapshot_memory_ranges.regions() { + gpa_to_file_offset.push((range.gpa, range.length, offset)); + offset += range.length; + } + + // Write only anonymous pages to the snapshot file. + // With a base, non-anon regions already have the correct content. + // Without a base, non-anon regions are zeros (sparse holes). + for range in filtered_ranges.regions() { + let file_off = + Self::calculate_file_offset_for_gpa(range.gpa, range.length, &gpa_to_file_offset)?; + self.save_range_to_file(&memory_file, range, file_off)?; + } + + info!( + "PagemapAnon snapshot saved: {} anon bytes written to {:?}", + stats.saved_bytes, + memory_file_target.path() + ); + Ok(()) + } + + /// Save a soft-dirty incremental snapshot. + /// + /// This method writes a delta snapshot containing only the pages that + /// must change in the destination snapshot file. We compute that set as + /// the **intersection** of: + /// * pagemap_anon (Copy-on-Written pages — only these can ever differ + /// from the base snapshot file the guest was restored from), and + /// * `/proc/self/pagemap` bit 55 (pages written since the previous + /// `clear_soft_dirty()`). + /// + /// To remain compatible with the rest of the VMM (snapshot consumers + /// expect a self-contained, full-size memory image at the destination), + /// the resulting file is **not** a raw delta. We prepare a base image + /// at the destination and then overwrite only the filtered pages on + /// top. + /// + /// Lazy probe / arm: + /// Soft-dirty tracking is **not** probed nor armed at boot or restore + /// time anymore — `clear_refs(4)` walks every PTE under mmap_lock and + /// is the dominant pause-time cost on multi-GiB guests. Instead, the + /// very first call to this method: + /// 1. Tries `probe_soft_dirty_support()` (which itself performs the + /// initial `clear_soft_dirty()` and arms tracking on success). + /// 2. Writes a snapshot using the pagemap_anon set (every CoW page), + /// because there is no prior soft-dirty window to take an + /// intersection against. + /// 3. Sets `soft_dirty_armed = true` only on probe success. + /// + /// Subsequent calls (with `soft_dirty_armed == true`) take the + /// anon ∩ soft-dirty intersection, write only the changed CoW pages, + /// and re-arm via `clear_soft_dirty()` for the next cycle. + /// + /// Degradation: if `probe_soft_dirty_support()` returns false (kernel + /// without `CONFIG_MEM_SOFT_DIRTY=y`), or any `clear_soft_dirty()` + /// call fails at runtime, this method silently falls back to the + /// pagemap_anon path and leaves `soft_dirty_armed` at false so the + /// next cycle retries. The fallback is logged but not surfaced as + /// an error to the caller. + fn send_soft_dirty_memory( + &self, + destination_url: &str, + memory_vol_url: &Option, + ) -> result::Result<(), MigratableError> { + if self.snapshot_memory_ranges.is_empty() { + return Ok(()); + } + + // First-time path (or any time we are not currently armed): we + // cannot trust the soft-dirty bitmap because there is no prior + // `clear_soft_dirty()` to anchor the window, so write the full + // anon-page set. Then try to arm tracking for the next cycle. + if !self.soft_dirty_armed.load(Ordering::Acquire) { + info!( + "Soft-dirty: tracker not yet armed, writing full anon-page snapshot \ + and attempting to arm soft-dirty for the next cycle" + ); + + // Write a full anon-page (pagemap_anon) snapshot. This already + // matches what `send_pagemap_anon_memory` does and it produces + // a self-contained snapshot file. + self.send_pagemap_anon_memory(destination_url, memory_vol_url)?; + + // Probe + arm. `probe_soft_dirty_support()` writes "4" to + // /proc/self/clear_refs, which both detects kernel support + // (returns false on EINVAL when CONFIG_MEM_SOFT_DIRTY=n) and, + // on success, clears every PTE's bit 55 — which is exactly + // the "arm" operation for the next snapshot window. + if probe_soft_dirty_support() { + self.soft_dirty_armed.store(true, Ordering::Release); + info!("Soft-dirty: tracker armed for next snapshot cycle"); + } else { + debug!( + "Soft-dirty: kernel does not support CONFIG_MEM_SOFT_DIRTY, \ + subsequent snapshots will keep using the anon-page path" + ); + } + + return Ok(()); + } + + // Steady-state path: the tracker has been armed by a previous + // call, so pagemap bit 55 reflects only writes that happened in + // this window. Filter pages by anon ∩ soft-dirty to get the + // minimal set that has actually changed since the last snapshot. + let memory_file_target = + MemorySnapshotFile::from_snapshot_url(destination_url, memory_vol_url.as_deref())?; + + let guest_memory = self.guest_memory.memory(); + let (filtered_ranges, stats) = filter_memory_ranges_by_anon_and_soft_dirty( + &guest_memory, + &self.snapshot_memory_ranges, + ) + .map_err(|e| { + MigratableError::MigrateSend(anyhow!( + "Failed to filter memory with anon ∩ soft-dirty: {}", + e + )) + })?; + + info!( + "Soft-dirty snapshot (anon ∩ soft-dirty): total={} bytes ({} pages), \ + dirty={} bytes ({} pages), savings={:.1}%", + stats.total_bytes, + stats.total_pages, + stats.saved_bytes, + stats.dirty_pages, + stats.savings_percentage() + ); + + // Prepare the destination file. The previous snapshot already + // contains every still-clean page; we just overlay the changed + // ones in place. The base file is mandatory here: if it doesn't + // exist, a soft-dirty (delta) snapshot is meaningless because + // there is no full image to overlay onto. Refuse the request and + // let the caller take a Full snapshot first. + if !memory_file_target.path().exists() { + return Err(MigratableError::MigrateSend(anyhow!( + "Base snapshot file not found at {:?}, soft-dirty snapshot requires \ + an existing base file; take a full snapshot first", + memory_file_target.path() + ))); + } + let memory_file = memory_file_target.open_read_write()?; + + // Build a GPA-to-file-offset mapping for calculating offsets. + let mut gpa_to_file_offset: Vec<(u64, u64, u64)> = Vec::new(); + let mut offset = 0_u64; + for range in self.snapshot_memory_ranges.regions() { + gpa_to_file_offset.push((range.gpa, range.length, offset)); + offset += range.length; + } + + // Overlay only the filtered (anon ∩ soft-dirty) pages. + for range in filtered_ranges.regions() { + let file_off = + Self::calculate_file_offset_for_gpa(range.gpa, range.length, &gpa_to_file_offset)?; + self.save_range_to_file(&memory_file, range, file_off)?; + } + info!( + "Soft-dirty snapshot saved: {} dirty bytes written to {:?}", + stats.saved_bytes, + memory_file_target.path() + ); + + // Re-arm tracking for the next cycle. + if let Err(e) = clear_soft_dirty() { + warn!( + "Soft-dirty: clear_soft_dirty() failed after writing delta ({}), \ + disarming; the next snapshot will fall back to the full \ + anon-page path and try to re-arm", + e + ); + self.soft_dirty_armed.store(false, Ordering::Release); + } + + Ok(()) + } + + /// Calculate the file offset for a given GPA within the snapshot file. + /// + /// The snapshot file stores memory regions sequentially. This method finds + /// which region contains the given GPA and computes the corresponding + /// file offset. + fn calculate_file_offset_for_gpa( + gpa: u64, + length: u64, + gpa_to_file_offset: &[(u64, u64, u64)], + ) -> result::Result { + for &(region_gpa, region_length, file_offset) in gpa_to_file_offset { + if gpa >= region_gpa && gpa + length <= region_gpa + region_length { + return Ok(file_offset + (gpa - region_gpa)); + } + } + Err(MigratableError::MigrateSend(anyhow!( + "Could not find file offset for GPA 0x{:x} (length {})", + gpa, + length + ))) + } +} + +struct MemoryNotify { + slot_id: usize, +} + +impl Aml for MemoryNotify { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let object = aml::Path::new(&format!("M{:03}", self.slot_id)); + aml::If::new( + &aml::Equal::new(&aml::Arg(0), &self.slot_id), + vec![&aml::Notify::new(&object, &aml::Arg(1))], + ) + .append_aml_bytes(bytes) + } +} + +struct MemorySlot { + slot_id: usize, +} + +impl Aml for MemorySlot { + fn append_aml_bytes(&self, bytes: &mut Vec) { + aml::Device::new( + format!("M{:03}", self.slot_id).as_str().into(), + vec![ + &aml::Name::new("_HID".into(), &aml::EisaName::new("PNP0C80")), + &aml::Name::new("_UID".into(), &self.slot_id), + /* + _STA return value: + Bit [0] – Set if the device is present. + Bit [1] – Set if the device is enabled and decoding its resources. + Bit [2] – Set if the device should be shown in the UI. + Bit [3] – Set if the device is functioning properly (cleared if device failed its diagnostics). + Bit [4] – Set if the battery is present. + Bits [31:5] – Reserved (must be cleared). + */ + &aml::Method::new( + "_STA".into(), + 0, + false, + // Call into MSTA method which will interrogate device + vec![&aml::Return::new(&aml::MethodCall::new( + "MSTA".into(), + vec![&self.slot_id], + ))], + ), + // Get details of memory + &aml::Method::new( + "_CRS".into(), + 0, + false, + // Call into MCRS which provides actual memory details + vec![&aml::Return::new(&aml::MethodCall::new( + "MCRS".into(), + vec![&self.slot_id], + ))], + ), + ], + ) + .append_aml_bytes(bytes) + } +} + +struct MemorySlots { + slots: usize, +} + +impl Aml for MemorySlots { + fn append_aml_bytes(&self, bytes: &mut Vec) { + for slot_id in 0..self.slots { + MemorySlot { slot_id }.append_aml_bytes(bytes); + } + } +} + +struct MemoryMethods { + slots: usize, +} + +impl Aml for MemoryMethods { + fn append_aml_bytes(&self, bytes: &mut Vec) { + // Add "MTFY" notification method + let mut memory_notifies = Vec::new(); + for slot_id in 0..self.slots { + memory_notifies.push(MemoryNotify { slot_id }); + } + + let mut memory_notifies_refs: Vec<&dyn aml::Aml> = Vec::new(); + for memory_notifier in memory_notifies.iter() { + memory_notifies_refs.push(memory_notifier); + } + + aml::Method::new("MTFY".into(), 2, true, memory_notifies_refs).append_aml_bytes(bytes); + + // MSCN method + aml::Method::new( + "MSCN".into(), + 0, + true, + vec![ + // Take lock defined above + &aml::Acquire::new("MLCK".into(), 0xffff), + &aml::Store::new(&aml::Local(0), &aml::ZERO), + &aml::While::new( + &aml::LessThan::new(&aml::Local(0), &self.slots), + vec![ + // Write slot number (in first argument) to I/O port via field + &aml::Store::new(&aml::Path::new("\\_SB_.MHPC.MSEL"), &aml::Local(0)), + // Check if MINS bit is set (inserting) + &aml::If::new( + &aml::Equal::new(&aml::Path::new("\\_SB_.MHPC.MINS"), &aml::ONE), + // Notify device if it is + vec![ + &aml::MethodCall::new( + "MTFY".into(), + vec![&aml::Local(0), &aml::ONE], + ), + // Reset MINS bit + &aml::Store::new(&aml::Path::new("\\_SB_.MHPC.MINS"), &aml::ONE), + ], + ), + // Check if MRMV bit is set + &aml::If::new( + &aml::Equal::new(&aml::Path::new("\\_SB_.MHPC.MRMV"), &aml::ONE), + // Notify device if it is (with the eject constant 0x3) + vec![ + &aml::MethodCall::new("MTFY".into(), vec![&aml::Local(0), &3u8]), + // Reset MRMV bit + &aml::Store::new(&aml::Path::new("\\_SB_.MHPC.MRMV"), &aml::ONE), + ], + ), + &aml::Add::new(&aml::Local(0), &aml::Local(0), &aml::ONE), + ], + ), + // Release lock + &aml::Release::new("MLCK".into()), + ], + ) + .append_aml_bytes(bytes); + + // Memory status method + aml::Method::new( + "MSTA".into(), + 1, + true, + vec![ + // Take lock defined above + &aml::Acquire::new("MLCK".into(), 0xffff), + // Write slot number (in first argument) to I/O port via field + &aml::Store::new(&aml::Path::new("\\_SB_.MHPC.MSEL"), &aml::Arg(0)), + &aml::Store::new(&aml::Local(0), &aml::ZERO), + // Check if MEN_ bit is set, if so make the local variable 0xf (see _STA for details of meaning) + &aml::If::new( + &aml::Equal::new(&aml::Path::new("\\_SB_.MHPC.MEN_"), &aml::ONE), + vec![&aml::Store::new(&aml::Local(0), &0xfu8)], + ), + // Release lock + &aml::Release::new("MLCK".into()), + // Return 0 or 0xf + &aml::Return::new(&aml::Local(0)), + ], + ) + .append_aml_bytes(bytes); + + // Memory range method + aml::Method::new( + "MCRS".into(), + 1, + true, + vec![ + // Take lock defined above + &aml::Acquire::new("MLCK".into(), 0xffff), + // Write slot number (in first argument) to I/O port via field + &aml::Store::new(&aml::Path::new("\\_SB_.MHPC.MSEL"), &aml::Arg(0)), + &aml::Name::new( + "MR64".into(), + &aml::ResourceTemplate::new(vec![&aml::AddressSpace::new_memory( + aml::AddressSpaceCachable::Cacheable, + true, + 0x0000_0000_0000_0000u64, + 0xFFFF_FFFF_FFFF_FFFEu64, + )]), + ), + &aml::CreateField::::new(&aml::Path::new("MR64"), &14usize, "MINL".into()), + &aml::CreateField::::new(&aml::Path::new("MR64"), &18usize, "MINH".into()), + &aml::CreateField::::new(&aml::Path::new("MR64"), &22usize, "MAXL".into()), + &aml::CreateField::::new(&aml::Path::new("MR64"), &26usize, "MAXH".into()), + &aml::CreateField::::new(&aml::Path::new("MR64"), &38usize, "LENL".into()), + &aml::CreateField::::new(&aml::Path::new("MR64"), &42usize, "LENH".into()), + &aml::Store::new(&aml::Path::new("MINL"), &aml::Path::new("\\_SB_.MHPC.MHBL")), + &aml::Store::new(&aml::Path::new("MINH"), &aml::Path::new("\\_SB_.MHPC.MHBH")), + &aml::Store::new(&aml::Path::new("LENL"), &aml::Path::new("\\_SB_.MHPC.MHLL")), + &aml::Store::new(&aml::Path::new("LENH"), &aml::Path::new("\\_SB_.MHPC.MHLH")), + &aml::Add::new( + &aml::Path::new("MAXL"), + &aml::Path::new("MINL"), + &aml::Path::new("LENL"), + ), + &aml::Add::new( + &aml::Path::new("MAXH"), + &aml::Path::new("MINH"), + &aml::Path::new("LENH"), + ), + &aml::If::new( + &aml::LessThan::new(&aml::Path::new("MAXL"), &aml::Path::new("MINL")), + vec![&aml::Add::new( + &aml::Path::new("MAXH"), + &aml::ONE, + &aml::Path::new("MAXH"), + )], + ), + &aml::Subtract::new(&aml::Path::new("MAXL"), &aml::Path::new("MAXL"), &aml::ONE), + // Release lock + &aml::Release::new("MLCK".into()), + &aml::Return::new(&aml::Path::new("MR64")), + ], + ) + .append_aml_bytes(bytes) + } +} + +impl Aml for MemoryManager { + fn append_aml_bytes(&self, bytes: &mut Vec) { + if let Some(acpi_address) = self.acpi_address { + // Memory Hotplug Controller + aml::Device::new( + "_SB_.MHPC".into(), + vec![ + &aml::Name::new("_HID".into(), &aml::EisaName::new("PNP0A06")), + &aml::Name::new("_UID".into(), &"Memory Hotplug Controller"), + // Mutex to protect concurrent access as we write to choose slot and then read back status + &aml::Mutex::new("MLCK".into(), 0), + &aml::Name::new( + "_CRS".into(), + &aml::ResourceTemplate::new(vec![&aml::AddressSpace::new_memory( + aml::AddressSpaceCachable::NotCacheable, + true, + acpi_address.0, + acpi_address.0 + MEMORY_MANAGER_ACPI_SIZE as u64 - 1, + )]), + ), + // OpRegion and Fields map MMIO range into individual field values + &aml::OpRegion::new( + "MHPR".into(), + aml::OpRegionSpace::SystemMemory, + acpi_address.0 as usize, + MEMORY_MANAGER_ACPI_SIZE, + ), + &aml::Field::new( + "MHPR".into(), + aml::FieldAccessType::DWord, + aml::FieldUpdateRule::Preserve, + vec![ + aml::FieldEntry::Named(*b"MHBL", 32), // Base (low 4 bytes) + aml::FieldEntry::Named(*b"MHBH", 32), // Base (high 4 bytes) + aml::FieldEntry::Named(*b"MHLL", 32), // Length (low 4 bytes) + aml::FieldEntry::Named(*b"MHLH", 32), // Length (high 4 bytes) + ], + ), + &aml::Field::new( + "MHPR".into(), + aml::FieldAccessType::DWord, + aml::FieldUpdateRule::Preserve, + vec![ + aml::FieldEntry::Reserved(128), + aml::FieldEntry::Named(*b"MHPX", 32), // PXM + ], + ), + &aml::Field::new( + "MHPR".into(), + aml::FieldAccessType::Byte, + aml::FieldUpdateRule::WriteAsZeroes, + vec![ + aml::FieldEntry::Reserved(160), + aml::FieldEntry::Named(*b"MEN_", 1), // Enabled + aml::FieldEntry::Named(*b"MINS", 1), // Inserting + aml::FieldEntry::Named(*b"MRMV", 1), // Removing + aml::FieldEntry::Named(*b"MEJ0", 1), // Ejecting + ], + ), + &aml::Field::new( + "MHPR".into(), + aml::FieldAccessType::DWord, + aml::FieldUpdateRule::Preserve, + vec![ + aml::FieldEntry::Named(*b"MSEL", 32), // Selector + aml::FieldEntry::Named(*b"MOEV", 32), // Event + aml::FieldEntry::Named(*b"MOSC", 32), // OSC + ], + ), + &MemoryMethods { + slots: self.hotplug_slots.len(), + }, + &MemorySlots { + slots: self.hotplug_slots.len(), + }, + ], + ) + .append_aml_bytes(bytes); + } else { + aml::Device::new( + "_SB_.MHPC".into(), + vec![ + &aml::Name::new("_HID".into(), &aml::EisaName::new("PNP0A06")), + &aml::Name::new("_UID".into(), &"Memory Hotplug Controller"), + // Empty MSCN for GED + &aml::Method::new("MSCN".into(), 0, true, vec![]), + ], + ) + .append_aml_bytes(bytes); + } + + #[cfg(target_arch = "x86_64")] + { + if let Some(sgx_epc_region) = &self.sgx_epc_region { + let min = sgx_epc_region.start().raw_value(); + let max = min + sgx_epc_region.size() - 1; + // SGX EPC region + aml::Device::new( + "_SB_.EPC_".into(), + vec![ + &aml::Name::new("_HID".into(), &aml::EisaName::new("INT0E0C")), + // QWORD describing the EPC region start and size + &aml::Name::new( + "_CRS".into(), + &aml::ResourceTemplate::new(vec![&aml::AddressSpace::new_memory( + aml::AddressSpaceCachable::NotCacheable, + true, + min, + max, + )]), + ), + &aml::Method::new("_STA".into(), 0, false, vec![&aml::Return::new(&0xfu8)]), + ], + ) + .append_aml_bytes(bytes); + } + } + } +} + +impl Pausable for MemoryManager {} + +#[derive(Clone, Serialize, Deserialize)] +pub struct MemoryManagerSnapshotData { + memory_ranges: MemoryRangeTable, + guest_ram_mappings: Vec, + start_of_device_area: u64, + boot_ram: u64, + current_ram: u64, + arch_mem_regions: Vec, + hotplug_slots: Vec, + next_memory_slot: u32, + selected_slot: usize, + next_hotplug_slot: usize, +} + +impl Snapshottable for MemoryManager { + fn id(&self) -> String { + MEMORY_MANAGER_SNAPSHOT_ID.to_string() + } + + fn snapshot(&mut self) -> result::Result { + let mut memory_manager_snapshot = Snapshot::new(MEMORY_MANAGER_SNAPSHOT_ID); + + let memory_ranges = self.memory_range_table(true)?; + + // Store locally this list of ranges as it will be used through the + // Transportable::send() implementation. The point is to avoid the + // duplication of code regarding the creation of the path for each + // region. The 'snapshot' step creates the list of memory regions, + // including information about the need to copy a memory region or + // not. This saves the 'send' step having to go through the same + // process, and instead it can directly proceed with storing the + // memory range content for the ranges requiring it. + self.snapshot_memory_ranges = memory_ranges; + + memory_manager_snapshot.add_data_section(SnapshotDataSection::new_from_state( + MEMORY_MANAGER_SNAPSHOT_ID, + &self.snapshot_data(), + )?); + + Ok(memory_manager_snapshot) + } +} + +impl Transportable for MemoryManager { + fn send( + &self, + _snapshot: &Snapshot, + config: &SnapshotConfig, + ) -> result::Result<(), MigratableError> { + if self.snapshot_memory_ranges.is_empty() { + return Ok(()); + } + + let destination_url = &config.destination_url; + let memory_file_target = MemorySnapshotFile::from_snapshot_url( + destination_url, + config.memory_vol_url.as_deref(), + )?; + + if self.dirty_log { + let guest_memory = self.guest_memory.memory(); + let memory_file = memory_file_target.open_for_fresh_write()?; + info!("Saving dirty guest memory to snapshot image file."); + let total_size = guest_memory.iter().map(|region| region.len()).sum::(); + if !memory_file_target.is_external() { + memory_file + .set_len(total_size) + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + } + + let mut offset = 0_u64; + for range in self.snapshot_memory_ranges.regions() { + let vmm_dirty_bitmap = match self + .guest_memory + .memory() + .find_region(GuestAddress(range.gpa)) + { + Some(region) => { + assert!(region.start_addr().raw_value() == range.gpa); + assert!(region.len() == range.length); + region.bitmap().get_and_reset() + } + None => { + return Err(MigratableError::MigrateSend(anyhow!( + "Error finding 'guest memory region' with address {:x}", + range.gpa + ))) + } + }; + + let slot = match self + .guest_ram_mappings + .iter() + .find(|map| map.gpa == range.gpa && map.size == range.length) + { + Some(map) => map.slot, + None => { + return Err(MigratableError::MigrateSend(anyhow!( + "Error finding 'guest ram mapping' with address {:x}", + range.gpa + ))) + } + }; + + let vm_dirty_bitmap = self + .vm + .get_dirty_log(slot, range.gpa, range.length) + .map_err(|e| { + MigratableError::MigrateSend(anyhow!("Error getting VM dirty log {}", e)) + })?; + + let dirty_bitmap: Vec = vm_dirty_bitmap + .iter() + .zip(vmm_dirty_bitmap.iter()) + .map(|(x, y)| x | y) + .collect(); + + let sub_table = + MemoryRangeTable::from_bitmap(dirty_bitmap, range.gpa, host_page_size()); + + if !sub_table.regions().is_empty() { + for r in sub_table.regions() { + self.save_range_to_file(&memory_file, r, r.gpa - range.gpa + offset)?; + } + } + + // Move to next range. + offset += range.length; + } + return Ok(()); + } + + match config.snapshot_type { + SnapshotType::Incremental => { + self.send_pagemap_anon_memory(destination_url, &config.memory_vol_url)?; + } + SnapshotType::SoftDirty => { + self.send_soft_dirty_memory(destination_url, &config.memory_vol_url)?; + } + SnapshotType::Full => { + let memory_file = memory_file_target.open_for_fresh_write()?; + + info!("Saving full guest memory to snapshot image file."); + let mut file_offset = 0u64; + for range in self.snapshot_memory_ranges.regions() { + self.save_range_to_file(&memory_file, range, file_offset)?; + file_offset += range.length; + } + } + } + Ok(()) + } +} + +impl Migratable for MemoryManager { + // Start the dirty log in the hypervisor (kvm/mshv). + // Also, reset the dirty bitmap logged by the vmm. + // Just before we do a bulk copy we want to start/clear the dirty log so that + // pages touched during our bulk copy are tracked. + fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + self.vm.start_dirty_log().map_err(|e| { + MigratableError::MigrateSend(anyhow!("Error starting VM dirty log {}", e)) + })?; + + for r in self.guest_memory.memory().iter() { + r.bitmap().reset(); + } + + Ok(()) + } + + fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + self.vm.stop_dirty_log().map_err(|e| { + MigratableError::MigrateSend(anyhow!("Error stopping VM dirty log {}", e)) + })?; + + Ok(()) + } + + // Generate a table for the pages that are dirty. The dirty pages are collapsed + // together in the table if they are contiguous. + fn dirty_log(&mut self) -> std::result::Result { + let mut table = MemoryRangeTable::default(); + for r in &self.guest_ram_mappings { + let vm_dirty_bitmap = self.vm.get_dirty_log(r.slot, r.gpa, r.size).map_err(|e| { + MigratableError::MigrateSend(anyhow!("Error getting VM dirty log {}", e)) + })?; + let vmm_dirty_bitmap = match self.guest_memory.memory().find_region(GuestAddress(r.gpa)) + { + Some(region) => { + assert!(region.start_addr().raw_value() == r.gpa); + assert!(region.len() == r.size); + region.bitmap().get_and_reset() + } + None => { + return Err(MigratableError::MigrateSend(anyhow!( + "Error finding 'guest memory region' with address {:x}", + r.gpa + ))) + } + }; + + let dirty_bitmap: Vec = vm_dirty_bitmap + .iter() + .zip(vmm_dirty_bitmap.iter()) + .map(|(x, y)| x | y) + .collect(); + + let sub_table = MemoryRangeTable::from_bitmap(dirty_bitmap, r.gpa, host_page_size()); + + if sub_table.regions().is_empty() { + info!("Dirty Memory Range Table is empty"); + } else { + info!("Dirty Memory Range Table:"); + for range in sub_table.regions() { + info!("GPA: {:x} size: {} (KiB)", range.gpa, range.length / 1024); + } + } + + table.extend(sub_table); + } + Ok(table) + } +} diff --git a/vendor/cube-hypervisor/vmm/src/migration.rs b/vendor/cube-hypervisor/vmm/src/migration.rs new file mode 100644 index 0000000..50cbc67 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/migration.rs @@ -0,0 +1,188 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +#[cfg(feature = "guest_debug")] +use crate::coredump::GuestDebuggableError; +use crate::vm::VmSnapshot; +use crate::vm::VM_SNAPSHOT_ID; +use crate::vm_config::VmConfig; +use anyhow::anyhow; +use std::fs::{metadata, File}; +use std::io::Read; +use std::os::unix::fs::FileTypeExt; +use std::path::PathBuf; +use vm_migration::{MigratableError, Snapshot}; + +pub const SNAPSHOT_STATE_FILE: &str = "state.json"; +pub const SNAPSHOT_CONFIG_FILE: &str = "config.json"; + +fn file_url_to_path(url: &str, kind: &str) -> std::result::Result { + url.strip_prefix("file://") + .ok_or_else(|| anyhow!("Could not extract {} from URL: {}", kind, url)) + .map(PathBuf::from) +} + +pub fn url_to_path(url: &str) -> std::result::Result { + let path = file_url_to_path(url, "path").map_err(MigratableError::MigrateSend)?; + + if !path.is_dir() { + return Err(MigratableError::MigrateSend(anyhow!( + "Destination is not a directory" + ))); + } + + Ok(path) +} + +pub fn memory_blob_to_path(input: &str) -> std::result::Result { + let path = if input.starts_with("file://") { + file_url_to_path(input, "memory blob path").map_err(MigratableError::MigrateSend)? + } else if input.contains("://") { + return Err(MigratableError::MigrateSend(anyhow!( + "Unsupported memory blob URL scheme: {}", + input + ))); + } else { + PathBuf::from(input) + }; + + if !path.is_absolute() { + return Err(MigratableError::MigrateSend(anyhow!( + "Memory blob path must be absolute: {}", + path.display() + ))); + } + + let file_type = metadata(&path) + .map_err(|e| { + MigratableError::MigrateSend(anyhow!( + "Failed to stat memory blob path {}: {}", + path.display(), + e + )) + })? + .file_type(); + + if file_type.is_dir() { + return Err(MigratableError::MigrateSend(anyhow!( + "Memory blob path must not be a directory: {}", + path.display() + ))); + } + + if !(file_type.is_file() || file_type.is_block_device()) { + return Err(MigratableError::MigrateSend(anyhow!( + "Memory blob path must point to an existing regular file or block device: {}", + path.display() + ))); + } + + Ok(path) +} + +#[cfg(feature = "guest_debug")] +pub fn url_to_file(url: &str) -> std::result::Result { + let file = + file_url_to_path(url, "file").map_err(|e| GuestDebuggableError::Coredump(anyhow!(e)))?; + + Ok(file) +} + +pub fn recv_vm_config(source_url: &str) -> std::result::Result { + let mut vm_config_path = url_to_path(source_url)?; + + vm_config_path.push(SNAPSHOT_CONFIG_FILE); + + // Try opening the snapshot file + let mut vm_config_file = + File::open(vm_config_path).map_err(|e| MigratableError::MigrateReceive(e.into()))?; + let mut bytes = Vec::new(); + vm_config_file.read_to_end(&mut bytes).unwrap(); + + serde_json::from_slice(&bytes).map_err(|e| MigratableError::MigrateReceive(e.into())) +} + +pub fn recv_vm_state(source_url: &str) -> std::result::Result { + let mut vm_state_path = url_to_path(source_url)?; + + vm_state_path.push(SNAPSHOT_STATE_FILE); + + // Try opening the snapshot file + let mut vm_state_file = + File::open(vm_state_path).map_err(|e| MigratableError::MigrateReceive(e.into()))?; + let mut bytes = Vec::new(); + vm_state_file.read_to_end(&mut bytes).unwrap(); + + serde_json::from_slice(&bytes).map_err(|e| MigratableError::MigrateReceive(e.into())) +} + +pub fn get_vm_snapshot(snapshot: &Snapshot) -> std::result::Result { + if let Some(vm_section) = snapshot + .snapshot_data + .get(&format!("{}-section", VM_SNAPSHOT_ID)) + { + return vm_section.to_state(); + } + + Err(MigratableError::Restore(anyhow!( + "Could not find VM config snapshot section" + ))) +} + +#[cfg(test)] +mod tests { + use super::{memory_blob_to_path, url_to_path}; + use std::fs; + use std::path::PathBuf; + use std::time::{SystemTime, UNIX_EPOCH}; + + fn unique_temp_path(name: &str) -> PathBuf { + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + std::env::temp_dir().join(format!("ch-migration-{}-{}", name, nanos)) + } + + #[test] + fn memory_blob_accepts_absolute_regular_file_and_file_url() { + let file_path = unique_temp_path("memory-blob"); + fs::write(&file_path, b"memory").unwrap(); + + let file_url = format!("file://{}", file_path.display()); + assert_eq!( + memory_blob_to_path(file_path.to_str().unwrap()).unwrap(), + file_path + ); + assert_eq!(memory_blob_to_path(&file_url).unwrap(), file_path); + + fs::remove_file(file_path).unwrap(); + } + + #[test] + fn memory_blob_rejects_relative_paths_directories_and_invalid_schemes() { + let dir_path = unique_temp_path("memory-dir"); + fs::create_dir_all(&dir_path).unwrap(); + let missing_path = unique_temp_path("missing"); + + assert!(memory_blob_to_path("relative/path").is_err()); + assert!(memory_blob_to_path(dir_path.to_str().unwrap()).is_err()); + assert!(memory_blob_to_path(missing_path.to_str().unwrap()).is_err()); + assert!(memory_blob_to_path("dev:///tmp/memory").is_err()); + + fs::remove_dir_all(dir_path).unwrap(); + } + + #[test] + fn url_to_path_still_requires_directory_file_url() { + let dir_path = unique_temp_path("snapshot-dir"); + fs::create_dir_all(&dir_path).unwrap(); + let file_url = format!("file://{}", dir_path.display()); + + assert_eq!(url_to_path(&file_url).unwrap(), dir_path); + assert!(url_to_path(dir_path.to_str().unwrap()).is_err()); + + fs::remove_dir_all(dir_path).unwrap(); + } +} diff --git a/vendor/cube-hypervisor/vmm/src/pagemap_anon.rs b/vendor/cube-hypervisor/vmm/src/pagemap_anon.rs new file mode 100644 index 0000000..fd2652e --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/pagemap_anon.rs @@ -0,0 +1,565 @@ +// Copyright © 2026 Tencent Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +//! Incremental snapshots of Guest-written CoW anonymous pages. +//! +//! A page is saved when `swapped || (present && !PM_FILE)`, all three flags +//! coming from a single `/proc/self/pagemap` entry: bit 63 (`present`), bit 62 +//! (`swapped`) and bit 61 (`PM_FILE`). No PFN is involved, so kernel page +//! migration cannot invalidate the classification, and reading the flags needs +//! no `CAP_SYS_ADMIN`. +//! +//! `PM_FILE` is the *negative* signal: the kernel only sets it for pages it +//! reports as non-anonymous (`page && !PageAnon(page)`), so a missing +//! `PM_FILE` is classified as "save". Kernels predating upstream `3f9f022e` +//! (6.6.44 on the 6.6 stable line, ~6.11 mainline) omit `PM_FILE` on +//! PMD-mapped file THPs, which makes those pages be saved unnecessarily: that +//! costs savings, never correctness, and requires file-backed THP (tmpfs with +//! shmem THP explicitly enabled), which Cube hosts do not have +//! (`transparent_hugepage=never` and shmem THP defaults to off). +//! +//! Under `MAP_PRIVATE` restore, unread and read-only file pages are skipped; +//! Guest writes become private anon and are saved. + +use log::{debug, trace}; +use once_cell::sync::Lazy; +use std::fs::File; +use std::io::{self, Read, Seek, SeekFrom}; +use thiserror::Error; +use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap}; +use vm_migration::protocol::{MemoryRange, MemoryRangeTable}; + +/// Host page size in bytes, probed once from `sysconf(_SC_PAGESIZE)`. +/// +/// This is 4 KiB on x86_64 but 64 KiB on ARM64 hosts configured with 64 KiB +/// base pages. `/proc/self/pagemap` is indexed in units of the kernel's real +/// page size, so every page-index, seek-offset and range-length computation +/// must use this value — hardcoding 4096 would mis-index the pagemap and +/// silently corrupt snapshots on 64 KiB kernels. +/// +/// The value is fixed for the process lifetime, so probe it once and cache it. +static HOST_PAGE_SIZE: Lazy = Lazy::new(|| { + // Trivially safe: sysconf(_SC_PAGESIZE) takes no pointer argument. + let ret = unsafe { libc::sysconf(libc::_SC_PAGESIZE) }; + // POSIX returns -1 on failure; a non-positive page size would corrupt all + // pagemap offset math. The checked conversion rejects any negative value. + u64::try_from(ret).expect("sysconf(_SC_PAGESIZE) returned non-positive value") +}); + +/// Returns the host page size in bytes (cached; probed once via sysconf). +pub fn host_page_size() -> u64 { + *HOST_PAGE_SIZE +} + +/// Coalesce a per-page boolean bitmap into contiguous guest-physical ranges. +/// +/// `gpa` is the base guest-physical address the bitmap covers and `page_size` +/// is the byte granularity each bit represents. Returns the merged ranges plus +/// the number of set pages (for stats/accounting). +/// +/// Kept as a pure function (no `/proc` access) with an explicit `page_size` +/// argument so it can be exercised with an injected page size — in particular +/// 65536 to prove the ARM64 64 KiB path — without a matching-page-size kernel. +pub(crate) fn coalesce_pages_to_ranges( + gpa: u64, + bitmap: &[bool], + page_size: u64, +) -> (Vec, u64) { + let mut ranges = Vec::new(); + let mut set_pages: u64 = 0; + let mut current_range_start: Option = None; + let mut current_range_length: u64 = 0; + + for (page_idx, &set) in bitmap.iter().enumerate() { + let page_gpa = gpa + (page_idx as u64 * page_size); + + if set { + set_pages += 1; + if current_range_start.is_none() { + current_range_start = Some(page_gpa); + current_range_length = page_size; + } else { + current_range_length += page_size; + } + } else if let Some(start) = current_range_start.take() { + ranges.push(MemoryRange { + gpa: start, + length: current_range_length, + }); + current_range_length = 0; + } + } + + if let Some(start) = current_range_start { + ranges.push(MemoryRange { + gpa: start, + length: current_range_length, + }); + } + + (ranges, set_pages) +} + +/// Size of a pagemap entry in bytes +const PAGEMAP_ENTRY_SIZE: u64 = 8; + +/// Bit 63: page is present in RAM +const PAGEMAP_PRESENT_BIT: u64 = 1 << 63; + +/// Bit 62: page is in swap +const PAGEMAP_SWAPPED_BIT: u64 = 1 << 62; + +/// Bit 61: `PM_FILE` (file-backed or shared-anon; private CoW anon has this clear). +const PAGEMAP_FILE_BIT: u64 = 1 << 61; + +/// `true` if this pagemap entry must be written into an incremental snapshot. +pub(crate) fn pagemap_entry_is_cow_anon(entry: u64) -> bool { + let swapped = (entry & PAGEMAP_SWAPPED_BIT) != 0; + let present = (entry & PAGEMAP_PRESENT_BIT) != 0; + let pm_file = (entry & PAGEMAP_FILE_BIT) != 0; + swapped || (present && !pm_file) +} + +/// Errors related to pagemap_anon operations +#[derive(Debug, Error)] +pub enum PagemapAnonError { + #[error("Failed to open {path}: {source}")] + OpenFailed { + path: String, + #[source] + source: io::Error, + }, + + #[error("Failed to read {path}: {source}")] + ReadFailed { + path: String, + #[source] + source: io::Error, + }, + + #[error("Failed to seek in {path}: {source}")] + SeekFailed { + path: String, + #[source] + source: io::Error, + }, + + #[error("Failed to get host address for guest memory region")] + GetHostAddressFailed, + + #[error("Memory region not aligned to page boundary")] + NotPageAligned, +} + +/// Result type for pagemap_anon operations +pub type Result = std::result::Result; + +/// Statistics about pagemap_anon filtering results +#[derive(Debug, Default, Clone)] +pub struct PagemapAnonStats { + /// Total number of pages in the memory regions + pub total_pages: u64, + /// Number of anonymous pages (CoW pages written by Guest) + pub anon_pages: u64, + /// Number of pages that are swapped out (also counted as anon) + pub swapped_pages: u64, + /// Total bytes in all memory regions + pub total_bytes: u64, + /// Bytes that will be saved (anonymous pages) + pub saved_bytes: u64, +} + +impl PagemapAnonStats { + /// Calculate the percentage of memory saved (not needing to be snapshotted) + pub fn savings_percentage(&self) -> f64 { + if self.total_bytes == 0 { + return 0.0; + } + ((self.total_bytes - self.saved_bytes) as f64 / self.total_bytes as f64) * 100.0 + } +} + +/// Per-page CoW-anon bitmap, classified from pagemap bit 61 (`PM_FILE`). +pub fn get_anon_pages(host_addr: u64, length: u64) -> Result> { + Ok(scan_pagemap_cow_anon(host_addr, length)?.0) +} + +fn read_pagemap_entries(host_addr: u64, length: u64) -> Result> { + let page_size = host_page_size(); + if host_addr % page_size != 0 { + return Err(PagemapAnonError::NotPageAligned); + } + + let num_pages = length.div_ceil(page_size) as usize; + let start_page = host_addr / page_size; + + let mut pagemap_file = + File::open("/proc/self/pagemap").map_err(|e| PagemapAnonError::OpenFailed { + path: "/proc/self/pagemap".to_string(), + source: e, + })?; + + let pagemap_offset = start_page * PAGEMAP_ENTRY_SIZE; + pagemap_file + .seek(SeekFrom::Start(pagemap_offset)) + .map_err(|e| PagemapAnonError::SeekFailed { + path: "/proc/self/pagemap".to_string(), + source: e, + })?; + + let buf_size = num_pages * PAGEMAP_ENTRY_SIZE as usize; + let mut pagemap_buf = vec![0u8; buf_size]; + pagemap_file + .read_exact(&mut pagemap_buf) + .map_err(|e| PagemapAnonError::ReadFailed { + path: "/proc/self/pagemap".to_string(), + source: e, + })?; + + Ok(pagemap_buf + .chunks_exact(PAGEMAP_ENTRY_SIZE as usize) + .map(|chunk| u64::from_ne_bytes(chunk.try_into().unwrap())) + .collect()) +} + +/// Bit-61 scan. Returns (must-save bitmap, swapped-page count). +pub(crate) fn scan_pagemap_cow_anon(host_addr: u64, length: u64) -> Result<(Vec, u64)> { + let entries = read_pagemap_entries(host_addr, length)?; + let mut result = vec![false; entries.len()]; + let mut swapped_pages = 0u64; + + for (item, entry) in result.iter_mut().zip(entries.iter()) { + if (entry & PAGEMAP_SWAPPED_BIT) != 0 { + swapped_pages += 1; + } + *item = pagemap_entry_is_cow_anon(*entry); + } + + Ok((result, swapped_pages)) +} + +/// Filter memory ranges by pagemap_anon, returning only ranges with anonymous (CoW) pages. +/// +/// This function takes a table of memory ranges and returns a new table +/// containing only the pages that are anonymous (written by Guest via CoW). +/// +/// # Arguments +/// * `guest_memory` - The guest memory object +/// * `ranges` - The original memory range table +/// +/// # Returns +/// A tuple containing: +/// - The filtered memory range table (only anonymous pages) +/// - Statistics about the filtering +pub fn filter_memory_ranges_by_pagemap_anon( + guest_memory: &GuestMemoryMmap, + ranges: &MemoryRangeTable, +) -> Result<(MemoryRangeTable, PagemapAnonStats)> { + let mut filtered_ranges = MemoryRangeTable::default(); + let mut stats = PagemapAnonStats::default(); + let page_size = host_page_size(); + + debug!( + "Starting pagemap_anon filtering for {} memory regions", + ranges.regions().len() + ); + + for range in ranges.regions() { + let gpa = range.gpa; + let length = range.length; + + stats.total_bytes += length; + stats.total_pages += length.div_ceil(page_size); + + trace!( + "Processing memory region: GPA=0x{:x}, length={}", + gpa, + length + ); + + // Get host virtual address for this guest physical address + let host_addr = guest_memory + .get_host_address(GuestAddress(gpa)) + .map_err(|_| PagemapAnonError::GetHostAddressFailed)?; + + let (anon_pages, swapped_count) = scan_pagemap_cow_anon(host_addr as u64, length)?; + + // Convert bitmap to memory ranges (merge consecutive anonymous pages) + let (region_ranges, anon_count) = coalesce_pages_to_ranges(gpa, &anon_pages, page_size); + stats.anon_pages += anon_count; + stats.swapped_pages += swapped_count; + stats.saved_bytes += anon_count * page_size; + for r in region_ranges { + filtered_ranges.push(r); + } + } + + debug!( + "PagemapAnon filtering complete: {} anon ranges, {} total pages, {} anon pages ({} swapped)", + filtered_ranges.regions().len(), + stats.total_pages, + stats.anon_pages, + stats.swapped_pages + ); + + if stats.total_pages > 0 { + let anon_pct = (stats.anon_pages as f64 / stats.total_pages as f64) * 100.0; + debug!( + "PagemapAnon stats: {:.1}% anonymous pages, {:.1}% savings vs full snapshot", + anon_pct, + stats.savings_percentage() + ); + } + + Ok((filtered_ranges, stats)) +} + +#[cfg(test)] +#[path = "pagemap_anon_bench.rs"] +mod benchmark; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_pagemap_anon_stats_savings_percentage() { + let stats = PagemapAnonStats { + total_bytes: 1000, + saved_bytes: 250, + ..Default::default() + }; + + assert!((stats.savings_percentage() - 75.0).abs() < 0.01); + } + + #[test] + fn test_pagemap_anon_stats_zero_total() { + let stats = PagemapAnonStats::default(); + assert_eq!(stats.savings_percentage(), 0.0); + } + + #[test] + fn test_pagemap_anon_stats_full_anon() { + let stats = PagemapAnonStats { + total_bytes: 4096, + saved_bytes: 4096, + ..Default::default() + }; + + assert!((stats.savings_percentage() - 0.0).abs() < 0.01); + } + + #[test] + fn test_get_anon_pages_not_page_aligned() { + // An address one byte past a page boundary must never be page-aligned, + // regardless of whether the host uses 4 KiB or 64 KiB pages. + let unaligned = host_page_size() + 1; + let result = get_anon_pages(unaligned, host_page_size()); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + PagemapAnonError::NotPageAligned + )); + } + + #[test] + fn test_host_page_size_is_power_of_two() { + let ps = host_page_size(); + assert!(ps >= 4096, "page size unexpectedly small: {ps}"); + assert!(ps.is_power_of_two(), "page size not a power of two: {ps}"); + } + + #[test] + fn test_pagemap_constants() { + // Verify bit positions are correct + assert_eq!(PAGEMAP_PRESENT_BIT, 1u64 << 63); + assert_eq!(PAGEMAP_SWAPPED_BIT, 1u64 << 62); + assert_eq!(PAGEMAP_FILE_BIT, 1u64 << 61); + } + + /// Bit 55 is only used here to prove the decoder ignores soft-dirty on + /// file-backed pages. Soft-dirty tracking itself lives in `soft_dirty.rs`. + const PAGEMAP_SOFT_DIRTY_BIT: u64 = 1 << 55; + + #[test] + fn test_pagemap_entry_is_cow_anon_decoder() { + assert!( + !pagemap_entry_is_cow_anon(0), + "zero entry (never mapped) must not be saved" + ); + assert!( + pagemap_entry_is_cow_anon(PAGEMAP_PRESENT_BIT), + "present, non-file → private anon CoW, must save" + ); + assert!( + !pagemap_entry_is_cow_anon(PAGEMAP_PRESENT_BIT | PAGEMAP_FILE_BIT), + "present file-backed page must not be saved" + ); + assert!( + pagemap_entry_is_cow_anon(PAGEMAP_SWAPPED_BIT), + "swapped-out anonymous page must be saved" + ); + assert!( + pagemap_entry_is_cow_anon(PAGEMAP_PRESENT_BIT | PAGEMAP_SWAPPED_BIT), + "present+swap should not occur; still conservative must-save" + ); + assert!( + !pagemap_entry_is_cow_anon( + PAGEMAP_PRESENT_BIT | PAGEMAP_FILE_BIT | PAGEMAP_SOFT_DIRTY_BIT + ), + "file page with write-protect tracking is still a file page" + ); + } + + /// Coalescing must produce byte offsets/lengths scaled by the *injected* + /// page size. Running the identical bitmap through 4 KiB and 64 KiB proves + /// the ARM64 64 KiB path: a hardcoded 4096 would fail the 65536 case. + #[test] + fn test_coalesce_pages_to_ranges_page_size_matrix() { + // Bitmap: pages 1,2 dirty; page 3 clean; page 5 dirty. + // Expect two ranges: [1..=2] (2 pages) and [5] (1 page). + let bitmap = [false, true, true, false, false, true]; + + for &page_size in &[4096u64, 65536u64] { + let gpa_base = 0x1000_0000; + let (ranges, set_pages) = coalesce_pages_to_ranges(gpa_base, &bitmap, page_size); + + assert_eq!(set_pages, 3, "page_size={page_size}: wrong set-page count"); + + let got: Vec<(u64, u64)> = ranges.iter().map(|r| (r.gpa, r.length)).collect(); + assert_eq!( + got, + vec![ + (gpa_base + page_size, 2 * page_size), + (gpa_base + 5 * page_size, page_size), + ], + "page_size={page_size}: ranges must scale with page size" + ); + } + } + + #[test] + fn test_coalesce_pages_to_ranges_all_and_none() { + for &page_size in &[4096u64, 65536u64] { + // All clean -> no ranges. + let (ranges, set) = coalesce_pages_to_ranges(0, &[false; 4], page_size); + assert!(ranges.is_empty()); + assert_eq!(set, 0); + + // All dirty -> single coalesced range spanning every page. + let (ranges, set) = coalesce_pages_to_ranges(0, &[true; 4], page_size); + assert_eq!(set, 4); + let got: Vec<(u64, u64)> = ranges.iter().map(|r| (r.gpa, r.length)).collect(); + assert_eq!(got, vec![(0, 4 * page_size)]); + } + } + + /// MAP_PRIVATE: skip unread/read-only file pages; save CoW writes. + #[test] + fn test_get_anon_pages_map_private_file() { + let fixture = MapPrivateCowFixture::new(); + let (bitmap, _) = scan_pagemap_cow_anon(fixture.host_addr(), fixture.length()) + .expect("scan_pagemap_cow_anon"); + fixture.assert_expected_bitmap(&bitmap); + } + + struct MapPrivateCowFixture { + ptr: *mut libc::c_void, + len: usize, + num_pages: usize, + } + + impl MapPrivateCowFixture { + fn new() -> Self { + use std::io::Write; + use std::os::unix::io::AsRawFd; + + let page_size = host_page_size() as usize; + const NUM_PAGES: usize = 8; + let len = page_size * NUM_PAGES; + + let path = std::env::temp_dir().join(format!( + "cube_pagemap_anon_map_private_{}_{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + + let mut file = std::fs::OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .open(&path) + .expect("create temp file for MAP_PRIVATE fixture"); + let mut contents = vec![0u8; len]; + for page in 0..NUM_PAGES { + contents[page * page_size] = 0xA0 + page as u8; + } + file.write_all(&contents) + .expect("write pattern into temp file"); + let _ = std::fs::remove_file(&path); + + let ptr = unsafe { + libc::mmap( + std::ptr::null_mut(), + len, + libc::PROT_READ | libc::PROT_WRITE, + libc::MAP_PRIVATE, + file.as_raw_fd(), + 0, + ) + }; + assert_ne!( + ptr, + libc::MAP_FAILED, + "mmap MAP_PRIVATE failed: {}", + std::io::Error::last_os_error() + ); + + let base = ptr as *mut u8; + unsafe { + let _ = std::ptr::read_volatile(base.add(page_size)); + std::ptr::write_volatile(base.add(2 * page_size), 0xc2); + std::ptr::write_volatile(base.add(4 * page_size), 0xc4); + } + + Self { + ptr, + len, + num_pages: NUM_PAGES, + } + } + + fn host_addr(&self) -> u64 { + self.ptr as u64 + } + + fn length(&self) -> u64 { + self.len as u64 + } + + fn assert_expected_bitmap(&self, bitmap: &[bool]) { + assert_eq!(bitmap.len(), self.num_pages); + assert!(!bitmap[0], "untouched page must not be saved"); + assert!(!bitmap[1], "read-only file page must not be saved"); + assert!(bitmap[2], "written CoW page must be saved"); + assert!(!bitmap[3], "untouched page must not be saved"); + assert!(bitmap[4], "written CoW page must be saved"); + for i in 5..self.num_pages { + assert!(!bitmap[i], "untouched page {i} must not be saved"); + } + } + } + + impl Drop for MapPrivateCowFixture { + fn drop(&mut self) { + unsafe { + libc::munmap(self.ptr, self.len); + } + } + } +} diff --git a/vendor/cube-hypervisor/vmm/src/pagemap_anon_bench.rs b/vendor/cube-hypervisor/vmm/src/pagemap_anon_bench.rs new file mode 100644 index 0000000..1f9f993 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/pagemap_anon_bench.rs @@ -0,0 +1,178 @@ +// Copyright © 2026 Tencent Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +//! Micro-benchmark for the pagemap bit-61 CoW scan. +//! +//! Kept separate from `pagemap_anon.rs` so the production implementation and +//! its ordinary unit tests are not obscured by benchmark-only scaffolding. +//! +//! Ignored by default: it allocates up to `CUBE_PAGEMAP_BENCH_MIB` bytes of +//! file-backed memory. Run with `--ignored`. Optional env: +//! `CUBE_PAGEMAP_BENCH_MIB` (comma-separated sizes, default `64,256,1024`) and +//! `CUBE_PAGEMAP_BENCH_ITERS` (default `7`). + +use super::*; +use std::hint::black_box; +use std::io::Write; +use std::os::unix::io::AsRawFd; +use std::time::{Instant, SystemTime, UNIX_EPOCH}; + +struct PrivateFileMapping { + ptr: *mut libc::c_void, + length: usize, + /// Page indices the fixture CoW-wrote, i.e. the pages a snapshot must save. + cow_pages: Vec, +} + +/// One in every `COW_STRIDE` pages is written by the fixture. +const COW_STRIDE: usize = 10; + +impl PrivateFileMapping { + fn new(size_mib: u64, page_size: u64) -> Self { + let length = size_mib * 1024 * 1024; + let path = std::env::temp_dir().join(format!( + "cube_pagemap_bench_{}_{}", + std::process::id(), + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + let mut file = std::fs::OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .open(&path) + .expect("create benchmark backing file"); + + let pattern = vec![0x5a; 1024 * 1024]; + for _ in 0..size_mib { + file.write_all(&pattern) + .expect("populate benchmark backing file"); + } + + let ptr = unsafe { + libc::mmap( + std::ptr::null_mut(), + length as usize, + libc::PROT_READ | libc::PROT_WRITE, + libc::MAP_PRIVATE, + file.as_raw_fd(), + 0, + ) + }; + assert_ne!(ptr, libc::MAP_FAILED, "MAP_PRIVATE fixture failed"); + std::fs::remove_file(path).expect("unlink benchmark backing file"); + + // Restore-like workload: read every file page, then CoW-write 10%. + let base = ptr as *mut u8; + let pages = length / page_size; + for page in 0..pages { + unsafe { + black_box(std::ptr::read_volatile( + base.add((page * page_size) as usize), + )); + } + } + let cow_pages: Vec = (0..pages as usize).step_by(COW_STRIDE).collect(); + for &page in &cow_pages { + unsafe { + std::ptr::write_volatile(base.add(page * page_size as usize), 0xc2); + } + } + + Self { + ptr, + length: length as usize, + cow_pages, + } + } +} + +impl Drop for PrivateFileMapping { + fn drop(&mut self) { + unsafe { + libc::munmap(self.ptr, self.length); + } + } +} + +fn median_ns(mut samples: Vec) -> u128 { + samples.sort_unstable(); + samples[samples.len() / 2] +} + +fn measure(iterations: usize, mut scan: impl FnMut() -> Vec) -> u128 { + black_box(scan()); // warmup + median_ns( + (0..iterations) + .map(|_| { + let start = Instant::now(); + black_box(scan()); + start.elapsed().as_nanos() + }) + .collect(), + ) +} + +fn env_values(name: &str, default: &str) -> Vec { + std::env::var(name) + .unwrap_or_else(|_| default.to_string()) + .split(',') + .map(|value| value.trim().parse().expect("invalid benchmark setting")) + .collect() +} + +/// Measure the bit-61 CoW scan on a restore-like `MAP_PRIVATE` mapping. +#[test] +#[ignore = "allocates up to 1 GiB of file-backed memory; cargo test -- --ignored (CUBE_PAGEMAP_BENCH_MIB / CUBE_PAGEMAP_BENCH_ITERS)"] +fn benchmark_get_anon_pages() { + let page_size = host_page_size(); + let iterations = env_values("CUBE_PAGEMAP_BENCH_ITERS", "7")[0] as usize; + println!("pagemap_anon micro-benchmark: page_size={page_size}, iterations={iterations}"); + println!( + "{:<8} {:>10} {:>12} {:>12} {:>10} {:>10} {:>10}", + "MiB", "pages", "ms", "ns/page", "cow_pages", "saved", "saved_pct" + ); + + for size_mib in env_values("CUBE_PAGEMAP_BENCH_MIB", "64,256,1024") { + let mapping = PrivateFileMapping::new(size_mib, page_size); + let host_addr = mapping.ptr as u64; + let length = mapping.length as u64; + let pages = (length / page_size) as usize; + + let ns = measure(iterations, || { + scan_pagemap_cow_anon(host_addr, length) + .expect("pagemap scan failed") + .0 + }); + + let (bitmap, _swapped) = + scan_pagemap_cow_anon(host_addr, length).expect("pagemap classification failed"); + let saved = bitmap.iter().filter(|&&save| save).count(); + + // The correctness-critical direction: every page the Guest actually + // wrote (CoW) must be classified as "save". Read-only file pages are + // expected to be skipped; on a host with file-backed THP and a kernel + // predating `3f9f022e` they may be saved as well, which only costs + // savings and is therefore reported rather than asserted. + for &page in &mapping.cow_pages { + assert!( + bitmap[page], + "CoW-written page {page} was not selected for saving" + ); + } + + println!( + "{:<8} {:>10} {:>12.3} {:>12.1} {:>10} {:>10} {:>9.1}%", + size_mib, + pages, + ns as f64 / 1_000_000.0, + ns as f64 / pages as f64, + mapping.cow_pages.len(), + saved, + (saved as f64 / pages as f64) * 100.0 + ); + } +} diff --git a/vendor/cube-hypervisor/vmm/src/pci_segment.rs b/vendor/cube-hypervisor/vmm/src/pci_segment.rs new file mode 100644 index 0000000..0f988f1 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/pci_segment.rs @@ -0,0 +1,426 @@ +// Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 - 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause +// + +use crate::device_manager::{AddressManager, DeviceManagerError, DeviceManagerResult}; +use acpi_tables::aml::{self, Aml}; +use arch::layout; +use pci::{DeviceRelocation, PciBdf, PciBus, PciConfigMmio, PciRoot}; +#[cfg(target_arch = "x86_64")] +use pci::{PciConfigIo, PCI_CONFIG_IO_PORT, PCI_CONFIG_IO_PORT_SIZE}; +use std::sync::{Arc, Mutex}; +use uuid::Uuid; +use vm_allocator::AddressAllocator; +use vm_device::BusDevice; + +pub(crate) struct PciSegment { + pub(crate) id: u16, + pub(crate) pci_bus: Arc>, + pub(crate) pci_config_mmio: Arc>, + pub(crate) mmio_config_address: u64, + + #[cfg(target_arch = "x86_64")] + pub(crate) pci_config_io: Option>>, + + // Bitmap of PCI devices to hotplug. + pub(crate) pci_devices_up: u32, + // Bitmap of PCI devices to hotunplug. + pub(crate) pci_devices_down: u32, + // List of allocated IRQs for each PCI slot. + pub(crate) pci_irq_slots: [u8; 32], + + // Device memory covered by this segment + pub(crate) start_of_device_area: u64, + pub(crate) end_of_device_area: u64, + + pub(crate) allocator: Arc>, +} + +impl PciSegment { + pub(crate) fn new( + id: u16, + address_manager: &Arc, + allocator: Arc>, + pci_irq_slots: &[u8; 32], + ) -> DeviceManagerResult { + let pci_root = PciRoot::new(None); + let pci_bus = Arc::new(Mutex::new(PciBus::new( + pci_root, + Arc::clone(address_manager) as Arc, + ))); + + let pci_config_mmio = Arc::new(Mutex::new(PciConfigMmio::new(Arc::clone(&pci_bus)))); + let mmio_config_address = + layout::PCI_MMCONFIG_START.0 + layout::PCI_MMIO_CONFIG_SIZE_PER_SEGMENT * id as u64; + + address_manager + .mmio_bus + .insert( + Arc::clone(&pci_config_mmio) as Arc>, + mmio_config_address, + layout::PCI_MMIO_CONFIG_SIZE_PER_SEGMENT, + ) + .map_err(DeviceManagerError::BusError)?; + + let start_of_device_area = allocator.lock().unwrap().base().0; + let end_of_device_area = allocator.lock().unwrap().end().0; + + let segment = PciSegment { + id, + pci_bus, + pci_config_mmio, + mmio_config_address, + pci_devices_up: 0, + pci_devices_down: 0, + #[cfg(target_arch = "x86_64")] + pci_config_io: None, + allocator, + start_of_device_area, + end_of_device_area, + pci_irq_slots: *pci_irq_slots, + }; + + info!( + "Adding PCI segment: id={}, PCI MMIO config address: 0x{:x}, device area [0x{:x}-0x{:x}", + segment.id, segment.mmio_config_address, segment.start_of_device_area, segment.end_of_device_area + ); + Ok(segment) + } + + #[cfg(target_arch = "x86_64")] + pub(crate) fn new_default_segment( + address_manager: &Arc, + allocator: Arc>, + pci_irq_slots: &[u8; 32], + ) -> DeviceManagerResult { + let mut segment = Self::new(0, address_manager, allocator, pci_irq_slots)?; + let pci_config_io = Arc::new(Mutex::new(PciConfigIo::new(Arc::clone(&segment.pci_bus)))); + + address_manager + .io_bus + .insert( + pci_config_io.clone(), + PCI_CONFIG_IO_PORT, + PCI_CONFIG_IO_PORT_SIZE, + ) + .map_err(DeviceManagerError::BusError)?; + + segment.pci_config_io = Some(pci_config_io); + + Ok(segment) + } + + #[cfg(target_arch = "aarch64")] + pub(crate) fn new_default_segment( + address_manager: &Arc, + allocator: Arc>, + pci_irq_slots: &[u8; 32], + ) -> DeviceManagerResult { + Self::new(0, address_manager, allocator, pci_irq_slots) + } + + pub(crate) fn next_device_bdf(&self) -> DeviceManagerResult { + Ok(PciBdf::new( + self.id, + 0, + self.pci_bus + .lock() + .unwrap() + .next_device_id() + .map_err(DeviceManagerError::NextPciDeviceId)? as u8, + 0, + )) + } + + pub fn reserve_legacy_interrupts_for_pci_devices( + address_manager: &Arc, + pci_irq_slots: &mut [u8; 32], + ) -> DeviceManagerResult<()> { + // Reserve 8 IRQs which will be shared across all PCI devices. + let num_irqs = 8; + let mut irqs: Vec = Vec::new(); + for _ in 0..num_irqs { + irqs.push( + address_manager + .allocator + .lock() + .unwrap() + .allocate_irq() + .ok_or(DeviceManagerError::AllocateIrq)? as u8, + ); + } + + // There are 32 devices on the PCI bus, let's assign them an IRQ. + for i in 0..32 { + pci_irq_slots[i] = irqs[i % num_irqs]; + } + + Ok(()) + } +} + +struct PciDevSlot { + device_id: u8, +} + +impl Aml for PciDevSlot { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let sun = self.device_id; + let adr: u32 = (self.device_id as u32) << 16; + aml::Device::new( + format!("S{:03}", self.device_id).as_str().into(), + vec![ + &aml::Name::new("_SUN".into(), &sun), + &aml::Name::new("_ADR".into(), &adr), + &aml::Method::new( + "_EJ0".into(), + 1, + true, + vec![&aml::MethodCall::new( + "\\_SB_.PHPR.PCEJ".into(), + vec![&aml::Path::new("_SUN"), &aml::Path::new("_SEG")], + )], + ), + ], + ) + .append_aml_bytes(bytes) + } +} + +struct PciDevSlotNotify { + device_id: u8, +} + +impl Aml for PciDevSlotNotify { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let device_id_mask: u32 = 1 << self.device_id; + let object = aml::Path::new(&format!("S{:03}", self.device_id)); + aml::And::new(&aml::Local(0), &aml::Arg(0), &device_id_mask).append_aml_bytes(bytes); + aml::If::new( + &aml::Equal::new(&aml::Local(0), &device_id_mask), + vec![&aml::Notify::new(&object, &aml::Arg(1))], + ) + .append_aml_bytes(bytes); + } +} + +struct PciDevSlotMethods {} + +impl Aml for PciDevSlotMethods { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut device_notifies = Vec::new(); + for device_id in 0..32 { + device_notifies.push(PciDevSlotNotify { device_id }); + } + + let mut device_notifies_refs: Vec<&dyn aml::Aml> = Vec::new(); + for device_notify in device_notifies.iter() { + device_notifies_refs.push(device_notify); + } + + aml::Method::new("DVNT".into(), 2, true, device_notifies_refs).append_aml_bytes(bytes); + aml::Method::new( + "PCNT".into(), + 0, + true, + vec![ + &aml::Acquire::new("\\_SB_.PHPR.BLCK".into(), 0xffff), + &aml::Store::new(&aml::Path::new("\\_SB_.PHPR.PSEG"), &aml::Path::new("_SEG")), + &aml::MethodCall::new( + "DVNT".into(), + vec![&aml::Path::new("\\_SB_.PHPR.PCIU"), &aml::ONE], + ), + &aml::MethodCall::new( + "DVNT".into(), + vec![&aml::Path::new("\\_SB_.PHPR.PCID"), &3usize], + ), + &aml::Release::new("\\_SB_.PHPR.BLCK".into()), + ], + ) + .append_aml_bytes(bytes) + } +} + +struct PciDsmMethod {} + +impl Aml for PciDsmMethod { + fn append_aml_bytes(&self, bytes: &mut Vec) { + // Refer to ACPI spec v6.3 Ch 9.1.1 and PCI Firmware spec v3.3 Ch 4.6.1 + // _DSM (Device Specific Method), the following is the implementation in ASL. + /* + Method (_DSM, 4, NotSerialized) // _DSM: Device-Specific Method + { + If ((Arg0 == ToUUID ("e5c937d0-3553-4d7a-9117-ea4d19c3434d") /* Device Labeling Interface */)) + { + If ((Arg2 == Zero)) + { + Return (Buffer (One) { 0x21 }) + } + If ((Arg2 == 0x05)) + { + Return (Zero) + } + } + + Return (Buffer (One) { 0x00 }) + } + */ + /* + * As per ACPI v6.3 Ch 19.6.142, the UUID is required to be in mixed endian: + * Among the fields of a UUID: + * {d1 (8 digits)} - {d2 (4 digits)} - {d3 (4 digits)} - {d4 (16 digits)} + * d1 ~ d3 need to be little endian, d4 be big endian. + * See https://en.wikipedia.org/wiki/Universally_unique_identifier#Encoding . + */ + let uuid = Uuid::parse_str("E5C937D0-3553-4D7A-9117-EA4D19C3434D").unwrap(); + let (uuid_d1, uuid_d2, uuid_d3, uuid_d4) = uuid.as_fields(); + let mut uuid_buf = vec![]; + uuid_buf.extend(uuid_d1.to_le_bytes()); + uuid_buf.extend(uuid_d2.to_le_bytes()); + uuid_buf.extend(uuid_d3.to_le_bytes()); + uuid_buf.extend(uuid_d4); + aml::Method::new( + "_DSM".into(), + 4, + false, + vec![ + &aml::If::new( + &aml::Equal::new(&aml::Arg(0), &aml::Buffer::new(uuid_buf)), + vec![ + &aml::If::new( + &aml::Equal::new(&aml::Arg(2), &aml::ZERO), + vec![&aml::Return::new(&aml::Buffer::new(vec![0x21]))], + ), + &aml::If::new( + &aml::Equal::new(&aml::Arg(2), &0x05u8), + vec![&aml::Return::new(&aml::ZERO)], + ), + ], + ), + &aml::Return::new(&aml::Buffer::new(vec![0])), + ], + ) + .append_aml_bytes(bytes) + } +} + +impl Aml for PciSegment { + fn append_aml_bytes(&self, bytes: &mut Vec) { + let mut pci_dsdt_inner_data: Vec<&dyn aml::Aml> = Vec::new(); + let hid = aml::Name::new("_HID".into(), &aml::EisaName::new("PNP0A08")); + pci_dsdt_inner_data.push(&hid); + let cid = aml::Name::new("_CID".into(), &aml::EisaName::new("PNP0A03")); + pci_dsdt_inner_data.push(&cid); + let adr = aml::Name::new("_ADR".into(), &aml::ZERO); + pci_dsdt_inner_data.push(&adr); + let seg = aml::Name::new("_SEG".into(), &self.id); + pci_dsdt_inner_data.push(&seg); + let uid = aml::Name::new("_UID".into(), &aml::ZERO); + pci_dsdt_inner_data.push(&uid); + let cca = aml::Name::new("_CCA".into(), &aml::ONE); + pci_dsdt_inner_data.push(&cca); + let supp = aml::Name::new("SUPP".into(), &aml::ZERO); + pci_dsdt_inner_data.push(&supp); + + // Since Cloud Hypervisor supports only one PCI bus, it can be tied + // to the NUMA node 0. It's up to the user to organize the NUMA nodes + // so that the PCI bus relates to the expected vCPUs and guest RAM. + let proximity_domain = 0u32; + let pxm_return = aml::Return::new(&proximity_domain); + let pxm = aml::Method::new("_PXM".into(), 0, false, vec![&pxm_return]); + pci_dsdt_inner_data.push(&pxm); + + let pci_dsm = PciDsmMethod {}; + pci_dsdt_inner_data.push(&pci_dsm); + + let crs = if self.id == 0 { + aml::Name::new( + "_CRS".into(), + &aml::ResourceTemplate::new(vec![ + &aml::AddressSpace::new_bus_number(0x0u16, 0x0u16), + #[cfg(target_arch = "x86_64")] + &aml::Io::new(0xcf8, 0xcf8, 1, 0x8), + &aml::AddressSpace::new_memory( + aml::AddressSpaceCachable::NotCacheable, + true, + layout::MEM_32BIT_DEVICES_START.0 as u32, + (layout::MEM_32BIT_DEVICES_START.0 + layout::MEM_32BIT_DEVICES_SIZE - 1) + as u32, + ), + &aml::AddressSpace::new_memory( + aml::AddressSpaceCachable::NotCacheable, + true, + self.start_of_device_area, + self.end_of_device_area, + ), + #[cfg(target_arch = "x86_64")] + &aml::AddressSpace::new_io(0u16, 0x0cf7u16), + #[cfg(target_arch = "x86_64")] + &aml::AddressSpace::new_io(0x0d00u16, 0xffffu16), + ]), + ) + } else { + aml::Name::new( + "_CRS".into(), + &aml::ResourceTemplate::new(vec![ + &aml::AddressSpace::new_bus_number(0x0u16, 0x0u16), + &aml::Memory32Fixed::new( + true, + self.mmio_config_address as u32, + layout::PCI_MMIO_CONFIG_SIZE_PER_SEGMENT as u32, + ), + &aml::AddressSpace::new_memory( + aml::AddressSpaceCachable::NotCacheable, + true, + self.start_of_device_area, + self.end_of_device_area, + ), + ]), + ) + }; + pci_dsdt_inner_data.push(&crs); + + let mut pci_devices = Vec::new(); + for device_id in 0..32 { + let pci_device = PciDevSlot { device_id }; + pci_devices.push(pci_device); + } + for pci_device in pci_devices.iter() { + pci_dsdt_inner_data.push(pci_device); + } + + let pci_device_methods = PciDevSlotMethods {}; + pci_dsdt_inner_data.push(&pci_device_methods); + + // Build PCI routing table, listing IRQs assigned to PCI devices. + let prt_package_list: Vec<(u32, u32)> = self + .pci_irq_slots + .iter() + .enumerate() + .map(|(i, irq)| (((((i as u32) & 0x1fu32) << 16) | 0xffffu32), *irq as u32)) + .collect(); + let prt_package_list: Vec = prt_package_list + .iter() + .map(|(bdf, irq)| aml::Package::new(vec![bdf, &0u8, &0u8, irq])) + .collect(); + let prt_package_list: Vec<&dyn Aml> = prt_package_list + .iter() + .map(|item| item as &dyn Aml) + .collect(); + let prt = aml::Name::new("_PRT".into(), &aml::Package::new(prt_package_list)); + pci_dsdt_inner_data.push(&prt); + + aml::Device::new( + format!("_SB_.PCI{:X}", self.id).as_str().into(), + pci_dsdt_inner_data, + ) + .append_aml_bytes(bytes) + } +} diff --git a/vendor/cube-hypervisor/vmm/src/seccomp_filters.rs b/vendor/cube-hypervisor/vmm/src/seccomp_filters.rs new file mode 100644 index 0000000..977f23c --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/seccomp_filters.rs @@ -0,0 +1,1004 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use std::sync::RwLock; + +use hypervisor::HypervisorType; +use lazy_static::lazy_static; +use seccompiler::{ + BackendError, BpfProgram, Error, SeccompAction, SeccompCmpArgLen as ArgLen, SeccompCmpOp::Eq, + SeccompCondition as Cond, SeccompFilter, SeccompRule, +}; +use std::convert::TryInto; + +lazy_static! { + static ref RUTIME_RULES: RwLock)>> = RwLock::new(Vec::new()); +} + +pub enum Thread { + Api, + SignalHandler, + Vcpu, + Vmm, + PtyForeground, + All, +} + +/// Shorthand for chaining `SeccompCondition`s with the `and` operator in a `SeccompRule`. +/// The rule will take the `Allow` action if _all_ the conditions are true. +/// +/// [`SeccompCondition`]: struct.SeccompCondition.html +/// [`SeccompRule`]: struct.SeccompRule.html +macro_rules! and { + ($($x:expr),*) => (SeccompRule::new(vec![$($x),*]).unwrap()) +} + +/// Shorthand for chaining `SeccompRule`s with the `or` operator in a `SeccompFilter`. +/// +/// [`SeccompFilter`]: struct.SeccompFilter.html +/// [`SeccompRule`]: struct.SeccompRule.html +macro_rules! or { + ($($x:expr,)*) => (vec![$($x),*]); + ($($x:expr),*) => (vec![$($x),*]) +} + +// See include/uapi/asm-generic/ioctls.h in the kernel code. +const TCGETS: u64 = 0x5401; +const TCSETS: u64 = 0x5402; +const TIOCSCTTY: u64 = 0x540E; +const TIOCSPGRP: u64 = 0x5410; +const TIOCGWINSZ: u64 = 0x5413; +const TIOCSPTLCK: u64 = 0x4004_5431; +const TIOCGTPEER: u64 = 0x5441; +const FIOCLEX: u64 = 0x5451; +const FIONBIO: u64 = 0x5421; + +// See include/uapi/linux/fs.h in the kernel code. +const BLKSSZGET: u64 = 0x1268; +const BLKPBSZGET: u64 = 0x127b; +const BLKIOMIN: u64 = 0x1278; +const BLKIOOPT: u64 = 0x1279; + +// See include/uapi/linux/if_tun.h in the kernel code. +const TUNGETIFF: u64 = 0x8004_54d2; +const TUNSETIFF: u64 = 0x4004_54ca; +const TUNSETOFFLOAD: u64 = 0x4004_54d0; +const TUNSETVNETHDRSZ: u64 = 0x4004_54d8; +const TUNGETFEATURES: u64 = 0x8004_54cf; + +// See include/uapi/linux/sockios.h in the kernel code. +const SIOCGIFFLAGS: u64 = 0x8913; +const SIOCGIFHWADDR: u64 = 0x8927; +const SIOCSIFFLAGS: u64 = 0x8914; +const SIOCSIFADDR: u64 = 0x8916; +const SIOCGIFMTU: u64 = 0x8921; +const SIOCSIFMTU: u64 = 0x8922; +const SIOCSIFHWADDR: u64 = 0x8924; +const SIOCSIFNETMASK: u64 = 0x891c; + +// See include/uapi/linux/vfio.h in the kernel code. +const VFIO_GET_API_VERSION: u64 = 0x3b64; +const VFIO_CHECK_EXTENSION: u64 = 0x3b65; +const VFIO_SET_IOMMU: u64 = 0x3b66; +const VFIO_GROUP_GET_STATUS: u64 = 0x3b67; +const VFIO_GROUP_SET_CONTAINER: u64 = 0x3b68; +const VFIO_GROUP_UNSET_CONTAINER: u64 = 0x3b69; +const VFIO_GROUP_GET_DEVICE_FD: u64 = 0x3b6a; +const VFIO_DEVICE_GET_INFO: u64 = 0x3b6b; +const VFIO_DEVICE_GET_REGION_INFO: u64 = 0x3b6c; +const VFIO_DEVICE_GET_IRQ_INFO: u64 = 0x3b6d; +const VFIO_DEVICE_SET_IRQS: u64 = 0x3b6e; +const VFIO_DEVICE_RESET: u64 = 0x3b6f; +const VFIO_IOMMU_MAP_DMA: u64 = 0x3b71; +const VFIO_IOMMU_UNMAP_DMA: u64 = 0x3b72; +const VFIO_DEVICE_IOEVENTFD: u64 = 0x3b74; + +// See include/uapi/linux/vhost.h in the kernel code +const VHOST_GET_FEATURES: u64 = 0x8008af00; +const VHOST_SET_FEATURES: u64 = 0x4008af00; +const VHOST_SET_OWNER: u64 = 0xaf01; +const VHOST_SET_VRING_NUM: u64 = 0x4008af10; +const VHOST_SET_VRING_ADDR: u64 = 0x4028af11; +const VHOST_SET_VRING_BASE: u64 = 0x4008af12; +const VHOST_SET_VRING_KICK: u64 = 0x4008af20; +const VHOST_SET_VRING_CALL: u64 = 0x4008af21; +const VHOST_SET_BACKEND_FEATURES: u64 = 0x4008af25; +const VHOST_GET_BACKEND_FEATURES: u64 = 0x8008af26; +const VHOST_VDPA_GET_DEVICE_ID: u64 = 0x8004af70; +const VHOST_VDPA_GET_STATUS: u64 = 0x8001af71; +const VHOST_VDPA_SET_STATUS: u64 = 0x4001af72; +const VHOST_VDPA_GET_CONFIG: u64 = 0x8008af73; +const VHOST_VDPA_SET_CONFIG: u64 = 0x4008af74; +const VHOST_VDPA_SET_VRING_ENABLE: u64 = 0x4008af75; +const VHOST_VDPA_GET_VRING_NUM: u64 = 0x8002af76; +const VHOST_VDPA_SET_CONFIG_CALL: u64 = 0x4004af77; +const VHOST_VDPA_GET_IOVA_RANGE: u64 = 0x8010af78; +const VHOST_VDPA_GET_CONFIG_SIZE: u64 = 0x8004af79; +const VHOST_VDPA_SUSPEND: u64 = 0xaf7d; + +// See include/uapi/linux/kvm.h in the kernel code. +#[cfg(feature = "kvm")] +mod kvm { + pub const KVM_GET_API_VERSION: u64 = 0xae00; + pub const KVM_CREATE_VM: u64 = 0xae01; + pub const KVM_CHECK_EXTENSION: u64 = 0xae03; + pub const KVM_GET_VCPU_MMAP_SIZE: u64 = 0xae04; + pub const KVM_CREATE_VCPU: u64 = 0xae41; + pub const KVM_CREATE_IRQCHIP: u64 = 0xae60; + pub const KVM_RUN: u64 = 0xae80; + pub const KVM_SET_MP_STATE: u64 = 0x4004_ae99; + pub const KVM_SET_GSI_ROUTING: u64 = 0x4008_ae6a; + pub const KVM_SET_DEVICE_ATTR: u64 = 0x4018_aee1; + pub const KVM_HAS_DEVICE_ATTR: u64 = 0x4018_aee3; + pub const KVM_SET_ONE_REG: u64 = 0x4010_aeac; + pub const KVM_SET_USER_MEMORY_REGION: u64 = 0x4020_ae46; + pub const KVM_IRQFD: u64 = 0x4020_ae76; + pub const KVM_IOEVENTFD: u64 = 0x4040_ae79; + pub const KVM_SET_VCPU_EVENTS: u64 = 0x4040_aea0; + pub const KVM_ENABLE_CAP: u64 = 0x4068_aea3; + pub const KVM_SET_REGS: u64 = 0x4090_ae82; + pub const KVM_GET_MP_STATE: u64 = 0x8004_ae98; + pub const KVM_GET_DEVICE_ATTR: u64 = 0x4018_aee2; + pub const KVM_GET_DIRTY_LOG: u64 = 0x4010_ae42; + pub const KVM_GET_VCPU_EVENTS: u64 = 0x8040_ae9f; + pub const KVM_GET_ONE_REG: u64 = 0x4010_aeab; + pub const KVM_GET_REGS: u64 = 0x8090_ae81; + pub const KVM_GET_SUPPORTED_CPUID: u64 = 0xc008_ae05; + pub const KVM_CREATE_DEVICE: u64 = 0xc00c_aee0; + pub const KVM_GET_REG_LIST: u64 = 0xc008_aeb0; + pub const KVM_MEMORY_ENCRYPT_OP: u64 = 0xc008_aeba; +} + +#[cfg(feature = "kvm")] +use kvm::*; + +// MSHV IOCTL code. This is unstable until the kernel code has been declared stable. +#[cfg(feature = "mshv")] +mod mshv { + pub const MSHV_GET_API_VERSION: u64 = 0xb800; + pub const MSHV_CREATE_VM: u64 = 0x4028_b801; + pub const MSHV_MAP_GUEST_MEMORY: u64 = 0x4020_b802; + pub const MSHV_UNMAP_GUEST_MEMORY: u64 = 0x4020_b803; + pub const MSHV_CREATE_VP: u64 = 0x4004_b804; + pub const MSHV_IRQFD: u64 = 0x4010_b80e; + pub const MSHV_IOEVENTFD: u64 = 0x4020_b80f; + pub const MSHV_SET_MSI_ROUTING: u64 = 0x4008_b811; + pub const MSHV_GET_VP_REGISTERS: u64 = 0xc010_b805; + pub const MSHV_SET_VP_REGISTERS: u64 = 0x4010_b806; + pub const MSHV_RUN_VP: u64 = 0x8100_b807; + pub const MSHV_GET_VP_STATE: u64 = 0xc028_b80a; + pub const MSHV_SET_VP_STATE: u64 = 0xc028_b80b; + pub const MSHV_SET_PARTITION_PROPERTY: u64 = 0x4010_b80c; + pub const MSHV_GET_GPA_ACCESS_STATES: u64 = 0xc01c_b812; + pub const MSHV_VP_TRANSLATE_GVA: u64 = 0xc020_b80e; + pub const MSHV_CREATE_PARTITION: u64 = 0x4030_b801; +} +#[cfg(feature = "mshv")] +use mshv::*; + +#[cfg(feature = "mshv")] +fn create_vmm_ioctl_seccomp_rule_common_mshv() -> Result, BackendError> { + Ok(or![ + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_GET_API_VERSION,)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_CREATE_VM)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_MAP_GUEST_MEMORY)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_UNMAP_GUEST_MEMORY)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_CREATE_VP)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_IRQFD)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_IOEVENTFD)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_SET_MSI_ROUTING)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_GET_VP_REGISTERS)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_SET_VP_REGISTERS)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_RUN_VP)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_GET_VP_STATE)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_SET_VP_STATE)?], + and![Cond::new( + 1, + ArgLen::Dword, + Eq, + MSHV_SET_PARTITION_PROPERTY + )?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_GET_GPA_ACCESS_STATES)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_VP_TRANSLATE_GVA)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_CREATE_PARTITION)?], + ]) +} + +#[cfg(feature = "kvm")] +fn create_vmm_ioctl_seccomp_rule_common_kvm() -> Result, BackendError> { + Ok(or![ + and![Cond::new(1, ArgLen::Dword, Eq, KVM_CHECK_EXTENSION)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_CREATE_DEVICE,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_CREATE_IRQCHIP,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_CREATE_VCPU)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_CREATE_VM)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_ENABLE_CAP)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_API_VERSION,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_DEVICE_ATTR,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_DIRTY_LOG)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_MP_STATE)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_ONE_REG)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_REGS)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_REG_LIST)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_SUPPORTED_CPUID,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_VCPU_EVENTS,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_VCPU_MMAP_SIZE,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_IOEVENTFD)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_IRQFD)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_RUN)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_MEMORY_ENCRYPT_OP)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_DEVICE_ATTR,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_HAS_DEVICE_ATTR,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_GSI_ROUTING)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_MP_STATE)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_ONE_REG)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_REGS)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_USER_MEMORY_REGION,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_VCPU_EVENTS,)?], + ]) +} + +fn create_vmm_ioctl_seccomp_rule_hypervisor( + hypervisor_type: HypervisorType, +) -> Result, BackendError> { + match hypervisor_type { + #[cfg(feature = "kvm")] + HypervisorType::Kvm | HypervisorType::KvmPvm => create_vmm_ioctl_seccomp_rule_common_kvm(), + #[cfg(feature = "mshv")] + HypervisorType::Mshv => create_vmm_ioctl_seccomp_rule_common_mshv(), + #[allow(unreachable_patterns)] + _ => panic!("Invalid hypervisor {:?}", hypervisor_type), + } +} + +fn create_vmm_ioctl_seccomp_rule_common( + hypervisor_type: HypervisorType, +) -> Result, BackendError> { + let mut common_rules = or![ + and![Cond::new(1, ArgLen::Dword, Eq, BLKSSZGET)?], + and![Cond::new(1, ArgLen::Dword, Eq, BLKPBSZGET)?], + and![Cond::new(1, ArgLen::Dword, Eq, BLKIOMIN)?], + and![Cond::new(1, ArgLen::Dword, Eq, BLKIOOPT)?], + and![Cond::new(1, ArgLen::Dword, Eq, FIOCLEX)?], + and![Cond::new(1, ArgLen::Dword, Eq, FIONBIO)?], + and![Cond::new(1, ArgLen::Dword, Eq, SIOCGIFFLAGS)?], + and![Cond::new(1, ArgLen::Dword, Eq, SIOCGIFHWADDR)?], + and![Cond::new(1, ArgLen::Dword, Eq, SIOCGIFMTU)?], + and![Cond::new(1, ArgLen::Dword, Eq, SIOCSIFADDR)?], + and![Cond::new(1, ArgLen::Dword, Eq, SIOCSIFFLAGS)?], + and![Cond::new(1, ArgLen::Dword, Eq, SIOCSIFHWADDR)?], + and![Cond::new(1, ArgLen::Dword, Eq, SIOCSIFMTU)?], + and![Cond::new(1, ArgLen::Dword, Eq, SIOCSIFNETMASK)?], + and![Cond::new(1, ArgLen::Dword, Eq, TCSETS)?], + and![Cond::new(1, ArgLen::Dword, Eq, TCGETS)?], + and![Cond::new(1, ArgLen::Dword, Eq, TIOCGTPEER)?], + and![Cond::new(1, ArgLen::Dword, Eq, TIOCGWINSZ)?], + and![Cond::new(1, ArgLen::Dword, Eq, TIOCSCTTY)?], + and![Cond::new(1, ArgLen::Dword, Eq, TIOCSPGRP)?], + and![Cond::new(1, ArgLen::Dword, Eq, TIOCSPTLCK)?], + and![Cond::new(1, ArgLen::Dword, Eq, TUNGETFEATURES)?], + and![Cond::new(1, ArgLen::Dword, Eq, TUNGETIFF)?], + and![Cond::new(1, ArgLen::Dword, Eq, TUNSETIFF)?], + and![Cond::new(1, ArgLen::Dword, Eq, TUNSETOFFLOAD)?], + and![Cond::new(1, ArgLen::Dword, Eq, TUNSETVNETHDRSZ)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_GET_API_VERSION)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_CHECK_EXTENSION)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_SET_IOMMU)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_GROUP_GET_STATUS)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_GROUP_SET_CONTAINER)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_GROUP_UNSET_CONTAINER)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_GROUP_GET_DEVICE_FD)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_DEVICE_GET_INFO)?], + and![Cond::new( + 1, + ArgLen::Dword, + Eq, + VFIO_DEVICE_GET_REGION_INFO + )?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_DEVICE_GET_IRQ_INFO)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_DEVICE_SET_IRQS)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_DEVICE_RESET)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_IOMMU_MAP_DMA)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_IOMMU_UNMAP_DMA)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_DEVICE_IOEVENTFD)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_GET_FEATURES)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_SET_FEATURES)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_SET_OWNER)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_SET_VRING_NUM)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_SET_VRING_ADDR)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_SET_VRING_BASE)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_SET_VRING_KICK)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_SET_VRING_CALL)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_SET_BACKEND_FEATURES)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_GET_BACKEND_FEATURES)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_GET_DEVICE_ID)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_GET_STATUS)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_SET_STATUS)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_GET_CONFIG)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_SET_CONFIG)?], + and![Cond::new( + 1, + ArgLen::Dword, + Eq, + VHOST_VDPA_SET_VRING_ENABLE + )?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_GET_VRING_NUM)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_SET_CONFIG_CALL)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_GET_IOVA_RANGE)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_GET_CONFIG_SIZE)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_SUSPEND)?], + ]; + + let hypervisor_rules = create_vmm_ioctl_seccomp_rule_hypervisor(hypervisor_type)?; + + common_rules.extend(hypervisor_rules); + + Ok(common_rules) +} + +#[cfg(all(target_arch = "x86_64", feature = "kvm"))] +fn create_vmm_ioctl_seccomp_rule_kvm() -> Result, BackendError> { + const KVM_CREATE_PIT2: u64 = 0x4040_ae77; + const KVM_GET_CLOCK: u64 = 0x8030_ae7c; + const KVM_GET_CPUID2: u64 = 0xc008_ae91; + const KVM_GET_FPU: u64 = 0x81a0_ae8c; + const KVM_GET_LAPIC: u64 = 0x8400_ae8e; + const KVM_GET_MSR_INDEX_LIST: u64 = 0xc004_ae02; + const KVM_GET_MSRS: u64 = 0xc008_ae88; + const KVM_GET_SREGS: u64 = 0x8138_ae83; + const KVM_GET_XCRS: u64 = 0x8188_aea6; + const KVM_GET_XSAVE: u64 = 0x9000_aea4; + const KVM_KVMCLOCK_CTRL: u64 = 0xaead; + const KVM_SET_CLOCK: u64 = 0x4030_ae7b; + const KVM_SET_CPUID2: u64 = 0x4008_ae90; + const KVM_SET_FPU: u64 = 0x41a0_ae8d; + const KVM_SET_IDENTITY_MAP_ADDR: u64 = 0x4008_ae48; + const KVM_SET_LAPIC: u64 = 0x4400_ae8f; + const KVM_SET_MSRS: u64 = 0x4008_ae89; + const KVM_SET_SREGS: u64 = 0x4138_ae84; + const KVM_SET_TSS_ADDR: u64 = 0xae47; + const KVM_SET_XCRS: u64 = 0x4188_aea7; + const KVM_SET_XSAVE: u64 = 0x5000_aea5; + const KVM_SET_GUEST_DEBUG: u64 = 0x4048_ae9b; + const KVM_TRANSLATE: u64 = 0xc018_ae85; + + let common_rules = create_vmm_ioctl_seccomp_rule_common(HypervisorType::Kvm)?; + let mut arch_rules = or![ + and![Cond::new(1, ArgLen::Dword, Eq, KVM_CREATE_PIT2)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_CLOCK,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_CPUID2,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_FPU)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_LAPIC)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_MSR_INDEX_LIST)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_MSRS)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_SREGS)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_XCRS,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_GET_XSAVE,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_KVMCLOCK_CTRL)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_CLOCK)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_CPUID2)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_FPU)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_IDENTITY_MAP_ADDR)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_LAPIC)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_SREGS)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_TSS_ADDR,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_MSRS)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_XCRS,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_XSAVE,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_GUEST_DEBUG,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_TRANSLATE,)?], + ]; + arch_rules.extend(common_rules); + + Ok(arch_rules) +} + +#[cfg(all(target_arch = "aarch64", feature = "kvm"))] +fn create_vmm_ioctl_seccomp_rule_kvm() -> Result, BackendError> { + const KVM_ARM_PREFERRED_TARGET: u64 = 0x8020_aeaf; + const KVM_ARM_VCPU_INIT: u64 = 0x4020_aeae; + const KVM_SET_GUEST_DEBUG: u64 = 0x4208_ae9b; + + let common_rules = create_vmm_ioctl_seccomp_rule_common(HypervisorType::Kvm)?; + let mut arch_rules = or![ + and![Cond::new(1, ArgLen::Dword, Eq, KVM_ARM_PREFERRED_TARGET,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_ARM_VCPU_INIT,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_GUEST_DEBUG,)?], + ]; + arch_rules.extend(common_rules); + + Ok(arch_rules) +} + +#[cfg(all(target_arch = "x86_64", feature = "mshv"))] +fn create_vmm_ioctl_seccomp_rule_mshv() -> Result, BackendError> { + create_vmm_ioctl_seccomp_rule_common(HypervisorType::Mshv) +} + +fn create_vmm_ioctl_seccomp_rule( + hypervisor_type: HypervisorType, +) -> Result, BackendError> { + match hypervisor_type { + #[cfg(feature = "kvm")] + HypervisorType::Kvm | HypervisorType::KvmPvm => create_vmm_ioctl_seccomp_rule_kvm(), + #[cfg(feature = "mshv")] + HypervisorType::Mshv => create_vmm_ioctl_seccomp_rule_mshv(), + #[allow(unreachable_patterns)] + _ => panic!("Invalid hypervisor {:?}", hypervisor_type), + } +} + +fn create_api_ioctl_seccomp_rule() -> Result, BackendError> { + Ok(or![and![Cond::new(1, ArgLen::Dword, Eq, FIONBIO)?]]) +} + +fn create_signal_handler_ioctl_seccomp_rule() -> Result, BackendError> { + Ok(or![ + and![Cond::new(1, ArgLen::Dword, Eq, TCGETS)?], + and![Cond::new(1, ArgLen::Dword, Eq, TCSETS)?], + and![Cond::new(1, ArgLen::Dword, Eq, TIOCGWINSZ)?], + ]) +} + +fn signal_handler_thread_rules() -> Result)>, BackendError> { + Ok(vec![ + (libc::SYS_brk, vec![]), + (libc::SYS_close, vec![]), + (libc::SYS_exit, vec![]), + (libc::SYS_exit_group, vec![]), + (libc::SYS_futex, vec![]), + (libc::SYS_ioctl, create_signal_handler_ioctl_seccomp_rule()?), + (libc::SYS_madvise, vec![]), + (libc::SYS_mmap, vec![]), + (libc::SYS_munmap, vec![]), + (libc::SYS_recvfrom, vec![]), + (libc::SYS_rt_sigprocmask, vec![]), + (libc::SYS_rt_sigreturn, vec![]), + (libc::SYS_sendto, vec![]), + (libc::SYS_sigaltstack, vec![]), + (libc::SYS_write, vec![]), + ]) +} + +fn create_pty_foreground_ioctl_seccomp_rule() -> Result, BackendError> { + Ok(or![ + and![Cond::new(1, ArgLen::Dword, Eq, TIOCSCTTY)?], + and![Cond::new(1, ArgLen::Dword, Eq, TIOCSPGRP)?], + ]) +} + +fn pty_foreground_thread_rules() -> Result)>, BackendError> { + Ok(vec![ + (libc::SYS_close, vec![]), + (libc::SYS_exit_group, vec![]), + (libc::SYS_getpgid, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_getpgrp, vec![]), + (libc::SYS_ioctl, create_pty_foreground_ioctl_seccomp_rule()?), + (libc::SYS_munmap, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_poll, vec![]), + #[cfg(target_arch = "aarch64")] + (libc::SYS_ppoll, vec![]), + (libc::SYS_read, vec![]), + (libc::SYS_rt_sigaction, vec![]), + (libc::SYS_rt_sigreturn, vec![]), + (libc::SYS_setsid, vec![]), + (libc::SYS_sigaltstack, vec![]), + (libc::SYS_write, vec![]), + ]) +} + +// The filter containing the white listed syscall rules required by the VMM to +// function. +fn vmm_thread_rules( + hypervisor_type: HypervisorType, +) -> Result)>, BackendError> { + Ok(vec![ + (libc::SYS_accept4, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_access, vec![]), + (libc::SYS_bind, vec![]), + (libc::SYS_brk, vec![]), + (libc::SYS_clock_gettime, vec![]), + (libc::SYS_clock_nanosleep, vec![]), + (libc::SYS_clone, vec![]), + (libc::SYS_clone3, vec![]), + (libc::SYS_close, vec![]), + (libc::SYS_connect, vec![]), + (libc::SYS_dup, vec![]), + (libc::SYS_epoll_create1, vec![]), + (libc::SYS_epoll_ctl, vec![]), + (libc::SYS_epoll_pwait, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_epoll_wait, vec![]), + (libc::SYS_eventfd2, vec![]), + (libc::SYS_exit, vec![]), + (libc::SYS_exit_group, vec![]), + (libc::SYS_fallocate, vec![]), + (libc::SYS_fcntl, vec![]), + (libc::SYS_fdatasync, vec![]), + (libc::SYS_fstat, vec![]), + (libc::SYS_fsync, vec![]), + (libc::SYS_ftruncate, vec![]), + #[cfg(target_arch = "aarch64")] + (libc::SYS_faccessat, vec![]), + #[cfg(target_arch = "aarch64")] + (libc::SYS_newfstatat, vec![]), + (libc::SYS_futex, vec![]), + (libc::SYS_getpgid, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_getpgrp, vec![]), + (libc::SYS_getpid, vec![]), + (libc::SYS_getrandom, vec![]), + // Device threads inherit this filter before installing their own + // narrower per-device filter. + ( + libc::SYS_getsockopt, + virtio_devices::seccomp_filters::create_virtio_device_getsockopt_seccomp_rule(), + ), + (libc::SYS_gettid, vec![]), + (libc::SYS_gettimeofday, vec![]), + (libc::SYS_getuid, vec![]), + ( + libc::SYS_ioctl, + create_vmm_ioctl_seccomp_rule(hypervisor_type)?, + ), + (libc::SYS_getcwd, vec![]), + (libc::SYS_io_uring_enter, vec![]), + (libc::SYS_io_uring_setup, vec![]), + (libc::SYS_io_uring_register, vec![]), + (libc::SYS_kill, vec![]), + (libc::SYS_listen, vec![]), + (libc::SYS_lseek, vec![]), + (libc::SYS_madvise, vec![]), + (libc::SYS_mbind, vec![]), + (libc::SYS_memfd_create, vec![]), + (libc::SYS_mmap, vec![]), + (libc::SYS_mprotect, vec![]), + (libc::SYS_mremap, vec![]), + (libc::SYS_munmap, vec![]), + (libc::SYS_nanosleep, vec![]), + (libc::SYS_newfstatat, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_open, vec![]), + (libc::SYS_openat, vec![]), + (libc::SYS_pipe2, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_poll, vec![]), + #[cfg(target_arch = "aarch64")] + (libc::SYS_ppoll, vec![]), + (libc::SYS_prctl, vec![]), + (libc::SYS_pread64, vec![]), + (libc::SYS_preadv, vec![]), + (libc::SYS_prlimit64, vec![]), + (libc::SYS_pwrite64, vec![]), + (libc::SYS_pwritev, vec![]), + (libc::SYS_read, vec![]), + (libc::SYS_readv, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_readlink, vec![]), + #[cfg(target_arch = "aarch64")] + (libc::SYS_readlinkat, vec![]), + (libc::SYS_recvfrom, vec![]), + (libc::SYS_recvmsg, vec![]), + (libc::SYS_restart_syscall, vec![]), + // musl is missing this constant + // (libc::SYS_rseq, vec![]), + #[cfg(target_arch = "x86_64")] + (334, vec![]), + #[cfg(target_arch = "aarch64")] + (293, vec![]), + (libc::SYS_rt_sigaction, vec![]), + (libc::SYS_rt_sigprocmask, vec![]), + (libc::SYS_rt_sigreturn, vec![]), + (libc::SYS_sched_getaffinity, vec![]), + (libc::SYS_sched_setaffinity, vec![]), + (libc::SYS_sendmsg, vec![]), + (libc::SYS_sendto, vec![]), + (libc::SYS_set_robust_list, vec![]), + (libc::SYS_setsid, vec![]), + (libc::SYS_shutdown, vec![]), + (libc::SYS_sigaltstack, vec![]), + ( + libc::SYS_socket, + or![ + and![Cond::new(0, ArgLen::Dword, Eq, libc::AF_UNIX as u64)?], + and![Cond::new(0, ArgLen::Dword, Eq, libc::AF_INET as u64)?], + ], + ), + (libc::SYS_socketpair, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_stat, vec![]), + (libc::SYS_statx, vec![]), + (libc::SYS_tgkill, vec![]), + (libc::SYS_timerfd_create, vec![]), + (libc::SYS_timerfd_settime, vec![]), + (libc::SYS_tkill, vec![]), + ( + libc::SYS_umask, + or![and![Cond::new(0, ArgLen::Dword, Eq, 0o077)?]], + ), + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64le" + ))] + (libc::SYS_unlink, vec![]), + (libc::SYS_unlinkat, vec![]), + (libc::SYS_wait4, vec![]), + (libc::SYS_write, vec![]), + (libc::SYS_writev, vec![]), + // native virtiofs stuff + (libc::SYS_capget, vec![]), // For CAP_FSETID + (libc::SYS_capset, vec![]), + (libc::SYS_copy_file_range, vec![]), + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64le" + ))] + (libc::SYS_epoll_create, vec![]), + (libc::SYS_fchdir, vec![]), + (libc::SYS_fchmod, vec![]), + (libc::SYS_fchmodat, vec![]), + (libc::SYS_fchownat, vec![]), + (libc::SYS_fgetxattr, vec![]), + (libc::SYS_flistxattr, vec![]), + (libc::SYS_flock, vec![]), + (libc::SYS_fremovexattr, vec![]), + (libc::SYS_fsetxattr, vec![]), + #[cfg(target_arch = "s390x")] + (libc::SYS_fstatfs64, vec![]), + (libc::SYS_fstatfs, vec![]), + #[cfg(any( + target_arch = "x86_64", + target_arch = "s390x", + target_arch = "powerpc64le" + ))] + (libc::SYS_getdents, vec![]), + (libc::SYS_getdents64, vec![]), + (libc::SYS_getegid, vec![]), + (libc::SYS_geteuid, vec![]), + (libc::SYS_getxattr, vec![]), + (libc::SYS_linkat, vec![]), + (libc::SYS_listxattr, vec![]), + (libc::SYS_mkdirat, vec![]), + (libc::SYS_mknodat, vec![]), + (libc::SYS_name_to_handle_at, vec![]), + (libc::SYS_openat2, vec![]), + (libc::SYS_open_by_handle_at, vec![]), + (libc::SYS_pwritev2, vec![]), + (libc::SYS_renameat, vec![]), + (libc::SYS_renameat2, vec![]), + (libc::SYS_removexattr, vec![]), + (libc::SYS_setresgid, vec![]), + (libc::SYS_setresuid, vec![]), + //(libc::SYS_setresgid32); Needed on some platforms, + //(libc::SYS_setresuid32); Needed on some platforms + (libc::SYS_setxattr, vec![]), + #[cfg(target_arch = "s390x")] + (libc::SYS_sigreturn, vec![]), + (libc::SYS_symlinkat, vec![]), + (libc::SYS_syncfs, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_time, vec![]), // Rarely needed, except on static builds + (libc::SYS_unshare, vec![]), + (libc::SYS_utimensat, vec![]), + // to support operations in init_backendfs() + #[cfg(target_arch = "x86_64")] + (libc::SYS_lstat, vec![]), + // Async log. + (libc::SYS_sched_yield, vec![]), + (libc::SYS_setsockopt, vec![]), + ]) +} + +#[cfg(feature = "kvm")] +fn create_vcpu_ioctl_seccomp_rule_kvm() -> Result, BackendError> { + Ok(or![ + and![Cond::new(1, ArgLen::Dword, Eq, KVM_CHECK_EXTENSION,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_IOEVENTFD)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_IRQFD,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_DEVICE_ATTR,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_GSI_ROUTING,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_SET_USER_MEMORY_REGION,)?], + and![Cond::new(1, ArgLen::Dword, Eq, KVM_RUN,)?], + ]) +} + +#[cfg(feature = "mshv")] +fn create_vcpu_ioctl_seccomp_rule_mshv() -> Result, BackendError> { + Ok(or![ + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_SET_MSI_ROUTING)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_IOEVENTFD)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_IRQFD)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_RUN_VP)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_GET_VP_REGISTERS)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_SET_VP_REGISTERS)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_MAP_GUEST_MEMORY)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_UNMAP_GUEST_MEMORY)?], + and![Cond::new(1, ArgLen::Dword, Eq, MSHV_VP_TRANSLATE_GVA)?], + ]) +} + +fn create_vcpu_ioctl_seccomp_rule_hypervisor( + hypervisor_type: HypervisorType, +) -> Result, BackendError> { + match hypervisor_type { + #[cfg(feature = "kvm")] + HypervisorType::Kvm | HypervisorType::KvmPvm => create_vcpu_ioctl_seccomp_rule_kvm(), + #[cfg(feature = "mshv")] + HypervisorType::Mshv => create_vcpu_ioctl_seccomp_rule_mshv(), + #[allow(unreachable_patterns)] + _ => panic!("Invalid hypervisor {:?}", hypervisor_type), + } +} + +fn create_vcpu_ioctl_seccomp_rule( + hypervisor_type: HypervisorType, +) -> Result, BackendError> { + let mut rules = or![ + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_DEVICE_SET_IRQS)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_GROUP_UNSET_CONTAINER)?], + and![Cond::new(1, ArgLen::Dword, Eq, VFIO_IOMMU_UNMAP_DMA)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_SET_STATUS)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_GET_CONFIG)?], + and![Cond::new(1, ArgLen::Dword, Eq, VHOST_VDPA_SET_CONFIG)?], + and![Cond::new( + 1, + ArgLen::Dword, + Eq, + VHOST_VDPA_SET_VRING_ENABLE + )?], + ]; + + let hypervisor_rules = create_vcpu_ioctl_seccomp_rule_hypervisor(hypervisor_type)?; + + rules.extend(hypervisor_rules); + + Ok(rules) +} + +fn vcpu_thread_rules( + hypervisor_type: HypervisorType, +) -> Result)>, BackendError> { + Ok(vec![ + (libc::SYS_brk, vec![]), + (libc::SYS_clock_gettime, vec![]), + (libc::SYS_clock_nanosleep, vec![]), + (libc::SYS_close, vec![]), + (libc::SYS_dup, vec![]), + (libc::SYS_exit, vec![]), + (libc::SYS_epoll_ctl, vec![]), + (libc::SYS_fstat, vec![]), + (libc::SYS_futex, vec![]), + (libc::SYS_getrandom, vec![]), + (libc::SYS_getpid, vec![]), + ( + libc::SYS_ioctl, + create_vcpu_ioctl_seccomp_rule(hypervisor_type)?, + ), + (libc::SYS_lseek, vec![]), + (libc::SYS_madvise, vec![]), + (libc::SYS_mmap, vec![]), + (libc::SYS_mprotect, vec![]), + (libc::SYS_munmap, vec![]), + (libc::SYS_nanosleep, vec![]), + (libc::SYS_newfstatat, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_open, vec![]), + (libc::SYS_openat, vec![]), + (libc::SYS_pread64, vec![]), + (libc::SYS_pwrite64, vec![]), + (libc::SYS_read, vec![]), + (libc::SYS_recvfrom, vec![]), + (libc::SYS_recvmsg, vec![]), + (libc::SYS_rt_sigaction, vec![]), + (libc::SYS_rt_sigprocmask, vec![]), + (libc::SYS_rt_sigreturn, vec![]), + (libc::SYS_sendmsg, vec![]), + (libc::SYS_shutdown, vec![]), + (libc::SYS_sigaltstack, vec![]), + (libc::SYS_tgkill, vec![]), + (libc::SYS_tkill, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_unlink, vec![]), + #[cfg(target_arch = "aarch64")] + (libc::SYS_unlinkat, vec![]), + (libc::SYS_write, vec![]), + (libc::SYS_writev, vec![]), + ]) +} + +// The filter containing the white listed syscall rules required by the API to +// function. +fn api_thread_rules() -> Result)>, BackendError> { + Ok(vec![ + (libc::SYS_accept4, vec![]), + (libc::SYS_brk, vec![]), + (libc::SYS_close, vec![]), + (libc::SYS_dup, vec![]), + (libc::SYS_epoll_create1, vec![]), + (libc::SYS_epoll_ctl, vec![]), + (libc::SYS_epoll_pwait, vec![]), + #[cfg(target_arch = "x86_64")] + (libc::SYS_epoll_wait, vec![]), + (libc::SYS_exit, vec![]), + (libc::SYS_fcntl, vec![]), + (libc::SYS_futex, vec![]), + (libc::SYS_getrandom, vec![]), + (libc::SYS_ioctl, create_api_ioctl_seccomp_rule()?), + (libc::SYS_madvise, vec![]), + (libc::SYS_mmap, vec![]), + (libc::SYS_mprotect, vec![]), + (libc::SYS_munmap, vec![]), + (libc::SYS_recvfrom, vec![]), + (libc::SYS_recvmsg, vec![]), + (libc::SYS_sigaltstack, vec![]), + (libc::SYS_write, vec![]), + ]) +} + +fn thread_rules( + hypervisor_type: HypervisorType, +) -> Result)>, BackendError> { + let mut rules = api_thread_rules()?; + rules.append(&mut signal_handler_thread_rules()?); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut vcpu_thread_rules(hypervisor_type).unwrap()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut vmm_thread_rules(hypervisor_type).unwrap()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut pty_foreground_thread_rules().unwrap()); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + rules.append(&mut virtio_devices::seccomp_filters::virtio_device_thread_rules()); + rules.append(&mut create_runtime_seccomp_rules()?); + rules.retain(|(num, _)| *num != libc::SYS_ioctl); + + let mut ioctl_rule = vec![]; + let rule = create_signal_handler_ioctl_seccomp_rule()?; + ioctl_rule.extend(rule); + let rule = create_pty_foreground_ioctl_seccomp_rule()?; + ioctl_rule.extend(rule); + let rule = create_vmm_ioctl_seccomp_rule(hypervisor_type)?; + ioctl_rule.extend(rule); + let rule = create_vcpu_ioctl_seccomp_rule(hypervisor_type)?; + ioctl_rule.extend(rule); + let rule = create_api_ioctl_seccomp_rule()?; + ioctl_rule.extend(rule); + let rule = virtio_devices::seccomp_filters::create_virtio_device_ioctl_seccomp_rule(); + ioctl_rule.extend(rule); + + let mut ioctl_rules = vec![(libc::SYS_ioctl, ioctl_rule)]; + rules.append(&mut ioctl_rules); + + Ok(rules) +} + +fn create_runtime_seccomp_rules() -> Result)>, BackendError> { + Ok(RUTIME_RULES.read().unwrap().to_vec()) +} + +pub fn set_runtime_seccomp_rules(rules: Vec<(i64, Vec)>) { + *RUTIME_RULES.write().unwrap() = rules; +} + +fn get_seccomp_rules( + thread_type: Thread, + hypervisor_type: HypervisorType, +) -> Result)>, BackendError> { + match thread_type { + Thread::Api => Ok(api_thread_rules()?), + Thread::SignalHandler => Ok(signal_handler_thread_rules()?), + Thread::Vcpu => Ok(vcpu_thread_rules(hypervisor_type)?), + Thread::Vmm => Ok(vmm_thread_rules(hypervisor_type)?), + Thread::PtyForeground => Ok(pty_foreground_thread_rules()?), + Thread::All => Ok(thread_rules(hypervisor_type)?), + } +} + +/// Generate a BPF program based on the seccomp_action value +pub fn get_seccomp_filter( + seccomp_action: &SeccompAction, + thread_type: Thread, + hypervisor_type: HypervisorType, +) -> Result { + match seccomp_action { + SeccompAction::Allow => Ok(vec![]), + SeccompAction::Log => SeccompFilter::new( + get_seccomp_rules(thread_type, hypervisor_type) + .map_err(Error::Backend)? + .into_iter() + .collect(), + SeccompAction::Log, + SeccompAction::Allow, + std::env::consts::ARCH.try_into().unwrap(), + ) + .and_then(|filter| filter.try_into()) + .map_err(Error::Backend), + SeccompAction::KillProcess => match thread_type { + Thread::All => SeccompFilter::new( + get_seccomp_rules(thread_type, hypervisor_type) + .map_err(Error::Backend)? + .into_iter() + .collect(), + SeccompAction::Trap, + SeccompAction::Allow, + std::env::consts::ARCH.try_into().unwrap(), + ) + .and_then(|filter| filter.try_into()) + .map_err(Error::Backend), + _ => Ok(vec![]), + }, + _ => match thread_type { + Thread::All => Ok(vec![]), + _ => SeccompFilter::new( + get_seccomp_rules(thread_type, hypervisor_type) + .map_err(Error::Backend)? + .into_iter() + .collect(), + SeccompAction::Trap, + SeccompAction::Allow, + std::env::consts::ARCH.try_into().unwrap(), + ) + .and_then(|filter| filter.try_into()) + .map_err(Error::Backend), + }, + } +} + +#[cfg(all(test, target_os = "linux", feature = "kvm"))] +mod tests { + use super::{get_seccomp_filter, Thread}; + use hypervisor::HypervisorType; + use seccompiler::{apply_filter, SeccompAction}; + use std::os::unix::io::AsRawFd; + use std::os::unix::net::UnixStream; + use virtio_devices::seccomp_filters::{ + get_seccomp_filter as get_virtio_filter, Thread as VirtioThread, + }; + + #[test] + fn strict_vmm_parent_filter_allows_vsock_peer_credentials() { + let (socket, _peer) = UnixStream::pair().unwrap(); + let parent_filter = + get_seccomp_filter(&SeccompAction::Trap, Thread::Vmm, HypervisorType::Kvm).unwrap(); + let child_filter = + get_virtio_filter(&SeccompAction::Trap, VirtioThread::VirtioVsock).unwrap(); + let pid = unsafe { libc::fork() }; + assert!(pid >= 0, "fork failed: {}", std::io::Error::last_os_error()); + + if pid == 0 { + if apply_filter(&parent_filter).is_err() || apply_filter(&child_filter).is_err() { + unsafe { libc::syscall(libc::SYS_exit, 3) }; + } + let mut credentials = std::mem::MaybeUninit::::uninit(); + let mut length = std::mem::size_of::() as libc::socklen_t; + let result = unsafe { + libc::getsockopt( + socket.as_raw_fd(), + libc::SOL_SOCKET, + libc::SO_PEERCRED, + credentials.as_mut_ptr().cast(), + &mut length, + ) + }; + let exit_code = if result == 0 { 0 } else { 2 }; + unsafe { + libc::syscall(libc::SYS_exit, exit_code); + } + unreachable!(); + } + + let mut status = 0; + assert_eq!(unsafe { libc::waitpid(pid, &mut status, 0) }, pid); + assert!( + libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0, + "inherited VMM filter rejected getsockopt(SOL_SOCKET, SO_PEERCRED): wait status {status:#x}" + ); + } +} diff --git a/vendor/cube-hypervisor/vmm/src/serial_manager.rs b/vendor/cube-hypervisor/vmm/src/serial_manager.rs new file mode 100644 index 0000000..fa12d44 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/serial_manager.rs @@ -0,0 +1,325 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::device_manager::PtyPair; +use crate::vm_config::ConsoleOutputMode; +#[cfg(target_arch = "aarch64")] +use devices::legacy::Pl011; +#[cfg(target_arch = "x86_64")] +use devices::legacy::Serial; +use libc::EFD_NONBLOCK; +use serial_buffer::SerialBuffer; +use std::fs::File; +use std::io::Read; +use std::os::unix::io::{AsRawFd, FromRawFd}; +use std::panic::AssertUnwindSafe; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex}; +use std::{io, result, thread}; +use thiserror::Error; +use vmm_sys_util::eventfd::EventFd; + +#[derive(Debug, Error)] +pub enum Error { + /// Cannot clone File. + #[error("Error cloning File: {0}")] + FileClone(#[source] io::Error), + + /// Cannot create epoll context. + #[error("Error creating epoll context: {0}")] + Epoll(#[source] io::Error), + + /// Cannot handle the VM input stream. + #[error("Error handling VM input: {0:?}")] + ReadInput(#[source] io::Error), + + /// Cannot queue input to the serial device. + #[error("Error queuing input to the serial device: {0}")] + QueueInput(#[source] vmm_sys_util::errno::Error), + + /// Cannot flush output on the serial buffer. + #[error("Error flushing serial device's output buffer: {0}")] + FlushOutput(#[source] io::Error), + + /// Cannot make the file descriptor non-blocking. + #[error("Error making input file descriptor non-blocking: {0}")] + SetNonBlocking(#[source] io::Error), + + /// Cannot create EventFd. + #[error("Error creating EventFd: {0}")] + EventFd(#[source] io::Error), + + /// Cannot spawn SerialManager thread. + #[error("Error spawning SerialManager thread: {0}")] + SpawnSerialManager(#[source] io::Error), +} +pub type Result = result::Result; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u64)] +pub enum EpollDispatch { + File = 0, + Kill = 1, + Unknown, +} + +impl From for EpollDispatch { + fn from(v: u64) -> Self { + use EpollDispatch::*; + match v { + 0 => File, + 1 => Kill, + _ => Unknown, + } + } +} + +pub struct SerialManager { + #[cfg(target_arch = "x86_64")] + serial: Arc>, + #[cfg(target_arch = "aarch64")] + serial: Arc>, + epoll_file: File, + in_file: File, + kill_evt: EventFd, + handle: Option>, + pty_write_out: Option>, +} + +impl SerialManager { + pub fn new( + #[cfg(target_arch = "x86_64")] serial: Arc>, + #[cfg(target_arch = "aarch64")] serial: Arc>, + pty_pair: Option>>, + mode: ConsoleOutputMode, + ) -> Result> { + let in_file = match mode { + ConsoleOutputMode::Pty => { + if let Some(pty_pair) = pty_pair { + pty_pair + .lock() + .unwrap() + .main + .try_clone() + .map_err(Error::FileClone)? + } else { + return Ok(None); + } + } + ConsoleOutputMode::Tty => { + // If running on an interactive TTY then accept input + if unsafe { libc::isatty(libc::STDIN_FILENO) == 1 } { + let stdin_clone = unsafe { File::from_raw_fd(libc::dup(libc::STDIN_FILENO)) }; + let ret = unsafe { + let mut flags = libc::fcntl(stdin_clone.as_raw_fd(), libc::F_GETFL); + flags |= libc::O_NONBLOCK; + libc::fcntl(stdin_clone.as_raw_fd(), libc::F_SETFL, flags) + }; + + if ret < 0 { + return Err(Error::SetNonBlocking(std::io::Error::last_os_error())); + } + + stdin_clone + } else { + return Ok(None); + } + } + _ => return Ok(None), + }; + + let epoll_fd = epoll::create(true).map_err(Error::Epoll)?; + let kill_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFd)?; + + epoll::ctl( + epoll_fd, + epoll::ControlOptions::EPOLL_CTL_ADD, + kill_evt.as_raw_fd(), + epoll::Event::new(epoll::Events::EPOLLIN, EpollDispatch::Kill as u64), + ) + .map_err(Error::Epoll)?; + + epoll::ctl( + epoll_fd, + epoll::ControlOptions::EPOLL_CTL_ADD, + in_file.as_raw_fd(), + epoll::Event::new(epoll::Events::EPOLLIN, EpollDispatch::File as u64), + ) + .map_err(Error::Epoll)?; + + let mut pty_write_out = None; + if mode == ConsoleOutputMode::Pty { + let write_out = Arc::new(AtomicBool::new(false)); + pty_write_out = Some(write_out.clone()); + let writer = in_file.try_clone().map_err(Error::FileClone)?; + let buffer = SerialBuffer::new(Box::new(writer), write_out); + serial.as_ref().lock().unwrap().set_out(Box::new(buffer)); + } + + // Use 'File' to enforce closing on 'epoll_fd' + let epoll_file = unsafe { File::from_raw_fd(epoll_fd) }; + + Ok(Some(SerialManager { + serial, + epoll_file, + in_file, + kill_evt, + handle: None, + pty_write_out, + })) + } + + // This function should be called when the other end of the PTY is + // connected. It verifies if this is the first time it's been invoked + // after the connection happened, and if that's the case it flushes + // all output from the serial to the PTY. Otherwise, it's a no-op. + fn trigger_pty_flush( + #[cfg(target_arch = "x86_64")] serial: &Arc>, + #[cfg(target_arch = "aarch64")] serial: &Arc>, + pty_write_out: Option<&Arc>, + ) -> Result<()> { + if let Some(pty_write_out) = &pty_write_out { + if pty_write_out.load(Ordering::Acquire) { + return Ok(()); + } + + pty_write_out.store(true, Ordering::Release); + + serial + .lock() + .unwrap() + .flush_output() + .map_err(Error::FlushOutput)?; + } + + Ok(()) + } + + pub fn start_thread(&mut self, exit_evt: EventFd) -> Result<()> { + // Don't allow this to be run if the handle exists + if self.handle.is_some() { + warn!("Tried to start multiple SerialManager threads, ignoring"); + return Ok(()); + } + + let epoll_fd = self.epoll_file.as_raw_fd(); + let mut in_file = self.in_file.try_clone().map_err(Error::FileClone)?; + let serial = self.serial.clone(); + let pty_write_out = self.pty_write_out.clone(); + + // In case of PTY, we want to be able to detect a connection on the + // other end of the PTY. This is done by detecting there's no event + // triggered on the epoll, which is the reason why we want the + // epoll_wait() function to return after the timeout expired. + // In case of TTY, we don't expect to detect such behavior, which is + // why we can afford to block until an actual event is triggered. + let timeout = if pty_write_out.is_some() { 500 } else { -1 }; + + let thread = thread::Builder::new() + .name("serial-manager".to_string()) + .spawn(move || { + std::panic::catch_unwind(AssertUnwindSafe(move || { + // 3 for File, Kill, and Unknown + const EPOLL_EVENTS_LEN: usize = 3; + + let mut events = + vec![epoll::Event::new(epoll::Events::empty(), 0); EPOLL_EVENTS_LEN]; + + loop { + let num_events = match epoll::wait(epoll_fd, timeout, &mut events[..]) { + Ok(res) => res, + Err(e) => { + if e.kind() == io::ErrorKind::Interrupted { + // It's well defined from the epoll_wait() syscall + // documentation that the epoll loop can be interrupted + // before any of the requested events occurred or the + // timeout expired. In both those cases, epoll_wait() + // returns an error of type EINTR, but this should not + // be considered as a regular error. Instead it is more + // appropriate to retry, by calling into epoll_wait(). + continue; + } else { + return Err(Error::Epoll(e)); + } + } + }; + + if num_events == 0 { + // This very specific case happens when the serial is connected + // to a PTY. We know EPOLLHUP is always present when there's nothing + // connected at the other end of the PTY. That's why getting no event + // means we can flush the output of the serial through the PTY. + Self::trigger_pty_flush(&serial, pty_write_out.as_ref())?; + continue; + } + + for event in events.iter().take(num_events) { + let dispatch_event: EpollDispatch = event.data.into(); + match dispatch_event { + EpollDispatch::Unknown => { + let event = event.data; + warn!("Unknown serial manager loop event: {}", event); + } + EpollDispatch::File => { + if event.events & libc::EPOLLIN as u32 != 0 { + let mut input = [0u8; 64]; + let count = + in_file.read(&mut input).map_err(Error::ReadInput)?; + + // Replace "\n" with "\r" to deal with Windows SAC (#1170) + if count == 1 && input[0] == 0x0a { + input[0] = 0x0d; + } + + serial + .as_ref() + .lock() + .unwrap() + .queue_input_bytes(&input[..count]) + .map_err(Error::QueueInput)?; + } + if event.events & libc::EPOLLHUP as u32 != 0 { + if let Some(pty_write_out) = &pty_write_out { + pty_write_out.store(false, Ordering::Release); + } + // It's really important to sleep here as this will prevent + // the current thread from consuming 100% of the CPU cycles + // when waiting for someone to connect to the PTY. + std::thread::sleep(std::time::Duration::from_millis(500)); + } else { + // If the EPOLLHUP flag is not up on the associated event, we + // can assume the other end of the PTY is connected and therefore + // we can flush the output of the serial to it. + Self::trigger_pty_flush(&serial, pty_write_out.as_ref())?; + } + } + EpollDispatch::Kill => { + info!("KILL event received, stopping epoll loop"); + return Ok(()); + } + } + } + } + })) + .map_err(|_| { + error!("serial-manager thread panicked"); + exit_evt.write(1).ok() + }) + .ok(); + }) + .map_err(Error::SpawnSerialManager)?; + self.handle = Some(thread); + Ok(()) + } +} + +impl Drop for SerialManager { + fn drop(&mut self) { + self.kill_evt.write(1).ok(); + if let Some(handle) = self.handle.take() { + handle.join().ok(); + } + } +} diff --git a/vendor/cube-hypervisor/vmm/src/sigwinch_listener.rs b/vendor/cube-hypervisor/vmm/src/sigwinch_listener.rs new file mode 100644 index 0000000..4b9cac6 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/sigwinch_listener.rs @@ -0,0 +1,149 @@ +// Copyright 2021 Alyssa Ross +// SPDX-License-Identifier: Apache-2.0 + +use crate::clone3::{clone3, clone_args, CLONE_CLEAR_SIGHAND}; +use libc::{ + c_int, c_void, close, getpgrp, ioctl, pipe2, poll, pollfd, setsid, sigemptyset, siginfo_t, + sigprocmask, tcsetpgrp, O_CLOEXEC, POLLERR, SIGWINCH, SIG_SETMASK, STDIN_FILENO, STDOUT_FILENO, + TIOCSCTTY, +}; +use seccompiler::{apply_filter, BpfProgram}; +use std::cell::RefCell; +use std::fs::File; +use std::io::{self, ErrorKind, Read, Write}; +use std::mem::size_of; +use std::mem::MaybeUninit; +use std::os::unix::prelude::*; +use std::process::exit; +use std::ptr::null_mut; +use vmm_sys_util::signal::register_signal_handler; + +thread_local! { + // The tty file descriptor is stored in a global variable so it + // can be accessed by a signal handler. + static TX: RefCell> = RefCell::new(None); +} + +fn with_tx R>(f: F) -> R { + TX.with(|tx| f(tx.borrow().as_ref().unwrap())) +} + +// This function has to be safe to call from a signal handler, and +// therefore must not panic. +fn notify() { + if let Err(e) = with_tx(|mut tx| tx.write_all(b"\n")) { + if e.kind() == ErrorKind::BrokenPipe { + exit(0); + } + exit(1); + } +} + +extern "C" fn sigwinch_handler(_signo: c_int, _info: *mut siginfo_t, _unused: *mut c_void) { + notify(); +} + +fn unblock_all_signals() -> io::Result<()> { + let mut set = MaybeUninit::uninit(); + if unsafe { sigemptyset(set.as_mut_ptr()) } == -1 { + return Err(io::Error::last_os_error()); + } + let set = unsafe { set.assume_init() }; + + if unsafe { sigprocmask(SIG_SETMASK, &set, null_mut()) } == -1 { + return Err(io::Error::last_os_error()); + } + + Ok(()) +} + +fn sigwinch_listener_main(seccomp_filter: BpfProgram, tx: File, pty: File) -> ! { + TX.with(|opt| opt.replace(Some(tx))); + + let pty_fd = pty.into_raw_fd(); + + unsafe { + close(STDIN_FILENO); + close(STDOUT_FILENO); + } + + unblock_all_signals().unwrap(); + + if !seccomp_filter.is_empty() { + apply_filter(&seccomp_filter).unwrap(); + } + + register_signal_handler(SIGWINCH, sigwinch_handler).unwrap(); + + unsafe { + // Create a new session (and therefore a new process group). + assert_ne!(setsid(), -1); + + // Set the tty to be this process's controlling terminal. + assert_ne!(ioctl(pty_fd, TIOCSCTTY, 0), -1); + + // Become the foreground process group of the tty. + assert_ne!(tcsetpgrp(pty_fd, getpgrp()), -1); + + // Close the PTY fd + assert_ne!(close(pty_fd), -1); + } + + notify(); + + // Wait for the pipe to close, indicating the parent has exited. + with_tx(|tx| { + let mut pollfd = pollfd { + fd: tx.as_raw_fd(), + events: 0, + revents: 0, + }; + + while unsafe { poll(&mut pollfd, 1, -1) } == -1 { + let e = io::Error::last_os_error(); + assert!( + matches!(e.kind(), ErrorKind::Interrupted | ErrorKind::WouldBlock), + "poll: {}", + e + ); + } + + assert_eq!(pollfd.revents, POLLERR); + }); + + exit(0); +} + +pub fn start_sigwinch_listener( + seccomp_filter: BpfProgram, + pty_main: File, + pty_sub: File, +) -> io::Result { + let mut pipe = [-1; 2]; + if unsafe { pipe2(pipe.as_mut_ptr(), O_CLOEXEC) } == -1 { + return Err(io::Error::last_os_error()); + } + + let mut rx = unsafe { File::from_raw_fd(pipe[0]) }; + let tx = unsafe { File::from_raw_fd(pipe[1]) }; + + let mut args = clone_args::default(); + args.flags |= CLONE_CLEAR_SIGHAND; + + match unsafe { clone3(&mut args, size_of::()) } { + -1 => return Err(io::Error::last_os_error()), + 0 => { + drop(rx); + drop(pty_main); + sigwinch_listener_main(seccomp_filter, tx, pty_sub); + } + _ => (), + } + + drop(tx); + + // Wait for a notification indicating readiness. + rx.read_exact(&mut [0])?; + + Ok(rx) +} diff --git a/vendor/cube-hypervisor/vmm/src/soft_dirty.rs b/vendor/cube-hypervisor/vmm/src/soft_dirty.rs new file mode 100644 index 0000000..64ddbf2 --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/soft_dirty.rs @@ -0,0 +1,834 @@ +// Copyright © 2026 Tencent Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +//! Soft-dirty snapshot support +//! +//! This module provides functionality for creating soft-dirty-based incremental +//! snapshots that only save pages actually written by the guest **since the +//! previous soft-dirty snapshot** (a true delta), by toggling +//! `/proc/self/clear_refs` and inspecting bit 55 of `/proc/self/pagemap`. +//! +//! Compared with the pagemap_anon path (which records every CoW-anonymous page +//! ever touched since restore), soft-dirty produces a much smaller delta on +//! the second and later incremental snapshots because the bit is reset on each +//! cycle. +//! +//! Lifecycle: +//! 1. First snapshot: write the full base, then `clear_soft_dirty()` to arm +//! the kernel tracker. +//! 2. Subsequent snapshots: read pagemap bit 55, write only pages with the +//! bit set, then `clear_soft_dirty()` again to start the next window. +//! +//! Requires `CONFIG_MEM_SOFT_DIRTY=y`. On kernels without this config the +//! caller is expected to silently fall back to the pagemap_anon path. + +use crate::pagemap_anon::{self, coalesce_pages_to_ranges, get_anon_pages, host_page_size}; +use log::{debug, info, trace}; +use std::fs::{File, OpenOptions}; +use std::io::{self, Read, Seek, SeekFrom, Write}; +use std::time::Instant; +use thiserror::Error; +use vm_memory::{Bytes, GuestAddress, GuestMemory, GuestMemoryMmap}; +#[cfg(test)] +use vm_migration::protocol::MemoryRange; +use vm_migration::protocol::MemoryRangeTable; + +/// Size of a pagemap entry in bytes +const PAGEMAP_ENTRY_SIZE: u64 = 8; + +/// Bit 55 in pagemap: page has been written since the last clear_refs(4) +const PAGEMAP_SOFT_DIRTY_BIT: u64 = 1 << 55; + +/// Bit 63: page is present in RAM +const PAGEMAP_PRESENT_BIT: u64 = 1 << 63; + +/// Bit 62: page is in swap +const PAGEMAP_SWAPPED_BIT: u64 = 1 << 62; + +/// Magic value to write to /proc/self/clear_refs to clear the soft-dirty +/// bit on every page table entry of the current process and (re)arm the +/// VM_SOFTDIRTY tracker. +const CLEAR_REFS_SOFT_DIRTY: &[u8] = b"4\n"; + +/// Errors related to soft-dirty operations +#[derive(Debug, Error)] +pub enum SoftDirtyError { + #[error("Failed to open {path}: {source}")] + OpenFailed { + path: String, + #[source] + source: io::Error, + }, + + #[error("Failed to read {path}: {source}")] + ReadFailed { + path: String, + #[source] + source: io::Error, + }, + + #[error("Failed to seek in {path}: {source}")] + SeekFailed { + path: String, + #[source] + source: io::Error, + }, + + #[error("Failed to write to {path}: {source}")] + WriteFailed { + path: String, + #[source] + source: io::Error, + }, + + #[error("Failed to get host address for guest memory region")] + GetHostAddressFailed, + + #[error("Memory region not aligned to page boundary")] + NotPageAligned, + + #[error("Failed to probe anonymous pages via pagemap_anon: {0}")] + AnonProbe(#[from] pagemap_anon::PagemapAnonError), +} + +/// Result type for soft-dirty operations +pub type Result = std::result::Result; + +/// Statistics about soft-dirty filtering results +#[derive(Debug, Default, Clone)] +pub struct SoftDirtyStats { + /// Total number of pages in the memory regions + pub total_pages: u64, + /// Number of pages with the soft-dirty bit set (written since last clear) + pub dirty_pages: u64, + /// Total bytes in all memory regions + pub total_bytes: u64, + /// Bytes that will be written (dirty pages only) + pub saved_bytes: u64, +} + +impl SoftDirtyStats { + /// Calculate the percentage of memory saved (not needing to be snapshotted) + pub fn savings_percentage(&self) -> f64 { + if self.total_bytes == 0 { + return 0.0; + } + ((self.total_bytes - self.saved_bytes) as f64 / self.total_bytes as f64) * 100.0 + } +} + +/// Probe whether the running kernel actually supports soft-dirty tracking, +/// and on success leave the tracker armed for the next snapshot window. +/// +/// # Approach +/// +/// Writing "4" to `/proc/self/clear_refs` always succeeds (even when +/// `CONFIG_MEM_SOFT_DIRTY=n` the kernel silently no-ops), so its return +/// value is not a reliable probe. Instead we perform a real round-trip: +/// +/// 1. `mmap` a fresh anonymous page. On kernels with `CONFIG_MEM_SOFT_DIRTY=y` +/// the VMA is created with the `VM_SOFTDIRTY` flag, so the **first write** +/// that installs the PTE will propagate that flag into PTE bit 55. +/// On kernels without the config, bit 55 is never observed by the page +/// table walker and stays 0. +/// +/// 2. Write one byte to the page. This forces a minor fault, the kernel +/// installs (or re-validates) the PTE, and — when CONFIG_MEM_SOFT_DIRTY +/// is active — sets bit 55 to 1. +/// +/// 3. Read `/proc/self/pagemap` for that page. If bit 55 is 1 the kernel +/// really tracks soft-dirty; if it is 0 the kernel does not. +/// +/// 4. **Only on success** do we call `clear_soft_dirty()` once to arm the +/// tracker process-wide for the caller's next snapshot window. When the +/// kernel does not support soft-dirty, no `clear_refs(4)` is performed +/// at all, saving the heavy mmap_lock-write PTE walk on unsupported +/// kernels. +/// +/// # Arm side-effect +/// +/// When the probe succeeds, `clear_soft_dirty()` is called exactly once, +/// which clears every PTE soft-dirty bit process-wide. The caller +/// (`MemoryManager`) depends on this to start a new observation window. +/// When the probe fails, no arm is performed and the caller must fall back +/// to the pagemap_anon path. +pub fn probe_soft_dirty_support() -> bool { + let page_size = host_page_size(); + + // Step 1: allocate a page-aligned anonymous scratch region. The + // `GuestMemoryMmap::from_ranges` API issues an `mmap(MAP_PRIVATE | + // MAP_ANONYMOUS)`, which is exactly the VMA type whose PTEs the + // soft-dirty tracker targets. All `unsafe` is encapsulated by + // `vm-memory`, so this function stays safe. + let scratch = match GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), page_size as usize)]) + { + Ok(mem) => mem, + Err(e) => { + debug!("Soft-dirty probe: scratch mmap failed: {:?}", e); + return false; + } + }; + + let host_addr = match scratch.get_host_address(GuestAddress(0)) { + Ok(p) => p as u64, + Err(e) => { + debug!("Soft-dirty probe: get_host_address failed: {:?}", e); + return false; + } + }; + + // Step 2: write one byte. This is the **first** write to this page, + // so the kernel must install a real PTE. On CONFIG_MEM_SOFT_DIRTY=y + // kernels the VMA carries VM_SOFTDIRTY, and the fault handler + // propagates that into PTE bit 55 accordingly. + if let Err(e) = scratch.write(&[0xabu8], GuestAddress(0)) { + debug!("Soft-dirty probe: verification write failed: {:?}", e); + return false; + } + + // Step 3: read the pagemap entry and check bit 55. + let bit55_set = get_soft_dirty_pages(host_addr, page_size) + .map(|bits| bits.len() == 1 && bits[0]) + .unwrap_or(false); + + // `scratch` is dropped at end of scope → `munmap` — no manual cleanup. + + if !bit55_set { + debug!( + "Soft-dirty probe: write succeeded but bit 55 never flipped — \ + kernel lacks CONFIG_MEM_SOFT_DIRTY \ + (e.g. ARM64 or a stripped-down x86 kernel)" + ); + return false; + } + + // Step 4: kernel really supports soft-dirty. Arm the tracker by + // clearing every PTE's soft-dirty bit process-wide. This is the + // *only* `clear_refs(4)` call in the probe. + if let Err(e) = clear_soft_dirty() { + debug!("Soft-dirty probe: post-verify arm failed: {}", e); + return false; + } + + true +} + +/// Clear the soft-dirty bit on every PTE of the current process and (re)arm +/// the VM_SOFTDIRTY tracker on every VMA, by writing "4" to +/// `/proc/self/clear_refs`. +/// +/// After this call, any page subsequently written by the guest (vCPU stores +/// or device DMA into guest memory) will be marked with bit 55 in +/// `/proc/self/pagemap`. +pub fn clear_soft_dirty() -> Result<()> { + let start = Instant::now(); + let mut f = OpenOptions::new() + .write(true) + .open("/proc/self/clear_refs") + .map_err(|e| SoftDirtyError::OpenFailed { + path: "/proc/self/clear_refs".to_string(), + source: e, + })?; + f.write_all(CLEAR_REFS_SOFT_DIRTY) + .map_err(|e| SoftDirtyError::WriteFailed { + path: "/proc/self/clear_refs".to_string(), + source: e, + })?; + let elapsed = start.elapsed(); + // The kernel walks every PTE of every anonymous VMA under mmap_lock + // (write) here, and on large guests this is the dominant snapshot-time + // stall (hundreds of ms on multi-GiB VMs). Log at info so operators + // can correlate it with guest pause-time spikes without enabling debug. + info!( + "soft-dirty: clear_refs(4) took {} us ({:.3} ms)", + elapsed.as_micros(), + elapsed.as_secs_f64() * 1000.0 + ); + Ok(()) +} + +/// Get the soft-dirty bitmap for a memory region by reading +/// `/proc/self/pagemap`. +/// +/// # Arguments +/// * `host_addr` - Host virtual address of the memory region (must be page-aligned) +/// * `length` - Length of the memory region in bytes +/// +/// # Returns +/// A vector of bools where each bool indicates whether the corresponding +/// page has been written since the previous `clear_soft_dirty()` call. +/// +/// A page counts as soft-dirty if either: +/// - It is present and bit 55 is set, or +/// - It is in swap (bit 62) — swapped pages are by definition pages that +/// were anonymous and previously written, so they must be saved on the +/// next snapshot to be safe. +pub fn get_soft_dirty_pages(host_addr: u64, length: u64) -> Result> { + let page_size = host_page_size(); + if host_addr % page_size != 0 { + return Err(SoftDirtyError::NotPageAligned); + } + + let num_pages = length.div_ceil(page_size) as usize; + let start_page = host_addr / page_size; + + let mut pagemap_file = + File::open("/proc/self/pagemap").map_err(|e| SoftDirtyError::OpenFailed { + path: "/proc/self/pagemap".to_string(), + source: e, + })?; + + let pagemap_offset = start_page * PAGEMAP_ENTRY_SIZE; + pagemap_file + .seek(SeekFrom::Start(pagemap_offset)) + .map_err(|e| SoftDirtyError::SeekFailed { + path: "/proc/self/pagemap".to_string(), + source: e, + })?; + + let buf_size = num_pages * PAGEMAP_ENTRY_SIZE as usize; + let mut pagemap_buf = vec![0u8; buf_size]; + pagemap_file + .read_exact(&mut pagemap_buf) + .map_err(|e| SoftDirtyError::ReadFailed { + path: "/proc/self/pagemap".to_string(), + source: e, + })?; + + let mut result = vec![false; num_pages]; + for (i, item) in result.iter_mut().enumerate().take(num_pages) { + let entry_offset = i * PAGEMAP_ENTRY_SIZE as usize; + let entry = u64::from_ne_bytes( + pagemap_buf[entry_offset..entry_offset + PAGEMAP_ENTRY_SIZE as usize] + .try_into() + .unwrap(), + ); + + let present = (entry & PAGEMAP_PRESENT_BIT) != 0; + let swapped = (entry & PAGEMAP_SWAPPED_BIT) != 0; + let soft_dirty = (entry & PAGEMAP_SOFT_DIRTY_BIT) != 0; + + // Treat swapped anonymous pages as dirty: they were written before + // being swapped out, and the kernel does not preserve the soft-dirty + // bit across swap-in on all kernels. + if swapped { + *item = true; + continue; + } + + if present && soft_dirty { + *item = true; + } + } + + Ok(result) +} + +/// Filter memory ranges by soft-dirty, returning only ranges with pages +/// written since the previous `clear_soft_dirty()` call. +/// +/// # Arguments +/// * `guest_memory` - The guest memory object +/// * `ranges` - The original memory range table +/// +/// # Returns +/// A tuple containing: +/// - The filtered memory range table (only soft-dirty pages, merged into +/// contiguous runs) +/// - Statistics about the filtering +pub fn filter_memory_ranges_by_soft_dirty( + guest_memory: &GuestMemoryMmap, + ranges: &MemoryRangeTable, +) -> Result<(MemoryRangeTable, SoftDirtyStats)> { + let mut filtered_ranges = MemoryRangeTable::default(); + let mut stats = SoftDirtyStats::default(); + let page_size = host_page_size(); + + debug!( + "Starting soft-dirty filtering for {} memory regions", + ranges.regions().len() + ); + + for range in ranges.regions() { + let gpa = range.gpa; + let length = range.length; + + stats.total_bytes += length; + stats.total_pages += length.div_ceil(page_size); + + trace!( + "Processing memory region: GPA=0x{:x}, length={}", + gpa, + length + ); + + let host_addr = guest_memory + .get_host_address(GuestAddress(gpa)) + .map_err(|_| SoftDirtyError::GetHostAddressFailed)?; + + let dirty_pages = get_soft_dirty_pages(host_addr as u64, length)?; + + let (region_ranges, dirty_count) = coalesce_pages_to_ranges(gpa, &dirty_pages, page_size); + stats.dirty_pages += dirty_count; + stats.saved_bytes += dirty_count * page_size; + for r in region_ranges { + filtered_ranges.push(r); + } + } + + debug!( + "Soft-dirty filtering complete: {} dirty ranges, {} total pages, {} dirty pages", + filtered_ranges.regions().len(), + stats.total_pages, + stats.dirty_pages + ); + + if stats.total_pages > 0 { + let dirty_pct = (stats.dirty_pages as f64 / stats.total_pages as f64) * 100.0; + debug!( + "Soft-dirty stats: {:.1}% dirty pages, {:.1}% savings vs full snapshot", + dirty_pct, + stats.savings_percentage() + ); + } + + Ok((filtered_ranges, stats)) +} + +/// Filter memory ranges by the **intersection** of anonymous (CoW) pages +/// and soft-dirty pages. +/// +/// The set of pages we want to write into an incremental snapshot is exactly: +/// * pages that have actually been Copy-on-Written by the guest (anonymous +/// after the `MAP_PRIVATE` restore — only these can ever differ from the +/// base snapshot file), AND +/// * pages whose contents have changed **since the previous +/// `clear_soft_dirty()`** (the delta this cycle must record). +/// +/// Either signal alone over- or under-approximates: +/// * "anonymous only" is correct but cumulative — every page CoW'd since +/// restore shows up forever, even if it has not been modified in the +/// current snapshot window. +/// * "soft-dirty only" can include host-side writes that landed on +/// file-backed page-cache pages (e.g. shared-mmap restore); those pages +/// are the same content as the base file on disk and re-saving them +/// would be wasteful — and on the very first soft-dirty cycle of a +/// freshly-armed tracker every guest write since arm shows up, +/// including writes that fault in non-anon pages. +/// +/// Taking the intersection gives us exactly the anon pages whose contents +/// changed in the current window, which is both minimal and safe. +pub fn filter_memory_ranges_by_anon_and_soft_dirty( + guest_memory: &GuestMemoryMmap, + ranges: &MemoryRangeTable, +) -> Result<(MemoryRangeTable, SoftDirtyStats)> { + let mut filtered_ranges = MemoryRangeTable::default(); + let mut stats = SoftDirtyStats::default(); + let page_size = host_page_size(); + + debug!( + "Starting anon ∩ soft-dirty filtering for {} memory regions", + ranges.regions().len() + ); + + for range in ranges.regions() { + let gpa = range.gpa; + let length = range.length; + + stats.total_bytes += length; + stats.total_pages += length.div_ceil(page_size); + + trace!( + "Processing memory region: GPA=0x{:x}, length={}", + gpa, + length + ); + + let host_addr = guest_memory + .get_host_address(GuestAddress(gpa)) + .map_err(|_| SoftDirtyError::GetHostAddressFailed)?; + + let anon_pages = get_anon_pages(host_addr as u64, length)?; + let dirty_pages = get_soft_dirty_pages(host_addr as u64, length)?; + + debug_assert_eq!(anon_pages.len(), dirty_pages.len()); + + // A page must be saved only when it is both anonymous (CoW) and + // soft-dirty in the current window. + let must_save: Vec = anon_pages + .iter() + .zip(dirty_pages.iter()) + .map(|(&is_anon, &is_dirty)| is_anon && is_dirty) + .collect(); + + let (region_ranges, save_count) = coalesce_pages_to_ranges(gpa, &must_save, page_size); + stats.dirty_pages += save_count; + stats.saved_bytes += save_count * page_size; + for r in region_ranges { + filtered_ranges.push(r); + } + } + + debug!( + "anon ∩ soft-dirty filtering complete: {} ranges, {} total pages, {} pages to save", + filtered_ranges.regions().len(), + stats.total_pages, + stats.dirty_pages + ); + + if stats.total_pages > 0 { + let dirty_pct = (stats.dirty_pages as f64 / stats.total_pages as f64) * 100.0; + debug!( + "anon ∩ soft-dirty stats: {:.1}% pages to save, {:.1}% savings vs full snapshot", + dirty_pct, + stats.savings_percentage() + ); + } + + Ok((filtered_ranges, stats)) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::Mutex; + + /// `/proc/self/clear_refs` and the soft-dirty PTE bit are *process*-wide + /// state. When `cargo test` runs cases concurrently, one test's + /// `clear_soft_dirty()` can race with another test's "write then read + /// bit 55" check and clear the bit before it is observed, producing + /// spurious failures. Serialize every test that calls `clear_soft_dirty` + /// or reads the pagemap of pages we just dirtied through this lock. + static CLEAR_REFS_LOCK: Mutex<()> = Mutex::new(()); + + #[test] + fn test_soft_dirty_stats_savings_percentage() { + let stats = SoftDirtyStats { + total_bytes: 1000, + saved_bytes: 250, + ..Default::default() + }; + assert!((stats.savings_percentage() - 75.0).abs() < 0.01); + } + + #[test] + fn test_soft_dirty_stats_zero_total() { + let stats = SoftDirtyStats::default(); + assert_eq!(stats.savings_percentage(), 0.0); + } + + #[test] + fn test_soft_dirty_stats_full_dirty() { + let stats = SoftDirtyStats { + total_bytes: 4096, + saved_bytes: 4096, + ..Default::default() + }; + assert!((stats.savings_percentage() - 0.0).abs() < 0.01); + } + + #[test] + fn test_get_soft_dirty_pages_not_page_aligned() { + // One byte past a page boundary is never aligned on 4 KiB or 64 KiB hosts. + let unaligned = host_page_size() + 1; + let result = get_soft_dirty_pages(unaligned, host_page_size()); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + SoftDirtyError::NotPageAligned + )); + } + + #[test] + fn test_pagemap_bit_constants() { + assert_eq!(PAGEMAP_SOFT_DIRTY_BIT, 1u64 << 55); + assert_eq!(PAGEMAP_PRESENT_BIT, 1u64 << 63); + assert_eq!(PAGEMAP_SWAPPED_BIT, 1u64 << 62); + } + + /// Self-test: if the running kernel supports soft-dirty, allocating and + /// writing a scratch page after `clear_soft_dirty()` should make that + /// page show up as dirty in the pagemap. + /// + /// Skipped silently when the kernel does not support soft-dirty (e.g. + /// running on a stripped-down CI kernel). + #[test] + fn test_clear_and_observe_dirty_page() { + // Hold the process-wide lock for the entire "clear -> write -> read + // pagemap" sequence; without it a parallel test calling + // clear_soft_dirty() can wipe our bit before we read it. + let _guard = CLEAR_REFS_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + + if !probe_soft_dirty_support() { + eprintln!("kernel has no soft-dirty support; skipping"); + return; + } + + // Allocate one page-aligned scratch buffer. + let page = host_page_size() as usize; + let layout = std::alloc::Layout::from_size_align(page, page).unwrap(); + // SAFETY: standard allocation followed by an explicit deallocation + // at the end of the test. + let ptr = unsafe { std::alloc::alloc_zeroed(layout) }; + assert!(!ptr.is_null()); + + // Touch (write) the page to make sure it has a PTE, then clear so + // the dirty bit starts at 0. + unsafe { ptr.write_volatile(0) }; + clear_soft_dirty().expect("clear_soft_dirty failed"); + + // Right after clear, the page should not be soft-dirty. + let before = get_soft_dirty_pages(ptr as u64, page as u64).expect("get_soft_dirty_pages"); + assert_eq!(before.len(), 1); + + // Now write to the page. + unsafe { ptr.write_volatile(0xab) }; + + // It should now show up as soft-dirty. + let after = get_soft_dirty_pages(ptr as u64, page as u64).expect("get_soft_dirty_pages"); + assert_eq!(after.len(), 1); + assert!( + after[0], + "page expected to be soft-dirty after a write, but bit 55 was not set" + ); + + // SAFETY: matches the alloc above. + unsafe { std::alloc::dealloc(ptr, layout) }; + } + + /// End-to-end integration test that drives `filter_memory_ranges_by_soft_dirty` + /// through the same call sequence `MemoryManager::send_soft_dirty_memory()` + /// performs at runtime, but against a real `GuestMemoryMmap` backed by an + /// anonymous private mapping owned by the test process itself. + /// + /// Sequence: + /// round 1 (base) : clear_soft_dirty() -> arm tracker + /// round 2 (delta #1) : write pages [2..=4] -> filter must return exactly those + /// clear_soft_dirty() -> arm next window + /// round 3 (delta #2) : write page [7] -> filter must return ONLY page 7 + /// (i.e. soft-dirty really is a delta, + /// not a cumulative set) + /// clear_soft_dirty() -> arm next window + /// round 4 (delta empty) : write nothing -> filter must return empty + /// + /// Skipped silently when the host kernel lacks `CONFIG_MEM_SOFT_DIRTY=y`. + #[test] + fn test_filter_memory_ranges_by_soft_dirty_end_to_end() { + // Serialize against any other test that touches /proc/self/clear_refs + // — it is process-wide state. + let _guard = CLEAR_REFS_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + + if !probe_soft_dirty_support() { + eprintln!( + "kernel has no soft-dirty support (CONFIG_MEM_SOFT_DIRTY=n); skipping {}", + "test_filter_memory_ranges_by_soft_dirty_end_to_end" + ); + return; + } + + // Build a single-region GuestMemoryMmap of 16 pages starting at GPA 0. + // GuestMemoryMmap::from_ranges() defaults to an anonymous MAP_PRIVATE + // mapping, which is exactly the kind of memory soft-dirty tracks. + const NUM_PAGES: u64 = 16; + // Drive the test at the host's real page size so it exercises the + // 64 KiB path when run on an ARM64 64 KiB kernel. + let page_size = host_page_size(); + let region_len = (NUM_PAGES * page_size) as usize; + let guest_memory = + GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), region_len)]).unwrap(); + + // Pre-fault every page once so the kernel has real PTEs for them. + // Without this, pages may remain "not present" and the test would + // observe zero dirty pages even after writes (because the writes + // themselves are what fault them in). + let host_base = guest_memory + .get_host_address(GuestAddress(0)) + .expect("get_host_address") as *mut u8; + for p in 0..NUM_PAGES { + // SAFETY: host_base..host_base+region_len is a valid mapping + // owned by `guest_memory`; we touch one byte per page. + unsafe { host_base.add((p * page_size) as usize).write_volatile(0) }; + } + + let ranges = { + let mut t = MemoryRangeTable::default(); + t.push(MemoryRange { + gpa: 0, + length: NUM_PAGES * page_size, + }); + t + }; + + // Helper: write one byte into the page at `page_idx`. + let dirty_page = |page_idx: u64, val: u8| { + // SAFETY: 0 <= page_idx < NUM_PAGES, mapping is at least + // NUM_PAGES * page_size bytes long. + unsafe { + host_base + .add((page_idx * page_size) as usize) + .write_volatile(val) + }; + }; + + // Helper: collect the (gpa, length) pairs that the filter returned, + // for ergonomic assertions. + let collect = |table: &MemoryRangeTable| -> Vec<(u64, u64)> { + table.regions().iter().map(|r| (r.gpa, r.length)).collect() + }; + + // ---------------- Round 1: arm the tracker (the "base" cycle) ---------------- + clear_soft_dirty().expect("clear_soft_dirty: round 1"); + + // ---------------- Round 2: dirty pages 2,3,4 -> must show up ---------------- + dirty_page(2, 0xa1); + dirty_page(3, 0xa2); + dirty_page(4, 0xa3); + + let (filtered, stats) = + filter_memory_ranges_by_soft_dirty(&guest_memory, &ranges).expect("filter round 2"); + + // The three writes must be coalesced into exactly one contiguous run + // [page 2 .. page 5). + assert_eq!( + collect(&filtered), + vec![(2 * page_size, 3 * page_size)], + "round 2: expected exactly the three written pages, coalesced" + ); + assert_eq!(stats.total_pages, NUM_PAGES); + assert_eq!( + stats.dirty_pages, 3, + "round 2: expected exactly 3 dirty pages" + ); + assert_eq!(stats.saved_bytes, 3 * page_size); + + // Re-arm the tracker for the next window. After this point, the bits + // for pages 2/3/4 must be cleared again. + clear_soft_dirty().expect("clear_soft_dirty: round 2 -> 3"); + + // ---------------- Round 3: dirty only page 7 -> must be the ONLY dirty page ---------------- + // This is the key assertion: soft-dirty must behave as a *delta*, + // not a cumulative set. If it were cumulative (like pagemap_anon), + // pages 2/3/4 would still show up here. + dirty_page(7, 0xb1); + + let (filtered, stats) = + filter_memory_ranges_by_soft_dirty(&guest_memory, &ranges).expect("filter round 3"); + + assert_eq!( + collect(&filtered), + vec![(7 * page_size, page_size)], + "round 3: expected ONLY page 7, soft-dirty must be a delta and \ + must NOT report pages 2/3/4 from the previous window" + ); + assert_eq!( + stats.dirty_pages, 1, + "round 3: expected exactly 1 dirty page (true delta)" + ); + + clear_soft_dirty().expect("clear_soft_dirty: round 3 -> 4"); + + // ---------------- Round 4: write nothing -> empty delta ---------------- + let (filtered, stats) = + filter_memory_ranges_by_soft_dirty(&guest_memory, &ranges).expect("filter round 4"); + + assert!( + filtered.regions().is_empty(), + "round 4: expected an empty delta with no writes since clear, got {:?}", + collect(&filtered) + ); + assert_eq!( + stats.dirty_pages, 0, + "round 4: dirty_pages must be 0 with no writes since clear" + ); + assert_eq!(stats.total_pages, NUM_PAGES); + assert!( + (stats.savings_percentage() - 100.0).abs() < 0.01, + "round 4: with zero dirty pages, savings should be 100%, got {}", + stats.savings_percentage() + ); + } + + /// End-to-end test for `filter_memory_ranges_by_anon_and_soft_dirty`: + /// the result must be the intersection of "anonymous (CoW)" and + /// "soft-dirty since the last clear". + /// + /// We build an anonymous private mapping (every present page in it + /// is anon by construction), so on this fixture anon == always true + /// and the intersection collapses to "soft-dirty only" — i.e. the + /// helper must produce exactly the same per-window delta as + /// `filter_memory_ranges_by_soft_dirty` does. + /// + /// Skipped silently when the host kernel lacks `CONFIG_MEM_SOFT_DIRTY=y`. + #[test] + fn test_filter_memory_ranges_by_anon_and_soft_dirty_end_to_end() { + let _guard = CLEAR_REFS_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + + if !probe_soft_dirty_support() { + eprintln!( + "kernel has no soft-dirty support (CONFIG_MEM_SOFT_DIRTY=n); skipping {}", + "test_filter_memory_ranges_by_anon_and_soft_dirty_end_to_end" + ); + return; + } + + const NUM_PAGES: u64 = 16; + // Use the host's real page size so this covers ARM64 64 KiB kernels. + let page_size = host_page_size(); + let region_len = (NUM_PAGES * page_size) as usize; + let guest_memory = + GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), region_len)]).unwrap(); + + let host_base = guest_memory + .get_host_address(GuestAddress(0)) + .expect("get_host_address") as *mut u8; + for p in 0..NUM_PAGES { + // SAFETY: host_base..host_base+region_len is owned by guest_memory. + unsafe { host_base.add((p * page_size) as usize).write_volatile(0) }; + } + + let ranges = { + let mut t = MemoryRangeTable::default(); + t.push(MemoryRange { + gpa: 0, + length: NUM_PAGES * page_size, + }); + t + }; + + let dirty_page = |page_idx: u64, val: u8| { + // SAFETY: 0 <= page_idx < NUM_PAGES. + unsafe { + host_base + .add((page_idx * page_size) as usize) + .write_volatile(val) + }; + }; + + let collect = |table: &MemoryRangeTable| -> Vec<(u64, u64)> { + table.regions().iter().map(|r| (r.gpa, r.length)).collect() + }; + + // Arm the tracker. + clear_soft_dirty().expect("clear_soft_dirty: round 1"); + + // Dirty pages 5 and 6 (will be one coalesced run after AND). + dirty_page(5, 0xc1); + dirty_page(6, 0xc2); + + let (filtered, stats) = filter_memory_ranges_by_anon_and_soft_dirty(&guest_memory, &ranges) + .expect("anon ∩ soft-dirty filter failed"); + + // Every page in this anon MAP_PRIVATE mapping is anon, so + // anon ∩ soft-dirty == soft-dirty exactly: only pages 5 and 6. + assert_eq!( + collect(&filtered), + vec![(5 * page_size, 2 * page_size)], + "anon ∩ soft-dirty must yield exactly the two pages \ + written in this window, coalesced" + ); + assert_eq!(stats.total_pages, NUM_PAGES); + assert_eq!(stats.dirty_pages, 2); + assert_eq!(stats.saved_bytes, 2 * page_size); + } +} diff --git a/vendor/cube-hypervisor/vmm/src/vm.rs b/vendor/cube-hypervisor/vmm/src/vm.rs new file mode 100644 index 0000000..30f5bfb --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/vm.rs @@ -0,0 +1,3486 @@ +// Copyright © 2020, Oracle and/or its affiliates. +// +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Portions Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause +// + +use crate::config::{add_to_config, ValidationError}; +#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] +use crate::coredump::{ + CpuElf64Writable, DumpState, Elf64Writable, GuestDebuggable, GuestDebuggableError, NoteDescType, +}; +use crate::cpu; +use crate::device_manager::{Console, DeviceManager, DeviceManagerError, PtyPair}; +use crate::device_tree::DeviceTree; +#[cfg(feature = "guest_debug")] +use crate::gdb::{Debuggable, DebuggableError, GdbRequestPayload, GdbResponsePayload}; +use crate::memory_manager::{ + Error as MemoryManagerError, MemoryManager, MemoryManagerSnapshotData, +}; +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +use crate::migration::get_vm_snapshot; +#[cfg(feature = "guest_debug")] +use crate::migration::url_to_file; +use crate::migration::{url_to_path, SNAPSHOT_CONFIG_FILE, SNAPSHOT_STATE_FILE}; +use crate::seccomp_filters::{get_seccomp_filter, Thread}; +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +use crate::vm_config::CompatibleMode; +use crate::vm_config::{ + DeviceConfig, DiskConfig, FsConfig, HotplugMethod, NetConfig, NumaConfig, PayloadConfig, + PmemConfig, UserDeviceConfig, VdpaConfig, VmConfig, VsockConfig, +}; +use crate::GuestMemoryMmap; +use crate::{ + PciDeviceInfo, CPU_MANAGER_SNAPSHOT_ID, DEVICE_MANAGER_SNAPSHOT_ID, MEMORY_MANAGER_SNAPSHOT_ID, +}; +use anyhow::anyhow; +use arch::get_host_cpu_phys_bits; +#[cfg(target_arch = "x86_64")] +use arch::layout::{KVM_IDENTITY_MAP_START, KVM_TSS_START}; +#[cfg(feature = "tdx")] +use arch::x86_64::tdx::TdvfSection; +use arch::EntryPoint; +#[cfg(target_arch = "aarch64")] +use arch::PciSpaceInfo; +use arch::{NumaNode, NumaNodes}; +#[cfg(target_arch = "aarch64")] +use devices::gic::{Gic, GIC_V3_ITS_SNAPSHOT_ID}; +#[cfg(target_arch = "aarch64")] +use devices::interrupt_controller::{self, InterruptController}; +use devices::AcpiNotificationFlags; +use event_notifier::{event_notify, NotifyEvent}; +#[cfg(all(target_arch = "aarch64", feature = "guest_debug"))] +use gdbstub_arch::aarch64::reg::AArch64CoreRegs as CoreRegs; +#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] +use gdbstub_arch::x86::reg::X86_64CoreRegs as CoreRegs; +use hypervisor::HypervisorType; +use hypervisor::{HypervisorVmError, VmOps}; +use linux_loader::cmdline::Cmdline; +#[cfg(feature = "guest_debug")] +use linux_loader::elf; +#[cfg(target_arch = "x86_64")] +use linux_loader::loader::elf::PvhBootCapability::PvhEntryPresent; +#[cfg(target_arch = "aarch64")] +use linux_loader::loader::pe::Error::InvalidImageMagicNumber; +use linux_loader::loader::KernelLoader; +use seccompiler::{apply_filter, SeccompAction}; +use serde::{Deserialize, Serialize}; +use signal_hook::{consts::SIGWINCH, iterator::backend::Handle, iterator::Signals}; +use std::cmp; +use std::collections::BTreeMap; +use std::collections::HashMap; +use std::convert::TryInto; +use std::fs::{File, OpenOptions}; +use std::io::{self, Seek, SeekFrom, Write}; +#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] +use std::mem::size_of; +use std::num::Wrapping; +use std::ops::Deref; +use std::os::unix::net::UnixStream; +use std::panic::AssertUnwindSafe; +use std::sync::atomic::AtomicBool; +use std::sync::{Arc, Mutex, RwLock}; +use std::time::Instant; +use std::{result, str, thread}; +use thiserror::Error; +use tracer::trace_scoped; +use vm_device::Bus; +#[cfg(target_arch = "x86_64")] +use vm_device::BusDevice; +#[cfg(feature = "tdx")] +use vm_memory::{Address, ByteValued, GuestMemoryRegion, ReadVolatile}; +use vm_memory::{ + Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic, WriteVolatile, +}; +use vm_migration::protocol::{Request, Response, Status}; +use vm_migration::{ + protocol::MemoryRangeTable, snapshot_from_id, Migratable, MigratableError, Pausable, Snapshot, + SnapshotConfig, Snapshottable, Transportable, +}; +use vmm_sys_util::eventfd::EventFd; +use vmm_sys_util::signal::unblock_signal; +use vmm_sys_util::sock_ctrl_msg::ScmSocket; +use vmm_sys_util::terminal::Terminal; + +/// Errors associated with VM management +#[derive(Debug, Error)] +pub enum Error { + #[error("Cannot open kernel file: {0}")] + KernelFile(#[source] io::Error), + + #[error("Cannot open initramfs file: {0}")] + InitramfsFile(#[source] io::Error), + + #[error("Cannot load the kernel into memory: {0}")] + KernelLoad(#[source] linux_loader::loader::Error), + + #[cfg(target_arch = "aarch64")] + #[error("Cannot load the UEFI binary in memory: {0:?}")] + UefiLoad(arch::aarch64::uefi::Error), + + #[error("Cannot load the initramfs into memory")] + InitramfsLoad, + + #[error("Cannot load the kernel command line in memory: {0}")] + LoadCmdLine(#[source] linux_loader::loader::Error), + + #[error("Cannot modify the kernel command line: {0}")] + CmdLineInsertStr(#[source] linux_loader::cmdline::Error), + + #[error("Cannot create the kernel command line: {0}")] + CmdLineCreate(#[source] linux_loader::cmdline::Error), + + #[error("Cannot configure system: {0}")] + ConfigureSystem(#[source] arch::Error), + + #[cfg(target_arch = "aarch64")] + #[error("Cannot enable interrupt controller: {0:?}")] + EnableInterruptController(interrupt_controller::Error), + + #[error("VM state is poisoned")] + PoisonedState, + + #[error("Error from device manager: {0:?}")] + DeviceManager(DeviceManagerError), + + #[error("Cannot setup terminal in raw mode: {0}")] + SetTerminalRaw(#[source] vmm_sys_util::errno::Error), + + #[error("Cannot setup terminal in canonical mode.: {0}")] + SetTerminalCanon(#[source] vmm_sys_util::errno::Error), + + #[error("Cannot spawn a signal handler thread: {0}")] + SignalHandlerSpawn(#[source] io::Error), + + #[error("Failed to join on threads: {0:?}")] + ThreadCleanup(std::boxed::Box), + + #[error("VM config is missing")] + VmMissingConfig, + + #[error("VM is not created")] + VmNotCreated, + + #[error("VM is already created")] + VmAlreadyCreated, + + #[error("VM is not running")] + VmNotRunning, + + #[error("Cannot clone EventFd: {0}")] + EventFdClone(#[source] io::Error), + + #[error("invalid VM state transition: {0:?} to {1:?}")] + InvalidStateTransition(VmState, VmState), + + #[error("Error from CPU manager: {0}")] + CpuManager(#[source] cpu::Error), + + #[error("Cannot pause devices: {0}")] + PauseDevices(#[source] MigratableError), + + #[error("Cannot resume devices: {0}")] + ResumeDevices(#[source] MigratableError), + + #[error("Cannot pause CPUs: {0}")] + PauseCpus(#[source] MigratableError), + + #[error("Cannot resume cpus: {0}")] + ResumeCpus(#[source] MigratableError), + + #[error("Cannot pause VM: {0}")] + Pause(#[source] MigratableError), + + #[error("Cannot resume VM: {0}")] + Resume(#[source] MigratableError), + + #[error("Memory manager error: {0:?}")] + MemoryManager(MemoryManagerError), + + #[error("Eventfd write error: {0}")] + EventfdError(#[source] std::io::Error), + + #[error("Cannot snapshot VM: {0}")] + Snapshot(#[source] MigratableError), + + #[error("Cannot restore VM: {0}")] + Restore(#[source] MigratableError), + + #[error("Cannot send VM snapshot: {0}")] + SnapshotSend(#[source] MigratableError), + + #[error("Invalid restore source URL")] + InvalidRestoreSourceUrl, + + #[error("Failed to validate config: {0}")] + ConfigValidation(#[source] ValidationError), + + #[error("Too many virtio-vsock devices")] + TooManyVsockDevices, + + #[error("Failed serializing into JSON: {0}")] + SerializeJson(#[source] serde_json::Error), + + #[error("Invalid NUMA configuration")] + InvalidNumaConfig, + + #[error("Cannot create seccomp filter: {0}")] + CreateSeccompFilter(#[source] seccompiler::Error), + + #[error("Cannot apply seccomp filter: {0}")] + ApplySeccompFilter(#[source] seccompiler::Error), + + #[error("Failed resizing a memory zone")] + ResizeZone, + + #[error("Cannot activate virtio devices: {0:?}")] + ActivateVirtioDevices(DeviceManagerError), + + #[error("Error triggering power button: {0:?}")] + PowerButton(DeviceManagerError), + + #[error("Kernel lacks PVH header")] + KernelMissingPvhHeader, + + #[error("Failed to allocate firmware RAM: {0:?}")] + AllocateFirmwareMemory(MemoryManagerError), + + #[error("Error manipulating firmware file: {0}")] + FirmwareFile(#[source] std::io::Error), + + #[error("Firmware too big")] + FirmwareTooLarge, + + #[error("Failed to copy firmware to memory: {0}")] + FirmwareLoad(#[source] vm_memory::GuestMemoryError), + + #[cfg(feature = "tdx")] + #[error("Error performing I/O on TDX firmware file: {0}")] + LoadTdvf(#[source] std::io::Error), + + #[cfg(feature = "tdx")] + #[error("Error performing I/O on the TDX payload file: {0}")] + LoadPayload(#[source] std::io::Error), + + #[cfg(feature = "tdx")] + #[error("Error parsing TDVF: {0}")] + ParseTdvf(#[source] arch::x86_64::tdx::TdvfError), + + #[cfg(feature = "tdx")] + #[error("Error populating TDX HOB: {0}")] + PopulateHob(#[source] arch::x86_64::tdx::TdvfError), + + #[cfg(feature = "tdx")] + #[error("Error allocating TDVF memory: {0:?}")] + AllocatingTdvfMemory(crate::memory_manager::Error), + + #[cfg(feature = "tdx")] + #[error("Error enabling TDX VM: {0}")] + InitializeTdxVm(#[source] hypervisor::HypervisorVmError), + + #[cfg(feature = "tdx")] + #[error("Error enabling TDX memory region: {0}")] + InitializeTdxMemoryRegion(#[source] hypervisor::HypervisorVmError), + + #[cfg(feature = "tdx")] + #[error("Error finalizing TDX VM: {0}")] + FinalizeTdx(#[source] hypervisor::HypervisorVmError), + + #[cfg(feature = "tdx")] + #[error("TDX firmware missing")] + TdxFirmwareMissing, + + #[cfg(feature = "tdx")] + #[error("Invalid TDX payload type")] + InvalidPayloadType, + + #[cfg(feature = "guest_debug")] + #[error("Error debugging VM: {0:?}")] + Debug(DebuggableError), + + #[error("Error spawning kernel loading thread")] + KernelLoadThreadSpawn(std::io::Error), + + #[error("Error joining kernel loading thread")] + KernelLoadThreadJoin(std::boxed::Box), + + #[error("Payload configuration is not bootable")] + InvalidPayload, + + #[cfg(feature = "guest_debug")] + #[error("Error coredumping VM: {0:?}")] + Coredump(GuestDebuggableError), +} +pub type Result = result::Result; + +#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] +pub enum VmState { + Created, + Running, + Shutdown, + Paused, + BreakPoint, +} + +impl VmState { + fn valid_transition(self, new_state: VmState) -> Result<()> { + match self { + VmState::Created => match new_state { + VmState::Created | VmState::Shutdown => { + Err(Error::InvalidStateTransition(self, new_state)) + } + VmState::Running | VmState::Paused | VmState::BreakPoint => Ok(()), + }, + + VmState::Running => match new_state { + VmState::Created | VmState::Running => { + Err(Error::InvalidStateTransition(self, new_state)) + } + VmState::Paused | VmState::Shutdown | VmState::BreakPoint => Ok(()), + }, + + VmState::Shutdown => match new_state { + VmState::Paused | VmState::Created | VmState::Shutdown | VmState::BreakPoint => { + Err(Error::InvalidStateTransition(self, new_state)) + } + VmState::Running => Ok(()), + }, + + VmState::Paused => match new_state { + VmState::Created | VmState::Paused | VmState::BreakPoint => { + Err(Error::InvalidStateTransition(self, new_state)) + } + VmState::Running | VmState::Shutdown => Ok(()), + }, + VmState::BreakPoint => match new_state { + VmState::Created | VmState::Running => Ok(()), + _ => Err(Error::InvalidStateTransition(self, new_state)), + }, + } + } +} + +struct VmOpsHandler { + memory: GuestMemoryAtomic, + #[cfg(target_arch = "x86_64")] + io_bus: Arc, + mmio_bus: Arc, + #[cfg(target_arch = "x86_64")] + pci_config_io: Arc>, +} + +impl VmOps for VmOpsHandler { + fn guest_mem_write(&self, gpa: u64, buf: &[u8]) -> result::Result { + self.memory + .memory() + .write(buf, GuestAddress(gpa)) + .map_err(|e| HypervisorVmError::GuestMemWrite(e.into())) + } + + fn guest_mem_read(&self, gpa: u64, buf: &mut [u8]) -> result::Result { + self.memory + .memory() + .read(buf, GuestAddress(gpa)) + .map_err(|e| HypervisorVmError::GuestMemRead(e.into())) + } + + fn mmio_read(&self, gpa: u64, data: &mut [u8]) -> result::Result<(), HypervisorVmError> { + if let Err(vm_device::BusError::MissingAddressRange) = self.mmio_bus.read(gpa, data) { + info!("Guest MMIO read to unregistered address 0x{:x}", gpa); + } + Ok(()) + } + + fn mmio_write(&self, gpa: u64, data: &[u8]) -> result::Result<(), HypervisorVmError> { + match self.mmio_bus.write(gpa, data) { + Err(vm_device::BusError::MissingAddressRange) => { + info!("Guest MMIO write to unregistered address 0x{:x}", gpa); + } + Ok(Some(barrier)) => { + info!("Waiting for barrier"); + barrier.wait(); + info!("Barrier released"); + } + _ => {} + }; + Ok(()) + } + + #[cfg(target_arch = "x86_64")] + fn pio_read(&self, port: u64, data: &mut [u8]) -> result::Result<(), HypervisorVmError> { + use pci::{PCI_CONFIG_IO_PORT, PCI_CONFIG_IO_PORT_SIZE}; + + if (PCI_CONFIG_IO_PORT..(PCI_CONFIG_IO_PORT + PCI_CONFIG_IO_PORT_SIZE)).contains(&port) { + self.pci_config_io.lock().unwrap().read( + PCI_CONFIG_IO_PORT, + port - PCI_CONFIG_IO_PORT, + data, + ); + return Ok(()); + } + + if let Err(vm_device::BusError::MissingAddressRange) = self.io_bus.read(port, data) { + info!("Guest PIO read to unregistered address 0x{:x}", port); + } + Ok(()) + } + + #[cfg(target_arch = "x86_64")] + fn pio_write(&self, port: u64, data: &[u8]) -> result::Result<(), HypervisorVmError> { + use pci::{PCI_CONFIG_IO_PORT, PCI_CONFIG_IO_PORT_SIZE}; + + if (PCI_CONFIG_IO_PORT..(PCI_CONFIG_IO_PORT + PCI_CONFIG_IO_PORT_SIZE)).contains(&port) { + self.pci_config_io.lock().unwrap().write( + PCI_CONFIG_IO_PORT, + port - PCI_CONFIG_IO_PORT, + data, + ); + return Ok(()); + } + + match self.io_bus.write(port, data) { + Err(vm_device::BusError::MissingAddressRange) => { + info!("Guest PIO write to unregistered address 0x{:x}", port); + } + Ok(Some(barrier)) => { + info!("Waiting for barrier"); + barrier.wait(); + info!("Barrier released"); + } + _ => {} + }; + Ok(()) + } +} + +pub fn physical_bits(max_phys_bits: u8, hypervisor_type: HypervisorType) -> u8 { + let host_phys_bits = get_host_cpu_phys_bits(); + + let guest_phys_bits = if matches!(hypervisor_type, HypervisorType::KvmPvm) { + cmp::min(max_phys_bits, 43) + } else { + max_phys_bits + }; + + debug!( + "physical bits {:?} for hypervisor {:?}", + guest_phys_bits, hypervisor_type + ); + + cmp::min(host_phys_bits, guest_phys_bits) +} + +pub struct Vm { + #[cfg(feature = "tdx")] + kernel: Option, + initramfs: Option, + threads: Vec>, + device_manager: Arc>, + config: Arc>, + on_tty: bool, + signals: Option, + state: RwLock, + cpu_manager: Arc>, + memory_manager: Arc>, + #[cfg_attr(not(feature = "kvm"), allow(dead_code))] + // The hypervisor abstracted virtual machine. + vm: Arc, + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + saved_clock: Option, + numa_nodes: NumaNodes, + seccomp_action: SeccompAction, + exit_evt: EventFd, + hypervisor: Arc, + stop_on_boot: bool, + load_payload_handle: Option>>, +} + +impl Vm { + pub const HANDLED_SIGNALS: [i32; 1] = [SIGWINCH]; + + #[allow(clippy::too_many_arguments)] + pub fn new_from_memory_manager( + config: Arc>, + memory_manager: Arc>, + vm: Arc, + exit_evt: EventFd, + reset_evt: EventFd, + #[cfg(feature = "guest_debug")] vm_debug_evt: EventFd, + seccomp_action: &SeccompAction, + hypervisor: Arc, + activate_evt: EventFd, + restoring: bool, + timestamp: Instant, + snapshot: Option<&Snapshot>, + sandbox_id: String, + vcpu_started: Arc, + ) -> Result { + trace_scoped!("Vm::new_from_memory_manager"); + + let boot_id_list = config + .lock() + .unwrap() + .validate() + .map_err(Error::ConfigValidation)?; + + let load_payload_handle = if !restoring { + Self::load_payload_async(&memory_manager, &config)? + } else { + None + }; + + info!("Booting VM from config: {:?}", &config); + + // Create NUMA nodes based on NumaConfig. + let numa_nodes = + Self::create_numa_nodes(config.lock().unwrap().numa.clone(), &memory_manager)?; + + #[cfg(feature = "tdx")] + let tdx_enabled = config.lock().unwrap().is_tdx_enabled(); + #[cfg(feature = "tdx")] + let force_iommu = tdx_enabled; + #[cfg(not(feature = "tdx"))] + let force_iommu = false; + + #[cfg(feature = "guest_debug")] + let stop_on_boot = config.lock().unwrap().gdb; + #[cfg(not(feature = "guest_debug"))] + let stop_on_boot = false; + + let device_manager = DeviceManager::new( + hypervisor.hypervisor_type(), + vm.clone(), + config.clone(), + memory_manager.clone(), + &exit_evt, + &reset_evt, + seccomp_action.clone(), + numa_nodes.clone(), + &activate_evt, + force_iommu, + restoring, + boot_id_list, + timestamp, + snapshot_from_id(snapshot, DEVICE_MANAGER_SNAPSHOT_ID), + sandbox_id, + ) + .map_err(Error::DeviceManager)?; + + let memory = memory_manager.lock().unwrap().guest_memory(); + #[cfg(target_arch = "x86_64")] + let io_bus = Arc::clone(device_manager.lock().unwrap().io_bus()); + let mmio_bus = Arc::clone(device_manager.lock().unwrap().mmio_bus()); + + #[cfg(target_arch = "x86_64")] + let pci_config_io = + device_manager.lock().unwrap().pci_config_io() as Arc>; + let vm_ops: Arc = Arc::new(VmOpsHandler { + memory, + #[cfg(target_arch = "x86_64")] + io_bus, + mmio_bus, + #[cfg(target_arch = "x86_64")] + pci_config_io, + }); + + let exit_evt_clone = exit_evt.try_clone().map_err(Error::EventFdClone)?; + let cpus_config = { &config.lock().unwrap().cpus.clone() }; + let cpu_manager = cpu::CpuManager::new( + cpus_config, + &device_manager, + &memory_manager, + vm.clone(), + exit_evt_clone, + reset_evt, + #[cfg(feature = "guest_debug")] + vm_debug_evt, + hypervisor.clone(), + seccomp_action.clone(), + vm_ops, + #[cfg(feature = "tdx")] + tdx_enabled, + &numa_nodes, + vcpu_started, + ) + .map_err(Error::CpuManager)?; + + let on_tty = unsafe { libc::isatty(libc::STDIN_FILENO) } != 0; + + #[cfg(feature = "tdx")] + let kernel = config + .lock() + .unwrap() + .payload + .as_ref() + .map(|p| p.kernel.as_ref().map(File::open)) + .unwrap_or_default() + .transpose() + .map_err(Error::KernelFile)?; + + let initramfs = config + .lock() + .unwrap() + .payload + .as_ref() + .map(|p| p.initramfs.as_ref().map(File::open)) + .unwrap_or_default() + .transpose() + .map_err(Error::InitramfsFile)?; + + Ok(Vm { + #[cfg(feature = "tdx")] + kernel, + initramfs, + device_manager, + config, + on_tty, + threads: Vec::with_capacity(1), + signals: None, + state: RwLock::new(VmState::Created), + cpu_manager, + memory_manager, + vm, + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + saved_clock: None, + numa_nodes, + seccomp_action: seccomp_action.clone(), + exit_evt, + hypervisor, + stop_on_boot, + load_payload_handle, + }) + } + + fn create_numa_nodes( + configs: Option>, + memory_manager: &Arc>, + ) -> Result { + let mm = memory_manager.lock().unwrap(); + let mm_zones = mm.memory_zones(); + let mut numa_nodes = BTreeMap::new(); + + if let Some(configs) = &configs { + for config in configs.iter() { + if numa_nodes.contains_key(&config.guest_numa_id) { + error!("Can't define twice the same NUMA node"); + return Err(Error::InvalidNumaConfig); + } + + let mut node = NumaNode::default(); + + if let Some(memory_zones) = &config.memory_zones { + for memory_zone in memory_zones.iter() { + if let Some(mm_zone) = mm_zones.get(memory_zone) { + node.memory_regions.extend(mm_zone.regions().clone()); + if let Some(virtiomem_zone) = mm_zone.virtio_mem_zone() { + node.hotplug_regions.push(virtiomem_zone.region().clone()); + } + node.memory_zones.push(memory_zone.clone()); + } else { + error!("Unknown memory zone '{}'", memory_zone); + return Err(Error::InvalidNumaConfig); + } + } + } + + if let Some(cpus) = &config.cpus { + node.cpus.extend(cpus); + } + + if let Some(distances) = &config.distances { + for distance in distances.iter() { + let dest = distance.destination; + let dist = distance.distance; + + if !configs.iter().any(|cfg| cfg.guest_numa_id == dest) { + error!("Unknown destination NUMA node {}", dest); + return Err(Error::InvalidNumaConfig); + } + + if node.distances.contains_key(&dest) { + error!("Destination NUMA node {} has been already set", dest); + return Err(Error::InvalidNumaConfig); + } + + node.distances.insert(dest, dist); + } + } + + #[cfg(target_arch = "x86_64")] + if let Some(sgx_epc_sections) = &config.sgx_epc_sections { + if let Some(sgx_epc_region) = mm.sgx_epc_region() { + let mm_sections = sgx_epc_region.epc_sections(); + for sgx_epc_section in sgx_epc_sections.iter() { + if let Some(mm_section) = mm_sections.get(sgx_epc_section) { + node.sgx_epc_sections.push(mm_section.clone()); + } else { + error!("Unknown SGX EPC section '{}'", sgx_epc_section); + return Err(Error::InvalidNumaConfig); + } + } + } else { + error!("Missing SGX EPC region"); + return Err(Error::InvalidNumaConfig); + } + } + + numa_nodes.insert(config.guest_numa_id, node); + } + } + + Ok(numa_nodes) + } + + #[allow(clippy::too_many_arguments)] + pub fn new( + config: Arc>, + exit_evt: EventFd, + reset_evt: EventFd, + #[cfg(feature = "guest_debug")] vm_debug_evt: EventFd, + seccomp_action: &SeccompAction, + hypervisor: Arc, + activate_evt: EventFd, + serial_pty: Option, + console_pty: Option, + console_resize_pipe: Option, + sandbox_id: String, + vcpu_started: Arc, + ) -> Result { + trace_scoped!("Vm::new"); + + let timestamp = Instant::now(); + + #[cfg(feature = "tdx")] + let tdx_enabled = config.lock().unwrap().is_tdx_enabled(); + + let vm = Self::create_hypervisor_vm( + &hypervisor, + #[cfg(feature = "tdx")] + tdx_enabled, + )?; + + let phys_bits = physical_bits( + config.lock().unwrap().cpus.max_phys_bits, + hypervisor.hypervisor_type(), + ); + + #[cfg(target_arch = "x86_64")] + let sgx_epc_config = config.lock().unwrap().sgx_epc.clone(); + + let memory_manager = MemoryManager::new( + vm.clone(), + &config.lock().unwrap().memory.clone(), + None, + phys_bits, + #[cfg(feature = "tdx")] + tdx_enabled, + None, + None, + None, + #[cfg(target_arch = "x86_64")] + sgx_epc_config, + ) + .map_err(Error::MemoryManager)?; + + let new_vm = Vm::new_from_memory_manager( + config, + memory_manager, + vm, + exit_evt, + reset_evt, + #[cfg(feature = "guest_debug")] + vm_debug_evt, + seccomp_action, + hypervisor, + activate_evt, + false, + timestamp, + None, + sandbox_id, + vcpu_started, + )?; + + // The device manager must create the devices from here as it is part + // of the regular code path creating everything from scratch. + new_vm + .device_manager + .lock() + .unwrap() + .create_devices(serial_pty, console_pty, console_resize_pipe) + .map_err(Error::DeviceManager)?; + Ok(new_vm) + } + + #[allow(clippy::too_many_arguments)] + pub fn new_from_snapshot( + snapshot: &Snapshot, + vm_config: Arc>, + exit_evt: EventFd, + reset_evt: EventFd, + #[cfg(feature = "guest_debug")] vm_debug_evt: EventFd, + source_url: Option<&str>, + prefault: bool, + seccomp_action: &SeccompAction, + hypervisor: Arc, + activate_evt: EventFd, + sandbox_id: String, + vcpu_started: Arc, + memory_vol_url: Option<&str>, + ) -> Result { + let timestamp = Instant::now(); + + let vm = Self::create_hypervisor_vm( + &hypervisor, + #[cfg(feature = "tdx")] + false, + )?; + + let memory_manager = if let Some(memory_manager_snapshot) = + snapshot.snapshots.get(MEMORY_MANAGER_SNAPSHOT_ID) + { + let phys_bits = physical_bits( + vm_config.lock().unwrap().cpus.max_phys_bits, + hypervisor.hypervisor_type(), + ); + MemoryManager::new_from_snapshot( + memory_manager_snapshot, + vm.clone(), + &vm_config.lock().unwrap().memory.clone(), + source_url, + prefault, + phys_bits, + memory_vol_url, + ) + .map_err(Error::MemoryManager)? + } else { + return Err(Error::Restore(MigratableError::Restore(anyhow!( + "Missing memory manager snapshot" + )))); + }; + + Vm::new_from_memory_manager( + vm_config, + memory_manager, + vm, + exit_evt, + reset_evt, + #[cfg(feature = "guest_debug")] + vm_debug_evt, + seccomp_action, + hypervisor, + activate_evt, + true, + timestamp, + Some(snapshot), + sandbox_id, + vcpu_started, + ) + } + + pub fn create_hypervisor_vm( + hypervisor: &Arc, + #[cfg(feature = "tdx")] tdx_enabled: bool, + ) -> Result> { + hypervisor.check_required_extensions().unwrap(); + + #[cfg(feature = "tdx")] + let vm = hypervisor + .create_vm_with_type(if tdx_enabled { + 2 // KVM_X86_TDX_VM + } else { + 0 // KVM_X86_LEGACY_VM + }) + .unwrap(); + #[cfg(not(feature = "tdx"))] + let vm = hypervisor.create_vm().unwrap(); + + #[cfg(target_arch = "x86_64")] + { + vm.set_identity_map_address(KVM_IDENTITY_MAP_START.0) + .unwrap(); + vm.set_tss_address(KVM_TSS_START.0 as usize).unwrap(); + vm.enable_split_irq().unwrap(); + } + + Ok(vm) + } + + fn load_initramfs(&mut self, guest_mem: &GuestMemoryMmap) -> Result { + let initramfs = self.initramfs.as_mut().unwrap(); + let size: usize = initramfs + .seek(SeekFrom::End(0)) + .map_err(|_| Error::InitramfsLoad)? + .try_into() + .unwrap(); + initramfs + .seek(SeekFrom::Start(0)) + .map_err(|_| Error::InitramfsLoad)?; + + let address = + arch::initramfs_load_addr(guest_mem, size).map_err(|_| Error::InitramfsLoad)?; + let address = GuestAddress(address); + + guest_mem + .read_volatile_from(address, initramfs, size) + .map_err(|_| Error::InitramfsLoad)?; + + info!("Initramfs loaded: address = 0x{:x}", address.0); + Ok(arch::InitramfsConfig { address, size }) + } + + fn generate_cmdline( + payload: &PayloadConfig, + #[cfg(target_arch = "aarch64")] device_manager: &Arc>, + ) -> Result { + let mut cmdline = Cmdline::new(arch::CMDLINE_MAX_SIZE).map_err(Error::CmdLineCreate)?; + if let Some(s) = payload.cmdline.as_ref() { + cmdline.insert_str(s).map_err(Error::CmdLineInsertStr)?; + } + + #[cfg(target_arch = "aarch64")] + for entry in device_manager.lock().unwrap().cmdline_additions() { + cmdline.insert_str(entry).map_err(Error::CmdLineInsertStr)?; + } + Ok(cmdline) + } + + #[cfg(target_arch = "aarch64")] + fn load_firmware(mut firmware: &File, memory_manager: Arc>) -> Result<()> { + let uefi_flash = memory_manager.lock().as_ref().unwrap().uefi_flash(); + let mem = uefi_flash.memory(); + arch::aarch64::uefi::load_uefi(mem.deref(), arch::layout::UEFI_START, &mut firmware) + .map_err(Error::UefiLoad)?; + Ok(()) + } + + #[cfg(target_arch = "aarch64")] + fn load_kernel( + firmware: Option, + kernel: Option, + memory_manager: Arc>, + ) -> Result { + let guest_memory = memory_manager.lock().as_ref().unwrap().guest_memory(); + let mem = guest_memory.memory(); + let entry_addr = match (firmware, kernel) { + (None, Some(mut kernel)) => { + match linux_loader::loader::pe::PE::load( + mem.deref(), + Some(arch::layout::KERNEL_START), + &mut kernel, + None, + ) { + Ok(entry_addr) => entry_addr.kernel_load, + // Try to load the binary as kernel PE file at first. + // If failed, retry to load it as UEFI binary. + // As the UEFI binary is formatless, it must be the last option to try. + Err(linux_loader::loader::Error::Pe(InvalidImageMagicNumber)) => { + Self::load_firmware(&kernel, memory_manager)?; + arch::layout::UEFI_START + } + Err(e) => { + return Err(Error::KernelLoad(e)); + } + } + } + (Some(firmware), None) => { + Self::load_firmware(&firmware, memory_manager)?; + arch::layout::UEFI_START + } + _ => return Err(Error::InvalidPayload), + }; + + Ok(EntryPoint { entry_addr }) + } + + #[cfg(target_arch = "x86_64")] + fn load_kernel( + mut kernel: File, + cmdline: Option, + memory_manager: Arc>, + ) -> Result { + info!("Loading kernel"); + + let mem = { + let guest_memory = memory_manager.lock().as_ref().unwrap().guest_memory(); + guest_memory.memory() + }; + let entry_addr = linux_loader::loader::elf::Elf::load( + mem.deref(), + None, + &mut kernel, + Some(arch::layout::HIGH_RAM_START), + ) + .map_err(Error::KernelLoad)?; + + if let Some(cmdline) = cmdline { + linux_loader::loader::load_cmdline(mem.deref(), arch::layout::CMDLINE_START, &cmdline) + .map_err(Error::LoadCmdLine)?; + } + + if let PvhEntryPresent(entry_addr) = entry_addr.pvh_boot_cap { + // Use the PVH kernel entry point to boot the guest + info!("Kernel loaded: entry_addr = 0x{:x}", entry_addr.0); + Ok(EntryPoint { + entry_addr: Some(entry_addr), + }) + } else { + Err(Error::KernelMissingPvhHeader) + } + } + + #[cfg(target_arch = "x86_64")] + fn load_payload( + payload: &PayloadConfig, + memory_manager: Arc>, + ) -> Result { + trace_scoped!("load_payload"); + match ( + &payload.firmware, + &payload.kernel, + &payload.initramfs, + &payload.cmdline, + ) { + (Some(firmware), None, None, None) => { + let firmware = File::open(firmware).map_err(Error::FirmwareFile)?; + Self::load_kernel(firmware, None, memory_manager) + } + (None, Some(kernel), _, _) => { + let kernel = File::open(kernel).map_err(Error::KernelFile)?; + let cmdline = Self::generate_cmdline(payload)?; + Self::load_kernel(kernel, Some(cmdline), memory_manager) + } + _ => Err(Error::InvalidPayload), + } + } + + #[cfg(target_arch = "aarch64")] + fn load_payload( + payload: &PayloadConfig, + memory_manager: Arc>, + ) -> Result { + match (&payload.firmware, &payload.kernel) { + (Some(firmware), None) => { + let firmware = File::open(firmware).map_err(Error::FirmwareFile)?; + Self::load_kernel(Some(firmware), None, memory_manager) + } + (None, Some(kernel)) => { + let kernel = File::open(kernel).map_err(Error::KernelFile)?; + Self::load_kernel(None, Some(kernel), memory_manager) + } + _ => Err(Error::InvalidPayload), + } + } + + fn load_payload_async( + memory_manager: &Arc>, + config: &Arc>, + ) -> Result>>> { + // Kernel with TDX is loaded in a different manner + #[cfg(feature = "tdx")] + if config.lock().unwrap().is_tdx_enabled() { + return Ok(None); + } + + config + .lock() + .unwrap() + .payload + .as_ref() + .map(|payload| { + let memory_manager = memory_manager.clone(); + let payload = payload.clone(); + + std::thread::Builder::new() + .name("payload_loader".into()) + .spawn(move || Self::load_payload(&payload, memory_manager)) + .map_err(Error::KernelLoadThreadSpawn) + }) + .transpose() + } + + #[cfg(target_arch = "x86_64")] + fn configure_system(&mut self, rsdp_addr: GuestAddress) -> Result<()> { + trace_scoped!("configure_system"); + info!("Configuring system"); + let mem = self.memory_manager.lock().unwrap().boot_guest_memory(); + + let initramfs_config = match self.initramfs { + Some(_) => Some(self.load_initramfs(&mem)?), + None => None, + }; + + let boot_vcpus = self.cpu_manager.lock().unwrap().boot_vcpus(); + let rsdp_addr = Some(rsdp_addr); + let sgx_epc_region = self + .memory_manager + .lock() + .unwrap() + .sgx_epc_region() + .as_ref() + .cloned(); + + let serial_number = self + .config + .lock() + .unwrap() + .platform + .as_ref() + .and_then(|p| p.serial_number.clone()); + + let uuid = self + .config + .lock() + .unwrap() + .platform + .as_ref() + .and_then(|p| p.uuid.clone()); + + let oem_strings = self + .config + .lock() + .unwrap() + .platform + .as_ref() + .and_then(|p| p.oem_strings.clone()); + + let oem_strings = oem_strings + .as_deref() + .map(|strings| strings.iter().map(|s| s.as_ref()).collect::>()); + + arch::configure_system( + &mem, + arch::layout::CMDLINE_START, + &initramfs_config, + boot_vcpus, + rsdp_addr, + sgx_epc_region, + serial_number.as_deref(), + uuid.as_deref(), + oem_strings.as_deref(), + ) + .map_err(Error::ConfigureSystem)?; + Ok(()) + } + + #[cfg(target_arch = "aarch64")] + fn configure_system(&mut self, _rsdp_addr: GuestAddress) -> Result<()> { + let cmdline = Self::generate_cmdline( + self.config.lock().unwrap().payload.as_ref().unwrap(), + &self.device_manager, + )?; + let vcpu_mpidrs = self.cpu_manager.lock().unwrap().get_mpidrs(); + let vcpu_topology = self.cpu_manager.lock().unwrap().get_vcpu_topology(); + let mem = self.memory_manager.lock().unwrap().boot_guest_memory(); + let mut pci_space_info: Vec = Vec::new(); + let initramfs_config = match self.initramfs { + Some(_) => Some(self.load_initramfs(&mem)?), + None => None, + }; + + let device_info = &self + .device_manager + .lock() + .unwrap() + .get_device_info() + .clone(); + + for pci_segment in self.device_manager.lock().unwrap().pci_segments().iter() { + let pci_space = PciSpaceInfo { + pci_segment_id: pci_segment.id, + mmio_config_address: pci_segment.mmio_config_address, + pci_device_space_start: pci_segment.start_of_device_area, + pci_device_space_size: pci_segment.end_of_device_area + - pci_segment.start_of_device_area + + 1, + }; + pci_space_info.push(pci_space); + } + + let virtio_iommu_bdf = self + .device_manager + .lock() + .unwrap() + .iommu_attached_devices() + .as_ref() + .map(|(v, _)| *v); + + let vcpu_count = self.cpu_manager.lock().unwrap().boot_vcpus() as u64; + let vgic = self + .device_manager + .lock() + .unwrap() + .get_interrupt_controller() + .unwrap() + .lock() + .unwrap() + .create_vgic( + &self.memory_manager.lock().as_ref().unwrap().vm, + Gic::create_default_config(vcpu_count), + ) + .map_err(|_| { + Error::ConfigureSystem(arch::Error::PlatformSpecific( + arch::aarch64::Error::SetupGic, + )) + })?; + + // PMU interrupt sticks to PPI, so need to be added by 16 to get real irq number. + let pmu_supported = self + .cpu_manager + .lock() + .unwrap() + .init_pmu(arch::aarch64::fdt::AARCH64_PMU_IRQ + 16) + .map_err(|_| { + Error::ConfigureSystem(arch::Error::PlatformSpecific( + arch::aarch64::Error::VcpuInitPmu, + )) + })?; + + arch::configure_system( + &mem, + cmdline.as_cstring().unwrap().to_str().unwrap(), + vcpu_mpidrs, + vcpu_topology, + device_info, + &initramfs_config, + &pci_space_info, + virtio_iommu_bdf.map(|bdf| bdf.into()), + &vgic, + &self.numa_nodes, + pmu_supported, + ) + .map_err(Error::ConfigureSystem)?; + + // Activate gic device + self.device_manager + .lock() + .unwrap() + .get_interrupt_controller() + .unwrap() + .lock() + .unwrap() + .enable() + .map_err(Error::EnableInterruptController)?; + + Ok(()) + } + + pub fn serial_pty(&self) -> Option { + self.device_manager.lock().unwrap().serial_pty() + } + + pub fn console_pty(&self) -> Option { + self.device_manager.lock().unwrap().console_pty() + } + + pub fn console_resize_pipe(&self) -> Option> { + self.device_manager.lock().unwrap().console_resize_pipe() + } + + pub fn fs_device_update(&self, fs_cfg: FsConfig) -> Result<()> { + self.device_manager + .lock() + .unwrap() + .fs_device_update(fs_cfg) + .map_err(Error::DeviceManager)?; + + Ok(()) + } + + pub fn shutdown(&mut self) -> Result<()> { + let mut state = self.state.try_write().map_err(|_| Error::PoisonedState)?; + let new_state = VmState::Shutdown; + + state.valid_transition(new_state)?; + + if self.on_tty { + // Don't forget to set the terminal in canonical mode + // before to exit. + io::stdin() + .lock() + .set_canon_mode() + .map_err(Error::SetTerminalCanon)?; + } + + // Trigger the termination of the signal_handler thread + if let Some(signals) = self.signals.take() { + signals.close(); + } + + // Wake up the DeviceManager threads so they will get terminated cleanly + self.device_manager + .lock() + .unwrap() + .resume() + .map_err(Error::Resume)?; + + self.cpu_manager + .lock() + .unwrap() + .shutdown() + .map_err(Error::CpuManager)?; + + // Wait for all the threads to finish + for thread in self.threads.drain(..) { + thread.join().map_err(Error::ThreadCleanup)? + } + *state = new_state; + + event!("vm", "shutdown"); + event_notify!(NotifyEvent::VmShutdown); + + Ok(()) + } + + pub fn sys_started(&self) -> bool { + self.device_manager.lock().unwrap().sys_started() + } + + pub fn resize( + &mut self, + desired_vcpus: Option, + desired_memory: Option, + desired_balloon: Option, + ) -> Result<()> { + event!("vm", "resizing"); + + if let Some(desired_vcpus) = desired_vcpus { + if self + .cpu_manager + .lock() + .unwrap() + .resize(desired_vcpus) + .map_err(Error::CpuManager)? + { + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::CPU_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + } + self.config.lock().unwrap().cpus.boot_vcpus = desired_vcpus; + } + + if let Some(desired_memory) = desired_memory { + let new_region = self + .memory_manager + .lock() + .unwrap() + .resize(desired_memory) + .map_err(Error::MemoryManager)?; + + let memory_config = &mut self.config.lock().unwrap().memory; + + if let Some(new_region) = &new_region { + self.device_manager + .lock() + .unwrap() + .update_memory(new_region) + .map_err(Error::DeviceManager)?; + + match memory_config.hotplug_method { + HotplugMethod::Acpi => { + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::MEMORY_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + } + HotplugMethod::VirtioMem => {} + } + } + + // We update the VM config regardless of the actual guest resize + // operation result (happened or not), so that if the VM reboots + // it will be running with the last configure memory size. + match memory_config.hotplug_method { + HotplugMethod::Acpi => memory_config.size = desired_memory, + HotplugMethod::VirtioMem => { + if desired_memory > memory_config.size { + memory_config.hotplugged_size = Some(desired_memory - memory_config.size); + } else { + memory_config.hotplugged_size = None; + } + } + } + } + + if let Some(desired_balloon) = desired_balloon { + self.device_manager + .lock() + .unwrap() + .resize_balloon(desired_balloon) + .map_err(Error::DeviceManager)?; + + // Update the configuration value for the balloon size to ensure + // a reboot would use the right value. + if let Some(balloon_config) = &mut self.config.lock().unwrap().balloon { + balloon_config.size = desired_balloon; + } + } + + event!("vm", "resized"); + + Ok(()) + } + + pub fn resize_zone(&mut self, id: String, desired_memory: u64) -> Result<()> { + let memory_config = &mut self.config.lock().unwrap().memory; + + if let Some(zones) = &mut memory_config.zones { + for zone in zones.iter_mut() { + if zone.id == id { + if desired_memory >= zone.size { + let hotplugged_size = desired_memory - zone.size; + self.memory_manager + .lock() + .unwrap() + .resize_zone(&id, desired_memory - zone.size) + .map_err(Error::MemoryManager)?; + // We update the memory zone config regardless of the + // actual 'resize-zone' operation result (happened or + // not), so that if the VM reboots it will be running + // with the last configured memory zone size. + zone.hotplugged_size = Some(hotplugged_size); + + return Ok(()); + } else { + error!( + "Invalid to ask less ({}) than boot RAM ({}) for \ + this memory zone", + desired_memory, zone.size, + ); + return Err(Error::ResizeZone); + } + } + } + } + + error!("Could not find the memory zone {} for the resize", id); + Err(Error::ResizeZone) + } + + pub fn add_device(&mut self, mut device_cfg: DeviceConfig) -> Result { + let pci_device_info = self + .device_manager + .lock() + .unwrap() + .add_device(&mut device_cfg) + .map_err(Error::DeviceManager)?; + + // Update VmConfig by adding the new device. This is important to + // ensure the device would be created in case of a reboot. + { + let mut config = self.config.lock().unwrap(); + add_to_config(&mut config.devices, device_cfg); + } + + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::PCI_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + + Ok(pci_device_info) + } + + pub fn add_user_device(&mut self, mut device_cfg: UserDeviceConfig) -> Result { + let pci_device_info = self + .device_manager + .lock() + .unwrap() + .add_user_device(&mut device_cfg) + .map_err(Error::DeviceManager)?; + + // Update VmConfig by adding the new device. This is important to + // ensure the device would be created in case of a reboot. + { + let mut config = self.config.lock().unwrap(); + add_to_config(&mut config.user_devices, device_cfg); + } + + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::PCI_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + + Ok(pci_device_info) + } + + pub fn remove_device(&mut self, id: String) -> Result<()> { + self.device_manager + .lock() + .unwrap() + .remove_device(id.clone()) + .map_err(Error::DeviceManager)?; + + // Update VmConfig by removing the device. This is important to + // ensure the device would not be created in case of a reboot. + let mut config = self.config.lock().unwrap(); + + // Remove if VFIO device + if let Some(devices) = config.devices.as_mut() { + devices.retain(|dev| dev.id.as_ref() != Some(&id)); + } + + // Remove if VFIO user device + if let Some(user_devices) = config.user_devices.as_mut() { + user_devices.retain(|dev| dev.id.as_ref() != Some(&id)); + } + + // Remove if disk device + if let Some(disks) = config.disks.as_mut() { + disks.retain(|dev| dev.id.as_ref() != Some(&id)); + } + + // Remove if fs device + if let Some(fs) = config.fs.as_mut() { + fs.retain(|dev| dev.id.as_ref() != Some(&id)); + } + + // Remove if net device + if let Some(net) = config.net.as_mut() { + net.retain(|dev| dev.id.as_ref() != Some(&id)); + } + + // Remove if pmem device + if let Some(pmem) = config.pmem.as_mut() { + pmem.retain(|dev| dev.id.as_ref() != Some(&id)); + } + + // Remove if vDPA device + if let Some(vdpa) = config.vdpa.as_mut() { + vdpa.retain(|dev| dev.id.as_ref() != Some(&id)); + } + + // Remove if vsock device + if let Some(vsock) = config.vsock.as_ref() { + if vsock.id.as_ref() == Some(&id) { + config.vsock = None; + } + } + + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::PCI_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + Ok(()) + } + + pub fn add_disk(&mut self, mut disk_cfg: DiskConfig) -> Result { + let pci_device_info = self + .device_manager + .lock() + .unwrap() + .add_disk(&mut disk_cfg) + .map_err(Error::DeviceManager)?; + + // Update VmConfig by adding the new device. This is important to + // ensure the device would be created in case of a reboot. + { + let mut config = self.config.lock().unwrap(); + add_to_config(&mut config.disks, disk_cfg); + } + + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::PCI_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + + Ok(pci_device_info) + } + + pub fn add_fs(&mut self, mut fs_cfg: FsConfig) -> Result { + let pci_device_info = self + .device_manager + .lock() + .unwrap() + .add_fs(&mut fs_cfg) + .map_err(Error::DeviceManager)?; + + // Update VmConfig by adding the new device. This is important to + // ensure the device would be created in case of a reboot. + { + let mut config = self.config.lock().unwrap(); + add_to_config(&mut config.fs, fs_cfg); + } + + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::PCI_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + + Ok(pci_device_info) + } + + pub fn add_pmem(&mut self, mut pmem_cfg: PmemConfig) -> Result { + let pci_device_info = self + .device_manager + .lock() + .unwrap() + .add_pmem(&mut pmem_cfg) + .map_err(Error::DeviceManager)?; + + // Update VmConfig by adding the new device. This is important to + // ensure the device would be created in case of a reboot. + { + let mut config = self.config.lock().unwrap(); + add_to_config(&mut config.pmem, pmem_cfg); + } + + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::PCI_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + + Ok(pci_device_info) + } + + pub fn add_net(&mut self, mut net_cfg: NetConfig) -> Result { + let pci_device_info = self + .device_manager + .lock() + .unwrap() + .add_net(&mut net_cfg) + .map_err(Error::DeviceManager)?; + + // Update VmConfig by adding the new device. This is important to + // ensure the device would be created in case of a reboot. + { + let mut config = self.config.lock().unwrap(); + add_to_config(&mut config.net, net_cfg); + } + + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::PCI_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + + Ok(pci_device_info) + } + + pub fn add_vdpa(&mut self, mut vdpa_cfg: VdpaConfig) -> Result { + let pci_device_info = self + .device_manager + .lock() + .unwrap() + .add_vdpa(&mut vdpa_cfg) + .map_err(Error::DeviceManager)?; + + // Update VmConfig by adding the new device. This is important to + // ensure the device would be created in case of a reboot. + { + let mut config = self.config.lock().unwrap(); + add_to_config(&mut config.vdpa, vdpa_cfg); + } + + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::PCI_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + + Ok(pci_device_info) + } + + pub fn add_vsock(&mut self, mut vsock_cfg: VsockConfig) -> Result { + let pci_device_info = self + .device_manager + .lock() + .unwrap() + .add_vsock(&mut vsock_cfg) + .map_err(Error::DeviceManager)?; + + // Update VmConfig by adding the new device. This is important to + // ensure the device would be created in case of a reboot. + { + let mut config = self.config.lock().unwrap(); + config.vsock = Some(vsock_cfg); + } + + self.device_manager + .lock() + .unwrap() + .notify_hotplug(AcpiNotificationFlags::PCI_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + + Ok(pci_device_info) + } + + pub fn counters(&self) -> Result>>> { + Ok(self.device_manager.lock().unwrap().counters()) + } + + fn signal_handler(mut signals: Signals, console_input_clone: Arc) { + for sig in &Vm::HANDLED_SIGNALS { + unblock_signal(*sig).unwrap(); + } + + for signal in signals.forever() { + if signal == SIGWINCH { + console_input_clone.update_console_size(); + } + } + } + + #[cfg(feature = "tdx")] + fn init_tdx(&mut self) -> Result<()> { + let cpuid = self.cpu_manager.lock().unwrap().common_cpuid(); + let max_vcpus = self.cpu_manager.lock().unwrap().max_vcpus() as u32; + self.vm + .tdx_init(&cpuid, max_vcpus) + .map_err(Error::InitializeTdxVm)?; + Ok(()) + } + + #[cfg(feature = "tdx")] + fn extract_tdvf_sections(&mut self) -> Result> { + use arch::x86_64::tdx::*; + + let firmware_path = self + .config + .lock() + .unwrap() + .payload + .as_ref() + .unwrap() + .firmware + .clone() + .ok_or(Error::TdxFirmwareMissing)?; + // The TDVF file contains a table of section as well as code + let mut firmware_file = File::open(firmware_path).map_err(Error::LoadTdvf)?; + + // For all the sections allocate some RAM backing them + parse_tdvf_sections(&mut firmware_file).map_err(Error::ParseTdvf) + } + + #[cfg(feature = "tdx")] + fn hob_memory_resources( + mut sorted_sections: Vec, + guest_memory: &GuestMemoryMmap, + ) -> Vec<(u64, u64, bool)> { + let mut list = Vec::new(); + + let mut current_section = sorted_sections.pop(); + + // RAM regions interleaved with TDVF sections + let mut next_start_addr = 0; + for region in guest_memory.iter() { + let region_start = region.start_addr().0; + let region_end = region.last_addr().0; + if region_start > next_start_addr { + next_start_addr = region_start; + } + + loop { + let (start, size, ram) = if let Some(section) = ¤t_section { + if section.address <= next_start_addr { + (section.address, section.size, false) + } else { + let last_addr = std::cmp::min(section.address - 1, region_end); + (next_start_addr, last_addr - next_start_addr + 1, true) + } + } else { + (next_start_addr, region_end - next_start_addr + 1, true) + }; + + list.push((start, size, ram)); + + if !ram { + current_section = sorted_sections.pop(); + } + + next_start_addr = start + size; + + if region_start > next_start_addr { + next_start_addr = region_start; + } + + if next_start_addr > region_end { + break; + } + } + } + + // Once all the interleaved sections have been processed, let's simply + // pull the remaining ones. + if let Some(section) = current_section { + list.push((section.address, section.size, false)); + } + while let Some(section) = sorted_sections.pop() { + list.push((section.address, section.size, false)); + } + + list + } + + #[cfg(feature = "tdx")] + fn populate_tdx_sections(&mut self, sections: &[TdvfSection]) -> Result> { + use arch::x86_64::tdx::*; + // Get the memory end *before* we start adding TDVF ram regions + let boot_guest_memory = self + .memory_manager + .lock() + .as_ref() + .unwrap() + .boot_guest_memory(); + for section in sections { + // No need to allocate if the section falls within guest RAM ranges + if boot_guest_memory.address_in_range(GuestAddress(section.address)) { + info!( + "Not allocating TDVF Section: {:x?} since it is already part of guest RAM", + section + ); + continue; + } + + info!("Allocating TDVF Section: {:x?}", section); + self.memory_manager + .lock() + .unwrap() + .add_ram_region(GuestAddress(section.address), section.size as usize) + .map_err(Error::AllocatingTdvfMemory)?; + } + + // The TDVF file contains a table of section as well as code + let firmware_path = self + .config + .lock() + .unwrap() + .payload + .as_ref() + .unwrap() + .firmware + .clone() + .ok_or(Error::TdxFirmwareMissing)?; + let mut firmware_file = File::open(firmware_path).map_err(Error::LoadTdvf)?; + + // The guest memory at this point now has all the required regions so it + // is safe to copy from the TDVF file into it. + let guest_memory = self.memory_manager.lock().as_ref().unwrap().guest_memory(); + let mem = guest_memory.memory(); + let mut payload_info = None; + let mut hob_offset = None; + for section in sections { + info!("Populating TDVF Section: {:x?}", section); + match section.r#type { + TdvfSectionType::Bfv | TdvfSectionType::Cfv => { + info!("Copying section to guest memory"); + firmware_file + .seek(SeekFrom::Start(section.data_offset as u64)) + .map_err(Error::LoadTdvf)?; + mem.read_volatile_from( + GuestAddress(section.address), + &mut firmware_file, + section.data_size as usize, + ) + .unwrap(); + } + TdvfSectionType::TdHob => { + hob_offset = Some(section.address); + } + TdvfSectionType::Payload => { + info!("Copying payload to guest memory"); + if let Some(payload_file) = self.kernel.as_mut() { + let payload_size = payload_file + .seek(SeekFrom::End(0)) + .map_err(Error::LoadPayload)?; + + payload_file + .seek(SeekFrom::Start(0x1f1)) + .map_err(Error::LoadPayload)?; + + let mut payload_header = linux_loader::bootparam::setup_header::default(); + payload_file + .read_volatile(&mut payload_header.as_bytes()) + .unwrap(); + + if payload_header.header != 0x5372_6448 { + return Err(Error::InvalidPayloadType); + } + + if (payload_header.version < 0x0200) + || ((payload_header.loadflags & 0x1) == 0x0) + { + return Err(Error::InvalidPayloadType); + } + + payload_file + .seek(SeekFrom::Start(0)) + .map_err(Error::LoadPayload)?; + mem.read_volatile_from( + GuestAddress(section.address), + payload_file, + payload_size as usize, + ) + .unwrap(); + + // Create the payload info that will be inserted into + // the HOB. + payload_info = Some(PayloadInfo { + image_type: PayloadImageType::BzImage, + entry_point: section.address, + }); + } + } + TdvfSectionType::PayloadParam => { + info!("Copying payload parameters to guest memory"); + let cmdline = Self::generate_cmdline( + self.config.lock().unwrap().payload.as_ref().unwrap(), + )?; + mem.write_slice( + cmdline.as_cstring().unwrap().as_bytes_with_nul(), + GuestAddress(section.address), + ) + .unwrap(); + } + _ => {} + } + } + + // Generate HOB + let mut hob = TdHob::start(hob_offset.unwrap()); + + let mut sorted_sections = sections.to_vec(); + sorted_sections.retain(|section| matches!(section.r#type, TdvfSectionType::TempMem)); + + sorted_sections.sort_by_key(|section| section.address); + sorted_sections.reverse(); + + for (start, size, ram) in Vm::hob_memory_resources(sorted_sections, &boot_guest_memory) { + hob.add_memory_resource(&mem, start, size, ram) + .map_err(Error::PopulateHob)?; + } + + // MMIO regions + hob.add_mmio_resource( + &mem, + arch::layout::MEM_32BIT_DEVICES_START.raw_value(), + arch::layout::APIC_START.raw_value() + - arch::layout::MEM_32BIT_DEVICES_START.raw_value(), + ) + .map_err(Error::PopulateHob)?; + let start_of_device_area = self + .memory_manager + .lock() + .unwrap() + .start_of_device_area() + .raw_value(); + let end_of_device_area = self + .memory_manager + .lock() + .unwrap() + .end_of_device_area() + .raw_value(); + hob.add_mmio_resource( + &mem, + start_of_device_area, + end_of_device_area - start_of_device_area, + ) + .map_err(Error::PopulateHob)?; + + // Loop over the ACPI tables and copy them to the HOB. + + for acpi_table in crate::acpi::create_acpi_tables_tdx( + &self.device_manager, + &self.cpu_manager, + &self.memory_manager, + &self.numa_nodes, + ) { + hob.add_acpi_table(&mem, acpi_table.as_slice()) + .map_err(Error::PopulateHob)?; + } + + // If a payload info has been created, let's insert it into the HOB. + if let Some(payload_info) = payload_info { + hob.add_payload(&mem, payload_info) + .map_err(Error::PopulateHob)?; + } + + hob.finish(&mem).map_err(Error::PopulateHob)?; + + Ok(hob_offset) + } + + #[cfg(feature = "tdx")] + fn init_tdx_memory(&mut self, sections: &[TdvfSection]) -> Result<()> { + let guest_memory = self.memory_manager.lock().as_ref().unwrap().guest_memory(); + let mem = guest_memory.memory(); + + for section in sections { + self.vm + .tdx_init_memory_region( + mem.get_host_address(GuestAddress(section.address)).unwrap() as u64, + section.address, + section.size, + /* TDVF_SECTION_ATTRIBUTES_EXTENDMR */ + section.attributes == 1, + ) + .map_err(Error::InitializeTdxMemoryRegion)?; + } + + Ok(()) + } + + fn setup_signal_handler(&mut self) -> Result<()> { + let console = self.device_manager.lock().unwrap().console().clone(); + let signals = Signals::new(Vm::HANDLED_SIGNALS); + + if !console.need_resize() { + return Ok(()); + } + + match signals { + Ok(signals) => { + self.signals = Some(signals.handle()); + let exit_evt = self.exit_evt.try_clone().map_err(Error::EventFdClone)?; + let signal_handler_seccomp_filter = get_seccomp_filter( + &self.seccomp_action, + Thread::SignalHandler, + self.hypervisor.hypervisor_type(), + ) + .map_err(Error::CreateSeccompFilter)?; + self.threads.push( + thread::Builder::new() + .name("vm_signal_handler".to_string()) + .spawn(move || { + if !signal_handler_seccomp_filter.is_empty() { + if let Err(e) = apply_filter(&signal_handler_seccomp_filter) + .map_err(Error::ApplySeccompFilter) + { + error!("Error applying seccomp filter: {:?}", e); + exit_evt.write(1).ok(); + return; + } + } + std::panic::catch_unwind(AssertUnwindSafe(|| { + Vm::signal_handler(signals, console); + })) + .map_err(|_| { + error!("signal_handler thead panicked"); + exit_evt.write(1).ok() + }) + .ok(); + }) + .map_err(Error::SignalHandlerSpawn)?, + ); + } + Err(e) => error!("Signal not found {}", e), + } + Ok(()) + } + + fn setup_tty(&self) -> Result<()> { + if self.on_tty { + io::stdin() + .lock() + .set_raw_mode() + .map_err(Error::SetTerminalRaw)?; + } + + Ok(()) + } + + // Creates ACPI tables + // In case of TDX being used, this is a no-op since the tables will be + // created and passed when populating the HOB. + + fn create_acpi_tables(&self) -> Option { + #[cfg(feature = "tdx")] + if self.config.lock().unwrap().is_tdx_enabled() { + return None; + } + let mem = self.memory_manager.lock().unwrap().guest_memory().memory(); + let tpm_enabled = self.config.lock().unwrap().tpm.is_some(); + let rsdp_addr = crate::acpi::create_acpi_tables( + &mem, + &self.device_manager, + &self.cpu_manager, + &self.memory_manager, + &self.numa_nodes, + tpm_enabled, + ); + info!("Created ACPI tables: rsdp_addr = 0x{:x}", rsdp_addr.0); + + Some(rsdp_addr) + } + + fn entry_point(&mut self) -> Result> { + trace_scoped!("entry_point"); + + self.load_payload_handle + .take() + .map(|handle| handle.join().map_err(Error::KernelLoadThreadJoin)?) + .transpose() + } + + pub fn boot(&mut self) -> Result<()> { + trace_scoped!("Vm::boot"); + info!("Booting VM"); + event!("vm", "booting"); + let current_state = self.get_state()?; + if current_state == VmState::Paused { + return self.resume().map_err(Error::Resume); + } + + let new_state = if self.stop_on_boot { + VmState::BreakPoint + } else { + VmState::Running + }; + current_state.valid_transition(new_state)?; + + // Do earlier to parallelise with loading kernel + #[cfg(target_arch = "x86_64")] + let rsdp_addr = self.create_acpi_tables(); + + self.setup_signal_handler()?; + self.setup_tty()?; + + // Load kernel synchronously or if asynchronous then wait for load to + // finish. + let entry_point = self.entry_point()?; + + #[cfg(feature = "tdx")] + let tdx_enabled = self.config.lock().unwrap().is_tdx_enabled(); + + // The initial TDX configuration must be done before the vCPUs are + // created + #[cfg(feature = "tdx")] + if tdx_enabled { + self.init_tdx()?; + } + + // Create and configure vcpus + self.cpu_manager + .lock() + .unwrap() + .create_boot_vcpus(entry_point) + .map_err(Error::CpuManager)?; + + #[cfg(feature = "tdx")] + let sections = if tdx_enabled { + self.extract_tdvf_sections()? + } else { + Vec::new() + }; + + // Configuring the TDX regions requires that the vCPUs are created. + #[cfg(feature = "tdx")] + let hob_address = if tdx_enabled { + // TDX sections are written to memory. + self.populate_tdx_sections(§ions)? + } else { + None + }; + + // On aarch64 the ACPI tables depend on the vCPU mpidr which is only + // available after they are configured + #[cfg(target_arch = "aarch64")] + let rsdp_addr = self.create_acpi_tables(); + + // Configure shared state based on loaded kernel + entry_point + .map(|_| { + // Safe to unwrap rsdp_addr as we know it can't be None when + // the entry_point is Some. + self.configure_system(rsdp_addr.unwrap()) + }) + .transpose()?; + + #[cfg(feature = "tdx")] + if let Some(hob_address) = hob_address { + // With the HOB address extracted the vCPUs can have + // their TDX state configured. + self.cpu_manager + .lock() + .unwrap() + .initialize_tdx(hob_address) + .map_err(Error::CpuManager)?; + // Let the hypervisor know which memory ranges are shared with the + // guest. This prevents the guest from ignoring/discarding memory + // regions provided by the host. + self.init_tdx_memory(§ions)?; + // With TDX memory and CPU state configured TDX setup is complete + self.vm.tdx_finalize().map_err(Error::FinalizeTdx)?; + } + + self.cpu_manager + .lock() + .unwrap() + .start_boot_vcpus(new_state == VmState::BreakPoint) + .map_err(Error::CpuManager)?; + + let mut state = self.state.try_write().map_err(|_| Error::PoisonedState)?; + *state = new_state; + event!("vm", "booted"); + Ok(()) + } + + /// Gets a thread-safe reference counted pointer to the VM configuration. + pub fn get_config(&self) -> Arc> { + Arc::clone(&self.config) + } + + /// Get the VM state. Returns an error if the state is poisoned. + pub fn get_state(&self) -> Result { + self.state + .try_read() + .map_err(|_| Error::PoisonedState) + .map(|state| *state) + } + + /// Load saved clock from snapshot + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + pub fn load_clock_from_snapshot( + &mut self, + snapshot: &Snapshot, + ) -> Result> { + let vm_snapshot = get_vm_snapshot(snapshot).map_err(Error::Restore)?; + self.saved_clock = vm_snapshot.clock; + Ok(self.saved_clock) + } + + #[cfg(target_arch = "aarch64")] + /// Add the vGIC section to the VM snapshot. + fn add_vgic_snapshot_section( + &self, + vm_snapshot: &mut Snapshot, + ) -> std::result::Result<(), MigratableError> { + let saved_vcpu_states = self.cpu_manager.lock().unwrap().get_saved_states(); + self.device_manager + .lock() + .unwrap() + .get_interrupt_controller() + .unwrap() + .lock() + .unwrap() + .set_gicr_typers(&saved_vcpu_states); + + vm_snapshot.add_snapshot( + self.device_manager + .lock() + .unwrap() + .get_interrupt_controller() + .unwrap() + .lock() + .unwrap() + .snapshot()?, + ); + + Ok(()) + } + + #[cfg(target_arch = "aarch64")] + /// Restore the vGIC from the VM snapshot and enable the interrupt controller routing. + fn restore_vgic_and_enable_interrupt( + &self, + vm_snapshot: &Snapshot, + ) -> std::result::Result<(), MigratableError> { + let saved_vcpu_states = self.cpu_manager.lock().unwrap().get_saved_states(); + // The number of vCPUs is the same as the number of saved vCPU states. + let vcpu_numbers = saved_vcpu_states.len(); + + // Creating a GIC device here, as the GIC will not be created when + // restoring the device manager. Note that currently only the bare GICv3 + // without ITS is supported. + let vcpu_count = vcpu_numbers.try_into().unwrap(); + self.device_manager + .lock() + .unwrap() + .get_interrupt_controller() + .unwrap() + .lock() + .unwrap() + .create_vgic(&self.vm, Gic::create_default_config(vcpu_count)) + .map_err(|e| MigratableError::Restore(anyhow!("Could not create GIC: {:#?}", e)))?; + + // PMU interrupt sticks to PPI, so need to be added by 16 to get real irq number. + self.cpu_manager + .lock() + .unwrap() + .init_pmu(arch::aarch64::fdt::AARCH64_PMU_IRQ + 16) + .map_err(|e| MigratableError::Restore(anyhow!("Error init PMU: {:?}", e)))?; + + // Here we prepare the GICR_TYPER registers from the restored vCPU states. + self.device_manager + .lock() + .unwrap() + .get_interrupt_controller() + .unwrap() + .lock() + .unwrap() + .set_gicr_typers(&saved_vcpu_states); + + // Restore GIC states. + if let Some(gicv3_its_snapshot) = vm_snapshot.snapshots.get(GIC_V3_ITS_SNAPSHOT_ID) { + self.device_manager + .lock() + .unwrap() + .get_interrupt_controller() + .unwrap() + .lock() + .unwrap() + .restore(*gicv3_its_snapshot.clone())?; + } else { + return Err(MigratableError::Restore(anyhow!( + "Missing GicV3Its snapshot" + ))); + } + + // Activate gic device + self.device_manager + .lock() + .unwrap() + .get_interrupt_controller() + .unwrap() + .lock() + .unwrap() + .enable() + .map_err(|e| { + MigratableError::Restore(anyhow!( + "Could not enable interrupt controller routing: {:#?}", + e + )) + })?; + + Ok(()) + } + + /// Gets the actual size of the balloon. + pub fn balloon_size(&self) -> u64 { + self.device_manager.lock().unwrap().balloon_size() + } + + pub fn send_memory_fds( + &mut self, + socket: &mut UnixStream, + ) -> std::result::Result<(), MigratableError> { + for (slot, fd) in self + .memory_manager + .lock() + .unwrap() + .memory_slot_fds() + .drain() + { + Request::memory_fd(std::mem::size_of_val(&slot) as u64) + .write_to(socket) + .map_err(|e| { + MigratableError::MigrateSend(anyhow!("Error sending memory fd request: {}", e)) + })?; + socket + .send_with_fd(&slot.to_le_bytes()[..], fd) + .map_err(|e| { + MigratableError::MigrateSend(anyhow!("Error sending memory fd: {}", e)) + })?; + + let res = Response::read_from(socket)?; + if res.status() != Status::Ok { + warn!("Error during memory fd migration"); + Request::abandon().write_to(socket)?; + Response::read_from(socket).ok(); + return Err(MigratableError::MigrateSend(anyhow!( + "Error during memory fd migration" + ))); + } + } + + Ok(()) + } + + pub fn send_memory_regions( + &mut self, + ranges: &MemoryRangeTable, + fd: &mut F, + ) -> std::result::Result<(), MigratableError> + where + F: WriteVolatile, + { + let guest_memory = self.memory_manager.lock().as_ref().unwrap().guest_memory(); + let mem = guest_memory.memory(); + + for range in ranges.regions() { + let mut offset: u64 = 0; + // Here we are manually handling the retry in case we can't the + // whole region at once because we can't use the implementation + // from vm-memory::GuestMemory of write_all_to() as it is not + // following the correct behavior. For more info about this issue + // see: https://github.com/rust-vmm/vm-memory/issues/174 + loop { + let bytes_written = mem + .write_volatile_to( + GuestAddress(range.gpa + offset), + fd, + (range.length - offset) as usize, + ) + .map_err(|e| { + MigratableError::MigrateSend(anyhow!( + "Error transferring memory to socket: {}", + e + )) + })?; + offset += bytes_written as u64; + + if offset == range.length { + break; + } + } + } + + Ok(()) + } + + pub fn memory_range_table(&self) -> std::result::Result { + self.memory_manager + .lock() + .unwrap() + .memory_range_table(false) + } + + pub fn device_tree(&self) -> Arc> { + self.device_manager.lock().unwrap().device_tree() + } + + pub fn activate_virtio_devices(&self) -> Result<()> { + self.device_manager + .lock() + .unwrap() + .activate_virtio_devices() + .map_err(Error::ActivateVirtioDevices) + } + + #[cfg(target_arch = "x86_64")] + pub fn power_button(&self) -> Result<()> { + return self + .device_manager + .lock() + .unwrap() + .notify_power_button() + .map_err(Error::PowerButton); + } + + #[cfg(target_arch = "aarch64")] + pub fn power_button(&self) -> Result<()> { + self.device_manager + .lock() + .unwrap() + .notify_power_button() + .map_err(Error::PowerButton) + } + + pub fn memory_manager_data(&self) -> MemoryManagerSnapshotData { + self.memory_manager.lock().unwrap().snapshot_data() + } + + /// Reference the pause path holds past the Vm drop, deferring the + /// guest memory unmap and the KVM fd close past the pause reply. + pub(crate) fn memory_manager(&self) -> Arc> { + self.memory_manager.clone() + } + + #[cfg(feature = "guest_debug")] + pub fn debug_request( + &mut self, + gdb_request: &GdbRequestPayload, + cpu_id: usize, + ) -> Result { + use GdbRequestPayload::*; + match gdb_request { + SetSingleStep(single_step) => { + self.set_guest_debug(cpu_id, &[], *single_step) + .map_err(Error::Debug)?; + } + SetHwBreakPoint(addrs) => { + self.set_guest_debug(cpu_id, addrs, false) + .map_err(Error::Debug)?; + } + Pause => { + self.debug_pause().map_err(Error::Debug)?; + } + Resume => { + self.debug_resume().map_err(Error::Debug)?; + } + ReadRegs => { + let regs = self.read_regs(cpu_id).map_err(Error::Debug)?; + return Ok(GdbResponsePayload::RegValues(Box::new(regs))); + } + WriteRegs(regs) => { + self.write_regs(cpu_id, regs).map_err(Error::Debug)?; + } + ReadMem(vaddr, len) => { + let mem = self.read_mem(cpu_id, *vaddr, *len).map_err(Error::Debug)?; + return Ok(GdbResponsePayload::MemoryRegion(mem)); + } + WriteMem(vaddr, data) => { + self.write_mem(cpu_id, vaddr, data).map_err(Error::Debug)?; + } + ActiveVcpus => { + let active_vcpus = self.active_vcpus(); + return Ok(GdbResponsePayload::ActiveVcpus(active_vcpus)); + } + } + Ok(GdbResponsePayload::CommandComplete) + } + + #[cfg(feature = "guest_debug")] + fn get_dump_state( + &mut self, + destination_url: &str, + ) -> std::result::Result { + let nr_cpus = self.config.lock().unwrap().cpus.boot_vcpus as u32; + let elf_note_size = self.get_note_size(NoteDescType::ElfAndVmm, nr_cpus) as isize; + let mut elf_phdr_num = 1; + let elf_sh_info = 0; + let coredump_file_path = url_to_file(destination_url)?; + let mapping_num = self.memory_manager.lock().unwrap().num_guest_ram_mappings(); + + if mapping_num < UINT16_MAX - 2 { + elf_phdr_num += mapping_num as u16; + } else { + panic!("mapping num beyond 65535 not supported"); + } + let coredump_file = OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .open(coredump_file_path) + .map_err(|e| GuestDebuggableError::Coredump(e.into()))?; + + let mem_offset = self.coredump_get_mem_offset(elf_phdr_num, elf_note_size); + let mem_data = self + .memory_manager + .lock() + .unwrap() + .coredump_memory_regions(mem_offset); + + Ok(DumpState { + elf_note_size, + elf_phdr_num, + elf_sh_info, + mem_offset, + mem_info: Some(mem_data), + file: Some(coredump_file), + }) + } + + #[cfg(feature = "guest_debug")] + fn coredump_get_mem_offset(&self, phdr_num: u16, note_size: isize) -> u64 { + size_of::() as u64 + + note_size as u64 + + size_of::() as u64 * phdr_num as u64 + } +} + +impl Pausable for Vm { + fn pause(&mut self) -> std::result::Result<(), MigratableError> { + event!("vm", "pausing"); + let mut state = self + .state + .try_write() + .map_err(|e| MigratableError::Pause(anyhow!("Could not get VM state: {}", e)))?; + let new_state = VmState::Paused; + + state + .valid_transition(new_state) + .map_err(|e| MigratableError::Pause(anyhow!("Invalid transition: {:?}", e)))?; + + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + { + let mut clock = self + .vm + .get_clock() + .map_err(|e| MigratableError::Pause(anyhow!("Could not get VM clock: {}", e)))?; + clock.reset_flags(); + self.saved_clock = Some(clock); + } + + // Before pausing the vCPUs activate any pending virtio devices that might + // need activation between starting the pause (or e.g. a migration it's part of) + self.activate_virtio_devices().map_err(|e| { + MigratableError::Pause(anyhow!("Error activating pending virtio devices: {:?}", e)) + })?; + + self.cpu_manager.lock().unwrap().pause()?; + self.device_manager.lock().unwrap().pause()?; + + *state = new_state; + + event!("vm", "paused"); + Ok(()) + } + + fn resume(&mut self) -> std::result::Result<(), MigratableError> { + event!("vm", "resuming"); + let mut state = self + .state + .try_write() + .map_err(|e| MigratableError::Resume(anyhow!("Could not get VM state: {}", e)))?; + let new_state = VmState::Running; + + state + .valid_transition(new_state) + .map_err(|e| MigratableError::Resume(anyhow!("Invalid transition: {:?}", e)))?; + + self.cpu_manager.lock().unwrap().resume()?; + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + { + if let Some(clock) = &self.saved_clock { + self.vm.set_clock(clock).map_err(|e| { + MigratableError::Resume(anyhow!("Could not set VM clock: {}", e)) + })?; + } + } + self.device_manager.lock().unwrap().resume()?; + + // And we're back to the Running state. + *state = new_state; + info!("vm has been resumed"); + event!("vm", "resumed"); + Ok(()) + } +} + +#[derive(Serialize, Deserialize)] +pub struct VmSnapshot { + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + pub clock: Option, + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + pub common_cpuid: Vec, +} + +pub const VM_SNAPSHOT_ID: &str = "vm"; +impl Snapshottable for Vm { + fn id(&self) -> String { + VM_SNAPSHOT_ID.to_string() + } + + fn snapshot(&mut self) -> std::result::Result { + event!("vm", "snapshotting"); + + #[cfg(feature = "tdx")] + let tdx_enabled = self.config.lock().unwrap().is_tdx_enabled(); + + #[cfg(feature = "tdx")] + { + if tdx_enabled { + return Err(MigratableError::Snapshot(anyhow!( + "Snapshot not possible with TDX VM" + ))); + } + } + + let current_state = self.get_state().unwrap(); + if current_state != VmState::Paused { + return Err(MigratableError::Snapshot(anyhow!( + "Trying to snapshot while VM is running" + ))); + } + + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + let common_cpuid = { + let phys_bits = physical_bits( + self.config.lock().unwrap().cpus.max_phys_bits, + self.hypervisor.hypervisor_type(), + ); + let cpu_config = &mut self.config.lock().unwrap().cpus; + + let (vendor_compatible, arch_compatible) = match cpu_config.compatible { + CompatibleMode::Vendor => (true, false), + CompatibleMode::Max => (true, true), + CompatibleMode::Ignore => (false, false), + }; + + arch::generate_common_cpuid( + self.hypervisor.clone(), + None, + None, + phys_bits, + cpu_config.kvm_hyperv, + #[cfg(feature = "tdx")] + tdx_enabled, + vendor_compatible, + arch_compatible, + ) + .map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error generating common cpuid: {:?}", e)) + })? + }; + + let vm_snapshot_state = VmSnapshot { + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + clock: self.saved_clock, + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + common_cpuid, + }; + let mut vm_snapshot = Snapshot::new_from_state(VM_SNAPSHOT_ID, &vm_snapshot_state)?; + + vm_snapshot.add_snapshot(self.cpu_manager.lock().unwrap().snapshot()?); + vm_snapshot.add_snapshot(self.memory_manager.lock().unwrap().snapshot()?); + + #[cfg(target_arch = "aarch64")] + self.add_vgic_snapshot_section(&mut vm_snapshot) + .map_err(|e| MigratableError::Snapshot(e.into()))?; + + vm_snapshot.add_snapshot(self.device_manager.lock().unwrap().snapshot()?); + + event!("vm", "snapshotted"); + Ok(vm_snapshot) + } + + fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { + event!("vm", "restoring"); + + let current_state = self + .get_state() + .map_err(|e| MigratableError::Restore(anyhow!("Could not get VM state: {:#?}", e)))?; + let new_state = VmState::Paused; + current_state.valid_transition(new_state).map_err(|e| { + MigratableError::Restore(anyhow!("Could not restore VM state: {:#?}", e)) + })?; + + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + self.load_clock_from_snapshot(&snapshot) + .map_err(|e| MigratableError::Restore(anyhow!("Error restoring clock: {:?}", e)))?; + + if let Some(device_manager_snapshot) = snapshot.snapshots.get(DEVICE_MANAGER_SNAPSHOT_ID) { + self.device_manager + .lock() + .unwrap() + .restore(*device_manager_snapshot.clone())?; + } else { + return Err(MigratableError::Restore(anyhow!( + "Missing device manager snapshot" + ))); + } + + if let Some(cpu_manager_snapshot) = snapshot.snapshots.get(CPU_MANAGER_SNAPSHOT_ID) { + self.cpu_manager + .lock() + .unwrap() + .restore(*cpu_manager_snapshot.clone())?; + } else { + return Err(MigratableError::Restore(anyhow!( + "Missing CPU manager snapshot" + ))); + } + + #[cfg(target_arch = "aarch64")] + self.restore_vgic_and_enable_interrupt(&snapshot)?; + + if let Some(device_manager_snapshot) = snapshot.snapshots.get(DEVICE_MANAGER_SNAPSHOT_ID) { + self.device_manager + .lock() + .unwrap() + .restore_devices(*device_manager_snapshot.clone())?; + } else { + return Err(MigratableError::Restore(anyhow!( + "Missing device manager snapshot" + ))); + } + + // Now we can start all vCPUs from here. + self.cpu_manager + .lock() + .unwrap() + .start_restored_vcpus() + .map_err(|e| { + MigratableError::Restore(anyhow!("Cannot start restored vCPUs: {:#?}", e)) + })?; + + self.setup_signal_handler().map_err(|e| { + MigratableError::Restore(anyhow!("Could not setup signal handler: {:#?}", e)) + })?; + self.setup_tty() + .map_err(|e| MigratableError::Restore(anyhow!("Could not setup tty: {:#?}", e)))?; + + let mut state = self + .state + .try_write() + .map_err(|e| MigratableError::Restore(anyhow!("Could not set VM state: {:#?}", e)))?; + *state = new_state; + + info!("vm has been restored"); + event!("vm", "restored"); + Ok(()) + } +} + +impl Transportable for Vm { + fn send( + &self, + snapshot: &Snapshot, + config: &SnapshotConfig, + ) -> std::result::Result<(), MigratableError> { + let destination_url = &config.destination_url; + let mut snapshot_config_path = url_to_path(destination_url)?; + snapshot_config_path.push(SNAPSHOT_CONFIG_FILE); + + // Create the snapshot config file + let mut snapshot_config_file = OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .open(snapshot_config_path) + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + + // Serialize and write the snapshot config + let vm_config = serde_json::to_string(self.config.lock().unwrap().deref()) + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + + snapshot_config_file + .write(vm_config.as_bytes()) + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + + // Ensure config data is flushed to disk for cross-machine pause-snapshot. + snapshot_config_file + .sync_all() + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + + let mut snapshot_state_path = url_to_path(destination_url)?; + snapshot_state_path.push(SNAPSHOT_STATE_FILE); + + // Create the snapshot state file + let mut snapshot_state_file = OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .open(snapshot_state_path) + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + + // Clone the snapshot and add metadata. + let mut snapshot_with_meta = snapshot.clone(); + snapshot_with_meta.metadata = Some(vm_migration::SnapshotMetadata { + snapshot_type: config.snapshot_type, + }); + + let vm_state = serde_json::to_vec(&snapshot_with_meta) + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + + snapshot_state_file + .write(&vm_state) + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + // Ensure state data is flushed to disk for cross-machine pause-snapshot. + + snapshot_state_file + .sync_all() + .map_err(|e| MigratableError::MigrateSend(e.into()))?; + + // Tell the memory manager to also send/write its own snapshot. + if let Some(memory_manager_snapshot) = snapshot.snapshots.get(MEMORY_MANAGER_SNAPSHOT_ID) { + let mm_config = SnapshotConfig { + destination_url: destination_url.clone(), + snapshot_type: config.snapshot_type, + memory_vol_url: config.memory_vol_url.clone(), + }; + self.memory_manager + .lock() + .unwrap() + .send(&memory_manager_snapshot.clone(), &mm_config)?; + } else { + return Err(MigratableError::Restore(anyhow!( + "Missing memory manager snapshot" + ))); + } + + Ok(()) + } +} + +impl Migratable for Vm { + fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + self.memory_manager.lock().unwrap().start_dirty_log()?; + self.device_manager.lock().unwrap().start_dirty_log() + } + + fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> { + self.memory_manager.lock().unwrap().stop_dirty_log()?; + self.device_manager.lock().unwrap().stop_dirty_log() + } + + fn dirty_log(&mut self) -> std::result::Result { + Ok(MemoryRangeTable::new_from_tables(vec![ + self.memory_manager.lock().unwrap().dirty_log()?, + self.device_manager.lock().unwrap().dirty_log()?, + ])) + } + + fn start_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.memory_manager.lock().unwrap().start_migration()?; + self.device_manager.lock().unwrap().start_migration() + } + + fn complete_migration(&mut self) -> std::result::Result<(), MigratableError> { + self.memory_manager.lock().unwrap().complete_migration()?; + self.device_manager.lock().unwrap().complete_migration() + } +} + +#[cfg(feature = "guest_debug")] +impl Debuggable for Vm { + fn set_guest_debug( + &self, + cpu_id: usize, + addrs: &[GuestAddress], + singlestep: bool, + ) -> std::result::Result<(), DebuggableError> { + self.cpu_manager + .lock() + .unwrap() + .set_guest_debug(cpu_id, addrs, singlestep) + } + + fn debug_pause(&mut self) -> std::result::Result<(), DebuggableError> { + if *self.state.read().unwrap() == VmState::Running { + self.pause().map_err(DebuggableError::Pause)?; + } + + let mut state = self + .state + .try_write() + .map_err(|_| DebuggableError::PoisonedState)?; + *state = VmState::BreakPoint; + Ok(()) + } + + fn debug_resume(&mut self) -> std::result::Result<(), DebuggableError> { + if *self.state.read().unwrap() == VmState::BreakPoint { + self.resume().map_err(DebuggableError::Pause)?; + } + + Ok(()) + } + + fn read_regs(&self, cpu_id: usize) -> std::result::Result { + self.cpu_manager.lock().unwrap().read_regs(cpu_id) + } + + fn write_regs( + &self, + cpu_id: usize, + regs: &CoreRegs, + ) -> std::result::Result<(), DebuggableError> { + self.cpu_manager.lock().unwrap().write_regs(cpu_id, regs) + } + + fn read_mem( + &self, + cpu_id: usize, + vaddr: GuestAddress, + len: usize, + ) -> std::result::Result, DebuggableError> { + self.cpu_manager + .lock() + .unwrap() + .read_mem(cpu_id, vaddr, len) + } + + fn write_mem( + &self, + cpu_id: usize, + vaddr: &GuestAddress, + data: &[u8], + ) -> std::result::Result<(), DebuggableError> { + self.cpu_manager + .lock() + .unwrap() + .write_mem(cpu_id, vaddr, data) + } + + fn active_vcpus(&self) -> usize { + let active_vcpus = self.cpu_manager.lock().unwrap().active_vcpus(); + if active_vcpus > 0 { + active_vcpus + } else { + // The VM is not booted yet. Report boot_vcpus() instead. + self.cpu_manager.lock().unwrap().boot_vcpus() as usize + } + } +} + +#[cfg(feature = "guest_debug")] +pub const UINT16_MAX: u32 = 65535; + +#[cfg(feature = "guest_debug")] +impl Elf64Writable for Vm {} + +#[cfg(feature = "guest_debug")] +impl GuestDebuggable for Vm { + fn coredump(&mut self, destination_url: &str) -> std::result::Result<(), GuestDebuggableError> { + event!("vm", "coredumping"); + + #[cfg(feature = "tdx")] + { + if self.config.lock().unwrap().tdx.is_some() { + return Err(GuestDebuggableError::Coredump(anyhow!( + "Coredump not possible with TDX VM" + ))); + } + } + + let current_state = self.get_state().unwrap(); + if current_state != VmState::Paused { + return Err(GuestDebuggableError::Coredump(anyhow!( + "Trying to coredump while VM is running" + ))); + } + + let coredump_state = self.get_dump_state(destination_url)?; + + self.write_header(&coredump_state)?; + self.write_note(&coredump_state)?; + self.write_loads(&coredump_state)?; + + self.cpu_manager + .lock() + .unwrap() + .cpu_write_elf64_note(&coredump_state)?; + self.cpu_manager + .lock() + .unwrap() + .cpu_write_vmm_note(&coredump_state)?; + + self.memory_manager + .lock() + .unwrap() + .coredump_iterate_save_mem(&coredump_state) + } +} + +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +#[cfg(test)] +mod tests { + use super::*; + + fn test_vm_state_transitions(state: VmState) { + match state { + VmState::Created => { + // Check the transitions from Created + assert!(state.valid_transition(VmState::Created).is_err()); + assert!(state.valid_transition(VmState::Running).is_ok()); + assert!(state.valid_transition(VmState::Shutdown).is_err()); + assert!(state.valid_transition(VmState::Paused).is_ok()); + assert!(state.valid_transition(VmState::BreakPoint).is_ok()); + } + VmState::Running => { + // Check the transitions from Running + assert!(state.valid_transition(VmState::Created).is_err()); + assert!(state.valid_transition(VmState::Running).is_err()); + assert!(state.valid_transition(VmState::Shutdown).is_ok()); + assert!(state.valid_transition(VmState::Paused).is_ok()); + assert!(state.valid_transition(VmState::BreakPoint).is_ok()); + } + VmState::Shutdown => { + // Check the transitions from Shutdown + assert!(state.valid_transition(VmState::Created).is_err()); + assert!(state.valid_transition(VmState::Running).is_ok()); + assert!(state.valid_transition(VmState::Shutdown).is_err()); + assert!(state.valid_transition(VmState::Paused).is_err()); + assert!(state.valid_transition(VmState::BreakPoint).is_err()); + } + VmState::Paused => { + // Check the transitions from Paused + assert!(state.valid_transition(VmState::Created).is_err()); + assert!(state.valid_transition(VmState::Running).is_ok()); + assert!(state.valid_transition(VmState::Shutdown).is_ok()); + assert!(state.valid_transition(VmState::Paused).is_err()); + assert!(state.valid_transition(VmState::BreakPoint).is_err()); + } + VmState::BreakPoint => { + // Check the transitions from Breakpoint + assert!(state.valid_transition(VmState::Created).is_ok()); + assert!(state.valid_transition(VmState::Running).is_ok()); + assert!(state.valid_transition(VmState::Shutdown).is_err()); + assert!(state.valid_transition(VmState::Paused).is_err()); + assert!(state.valid_transition(VmState::BreakPoint).is_err()); + } + } + } + + #[test] + fn test_vm_created_transitions() { + test_vm_state_transitions(VmState::Created); + } + + #[test] + fn test_vm_running_transitions() { + test_vm_state_transitions(VmState::Running); + } + + #[test] + fn test_vm_shutdown_transitions() { + test_vm_state_transitions(VmState::Shutdown); + } + + #[test] + fn test_vm_paused_transitions() { + test_vm_state_transitions(VmState::Paused); + } + + #[cfg(feature = "tdx")] + #[test] + fn test_hob_memory_resources() { + // Case 1: Two TDVF sections in the middle of the RAM + let sections = vec![ + TdvfSection { + address: 0xc000, + size: 0x1000, + ..Default::default() + }, + TdvfSection { + address: 0x1000, + size: 0x4000, + ..Default::default() + }, + ]; + let guest_ranges: Vec<(GuestAddress, usize)> = vec![(GuestAddress(0), 0x1000_0000)]; + let expected = vec![ + (0, 0x1000, true), + (0x1000, 0x4000, false), + (0x5000, 0x7000, true), + (0xc000, 0x1000, false), + (0xd000, 0x0fff_3000, true), + ]; + assert_eq!( + expected, + Vm::hob_memory_resources( + sections, + &GuestMemoryMmap::from_ranges(&guest_ranges).unwrap() + ) + ); + + // Case 2: Two TDVF sections with no conflict with the RAM + let sections = vec![ + TdvfSection { + address: 0x1000_1000, + size: 0x1000, + ..Default::default() + }, + TdvfSection { + address: 0, + size: 0x1000, + ..Default::default() + }, + ]; + let guest_ranges: Vec<(GuestAddress, usize)> = vec![(GuestAddress(0x1000), 0x1000_0000)]; + let expected = vec![ + (0, 0x1000, false), + (0x1000, 0x1000_0000, true), + (0x1000_1000, 0x1000, false), + ]; + assert_eq!( + expected, + Vm::hob_memory_resources( + sections, + &GuestMemoryMmap::from_ranges(&guest_ranges).unwrap() + ) + ); + + // Case 3: Two TDVF sections with partial conflicts with the RAM + let sections = vec![ + TdvfSection { + address: 0x1000_0000, + size: 0x2000, + ..Default::default() + }, + TdvfSection { + address: 0, + size: 0x2000, + ..Default::default() + }, + ]; + let guest_ranges: Vec<(GuestAddress, usize)> = vec![(GuestAddress(0x1000), 0x1000_0000)]; + let expected = vec![ + (0, 0x2000, false), + (0x2000, 0x0fff_e000, true), + (0x1000_0000, 0x2000, false), + ]; + assert_eq!( + expected, + Vm::hob_memory_resources( + sections, + &GuestMemoryMmap::from_ranges(&guest_ranges).unwrap() + ) + ); + + // Case 4: Two TDVF sections with no conflict before the RAM and two + // more additional sections with no conflict after the RAM. + let sections = vec![ + TdvfSection { + address: 0x2000_1000, + size: 0x1000, + ..Default::default() + }, + TdvfSection { + address: 0x2000_0000, + size: 0x1000, + ..Default::default() + }, + TdvfSection { + address: 0x1000, + size: 0x1000, + ..Default::default() + }, + TdvfSection { + address: 0, + size: 0x1000, + ..Default::default() + }, + ]; + let guest_ranges: Vec<(GuestAddress, usize)> = vec![(GuestAddress(0x4000), 0x1000_0000)]; + let expected = vec![ + (0, 0x1000, false), + (0x1000, 0x1000, false), + (0x4000, 0x1000_0000, true), + (0x2000_0000, 0x1000, false), + (0x2000_1000, 0x1000, false), + ]; + assert_eq!( + expected, + Vm::hob_memory_resources( + sections, + &GuestMemoryMmap::from_ranges(&guest_ranges).unwrap() + ) + ); + + // Case 5: One TDVF section overriding the entire RAM + let sections = vec![TdvfSection { + address: 0, + size: 0x2000_0000, + ..Default::default() + }]; + let guest_ranges: Vec<(GuestAddress, usize)> = vec![(GuestAddress(0x1000), 0x1000_0000)]; + let expected = vec![(0, 0x2000_0000, false)]; + assert_eq!( + expected, + Vm::hob_memory_resources( + sections, + &GuestMemoryMmap::from_ranges(&guest_ranges).unwrap() + ) + ); + + // Case 6: Two TDVF sections with no conflict with 2 RAM regions + let sections = vec![ + TdvfSection { + address: 0x1000_2000, + size: 0x2000, + ..Default::default() + }, + TdvfSection { + address: 0, + size: 0x2000, + ..Default::default() + }, + ]; + let guest_ranges: Vec<(GuestAddress, usize)> = vec![ + (GuestAddress(0x2000), 0x1000_0000), + (GuestAddress(0x1000_4000), 0x1000_0000), + ]; + let expected = vec![ + (0, 0x2000, false), + (0x2000, 0x1000_0000, true), + (0x1000_2000, 0x2000, false), + (0x1000_4000, 0x1000_0000, true), + ]; + assert_eq!( + expected, + Vm::hob_memory_resources( + sections, + &GuestMemoryMmap::from_ranges(&guest_ranges).unwrap() + ) + ); + + // Case 7: Two TDVF sections with partial conflicts with 2 RAM regions + let sections = vec![ + TdvfSection { + address: 0x1000_0000, + size: 0x4000, + ..Default::default() + }, + TdvfSection { + address: 0, + size: 0x4000, + ..Default::default() + }, + ]; + let guest_ranges: Vec<(GuestAddress, usize)> = vec![ + (GuestAddress(0x1000), 0x1000_0000), + (GuestAddress(0x1000_3000), 0x1000_0000), + ]; + let expected = vec![ + (0, 0x4000, false), + (0x4000, 0x0fff_c000, true), + (0x1000_0000, 0x4000, false), + (0x1000_4000, 0x0fff_f000, true), + ]; + assert_eq!( + expected, + Vm::hob_memory_resources( + sections, + &GuestMemoryMmap::from_ranges(&guest_ranges).unwrap() + ) + ); + } +} + +#[cfg(target_arch = "aarch64")] +#[cfg(test)] +mod tests { + use super::*; + use crate::GuestMemoryMmap; + use arch::aarch64::fdt::create_fdt; + use arch::aarch64::layout; + use arch::{DeviceType, MmioDeviceInfo}; + + const LEN: u64 = 4096; + + #[test] + fn test_create_fdt_with_devices() { + let regions = vec![(layout::RAM_START, (layout::FDT_MAX_SIZE + 0x1000) as usize)]; + let mem = GuestMemoryMmap::from_ranges(®ions).expect("Cannot initialize memory"); + + let dev_info: HashMap<(DeviceType, std::string::String), MmioDeviceInfo> = [ + ( + (DeviceType::Serial, DeviceType::Serial.to_string()), + MmioDeviceInfo { + addr: 0x00, + len: LEN, + irq: 33, + }, + ), + ( + (DeviceType::Virtio(1), "virtio".to_string()), + MmioDeviceInfo { + addr: LEN, + len: LEN, + irq: 34, + }, + ), + ( + (DeviceType::Rtc, "rtc".to_string()), + MmioDeviceInfo { + addr: 2 * LEN, + len: LEN, + irq: 35, + }, + ), + ] + .iter() + .cloned() + .collect(); + + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().unwrap(); + let gic = vm + .create_vgic(Gic::create_default_config(1)) + .expect("Cannot create gic"); + assert!(create_fdt( + &mem, + "console=tty0", + vec![0], + Some((0, 0, 0)), + &dev_info, + &gic, + &None, + &Vec::new(), + &BTreeMap::new(), + None, + true, + ) + .is_ok()) + } +} + +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +#[test] +pub fn test_vm() { + use hypervisor::VmExit; + use vm_memory::{Address, GuestMemory, GuestMemoryRegion}; + // This example based on https://lwn.net/Articles/658511/ + let code = [ + 0xba, 0xf8, 0x03, /* mov $0x3f8, %dx */ + 0x00, 0xd8, /* add %bl, %al */ + 0x04, b'0', /* add $'0', %al */ + 0xee, /* out %al, (%dx) */ + 0xb0, b'\n', /* mov $'\n', %al */ + 0xee, /* out %al, (%dx) */ + 0xf4, /* hlt */ + ]; + + let mem_size = 0x1000; + let load_addr = GuestAddress(0x1000); + let mem = GuestMemoryMmap::from_ranges(&[(load_addr, mem_size)]).unwrap(); + + let hv = hypervisor::new().unwrap(); + let vm = hv.create_vm().expect("new VM creation failed"); + + for (index, region) in mem.iter().enumerate() { + let mem_region = vm.make_user_memory_region( + index as u32, + region.start_addr().raw_value(), + region.len(), + region.as_ptr() as u64, + false, + false, + ); + + vm.create_user_memory_region(mem_region) + .expect("Cannot configure guest memory"); + } + mem.write_slice(&code, load_addr) + .expect("Writing code to memory failed"); + + let vcpu = vm.create_vcpu(0, None).expect("new Vcpu failed"); + + let mut vcpu_sregs = vcpu.get_sregs().expect("get sregs failed"); + vcpu_sregs.cs.base = 0; + vcpu_sregs.cs.selector = 0; + vcpu.set_sregs(&vcpu_sregs).expect("set sregs failed"); + + let mut vcpu_regs = vcpu.get_regs().expect("get regs failed"); + vcpu_regs.rip = 0x1000; + vcpu_regs.rax = 2; + vcpu_regs.rbx = 3; + vcpu_regs.rflags = 2; + vcpu.set_regs(&vcpu_regs).expect("set regs failed"); + + loop { + match vcpu.run().expect("run failed") { + VmExit::Reset => { + println!("HLT"); + break; + } + VmExit::Ignore => {} + r => panic!("unexpected exit reason: {:?}", r), + } + } +} + +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] +#[test] +pub fn test_vm_snapshot_restore() { + let vm_snapshot_state = VmSnapshot { + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + clock: Some(hypervisor::ClockData::Kvm( + hypervisor::kvm::kvm_clock_data::default(), + )), + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] + common_cpuid: vec![], + }; + + let snapshot = Snapshot::new_from_state(VM_SNAPSHOT_ID, &vm_snapshot_state).unwrap(); + + let vm_snapshot = get_vm_snapshot(&snapshot).map_err(Error::Restore).unwrap(); + println!("{:?}", vm_snapshot.clock.unwrap()); +} diff --git a/vendor/cube-hypervisor/vmm/src/vm_config.rs b/vendor/cube-hypervisor/vmm/src/vm_config.rs new file mode 100644 index 0000000..c505f0c --- /dev/null +++ b/vendor/cube-hypervisor/vmm/src/vm_config.rs @@ -0,0 +1,654 @@ +// Copyright © 2022 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// +pub use net_util::MacAddr; + +use serde::{Deserialize, Serialize}; +use std::{net::Ipv4Addr, path::PathBuf}; +use virtio_devices::fs::BackendFsConfig; +use virtio_devices::RateLimiterConfig; + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +pub enum CompatibleMode { + #[default] + Vendor, + Max, + Ignore, +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct CpuAffinity { + pub vcpu: u8, + pub host_cpus: Vec, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)] +pub struct CpuFeatures { + #[cfg(target_arch = "x86_64")] + #[serde(default)] + pub amx: bool, +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct CpuTopology { + pub threads_per_core: u8, + pub cores_per_die: u8, + pub dies_per_package: u8, + pub packages: u8, +} + +// When booting with PVH boot the maximum physical addressable size +// is a 46 bit address space even when the host supports with 5-level +// paging. +pub const DEFAULT_MAX_PHYS_BITS: u8 = 46; + +pub fn default_cpuconfig_max_phys_bits() -> u8 { + DEFAULT_MAX_PHYS_BITS +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct CpusConfig { + pub boot_vcpus: u8, + pub max_vcpus: u8, + #[serde(default)] + pub topology: Option, + #[serde(default)] + pub kvm_hyperv: bool, + #[serde(default = "default_cpuconfig_max_phys_bits")] + pub max_phys_bits: u8, + #[serde(default)] + pub affinity: Option>, + #[serde(default)] + pub features: CpuFeatures, + #[serde(default)] + pub compatible: CompatibleMode, +} + +pub const DEFAULT_VCPUS: u8 = 1; + +impl Default for CpusConfig { + fn default() -> Self { + CpusConfig { + boot_vcpus: DEFAULT_VCPUS, + max_vcpus: DEFAULT_VCPUS, + topology: None, + kvm_hyperv: false, + max_phys_bits: DEFAULT_MAX_PHYS_BITS, + affinity: None, + features: CpuFeatures::default(), + compatible: CompatibleMode::Vendor, + } + } +} + +pub const DEFAULT_NUM_PCI_SEGMENTS: u16 = 1; +pub fn default_platformconfig_num_pci_segments() -> u16 { + DEFAULT_NUM_PCI_SEGMENTS +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct PlatformConfig { + #[serde(default = "default_platformconfig_num_pci_segments")] + pub num_pci_segments: u16, + #[serde(default)] + pub iommu_segments: Option>, + #[serde(default)] + pub serial_number: Option, + #[serde(default)] + pub uuid: Option, + #[serde(default)] + pub oem_strings: Option>, + #[cfg(feature = "tdx")] + #[serde(default)] + pub tdx: bool, +} + +impl Default for PlatformConfig { + fn default() -> Self { + PlatformConfig { + num_pci_segments: DEFAULT_NUM_PCI_SEGMENTS, + iommu_segments: None, + serial_number: None, + uuid: None, + oem_strings: None, + #[cfg(feature = "tdx")] + tdx: false, + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct MemoryZoneConfig { + pub id: String, + pub size: u64, + #[serde(default)] + pub file: Option, + #[serde(default)] + pub shared: bool, + #[serde(default)] + pub hugepages: bool, + #[serde(default)] + pub hugepage_size: Option, + #[serde(default)] + pub host_numa_node: Option, + #[serde(default)] + pub hotplug_size: Option, + #[serde(default)] + pub hotplugged_size: Option, + #[serde(default)] + pub prefault: bool, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub enum HotplugMethod { + Acpi, + VirtioMem, +} + +impl Default for HotplugMethod { + fn default() -> Self { + HotplugMethod::Acpi + } +} + +fn default_memoryconfig_thp() -> bool { + true +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct MemoryConfig { + pub size: u64, + #[serde(default)] + pub mergeable: bool, + #[serde(default)] + pub hotplug_method: HotplugMethod, + #[serde(default)] + pub hotplug_size: Option, + #[serde(default)] + pub hotplugged_size: Option, + #[serde(default)] + pub shared: bool, + #[serde(default)] + pub hugepages: bool, + #[serde(default)] + pub hugepage_size: Option, + #[serde(default)] + pub prefault: bool, + #[serde(default)] + pub zones: Option>, + #[serde(default = "default_memoryconfig_thp")] + pub thp: bool, + #[serde(default)] + pub dirty_log: bool, +} + +pub const DEFAULT_MEMORY_MB: u64 = 512; + +impl Default for MemoryConfig { + fn default() -> Self { + MemoryConfig { + size: DEFAULT_MEMORY_MB << 20, + mergeable: false, + hotplug_method: HotplugMethod::Acpi, + hotplug_size: None, + hotplugged_size: None, + shared: false, + hugepages: false, + hugepage_size: None, + prefault: false, + zones: None, + thp: true, + dirty_log: false, + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub enum VhostMode { + Client, + Server, +} + +impl Default for VhostMode { + fn default() -> Self { + VhostMode::Client + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct DiskConfig { + pub path: Option, + #[serde(default)] + pub readonly: bool, + #[serde(default)] + pub direct: bool, + #[serde(default)] + pub iommu: bool, + #[serde(default = "default_diskconfig_num_queues")] + pub num_queues: usize, + #[serde(default = "default_diskconfig_queue_size")] + pub queue_size: u16, + #[serde(default)] + pub vhost_user: bool, + pub vhost_socket: Option, + #[serde(default)] + pub rate_limiter_config: Option, + #[serde(default)] + pub id: Option, + // For testing use only. Not exposed in API. + #[serde(default)] + pub disable_io_uring: bool, + #[serde(default)] + pub pci_segment: u16, +} + +pub const DEFAULT_DISK_NUM_QUEUES: usize = 1; + +pub fn default_diskconfig_num_queues() -> usize { + DEFAULT_DISK_NUM_QUEUES +} + +pub const DEFAULT_DISK_QUEUE_SIZE: u16 = 128; + +pub fn default_diskconfig_queue_size() -> u16 { + DEFAULT_DISK_QUEUE_SIZE +} + +impl Default for DiskConfig { + fn default() -> Self { + Self { + path: None, + readonly: false, + direct: false, + iommu: false, + num_queues: default_diskconfig_num_queues(), + queue_size: default_diskconfig_queue_size(), + vhost_user: false, + vhost_socket: None, + id: None, + disable_io_uring: false, + rate_limiter_config: None, + pci_segment: 0, + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct NetConfig { + #[serde(default = "default_netconfig_tap")] + pub tap: Option, + #[serde(default = "default_netconfig_ip")] + pub ip: Ipv4Addr, + #[serde(default = "default_netconfig_mask")] + pub mask: Ipv4Addr, + #[serde(default = "default_netconfig_mac")] + pub mac: MacAddr, + #[serde(default)] + pub host_mac: Option, + #[serde(default)] + pub mtu: Option, + #[serde(default)] + pub iommu: bool, + #[serde(default = "default_netconfig_num_queues")] + pub num_queues: usize, + #[serde(default = "default_netconfig_queue_size")] + pub queue_size: u16, + #[serde(default)] + pub vhost_user: bool, + pub vhost_socket: Option, + #[serde(default)] + pub vhost_mode: VhostMode, + #[serde(default)] + pub id: Option, + #[serde(default)] + pub fds: Option>, + #[serde(default)] + pub rate_limiter_config: Option, + #[serde(default)] + pub pci_segment: u16, +} + +pub fn default_netconfig_tap() -> Option { + None +} + +pub fn default_netconfig_ip() -> Ipv4Addr { + Ipv4Addr::new(192, 168, 249, 1) +} + +pub fn default_netconfig_mask() -> Ipv4Addr { + Ipv4Addr::new(255, 255, 255, 0) +} + +pub fn default_netconfig_mac() -> MacAddr { + MacAddr::local_random() +} + +pub const DEFAULT_NET_NUM_QUEUES: usize = 2; + +pub fn default_netconfig_num_queues() -> usize { + DEFAULT_NET_NUM_QUEUES +} + +pub const DEFAULT_NET_QUEUE_SIZE: u16 = 256; + +pub fn default_netconfig_queue_size() -> u16 { + DEFAULT_NET_QUEUE_SIZE +} + +impl Default for NetConfig { + fn default() -> Self { + Self { + tap: default_netconfig_tap(), + ip: default_netconfig_ip(), + mask: default_netconfig_mask(), + mac: default_netconfig_mac(), + host_mac: None, + mtu: None, + iommu: false, + num_queues: default_netconfig_num_queues(), + queue_size: default_netconfig_queue_size(), + vhost_user: false, + vhost_socket: None, + vhost_mode: VhostMode::Client, + id: None, + fds: None, + rate_limiter_config: None, + pci_segment: 0, + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct RngConfig { + pub src: PathBuf, + #[serde(default)] + pub iommu: bool, +} + +pub const DEFAULT_RNG_SOURCE: &str = "/dev/urandom"; + +impl Default for RngConfig { + fn default() -> Self { + RngConfig { + src: PathBuf::from(DEFAULT_RNG_SOURCE), + iommu: false, + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct BalloonConfig { + pub size: u64, + /// Option to deflate the balloon in case the guest is out of memory. + #[serde(default)] + pub deflate_on_oom: bool, + /// Option to enable free page reporting from the guest. + #[serde(default)] + pub free_page_reporting: bool, +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct FsConfig { + pub tag: String, + #[serde(default)] + pub socket: PathBuf, + #[serde(default = "default_fsconfig_num_queues")] + pub num_queues: usize, + #[serde(default = "default_fsconfig_queue_size")] + pub queue_size: u16, + #[serde(default)] + pub id: Option, + #[serde(default)] + pub pci_segment: u16, + #[serde(default)] + pub backendfs_config: Option, + #[serde(default)] + pub rate_limiter_config: Option, +} + +pub fn default_fsconfig_num_queues() -> usize { + 1 +} + +pub fn default_fsconfig_queue_size() -> u16 { + 1024 +} + +impl Default for FsConfig { + fn default() -> Self { + Self { + tag: "".to_owned(), + socket: PathBuf::new(), + num_queues: default_fsconfig_num_queues(), + queue_size: default_fsconfig_queue_size(), + id: None, + pci_segment: 0, + backendfs_config: None, + rate_limiter_config: None, + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +pub struct PmemConfig { + pub file: PathBuf, + #[serde(default)] + pub size: Option, + #[serde(default)] + pub iommu: bool, + #[serde(default)] + pub discard_writes: bool, + #[serde(default)] + pub id: Option, + #[serde(default)] + pub pci_segment: u16, +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub enum ConsoleOutputMode { + Off, + Pty, + Tty, + File, + Null, +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct ConsoleConfig { + #[serde(default = "default_consoleconfig_file")] + pub file: Option, + pub mode: ConsoleOutputMode, + #[serde(default)] + pub iommu: bool, + #[serde(default)] + pub sigwinch: bool, +} + +pub fn default_consoleconfig_file() -> Option { + None +} + +impl Default for ConsoleConfig { + fn default() -> Self { + default_console() + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +pub struct DeviceConfig { + pub path: PathBuf, + #[serde(default)] + pub iommu: bool, + #[serde(default)] + pub id: Option, + #[serde(default)] + pub pci_segment: u16, +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +pub struct UserDeviceConfig { + pub socket: PathBuf, + #[serde(default)] + pub id: Option, + #[serde(default)] + pub pci_segment: u16, +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +pub struct VdpaConfig { + pub path: PathBuf, + #[serde(default = "default_vdpaconfig_num_queues")] + pub num_queues: usize, + #[serde(default)] + pub iommu: bool, + #[serde(default)] + pub id: Option, + #[serde(default)] + pub pci_segment: u16, +} + +pub fn default_vdpaconfig_num_queues() -> usize { + 1 +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +pub struct VsockConfig { + pub cid: u64, + pub socket: PathBuf, + #[serde(default)] + pub iommu: bool, + #[serde(default)] + pub id: Option, + #[serde(default)] + pub pci_segment: u16, + #[serde(default)] + pub muxer_epoll_nested: bool, +} + +pub const DEFAULT_IVSHMEM_SIZE: usize = 128; + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct IvshmemConfig { + pub path: PathBuf, + pub size: usize, +} + +impl Default for IvshmemConfig { + fn default() -> Self { + Self { + path: PathBuf::new(), + size: DEFAULT_IVSHMEM_SIZE << 20, + } + } +} + +#[cfg(target_arch = "x86_64")] +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +pub struct SgxEpcConfig { + pub id: String, + #[serde(default)] + pub size: u64, + #[serde(default)] + pub prefault: bool, +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +pub struct NumaDistance { + #[serde(default)] + pub destination: u32, + #[serde(default)] + pub distance: u8, +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +pub struct NumaConfig { + #[serde(default)] + pub guest_numa_id: u32, + #[serde(default)] + pub cpus: Option>, + #[serde(default)] + pub distances: Option>, + #[serde(default)] + pub memory_zones: Option>, + #[cfg(target_arch = "x86_64")] + #[serde(default)] + pub sgx_epc_sections: Option>, +} + +#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +pub struct PayloadConfig { + #[serde(default)] + pub firmware: Option, + #[serde(default)] + pub kernel: Option, + #[serde(default)] + pub cmdline: Option, + #[serde(default)] + pub initramfs: Option, +} + +pub fn default_serial() -> ConsoleConfig { + ConsoleConfig { + file: None, + mode: ConsoleOutputMode::Null, + iommu: false, + sigwinch: false, + } +} + +pub fn default_console() -> ConsoleConfig { + ConsoleConfig { + file: None, + mode: ConsoleOutputMode::Tty, + iommu: false, + sigwinch: false, + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct TpmConfig { + pub socket: PathBuf, +} + +#[derive(Clone, Default, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct VmConfig { + #[serde(default)] + pub cpus: CpusConfig, + #[serde(default)] + pub memory: MemoryConfig, + pub payload: Option, + pub disks: Option>, + pub net: Option>, + #[serde(default)] + pub rng: RngConfig, + pub balloon: Option, + pub fs: Option>, + pub pmem: Option>, + #[serde(default = "default_serial")] + pub serial: ConsoleConfig, + #[serde(default = "default_console")] + pub console: ConsoleConfig, + pub devices: Option>, + pub user_devices: Option>, + pub vdpa: Option>, + pub vsock: Option, + #[serde(default)] + pub pvpanic: bool, + #[serde(default)] + pub iommu: bool, + #[cfg(target_arch = "x86_64")] + pub sgx_epc: Option>, + pub numa: Option>, + #[serde(default)] + pub watchdog: bool, + #[cfg(feature = "guest_debug")] + #[serde(default)] + pub gdb: bool, + pub platform: Option, + pub tpm: Option, + #[serde(default)] + pub sys_ctrl: bool, + pub ivshmem: Option, +} diff --git a/vendor/cube-hypervisor/workflows/1check.sh b/vendor/cube-hypervisor/workflows/1check.sh new file mode 100755 index 0000000..029e11b --- /dev/null +++ b/vendor/cube-hypervisor/workflows/1check.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +export CARGO_NET_GIT_FETCH_WITH_CLI=true + +echo "install cargo toolchain..." +curl https://sh.rustup.rs -sSf | sh -s -- -y +source "$HOME/.cargo/env" + + +rustup default 1.74.0 +rustc --version + +#echo "installing cross tools" +#cargo install cross +#rustup target add aarch64-unknown-linux-gnu +#yum install gcc-aarch64-linux-gnu gcc-c++-aarch64-linux-gnu -y + + +echo "cargo clippy..." +#cargo rustc --locked --bin cube-hypervisor -- -D warnings -D clippy::undocumented_unsafe_blocks +cargo fmt -- --check +#cross clippy --target=aarch64-unknown-linux-gnu --locked --all --all-targets --no-default-features --tests --examples --features kvm -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=aarch64-unknown-linux-gnu --locked --all --all-targets --tests --examples -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=aarch64-unknown-linux-gnu --locked --all --all-targets --tests --examples --features guest_debug -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=aarch64-unknown-linux-gnu --locked --all --all-targets --tests --examples --features tracing -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=aarch64-unknown-linux-musl --locked --all --all-targets --no-default-features --tests --examples --features kvm -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=aarch64-unknown-linux-musl --locked --all --all-targets --tests --examples -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=aarch64-unknown-linux-musl --locked --all --all-targets --tests --examples --features guest_debug -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=aarch64-unknown-linux-musl --locked --all --all-targets --tests --examples --features tracing -- -D warnings -D clippy::undocumented_unsafe_blocks +cargo clippy --target=x86_64-unknown-linux-gnu --all --all-targets --no-default-features --tests --examples --features kvm -- -D warnings -D clippy::undocumented_unsafe_blocks +cargo clippy --target=x86_64-unknown-linux-gnu --all --all-targets --tests --examples -- -D warnings -D clippy::undocumented_unsafe_blocks +cargo clippy --target=x86_64-unknown-linux-gnu --all --all-targets --tests --examples --features guest_debug -- -D warnings -D clippy::undocumented_unsafe_blocks +cargo clippy --target=x86_64-unknown-linux-gnu --all --all-targets --tests --examples --features tracing -- -D warnings -D clippy::undocumented_unsafe_blocks +cargo clippy --target=x86_64-unknown-linux-gnu --all --all-targets --no-default-features --tests --examples --features mshv -- -D warnings -D clippy::undocumented_unsafe_blocks +cargo clippy --target=x86_64-unknown-linux-gnu --all --all-targets --no-default-features --tests --examples --features mshv,kvm -- -D warnings -D clippy::undocumented_unsafe_blocks +cargo clippy --target=x86_64-unknown-linux-gnu --all --all-targets --no-default-features --tests --examples --features tdx,kvm -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=x86_64-unknown-linux-musl --locked --all --all-targets --no-default-features --tests --examples --features kvm -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=x86_64-unknown-linux-musl --locked --all --all-targets --tests --examples -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=x86_64-unknown-linux-musl --locked --all --all-targets --tests --examples --features guest_debug -- -D warnings -D clippy::undocumented_unsafe_blocks +#cross clippy --target=x86_64-unknown-linux-musl --locked --all --all-targets --tests --examples --features tracing -- -D warnings -D clippy::undocumented_unsafe_blocks diff --git a/vendor/cube-hypervisor/workflows/2upload.sh b/vendor/cube-hypervisor/workflows/2upload.sh new file mode 100755 index 0000000..771affe --- /dev/null +++ b/vendor/cube-hypervisor/workflows/2upload.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +export CARGO_NET_GIT_FETCH_WITH_CLI=true +source "$HOME/.cargo/env" +rustup default 1.74.0 +rustc --version +# 获取 Git 版本短码 +git_id=$(git rev-parse --short HEAD) + +task_id="${git_id}$(date +%s)" +# "uuid.txt" used for 3callback.sh only +echo "$task_id" > "task_id.txt" + +FILE_NAME="cube-src-$task_id.tar.gz" + +echo "cargo vendor..." + +vendor_out="$(cargo vendor)" +if [ ! -d ".cargo" ]; then + mkdir .cargo +fi +echo "$vendor_out" >> ".cargo/config.toml" + +pushd ../ +# 创建一个名为 "ch-${commit_id}$(date +%s).tar.gz" 的压缩文件 +tar -czvf "$FILE_NAME" --exclude="workspace/target" workspace/ + +pip install -U cos-python-sdk-v5 +python3 workspace/workflows/cos_agent.py upload "$FILE_NAME" +RES=$? +rm "$FILE_NAME" +popd || exit 1 +exit $RES From 0841c051d722528db31a2a2cfb6d2710748e6c1f Mon Sep 17 00:00:00 2001 From: gatewayj <835269233@qq.com> Date: Sun, 15 Nov 2026 18:42:04 +0800 Subject: [PATCH 10/12] refactor: organize hypervisor sources under sandbox --- .gitignore | 2 +- Cargo.toml | 2 +- THIRD_PARTY_NOTICES.md | 10 +++++----- docs/sandbox.md | 2 +- runtime/sandbox-worker/Cargo.toml | 2 +- .../.github/ISSUE_TEMPLATE/bug_report.md | 0 .../hypervisor}/.github/dependabot.yml | 0 .../hypervisor}/.github/workflows/audit.yaml | 0 .../hypervisor}/.github/workflows/build.yaml | 0 .../hypervisor}/.github/workflows/dco.yaml | 0 .../hypervisor}/.github/workflows/docker-image.yaml | 0 .../hypervisor}/.github/workflows/fuzz-build.yaml | 0 .../hypervisor}/.github/workflows/quality.yaml | 0 .../hypervisor}/.github/workflows/release.yaml | 0 .../hypervisor}/.gitignore | 0 .../hypervisor}/.rustfmt.toml | 0 .../hypervisor}/CODEOWNERS | 0 .../hypervisor}/CODE_OF_CONDUCT.md | 0 .../hypervisor}/CONTRIBUTING.md | 0 .../hypervisor}/CREDITS.md | 0 .../hypervisor}/Cargo.lock | 0 .../hypervisor}/Cargo.toml | 0 .../hypervisor}/Jenkinsfile | 0 .../hypervisor}/LICENSE-APACHE | 0 .../hypervisor}/LICENSE-BSD-3-Clause | 0 .../hypervisor}/LICENSE-CUBESANDBOX | 0 .../hypervisor}/MAINTAINERS.md | 0 .../cube-hypervisor => sandbox/hypervisor}/Makefile | 0 .../hypervisor}/README.md | 0 .../hypervisor}/UPSTREAM.json | 0 .../hypervisor}/acpi_tables/Cargo.toml | 0 .../hypervisor}/acpi_tables/src/aml.rs | 0 .../hypervisor}/acpi_tables/src/lib.rs | 0 .../hypervisor}/acpi_tables/src/rsdp.rs | 0 .../hypervisor}/acpi_tables/src/sdt.rs | 0 .../hypervisor}/api_client/Cargo.toml | 0 .../hypervisor}/api_client/src/lib.rs | 0 .../hypervisor}/arch/Cargo.toml | 0 .../hypervisor}/arch/src/aarch64/fdt.rs | 0 .../hypervisor}/arch/src/aarch64/layout.rs | 0 .../hypervisor}/arch/src/aarch64/mod.rs | 0 .../hypervisor}/arch/src/aarch64/regs.rs | 0 .../hypervisor}/arch/src/aarch64/uefi.rs | 0 .../hypervisor}/arch/src/lib.rs | 0 .../hypervisor}/arch/src/x86_64/cpuid_filter.rs | 0 .../hypervisor}/arch/src/x86_64/interrupts.rs | 0 .../hypervisor}/arch/src/x86_64/layout.rs | 0 .../hypervisor}/arch/src/x86_64/mod.rs | 0 .../hypervisor}/arch/src/x86_64/mpspec.rs | 0 .../hypervisor}/arch/src/x86_64/mptable.rs | 0 .../hypervisor}/arch/src/x86_64/regs.rs | 0 .../hypervisor}/arch/src/x86_64/smbios.rs | 0 .../hypervisor}/arch/src/x86_64/tdx/mod.rs | 0 .../hypervisor}/block_util/Cargo.toml | 0 .../hypervisor}/block_util/src/async_io.rs | 0 .../hypervisor}/block_util/src/fixed_vhd_async.rs | 0 .../hypervisor}/block_util/src/fixed_vhd_sync.rs | 0 .../hypervisor}/block_util/src/lib.rs | 0 .../hypervisor}/block_util/src/qcow_sync.rs | 0 .../hypervisor}/block_util/src/raw_async.rs | 0 .../hypervisor}/block_util/src/raw_sync.rs | 0 .../hypervisor}/block_util/src/vhd.rs | 0 .../hypervisor}/block_util/src/vhdx_sync.rs | 0 .../cube-hypervisor => sandbox/hypervisor}/build.rs | 0 .../hypervisor}/devices/Cargo.toml | 0 .../hypervisor}/devices/src/acpi.rs | 0 .../hypervisor}/devices/src/gic.rs | 0 .../hypervisor}/devices/src/interrupt_controller.rs | 0 .../hypervisor}/devices/src/ioapic.rs | 0 .../hypervisor}/devices/src/ivshmem.rs | 0 .../hypervisor}/devices/src/legacy/cmos.rs | 0 .../hypervisor}/devices/src/legacy/debug_port.rs | 0 .../hypervisor}/devices/src/legacy/fwdebug.rs | 0 .../hypervisor}/devices/src/legacy/gpio_pl061.rs | 0 .../hypervisor}/devices/src/legacy/i8042.rs | 0 .../hypervisor}/devices/src/legacy/mod.rs | 0 .../hypervisor}/devices/src/legacy/rtc_pl031.rs | 0 .../hypervisor}/devices/src/legacy/serial.rs | 0 .../hypervisor}/devices/src/legacy/sys_ctrl.rs | 0 .../hypervisor}/devices/src/legacy/uart_pl011.rs | 0 .../hypervisor}/devices/src/lib.rs | 0 .../hypervisor}/devices/src/pvpanic.rs | 0 .../hypervisor}/devices/src/tpm.rs | 0 .../hypervisor}/docs/COPYING.docs | 0 .../hypervisor}/docs/api.md | 0 .../hypervisor}/docs/arm64.md | 0 .../hypervisor}/docs/balloon.md | 0 .../hypervisor}/docs/building.md | 0 .../hypervisor}/docs/cpu.md | 0 .../hypervisor}/docs/custom-image.md | 0 .../hypervisor}/docs/debug-port.md | 0 .../hypervisor}/docs/device_model.md | 0 .../hypervisor}/docs/fs.md | 0 .../hypervisor}/docs/fuzzing.md | 0 .../hypervisor}/docs/gdb.md | 0 .../hypervisor}/docs/hotplug.md | 0 .../hypervisor}/docs/intel_sgx.md | 0 .../hypervisor}/docs/intel_tdx.md | 0 .../hypervisor}/docs/io_throttling.md | 0 .../hypervisor}/docs/iommu.md | 0 .../hypervisor}/docs/live_migration.md | 0 .../hypervisor}/docs/logging.md | 0 .../hypervisor}/docs/macvtap-bridge.md | 0 .../hypervisor}/docs/memory.md | 0 .../hypervisor}/docs/performance_metrics.md | 0 .../hypervisor}/docs/profiling.md | 0 .../hypervisor}/docs/releases.md | 0 .../hypervisor}/docs/seccomp.md | 0 .../hypervisor}/docs/snapshot_restore.md | 0 .../hypervisor}/docs/tpm.md | 0 .../hypervisor}/docs/tracing.md | 0 .../hypervisor}/docs/uefi.md | 0 .../hypervisor}/docs/vdpa.md | 0 .../hypervisor}/docs/vfio-user.md | 0 .../hypervisor}/docs/vfio.md | 0 .../hypervisor}/docs/vhost-user-blk-testing.md | 0 .../hypervisor}/docs/vhost-user-net-testing.md | 0 .../hypervisor}/docs/virtiofs-root.md | 0 .../hypervisor}/docs/vsock.md | 0 .../hypervisor}/docs/windows.md | 0 .../hypervisor}/event_monitor/Cargo.toml | 0 .../hypervisor}/event_monitor/src/lib.rs | 0 .../hypervisor}/event_notifier/Cargo.toml | 0 .../hypervisor}/event_notifier/src/lib.rs | 0 .../hypervisor}/fix_lock.py | 0 .../hypervisor}/fuzz/.gitignore | 0 .../hypervisor}/fuzz/Cargo.toml | 0 .../hypervisor}/fuzz/fuzz_targets/balloon.rs | 0 .../hypervisor}/fuzz/fuzz_targets/block.rs | 0 .../hypervisor}/fuzz/fuzz_targets/cmos.rs | 0 .../hypervisor}/fuzz/fuzz_targets/console.rs | 0 .../hypervisor}/fuzz/fuzz_targets/http_api.rs | 0 .../hypervisor}/fuzz/fuzz_targets/iommu.rs | 0 .../hypervisor}/fuzz/fuzz_targets/mem.rs | 0 .../hypervisor}/fuzz/fuzz_targets/pmem.rs | 0 .../hypervisor}/fuzz/fuzz_targets/qcow.rs | 0 .../hypervisor}/fuzz/fuzz_targets/rng.rs | 0 .../hypervisor}/fuzz/fuzz_targets/serial.rs | 0 .../hypervisor}/fuzz/fuzz_targets/vhdx.rs | 0 .../hypervisor}/fuzz/fuzz_targets/watchdog.rs | 0 .../hypervisor}/hypervisor/Cargo.toml | 0 .../hypervisor}/hypervisor/src/arch/aarch64/gic.rs | 0 .../hypervisor}/hypervisor/src/arch/aarch64/mod.rs | 0 .../hypervisor}/hypervisor/src/arch/emulator/mod.rs | 0 .../hypervisor}/hypervisor/src/arch/mod.rs | 0 .../src/arch/x86/emulator/instructions/cmp.rs | 0 .../src/arch/x86/emulator/instructions/mod.rs | 0 .../src/arch/x86/emulator/instructions/mov.rs | 0 .../src/arch/x86/emulator/instructions/movs.rs | 0 .../src/arch/x86/emulator/instructions/or.rs | 0 .../hypervisor/src/arch/x86/emulator/mod.rs | 0 .../hypervisor}/hypervisor/src/arch/x86/gdt.rs | 0 .../hypervisor}/hypervisor/src/arch/x86/mod.rs | 0 .../hypervisor/src/arch/x86/msr_index.rs | 0 .../hypervisor}/hypervisor/src/arch/x86/regs.rs | 0 .../hypervisor}/hypervisor/src/cpu.rs | 0 .../hypervisor}/hypervisor/src/device.rs | 0 .../hypervisor}/hypervisor/src/hypervisor.rs | 0 .../hypervisor/src/kvm/aarch64/gic/dist_regs.rs | 0 .../hypervisor/src/kvm/aarch64/gic/icc_regs.rs | 0 .../hypervisor/src/kvm/aarch64/gic/mod.rs | 0 .../hypervisor/src/kvm/aarch64/gic/redist_regs.rs | 0 .../hypervisor}/hypervisor/src/kvm/aarch64/mod.rs | 0 .../hypervisor}/hypervisor/src/kvm/mod.rs | 0 .../hypervisor}/hypervisor/src/kvm/x86_64/mod.rs | 0 .../hypervisor}/hypervisor/src/lib.rs | 0 .../hypervisor}/hypervisor/src/mshv/mod.rs | 0 .../hypervisor}/hypervisor/src/mshv/x86_64/mod.rs | 0 .../hypervisor}/hypervisor/src/vm.rs | 0 .../hypervisor}/log_json/Cargo.toml | 0 .../hypervisor}/log_json/src/lib.rs | 0 .../hypervisor}/logging/Cargo.toml | 0 .../hypervisor}/logging/src/lib.rs | 0 .../hypervisor}/net_gen/Cargo.toml | 0 .../hypervisor}/net_gen/src/if_tun.rs | 0 .../hypervisor}/net_gen/src/iff.rs | 0 .../hypervisor}/net_gen/src/inn.rs | 0 .../hypervisor}/net_gen/src/lib.rs | 0 .../hypervisor}/net_gen/src/sockios.rs | 0 .../hypervisor}/net_util/Cargo.toml | 0 .../hypervisor}/net_util/src/ctrl_queue.rs | 0 .../hypervisor}/net_util/src/lib.rs | 0 .../hypervisor}/net_util/src/mac.rs | 0 .../hypervisor}/net_util/src/open_tap.rs | 0 .../hypervisor}/net_util/src/queue_pair.rs | 0 .../hypervisor}/net_util/src/tap.rs | 0 .../hypervisor}/option_parser/Cargo.toml | 0 .../hypervisor}/option_parser/src/lib.rs | 0 .../hypervisor}/pci/Cargo.toml | 0 .../hypervisor}/pci/src/bus.rs | 0 .../hypervisor}/pci/src/configuration.rs | 0 .../hypervisor}/pci/src/device.rs | 0 .../hypervisor}/pci/src/lib.rs | 0 .../hypervisor}/pci/src/msi.rs | 0 .../hypervisor}/pci/src/msix.rs | 0 .../hypervisor}/pci/src/vfio.rs | 0 .../hypervisor}/pci/src/vfio_user.rs | 0 .../hypervisor}/performance-metrics/Cargo.toml | 0 .../hypervisor}/performance-metrics/build.rs | 0 .../hypervisor}/performance-metrics/src/main.rs | 0 .../performance-metrics/src/performance_tests.rs | 0 .../hypervisor}/qcow/Cargo.toml | 0 .../hypervisor}/qcow/src/qcow.rs | 0 .../hypervisor}/qcow/src/qcow_raw_file.rs | 0 .../hypervisor}/qcow/src/raw_file.rs | 0 .../hypervisor}/qcow/src/refcount.rs | 0 .../hypervisor}/qcow/src/vec_cache.rs | 0 .../hypervisor}/rate_limiter/Cargo.toml | 0 .../hypervisor}/rate_limiter/src/lib.rs | 0 .../hypervisor}/release-notes.md | 0 .../hypervisor}/resources/Dockerfile | 0 .../hypervisor}/resources/linux-config-aarch64 | 0 .../hypervisor}/resources/linux-config-x86_64 | 0 .../hypervisor}/scripts/ch-trace-visualiser.py | 0 .../scripts/check-image-compatability.sh | 0 .../hypervisor}/scripts/common-aarch64.sh | 0 .../hypervisor}/scripts/create-cloud-init.sh | 0 .../hypervisor}/scripts/dev_cli.sh | 0 .../hypervisor}/scripts/prepare_vdpa.sh | 0 .../scripts/run_integration_tests_aarch64.sh | 0 .../scripts/run_integration_tests_live_migration.sh | 0 .../scripts/run_integration_tests_rate_limiter.sh | 0 .../scripts/run_integration_tests_sgx.sh | 0 .../scripts/run_integration_tests_vfio.sh | 0 .../run_integration_tests_windows_aarch64.sh | 0 .../scripts/run_integration_tests_windows_x86_64.sh | 0 .../scripts/run_integration_tests_x86_64.sh | 0 .../hypervisor}/scripts/run_metrics.sh | 0 .../hypervisor}/scripts/run_openapi_tests.sh | 0 .../hypervisor}/scripts/run_unit_tests.sh | 0 .../hypervisor}/scripts/sha1sums-aarch64 | 0 .../hypervisor}/scripts/sha1sums-x86_64 | 0 .../hypervisor}/scripts/test-util.sh | 0 .../windows-11-iot-enterprise-aarch64.raw.sha1 | 0 .../hypervisor}/serial_buffer/Cargo.toml | 0 .../hypervisor}/serial_buffer/src/lib.rs | 0 .../hypervisor}/src/bin/ch-remote.rs | 0 .../hypervisor}/src/bin/cube-cpuid.rs | 0 .../hypervisor}/src/common.rs | 0 .../hypervisor}/src/lib.rs | 0 .../hypervisor}/src/main.rs | 0 .../hypervisor}/src/vmm_config.rs | 0 .../test_data/cloud-init/ubuntu/meta-data | 0 .../test_data/cloud-init/ubuntu/network-config | 0 .../test_data/cloud-init/ubuntu/user-data | 0 .../hypervisor}/test_infra/Cargo.toml | 0 .../hypervisor}/test_infra/src/lib.rs | 0 .../hypervisor}/tests/integration.rs | 0 .../hypervisor}/tpm/Cargo.toml | 0 .../hypervisor}/tpm/src/emulator.rs | 0 .../hypervisor}/tpm/src/lib.rs | 0 .../hypervisor}/tpm/src/socket.rs | 0 .../hypervisor}/tracer/Cargo.toml | 0 .../hypervisor}/tracer/src/lib.rs | 0 .../hypervisor}/tracer/src/tracer.rs | 0 .../hypervisor}/tracer/src/tracer_noop.rs | 0 .../hypervisor}/vfio_user/Cargo.toml | 0 .../hypervisor}/vfio_user/src/lib.rs | 0 .../hypervisor}/vhdx/Cargo.toml | 0 .../hypervisor}/vhdx/src/lib.rs | 0 .../hypervisor}/vhdx/src/vhdx.rs | 0 .../hypervisor}/vhdx/src/vhdx_bat.rs | 0 .../hypervisor}/vhdx/src/vhdx_header.rs | 0 .../hypervisor}/vhdx/src/vhdx_io.rs | 0 .../hypervisor}/vhdx/src/vhdx_metadata.rs | 0 .../hypervisor}/vhost_user_block/Cargo.toml | 0 .../hypervisor}/vhost_user_block/src/lib.rs | 0 .../hypervisor}/vhost_user_block/src/main.rs | 0 .../hypervisor}/vhost_user_net/Cargo.toml | 0 .../hypervisor}/vhost_user_net/src/lib.rs | 0 .../hypervisor}/vhost_user_net/src/main.rs | 0 .../hypervisor}/virtio-devices/Cargo.toml | 0 .../hypervisor}/virtio-devices/src/balloon.rs | 0 .../hypervisor}/virtio-devices/src/block.rs | 0 .../hypervisor}/virtio-devices/src/console.rs | 0 .../hypervisor}/virtio-devices/src/device.rs | 0 .../hypervisor}/virtio-devices/src/epoll_helper.rs | 0 .../hypervisor}/virtio-devices/src/fs.rs | 0 .../hypervisor}/virtio-devices/src/iommu.rs | 0 .../hypervisor}/virtio-devices/src/lib.rs | 0 .../hypervisor}/virtio-devices/src/mem.rs | 0 .../hypervisor}/virtio-devices/src/net.rs | 0 .../hypervisor}/virtio-devices/src/pmem.rs | 0 .../hypervisor}/virtio-devices/src/rng.rs | 0 .../virtio-devices/src/seccomp_filters.rs | 0 .../hypervisor}/virtio-devices/src/thread_helper.rs | 0 .../hypervisor}/virtio-devices/src/transport/mod.rs | 0 .../src/transport/pci_common_config.rs | 0 .../virtio-devices/src/transport/pci_device.rs | 0 .../hypervisor}/virtio-devices/src/vdpa.rs | 0 .../virtio-devices/src/vhost_user/blk.rs | 0 .../hypervisor}/virtio-devices/src/vhost_user/fs.rs | 0 .../virtio-devices/src/vhost_user/mod.rs | 0 .../virtio-devices/src/vhost_user/net.rs | 0 .../virtio-devices/src/vhost_user/vu_common_ctrl.rs | 0 .../virtio-devices/src/vsock/csm/connection.rs | 0 .../hypervisor}/virtio-devices/src/vsock/csm/mod.rs | 0 .../virtio-devices/src/vsock/csm/txbuf.rs | 0 .../hypervisor}/virtio-devices/src/vsock/device.rs | 0 .../hypervisor}/virtio-devices/src/vsock/mod.rs | 0 .../hypervisor}/virtio-devices/src/vsock/packet.rs | 0 .../virtio-devices/src/vsock/unix/mod.rs | 0 .../virtio-devices/src/vsock/unix/muxer.rs | 0 .../virtio-devices/src/vsock/unix/muxer_killq.rs | 0 .../virtio-devices/src/vsock/unix/muxer_rxq.rs | 0 .../virtio-devices/src/vsock/unix/stream.rs | 0 .../hypervisor}/virtio-devices/src/watchdog.rs | 0 .../hypervisor}/virtiofsd/.gitignore | 0 .../hypervisor}/virtiofsd/.gitlab-ci.yml | 0 .../hypervisor}/virtiofsd/50-virtiofsd.json | 0 .../hypervisor}/virtiofsd/CONTRIBUTING.md | 0 .../hypervisor}/virtiofsd/Cargo.toml | 0 .../hypervisor}/virtiofsd/LICENSE-APACHE | 0 .../hypervisor}/virtiofsd/LICENSE-BSD-3-Clause | 0 .../hypervisor}/virtiofsd/README.md | 0 .../hypervisor}/virtiofsd/doc/xattr-mapping.md | 0 .../docs/preserialization-storeonly-design.md | 0 .../hypervisor}/virtiofsd/rustfmt.toml | 0 .../hypervisor}/virtiofsd/src/descriptor_utils.rs | 0 .../hypervisor}/virtiofsd/src/file_traits.rs | 0 .../hypervisor}/virtiofsd/src/filesystem.rs | 0 .../virtiofsd/src/fs_cache_req_handler.rs | 0 .../hypervisor}/virtiofsd/src/fuse.rs | 0 .../hypervisor}/virtiofsd/src/idmap.rs | 0 .../hypervisor}/virtiofsd/src/lib.rs | 0 .../hypervisor}/virtiofsd/src/limits.rs | 0 .../hypervisor}/virtiofsd/src/macros.rs | 0 .../hypervisor}/virtiofsd/src/main.rs | 0 .../hypervisor}/virtiofsd/src/oslib.rs | 0 .../virtiofsd/src/passthrough/credentials.rs | 0 .../src/passthrough/device_state/deserialization.rs | 0 .../virtiofsd/src/passthrough/device_state/mod.rs | 0 .../passthrough/device_state/preserialization.rs | 0 .../src/passthrough/device_state/serialization.rs | 0 .../src/passthrough/device_state/serialized.rs | 0 .../virtiofsd/src/passthrough/file_handle.rs | 0 .../virtiofsd/src/passthrough/inode_store.rs | 0 .../hypervisor}/virtiofsd/src/passthrough/mod.rs | 0 .../virtiofsd/src/passthrough/mount_fd.rs | 0 .../virtiofsd/src/passthrough/read_only.rs | 0 .../hypervisor}/virtiofsd/src/passthrough/stat.rs | 0 .../virtiofsd/src/passthrough/stat/file_status.rs | 0 .../hypervisor}/virtiofsd/src/passthrough/util.rs | 0 .../virtiofsd/src/passthrough/xattrmap.rs | 0 .../hypervisor}/virtiofsd/src/read_dir.rs | 0 .../hypervisor}/virtiofsd/src/sandbox.rs | 0 .../hypervisor}/virtiofsd/src/seccomp.rs | 0 .../hypervisor}/virtiofsd/src/server.rs | 0 .../hypervisor}/virtiofsd/src/util.rs | 0 .../hypervisor}/vm-allocator/Cargo.toml | 0 .../hypervisor}/vm-allocator/src/address.rs | 0 .../hypervisor}/vm-allocator/src/gsi.rs | 0 .../hypervisor}/vm-allocator/src/lib.rs | 0 .../hypervisor}/vm-allocator/src/system.rs | 0 .../hypervisor}/vm-device/Cargo.toml | 0 .../hypervisor}/vm-device/src/bus.rs | 0 .../hypervisor}/vm-device/src/dma_mapping/mod.rs | 0 .../hypervisor}/vm-device/src/dma_mapping/vfio.rs | 0 .../hypervisor}/vm-device/src/interrupt/mod.rs | 0 .../hypervisor}/vm-device/src/lib.rs | 0 .../hypervisor}/vm-migration/Cargo.toml | 0 .../hypervisor}/vm-migration/src/lib.rs | 0 .../hypervisor}/vm-migration/src/protocol.rs | 0 .../hypervisor}/vm-virtio/Cargo.toml | 0 .../hypervisor}/vm-virtio/src/checked_descriptor.rs | 0 .../hypervisor}/vm-virtio/src/lib.rs | 0 .../hypervisor}/vm-virtio/src/queue.rs | 0 .../hypervisor}/vmm/Cargo.toml | 0 .../hypervisor}/vmm/src/acpi.rs | 0 .../hypervisor}/vmm/src/api/http/http_endpoint.rs | 0 .../hypervisor}/vmm/src/api/http/mod.rs | 0 .../hypervisor}/vmm/src/api/mod.rs | 0 .../vmm/src/api/openapi/cloud-hypervisor.yaml | 0 .../hypervisor}/vmm/src/api/service.rs | 0 .../hypervisor}/vmm/src/clone3.rs | 0 .../hypervisor}/vmm/src/config.rs | 0 .../hypervisor}/vmm/src/coredump.rs | 0 .../hypervisor}/vmm/src/cpu.rs | 0 .../hypervisor}/vmm/src/device_manager.rs | 0 .../hypervisor}/vmm/src/device_tree.rs | 0 .../hypervisor}/vmm/src/gdb.rs | 0 .../hypervisor}/vmm/src/interrupt.rs | 0 .../hypervisor}/vmm/src/lib.rs | 0 .../hypervisor}/vmm/src/memory_manager.rs | 0 .../hypervisor}/vmm/src/migration.rs | 0 .../hypervisor}/vmm/src/pagemap_anon.rs | 0 .../hypervisor}/vmm/src/pagemap_anon_bench.rs | 0 .../hypervisor}/vmm/src/pci_segment.rs | 0 .../hypervisor}/vmm/src/seccomp_filters.rs | 0 .../hypervisor}/vmm/src/serial_manager.rs | 0 .../hypervisor}/vmm/src/sigwinch_listener.rs | 0 .../hypervisor}/vmm/src/soft_dirty.rs | 0 .../hypervisor}/vmm/src/vm.rs | 0 .../hypervisor}/vmm/src/vm_config.rs | 0 .../hypervisor}/workflows/1check.sh | 0 .../hypervisor}/workflows/2upload.sh | 0 tests/sandbox/check_sources.py | 2 +- vendor/cube-hypervisor/README.agentdrive.md | 13 ------------- 398 files changed, 10 insertions(+), 23 deletions(-) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.github/ISSUE_TEMPLATE/bug_report.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.github/dependabot.yml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.github/workflows/audit.yaml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.github/workflows/build.yaml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.github/workflows/dco.yaml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.github/workflows/docker-image.yaml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.github/workflows/fuzz-build.yaml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.github/workflows/quality.yaml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.github/workflows/release.yaml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.gitignore (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/.rustfmt.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/CODEOWNERS (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/CODE_OF_CONDUCT.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/CONTRIBUTING.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/CREDITS.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/Cargo.lock (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/Jenkinsfile (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/LICENSE-APACHE (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/LICENSE-BSD-3-Clause (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/LICENSE-CUBESANDBOX (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/MAINTAINERS.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/Makefile (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/README.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/UPSTREAM.json (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/acpi_tables/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/acpi_tables/src/aml.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/acpi_tables/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/acpi_tables/src/rsdp.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/acpi_tables/src/sdt.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/api_client/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/api_client/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/aarch64/fdt.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/aarch64/layout.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/aarch64/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/aarch64/regs.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/aarch64/uefi.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/x86_64/cpuid_filter.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/x86_64/interrupts.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/x86_64/layout.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/x86_64/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/x86_64/mpspec.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/x86_64/mptable.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/x86_64/regs.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/x86_64/smbios.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/arch/src/x86_64/tdx/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/block_util/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/block_util/src/async_io.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/block_util/src/fixed_vhd_async.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/block_util/src/fixed_vhd_sync.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/block_util/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/block_util/src/qcow_sync.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/block_util/src/raw_async.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/block_util/src/raw_sync.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/block_util/src/vhd.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/block_util/src/vhdx_sync.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/build.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/acpi.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/gic.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/interrupt_controller.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/ioapic.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/ivshmem.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/legacy/cmos.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/legacy/debug_port.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/legacy/fwdebug.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/legacy/gpio_pl061.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/legacy/i8042.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/legacy/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/legacy/rtc_pl031.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/legacy/serial.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/legacy/sys_ctrl.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/legacy/uart_pl011.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/pvpanic.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/devices/src/tpm.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/COPYING.docs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/api.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/arm64.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/balloon.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/building.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/cpu.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/custom-image.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/debug-port.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/device_model.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/fs.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/fuzzing.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/gdb.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/hotplug.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/intel_sgx.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/intel_tdx.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/io_throttling.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/iommu.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/live_migration.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/logging.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/macvtap-bridge.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/memory.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/performance_metrics.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/profiling.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/releases.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/seccomp.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/snapshot_restore.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/tpm.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/tracing.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/uefi.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/vdpa.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/vfio-user.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/vfio.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/vhost-user-blk-testing.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/vhost-user-net-testing.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/virtiofs-root.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/vsock.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/docs/windows.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/event_monitor/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/event_monitor/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/event_notifier/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/event_notifier/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fix_lock.py (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/.gitignore (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/balloon.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/block.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/cmos.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/console.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/http_api.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/iommu.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/mem.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/pmem.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/qcow.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/rng.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/serial.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/vhdx.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/fuzz/fuzz_targets/watchdog.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/aarch64/gic.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/aarch64/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/emulator/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/x86/emulator/instructions/cmp.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/x86/emulator/instructions/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/x86/emulator/instructions/mov.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/x86/emulator/instructions/movs.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/x86/emulator/instructions/or.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/x86/emulator/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/x86/gdt.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/x86/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/x86/msr_index.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/arch/x86/regs.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/cpu.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/device.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/hypervisor.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/kvm/aarch64/gic/dist_regs.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/kvm/aarch64/gic/icc_regs.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/kvm/aarch64/gic/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/kvm/aarch64/gic/redist_regs.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/kvm/aarch64/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/kvm/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/kvm/x86_64/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/mshv/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/mshv/x86_64/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/hypervisor/src/vm.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/log_json/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/log_json/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/logging/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/logging/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_gen/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_gen/src/if_tun.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_gen/src/iff.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_gen/src/inn.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_gen/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_gen/src/sockios.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_util/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_util/src/ctrl_queue.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_util/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_util/src/mac.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_util/src/open_tap.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_util/src/queue_pair.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/net_util/src/tap.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/option_parser/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/option_parser/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/pci/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/pci/src/bus.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/pci/src/configuration.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/pci/src/device.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/pci/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/pci/src/msi.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/pci/src/msix.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/pci/src/vfio.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/pci/src/vfio_user.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/performance-metrics/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/performance-metrics/build.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/performance-metrics/src/main.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/performance-metrics/src/performance_tests.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/qcow/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/qcow/src/qcow.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/qcow/src/qcow_raw_file.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/qcow/src/raw_file.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/qcow/src/refcount.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/qcow/src/vec_cache.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/rate_limiter/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/rate_limiter/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/release-notes.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/resources/Dockerfile (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/resources/linux-config-aarch64 (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/resources/linux-config-x86_64 (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/ch-trace-visualiser.py (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/check-image-compatability.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/common-aarch64.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/create-cloud-init.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/dev_cli.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/prepare_vdpa.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_integration_tests_aarch64.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_integration_tests_live_migration.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_integration_tests_rate_limiter.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_integration_tests_sgx.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_integration_tests_vfio.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_integration_tests_windows_aarch64.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_integration_tests_windows_x86_64.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_integration_tests_x86_64.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_metrics.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_openapi_tests.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/run_unit_tests.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/sha1sums-aarch64 (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/sha1sums-x86_64 (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/test-util.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/scripts/windows-11-iot-enterprise-aarch64.raw.sha1 (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/serial_buffer/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/serial_buffer/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/src/bin/ch-remote.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/src/bin/cube-cpuid.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/src/common.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/src/main.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/src/vmm_config.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/test_data/cloud-init/ubuntu/meta-data (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/test_data/cloud-init/ubuntu/network-config (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/test_data/cloud-init/ubuntu/user-data (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/test_infra/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/test_infra/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/tests/integration.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/tpm/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/tpm/src/emulator.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/tpm/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/tpm/src/socket.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/tracer/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/tracer/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/tracer/src/tracer.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/tracer/src/tracer_noop.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vfio_user/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vfio_user/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhdx/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhdx/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhdx/src/vhdx.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhdx/src/vhdx_bat.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhdx/src/vhdx_header.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhdx/src/vhdx_io.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhdx/src/vhdx_metadata.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhost_user_block/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhost_user_block/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhost_user_block/src/main.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhost_user_net/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhost_user_net/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vhost_user_net/src/main.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/balloon.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/block.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/console.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/device.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/epoll_helper.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/fs.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/iommu.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/mem.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/net.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/pmem.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/rng.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/seccomp_filters.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/thread_helper.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/transport/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/transport/pci_common_config.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/transport/pci_device.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vdpa.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vhost_user/blk.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vhost_user/fs.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vhost_user/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vhost_user/net.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vhost_user/vu_common_ctrl.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/csm/connection.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/csm/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/csm/txbuf.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/device.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/packet.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/unix/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/unix/muxer.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/unix/muxer_killq.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/unix/muxer_rxq.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/vsock/unix/stream.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtio-devices/src/watchdog.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/.gitignore (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/.gitlab-ci.yml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/50-virtiofsd.json (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/CONTRIBUTING.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/LICENSE-APACHE (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/LICENSE-BSD-3-Clause (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/README.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/doc/xattr-mapping.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/docs/preserialization-storeonly-design.md (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/rustfmt.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/descriptor_utils.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/file_traits.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/filesystem.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/fs_cache_req_handler.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/fuse.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/idmap.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/limits.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/macros.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/main.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/oslib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/credentials.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/device_state/deserialization.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/device_state/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/device_state/preserialization.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/device_state/serialization.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/device_state/serialized.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/file_handle.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/inode_store.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/mount_fd.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/read_only.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/stat.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/stat/file_status.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/util.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/passthrough/xattrmap.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/read_dir.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/sandbox.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/seccomp.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/server.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/virtiofsd/src/util.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-allocator/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-allocator/src/address.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-allocator/src/gsi.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-allocator/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-allocator/src/system.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-device/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-device/src/bus.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-device/src/dma_mapping/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-device/src/dma_mapping/vfio.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-device/src/interrupt/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-device/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-migration/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-migration/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-migration/src/protocol.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-virtio/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-virtio/src/checked_descriptor.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-virtio/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vm-virtio/src/queue.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/Cargo.toml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/acpi.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/api/http/http_endpoint.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/api/http/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/api/mod.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/api/openapi/cloud-hypervisor.yaml (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/api/service.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/clone3.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/config.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/coredump.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/cpu.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/device_manager.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/device_tree.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/gdb.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/interrupt.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/lib.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/memory_manager.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/migration.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/pagemap_anon.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/pagemap_anon_bench.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/pci_segment.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/seccomp_filters.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/serial_manager.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/sigwinch_listener.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/soft_dirty.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/vm.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/vmm/src/vm_config.rs (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/workflows/1check.sh (100%) rename {vendor/cube-hypervisor => sandbox/hypervisor}/workflows/2upload.sh (100%) delete mode 100644 vendor/cube-hypervisor/README.agentdrive.md diff --git a/.gitignore b/.gitignore index 59af0fc..5604bb9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ /vendor/winfsp_wrs/target/ /vendor/winfsp_wrs/Cargo.lock -/vendor/cube-hypervisor/target/ +/sandbox/hypervisor/target/ /runtime/sandbox-worker/target/ /tests/dashboard/node_modules/ diff --git a/Cargo.toml b/Cargo.toml index d31afca..ad30e7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = ["apps/*", "crates/*"] resolver = "3" -exclude = ["vendor/winfsp_wrs", "vendor/cube-hypervisor"] +exclude = ["vendor/winfsp_wrs", "sandbox/hypervisor"] [workspace.package] version = "0.1.0" diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 3e0595a..ed071ec 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -1,11 +1,11 @@ # Sandbox 第三方代码说明 -AgentDrive 的 Sandbox worker 包含 TencentCloud/CubeSandbox 的 hypervisor 源码,固定来源为 commit `4411c96ba8658a959de34693f54f4918db2a432f`。代码存放在 `vendor/cube-hypervisor/`,该目录保留 Cloud Hypervisor、Tencent、rust-vmm、virtiofsd 及其他贡献者的原有版权声明。 +AgentDrive 的 Sandbox worker 包含 TencentCloud/CubeSandbox 的 hypervisor 源码,固定来源为 commit `4411c96ba8658a959de34693f54f4918db2a432f`。代码存放在 `sandbox/hypervisor/`,该目录保留 Cloud Hypervisor、Tencent、rust-vmm、virtiofsd 及其他贡献者的原有版权声明。 -- 来源记录和完整文件校验值:`vendor/cube-hypervisor/UPSTREAM.json`。 -- CubeSandbox 许可证:`vendor/cube-hypervisor/LICENSE-CUBESANDBOX`。 -- Hypervisor 许可证:`vendor/cube-hypervisor/LICENSE-APACHE`、`LICENSE-BSD-3-Clause`。 -- virtiofsd 许可证:`vendor/cube-hypervisor/virtiofsd/LICENSE-APACHE`、`LICENSE-BSD-3-Clause`。 +- 来源记录和完整文件校验值:`sandbox/hypervisor/UPSTREAM.json`。 +- CubeSandbox 许可证:`sandbox/hypervisor/LICENSE-CUBESANDBOX`。 +- Hypervisor 许可证:`sandbox/hypervisor/LICENSE-APACHE`、`LICENSE-BSD-3-Clause`。 +- virtiofsd 许可证:`sandbox/hypervisor/virtiofsd/LICENSE-APACHE`、`LICENSE-BSD-3-Clause`。 - 原文件中的单独声明继续适用。 Ubuntu 桌面模板中的部分配置和启动文件来源于同一 commit 的 `examples/ubuntu-desktop/`。具体文件与 AgentDrive 修改记录见 `build/sandbox/SOURCES.md`,许可证见 `build/sandbox/licenses/CubeSandbox-LICENSE`。 diff --git a/docs/sandbox.md b/docs/sandbox.md index 5f619b5..6b9f51a 100644 --- a/docs/sandbox.md +++ b/docs/sandbox.md @@ -72,7 +72,7 @@ cargo build --release -p agentdrived -p agentdrive cargo build --release --locked --manifest-path runtime/sandbox-worker/Cargo.toml ``` -worker 使用 `vendor/cube-hypervisor` 中的本地源码,通过独立 Cargo workspace 构建。源码来自 CubeSandbox commit `4411c96ba8658a959de34693f54f4918db2a432f`,原文件、PVM/KVM 支持及内置 virtio-fs 实现均保留。来源、文件校验值和许可证位于该目录,`make sandbox-verify` 检查来源记录、worker 编译和模板相关测试。linux-loader 固定为 0.13.0。 +worker 使用 `sandbox/hypervisor` 中的本地源码,通过独立 Cargo workspace 构建。源码来自 CubeSandbox commit `4411c96ba8658a959de34693f54f4918db2a432f`,原文件、PVM/KVM 支持及内置 virtio-fs 实现均保留。来源、文件校验值和许可证位于该目录,`make sandbox-verify` 检查来源记录、worker 编译和模板相关测试。linux-loader 固定为 0.13.0。 Ubuntu 模板继续基于腾讯的 `ghcr.io/tencentcloud/cubesandbox-base:2026.16` 镜像。使用 `CUBE_BASE_IMAGE` 可以指定同一镜像的内部镜像仓库地址。桌面文件来源和修改记录见 `build/sandbox/SOURCES.md`,该说明及上游许可证随模板安装到 `/usr/share/doc/agentdrive-sandbox/`。 diff --git a/runtime/sandbox-worker/Cargo.toml b/runtime/sandbox-worker/Cargo.toml index 9bdf8aa..10f61de 100644 --- a/runtime/sandbox-worker/Cargo.toml +++ b/runtime/sandbox-worker/Cargo.toml @@ -7,7 +7,7 @@ license = "Apache-2.0" [workspace] [dependencies] -cube-hypervisor = { path = "../../vendor/cube-hypervisor", features = ["lib_support"] } +cube-hypervisor = { path = "../../sandbox/hypervisor", features = ["lib_support"] } serde_json = "1" # CubeSandbox v0.7.3 uses vm-memory 0.16; linux-loader 0.13.2 uses 0.17. linux-loader = "=0.13.0" diff --git a/vendor/cube-hypervisor/.github/ISSUE_TEMPLATE/bug_report.md b/sandbox/hypervisor/.github/ISSUE_TEMPLATE/bug_report.md similarity index 100% rename from vendor/cube-hypervisor/.github/ISSUE_TEMPLATE/bug_report.md rename to sandbox/hypervisor/.github/ISSUE_TEMPLATE/bug_report.md diff --git a/vendor/cube-hypervisor/.github/dependabot.yml b/sandbox/hypervisor/.github/dependabot.yml similarity index 100% rename from vendor/cube-hypervisor/.github/dependabot.yml rename to sandbox/hypervisor/.github/dependabot.yml diff --git a/vendor/cube-hypervisor/.github/workflows/audit.yaml b/sandbox/hypervisor/.github/workflows/audit.yaml similarity index 100% rename from vendor/cube-hypervisor/.github/workflows/audit.yaml rename to sandbox/hypervisor/.github/workflows/audit.yaml diff --git a/vendor/cube-hypervisor/.github/workflows/build.yaml b/sandbox/hypervisor/.github/workflows/build.yaml similarity index 100% rename from vendor/cube-hypervisor/.github/workflows/build.yaml rename to sandbox/hypervisor/.github/workflows/build.yaml diff --git a/vendor/cube-hypervisor/.github/workflows/dco.yaml b/sandbox/hypervisor/.github/workflows/dco.yaml similarity index 100% rename from vendor/cube-hypervisor/.github/workflows/dco.yaml rename to sandbox/hypervisor/.github/workflows/dco.yaml diff --git a/vendor/cube-hypervisor/.github/workflows/docker-image.yaml b/sandbox/hypervisor/.github/workflows/docker-image.yaml similarity index 100% rename from vendor/cube-hypervisor/.github/workflows/docker-image.yaml rename to sandbox/hypervisor/.github/workflows/docker-image.yaml diff --git a/vendor/cube-hypervisor/.github/workflows/fuzz-build.yaml b/sandbox/hypervisor/.github/workflows/fuzz-build.yaml similarity index 100% rename from vendor/cube-hypervisor/.github/workflows/fuzz-build.yaml rename to sandbox/hypervisor/.github/workflows/fuzz-build.yaml diff --git a/vendor/cube-hypervisor/.github/workflows/quality.yaml b/sandbox/hypervisor/.github/workflows/quality.yaml similarity index 100% rename from vendor/cube-hypervisor/.github/workflows/quality.yaml rename to sandbox/hypervisor/.github/workflows/quality.yaml diff --git a/vendor/cube-hypervisor/.github/workflows/release.yaml b/sandbox/hypervisor/.github/workflows/release.yaml similarity index 100% rename from vendor/cube-hypervisor/.github/workflows/release.yaml rename to sandbox/hypervisor/.github/workflows/release.yaml diff --git a/vendor/cube-hypervisor/.gitignore b/sandbox/hypervisor/.gitignore similarity index 100% rename from vendor/cube-hypervisor/.gitignore rename to sandbox/hypervisor/.gitignore diff --git a/vendor/cube-hypervisor/.rustfmt.toml b/sandbox/hypervisor/.rustfmt.toml similarity index 100% rename from vendor/cube-hypervisor/.rustfmt.toml rename to sandbox/hypervisor/.rustfmt.toml diff --git a/vendor/cube-hypervisor/CODEOWNERS b/sandbox/hypervisor/CODEOWNERS similarity index 100% rename from vendor/cube-hypervisor/CODEOWNERS rename to sandbox/hypervisor/CODEOWNERS diff --git a/vendor/cube-hypervisor/CODE_OF_CONDUCT.md b/sandbox/hypervisor/CODE_OF_CONDUCT.md similarity index 100% rename from vendor/cube-hypervisor/CODE_OF_CONDUCT.md rename to sandbox/hypervisor/CODE_OF_CONDUCT.md diff --git a/vendor/cube-hypervisor/CONTRIBUTING.md b/sandbox/hypervisor/CONTRIBUTING.md similarity index 100% rename from vendor/cube-hypervisor/CONTRIBUTING.md rename to sandbox/hypervisor/CONTRIBUTING.md diff --git a/vendor/cube-hypervisor/CREDITS.md b/sandbox/hypervisor/CREDITS.md similarity index 100% rename from vendor/cube-hypervisor/CREDITS.md rename to sandbox/hypervisor/CREDITS.md diff --git a/vendor/cube-hypervisor/Cargo.lock b/sandbox/hypervisor/Cargo.lock similarity index 100% rename from vendor/cube-hypervisor/Cargo.lock rename to sandbox/hypervisor/Cargo.lock diff --git a/vendor/cube-hypervisor/Cargo.toml b/sandbox/hypervisor/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/Cargo.toml rename to sandbox/hypervisor/Cargo.toml diff --git a/vendor/cube-hypervisor/Jenkinsfile b/sandbox/hypervisor/Jenkinsfile similarity index 100% rename from vendor/cube-hypervisor/Jenkinsfile rename to sandbox/hypervisor/Jenkinsfile diff --git a/vendor/cube-hypervisor/LICENSE-APACHE b/sandbox/hypervisor/LICENSE-APACHE similarity index 100% rename from vendor/cube-hypervisor/LICENSE-APACHE rename to sandbox/hypervisor/LICENSE-APACHE diff --git a/vendor/cube-hypervisor/LICENSE-BSD-3-Clause b/sandbox/hypervisor/LICENSE-BSD-3-Clause similarity index 100% rename from vendor/cube-hypervisor/LICENSE-BSD-3-Clause rename to sandbox/hypervisor/LICENSE-BSD-3-Clause diff --git a/vendor/cube-hypervisor/LICENSE-CUBESANDBOX b/sandbox/hypervisor/LICENSE-CUBESANDBOX similarity index 100% rename from vendor/cube-hypervisor/LICENSE-CUBESANDBOX rename to sandbox/hypervisor/LICENSE-CUBESANDBOX diff --git a/vendor/cube-hypervisor/MAINTAINERS.md b/sandbox/hypervisor/MAINTAINERS.md similarity index 100% rename from vendor/cube-hypervisor/MAINTAINERS.md rename to sandbox/hypervisor/MAINTAINERS.md diff --git a/vendor/cube-hypervisor/Makefile b/sandbox/hypervisor/Makefile similarity index 100% rename from vendor/cube-hypervisor/Makefile rename to sandbox/hypervisor/Makefile diff --git a/vendor/cube-hypervisor/README.md b/sandbox/hypervisor/README.md similarity index 100% rename from vendor/cube-hypervisor/README.md rename to sandbox/hypervisor/README.md diff --git a/vendor/cube-hypervisor/UPSTREAM.json b/sandbox/hypervisor/UPSTREAM.json similarity index 100% rename from vendor/cube-hypervisor/UPSTREAM.json rename to sandbox/hypervisor/UPSTREAM.json diff --git a/vendor/cube-hypervisor/acpi_tables/Cargo.toml b/sandbox/hypervisor/acpi_tables/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/acpi_tables/Cargo.toml rename to sandbox/hypervisor/acpi_tables/Cargo.toml diff --git a/vendor/cube-hypervisor/acpi_tables/src/aml.rs b/sandbox/hypervisor/acpi_tables/src/aml.rs similarity index 100% rename from vendor/cube-hypervisor/acpi_tables/src/aml.rs rename to sandbox/hypervisor/acpi_tables/src/aml.rs diff --git a/vendor/cube-hypervisor/acpi_tables/src/lib.rs b/sandbox/hypervisor/acpi_tables/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/acpi_tables/src/lib.rs rename to sandbox/hypervisor/acpi_tables/src/lib.rs diff --git a/vendor/cube-hypervisor/acpi_tables/src/rsdp.rs b/sandbox/hypervisor/acpi_tables/src/rsdp.rs similarity index 100% rename from vendor/cube-hypervisor/acpi_tables/src/rsdp.rs rename to sandbox/hypervisor/acpi_tables/src/rsdp.rs diff --git a/vendor/cube-hypervisor/acpi_tables/src/sdt.rs b/sandbox/hypervisor/acpi_tables/src/sdt.rs similarity index 100% rename from vendor/cube-hypervisor/acpi_tables/src/sdt.rs rename to sandbox/hypervisor/acpi_tables/src/sdt.rs diff --git a/vendor/cube-hypervisor/api_client/Cargo.toml b/sandbox/hypervisor/api_client/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/api_client/Cargo.toml rename to sandbox/hypervisor/api_client/Cargo.toml diff --git a/vendor/cube-hypervisor/api_client/src/lib.rs b/sandbox/hypervisor/api_client/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/api_client/src/lib.rs rename to sandbox/hypervisor/api_client/src/lib.rs diff --git a/vendor/cube-hypervisor/arch/Cargo.toml b/sandbox/hypervisor/arch/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/arch/Cargo.toml rename to sandbox/hypervisor/arch/Cargo.toml diff --git a/vendor/cube-hypervisor/arch/src/aarch64/fdt.rs b/sandbox/hypervisor/arch/src/aarch64/fdt.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/aarch64/fdt.rs rename to sandbox/hypervisor/arch/src/aarch64/fdt.rs diff --git a/vendor/cube-hypervisor/arch/src/aarch64/layout.rs b/sandbox/hypervisor/arch/src/aarch64/layout.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/aarch64/layout.rs rename to sandbox/hypervisor/arch/src/aarch64/layout.rs diff --git a/vendor/cube-hypervisor/arch/src/aarch64/mod.rs b/sandbox/hypervisor/arch/src/aarch64/mod.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/aarch64/mod.rs rename to sandbox/hypervisor/arch/src/aarch64/mod.rs diff --git a/vendor/cube-hypervisor/arch/src/aarch64/regs.rs b/sandbox/hypervisor/arch/src/aarch64/regs.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/aarch64/regs.rs rename to sandbox/hypervisor/arch/src/aarch64/regs.rs diff --git a/vendor/cube-hypervisor/arch/src/aarch64/uefi.rs b/sandbox/hypervisor/arch/src/aarch64/uefi.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/aarch64/uefi.rs rename to sandbox/hypervisor/arch/src/aarch64/uefi.rs diff --git a/vendor/cube-hypervisor/arch/src/lib.rs b/sandbox/hypervisor/arch/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/lib.rs rename to sandbox/hypervisor/arch/src/lib.rs diff --git a/vendor/cube-hypervisor/arch/src/x86_64/cpuid_filter.rs b/sandbox/hypervisor/arch/src/x86_64/cpuid_filter.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/x86_64/cpuid_filter.rs rename to sandbox/hypervisor/arch/src/x86_64/cpuid_filter.rs diff --git a/vendor/cube-hypervisor/arch/src/x86_64/interrupts.rs b/sandbox/hypervisor/arch/src/x86_64/interrupts.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/x86_64/interrupts.rs rename to sandbox/hypervisor/arch/src/x86_64/interrupts.rs diff --git a/vendor/cube-hypervisor/arch/src/x86_64/layout.rs b/sandbox/hypervisor/arch/src/x86_64/layout.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/x86_64/layout.rs rename to sandbox/hypervisor/arch/src/x86_64/layout.rs diff --git a/vendor/cube-hypervisor/arch/src/x86_64/mod.rs b/sandbox/hypervisor/arch/src/x86_64/mod.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/x86_64/mod.rs rename to sandbox/hypervisor/arch/src/x86_64/mod.rs diff --git a/vendor/cube-hypervisor/arch/src/x86_64/mpspec.rs b/sandbox/hypervisor/arch/src/x86_64/mpspec.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/x86_64/mpspec.rs rename to sandbox/hypervisor/arch/src/x86_64/mpspec.rs diff --git a/vendor/cube-hypervisor/arch/src/x86_64/mptable.rs b/sandbox/hypervisor/arch/src/x86_64/mptable.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/x86_64/mptable.rs rename to sandbox/hypervisor/arch/src/x86_64/mptable.rs diff --git a/vendor/cube-hypervisor/arch/src/x86_64/regs.rs b/sandbox/hypervisor/arch/src/x86_64/regs.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/x86_64/regs.rs rename to sandbox/hypervisor/arch/src/x86_64/regs.rs diff --git a/vendor/cube-hypervisor/arch/src/x86_64/smbios.rs b/sandbox/hypervisor/arch/src/x86_64/smbios.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/x86_64/smbios.rs rename to sandbox/hypervisor/arch/src/x86_64/smbios.rs diff --git a/vendor/cube-hypervisor/arch/src/x86_64/tdx/mod.rs b/sandbox/hypervisor/arch/src/x86_64/tdx/mod.rs similarity index 100% rename from vendor/cube-hypervisor/arch/src/x86_64/tdx/mod.rs rename to sandbox/hypervisor/arch/src/x86_64/tdx/mod.rs diff --git a/vendor/cube-hypervisor/block_util/Cargo.toml b/sandbox/hypervisor/block_util/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/block_util/Cargo.toml rename to sandbox/hypervisor/block_util/Cargo.toml diff --git a/vendor/cube-hypervisor/block_util/src/async_io.rs b/sandbox/hypervisor/block_util/src/async_io.rs similarity index 100% rename from vendor/cube-hypervisor/block_util/src/async_io.rs rename to sandbox/hypervisor/block_util/src/async_io.rs diff --git a/vendor/cube-hypervisor/block_util/src/fixed_vhd_async.rs b/sandbox/hypervisor/block_util/src/fixed_vhd_async.rs similarity index 100% rename from vendor/cube-hypervisor/block_util/src/fixed_vhd_async.rs rename to sandbox/hypervisor/block_util/src/fixed_vhd_async.rs diff --git a/vendor/cube-hypervisor/block_util/src/fixed_vhd_sync.rs b/sandbox/hypervisor/block_util/src/fixed_vhd_sync.rs similarity index 100% rename from vendor/cube-hypervisor/block_util/src/fixed_vhd_sync.rs rename to sandbox/hypervisor/block_util/src/fixed_vhd_sync.rs diff --git a/vendor/cube-hypervisor/block_util/src/lib.rs b/sandbox/hypervisor/block_util/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/block_util/src/lib.rs rename to sandbox/hypervisor/block_util/src/lib.rs diff --git a/vendor/cube-hypervisor/block_util/src/qcow_sync.rs b/sandbox/hypervisor/block_util/src/qcow_sync.rs similarity index 100% rename from vendor/cube-hypervisor/block_util/src/qcow_sync.rs rename to sandbox/hypervisor/block_util/src/qcow_sync.rs diff --git a/vendor/cube-hypervisor/block_util/src/raw_async.rs b/sandbox/hypervisor/block_util/src/raw_async.rs similarity index 100% rename from vendor/cube-hypervisor/block_util/src/raw_async.rs rename to sandbox/hypervisor/block_util/src/raw_async.rs diff --git a/vendor/cube-hypervisor/block_util/src/raw_sync.rs b/sandbox/hypervisor/block_util/src/raw_sync.rs similarity index 100% rename from vendor/cube-hypervisor/block_util/src/raw_sync.rs rename to sandbox/hypervisor/block_util/src/raw_sync.rs diff --git a/vendor/cube-hypervisor/block_util/src/vhd.rs b/sandbox/hypervisor/block_util/src/vhd.rs similarity index 100% rename from vendor/cube-hypervisor/block_util/src/vhd.rs rename to sandbox/hypervisor/block_util/src/vhd.rs diff --git a/vendor/cube-hypervisor/block_util/src/vhdx_sync.rs b/sandbox/hypervisor/block_util/src/vhdx_sync.rs similarity index 100% rename from vendor/cube-hypervisor/block_util/src/vhdx_sync.rs rename to sandbox/hypervisor/block_util/src/vhdx_sync.rs diff --git a/vendor/cube-hypervisor/build.rs b/sandbox/hypervisor/build.rs similarity index 100% rename from vendor/cube-hypervisor/build.rs rename to sandbox/hypervisor/build.rs diff --git a/vendor/cube-hypervisor/devices/Cargo.toml b/sandbox/hypervisor/devices/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/devices/Cargo.toml rename to sandbox/hypervisor/devices/Cargo.toml diff --git a/vendor/cube-hypervisor/devices/src/acpi.rs b/sandbox/hypervisor/devices/src/acpi.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/acpi.rs rename to sandbox/hypervisor/devices/src/acpi.rs diff --git a/vendor/cube-hypervisor/devices/src/gic.rs b/sandbox/hypervisor/devices/src/gic.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/gic.rs rename to sandbox/hypervisor/devices/src/gic.rs diff --git a/vendor/cube-hypervisor/devices/src/interrupt_controller.rs b/sandbox/hypervisor/devices/src/interrupt_controller.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/interrupt_controller.rs rename to sandbox/hypervisor/devices/src/interrupt_controller.rs diff --git a/vendor/cube-hypervisor/devices/src/ioapic.rs b/sandbox/hypervisor/devices/src/ioapic.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/ioapic.rs rename to sandbox/hypervisor/devices/src/ioapic.rs diff --git a/vendor/cube-hypervisor/devices/src/ivshmem.rs b/sandbox/hypervisor/devices/src/ivshmem.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/ivshmem.rs rename to sandbox/hypervisor/devices/src/ivshmem.rs diff --git a/vendor/cube-hypervisor/devices/src/legacy/cmos.rs b/sandbox/hypervisor/devices/src/legacy/cmos.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/legacy/cmos.rs rename to sandbox/hypervisor/devices/src/legacy/cmos.rs diff --git a/vendor/cube-hypervisor/devices/src/legacy/debug_port.rs b/sandbox/hypervisor/devices/src/legacy/debug_port.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/legacy/debug_port.rs rename to sandbox/hypervisor/devices/src/legacy/debug_port.rs diff --git a/vendor/cube-hypervisor/devices/src/legacy/fwdebug.rs b/sandbox/hypervisor/devices/src/legacy/fwdebug.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/legacy/fwdebug.rs rename to sandbox/hypervisor/devices/src/legacy/fwdebug.rs diff --git a/vendor/cube-hypervisor/devices/src/legacy/gpio_pl061.rs b/sandbox/hypervisor/devices/src/legacy/gpio_pl061.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/legacy/gpio_pl061.rs rename to sandbox/hypervisor/devices/src/legacy/gpio_pl061.rs diff --git a/vendor/cube-hypervisor/devices/src/legacy/i8042.rs b/sandbox/hypervisor/devices/src/legacy/i8042.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/legacy/i8042.rs rename to sandbox/hypervisor/devices/src/legacy/i8042.rs diff --git a/vendor/cube-hypervisor/devices/src/legacy/mod.rs b/sandbox/hypervisor/devices/src/legacy/mod.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/legacy/mod.rs rename to sandbox/hypervisor/devices/src/legacy/mod.rs diff --git a/vendor/cube-hypervisor/devices/src/legacy/rtc_pl031.rs b/sandbox/hypervisor/devices/src/legacy/rtc_pl031.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/legacy/rtc_pl031.rs rename to sandbox/hypervisor/devices/src/legacy/rtc_pl031.rs diff --git a/vendor/cube-hypervisor/devices/src/legacy/serial.rs b/sandbox/hypervisor/devices/src/legacy/serial.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/legacy/serial.rs rename to sandbox/hypervisor/devices/src/legacy/serial.rs diff --git a/vendor/cube-hypervisor/devices/src/legacy/sys_ctrl.rs b/sandbox/hypervisor/devices/src/legacy/sys_ctrl.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/legacy/sys_ctrl.rs rename to sandbox/hypervisor/devices/src/legacy/sys_ctrl.rs diff --git a/vendor/cube-hypervisor/devices/src/legacy/uart_pl011.rs b/sandbox/hypervisor/devices/src/legacy/uart_pl011.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/legacy/uart_pl011.rs rename to sandbox/hypervisor/devices/src/legacy/uart_pl011.rs diff --git a/vendor/cube-hypervisor/devices/src/lib.rs b/sandbox/hypervisor/devices/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/lib.rs rename to sandbox/hypervisor/devices/src/lib.rs diff --git a/vendor/cube-hypervisor/devices/src/pvpanic.rs b/sandbox/hypervisor/devices/src/pvpanic.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/pvpanic.rs rename to sandbox/hypervisor/devices/src/pvpanic.rs diff --git a/vendor/cube-hypervisor/devices/src/tpm.rs b/sandbox/hypervisor/devices/src/tpm.rs similarity index 100% rename from vendor/cube-hypervisor/devices/src/tpm.rs rename to sandbox/hypervisor/devices/src/tpm.rs diff --git a/vendor/cube-hypervisor/docs/COPYING.docs b/sandbox/hypervisor/docs/COPYING.docs similarity index 100% rename from vendor/cube-hypervisor/docs/COPYING.docs rename to sandbox/hypervisor/docs/COPYING.docs diff --git a/vendor/cube-hypervisor/docs/api.md b/sandbox/hypervisor/docs/api.md similarity index 100% rename from vendor/cube-hypervisor/docs/api.md rename to sandbox/hypervisor/docs/api.md diff --git a/vendor/cube-hypervisor/docs/arm64.md b/sandbox/hypervisor/docs/arm64.md similarity index 100% rename from vendor/cube-hypervisor/docs/arm64.md rename to sandbox/hypervisor/docs/arm64.md diff --git a/vendor/cube-hypervisor/docs/balloon.md b/sandbox/hypervisor/docs/balloon.md similarity index 100% rename from vendor/cube-hypervisor/docs/balloon.md rename to sandbox/hypervisor/docs/balloon.md diff --git a/vendor/cube-hypervisor/docs/building.md b/sandbox/hypervisor/docs/building.md similarity index 100% rename from vendor/cube-hypervisor/docs/building.md rename to sandbox/hypervisor/docs/building.md diff --git a/vendor/cube-hypervisor/docs/cpu.md b/sandbox/hypervisor/docs/cpu.md similarity index 100% rename from vendor/cube-hypervisor/docs/cpu.md rename to sandbox/hypervisor/docs/cpu.md diff --git a/vendor/cube-hypervisor/docs/custom-image.md b/sandbox/hypervisor/docs/custom-image.md similarity index 100% rename from vendor/cube-hypervisor/docs/custom-image.md rename to sandbox/hypervisor/docs/custom-image.md diff --git a/vendor/cube-hypervisor/docs/debug-port.md b/sandbox/hypervisor/docs/debug-port.md similarity index 100% rename from vendor/cube-hypervisor/docs/debug-port.md rename to sandbox/hypervisor/docs/debug-port.md diff --git a/vendor/cube-hypervisor/docs/device_model.md b/sandbox/hypervisor/docs/device_model.md similarity index 100% rename from vendor/cube-hypervisor/docs/device_model.md rename to sandbox/hypervisor/docs/device_model.md diff --git a/vendor/cube-hypervisor/docs/fs.md b/sandbox/hypervisor/docs/fs.md similarity index 100% rename from vendor/cube-hypervisor/docs/fs.md rename to sandbox/hypervisor/docs/fs.md diff --git a/vendor/cube-hypervisor/docs/fuzzing.md b/sandbox/hypervisor/docs/fuzzing.md similarity index 100% rename from vendor/cube-hypervisor/docs/fuzzing.md rename to sandbox/hypervisor/docs/fuzzing.md diff --git a/vendor/cube-hypervisor/docs/gdb.md b/sandbox/hypervisor/docs/gdb.md similarity index 100% rename from vendor/cube-hypervisor/docs/gdb.md rename to sandbox/hypervisor/docs/gdb.md diff --git a/vendor/cube-hypervisor/docs/hotplug.md b/sandbox/hypervisor/docs/hotplug.md similarity index 100% rename from vendor/cube-hypervisor/docs/hotplug.md rename to sandbox/hypervisor/docs/hotplug.md diff --git a/vendor/cube-hypervisor/docs/intel_sgx.md b/sandbox/hypervisor/docs/intel_sgx.md similarity index 100% rename from vendor/cube-hypervisor/docs/intel_sgx.md rename to sandbox/hypervisor/docs/intel_sgx.md diff --git a/vendor/cube-hypervisor/docs/intel_tdx.md b/sandbox/hypervisor/docs/intel_tdx.md similarity index 100% rename from vendor/cube-hypervisor/docs/intel_tdx.md rename to sandbox/hypervisor/docs/intel_tdx.md diff --git a/vendor/cube-hypervisor/docs/io_throttling.md b/sandbox/hypervisor/docs/io_throttling.md similarity index 100% rename from vendor/cube-hypervisor/docs/io_throttling.md rename to sandbox/hypervisor/docs/io_throttling.md diff --git a/vendor/cube-hypervisor/docs/iommu.md b/sandbox/hypervisor/docs/iommu.md similarity index 100% rename from vendor/cube-hypervisor/docs/iommu.md rename to sandbox/hypervisor/docs/iommu.md diff --git a/vendor/cube-hypervisor/docs/live_migration.md b/sandbox/hypervisor/docs/live_migration.md similarity index 100% rename from vendor/cube-hypervisor/docs/live_migration.md rename to sandbox/hypervisor/docs/live_migration.md diff --git a/vendor/cube-hypervisor/docs/logging.md b/sandbox/hypervisor/docs/logging.md similarity index 100% rename from vendor/cube-hypervisor/docs/logging.md rename to sandbox/hypervisor/docs/logging.md diff --git a/vendor/cube-hypervisor/docs/macvtap-bridge.md b/sandbox/hypervisor/docs/macvtap-bridge.md similarity index 100% rename from vendor/cube-hypervisor/docs/macvtap-bridge.md rename to sandbox/hypervisor/docs/macvtap-bridge.md diff --git a/vendor/cube-hypervisor/docs/memory.md b/sandbox/hypervisor/docs/memory.md similarity index 100% rename from vendor/cube-hypervisor/docs/memory.md rename to sandbox/hypervisor/docs/memory.md diff --git a/vendor/cube-hypervisor/docs/performance_metrics.md b/sandbox/hypervisor/docs/performance_metrics.md similarity index 100% rename from vendor/cube-hypervisor/docs/performance_metrics.md rename to sandbox/hypervisor/docs/performance_metrics.md diff --git a/vendor/cube-hypervisor/docs/profiling.md b/sandbox/hypervisor/docs/profiling.md similarity index 100% rename from vendor/cube-hypervisor/docs/profiling.md rename to sandbox/hypervisor/docs/profiling.md diff --git a/vendor/cube-hypervisor/docs/releases.md b/sandbox/hypervisor/docs/releases.md similarity index 100% rename from vendor/cube-hypervisor/docs/releases.md rename to sandbox/hypervisor/docs/releases.md diff --git a/vendor/cube-hypervisor/docs/seccomp.md b/sandbox/hypervisor/docs/seccomp.md similarity index 100% rename from vendor/cube-hypervisor/docs/seccomp.md rename to sandbox/hypervisor/docs/seccomp.md diff --git a/vendor/cube-hypervisor/docs/snapshot_restore.md b/sandbox/hypervisor/docs/snapshot_restore.md similarity index 100% rename from vendor/cube-hypervisor/docs/snapshot_restore.md rename to sandbox/hypervisor/docs/snapshot_restore.md diff --git a/vendor/cube-hypervisor/docs/tpm.md b/sandbox/hypervisor/docs/tpm.md similarity index 100% rename from vendor/cube-hypervisor/docs/tpm.md rename to sandbox/hypervisor/docs/tpm.md diff --git a/vendor/cube-hypervisor/docs/tracing.md b/sandbox/hypervisor/docs/tracing.md similarity index 100% rename from vendor/cube-hypervisor/docs/tracing.md rename to sandbox/hypervisor/docs/tracing.md diff --git a/vendor/cube-hypervisor/docs/uefi.md b/sandbox/hypervisor/docs/uefi.md similarity index 100% rename from vendor/cube-hypervisor/docs/uefi.md rename to sandbox/hypervisor/docs/uefi.md diff --git a/vendor/cube-hypervisor/docs/vdpa.md b/sandbox/hypervisor/docs/vdpa.md similarity index 100% rename from vendor/cube-hypervisor/docs/vdpa.md rename to sandbox/hypervisor/docs/vdpa.md diff --git a/vendor/cube-hypervisor/docs/vfio-user.md b/sandbox/hypervisor/docs/vfio-user.md similarity index 100% rename from vendor/cube-hypervisor/docs/vfio-user.md rename to sandbox/hypervisor/docs/vfio-user.md diff --git a/vendor/cube-hypervisor/docs/vfio.md b/sandbox/hypervisor/docs/vfio.md similarity index 100% rename from vendor/cube-hypervisor/docs/vfio.md rename to sandbox/hypervisor/docs/vfio.md diff --git a/vendor/cube-hypervisor/docs/vhost-user-blk-testing.md b/sandbox/hypervisor/docs/vhost-user-blk-testing.md similarity index 100% rename from vendor/cube-hypervisor/docs/vhost-user-blk-testing.md rename to sandbox/hypervisor/docs/vhost-user-blk-testing.md diff --git a/vendor/cube-hypervisor/docs/vhost-user-net-testing.md b/sandbox/hypervisor/docs/vhost-user-net-testing.md similarity index 100% rename from vendor/cube-hypervisor/docs/vhost-user-net-testing.md rename to sandbox/hypervisor/docs/vhost-user-net-testing.md diff --git a/vendor/cube-hypervisor/docs/virtiofs-root.md b/sandbox/hypervisor/docs/virtiofs-root.md similarity index 100% rename from vendor/cube-hypervisor/docs/virtiofs-root.md rename to sandbox/hypervisor/docs/virtiofs-root.md diff --git a/vendor/cube-hypervisor/docs/vsock.md b/sandbox/hypervisor/docs/vsock.md similarity index 100% rename from vendor/cube-hypervisor/docs/vsock.md rename to sandbox/hypervisor/docs/vsock.md diff --git a/vendor/cube-hypervisor/docs/windows.md b/sandbox/hypervisor/docs/windows.md similarity index 100% rename from vendor/cube-hypervisor/docs/windows.md rename to sandbox/hypervisor/docs/windows.md diff --git a/vendor/cube-hypervisor/event_monitor/Cargo.toml b/sandbox/hypervisor/event_monitor/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/event_monitor/Cargo.toml rename to sandbox/hypervisor/event_monitor/Cargo.toml diff --git a/vendor/cube-hypervisor/event_monitor/src/lib.rs b/sandbox/hypervisor/event_monitor/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/event_monitor/src/lib.rs rename to sandbox/hypervisor/event_monitor/src/lib.rs diff --git a/vendor/cube-hypervisor/event_notifier/Cargo.toml b/sandbox/hypervisor/event_notifier/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/event_notifier/Cargo.toml rename to sandbox/hypervisor/event_notifier/Cargo.toml diff --git a/vendor/cube-hypervisor/event_notifier/src/lib.rs b/sandbox/hypervisor/event_notifier/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/event_notifier/src/lib.rs rename to sandbox/hypervisor/event_notifier/src/lib.rs diff --git a/vendor/cube-hypervisor/fix_lock.py b/sandbox/hypervisor/fix_lock.py similarity index 100% rename from vendor/cube-hypervisor/fix_lock.py rename to sandbox/hypervisor/fix_lock.py diff --git a/vendor/cube-hypervisor/fuzz/.gitignore b/sandbox/hypervisor/fuzz/.gitignore similarity index 100% rename from vendor/cube-hypervisor/fuzz/.gitignore rename to sandbox/hypervisor/fuzz/.gitignore diff --git a/vendor/cube-hypervisor/fuzz/Cargo.toml b/sandbox/hypervisor/fuzz/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/fuzz/Cargo.toml rename to sandbox/hypervisor/fuzz/Cargo.toml diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/balloon.rs b/sandbox/hypervisor/fuzz/fuzz_targets/balloon.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/balloon.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/balloon.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/block.rs b/sandbox/hypervisor/fuzz/fuzz_targets/block.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/block.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/block.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/cmos.rs b/sandbox/hypervisor/fuzz/fuzz_targets/cmos.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/cmos.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/cmos.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/console.rs b/sandbox/hypervisor/fuzz/fuzz_targets/console.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/console.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/console.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/http_api.rs b/sandbox/hypervisor/fuzz/fuzz_targets/http_api.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/http_api.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/http_api.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/iommu.rs b/sandbox/hypervisor/fuzz/fuzz_targets/iommu.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/iommu.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/iommu.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/mem.rs b/sandbox/hypervisor/fuzz/fuzz_targets/mem.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/mem.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/mem.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/pmem.rs b/sandbox/hypervisor/fuzz/fuzz_targets/pmem.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/pmem.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/pmem.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/qcow.rs b/sandbox/hypervisor/fuzz/fuzz_targets/qcow.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/qcow.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/qcow.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/rng.rs b/sandbox/hypervisor/fuzz/fuzz_targets/rng.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/rng.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/rng.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/serial.rs b/sandbox/hypervisor/fuzz/fuzz_targets/serial.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/serial.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/serial.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/vhdx.rs b/sandbox/hypervisor/fuzz/fuzz_targets/vhdx.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/vhdx.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/vhdx.rs diff --git a/vendor/cube-hypervisor/fuzz/fuzz_targets/watchdog.rs b/sandbox/hypervisor/fuzz/fuzz_targets/watchdog.rs similarity index 100% rename from vendor/cube-hypervisor/fuzz/fuzz_targets/watchdog.rs rename to sandbox/hypervisor/fuzz/fuzz_targets/watchdog.rs diff --git a/vendor/cube-hypervisor/hypervisor/Cargo.toml b/sandbox/hypervisor/hypervisor/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/hypervisor/Cargo.toml rename to sandbox/hypervisor/hypervisor/Cargo.toml diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/aarch64/gic.rs b/sandbox/hypervisor/hypervisor/src/arch/aarch64/gic.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/aarch64/gic.rs rename to sandbox/hypervisor/hypervisor/src/arch/aarch64/gic.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/aarch64/mod.rs b/sandbox/hypervisor/hypervisor/src/arch/aarch64/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/aarch64/mod.rs rename to sandbox/hypervisor/hypervisor/src/arch/aarch64/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/emulator/mod.rs b/sandbox/hypervisor/hypervisor/src/arch/emulator/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/emulator/mod.rs rename to sandbox/hypervisor/hypervisor/src/arch/emulator/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/mod.rs b/sandbox/hypervisor/hypervisor/src/arch/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/mod.rs rename to sandbox/hypervisor/hypervisor/src/arch/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/cmp.rs b/sandbox/hypervisor/hypervisor/src/arch/x86/emulator/instructions/cmp.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/cmp.rs rename to sandbox/hypervisor/hypervisor/src/arch/x86/emulator/instructions/cmp.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mod.rs b/sandbox/hypervisor/hypervisor/src/arch/x86/emulator/instructions/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mod.rs rename to sandbox/hypervisor/hypervisor/src/arch/x86/emulator/instructions/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mov.rs b/sandbox/hypervisor/hypervisor/src/arch/x86/emulator/instructions/mov.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/mov.rs rename to sandbox/hypervisor/hypervisor/src/arch/x86/emulator/instructions/mov.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/movs.rs b/sandbox/hypervisor/hypervisor/src/arch/x86/emulator/instructions/movs.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/movs.rs rename to sandbox/hypervisor/hypervisor/src/arch/x86/emulator/instructions/movs.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/or.rs b/sandbox/hypervisor/hypervisor/src/arch/x86/emulator/instructions/or.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/instructions/or.rs rename to sandbox/hypervisor/hypervisor/src/arch/x86/emulator/instructions/or.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/mod.rs b/sandbox/hypervisor/hypervisor/src/arch/x86/emulator/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/x86/emulator/mod.rs rename to sandbox/hypervisor/hypervisor/src/arch/x86/emulator/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/gdt.rs b/sandbox/hypervisor/hypervisor/src/arch/x86/gdt.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/x86/gdt.rs rename to sandbox/hypervisor/hypervisor/src/arch/x86/gdt.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/mod.rs b/sandbox/hypervisor/hypervisor/src/arch/x86/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/x86/mod.rs rename to sandbox/hypervisor/hypervisor/src/arch/x86/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/msr_index.rs b/sandbox/hypervisor/hypervisor/src/arch/x86/msr_index.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/x86/msr_index.rs rename to sandbox/hypervisor/hypervisor/src/arch/x86/msr_index.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/arch/x86/regs.rs b/sandbox/hypervisor/hypervisor/src/arch/x86/regs.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/arch/x86/regs.rs rename to sandbox/hypervisor/hypervisor/src/arch/x86/regs.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/cpu.rs b/sandbox/hypervisor/hypervisor/src/cpu.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/cpu.rs rename to sandbox/hypervisor/hypervisor/src/cpu.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/device.rs b/sandbox/hypervisor/hypervisor/src/device.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/device.rs rename to sandbox/hypervisor/hypervisor/src/device.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/hypervisor.rs b/sandbox/hypervisor/hypervisor/src/hypervisor.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/hypervisor.rs rename to sandbox/hypervisor/hypervisor/src/hypervisor.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/dist_regs.rs b/sandbox/hypervisor/hypervisor/src/kvm/aarch64/gic/dist_regs.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/dist_regs.rs rename to sandbox/hypervisor/hypervisor/src/kvm/aarch64/gic/dist_regs.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/icc_regs.rs b/sandbox/hypervisor/hypervisor/src/kvm/aarch64/gic/icc_regs.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/icc_regs.rs rename to sandbox/hypervisor/hypervisor/src/kvm/aarch64/gic/icc_regs.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/mod.rs b/sandbox/hypervisor/hypervisor/src/kvm/aarch64/gic/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/mod.rs rename to sandbox/hypervisor/hypervisor/src/kvm/aarch64/gic/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/redist_regs.rs b/sandbox/hypervisor/hypervisor/src/kvm/aarch64/gic/redist_regs.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/gic/redist_regs.rs rename to sandbox/hypervisor/hypervisor/src/kvm/aarch64/gic/redist_regs.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/mod.rs b/sandbox/hypervisor/hypervisor/src/kvm/aarch64/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/kvm/aarch64/mod.rs rename to sandbox/hypervisor/hypervisor/src/kvm/aarch64/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/mod.rs b/sandbox/hypervisor/hypervisor/src/kvm/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/kvm/mod.rs rename to sandbox/hypervisor/hypervisor/src/kvm/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/kvm/x86_64/mod.rs b/sandbox/hypervisor/hypervisor/src/kvm/x86_64/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/kvm/x86_64/mod.rs rename to sandbox/hypervisor/hypervisor/src/kvm/x86_64/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/lib.rs b/sandbox/hypervisor/hypervisor/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/lib.rs rename to sandbox/hypervisor/hypervisor/src/lib.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/mshv/mod.rs b/sandbox/hypervisor/hypervisor/src/mshv/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/mshv/mod.rs rename to sandbox/hypervisor/hypervisor/src/mshv/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/mshv/x86_64/mod.rs b/sandbox/hypervisor/hypervisor/src/mshv/x86_64/mod.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/mshv/x86_64/mod.rs rename to sandbox/hypervisor/hypervisor/src/mshv/x86_64/mod.rs diff --git a/vendor/cube-hypervisor/hypervisor/src/vm.rs b/sandbox/hypervisor/hypervisor/src/vm.rs similarity index 100% rename from vendor/cube-hypervisor/hypervisor/src/vm.rs rename to sandbox/hypervisor/hypervisor/src/vm.rs diff --git a/vendor/cube-hypervisor/log_json/Cargo.toml b/sandbox/hypervisor/log_json/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/log_json/Cargo.toml rename to sandbox/hypervisor/log_json/Cargo.toml diff --git a/vendor/cube-hypervisor/log_json/src/lib.rs b/sandbox/hypervisor/log_json/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/log_json/src/lib.rs rename to sandbox/hypervisor/log_json/src/lib.rs diff --git a/vendor/cube-hypervisor/logging/Cargo.toml b/sandbox/hypervisor/logging/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/logging/Cargo.toml rename to sandbox/hypervisor/logging/Cargo.toml diff --git a/vendor/cube-hypervisor/logging/src/lib.rs b/sandbox/hypervisor/logging/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/logging/src/lib.rs rename to sandbox/hypervisor/logging/src/lib.rs diff --git a/vendor/cube-hypervisor/net_gen/Cargo.toml b/sandbox/hypervisor/net_gen/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/net_gen/Cargo.toml rename to sandbox/hypervisor/net_gen/Cargo.toml diff --git a/vendor/cube-hypervisor/net_gen/src/if_tun.rs b/sandbox/hypervisor/net_gen/src/if_tun.rs similarity index 100% rename from vendor/cube-hypervisor/net_gen/src/if_tun.rs rename to sandbox/hypervisor/net_gen/src/if_tun.rs diff --git a/vendor/cube-hypervisor/net_gen/src/iff.rs b/sandbox/hypervisor/net_gen/src/iff.rs similarity index 100% rename from vendor/cube-hypervisor/net_gen/src/iff.rs rename to sandbox/hypervisor/net_gen/src/iff.rs diff --git a/vendor/cube-hypervisor/net_gen/src/inn.rs b/sandbox/hypervisor/net_gen/src/inn.rs similarity index 100% rename from vendor/cube-hypervisor/net_gen/src/inn.rs rename to sandbox/hypervisor/net_gen/src/inn.rs diff --git a/vendor/cube-hypervisor/net_gen/src/lib.rs b/sandbox/hypervisor/net_gen/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/net_gen/src/lib.rs rename to sandbox/hypervisor/net_gen/src/lib.rs diff --git a/vendor/cube-hypervisor/net_gen/src/sockios.rs b/sandbox/hypervisor/net_gen/src/sockios.rs similarity index 100% rename from vendor/cube-hypervisor/net_gen/src/sockios.rs rename to sandbox/hypervisor/net_gen/src/sockios.rs diff --git a/vendor/cube-hypervisor/net_util/Cargo.toml b/sandbox/hypervisor/net_util/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/net_util/Cargo.toml rename to sandbox/hypervisor/net_util/Cargo.toml diff --git a/vendor/cube-hypervisor/net_util/src/ctrl_queue.rs b/sandbox/hypervisor/net_util/src/ctrl_queue.rs similarity index 100% rename from vendor/cube-hypervisor/net_util/src/ctrl_queue.rs rename to sandbox/hypervisor/net_util/src/ctrl_queue.rs diff --git a/vendor/cube-hypervisor/net_util/src/lib.rs b/sandbox/hypervisor/net_util/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/net_util/src/lib.rs rename to sandbox/hypervisor/net_util/src/lib.rs diff --git a/vendor/cube-hypervisor/net_util/src/mac.rs b/sandbox/hypervisor/net_util/src/mac.rs similarity index 100% rename from vendor/cube-hypervisor/net_util/src/mac.rs rename to sandbox/hypervisor/net_util/src/mac.rs diff --git a/vendor/cube-hypervisor/net_util/src/open_tap.rs b/sandbox/hypervisor/net_util/src/open_tap.rs similarity index 100% rename from vendor/cube-hypervisor/net_util/src/open_tap.rs rename to sandbox/hypervisor/net_util/src/open_tap.rs diff --git a/vendor/cube-hypervisor/net_util/src/queue_pair.rs b/sandbox/hypervisor/net_util/src/queue_pair.rs similarity index 100% rename from vendor/cube-hypervisor/net_util/src/queue_pair.rs rename to sandbox/hypervisor/net_util/src/queue_pair.rs diff --git a/vendor/cube-hypervisor/net_util/src/tap.rs b/sandbox/hypervisor/net_util/src/tap.rs similarity index 100% rename from vendor/cube-hypervisor/net_util/src/tap.rs rename to sandbox/hypervisor/net_util/src/tap.rs diff --git a/vendor/cube-hypervisor/option_parser/Cargo.toml b/sandbox/hypervisor/option_parser/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/option_parser/Cargo.toml rename to sandbox/hypervisor/option_parser/Cargo.toml diff --git a/vendor/cube-hypervisor/option_parser/src/lib.rs b/sandbox/hypervisor/option_parser/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/option_parser/src/lib.rs rename to sandbox/hypervisor/option_parser/src/lib.rs diff --git a/vendor/cube-hypervisor/pci/Cargo.toml b/sandbox/hypervisor/pci/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/pci/Cargo.toml rename to sandbox/hypervisor/pci/Cargo.toml diff --git a/vendor/cube-hypervisor/pci/src/bus.rs b/sandbox/hypervisor/pci/src/bus.rs similarity index 100% rename from vendor/cube-hypervisor/pci/src/bus.rs rename to sandbox/hypervisor/pci/src/bus.rs diff --git a/vendor/cube-hypervisor/pci/src/configuration.rs b/sandbox/hypervisor/pci/src/configuration.rs similarity index 100% rename from vendor/cube-hypervisor/pci/src/configuration.rs rename to sandbox/hypervisor/pci/src/configuration.rs diff --git a/vendor/cube-hypervisor/pci/src/device.rs b/sandbox/hypervisor/pci/src/device.rs similarity index 100% rename from vendor/cube-hypervisor/pci/src/device.rs rename to sandbox/hypervisor/pci/src/device.rs diff --git a/vendor/cube-hypervisor/pci/src/lib.rs b/sandbox/hypervisor/pci/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/pci/src/lib.rs rename to sandbox/hypervisor/pci/src/lib.rs diff --git a/vendor/cube-hypervisor/pci/src/msi.rs b/sandbox/hypervisor/pci/src/msi.rs similarity index 100% rename from vendor/cube-hypervisor/pci/src/msi.rs rename to sandbox/hypervisor/pci/src/msi.rs diff --git a/vendor/cube-hypervisor/pci/src/msix.rs b/sandbox/hypervisor/pci/src/msix.rs similarity index 100% rename from vendor/cube-hypervisor/pci/src/msix.rs rename to sandbox/hypervisor/pci/src/msix.rs diff --git a/vendor/cube-hypervisor/pci/src/vfio.rs b/sandbox/hypervisor/pci/src/vfio.rs similarity index 100% rename from vendor/cube-hypervisor/pci/src/vfio.rs rename to sandbox/hypervisor/pci/src/vfio.rs diff --git a/vendor/cube-hypervisor/pci/src/vfio_user.rs b/sandbox/hypervisor/pci/src/vfio_user.rs similarity index 100% rename from vendor/cube-hypervisor/pci/src/vfio_user.rs rename to sandbox/hypervisor/pci/src/vfio_user.rs diff --git a/vendor/cube-hypervisor/performance-metrics/Cargo.toml b/sandbox/hypervisor/performance-metrics/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/performance-metrics/Cargo.toml rename to sandbox/hypervisor/performance-metrics/Cargo.toml diff --git a/vendor/cube-hypervisor/performance-metrics/build.rs b/sandbox/hypervisor/performance-metrics/build.rs similarity index 100% rename from vendor/cube-hypervisor/performance-metrics/build.rs rename to sandbox/hypervisor/performance-metrics/build.rs diff --git a/vendor/cube-hypervisor/performance-metrics/src/main.rs b/sandbox/hypervisor/performance-metrics/src/main.rs similarity index 100% rename from vendor/cube-hypervisor/performance-metrics/src/main.rs rename to sandbox/hypervisor/performance-metrics/src/main.rs diff --git a/vendor/cube-hypervisor/performance-metrics/src/performance_tests.rs b/sandbox/hypervisor/performance-metrics/src/performance_tests.rs similarity index 100% rename from vendor/cube-hypervisor/performance-metrics/src/performance_tests.rs rename to sandbox/hypervisor/performance-metrics/src/performance_tests.rs diff --git a/vendor/cube-hypervisor/qcow/Cargo.toml b/sandbox/hypervisor/qcow/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/qcow/Cargo.toml rename to sandbox/hypervisor/qcow/Cargo.toml diff --git a/vendor/cube-hypervisor/qcow/src/qcow.rs b/sandbox/hypervisor/qcow/src/qcow.rs similarity index 100% rename from vendor/cube-hypervisor/qcow/src/qcow.rs rename to sandbox/hypervisor/qcow/src/qcow.rs diff --git a/vendor/cube-hypervisor/qcow/src/qcow_raw_file.rs b/sandbox/hypervisor/qcow/src/qcow_raw_file.rs similarity index 100% rename from vendor/cube-hypervisor/qcow/src/qcow_raw_file.rs rename to sandbox/hypervisor/qcow/src/qcow_raw_file.rs diff --git a/vendor/cube-hypervisor/qcow/src/raw_file.rs b/sandbox/hypervisor/qcow/src/raw_file.rs similarity index 100% rename from vendor/cube-hypervisor/qcow/src/raw_file.rs rename to sandbox/hypervisor/qcow/src/raw_file.rs diff --git a/vendor/cube-hypervisor/qcow/src/refcount.rs b/sandbox/hypervisor/qcow/src/refcount.rs similarity index 100% rename from vendor/cube-hypervisor/qcow/src/refcount.rs rename to sandbox/hypervisor/qcow/src/refcount.rs diff --git a/vendor/cube-hypervisor/qcow/src/vec_cache.rs b/sandbox/hypervisor/qcow/src/vec_cache.rs similarity index 100% rename from vendor/cube-hypervisor/qcow/src/vec_cache.rs rename to sandbox/hypervisor/qcow/src/vec_cache.rs diff --git a/vendor/cube-hypervisor/rate_limiter/Cargo.toml b/sandbox/hypervisor/rate_limiter/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/rate_limiter/Cargo.toml rename to sandbox/hypervisor/rate_limiter/Cargo.toml diff --git a/vendor/cube-hypervisor/rate_limiter/src/lib.rs b/sandbox/hypervisor/rate_limiter/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/rate_limiter/src/lib.rs rename to sandbox/hypervisor/rate_limiter/src/lib.rs diff --git a/vendor/cube-hypervisor/release-notes.md b/sandbox/hypervisor/release-notes.md similarity index 100% rename from vendor/cube-hypervisor/release-notes.md rename to sandbox/hypervisor/release-notes.md diff --git a/vendor/cube-hypervisor/resources/Dockerfile b/sandbox/hypervisor/resources/Dockerfile similarity index 100% rename from vendor/cube-hypervisor/resources/Dockerfile rename to sandbox/hypervisor/resources/Dockerfile diff --git a/vendor/cube-hypervisor/resources/linux-config-aarch64 b/sandbox/hypervisor/resources/linux-config-aarch64 similarity index 100% rename from vendor/cube-hypervisor/resources/linux-config-aarch64 rename to sandbox/hypervisor/resources/linux-config-aarch64 diff --git a/vendor/cube-hypervisor/resources/linux-config-x86_64 b/sandbox/hypervisor/resources/linux-config-x86_64 similarity index 100% rename from vendor/cube-hypervisor/resources/linux-config-x86_64 rename to sandbox/hypervisor/resources/linux-config-x86_64 diff --git a/vendor/cube-hypervisor/scripts/ch-trace-visualiser.py b/sandbox/hypervisor/scripts/ch-trace-visualiser.py similarity index 100% rename from vendor/cube-hypervisor/scripts/ch-trace-visualiser.py rename to sandbox/hypervisor/scripts/ch-trace-visualiser.py diff --git a/vendor/cube-hypervisor/scripts/check-image-compatability.sh b/sandbox/hypervisor/scripts/check-image-compatability.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/check-image-compatability.sh rename to sandbox/hypervisor/scripts/check-image-compatability.sh diff --git a/vendor/cube-hypervisor/scripts/common-aarch64.sh b/sandbox/hypervisor/scripts/common-aarch64.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/common-aarch64.sh rename to sandbox/hypervisor/scripts/common-aarch64.sh diff --git a/vendor/cube-hypervisor/scripts/create-cloud-init.sh b/sandbox/hypervisor/scripts/create-cloud-init.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/create-cloud-init.sh rename to sandbox/hypervisor/scripts/create-cloud-init.sh diff --git a/vendor/cube-hypervisor/scripts/dev_cli.sh b/sandbox/hypervisor/scripts/dev_cli.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/dev_cli.sh rename to sandbox/hypervisor/scripts/dev_cli.sh diff --git a/vendor/cube-hypervisor/scripts/prepare_vdpa.sh b/sandbox/hypervisor/scripts/prepare_vdpa.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/prepare_vdpa.sh rename to sandbox/hypervisor/scripts/prepare_vdpa.sh diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_aarch64.sh b/sandbox/hypervisor/scripts/run_integration_tests_aarch64.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_integration_tests_aarch64.sh rename to sandbox/hypervisor/scripts/run_integration_tests_aarch64.sh diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_live_migration.sh b/sandbox/hypervisor/scripts/run_integration_tests_live_migration.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_integration_tests_live_migration.sh rename to sandbox/hypervisor/scripts/run_integration_tests_live_migration.sh diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_rate_limiter.sh b/sandbox/hypervisor/scripts/run_integration_tests_rate_limiter.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_integration_tests_rate_limiter.sh rename to sandbox/hypervisor/scripts/run_integration_tests_rate_limiter.sh diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_sgx.sh b/sandbox/hypervisor/scripts/run_integration_tests_sgx.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_integration_tests_sgx.sh rename to sandbox/hypervisor/scripts/run_integration_tests_sgx.sh diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_vfio.sh b/sandbox/hypervisor/scripts/run_integration_tests_vfio.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_integration_tests_vfio.sh rename to sandbox/hypervisor/scripts/run_integration_tests_vfio.sh diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_windows_aarch64.sh b/sandbox/hypervisor/scripts/run_integration_tests_windows_aarch64.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_integration_tests_windows_aarch64.sh rename to sandbox/hypervisor/scripts/run_integration_tests_windows_aarch64.sh diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_windows_x86_64.sh b/sandbox/hypervisor/scripts/run_integration_tests_windows_x86_64.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_integration_tests_windows_x86_64.sh rename to sandbox/hypervisor/scripts/run_integration_tests_windows_x86_64.sh diff --git a/vendor/cube-hypervisor/scripts/run_integration_tests_x86_64.sh b/sandbox/hypervisor/scripts/run_integration_tests_x86_64.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_integration_tests_x86_64.sh rename to sandbox/hypervisor/scripts/run_integration_tests_x86_64.sh diff --git a/vendor/cube-hypervisor/scripts/run_metrics.sh b/sandbox/hypervisor/scripts/run_metrics.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_metrics.sh rename to sandbox/hypervisor/scripts/run_metrics.sh diff --git a/vendor/cube-hypervisor/scripts/run_openapi_tests.sh b/sandbox/hypervisor/scripts/run_openapi_tests.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_openapi_tests.sh rename to sandbox/hypervisor/scripts/run_openapi_tests.sh diff --git a/vendor/cube-hypervisor/scripts/run_unit_tests.sh b/sandbox/hypervisor/scripts/run_unit_tests.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/run_unit_tests.sh rename to sandbox/hypervisor/scripts/run_unit_tests.sh diff --git a/vendor/cube-hypervisor/scripts/sha1sums-aarch64 b/sandbox/hypervisor/scripts/sha1sums-aarch64 similarity index 100% rename from vendor/cube-hypervisor/scripts/sha1sums-aarch64 rename to sandbox/hypervisor/scripts/sha1sums-aarch64 diff --git a/vendor/cube-hypervisor/scripts/sha1sums-x86_64 b/sandbox/hypervisor/scripts/sha1sums-x86_64 similarity index 100% rename from vendor/cube-hypervisor/scripts/sha1sums-x86_64 rename to sandbox/hypervisor/scripts/sha1sums-x86_64 diff --git a/vendor/cube-hypervisor/scripts/test-util.sh b/sandbox/hypervisor/scripts/test-util.sh similarity index 100% rename from vendor/cube-hypervisor/scripts/test-util.sh rename to sandbox/hypervisor/scripts/test-util.sh diff --git a/vendor/cube-hypervisor/scripts/windows-11-iot-enterprise-aarch64.raw.sha1 b/sandbox/hypervisor/scripts/windows-11-iot-enterprise-aarch64.raw.sha1 similarity index 100% rename from vendor/cube-hypervisor/scripts/windows-11-iot-enterprise-aarch64.raw.sha1 rename to sandbox/hypervisor/scripts/windows-11-iot-enterprise-aarch64.raw.sha1 diff --git a/vendor/cube-hypervisor/serial_buffer/Cargo.toml b/sandbox/hypervisor/serial_buffer/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/serial_buffer/Cargo.toml rename to sandbox/hypervisor/serial_buffer/Cargo.toml diff --git a/vendor/cube-hypervisor/serial_buffer/src/lib.rs b/sandbox/hypervisor/serial_buffer/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/serial_buffer/src/lib.rs rename to sandbox/hypervisor/serial_buffer/src/lib.rs diff --git a/vendor/cube-hypervisor/src/bin/ch-remote.rs b/sandbox/hypervisor/src/bin/ch-remote.rs similarity index 100% rename from vendor/cube-hypervisor/src/bin/ch-remote.rs rename to sandbox/hypervisor/src/bin/ch-remote.rs diff --git a/vendor/cube-hypervisor/src/bin/cube-cpuid.rs b/sandbox/hypervisor/src/bin/cube-cpuid.rs similarity index 100% rename from vendor/cube-hypervisor/src/bin/cube-cpuid.rs rename to sandbox/hypervisor/src/bin/cube-cpuid.rs diff --git a/vendor/cube-hypervisor/src/common.rs b/sandbox/hypervisor/src/common.rs similarity index 100% rename from vendor/cube-hypervisor/src/common.rs rename to sandbox/hypervisor/src/common.rs diff --git a/vendor/cube-hypervisor/src/lib.rs b/sandbox/hypervisor/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/src/lib.rs rename to sandbox/hypervisor/src/lib.rs diff --git a/vendor/cube-hypervisor/src/main.rs b/sandbox/hypervisor/src/main.rs similarity index 100% rename from vendor/cube-hypervisor/src/main.rs rename to sandbox/hypervisor/src/main.rs diff --git a/vendor/cube-hypervisor/src/vmm_config.rs b/sandbox/hypervisor/src/vmm_config.rs similarity index 100% rename from vendor/cube-hypervisor/src/vmm_config.rs rename to sandbox/hypervisor/src/vmm_config.rs diff --git a/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/meta-data b/sandbox/hypervisor/test_data/cloud-init/ubuntu/meta-data similarity index 100% rename from vendor/cube-hypervisor/test_data/cloud-init/ubuntu/meta-data rename to sandbox/hypervisor/test_data/cloud-init/ubuntu/meta-data diff --git a/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/network-config b/sandbox/hypervisor/test_data/cloud-init/ubuntu/network-config similarity index 100% rename from vendor/cube-hypervisor/test_data/cloud-init/ubuntu/network-config rename to sandbox/hypervisor/test_data/cloud-init/ubuntu/network-config diff --git a/vendor/cube-hypervisor/test_data/cloud-init/ubuntu/user-data b/sandbox/hypervisor/test_data/cloud-init/ubuntu/user-data similarity index 100% rename from vendor/cube-hypervisor/test_data/cloud-init/ubuntu/user-data rename to sandbox/hypervisor/test_data/cloud-init/ubuntu/user-data diff --git a/vendor/cube-hypervisor/test_infra/Cargo.toml b/sandbox/hypervisor/test_infra/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/test_infra/Cargo.toml rename to sandbox/hypervisor/test_infra/Cargo.toml diff --git a/vendor/cube-hypervisor/test_infra/src/lib.rs b/sandbox/hypervisor/test_infra/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/test_infra/src/lib.rs rename to sandbox/hypervisor/test_infra/src/lib.rs diff --git a/vendor/cube-hypervisor/tests/integration.rs b/sandbox/hypervisor/tests/integration.rs similarity index 100% rename from vendor/cube-hypervisor/tests/integration.rs rename to sandbox/hypervisor/tests/integration.rs diff --git a/vendor/cube-hypervisor/tpm/Cargo.toml b/sandbox/hypervisor/tpm/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/tpm/Cargo.toml rename to sandbox/hypervisor/tpm/Cargo.toml diff --git a/vendor/cube-hypervisor/tpm/src/emulator.rs b/sandbox/hypervisor/tpm/src/emulator.rs similarity index 100% rename from vendor/cube-hypervisor/tpm/src/emulator.rs rename to sandbox/hypervisor/tpm/src/emulator.rs diff --git a/vendor/cube-hypervisor/tpm/src/lib.rs b/sandbox/hypervisor/tpm/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/tpm/src/lib.rs rename to sandbox/hypervisor/tpm/src/lib.rs diff --git a/vendor/cube-hypervisor/tpm/src/socket.rs b/sandbox/hypervisor/tpm/src/socket.rs similarity index 100% rename from vendor/cube-hypervisor/tpm/src/socket.rs rename to sandbox/hypervisor/tpm/src/socket.rs diff --git a/vendor/cube-hypervisor/tracer/Cargo.toml b/sandbox/hypervisor/tracer/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/tracer/Cargo.toml rename to sandbox/hypervisor/tracer/Cargo.toml diff --git a/vendor/cube-hypervisor/tracer/src/lib.rs b/sandbox/hypervisor/tracer/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/tracer/src/lib.rs rename to sandbox/hypervisor/tracer/src/lib.rs diff --git a/vendor/cube-hypervisor/tracer/src/tracer.rs b/sandbox/hypervisor/tracer/src/tracer.rs similarity index 100% rename from vendor/cube-hypervisor/tracer/src/tracer.rs rename to sandbox/hypervisor/tracer/src/tracer.rs diff --git a/vendor/cube-hypervisor/tracer/src/tracer_noop.rs b/sandbox/hypervisor/tracer/src/tracer_noop.rs similarity index 100% rename from vendor/cube-hypervisor/tracer/src/tracer_noop.rs rename to sandbox/hypervisor/tracer/src/tracer_noop.rs diff --git a/vendor/cube-hypervisor/vfio_user/Cargo.toml b/sandbox/hypervisor/vfio_user/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/vfio_user/Cargo.toml rename to sandbox/hypervisor/vfio_user/Cargo.toml diff --git a/vendor/cube-hypervisor/vfio_user/src/lib.rs b/sandbox/hypervisor/vfio_user/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/vfio_user/src/lib.rs rename to sandbox/hypervisor/vfio_user/src/lib.rs diff --git a/vendor/cube-hypervisor/vhdx/Cargo.toml b/sandbox/hypervisor/vhdx/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/vhdx/Cargo.toml rename to sandbox/hypervisor/vhdx/Cargo.toml diff --git a/vendor/cube-hypervisor/vhdx/src/lib.rs b/sandbox/hypervisor/vhdx/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/vhdx/src/lib.rs rename to sandbox/hypervisor/vhdx/src/lib.rs diff --git a/vendor/cube-hypervisor/vhdx/src/vhdx.rs b/sandbox/hypervisor/vhdx/src/vhdx.rs similarity index 100% rename from vendor/cube-hypervisor/vhdx/src/vhdx.rs rename to sandbox/hypervisor/vhdx/src/vhdx.rs diff --git a/vendor/cube-hypervisor/vhdx/src/vhdx_bat.rs b/sandbox/hypervisor/vhdx/src/vhdx_bat.rs similarity index 100% rename from vendor/cube-hypervisor/vhdx/src/vhdx_bat.rs rename to sandbox/hypervisor/vhdx/src/vhdx_bat.rs diff --git a/vendor/cube-hypervisor/vhdx/src/vhdx_header.rs b/sandbox/hypervisor/vhdx/src/vhdx_header.rs similarity index 100% rename from vendor/cube-hypervisor/vhdx/src/vhdx_header.rs rename to sandbox/hypervisor/vhdx/src/vhdx_header.rs diff --git a/vendor/cube-hypervisor/vhdx/src/vhdx_io.rs b/sandbox/hypervisor/vhdx/src/vhdx_io.rs similarity index 100% rename from vendor/cube-hypervisor/vhdx/src/vhdx_io.rs rename to sandbox/hypervisor/vhdx/src/vhdx_io.rs diff --git a/vendor/cube-hypervisor/vhdx/src/vhdx_metadata.rs b/sandbox/hypervisor/vhdx/src/vhdx_metadata.rs similarity index 100% rename from vendor/cube-hypervisor/vhdx/src/vhdx_metadata.rs rename to sandbox/hypervisor/vhdx/src/vhdx_metadata.rs diff --git a/vendor/cube-hypervisor/vhost_user_block/Cargo.toml b/sandbox/hypervisor/vhost_user_block/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/vhost_user_block/Cargo.toml rename to sandbox/hypervisor/vhost_user_block/Cargo.toml diff --git a/vendor/cube-hypervisor/vhost_user_block/src/lib.rs b/sandbox/hypervisor/vhost_user_block/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/vhost_user_block/src/lib.rs rename to sandbox/hypervisor/vhost_user_block/src/lib.rs diff --git a/vendor/cube-hypervisor/vhost_user_block/src/main.rs b/sandbox/hypervisor/vhost_user_block/src/main.rs similarity index 100% rename from vendor/cube-hypervisor/vhost_user_block/src/main.rs rename to sandbox/hypervisor/vhost_user_block/src/main.rs diff --git a/vendor/cube-hypervisor/vhost_user_net/Cargo.toml b/sandbox/hypervisor/vhost_user_net/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/vhost_user_net/Cargo.toml rename to sandbox/hypervisor/vhost_user_net/Cargo.toml diff --git a/vendor/cube-hypervisor/vhost_user_net/src/lib.rs b/sandbox/hypervisor/vhost_user_net/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/vhost_user_net/src/lib.rs rename to sandbox/hypervisor/vhost_user_net/src/lib.rs diff --git a/vendor/cube-hypervisor/vhost_user_net/src/main.rs b/sandbox/hypervisor/vhost_user_net/src/main.rs similarity index 100% rename from vendor/cube-hypervisor/vhost_user_net/src/main.rs rename to sandbox/hypervisor/vhost_user_net/src/main.rs diff --git a/vendor/cube-hypervisor/virtio-devices/Cargo.toml b/sandbox/hypervisor/virtio-devices/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/Cargo.toml rename to sandbox/hypervisor/virtio-devices/Cargo.toml diff --git a/vendor/cube-hypervisor/virtio-devices/src/balloon.rs b/sandbox/hypervisor/virtio-devices/src/balloon.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/balloon.rs rename to sandbox/hypervisor/virtio-devices/src/balloon.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/block.rs b/sandbox/hypervisor/virtio-devices/src/block.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/block.rs rename to sandbox/hypervisor/virtio-devices/src/block.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/console.rs b/sandbox/hypervisor/virtio-devices/src/console.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/console.rs rename to sandbox/hypervisor/virtio-devices/src/console.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/device.rs b/sandbox/hypervisor/virtio-devices/src/device.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/device.rs rename to sandbox/hypervisor/virtio-devices/src/device.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/epoll_helper.rs b/sandbox/hypervisor/virtio-devices/src/epoll_helper.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/epoll_helper.rs rename to sandbox/hypervisor/virtio-devices/src/epoll_helper.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/fs.rs b/sandbox/hypervisor/virtio-devices/src/fs.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/fs.rs rename to sandbox/hypervisor/virtio-devices/src/fs.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/iommu.rs b/sandbox/hypervisor/virtio-devices/src/iommu.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/iommu.rs rename to sandbox/hypervisor/virtio-devices/src/iommu.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/lib.rs b/sandbox/hypervisor/virtio-devices/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/lib.rs rename to sandbox/hypervisor/virtio-devices/src/lib.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/mem.rs b/sandbox/hypervisor/virtio-devices/src/mem.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/mem.rs rename to sandbox/hypervisor/virtio-devices/src/mem.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/net.rs b/sandbox/hypervisor/virtio-devices/src/net.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/net.rs rename to sandbox/hypervisor/virtio-devices/src/net.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/pmem.rs b/sandbox/hypervisor/virtio-devices/src/pmem.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/pmem.rs rename to sandbox/hypervisor/virtio-devices/src/pmem.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/rng.rs b/sandbox/hypervisor/virtio-devices/src/rng.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/rng.rs rename to sandbox/hypervisor/virtio-devices/src/rng.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/seccomp_filters.rs b/sandbox/hypervisor/virtio-devices/src/seccomp_filters.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/seccomp_filters.rs rename to sandbox/hypervisor/virtio-devices/src/seccomp_filters.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/thread_helper.rs b/sandbox/hypervisor/virtio-devices/src/thread_helper.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/thread_helper.rs rename to sandbox/hypervisor/virtio-devices/src/thread_helper.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/transport/mod.rs b/sandbox/hypervisor/virtio-devices/src/transport/mod.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/transport/mod.rs rename to sandbox/hypervisor/virtio-devices/src/transport/mod.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/transport/pci_common_config.rs b/sandbox/hypervisor/virtio-devices/src/transport/pci_common_config.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/transport/pci_common_config.rs rename to sandbox/hypervisor/virtio-devices/src/transport/pci_common_config.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/transport/pci_device.rs b/sandbox/hypervisor/virtio-devices/src/transport/pci_device.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/transport/pci_device.rs rename to sandbox/hypervisor/virtio-devices/src/transport/pci_device.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vdpa.rs b/sandbox/hypervisor/virtio-devices/src/vdpa.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vdpa.rs rename to sandbox/hypervisor/virtio-devices/src/vdpa.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vhost_user/blk.rs b/sandbox/hypervisor/virtio-devices/src/vhost_user/blk.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vhost_user/blk.rs rename to sandbox/hypervisor/virtio-devices/src/vhost_user/blk.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vhost_user/fs.rs b/sandbox/hypervisor/virtio-devices/src/vhost_user/fs.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vhost_user/fs.rs rename to sandbox/hypervisor/virtio-devices/src/vhost_user/fs.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vhost_user/mod.rs b/sandbox/hypervisor/virtio-devices/src/vhost_user/mod.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vhost_user/mod.rs rename to sandbox/hypervisor/virtio-devices/src/vhost_user/mod.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vhost_user/net.rs b/sandbox/hypervisor/virtio-devices/src/vhost_user/net.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vhost_user/net.rs rename to sandbox/hypervisor/virtio-devices/src/vhost_user/net.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vhost_user/vu_common_ctrl.rs b/sandbox/hypervisor/virtio-devices/src/vhost_user/vu_common_ctrl.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vhost_user/vu_common_ctrl.rs rename to sandbox/hypervisor/virtio-devices/src/vhost_user/vu_common_ctrl.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/connection.rs b/sandbox/hypervisor/virtio-devices/src/vsock/csm/connection.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/csm/connection.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/csm/connection.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/mod.rs b/sandbox/hypervisor/virtio-devices/src/vsock/csm/mod.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/csm/mod.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/csm/mod.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/csm/txbuf.rs b/sandbox/hypervisor/virtio-devices/src/vsock/csm/txbuf.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/csm/txbuf.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/csm/txbuf.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/device.rs b/sandbox/hypervisor/virtio-devices/src/vsock/device.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/device.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/device.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/mod.rs b/sandbox/hypervisor/virtio-devices/src/vsock/mod.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/mod.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/mod.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/packet.rs b/sandbox/hypervisor/virtio-devices/src/vsock/packet.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/packet.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/packet.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/mod.rs b/sandbox/hypervisor/virtio-devices/src/vsock/unix/mod.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/unix/mod.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/unix/mod.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer.rs b/sandbox/hypervisor/virtio-devices/src/vsock/unix/muxer.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/unix/muxer.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_killq.rs b/sandbox/hypervisor/virtio-devices/src/vsock/unix/muxer_killq.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_killq.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/unix/muxer_killq.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_rxq.rs b/sandbox/hypervisor/virtio-devices/src/vsock/unix/muxer_rxq.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/unix/muxer_rxq.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/unix/muxer_rxq.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/vsock/unix/stream.rs b/sandbox/hypervisor/virtio-devices/src/vsock/unix/stream.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/vsock/unix/stream.rs rename to sandbox/hypervisor/virtio-devices/src/vsock/unix/stream.rs diff --git a/vendor/cube-hypervisor/virtio-devices/src/watchdog.rs b/sandbox/hypervisor/virtio-devices/src/watchdog.rs similarity index 100% rename from vendor/cube-hypervisor/virtio-devices/src/watchdog.rs rename to sandbox/hypervisor/virtio-devices/src/watchdog.rs diff --git a/vendor/cube-hypervisor/virtiofsd/.gitignore b/sandbox/hypervisor/virtiofsd/.gitignore similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/.gitignore rename to sandbox/hypervisor/virtiofsd/.gitignore diff --git a/vendor/cube-hypervisor/virtiofsd/.gitlab-ci.yml b/sandbox/hypervisor/virtiofsd/.gitlab-ci.yml similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/.gitlab-ci.yml rename to sandbox/hypervisor/virtiofsd/.gitlab-ci.yml diff --git a/vendor/cube-hypervisor/virtiofsd/50-virtiofsd.json b/sandbox/hypervisor/virtiofsd/50-virtiofsd.json similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/50-virtiofsd.json rename to sandbox/hypervisor/virtiofsd/50-virtiofsd.json diff --git a/vendor/cube-hypervisor/virtiofsd/CONTRIBUTING.md b/sandbox/hypervisor/virtiofsd/CONTRIBUTING.md similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/CONTRIBUTING.md rename to sandbox/hypervisor/virtiofsd/CONTRIBUTING.md diff --git a/vendor/cube-hypervisor/virtiofsd/Cargo.toml b/sandbox/hypervisor/virtiofsd/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/Cargo.toml rename to sandbox/hypervisor/virtiofsd/Cargo.toml diff --git a/vendor/cube-hypervisor/virtiofsd/LICENSE-APACHE b/sandbox/hypervisor/virtiofsd/LICENSE-APACHE similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/LICENSE-APACHE rename to sandbox/hypervisor/virtiofsd/LICENSE-APACHE diff --git a/vendor/cube-hypervisor/virtiofsd/LICENSE-BSD-3-Clause b/sandbox/hypervisor/virtiofsd/LICENSE-BSD-3-Clause similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/LICENSE-BSD-3-Clause rename to sandbox/hypervisor/virtiofsd/LICENSE-BSD-3-Clause diff --git a/vendor/cube-hypervisor/virtiofsd/README.md b/sandbox/hypervisor/virtiofsd/README.md similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/README.md rename to sandbox/hypervisor/virtiofsd/README.md diff --git a/vendor/cube-hypervisor/virtiofsd/doc/xattr-mapping.md b/sandbox/hypervisor/virtiofsd/doc/xattr-mapping.md similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/doc/xattr-mapping.md rename to sandbox/hypervisor/virtiofsd/doc/xattr-mapping.md diff --git a/vendor/cube-hypervisor/virtiofsd/docs/preserialization-storeonly-design.md b/sandbox/hypervisor/virtiofsd/docs/preserialization-storeonly-design.md similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/docs/preserialization-storeonly-design.md rename to sandbox/hypervisor/virtiofsd/docs/preserialization-storeonly-design.md diff --git a/vendor/cube-hypervisor/virtiofsd/rustfmt.toml b/sandbox/hypervisor/virtiofsd/rustfmt.toml similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/rustfmt.toml rename to sandbox/hypervisor/virtiofsd/rustfmt.toml diff --git a/vendor/cube-hypervisor/virtiofsd/src/descriptor_utils.rs b/sandbox/hypervisor/virtiofsd/src/descriptor_utils.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/descriptor_utils.rs rename to sandbox/hypervisor/virtiofsd/src/descriptor_utils.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/file_traits.rs b/sandbox/hypervisor/virtiofsd/src/file_traits.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/file_traits.rs rename to sandbox/hypervisor/virtiofsd/src/file_traits.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/filesystem.rs b/sandbox/hypervisor/virtiofsd/src/filesystem.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/filesystem.rs rename to sandbox/hypervisor/virtiofsd/src/filesystem.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/fs_cache_req_handler.rs b/sandbox/hypervisor/virtiofsd/src/fs_cache_req_handler.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/fs_cache_req_handler.rs rename to sandbox/hypervisor/virtiofsd/src/fs_cache_req_handler.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/fuse.rs b/sandbox/hypervisor/virtiofsd/src/fuse.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/fuse.rs rename to sandbox/hypervisor/virtiofsd/src/fuse.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/idmap.rs b/sandbox/hypervisor/virtiofsd/src/idmap.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/idmap.rs rename to sandbox/hypervisor/virtiofsd/src/idmap.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/lib.rs b/sandbox/hypervisor/virtiofsd/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/lib.rs rename to sandbox/hypervisor/virtiofsd/src/lib.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/limits.rs b/sandbox/hypervisor/virtiofsd/src/limits.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/limits.rs rename to sandbox/hypervisor/virtiofsd/src/limits.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/macros.rs b/sandbox/hypervisor/virtiofsd/src/macros.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/macros.rs rename to sandbox/hypervisor/virtiofsd/src/macros.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/main.rs b/sandbox/hypervisor/virtiofsd/src/main.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/main.rs rename to sandbox/hypervisor/virtiofsd/src/main.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/oslib.rs b/sandbox/hypervisor/virtiofsd/src/oslib.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/oslib.rs rename to sandbox/hypervisor/virtiofsd/src/oslib.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/credentials.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/credentials.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/credentials.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/credentials.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/deserialization.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/device_state/deserialization.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/deserialization.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/device_state/deserialization.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/mod.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/device_state/mod.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/mod.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/device_state/mod.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/preserialization.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/device_state/preserialization.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/preserialization.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/device_state/preserialization.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialization.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/device_state/serialization.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialization.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/device_state/serialization.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialized.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/device_state/serialized.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/device_state/serialized.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/device_state/serialized.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/file_handle.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/file_handle.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/file_handle.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/file_handle.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/inode_store.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/inode_store.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/inode_store.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/inode_store.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/mod.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/mod.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/mod.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/mod.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/mount_fd.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/mount_fd.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/mount_fd.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/mount_fd.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/read_only.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/read_only.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/read_only.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/read_only.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/stat.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/stat.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/stat.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/stat.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/stat/file_status.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/stat/file_status.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/stat/file_status.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/stat/file_status.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/util.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/util.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/util.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/util.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/passthrough/xattrmap.rs b/sandbox/hypervisor/virtiofsd/src/passthrough/xattrmap.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/passthrough/xattrmap.rs rename to sandbox/hypervisor/virtiofsd/src/passthrough/xattrmap.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/read_dir.rs b/sandbox/hypervisor/virtiofsd/src/read_dir.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/read_dir.rs rename to sandbox/hypervisor/virtiofsd/src/read_dir.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/sandbox.rs b/sandbox/hypervisor/virtiofsd/src/sandbox.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/sandbox.rs rename to sandbox/hypervisor/virtiofsd/src/sandbox.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/seccomp.rs b/sandbox/hypervisor/virtiofsd/src/seccomp.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/seccomp.rs rename to sandbox/hypervisor/virtiofsd/src/seccomp.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/server.rs b/sandbox/hypervisor/virtiofsd/src/server.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/server.rs rename to sandbox/hypervisor/virtiofsd/src/server.rs diff --git a/vendor/cube-hypervisor/virtiofsd/src/util.rs b/sandbox/hypervisor/virtiofsd/src/util.rs similarity index 100% rename from vendor/cube-hypervisor/virtiofsd/src/util.rs rename to sandbox/hypervisor/virtiofsd/src/util.rs diff --git a/vendor/cube-hypervisor/vm-allocator/Cargo.toml b/sandbox/hypervisor/vm-allocator/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/vm-allocator/Cargo.toml rename to sandbox/hypervisor/vm-allocator/Cargo.toml diff --git a/vendor/cube-hypervisor/vm-allocator/src/address.rs b/sandbox/hypervisor/vm-allocator/src/address.rs similarity index 100% rename from vendor/cube-hypervisor/vm-allocator/src/address.rs rename to sandbox/hypervisor/vm-allocator/src/address.rs diff --git a/vendor/cube-hypervisor/vm-allocator/src/gsi.rs b/sandbox/hypervisor/vm-allocator/src/gsi.rs similarity index 100% rename from vendor/cube-hypervisor/vm-allocator/src/gsi.rs rename to sandbox/hypervisor/vm-allocator/src/gsi.rs diff --git a/vendor/cube-hypervisor/vm-allocator/src/lib.rs b/sandbox/hypervisor/vm-allocator/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/vm-allocator/src/lib.rs rename to sandbox/hypervisor/vm-allocator/src/lib.rs diff --git a/vendor/cube-hypervisor/vm-allocator/src/system.rs b/sandbox/hypervisor/vm-allocator/src/system.rs similarity index 100% rename from vendor/cube-hypervisor/vm-allocator/src/system.rs rename to sandbox/hypervisor/vm-allocator/src/system.rs diff --git a/vendor/cube-hypervisor/vm-device/Cargo.toml b/sandbox/hypervisor/vm-device/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/vm-device/Cargo.toml rename to sandbox/hypervisor/vm-device/Cargo.toml diff --git a/vendor/cube-hypervisor/vm-device/src/bus.rs b/sandbox/hypervisor/vm-device/src/bus.rs similarity index 100% rename from vendor/cube-hypervisor/vm-device/src/bus.rs rename to sandbox/hypervisor/vm-device/src/bus.rs diff --git a/vendor/cube-hypervisor/vm-device/src/dma_mapping/mod.rs b/sandbox/hypervisor/vm-device/src/dma_mapping/mod.rs similarity index 100% rename from vendor/cube-hypervisor/vm-device/src/dma_mapping/mod.rs rename to sandbox/hypervisor/vm-device/src/dma_mapping/mod.rs diff --git a/vendor/cube-hypervisor/vm-device/src/dma_mapping/vfio.rs b/sandbox/hypervisor/vm-device/src/dma_mapping/vfio.rs similarity index 100% rename from vendor/cube-hypervisor/vm-device/src/dma_mapping/vfio.rs rename to sandbox/hypervisor/vm-device/src/dma_mapping/vfio.rs diff --git a/vendor/cube-hypervisor/vm-device/src/interrupt/mod.rs b/sandbox/hypervisor/vm-device/src/interrupt/mod.rs similarity index 100% rename from vendor/cube-hypervisor/vm-device/src/interrupt/mod.rs rename to sandbox/hypervisor/vm-device/src/interrupt/mod.rs diff --git a/vendor/cube-hypervisor/vm-device/src/lib.rs b/sandbox/hypervisor/vm-device/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/vm-device/src/lib.rs rename to sandbox/hypervisor/vm-device/src/lib.rs diff --git a/vendor/cube-hypervisor/vm-migration/Cargo.toml b/sandbox/hypervisor/vm-migration/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/vm-migration/Cargo.toml rename to sandbox/hypervisor/vm-migration/Cargo.toml diff --git a/vendor/cube-hypervisor/vm-migration/src/lib.rs b/sandbox/hypervisor/vm-migration/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/vm-migration/src/lib.rs rename to sandbox/hypervisor/vm-migration/src/lib.rs diff --git a/vendor/cube-hypervisor/vm-migration/src/protocol.rs b/sandbox/hypervisor/vm-migration/src/protocol.rs similarity index 100% rename from vendor/cube-hypervisor/vm-migration/src/protocol.rs rename to sandbox/hypervisor/vm-migration/src/protocol.rs diff --git a/vendor/cube-hypervisor/vm-virtio/Cargo.toml b/sandbox/hypervisor/vm-virtio/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/vm-virtio/Cargo.toml rename to sandbox/hypervisor/vm-virtio/Cargo.toml diff --git a/vendor/cube-hypervisor/vm-virtio/src/checked_descriptor.rs b/sandbox/hypervisor/vm-virtio/src/checked_descriptor.rs similarity index 100% rename from vendor/cube-hypervisor/vm-virtio/src/checked_descriptor.rs rename to sandbox/hypervisor/vm-virtio/src/checked_descriptor.rs diff --git a/vendor/cube-hypervisor/vm-virtio/src/lib.rs b/sandbox/hypervisor/vm-virtio/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/vm-virtio/src/lib.rs rename to sandbox/hypervisor/vm-virtio/src/lib.rs diff --git a/vendor/cube-hypervisor/vm-virtio/src/queue.rs b/sandbox/hypervisor/vm-virtio/src/queue.rs similarity index 100% rename from vendor/cube-hypervisor/vm-virtio/src/queue.rs rename to sandbox/hypervisor/vm-virtio/src/queue.rs diff --git a/vendor/cube-hypervisor/vmm/Cargo.toml b/sandbox/hypervisor/vmm/Cargo.toml similarity index 100% rename from vendor/cube-hypervisor/vmm/Cargo.toml rename to sandbox/hypervisor/vmm/Cargo.toml diff --git a/vendor/cube-hypervisor/vmm/src/acpi.rs b/sandbox/hypervisor/vmm/src/acpi.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/acpi.rs rename to sandbox/hypervisor/vmm/src/acpi.rs diff --git a/vendor/cube-hypervisor/vmm/src/api/http/http_endpoint.rs b/sandbox/hypervisor/vmm/src/api/http/http_endpoint.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/api/http/http_endpoint.rs rename to sandbox/hypervisor/vmm/src/api/http/http_endpoint.rs diff --git a/vendor/cube-hypervisor/vmm/src/api/http/mod.rs b/sandbox/hypervisor/vmm/src/api/http/mod.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/api/http/mod.rs rename to sandbox/hypervisor/vmm/src/api/http/mod.rs diff --git a/vendor/cube-hypervisor/vmm/src/api/mod.rs b/sandbox/hypervisor/vmm/src/api/mod.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/api/mod.rs rename to sandbox/hypervisor/vmm/src/api/mod.rs diff --git a/vendor/cube-hypervisor/vmm/src/api/openapi/cloud-hypervisor.yaml b/sandbox/hypervisor/vmm/src/api/openapi/cloud-hypervisor.yaml similarity index 100% rename from vendor/cube-hypervisor/vmm/src/api/openapi/cloud-hypervisor.yaml rename to sandbox/hypervisor/vmm/src/api/openapi/cloud-hypervisor.yaml diff --git a/vendor/cube-hypervisor/vmm/src/api/service.rs b/sandbox/hypervisor/vmm/src/api/service.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/api/service.rs rename to sandbox/hypervisor/vmm/src/api/service.rs diff --git a/vendor/cube-hypervisor/vmm/src/clone3.rs b/sandbox/hypervisor/vmm/src/clone3.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/clone3.rs rename to sandbox/hypervisor/vmm/src/clone3.rs diff --git a/vendor/cube-hypervisor/vmm/src/config.rs b/sandbox/hypervisor/vmm/src/config.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/config.rs rename to sandbox/hypervisor/vmm/src/config.rs diff --git a/vendor/cube-hypervisor/vmm/src/coredump.rs b/sandbox/hypervisor/vmm/src/coredump.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/coredump.rs rename to sandbox/hypervisor/vmm/src/coredump.rs diff --git a/vendor/cube-hypervisor/vmm/src/cpu.rs b/sandbox/hypervisor/vmm/src/cpu.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/cpu.rs rename to sandbox/hypervisor/vmm/src/cpu.rs diff --git a/vendor/cube-hypervisor/vmm/src/device_manager.rs b/sandbox/hypervisor/vmm/src/device_manager.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/device_manager.rs rename to sandbox/hypervisor/vmm/src/device_manager.rs diff --git a/vendor/cube-hypervisor/vmm/src/device_tree.rs b/sandbox/hypervisor/vmm/src/device_tree.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/device_tree.rs rename to sandbox/hypervisor/vmm/src/device_tree.rs diff --git a/vendor/cube-hypervisor/vmm/src/gdb.rs b/sandbox/hypervisor/vmm/src/gdb.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/gdb.rs rename to sandbox/hypervisor/vmm/src/gdb.rs diff --git a/vendor/cube-hypervisor/vmm/src/interrupt.rs b/sandbox/hypervisor/vmm/src/interrupt.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/interrupt.rs rename to sandbox/hypervisor/vmm/src/interrupt.rs diff --git a/vendor/cube-hypervisor/vmm/src/lib.rs b/sandbox/hypervisor/vmm/src/lib.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/lib.rs rename to sandbox/hypervisor/vmm/src/lib.rs diff --git a/vendor/cube-hypervisor/vmm/src/memory_manager.rs b/sandbox/hypervisor/vmm/src/memory_manager.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/memory_manager.rs rename to sandbox/hypervisor/vmm/src/memory_manager.rs diff --git a/vendor/cube-hypervisor/vmm/src/migration.rs b/sandbox/hypervisor/vmm/src/migration.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/migration.rs rename to sandbox/hypervisor/vmm/src/migration.rs diff --git a/vendor/cube-hypervisor/vmm/src/pagemap_anon.rs b/sandbox/hypervisor/vmm/src/pagemap_anon.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/pagemap_anon.rs rename to sandbox/hypervisor/vmm/src/pagemap_anon.rs diff --git a/vendor/cube-hypervisor/vmm/src/pagemap_anon_bench.rs b/sandbox/hypervisor/vmm/src/pagemap_anon_bench.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/pagemap_anon_bench.rs rename to sandbox/hypervisor/vmm/src/pagemap_anon_bench.rs diff --git a/vendor/cube-hypervisor/vmm/src/pci_segment.rs b/sandbox/hypervisor/vmm/src/pci_segment.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/pci_segment.rs rename to sandbox/hypervisor/vmm/src/pci_segment.rs diff --git a/vendor/cube-hypervisor/vmm/src/seccomp_filters.rs b/sandbox/hypervisor/vmm/src/seccomp_filters.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/seccomp_filters.rs rename to sandbox/hypervisor/vmm/src/seccomp_filters.rs diff --git a/vendor/cube-hypervisor/vmm/src/serial_manager.rs b/sandbox/hypervisor/vmm/src/serial_manager.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/serial_manager.rs rename to sandbox/hypervisor/vmm/src/serial_manager.rs diff --git a/vendor/cube-hypervisor/vmm/src/sigwinch_listener.rs b/sandbox/hypervisor/vmm/src/sigwinch_listener.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/sigwinch_listener.rs rename to sandbox/hypervisor/vmm/src/sigwinch_listener.rs diff --git a/vendor/cube-hypervisor/vmm/src/soft_dirty.rs b/sandbox/hypervisor/vmm/src/soft_dirty.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/soft_dirty.rs rename to sandbox/hypervisor/vmm/src/soft_dirty.rs diff --git a/vendor/cube-hypervisor/vmm/src/vm.rs b/sandbox/hypervisor/vmm/src/vm.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/vm.rs rename to sandbox/hypervisor/vmm/src/vm.rs diff --git a/vendor/cube-hypervisor/vmm/src/vm_config.rs b/sandbox/hypervisor/vmm/src/vm_config.rs similarity index 100% rename from vendor/cube-hypervisor/vmm/src/vm_config.rs rename to sandbox/hypervisor/vmm/src/vm_config.rs diff --git a/vendor/cube-hypervisor/workflows/1check.sh b/sandbox/hypervisor/workflows/1check.sh similarity index 100% rename from vendor/cube-hypervisor/workflows/1check.sh rename to sandbox/hypervisor/workflows/1check.sh diff --git a/vendor/cube-hypervisor/workflows/2upload.sh b/sandbox/hypervisor/workflows/2upload.sh similarity index 100% rename from vendor/cube-hypervisor/workflows/2upload.sh rename to sandbox/hypervisor/workflows/2upload.sh diff --git a/tests/sandbox/check_sources.py b/tests/sandbox/check_sources.py index adbcbd7..b43ccb0 100644 --- a/tests/sandbox/check_sources.py +++ b/tests/sandbox/check_sources.py @@ -8,7 +8,7 @@ def main(): - vendor = ROOT / 'vendor/cube-hypervisor' + vendor = ROOT / 'sandbox/hypervisor' source = json.loads((vendor / 'UPSTREAM.json').read_text()) failures = [] for name, expected in source['files_sha256'].items(): diff --git a/vendor/cube-hypervisor/README.agentdrive.md b/vendor/cube-hypervisor/README.agentdrive.md deleted file mode 100644 index bb5cda4..0000000 --- a/vendor/cube-hypervisor/README.agentdrive.md +++ /dev/null @@ -1,13 +0,0 @@ -# AgentDrive 使用的 hypervisor 源码 - -来源:https://github.com/TencentCloud/CubeSandbox - -固定 commit:`4411c96ba8658a959de34693f54f4918db2a432f`(v0.7.3)。导入范围为该版本的完整 `hypervisor/` 目录,并附带仓库根目录许可证 `LICENSE-CUBESANDBOX`。原文件内容和权限保留,文件校验值记录在 `UPSTREAM.json`。 - -`runtime/sandbox-worker` 通过 Cargo 本地路径依赖使用本目录。保留独立 Cargo workspace、模块名称、KVM/PVM 实现、内置 virtio-fs 和原有构建文件。导入时没有修改上游代码。 - -许可证以原文件声明及 `LICENSE-APACHE`、`LICENSE-BSD-3-Clause`、`virtiofsd/LICENSE-*` 为准。CubeSandbox 的 Tencent 修改及 Cloud Hypervisor、rust-vmm、virtiofsd 等上游版权声明保留。 - -更新源码时需要记录新 commit,更新来源文件校验值,审查上游修改,执行 worker 构建以及桌面、MCP、分支文件和内核选择验证。本目录的修改应在此记录,并在被修改文件中注明。 - -校验命令:`python3 tests/sandbox/check_sources.py`。 From 777eac6f5386e8f0929689a4a44374af8633e838 Mon Sep 17 00:00:00 2001 From: gatewayj <835269233@qq.com> Date: Sun, 15 Nov 2026 18:45:32 +0800 Subject: [PATCH 11/12] refactor: consolidate sandbox worker and hypervisor sources --- .gitignore | 2 +- Makefile | 2 +- THIRD_PARTY_NOTICES.md | 13 -- docs/sandbox.md | 4 +- .../.github/ISSUE_TEMPLATE/bug_report.md | 34 ----- sandbox/hypervisor/.github/dependabot.yml | 18 --- .../hypervisor/.github/workflows/audit.yaml | 15 -- .../hypervisor/.github/workflows/build.yaml | 58 ------- sandbox/hypervisor/.github/workflows/dco.yaml | 18 --- .../.github/workflows/docker-image.yaml | 59 -------- .../.github/workflows/fuzz-build.yaml | 29 ---- .../hypervisor/.github/workflows/quality.yaml | 104 ------------- .../hypervisor/.github/workflows/release.yaml | 141 ------------------ sandbox/hypervisor/UPSTREAM.json | 9 -- .../worker}/.gitignore | 0 .../worker}/Cargo.lock | 0 .../worker}/Cargo.toml | 2 +- .../worker}/src/main.rs | 0 tests/sandbox/check_sources.py | 2 +- 19 files changed, 6 insertions(+), 504 deletions(-) delete mode 100644 THIRD_PARTY_NOTICES.md delete mode 100644 sandbox/hypervisor/.github/ISSUE_TEMPLATE/bug_report.md delete mode 100644 sandbox/hypervisor/.github/dependabot.yml delete mode 100644 sandbox/hypervisor/.github/workflows/audit.yaml delete mode 100644 sandbox/hypervisor/.github/workflows/build.yaml delete mode 100644 sandbox/hypervisor/.github/workflows/dco.yaml delete mode 100644 sandbox/hypervisor/.github/workflows/docker-image.yaml delete mode 100644 sandbox/hypervisor/.github/workflows/fuzz-build.yaml delete mode 100644 sandbox/hypervisor/.github/workflows/quality.yaml delete mode 100644 sandbox/hypervisor/.github/workflows/release.yaml rename {runtime/sandbox-worker => sandbox/worker}/.gitignore (100%) rename {runtime/sandbox-worker => sandbox/worker}/Cargo.lock (100%) rename {runtime/sandbox-worker => sandbox/worker}/Cargo.toml (74%) rename {runtime/sandbox-worker => sandbox/worker}/src/main.rs (100%) diff --git a/.gitignore b/.gitignore index 5604bb9..d0164ed 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ /vendor/winfsp_wrs/target/ /vendor/winfsp_wrs/Cargo.lock /sandbox/hypervisor/target/ -/runtime/sandbox-worker/target/ +/sandbox/worker/target/ /tests/dashboard/node_modules/ /build/sandbox/mcp/node_modules/ diff --git a/Makefile b/Makefile index 1d03f97..4334f9f 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ architecture: verify: fmt-check clippy test architecture sandbox-verify: $(PYTHON) tests/sandbox/check_sources.py - +$(CARGO) check --locked --manifest-path runtime/sandbox-worker/Cargo.toml + +$(CARGO) check --locked --manifest-path sandbox/worker/Cargo.toml $(PYTHON) -m unittest discover -s tests/sandbox -p 'test_*.py' clean: $(CARGO) clean diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md deleted file mode 100644 index ed071ec..0000000 --- a/THIRD_PARTY_NOTICES.md +++ /dev/null @@ -1,13 +0,0 @@ -# Sandbox 第三方代码说明 - -AgentDrive 的 Sandbox worker 包含 TencentCloud/CubeSandbox 的 hypervisor 源码,固定来源为 commit `4411c96ba8658a959de34693f54f4918db2a432f`。代码存放在 `sandbox/hypervisor/`,该目录保留 Cloud Hypervisor、Tencent、rust-vmm、virtiofsd 及其他贡献者的原有版权声明。 - -- 来源记录和完整文件校验值:`sandbox/hypervisor/UPSTREAM.json`。 -- CubeSandbox 许可证:`sandbox/hypervisor/LICENSE-CUBESANDBOX`。 -- Hypervisor 许可证:`sandbox/hypervisor/LICENSE-APACHE`、`LICENSE-BSD-3-Clause`。 -- virtiofsd 许可证:`sandbox/hypervisor/virtiofsd/LICENSE-APACHE`、`LICENSE-BSD-3-Clause`。 -- 原文件中的单独声明继续适用。 - -Ubuntu 桌面模板中的部分配置和启动文件来源于同一 commit 的 `examples/ubuntu-desktop/`。具体文件与 AgentDrive 修改记录见 `build/sandbox/SOURCES.md`,许可证见 `build/sandbox/licenses/CubeSandbox-LICENSE`。 - -分发 Sandbox worker 时,应同时提供本说明、上述适用许可证和原有归属声明。分发 Ubuntu 模板及 Linux 内核时,还需要按照其中各组件的许可证提供相应材料。Ubuntu 软件包自带的版权文件保留在模板的 `/usr/share/doc/` 中。 diff --git a/docs/sandbox.md b/docs/sandbox.md index 6b9f51a..f573ff8 100644 --- a/docs/sandbox.md +++ b/docs/sandbox.md @@ -69,7 +69,7 @@ Chromium 在 Ubuntu 的 `DISPLAY=:0` 运行,用户目录固定为 `/home/user/ ```bash cargo build --release -p agentdrived -p agentdrive -cargo build --release --locked --manifest-path runtime/sandbox-worker/Cargo.toml +cargo build --release --locked --manifest-path sandbox/worker/Cargo.toml ``` worker 使用 `sandbox/hypervisor` 中的本地源码,通过独立 Cargo workspace 构建。源码来自 CubeSandbox commit `4411c96ba8658a959de34693f54f4918db2a432f`,原文件、PVM/KVM 支持及内置 virtio-fs 实现均保留。来源、文件校验值和许可证位于该目录,`make sandbox-verify` 检查来源记录、worker 编译和模板相关测试。linux-loader 固定为 0.13.0。 @@ -100,7 +100,7 @@ CSGClaw 模板通过 `build/sandbox/build-assistant-image.sh` 在本地构建的 ```json { - "worker": "/absolute/path/csgdrive/runtime/sandbox-worker/target/release/agentdrive-sandbox-worker", + "worker": "/absolute/path/csgdrive/sandbox/worker/target/release/agentdrive-sandbox-worker", "kernel": "/absolute/path/template/vmlinux", "image": "/absolute/path/template/ubuntu.ext4", "directory": "/absolute/path/agentdrive/sandboxes", diff --git a/sandbox/hypervisor/.github/ISSUE_TEMPLATE/bug_report.md b/sandbox/hypervisor/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index 7fc8dc7..0000000 --- a/sandbox/hypervisor/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -name: Bug report -about: File a bug report -title: '' -labels: '' -assignees: '' - ---- - -**Describe the bug** -A clear and concise description of what the bug is. - -**To Reproduce** -Steps to reproduce the behaviour: - -**Version** - -Output of `cloud-hypervisor --version`: - -Did you build from source, if so build command line (e.g. features): - -**VM configuration** - -What command line did you run (or JSON config data): - -Guest OS version details: - -Host OS version details: - -**Logs** - -Output of `cloud-hypervisor -v` from either standard error or via `--log-file`: - -Linux kernel output: diff --git a/sandbox/hypervisor/.github/dependabot.yml b/sandbox/hypervisor/.github/dependabot.yml deleted file mode 100644 index 0b03830..0000000 --- a/sandbox/hypervisor/.github/dependabot.yml +++ /dev/null @@ -1,18 +0,0 @@ -version: 2 -updates: -- package-ecosystem: cargo - directory: "/" - schedule: - interval: daily - open-pull-requests-limit: 1 - allow: - - dependency-type: direct - - dependency-type: indirect -- package-ecosystem: cargo - directory: "/fuzz" - schedule: - interval: daily - open-pull-requests-limit: 1 - allow: - - dependency-type: direct - - dependency-type: indirect diff --git a/sandbox/hypervisor/.github/workflows/audit.yaml b/sandbox/hypervisor/.github/workflows/audit.yaml deleted file mode 100644 index 17e3fe4..0000000 --- a/sandbox/hypervisor/.github/workflows/audit.yaml +++ /dev/null @@ -1,15 +0,0 @@ -name: Cloud Hypervisor Dependency Audit -on: - pull_request: - paths: - - '**/Cargo.toml' - - '**/Cargo.lock' -jobs: - security_audit: - name: Audit - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - uses: actions-rs/audit-check@v1 - with: - token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/sandbox/hypervisor/.github/workflows/build.yaml b/sandbox/hypervisor/.github/workflows/build.yaml deleted file mode 100644 index 15824bb..0000000 --- a/sandbox/hypervisor/.github/workflows/build.yaml +++ /dev/null @@ -1,58 +0,0 @@ -name: Cloud Hypervisor Build -on: [pull_request, create] - -jobs: - build: - if: github.event_name == 'pull_request' - name: Build - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - rust: - - stable - - beta - - nightly - - "1.60" - target: - - x86_64-unknown-linux-gnu - - x86_64-unknown-linux-musl - steps: - - name: Code checkout - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - - name: Install musl-gcc - run: sudo apt install -y musl-tools - - - name: Install Rust toolchain (${{ matrix.rust }}) - uses: actions-rs/toolchain@v1 - with: - toolchain: ${{ matrix.rust }} - target: ${{ matrix.target }} - override: true - - - name: Build (default features) - run: cargo rustc --locked --bin cloud-hypervisor -- -D warnings - - - name: Build (kvm) - run: cargo rustc --locked --bin cloud-hypervisor --no-default-features --features "kvm" -- -D warnings - - - name: Build (default features + tdx) - run: cargo rustc --locked --bin cloud-hypervisor --features "tdx" -- -D warnings - - - name: Build (default features + guest_debug) - run: cargo rustc --locked --bin cloud-hypervisor --features "guest_debug" -- -D warnings - - - name: Build (mshv) - run: cargo rustc --locked --bin cloud-hypervisor --no-default-features --features "mshv" -- -D warnings - - - name: Build (mshv + kvm) - run: cargo rustc --locked --bin cloud-hypervisor --no-default-features --features "mshv,kvm" -- -D warnings - - - name: Release Build (default features) - run: cargo build --locked --all --release --target=${{ matrix.target }} - - - name: Check build did not modify any files - run: test -z "$(git status --porcelain)" diff --git a/sandbox/hypervisor/.github/workflows/dco.yaml b/sandbox/hypervisor/.github/workflows/dco.yaml deleted file mode 100644 index 531eaf5..0000000 --- a/sandbox/hypervisor/.github/workflows/dco.yaml +++ /dev/null @@ -1,18 +0,0 @@ -name: DCO -on: - pull_request: -jobs: - check: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Set up Python 3.x - uses: actions/setup-python@v1 - with: - python-version: '3.x' - - name: Check DCO - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - pip3 install -U dco-check - dco-check -e "49699333+dependabot[bot]@users.noreply.github.com" diff --git a/sandbox/hypervisor/.github/workflows/docker-image.yaml b/sandbox/hypervisor/.github/workflows/docker-image.yaml deleted file mode 100644 index ddfdc14..0000000 --- a/sandbox/hypervisor/.github/workflows/docker-image.yaml +++ /dev/null @@ -1,59 +0,0 @@ -name: Cloud Hypervisor's Docker image update - -on: - push: - branches: main - paths: resources/Dockerfile - pull_request: - paths: resources/Dockerfile - -jobs: - main: - runs-on: ubuntu-latest - steps: - - name: Code checkout - uses: actions/checkout@v2 - - - name: Set up QEMU - uses: docker/setup-qemu-action@v1 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - - name: Login to DockerHub - if: ${{ github.event_name == 'push' }} - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Docker meta - id: meta - uses: docker/metadata-action@v3 - with: - # list of Docker images to use as base name for tags - images: cloudhypervisor/dev - # generate Docker tags based on the following events/attributes - tags: | - type=raw,value={{date 'YYYYMMDD'}}-0 - type=sha - - - name: Build and push - if: ${{ github.event_name == 'push' }} - uses: docker/build-push-action@v2 - with: - file: ./resources/Dockerfile - platforms: linux/amd64,linux/arm64 - push: true - tags: ${{ steps.meta.outputs.tags }} - - - name: Build only - if: ${{ github.event_name == 'pull_request' }} - uses: docker/build-push-action@v2 - with: - file: ./resources/Dockerfile - platforms: linux/amd64,linux/arm64 - tags: ${{ steps.meta.outputs.tags }} - - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} diff --git a/sandbox/hypervisor/.github/workflows/fuzz-build.yaml b/sandbox/hypervisor/.github/workflows/fuzz-build.yaml deleted file mode 100644 index d7ebba0..0000000 --- a/sandbox/hypervisor/.github/workflows/fuzz-build.yaml +++ /dev/null @@ -1,29 +0,0 @@ -name: Cloud Hypervisor Cargo Fuzz Build -on: [pull_request, create] - -jobs: - build: - if: github.event_name == 'pull_request' - name: Cargo Fuzz Build - runs-on: ubuntu-latest - strategy: - matrix: - rust: - - nightly - target: - - x86_64-unknown-linux-gnu - steps: - - name: Code checkout - uses: actions/checkout@v2 - - name: Install Rust toolchain (${{ matrix.rust }}) - uses: actions-rs/toolchain@v1 - with: - toolchain: ${{ matrix.rust }} - target: ${{ matrix.target }} - override: true - - name: Install Cargo fuzz - # Temporary fix for cargo-fuzz on latest nightly: https://github.com/rust-fuzz/cargo-fuzz/issues/276 - #run: cargo install cargo-fuzz - run: cargo install --git https://github.com/rust-fuzz/cargo-fuzz --rev b4df3e58f767b5cad8d1aa6753961003f56f3609 - - name: Cargo Fuzz Build - run: cargo fuzz build diff --git a/sandbox/hypervisor/.github/workflows/quality.yaml b/sandbox/hypervisor/.github/workflows/quality.yaml deleted file mode 100644 index b77fc51..0000000 --- a/sandbox/hypervisor/.github/workflows/quality.yaml +++ /dev/null @@ -1,104 +0,0 @@ -name: Cloud Hypervisor Quality Checks -on: [pull_request, create] - -jobs: - build: - if: github.event_name == 'pull_request' - name: Quality (clippy, rustfmt) - runs-on: ubuntu-latest - continue-on-error: ${{ matrix.experimental }} - strategy: - fail-fast: false - matrix: - rust: - - stable - target: - - x86_64-unknown-linux-gnu - - aarch64-unknown-linux-gnu - experimental: [false] - include: - - rust: beta - target: x86_64-unknown-linux-gnu - experimental: true - - rust: beta - target: aarch64-unknown-linux-gnu - experimental: true - steps: - - name: Code checkout - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - - name: Install Rust toolchain (${{ matrix.rust }}) - uses: actions-rs/toolchain@v1 - with: - toolchain: ${{ matrix.rust }} - target: ${{ matrix.target }} - override: true - components: rustfmt, clippy - - - name: Debug Check (default features) - if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }} - run: | - set -e - commits=$(git rev-list origin/${{ github.base_ref }}..${{ github.sha }}) - for commit in $commits; do git checkout $commit; cargo check --tests --all --target=${{ matrix.target }}; done - git checkout ${{ github.sha }} - - - name: Formatting (rustfmt) - run: cargo fmt -- --check - - - name: Clippy (kvm) - uses: actions-rs/cargo@v1 - with: - use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} - command: clippy - args: --locked --all --all-targets --no-default-features --tests --features "kvm" -- -D warnings - - - name: Clippy (default features) - uses: actions-rs/cargo@v1 - with: - use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} - command: clippy - args: --locked --all --all-targets --tests -- -D warnings - - - name: Clippy (default features + guest_debug) - uses: actions-rs/cargo@v1 - with: - use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} - command: clippy - args: --locked --all --all-targets --tests --features "guest_debug" -- -D warnings - - - name: Clippy (default features + tracing) - uses: actions-rs/cargo@v1 - with: - use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} - command: clippy - args: --locked --all --all-targets --tests --features "tracing" -- -D warnings - - - name: Clippy (mshv) - if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }} - uses: actions-rs/cargo@v1 - with: - use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} - command: clippy - args: --locked --all --all-targets --no-default-features --tests --features "mshv" -- -D warnings - - - name: Clippy (mshv + kvm) - if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }} - uses: actions-rs/cargo@v1 - with: - use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} - command: clippy - args: --locked --all --all-targets --no-default-features --tests --features "mshv,kvm" -- -D warnings - - - name: Clippy (kvm + tdx) - if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }} - uses: actions-rs/cargo@v1 - with: - use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} - command: clippy - args: --locked --all --all-targets --no-default-features --tests --features "tdx,kvm" -- -D warnings - - - name: Check build did not modify any files - run: test -z "$(git status --porcelain)" diff --git a/sandbox/hypervisor/.github/workflows/release.yaml b/sandbox/hypervisor/.github/workflows/release.yaml deleted file mode 100644 index 5e49600..0000000 --- a/sandbox/hypervisor/.github/workflows/release.yaml +++ /dev/null @@ -1,141 +0,0 @@ -name: Cloud Hypervisor Release -on: [create] - -jobs: - release: - if: github.event_name == 'create' && github.event.ref_type == 'tag' - name: Release - runs-on: ubuntu-latest - steps: - - name: Code checkout - uses: actions/checkout@v2 - - name: Install musl-gcc - run: sudo apt install -y musl-tools - - name: Create release directory - run: rsync -rv --exclude=.git . ../cloud-hypervisor-${{ github.event.ref }} - - name: Install Rust toolchain (x86_64-unknown-linux-gnu) - uses: actions-rs/toolchain@v1 - with: - toolchain: "1.62" - target: x86_64-unknown-linux-gnu - - name: Install Rust toolchain (x86_64-unknown-linux-musl) - uses: actions-rs/toolchain@v1 - with: - toolchain: "1.62" - target: x86_64-unknown-linux-musl - - name: Build - uses: actions-rs/cargo@v1 - with: - toolchain: "1.62" - command: build - args: --all --release --target=x86_64-unknown-linux-gnu - - name: Static Build - uses: actions-rs/cargo@v1 - with: - toolchain: "1.62" - command: build - args: --all --release --target=x86_64-unknown-linux-musl - - name: Strip cloud-hypervisor binaries - run: strip target/*/release/cloud-hypervisor - - name: Install Rust toolchain (aarch64-unknown-linux-musl) - uses: actions-rs/toolchain@v1 - with: - toolchain: "1.62" - target: aarch64-unknown-linux-musl - override: true - - name: Static Build (AArch64) - uses: actions-rs/cargo@v1 - with: - use-cross: true - command: build - args: --all --release --target=aarch64-unknown-linux-musl - - name: Vendor - working-directory: ../cloud-hypervisor-${{ github.event.ref }} - run: | - mkdir ../vendor-cargo-home - export CARGO_HOME=$(realpath ../vendor-cargo-home) - mkdir .cargo - cargo vendor > .cargo/config.toml - - name: Create Release - id: create_release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: ${{ github.ref }} - draft: true - prerelease: true - - name: Create vendored source archive - working-directory: ../ - run: tar cJf cloud-hypervisor-${{ github.event.ref }}.tar.xz cloud-hypervisor-${{ github.event.ref }} - - name: Upload cloud-hypervisor vendored source archive - id: upload-release-cloud-hypervisor-vendored-sources - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ../cloud-hypervisor-${{ github.event.ref }}.tar.xz - asset_name: cloud-hypervisor-${{ github.event.ref }}.tar.xz - asset_content_type: application/x-xz - - name: Upload cloud-hypervisor - id: upload-release-cloud-hypervisor - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: target/x86_64-unknown-linux-gnu/release/cloud-hypervisor - asset_name: cloud-hypervisor - asset_content_type: application/octet-stream - - name: Upload static cloud-hypervisor - id: upload-release-static-cloud-hypervisor - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: target/x86_64-unknown-linux-musl/release/cloud-hypervisor - asset_name: cloud-hypervisor-static - asset_content_type: application/octet-stream - - name: Upload ch-remote - id: upload-release-ch-remote - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: target/x86_64-unknown-linux-gnu/release/ch-remote - asset_name: ch-remote - asset_content_type: application/octet-stream - - name: Upload static-ch-remote - id: upload-release-static-ch-remote - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: target/x86_64-unknown-linux-musl/release/ch-remote - asset_name: ch-remote-static - asset_content_type: application/octet-stream - - name: Upload static AArch64 cloud-hypervisor - id: upload-release-static-aarch64-cloud-hypervisor - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: target/aarch64-unknown-linux-musl/release/cloud-hypervisor - asset_name: cloud-hypervisor-static-aarch64 - asset_content_type: application/octet-stream - - name: Upload static AArch64 ch-remote - id: upload-release-static-aarch64-ch-remote - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: target/aarch64-unknown-linux-musl/release/ch-remote - asset_name: ch-remote-static-aarch64 - asset_content_type: application/octet-stream diff --git a/sandbox/hypervisor/UPSTREAM.json b/sandbox/hypervisor/UPSTREAM.json index 0c0401c..4d394fc 100644 --- a/sandbox/hypervisor/UPSTREAM.json +++ b/sandbox/hypervisor/UPSTREAM.json @@ -3,15 +3,6 @@ "commit": "4411c96ba8658a959de34693f54f4918db2a432f", "subdirectory": "hypervisor", "files_sha256": { - ".github/ISSUE_TEMPLATE/bug_report.md": "1431ccf406fe8166b2343356eadda8599a6e319bd2a426c5fa30df0f210cea2f", - ".github/dependabot.yml": "de1d3dc68293e7a659b24f4a2eddb145f192a1c4d3d8feb2b60a380f613268d1", - ".github/workflows/audit.yaml": "9dee536653e08abc37de9af3fd8c18627a1e9e3c11b87af2a402c12ab68a4e2a", - ".github/workflows/build.yaml": "3aa8e42c65ce1ecee233eff828a9ecb30774503823bceba7a3638fa9df77c689", - ".github/workflows/dco.yaml": "e182d7108113eeab24c6f314414feeca817a6f1423e448ffef9da76c20e79b16", - ".github/workflows/docker-image.yaml": "f9e43e1a924b85855ad1d662cd4b1b01c447cf022fc7c9b7c54b998e34629c87", - ".github/workflows/fuzz-build.yaml": "7c56b4b00e4608c330d80926345fe3d61738d996abc499f9e236a78d80708679", - ".github/workflows/quality.yaml": "4628c1daf474fe73085eb664204b4227aa9d89cbed771d77437b652f7a793607", - ".github/workflows/release.yaml": "cc82cb07218229982abf2e7dc0e40f893e7dc334d1c93084f351eea3d83b0aa4", ".gitignore": "d394da81affc73405ae8831ecff1dd4353318548963f7c13d84a449f77e4dc08", ".rustfmt.toml": "fb15e8e45ebf082778eb6032eafb480cb52624bb8d415ef6db398571c3ae53ad", "CODEOWNERS": "b68dc6affcc8052e5bdb72c492bf270d4dde2087975b44bbb4249a1b57385c7b", diff --git a/runtime/sandbox-worker/.gitignore b/sandbox/worker/.gitignore similarity index 100% rename from runtime/sandbox-worker/.gitignore rename to sandbox/worker/.gitignore diff --git a/runtime/sandbox-worker/Cargo.lock b/sandbox/worker/Cargo.lock similarity index 100% rename from runtime/sandbox-worker/Cargo.lock rename to sandbox/worker/Cargo.lock diff --git a/runtime/sandbox-worker/Cargo.toml b/sandbox/worker/Cargo.toml similarity index 74% rename from runtime/sandbox-worker/Cargo.toml rename to sandbox/worker/Cargo.toml index 10f61de..dc2af2f 100644 --- a/runtime/sandbox-worker/Cargo.toml +++ b/sandbox/worker/Cargo.toml @@ -7,7 +7,7 @@ license = "Apache-2.0" [workspace] [dependencies] -cube-hypervisor = { path = "../../sandbox/hypervisor", features = ["lib_support"] } +cube-hypervisor = { path = "../hypervisor", features = ["lib_support"] } serde_json = "1" # CubeSandbox v0.7.3 uses vm-memory 0.16; linux-loader 0.13.2 uses 0.17. linux-loader = "=0.13.0" diff --git a/runtime/sandbox-worker/src/main.rs b/sandbox/worker/src/main.rs similarity index 100% rename from runtime/sandbox-worker/src/main.rs rename to sandbox/worker/src/main.rs diff --git a/tests/sandbox/check_sources.py b/tests/sandbox/check_sources.py index b43ccb0..1126d20 100644 --- a/tests/sandbox/check_sources.py +++ b/tests/sandbox/check_sources.py @@ -19,7 +19,7 @@ def main(): 'virtiofsd/LICENSE-APACHE', 'virtiofsd/LICENSE-BSD-3-Clause']: if name not in source['files_sha256']: failures.append(f'来源记录缺少许可证:{name}') - worker = ROOT / 'runtime/sandbox-worker' + worker = ROOT / 'sandbox/worker' metadata = json.loads(subprocess.check_output([ 'cargo', 'metadata', '--locked', '--offline', '--no-deps', '--format-version=1', '--manifest-path', str(worker / 'Cargo.toml'), From 13b7952dbbe535cda5ca8de13a7bb74a0ac1b8cc Mon Sep 17 00:00:00 2001 From: gatewayj <835269233@qq.com> Date: Sun, 15 Nov 2026 19:04:04 +0800 Subject: [PATCH 12/12] refactor: organize sandbox templates under sandbox --- .gitignore | 6 +++--- docs/sandbox.md | 20 +++++++++---------- .../templates}/.dockerignore | 0 .../sandbox => sandbox/templates}/.gitignore | 0 .../templates}/64-agentdrive-fonts.conf | 0 .../90-cube-ubuntu-desktop.gschema.override | 0 .../sandbox => sandbox/templates}/Dockerfile | 0 .../sandbox => sandbox/templates}/SOURCES.md | 0 .../templates}/Xwrapper.config | 0 .../templates}/assistant/Dockerfile | 0 .../templates}/assistant/assistant-mcp.py | 0 .../templates}/assistant/assistant-service.py | 0 .../templates}/assistant/runtime.json | 0 .../templates}/browser-session.py | 0 .../templates}/build-assistant-image.sh | 0 .../templates}/build-image.sh | 0 {build/sandbox => sandbox/templates}/chromium | 0 .../templates}/chromium.desktop | 0 .../templates}/chromium.json | 0 .../templates}/desktop_environment.py | 0 .../templates}/desktop_mcp.py | 0 .../templates}/guest-helper.py | 0 .../templates}/guest-init.sh | 0 .../templates}/install-chromium.py | 0 .../templates}/install-kernels.sh | 0 .../templates}/licenses/CubeSandbox-LICENSE | 0 .../templates}/mcp-proxy.py | 0 .../templates}/mcp/gateway.mjs | 0 .../templates}/mcp/package-lock.json | 0 .../templates}/mcp/package.json | 0 .../templates}/mcp/playwright.json | 0 .../templates}/mcp/providers.json | 0 .../templates}/mimeapps.list | 0 .../templates}/novnc-index.html | 0 .../templates}/prepare-network.sh | 0 .../templates}/start-desktop.sh | 0 .../templates}/template-manifest.py | 0 .../sandbox => sandbox/templates}/xorg.conf | 0 tests/sandbox/mcp-gateway.test.mjs | 10 +++++----- tests/sandbox/test_assistant_service.py | 2 +- tests/sandbox/test_chromium_installer.py | 2 +- tests/sandbox/test_desktop_mcp.py | 4 ++-- tests/sandbox/test_guest_mcp_bridge.py | 2 +- tests/sandbox/test_guest_readiness.py | 2 +- tests/sandbox/test_install_kernels.py | 2 +- tests/sandbox/test_mcp_proxy.py | 2 +- 46 files changed, 26 insertions(+), 26 deletions(-) rename {build/sandbox => sandbox/templates}/.dockerignore (100%) rename {build/sandbox => sandbox/templates}/.gitignore (100%) rename {build/sandbox => sandbox/templates}/64-agentdrive-fonts.conf (100%) rename {build/sandbox => sandbox/templates}/90-cube-ubuntu-desktop.gschema.override (100%) rename {build/sandbox => sandbox/templates}/Dockerfile (100%) rename {build/sandbox => sandbox/templates}/SOURCES.md (100%) rename {build/sandbox => sandbox/templates}/Xwrapper.config (100%) rename {build/sandbox => sandbox/templates}/assistant/Dockerfile (100%) rename {build/sandbox => sandbox/templates}/assistant/assistant-mcp.py (100%) rename {build/sandbox => sandbox/templates}/assistant/assistant-service.py (100%) rename {build/sandbox => sandbox/templates}/assistant/runtime.json (100%) rename {build/sandbox => sandbox/templates}/browser-session.py (100%) rename {build/sandbox => sandbox/templates}/build-assistant-image.sh (100%) rename {build/sandbox => sandbox/templates}/build-image.sh (100%) rename {build/sandbox => sandbox/templates}/chromium (100%) rename {build/sandbox => sandbox/templates}/chromium.desktop (100%) rename {build/sandbox => sandbox/templates}/chromium.json (100%) rename {build/sandbox => sandbox/templates}/desktop_environment.py (100%) rename {build/sandbox => sandbox/templates}/desktop_mcp.py (100%) rename {build/sandbox => sandbox/templates}/guest-helper.py (100%) rename {build/sandbox => sandbox/templates}/guest-init.sh (100%) rename {build/sandbox => sandbox/templates}/install-chromium.py (100%) rename {build/sandbox => sandbox/templates}/install-kernels.sh (100%) rename {build/sandbox => sandbox/templates}/licenses/CubeSandbox-LICENSE (100%) rename {build/sandbox => sandbox/templates}/mcp-proxy.py (100%) rename {build/sandbox => sandbox/templates}/mcp/gateway.mjs (100%) rename {build/sandbox => sandbox/templates}/mcp/package-lock.json (100%) rename {build/sandbox => sandbox/templates}/mcp/package.json (100%) rename {build/sandbox => sandbox/templates}/mcp/playwright.json (100%) rename {build/sandbox => sandbox/templates}/mcp/providers.json (100%) rename {build/sandbox => sandbox/templates}/mimeapps.list (100%) rename {build/sandbox => sandbox/templates}/novnc-index.html (100%) rename {build/sandbox => sandbox/templates}/prepare-network.sh (100%) rename {build/sandbox => sandbox/templates}/start-desktop.sh (100%) rename {build/sandbox => sandbox/templates}/template-manifest.py (100%) rename {build/sandbox => sandbox/templates}/xorg.conf (100%) diff --git a/.gitignore b/.gitignore index d0164ed..d03c4f3 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,7 @@ /sandbox/worker/target/ /tests/dashboard/node_modules/ -/build/sandbox/mcp/node_modules/ -/build/sandbox/.cache/ -/build/sandbox/**/__pycache__/ +/sandbox/templates/mcp/node_modules/ +/sandbox/templates/.cache/ +/sandbox/templates/**/__pycache__/ /tests/sandbox/__pycache__/ diff --git a/docs/sandbox.md b/docs/sandbox.md index f573ff8..012144e 100644 --- a/docs/sandbox.md +++ b/docs/sandbox.md @@ -20,7 +20,7 @@ CSGClaw 的登录回调使用当前实例的 `csgclaw.<工作区>.<分支>.local 模板安装 `xdg-utils`,桌面启动时为用户配置 Chromium 为 HTTP、HTTPS 和 HTML 的默认应用。在桌面终端执行 `xdg-open https://example.com` 即可打开网页。 -Chromium 来自官方 Linux 构建,版本与 SHA-256 记录在 `build/sandbox/chromium.json`,构建模板时校验下载内容。该构建需要随模板更新。程序以桌面用户身份运行,并保留 Chromium 自身的进程隔离。模板包含 `fonts-noto-cjk` 和 `fonts-noto-color-emoji`,支持中文与表情显示。 +Chromium 来自官方 Linux 构建,版本与 SHA-256 记录在 `sandbox/templates/chromium.json`,构建模板时校验下载内容。该构建需要随模板更新。程序以桌面用户身份运行,并保留 Chromium 自身的进程隔离。模板包含 `fonts-noto-cjk` 和 `fonts-noto-color-emoji`,支持中文与表情显示。 当前桌面会话通过脚本启动,Chromium 使用 `basic` 凭据存储,以便直接启动浏览器。该模式没有系统密钥环提供的独立凭据加密;实例系统磁盘需要按用户数据保护。 @@ -44,7 +44,7 @@ Sandbox 对外提供一个统一 MCP 入口,内部运行 computer-use-linux "ubuntu_desktop": { "command": "python3", "args": [ - "/absolute/path/csgdrive/build/sandbox/mcp-proxy.py", + "/absolute/path/csgdrive/sandbox/templates/mcp-proxy.py", "--socket", "/absolute/path/agentdrive/sandboxes/BRANCH_ID/vsock.sock" ] @@ -61,7 +61,7 @@ Chromium 在 Ubuntu 的 `DISPLAY=:0` 运行,用户目录固定为 `/home/user/ 可以给 Agent 下达:「打开 Python 官网,新建标签页,点击指定按钮,并将下载文件保存到当前分支。」安装在 Ubuntu 内的 Agent 可以直接启动 `/usr/local/bin/node /opt/agentdrive-mcp/gateway.mjs` 连接相同工具入口。 -内部服务在 `build/sandbox/mcp/providers.json` 注册。新增服务时填写 `id`、`command`、`args`,用 `prefix` 区分工具名称,再安装对应程序并构建模板。Agent 的配置保持不变,重新连接后获得新的工具列表。工具名称冲突会明确报错,单个服务断开时其他服务仍可使用。当前统一入口合并 MCP tools,转发原始参数、结构化结果、图片和工具错误,支持分页工具列表、请求取消及连接清理。 +内部服务在 `sandbox/templates/mcp/providers.json` 注册。新增服务时填写 `id`、`command`、`args`,用 `prefix` 区分工具名称,再安装对应程序并构建模板。Agent 的配置保持不变,重新连接后获得新的工具列表。工具名称冲突会明确报错,单个服务断开时其他服务仍可使用。当前统一入口合并 MCP tools,转发原始参数、结构化结果、图片和工具错误,支持分页工具列表、请求取消及连接清理。 ## 构建 @@ -74,19 +74,19 @@ cargo build --release --locked --manifest-path sandbox/worker/Cargo.toml worker 使用 `sandbox/hypervisor` 中的本地源码,通过独立 Cargo workspace 构建。源码来自 CubeSandbox commit `4411c96ba8658a959de34693f54f4918db2a432f`,原文件、PVM/KVM 支持及内置 virtio-fs 实现均保留。来源、文件校验值和许可证位于该目录,`make sandbox-verify` 检查来源记录、worker 编译和模板相关测试。linux-loader 固定为 0.13.0。 -Ubuntu 模板继续基于腾讯的 `ghcr.io/tencentcloud/cubesandbox-base:2026.16` 镜像。使用 `CUBE_BASE_IMAGE` 可以指定同一镜像的内部镜像仓库地址。桌面文件来源和修改记录见 `build/sandbox/SOURCES.md`,该说明及上游许可证随模板安装到 `/usr/share/doc/agentdrive-sandbox/`。 +Ubuntu 模板继续基于腾讯的 `ghcr.io/tencentcloud/cubesandbox-base:2026.16` 镜像。使用 `CUBE_BASE_IMAGE` 可以指定同一镜像的内部镜像仓库地址。桌面文件来源和修改记录见 `sandbox/templates/SOURCES.md`,该说明及上游许可证随模板安装到 `/usr/share/doc/agentdrive-sandbox/`。 Ubuntu 模板安装 computer-use-linux 0.7.53,下载文件使用 SHA-256 验证。Playwright MCP 固定为 0.0.85,MCP SDK 固定为 1.32.2;Node.js 构建镜像固定 digest,npm 依赖通过 package-lock.json 和 npm ci 安装。 准备与导入的 hypervisor 兼容的 KVM 内核,以及需要使用 PVM 时对应的内核。使用下列命令构建模板,输出目录需要尚未存在: ```bash -build/sandbox/build-image.sh /absolute/path/template /absolute/path/vmlinux +sandbox/templates/build-image.sh /absolute/path/template /absolute/path/vmlinux ``` 构建脚本生成 `kernels.json`,记录 KVM/PVM 内核文件的 SHA-256;内核源码、配置及许可证资料需要随部署产物保存。系统磁盘直接启动 guest-init.sh,guest helper 通过 vsock 提供桌面连接、截图检查、文件刷新和 MCP 标准输入输出。 -CSGClaw 模板通过 `build/sandbox/build-assistant-image.sh` 在本地构建的 `agentdrive-ubuntu:local` 上增加助手。先完成桌面模板构建,再执行助手构建脚本。更新部署时切换模板路径和版本;已有实例保留原系统磁盘,新建实例使用新模板。替换 worker 后需要重启服务及实例。 +CSGClaw 模板通过 `sandbox/templates/build-assistant-image.sh` 在本地构建的 `agentdrive-ubuntu:local` 上增加助手。先完成桌面模板构建,再执行助手构建脚本。更新部署时切换模板路径和版本;已有实例保留原系统磁盘,新建实例使用新模板。替换 worker 后需要重启服务及实例。 ## 启动配置 @@ -106,7 +106,7 @@ CSGClaw 模板通过 `build/sandbox/build-assistant-image.sh` 在本地构建的 "directory": "/absolute/path/agentdrive/sandboxes", "mount_root": "/absolute/path/agentdrive/mounts", "novnc_dir": "/absolute/path/template/novnc", - "mcp_proxy": "/absolute/path/csgdrive/build/sandbox/mcp-proxy.py", + "mcp_proxy": "/absolute/path/csgdrive/sandbox/templates/mcp-proxy.py", "max_instances": 2, "networks": [ {"tap": "adsb0", "guest_ip": "172.30.80.2", "gateway": "172.30.80.1", "dns": ["223.5.5.5", "1.1.1.1"]}, @@ -118,7 +118,7 @@ CSGClaw 模板通过 `build/sandbox/build-assistant-image.sh` 在本地构建的 准备外网接口,参数使用运行 agentdrived 的用户 UID: ```bash -sudo bash build/sandbox/prepare-network.sh "$(id -u)" +sudo bash sandbox/templates/prepare-network.sh "$(id -u)" ``` 脚本创建 `adsb0` 和 `adsb1`,使用 `172.30.80.0/29` 地址范围。运行前应确认该范围未被其他网络使用。每个运行中的实例占用一个接口,停止后释放接口。接口由普通用户打开,网络参数通过内核命令行传给 Ubuntu,启动时配置地址、默认路由和 DNS。省略 `networks` 时实例保持离线;配置了网络但接口不可用时,页面显示启动错误。 @@ -193,7 +193,7 @@ python3 tests/sandbox/playwright_probe.py \ 统一入口和通信通道的自动化测试: ```bash -npm ci --prefix build/sandbox/mcp --ignore-scripts +npm ci --prefix sandbox/templates/mcp --ignore-scripts node --test tests/sandbox/mcp-gateway.test.mjs python3 -m unittest discover -s tests/sandbox -p 'test_*.py' ``` @@ -220,7 +220,7 @@ python3 -m unittest discover -s tests/sandbox -p 'test_*.py' `ubuntu-csgclaw` 模板在现有 GNOME 镜像中预装 CSGClaw v0.10.0 完整发布包,包括 Web UI 与 Agent 执行程序。发布包 SHA-256 在构建脚本中校验,模型凭据不写入模板。需要已经构建的 `agentdrive-ubuntu:local` 基础镜像: ```bash -bash build/sandbox/build-assistant-image.sh /absolute/path/ubuntu-csgclaw /absolute/path/vmlinux +bash sandbox/templates/build-assistant-image.sh /absolute/path/ubuntu-csgclaw /absolute/path/vmlinux ``` 在 `AGENTDRIVE_SANDBOX_CONFIG` 文件中添加以下 `templates` 条目,重新启动 AgentDrive: diff --git a/build/sandbox/.dockerignore b/sandbox/templates/.dockerignore similarity index 100% rename from build/sandbox/.dockerignore rename to sandbox/templates/.dockerignore diff --git a/build/sandbox/.gitignore b/sandbox/templates/.gitignore similarity index 100% rename from build/sandbox/.gitignore rename to sandbox/templates/.gitignore diff --git a/build/sandbox/64-agentdrive-fonts.conf b/sandbox/templates/64-agentdrive-fonts.conf similarity index 100% rename from build/sandbox/64-agentdrive-fonts.conf rename to sandbox/templates/64-agentdrive-fonts.conf diff --git a/build/sandbox/90-cube-ubuntu-desktop.gschema.override b/sandbox/templates/90-cube-ubuntu-desktop.gschema.override similarity index 100% rename from build/sandbox/90-cube-ubuntu-desktop.gschema.override rename to sandbox/templates/90-cube-ubuntu-desktop.gschema.override diff --git a/build/sandbox/Dockerfile b/sandbox/templates/Dockerfile similarity index 100% rename from build/sandbox/Dockerfile rename to sandbox/templates/Dockerfile diff --git a/build/sandbox/SOURCES.md b/sandbox/templates/SOURCES.md similarity index 100% rename from build/sandbox/SOURCES.md rename to sandbox/templates/SOURCES.md diff --git a/build/sandbox/Xwrapper.config b/sandbox/templates/Xwrapper.config similarity index 100% rename from build/sandbox/Xwrapper.config rename to sandbox/templates/Xwrapper.config diff --git a/build/sandbox/assistant/Dockerfile b/sandbox/templates/assistant/Dockerfile similarity index 100% rename from build/sandbox/assistant/Dockerfile rename to sandbox/templates/assistant/Dockerfile diff --git a/build/sandbox/assistant/assistant-mcp.py b/sandbox/templates/assistant/assistant-mcp.py similarity index 100% rename from build/sandbox/assistant/assistant-mcp.py rename to sandbox/templates/assistant/assistant-mcp.py diff --git a/build/sandbox/assistant/assistant-service.py b/sandbox/templates/assistant/assistant-service.py similarity index 100% rename from build/sandbox/assistant/assistant-service.py rename to sandbox/templates/assistant/assistant-service.py diff --git a/build/sandbox/assistant/runtime.json b/sandbox/templates/assistant/runtime.json similarity index 100% rename from build/sandbox/assistant/runtime.json rename to sandbox/templates/assistant/runtime.json diff --git a/build/sandbox/browser-session.py b/sandbox/templates/browser-session.py similarity index 100% rename from build/sandbox/browser-session.py rename to sandbox/templates/browser-session.py diff --git a/build/sandbox/build-assistant-image.sh b/sandbox/templates/build-assistant-image.sh similarity index 100% rename from build/sandbox/build-assistant-image.sh rename to sandbox/templates/build-assistant-image.sh diff --git a/build/sandbox/build-image.sh b/sandbox/templates/build-image.sh similarity index 100% rename from build/sandbox/build-image.sh rename to sandbox/templates/build-image.sh diff --git a/build/sandbox/chromium b/sandbox/templates/chromium similarity index 100% rename from build/sandbox/chromium rename to sandbox/templates/chromium diff --git a/build/sandbox/chromium.desktop b/sandbox/templates/chromium.desktop similarity index 100% rename from build/sandbox/chromium.desktop rename to sandbox/templates/chromium.desktop diff --git a/build/sandbox/chromium.json b/sandbox/templates/chromium.json similarity index 100% rename from build/sandbox/chromium.json rename to sandbox/templates/chromium.json diff --git a/build/sandbox/desktop_environment.py b/sandbox/templates/desktop_environment.py similarity index 100% rename from build/sandbox/desktop_environment.py rename to sandbox/templates/desktop_environment.py diff --git a/build/sandbox/desktop_mcp.py b/sandbox/templates/desktop_mcp.py similarity index 100% rename from build/sandbox/desktop_mcp.py rename to sandbox/templates/desktop_mcp.py diff --git a/build/sandbox/guest-helper.py b/sandbox/templates/guest-helper.py similarity index 100% rename from build/sandbox/guest-helper.py rename to sandbox/templates/guest-helper.py diff --git a/build/sandbox/guest-init.sh b/sandbox/templates/guest-init.sh similarity index 100% rename from build/sandbox/guest-init.sh rename to sandbox/templates/guest-init.sh diff --git a/build/sandbox/install-chromium.py b/sandbox/templates/install-chromium.py similarity index 100% rename from build/sandbox/install-chromium.py rename to sandbox/templates/install-chromium.py diff --git a/build/sandbox/install-kernels.sh b/sandbox/templates/install-kernels.sh similarity index 100% rename from build/sandbox/install-kernels.sh rename to sandbox/templates/install-kernels.sh diff --git a/build/sandbox/licenses/CubeSandbox-LICENSE b/sandbox/templates/licenses/CubeSandbox-LICENSE similarity index 100% rename from build/sandbox/licenses/CubeSandbox-LICENSE rename to sandbox/templates/licenses/CubeSandbox-LICENSE diff --git a/build/sandbox/mcp-proxy.py b/sandbox/templates/mcp-proxy.py similarity index 100% rename from build/sandbox/mcp-proxy.py rename to sandbox/templates/mcp-proxy.py diff --git a/build/sandbox/mcp/gateway.mjs b/sandbox/templates/mcp/gateway.mjs similarity index 100% rename from build/sandbox/mcp/gateway.mjs rename to sandbox/templates/mcp/gateway.mjs diff --git a/build/sandbox/mcp/package-lock.json b/sandbox/templates/mcp/package-lock.json similarity index 100% rename from build/sandbox/mcp/package-lock.json rename to sandbox/templates/mcp/package-lock.json diff --git a/build/sandbox/mcp/package.json b/sandbox/templates/mcp/package.json similarity index 100% rename from build/sandbox/mcp/package.json rename to sandbox/templates/mcp/package.json diff --git a/build/sandbox/mcp/playwright.json b/sandbox/templates/mcp/playwright.json similarity index 100% rename from build/sandbox/mcp/playwright.json rename to sandbox/templates/mcp/playwright.json diff --git a/build/sandbox/mcp/providers.json b/sandbox/templates/mcp/providers.json similarity index 100% rename from build/sandbox/mcp/providers.json rename to sandbox/templates/mcp/providers.json diff --git a/build/sandbox/mimeapps.list b/sandbox/templates/mimeapps.list similarity index 100% rename from build/sandbox/mimeapps.list rename to sandbox/templates/mimeapps.list diff --git a/build/sandbox/novnc-index.html b/sandbox/templates/novnc-index.html similarity index 100% rename from build/sandbox/novnc-index.html rename to sandbox/templates/novnc-index.html diff --git a/build/sandbox/prepare-network.sh b/sandbox/templates/prepare-network.sh similarity index 100% rename from build/sandbox/prepare-network.sh rename to sandbox/templates/prepare-network.sh diff --git a/build/sandbox/start-desktop.sh b/sandbox/templates/start-desktop.sh similarity index 100% rename from build/sandbox/start-desktop.sh rename to sandbox/templates/start-desktop.sh diff --git a/build/sandbox/template-manifest.py b/sandbox/templates/template-manifest.py similarity index 100% rename from build/sandbox/template-manifest.py rename to sandbox/templates/template-manifest.py diff --git a/build/sandbox/xorg.conf b/sandbox/templates/xorg.conf similarity index 100% rename from build/sandbox/xorg.conf rename to sandbox/templates/xorg.conf diff --git a/tests/sandbox/mcp-gateway.test.mjs b/tests/sandbox/mcp-gateway.test.mjs index b203636..ec27676 100644 --- a/tests/sandbox/mcp-gateway.test.mjs +++ b/tests/sandbox/mcp-gateway.test.mjs @@ -4,10 +4,10 @@ import {fileURLToPath} from 'node:url'; import {mkdtemp,writeFile,rm} from 'node:fs/promises'; import {tmpdir} from 'node:os'; import {join} from 'node:path'; -import {Client} from '../../build/sandbox/mcp/node_modules/@modelcontextprotocol/sdk/dist/esm/client/index.js'; -import {InMemoryTransport} from '../../build/sandbox/mcp/node_modules/@modelcontextprotocol/sdk/dist/esm/inMemory.js'; -import {StdioClientTransport} from '../../build/sandbox/mcp/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js'; -import {createGateway} from '../../build/sandbox/mcp/gateway.mjs'; +import {Client} from '../../sandbox/templates/mcp/node_modules/@modelcontextprotocol/sdk/dist/esm/client/index.js'; +import {InMemoryTransport} from '../../sandbox/templates/mcp/node_modules/@modelcontextprotocol/sdk/dist/esm/inMemory.js'; +import {StdioClientTransport} from '../../sandbox/templates/mcp/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js'; +import {createGateway} from '../../sandbox/templates/mcp/gateway.mjs'; const fixture=fileURLToPath(new URL('./mcp-fixture.py',import.meta.url)); const provider=(id,prefix='')=>({id,prefix,command:'python3',args:[fixture,id]}); @@ -48,7 +48,7 @@ test('one unavailable provider leaves other tools usable and reports its failure test('stdio connection negotiates once and disconnect closes its children',async t=>{ const directory=await mkdtemp(join(tmpdir(),'agentdrive-gateway-')); const config=join(directory,'providers.json');await writeFile(config,JSON.stringify({cwd:directory,providers:[provider('desktop'),provider('browser','browser_')]})); - const transport=new StdioClientTransport({command:process.execPath,args:[fileURLToPath(new URL('../../build/sandbox/mcp/gateway.mjs',import.meta.url)),config],stderr:'pipe'}); + const transport=new StdioClientTransport({command:process.execPath,args:[fileURLToPath(new URL('../../sandbox/templates/mcp/gateway.mjs',import.meta.url)),config],stderr:'pipe'}); const client=new Client({name:'external-agent',version:'1'}); t.after(async()=>{await client.close();await rm(directory,{recursive:true,force:true});}); await client.connect(transport);assert.equal(client.getServerVersion().name,'agentdrive-sandbox'); diff --git a/tests/sandbox/test_assistant_service.py b/tests/sandbox/test_assistant_service.py index 61030d5..b18caec 100644 --- a/tests/sandbox/test_assistant_service.py +++ b/tests/sandbox/test_assistant_service.py @@ -5,7 +5,7 @@ import unittest from unittest.mock import patch -spec = importlib.util.spec_from_file_location('assistant_service', Path(__file__).resolve().parents[2] / 'build/sandbox/assistant/assistant-service.py') +spec = importlib.util.spec_from_file_location('assistant_service', Path(__file__).resolve().parents[2] / 'sandbox/templates/assistant/assistant-service.py') assistant = importlib.util.module_from_spec(spec) with patch('pwd.getpwnam', return_value=types.SimpleNamespace(pw_uid=1000, pw_gid=1000, pw_dir='/home/user')): spec.loader.exec_module(assistant) diff --git a/tests/sandbox/test_chromium_installer.py b/tests/sandbox/test_chromium_installer.py index 3e37075..b12dbb3 100644 --- a/tests/sandbox/test_chromium_installer.py +++ b/tests/sandbox/test_chromium_installer.py @@ -8,7 +8,7 @@ import zipfile spec = importlib.util.spec_from_file_location( - 'install_chromium', Path(__file__).resolve().parents[2] / 'build/sandbox/install-chromium.py') + 'install_chromium', Path(__file__).resolve().parents[2] / 'sandbox/templates/install-chromium.py') installer = importlib.util.module_from_spec(spec) spec.loader.exec_module(installer) diff --git a/tests/sandbox/test_desktop_mcp.py b/tests/sandbox/test_desktop_mcp.py index 071a18c..5fbc484 100644 --- a/tests/sandbox/test_desktop_mcp.py +++ b/tests/sandbox/test_desktop_mcp.py @@ -7,7 +7,7 @@ import unittest from pathlib import Path -sys.path.insert(0, str(Path(__file__).resolve().parents[2] / 'build/sandbox')) +sys.path.insert(0, str(Path(__file__).resolve().parents[2] / 'sandbox/templates')) from desktop_environment import environment_instructions from desktop_mcp import enrich, paste_request @@ -23,7 +23,7 @@ def test_stream_adapter_drains_responses_after_client_eof(self): result={'tools':[]} if request['method']=='tools/list' else {'content':[{'type':'text','text':'unchanged'}]} print(json.dumps({'jsonrpc':'2.0','id':request['id'],'result':result}),flush=True) ''') - source = str(Path(__file__).resolve().parents[2] / 'build/sandbox') + source = str(Path(__file__).resolve().parents[2] / 'sandbox/templates') program = ('import sys;sys.path.insert(0,sys.argv[1]);import desktop_mcp as m;' 'm.environment_info=lambda: {};m.environment_instructions=lambda info: "environment";' 'original=m.subprocess.Popen;' diff --git a/tests/sandbox/test_guest_mcp_bridge.py b/tests/sandbox/test_guest_mcp_bridge.py index f43b9a8..c5ed2fd 100644 --- a/tests/sandbox/test_guest_mcp_bridge.py +++ b/tests/sandbox/test_guest_mcp_bridge.py @@ -10,7 +10,7 @@ import unittest from unittest.mock import patch -spec = importlib.util.spec_from_file_location('guest_helper', Path(__file__).resolve().parents[2] / 'build/sandbox/guest-helper.py') +spec = importlib.util.spec_from_file_location('guest_helper', Path(__file__).resolve().parents[2] / 'sandbox/templates/guest-helper.py') helper = importlib.util.module_from_spec(spec) spec.loader.exec_module(helper) diff --git a/tests/sandbox/test_guest_readiness.py b/tests/sandbox/test_guest_readiness.py index daf89b4..9cfcdf1 100644 --- a/tests/sandbox/test_guest_readiness.py +++ b/tests/sandbox/test_guest_readiness.py @@ -7,7 +7,7 @@ import unittest from unittest.mock import MagicMock, patch -spec = importlib.util.spec_from_file_location('guest_helper', Path(__file__).resolve().parents[2] / 'build/sandbox/guest-helper.py') +spec = importlib.util.spec_from_file_location('guest_helper', Path(__file__).resolve().parents[2] / 'sandbox/templates/guest-helper.py') helper = importlib.util.module_from_spec(spec) spec.loader.exec_module(helper) diff --git a/tests/sandbox/test_install_kernels.py b/tests/sandbox/test_install_kernels.py index f8e007d..ac0c84b 100644 --- a/tests/sandbox/test_install_kernels.py +++ b/tests/sandbox/test_install_kernels.py @@ -6,7 +6,7 @@ import tempfile import unittest -SCRIPT = Path(__file__).resolve().parents[2] / 'build/sandbox/install-kernels.sh' +SCRIPT = Path(__file__).resolve().parents[2] / 'sandbox/templates/install-kernels.sh' class InstallKernelsTest(unittest.TestCase): diff --git a/tests/sandbox/test_mcp_proxy.py b/tests/sandbox/test_mcp_proxy.py index 25eaeea..858b1a5 100644 --- a/tests/sandbox/test_mcp_proxy.py +++ b/tests/sandbox/test_mcp_proxy.py @@ -7,7 +7,7 @@ import unittest from pathlib import Path -PROXY = Path(__file__).resolve().parents[2] / 'build/sandbox/mcp-proxy.py' +PROXY = Path(__file__).resolve().parents[2] / 'sandbox/templates/mcp-proxy.py' class ProxyTest(unittest.TestCase):