Skip to content

Commit ba2d70a

Browse files
[KVM] CPU Features for System VMs (#10964)
* CPU features for System VMs * Apply guest.cpu.features for System VMs
1 parent 2c34f5e commit ba2d70a

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

agent/conf/agent.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ hypervisor.type=kvm
213213
# If null (default), defaults to the VM's OS architecture
214214
#guest.cpu.arch=
215215

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

220221
# Disables memory ballooning on VM guests for overcommit.

agent/src/main/java/com/cloud/agent/properties/AgentProperties.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,9 @@ public class AgentProperties{
425425
public static final Property<String> GUEST_CPU_ARCH = new Property<>("guest.cpu.arch", null, String.class);
426426

427427
/**
428-
* This param will require CPU features on the CPU section.<br>
429-
* The features listed in this property must be separated by a blank space (see example below).<br>
428+
* Specifies required CPU features for end-user and system VMs.<br>
429+
* These features must be present on the host CPU for VM deployment.<br>
430+
* Multiple features should be separated by whitespace (see example below).<br>
430431
* Possible values: vmx vme <br>
431432
* Data type: String.<br>
432433
* Default value: <code>null</code>

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,15 +1316,7 @@ public boolean configure(final String name, final Map<String, Object> params) th
13161316
params.put("guest.cpu.model", guestCpuModel);
13171317
}
13181318

1319-
final String cpuFeatures = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES);
1320-
if (cpuFeatures != null) {
1321-
this.cpuFeatures = new ArrayList<String>();
1322-
for (final String feature: cpuFeatures.split(" ")) {
1323-
if (!feature.isEmpty()) {
1324-
this.cpuFeatures.add(feature);
1325-
}
1326-
}
1327-
}
1319+
this.cpuFeatures = parseCpuFeatures(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES));
13281320

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

@@ -1430,6 +1422,22 @@ public boolean configure(final String name, final Map<String, Object> params) th
14301422
return true;
14311423
}
14321424

1425+
/**
1426+
* Parses a string containing whitespace-separated CPU feature names and converts it into a list.
1427+
*
1428+
* @param features A string containing whitespace-separated CPU feature names to be parsed.
1429+
* @return A list of CPU feature strings. Returns an empty list if {@code features} is null.
1430+
*/
1431+
protected List<String> parseCpuFeatures(String features) {
1432+
if (features == null) {
1433+
return new ArrayList<>();
1434+
}
1435+
1436+
return Arrays.stream(features.split(" "))
1437+
.filter(feature -> !feature.isEmpty())
1438+
.collect(Collectors.toList());
1439+
}
1440+
14331441
/**
14341442
* Gets the ID list of the VMs to set memory balloon stats period.
14351443
* @param conn the Libvirt connection.
@@ -3210,9 +3218,7 @@ private CpuModeDef createCpuModeDef(VirtualMachineTO vmTO, int vcpus) {
32103218
String cpuModel = MapUtils.isNotEmpty(details) && details.get(VmDetailConstants.GUEST_CPU_MODEL) != null ? details.get(VmDetailConstants.GUEST_CPU_MODEL) : guestCpuModel;
32113219
cmd.setMode(cpuMode);
32123220
cmd.setModel(cpuModel);
3213-
if (VirtualMachine.Type.User.equals(vmTO.getType())) {
3214-
cmd.setFeatures(cpuFeatures);
3215-
}
3221+
cmd.setFeatures(cpuFeatures);
32163222
int vCpusInDef = vmTO.getVcpuMaxLimit() == null ? vcpus : vmTO.getVcpuMaxLimit();
32173223
setCpuTopology(cmd, vCpusInDef, vmTO.getDetails());
32183224
return cmd;

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7126,4 +7126,20 @@ public void testParseGpuDevicesFromResult_withSuccess() {
71267126
assertEquals("2g.10gb", vfInstance2.getModelName());
71277127
assertNull(vfInstance2.getVmName());
71287128
}
7129+
7130+
@Test
7131+
public void parseCpuFeaturesTestReturnEmptyListWhenFeaturesIsNull() {
7132+
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(null);
7133+
Assert.assertEquals(0, cpuFeatures.size());
7134+
}
7135+
7136+
@Test
7137+
public void parseCpuFeaturesTestReturnListOfCpuFeaturesAndIgnoreMultipleWhitespacesAlongsideEachOther() {
7138+
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(" -mca mce -mmx hle ");
7139+
Assert.assertEquals(4, cpuFeatures.size());
7140+
Assert.assertEquals("-mca", cpuFeatures.get(0));
7141+
Assert.assertEquals("mce", cpuFeatures.get(1));
7142+
Assert.assertEquals("-mmx", cpuFeatures.get(2));
7143+
Assert.assertEquals("hle", cpuFeatures.get(3));
7144+
}
71297145
}

0 commit comments

Comments
 (0)