cpu_conf.c 24.0 KB
Newer Older
1 2 3
/*
 * cpu_conf.h: CPU XML handling
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2009-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25
 *
 * Authors:
 *      Jiri Denemark <jdenemar@redhat.com>
 */

#include <config.h>

26
#include "virerror.h"
27
#include "viralloc.h"
28
#include "virbuffer.h"
29
#include "cpu_conf.h"
30
#include "domain_conf.h"
31
#include "virstring.h"
32 33 34

#define VIR_FROM_THIS VIR_FROM_CPU

35 36 37
VIR_ENUM_IMPL(virCPU, VIR_CPU_TYPE_LAST,
              "host", "guest", "auto")

38 39 40 41 42
VIR_ENUM_IMPL(virCPUMode, VIR_CPU_MODE_LAST,
              "custom",
              "host-model",
              "host-passthrough")

43 44 45 46 47
VIR_ENUM_IMPL(virCPUMatch, VIR_CPU_MATCH_LAST,
              "minimum",
              "exact",
              "strict")

48 49 50 51
VIR_ENUM_IMPL(virCPUFallback, VIR_CPU_FALLBACK_LAST,
              "allow",
              "forbid")

52 53 54 55 56 57 58 59
VIR_ENUM_IMPL(virCPUFeaturePolicy, VIR_CPU_FEATURE_LAST,
              "force",
              "require",
              "optional",
              "disable",
              "forbid")


60 61 62 63 64 65 66
void ATTRIBUTE_NONNULL(1)
virCPUDefFreeModel(virCPUDefPtr def)
{
    unsigned int i;

    VIR_FREE(def->model);
    VIR_FREE(def->vendor);
67
    VIR_FREE(def->vendor_id);
68 69 70 71 72 73

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

74 75 76 77 78 79 80 81
void
virCPUDefFree(virCPUDefPtr def)
{
    unsigned int i;

    if (!def)
        return;

82
    virCPUDefFreeModel(def);
83

84
    for (i = 0; i < def->ncells; i++) {
85
        virBitmapFree(def->cells[i].cpumask);
86 87 88
        VIR_FREE(def->cells[i].cpustr);
    }
    VIR_FREE(def->cells);
89
    VIR_FREE(def->vendor_id);
90

91 92 93 94
    VIR_FREE(def);
}


95 96 97 98 99 100 101
int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
virCPUDefCopyModel(virCPUDefPtr dst,
                   const virCPUDefPtr src,
                   bool resetPolicy)
{
    unsigned int i;

102 103 104 105
    if (VIR_STRDUP(dst->model, src->model) < 0 ||
        VIR_STRDUP(dst->vendor, src->vendor) < 0 ||
        VIR_STRDUP(dst->vendor_id, src->vendor_id) < 0 ||
        VIR_ALLOC_N(dst->features, src->nfeatures) < 0)
106
        return -1;
107 108 109 110 111 112 113 114 115 116 117 118 119 120
    dst->nfeatures_max = dst->nfeatures = src->nfeatures;

    for (i = 0; i < dst->nfeatures; i++) {
        if (dst->type != src->type && resetPolicy) {
            if (dst->type == VIR_CPU_TYPE_HOST)
                dst->features[i].policy = -1;
            else if (src->features[i].policy == -1)
                dst->features[i].policy = VIR_CPU_FEATURE_REQUIRE;
            else
                dst->features[i].policy = src->features[i].policy;
        } else {
            dst->features[i].policy = src->features[i].policy;
        }

121 122
        if (VIR_STRDUP(dst->features[i].name, src->features[i].name) < 0)
            return -1;
123 124 125 126 127
    }

    return 0;
}

128 129 130 131 132 133
virCPUDefPtr
virCPUDefCopy(const virCPUDefPtr cpu)
{
    virCPUDefPtr copy;
    unsigned int i;

134
    if (!cpu || VIR_ALLOC(copy) < 0)
135 136 137
        return NULL;

    copy->type = cpu->type;
138
    copy->mode = cpu->mode;
139
    copy->match = cpu->match;
140
    copy->fallback = cpu->fallback;
141 142 143
    copy->sockets = cpu->sockets;
    copy->cores = cpu->cores;
    copy->threads = cpu->threads;
144
    copy->arch = cpu->arch;
145 146 147

    if (virCPUDefCopyModel(copy, cpu, false) < 0)
        goto error;
148 149 150

    if (cpu->ncells) {
        if (VIR_ALLOC_N(copy->cells, cpu->ncells) < 0)
151
            goto error;
152 153 154 155 156 157
        copy->ncells_max = copy->ncells = cpu->ncells;

        for (i = 0; i < cpu->ncells; i++) {
            copy->cells[i].cellid = cpu->cells[i].cellid;
            copy->cells[i].mem = cpu->cells[i].mem;

158 159 160
            copy->cells[i].cpumask = virBitmapNewCopy(cpu->cells[i].cpumask);

            if (!copy->cells[i].cpumask)
161
                goto error;
162

163 164
            if (VIR_STRDUP(copy->cells[i].cpustr, cpu->cells[i].cpustr) < 0)
                goto error;
165 166 167
        }
        copy->cells_cpus = cpu->cells_cpus;
    }
168 169 170

    return copy;

171
error:
172 173 174 175
    virCPUDefFree(copy);
    return NULL;
}

176
virCPUDefPtr
177
virCPUDefParseXML(const xmlNodePtr node,
178 179 180 181 182 183 184
                  xmlXPathContextPtr ctxt,
                  enum virCPUType mode)
{
    virCPUDefPtr def;
    xmlNodePtr *nodes = NULL;
    int n;
    unsigned int i;
185
    char *cpuMode;
186 187
    char *fallback = NULL;
    char *vendor_id = NULL;
188

189
    if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
190 191 192
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s",
                       _("XML does not contain expected 'cpu' element"));
193 194 195
        return NULL;
    }

