cpu_conf.c 24.4 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 26 27 28 29 30
 *
 * 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"
31
#include "domain_conf.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 82
void
virCPUDefFree(virCPUDefPtr def)
{
    unsigned int i;

    if (!def)
        return;

    VIR_FREE(def->arch);
83
    virCPUDefFreeModel(def);
84

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

92 93 94 95
    VIR_FREE(def);
}


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

    if ((src->model && !(dst->model = strdup(src->model)))
        || (src->vendor && !(dst->vendor = strdup(src->vendor)))
105
        || (src->vendor_id && !(dst->vendor_id = strdup(src->vendor_id)))
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        || VIR_ALLOC_N(dst->features, src->nfeatures) < 0)
        goto no_memory;
    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;
        }

        if (!(dst->features[i].name = strdup(src->features[i].name)))
            goto no_memory;
    }

    return 0;

no_memory:
    virReportOOMError();
    return -1;
}

133 134 135 136 137 138 139 140 141
virCPUDefPtr
virCPUDefCopy(const virCPUDefPtr cpu)
{
    virCPUDefPtr copy;
    unsigned int i;

    if (!cpu)
        return NULL;

142
    if (VIR_ALLOC(copy) < 0)
143 144 145
        goto no_memory;

    copy->type = cpu->type;
146
    copy->mode = cpu->mode;
147
    copy->match = cpu->match;
148
    copy->fallback = cpu->fallback;
149 150 151 152
    copy->sockets = cpu->sockets;
    copy->cores = cpu->cores;
    copy->threads = cpu->threads;

153 154 155 156 157
    if (cpu->arch && !(copy->arch = strdup(cpu->arch)))
        goto no_memory;

    if (virCPUDefCopyModel(copy, cpu, false) < 0)
        goto error;
158 159 160 161 162 163 164 165 166 167

    if (cpu->ncells) {
        if (VIR_ALLOC_N(copy->cells, cpu->ncells) < 0)
            goto no_memory;
        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;

168 169 170
            copy->cells[i].cpumask = virBitmapNewCopy(cpu->cells[i].cpumask);

            if (!copy->cells[i].cpumask)
171 172 173 174 175 176 177
                goto no_memory;

            if (!(copy->cells[i].cpustr = strdup(cpu->cells[i].cpustr)))
                goto no_memory;
        }
        copy->cells_cpus = cpu->cells_cpus;
    }
178 179 180 181 182

    return copy;

no_memory:
    virReportOOMError();
183
error:
184 185 186 187
    virCPUDefFree(copy);
    return NULL;
}

188
virCPUDefPtr
189
virCPUDefParseXML(const xmlNodePtr node,
190 191 192 193 194 195 196
                  xmlXPathContextPtr ctxt,
                  enum virCPUType mode)
{
    virCPUDefPtr def;
    xmlNodePtr *nodes = NULL;
    int n;
    unsigned int i;
197
    char *cpuMode;
198

199
    if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
200 201 202
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s",
                       _("XML does not contain expected 'cpu' element"));
203 204 205
        return NULL;
    }

206
    if (VIR_ALLOC(def) < 0) {
207
        virReportOOMError();
208 209 210
        return NULL;
    }

211
    if (mode == VIR_CPU_TYPE_AUTO) {
212 213
        if (virXPathBoolean("boolean(./arch)", ctxt)) {
            if (virXPathBoolean("boolean(./@match)", ctxt)) {
214 215 216
                virReportError(VIR_ERR_XML_ERROR, "%s",
                               _("'arch' element element cannot be used inside 'cpu'"
                                 " element with 'match' attribute'"));
217 218
                goto error;
            }
219
            def->type = VIR_CPU_TYPE_HOST;
220
        } else {
221
            def->type = VIR_CPU_TYPE_GUEST;
222 223
        }
    } else {
224
        def->type = mode;
225 226 227 228 229
    }

    if ((cpuMode = virXMLPropString(node, "mode"))) {
        if (def->type == VIR_CPU_TYPE_HOST) {
            VIR_FREE(cpuMode);
230 231
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Attribute mode is only allowed for guest CPU"));
232 233 234 235 236
            goto error;
        } else {
            def->mode = virCPUModeTypeFromString(cpuMode);

            if (def->mode < 0) {
237 238 239
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Invalid mode attribute '%s'"),
                               cpuMode);
240
                VIR_FREE(cpuMode);
241 242
                goto error;
            }
243
            VIR_FREE(cpuMode);
244 245 246 247 248 249 250
        }
    } else {
        if (def->type == VIR_CPU_TYPE_HOST)
            def->mode = -1;
        else
            def->mode = VIR_CPU_MODE_CUSTOM;
    }
