cpu_conf.c 23.5 KB
Newer Older
1
/*
2
 * cpu_conf.c: CPU XML handling
3
 *
4
 * Copyright (C) 2009-2015 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25
 *
 * Authors:
 *      Jiri Denemark <jdenemar@redhat.com>
 */

#include <config.h>

26
#include "virerror.h"
27
#include "viralloc.h"
28
#include "virbuffer.h"
29
#include "cpu_conf.h"
30
#include "domain_conf.h"
31
#include "virstring.h"
32 33 34

#define VIR_FROM_THIS VIR_FROM_CPU

35 36 37
VIR_ENUM_IMPL(virCPU, VIR_CPU_TYPE_LAST,
              "host", "guest", "auto")

38 39 40 41 42
VIR_ENUM_IMPL(virCPUMode, VIR_CPU_MODE_LAST,
              "custom",
              "host-model",
              "host-passthrough")

43 44 45 46 47
VIR_ENUM_IMPL(virCPUMatch, VIR_CPU_MATCH_LAST,
              "minimum",
              "exact",
              "strict")

48 49 50 51
VIR_ENUM_IMPL(virCPUFallback, VIR_CPU_FALLBACK_LAST,
              "allow",
              "forbid")

52 53 54 55 56 57 58
VIR_ENUM_IMPL(virCPUFeaturePolicy, VIR_CPU_FEATURE_LAST,
              "force",
              "require",
              "optional",
              "disable",
              "forbid")

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

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

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

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

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


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


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

103 104 105 106
    if (VIR_STRDUP(dst->model, src->model) < 0 ||
        VIR_STRDUP(dst->vendor, src->vendor) < 0 ||
        VIR_STRDUP(dst->vendor_id, src->vendor_id) < 0 ||
        VIR_ALLOC_N(dst->features, src->nfeatures) < 0)
107
        return -1;
108 109 110 111 112 113
    dst->nfeatures_max = src->nfeatures;
    dst->nfeatures = 0;

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

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

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

    return 0;
}

134

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
void
virCPUDefStealModel(virCPUDefPtr dst,
                    virCPUDefPtr src)
{
    virCPUDefFreeModel(dst);

    VIR_STEAL_PTR(dst->model, src->model);
    VIR_STEAL_PTR(dst->vendor, src->vendor);
    VIR_STEAL_PTR(dst->vendor_id, src->vendor_id);
    VIR_STEAL_PTR(dst->features, src->features);
    dst->nfeatures_max = src->nfeatures_max;
    src->nfeatures_max = 0;
    dst->nfeatures = src->nfeatures;
    src->nfeatures = 0;
}


152
virCPUDefPtr
153
virCPUDefCopyWithoutModel(const virCPUDef *cpu)
154 155 156
{
    virCPUDefPtr copy;

157
    if (!cpu || VIR_ALLOC(copy) < 0)
158 159 160
        return NULL;

    copy->type = cpu->type;
161
    copy->mode = cpu->mode;
162
    copy->match = cpu->match;
163
    copy->fallback = cpu->fallback;
164 165 166
    copy->sockets = cpu->sockets;
    copy->cores = cpu->cores;
    copy->threads = cpu->threads;
167
    copy->arch = cpu->arch;
168

169 170 171 172 173 174 175 176 177 178 179 180
    return copy;
}


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

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

181 182
    if (virCPUDefCopyModel(copy, cpu, false) < 0)
        goto error;
183

184 185
    return copy;

186
 error:
187 188 189 190
    virCPUDefFree(copy);
    return NULL;
}

191

192
virCPUDefPtr
193
virCPUDefParseXML(xmlNodePtr node,
194
                  xmlXPathContextPtr ctxt,
195
                  virCPUType mode)
196 197 198
{
    virCPUDefPtr def;
    xmlNodePtr *nodes = NULL;
199
    xmlNodePtr oldnode = ctxt->node;
200
    int n;
201
    size_t i;
202
    char *cpuMode;
203 204
    char *fallback = NULL;
    char *vendor_id = NULL;
205

206
    if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
207
        virReportError(VIR_ERR_XML_ERROR, "%s",
208
                       _("XML does not contain expected 'cpu' element"));
209 210 211
        return NULL;
    }

