cpu_conf.c 25.0 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 52 53
VIR_ENUM_IMPL(virCPUCheck, VIR_CPU_CHECK_LAST,
              "default",
              "none",
              "partial",
              "full")

54 55 56 57
VIR_ENUM_IMPL(virCPUFallback, VIR_CPU_FALLBACK_LAST,
              "allow",
              "forbid")

58 59 60 61 62 63 64
VIR_ENUM_IMPL(virCPUFeaturePolicy, VIR_CPU_FEATURE_LAST,
              "force",
              "require",
              "optional",
              "disable",
              "forbid")

65 66 67 68 69 70 71 72 73 74 75 76 77
void
virCPUDefFreeFeatures(virCPUDefPtr def)
{
    size_t i;

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

    def->nfeatures = def->nfeatures_max = 0;
}


78 79 80 81 82 83
void ATTRIBUTE_NONNULL(1)
virCPUDefFreeModel(virCPUDefPtr def)
{

    VIR_FREE(def->model);
    VIR_FREE(def->vendor);
84
    VIR_FREE(def->vendor_id);
85
    virCPUDefFreeFeatures(def);
86 87
}

88 89 90 91 92 93
void
virCPUDefFree(virCPUDefPtr def)
{
    if (!def)
        return;

94
    virCPUDefFreeModel(def);
95 96 97 98
    VIR_FREE(def);
}


99 100
int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
virCPUDefCopyModel(virCPUDefPtr dst,
101
                   const virCPUDef *src,
102
                   bool resetPolicy)
103 104 105 106 107 108 109 110 111 112 113
{
    return virCPUDefCopyModelFilter(dst, src, resetPolicy, NULL, NULL);
}


int
virCPUDefCopyModelFilter(virCPUDefPtr dst,
                         const virCPUDef *src,
                         bool resetPolicy,
                         virCPUDefFeatureFilter filter,
                         void *opaque)
114
{
115
    size_t i;
116
    size_t n;
117

118 119 120 121
    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)
122
        return -1;
123 124 125 126 127 128
    dst->nfeatures_max = src->nfeatures;
    dst->nfeatures = 0;

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

130
        n = dst->nfeatures++;
131 132
        if (dst->type != src->type && resetPolicy) {
            if (dst->type == VIR_CPU_TYPE_HOST)
133
                dst->features[n].policy = -1;
134
            else if (src->features[i].policy == -1)
135
                dst->features[n].policy = VIR_CPU_FEATURE_REQUIRE;
136
            else
137
                dst->features[n].policy = src->features[i].policy;
138
        } else {
139
            dst->features[n].policy = src->features[i].policy;
140 141
        }

142
        if (VIR_STRDUP(dst->features[n].name, src->features[i].name) < 0)
143
            return -1;
144 145 146 147 148
    }

    return 0;
}

149

150 151 152 153 154 155 156
/**
 * virCPUDefStealModel:
 *
 * Move CPU model related parts virCPUDef from @src to @dst. If @keepVendor
 * is true, the function keeps the original vendor/vendor_id in @dst rather
 * than overwriting it with the values from @src.
 */
157 158
void
virCPUDefStealModel(virCPUDefPtr dst,
159 160
                    virCPUDefPtr src,
                    bool keepVendor)
161
{
162 163
    char *vendor = NULL;
    char *vendor_id = NULL;
164 165 166 167 168 169

    if (keepVendor) {
        VIR_STEAL_PTR(vendor, dst->vendor);
        VIR_STEAL_PTR(vendor_id, dst->vendor_id);
    }

170 171 172 173 174 175 176 177
    virCPUDefFreeModel(dst);

    VIR_STEAL_PTR(dst->model, src->model);
    VIR_STEAL_PTR(dst->features, src->features);
    dst->nfeatures_max = src->nfeatures_max;
    src->nfeatures_max = 0;
    dst->nfeatures = src->nfeatures;
    src->nfeatures = 0;
178 179 180 181 182 183 184 185

    if (keepVendor) {
        dst->vendor = vendor;
        dst->vendor_id = vendor_id;
    } else {
        VIR_STEAL_PTR(dst->vendor, src->vendor);
        VIR_STEAL_PTR(dst->vendor_id, src->vendor_id);
    }
186 187 188
}