251 252

    if (def->type == VIR_CPU_TYPE_GUEST) {
253 254 255
        char *match = virXMLPropString(node, "match");

        if (!match) {
256 257 258 259
            if (virXPathBoolean("boolean(./model)", ctxt))
                def->match = VIR_CPU_MATCH_EXACT;
            else
                def->match = -1;
260 261 262 263 264
        } else {
            def->match = virCPUMatchTypeFromString(match);
            VIR_FREE(match);

            if (def->match < 0) {
265 266
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Invalid match attribute for CPU specification"));
267 268
                goto error;
            }
269 270 271 272
        }
    }

    if (def->type == VIR_CPU_TYPE_HOST) {
273
        def->arch = virXPathString("string(./arch[1])", ctxt);
274
        if (!def->arch) {
275 276
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Missing CPU architecture"));
277 278 279 280
            goto error;
        }
    }

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

288
    if (def->type == VIR_CPU_TYPE_GUEST &&
289 290 291 292 293 294 295 296 297 298
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {

        if (virXPathBoolean("boolean(./model[1]/@fallback)", ctxt)) {
            const char *fallback;

            fallback = virXPathString("string(./model[1]/@fallback)", ctxt);
            if (fallback) {
                def->fallback = virCPUFallbackTypeFromString(fallback);
                VIR_FREE(fallback);
                if (def->fallback < 0) {
299 300
                    virReportError(VIR_ERR_XML_ERROR, "%s",
                                   _("Invalid fallback attribute"));
301 302 303
                    goto error;
                }
            }
K
Ken ICHIKAWA 已提交
304
        }
305

K
Ken ICHIKAWA 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        if (virXPathBoolean("boolean(./model[1]/@vendor_id)", ctxt)) {
            char *vendor_id;

            vendor_id = virXPathString("string(./model[1]/@vendor_id)",
                                       ctxt);
            if (!vendor_id ||
                strlen(vendor_id) != VIR_CPU_VENDOR_ID_LENGTH) {
                virReportError(VIR_ERR_XML_ERROR,
                               _("vendor_id must be exactly"
                                 " %d characters long"),
                               VIR_CPU_VENDOR_ID_LENGTH);
                VIR_FREE(vendor_id);
                goto error;
            }
            /* ensure that the string can be passed to qemu*/
            for (i = 0; i < strlen(vendor_id); i++) {
                if (vendor_id[i]==',') {
                    virReportError(VIR_ERR_XML_ERROR, "%s",
                                   _("vendor id is invalid"));
325 326 327
                    VIR_FREE(vendor_id);
                    goto error;
                }
328
            }
K
Ken ICHIKAWA 已提交
329
            def->vendor_id = vendor_id;
330 331 332
        }
    }

J
Jiri Denemark 已提交
333 334
    def->vendor = virXPathString("string(./vendor[1])", ctxt);
    if (def->vendor && !def->model) {
335 336
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("CPU vendor specified without CPU model"));
J
Jiri Denemark 已提交
337 338 339
        goto error;
    }

340
    if (virXPathNode("./topology[1]", ctxt)) {
341 342 343
        int ret;
        unsigned long ul;

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

353
        ret = virXPathULong("string(./topology[1]/@cores)",
354 355
                            ctxt, &ul);
        if (ret < 0) {
356 357
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Missing 'cores' attribute in CPU topology"));
358 359 360 361
            goto error;
        }
        def->cores = (unsigned int) ul;

362
        ret = virXPathULong("string(./topology[1]/@threads)",
363 364
                            ctxt, &ul);
        if (ret < 0) {
365 366
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Missing 'threads' attribute in CPU topology"));
367 368 369 370 371
            goto error;
        }
        def->threads = (unsigned int) ul;

        if (!def->sockets || !def->cores || !def->threads) {
372 373
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Invalid CPU topology"));
374 375 376 377
            goto error;
        }
    }

378
    n = virXPathNodeSet("./feature", ctxt, &nodes);
379 380 381 382
    if (n < 0)
        goto error;

    if (n > 0) {
383
        if (!def->model) {
384 385
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Non-empty feature list specified without CPU model"));
386 387 388
            goto error;
        }

E
Eric Blake 已提交
389 390
        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                         def->nfeatures, n) < 0)
391 392 393 394 395 396 397 398 399 400 401 402 403
            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");
404 405 406 407
            if (strpolicy == NULL)
                policy = VIR_CPU_FEATURE_REQUIRE;
            else
                policy = virCPUFeaturePolicyTypeFromString(strpolicy);
408 409 410
            VIR_FREE(strpolicy);

            if (policy < 0) {
411 412
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Invalid CPU feature policy"));
413 414
                goto error;
            }
415
        } else {
416
            policy = -1;
417
        }
418 419 420

        if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
            VIR_FREE(name);
