From d720702911a8a4c65e1468893c071e1bb581dab0 Mon Sep 17 00:00:00 2001 From: Chao Wu Date: Wed, 27 Dec 2023 19:50:23 +0800 Subject: [PATCH] pci Signed-off-by: Chao Wu --- Cargo.lock | 19 ++++++++----------- Cargo.toml | 3 ++- src/api_server.rs | 18 ++++++++++++++++++ src/cli_instance.rs | 31 ++++++++++++++++++++++++++++++- src/main.rs | 26 ++++++++++++++++++++++++++ src/parser/args.rs | 28 ++++++++++++++++++++++++++++ src/parser/mod.rs | 2 ++ src/utils.rs | 1 + src/vmm_comm_trait.rs | 12 ++++++++++++ 9 files changed, 127 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c6c7a3..1cd07cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -280,7 +280,6 @@ dependencies = [ [[package]] name = "dbs-address-space" version = "0.3.0" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ "arc-swap", "lazy_static", @@ -294,7 +293,6 @@ dependencies = [ [[package]] name = "dbs-allocator" version = "0.1.1" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ "thiserror", ] @@ -302,7 +300,6 @@ dependencies = [ [[package]] name = "dbs-arch" version = "0.2.3" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ "kvm-bindings", "kvm-ioctls", @@ -316,7 +313,6 @@ dependencies = [ [[package]] name = "dbs-boot" version = "0.4.0" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ "dbs-arch", "kvm-bindings", @@ -357,15 +353,14 @@ dependencies = [ [[package]] name = "dbs-device" version = "0.2.0" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ + "downcast-rs", "thiserror", ] [[package]] name = "dbs-interrupt" version = "0.2.2" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ "dbs-arch", "dbs-device", @@ -378,7 +373,6 @@ dependencies = [ [[package]] name = "dbs-legacy-devices" version = "0.1.1" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ "dbs-device", "dbs-utils", @@ -392,7 +386,6 @@ dependencies = [ [[package]] name = "dbs-upcall" version = "0.3.0" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ "anyhow", "dbs-utils", @@ -405,7 +398,6 @@ dependencies = [ [[package]] name = "dbs-utils" version = "0.2.1" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ "anyhow", "event-manager", @@ -420,7 +412,6 @@ dependencies = [ [[package]] name = "dbs-virtio-devices" version = "0.3.1" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ "byteorder", "caps", @@ -446,6 +437,7 @@ dependencies = [ "serde_json", "thiserror", "threadpool", + "timerfd", "vhost", "virtio-bindings", "virtio-queue", @@ -496,10 +488,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "downcast-rs" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" + [[package]] name = "dragonball" version = "0.1.0" -source = "git+https://github.com/kata-containers/kata-containers?branch=main#38eb4077a6d0964834c9ea76d8609b01918198f3" dependencies = [ "anyhow", "arc-swap", diff --git a/Cargo.toml b/Cargo.toml index 3fefe7b..d1927be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -dragonball = { git = "https://github.com/kata-containers/kata-containers", branch = "main", features = [ +dragonball = { path = "/root/kata-containers/src/dragonball", features = [ "virtio-blk", "virtio-fs", "virtio-vsock", @@ -15,6 +15,7 @@ dragonball = { git = "https://github.com/kata-containers/kata-containers", branc "hotplug", "dbs-upcall", "vhost-user-net", + "vhost-user-net" ] } clap = { version = "4.0.27", features = ["derive"] } serde = "1.0.27" diff --git a/src/api_server.rs b/src/api_server.rs index 8003f29..1aca00e 100644 --- a/src/api_server.rs +++ b/src/api_server.rs @@ -12,6 +12,7 @@ use crossbeam_channel::{Receiver, Sender}; use dragonball::api::v1::{NetworkInterfaceConfig, VmmRequest, VmmResponse}; use dragonball::device_manager::blk_dev_mgr::BlockDeviceConfigInfo; use dragonball::device_manager::fs_dev_mgr::FsMountConfigInfo; +use dragonball::device_manager::vfio_dev_mgr::{HostDeviceConfig, VfioPciDeviceConfig}; use dragonball::vcpu::VcpuResizeInfo; use serde_json::Value; use vmm_sys_util::eventfd::EventFd; @@ -80,6 +81,23 @@ impl ApiServer { }; return self.resize_vcpu(resize_vcpu_cfg); } + Some("insert_host_device") => { + let host_device_config = HostDeviceConfig { + hostdev_id: v["hostdev_id"].as_str().unwrap().to_owned(), + sysfs_path: v["sysfs_path"].as_str().unwrap().to_owned(), + dev_config: VfioPciDeviceConfig { + bus_slot_func: v["bus_slot_func"].as_str().unwrap().to_owned(), + vendor_device_id: v["vendor_device_id"] + .as_u64() + .map(|vendor_device_id: u64| vendor_device_id as u32) + .unwrap(), + guest_dev_id: None, + clique_id: None, + }, + }; + self.insert_host_device(host_device_config) + .expect("Failed to insert a host device"); + } Some("insert_virnets") => { let config_json = match v["config"].as_str() { Some(config_json) => config_json, diff --git a/src/cli_instance.rs b/src/cli_instance.rs index 343a5b2..358e5e4 100644 --- a/src/cli_instance.rs +++ b/src/cli_instance.rs @@ -20,7 +20,10 @@ use dragonball::{ BlockDeviceConfigInfo, BootSourceConfig, InstanceInfo, NetworkInterfaceConfig, VmmRequest, VmmResponse, VsockDeviceConfigInfo, }, - device_manager::fs_dev_mgr::FsDeviceConfigInfo, + device_manager::{ + fs_dev_mgr::FsDeviceConfigInfo, + vfio_dev_mgr::{HostDeviceConfig, VfioPciDeviceConfig}, + }, vm::{CpuTopology, VmConfigInfo}, }; @@ -97,6 +100,7 @@ impl CliInstance { // as in crate `dragonball` serial_path will be assigned with a default value, // we need a special token to enable the stdio console. serial_path: serial_path.clone(), + pci_hotplug_enabled: args.host_device.pci_hotplug_enabled, }; if let Some(com1_sock_path) = serial_path { @@ -153,6 +157,31 @@ impl CliInstance { .expect("failed to set vsock socket path"); } + if !args.host_device.hostdev_id.is_none() { + let host_device_config = + HostDeviceConfig { + hostdev_id: args + .host_device + .hostdev_id + .expect("There has to be hostdev_id if you want to add host device."), + sysfs_path: args + .host_device + .sysfs_path + .expect("There has to be sysfs_path if you want to add host device."), + dev_config: VfioPciDeviceConfig { + bus_slot_func: args.host_device.bus_slot_func.expect( + "There has to be bus_slot_func if you want to add host device.", + ), + vendor_device_id: args.host_device.vendor_device_id.expect( + "There has to be vendor_device_id if you want to add host device.", + ), + guest_dev_id: args.host_device.guest_dev_id, + clique_id: args.host_device.clique_id, + }, + }; + self.insert_host_device(host_device_config) + .expect("Failed to insert a host device"); + } // Virtio devices if !args.virnets.is_empty() { let configs: Vec = serde_json::from_str(&args.virnets) diff --git a/src/main.rs b/src/main.rs index dafa520..06c5e71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,14 @@ use parser::run_with_cli; use crate::parser::args::{Commands, DBSArgs}; +use dragonball::api::v1::NetworkInterfaceConfig; +use slog::Drain; +use slog::*; +use slog_scope::set_global_logger; + +use std::str::FromStr; +use std::sync::Mutex; + mod api_client; mod api_server; mod cli_instance; @@ -20,6 +28,24 @@ fn main() -> Result<()> { let args: DBSArgs = DBSArgs::parse(); match args.command { Some(Commands::Create { create_args }) => { + // let log_file = &create_args.log_file; + // let log_level = Level::from_str(&create_args.log_level).unwrap(); + + // let file = std::fs::OpenOptions::new() + // .truncate(true) + // .read(true) + // .create(true) + // .write(true) + // .open(log_file) + // .expect("Cannot write to the log file."); + + // let root = slog::Logger::root( + // Mutex::new(slog_json::Json::default(file).filter_level(log_level)).map(slog::Fuse), + // o!("version" => env!("CARGO_PKG_VERSION")), + // ); + + // let _guard = set_global_logger(root); + // slog_stdlog::init().unwrap(); utils::setup_db_log(&create_args.log_file, &create_args.log_level); run_with_cli(create_args, &args.api_sock_path)?; } diff --git a/src/parser/args.rs b/src/parser/args.rs index b97cb5a..e9591ab 100644 --- a/src/parser/args.rs +++ b/src/parser/args.rs @@ -120,6 +120,10 @@ pub struct CreateArgs { #[clap(flatten)] pub mem: MemArgs, + /// features of host devices + #[clap(flatten)] + pub host_device: HostDeviceArgs, + // The serial path used to communicate with VM #[clap( short, @@ -282,6 +286,30 @@ pub struct MemArgs { pub mem_size: usize, } +#[derive(Args, Debug, Serialize, Deserialize, Clone)] +pub struct HostDeviceArgs { + #[clap( + long, + value_parser, + help = "whether pci hotplug ability is enabled or not", + default_value_t = false, + display_order = 2 + )] + pub pci_hotplug_enabled: bool, + #[clap(long, value_parser, help = "host dev id", display_order = 2)] + pub hostdev_id: Option, + #[clap(long, value_parser, help = "sys fs path", display_order = 2)] + pub sysfs_path: Option, + #[clap(long, value_parser, help = "bus slot function", display_order = 2)] + pub bus_slot_func: Option, + #[clap(long, value_parser, help = "vendor_device_id", display_order = 2)] + pub vendor_device_id: Option, + #[clap(long, value_parser, help = "guest_dev_id", display_order = 2)] + pub guest_dev_id: Option, + #[clap(long, value_parser, help = "clique_id", display_order = 2)] + pub clique_id: Option, +} + #[derive(Args, Debug, Serialize, Deserialize, Clone)] pub struct UpdateArgs { #[clap( diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 83e5a3f..257240f 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -83,6 +83,8 @@ pub fn run_with_cli(create_args: CreateArgs, api_sock_path: &String) -> Result Result<()> { + self.handle_request(Request::Sync(VmmAction::InsertHostDevice(host_device_cfg.clone()))) + .with_context(|| { + format!( + "Failed to insert host device hostdev_id {:?}, sysfs_path {:?}, host_device_cfg {:?}", + host_device_cfg.hostdev_id, host_device_cfg.sysfs_path, host_device_cfg.dev_config + ) + })?; + Ok(()) + } }