Skip to content

Commit

Permalink
pci
Browse files Browse the repository at this point in the history
Signed-off-by: Chao Wu <[email protected]>
  • Loading branch information
studychao committed Dec 27, 2023
1 parent 03b782b commit d720702
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 13 deletions.
19 changes: 8 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions src/api_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 30 additions & 1 deletion src/cli_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<NetworkInterfaceConfig> = serde_json::from_str(&args.virnets)
Expand Down
26 changes: 26 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)?;
}
Expand Down
28 changes: 28 additions & 0 deletions src/parser/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<String>,
#[clap(long, value_parser, help = "sys fs path", display_order = 2)]
pub sysfs_path: Option<String>,
#[clap(long, value_parser, help = "bus slot function", display_order = 2)]
pub bus_slot_func: Option<String>,
#[clap(long, value_parser, help = "vendor_device_id", display_order = 2)]
pub vendor_device_id: Option<u32>,
#[clap(long, value_parser, help = "guest_dev_id", display_order = 2)]
pub guest_dev_id: Option<u8>,
#[clap(long, value_parser, help = "clique_id", display_order = 2)]
pub clique_id: Option<u8>,
}

#[derive(Args, Debug, Serialize, Deserialize, Clone)]
pub struct UpdateArgs {
#[clap(
Expand Down
2 changes: 2 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub fn run_with_cli(create_args: CreateArgs, api_sock_path: &String) -> Result<i
println!("Warning: api server is not created because --api-sock-path is not provided when creating VM. Update command is not supported.");
}

println!("[CHAOTEST] hit here");

Ok(Vmm::run_vmm_event_loop(
Arc::new(Mutex::new(vmm)),
vmm_service,
Expand Down
1 change: 1 addition & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn setup_db_log(log_file_path: &String, log_level: &str) {
let guard = set_global_logger(root);
guard.cancel_reset();
slog_stdlog::init().unwrap();
println!("[CHAOTEST]successful!");
}

#[inline]
Expand Down
12 changes: 12 additions & 0 deletions src/vmm_comm_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use dragonball::api::v1::{
VmmData, VmmRequest, VmmResponse, VsockDeviceConfigInfo,
};
use dragonball::device_manager::fs_dev_mgr::{FsDeviceConfigInfo, FsMountConfigInfo};
use dragonball::device_manager::vfio_dev_mgr::HostDeviceConfig;
use dragonball::vcpu::VcpuResizeInfo;
use dragonball::vm::VmConfigInfo;
use vmm_sys_util::eventfd::EventFd;
Expand Down Expand Up @@ -168,4 +169,15 @@ pub trait VMMComm {
})?;
Ok(())
}

fn insert_host_device(&self, host_device_cfg: HostDeviceConfig) -> 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(())
}
}

0 comments on commit d720702

Please sign in to comment.