196
    if (VIR_ALLOC(def) < 0)
197 198
        return NULL;

199
    if (mode == VIR_CPU_TYPE_AUTO) {
200 201
        if (virXPathBoolean("boolean(./arch)", ctxt)) {
            if (virXPathBoolean("boolean(./@match)", ctxt)) {
202 203 204
                virReportError(VIR_ERR_XML_ERROR, "%s",
                               _("'arch' element element cannot be used inside 'cpu'"
                                 " element with 'match' attribute'"));
205 206
                goto error;
            }
207
            def->type = VIR_CPU_TYPE_HOST;
208
        } else {
209
            def->type = VIR_CPU_TYPE_GUEST;
210 211
        }
    } else {
212
        def->type = mode;
213 214 215 216 217
    }

    if ((cpuMode = virXMLPropString(node, "mode"))) {
        if (def->type == VIR_CPU_TYPE_HOST) {
            VIR_FREE(cpuMode);
218 219
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Attribute mode is only allowed for guest CPU"));
220 221 222 223 224
            goto error;
        } else {
            def->mode = virCPUModeTypeFromString(cpuMode);

            if (def->mode < 0) {
225 226 227
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Invalid mode attribute '%s'"),
                               cpuMode);
228
                VIR_FREE(cpuMode);
229 230
                goto error;
            }
231
            VIR_FREE(cpuMode);
232 233 234 235 236 237 238
        }
    } else {
        if (def->type == VIR_CPU_TYPE_HOST)
            def->mode = -1;
        else
            def->mode = VIR_CPU_MODE_CUSTOM;
    }
239 240

    if (def->type == VIR_CPU_TYPE_GUEST) {
241 242 243
        char *match = virXMLPropString(node, "match");

        if (!match) {
244 245 246 247
            if (virXPathBoolean("boolean(./model)", ctxt))
                def->match = VIR_CPU_MATCH_EXACT;
            else
                def->match = -1;
248 249 250 251 252
        } else {
            def->match = virCPUMatchTypeFromString(match);
            VIR_FREE(match);

            if (def->match < 0) {
P
Peter Krempa 已提交
253 254 255
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("Invalid match attribute for CPU "
                                 "specification"));
256 257
                goto error;
            }
258 259 260 261
        }
    }

    if (def->type == VIR_CPU_TYPE_HOST) {
262 263
        char *arch = virXPathString("string(./arch[1])", ctxt);
        if (!arch) {
264 265
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Missing CPU architecture"));
266 267
            goto error;
        }
268 269 270 271 272 273 274
        if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown architecture %s"), arch);
            VIR_FREE(arch);
            goto error;
        }
        VIR_FREE(arch);
