cpu_conf.c 22.2 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;
M
Michal Privoznik 已提交
438 439
    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
    int indent = virBufferGetIndent(buf, false);
440

441 442 443
    if (!def)
        return 0;

444 445 446 447 448 449
    virBufferAddLit(buf, "<cpu");
    if (def->type == VIR_CPU_TYPE_GUEST) {
        const char *tmp;

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

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

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

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

M
Michal Privoznik 已提交
480 481 482 483 484 485 486
    if (virBufferUse(&childrenBuf)) {
        virBufferAddLit(buf, ">\n");
        virBufferAddBuffer(buf, &childrenBuf);
        virBufferAddLit(buf, "</cpu>\n");
    } else {
        virBufferAddLit(buf, "/>\n");
    }
487

488 489
    ret = 0;
 cleanup:
M
Michal Privoznik 已提交
490
    virBufferFreeAndReset(&childrenBuf);
491
    return ret;
492 493
}

494
int
495
virCPUDefFormatBuf(virBufferPtr buf,
496
                   virCPUDefPtr def,
497
                   bool updateCPU)
498
{
499
    size_t i;
500 501
    bool formatModel;
    bool formatFallback;
502 503 504 505

    if (!def)
        return 0;

506
    formatModel = (def->mode == VIR_CPU_MODE_CUSTOM ||
507
                   def->mode == VIR_CPU_MODE_HOST_MODEL ||
508
                   updateCPU);
509 510 511 512
    formatFallback = (def->type == VIR_CPU_TYPE_GUEST &&
                      (def->mode == VIR_CPU_MODE_HOST_MODEL ||
                       (def->mode == VIR_CPU_MODE_CUSTOM && def->model)));

513 514
    if (!def->model &&
        def->mode != VIR_CPU_MODE_HOST_MODEL &&
515
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH &&
516
        def->nfeatures) {
517 518
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Non-empty feature list specified without CPU model"));
519 520 521
        return -1;
    }

522
    if ((formatModel && def->model) || formatFallback) {
523
        virBufferAddLit(buf, "<model");
524
        if (formatFallback) {
525 526 527 528
            const char *fallback;

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

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

548
    if (def->sockets && def->cores && def->threads) {
549
        virBufferAddLit(buf, "<topology");
550 551 552
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
553 554 555
        virBufferAddLit(buf, "/>\n");
    }

556 557
    for (i = 0; i < def->nfeatures; i++) {
        virCPUFeatureDefPtr feature = def->features + i;
558

559 560 561 562 563
        if (!feature->name) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing CPU feature name"));
            return -1;
        }
564

565 566
        if (def->type == VIR_CPU_TYPE_GUEST) {
            const char *policy;
567

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

    return 0;
}

P
Peter Krempa 已提交
586 587 588 589 590
static int
virCPUDefUpdateFeatureInternal(virCPUDefPtr def,
                               const char *name,
                               int policy,
                               bool update)
591
{
592
    size_t i;
593

P
Peter Krempa 已提交
594 595 596
    if (def->type == VIR_CPU_TYPE_HOST)
        policy = -1;

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

604
            virReportError(VIR_ERR_INTERNAL_ERROR,
C
Chen Fan 已提交
605
                           _("CPU feature '%s' specified more than once"),
606
                           name);
P
Peter Krempa 已提交
607

608 609 610 611
            return -1;
        }
    }

E
Eric Blake 已提交
612 613
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
614
        return -1;
615

616 617
    if (VIR_STRDUP(def->features[def->nfeatures].name, name) < 0)
        return -1;
618 619 620 621 622 623

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

    return 0;
}
624

P
Peter Krempa 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
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);
}

641 642 643 644 645
bool
virCPUDefIsEqual(virCPUDefPtr src,
                 virCPUDefPtr dst)
{
    bool identical = false;
646
    size_t i;
647 648 649 650 651

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

    if ((src && !dst) || (!src && dst)) {
652 653
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Target CPU does not match source"));
654 655 656 657
        goto cleanup;
    }

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

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

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

    if (STRNEQ_NULLABLE(src->model, dst->model)) {
682 683 684
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU model %s does not match source %s"),
                       NULLSTR(dst->model), NULLSTR(src->model));
685 686 687 688
        goto cleanup;
    }

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

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

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

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

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

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

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

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

    identical = true;

749
 cleanup:
750 751
    return identical;
}