cpu_conf.c 24.1 KB
Newer Older
1
/*
2
 * cpu_conf.c: CPU XML handling
3
 *
4
 * Copyright (C) 2009-2015 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
VIR_ENUM_IMPL(virCPUFeaturePolicy, VIR_CPU_FEATURE_LAST,
              "force",
              "require",
              "optional",
              "disable",
              "forbid")

59 60 61
void ATTRIBUTE_NONNULL(1)
virCPUDefFreeModel(virCPUDefPtr def)
{
62
    size_t i;
63 64 65

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

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

73 74 75 76 77 78
void
virCPUDefFree(virCPUDefPtr def)
{
    if (!def)
        return;

79
    virCPUDefFreeModel(def);
80 81 82 83
    VIR_FREE(def);
}


84 85
int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
virCPUDefCopyModel(virCPUDefPtr dst,
86
                   const virCPUDef *src,
87
                   bool resetPolicy)
88 89 90 91 92 93 94 95 96 97 98
{
    return virCPUDefCopyModelFilter(dst, src, resetPolicy, NULL, NULL);
}


int
virCPUDefCopyModelFilter(virCPUDefPtr dst,
                         const virCPUDef *src,
                         bool resetPolicy,
                         virCPUDefFeatureFilter filter,
                         void *opaque)
99
{
100
    size_t i;
101
    size_t n;
102

103 104 105 106
    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)
107
        return -1;
108 109 110 111 112 113
    dst->nfeatures_max = src->nfeatures;
    dst->nfeatures = 0;

    for (i = 0; i < src->nfeatures; i++) {
        if (filter && !filter(src->features[i].name, opaque))
            continue;
114

115
        n = dst->nfeatures++;
116 117
        if (dst->type != src->type && resetPolicy) {
            if (dst->type == VIR_CPU_TYPE_HOST)
118
                dst->features[n].policy = -1;
119
            else if (src->features[i].policy == -1)
120
                dst->features[n].policy = VIR_CPU_FEATURE_REQUIRE;
121
            else
122
                dst->features[n].policy = src->features[i].policy;
123
        } else {
124
            dst->features[n].policy = src->features[i].policy;
125 126
        }

127
        if (VIR_STRDUP(dst->features[n].name, src->features[i].name) < 0)
128
            return -1;
129 130 131 132 133
    }

    return 0;
}

134

135 136 137 138 139 140 141
/**
 * virCPUDefStealModel:
 *
 * Move CPU model related parts virCPUDef from @src to @dst. If @keepVendor
 * is true, the function keeps the original vendor/vendor_id in @dst rather
 * than overwriting it with the values from @src.
 */
142 143
void
virCPUDefStealModel(virCPUDefPtr dst,
144 145
                    virCPUDefPtr src,
                    bool keepVendor)
146
{
147 148
    char *vendor = NULL;
    char *vendor_id = NULL;
149 150 151 152 153 154

    if (keepVendor) {
        VIR_STEAL_PTR(vendor, dst->vendor);
        VIR_STEAL_PTR(vendor_id, dst->vendor_id);
    }

155 156 157 158 159 160 161 162
    virCPUDefFreeModel(dst);

    VIR_STEAL_PTR(dst->model, src->model);
    VIR_STEAL_PTR(dst->features, src->features);
    dst->nfeatures_max = src->nfeatures_max;
    src->nfeatures_max = 0;
    dst->nfeatures = src->nfeatures;
    src->nfeatures = 0;
163 164 165 166 167 168 169 170

    if (keepVendor) {
        dst->vendor = vendor;
        dst->vendor_id = vendor_id;
    } else {
        VIR_STEAL_PTR(dst->vendor, src->vendor);
        VIR_STEAL_PTR(dst->vendor_id, src->vendor_id);
    }
171 172 173
}


174
virCPUDefPtr
175
virCPUDefCopyWithoutModel(const virCPUDef *cpu)
176 177 178
{
    virCPUDefPtr copy;

179
    if (!cpu || VIR_ALLOC(copy) < 0)
180 181 182
        return NULL;

    copy->type = cpu->type;
183
    copy->mode = cpu->mode;
184
    copy->match = cpu->match;
185
    copy->fallback = cpu->fallback;
186 187 188
    copy->sockets = cpu->sockets;
    copy->cores = cpu->cores;
    copy->threads = cpu->threads;
189
    copy->arch = cpu->arch;
190

191 192 193 194 195 196 197 198 199 200 201 202
    return copy;
}


