cpu_conf.c 22.4 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 88
                   bool resetPolicy)
{
89
    size_t i;
90

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

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

    return 0;
}

117
virCPUDefPtr
118
virCPUDefCopy(const virCPUDef *cpu)
119 120 121
{
    virCPUDefPtr copy;

122
    if (!cpu || VIR_ALLOC(copy) < 0)
123 124 125
        return NULL;

    copy->type = cpu->type;
126
    copy->mode = cpu->mode;
127
    copy->match = cpu->match;
128
    copy->fallback = cpu->fallback;
129 130 131
    copy->sockets = cpu->sockets;
    copy->cores = cpu->cores;
    copy->threads = cpu->threads;
132
    copy->arch = cpu->arch;
133 134 135

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

137 138
    return copy;

139
 error:
140 141 142 143
    virCPUDefFree(copy);
    return NULL;
}

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

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

164
    if (VIR_ALLOC(def) < 0)
165 166
        return NULL;

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

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

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

    if (def->type == VIR_CPU_TYPE_GUEST) {
209 210 211
        char *match = virXMLPropString(node, "match");

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

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

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

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

252
    if (def->type == VIR_CPU_TYPE_GUEST &&
253 254
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {

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

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

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

K
Ken ICHIKAWA 已提交
279
            def->vendor_id = vendor_id;
280
            vendor_id = NULL;
281 282 283
        }
    }

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

291
    if (virXPathNode("./topology[1]", ctxt)) {
292 293 294
        int ret;
        unsigned long ul;

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

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

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

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

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

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

E
Eric Blake 已提交
340 341
        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                         def->nfeatures, n) < 0)
342
            goto error;
P
Peter Krempa 已提交
343

344 345 346
        def->nfeatures = n;
    }

347
    for (i = 0; i < n; i++) {
348 349
        char *name;
        int policy; /* enum virDomainCPUFeaturePolicy */
350
        size_t j;
351 352 353 354 355

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

            strpolicy = virXMLPropString(nodes[i], "policy");
356 357 358 359
            if (strpolicy == NULL)
                policy = VIR_CPU_FEATURE_REQUIRE;
            else
                policy = virCPUFeaturePolicyTypeFromString(strpolicy);
360 361 362
            VIR_FREE(strpolicy);

            if (policy < 0) {
363
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
364
                               _("Invalid CPU feature policy"));
365 366
                goto error;
            }
367
        } else {
368
            policy = -1;
369
        }
370 371 372

        if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
            VIR_FREE(name);
373 374
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU feature name"));
375 376 377
            goto error;
        }

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

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

392
 cleanup:
393
    ctxt->node = oldnode;
394 395
    VIR_FREE(fallback);
    VIR_FREE(vendor_id);
396 397 398
    VIR_FREE(nodes);
    return def;

399
 error:
400 401 402 403 404 405 406
    virCPUDefFree(def);
    def = NULL;
    goto cleanup;
}


char *
407
virCPUDefFormat(virCPUDefPtr def,
408
                virDomainNumaPtr numa,
409
                bool updateCPU)
410 411 412
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

413
    if (virCPUDefFormatBufFull(&buf, def, numa, updateCPU) < 0)
414 415
        goto cleanup;

416 417
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
418 419 420

    return virBufferContentAndReset(&buf);

421
 cleanup:
422
    virBufferFreeAndReset(&buf);
423 424 425 426
    return NULL;
}


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

438 439 440
    if (!def)
        return 0;

441
    /* Format attributes */
