Skip to content

Commit

Permalink
Merge "Support old & new versions of svm and vmx traits"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and openstack-gerrit committed Sep 11, 2020
2 parents 19cb983 + b420c34 commit 3b41633
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
17 changes: 12 additions & 5 deletions nova/tests/functional/libvirt/test_report_cpu_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def test_report_cpu_traits(self):
# trait values are coming from fakelibvirt's baselineCPU result.
# COMPUTE_NODE is always set on the compute node provider.
traits = self._get_provider_traits(self.host_uuid)
for trait in ('HW_CPU_X86_VMX', 'HW_CPU_X86_AESNI', 'COMPUTE_NODE'):
for trait in (
'HW_CPU_X86_VMX', 'HW_CPU_X86_INTEL_VMX', 'HW_CPU_X86_AESNI',
'COMPUTE_NODE',
):
self.assertIn(trait, traits)

self._create_trait('CUSTOM_TRAITS')
Expand All @@ -96,10 +99,14 @@ def test_report_cpu_traits(self):
# and it's not in the baseline for the host.
traits = set(self._get_provider_traits(self.host_uuid))
expected_traits = self.expected_libvirt_driver_capability_traits.union(
[u'HW_CPU_X86_VMX', u'HW_CPU_X86_AESNI', u'CUSTOM_TRAITS',
# The periodic restored the COMPUTE_NODE trait.
u'COMPUTE_NODE']
)
[
'HW_CPU_X86_VMX',
'HW_CPU_X86_INTEL_VMX',
'HW_CPU_X86_AESNI',
'CUSTOM_TRAITS',
# The periodic restored the COMPUTE_NODE trait.
'COMPUTE_NODE',
])
for trait in expected_traits:
self.assertIn(trait, traits)

Expand Down
18 changes: 11 additions & 7 deletions nova/tests/unit/virt/libvirt/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21101,7 +21101,7 @@ class TestUpdateProviderTree(test.NoDBTestCase):
pcpus = 12
memory_mb = 1024
disk_gb = 200
cpu_traits = {t: False for t in libvirt_utils.CPU_TRAITS_MAPPING.values()}
cpu_traits = libvirt_utils.cpu_features_to_traits({})

def setUp(self):
super(TestUpdateProviderTree, self).setUp()
Expand Down Expand Up @@ -21816,7 +21816,7 @@ class TraitsComparisonMixin(object):

def assertTraitsEqual(self, expected, actual):
exp = {t: t in expected
for t in libvirt_utils.CPU_TRAITS_MAPPING.values()}
for t in libvirt_utils.cpu_features_to_traits({})}
self.assertEqual(exp, actual)


Expand Down Expand Up @@ -25538,8 +25538,9 @@ def test_cpu_traits_with_passthrough_mode(self):
are calculated from fakelibvirt's baseline CPU features.
"""
self.flags(cpu_mode='host-passthrough', group='libvirt')
self.assertTraitsEqual(['HW_CPU_X86_AESNI', 'HW_CPU_X86_VMX'],
self.drvr._get_cpu_feature_traits())
self.assertTraitsEqual(
['HW_CPU_X86_AESNI', 'HW_CPU_X86_VMX', 'HW_CPU_X86_INTEL_VMX'],
self.drvr._get_cpu_feature_traits())

@mock.patch('nova.virt.libvirt.host.libvirt.Connection.baselineCPU')
def test_cpu_traits_with_mode_none(self, mock_baseline):
Expand All @@ -25548,9 +25549,10 @@ def test_cpu_traits_with_mode_none(self, mock_baseline):
"""
self.flags(cpu_mode='none', group='libvirt')
mock_baseline.return_value = _fake_qemu64_cpu_feature
self.assertTraitsEqual(['HW_CPU_X86_SSE', 'HW_CPU_X86_SVM',
'HW_CPU_X86_MMX', 'HW_CPU_X86_SSE2'],
self.drvr._get_cpu_feature_traits())
self.assertTraitsEqual(
['HW_CPU_X86_SSE', 'HW_CPU_X86_SVM', 'HW_CPU_X86_AMD_SVM',
'HW_CPU_X86_MMX', 'HW_CPU_X86_SSE2'],
self.drvr._get_cpu_feature_traits())

mock_baseline.assert_called_with([u'''<cpu>
<arch>x86_64</arch>
Expand Down Expand Up @@ -25638,6 +25640,7 @@ def test_cpu_traits_with_mode_custom_multi_models(self, mocked_baseline,
'HW_CPU_X86_SSE2',
'HW_CPU_X86_SSE',
'HW_CPU_X86_MMX',
'HW_CPU_X86_AMD_SVM',
'HW_CPU_X86_SVM'
], self.drvr._get_cpu_feature_traits()
)
Expand Down Expand Up @@ -25705,6 +25708,7 @@ def test_cpu_traits_with_mode_custom_multi_models_and_extra_flags(self,
'HW_CPU_X86_SSE2',
'HW_CPU_X86_SSE',
'HW_CPU_X86_MMX',
'HW_CPU_X86_AMD_SVM',
'HW_CPU_X86_SVM'
], self.drvr._get_cpu_feature_traits()
)
Expand Down
20 changes: 14 additions & 6 deletions nova/virt/libvirt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@
'sse4.2': os_traits.HW_CPU_X86_SSE42,
'sse4a': os_traits.HW_CPU_X86_SSE4A,
'ssse3': os_traits.HW_CPU_X86_SSSE3,
'svm': os_traits.HW_CPU_X86_SVM,
# We have to continue to support the old (generic) trait for the
# AMD-specific svm feature.
'svm': (os_traits.HW_CPU_X86_SVM, os_traits.HW_CPU_X86_AMD_SVM),
'tbm': os_traits.HW_CPU_X86_TBM,
'vmx': os_traits.HW_CPU_X86_VMX,
# We have to continue to support the old (generic) trait for the
# Intel-specific vmx feature.
'vmx': (os_traits.HW_CPU_X86_VMX, os_traits.HW_CPU_X86_INTEL_VMX),
'xop': os_traits.HW_CPU_X86_XOP
}

Expand Down Expand Up @@ -577,11 +581,15 @@ def cpu_features_to_traits(features: ty.Set[str]) -> ty.Dict[str, bool]:
"""Returns this driver's CPU traits dict where keys are trait names from
CPU_TRAITS_MAPPING, values are boolean indicates whether the trait should
be set in the provider tree.
:param features: A set of feature names (short, lowercase,
CPU_TRAITS_MAPPING's keys).
"""
traits = {trait_name: False for trait_name in CPU_TRAITS_MAPPING.values()}
for f in features:
if f in CPU_TRAITS_MAPPING:
traits[CPU_TRAITS_MAPPING[f]] = True
traits = {}
for feature_name, val in CPU_TRAITS_MAPPING.items():
trait_tuple = val if isinstance(val, tuple) else (val,)
for trait_name in trait_tuple:
traits[trait_name] = feature_name in features

return traits

Expand Down

0 comments on commit 3b41633

Please sign in to comment.