virtio-devices: Implement virtio-device rtc

This change will allow us to get accurate time over ptp in guests
started from a MSHV-virtualized Linux host. Implementing it as a
virtio device is preferable to using the existing kvm_ptp because:

kvm_ptp relies on hypercalls that only exist on host kernels running
kvm. Virtio-rtc gives us more flexibility in what clock types we want
to provide. We can later extend the device to implement multiple clocks
(smeared UTC, TAI, monotonic, etc.). Virtio-rtc protocol supports
alarms. Alarms may later enable usecases where the guests can do their
own VM lifecycle management without relying on a host-side
orchestrator.

Implement device backend for virtio-rtc. Currently this implementation
encompasses:

1. CONFIG, CAP, READ, CROSSCAP (returns false)
2. One PTP clock is presented of type
VIRTIO_RTC_CLOCK_UTC_MAYBE_SMEARED with leap_second_smearing
VIRTIO_RTC_SMEAR_UNSPECIFIED

The device is disabled by default, requiring --rtc to be passed

Not implemented but theoretically supported by virtio-rtc is:

1. Cross-timestamping support
2. The alarm queue

Fixes #7730

Signed-off-by: Cameron Baird <cameronbaird@microsoft.com>
This commit is contained in:
Cameron Baird
2026-03-03 23:49:38 +00:00
committed by Wei Liu
parent 1e18716fbd
commit b452440f6c
11 changed files with 858 additions and 3 deletions

View File

@@ -667,6 +667,8 @@ components:
watchdog:
type: boolean
default: false
rtc:
$ref: "#/components/schemas/RtcConfig"
pvpanic:
type: boolean
default: false
@@ -1087,6 +1089,21 @@ components:
src:
type: string
RtcConfig:
type: object
properties:
id:
type: string
pci_segment:
type: integer
format: int16
pci_device_id:
type: integer
format: uint8
iommu:
type: boolean
default: false
BalloonConfig:
required:
- size

View File