442 443 444 445 446
    if (def->type == VIR_CPU_TYPE_GUEST) {
        const char *tmp;

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

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

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

M
Michal Privoznik 已提交
475
    if (virDomainNumaDefCPUFormat(&childrenBuf, numa) < 0)
476
        goto cleanup;
477

478 479 480 481 482 483 484 485 486 487 488 489 490 491
    /* 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 已提交
492
    }
493

494 495
    ret = 0;
 cleanup:
496
    virBufferFreeAndReset(&attributeBuf);
M
Michal Privoznik 已提交
497
    virBufferFreeAndReset(&childrenBuf);
498
    return ret;
499 500
}

501
int
502
virCPUDefFormatBuf(virBufferPtr buf,
503
                   virCPUDefPtr def,
504
                   bool updateCPU)
505
{
506
    size_t i;
507 508
    bool formatModel;
    bool formatFallback;
509 510 511 512

    if (!def)
        return 0;

513
    formatModel = (def->mode == VIR_CPU_MODE_CUSTOM ||
514
                   def->mode == VIR_CPU_MODE_HOST_MODEL ||
515
                   updateCPU);
516 517 518 519
    formatFallback = (def->type == VIR_CPU_TYPE_GUEST &&
                      (def->mode == VIR_CPU_MODE_HOST_MODEL ||
                       (def->mode == VIR_CPU_MODE_CUSTOM && def->model)));

520
    if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM && def->nfeatures) {
521 522
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Non-empty feature list specified without CPU model"));
523 524 525
        return -1;
    }

526
    if ((formatModel && def->model) || formatFallback) {
527
        virBufferAddLit(buf, "<model");
528
        if (formatFallback) {
529 530 531 532
            const char *fallback;

            fallback = virCPUFallbackTypeToString(def->fallback);
            if (!fallback) {
533 534 535
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU fallback value: %d"),
                               def->fallback);
536 537 538
                return -1;
            }
            virBufferAsprintf(buf, " fallback='%s'", fallback);
539
            if (def->vendor_id)
540
                virBufferEscapeString(buf, " vendor_id='%s'", def->vendor_id);
541
        }
542
        if (formatModel && def->model) {
543
            virBufferEscapeString(buf, ">%s</model>\n", def->model);
544 545 546
        } else {
            virBufferAddLit(buf, "/>\n");
        }
547
    }
548

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

552
    if (def->sockets && def->cores && def->threads) {
553
        virBufferAddLit(buf, "<topology");
554 555 556
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
557 558 559
        virBufferAddLit(buf, "/>\n");
    }

560 561
    for (i = 0; i < def->nfeatures; i++) {
        virCPUFeatureDefPtr feature = def->features + i;
562

563 564 565 566 567
        if (!feature->name) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing CPU feature name"));
            return -1;
        }
568

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

572 573 574 575 576 577
            policy = virCPUFeaturePolicyTypeToString(feature->policy);
            if (!policy) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU feature policy %d"),
                               feature->policy);
                return -1;
578
            }
579 580 581 582 583
            virBufferAsprintf(buf, "<feature policy='%s' name='%s'/>\n",
                              policy, feature->name);
        } else {
            virBufferAsprintf(buf, "<feature name='%s'/>\n",
                              feature->name);
584 585 586 587 588 589
        }
    }

    return 0;
}

P
Peter Krempa 已提交
590 591 592 593 594
static int
virCPUDefUpdateFeatureInternal(virCPUDefPtr def,
                               const char *name,
                               int policy,
                               bool update)
595
{
596
    size_t i;
597

P
Peter Krempa 已提交
598 599 600
    if (def->type == VIR_CPU_TYPE_HOST)
        policy = -1;

601
    for (i = 0; i < def->nfeatures; i++) {
602
        if (STREQ(name, def->features[i].name)) {
P
Peter Krempa 已提交
603 604 605 606 607
            if (update) {
                def->features[i].policy = policy;
                return 0;
            }

608
            virReportError(VIR_ERR_INTERNAL_ERROR,
C
Chen Fan 已提交
609
                           _("CPU feature '%s' specified more than once"),
610
                           name);
P
Peter Krempa 已提交
611

612 613 614 615
            return -1;
        }
    }

E
Eric Blake 已提交
616 617
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
618
        return -1;
619

620 621
    if (VIR_STRDUP(def->features[def->nfeatures].name, name) < 0)
        return -1;
622 623 624 625 626 627

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

    return 0;
}
628

P
Peter Krempa 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
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);
}

645 646 647 648 649
bool
virCPUDefIsEqual(virCPUDefPtr src,
                 virCPUDefPtr dst)
{
    bool identical = false;
650
    size_t i;
651 652 653 654 655

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

    if ((src && !dst) || (!src && dst)) {
656 657
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Target CPU does not match source"));
658 659 660 661
        goto cleanup;
    }

    if (src->type != dst->type) {
662 663 664 665
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU type %s does not match source %s"),
                       virCPUTypeToString(dst->type),
                       virCPUTypeToString(src->type));
666 667 668
        goto cleanup;
    }

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

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

    if (STRNEQ_NULLABLE(src->model, dst->model)) {
686 687 688
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU model %s does not match source %s"),
                       NULLSTR(dst->model), NULLSTR(src->model));
689 690 691 692
        goto cleanup;
    }

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

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

706
    if (src->sockets != dst->sockets) {
707 708 709
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU sockets %d does not match source %d"),
                       dst->sockets, src->sockets);
710 711 712 713
        goto cleanup;
    }

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

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

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

734
    for (i = 0; i < src->nfeatures; i++) {
735
        if (STRNEQ(src->features[i].name, dst->features[i].name)) {
736 737 738
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Target CPU feature %s does not match source %s"),
                           dst->features[i].name, src->features[i].name);
739 740 741 742
            goto cleanup;
        }

        if (src->features[i].policy != dst->features[i].policy) {
743 744 745 746
            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));
747 748 749 750 751 752
            goto cleanup;
        }
    }

    identical = true;

753
 cleanup:
754 755
    return identical;
}