Subject: cpu: Have host-copy use domcaps if qemu is new enough (bz 1637767)
From: Cole Robinson crobinso@redhat.com Sat Oct 13 17:47:31 2018 -0400
Date: Sat Oct 13 17:48:25 2018 -0400:
Git: b051b6c95d8adca7f7ec6a8d582e62d6427d28e3

capabilities is known problematic. Use domcaps if libvirt+qemu
reports working host-model

https://bugzilla.redhat.com/show_bug.cgi?id=1637767

Index: virt-manager-1.5.1/tests/cli-test-xml/compare/virt-install-kvm-hostcopy-fallback.xml
===================================================================
--- /dev/null
+++ virt-manager-1.5.1/tests/cli-test-xml/compare/virt-install-kvm-hostcopy-fallback.xml
@@ -0,0 +1,161 @@
+<domain type="kvm">
+  <name>foobar</name>
+  <uuid>00000000-1111-2222-3333-444444444444</uuid>
+  <memory>65536</memory>
+  <currentMemory>65536</currentMemory>
+  <vcpu>1</vcpu>
+  <os>
+    <type arch="x86_64" machine="pc">hvm</type>
+    <boot dev="network"/>
+  </os>
+  <features>
+    <acpi/>
+    <apic/>
+    <vmport state="off"/>
+  </features>
+  <cpu mode="custom" match="exact">
+    <model>Opteron_G4</model>
+    <vendor>AMD</vendor>
+    <feature name="invtsc"/>
+    <feature name="perfctr_nb"/>
+    <feature name="perfctr_core"/>
+    <feature name="topoext"/>
+    <feature name="nodeid_msr"/>
+    <feature name="lwp"/>
+    <feature name="wdt"/>
+    <feature name="skinit"/>
+    <feature name="ibs"/>
+    <feature name="osvw"/>
+    <feature name="cr8legacy"/>
+    <feature name="extapic"/>
+    <feature name="cmp_legacy"/>
+    <feature name="fxsr_opt"/>
+    <feature name="mmxext"/>
+    <feature name="osxsave"/>
+    <feature name="monitor"/>
+    <feature name="ht"/>
+    <feature name="vme"/>
+  </cpu>
+  <clock offset="utc">
+    <timer name="rtc" tickpolicy="catchup"/>
+    <timer name="pit" tickpolicy="delay"/>
+    <timer name="hpet" present="no"/>
+  </clock>
+  <on_reboot>destroy</on_reboot>
+  <pm>
+    <suspend-to-mem enabled="no"/>
+    <suspend-to-disk enabled="no"/>
+  </pm>
+  <devices>
+    <emulator>/usr/bin/qemu-kvm</emulator>
+    <controller type="usb" index="0" model="ich9-ehci1"/>
+    <controller type="usb" index="0" model="ich9-uhci1">
+      <master startport="0"/>
+    </controller>
+    <controller type="usb" index="0" model="ich9-uhci2">
+      <master startport="2"/>
+    </controller>
+    <controller type="usb" index="0" model="ich9-uhci3">
+      <master startport="4"/>
+    </controller>
+    <interface type="bridge">
+      <source bridge="eth0"/>
+      <mac address="00:11:22:33:44:55"/>
+      <model type="e1000"/>
+    </interface>
+    <console type="pty"/>
+    <channel type="spicevmc">
+      <target type="virtio" name="com.redhat.spice.0"/>
+    </channel>
+    <input type="tablet" bus="usb"/>
+    <graphics type="spice" port="-1" tlsPort="-1" autoport="yes">
+      <image compression="off"/>
+    </graphics>
+    <sound model="ich6"/>
+    <video>
+      <model type="qxl"/>
+    </video>
+    <redirdev bus="usb" type="spicevmc"/>
+    <redirdev bus="usb" type="spicevmc"/>
+  </devices>
+</domain>
+<domain type="kvm">
+  <name>foobar</name>
+  <uuid>00000000-1111-2222-3333-444444444444</uuid>
+  <memory>65536</memory>
+  <currentMemory>65536</currentMemory>
+  <vcpu>1</vcpu>
+  <os>
+    <type arch="x86_64" machine="pc">hvm</type>
+    <boot dev="network"/>
+  </os>
+  <features>
+    <acpi/>
+    <apic/>
+    <vmport state="off"/>
+  </features>
+  <cpu mode="custom" match="exact">
+    <model>Opteron_G4</model>
+    <vendor>AMD</vendor>
+    <feature name="invtsc"/>
+    <feature name="perfctr_nb"/>
+    <feature name="perfctr_core"/>
+    <feature name="topoext"/>
+    <feature name="nodeid_msr"/>
+    <feature name="lwp"/>
+    <feature name="wdt"/>
+    <feature name="skinit"/>
+    <feature name="ibs"/>
+    <feature name="osvw"/>
+    <feature name="cr8legacy"/>
+    <feature name="extapic"/>
+    <feature name="cmp_legacy"/>
+    <feature name="fxsr_opt"/>
+    <feature name="mmxext"/>
+    <feature name="osxsave"/>
+    <feature name="monitor"/>
+    <feature name="ht"/>
+    <feature name="vme"/>
+  </cpu>
+  <clock offset="utc">
+    <timer name="rtc" tickpolicy="catchup"/>
+    <timer name="pit" tickpolicy="delay"/>
+    <timer name="hpet" present="no"/>
+  </clock>
+  <pm>
+    <suspend-to-mem enabled="no"/>
+    <suspend-to-disk enabled="no"/>
+  </pm>
+  <devices>
+    <emulator>/usr/bin/qemu-kvm</emulator>
+    <controller type="usb" index="0" model="ich9-ehci1"/>
+    <controller type="usb" index="0" model="ich9-uhci1">
+      <master startport="0"/>
+    </controller>
+    <controller type="usb" index="0" model="ich9-uhci2">
+      <master startport="2"/>
+    </controller>
+    <controller type="usb" index="0" model="ich9-uhci3">
+      <master startport="4"/>
+    </controller>
+    <interface type="bridge">
+      <source bridge="eth0"/>
+      <mac address="00:11:22:33:44:55"/>
+      <model type="e1000"/>
+    </interface>
+    <console type="pty"/>
+    <channel type="spicevmc">
+      <target type="virtio" name="com.redhat.spice.0"/>
+    </channel>
+    <input type="tablet" bus="usb"/>
+    <graphics type="spice" port="-1" tlsPort="-1" autoport="yes">
+      <image compression="off"/>
+    </graphics>
+    <sound model="ich6"/>
+    <video>
+      <model type="qxl"/>
+    </video>
+    <redirdev bus="usb" type="spicevmc"/>
+    <redirdev bus="usb" type="spicevmc"/>
+  </devices>
+</domain>
Index: virt-manager-1.5.1/tests/cli-test-xml/compare/virt-install-singleton-config-1.xml
===================================================================
--- virt-manager-1.5.1.orig/tests/cli-test-xml/compare/virt-install-singleton-config-1.xml
+++ virt-manager-1.5.1/tests/cli-test-xml/compare/virt-install-singleton-config-1.xml
@@ -24,7 +24,21 @@
     <acpi/>
     <vmport state="off"/>
   </features>