275 276
    }

277
    if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
278
        def->type == VIR_CPU_TYPE_HOST) {
279 280
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU model name"));
281 282 283
        goto error;
    }

284
    if (def->type == VIR_CPU_TYPE_GUEST &&
285 286
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {

287 288 289 290 291
        if ((fallback = virXPathString("string(./model[1]/@fallback)", ctxt))) {
            if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
                virReportError(VIR_ERR_XML_ERROR, "%s",
                               _("Invalid fallback attribute"));
                goto error;
292
            }
K
Ken ICHIKAWA 已提交
293
        }
294

295 296 297
        if ((vendor_id = virXPathString("string(./model[1]/@vendor_id)",
                                        ctxt))) {
            if (strlen(vendor_id) != VIR_CPU_VENDOR_ID_LENGTH) {
K
Ken ICHIKAWA 已提交
298
                virReportError(VIR_ERR_XML_ERROR,
299
                               _("vendor_id must be exactly %d characters long"),
K
Ken ICHIKAWA 已提交
300 301 302
                               VIR_CPU_VENDOR_ID_LENGTH);
                goto error;
            }
303

K
Ken ICHIKAWA 已提交
304
            /* ensure that the string can be passed to qemu*/
305
            if (strchr(vendor_id, ',')) {
K
Ken ICHIKAWA 已提交
306 307
                    virReportError(VIR_ERR_XML_ERROR, "%s",
                                   _("vendor id is invalid"));
308
                    goto error;
309
            }
310

K
Ken ICHIKAWA 已提交
311
            def->vendor_id = vendor_id;
312
            vendor_id = NULL;
313 314 315
        }
    }

J
Jiri Denemark 已提交
316 317
    def->vendor = virXPathString("string(./vendor[1])", ctxt);
    if (def->vendor && !def->model) {
318 319
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("CPU vendor specified without CPU model"));
J
Jiri Denemark 已提交
320 321 322
        goto error;
    }

323
    if (virXPathNode("./topology[1]", ctxt)) {
324 325 326
        int ret;
        unsigned long ul;

327
        ret = virXPathULong("string(./topology[1]/@sockets)",
328 329
                            ctxt, &ul);
        if (ret < 0) {
P
Peter Krempa 已提交
330 331
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing 'sockets' attribute in CPU topology"));
332 333 334 335
            goto error;
        }
        def->sockets = (unsigned int) ul;

336
        ret = virXPathULong("string(./topology[1]/@cores)",
337 338
                            ctxt, &ul);
        if (ret < 0) {
P
Peter Krempa 已提交
339 340
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing 'cores' attribute in CPU topology"));
341 342 343 344
            goto error;
        }
        def->cores = (unsigned int) ul;

345
        ret = virXPathULong("string(./topology[1]/@threads)",
346 347
                            ctxt, &ul);
        if (ret < 0) {
P
Peter Krempa 已提交
348 349
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing 'threads' attribute in CPU topology"));
350 351 352 353 354
            goto error;
        }
        def->threads = (unsigned int) ul;

        if (!def->sockets || !def->cores || !def->threads) {
355 356
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Invalid CPU topology"));
357 358 359 360
            goto error;
        }
    }

361
    n = virXPathNodeSet("./feature", ctxt, &nodes);
362 363 364 365
    if (n < 0)
        goto error;

    if (n > 0) {
366
        if (!def->model) {
P
Peter Krempa 已提交
367 368 369
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Non-empty feature list specified without "
                             "CPU model"));
370 371 372
            goto error;
        }

E
Eric Blake 已提交
373 374
        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                         def->nfeatures, n) < 0)
375
            goto error;
376 377 378
        def->nfeatures = n;
    }

