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 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
    char *fallback = NULL;
    char *vendor_id = NULL;
200

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

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

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

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

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

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

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

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

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

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

290
    if (def->type == VIR_CPU_TYPE_GUEST &&
291 292
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {

293 294 295 296 297
        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;
298
            }
K
Ken ICHIKAWA 已提交
299
        }
300

301 302 303
        if ((vendor_id = virXPathString("string(./model[1]/@vendor_id)",
                                        ctxt))) {
            if (strlen(vendor_id) != VIR_CPU_VENDOR_ID_LENGTH) {
K
Ken ICHIKAWA 已提交
304
                virReportError(VIR_ERR_XML_ERROR,
305
                               _("vendor_id must be exactly %d characters long"),
K
Ken ICHIKAWA 已提交
306 307 308
                               VIR_CPU_VENDOR_ID_LENGTH);
                goto error;
            }
309

K
Ken ICHIKAWA 已提交
310
            /* ensure that the string can be passed to qemu*/
311
            if (strchr(vendor_id, ',')) {
K
Ken ICHIKAWA 已提交
312 313
                    virReportError(VIR_ERR_XML_ERROR, "%s",
                                   _("vendor id is invalid"));
314
                    goto error;
315
            }
316

K
Ken ICHIKAWA 已提交
317
            def->vendor_id = vendor_id;
318
            vendor_id = NULL;
319 320 321
        }
    }

J
Jiri Denemark 已提交
322 323
    def->vendor = virXPathString("string(./vendor[1])", ctxt);
    if (def->vendor && !def->model) {
324 325
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("CPU vendor specified without CPU model"));
J
Jiri Denemark 已提交
326 327 328
        goto error;
    }

329
    if (virXPathNode("./topology[1]", ctxt)) {
330 331 332
        int ret;
        unsigned long ul;

333
        ret = virXPathULong("string(./topology[1]/@sockets)",
334 335
                            ctxt, &ul);
        if (ret < 0) {
336 337
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Missing 'sockets' attribute in CPU topology"));
338 339 340 341
            goto error;
        }
        def->sockets = (unsigned int) ul;

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

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

        if (!def->sockets || !def->cores || !def->threads) {
361 362
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Invalid CPU topology"));
363 364 365 366
            goto error;
        }
    }

367
    n = virXPathNodeSet("./feature", ctxt, &nodes);
368 369 370 371
    if (n < 0)
        goto error;

    if (n > 0) {
372
        if (!def->model) {
373 374
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Non-empty feature list specified without CPU model"));
375 376 377
            goto error;
        }

E
Eric Blake 已提交
378 379
        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                         def->nfeatures, n) < 0)
380 381 382 383 384 385 386 387 388 389 390 391 392
            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");
393 394 395 396
            if (strpolicy == NULL)
                policy = VIR_CPU_FEATURE_REQUIRE;
            else
                policy = virCPUFeaturePolicyTypeFromString(strpolicy);
397 398 399
            VIR_FREE(strpolicy);

            if (policy < 0) {
400 401
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Invalid CPU feature policy"));
402 403
                goto error;
            }
404
        } else {
405
            policy = -1;
406
        }
407 408 409

        if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
            VIR_FREE(name);
410 411
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Invalid CPU feature name"));
412 413 414 415 416
            goto error;
        }

        for (j = 0 ; j < i ; j++) {
            if (STREQ(name, def->features[j].name)) {
417 418 419
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("CPU feature `%s' specified more than once"),
                               name);
420 421 422 423 424 425 426 427 428
                VIR_FREE(name);
                goto error;
            }
        }

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

429 430 431 432
    if (virXPathNode("./numa[1]", ctxt)) {
        VIR_FREE(nodes);
        n = virXPathNodeSet("./numa[1]/cell", ctxt, &nodes);
        if (n <= 0) {
433 434
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("NUMA topology defined without NUMA cells"));
435 436 437 438 439 440 441 442 443 444
            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++) {
445
            char *cpus, *memory;
446 447 448
            int ret, ncpus = 0;

            def->cells[i].cellid = i;
449
            cpus = virXMLPropString(nodes[i], "cpus");
450
            if (!cpus) {
451 452
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Missing 'cpus' attribute in NUMA cell"));
453 454
                goto error;
            }
455
            def->cells[i].cpustr = cpus;
456

457 458
            ncpus = virBitmapParse(cpus, 0, &def->cells[i].cpumask,
                                   VIR_DOMAIN_CPUMASK_LEN);
459
            if (ncpus <= 0)
460 461 462 463 464
                goto error;
            def->cells_cpus += ncpus;

            memory = virXMLPropString(nodes[i], "memory");
            if (!memory) {
465 466
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Missing 'memory' attribute in NUMA cell"));
467 468 469 470 471
                goto error;
            }

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

481
cleanup:
482 483
    VIR_FREE(fallback);
    VIR_FREE(vendor_id);
484 485 486 487
    VIR_FREE(nodes);
    return def;

no_memory:
488
    virReportOOMError();
489 490 491 492 493 494 495 496 497

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


char *
498 499
virCPUDefFormat(virCPUDefPtr def,
                unsigned int flags)
500 501 502
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

503
    if (virCPUDefFormatBufFull(&buf, def, flags) < 0)
504 505 506 507 508 509 510 511
        goto cleanup;

    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

no_memory:
512
    virReportOOMError();
513
cleanup:
514
    virBufferFreeAndReset(&buf);
515 516 517 518
    return NULL;
}


519 520
int
virCPUDefFormatBufFull(virBufferPtr buf,
521 522
                       virCPUDefPtr def,
                       unsigned int flags)
