Skip to content

Commit 706e236

Browse files
Jijunustcweizhou
authored andcommitted
Add cpu model for kvm guest.Now all the kvm guest's cpu model is 'QEMU Virtual CPU version xxx'. This will affect the activation of Windows OS and low performance. I add three mode for user to indicate the guest cpu model.add libvirt version check
Signed-off-by: Wei Zhou <w.zhou@leaseweb.com> (cherry picked from commit 2903bb5)
1 parent 630d256 commit 706e236

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

agent/conf/agent.properties

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,24 @@ domr.scripts.dir=scripts/network/domr/kvm
8080
# native = com.cloud.hypervisor.kvm.resource.BridgeVifDriver
8181
# openvswitch = com.cloud.hypervisor.kvm.resource.OvsVifDriver
8282
#libvirt.vif.driver=com.cloud.hypervisor.kvm.resource.BridgeVifDriver
83+
84+
# setting to enable the cpu model to kvm guest globally.
85+
# three option:custom,host-model and host-passthrough.
86+
# custom - user custom the CPU model which specified by guest.cpu.model.
87+
# host-model - identify the named CPU model which most closely matches the host,
88+
# and then request additional CPU flags to complete the match. This should give
89+
# close to maximum functionality/performance, which maintaining good
90+
# reliability/compatibility if the guest is migrated to another host with slightly different host CPUs.
91+
# host-passthrough - tell KVM to passthrough the host CPU with no modifications.
92+
# The difference to host-model, instead of just matching feature flags,
93+
# every last detail of the host CPU is matched. This gives absolutely best performance,
94+
# and can be important to some apps which check low level CPU details,
95+
# but it comes at a cost wrt migration. The guest can only be migrated to
96+
# an exactly matching host CPU.
97+
#
98+
# guest.cpu.mode=custom|host-model|host-passthrough
99+
# This param is only valid if guest.cpu.mode=custom,
100+
# for examples:"Conroe" "Penryn", "Nehalem", "Westmere", "pentiumpro" and so
101+
# on,run virsh capabilities for more details.
102+
# guest.cpu.model=
103+

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
import com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool;
175175
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ClockDef;
176176
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ConsoleDef;
177+
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.CpuModeDef;
177178
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.CpuTuneDef;
178179
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DevicesDef;
179180
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef;
@@ -332,6 +333,8 @@ protected String getDefaultScriptsDir() {
332333
private boolean _can_bridge_firewall;
333334
protected String _localStoragePath;
334335
protected String _localStorageUUID;
336+
protected String _guestCpuMode;
337+
protected String _guestCpuModel;
335338
private final Map <String, String> _pifs = new HashMap<String, String>();
336339
private final Map<String, Map<String, String>> hostNetInfo = new HashMap<String, Map<String, String>>();
337340
private final Map<String, vmStats> _vmStats = new ConcurrentHashMap<String, vmStats>();
@@ -692,6 +695,18 @@ public boolean configure(String name, Map<String, Object> params)
692695

693696
}
694697

698+
_guestCpuMode = (String) params.get("guest.cpu.mode");
699+
if (_guestCpuMode != null) {
700+
_guestCpuModel = (String) params.get("guest.cpu.model");
701+
702+
if(_hypervisorLibvirtVersion < (9 * 1000 + 10)) {
703+
s_logger.warn("LibVirt version 0.9.10 required for guest cpu mode, but version " +
704+
prettyVersion(_hypervisorLibvirtVersion) + " detected, so it will be disabled");
705+
_guestCpuMode = null;
706+
_guestCpuModel = null;
707+
}
708+
}
709+
695710
String[] info = NetUtils.getNetworkParams(_privateNic);
696711

697712
_monitor = new KVMHAMonitor(null, info[0], _heartBeatPath);
@@ -3043,6 +3058,11 @@ protected LibvirtVMDef createVMFromSpec(VirtualMachineTO vmTO) {
30433058
grd.setVcpuNum(vmTO.getCpus());
30443059
vm.addComp(grd);
30453060

3061+
CpuModeDef cmd = new CpuModeDef();
3062+
cmd.setMode(_guestCpuMode);
3063+
cmd.setModel(_guestCpuModel);
3064+
vm.addComp(cmd);
3065+
30463066
CpuTuneDef ctd = new CpuTuneDef();
30473067
ctd.setShares(vmTO.getCpus() * vmTO.getSpeed());
30483068
vm.addComp(ctd);
@@ -4671,6 +4691,13 @@ private Answer execute(NetworkRulesSystemVmCommand cmd) {
46714691
return new Answer(cmd, success, "");
46724692
}
46734693

4694+
private String prettyVersion(long version) {
4695+
long major = version / 1000000;
4696+
long minor = version % 1000000 / 1000;
4697+
long release = version % 1000000 % 1000;
4698+
return major + "." + minor + "." + release;
4699+
}
4700+
46744701
@Override
46754702
public void setName(String name) {
46764703
// TODO Auto-generated method stub

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtVMDef.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,33 @@ public String toString() {
833833
}
834834
}
835835

836+
public static class CpuModeDef {
837+
private String _mode;
838+
private String _model;
839+
840+
public void setMode(String mode) {
841+
_mode = mode;
842+
}
843+
844+
public void setModel(String model) {
845+
_model = model;
846+
}
847+
848+
@Override
849+
public String toString() {
850+
StringBuilder modeBuidler = new StringBuilder();
851+
if ("custom".equalsIgnoreCase(_mode) && _model != null) {
852+
modeBuidler.append("<cpu mode='custom' match='exact'><model fallback='allow'>"
853+
+ _model + "</model></cpu>");
854+
} else if ("host-model".equals(_mode)) {
855+
modeBuidler.append("<cpu mode='host-model'><model fallback='allow'></model></cpu>");
856+
} else if ("host-passthrough".equals(_mode)) {
857+
modeBuidler.append("<cpu mode='host-passthrough'></cpu>");
858+
}
859+
return modeBuidler.toString();
860+
}
861+
}
862+
836863
public static class SerialDef {
837864
private final String _type;
838865
private final String _source;

plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtVMDefTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,25 @@ public void testInterfaceEtehrnet() {
3535

3636
assertEquals(expected, ifDef.toString());
3737
}
38+
39+
public void testCpuModeDef(){
40+
LibvirtVMDef.CpuModeDef cpuModeDef = new LibvirtVMDef.CpuModeDef();
41+
cpuModeDef.setMode("custom");
42+
cpuModeDef.setModel("Nehalem");
43+
44+
String expected1 = "<cpu mode='custom' match='exact'><model fallback='allow'>Nehalem</model></cpu>";
45+
46+
assertEquals(expected1, cpuModeDef.toString());
47+
48+
cpuModeDef.setMode("host-model");
49+
String expected2 = "<cpu mode='host-model'><model fallback='allow'></model></cpu>";
50+
51+
assertEquals(expected2, cpuModeDef.toString());
52+
53+
cpuModeDef.setMode("host-passthrough");
54+
String expected3 = "<cpu mode='host-passthrough'></cpu>";
55+
assertEquals(expected3, cpuModeDef.toString());
56+
57+
}
58+
3859
}

0 commit comments

Comments
 (0)