189
virCPUDefPtr
190
virCPUDefCopyWithoutModel(const virCPUDef *cpu)
191 192 193
{
    virCPUDefPtr copy;

194
    if (!cpu || VIR_ALLOC(copy) < 0)
195 196 197
        return NULL;

    copy->type = cpu->type;
198
    copy->mode = cpu->mode;
199
    copy->match = cpu->match;
200
    copy->check = cpu->check;
201
    copy->fallback = cpu->fallback;
202 203 204
    copy->sockets = cpu->sockets;
    copy->cores = cpu->cores;
    copy->threads = cpu->threads;
205
    copy->arch = cpu->arch;
206

207 208 209 210 211 212 213 214 215 216 217 218
    return copy;
}


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

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

219 220
    if (virCPUDefCopyModel(copy, cpu, false) < 0)
        goto error;
221

222 223
    return copy;

224
 error:
225 226 227 228
    virCPUDefFree(copy);
    return NULL;
}

229

230
virCPUDefPtr
231
virCPUDefParseXML(xmlNodePtr node,
232
                  xmlXPathContextPtr ctxt,
233
                  virCPUType type)
234 235 236
{
    virCPUDefPtr def;
    xmlNodePtr *nodes = NULL;
237
    xmlNodePtr oldnode = ctxt->node;
238
    int n;
239
    size_t i;
240
    char *cpuMode;
241 242
    char *fallback = NULL;
    char *vendor_id = NULL;
243

244
    if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
245
        virReportError(VIR_ERR_XML_ERROR, "%s",
246
                       _("XML does not contain expected 'cpu' element"));
247 248 249
        return NULL;
    }

250
    if (VIR_ALLOC(def) < 0)
251 252
        return NULL;

253
    if (type == VIR_CPU_TYPE_AUTO) {
254 255
        if (virXPathBoolean("boolean(./arch)", ctxt)) {
            if (virXPathBoolean("boolean(./@match)", ctxt)) {
256
                virReportError(VIR_ERR_XML_ERROR, "%s",
257
                               _("'arch' element cannot be used inside 'cpu'"
258
                                 " element with 'match' attribute'"));
259 260
                goto error;
            }
261
            def->type = VIR_CPU_TYPE_HOST;
262
        } else {
263
            def->type = VIR_CPU_TYPE_GUEST;
264 265
        }
    } else {
266
        def->type = type;
267 268 269 270 271
    }

    if ((cpuMode = virXMLPropString(node, "mode"))) {
        if (def->type == VIR_CPU_TYPE_HOST) {
            VIR_FREE(cpuMode);
272 273
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Attribute mode is only allowed for guest CPU"));
274 275 276 277 278
            goto error;
        } else {
            def->mode = virCPUModeTypeFromString(cpuMode);

            if (def->mode < 0) {
279
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
280 281
                               _("Invalid mode attribute '%s'"),
                               cpuMode);
282
                VIR_FREE(cpuMode);
283 284
                goto error;
            }
285
            VIR_FREE(cpuMode);
286 287 288 289 290 291 292
        }
    } else {
        if (def->type == VIR_CPU_TYPE_HOST)
            def->mode = -1;
        else
            def->mode = VIR_CPU_MODE_CUSTOM;
    }
293 294

    if (def->type == VIR_CPU_TYPE_GUEST) {
295
        char *match = virXMLPropString(node, "match");
296
        char *check;
297 298

        if (!match) {
299 300 301 302
            if (virXPathBoolean("boolean(./model)", ctxt))
                def->match = VIR_CPU_MATCH_EXACT;
            else
                def->match = -1;
303 304 305 306 307
        } else {
            def->match = virCPUMatchTypeFromString(match);
            VIR_FREE(match);

            if (def->match < 0) {
308
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
P
Peter Krempa 已提交
309 310
                               _("Invalid match attribute for CPU "
                                 "specification"));
311 312
                goto error;
            }
313
        }
