We use kvm as hypervisor, as the newer qemu-kvm and kernel support ptp_kvm feature. System real time of VM is synced to host real time with chronyd, the time error between VM and host is about 20ns after synchronization. On host, system real time is adjusted to NIC PF's PHC time by phy2sys/chronyd, and the PHC time is set by ptp4l to get a correct time from PTP grandmaster.
(CentOS 8 recommened)
sudo grubby --update-kernel=ALL --args="intel_iommu=on iommu=pt"
sudo reboot
sudo yum groupinstall "Virtualization Host"
sudo yum install virt-manager
please refer to the driver update section in vf guide.
echo <num> > /sys/class/net/<interface>/device/sriov_numvfs
- open virt-manager with GUI
- add a vm, choose ubuntu 20.04 minimal iso
- use bridged network or NAT for default NIC
- specify cpu core, memory, recommed 8 cpus and 8G memory (experimental)
- configure before install, add a pci paththrough device, choose the created vf
- start to install vm as normal ubuntu
- install linuxptp:
sudo yum install linuxptp
- configure ptp4l daemon
a. you can use any PF port with PHC support to sync time
b.ethtool -T ens801f2
check "PTP Hardware Clock: " is not 0
c. (optional) manully run ptp4lsudo ptp4l -i ens801f2 -m -s -H
- configure phc2sys daemon
a. (optional) mannully run phc2syssudo phc2sys -s ens801f2 -m -w
virsh --connect qemu:///system
edit vm0
- add iommu device to devices
<devices> <iommu model='intel'> <driver intremap='on' caching_mode='on' iotlb='on'/> </iommu> ... </devices>
- add this to features
<features> <ioapic driver='qemu'/> ... </features>
(Ubuntu 20.04 )
- enable ptp-kvm kernel module, reboot vm
echo ptp_kvm > /etc/modules-load.d/ptp_kvm.conf
- change chronyd config to use PHC0:
edit /etc/chrony/chrony.conf, delete "pool …"
add a line "refclock PHC /dev/ptp0 poll 2" - restart chronyd
systemctl restart chronyd
- check the time sync status, error should be tens of nanosenonds when ready
$ chronyc sources 210 Number of sources = 1 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== #* PHC0 0 2 377 5 -1ns[ -2ns] +/- 27ns
redhat - ptp_kvm
linuxptp
tsn - ptp4l,phc2sys
libvirt - IOMMU