virCPUDefPtr
virCPUDefCopy(const virCPUDef *cpu)
{
    virCPUDefPtr copy;

    if (!(copy = virCPUDefCopyWithoutModel(cpu)))
        return NULL;

203 204
    if (virCPUDefCopyModel(copy, cpu, false) < 0)
        goto error;
205

206 207
    return copy;

208
 error:
209 210 211 212
    virCPUDefFree(copy);
    return NULL;
}

213

214
virCPUDefPtr
215
virCPUDefParseXML(xmlNodePtr node,
216
                  xmlXPathContextPtr ctxt,
217
                  virCPUType mode)
218 219 220
{
    virCPUDefPtr def;
    xmlNodePtr *nodes = NULL;
221
    xmlNodePtr oldnode = ctxt->node;
222
    int n;
223
    size_t i;
224
    char *cpuMode;
225 226
    char *fallback = NULL;
    char *vendor_id = NULL;
227

228
    if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
229
        virReportError(VIR_ERR_XML_ERROR, "%s",
230
                       _("XML does not contain expected 'cpu' element"));
231 232 233
        return NULL;
    }

234
    if (VIR_ALLOC(def) < 0)
235 236
        return NULL;

237
    if (mode == VIR_CPU_TYPE_AUTO) {
238 239
        if (virXPathBoolean("boolean(./arch)", ctxt)) {
            if (virXPathBoolean("boolean(./@match)", ctxt)) {
240
                virReportError(VIR_ERR_XML_ERROR, "%s",
241
                               _("'arch' element cannot be used inside 'cpu'"
242
                                 " element with 'match' attribute'"));
243 244
                goto error;
            }
245
            def->type = VIR_CPU_TYPE_HOST;
246
        } else {
247
            def->type = VIR_CPU_TYPE_GUEST;
248 249
        }
    } else {
250
        def->type = mode;
251 252 253 254 255
    }

    if ((cpuMode = virXMLPropString(node, "mode"))) {
        if (def->type == VIR_CPU_TYPE_HOST) {
            VIR_FREE(cpuMode);
256 257
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Attribute mode is only allowed for guest CPU"));
258 259 260 261 262
            goto error;
        } else {
            def->mode = virCPUModeTypeFromString(cpuMode);

            if (def->mode < 0) {
263
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
264 265
                               _("Invalid mode attribute '%s'"),
                               cpuMode);
266
                VIR_FREE(cpuMode);
267 268
                goto error;
            }
269
            VIR_FREE(cpuMode);
270 271 272 273 274 275 276
        }
    } else {
        if (def->type == VIR_CPU_TYPE_HOST)
            def->mode = -1;
        else
            def->mode = VIR_CPU_MODE_CUSTOM;
    }
277 278

    if (def->type == VIR_CPU_TYPE_GUEST) {
279 280 281
        char *match = virXMLPropString(node, "match");

        if (!match) {
282 283 284 285
            if (virXPathBoolean("boolean(./model)", ctxt))
                def->match = VIR_CPU_MATCH_EXACT;
            else
                def->match = -1;
286 287 288 289 290
        } else {
            def->match = virCPUMatchTypeFromString(match);
            VIR_FREE(match);

            if (def->match < 0) {
291
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
P
Peter Krempa 已提交
292 293
                               _("Invalid match attribute for CPU "
                                 "specification"));
294 295
                goto error;
            }
296 297 298 299
        }
    }

    if (def->type == VIR_CPU_TYPE_HOST) {
300 301
        char *arch = virXPathString("string(./arch[1])", ctxt);
        if (!arch) {
302 303
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Missing CPU architecture"));
304 305
            goto error;
        }
306
        if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) {
307
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
308 309 310 311 312
                           _("Unknown architecture %s"), arch);
            VIR_FREE(arch);
            goto error;
        }
        VIR_FREE(arch);
313 314
    }

315
    if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
316
        def->type == VIR_CPU_TYPE_HOST) {
317 318
        virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("Missing CPU model name"));
319 320 321
        goto error;
    }

322
    if (def->type == VIR_CPU_TYPE_GUEST &&
323 324
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {

325 326
        if ((fallback = virXPathString("string(./model[1]/@fallback)", ctxt))) {
            if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
327
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
328 329
                               _("Invalid fallback attribute"));
                goto error;
330
            }
K
Ken ICHIKAWA 已提交
331
        }
332