212
    if (VIR_ALLOC(def) < 0)
213 214
        return NULL;

215
    if (mode == VIR_CPU_TYPE_AUTO) {
216 217
        if (virXPathBoolean("boolean(./arch)", ctxt)) {
            if (virXPathBoolean("boolean(./@match)", ctxt)) {
218
                virReportError(VIR_ERR_XML_ERROR, "%s",
219
                               _("'arch' element cannot be used inside 'cpu'"
220
                                 " element with 'match' attribute'"));
221 222
                goto error;
            }
223
            def->type = VIR_CPU_TYPE_HOST;
224
        } else {
225
            def->type = VIR_CPU_TYPE_GUEST;
226 227
        }
    } else {
228
        def->type = mode;
229 230 231 232 233
    }

    if ((cpuMode = virXMLPropString(node, "mode"))) {
        if (def->type == VIR_CPU_TYPE_HOST) {
            VIR_FREE(cpuMode);
234 235
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Attribute mode is only allowed for guest CPU"));
236 237 238 239 240
            goto error;
        } else {
            def->mode = virCPUModeTypeFromString(cpuMode);

            if (def->mode < 0) {
241
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
242 243
                               _("Invalid mode attribute '%s'"),
                               cpuMode);
244
                VIR_FREE(cpuMode);
245 246
                goto error;
            }
247
            VIR_FREE(cpuMode);
248 249 250 251 252 253 254
        }
    } else {
        if (def->type == VIR_CPU_TYPE_HOST)
            def->mode = -1;
        else
            def->mode = VIR_CPU_MODE_CUSTOM;
    }
255 256

    if (def->type == VIR_CPU_TYPE_GUEST) {
257 258 259
        char *match = virXMLPropString(node, "match");

        if (!match) {
260 261 262 263
            if (virXPathBoolean("boolean(./model)", ctxt))
                def->match = VIR_CPU_MATCH_EXACT;
            else
                def->match = -1;
264 265 266 267 268
        } else {
            def->match = virCPUMatchTypeFromString(match);
            VIR_FREE(match);

            if (def->match < 0) {
269
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
P
Peter Krempa 已提交
270 271
                               _("Invalid match attribute for CPU "
                                 "specification"));
272 273
                goto error;
            }
274 275 276 277
        }
    }

    if (def->type == VIR_CPU_TYPE_HOST) {
278 279
        char *arch = virXPathString("string(./arch[1])", ctxt);
        if (!arch) {
280 281
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Missing CPU architecture"));
282 283
            goto error;
        }
284
        if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) {
285
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
286 287 288 289 290
                           _("Unknown architecture %s"), arch);
            VIR_FREE(arch);
            goto error;
        }
        VIR_FREE(arch);
291 292
    }

293
    if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
294
        def->type == VIR_CPU_TYPE_HOST) {
295 296
        virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("Missing CPU model name"));
297 298 299
        goto error;
    }

300
    if (def->type == VIR_CPU_TYPE_GUEST &&
301 302
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {

303 304
        if ((fallback = virXPathString("string(./model[1]/@fallback)", ctxt))) {
            if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
305
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
306 307
                               _("Invalid fallback attribute"));
                goto error;
308
            }
K
Ken ICHIKAWA 已提交
309
        }
310

311 312 313
        if ((vendor_id = virXPathString("string(./model[1]/@vendor_id)",
                                        ctxt))) {
            if (strlen(vendor_id) != VIR_CPU_VENDOR_ID_LENGTH) {
K
Ken ICHIKAWA 已提交
314
                virReportError(VIR_ERR_XML_ERROR,
315
                               _("vendor_id must be exactly %d characters long"),
K
Ken ICHIKAWA 已提交
316 317 318
                               VIR_CPU_VENDOR_ID_LENGTH);
                goto error;
            }
319

K
Ken ICHIKAWA 已提交
320
            /* ensure that the string can be passed to qemu*/
321
            if (strchr(vendor_id, ',')) {
K
Ken ICHIKAWA 已提交
322 323
                    virReportError(VIR_ERR_XML_ERROR, "%s",
                                   _("vendor id is invalid"));
324
                    goto error;
325
            }
326

K
Ken ICHIKAWA 已提交
327
            def->vendor_id = vendor_id;
328
            vendor_id = NULL;
329 330 331
        }
    }

