Skip to content

Commit 52ec270

Browse files
committed
refactor(virtio): change to consts
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
1 parent 198e25b commit 52ec270

10 files changed

Lines changed: 27 additions & 26 deletions

File tree

alioth/src/virtio/dev/balloon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Virtio for Balloon {
190190
type Feature = BalloonFeature;
191191

192192
fn id(&self) -> DeviceId {
193-
DeviceId::Balloon
193+
DeviceId::BALLOON
194194
}
195195

196196
fn name(&self) -> &str {

alioth/src/virtio/dev/blk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl Virtio for Block {
285285
type Feature = BlockFeature;
286286

287287
fn id(&self) -> DeviceId {
288-
DeviceId::Block
288+
DeviceId::BLOCK
289289
}
290290

291291
fn name(&self) -> &str {

alioth/src/virtio/dev/entropy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl Virtio for Entropy {
8585
type Feature = EntropyFeature;
8686

8787
fn id(&self) -> DeviceId {
88-
DeviceId::Entropy
88+
DeviceId::ENTROPY
8989
}
9090

9191
fn name(&self) -> &str {

alioth/src/virtio/dev/entropy_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn entropy_test(fixture_ram_bus: RamBus, fixture_queues: Box<[QueueReg]>) {
7272
};
7373
let dev = param.build("entropy").unwrap();
7474

75-
assert_matches!(dev.id(), DeviceId::Entropy);
75+
assert_matches!(dev.id(), DeviceId::ENTROPY);
7676
assert_eq!(dev.name(), "entropy");
7777
assert_eq!(dev.num_queues(), 1);
7878
assert_matches!(*dev.config(), EntropyConfig);

alioth/src/virtio/dev/fs/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ where
399399
type Feature = FsFeature;
400400

401401
fn id(&self) -> DeviceId {
402-
DeviceId::FileSystem
402+
DeviceId::FILE_SYSTEM
403403
}
404404

405405
fn name(&self) -> &str {

alioth/src/virtio/dev/net/vmnet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl Virtio for Net {
236236
type Feature = NetFeature;
237237

238238
fn id(&self) -> DeviceId {
239-
DeviceId::Net
239+
DeviceId::NET
240240
}
241241

242242
fn name(&self) -> &str {

alioth/src/virtio/dev/vsock/uds_vsock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ impl Virtio for UdsVsock {
695695
type Feature = VsockFeature;
696696

697697
fn id(&self) -> DeviceId {
698-
DeviceId::Socket
698+
DeviceId::SOCKET
699699
}
700700

701701
fn name(&self) -> &str {

alioth/src/virtio/dev/vsock/uds_vsock_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn vsock_conn_test(fixture_ram_bus: RamBus, #[with(3)] fixture_queues: Box<[Queu
124124
};
125125
let dev = param.build("vsock").unwrap();
126126

127-
assert_matches!(dev.id(), DeviceId::Socket);
127+
assert_matches!(dev.id(), DeviceId::SOCKET);
128128
assert_eq!(dev.name(), "vsock");
129129
assert_eq!(dev.num_queues(), 3);
130130
assert_eq!(dev.config().guest_cid, GUEST_CID as u32);

alioth/src/virtio/pci.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,10 @@ const VIRTIO_DEVICE_ID_BASE: u16 = 0x1040;
576576

577577
fn get_class(id: DeviceId) -> (u8, u8) {
578578
match id {
579-
DeviceId::Net => (0x02, 0x00),
580-
DeviceId::FileSystem => (0x01, 0x80),
581-
DeviceId::Block => (0x01, 0x00),
582-
DeviceId::Socket => (0x02, 0x80),
579+
DeviceId::NET => (0x02, 0x00),
580+
DeviceId::FILE_SYSTEM => (0x01, 0x80),
581+
DeviceId::BLOCK => (0x01, 0x00),
582+
DeviceId::SOCKET => (0x02, 0x80),
583583
_ => (0xff, 0x00),
584584
}
585585
}
@@ -690,14 +690,14 @@ where
690690
let mut header = DeviceHeader {
691691
common: CommonHeader {
692692
vendor: VIRTIO_VENDOR_ID,
693-
device: VIRTIO_DEVICE_ID_BASE + dev.id as u16,
693+
device: VIRTIO_DEVICE_ID_BASE + dev.id.raw(),
694694
revision: 0x1,
695695
header_type: HeaderType::DEVICE,
696696
class,
697697
subclass,
698698
..Default::default()
699699
},
700-
subsystem: VIRTIO_DEVICE_ID_BASE + dev.id as u16,
700+
subsystem: VIRTIO_DEVICE_ID_BASE + dev.id.raw(),
701701
..Default::default()
702702
};
703703
let device_config = dev.device_config.clone();

alioth/src/virtio/virtio.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use std::path::Path;
3131

3232
use snafu::Snafu;
3333

34-
use crate::bitflags;
3534
use crate::errors::{DebugTrace, trace_error};
35+
use crate::{bitflags, consts};
3636

3737
#[trace_error]
3838
#[derive(Snafu, DebugTrace)]
@@ -99,17 +99,18 @@ const FEATURE_BUILT_IN: u128 = VirtioFeature::EVENT_IDX.bits()
9999
| VirtioFeature::RING_PACKED.bits()
100100
| VirtioFeature::VERSION_1.bits();
101101

102-
#[derive(Debug, Clone, Copy)]
103-
pub enum DeviceId {
104-
Net = 1,
105-
Block = 2,
106-
Entropy = 4,
107-
Balloon = 5,
108-
Socket = 19,
109-
Iommu = 23,
110-
Mem = 24,
111-
FileSystem = 26,
112-
Pmem = 27,
102+
consts! {
103+
pub struct DeviceId(u16) {
104+
NET = 1;
105+
BLOCK = 2;
106+
ENTROPY = 4;
107+
BALLOON = 5;
108+
SOCKET = 19;
109+
IOMMU = 23;
110+
MEM = 24;
111+
FILE_SYSTEM = 26;
112+
PMEM = 27;
113+
}
113114
}
114115

115116
bitflags! {

0 commit comments

Comments
 (0)