421 422
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Invalid CPU feature name"));
423 424 425 426 427
            goto error;
        }

        for (j = 0 ; j < i ; j++) {
            if (STREQ(name, def->features[j].name)) {
428 429 430
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("CPU feature `%s' specified more than once"),
                               name);
431 432 433 434 435 436 437 438 439
                VIR_FREE(name);
                goto error;
            }
        }

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

440 441 442 443
    if (virXPathNode("./numa[1]", ctxt)) {
        VIR_FREE(nodes);
        n = virXPathNodeSet("./numa[1]/cell", ctxt, &nodes);
        if (n <= 0) {
444 445
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("NUMA topology defined without NUMA cells"));
446 447 448 449 450 451 452 453 454 455
            goto error;
        }

        if (VIR_RESIZE_N(def->cells, def->ncells_max,
                         def->ncells, n) < 0)
            goto no_memory;

        def->ncells = n;

        for (i = 0 ; i < n ; i++) {
456
            char *cpus, *memory;
457 458 459
            int ret, ncpus = 0;

            def->cells[i].cellid = i;
460
            cpus = virXMLPropString(nodes[i], "cpus");
461
            if (!cpus) {
462 463
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Missing 'cpus' attribute in NUMA cell"));
464 465
                goto error;
            }
466
            def->cells[i].cpustr = cpus;
467

468 469
            ncpus = virBitmapParse(cpus, 0, &def->cells[i].cpumask,
                                   VIR_DOMAIN_CPUMASK_LEN);
470
            if (ncpus <= 0)
471 472 473 474 475
                goto error;
            def->cells_cpus += ncpus;

            memory = virXMLPropString(nodes[i], "memory");
            if (!memory) {
476 477
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Missing 'memory' attribute in NUMA cell"));
478 479 480 481 482
                goto error;
            }

            ret  = virStrToLong_ui(memory, NULL, 10, &def->cells[i].mem);
            if (ret == -1) {
483 484
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Invalid 'memory' attribute in NUMA cell"));
485 486 487 488 489 490 491
                VIR_FREE(memory);
                goto error;
            }
            VIR_FREE(memory);
        }
    }

492 493 494 495 496
cleanup:
    VIR_FREE(nodes);
    return def;

no_memory:
497
    virReportOOMError();
498 499 500 501 502 503 504 505 506

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


char *
507 508
virCPUDefFormat(virCPUDefPtr def,
                unsigned int flags)
509 510 511
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

512
    if (virCPUDefFormatBufFull(&buf, def, flags) < 0)
513 514 515 516 517 518 519 520
        goto cleanup;

    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

no_memory:
521
    virReportOOMError();
522
cleanup:
523
    virBufferFreeAndReset(&buf);
524 525 526 527
    return NULL;
}


528 529
int
virCPUDefFormatBufFull(virBufferPtr buf,
530 531
                       virCPUDefPtr def,
                       unsigned int flags)
532 533 534 535
{
    if (!def)
        return 0;

536 537 538 539 540 541
    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))) {
542 543
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU mode %d"), def->mode);
544 545 546
                return -1;
            }
            virBufferAsprintf(buf, " mode='%s'", tmp);
547 548
        }

549 550 551 552
        if (def->model &&
            (def->mode == VIR_CPU_MODE_CUSTOM ||
             (flags & VIR_DOMAIN_XML_UPDATE_CPU))) {
            if (!(tmp = virCPUMatchTypeToString(def->match))) {
553 554 555
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU match policy %d"),
                               def->match);
556 557 558 559
                return -1;
            }
            virBufferAsprintf(buf, " match='%s'", tmp);
        }
560
    }
561
    virBufferAddLit(buf, ">\n");
562 563 564 565 566

    if (def->arch)
        virBufferAsprintf(buf, "  <arch>%s</arch>\n", def->arch);

    virBufferAdjustIndent(buf, 2);
567
    if (virCPUDefFormatBuf(buf, def, flags) < 0)
568 569 570 571 572 573 574 575
        return -1;
    virBufferAdjustIndent(buf, -2);

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

    return 0;
}

576
int
577
virCPUDefFormatBuf(virBufferPtr buf,
578 579
                   virCPUDefPtr def,
                   unsigned int flags)
580 581
{
    unsigned int i;
582 583
    bool formatModel;
    bool formatFallback;
584 585 586 587

    if (!def)
        return 0;

588 589 590 591 592 593
    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)));

594
    if (!def->model && def->nfeatures) {
595 596
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Non-empty feature list specified without CPU model"));
597 598 599
        return -1;
    }

600
    if ((formatModel && def->model) || formatFallback) {
601
        virBufferAddLit(buf, "<model");
602
        if (formatFallback) {
603 604 605 606
            const char *fallback;

            fallback = virCPUFallbackTypeToString(def->fallback);
            if (!fallback) {
607 608 609
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU fallback value: %d"),
                               def->fallback);
610 611 612
                return -1;
            }
            virBufferAsprintf(buf, " fallback='%s'", fallback);
613 614
            if (def->vendor_id)
                virBufferAsprintf(buf, " vendor_id='%s'", def->vendor_id);
615
        }
