dragonball: Remove vhost-net dependency on virtio-net

This patch is to remove vhost-net dependency on virtio-net for
dbs-virtio-devices crate. Then, the feature of vhost-net is able to enable
without enabling virtio-net device, error, etc.

Fixes: #8423

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
This commit is contained in:
Xuewei Niu 2023-11-13 11:44:25 +08:00
parent dffc6f611c
commit 49c2e6e23c
6 changed files with 70 additions and 53 deletions

View File

@ -3,6 +3,8 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
use core::panic;
use dbs_utils::net::MacAddr; use dbs_utils::net::MacAddr;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -29,8 +31,14 @@ pub enum Backend {
} }
impl Default for Backend { impl Default for Backend {
#[allow(unreachable_code)]
fn default() -> Self { fn default() -> Self {
Self::Virtio(VirtioConfig::default()) #[cfg(feature = "virtio-net")]
return Self::Virtio(VirtioConfig::default());
#[cfg(feature = "vhost-net")]
return Self::Vhost(VirtioConfig::default());
panic!("no available default network backend")
} }
} }
@ -86,6 +94,7 @@ impl From<&NetworkInterfaceConfig> for VirtioNetDeviceConfigInfo {
.unwrap_or(virtio_net_dev_mgr::DEFAULT_QUEUE_SIZE); .unwrap_or(virtio_net_dev_mgr::DEFAULT_QUEUE_SIZE);
// It is safe because we tested the type of config before. // It is safe because we tested the type of config before.
#[allow(unreachable_patterns)]
let config = match &value.backend { let config = match &value.backend {
Backend::Virtio(config) => config, Backend::Virtio(config) => config,
_ => panic!("The virtio backend config is invalid: {:?}", value), _ => panic!("The virtio backend config is invalid: {:?}", value),
@ -125,6 +134,7 @@ impl From<&NetworkInterfaceConfig> for VhostNetDeviceConfigInfo {
.unwrap_or(vhost_net_dev_mgr::DEFAULT_QUEUE_SIZE); .unwrap_or(vhost_net_dev_mgr::DEFAULT_QUEUE_SIZE);
// It is safe because we tested the type of config before. // It is safe because we tested the type of config before.
#[allow(unreachable_patterns)]
let config = match &value.backend { let config = match &value.backend {
Backend::Vhost(config) => config, Backend::Vhost(config) => config,
_ => panic!("The virtio backend config is invalid: {:?}", value), _ => panic!("The virtio backend config is invalid: {:?}", value),

View File

@ -200,6 +200,8 @@ pub enum VmmAction {
#[cfg(feature = "virtio-net")] #[cfg(feature = "virtio-net")]
/// Update a network interface, after microVM start. Currently, the only updatable properties /// Update a network interface, after microVM start. Currently, the only updatable properties
/// are the RX and TX rate limiters. /// are the RX and TX rate limiters.
/// TODO: vhost-net rate limiters aren't implemented, see:
/// https://github.com/kata-containers/kata-containers/issues/8327
UpdateNetworkInterface(VirtioNetDeviceConfigUpdateInfo), UpdateNetworkInterface(VirtioNetDeviceConfigUpdateInfo),
#[cfg(feature = "virtio-fs")] #[cfg(feature = "virtio-fs")]
@ -318,6 +320,7 @@ impl VmmService {
VmmAction::RemoveBlockDevice(drive_id) => { VmmAction::RemoveBlockDevice(drive_id) => {
self.remove_block_device(vmm, event_mgr, &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 { VmmAction::InsertNetworkDevice(config) => match config.backend {
#[cfg(feature = "virtio-net")] #[cfg(feature = "virtio-net")]
Backend::Virtio(_) => self.add_virtio_net_device(vmm, event_mgr, config.into()), Backend::Virtio(_) => self.add_virtio_net_device(vmm, event_mgr, config.into()),

View File

@ -52,4 +52,4 @@ virtio-fs-pro = ["virtio-fs", "nydus-storage/backend-registry", "nydus-storage/b
virtio-mem = ["virtio-mmio"] virtio-mem = ["virtio-mmio"]
virtio-balloon = ["virtio-mmio"] virtio-balloon = ["virtio-mmio"]
vhost = ["virtio-mmio", "vhost-rs/vhost-user-master", "vhost-rs/vhost-kern"] vhost = ["virtio-mmio", "vhost-rs/vhost-user-master", "vhost-rs/vhost-kern"]
vhost-net = ["virtio-net", "vhost", "vhost-rs/vhost-net"] vhost-net = ["vhost", "vhost-rs/vhost-net"]

View File

@ -46,7 +46,10 @@ pub mod vhost;
use std::io::Error as IOError; use std::io::Error as IOError;
use net::NetError; #[cfg(any(feature = "virtio-net", feature = "vhost-net"))]
use dbs_utils::metric::SharedIncMetric;
#[cfg(any(feature = "virtio-net", feature = "vhost-net"))]
use serde::Serialize;
use virtio_queue::Error as VqError; use virtio_queue::Error as VqError;
use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemoryError}; use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemoryError};
@ -212,7 +215,7 @@ pub enum Error {
#[cfg(feature = "virtio-net")] #[cfg(feature = "virtio-net")]
#[error("virtio-net error: {0:?}")] #[error("virtio-net error: {0:?}")]
VirtioNet(NetError), VirtioNet(net::NetError),
#[cfg(feature = "vhost-net")] #[cfg(feature = "vhost-net")]
#[error("vhost-net error: {0:?}")] #[error("vhost-net error: {0:?}")]
@ -229,7 +232,7 @@ pub enum Error {
} }
// Error for tap devices // Error for tap devices
#[cfg(feature = "virtio-net")] #[cfg(any(feature = "virtio-net", feature = "vhost-net"))]
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum TapError { pub enum TapError {
#[error("missing {0} flags")] #[error("missing {0} flags")]
@ -248,6 +251,48 @@ pub enum TapError {
Enable(#[source] dbs_utils::net::TapError), Enable(#[source] dbs_utils::net::TapError),
} }
#[cfg(any(feature = "virtio-net", feature = "vhost-net"))]
#[inline]
pub fn vnet_hdr_len() -> usize {
std::mem::size_of::<virtio_bindings::bindings::virtio_net::virtio_net_hdr_v1>()
}
/// Metrics specific to the net device.
#[cfg(any(feature = "virtio-net", feature = "vhost-net"))]
#[derive(Default, Serialize)]
pub struct NetDeviceMetrics {
/// Number of times when handling events on a network device.
pub event_count: SharedIncMetric,
/// Number of times when activate failed on a network device.
pub activate_fails: SharedIncMetric,
/// Number of times when interacting with the space config of a network device failed.
pub cfg_fails: SharedIncMetric,
/// Number of times when handling events on a network device failed.
pub event_fails: SharedIncMetric,
/// Number of events associated with the receiving queue.
pub rx_queue_event_count: SharedIncMetric,
/// Number of events associated with the rate limiter installed on the receiving path.
pub rx_event_rate_limiter_count: SharedIncMetric,
/// Number of events received on the associated tap.
pub rx_tap_event_count: SharedIncMetric,
/// Number of bytes received.
pub rx_bytes_count: SharedIncMetric,
/// Number of packets received.
pub rx_packets_count: SharedIncMetric,
/// Number of errors while receiving data.
pub rx_fails: SharedIncMetric,
/// Number of transmitted bytes.
pub tx_bytes_count: SharedIncMetric,
/// Number of errors while transmitting data.
pub tx_fails: SharedIncMetric,
/// Number of transmitted packets.
pub tx_packets_count: SharedIncMetric,
/// Number of events associated with the transmitting queue.
pub tx_queue_event_count: SharedIncMetric,
/// Number of events associated with the rate limiter installed on the transmitting path.
pub tx_rate_limiter_event_count: SharedIncMetric,
}
/// Specialized std::result::Result for Virtio device operations. /// Specialized std::result::Result for Virtio device operations.
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;

View File

@ -9,7 +9,6 @@ use std::any::Any;
use std::cmp; use std::cmp;
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::mem;
use std::ops::Deref; use std::ops::Deref;
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
use std::sync::{mpsc, Arc}; use std::sync::{mpsc, Arc};
@ -18,12 +17,11 @@ use dbs_device::resources::ResourceConstraint;
use dbs_utils::epoll_manager::{ use dbs_utils::epoll_manager::{
EpollManager, EventOps, EventSet, Events, MutEventSubscriber, SubscriberId, EpollManager, EventOps, EventSet, Events, MutEventSubscriber, SubscriberId,
}; };
use dbs_utils::metric::{IncMetric, SharedIncMetric}; use dbs_utils::metric::IncMetric;
use dbs_utils::net::{net_gen, MacAddr, Tap, MAC_ADDR_LEN}; use dbs_utils::net::{net_gen, MacAddr, Tap, MAC_ADDR_LEN};
use dbs_utils::rate_limiter::{BucketUpdate, RateLimiter, TokenType}; use dbs_utils::rate_limiter::{BucketUpdate, RateLimiter, TokenType};
use libc; use libc;
use log::{debug, error, info, trace, warn}; use log::{debug, error, info, trace, warn};
use serde::Serialize;
use virtio_bindings::bindings::virtio_net::*; use virtio_bindings::bindings::virtio_net::*;
use virtio_queue::{QueueOwnedT, QueueSync, QueueT}; use virtio_queue::{QueueOwnedT, QueueSync, QueueT};
use vm_memory::{Bytes, GuestAddress, GuestAddressSpace, GuestMemoryRegion, GuestRegionMmap}; use vm_memory::{Bytes, GuestAddress, GuestAddressSpace, GuestMemoryRegion, GuestRegionMmap};
@ -31,8 +29,8 @@ use vmm_sys_util::eventfd::EventFd;
use crate::device::{VirtioDeviceConfig, VirtioDeviceInfo}; use crate::device::{VirtioDeviceConfig, VirtioDeviceInfo};
use crate::{ use crate::{
ActivateError, ActivateResult, ConfigResult, DbsGuestAddressSpace, Error, Result, TapError, vnet_hdr_len, ActivateError, ActivateResult, ConfigResult, DbsGuestAddressSpace, Error,
VirtioDevice, VirtioQueueConfig, TYPE_NET, NetDeviceMetrics, Result, TapError, VirtioDevice, VirtioQueueConfig, TYPE_NET,
}; };
const NET_DRIVER_NAME: &str = "virtio-net"; const NET_DRIVER_NAME: &str = "virtio-net";
@ -64,41 +62,6 @@ pub enum NetError {
TapError(#[source] TapError), TapError(#[source] TapError),
} }
/// Metrics specific to the net device.
#[derive(Default, Serialize)]
pub struct NetDeviceMetrics {
/// Number of times when handling events on a network device.
pub event_count: SharedIncMetric,
/// Number of times when activate failed on a network device.
pub activate_fails: SharedIncMetric,
/// Number of times when interacting with the space config of a network device failed.
pub cfg_fails: SharedIncMetric,
/// Number of times when handling events on a network device failed.
pub event_fails: SharedIncMetric,
/// Number of events associated with the receiving queue.
pub rx_queue_event_count: SharedIncMetric,
/// Number of events associated with the rate limiter installed on the receiving path.
pub rx_event_rate_limiter_count: SharedIncMetric,
/// Number of events received on the associated tap.
pub rx_tap_event_count: SharedIncMetric,
/// Number of bytes received.
pub rx_bytes_count: SharedIncMetric,
/// Number of packets received.
pub rx_packets_count: SharedIncMetric,
/// Number of errors while receiving data.
pub rx_fails: SharedIncMetric,
/// Number of transmitted bytes.
pub tx_bytes_count: SharedIncMetric,
/// Number of errors while transmitting data.
pub tx_fails: SharedIncMetric,
/// Number of transmitted packets.
pub tx_packets_count: SharedIncMetric,
/// Number of events associated with the transmitting queue.
pub tx_queue_event_count: SharedIncMetric,
/// Number of events associated with the rate limiter installed on the transmitting path.
pub tx_rate_limiter_event_count: SharedIncMetric,
}
struct TxVirtio<Q: QueueT> { struct TxVirtio<Q: QueueT> {
queue: VirtioQueueConfig<Q>, queue: VirtioQueueConfig<Q>,
rate_limiter: RateLimiter, rate_limiter: RateLimiter,
@ -143,10 +106,6 @@ impl<Q: QueueT> RxVirtio<Q> {
} }
} }
pub fn vnet_hdr_len() -> usize {
mem::size_of::<virtio_net_hdr_v1>()
}
#[allow(dead_code)] #[allow(dead_code)]
pub(crate) struct NetEpollHandler< pub(crate) struct NetEpollHandler<
AS: GuestAddressSpace, AS: GuestAddressSpace,

View File

@ -31,14 +31,14 @@ use virtio_bindings::bindings::virtio_ring::*;
use virtio_queue::{Descriptor, DescriptorChain, QueueT}; use virtio_queue::{Descriptor, DescriptorChain, QueueT};
use vm_memory::{Address, Bytes, GuestMemory, GuestMemoryRegion, MemoryRegionAddress}; use vm_memory::{Address, Bytes, GuestMemory, GuestMemoryRegion, MemoryRegionAddress};
use crate::net::{vnet_hdr_len, NetDeviceMetrics};
#[cfg(test)] #[cfg(test)]
use crate::vhost::vhost_kern::test_utils::{ use crate::vhost::vhost_kern::test_utils::{
MockVhostBackend as VhostBackend, MockVhostNet as VhostNet, MockVhostBackend as VhostBackend, MockVhostNet as VhostNet,
}; };
use crate::{ use crate::{
ActivateError, ConfigResult, DbsGuestAddressSpace, Error as VirtioError, vnet_hdr_len, ActivateError, ConfigResult, DbsGuestAddressSpace, Error as VirtioError,
Result as VirtioResult, TapError, VirtioDevice, VirtioDeviceConfig, VirtioDeviceInfo, TYPE_NET, NetDeviceMetrics, Result as VirtioResult, TapError, VirtioDevice, VirtioDeviceConfig,
VirtioDeviceInfo, TYPE_NET,
}; };
const NET_DRIVER_NAME: &str = "vhost-net"; const NET_DRIVER_NAME: &str = "vhost-net";