cpu_conf.c 24.5 KB
Newer Older
1
/*
2
 * cpu_conf.c: CPU XML handling
3
 *
4
 * Copyright (C) 2009-2014 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
int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
virCPUDefCopyModel(virCPUDefPtr dst,
97
                   const virCPUDef *src,
98 99
                   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
virCPUDefPtr
129
virCPUDefCopy(const virCPUDef *cpu)
130 131
{
    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(xmlNodePtr node,
178
                  xmlXPathContextPtr ctxt,
179
                  virCPUType mode)
180 181 182 183
{
    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_CONFIG_UNSUPPORTED,
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_CONFIG_UNSUPPORTED, "%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_CONFIG_UNSUPPORTED,
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
        if ((fallback = virXPathString("string(./model[1]/@fallback)", ctxt))) {
            if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
288
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
289 290
                               _("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
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
395
                               _("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
    VIR_FREE(nodes);
    return def;

481
 error:
482 483 484 485 486 487 488
    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
        goto cleanup;

497 498
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
499 500 501

    return virBufferContentAndReset(&buf);

502
 cleanup:
503
    virBufferFreeAndReset(&buf);
504 505 506 507
    return NULL;
}


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

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

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

    if (def->arch)
545
        virBufferAsprintf(buf, "<arch>%s</arch>\n",
546
                          virArchToString(def->arch));
547
    if (virCPUDefFormatBuf(buf, def, flags) < 0)
548 549 550 551 552 553 554 555
        return -1;
    virBufferAdjustIndent(buf, -2);

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

    return 0;
}

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

    if (!def)
        return 0;

568 569 570 571 572 573
    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)));

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

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

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

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

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

616 617
    for (i = 0; i < def->nfeatures; i++) {
        virCPUFeatureDefPtr feature = def->features + i;
618

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

625 626 627 628 629 630 631 632 633
        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;
634
            }
635 636 637 638 639
            virBufferAsprintf(buf, "<feature policy='%s' name='%s'/>\n",
                              policy, feature->name);
        } else {
            virBufferAsprintf(buf, "<feature name='%s'/>\n",
                              feature->name);
640 641 642
        }
    }

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

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

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

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

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

680 681 682 683
            return -1;
        }
    }

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

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

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

    return 0;
}
696

P
Peter Krempa 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
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);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    identical = true;

821
 cleanup:
822 823
    return identical;
}