616 617 618 619 620
        if (formatModel && def->model) {
            virBufferAsprintf(buf, ">%s</model>\n", def->model);
        } else {
            virBufferAddLit(buf, "/>\n");
        }
621
    }
622

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

626
    if (def->sockets && def->cores && def->threads) {
627
        virBufferAddLit(buf, "<topology");
628 629 630
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
631 632 633
        virBufferAddLit(buf, "/>\n");
    }

634 635 636
    if (formatModel) {
        for (i = 0 ; i < def->nfeatures ; i++) {
            virCPUFeatureDefPtr feature = def->features + i;
637

638
            if (!feature->name) {
639 640
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Missing CPU feature name"));
641 642
                return -1;
            }
643 644 645 646 647 648

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

                policy = virCPUFeaturePolicyTypeToString(feature->policy);
                if (!policy) {
649 650 651
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Unexpected CPU feature policy %d"),
                                   feature->policy);
652 653 654 655 656 657 658 659
                    return -1;
                }
                virBufferAsprintf(buf, "<feature policy='%s' name='%s'/>\n",
                                  policy, feature->name);
            } else {
                virBufferAsprintf(buf, "<feature name='%s'/>\n",
                                  feature->name);
            }
660 661 662
        }
    }

663 664 665 666 667 668 669 670 671 672
    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");
    }
673 674 675 676 677
    return 0;
}


int
678
virCPUDefAddFeature(virCPUDefPtr def,
679 680 681 682 683 684 685
                    const char *name,
                    int policy)
{
    int i;

    for (i = 0 ; i < def->nfeatures ; i++) {
        if (STREQ(name, def->features[i].name)) {
686 687
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("CPU feature `%s' specified more than once"), name);
688 689 690 691
            return -1;
        }
    }

E
Eric Blake 已提交
692 693
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
694 695 696 697 698 699 700 701 702 703 704 705 706 707
        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:
708
    virReportOOMError();
709 710
    return -1;
}
711 712 713 714 715 716 717 718 719 720 721 722

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

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

    if ((src && !dst) || (!src && dst)) {
723 724
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Target CPU does not match source"));
725 726 727 728
        goto cleanup;
    }

    if (src->type != dst->type) {
729 730 731 732
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU type %s does not match source %s"),
                       virCPUTypeToString(dst->type),
                       virCPUTypeToString(src->type));
733 734 735
        goto cleanup;
    }

736
    if (src->mode != dst->mode) {
737 738 739 740
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU mode %s does not match source %s"),
                       virCPUModeTypeToString(dst->mode),
                       virCPUModeTypeToString(src->mode));
741 742 743
        goto cleanup;
    }

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

    if (STRNEQ_NULLABLE(src->model, dst->model)) {
752 753 754
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU model %s does not match source %s"),
                       NULLSTR(dst->model), NULLSTR(src->model));
755 756 757 758
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(src->vendor, dst->vendor)) {
759 760 761
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU vendor %s does not match source %s"),
                       NULLSTR(dst->vendor), NULLSTR(src->vendor));
762 763 764
        goto cleanup;
    }

765
    if (STRNEQ_NULLABLE(src->vendor_id, dst->vendor_id)) {
766 767 768
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU model %s does not match source %s"),
                       NULLSTR(dst->vendor_id), NULLSTR(src->vendor_id));
769 770 771
        goto cleanup;
    }

772
    if (src->sockets != dst->sockets) {
773 774 775
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU sockets %d does not match source %d"),
                       dst->sockets, src->sockets);
776 777 778 779
        goto cleanup;
    }

    if (src->cores != dst->cores) {
780 781 782
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU cores %d does not match source %d"),
                       dst->cores, src->cores);
783 784 785 786
        goto cleanup;
    }

    if (src->threads != dst->threads) {
787 788 789
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU threads %d does not match source %d"),
                       dst->threads, src->threads);
790 791 792 793
        goto cleanup;
    }

    if (src->nfeatures != dst->nfeatures) {
794 795 796
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU feature count %zu does not match source %zu"),
                       dst->nfeatures, src->nfeatures);
797 798 799 800 801
        goto cleanup;
    }

    for (i = 0 ; i < src->nfeatures ; i++) {
        if (STRNEQ(src->features[i].name, dst->features[i].name)) {
802 803 804
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Target CPU feature %s does not match source %s"),
                           dst->features[i].name, src->features[i].name);
805 806 807 808
            goto cleanup;
        }

        if (src->features[i].policy != dst->features[i].policy) {
809 810 811 812
            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));
813 814 815 816 817 818 819 820 821
            goto cleanup;
        }
    }

    identical = true;

cleanup:
    return identical;
}