379
    for (i = 0; i < n; i++) {
380 381 382 383 384 385 386 387
        char *name;
        int policy; /* enum virDomainCPUFeaturePolicy */
        unsigned int j;

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

            strpolicy = virXMLPropString(nodes[i], "policy");
388 389 390 391
            if (strpolicy == NULL)
                policy = VIR_CPU_FEATURE_REQUIRE;
            else
                policy = virCPUFeaturePolicyTypeFromString(strpolicy);
392 393 394
            VIR_FREE(strpolicy);

            if (policy < 0) {
395 396
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Invalid CPU feature policy"));
397 398
                goto error;
            }
399
        } else {
400
            policy = -1;
401
        }
402 403 404

        if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
            VIR_FREE(name);
405 406
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Invalid CPU feature name"));
407 408 409
            goto error;
        }

410
        for (j = 0; j < i; j++) {
411
            if (STREQ(name, def->features[j].name)) {
412 413 414
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("CPU feature `%s' specified more than once"),
                               name);
415 416 417 418 419 420 421 422 423
                VIR_FREE(name);
                goto error;
            }
        }

        def->features[i].name = name;
        def->features[i].policy = policy;
    }

424 425 426 427
    if (virXPathNode("./numa[1]", ctxt)) {
        VIR_FREE(nodes);
        n = virXPathNodeSet("./numa[1]/cell", ctxt, &nodes);
        if (n <= 0) {
428 429
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("NUMA topology defined without NUMA cells"));
430 431 432 433 434
            goto error;
        }

        if (VIR_RESIZE_N(def->cells, def->ncells_max,
                         def->ncells, n) < 0)
435
            goto error;
436 437 438

        def->ncells = n;

439
        for (i = 0; i < n; i++) {
440
            char *cpus, *memory;
441 442 443
            int ret, ncpus = 0;

            def->cells[i].cellid = i;
444
            cpus = virXMLPropString(nodes[i], "cpus");
445
            if (!cpus) {
446 447
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Missing 'cpus' attribute in NUMA cell"));
448 449
                goto error;
            }
450
            def->cells[i].cpustr = cpus;
451

452 453
            ncpus = virBitmapParse(cpus, 0, &def->cells[i].cpumask,
                                   VIR_DOMAIN_CPUMASK_LEN);
454
            if (ncpus <= 0)
455 456 457 458 459
                goto error;
            def->cells_cpus += ncpus;

            memory = virXMLPropString(nodes[i], "memory");
            if (!memory) {
460 461
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Missing 'memory' attribute in NUMA cell"));
462 463 464 465 466
                goto error;
            }

            ret  = virStrToLong_ui(memory, NULL, 10, &def->cells[i].mem);
            if (ret == -1) {
467 468
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Invalid 'memory' attribute in NUMA cell"));
469 470 471 472 473 474 475
                VIR_FREE(memory);
                goto error;
            }
            VIR_FREE(memory);
        }
    }

476
cleanup:
477 478
    VIR_FREE(fallback);
    VIR_FREE(vendor_id);
479 480 481 482 483 484 485 486 487 488 489
    VIR_FREE(nodes);
    return def;

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


char *
490 491
virCPUDefFormat(virCPUDefPtr def,
                unsigned int flags)
492 493 494
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

495
    if (virCPUDefFormatBufFull(&buf, def, flags) < 0)
496 497 498 499 500 501 502 503
        goto cleanup;

    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

no_memory:
504
    virReportOOMError();
505
cleanup:
506
    virBufferFreeAndReset(&buf);
507 508 509 510
    return NULL;
}


511 512
int
virCPUDefFormatBufFull(virBufferPtr buf,
513 514
                       virCPUDefPtr def,
                       unsigned int flags)
515 516 517 518
{
    if (!def)
        return 0;

519 520 521 522 523 524
    virBufferAddLit(buf, "<cpu");
    if (def->type == VIR_CPU_TYPE_GUEST) {
        const char *tmp;

        if (def->mode != VIR_CPU_MODE_CUSTOM || def->model) {
            if (!(tmp = virCPUModeTypeToString(def->mode))) {
525 526
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU mode %d"), def->mode);
527 528 529
                return -1;
            }
            virBufferAsprintf(buf, " mode='%s'", tmp);
530 531
        }

532 533 534 535
        if (def->model &&
            (def->mode == VIR_CPU_MODE_CUSTOM ||
             (flags & VIR_DOMAIN_XML_UPDATE_CPU))) {
            if (!(tmp = virCPUMatchTypeToString(def->match))) {
536 537 538
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU match policy %d"),
                               def->match);
539 540 541 542
                return -1;
            }
            virBufferAsprintf(buf, " match='%s'", tmp);
        }
543
    }
