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

@@ -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,