cpu_conf.c 22.6 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
    VIR_FREE(def->vendor_id);
82

83 84 85 86
    VIR_FREE(def);
}


87 88
int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
virCPUDefCopyModel(virCPUDefPtr dst,
89
                   const virCPUDef *src,
90 91
                   bool resetPolicy)
{
92
    size_t i;
93

94 95 96 97
    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)
98
        return -1;
99 100 101 102 103 104 105 106 107 108 109 110 111 112
    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;
        }

113 114
        if (VIR_STRDUP(dst->features[i].name, src->features[i].name) < 0)
            return -1;
115 116 117 118 119
    }

    return 0;
}

120
virCPUDefPtr
121
virCPUDefCopy(const virCPUDef *cpu)
122 123 124
{
    virCPUDefPtr copy;

125
    if (!cpu || VIR_ALLOC(copy) < 0)
126 127 128
        return NULL;

    copy->type = cpu->type;
129
    copy->mode = cpu->mode;
130
    copy->match = cpu->match;
131
    copy->fallback = cpu->fallback;
132 133 134
    copy->sockets = cpu->sockets;
    copy->cores = cpu->cores;
    copy->threads = cpu->threads;
135
    copy->arch = cpu->arch;
136 137 138

    if (virCPUDefCopyModel(copy, cpu, false) < 0)
        goto error;
139

140 141
    return copy;

142
 error:
143 144 145 146
    virCPUDefFree(copy);
    return NULL;
}

147
virCPUDefPtr
148
virCPUDefParseXML(xmlNodePtr node,
149
                  xmlXPathContextPtr ctxt,
150
                  virCPUType mode)
151 152 153
{
    virCPUDefPtr def;
    xmlNodePtr *nodes = NULL;
154
    xmlNodePtr oldnode = ctxt->node;
155
    int n;
156
    size_t i;
157
    char *cpuMode;
158 159
    char *fallback = NULL;
    char *vendor_id = NULL;
160

161
    if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
162
        virReportError(VIR_ERR_XML_ERROR, "%s",
163
                       _("XML does not contain expected 'cpu' element"));
164 165 166
        return NULL;
    }

167
    if (VIR_ALLOC(def) < 0)
168 169
        return NULL;

170
    if (mode == VIR_CPU_TYPE_AUTO) {
171 172
        if (virXPathBoolean("boolean(./arch)", ctxt)) {
            if (virXPathBoolean("boolean(./@match)", ctxt)) {
173
                virReportError(VIR_ERR_XML_ERROR, "%s",
174
                               _("'arch' element cannot be used inside 'cpu'"
175
                                 " element with 'match' attribute'"));
176 177
                goto error;
            }
178
            def->type = VIR_CPU_TYPE_HOST;
179
        } else {
180
            def->type = VIR_CPU_TYPE_GUEST;
181 182
        }
    } else {
183
        def->type = mode;
184 185 186 187 188
    }

    if ((cpuMode = virXMLPropString(node, "mode"))) {
        if (def->type == VIR_CPU_TYPE_HOST) {
            VIR_FREE(cpuMode);
189 190
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Attribute mode is only allowed for guest CPU"));
191 192 193 194 195
            goto error;
        } else {
            def->mode = virCPUModeTypeFromString(cpuMode);

            if (def->mode < 0) {
196
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
197 198
                               _("Invalid mode attribute '%s'"),
                               cpuMode);
199
                VIR_FREE(cpuMode);
200 201
                goto error;
            }
202
            VIR_FREE(cpuMode);
203 204 205 206 207 208 209
        }
    } else {
        if (def->type == VIR_CPU_TYPE_HOST)
            def->mode = -1;
        else
            def->mode = VIR_CPU_MODE_CUSTOM;
    }
