Skip to content

virtio-devices: Implement a backend for virtio-rtc#7795

Open
Camelron wants to merge 3 commits intocloud-hypervisor:mainfrom
Camelron:cameronbaird/virtio-rtc
Open

virtio-devices: Implement a backend for virtio-rtc#7795
Camelron wants to merge 3 commits intocloud-hypervisor:mainfrom
Camelron:cameronbaird/virtio-rtc

Conversation

@Camelron
Copy link
Copy Markdown

@Camelron Camelron commented Mar 6, 2026

Motivation

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.

Change

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_SMEARED

KVM guests can already use kvm_ptp which is synchronous
and therefore, more accurate.

Not implemented but theoretically supported by virtio-rtc is:

  1. Cross-timestamping support
  2. The alarm queue

Fixes #7730

@Camelron Camelron requested a review from a team as a code owner March 6, 2026 00:26
Comment thread cloud-hypervisor/tests/integration.rs Outdated
Comment thread vmm/src/device_manager.rs Outdated
Comment thread vmm/src/device_manager.rs Outdated
Comment thread vmm/src/device_manager.rs Outdated
Comment thread virtio-devices/src/rtc.rs Outdated
@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch from 20490ff to 8071262 Compare March 6, 2026 01:08
@Camelron
Copy link
Copy Markdown
Author

Camelron commented Mar 6, 2026

Since it is probably relevant, I tested the case where we have a kvm-based guest with a kernel with both VIRTIO_PTP and CONFIG_PTP_1588_CLOCK_KVM.

Here we see that they are both available.

root [ / ]# cat /sys/class/ptp/ptp
ptp0/ ptp1/ 
root [ / ]# cat /sys/class/ptp/ptp0/clock_name 
Virtio PTP type 3/variant 0
root [ / ]# cat /sys/class/ptp/ptp1/clock_name 
KVM virtual PTP

The consequence of this is that guest images may need udev rules to distinguish between them e.g.

# /etc/udev/rules.d/99-ptp.rules
SUBSYSTEM=="ptp", ATTR{clock_name}=="Virtio PTP type 3/variant 0", SYMLINK+="ptp_virtio"
SUBSYSTEM=="ptp", ATTR{clock_name}=="KVM virtual PTP", SYMLINK+="ptp_kvm"

@DemiMarie
Copy link
Copy Markdown
Contributor

Does MSHV already have a paravirtualized timekeeping device?

Comment thread virtio-devices/src/rtc.rs Outdated
Comment thread virtio-devices/src/rtc.rs Outdated
Comment thread virtio-devices/src/rtc.rs
Comment thread virtio-devices/src/rtc.rs Outdated
Comment thread cloud-hypervisor/tests/integration.rs
@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch from 8071262 to d9e65fd Compare March 6, 2026 20:25
@Camelron
Copy link
Copy Markdown
Author

Camelron commented Mar 6, 2026

Does MSHV already have a paravirtualized timekeeping device?

We do--hyperv_clocksource_tsc_page. However hyperv_clocksource_tsc_page is a host-controlled clock, not a guest PHC device which is steerable in the guest by something like chronyd.

Comment thread cloud-hypervisor/tests/integration.rs Outdated
Comment thread virtio-devices/src/rtc.rs
Comment thread virtio-devices/src/rtc.rs
Comment thread virtio-devices/src/rtc.rs
Comment thread virtio-devices/src/rtc.rs
@phip1611
Copy link
Copy Markdown
Member

phip1611 commented Mar 9, 2026

Your PR and commit messages are missing a # Motivation section. A single descriptive sentence would be enough (who/which component would use that and why is it better than the status quo?). Could you please add one?

@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch 2 times, most recently from d6f64e8 to 76738c2 Compare March 10, 2026 20:25
Comment thread cloud-hypervisor/tests/integration.rs Outdated
Comment thread virtio-devices/src/rtc.rs
@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch 2 times, most recently from 2ed954a to 12f98a9 Compare March 11, 2026 20:48
Comment thread cloud-hypervisor/tests/integration.rs
@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch from 12f98a9 to ac105ed Compare March 23, 2026 16:33
@liuw
Copy link
Copy Markdown
Member

liuw commented Mar 23, 2026

@Camelron there seems to be some recent changes in the main branch. You need to rebase to the latest and build it locally before pushing again.

See commit f77c6ef.

@phip1611
Copy link
Copy Markdown
Member

ping @Camelron please fix your commit message and the CI

@rbradford rbradford marked this pull request as draft March 27, 2026 15:06
@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch from e63a78b to ee0be56 Compare March 27, 2026 22:47
@Camelron
Copy link
Copy Markdown
Author

Looking at the Tests (arm64) failure, it appears to be failing to build the devices crate due to a missing cached dependency? Hopefully transient?

Looking at the x86 failures, I see now that the tests depend on the ordering of PCI devices added to the guest. Pushing a fix to make virtio_rtc the last PCI device added (bdf 09..) since it was added most recently. Also incrementing the expected /sys/bus/pci/devices count to 9.

The virtio_rtc test appears to be failing despite recently merging the change to enable the virtio-rtc driver in the guest kernel: cloud-hypervisor/linux#26 . @weltling @phip1611 is there a way to verify in the CI what kernel version we use for the guest? I see that we are using 6.16.9+ which is the branch I merged the config change into..

