Skip to content

Commit e17dc26

Browse files
committed
refactor: Introduce VIRTIO_DEFAULT_FEATURES
This constant contains all the virtio features that all devices offer by default, so that we can avoid repeating them for each device. For now, its just VIRTIO_F_VERSION_1. Signed-off-by: Patrick Roy <[email protected]>
1 parent 8aae298 commit e17dc26

File tree

7 files changed

+24
-24
lines changed

7 files changed

+24
-24
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use super::{
2323
VIRTIO_BALLOON_S_MEMTOT, VIRTIO_BALLOON_S_MINFLT, VIRTIO_BALLOON_S_SWAP_IN,
2424
VIRTIO_BALLOON_S_SWAP_OUT,
2525
};
26+
use crate::devices::virtio::VIRTIO_DEFAULT_FEATURES;
2627
use crate::devices::virtio::balloon::BalloonError;
2728
use crate::devices::virtio::device::{IrqTrigger, IrqType};
28-
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
2929
use crate::logger::IncMetric;
3030
use crate::utils::u64_to_usize;
3131
use crate::vstate::memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemoryMmap};
@@ -206,7 +206,7 @@ impl Balloon {
206206
stats_polling_interval_s: u16,
207207
restored_from_file: bool,
208208
) -> Result<Balloon, BalloonError> {
209-
let mut avail_features = 1u64 << VIRTIO_F_VERSION_1;
209+
let mut avail_features = VIRTIO_DEFAULT_FEATURES;
210210

211211
if deflate_on_oom {
212212
avail_features |= 1u64 << VIRTIO_BALLOON_F_DEFLATE_ON_OOM;
@@ -727,7 +727,7 @@ pub(crate) mod tests {
727727
let mut balloon = Balloon::new(0, *deflate_on_oom, *stats_interval, false).unwrap();
728728
assert_eq!(balloon.device_type(), TYPE_BALLOON);
729729

730-
let features: u64 = (1u64 << VIRTIO_F_VERSION_1)
730+
let features: u64 = VIRTIO_DEFAULT_FEATURES
731731
| (u64::from(*deflate_on_oom) << VIRTIO_BALLOON_F_DEFLATE_ON_OOM)
732732
| ((u64::from(*stats_interval)) << VIRTIO_BALLOON_F_STATS_VQ);
733733

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ use super::{NUM_QUEUES, QUEUE_SIZE, VhostUserBlockError};
1616
use crate::devices::virtio::block::CacheType;
1717
use crate::devices::virtio::device::{DeviceState, IrqTrigger, IrqType, VirtioDevice};
1818
use crate::devices::virtio::generated::virtio_blk::{VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_RO};
19-
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
2019
use crate::devices::virtio::generated::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
2120
use crate::devices::virtio::queue::Queue;
2221
use crate::devices::virtio::vhost_user::{VhostUserHandleBackend, VhostUserHandleImpl};
2322
use crate::devices::virtio::vhost_user_metrics::{
2423
VhostUserDeviceMetrics, VhostUserMetricsPerDevice,
2524
};
26-
use crate::devices::virtio::{ActivateError, TYPE_BLOCK};
25+
use crate::devices::virtio::{ActivateError, TYPE_BLOCK, VIRTIO_DEFAULT_FEATURES};
2726
use crate::logger::{IncMetric, StoreMetric, log_dev_preview_warning};
2827
use crate::utils::u64_to_usize;
2928
use crate::vmm_config::drive::BlockDeviceConfig;
@@ -32,7 +31,7 @@ use crate::vstate::memory::GuestMemoryMmap;
3231
/// Block device config space size in bytes.
3332
const BLOCK_CONFIG_SPACE_SIZE: u32 = 60;
3433

35-
const AVAILABLE_FEATURES: u64 = (1 << VIRTIO_F_VERSION_1)
34+
const AVAILABLE_FEATURES: u64 = VIRTIO_DEFAULT_FEATURES
3635
| (1 << VIRTIO_RING_F_EVENT_IDX)
3736
// vhost-user specific bit. Not defined in standart virtio spec.
3837
// Specifies ability of frontend to negotiate protocol features.

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ use crate::devices::virtio::device::{DeviceState, IrqTrigger, IrqType, VirtioDev
2727
use crate::devices::virtio::generated::virtio_blk::{
2828
VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_RO, VIRTIO_BLK_ID_BYTES,
2929
};
30-
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
3130
use crate::devices::virtio::generated::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
3231
use crate::devices::virtio::queue::Queue;
33-
use crate::devices::virtio::{ActivateError, TYPE_BLOCK};
32+
use crate::devices::virtio::{ActivateError, TYPE_BLOCK, VIRTIO_DEFAULT_FEATURES};
3433
use crate::logger::{IncMetric, error, warn};
3534
use crate::rate_limiter::{BucketUpdate, RateLimiter};
3635
use crate::utils::u64_to_usize;
@@ -295,7 +294,7 @@ impl VirtioBlock {
295294
.map_err(VirtioBlockError::RateLimiter)?
296295
.unwrap_or_default();
297296

298-
let mut avail_features = (1u64 << VIRTIO_F_VERSION_1) | (1u64 << VIRTIO_RING_F_EVENT_IDX);
297+
let mut avail_features = VIRTIO_DEFAULT_FEATURES | (1u64 << VIRTIO_RING_F_EVENT_IDX);
299298

300299
if config.cache_type == CacheType::Writeback {
301300
avail_features |= 1u64 << VIRTIO_BLK_F_FLUSH;
@@ -769,7 +768,7 @@ mod tests {
769768

770769
assert_eq!(block.device_type(), TYPE_BLOCK);
771770

772-
let features: u64 = (1u64 << VIRTIO_F_VERSION_1) | (1u64 << VIRTIO_RING_F_EVENT_IDX);
771+
let features: u64 = VIRTIO_DEFAULT_FEATURES | (1u64 << VIRTIO_RING_F_EVENT_IDX);
773772

774773
assert_eq!(
775774
block.avail_features_by_page(0),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use std::any::Any;
1111

1212
use self::queue::QueueError;
13+
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
1314
use crate::devices::virtio::net::TapError;
1415

1516
pub mod balloon;
@@ -61,6 +62,9 @@ pub const TYPE_BALLOON: u32 = 5;
6162
/// queue events.
6263
pub const NOTIFY_REG_OFFSET: u32 = 0x50;
6364

65+
/// The set of virtio features that every Firecracker device offers.
66+
pub const VIRTIO_DEFAULT_FEATURES: u64 = 1 << VIRTIO_F_VERSION_1;
67+
6468
/// Errors triggered when activating a VirtioDevice.
6569
#[derive(Debug, thiserror::Error, displaydoc::Display)]
6670
pub enum ActivateError {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use vmm_sys_util::eventfd::EventFd;
1616

1717
use super::NET_QUEUE_MAX_SIZE;
1818
use crate::devices::virtio::device::{DeviceState, IrqTrigger, IrqType, VirtioDevice};
19-
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
2019
use crate::devices::virtio::generated::virtio_net::{
2120
VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
2221
VIRTIO_NET_F_GUEST_UFO, VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_TSO6, VIRTIO_NET_F_HOST_UFO,
@@ -32,7 +31,7 @@ use crate::devices::virtio::net::{
3231
MAX_BUFFER_SIZE, NET_QUEUE_SIZES, NetError, NetQueue, RX_INDEX, TX_INDEX, generated,
3332
};
3433
use crate::devices::virtio::queue::{DescriptorChain, Queue};
35-
use crate::devices::virtio::{ActivateError, TYPE_NET};
34+
use crate::devices::virtio::{ActivateError, TYPE_NET, VIRTIO_DEFAULT_FEATURES};
3635
use crate::devices::{DeviceError, report_net_event_fail};
3736
use crate::dumbo::pdu::arp::ETH_IPV4_FRAME_LEN;
3837
use crate::dumbo::pdu::ethernet::{EthernetFrame, PAYLOAD_OFFSET};
@@ -275,15 +274,15 @@ impl Net {
275274
rx_rate_limiter: RateLimiter,
276275
tx_rate_limiter: RateLimiter,
277276
) -> Result<Self, NetError> {
278-
let mut avail_features = (1 << VIRTIO_NET_F_GUEST_CSUM)
277+
let mut avail_features = VIRTIO_DEFAULT_FEATURES
278+
| (1 << VIRTIO_NET_F_GUEST_CSUM)
279279
| (1 << VIRTIO_NET_F_CSUM)
280280
| (1 << VIRTIO_NET_F_GUEST_TSO4)
281281
| (1 << VIRTIO_NET_F_GUEST_TSO6)
282282
| (1 << VIRTIO_NET_F_GUEST_UFO)
283283
| (1 << VIRTIO_NET_F_HOST_TSO4)
284284
| (1 << VIRTIO_NET_F_HOST_TSO6)
285285
| (1 << VIRTIO_NET_F_HOST_UFO)
286-
| (1 << VIRTIO_F_VERSION_1)
287286
| (1 << VIRTIO_NET_F_MRG_RXBUF)
288287
| (1 << VIRTIO_RING_F_EVENT_IDX);
289288

@@ -1116,7 +1115,8 @@ pub mod tests {
11161115
set_mac(&mut net, MacAddr::from_str("11:22:33:44:55:66").unwrap());
11171116

11181117
// Test `features()` and `ack_features()`.
1119-
let features = (1 << VIRTIO_NET_F_GUEST_CSUM)
1118+
let features = VIRTIO_DEFAULT_FEATURES
1119+
| (1 << VIRTIO_NET_F_GUEST_CSUM)
11201120
| (1 << VIRTIO_NET_F_CSUM)
11211121
| (1 << VIRTIO_NET_F_GUEST_TSO4)
11221122
| (1 << VIRTIO_NET_F_GUEST_TSO6)
@@ -1125,7 +1125,6 @@ pub mod tests {
11251125
| (1 << VIRTIO_NET_F_HOST_TSO4)
11261126
| (1 << VIRTIO_NET_F_HOST_TSO6)
11271127
| (1 << VIRTIO_NET_F_HOST_UFO)
1128-
| (1 << VIRTIO_F_VERSION_1)
11291128
| (1 << VIRTIO_NET_F_MRG_RXBUF)
11301129
| (1 << VIRTIO_RING_F_EVENT_IDX);
11311130

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ use super::metrics::METRICS;
1313
use super::{RNG_NUM_QUEUES, RNG_QUEUE};
1414
use crate::devices::DeviceError;
1515
use crate::devices::virtio::device::{DeviceState, IrqTrigger, IrqType, VirtioDevice};
16-
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
1716
use crate::devices::virtio::iov_deque::IovDequeError;
1817
use crate::devices::virtio::iovec::IoVecBufferMut;
1918
use crate::devices::virtio::queue::{FIRECRACKER_MAX_QUEUE_SIZE, Queue};
20-
use crate::devices::virtio::{ActivateError, TYPE_RNG};
19+
use crate::devices::virtio::{ActivateError, TYPE_RNG, VIRTIO_DEFAULT_FEATURES};
2120
use crate::logger::{IncMetric, debug, error};
2221
use crate::rate_limiter::{RateLimiter, TokenType};
2322
use crate::vstate::memory::GuestMemoryMmap;
@@ -72,7 +71,7 @@ impl Entropy {
7271
let irq_trigger = IrqTrigger::new()?;
7372

7473
Ok(Self {
75-
avail_features: 1 << VIRTIO_F_VERSION_1,
74+
avail_features: VIRTIO_DEFAULT_FEATURES,
7675
acked_features: 0u64,
7776
activate_event,
7877
device_state: DeviceState::Inactive,
@@ -312,6 +311,7 @@ mod tests {
312311
use super::*;
313312
use crate::check_metric_after_block;
314313
use crate::devices::virtio::device::VirtioDevice;
314+
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
315315
use crate::devices::virtio::queue::VIRTQ_DESC_F_WRITE;
316316
use crate::devices::virtio::test_utils::test::{
317317
VirtioTestDevice, VirtioTestHelper, create_virtio_mem,
@@ -397,7 +397,7 @@ mod tests {
397397
fn test_virtio_device_features() {
398398
let mut entropy_dev = default_entropy();
399399

400-
let features = 1 << VIRTIO_F_VERSION_1;
400+
let features = VIRTIO_DEFAULT_FEATURES;
401401

402402
assert_eq!(
403403
entropy_dev.avail_features_by_page(0),

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ use super::super::super::DeviceError;
2929
use super::defs::uapi;
3030
use super::packet::{VSOCK_PKT_HDR_SIZE, VsockPacketRx, VsockPacketTx};
3131
use super::{VsockBackend, defs};
32-
use crate::devices::virtio::ActivateError;
3332
use crate::devices::virtio::device::{DeviceState, IrqTrigger, IrqType, VirtioDevice};
34-
use crate::devices::virtio::generated::virtio_config::{VIRTIO_F_IN_ORDER, VIRTIO_F_VERSION_1};
33+
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_IN_ORDER;
3534
use crate::devices::virtio::queue::Queue as VirtQueue;
3635
use crate::devices::virtio::vsock::VsockError;
3736
use crate::devices::virtio::vsock::metrics::METRICS;
37+
use crate::devices::virtio::{ActivateError, VIRTIO_DEFAULT_FEATURES};
3838
use crate::logger::IncMetric;
3939
use crate::utils::byte_order;
4040
use crate::vstate::memory::{Bytes, GuestMemoryMmap};
@@ -49,8 +49,7 @@ pub(crate) const VIRTIO_VSOCK_EVENT_TRANSPORT_RESET: u32 = 0;
4949
/// - VIRTIO_F_VERSION_1: the device conforms to at least version 1.0 of the VirtIO spec.
5050
/// - VIRTIO_F_IN_ORDER: the device returns used buffers in the same order that the driver makes
5151
/// them available.
52-
pub(crate) const AVAIL_FEATURES: u64 =
53-
(1 << VIRTIO_F_VERSION_1 as u64) | (1 << VIRTIO_F_IN_ORDER as u64);
52+
pub(crate) const AVAIL_FEATURES: u64 = VIRTIO_DEFAULT_FEATURES | (1 << VIRTIO_F_IN_ORDER as u64);
5453

5554
/// Structure representing the vsock device.
5655
#[derive(Debug)]

0 commit comments

Comments
 (0)