J
Jiri Denemark 已提交
332 333
    def->vendor = virXPathString("string(./vendor[1])", ctxt);
    if (def->vendor && !def->model) {
334 335
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("CPU vendor specified without CPU model"));
J
Jiri Denemark 已提交
336 337 338
        goto error;
    }

339
    if (virXPathNode("./topology[1]", ctxt)) {
340 341 342
        int ret;
        unsigned long ul;

343
        ret = virXPathULong("string(./topology[1]/@sockets)",
344 345
                            ctxt, &ul);
        if (ret < 0) {
346
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
347
                           _("Missing 'sockets' attribute in CPU topology"));
348 349 350 351
            goto error;
        }
        def->sockets = (unsigned int) ul;

352
        ret = virXPathULong("string(./topology[1]/@cores)",
353 354
                            ctxt, &ul);
        if (ret < 0) {
355
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
356
                           _("Missing 'cores' attribute in CPU topology"));
357 358 359 360
            goto error;
        }
        def->cores = (unsigned int) ul;

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

        if (!def->sockets || !def->cores || !def->threads) {
371 372
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU topology"));
373 374 375 376
            goto error;
        }
    }

P
Peter Krempa 已提交
377
    if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
378 379 380
        goto error;

    if (n > 0) {
381
        if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM) {
382
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
383 384
                           _("Non-empty feature list specified without "
                             "CPU model"));
385 386 387
            goto error;
        }

E
Eric Blake 已提交
388 389
        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                         def->nfeatures, n) < 0)
390
            goto error;
P
Peter Krempa 已提交
391

392 393 394
        def->nfeatures = n;
    }

395
    for (i = 0; i < n; i++) {
396 397
        char *name;
        int policy; /* enum virDomainCPUFeaturePolicy */
398
        size_t j;
399 400 401 402 403

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

            strpolicy = virXMLPropString(nodes[i], "policy");
404 405 406 407
            if (strpolicy == NULL)
                policy = VIR_CPU_FEATURE_REQUIRE;
            else
                policy = virCPUFeaturePolicyTypeFromString(strpolicy);
408 409 410
            VIR_FREE(strpolicy);

            if (policy < 0) {
411
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
412
                               _("Invalid CPU feature policy"));
413 414
                goto error;
            }
415
        } else {
416
            policy = -1;
417
        }
418 419 420

        if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
            VIR_FREE(name);
421 422
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU feature name"));
423 424 425
            goto error;
        }

426
        for (j = 0; j < i; j++) {
427
            if (STREQ(name, def->features[j].name)) {
428
                virReportError(VIR_ERR_XML_ERROR,
C
Chen Fan 已提交
429
                               _("CPU feature '%s' specified more than once"),
430
                               name);
431 432 433 434 435 436 437 438 439
                VIR_FREE(name);
                goto error;
            }
        }

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

440
 cleanup:
441
    ctxt->node = oldnode;
442 443
    VIR_FREE(fallback);
    VIR_FREE(vendor_id);
444 445 446
    VIR_FREE(nodes);
    return def;

447
 error:
448 449 450 451 452 453 454
    virCPUDefFree(def);
    def = NULL;
    goto cleanup;
}


char *
455
virCPUDefFormat(virCPUDefPtr def,
456
                virDomainNumaPtr numa,
457
                bool updateCPU)
458 459 460
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

461
    if (virCPUDefFormatBufFull(&buf, def, numa, updateCPU) < 0)
462 463
        goto cleanup;

464 465
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
466 467 468

    return virBufferContentAndReset(&buf);

469
 cleanup:
470
    virBufferFreeAndReset(&buf);
471 472 473 474
    return NULL;
}


475 476
int
virCPUDefFormatBufFull(virBufferPtr buf,
477
                       virCPUDefPtr def,
478
                       virDomainNumaPtr numa,
479
                       bool updateCPU)
