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 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 334
        if (!def->model && def->mode != VIR_CPU_MODE_HOST_MODEL &&
            def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {
335
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
336 337
                           _("Non-empty feature list specified without "
                             "CPU model"));
338 339 340
            goto error;
        }

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

345 346 347
        def->nfeatures = n;
    }

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

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

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

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

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

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

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

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

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


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

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

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

    return virBufferContentAndReset(&buf);

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


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

439 440 441
    if (!def)
        return 0;

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

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

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

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

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

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

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

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

    if (!def)
        return 0;

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

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

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

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

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

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

564 565
    for (i = 0; i < def->nfeatures; i++) {
        virCPUFeatureDefPtr feature = def->features + i;
566

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

573 574
        if (def->type == VIR_CPU_TYPE_GUEST) {
            const char *policy;
575

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

    return 0;
}

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

P
Peter Krempa 已提交
602 603 604
    if (def->type == VIR_CPU_TYPE_HOST)
        policy = -1;

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

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

616 617 618 619
            return -1;
        }
    }

E
Eric Blake 已提交
620 621
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
622
        return -1;
623

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

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

    return 0;
}
632

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

649 650 651 652 653
bool
virCPUDefIsEqual(virCPUDefPtr src,
                 virCPUDefPtr dst)
{
    bool identical = false;
654
    size_t i;
655 656 657 658 659

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    identical = true;

757
 cleanup:
758 759
    return identical;
}