544
    virBufferAddLit(buf, ">\n");
545 546

    if (def->arch)
547 548
        virBufferAsprintf(buf, "  <arch>%s</arch>\n",
                          virArchToString(def->arch));
549 550

    virBufferAdjustIndent(buf, 2);
551
    if (virCPUDefFormatBuf(buf, def, flags) < 0)
552 553 554 555 556 557 558 559
        return -1;
    virBufferAdjustIndent(buf, -2);

    virBufferAddLit(buf, "</cpu>\n");

    return 0;
}

560
int
561
virCPUDefFormatBuf(virBufferPtr buf,
562 563
                   virCPUDefPtr def,
                   unsigned int flags)
564 565
{
    unsigned int i;
566 567
    bool formatModel;
    bool formatFallback;
568 569 570 571

    if (!def)
        return 0;

572 573 574 575 576 577
    formatModel = (def->mode == VIR_CPU_MODE_CUSTOM ||
                   (flags & VIR_DOMAIN_XML_UPDATE_CPU));
    formatFallback = (def->type == VIR_CPU_TYPE_GUEST &&
                      (def->mode == VIR_CPU_MODE_HOST_MODEL ||
                       (def->mode == VIR_CPU_MODE_CUSTOM && def->model)));

578
    if (!def->model && def->nfeatures) {
579 580
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Non-empty feature list specified without CPU model"));
581 582 583
        return -1;
    }

584
    if ((formatModel && def->model) || formatFallback) {
585
        virBufferAddLit(buf, "<model");
586
        if (formatFallback) {
587 588 589 590
            const char *fallback;

            fallback = virCPUFallbackTypeToString(def->fallback);
            if (!fallback) {
591 592 593
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU fallback value: %d"),
                               def->fallback);
594 595 596
                return -1;
            }
            virBufferAsprintf(buf, " fallback='%s'", fallback);
597 598
            if (def->vendor_id)
                virBufferAsprintf(buf, " vendor_id='%s'", def->vendor_id);
599
        }
600 601 602 603 604
        if (formatModel && def->model) {
            virBufferAsprintf(buf, ">%s</model>\n", def->model);
        } else {
            virBufferAddLit(buf, "/>\n");
        }
605
    }
606

607
    if (formatModel && def->vendor)
608
        virBufferAsprintf(buf, "<vendor>%s</vendor>\n", def->vendor);
J
Jiri Denemark 已提交
609

610
    if (def->sockets && def->cores && def->threads) {
611
        virBufferAddLit(buf, "<topology");
612 613 614
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
615 616 617
        virBufferAddLit(buf, "/>\n");
    }

618
    if (formatModel) {
619
        for (i = 0; i < def->nfeatures; i++) {
620
            virCPUFeatureDefPtr feature = def->features + i;
621

622
            if (!feature->name) {
623 624
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Missing CPU feature name"));
625 626
                return -1;
            }
627 628 629 630 631 632

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

                policy = virCPUFeaturePolicyTypeToString(feature->policy);
                if (!policy) {
633 634 635
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Unexpected CPU feature policy %d"),
                                   feature->policy);
636 637 638 639 640 641 642 643
                    return -1;
                }
                virBufferAsprintf(buf, "<feature policy='%s' name='%s'/>\n",
                                  policy, feature->name);
            } else {
                virBufferAsprintf(buf, "<feature name='%s'/>\n",
                                  feature->name);
            }
644 645 646
        }
    }

647 648 649 650 651 652 653 654 655 656
    if (def->ncells) {
        virBufferAddLit(buf, "<numa>\n");
        for (i = 0; i < def->ncells; i++) {
            virBufferAddLit(buf, "  <cell");
            virBufferAsprintf(buf, " cpus='%s'", def->cells[i].cpustr);
            virBufferAsprintf(buf, " memory='%d'", def->cells[i].mem);
            virBufferAddLit(buf, "/>\n");
        }
        virBufferAddLit(buf, "</numa>\n");
    }