314 315 316 317 318 319 320 321 322 323 324 325 326

        if ((check = virXMLPropString(node, "check"))) {
            int value = virCPUCheckTypeFromString(check);
            VIR_FREE(check);

            if (value < 0) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("Invalid check attribute for CPU "
                                 "specification"));
                goto error;
            }
            def->check = value;
        }
327 328 329
    }

    if (def->type == VIR_CPU_TYPE_HOST) {
330 331
        char *arch = virXPathString("string(./arch[1])", ctxt);
        if (!arch) {
332 333
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Missing CPU architecture"));
334 335
            goto error;
        }
336
        if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) {
337
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
338 339 340 341 342
                           _("Unknown architecture %s"), arch);
            VIR_FREE(arch);
            goto error;
        }
        VIR_FREE(arch);
343 344
    }

345
    if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
346
        def->type == VIR_CPU_TYPE_HOST) {
347 348
        virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("Missing CPU model name"));
349 350 351
        goto error;
    }

352
    if (def->type == VIR_CPU_TYPE_GUEST &&
353 354
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {

355 356
        if ((fallback = virXPathString("string(./model[1]/@fallback)", ctxt))) {
            if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
357
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
358 359
                               _("Invalid fallback attribute"));
                goto error;
360
            }
K
Ken ICHIKAWA 已提交
361
        }
362

363 364 365
        if ((vendor_id = virXPathString("string(./model[1]/@vendor_id)",
                                        ctxt))) {
            if (strlen(vendor_id) != VIR_CPU_VENDOR_ID_LENGTH) {
K
Ken ICHIKAWA 已提交
366
                virReportError(VIR_ERR_XML_ERROR,
367
                               _("vendor_id must be exactly %d characters long"),
K
Ken ICHIKAWA 已提交
368 369 370
                               VIR_CPU_VENDOR_ID_LENGTH);
                goto error;
            }
371

K
Ken ICHIKAWA 已提交
372
            /* ensure that the string can be passed to qemu*/
373
            if (strchr(vendor_id, ',')) {
K
Ken ICHIKAWA 已提交
374 375
                    virReportError(VIR_ERR_XML_ERROR, "%s",
                                   _("vendor id is invalid"));
376
                    goto error;
377
            }
378

K
Ken ICHIKAWA 已提交
379
            def->vendor_id = vendor_id;
380
            vendor_id = NULL;
381 382 383
        }
    }

J
Jiri Denemark 已提交
384 385
    def->vendor = virXPathString("string(./vendor[1])", ctxt);
    if (def->vendor && !def->model) {
386 387
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("CPU vendor specified without CPU model"));
J
Jiri Denemark 已提交
388 389 390
        goto error;
    }

391
    if (virXPathNode("./topology[1]", ctxt)) {
392 393 394
        int ret;
        unsigned long ul;

395
        ret = virXPathULong("string(./topology[1]/@sockets)",
396 397
                            ctxt, &ul);
        if (ret < 0) {
398
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
399
                           _("Missing 'sockets' attribute in CPU topology"));
400 401 402 403
            goto error;
        }
        def->sockets = (unsigned int) ul;

404
        ret = virXPathULong("string(./topology[1]/@cores)",
405 406
                            ctxt, &ul);
        if (ret < 0) {
407
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
408
                           _("Missing 'cores' attribute in CPU topology"));
409 410 411 412
            goto error;
        }
        def->cores = (unsigned int) ul;

413
        ret = virXPathULong("string(./topology[1]/@threads)",
414 415
                            ctxt, &ul);
        if (ret < 0) {
416
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
417
                           _("Missing 'threads' attribute in CPU topology"));
418 419 420 421 422
            goto error;
        }
        def->threads = (unsigned int) ul;

        if (!def->sockets || !def->cores || !def->threads) {
423 424
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU topology"));
425 426 427 428
            goto error;
        }
    }

P
Peter Krempa 已提交
429
    if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