480
{
481
    int ret = -1;
482
    virBuffer attributeBuf = VIR_BUFFER_INITIALIZER;
M
Michal Privoznik 已提交
483 484
    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
    int indent = virBufferGetIndent(buf, false);
485

486 487 488
    if (!def)
        return 0;

489
    /* Format attributes */
490 491 492 493 494
    if (def->type == VIR_CPU_TYPE_GUEST) {
        const char *tmp;

        if (def->mode != VIR_CPU_MODE_CUSTOM || def->model) {
            if (!(tmp = virCPUModeTypeToString(def->mode))) {
495 496
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU mode %d"), def->mode);
497
                goto cleanup;
498
            }
499
            virBufferAsprintf(&attributeBuf, " mode='%s'", tmp);
500 501
        }

502 503
        if (def->model &&
            (def->mode == VIR_CPU_MODE_CUSTOM ||
504
             updateCPU)) {
505
            if (!(tmp = virCPUMatchTypeToString(def->match))) {
506 507 508
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU match policy %d"),
                               def->match);
509
                goto cleanup;
510
            }
511
            virBufferAsprintf(&attributeBuf, " match='%s'", tmp);
512
        }
513 514
    }

515
    /* Format children */
M
Michal Privoznik 已提交
516
    virBufferAdjustIndent(&childrenBuf, indent + 2);
517
    if (def->arch)
M
Michal Privoznik 已提交
518
        virBufferAsprintf(&childrenBuf, "<arch>%s</arch>\n",
519
                          virArchToString(def->arch));
M
Michal Privoznik 已提交
520
    if (virCPUDefFormatBuf(&childrenBuf, def, updateCPU) < 0)
521
        goto cleanup;
522

M
Michal Privoznik 已提交
523
    if (virDomainNumaDefCPUFormat(&childrenBuf, numa) < 0)
524
        goto cleanup;
525