@@ -116,6 +116,9 @@ pub enum Error {
/// Error parsing RNG options
#[error("Error parsing --rng")]
ParseRng(#[source] OptionParserError),
/// Error parsing RTC options
#[error("Error parsing --rtc")]
ParseRtc(#[source] OptionParserError),
/// Error parsing balloon options
#[error("Error parsing --balloon")]
ParseBalloon(#[source] OptionParserError),
@@ -474,6 +477,7 @@ pub struct VmParams<'a> {
pub pvpanic: bool,
pub numa: Option<Vec<&'a str>>,
pub watchdog: bool,
pub rtc: Option<&'a str>,
#[cfg(feature = "guest_debug")]
pub gdb: bool,
pub pci_segments: Option<Vec<&'a str>>,
@@ -544,6 +548,7 @@ impl<'a> VmParams<'a> {
.get_many::<String>("numa")
.map(|x| x.map(|y| y as &str).collect());
let watchdog = args.get_flag("watchdog");
let rtc: Option<&str> = args.get_one::<String>("rtc").map(|x| x as &str);
let pci_segments: Option<Vec<&str>> = args
.get_many::<String>("pci-segment")
.map(|x| x.map(|y| y as &str).collect());
@@ -593,6 +598,7 @@ impl<'a> VmParams<'a> {
pvpanic,
numa,
watchdog,
rtc,
#[cfg(feature = "guest_debug")]
gdb,
pci_segments,
@@ -1780,6 +1786,28 @@ impl RngConfig {
}
}
impl RtcConfig {
pub const SYNTAX: &'static str = "Virtio RTC parameters \"\
iommu=on|off,id=<device_id>,\
pci_segment=<segment_id>,pci_device_id=<pci_slot>\". \
Passing --rtc with no arguments enables the device with default \
settings.";
pub fn parse(rtc: &str) -> Result<Self> {
let mut parser = OptionParser::new();
parser.add_all(PciDeviceCommonConfig::OPTIONS_IOMMU);
parser.parse(rtc).map_err(Error::ParseRtc)?;
let pci_common = PciDeviceCommonConfig::parse(rtc)?;
Ok(RtcConfig { pci_common })
}
pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> {
self.pci_common.validate(vm_config)
}
}
impl BalloonConfig {
pub const SYNTAX: &'static str = "Balloon parameters \"size=<balloon_size>,deflate_on_oom=on|off,\
free_page_reporting=on|off\"";
@@ -3054,6 +3082,12 @@ impl VmConfig {
Self::validate_identifier(&mut id_list, &self.rng.pci_common.id)?;
self.iommu |= self.rng.pci_common.iommu;
if let Some(rtc) = &self.rtc {
rtc.validate(self)?;
Self::validate_identifier(&mut id_list, &rtc.pci_common.id)?;
self.iommu |= rtc.pci_common.iommu;
}
self.console.validate(self)?;
Self::validate_identifier(&mut id_list, &self.console.pci_common.id)?;
self.iommu |= self.console.pci_common.iommu;
@@ -3290,6 +3324,11 @@ impl VmConfig {
let rng = RngConfig::parse(vm_params.rng)?;
let mut rtc: Option<RtcConfig> = None;
if let Some(rtc_params) = &vm_params.rtc {
rtc = Some(RtcConfig::parse(rtc_params)?);
}
let mut balloon: Option<BalloonConfig> = None;
if let Some(balloon_params) = &vm_params.balloon {
balloon = Some(BalloonConfig::parse(balloon_params)?);
@@ -3471,6 +3510,7 @@ impl VmConfig {
iommu: false, // updated in VmConfig::validate()
numa,
watchdog: vm_params.watchdog,
rtc,
#[cfg(feature = "guest_debug")]
gdb,
pci_segments,
@@ -3591,6 +3631,7 @@ impl Clone for VmConfig {
disks: self.disks.clone(),
net: self.net.clone(),
rng: self.rng.clone(),
rtc: self.rtc.clone(),
balloon: self.balloon.clone(),
#[cfg(feature = "pvmemcontrol")]
pvmemcontrol: self.pvmemcontrol.clone(),
@@ -4825,6 +4866,7 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
iommu: false,
numa: None,
watchdog: false,
rtc: None,
#[cfg(feature = "guest_debug")]
gdb: false,
pci_segments: None,
@@ -5072,6 +5114,7 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
iommu: false,
numa: None,
watchdog: false,
rtc: None,
#[cfg(feature = "guest_debug")]
gdb: false,
pci_segments: None,

View File

@@ -139,6 +139,7 @@ const DEBUGCON_DEVICE_NAME: &str = "__debug_console";
#[cfg(target_arch = "aarch64")]
const GPIO_DEVICE_NAME: &str = "__gpio";
const RNG_DEVICE_NAME: &str = "__rng";
const RTC_DEVICE_NAME: &str = "__rtc";
const IOMMU_DEVICE_NAME: &str = "__iommu";
#[cfg(feature = "pvmemcontrol")]
const PVMEMCONTROL_DEVICE_NAME: &str = "__pvmemcontrol";
@@ -193,6 +194,10 @@ pub enum DeviceManagerError {
#[error("Cannot create virtio-rng device")]
CreateVirtioRng(#[source] io::Error),
/// Cannot create virtio-rtc device
#[error("Cannot create virtio-rtc device")]
CreateVirtioRtc(#[source] io::Error),
/// Cannot create generic vhost-user device
#[error("Cannot create generic vhost-user device")]
CreateGenericVhostUser(#[source] virtio_devices::vhost_user::Error),
@@ -2654,6 +2659,9 @@ impl DeviceManager {
// Add vDPA devices if required
self.make_vdpa_devices(snapshot)?;
// Add virtio-rtc device
self.make_virtio_rtc_devices(snapshot)?;
Ok(())
}
/// Creates a [`MetaVirtioDevice`] from the provided [`DiskConfig`].
@@ -3116,6 +3124,53 @@ impl DeviceManager {
Ok(())
}
fn make_virtio_rtc_devices(&mut self, snapshot: Option<&Snapshot>) -> DeviceManagerResult<()> {
let Some(mut rtc_config) = self.config.lock().unwrap().rtc.clone() else {
return Ok(());
};
info!("Creating virtio-rtc device: {rtc_config:?}");
let id = match rtc_config.pci_common.id.as_ref() {
Some(id) => id.clone(),
None => rtc_config
.pci_common
.id
.insert(RTC_DEVICE_NAME.to_string())
.clone(),
};
let virtio_rtc_device = Arc::new(Mutex::new(
virtio_devices::Rtc::new(
id.clone(),
self.force_access_platform | rtc_config.pci_common.iommu,
self.seccomp_action.clone(),
self.exit_evt
.try_clone()
.map_err(DeviceManagerError::EventFd)?,
state_from_id(snapshot, id.as_str())
.map_err(DeviceManagerError::RestoreGetState)?,
)
.map_err(DeviceManagerError::CreateVirtioRtc)?,
));
self.virtio_devices.push(MetaVirtioDevice {
virtio_device: Arc::clone(&virtio_rtc_device)
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
pci_common: rtc_config.pci_common.clone(),
dma_handler: None,
});
// Fill the device tree with a new node. In case of restore, we
// know there is nothing to do, so we can simply override the
// existing entry.
self.device_tree
.lock()
.unwrap()
.insert(id.clone(), device_node!(id, virtio_rtc_device));
Ok(())
}
fn make_generic_vhost_user_device(
&mut self,
generic_vhost_user_cfg: &mut GenericVhostUserConfig,

View File

@@ -2755,6 +2755,7 @@ mod unit_tests {
iommu: false,
numa: None,
watchdog: false,
rtc: None,
#[cfg(feature = "guest_debug")]
gdb: false,
pci_segments: None,

View File

@@ -451,6 +451,12 @@ impl Default for RngConfig {
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct RtcConfig {
#[serde(flatten)]
pub pci_common: PciDeviceCommonConfig,
}
impl ApplyLandlock for RngConfig {
fn apply_landlock(&self, landlock: &mut Landlock) -> LandlockResult<()> {
// Rng Path only need read access
@@ -1051,6 +1057,8 @@ pub struct VmConfig {
pub numa: Option<Box<[NumaConfig]>>,
#[serde(default)]
pub watchdog: bool,
#[serde(default)]
pub rtc: Option<RtcConfig>,
#[cfg(feature = "guest_debug")]
#[serde(default)]
pub gdb: bool,