657 658 659 660 661
    return 0;
}


int
662
virCPUDefAddFeature(virCPUDefPtr def,
663 664 665 666 667
                    const char *name,
                    int policy)
{
    int i;

668
    for (i = 0; i < def->nfeatures; i++) {
669
        if (STREQ(name, def->features[i].name)) {
670 671
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("CPU feature `%s' specified more than once"), name);
672 673 674 675
            return -1;
        }
    }

E
Eric Blake 已提交
676 677
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
678
        return -1;
679 680 681 682

    if (def->type == VIR_CPU_TYPE_HOST)
        policy = -1;

683 684
    if (VIR_STRDUP(def->features[def->nfeatures].name, name) < 0)
        return -1;
685 686 687 688 689 690

    def->features[def->nfeatures].policy = policy;
    def->nfeatures++;

    return 0;
}
691 692 693 694 695 696 697 698 699 700 701 702

bool
virCPUDefIsEqual(virCPUDefPtr src,
                 virCPUDefPtr dst)
{
    bool identical = false;
    int i;

    if (!src && !dst)
        return true;

    if ((src && !dst) || (!src && dst)) {
703 704
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Target CPU does not match source"));
705 706 707 708
        goto cleanup;
    }

    if (src->type != dst->type) {
709 710 711 712
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU type %s does not match source %s"),
                       virCPUTypeToString(dst->type),
                       virCPUTypeToString(src->type));
713 714 715
        goto cleanup;
    }

716
    if (src->mode != dst->mode) {
717 718 719 720
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU mode %s does not match source %s"),
                       virCPUModeTypeToString(dst->mode),
                       virCPUModeTypeToString(src->mode));
721 722 723
        goto cleanup;
    }

724
    if (src->arch != dst->arch) {
725 726
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU arch %s does not match source %s"),
727 728
                       virArchToString(dst->arch),
                       virArchToString(src->arch));
729 730 731 732
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(src->model, dst->model)) {
733 734 735
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU model %s does not match source %s"),
                       NULLSTR(dst->model), NULLSTR(src->model));
736 737 738 739
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(src->vendor, dst->vendor)) {
740 741 742
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU vendor %s does not match source %s"),
                       NULLSTR(dst->vendor), NULLSTR(src->vendor));
743 744 745
        goto cleanup;
    }

746
    if (STRNEQ_NULLABLE(src->vendor_id, dst->vendor_id)) {
747 748 749
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU model %s does not match source %s"),
                       NULLSTR(dst->vendor_id), NULLSTR(src->vendor_id));
750 751 752
        goto cleanup;
    }

753
    if (src->sockets != dst->sockets) {
754 755 756
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU sockets %d does not match source %d"),
                       dst->sockets, src->sockets);
757 758 759 760
        goto cleanup;
    }

    if (src->cores != dst->cores) {
761 762 763
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU cores %d does not match source %d"),
                       dst->cores, src->cores);
764 765 766 767
        goto cleanup;
    }

    if (src->threads != dst->threads) {
768 769 770
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU threads %d does not match source %d"),
                       dst->threads, src->threads);
771 772 773 774
        goto cleanup;
    }

    if (src->nfeatures != dst->nfeatures) {
775 776 777
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU feature count %zu does not match source %zu"),
                       dst->nfeatures, src->nfeatures);
778 779 780
        goto cleanup;
    }

781
    for (i = 0; i < src->nfeatures; i++) {
782
        if (STRNEQ(src->features[i].name, dst->features[i].name)) {
783 784 785
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Target CPU feature %s does not match source %s"),
                           dst->features[i].name, src->features[i].name);
786 787 788 789
            goto cleanup;
        }

        if (src->features[i].policy != dst->features[i].policy) {
790 791 792 793
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Target CPU feature policy %s does not match source %s"),
                           virCPUFeaturePolicyTypeToString(dst->features[i].policy),
                           virCPUFeaturePolicyTypeToString(src->features[i].policy));
794 795 796 797 798 799 800 801 802
            goto cleanup;
        }
    }

    identical = true;

cleanup:
    return identical;
}