libvirt/src/conf/cpu_conf.c

427 lines
11 KiB
C
Raw Normal View History

/*
* cpu_conf.h: CPU XML handling
*
* Copyright (C) 2009, 2010 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* Jiri Denemark <jdenemar@redhat.com>
*/
#include <config.h>
#include "virterror_internal.h"
#include "memory.h"
#include "util.h"
#include "buf.h"
#include "cpu_conf.h"
#define VIR_FROM_THIS VIR_FROM_CPU
build: consistently use C99 varargs macros Prior to this patch, there was an inconsistent mix between GNU and C99. For consistency, and potential portability to other compilers, stick with the C99 vararg macro syntax. * src/conf/cpu_conf.c (virCPUReportError): Use C99 rather than GNU vararg macro syntax. * src/conf/domain_conf.c (virDomainReportError): Likewise. * src/conf/domain_event.c (eventReportError): Likewise. * src/conf/interface_conf.c (virInterfaceReportError): Likewise. * src/conf/network_conf.c (virNetworkReportError): Likewise. * src/conf/node_device_conf.h (virNodeDeviceReportError): Likewise. * src/conf/secret_conf.h (virSecretReportError): Likewise. * src/conf/storage_conf.h (virStorageReportError): Likewise. * src/esx/esx_device_monitor.c (ESX_ERROR): Use C99 rather than GNU vararg macro syntax. * src/esx/esx_driver.c (ESX_ERROR): Likewise. * src/esx/esx_interface_driver.c (ESX_ERROR): Likewise. * src/esx/esx_network_driver.c (ESX_ERROR): Likewise. * src/esx/esx_secret_driver.c (ESX_ERROR): Likewise. * src/esx/esx_storage_driver.c (ESX_ERROR): Likewise. * src/esx/esx_util.c (ESX_ERROR): Likewise. * src/esx/esx_vi.c (ESX_VI_ERROR): Likewise. * src/esx/esx_vi_methods.c (ESX_VI_ERROR): Likewise. * src/esx/esx_vi_types.c (ESX_VI_ERROR): Likewise. * src/esx/esx_vmx.c (ESX_ERROR): Likewise. * src/util/hostusb.c (usbReportError): Use C99 rather than GNU vararg macro syntax. * src/util/json.c (virJSONError): Likewise. * src/util/macvtap.c (ReportError): Likewise. * src/util/pci.c (pciReportError): Likewise. * src/util/stats_linux.c (virStatsError): Likewise. * src/util/util.c (virUtilError): Likewise. * src/util/xml.c (virXMLError): Likewise. * src/xen/proxy_internal.c (virProxyError): Use C99 rather than GNU vararg macro syntax. * src/xen/sexpr.c (virSexprError): Likewise. * src/xen/xen_driver.c (xenUnifiedError): Likewise. * src/xen/xen_hypervisor.c (virXenError): Likewise. * src/xen/xen_inotify.c (virXenInotifyError): Likewise. * src/xen/xend_internal.c (virXendError): Likewise. * src/xen/xm_internal.c (xenXMError): Likewise. * src/xen/xs_internal.c (virXenStoreError): Likewise. * src/cpu/cpu.h (virCPUReportError): Use C99 rather than GNU vararg macro syntax. * src/datatypes.c (virLibConnError): Likewise. * src/interface/netcf_driver.c (interfaceReportError): Likewise. * src/libvirt.c (virLibStreamError): Likewise. * src/lxc/lxc_conf.h (lxcError): Likewise. * src/network/bridge_driver.c (networkReportError): Likewise. * src/nodeinfo.c (nodeReportError): Likewise. * src/opennebula/one_conf.h (oneError): Likewise. * src/openvz/openvz_conf.h (openvzError): Likewise. * src/phyp/phyp_driver.c (PHYP_ERROR): Likewise. * src/qemu/qemu_conf.h (qemuReportError): Likewise. * src/remote/remote_driver.c (errorf): Likewise. * src/security/security_driver.h (virSecurityReportError): Likewise. * src/test/test_driver.c (testError): Likewise. * src/uml/uml_conf.h (umlReportError): Likewise. * src/vbox/vbox_driver.c (vboxError): Likewise. * src/vbox/vbox_tmpl.c (vboxError): Likewise.
2010-03-01 23:38:28 +00:00
#define virCPUReportError(code, ...) \
virReportErrorHelper(NULL, VIR_FROM_CPU, code, __FILE__, \
build: consistently use C99 varargs macros Prior to this patch, there was an inconsistent mix between GNU and C99. For consistency, and potential portability to other compilers, stick with the C99 vararg macro syntax. * src/conf/cpu_conf.c (virCPUReportError): Use C99 rather than GNU vararg macro syntax. * src/conf/domain_conf.c (virDomainReportError): Likewise. * src/conf/domain_event.c (eventReportError): Likewise. * src/conf/interface_conf.c (virInterfaceReportError): Likewise. * src/conf/network_conf.c (virNetworkReportError): Likewise. * src/conf/node_device_conf.h (virNodeDeviceReportError): Likewise. * src/conf/secret_conf.h (virSecretReportError): Likewise. * src/conf/storage_conf.h (virStorageReportError): Likewise. * src/esx/esx_device_monitor.c (ESX_ERROR): Use C99 rather than GNU vararg macro syntax. * src/esx/esx_driver.c (ESX_ERROR): Likewise. * src/esx/esx_interface_driver.c (ESX_ERROR): Likewise. * src/esx/esx_network_driver.c (ESX_ERROR): Likewise. * src/esx/esx_secret_driver.c (ESX_ERROR): Likewise. * src/esx/esx_storage_driver.c (ESX_ERROR): Likewise. * src/esx/esx_util.c (ESX_ERROR): Likewise. * src/esx/esx_vi.c (ESX_VI_ERROR): Likewise. * src/esx/esx_vi_methods.c (ESX_VI_ERROR): Likewise. * src/esx/esx_vi_types.c (ESX_VI_ERROR): Likewise. * src/esx/esx_vmx.c (ESX_ERROR): Likewise. * src/util/hostusb.c (usbReportError): Use C99 rather than GNU vararg macro syntax. * src/util/json.c (virJSONError): Likewise. * src/util/macvtap.c (ReportError): Likewise. * src/util/pci.c (pciReportError): Likewise. * src/util/stats_linux.c (virStatsError): Likewise. * src/util/util.c (virUtilError): Likewise. * src/util/xml.c (virXMLError): Likewise. * src/xen/proxy_internal.c (virProxyError): Use C99 rather than GNU vararg macro syntax. * src/xen/sexpr.c (virSexprError): Likewise. * src/xen/xen_driver.c (xenUnifiedError): Likewise. * src/xen/xen_hypervisor.c (virXenError): Likewise. * src/xen/xen_inotify.c (virXenInotifyError): Likewise. * src/xen/xend_internal.c (virXendError): Likewise. * src/xen/xm_internal.c (xenXMError): Likewise. * src/xen/xs_internal.c (virXenStoreError): Likewise. * src/cpu/cpu.h (virCPUReportError): Use C99 rather than GNU vararg macro syntax. * src/datatypes.c (virLibConnError): Likewise. * src/interface/netcf_driver.c (interfaceReportError): Likewise. * src/libvirt.c (virLibStreamError): Likewise. * src/lxc/lxc_conf.h (lxcError): Likewise. * src/network/bridge_driver.c (networkReportError): Likewise. * src/nodeinfo.c (nodeReportError): Likewise. * src/opennebula/one_conf.h (oneError): Likewise. * src/openvz/openvz_conf.h (openvzError): Likewise. * src/phyp/phyp_driver.c (PHYP_ERROR): Likewise. * src/qemu/qemu_conf.h (qemuReportError): Likewise. * src/remote/remote_driver.c (errorf): Likewise. * src/security/security_driver.h (virSecurityReportError): Likewise. * src/test/test_driver.c (testError): Likewise. * src/uml/uml_conf.h (umlReportError): Likewise. * src/vbox/vbox_driver.c (vboxError): Likewise. * src/vbox/vbox_tmpl.c (vboxError): Likewise.
2010-03-01 23:38:28 +00:00
__FUNCTION__, __LINE__, __VA_ARGS__)
VIR_ENUM_IMPL(virCPUMatch, VIR_CPU_MATCH_LAST,
"minimum",
"exact",
"strict")
VIR_ENUM_IMPL(virCPUFeaturePolicy, VIR_CPU_FEATURE_LAST,
"force",
"require",
"optional",
"disable",
"forbid")
void
virCPUDefFree(virCPUDefPtr def)
{
unsigned int i;
if (!def)
return;
VIR_FREE(def->model);
VIR_FREE(def->arch);
for (i = 0 ; i < def->nfeatures ; i++)
VIR_FREE(def->features[i].name);
VIR_FREE(def->features);
VIR_FREE(def);
}
virCPUDefPtr
virCPUDefCopy(const virCPUDefPtr cpu)
{
virCPUDefPtr copy;
unsigned int i;
if (!cpu)
return NULL;
if (VIR_ALLOC(copy) < 0
|| (cpu->arch && !(copy->arch = strdup(cpu->arch)))
|| (cpu->model && !(copy->model = strdup(cpu->model)))
|| VIR_ALLOC_N(copy->features, cpu->nfeatures) < 0)
goto no_memory;
copy->type = cpu->type;
copy->match = cpu->match;
copy->sockets = cpu->sockets;
copy->cores = cpu->cores;
copy->threads = cpu->threads;
copy->nfeatures = cpu->nfeatures;
for (i = 0; i < copy->nfeatures; i++) {
copy->features[i].policy = cpu->features[i].policy;
if (!(copy->features[i].name = strdup(cpu->features[i].name)))
goto no_memory;
}
return copy;
no_memory:
virReportOOMError();
virCPUDefFree(copy);
return NULL;
}
#ifndef PROXY
virCPUDefPtr
virCPUDefParseXML(const xmlNodePtr node,
xmlXPathContextPtr ctxt,
enum virCPUType mode)
{
virCPUDefPtr def;
xmlNodePtr *nodes = NULL;
int n;
unsigned int i;
if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s",
_("XML does not contain expected 'cpu' element"));
return NULL;
}
if (VIR_ALLOC(def) < 0) {
virReportOOMError();
return NULL;
}
if (mode == VIR_CPU_TYPE_AUTO) {
if (virXPathBoolean("boolean(./arch)", ctxt))
def->type = VIR_CPU_TYPE_HOST;
else
def->type = VIR_CPU_TYPE_GUEST;
} else
def->type = mode;
if (def->type == VIR_CPU_TYPE_GUEST) {
char *match = virXMLPropString(node, "match");
if (!match) {
if (virXPathBoolean("boolean(./model)", ctxt)) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Missing match attribute for CPU specification"));
goto error;
}
def->match = -1;
} else {
def->match = virCPUMatchTypeFromString(match);
VIR_FREE(match);
if (def->match < 0) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Invalid match attribute for CPU specification"));
goto error;
}
}
}
if (def->type == VIR_CPU_TYPE_HOST) {
def->arch = virXPathString("string(./arch[1])", ctxt);
if (!def->arch) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Missing CPU architecture"));
goto error;
}
}
if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
def->type == VIR_CPU_TYPE_HOST) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Missing CPU model name"));
goto error;
}
if (virXPathNode("./topology[1]", ctxt)) {
int ret;
unsigned long ul;
ret = virXPathULong("string(./topology[1]/@sockets)",
ctxt, &ul);
if (ret < 0) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Missing 'sockets' attribute in CPU topology"));
goto error;
}
def->sockets = (unsigned int) ul;
ret = virXPathULong("string(./topology[1]/@cores)",
ctxt, &ul);
if (ret < 0) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Missing 'cores' attribute in CPU topology"));
goto error;
}
def->cores = (unsigned int) ul;
ret = virXPathULong("string(./topology[1]/@threads)",
ctxt, &ul);
if (ret < 0) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Missing 'threads' attribute in CPU topology"));
goto error;
}
def->threads = (unsigned int) ul;
if (!def->sockets || !def->cores || !def->threads) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Invalid CPU topology"));
goto error;
}
}
n = virXPathNodeSet("./feature", ctxt, &nodes);
if (n < 0)
goto error;
if (n > 0) {
if (!def->model) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Non-empty feature list specified without CPU model"));
goto error;
}
if (VIR_ALLOC_N(def->features, n) < 0)
goto no_memory;
def->nfeatures = n;
}
for (i = 0 ; i < n ; i++) {
char *name;
int policy; /* enum virDomainCPUFeaturePolicy */
unsigned int j;
if (def->type == VIR_CPU_TYPE_GUEST) {
char *strpolicy;
strpolicy = virXMLPropString(nodes[i], "policy");
policy = virCPUFeaturePolicyTypeFromString(strpolicy);
VIR_FREE(strpolicy);
if (policy < 0) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Invalid CPU feature policy"));
goto error;
}
}
else
policy = -1;
if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
VIR_FREE(name);
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Invalid CPU feature name"));
goto error;
}
for (j = 0 ; j < i ; j++) {
if (STREQ(name, def->features[j].name)) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
_("CPU feature `%s' specified more than once"),
name);
VIR_FREE(name);
goto error;
}
}
def->features[i].name = name;
def->features[i].policy = policy;
}
cleanup:
VIR_FREE(nodes);
return def;
no_memory:
virReportOOMError();
error:
virCPUDefFree(def);
def = NULL;
goto cleanup;
}
#endif
char *
virCPUDefFormat(virCPUDefPtr def,
const char *indent,
int flags)
{
virBuffer buf = VIR_BUFFER_INITIALIZER;
if (virCPUDefFormatBuf(&buf, def, indent, flags) < 0)
goto cleanup;
if (virBufferError(&buf))
goto no_memory;
return virBufferContentAndReset(&buf);
no_memory:
virReportOOMError();
cleanup:
virBufferFreeAndReset(&buf);
return NULL;
}
int
virCPUDefFormatBuf(virBufferPtr buf,
virCPUDefPtr def,
const char *indent,
int flags)
{
unsigned int i;
if (!def)
return 0;
if (indent == NULL)
indent = "";
if (!def->model && def->nfeatures) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Non-empty feature list specified without CPU model"));
return -1;
}
if (!(flags & VIR_CPU_FORMAT_EMBEDED)) {
if (def->type == VIR_CPU_TYPE_GUEST && def->model) {
const char *match;
if (!(match = virCPUMatchTypeToString(def->match))) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
_("Unexpected CPU match policy %d"), def->match);
return -1;
}
virBufferVSprintf(buf, "%s<cpu match='%s'>\n", indent, match);
}
else
virBufferVSprintf(buf, "%s<cpu>\n", indent);
if (def->arch)
virBufferVSprintf(buf, "%s <arch>%s</arch>\n", indent, def->arch);
}
if (def->model)
virBufferVSprintf(buf, "%s <model>%s</model>\n", indent, def->model);
if (def->sockets && def->cores && def->threads) {
virBufferVSprintf(buf, "%s <topology", indent);
virBufferVSprintf(buf, " sockets='%u'", def->sockets);
virBufferVSprintf(buf, " cores='%u'", def->cores);
virBufferVSprintf(buf, " threads='%u'", def->threads);
virBufferAddLit(buf, "/>\n");
}
for (i = 0 ; i < def->nfeatures ; i++) {
virCPUFeatureDefPtr feature = def->features + i;
if (!feature->name) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Missing CPU feature name"));
return -1;
}
if (def->type == VIR_CPU_TYPE_GUEST) {
const char *policy;
policy = virCPUFeaturePolicyTypeToString(feature->policy);
if (!policy) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
_("Unexpected CPU feature policy %d"), feature->policy);
return -1;
}
virBufferVSprintf(buf, "%s <feature policy='%s' name='%s'/>\n",
indent, policy, feature->name);
}
else {
virBufferVSprintf(buf, "%s <feature name='%s'/>\n",
indent, feature->name);
}
}
if (!(flags & VIR_CPU_FORMAT_EMBEDED))
virBufferVSprintf(buf, "%s</cpu>\n", indent);
return 0;
}
int
virCPUDefAddFeature(virCPUDefPtr def,
const char *name,
int policy)
{
int i;
for (i = 0 ; i < def->nfeatures ; i++) {
if (STREQ(name, def->features[i].name)) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
_("CPU feature `%s' specified more than once"), name);
return -1;
}
}
if (VIR_REALLOC_N(def->features, def->nfeatures + 1) < 0)
goto no_memory;
if (def->type == VIR_CPU_TYPE_HOST)
policy = -1;
if (!(def->features[def->nfeatures].name = strdup(name)))
goto no_memory;
def->features[def->nfeatures].policy = policy;
def->nfeatures++;
return 0;
no_memory:
virReportOOMError();
return -1;
}