Skip to content

Commit

Permalink
Merge pull request kata-containers#8426 from justxuewei/vhost-rm-virt…
Browse files Browse the repository at this point in the history
…io-net

dragonball: Remove vhost-net dependency on virtio-net
  • Loading branch information
justxuewei authored Nov 15, 2023
2 parents 906f6b7 + 49c2e6e commit f18794d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 53 deletions.
12 changes: 11 additions & 1 deletion src/dragonball/src/api/v1/virtio_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//
// SPDX-License-Identifier: Apache-2.0

use core::panic;

use dbs_utils::net::MacAddr;
use serde::{Deserialize, Serialize};

Expand All @@ -29,8 +31,14 @@ pub enum Backend {
}

impl Default for Backend {
#[allow(unreachable_code)]
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")
}
}

Expand Down Expand Up @@ -86,6 +94,7 @@ impl From<&NetworkInterfaceConfig> for VirtioNetDeviceConfigInfo {
.unwrap_or(virtio_net_dev_mgr::DEFAULT_QUEUE_SIZE);

// It is safe because we tested the type of config before.
#[allow(unreachable_patterns)]
let config = match &value.backend {
Backend::Virtio(config) => config,
_ => panic!("The virtio backend config is invalid: {:?}", value),
Expand Down Expand Up @@ -125,6 +134,7 @@ impl From<&NetworkInterfaceConfig> for VhostNetDeviceConfigInfo {
.unwrap_or(vhost_net_dev_mgr::DEFAULT_QUEUE_SIZE);

// It is safe because we tested the type of config before.
#[allow(unreachable_patterns)]
let config = match &value.backend {
Backend::Vhost(config) => config,
_ => panic!("The virtio backend config is invalid: {:?}", value),
Expand Down
3 changes: 3 additions & 0 deletions src/dragonball/src/api/v1/vmm_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ pub enum VmmAction {
#[cfg(feature = "virtio-net")]
/// Update a network interface, after microVM start. Currently, the only updatable properties
/// 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),

#[cfg(feature = "virtio-fs")]
Expand Down Expand Up @@ -318,6 +320,7 @@ 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 {
#[cfg(feature = "virtio-net")]
Backend::Virtio(_) => self.add_virtio_net_device(vmm, event_mgr, config.into()),
Expand Down
2 changes: 1 addition & 1 deletion src/dragonball/src/dbs_virtio_devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ virtio-fs-pro = ["virtio-fs", "nydus-storage/backend-registry", "nydus-storage/b
virtio-mem = ["virtio-mmio"]
virtio-balloon = ["virtio-mmio"]
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"]
53 changes: 49 additions & 4 deletions src/dragonball/src/dbs_virtio_devices/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ pub mod vhost;

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 vm_memory::{GuestAddress, GuestAddressSpace, GuestMemoryError};

Expand Down Expand Up @@ -212,7 +215,7 @@ pub enum Error {

#[cfg(feature = "virtio-net")]
#[error("virtio-net error: {0:?}")]
VirtioNet(NetError),
VirtioNet(net::NetError),

#[cfg(feature = "vhost-net")]
#[error("vhost-net error: {0:?}")]
Expand All @@ -229,7 +232,7 @@ pub enum Error {
}

// Error for tap devices
#[cfg(feature = "virtio-net")]
#[cfg(any(feature = "virtio-net", feature = "vhost-net"))]
#[derive(Debug, thiserror::Error)]
pub enum TapError {
#[error("missing {0} flags")]
Expand All @@ -240,14 +243,56 @@ pub enum TapError {

#[error("failed to set vnet_hdr_size: {0}")]
SetVnetHdrSize(#[source] dbs_utils::net::TapError),

#[error("failed to open a tap device: {0}")]
Open(#[source] dbs_utils::net::TapError),

#[error("failed to enable a tap device: {0}")]
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.
pub type Result<T> = std::result::Result<T, Error>;

Expand Down
47 changes: 3 additions & 44 deletions src/dragonball/src/dbs_virtio_devices/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::any::Any;
use std::cmp;
use std::io::{self, Read, Write};
use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::os::unix::io::AsRawFd;
use std::sync::{mpsc, Arc};
Expand All @@ -18,21 +17,20 @@ use dbs_device::resources::ResourceConstraint;
use dbs_utils::epoll_manager::{
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::rate_limiter::{BucketUpdate, RateLimiter, TokenType};
use libc;
use log::{debug, error, info, trace, warn};
use serde::Serialize;
use virtio_bindings::bindings::virtio_net::*;
use virtio_queue::{QueueOwnedT, QueueSync, QueueT};
use vm_memory::{Bytes, GuestAddress, GuestAddressSpace, GuestMemoryRegion, GuestRegionMmap};
use vmm_sys_util::eventfd::EventFd;

use crate::device::{VirtioDeviceConfig, VirtioDeviceInfo};
use crate::{
ActivateError, ActivateResult, ConfigResult, DbsGuestAddressSpace, Error, Result, TapError,
VirtioDevice, VirtioQueueConfig, TYPE_NET,
vnet_hdr_len, ActivateError, ActivateResult, ConfigResult, DbsGuestAddressSpace, Error,
NetDeviceMetrics, Result, TapError, VirtioDevice, VirtioQueueConfig, TYPE_NET,
};

const NET_DRIVER_NAME: &str = "virtio-net";
Expand Down Expand Up @@ -64,41 +62,6 @@ pub enum NetError {
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> {
queue: VirtioQueueConfig<Q>,
rate_limiter: RateLimiter,
Expand Down Expand Up @@ -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)]
pub(crate) struct NetEpollHandler<
AS: GuestAddressSpace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ use virtio_bindings::bindings::virtio_ring::*;
use virtio_queue::{Descriptor, DescriptorChain, QueueT};
use vm_memory::{Address, Bytes, GuestMemory, GuestMemoryRegion, MemoryRegionAddress};

use crate::net::{vnet_hdr_len, NetDeviceMetrics};
#[cfg(test)]
use crate::vhost::vhost_kern::test_utils::{
MockVhostBackend as VhostBackend, MockVhostNet as VhostNet,
};
use crate::{
ActivateError, ConfigResult, DbsGuestAddressSpace, Error as VirtioError,
Result as VirtioResult, TapError, VirtioDevice, VirtioDeviceConfig, VirtioDeviceInfo, TYPE_NET,
vnet_hdr_len, ActivateError, ConfigResult, DbsGuestAddressSpace, Error as VirtioError,
NetDeviceMetrics, Result as VirtioResult, TapError, VirtioDevice, VirtioDeviceConfig,
VirtioDeviceInfo, TYPE_NET,
};

const NET_DRIVER_NAME: &str = "vhost-net";
Expand Down

0 comments on commit f18794d

Please sign in to comment.