Skip to content

[KVM] CPU Features for System VMs #10964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions agent/conf/agent.properties
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ hypervisor.type=kvm
# If null (default), defaults to the VM's OS architecture
#guest.cpu.arch=

# This param will require CPU features on the CPU section.
# The features listed in this property must be separated by a blank space (e.g.: vmx vme)
# Specifies required CPU features for end-user and system VMs.
# These features must be present on the host CPU for VM deployment.
# Multiple features should be separated by whitespace (e.g.: vmx vme).
#guest.cpu.features=

# Disables memory ballooning on VM guests for overcommit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,9 @@ public class AgentProperties{
public static final Property<String> GUEST_CPU_ARCH = new Property<>("guest.cpu.arch", null, String.class);

/**
* This param will require CPU features on the CPU section.<br>
* The features listed in this property must be separated by a blank space (see example below).<br>
* Specifies required CPU features for end-user and system VMs.<br>
* These features must be present on the host CPU for VM deployment.<br>
* Multiple features should be separated by whitespace (see example below).<br>
* Possible values: vmx vme <br>
* Data type: String.<br>
* Default value: <code>null</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,15 +1283,7 @@ public boolean configure(final String name, final Map<String, Object> params) th
params.put("guest.cpu.model", guestCpuModel);
}

final String cpuFeatures = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES);
if (cpuFeatures != null) {
this.cpuFeatures = new ArrayList<String>();
for (final String feature: cpuFeatures.split(" ")) {
if (!feature.isEmpty()) {
this.cpuFeatures.add(feature);
}
}
}
this.cpuFeatures = parseCpuFeatures(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES));

final String[] info = NetUtils.getNetworkParams(privateNic);

Expand Down Expand Up @@ -1397,6 +1389,22 @@ public boolean configure(final String name, final Map<String, Object> params) th
return true;
}

/**
* Parses a string containing whitespace-separated CPU feature names and converts it into a list.
*
* @param features A string containing whitespace-separated CPU feature names to be parsed.
* @return A list of CPU feature strings. Returns an empty list if {@code features} is null.
*/
protected List<String> parseCpuFeatures(String features) {
if (features == null) {
return new ArrayList<>();
}

return Arrays.stream(features.split(" "))
.filter(feature -> !feature.isEmpty())
.collect(Collectors.toList());
}

/**
* Gets the ID list of the VMs to set memory balloon stats period.
* @param conn the Libvirt connection.
Expand Down Expand Up @@ -2984,9 +2992,7 @@ private CpuModeDef createCpuModeDef(VirtualMachineTO vmTO, int vcpus) {
String cpuModel = MapUtils.isNotEmpty(details) && details.get(VmDetailConstants.GUEST_CPU_MODEL) != null ? details.get(VmDetailConstants.GUEST_CPU_MODEL) : guestCpuModel;
cmd.setMode(cpuMode);
cmd.setModel(cpuModel);
if (VirtualMachine.Type.User.equals(vmTO.getType())) {
cmd.setFeatures(cpuFeatures);
}
cmd.setFeatures(cpuFeatures);
int vCpusInDef = vmTO.getVcpuMaxLimit() == null ? vcpus : vmTO.getVcpuMaxLimit();
setCpuTopology(cmd, vCpusInDef, vmTO.getDetails());
return cmd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6620,4 +6620,20 @@ public void recreateCheckpointsOnVmTestVolumesHaveCheckpoints() {
Mockito.verify(libvirtComputingResourceSpy, Mockito.times(1)).recreateCheckpointsOfDisk(Mockito.any(), Mockito.any(), Mockito.any());
Assert.assertTrue(result);
}

@Test
public void parseCpuFeaturesTestReturnEmptyListWhenFeaturesIsNull() {
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(null);
Assert.assertEquals(0, cpuFeatures.size());
}

@Test
public void parseCpuFeaturesTestReturnListOfCpuFeaturesAndIgnoreMultipleWhitespacesAlongsideEachOther() {
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(" -mca mce -mmx hle ");
Assert.assertEquals(4, cpuFeatures.size());
Assert.assertEquals("-mca", cpuFeatures.get(0));
Assert.assertEquals("mce", cpuFeatures.get(1));
Assert.assertEquals("-mmx", cpuFeatures.get(2));
Assert.assertEquals("hle", cpuFeatures.get(3));
}
}
Loading