210 211

    if (def->type == VIR_CPU_TYPE_GUEST) {
212 213 214
        char *match = virXMLPropString(node, "match");

        if (!match) {
215 216 217 218
            if (virXPathBoolean("boolean(./model)", ctxt))
                def->match = VIR_CPU_MATCH_EXACT;
            else
                def->match = -1;
219 220 221 222 223
        } else {
            def->match = virCPUMatchTypeFromString(match);
            VIR_FREE(match);

            if (def->match < 0) {
224
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
P
Peter Krempa 已提交
225 226
                               _("Invalid match attribute for CPU "
                                 "specification"));
227 228
                goto error;
            }
229 230 231 232
        }
    }

    if (def->type == VIR_CPU_TYPE_HOST) {
233 234
        char *arch = virXPathString("string(./arch[1])", ctxt);
        if (!arch) {
235 236
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Missing CPU architecture"));
237 238
            goto error;
        }
239
        if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) {
240
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
241 242 243 244 245
                           _("Unknown architecture %s"), arch);
            VIR_FREE(arch);
            goto error;
        }
        VIR_FREE(arch);
246 247
    }

248
    if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
249
        def->type == VIR_CPU_TYPE_HOST) {
250 251
        virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("Missing CPU model name"));
252 253 254
        goto error;
    }

255
    if (def->type == VIR_CPU_TYPE_GUEST &&
256 257
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {

258 259
        if ((fallback = virXPathString("string(./model[1]/@fallback)", ctxt))) {
            if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
260
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
261 262
                               _("Invalid fallback attribute"));
                goto error;
263
            }
K
Ken ICHIKAWA 已提交
264
        }
265

266 267 268
        if ((vendor_id = virXPathString("string(./model[1]/@vendor_id)",
                                        ctxt))) {
            if (strlen(vendor_id) != VIR_CPU_VENDOR_ID_LENGTH) {
K
Ken ICHIKAWA 已提交
269
                virReportError(VIR_ERR_XML_ERROR,
270
                               _("vendor_id must be exactly %d characters long"),
K
Ken ICHIKAWA 已提交
271 272 273
                               VIR_CPU_VENDOR_ID_LENGTH);
                goto error;
            }
274

K
Ken ICHIKAWA 已提交
275
            /* ensure that the string can be passed to qemu*/
276
            if (strchr(vendor_id, ',')) {
K
Ken ICHIKAWA 已提交
277 278
                    virReportError(VIR_ERR_XML_ERROR, "%s",
                                   _("vendor id is invalid"));
279
                    goto error;
280
            }
281

K
Ken ICHIKAWA 已提交
282
            def->vendor_id = vendor_id;
283
            vendor_id = NULL;
284 285 286
        }
    }

J
Jiri Denemark 已提交
287 288
    def->vendor = virXPathString("string(./vendor[1])", ctxt);
    if (def->vendor && !def->model) {
289 290
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("CPU vendor specified without CPU model"));
J
Jiri Denemark 已提交
291 292 293
        goto error;
    }

294
    if (virXPathNode("./topology[1]", ctxt)) {
295 296 297
        int ret;
        unsigned long ul;

298
        ret = virXPathULong("string(./topology[1]/@sockets)",
299 300
                            ctxt, &ul);
        if (ret < 0) {
301
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
302
                           _("Missing 'sockets' attribute in CPU topology"));
303 304 305 306
            goto error;
        }
        def->sockets = (unsigned int) ul;

307
        ret = virXPathULong("string(./topology[1]/@cores)",
308 309
                            ctxt, &ul);
        if (ret < 0) {
310
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
311
                           _("Missing 'cores' attribute in CPU topology"));
312 313 314 315
            goto error;
        }
        def->cores = (unsigned int) ul;

316
        ret = virXPathULong("string(./topology[1]/@threads)",
317 318
                            ctxt, &ul);
        if (ret < 0) {
319
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
320
                           _("Missing 'threads' attribute in CPU topology"));
321 322 323 324 325
            goto error;
        }
        def->threads = (unsigned int) ul;

        if (!def->sockets || !def->cores || !def->threads) {
326 327
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU topology"));
328 329 330 331
            goto error;
        }
    }