333 334 335
        if ((vendor_id = virXPathString("string(./model[1]/@vendor_id)",
                                        ctxt))) {
            if (strlen(vendor_id) != VIR_CPU_VENDOR_ID_LENGTH) {
K
Ken ICHIKAWA 已提交
336
                virReportError(VIR_ERR_XML_ERROR,
337
                               _("vendor_id must be exactly %d characters long"),
K
Ken ICHIKAWA 已提交
338 339 340
                               VIR_CPU_VENDOR_ID_LENGTH);
                goto error;
            }
341

K
Ken ICHIKAWA 已提交
342
            /* ensure that the string can be passed to qemu*/
343
            if (strchr(vendor_id, ',')) {
K
Ken ICHIKAWA 已提交
344 345
                    virReportError(VIR_ERR_XML_ERROR, "%s",
                                   _("vendor id is invalid"));
346
                    goto error;
347
            }
348

K
Ken ICHIKAWA 已提交
349
            def->vendor_id = vendor_id;
350
            vendor_id = NULL;
351 352 353
        }
    }

J
Jiri Denemark 已提交
354 355
    def->vendor = virXPathString("string(./vendor[1])", ctxt);
    if (def->vendor && !def->model) {
356 357
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("CPU vendor specified without CPU model"));
J
Jiri Denemark 已提交
358 359 360
        goto error;
    }

361
    if (virXPathNode("./topology[1]", ctxt)) {
362 363 364
        int ret;
        unsigned long ul;

365
        ret = virXPathULong("string(./topology[1]/@sockets)",
366 367
                            ctxt, &ul);
        if (ret < 0) {
368
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
369
                           _("Missing 'sockets' attribute in CPU topology"));
370 371 372 373
            goto error;
        }
        def->sockets = (unsigned int) ul;

374
        ret = virXPathULong("string(./topology[1]/@cores)",
375 376
                            ctxt, &ul);
        if (ret < 0) {
377
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
378
                           _("Missing 'cores' attribute in CPU topology"));
379 380 381 382
            goto error;
        }
        def->cores = (unsigned int) ul;

383
        ret = virXPathULong("string(./topology[1]/@threads)",
384 385
                            ctxt, &ul);
        if (ret < 0) {
386
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
387
                           _("Missing 'threads' attribute in CPU topology"));
388 389 390 391 392
            goto error;
        }
        def->threads = (unsigned int) ul;

        if (!def->sockets || !def->cores || !def->threads) {
393 394
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU topology"));
395 396 397 398
            goto error;
        }
    }

P
Peter Krempa 已提交
399
    if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
400 401 402
        goto error;

    if (n > 0) {
403
        if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM) {
404
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
405 406
                           _("Non-empty feature list specified without "
                             "CPU model"));
407 408 409
            goto error;
        }

E
Eric Blake 已提交
410 411
        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                         def->nfeatures, n) < 0)
412
            goto error;
P
Peter Krempa 已提交
413

414 415 416
        def->nfeatures = n;
    }

417
    for (i = 0; i < n; i++) {
418 419
        char *name;
        int policy; /* enum virDomainCPUFeaturePolicy */
420
        size_t j;
421 422 423 424 425

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

            strpolicy = virXMLPropString(nodes[i], "policy");
426 427 428 429
            if (strpolicy == NULL)
                policy = VIR_CPU_FEATURE_REQUIRE;
            else
                policy = virCPUFeaturePolicyTypeFromString(strpolicy);
430 431 432
            VIR_FREE(strpolicy);

            if (policy < 0) {
433
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
434
                               _("Invalid CPU feature policy"));
435 436
                goto error;
            }
437
        } else {
438
            policy = -1;
439
        }
440 441 442

        if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
            VIR_FREE(name);
443 444
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU feature name"));
445 446 447
            goto error;
        }

448
        for (j = 0; j < i; j++) {
449
            if (STREQ(name, def->features[j].name)) {
450
                virReportError(VIR_ERR_XML_ERROR,
C
Chen Fan 已提交
451
                               _("CPU feature '%s' specified more than once"),
452
                               name);
453 454 455 456 457 458 459 460 461
                VIR_FREE(name);
                goto error;
            }
        }

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

462
 cleanup:
463
    ctxt->node = oldnode;
464 465
    VIR_FREE(fallback);
    VIR_FREE(vendor_id);
466 467 468
    VIR_FREE(nodes);
    return def;

469
 error:
470 471 472 473 474 475 476
    virCPUDefFree(def);
    def = NULL;
    goto cleanup;
}


char *
477
virCPUDefFormat(virCPUDefPtr def,
478
                virDomainNumaPtr numa,
479
                bool updateCPU)
480 481 482
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

483
    if (virCPUDefFormatBufFull(&buf, def, numa, updateCPU) < 0)
484 485
        goto cleanup;

486 487
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
488 489 490

    return virBufferContentAndReset(&buf);

491
 cleanup:
492
    virBufferFreeAndReset(&buf);
493 494 495 496
    return NULL;
}


497 498
int
virCPUDefFormatBufFull(virBufferPtr buf,
499
                       virCPUDefPtr def,
500
                       virDomainNumaPtr numa,
501
                       bool updateCPU)
