Skip to content

Commit d0885e1

Browse files
committed
refactor: moved CacheType out of Block to share it with vhost_user_blk
CacheType is shared between VirtioBlock and VhostUserBlock. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 8d47fb3 commit d0885e1

File tree

13 files changed

+41
-91
lines changed

13 files changed

+41
-91
lines changed

src/vmm/src/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,14 +990,15 @@ pub mod tests {
990990

991991
use super::*;
992992
use crate::arch::DeviceType;
993+
use crate::devices::virtio::block_common::CacheType;
993994
use crate::devices::virtio::rng::device::ENTROPY_DEV_ID;
994995
use crate::devices::virtio::vsock::{TYPE_VSOCK, VSOCK_DEV_ID};
995996
use crate::devices::virtio::{TYPE_BALLOON, TYPE_BLOCK, TYPE_RNG};
996997
use crate::mmds::data_store::{Mmds, MmdsVersion};
997998
use crate::mmds::ns::MmdsNetworkStack;
998999
use crate::vmm_config::balloon::{BalloonBuilder, BalloonDeviceConfig, BALLOON_DEV_ID};
9991000
use crate::vmm_config::boot_source::DEFAULT_KERNEL_CMDLINE;
1000-
use crate::vmm_config::drive::{BlockBuilder, BlockDeviceConfig, CacheType, FileEngineType};
1001+
use crate::vmm_config::drive::{BlockBuilder, BlockDeviceConfig, FileEngineType};
10011002
use crate::vmm_config::entropy::{EntropyDeviceBuilder, EntropyDeviceConfig};
10021003
use crate::vmm_config::net::{NetBuilder, NetworkInterfaceConfig};
10031004
use crate::vmm_config::vsock::tests::default_config;

src/vmm/src/device_manager/persist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ mod tests {
655655

656656
use super::*;
657657
use crate::builder::tests::*;
658-
use crate::devices::virtio::block::CacheType;
658+
use crate::devices::virtio::block_common::CacheType;
659659
use crate::devices::virtio::net::persist::NetConfigSpaceState;
660660
use crate::resources::VmmConfig;
661661
use crate::vmm_config::balloon::BalloonDeviceConfig;

src/vmm/src/devices/virtio/block/device.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,27 @@ use utils::eventfd::EventFd;
2020
use utils::kernel_version::{min_kernel_version_for_io_uring, KernelVersion};
2121
use utils::u64_to_usize;
2222

23-
use super::super::device::{DeviceState, VirtioDevice};
24-
use super::super::queue::Queue;
25-
use super::super::{ActivateError, TYPE_BLOCK};
2623
use super::io::async_io;
2724
use super::request::*;
2825
use super::{
2926
io as block_io, VirtioBlockError, BLOCK_CONFIG_SPACE_SIZE, BLOCK_QUEUE_SIZES, SECTOR_SHIFT,
3027
SECTOR_SIZE,
3128
};
3229
use crate::devices::virtio::block::block_metrics::{BlockDeviceMetrics, BlockMetricsPerDevice};
33-
use crate::devices::virtio::device::{IrqTrigger, IrqType};
30+
use crate::devices::virtio::block_common::CacheType;
31+
use crate::devices::virtio::device::{DeviceState, IrqTrigger, IrqType, VirtioDevice};
3432
use crate::devices::virtio::gen::virtio_blk::{
3533
VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_RO, VIRTIO_BLK_ID_BYTES, VIRTIO_F_VERSION_1,
3634
};
3735
use crate::devices::virtio::gen::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
36+
use crate::devices::virtio::queue::Queue;
37+
use crate::devices::virtio::{ActivateError, TYPE_BLOCK};
3838
use crate::logger::{error, warn, IncMetric};
3939
use crate::rate_limiter::{BucketUpdate, RateLimiter};
4040
use crate::vmm_config::drive::BlockDeviceConfig;
4141
use crate::vmm_config::RateLimiterConfig;
4242
use crate::vstate::memory::GuestMemoryMmap;
4343

44-
/// Configuration options for disk caching.
45-
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
46-
pub enum CacheType {
47-
/// Flushing mechanic will be advertised to the guest driver, but
48-
/// the operation will be a noop.
49-
#[default]
50-
Unsafe,
51-
/// Flushing mechanic will be advertised to the guest driver and
52-
/// flush requests coming from the guest will be performed using
53-
/// `fsync`.
54-
Writeback,
55-
}
56-
5744
/// The engine file type, either Sync or Async (through io_uring).
5845
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
5946
pub enum FileEngineType {

src/vmm/src/devices/virtio/block/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ pub mod test_utils;
1313

1414
use vm_memory::GuestMemoryError;
1515

16-
pub use self::device::{CacheType, VirtioBlock};
16+
pub use self::device::VirtioBlock;
1717
pub use self::event_handler::*;
1818
pub use self::request::*;
1919
use super::queue::FIRECRACKER_MAX_QUEUE_SIZE;
20+
pub use crate::devices::virtio::block_common::CacheType;
2021

2122
/// Size of config space for block device.
2223
pub const BLOCK_CONFIG_SPACE_SIZE: usize = 8;

src/vmm/src/devices/virtio/block/persist.rs

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,6 @@ use crate::rate_limiter::persist::RateLimiterState;
2424
use crate::rate_limiter::RateLimiter;
2525
use crate::vstate::memory::GuestMemoryMmap;
2626

27-
/// Holds info about block's cache type. Gets saved in snapshot.
28-
// NOTICE: Any changes to this structure require a snapshot version bump.
29-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Versionize)]
30-
pub enum CacheTypeState {
31-
/// Flushing mechanic will be advertised to the guest driver, but
32-
/// the operation will be a noop.
33-
Unsafe,
34-
/// Flushing mechanic will be advertised to the guest driver and
35-
/// flush requests coming from the guest will be performed using
36-
/// `fsync`.
37-
Writeback,
38-
}
39-
40-
impl From<CacheType> for CacheTypeState {
41-
fn from(cache_type: CacheType) -> Self {
42-
match cache_type {
43-
CacheType::Unsafe => CacheTypeState::Unsafe,
44-
CacheType::Writeback => CacheTypeState::Writeback,
45-
}
46-
}
47-
}
48-
49-
impl From<CacheTypeState> for CacheType {
50-
fn from(cache_type_state: CacheTypeState) -> Self {
51-
match cache_type_state {
52-
CacheTypeState::Unsafe => CacheType::Unsafe,
53-
CacheTypeState::Writeback => CacheType::Writeback,
54-
}
55-
}
56-
}
57-
5827
/// Holds info about block's file engine type. Gets saved in snapshot.
5928
// NOTICE: Any changes to this structure require a snapshot version bump.
6029
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Versionize)]
@@ -93,7 +62,7 @@ pub struct VirtioBlockState {
9362
id: String,
9463
partuuid: Option<String>,
9564
#[version(start = 2, default_fn = "default_cache_type_flush")]
96-
cache_type: CacheTypeState,
65+
cache_type: CacheType,
9766
root_device: bool,
9867
disk_path: String,
9968
virtio_state: VirtioDeviceState,
@@ -103,8 +72,8 @@ pub struct VirtioBlockState {
10372
}
10473

10574
impl VirtioBlockState {
106-
fn default_cache_type_flush(_source_version: u16) -> CacheTypeState {
107-
CacheTypeState::Unsafe
75+
fn default_cache_type_flush(_source_version: u16) -> CacheType {
76+
CacheType::Unsafe
10877
}
10978
}
11079

@@ -125,7 +94,7 @@ impl Persist<'_> for VirtioBlock {
12594
VirtioBlockState {
12695
id: self.id.clone(),
12796
partuuid: self.partuuid.clone(),
128-
cache_type: CacheTypeState::from(self.cache_type()),
97+
cache_type: self.cache_type(),
12998
root_device: self.root_device,
13099
disk_path: self.disk.file_path().clone(),
131100
virtio_state: VirtioDeviceState::from_device(self),
@@ -223,36 +192,6 @@ mod tests {
223192
use crate::devices::virtio::device::VirtioDevice;
224193
use crate::devices::virtio::test_utils::default_mem;
225194

226-
#[test]
227-
fn test_cache_type_state_from() {
228-
assert_eq!(
229-
CacheTypeState::Unsafe,
230-
CacheTypeState::from(CacheType::Unsafe)
231-
);
232-
assert_eq!(
233-
CacheTypeState::Writeback,
234-
CacheTypeState::from(CacheType::Writeback)
235-
);
236-
}
237-
238-
#[test]
239-
fn test_cache_type_state_into() {
240-
assert_eq!(CacheType::Unsafe, CacheTypeState::Unsafe.into());
241-
assert_eq!(CacheType::Writeback, CacheTypeState::Writeback.into());
242-
}
243-
244-
#[test]
245-
fn test_default_cache_type_flush() {
246-
assert_eq!(
247-
VirtioBlockState::default_cache_type_flush(2),
248-
CacheTypeState::Unsafe
249-
);
250-
assert_eq!(
251-
VirtioBlockState::default_cache_type_flush(3),
252-
CacheTypeState::Unsafe
253-
);
254-
}
255-
256195
#[test]
257196
fn test_cache_semantic_ser() {
258197
// We create the backing file here so that it exists for the whole lifetime of the test.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use serde::{Deserialize, Serialize};
5+
use versionize::{VersionMap, Versionize, VersionizeError, VersionizeResult};
6+
use versionize_derive::Versionize;
7+
8+
/// Configuration options for disk caching.
9+
// NOTICE: Any changes to this structure require a snapshot version bump.
10+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize, Versionize)]
11+
pub enum CacheType {
12+
/// Flushing mechanic not will be advertised to the guest driver
13+
#[default]
14+
Unsafe,
15+
/// Flushing mechanic will be advertised to the guest driver and
16+
/// flush requests coming from the guest will be performed using
17+
/// `fsync`.
18+
Writeback,
19+
}

src/vmm/src/devices/virtio/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::io::Error as IOError;
1212

1313
pub mod balloon;
1414
pub mod block;
15+
pub mod block_common;
1516
pub mod device;
1617
pub mod gen;
1718
pub mod iovec;

src/vmm/src/devices/virtio/vhost_user_block/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use vhost::vhost_user::message::*;
1616
use vhost::vhost_user::VhostUserFrontend;
1717

1818
use super::{VhostUserBlockError, NUM_QUEUES, QUEUE_SIZE};
19-
use crate::devices::virtio::block::CacheType;
19+
use crate::devices::virtio::block_common::CacheType;
2020
use crate::devices::virtio::device::{DeviceState, IrqTrigger, VirtioDevice};
2121
use crate::devices::virtio::gen::virtio_blk::{
2222
VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_RO, VIRTIO_F_VERSION_1,

src/vmm/src/devices/virtio/vhost_user_block/persist.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use versionize_derive::Versionize;
99

1010
use super::device::VhostUserBlock;
1111
use super::VhostUserBlockError;
12-
use crate::devices::virtio::block::persist::CacheTypeState;
12+
use crate::devices::virtio::block_common::CacheType;
1313
use crate::devices::virtio::persist::VirtioDeviceState;
1414
use crate::vstate::memory::GuestMemoryMmap;
1515

@@ -19,7 +19,7 @@ use crate::vstate::memory::GuestMemoryMmap;
1919
pub struct VhostUserBlockState {
2020
id: String,
2121
partuuid: Option<String>,
22-
cache_type: CacheTypeState,
22+
cache_type: CacheType,
2323
root_device: bool,
2424
socket_path: String,
2525
vu_acked_protocol_features: u64,

src/vmm/src/persist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,8 @@ mod tests {
588588
};
589589
#[cfg(target_arch = "aarch64")]
590590
use crate::construct_kvm_mpidrs;
591+
use crate::devices::virtio::block_common::CacheType;
591592
use crate::vmm_config::balloon::BalloonDeviceConfig;
592-
use crate::vmm_config::drive::CacheType;
593593
use crate::vmm_config::net::NetworkInterfaceConfig;
594594
use crate::vmm_config::vsock::tests::default_config;
595595
use crate::Vmm;

0 commit comments

Comments
 (0)