P
Peter Krempa 已提交
332
    if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
333 334 335
        goto error;

    if (n > 0) {
336 337
        if (!def->model && def->mode != VIR_CPU_MODE_HOST_MODEL &&
            def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {
338
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
339 340
                           _("Non-empty feature list specified without "
                             "CPU model"));
341 342 343
            goto error;
        }

E
Eric Blake 已提交
344 345
        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                         def->nfeatures, n) < 0)
346
            goto error;
P
Peter Krempa 已提交
347

348 349 350
        def->nfeatures = n;
    }

351
    for (i = 0; i < n; i++) {
352 353
        char *name;
        int policy; /* enum virDomainCPUFeaturePolicy */
354
        size_t j;
355 356 357 358 359

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

            strpolicy = virXMLPropString(nodes[i], "policy");
360 361 362 363
            if (strpolicy == NULL)
                policy = VIR_CPU_FEATURE_REQUIRE;
            else
                policy = virCPUFeaturePolicyTypeFromString(strpolicy);
364 365 366
            VIR_FREE(strpolicy);

            if (policy < 0) {
367
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
368
                               _("Invalid CPU feature policy"));
369 370
                goto error;
            }
371
        } else {
372
            policy = -1;
373
        }
374 375 376

        if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
            VIR_FREE(name);
377 378
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU feature name"));
379 380 381
            goto error;
        }

382
        for (j = 0; j < i; j++) {
383
            if (STREQ(name, def->features[j].name)) {
384
                virReportError(VIR_ERR_XML_ERROR,
C
Chen Fan 已提交
385
                               _("CPU feature '%s' specified more than once"),
386
                               name);
387 388 389 390 391 392 393 394 395
                VIR_FREE(name);
                goto error;
            }
        }

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

396
 cleanup:
397
    ctxt->node = oldnode;
398 399
    VIR_FREE(fallback);
    VIR_FREE(vendor_id);
400 401 402
    VIR_FREE(nodes);
    return def;

403
 error:
404 405 406 407 408 409 410
    virCPUDefFree(def);
    def = NULL;
    goto cleanup;
}


char *
411
virCPUDefFormat(virCPUDefPtr def,
412
                virDomainNumaPtr numa,
413
                bool updateCPU)
414 415 416
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

417
    if (virCPUDefFormatBufFull(&buf, def, numa, updateCPU) < 0)
418 419
        goto cleanup;

420 421
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
422 423 424

    return virBufferContentAndReset(&buf);

425
 cleanup:
426
    virBufferFreeAndReset(&buf);
427 428 429 430
    return NULL;
}


431 432
int
virCPUDefFormatBufFull(virBufferPtr buf,
433
                       virCPUDefPtr def,
434
                       virDomainNumaPtr numa,
435
                       bool updateCPU)
436
{
437
    int ret = -1;
438
    virBuffer attributeBuf = VIR_BUFFER_INITIALIZER;
M
Michal Privoznik 已提交
439 440
    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
    int indent = virBufferGetIndent(buf, false);
441

442 443 444
    if (!def)
        return 0;

445
    /* Format attributes */
446 447 448 449 450
    if (def->type == VIR_CPU_TYPE_GUEST) {
        const char *tmp;

        if (def->mode != VIR_CPU_MODE_CUSTOM || def->model) {
            if (!(tmp = virCPUModeTypeToString(def->mode))) {
451 452
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU mode %d"), def->mode);
453
                goto cleanup;
454
            }
455
            virBufferAsprintf(&attributeBuf, " mode='%s'", tmp);
456 457
        }

458 459
        if (def->model &&
            (def->mode == VIR_CPU_MODE_CUSTOM ||
460
             updateCPU)) {
461
            if (!(tmp = virCPUMatchTypeToString(def->match))) {
462 463 464
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU match policy %d"),
                               def->match);
465
                goto cleanup;
466
            }
467
            virBufferAsprintf(&attributeBuf, " match='%s'", tmp);
468
        }
469 470
    }

