diff --git a/src/dragonball/src/api/v1/virtio_net.rs b/src/dragonball/src/api/v1/virtio_net.rs index dce9e831f9..61ce65e76f 100644 --- a/src/dragonball/src/api/v1/virtio_net.rs +++ b/src/dragonball/src/api/v1/virtio_net.rs @@ -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 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 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 { diff --git a/src/dragonball/src/api/v1/vmm_action.rs b/src/dragonball/src/api/v1/vmm_action.rs index dc2f2f0651..7703471873 100644 --- a/src/dragonball/src/api/v1/vmm_action.rs +++ b/src/dragonball/src/api/v1/vmm_action.rs @@ -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) => { diff --git a/src/dragonball/src/device_manager/vhost_net_dev_mgr.rs b/src/dragonball/src/device_manager/vhost_net_dev_mgr.rs index a7666b47a8..9d90b7ffb8 100644 --- a/src/dragonball/src/device_manager/vhost_net_dev_mgr.rs +++ b/src/dragonball/src/device_manager/vhost_net_dev_mgr.rs @@ -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() diff --git a/src/dragonball/src/device_manager/virtio_net_dev_mgr.rs b/src/dragonball/src/device_manager/virtio_net_dev_mgr.rs index ddbce30057..cfcf6505e4 100644 --- a/src/dragonball/src/device_manager/virtio_net_dev_mgr.rs +++ b/src/dragonball/src/device_manager/virtio_net_dev_mgr.rs @@ -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 { 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::>()