cpu_conf.c 11.9 KB
Newer Older
1 2 3
/*
 * cpu_conf.h: CPU XML handling
 *
4
 * Copyright (C) 2009, 2010 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * 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

34
#define virCPUReportError(code, ...)                              \
35
    virReportErrorHelper(NULL, VIR_FROM_CPU, code, __FILE__,      \
36
                         __FUNCTION__, __LINE__, __VA_ARGS__)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

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);
J
Jiri Denemark 已提交
61
    VIR_FREE(def->vendor);
62 63 64 65 66 67 68 69 70

    for (i = 0 ; i < def->nfeatures ; i++)
        VIR_FREE(def->features[i].name);
    VIR_FREE(def->features);

    VIR_FREE(def);
}


71 72 73 74 75 76 77 78 79 80 81 82
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)))
J
Jiri Denemark 已提交
83
        || (cpu->vendor && !(copy->vendor = strdup(cpu->vendor)))
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        || 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;
}


109 110
#ifndef PROXY
virCPUDefPtr
111
virCPUDefParseXML(const xmlNodePtr node,
112 113 114 115 116 117 118 119
                  xmlXPathContextPtr ctxt,
                  enum virCPUType mode)
{
    virCPUDefPtr def;
    xmlNodePtr *nodes = NULL;
    int n;
    unsigned int i;

120 121 122 123 124 125 126
    if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                          "%s",
                          _("XML does not contain expected 'cpu' element"));
        return NULL;
    }

127
    if (VIR_ALLOC(def) < 0) {
128
        virReportOOMError();
129 130 131
        return NULL;
    }

132
    if (mode == VIR_CPU_TYPE_AUTO) {
133
        if (virXPathBoolean("boolean(./arch)", ctxt))
134 135 136 137
            def->type = VIR_CPU_TYPE_HOST;
        else
            def->type = VIR_CPU_TYPE_GUEST;
    } else
138 139 140
        def->type = mode;

    if (def->type == VIR_CPU_TYPE_GUEST) {
141 142 143
        char *match = virXMLPropString(node, "match");

        if (!match) {
144
            if (virXPathBoolean("boolean(./model)", ctxt)) {
145
                virCPUReportError(VIR_ERR_INTERNAL_ERROR,
146 147 148 149 150 151 152 153 154
                        "%s", _("Missing match attribute for CPU specification"));
                goto error;
            }
            def->match = -1;
        } else {
            def->match = virCPUMatchTypeFromString(match);
            VIR_FREE(match);

            if (def->match < 0) {
155
                virCPUReportError(VIR_ERR_INTERNAL_ERROR,
156 157 158
                        "%s", _("Invalid match attribute for CPU specification"));
                goto error;
            }
159 160 161 162
        }
    }

    if (def->type == VIR_CPU_TYPE_HOST) {
163
        def->arch = virXPathString("string(./arch[1])", ctxt);
164
        if (!def->arch) {
165
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
166 167 168 169 170
                    "%s", _("Missing CPU architecture"));
            goto error;
        }
    }

171
    if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
172
        def->type == VIR_CPU_TYPE_HOST) {
173
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
174 175 176 177
                "%s", _("Missing CPU model name"));
        goto error;
    }

J
Jiri Denemark 已提交
178 179 180 181 182 183 184
    def->vendor = virXPathString("string(./vendor[1])", ctxt);
    if (def->vendor && !def->model) {
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                "%s", _("CPU vendor specified without CPU model"));
        goto error;
    }

185
    if (virXPathNode("./topology[1]", ctxt)) {
186 187 188
        int ret;
        unsigned long ul;

189
        ret = virXPathULong("string(./topology[1]/@sockets)",
190 191
                            ctxt, &ul);
        if (ret < 0) {
192
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
193 194 195 196 197
                    "%s", _("Missing 'sockets' attribute in CPU topology"));
            goto error;
        }
        def->sockets = (unsigned int) ul;

198
        ret = virXPathULong("string(./topology[1]/@cores)",
199 200
                            ctxt, &ul);
        if (ret < 0) {
201
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
202 203 204 205 206
                    "%s", _("Missing 'cores' attribute in CPU topology"));
            goto error;
        }
        def->cores = (unsigned int) ul;

207
        ret = virXPathULong("string(./topology[1]/@threads)",
208 209
                            ctxt, &ul);
        if (ret < 0) {
210
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
211 212 213 214 215 216
                    "%s", _("Missing 'threads' attribute in CPU topology"));
            goto error;
        }
        def->threads = (unsigned int) ul;

        if (!def->sockets || !def->cores || !def->threads) {
217
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
218 219 220 221 222
                    "%s", _("Invalid CPU topology"));
            goto error;
        }
    }

223
    n = virXPathNodeSet("./feature", ctxt, &nodes);
224 225 226 227
    if (n < 0)
        goto error;

    if (n > 0) {
228
        if (!def->model) {
229
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
230 231 232 233
                    "%s", _("Non-empty feature list specified without CPU model"));
            goto error;
        }

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        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) {
252
                virCPUReportError(VIR_ERR_INTERNAL_ERROR,
253 254 255 256 257 258 259 260 261
                        "%s", _("Invalid CPU feature policy"));
                goto error;
            }
        }
        else
            policy = -1;

        if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
            VIR_FREE(name);
262
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
263 264 265 266 267 268
                    "%s", _("Invalid CPU feature name"));
            goto error;
        }

        for (j = 0 ; j < i ; j++) {
            if (STREQ(name, def->features[j].name)) {
269
                virCPUReportError(VIR_ERR_INTERNAL_ERROR,
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
                        _("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:
287
    virReportOOMError();
288 289 290 291 292 293 294 295 296 297

error:
    virCPUDefFree(def);
    def = NULL;
    goto cleanup;
}
#endif


char *
298
virCPUDefFormat(virCPUDefPtr def,
299 300 301 302 303
                const char *indent,
                int flags)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

304
    if (virCPUDefFormatBuf(&buf, def, indent, flags) < 0)
305 306 307 308 309 310 311 312
        goto cleanup;

    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

no_memory:
313
    virReportOOMError();
314
cleanup:
315
    virBufferFreeAndReset(&buf);
316 317 318 319 320
    return NULL;
}


int
321
virCPUDefFormatBuf(virBufferPtr buf,
322 323 324 325 326 327 328 329 330 331 332 333
                   virCPUDefPtr def,
                   const char *indent,
                   int flags)
{
    unsigned int i;

    if (!def)
        return 0;

    if (indent == NULL)
        indent = "";

334
    if (!def->model && def->nfeatures) {
335
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
336
                "%s", _("Non-empty feature list specified without CPU model"));
337 338 339 340
        return -1;
    }

    if (!(flags & VIR_CPU_FORMAT_EMBEDED)) {
341
        if (def->type == VIR_CPU_TYPE_GUEST && def->model) {
342 343
            const char *match;
            if (!(match = virCPUMatchTypeToString(def->match))) {
344
                virCPUReportError(VIR_ERR_INTERNAL_ERROR,
345 346 347 348 349 350 351 352 353 354 355 356 357
                        _("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);
    }

358 359
    if (def->model)
        virBufferVSprintf(buf, "%s  <model>%s</model>\n", indent, def->model);
360

J
Jiri Denemark 已提交
361 362 363 364 365
    if (def->vendor) {
        virBufferVSprintf(buf, "%s  <vendor>%s</vendor>\n",
                          indent, def->vendor);
    }

366 367 368 369 370 371 372 373 374 375 376 377
    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) {
378
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
379 380 381 382 383 384 385 386 387
                    "%s", _("Missing CPU feature name"));
            return -1;
        }

        if (def->type == VIR_CPU_TYPE_GUEST) {
            const char *policy;

            policy = virCPUFeaturePolicyTypeToString(feature->policy);
            if (!policy) {
388
                virCPUReportError(VIR_ERR_INTERNAL_ERROR,
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
                        _("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
409
virCPUDefAddFeature(virCPUDefPtr def,
410 411 412 413 414 415 416
                    const char *name,
                    int policy)
{
    int i;

    for (i = 0 ; i < def->nfeatures ; i++) {
        if (STREQ(name, def->features[i].name)) {
417
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
                    _("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:
438
    virReportOOMError();
439 440
    return -1;
}