2009-12-18 13:44:55 +00:00
|
|
|
/*
|
|
|
|
* cpu_conf.h: CPU XML handling
|
|
|
|
*
|
2010-01-12 14:25:36 +00:00
|
|
|
* Copyright (C) 2009, 2010 Red Hat, Inc.
|
2009-12-18 13:44:55 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2010-03-01 23:38:28 +00:00
|
|
|
#define virCPUReportError(code, ...) \
|
2010-02-10 12:22:36 +00:00
|
|
|
virReportErrorHelper(NULL, VIR_FROM_CPU, code, __FILE__, \
|
2010-03-01 23:38:28 +00:00
|
|
|
__FUNCTION__, __LINE__, __VA_ARGS__)
|
2009-12-18 13:44:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-23 08:34:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-18 13:44:55 +00:00
|
|
|
#ifndef PROXY
|
|
|
|
virCPUDefPtr
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUDefParseXML(const xmlNodePtr node,
|
2009-12-18 13:44:55 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
enum virCPUType mode)
|
|
|
|
{
|
|
|
|
virCPUDefPtr def;
|
|
|
|
xmlNodePtr *nodes = NULL;
|
|
|
|
int n;
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-03-31 23:02:57 +00:00
|
|
|
if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
|
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s",
|
|
|
|
_("XML does not contain expected 'cpu' element"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-18 13:44:55 +00:00
|
|
|
if (VIR_ALLOC(def) < 0) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-12-18 13:44:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-12 14:25:36 +00:00
|
|
|
if (mode == VIR_CPU_TYPE_AUTO) {
|
2010-02-04 21:52:34 +00:00
|
|
|
if (virXPathBoolean("boolean(./arch)", ctxt))
|
2010-01-12 14:25:36 +00:00
|
|
|
def->type = VIR_CPU_TYPE_HOST;
|
|
|
|
else
|
|
|
|
def->type = VIR_CPU_TYPE_GUEST;
|
|
|
|
} else
|
2009-12-18 13:44:55 +00:00
|
|
|
def->type = mode;
|
|
|
|
|
|
|
|
if (def->type == VIR_CPU_TYPE_GUEST) {
|
2010-01-12 14:25:36 +00:00
|
|
|
char *match = virXMLPropString(node, "match");
|
|
|
|
|
|
|
|
if (!match) {
|
2010-02-04 21:52:34 +00:00
|
|
|
if (virXPathBoolean("boolean(./model)", ctxt)) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-12 14:25:36 +00:00
|
|
|
"%s", _("Missing match attribute for CPU specification"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
def->match = -1;
|
|
|
|
} else {
|
|
|
|
def->match = virCPUMatchTypeFromString(match);
|
|
|
|
VIR_FREE(match);
|
|
|
|
|
|
|
|
if (def->match < 0) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-12 14:25:36 +00:00
|
|
|
"%s", _("Invalid match attribute for CPU specification"));
|
|
|
|
goto error;
|
|
|
|
}
|
2009-12-18 13:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (def->type == VIR_CPU_TYPE_HOST) {
|
2010-02-04 21:52:34 +00:00
|
|
|
def->arch = virXPathString("string(./arch[1])", ctxt);
|
2009-12-18 13:44:55 +00:00
|
|
|
if (!def->arch) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
"%s", _("Missing CPU architecture"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-04 21:52:34 +00:00
|
|
|
if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
|
2010-01-12 14:25:36 +00:00
|
|
|
def->type == VIR_CPU_TYPE_HOST) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
"%s", _("Missing CPU model name"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2010-02-04 21:52:34 +00:00
|
|
|
if (virXPathNode("./topology[1]", ctxt)) {
|
2009-12-18 13:44:55 +00:00
|
|
|
int ret;
|
|
|
|
unsigned long ul;
|
|
|
|
|
2010-02-04 21:52:34 +00:00
|
|
|
ret = virXPathULong("string(./topology[1]/@sockets)",
|
2009-12-18 13:44:55 +00:00
|
|
|
ctxt, &ul);
|
|
|
|
if (ret < 0) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
"%s", _("Missing 'sockets' attribute in CPU topology"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
def->sockets = (unsigned int) ul;
|
|
|
|
|
2010-02-04 21:52:34 +00:00
|
|
|
ret = virXPathULong("string(./topology[1]/@cores)",
|
2009-12-18 13:44:55 +00:00
|
|
|
ctxt, &ul);
|
|
|
|
if (ret < 0) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
"%s", _("Missing 'cores' attribute in CPU topology"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
def->cores = (unsigned int) ul;
|
|
|
|
|
2010-02-04 21:52:34 +00:00
|
|
|
ret = virXPathULong("string(./topology[1]/@threads)",
|
2009-12-18 13:44:55 +00:00
|
|
|
ctxt, &ul);
|
|
|
|
if (ret < 0) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
"%s", _("Missing 'threads' attribute in CPU topology"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
def->threads = (unsigned int) ul;
|
|
|
|
|
|
|
|
if (!def->sockets || !def->cores || !def->threads) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
"%s", _("Invalid CPU topology"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-04 21:52:34 +00:00
|
|
|
n = virXPathNodeSet("./feature", ctxt, &nodes);
|
2009-12-18 13:44:55 +00:00
|
|
|
if (n < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (n > 0) {
|
2010-01-12 14:25:36 +00:00
|
|
|
if (!def->model) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-12 14:25:36 +00:00
|
|
|
"%s", _("Non-empty feature list specified without CPU model"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2009-12-18 13:44:55 +00:00
|
|
|
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) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
"%s", _("Invalid CPU feature policy"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
policy = -1;
|
|
|
|
|
|
|
|
if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
|
|
|
|
VIR_FREE(name);
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
"%s", _("Invalid CPU feature name"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0 ; j < i ; j++) {
|
|
|
|
if (STREQ(name, def->features[j].name)) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
_("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:
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-12-18 13:44:55 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
virCPUDefFree(def);
|
|
|
|
def = NULL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUDefFormat(virCPUDefPtr def,
|
2009-12-18 13:44:55 +00:00
|
|
|
const char *indent,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
|
2010-02-10 12:22:36 +00:00
|
|
|
if (virCPUDefFormatBuf(&buf, def, indent, flags) < 0)
|
2009-12-18 13:44:55 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virBufferError(&buf))
|
|
|
|
goto no_memory;
|
|
|
|
|
|
|
|
return virBufferContentAndReset(&buf);
|
|
|
|
|
|
|
|
no_memory:
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-12-18 13:44:55 +00:00
|
|
|
cleanup:
|
2010-01-30 16:27:05 +00:00
|
|
|
virBufferFreeAndReset(&buf);
|
2009-12-18 13:44:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUDefFormatBuf(virBufferPtr buf,
|
2009-12-18 13:44:55 +00:00
|
|
|
virCPUDefPtr def,
|
|
|
|
const char *indent,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!def)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (indent == NULL)
|
|
|
|
indent = "";
|
|
|
|
|
2010-01-12 14:25:36 +00:00
|
|
|
if (!def->model && def->nfeatures) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-12 14:25:36 +00:00
|
|
|
"%s", _("Non-empty feature list specified without CPU model"));
|
2009-12-18 13:44:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & VIR_CPU_FORMAT_EMBEDED)) {
|
2010-01-12 14:25:36 +00:00
|
|
|
if (def->type == VIR_CPU_TYPE_GUEST && def->model) {
|
2009-12-18 13:44:55 +00:00
|
|
|
const char *match;
|
|
|
|
if (!(match = virCPUMatchTypeToString(def->match))) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
_("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);
|
|
|
|
}
|
|
|
|
|
2010-01-12 14:25:36 +00:00
|
|
|
if (def->model)
|
|
|
|
virBufferVSprintf(buf, "%s <model>%s</model>\n", indent, def->model);
|
2009-12-18 13:44:55 +00:00
|
|
|
|
|
|
|
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) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
"%s", _("Missing CPU feature name"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (def->type == VIR_CPU_TYPE_GUEST) {
|
|
|
|
const char *policy;
|
|
|
|
|
|
|
|
policy = virCPUFeaturePolicyTypeToString(feature->policy);
|
|
|
|
if (!policy) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
_("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
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUDefAddFeature(virCPUDefPtr def,
|
2009-12-18 13:44:55 +00:00
|
|
|
const char *name,
|
|
|
|
int policy)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0 ; i < def->nfeatures ; i++) {
|
|
|
|
if (STREQ(name, def->features[i].name)) {
|
2010-02-10 12:22:36 +00:00
|
|
|
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-18 13:44:55 +00:00
|
|
|
_("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:
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-12-18 13:44:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|