Blame SOURCES/virt-manager-domcapabilities-introduce-get_cpu_security_features.patch

3d61c0
From ac9aae920fd7705a65f6bf0b236c4baa05e9bf30 Mon Sep 17 00:00:00 2001
3d61c0
Message-Id: <ac9aae920fd7705a65f6bf0b236c4baa05e9bf30@dist-git>
3d61c0
From: Pavel Hrdina <phrdina@redhat.com>
3d61c0
Date: Fri, 15 Mar 2019 09:49:56 +0100
3d61c0
Subject: [PATCH] domcapabilities: introduce get_cpu_security_features
3d61c0
3d61c0
Get all CPU security features that we should enable for guests.
3d61c0
3d61c0
In order to do that we need to get CPU definition from domain
3d61c0
capabilities and modify the XML so it is in required format for
3d61c0
libvirt CPU baseline APIs.  We will prefer the baselineHypervisorCPU
3d61c0
API because that considers what QEMU actually supports and we will
3d61c0
fallback to baselineCPU API if the better one is not supported by
3d61c0
libvirt.
3d61c0
3d61c0
This way we can figure out which of the security features are actually
3d61c0
available on that specific host for that specific QEMU binary.
3d61c0
3d61c0
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
3d61c0
Reviewed-by: Cole Robinson <crobinso@redhat.com>
3d61c0
(cherry picked from commit 4a8b6363c0891e37d9532213a046c5c57aedfd8b)
3d61c0
3d61c0
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1582667
3d61c0
3d61c0
Conflicts:
3d61c0
    - upstream renamed CPU class to DomainCpu
3d61c0
    - upstream renamed get_xml_config() to get_xml()
3d61c0
    - python 2.7 cannot handle Element.attrib set to None
3d61c0
3d61c0
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
3d61c0
Reviewed-by: Cole Robinson <crobinso@redhat.com>
3d61c0
---
3d61c0
 virtinst/domcapabilities.py | 57 +++++++++++++++++++++++++++++++++++++
3d61c0
 1 file changed, 57 insertions(+)
3d61c0
3d61c0
diff --git a/virtinst/domcapabilities.py b/virtinst/domcapabilities.py
3d61c0
index 20b6b38d..12755940 100644
3d61c0
--- a/virtinst/domcapabilities.py
3d61c0
+++ b/virtinst/domcapabilities.py
3d61c0
@@ -20,7 +20,11 @@
3d61c0
 
3d61c0
 import logging
3d61c0
 import re
3d61c0
+import xml.etree.ElementTree as ET
3d61c0
 
3d61c0
+import libvirt
3d61c0
+
3d61c0
+from . import CPU
3d61c0
 from .xmlbuilder import XMLBuilder, XMLChildProperty, XMLProperty
3d61c0
 
3d61c0
 
3d61c0
@@ -210,6 +214,59 @@ class DomainCapabilities(XMLBuilder):
3d61c0
                 return True
3d61c0
         return False
3d61c0
 
3d61c0
+    def _convert_mode_to_cpu(self, xml):
3d61c0
+        root = ET.fromstring(xml)
3d61c0
+        root.tag = "cpu"
3d61c0
+        root.attrib = {}
3d61c0
+        arch = ET.SubElement(root, "arch")
3d61c0
+        arch.text = self.arch
3d61c0
+        return ET.tostring(root, encoding="UTF-8")
3d61c0
+
3d61c0
+    def _get_expandned_cpu(self, mode):
3d61c0
+        cpuXML = self._convert_mode_to_cpu(mode.get_xml_config())
3d61c0
+        logging.debug("CPU XML for security flag baseline: %s", cpuXML)
3d61c0
+
3d61c0
+        try:
3d61c0
+            expandedXML = self.conn.baselineHypervisorCPU(
3d61c0
+                    self.path, self.arch, self.machine, self.domain, [cpuXML],
3d61c0
+                    libvirt.VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES)
3d61c0
+        except libvirt.libvirtError:
3d61c0
+            expandedXML = self.conn.baselineCPU([cpuXML],
3d61c0
+                    libvirt.VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES)
3d61c0
+
3d61c0
+        logging.debug("Expanded CPU XML: %s", expandedXML)
3d61c0
+
3d61c0
+        return CPU(self.conn, expandedXML)
3d61c0
+
3d61c0
+    def get_cpu_security_features(self):
3d61c0
+        sec_features = [
3d61c0
+                'pcid',
3d61c0
+                'spec-ctrl',
3d61c0
+                'ssbd',
3d61c0
+                'pdpe1gb',
3d61c0
+                'ibpb',
3d61c0
+                'virt-ssbd',
3d61c0
+                'amd-ssbd',
3d61c0
+                'amd-no-ssb']
3d61c0
+
3d61c0
+        features = []
3d61c0
+
3d61c0
+        for m in self.cpu.modes:
3d61c0
+            if m.name != "host-model" or not m.supported:
3d61c0
+                continue
3d61c0
+
3d61c0
+            try:
3d61c0
+                cpu = self._get_expandned_cpu(m)
3d61c0
+            except libvirt.libvirtError as e:
3d61c0
+                logging.warning(_("Failed to get expanded CPU XML: %s"), e)
3d61c0
+                break
3d61c0
+
3d61c0
+            for feature in cpu.features:
3d61c0
+                if feature.name in sec_features:
3d61c0
+                    features.append(feature.name)
3d61c0
+
3d61c0
+        return features
3d61c0
+
3d61c0
 
3d61c0
     _XML_ROOT_NAME = "domainCapabilities"
3d61c0
     os = XMLChildProperty(_OS, is_single=True)
3d61c0
-- 
3d61c0
2.20.1
3d61c0