qemu_agent: Move updater function for VCPU hotplug into qemu_agent.c

To allow testing of the cpu updater function, this function needs to be
available separately. Export it from qemu_agent.c where it should
belong.
This commit is contained in:
Peter Krempa 2013-07-30 12:04:21 +02:00
parent 2e38753fb0
commit 18a629167c
3 changed files with 67 additions and 63 deletions

View File

@ -1597,3 +1597,66 @@ cleanup:
virJSONValueFree(cpus);
return ret;
}
/* modify the cpu info structure to set the correct amount of cpus */
int
qemuAgentUpdateCPUInfo(unsigned int nvcpus,
qemuAgentCPUInfoPtr cpuinfo,
int ncpuinfo)
{
size_t i;
int nonline = 0;
int nofflinable = 0;
/* count the active and offlinable cpus */
for (i = 0; i < ncpuinfo; i++) {
if (cpuinfo[i].online)
nonline++;
if (cpuinfo[i].offlinable && cpuinfo[i].online)
nofflinable++;
/* This shouldn't happen, but we can't trust the guest agent */
if (!cpuinfo[i].online && !cpuinfo[i].offlinable) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Invalid data provided by guest agent"));
return -1;
}
}
/* the guest agent reported less cpus than requested */
if (nvcpus > ncpuinfo) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("guest agent reports less cpu than requested"));
return -1;
}
/* not enough offlinable CPUs to support the request */
if (nvcpus < nonline - nofflinable) {
virReportError(VIR_ERR_INVALID_ARG, "%s",
_("Cannot offline enough CPUs"));
return -1;
}
for (i = 0; i < ncpuinfo; i++) {
if (nvcpus < nonline) {
/* unplug */
if (cpuinfo[i].offlinable && cpuinfo[i].online) {
cpuinfo[i].online = false;
nonline--;
}
} else if (nvcpus > nonline) {
/* plug */
if (!cpuinfo[i].online) {
cpuinfo[i].online = true;
nonline++;
}
} else {
/* done */
break;
}
}
return 0;
}

View File

@ -94,4 +94,7 @@ struct _qemuAgentCPUInfo {
int qemuAgentGetVCPUs(qemuAgentPtr mon, qemuAgentCPUInfoPtr *info);
int qemuAgentSetVCPUs(qemuAgentPtr mon, qemuAgentCPUInfoPtr cpus, size_t ncpus);
int qemuAgentUpdateCPUInfo(unsigned int nvcpus,
qemuAgentCPUInfoPtr cpuinfo,
int ncpuinfo);
#endif /* __QEMU_AGENT_H__ */

View File

@ -4089,68 +4089,6 @@ unsupported:
}
static int
qemuDomainPrepareAgentVCPUs(unsigned int nvcpus,
qemuAgentCPUInfoPtr cpuinfo,
int ncpuinfo)
{
size_t i;
int nonline = 0;
int nofflinable = 0;
/* count the active and offlinable cpus */
for (i = 0; i < ncpuinfo; i++) {
if (cpuinfo[i].online)
nonline++;
if (cpuinfo[i].offlinable && cpuinfo[i].online)
nofflinable++;
/* This shouldn't happen, but we can't trust the guest agent */
if (!cpuinfo[i].online && !cpuinfo[i].offlinable) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Invalid data provided by guest agent"));
return -1;
}
}
/* the guest agent reported less cpus than requested */
if (nvcpus > ncpuinfo) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("guest agent reports less cpu than requested"));
return -1;
}
/* not enough offlinable CPUs to support the request */
if (nvcpus < nonline - nofflinable) {
virReportError(VIR_ERR_INVALID_ARG, "%s",
_("Cannot offline enough CPUs"));
return -1;
}
for (i = 0; i < ncpuinfo; i++) {
if (nvcpus < nonline) {
/* unplug */
if (cpuinfo[i].offlinable && cpuinfo[i].online) {
cpuinfo[i].online = false;
nonline--;
}
} else if (nvcpus > nonline) {
/* plug */
if (!cpuinfo[i].online) {
cpuinfo[i].online = true;
nonline++;
}
} else {
/* done */
break;
}
}
return 0;
}
static int
qemuDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
unsigned int flags)
@ -4243,7 +4181,7 @@ qemuDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
if (ncpuinfo < 0)
goto endjob;
if (qemuDomainPrepareAgentVCPUs(nvcpus, cpuinfo, ncpuinfo) < 0)
if (qemuAgentUpdateCPUInfo(nvcpus, cpuinfo, ncpuinfo) < 0)
goto endjob;
qemuDomainObjEnterAgent(vm);