502
{
503
    int ret = -1;
504
    virBuffer attributeBuf = VIR_BUFFER_INITIALIZER;
M
Michal Privoznik 已提交
505 506
    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
    int indent = virBufferGetIndent(buf, false);
507

508 509 510
    if (!def)
        return 0;

511
    /* Format attributes */
512 513 514 515 516
    if (def->type == VIR_CPU_TYPE_GUEST) {
        const char *tmp;

        if (def->mode != VIR_CPU_MODE_CUSTOM || def->model) {
            if (!(tmp = virCPUModeTypeToString(def->mode))) {
517 518
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU mode %d"), def->mode);
519
                goto cleanup;
520
            }
521
            virBufferAsprintf(&attributeBuf, " mode='%s'", tmp);
522 523
        }

524 525
        if (def->model &&
            (def->mode == VIR_CPU_MODE_CUSTOM ||
526
             updateCPU)) {
527
            if (!(tmp = virCPUMatchTypeToString(def->match))) {
528 529 530
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU match policy %d"),
                               def->match);
531
                goto cleanup;
532
            }
533
            virBufferAsprintf(&attributeBuf, " match='%s'", tmp);
534
        }
535 536
    }

537
    /* Format children */
M
Michal Privoznik 已提交
538
    virBufferAdjustIndent(&childrenBuf, indent + 2);
539
    if (def->type == VIR_CPU_TYPE_HOST && def->arch)
M
Michal Privoznik 已提交
540
        virBufferAsprintf(&childrenBuf, "<arch>%s</arch>\n",
541
                          virArchToString(def->arch));
M
Michal Privoznik 已提交
542
    if (virCPUDefFormatBuf(&childrenBuf, def, updateCPU) < 0)
543
        goto cleanup;
544

M
Michal Privoznik 已提交
545
    if (virDomainNumaDefCPUFormat(&childrenBuf, numa) < 0)
546
        goto cleanup;
547

548 549 550 551 552 553 554 555 556 557 558 559 560 561
    /* Put it all together */
    if (virBufferUse(&attributeBuf) || virBufferUse(&childrenBuf)) {
        virBufferAddLit(buf, "<cpu");

        if (virBufferUse(&attributeBuf))
            virBufferAddBuffer(buf, &attributeBuf);

        if (virBufferUse(&childrenBuf)) {
            virBufferAddLit(buf, ">\n");
            virBufferAddBuffer(buf, &childrenBuf);
            virBufferAddLit(buf, "</cpu>\n");
        } else {
            virBufferAddLit(buf, "/>\n");
        }
M
Michal Privoznik 已提交
562
    }
563

564 565
    ret = 0;
 cleanup:
566
    virBufferFreeAndReset(&attributeBuf);
M
Michal Privoznik 已提交
567
    virBufferFreeAndReset(&childrenBuf);
568
    return ret;
569 570
}

571
int
572
virCPUDefFormatBuf(virBufferPtr buf,
573
                   virCPUDefPtr def,
574
                   bool updateCPU)
575
{
576
    size_t i;
577 578
    bool formatModel;
    bool formatFallback;
579 580 581 582

    if (!def)
        return 0;

583
    formatModel = (def->mode == VIR_CPU_MODE_CUSTOM ||
584
                   def->mode == VIR_CPU_MODE_HOST_MODEL ||
585
                   updateCPU);
586 587 588 589
    formatFallback = (def->type == VIR_CPU_TYPE_GUEST &&
                      (def->mode == VIR_CPU_MODE_HOST_MODEL ||
                       (def->mode == VIR_CPU_MODE_CUSTOM && def->model)));

590
    if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM && def->nfeatures) {
591 592
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Non-empty feature list specified without CPU model"));
593 594 595
        return -1;
    }

596
    if ((formatModel && def->model) || formatFallback) {
597
        virBufferAddLit(buf, "<model");
598
        if (formatFallback) {
599 600 601 602
            const char *fallback;

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

619
    if (formatModel && def->vendor)
620
        virBufferEscapeString(buf, "<vendor>%s</vendor>\n", def->vendor);
J
Jiri Denemark 已提交
621

622
    if (def->sockets && def->cores && def->threads) {
623
        virBufferAddLit(buf, "<topology");
624 625 626
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
627 628 629
        virBufferAddLit(buf, "/>\n");
    }

630 631
    for (i = 0; i < def->nfeatures; i++) {
        virCPUFeatureDefPtr feature = def->features + i;
632

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

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

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

    return 0;
}

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

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

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

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

682 683 684 685
            return -1;
        }
    }

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

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

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

    return 0;
}
698

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    identical = true;

823
 cleanup:
824 825
    return identical;
}