Skip to content

Commit

Permalink
Minor changes for studychao's comments
Browse files Browse the repository at this point in the history
- Errors have net device type and name.
- Formating the import blocks of  "vmm_comm_trait.rs".

Signed-off-by: Xuewei Niu <[email protected]>
  • Loading branch information
justxuewei committed Nov 23, 2023
1 parent 6a4fb7a commit d2637b3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
9 changes: 7 additions & 2 deletions src/api_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use dragonball::vcpu::VcpuResizeInfo;
use serde_json::Value;
use vmm_sys_util::eventfd::EventFd;

use crate::utils;
use crate::vmm_comm_trait::VMMComm;

pub struct ApiServer {
Expand Down Expand Up @@ -87,8 +88,12 @@ impl ApiServer {
let configs: Vec<NetworkInterfaceConfig> = serde_json::from_str(config_json)
.context("Parse NetworkInterfaceConfig from JSON")?;
for config in configs.iter() {
self.insert_virnet(config.clone())
.context("Insert a virtio device to the Dragonball")?;
self.insert_virnet(config.clone()).with_context(|| {
format!(
"Insert a {} device to the Dragonball",
utils::net_device_name(config)
)
})?;
}
}
Some("insert_virblks") => {
Expand Down
14 changes: 14 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use dragonball::api::v1::NetworkInterfaceConfig;
use slog::Drain;
use slog::*;
use slog_scope::set_global_logger;
Expand Down Expand Up @@ -25,3 +26,16 @@ pub fn setup_db_log(log_file_path: &String, log_level: &str) {
guard.cancel_reset();
slog_stdlog::init().unwrap();
}

#[inline]
/// Get net device name from `NetworkInterfaceConfig`.
pub(crate) fn net_device_name(config: &NetworkInterfaceConfig) -> String {
match &config.backend {
dragonball::api::v1::Backend::Virtio(config) => {
format!("virtio-net({})", config.host_dev_name)
}
dragonball::api::v1::Backend::Vhost(config) => {
format!("vhost-net({})", config.host_dev_name)
}
}
}
34 changes: 20 additions & 14 deletions src/vmm_comm_trait.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use crossbeam_channel::{Receiver, Sender};
use vmm_sys_util::eventfd::EventFd;

use anyhow::{anyhow, Context, Result};
use std::sync::{Arc, Mutex};

use dragonball::{
api::v1::{
BlockDeviceConfigInfo, BootSourceConfig, NetworkInterfaceConfig, VmmAction, VmmActionError,
VmmData, VmmRequest, VmmResponse, VsockDeviceConfigInfo,
},
device_manager::fs_dev_mgr::{FsDeviceConfigInfo, FsMountConfigInfo},
vcpu::VcpuResizeInfo,
vm::VmConfigInfo,
use anyhow::{anyhow, Context, Result};
use crossbeam_channel::{Receiver, Sender};
use dragonball::api::v1::{
BlockDeviceConfigInfo, BootSourceConfig, NetworkInterfaceConfig, VmmAction, VmmActionError,
VmmData, VmmRequest, VmmResponse, VsockDeviceConfigInfo,
};
use dragonball::device_manager::fs_dev_mgr::{FsDeviceConfigInfo, FsMountConfigInfo};
use dragonball::vcpu::VcpuResizeInfo;
use dragonball::vm::VmConfigInfo;
use vmm_sys_util::eventfd::EventFd;

use crate::utils;

pub enum Request {
Sync(VmmAction),
Expand Down Expand Up @@ -135,8 +134,15 @@ pub trait VMMComm {
}

fn insert_virnet(&self, config: NetworkInterfaceConfig) -> Result<()> {
self.handle_request(Request::Sync(VmmAction::InsertNetworkDevice(config)))
.context("Request to insert a virtio device")?;
self.handle_request(Request::Sync(VmmAction::InsertNetworkDevice(
config.clone(),
)))
.with_context(|| {
format!(
"Request to insert a {} device",
utils::net_device_name(&config)
)
})?;
Ok(())
}

Expand Down

0 comments on commit d2637b3

Please sign in to comment.