526 527 528 529 530 531 532 533 534 535 536 537 538 539
    /* 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 已提交
540
    }
541

542 543
    ret = 0;
 cleanup:
544
    virBufferFreeAndReset(&attributeBuf);
M
Michal Privoznik 已提交
545
    virBufferFreeAndReset(&childrenBuf);
546
    return ret;
547 548
}

549
int
550
virCPUDefFormatBuf(virBufferPtr buf,
551
                   virCPUDefPtr def,
552
                   bool updateCPU)
553
{
554
    size_t i;
555 556
    bool formatModel;
    bool formatFallback;
557 558 559 560

    if (!def)
        return 0;

561
    formatModel = (def->mode == VIR_CPU_MODE_CUSTOM ||
562
                   def->mode == VIR_CPU_MODE_HOST_MODEL ||
563
                   updateCPU);
564 565 566 567
    formatFallback = (def->type == VIR_CPU_TYPE_GUEST &&
                      (def->mode == VIR_CPU_MODE_HOST_MODEL ||
                       (def->mode == VIR_CPU_MODE_CUSTOM && def->model)));

568
    if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM && def->nfeatures) {
569 570
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Non-empty feature list specified without CPU model"));
571 572 573
        return -1;
    }

574
    if ((formatModel && def->model) || formatFallback) {
575
        virBufferAddLit(buf, "<model");
576
        if (formatFallback) {
577 578 579 580
            const char *fallback;

            fallback = virCPUFallbackTypeToString(def->fallback);
            if (!fallback) {
581 582 583
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU fallback value: %d"),
                               def->fallback);
584 585 586
                return -1;
            }
            virBufferAsprintf(buf, " fallback='%s'", fallback);
587
            if (def->vendor_id)
588
                virBufferEscapeString(buf, " vendor_id='%s'", def->vendor_id);
589
        }
590
        if (formatModel && def->model) {
591
            virBufferEscapeString(buf, ">%s</model>\n", def->model);
592 593 594
        } else {
            virBufferAddLit(buf, "/>\n");
        }
595
    }
596

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

600
    if (def->sockets && def->cores && def->threads) {
601
        virBufferAddLit(buf, "<topology");
602 603 604
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
605 606 607
        virBufferAddLit(buf, "/>\n");
    }

608 609
    for (i = 0; i < def->nfeatures; i++) {
        virCPUFeatureDefPtr feature = def->features + i;
610

611 612 613 614 615
        if (!feature->name) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing CPU feature name"));
            return -1;
        }
616

617 618
        if (def->type == VIR_CPU_TYPE_GUEST) {
            const char *policy;
619

620 621 622 623 624 625
            policy = virCPUFeaturePolicyTypeToString(feature->policy);
            if (!policy) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU feature policy %d"),
                               feature->policy);
                return -1;
626
            }
627 628 629 630 631
            virBufferAsprintf(buf, "<feature policy='%s' name='%s'/>\n",
                              policy, feature->name);
        } else {
            virBufferAsprintf(buf, "<feature name='%s'/>\n",
                              feature->name);
632 633 634 635 636 637
        }
    }

    return 0;
}

P
Peter Krempa 已提交
638 639 640 641 642
static int
virCPUDefUpdateFeatureInternal(virCPUDefPtr def,
                               const char *name,
                               int policy,
                               bool update)
643
{
644
    size_t i;
645

P
Peter Krempa 已提交
646 647 648
    if (def->type == VIR_CPU_TYPE_HOST)
        policy = -1;

649
    for (i = 0; i < def->nfeatures; i++) {
650
        if (STREQ(name, def->features[i].name)) {
P
Peter Krempa 已提交
651 652 653 654 655
            if (update) {
                def->features[i].policy = policy;
                return 0;
            }

656
            virReportError(VIR_ERR_INTERNAL_ERROR,
C
Chen Fan 已提交
657
                           _("CPU feature '%s' specified more than once"),
658
                           name);
P
Peter Krempa 已提交
659

660 661 662 663
            return -1;
        }
    }

E
Eric Blake 已提交
664 665
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
666
        return -1;
667

668 669
    if (VIR_STRDUP(def->features[def->nfeatures].name, name) < 0)
        return -1;
670 671 672 673 674 675

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

    return 0;
}
676

P
Peter Krempa 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
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);
}

693 694 695 696 697
bool
virCPUDefIsEqual(virCPUDefPtr src,
                 virCPUDefPtr dst)
{
    bool identical = false;
698
    size_t i;
699 700 701 702 703

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

    if ((src && !dst) || (!src && dst)) {
704 705
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Target CPU does not match source"));
706 707 708 709
        goto cleanup;
    }

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

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

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

    if (STRNEQ_NULLABLE(src->model, dst->model)) {
734 735 736
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU model %s does not match source %s"),
                       NULLSTR(dst->model), NULLSTR(src->model));
737 738 739 740
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(src->vendor, dst->vendor)) {
741 742 743
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU vendor %s does not match source %s"),
                       NULLSTR(dst->vendor), NULLSTR(src->vendor));
744 745 746
        goto cleanup;
    }

747
    if (STRNEQ_NULLABLE(src->vendor_id, dst->vendor_id)) {
748
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
749
                       _("Target CPU vendor id %s does not match source %s"),
750
                       NULLSTR(dst->vendor_id), NULLSTR(src->vendor_id));
751 752 753
        goto cleanup;
    }

754
    if (src->sockets != dst->sockets) {
755 756 757
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU sockets %d does not match source %d"),
                       dst->sockets, src->sockets);
758 759 760 761
        goto cleanup;
    }

    if (src->cores != dst->cores) {
762 763 764
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU cores %d does not match source %d"),
                       dst->cores, src->cores);
765 766 767 768
        goto cleanup;
    }

    if (src->threads != dst->threads) {
769 770 771
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU threads %d does not match source %d"),
                       dst->threads, src->threads);
772 773 774 775
        goto cleanup;
    }

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

782
    for (i = 0; i < src->nfeatures; i++) {
783
        if (STRNEQ(src->features[i].name, dst->features[i].name)) {
784 785 786
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Target CPU feature %s does not match source %s"),
                           dst->features[i].name, src->features[i].name);
787 788 789 790
            goto cleanup;
        }

        if (src->features[i].policy != dst->features[i].policy) {
791 792 793 794
            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));
795 796 797 798 799 800
            goto cleanup;
        }
    }

    identical = true;

801
 cleanup:
802 803
    return identical;
}