523 524 525 526
{
    if (!def)
        return 0;

527 528 529 530 531 532
    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))) {
533 534
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU mode %d"), def->mode);
535 536 537
                return -1;
            }
            virBufferAsprintf(buf, " mode='%s'", tmp);
538 539
        }

540 541 542 543
        if (def->model &&
            (def->mode == VIR_CPU_MODE_CUSTOM ||
             (flags & VIR_DOMAIN_XML_UPDATE_CPU))) {
            if (!(tmp = virCPUMatchTypeToString(def->match))) {
544 545 546
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU match policy %d"),
                               def->match);
547 548 549 550
                return -1;
            }
            virBufferAsprintf(buf, " match='%s'", tmp);
        }
551
    }
552
    virBufferAddLit(buf, ">\n");
553 554 555 556 557

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

    virBufferAdjustIndent(buf, 2);
558
    if (virCPUDefFormatBuf(buf, def, flags) < 0)
559 560 561 562 563 564 565 566
        return -1;
    virBufferAdjustIndent(buf, -2);

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

    return 0;
}

567
int
568
virCPUDefFormatBuf(virBufferPtr buf,
569 570
                   virCPUDefPtr def,
                   unsigned int flags)
571 572
{
    unsigned int i;
573 574
    bool formatModel;
    bool formatFallback;
575 576 577 578

    if (!def)
        return 0;

579 580 581 582 583 584
    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)));

585
    if (!def->model && def->nfeatures) {
586 587
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Non-empty feature list specified without CPU model"));
588 589 590
        return -1;
    }

591
    if ((formatModel && def->model) || formatFallback) {
592
        virBufferAddLit(buf, "<model");
593
        if (formatFallback) {
594 595 596 597
            const char *fallback;

            fallback = virCPUFallbackTypeToString(def->fallback);
            if (!fallback) {
598 599 600
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU fallback value: %d"),
                               def->fallback);
601 602 603
                return -1;
            }
            virBufferAsprintf(buf, " fallback='%s'", fallback);
604 605
            if (def->vendor_id)
                virBufferAsprintf(buf, " vendor_id='%s'", def->vendor_id);
606
        }
607 608 609 610 611
        if (formatModel && def->model) {
            virBufferAsprintf(buf, ">%s</model>\n", def->model);
        } else {
            virBufferAddLit(buf, "/>\n");
        }
612
    }
613

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

617
    if (def->sockets && def->cores && def->threads) {
618
        virBufferAddLit(buf, "<topology");
619 620 621
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
622 623 624
        virBufferAddLit(buf, "/>\n");
    }

625 626 627
    if (formatModel) {
        for (i = 0 ; i < def->nfeatures ; i++) {
            virCPUFeatureDefPtr feature = def->features + i;
628

629
            if (!feature->name) {
630 631
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("Missing CPU feature name"));
632 633
                return -1;
            }
634 635 636 637 638 639

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

                policy = virCPUFeaturePolicyTypeToString(feature->policy);
                if (!policy) {
640 641 642
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Unexpected CPU feature policy %d"),
                                   feature->policy);
643 644 645 646 647 648 649 650
                    return -1;
                }
                virBufferAsprintf(buf, "<feature policy='%s' name='%s'/>\n",
                                  policy, feature->name);
            } else {
                virBufferAsprintf(buf, "<feature name='%s'/>\n",
                                  feature->name);
            }
651 652 653
        }
    }

654 655 656 657 658 659 660 661 662 663
    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");
    }
664 665 666 667 668
    return 0;
}


int
669
virCPUDefAddFeature(virCPUDefPtr def,
670 671 672 673 674 675 676
                    const char *name,
                    int policy)
{
    int i;

    for (i = 0 ; i < def->nfeatures ; i++) {
        if (STREQ(name, def->features[i].name)) {
677 678
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("CPU feature `%s' specified more than once"), name);
679 680 681 682
            return -1;
        }
    }

E
Eric Blake 已提交
683 684
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
685 686 687 688 689 690 691 692 693 694 695 696 697 698
        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:
699
    virReportOOMError();
700 701
    return -1;
}
702 703 704 705 706 707 708 709 710 711 712 713

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

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

    if ((src && !dst) || (!src && dst)) {
714 715
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Target CPU does not match source"));
716 717 718 719
        goto cleanup;
    }

    if (src->type != dst->type) {
720 721 722 723
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU type %s does not match source %s"),
                       virCPUTypeToString(dst->type),
                       virCPUTypeToString(src->type));
724 725 726
        goto cleanup;
    }

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

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

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

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

756
    if (STRNEQ_NULLABLE(src->vendor_id, dst->vendor_id)) {
757 758 759
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU model %s does not match source %s"),
                       NULLSTR(dst->vendor_id), NULLSTR(src->vendor_id));
760 761 762
        goto cleanup;
    }

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

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

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

    if (src->nfeatures != dst->nfeatures) {
785 786 787
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU feature count %zu does not match source %zu"),
                       dst->nfeatures, src->nfeatures);
788 789 790 791 792
        goto cleanup;
    }

    for (i = 0 ; i < src->nfeatures ; i++) {
        if (STRNEQ(src->features[i].name, dst->features[i].name)) {
793 794 795
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Target CPU feature %s does not match source %s"),
                           dst->features[i].name, src->features[i].name);
796 797 798 799
            goto cleanup;
        }

        if (src->features[i].policy != dst->features[i].policy) {
800 801 802 803
            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));
804 805 806 807 808 809 810 811 812
            goto cleanup;
        }
    }

    identical = true;

cleanup:
    return identical;
}