Skip to content

Commit

Permalink
feat(virtio)!: associated type Feature for Virtio
Browse files Browse the repository at this point in the history
Add the associated type `Feature` to the trait `Virtio` so that the
feature bits can be pretty printed universally, benefited from the
crate bitflags.

Signed-off-by: Changyuan Lyu <[email protected]>
  • Loading branch information
Lencerf committed May 31, 2024
1 parent e2f11df commit 60fedf3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions alioth/src/virtio/dev/blk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ impl Block {

impl Virtio for Block {
type Config = BlockConfig;
type Feature = BlockFeature;

fn reset(&mut self, _registry: &Registry) {}

Expand Down
13 changes: 12 additions & 1 deletion alioth/src/virtio/dev/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::sync::Arc;
use std::thread::JoinHandle;

use bitfield::bitfield;
use bitflags::Flags;
use mio::event::Event;
use mio::unix::SourceFd;
use mio::{Events, Interest, Poll, Registry, Token, Waker};
Expand All @@ -39,6 +40,7 @@ pub mod net;

pub trait Virtio: Debug + Send + Sync + 'static {
type Config: Mmio;
type Feature: Flags<Bits = u64> + Debug;

fn num_queues(&self) -> u16;
fn reset(&mut self, registry: &Registry);
Expand Down Expand Up @@ -203,6 +205,10 @@ where
log::debug!("worker {}: done", device_worker.name)
}
})?;
log::debug!(
"{name}: created with {:x?}",
D::Feature::from_bits_truncate(reg.device_feature)
);
let virtio_dev = VirtioDevice {
name,
reg,
Expand Down Expand Up @@ -323,7 +329,12 @@ where
let split_queues = self.queue_regs.iter().map(new_queue).collect();
Queues::Split(split_queues)
};
log::debug!("{}: started with feature bit {feature:#b}", self.name);
log::debug!(
"{}: activated with {:x?} {:x?}",
self.name,
VirtioFeature::from_bits_retain(feature & !D::Feature::all().bits()),
D::Feature::from_bits_truncate(feature)
);
self.handle_wake_events(&irq_sender)?;
let mut events = Events::with_capacity(128);
loop {
Expand Down
7 changes: 7 additions & 0 deletions alioth/src/virtio/dev/entropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::mem::size_of;
use std::os::unix::prelude::OpenOptionsExt;
use std::sync::Arc;

use bitflags::bitflags;
use libc::O_NONBLOCK;
use mio::event::Event;
use mio::Registry;
Expand Down Expand Up @@ -47,6 +48,11 @@ impl Mmio for EntropyConfig {
}
}

bitflags! {
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EntropyFeature: u64 { }
}

#[derive(Debug)]
pub struct Entropy {
name: Arc<String>,
Expand All @@ -69,6 +75,7 @@ impl Entropy {

impl Virtio for Entropy {
type Config = EntropyConfig;
type Feature = EntropyFeature;

fn num_queues(&self) -> u16 {
1
Expand Down
3 changes: 1 addition & 2 deletions alioth/src/virtio/dev/net/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ impl Net {
| NetFeature::HOST_UFO
| NetFeature::HOST_USO
| detect_tap_offload(&file);
log::debug!("{name}: device fature: {dev_feat:x?}");
setup_tap(&mut file, param.if_name.as_deref())?;
let net = Net {
name,
Expand All @@ -165,6 +164,7 @@ impl Net {

impl Virtio for Net {
type Config = NetConfig;
type Feature = NetFeature;

fn num_queues(&self) -> u16 {
let data_queues = self.config.max_queue_pairs << 1;
Expand Down Expand Up @@ -193,7 +193,6 @@ impl Virtio for Net {

fn activate(&mut self, registry: &Registry, feature: u64, _memory: &RamBus) -> Result<()> {
let feature = NetFeature::from_bits_retain(feature);
log::debug!("{}: driver feature: {:?}", self.name, feature);
enable_tap_offload(&mut self.tap, feature)?;
registry.register(
&mut SourceFd(&self.tap.as_raw_fd()),
Expand Down

0 comments on commit 60fedf3

Please sign in to comment.