430 431 432
        goto error;

    if (n > 0) {
433
        if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM) {
434
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
435 436
                           _("Non-empty feature list specified without "
                             "CPU model"));
437 438 439
            goto error;
        }

E
Eric Blake 已提交
440 441
        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                         def->nfeatures, n) < 0)
442
            goto error;
P
Peter Krempa 已提交
443

444 445 446
        def->nfeatures = n;
    }

447
    for (i = 0; i < n; i++) {
448 449
        char *name;
        int policy; /* enum virDomainCPUFeaturePolicy */
450
        size_t j;
451 452 453 454 455

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

            strpolicy = virXMLPropString(nodes[i], "policy");
456 457 458 459
            if (strpolicy == NULL)
                policy = VIR_CPU_FEATURE_REQUIRE;
            else
                policy = virCPUFeaturePolicyTypeFromString(strpolicy);
460 461 462
            VIR_FREE(strpolicy);

            if (policy < 0) {
463
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
464
                               _("Invalid CPU feature policy"));
465 466
                goto error;
            }
467
        } else {
468
            policy = -1;
469
        }
470 471 472

        if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
            VIR_FREE(name);
473 474
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU feature name"));
475 476 477
            goto error;
        }

478
        for (j = 0; j < i; j++) {
479
            if (STREQ(name, def->features[j].name)) {
480
                virReportError(VIR_ERR_XML_ERROR,
C
Chen Fan 已提交
481
                               _("CPU feature '%s' specified more than once"),
482
                               name);
483 484 485 486 487 488 489 490 491
                VIR_FREE(name);
                goto error;
            }
        }

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

492
 cleanup:
493
    ctxt->node = oldnode;
494 495
    VIR_FREE(fallback);
    VIR_FREE(vendor_id);
496 497 498
    VIR_FREE(nodes);
    return def;

499
 error:
500 501 502 503 504 505 506
    virCPUDefFree(def);
    def = NULL;
    goto cleanup;
}


char *
507
virCPUDefFormat(virCPUDefPtr def,
508
                virDomainNumaPtr numa,
509
                bool updateCPU)
510 511 512
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

513
    if (virCPUDefFormatBufFull(&buf, def, numa, updateCPU) < 0)
514 515
        goto cleanup;

516 517
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
518 519 520

    return virBufferContentAndReset(&buf);

521
 cleanup:
522
    virBufferFreeAndReset(&buf);
523 524 525 526
    return NULL;
}


527 528
int
virCPUDefFormatBufFull(virBufferPtr buf,
529
                       virCPUDefPtr def,
530
                       virDomainNumaPtr numa,
531
                       bool updateCPU)
532
{
533
    int ret = -1;
534
    virBuffer attributeBuf = VIR_BUFFER_INITIALIZER;
M
Michal Privoznik 已提交
535 536
    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
    int indent = virBufferGetIndent(buf, false);
537

538 539 540
    if (!def)
        return 0;

541
    /* Format attributes */
542 543 544 545 546
    if (def->type == VIR_CPU_TYPE_GUEST) {
        const char *tmp;

        if (def->mode != VIR_CPU_MODE_CUSTOM || def->model) {
            if (!(tmp = virCPUModeTypeToString(def->mode))) {
547 548
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU mode %d"), def->mode);
549
                goto cleanup;
550
            }
551
            virBufferAsprintf(&attributeBuf, " mode='%s'", tmp);
552 553
        }

554 555
        if (def->model &&
            (def->mode == VIR_CPU_MODE_CUSTOM ||
556
             updateCPU)) {
557
            if (!(tmp = virCPUMatchTypeToString(def->match))) {
558 559 560
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU match policy %d"),
                               def->match);
561
                goto cleanup;
562
            }
563
            virBufferAsprintf(&attributeBuf, " match='%s'", tmp);
564
        }
565 566 567 568 569

        if (def->check) {
            virBufferAsprintf(&attributeBuf, " check='%s'",
                              virCPUCheckTypeToString(def->check));
        }
570 571
    }

572
    /* Format children */
