Skip to content

Commit b94f914

Browse files
bernardodemarcodhslove
authored andcommitted
[KVM] CPU Features for System VMs (apache#10964)
* CPU features for System VMs * Apply guest.cpu.features for System VMs
1 parent 3bbdedc commit b94f914

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
@@ -1413,15 +1413,7 @@ public boolean configure(final String name, final Map<String, Object> params) th
14131413
guestCpuMode = "host-passthrough";
14141414
}
14151415

1416-
final String cpuFeatures = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES);
1417-
if (cpuFeatures != null) {
1418-
this.cpuFeatures = new ArrayList<String>();
1419-
for (final String feature: cpuFeatures.split(" ")) {
1420-
if (!feature.isEmpty()) {
1421-
this.cpuFeatures.add(feature);
1422-
}
1423-
}
1424-
}
1416+
this.cpuFeatures = parseCpuFeatures(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES));
14251417

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

@@ -1529,6 +1521,22 @@ public boolean configure(final String name, final Map<String, Object> params) th
15291521
return true;
15301522
}
15311523

1524+
/**
1525+
* Parses a string containing whitespace-separated CPU feature names and converts it into a list.
1526+
*
1527+
* @param features A string containing whitespace-separated CPU feature names to be parsed.
1528+
* @return A list of CPU feature strings. Returns an empty list if {@code features} is null.
1529+
*/
1530+
protected List<String> parseCpuFeatures(String features) {
1531+
if (features == null) {
1532+
return new ArrayList<>();
1533+
}
1534+
1535+
return Arrays.stream(features.split(" "))
1536+
.filter(feature -> !feature.isEmpty())
1537+
.collect(Collectors.toList());
1538+
}
1539+
15321540
/**
15331541
* Gets the ID list of the VMs to set memory balloon stats period.
15341542
* @param conn the Libvirt connection.
@@ -3515,9 +3523,7 @@ private CpuModeDef createCpuModeDef(VirtualMachineTO vmTO, int vcpus) {
35153523
String cpuModel = MapUtils.isNotEmpty(details) && details.get(VmDetailConstants.GUEST_CPU_MODEL) != null ? details.get(VmDetailConstants.GUEST_CPU_MODEL) : guestCpuModel;
35163524
cmd.setMode(cpuMode);
35173525
cmd.setModel(cpuModel);
3518-
if (VirtualMachine.Type.User.equals(vmTO.getType())) {
3519-
cmd.setFeatures(cpuFeatures);
3520-
}
3526+
cmd.setFeatures(cpuFeatures);
35213527
int vCpusInDef = vmTO.getVcpuMaxLimit() == null ? vcpus : vmTO.getVcpuMaxLimit();
35223528
setCpuTopology(cmd, vCpusInDef, vmTO.getDetails());
35233529
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)