cpu_conf.c 24.4 KB
Newer Older
1
/*
2
 * cpu_conf.c: CPU XML handling
3
 *
4
 * Copyright (C) 2009-2013 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
void ATTRIBUTE_NONNULL(1)
virCPUDefFreeModel(virCPUDefPtr def)
{
63
    size_t i;
64 65 66

    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
void
virCPUDefFree(virCPUDefPtr def)
{
77
    size_t i;
78 79 80 81

    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
int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
virCPUDefCopyModel(virCPUDefPtr dst,
                   const virCPUDefPtr src,
                   bool resetPolicy)
{
100
    size_t i;
101

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
virCPUDefPtr
virCPUDefCopy(const virCPUDefPtr cpu)
{
    virCPUDefPtr copy;
132
    size_t i;
133

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
                  xmlXPathContextPtr ctxt,
                  enum virCPUType mode)
{
    virCPUDefPtr def;
    xmlNodePtr *nodes = NULL;
    int n;
184
    size_t i;
185
    char *cpuMode;
186 187
    char *fallback = NULL;
    char *vendor_id = NULL;
188

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

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

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

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

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

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

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

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

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

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

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

286 287 288 289 290
        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;
291
            }
K
Ken ICHIKAWA 已提交
292
        }
293

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

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

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

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

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

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

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

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

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

P
Peter Krempa 已提交
360
    if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
361 362 363
        goto error;

    if (n > 0) {
364
        if (!def->model && def->mode != VIR_CPU_MODE_HOST_MODEL) {
365
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
366 367
                           _("Non-empty feature list specified without "
                             "CPU model"));
368 369 370
            goto error;
        }

E
Eric Blake 已提交
371 372
        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                         def->nfeatures, n) < 0)
373
            goto error;
P
Peter Krempa 已提交
374

375 376 377
        def->nfeatures = n;
    }

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

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

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

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

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

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

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

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

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

        def->ncells = n;

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

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

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

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

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

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

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


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

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

    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

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


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

518 519 520 521 522 523
    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))) {
524 525
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU mode %d"), def->mode);
526 527 528
                return -1;
            }
            virBufferAsprintf(buf, " mode='%s'", tmp);
529 530
        }

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

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

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

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

    return 0;
}

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

    if (!def)
        return 0;

571 572 573 574 575 576
    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)));

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

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

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

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

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

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

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

628 629 630 631 632 633 634 635 636
        if (def->type == VIR_CPU_TYPE_GUEST) {
            const char *policy;

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

646 647 648 649 650 651 652 653 654 655
    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");
    }
656 657 658
    return 0;
}

P
Peter Krempa 已提交
659 660 661 662 663
static int
virCPUDefUpdateFeatureInternal(virCPUDefPtr def,
                               const char *name,
                               int policy,
                               bool update)
664
{
665
    size_t i;
666

P
Peter Krempa 已提交
667 668 669
    if (def->type == VIR_CPU_TYPE_HOST)
        policy = -1;

670
    for (i = 0; i < def->nfeatures; i++) {
671
        if (STREQ(name, def->features[i].name)) {
P
Peter Krempa 已提交
672 673 674 675 676
            if (update) {
                def->features[i].policy = policy;
                return 0;
            }

677
            virReportError(VIR_ERR_INTERNAL_ERROR,
678 679
                           _("CPU feature `%s' specified more than once"),
                           name);
P
Peter Krempa 已提交
680

681 682 683 684
            return -1;
        }
    }

E
Eric Blake 已提交
685 686
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
687
        return -1;
688

689 690
    if (VIR_STRDUP(def->features[def->nfeatures].name, name) < 0)
        return -1;
691 692 693 694 695 696

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

    return 0;
}
697

P
Peter Krempa 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
int
virCPUDefUpdateFeature(virCPUDefPtr def,
                       const char *name,
                       int policy)
{
    return virCPUDefUpdateFeatureInternal(def, name, policy, true);
}

int
virCPUDefAddFeature(virCPUDefPtr def,
                    const char *name,
                    int policy)
{
    return virCPUDefUpdateFeatureInternal(def, name, policy, false);
}

714 715 716 717 718
bool
virCPUDefIsEqual(virCPUDefPtr src,
                 virCPUDefPtr dst)
{
    bool identical = false;
719
    size_t i;
720 721 722 723 724

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

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

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

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

746
    if (src->arch != dst->arch) {
747 748
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU arch %s does not match source %s"),
749 750
                       virArchToString(dst->arch),
                       virArchToString(src->arch));
751 752 753 754
        goto cleanup;
    }

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

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

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

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

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

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

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

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

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

    identical = true;

cleanup:
    return identical;
}