@Camelron Camelron marked this pull request as ready for review March 27, 2026 22:48
@weltling
Copy link
Copy Markdown
Member

kernel: cloud-hypervisor/linux#26 . @weltling @phip1611 is there a way to verify in the CI what kernel version we use for the guest? I see that we are using 6.16.9+ which is the branch I merged the config change into..

As far as it shows, there's no new kernel release yet, so guess it's not updated on CI yet.

For the other tests - those seem irrelevant. Though, you could just retrigger the PR or check as a PR against your own fork, if it tells the diff. But most likely not necessary.

Thanks

@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch from 5ccd181 to cdb3bc2 Compare March 31, 2026 20:11
@phip1611
Copy link
Copy Markdown
Member

phip1611 commented Apr 1, 2026

We are now waiting for a release 03/24 or later here cloud-hypervisor/linux/releases to verify that the new test passes once the driver is available in the guest.

Is someone actively working on that? What are the next steps to proceed here? Sorry, I don't have the full context of this right now

@liuw
Copy link
Copy Markdown
Member

liuw commented Apr 2, 2026

There is a pending release for the kernel by github-actions. @rbradford @likebreath can we publish that?

@likebreath
Copy link
Copy Markdown
Member

There is a pending release for the kernel by github-actions. @rbradford @likebreath can we publish that?

Done! Here it is: https://github.com/cloud-hypervisor/linux/releases/tag/ch-release-v6.16.9-20260324

@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch from a7583a6 to 2853baa Compare April 3, 2026 18:09
@liuw liuw force-pushed the cameronbaird/virtio-rtc branch from 2853baa to f24f8c0 Compare April 4, 2026 05:15
@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch 2 times, most recently from 83f2c6d to 244e9d8 Compare April 8, 2026 17:27
@Camelron
Copy link
Copy Markdown
Author

Camelron commented Apr 9, 2026

@rbradford all changes in the CI are now accounted for and we are passing. Mind taking a final look?

Thank you.

Copy link
Copy Markdown
Member

@rbradford rbradford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should always be included as PCI slots are valuable resource on segment 0, especially on KVM where there is already kvmclock and this doesn't add any functionality.

Please make it conditional like watchdog - I would be happy for it to be a different default depending on the running hypervisor.

assert!(
exec_host_command_status(&format!(
"sudo ip link add link {phy_net} name {host_macvtap_name} type macvtap mod bridge"
"sudo ip link add link {phy_net} name {host_macvtap_name} type macvtap mode bridge"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this worked before but it just nicer aesthetically? It's a good change but it deserves its own commit.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did hit a failure due to this "mod bridge" hence the change. I don't see how though with the CI already persisting long before I came around. Will pull it out for now and see if things pass as-is.

Comment thread vm-virtio/src/lib.rs Outdated
VirtioDeviceType::Vsock => "vsock",
VirtioDeviceType::Iommu => "iommu",
VirtioDeviceType::Mem => "mem",
VirtioDeviceType::Rtc => "rtc",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should be in the same order as the numerical order (i.e. same order as From<..> implementation,

@phip1611
Copy link
Copy Markdown
Member

phip1611 commented Apr 13, 2026

I don't think this should always be included as PCI slots are valuable resource on segment 0, especially on KVM where there is already kvmclock and this doesn't add any functionality.

Yup, please no more implicit default devices. This makes BDF handling in management software (e.g., libvirt) more complicated.

@rbradford
Copy link
Copy Markdown
Member

@Camelron Ping?

@Camelron
Copy link
Copy Markdown
Author

Sorry, I've been heads down on some other tasks. Will address requested changes today.

@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch 3 times, most recently from f711a6f to 6b4ad2d Compare April 17, 2026 18:08
Comment thread vm-virtio/src/lib.rs
VirtioDeviceType::Rng => "rng",
VirtioDeviceType::Balloon => "balloon",
VirtioDeviceType::Gpu => "gpu",
VirtioDeviceType::Fs9P => "9p",
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9p was in the wrong order too so I moved it. Can be separated out if preferred.

Move to the latest release for cloud-hypervisor/linux.

Contains configs needed for virtio-rtc

Signed-off-by: Cameron Baird <cameronbaird@microsoft.com>
@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch from 6b4ad2d to 9cb7ded Compare April 17, 2026 18:45
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_SMEARED

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

Not implemented but theoretically supported by virtio-rtc is:

1. Cross-timestamping support
2. The alarm queue

Fixes cloud-hypervisor#7730

Signed-off-by: Cameron Baird <cameronbaird@microsoft.com>
Integration test for virtio-devices/src/rtc.rs. Requires that
the test kernel has the following configs:

CONFIG_PTP_1588_CLOCK=y
CONFIG_VIRTIO_RTC=y
CONFIG_VIRTIO_RTC_PTP=y

Signed-off-by: Cameron Baird <cameronbaird@microsoft.com>
@Camelron Camelron force-pushed the cameronbaird/virtio-rtc branch from 9cb7ded to b7f20c1 Compare April 17, 2026 19:11
@Camelron
Copy link
Copy Markdown
Author

@rbradford all comments should now be addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement a backend for virtio-rtc

7 participants