471
    /* Format children */
M
Michal Privoznik 已提交
472
    virBufferAdjustIndent(&childrenBuf, indent + 2);
473
    if (def->arch)
M
Michal Privoznik 已提交
474
        virBufferAsprintf(&childrenBuf, "<arch>%s</arch>\n",
475
                          virArchToString(def->arch));
M
Michal Privoznik 已提交
476
    if (virCPUDefFormatBuf(&childrenBuf, def, updateCPU) < 0)
477
        goto cleanup;
478

M
Michal Privoznik 已提交
479
    if (virDomainNumaDefCPUFormat(&childrenBuf, numa) < 0)
480
        goto cleanup;
481

482 483 484 485 486 487 488 489 490 491 492 493 494 495
    /* 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 已提交
496
    }
497

498 499
    ret = 0;
 cleanup:
500
    virBufferFreeAndReset(&attributeBuf);
M
Michal Privoznik 已提交
501
    virBufferFreeAndReset(&childrenBuf);
502
    return ret;
503 504
}

505
int
506
virCPUDefFormatBuf(virBufferPtr buf,
507
                   virCPUDefPtr def,
508
                   bool updateCPU)
509
{
510
    size_t i;
511 512
    bool formatModel;
    bool formatFallback;
513 514 515 516

    if (!def)
        return 0;

517
    formatModel = (def->mode == VIR_CPU_MODE_CUSTOM ||
518
                   def->mode == VIR_CPU_MODE_HOST_MODEL ||
519
                   updateCPU);
520 521 522 523
    formatFallback = (def->type == VIR_CPU_TYPE_GUEST &&
                      (def->mode == VIR_CPU_MODE_HOST_MODEL ||
                       (def->mode == VIR_CPU_MODE_CUSTOM && def->model)));

524 525
    if (!def->model &&
        def->mode != VIR_CPU_MODE_HOST_MODEL &&
526
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH &&
527
        def->nfeatures) {
528 529
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Non-empty feature list specified without CPU model"));
530 531 532
        return -1;
    }

533
    if ((formatModel && def->model) || formatFallback) {
534
        virBufferAddLit(buf, "<model");
535
        if (formatFallback) {
536 537 538 539
            const char *fallback;

            fallback = virCPUFallbackTypeToString(def->fallback);
            if (!fallback) {
540 541 542
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU fallback value: %d"),
                               def->fallback);
543 544 545
                return -1;
            }
            virBufferAsprintf(buf, " fallback='%s'", fallback);
546 547
            if (def->vendor_id)
                virBufferAsprintf(buf, " vendor_id='%s'", def->vendor_id);
548
        }
549 550 551 552 553
        if (formatModel && def->model) {
            virBufferAsprintf(buf, ">%s</model>\n", def->model);
        } else {
            virBufferAddLit(buf, "/>\n");
        }
554
    }
555

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

559
    if (def->sockets && def->cores && def->threads) {
560
        virBufferAddLit(buf, "<topology");
561 562 563
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
564 565 566
        virBufferAddLit(buf, "/>\n");
    }

567 568
    for (i = 0; i < def->nfeatures; i++) {
        virCPUFeatureDefPtr feature = def->features + i;
569

570 571 572 573 574
        if (!feature->name) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing CPU feature name"));
            return -1;
        }
575

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

579 580 581 582 583 584
            policy = virCPUFeaturePolicyTypeToString(feature->policy);
            if (!policy) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU feature policy %d"),
                               feature->policy);
                return -1;
585
            }
586 587 588 589 590
            virBufferAsprintf(buf, "<feature policy='%s' name='%s'/>\n",
                              policy, feature->name);
        } else {
            virBufferAsprintf(buf, "<feature name='%s'/>\n",
                              feature->name);
591 592 593 594 595 596
        }
    }

    return 0;
}

P
Peter Krempa 已提交
597 598 599 600 601
static int
virCPUDefUpdateFeatureInternal(virCPUDefPtr def,
                               const char *name,
                               int policy,
                               bool update)
602
{
603
    size_t i;
604

P
Peter Krempa 已提交
605 606 607
    if (def->type == VIR_CPU_TYPE_HOST)
        policy = -1;

608
    for (i = 0; i < def->nfeatures; i++) {
609
        if (STREQ(name, def->features[i].name)) {
P
Peter Krempa 已提交
610 611 612 613 614
            if (update) {
                def->features[i].policy = policy;
                return 0;
            }

615
            virReportError(VIR_ERR_INTERNAL_ERROR,
C
Chen Fan 已提交
616
                           _("CPU feature '%s' specified more than once"),
617
                           name);
P
Peter Krempa 已提交
618

619 620 621 622
            return -1;
        }
    }

E
Eric Blake 已提交
623 624
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
625
        return -1;
626

627 628
    if (VIR_STRDUP(def->features[def->nfeatures].name, name) < 0)
        return -1;
629 630 631 632 633 634

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

    return 0;
}
635

P
Peter Krempa 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
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);
}

652 653 654 655 656
bool
virCPUDefIsEqual(virCPUDefPtr src,
                 virCPUDefPtr dst)
{
    bool identical = false;
657
    size_t i;
658 659 660 661 662

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

    if ((src && !dst) || (!src && dst)) {
663 664
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Target CPU does not match source"));
665 666 667 668
        goto cleanup;
    }

    if (src->type != dst->type) {
669 670 671 672
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU type %s does not match source %s"),
                       virCPUTypeToString(dst->type),
                       virCPUTypeToString(src->type));
673 674 675
        goto cleanup;
    }

676
    if (src->mode != dst->mode) {
677 678 679 680
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU mode %s does not match source %s"),
                       virCPUModeTypeToString(dst->mode),
                       virCPUModeTypeToString(src->mode));
681 682 683
        goto cleanup;
    }

684
    if (src->arch != dst->arch) {
685 686
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU arch %s does not match source %s"),
687 688
                       virArchToString(dst->arch),
                       virArchToString(src->arch));
689 690 691 692
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(src->model, dst->model)) {
693 694 695
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU model %s does not match source %s"),
                       NULLSTR(dst->model), NULLSTR(src->model));
696 697 698 699
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(src->vendor, dst->vendor)) {
700 701 702
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU vendor %s does not match source %s"),
                       NULLSTR(dst->vendor), NULLSTR(src->vendor));
703 704 705
        goto cleanup;
    }

706
    if (STRNEQ_NULLABLE(src->vendor_id, dst->vendor_id)) {
707
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
708
                       _("Target CPU vendor id %s does not match source %s"),
709
                       NULLSTR(dst->vendor_id), NULLSTR(src->vendor_id));
710 711 712
        goto cleanup;
    }

713
    if (src->sockets != dst->sockets) {
714 715 716
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU sockets %d does not match source %d"),
                       dst->sockets, src->sockets);
717 718 719 720
        goto cleanup;
    }

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

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

    if (src->nfeatures != dst->nfeatures) {
735 736 737
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU feature count %zu does not match source %zu"),
                       dst->nfeatures, src->nfeatures);
738 739 740
        goto cleanup;
    }

741
    for (i = 0; i < src->nfeatures; i++) {
742
        if (STRNEQ(src->features[i].name, dst->features[i].name)) {
743 744 745
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Target CPU feature %s does not match source %s"),
                           dst->features[i].name, src->features[i].name);
746 747 748 749
            goto cleanup;
        }

        if (src->features[i].policy != dst->features[i].policy) {
750 751 752 753
            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));
754 755 756 757 758 759
            goto cleanup;
        }
    }

    identical = true;

760
 cleanup:
761 762
    return identical;
}