dragonball: Minor changes for Chao's comments

- Remove two panic statements from InsertNetworkDevice test.
- Rename `NUM_QUEUES` to `DEFAULT_NUM_QUEUES`, `QUEUE_SIZE` to
  `DEFAULT_QUEUE_SIZE` for vhost-net and virtio-net.

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
This commit is contained in:
Xuewei Niu 2023-11-01 09:56:34 +08:00
parent dcdf3c6556
commit 6cd572dbbb
4 changed files with 24 additions and 26 deletions

View File

@ -19,9 +19,11 @@ use crate::device_manager::virtio_net_dev_mgr;
/// An enum to specify a backend of Virtio network
pub enum Backend {
#[serde(rename = "virtio")]
#[cfg(feature = "virtio-net")]
/// Virtio-net
Virtio(VirtioConfig),
#[serde(rename = "vhost")]
#[cfg(feature = "vhost-net")]
/// Vhost-net
Vhost(VirtioConfig),
}
@ -79,7 +81,9 @@ impl From<NetworkInterfaceConfig> for VirtioNetDeviceConfigInfo {
#[cfg(feature = "virtio-net")]
impl From<&NetworkInterfaceConfig> for VirtioNetDeviceConfigInfo {
fn from(value: &NetworkInterfaceConfig) -> Self {
let queue_size = value.queue_size.unwrap_or(virtio_net_dev_mgr::QUEUE_SIZE);
let queue_size = value
.queue_size
.unwrap_or(virtio_net_dev_mgr::DEFAULT_QUEUE_SIZE);
// It is safe because we tested the type of config before.
let config = match &value.backend {
@ -90,7 +94,7 @@ impl From<&NetworkInterfaceConfig> for VirtioNetDeviceConfigInfo {
Self {
iface_id: config.iface_id.clone(),
host_dev_name: config.host_dev_name.clone(),
num_queues: virtio_net_dev_mgr::NUM_QUEUES,
num_queues: virtio_net_dev_mgr::DEFAULT_NUM_QUEUES,
queue_size,
guest_mac: value.guest_mac,
rx_rate_limiter: config.rx_rate_limiter.clone(),
@ -113,8 +117,12 @@ impl From<NetworkInterfaceConfig> for VhostNetDeviceConfigInfo {
#[cfg(feature = "vhost-net")]
impl From<&NetworkInterfaceConfig> for VhostNetDeviceConfigInfo {
fn from(value: &NetworkInterfaceConfig) -> Self {
let num_queues = value.num_queues.unwrap_or(vhost_net_dev_mgr::NUM_QUEUES);
let queue_size = value.queue_size.unwrap_or(vhost_net_dev_mgr::QUEUE_SIZE);
let num_queues = value
.num_queues
.unwrap_or(vhost_net_dev_mgr::DEFAULT_NUM_QUEUES);
let queue_size = value
.queue_size
.unwrap_or(vhost_net_dev_mgr::DEFAULT_QUEUE_SIZE);
// It is safe because we tested the type of config before.
let config = match &value.backend {

View File

@ -191,7 +191,6 @@ pub enum VmmAction {
/// are the RX and TX rate limiters.
UpdateBlockDevice(BlockDeviceConfigUpdateInfo),
#[cfg(any(feature = "virtio-net", feature = "vhost-net"))]
/// Add a new network interface config or update one that already exists using the
/// `NetworkInterfaceConfig` as input. This action can only be called before the microVM has
/// booted. The response is sent using the `OutcomeSender`.
@ -318,20 +317,11 @@ impl VmmService {
VmmAction::RemoveBlockDevice(drive_id) => {
self.remove_block_device(vmm, event_mgr, &drive_id)
}
#[cfg(any(feature = "virtio-net", feature = "vhost-net"))]
VmmAction::InsertNetworkDevice(config) => match config.backend {
Backend::Virtio(_) => {
#[cfg(not(feature = "virtio-net"))]
panic!("virtio-net feature is not enabled");
#[cfg(feature = "virtio-net")]
self.add_virtio_net_device(vmm, event_mgr, config.into())
}
Backend::Vhost(_) => {
#[cfg(not(feature = "vhost-net"))]
panic!("vhost-net feature is not enabled");
#[cfg(feature = "vhost-net")]
self.add_vhost_net_device(vmm, event_mgr, config.into())
}
#[cfg(feature = "virtio-net")]
Backend::Virtio(_) => self.add_virtio_net_device(vmm, event_mgr, config.into()),
#[cfg(feature = "vhost-net")]
Backend::Vhost(_) => self.add_vhost_net_device(vmm, event_mgr, config.into()),
},
#[cfg(feature = "virtio-net")]
VmmAction::UpdateNetworkInterface(netif_update) => {

View File

@ -17,9 +17,9 @@ use crate::address_space_manager::{GuestAddressSpaceImpl, GuestRegionImpl};
use crate::config_manager::{ConfigItem, DeviceConfigInfos};
/// Default number of virtio queues, one rx/tx pair.
pub const NUM_QUEUES: usize = 2;
pub const DEFAULT_NUM_QUEUES: usize = 2;
/// Default size of virtio queues.
pub const QUEUE_SIZE: u16 = 256;
pub const DEFAULT_QUEUE_SIZE: u16 = 256;
// The flag of whether to use the shared irq.
const USE_SHARED_IRQ: bool = true;
// The flag of whether to use the generic irq.
@ -96,12 +96,12 @@ impl VhostNetDeviceConfigInfo {
let queue_size = if self.queue_size > 0 {
self.queue_size
} else {
QUEUE_SIZE
DEFAULT_QUEUE_SIZE
};
let num_queues = if self.num_queues > 0 {
self.num_queues
} else {
NUM_QUEUES
DEFAULT_NUM_QUEUES
};
(0..num_queues).map(|_| queue_size).collect()

View File

@ -26,9 +26,9 @@ use crate::get_bucket_update;
use super::DbsMmioV2Device;
/// Default number of virtio queues, one rx/tx pair.
pub const NUM_QUEUES: usize = 2;
pub const DEFAULT_NUM_QUEUES: usize = 2;
/// Default size of virtio queues.
pub const QUEUE_SIZE: u16 = 256;
pub const DEFAULT_QUEUE_SIZE: u16 = 256;
// The flag of whether to use the shared irq.
const USE_SHARED_IRQ: bool = true;
// The flag of whether to use the generic irq.
@ -164,12 +164,12 @@ impl VirtioNetDeviceConfigInfo {
pub fn queue_sizes(&self) -> Vec<u16> {
let mut queue_size = self.queue_size;
if queue_size == 0 {
queue_size = QUEUE_SIZE;
queue_size = DEFAULT_QUEUE_SIZE;
}
let num_queues = if self.num_queues > 0 {
self.num_queues
} else {
NUM_QUEUES
DEFAULT_NUM_QUEUES
};
(0..num_queues).map(|_| queue_size).collect::<Vec<u16>>()