-  <cpu mode="host-model"/>
+  <cpu mode="custom" match="exact">
+    <model fallback="forbid">Broadwell</model>
+    <vendor>Intel</vendor>
+    <feature policy="require" name="vme"/>
+    <feature policy="require" name="ss"/>
+    <feature policy="require" name="f16c"/>
+    <feature policy="require" name="rdrand"/>
+    <feature policy="require" name="hypervisor"/>
+    <feature policy="require" name="arat"/>
+    <feature policy="require" name="tsc_adjust"/>
+    <feature policy="require" name="xsaveopt"/>
+    <feature policy="require" name="pdpe1gb"/>
+    <feature policy="require" name="abm"/>
+    <feature policy="require" name="invtsc"/>
+  </cpu>
   <clock offset="localtime">
     <timer name="rtc" tickpolicy="catchup"/>
     <timer name="pit" tickpolicy="delay"/>
Index: virt-manager-1.5.1/tests/clitest.py
===================================================================
--- virt-manager-1.5.1.orig/tests/clitest.py
+++ virt-manager-1.5.1/tests/clitest.py
@@ -389,7 +389,7 @@ c = vinst.add_category("xml-comparsion",
 c.add_compare(""" \
 --memory 1024 \
 --vcpus 4 --cpuset=1,3-5 \
---cpu host \
+--cpu host-copy \
 --description \"foobar & baz\" \
 --boot uefi,smbios_mode=emulate \
 --security type=dynamic \
@@ -805,7 +805,8 @@ c.add_compare("--disk none --location %(
 c.add_compare("--disk none --location nfs:example.com/fake --nonetworks", "location-nfs")  # Using --location nfs
 c.add_compare("--disk %(EXISTIMG1)s --pxe --os-variant rhel6.4", "kvm-rhel6")  # RHEL6 defaults
 c.add_compare("--disk %(EXISTIMG1)s --pxe --os-variant rhel7.0", "kvm-rhel7")  # RHEL7 defaults
-c.add_compare("--disk %(EXISTIMG1)s --pxe --os-variant centos7.0", "kvm-centos7")  # Centos 7 defaults
+c.add_compare("--connect " + utils.URIs.kvm_nodomcaps + " --cpu host-copy --disk none --pxe", "kvm-hostcopy-fallback")  # No domcaps so need to use capabilities for CPU host-copy
+c.add_compare("--disk %(EXISTIMG1)s --pxe --os-variant centos7.0", "kvm-centos7", skip_check=OLD_OSINFO)  # Centos 7 defaults
 c.add_compare("--os-variant win7 --cdrom %(EXISTIMG2)s --boot loader_type=pflash,loader=CODE.fd,nvram_template=VARS.fd --disk %(EXISTIMG1)s", "win7-uefi")  # no HYPER-V with UEFI
 c.add_compare("--machine q35 --cdrom %(EXISTIMG2)s --disk %(EXISTIMG1)s", "q35-defaults")  # proper q35 disk defaults
 c.add_compare("--disk size=20 --os-variant solaris10", "solaris10-defaults")  # test solaris OS defaults
Index: virt-manager-1.5.1/tests/xmlparse.py
===================================================================
--- virt-manager-1.5.1.orig/tests/xmlparse.py
+++ virt-manager-1.5.1/tests/xmlparse.py
@@ -1362,7 +1362,7 @@ class XMLParseTest(unittest.TestCase):
         outfile = "tests/xmlparse-xml/%s-out.xml" % basename
         guest = virtinst.Guest(kvmconn, parsexml=open(infile).read())
 
-        guest.cpu.copy_host_cpu()
+        guest.cpu.copy_host_cpu(guest)
         guest.cpu.clear()
         utils.diff_compare(guest.get_xml_config(), outfile)
 
Index: virt-manager-1.5.1/virtinst/cpu.py
===================================================================
--- virt-manager-1.5.1.orig/virtinst/cpu.py
+++ virt-manager-1.5.1/virtinst/cpu.py
@@ -106,7 +106,7 @@ class CPU(XMLBuilder):
                 self.remove_feature(f)
             self.mode = val
         elif val == self.SPECIAL_MODE_HOST_COPY:
-            self.copy_host_cpu()
+            self.copy_host_cpu(guest)
         elif (val == self.SPECIAL_MODE_HV_DEFAULT or
               val == self.SPECIAL_MODE_CLEAR):
             self.clear()
@@ -142,24 +142,37 @@ class CPU(XMLBuilder):
         self.add_child(obj)
         return obj
 
-    def copy_host_cpu(self):
+    def copy_host_cpu(self, guest):
         """
-        Enact the equivalent of qemu -cpu host, pulling all info
-        from capabilities about the host CPU
+        Try to manually mimic host-model, copying all the info
+        preferably out of domcapabilities, but capabilities as fallback.
         """
-        cpu = self.conn.caps.host.cpu
-        if not cpu.model:
-            raise ValueError(_("No host CPU reported in capabilities"))
+
+        domcaps = guest.lookup_domcaps()
+        if domcaps.supports_safe_host_model():
+            logging.debug("Using domcaps for host-copy")
+            cpu = domcaps.cpu.get_mode("host-model")
+            model = cpu.models[0].model
+            fallback = cpu.models[0].fallback
+        else:
+            cpu = self.conn.caps.host.cpu
+            model = cpu.model
+            fallback = None
+            if not model:
+                raise ValueError(_("No host CPU reported in capabilities"))
 
         self.mode = "custom"
         self.match = "exact"
-        self.model = cpu.model
+        self.model = model
+        if fallback:
+            self.model_fallback = fallback
         self.vendor = cpu.vendor
 
         for feature in self.features:
             self.remove_feature(feature)
         for feature in cpu.features:
-            self.add_feature(feature.name)
+            policy = getattr(feature, "policy", "require")
+            self.add_feature(feature.name, policy)
 
     def vcpus_from_topology(self):
         """
Index: virt-manager-1.5.1/virtinst/domcapabilities.py
===================================================================
--- virt-manager-1.5.1.orig/virtinst/domcapabilities.py
+++ virt-manager-1.5.1/virtinst/domcapabilities.py
@@ -81,18 +81,28 @@ class _CPUModel(XMLBuilder):
     _XML_ROOT_NAME = "model"
     model = XMLProperty(".")
     usable = XMLProperty("./@usable", is_yesno=True)
+    fallback = XMLProperty("./@fallback")
+
+
+class _CPUFeature(XMLBuilder):
+    _XML_ROOT_NAME = "feature"
+    name = XMLProperty("./@name")
+    policy = XMLProperty("./@policy")
 
 
 class _CPUMode(XMLBuilder):
     _XML_ROOT_NAME = "mode"
     name = XMLProperty("./@name")
-    models = XMLChildProperty(_CPUModel)
+    vendor = XMLProperty("./vendor")
 
+    models = XMLChildProperty(_CPUModel)
     def get_model(self, name):
         for model in self.models:
             if model.model == name:
                 return model
 
+    features = XMLChildProperty(_CPUFeature)
+
 class _CPU(XMLBuilder):
     _XML_ROOT_NAME = "cpu"
     modes = XMLChildProperty(_CPUMode)