M
Michal Privoznik 已提交
573
    virBufferAdjustIndent(&childrenBuf, indent + 2);
574
    if (def->type == VIR_CPU_TYPE_HOST && def->arch)
M
Michal Privoznik 已提交
575
        virBufferAsprintf(&childrenBuf, "<arch>%s</arch>\n",
576
                          virArchToString(def->arch));
M
Michal Privoznik 已提交
577
    if (virCPUDefFormatBuf(&childrenBuf, def, updateCPU) < 0)
578
        goto cleanup;
579

M
Michal Privoznik 已提交
580
    if (virDomainNumaDefCPUFormat(&childrenBuf, numa) < 0)
581
        goto cleanup;
582

583 584 585 586 587 588 589 590 591 592 593 594 595 596
    /* 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 已提交
597
    }
598

599 600
    ret = 0;
 cleanup:
601
    virBufferFreeAndReset(&attributeBuf);
M
Michal Privoznik 已提交
602
    virBufferFreeAndReset(&childrenBuf);
603
    return ret;
604 605
}

606
int
607
virCPUDefFormatBuf(virBufferPtr buf,
608
                   virCPUDefPtr def,
609
                   bool updateCPU)
610
{
611
    size_t i;
612 613
    bool formatModel;
    bool formatFallback;
614 615 616 617

    if (!def)
        return 0;

618
    formatModel = (def->mode == VIR_CPU_MODE_CUSTOM ||
619
                   def->mode == VIR_CPU_MODE_HOST_MODEL ||
620
                   updateCPU);
621 622 623 624
    formatFallback = (def->type == VIR_CPU_TYPE_GUEST &&
                      (def->mode == VIR_CPU_MODE_HOST_MODEL ||
                       (def->mode == VIR_CPU_MODE_CUSTOM && def->model)));

625
    if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM && def->nfeatures) {
626 627
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Non-empty feature list specified without CPU model"));
628 629 630
        return -1;
    }

631
    if ((formatModel && def->model) || formatFallback) {
632
        virBufferAddLit(buf, "<model");
633
        if (formatFallback) {
634 635 636 637
            const char *fallback;

            fallback = virCPUFallbackTypeToString(def->fallback);
            if (!fallback) {
638 639 640
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU fallback value: %d"),
                               def->fallback);
641 642 643
                return -1;
            }
            virBufferAsprintf(buf, " fallback='%s'", fallback);
644
            if (def->vendor_id)
645
                virBufferEscapeString(buf, " vendor_id='%s'", def->vendor_id);
646
        }
647
        if (formatModel && def->model) {
648
            virBufferEscapeString(buf, ">%s</model>\n", def->model);
649 650 651
        } else {
            virBufferAddLit(buf, "/>\n");
        }
652
    }
653

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

657
    if (def->sockets && def->cores && def->threads) {
658
        virBufferAddLit(buf, "<topology");
659 660 661
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
662 663 664
        virBufferAddLit(buf, "/>\n");
    }

665 666
    for (i = 0; i < def->nfeatures; i++) {
        virCPUFeatureDefPtr feature = def->features + i;
667

668 669 670 671 672
        if (!feature->name) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing CPU feature name"));
            return -1;
        }
673

674 675
        if (def->type == VIR_CPU_TYPE_GUEST) {
            const char *policy;
676

677 678 679 680 681 682
            policy = virCPUFeaturePolicyTypeToString(feature->policy);
            if (!policy) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU feature policy %d"),
                               feature->policy);
                return -1;
683
            }
684 685 686 687 688
            virBufferAsprintf(buf, "<feature policy='%s' name='%s'/>\n",
                              policy, feature->name);
        } else {
            virBufferAsprintf(buf, "<feature name='%s'/>\n",
                              feature->name);
689 690 691 692 693 694
        }
    }

    return 0;
}

P
Peter Krempa 已提交
695 696 697 698 699
static int
virCPUDefUpdateFeatureInternal(virCPUDefPtr def,
                               const char *name,
                               int policy,
                               bool update)
700
{
701
    size_t i;
702

P
Peter Krempa 已提交
703 704 705
    if (def->type == VIR_CPU_TYPE_HOST)
        policy = -1;

706
    for (i = 0; i < def->nfeatures; i++) {
707
        if (STREQ(name, def->features[i].name)) {
P
Peter Krempa 已提交
708 709 710 711 712
            if (update) {
                def->features[i].policy = policy;
                return 0;
            }

713
            virReportError(VIR_ERR_INTERNAL_ERROR,
C
Chen Fan 已提交
714
                           _("CPU feature '%s' specified more than once"),
715
                           name);
P
Peter Krempa 已提交
716

717 718 719 720
            return -1;
        }
    }

E
Eric Blake 已提交
721 722
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
723
        return -1;
724

725 726
    if (VIR_STRDUP(def->features[def->nfeatures].name, name) < 0)
        return -1;
727 728 729 730 731 732

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

    return 0;
}
733

P
Peter Krempa 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
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);
}

750 751 752 753 754
bool
virCPUDefIsEqual(virCPUDefPtr src,
                 virCPUDefPtr dst)
{
    bool identical = false;
755
    size_t i;
756 757 758 759 760

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

    if ((src && !dst) || (!src && dst)) {
761 762
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Target CPU does not match source"));
763 764 765 766
        goto cleanup;
    }

    if (src->type != dst->type) {
767 768 769 770
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU type %s does not match source %s"),
                       virCPUTypeToString(dst->type),
                       virCPUTypeToString(src->type));
771 772 773
        goto cleanup;
    }

774
    if (src->mode != dst->mode) {
775 776 777 778
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU mode %s does not match source %s"),
                       virCPUModeTypeToString(dst->mode),
                       virCPUModeTypeToString(src->mode));
779 780 781
        goto cleanup;
    }

782
    if (src->arch != dst->arch) {
783 784
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU arch %s does not match source %s"),
785 786
                       virArchToString(dst->arch),
                       virArchToString(src->arch));
787 788 789 790
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(src->model, dst->model)) {
791 792 793
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU model %s does not match source %s"),
                       NULLSTR(dst->model), NULLSTR(src->model));
794 795 796 797
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(src->vendor, dst->vendor)) {
798 799 800
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU vendor %s does not match source %s"),
                       NULLSTR(dst->vendor), NULLSTR(src->vendor));
801 802 803
        goto cleanup;
    }

804
    if (STRNEQ_NULLABLE(src->vendor_id, dst->vendor_id)) {
805
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
806
                       _("Target CPU vendor id %s does not match source %s"),
807
                       NULLSTR(dst->vendor_id), NULLSTR(src->vendor_id));
808 809 810
        goto cleanup;
    }

811
    if (src->sockets != dst->sockets) {
812 813 814
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU sockets %d does not match source %d"),
                       dst->sockets, src->sockets);
815 816 817 818
        goto cleanup;
    }

    if (src->cores != dst->cores) {
819 820 821
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU cores %d does not match source %d"),
                       dst->cores, src->cores);
822 823 824 825
        goto cleanup;
    }

    if (src->threads != dst->threads) {
826 827 828
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU threads %d does not match source %d"),
                       dst->threads, src->threads);
829 830 831 832
        goto cleanup;
    }

    if (src->nfeatures != dst->nfeatures) {
833 834 835
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target CPU feature count %zu does not match source %zu"),
                       dst->nfeatures, src->nfeatures);
836 837 838
        goto cleanup;
    }

839
    for (i = 0; i < src->nfeatures; i++) {
840
        if (STRNEQ(src->features[i].name, dst->features[i].name)) {
841 842 843
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Target CPU feature %s does not match source %s"),
                           dst->features[i].name, src->features[i].name);
844 845 846 847
            goto cleanup;
        }

        if (src->features[i].policy != dst->features[i].policy) {
848 849 850 851
            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));
852 853 854 855 856 857
            goto cleanup;
        }
    }

    identical = true;

858
 cleanup:
859 860
    return identical;
}