cpu_conf.c 28.9 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
#include "virlog.h"
33 34 35

#define VIR_FROM_THIS VIR_FROM_CPU

36 37
VIR_LOG_INIT("conf.cpu_conf");

38 39 40
VIR_ENUM_IMPL(virCPU, VIR_CPU_TYPE_LAST,
              "host", "guest", "auto")

41 42 43 44 45
VIR_ENUM_IMPL(virCPUMode, VIR_CPU_MODE_LAST,
              "custom",
              "host-model",
              "host-passthrough")

46 47 48 49 50
VIR_ENUM_IMPL(virCPUMatch, VIR_CPU_MATCH_LAST,
              "minimum",
              "exact",
              "strict")

51 52 53 54 55 56
VIR_ENUM_IMPL(virCPUCheck, VIR_CPU_CHECK_LAST,
              "default",
              "none",
              "partial",
              "full")

57 58 59 60
VIR_ENUM_IMPL(virCPUFallback, VIR_CPU_FALLBACK_LAST,
              "allow",
              "forbid")

61 62 63 64 65 66 67
VIR_ENUM_IMPL(virCPUFeaturePolicy, VIR_CPU_FEATURE_LAST,
              "force",
              "require",
              "optional",
              "disable",
              "forbid")

68 69 70 71 72 73
VIR_ENUM_IMPL(virCPUCacheMode, VIR_CPU_CACHE_MODE_LAST,
              "emulate",
              "passthrough",
              "disable")


74 75 76 77 78 79 80 81 82 83 84 85 86
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;
}


87 88 89 90 91 92
void ATTRIBUTE_NONNULL(1)
virCPUDefFreeModel(virCPUDefPtr def)
{

    VIR_FREE(def->model);
    VIR_FREE(def->vendor);
93
    VIR_FREE(def->vendor_id);
94
    virCPUDefFreeFeatures(def);
95 96
}

97 98 99 100 101 102
void
virCPUDefFree(virCPUDefPtr def)
{
    if (!def)
        return;

103
    virCPUDefFreeModel(def);
104
    VIR_FREE(def->cache);
105 106 107 108
    VIR_FREE(def);
}


109 110
int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
virCPUDefCopyModel(virCPUDefPtr dst,
111
                   const virCPUDef *src,
112
                   bool resetPolicy)
113 114 115 116 117 118 119 120 121 122 123
{
    return virCPUDefCopyModelFilter(dst, src, resetPolicy, NULL, NULL);
}


int
virCPUDefCopyModelFilter(virCPUDefPtr dst,
                         const virCPUDef *src,
                         bool resetPolicy,
                         virCPUDefFeatureFilter filter,
                         void *opaque)
124
{
125
    size_t i;
126
    size_t n;
127

128 129 130 131
    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)
132
        return -1;
133 134 135 136 137 138
    dst->nfeatures_max = src->nfeatures;
    dst->nfeatures = 0;

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

140
        n = dst->nfeatures++;
141 142
        if (dst->type != src->type && resetPolicy) {
            if (dst->type == VIR_CPU_TYPE_HOST)
143
                dst->features[n].policy = -1;
144
            else if (src->features[i].policy == -1)
145
                dst->features[n].policy = VIR_CPU_FEATURE_REQUIRE;
146
            else
147
                dst->features[n].policy = src->features[i].policy;
148
        } else {
149
            dst->features[n].policy = src->features[i].policy;
150 151
        }

152
        if (VIR_STRDUP(dst->features[n].name, src->features[i].name) < 0)
153
            return -1;
154 155 156 157 158
    }

    return 0;
}

159

160 161 162 163 164 165 166
/**
 * 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.
 */
167 168
void
virCPUDefStealModel(virCPUDefPtr dst,
169 170
                    virCPUDefPtr src,
                    bool keepVendor)
171
{
172 173
    char *vendor = NULL;
    char *vendor_id = NULL;
174 175 176 177 178 179

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

180 181 182 183 184 185 186 187
    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;
188 189 190 191 192 193 194 195

    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);
    }
196 197 198
}


199
virCPUDefPtr
200
virCPUDefCopyWithoutModel(const virCPUDef *cpu)
201 202 203
{
    virCPUDefPtr copy;

204
    if (!cpu || VIR_ALLOC(copy) < 0)
205 206 207
        return NULL;

    copy->type = cpu->type;
208
    copy->mode = cpu->mode;
209
    copy->match = cpu->match;
210
    copy->check = cpu->check;
211
    copy->fallback = cpu->fallback;
212 213 214
    copy->sockets = cpu->sockets;
    copy->cores = cpu->cores;
    copy->threads = cpu->threads;
215
    copy->arch = cpu->arch;
216

217 218 219 220 221 222 223
    if (cpu->cache) {
        if (VIR_ALLOC(copy->cache) < 0)
            goto error;

        *copy->cache = *cpu->cache;
    }

224
    return copy;
225 226 227 228

 error:
    virCPUDefFree(copy);
    return NULL;
229 230 231 232 233 234 235 236 237 238 239
}


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

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

240 241
    if (virCPUDefCopyModel(copy, cpu, false) < 0)
        goto error;
242

243 244
    return copy;

245
 error:
246 247 248 249
    virCPUDefFree(copy);
    return NULL;
}

250

J
Jiri Denemark 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/*
 * Parses CPU definition XML from a node pointed to by @xpath. If @xpath is
 * NULL, the current node of @ctxt is used (i.e., it is a shortcut to ".").
 *
 * Missing <cpu> element in the XML document is not considered an error unless
 * @xpath is NULL in which case the function expects it was provided with a
 * valid <cpu> element already. In other words, the function returns success
 * and sets @cpu to NULL if @xpath is not NULL and the node pointed to by
 * @xpath is not found.
 *
 * Returns 0 on success, -1 on error.
 */
int
virCPUDefParseXML(xmlXPathContextPtr ctxt,
                  const char *xpath,
                  virCPUType type,
                  virCPUDefPtr *cpu)
268
{
J
Jiri Denemark 已提交
269
    virCPUDefPtr def = NULL;
270
    xmlNodePtr *nodes = NULL;
271
    xmlNodePtr oldnode = ctxt->node;
272
    int n;
273
    size_t i;
274
    char *cpuMode;
275 276
    char *fallback = NULL;
    char *vendor_id = NULL;
J
Jiri Denemark 已提交
277
    int ret = -1;
278

J
Jiri Denemark 已提交
279 280 281 282 283 284 285
    *cpu = NULL;

    if (xpath && !(ctxt->node = virXPathNode(xpath, ctxt))) {
        ret = 0;
        goto cleanup;
    }

286
    if (!virXMLNodeNameEqual(ctxt->node, "cpu")) {
287
        virReportError(VIR_ERR_XML_ERROR, "%s",
288
                       _("XML does not contain expected 'cpu' element"));
J
Jiri Denemark 已提交
289
        goto cleanup;
290 291
    }

292
    if (VIR_ALLOC(def) < 0)
J
Jiri Denemark 已提交
293
        goto cleanup;
294

295
    if (type == VIR_CPU_TYPE_AUTO) {
296 297
        if (virXPathBoolean("boolean(./arch)", ctxt)) {
            if (virXPathBoolean("boolean(./@match)", ctxt)) {
298
                virReportError(VIR_ERR_XML_ERROR, "%s",
299
                               _("'arch' element cannot be used inside 'cpu'"
300
                                 " element with 'match' attribute'"));
J
Jiri Denemark 已提交
301
                goto cleanup;
302
            }
303
            def->type = VIR_CPU_TYPE_HOST;
304
        } else {
305
            def->type = VIR_CPU_TYPE_GUEST;
306 307
        }
    } else {
308
        def->type = type;
309 310
    }

J
Jiri Denemark 已提交
311
    if ((cpuMode = virXMLPropString(ctxt->node, "mode"))) {
312 313
        if (def->type == VIR_CPU_TYPE_HOST) {
            VIR_FREE(cpuMode);
314 315
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Attribute mode is only allowed for guest CPU"));
J
Jiri Denemark 已提交
316
            goto cleanup;
317 318 319 320
        } else {
            def->mode = virCPUModeTypeFromString(cpuMode);

            if (def->mode < 0) {
321
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
322 323
                               _("Invalid mode attribute '%s'"),
                               cpuMode);
324
                VIR_FREE(cpuMode);
J
Jiri Denemark 已提交
325
                goto cleanup;
326
            }
327
            VIR_FREE(cpuMode);
328 329 330 331 332 333 334
        }
    } else {
        if (def->type == VIR_CPU_TYPE_HOST)
            def->mode = -1;
        else
            def->mode = VIR_CPU_MODE_CUSTOM;
    }
335 336

    if (def->type == VIR_CPU_TYPE_GUEST) {
J
Jiri Denemark 已提交
337
        char *match = virXMLPropString(ctxt->node, "match");
338
        char *check;
339 340

        if (!match) {
341 342 343 344
            if (virXPathBoolean("boolean(./model)", ctxt))
                def->match = VIR_CPU_MATCH_EXACT;
            else
                def->match = -1;
345 346 347 348 349
        } else {
            def->match = virCPUMatchTypeFromString(match);
            VIR_FREE(match);

            if (def->match < 0) {
350
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
P
Peter Krempa 已提交
351 352
                               _("Invalid match attribute for CPU "
                                 "specification"));
J
Jiri Denemark 已提交
353
                goto cleanup;
354
            }
355
        }
356

J
Jiri Denemark 已提交
357
        if ((check = virXMLPropString(ctxt->node, "check"))) {
358 359 360 361 362 363 364
            int value = virCPUCheckTypeFromString(check);
            VIR_FREE(check);

            if (value < 0) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("Invalid check attribute for CPU "
                                 "specification"));
J
Jiri Denemark 已提交
365
                goto cleanup;
366 367 368
            }
            def->check = value;
        }
369 370 371
    }

    if (def->type == VIR_CPU_TYPE_HOST) {
372 373
        char *arch = virXPathString("string(./arch[1])", ctxt);
        if (!arch) {
374 375
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Missing CPU architecture"));
J
Jiri Denemark 已提交
376
            goto cleanup;
377
        }
378
        if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) {
379
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
380 381
                           _("Unknown architecture %s"), arch);
            VIR_FREE(arch);
J
Jiri Denemark 已提交
382
            goto cleanup;
383 384
        }
        VIR_FREE(arch);
385 386
    }

387
    if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
388
        def->type == VIR_CPU_TYPE_HOST) {
389 390
        virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("Missing CPU model name"));
J
Jiri Denemark 已提交
391
        goto cleanup;
392 393
    }

394
    if (def->type == VIR_CPU_TYPE_GUEST &&
395 396
        def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {

397 398
        if ((fallback = virXPathString("string(./model[1]/@fallback)", ctxt))) {
            if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
399
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
400
                               _("Invalid fallback attribute"));
J
Jiri Denemark 已提交
401
                goto cleanup;
402
            }
K
Ken ICHIKAWA 已提交
403
        }
404

405 406 407
        if ((vendor_id = virXPathString("string(./model[1]/@vendor_id)",
                                        ctxt))) {
            if (strlen(vendor_id) != VIR_CPU_VENDOR_ID_LENGTH) {
K
Ken ICHIKAWA 已提交
408
                virReportError(VIR_ERR_XML_ERROR,
409
                               _("vendor_id must be exactly %d characters long"),
K
Ken ICHIKAWA 已提交
410
                               VIR_CPU_VENDOR_ID_LENGTH);
J
Jiri Denemark 已提交
411
                goto cleanup;
K
Ken ICHIKAWA 已提交
412
            }
413

K
Ken ICHIKAWA 已提交
414
            /* ensure that the string can be passed to qemu*/
415
            if (strchr(vendor_id, ',')) {
K
Ken ICHIKAWA 已提交
416 417
                    virReportError(VIR_ERR_XML_ERROR, "%s",
                                   _("vendor id is invalid"));
J
Jiri Denemark 已提交
418
                    goto cleanup;
419
            }
420

K
Ken ICHIKAWA 已提交
421
            def->vendor_id = vendor_id;
422
            vendor_id = NULL;
423 424 425
        }
    }

J
Jiri Denemark 已提交
426 427
    def->vendor = virXPathString("string(./vendor[1])", ctxt);
    if (def->vendor && !def->model) {
428 429
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("CPU vendor specified without CPU model"));
J
Jiri Denemark 已提交
430
        goto cleanup;
J
Jiri Denemark 已提交
431 432
    }

433
    if (virXPathNode("./topology[1]", ctxt)) {
434 435
        unsigned long ul;

J
Jiri Denemark 已提交
436
        if (virXPathULong("string(./topology[1]/@sockets)", ctxt, &ul) < 0) {
437
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
438
                           _("Missing 'sockets' attribute in CPU topology"));
J
Jiri Denemark 已提交
439
            goto cleanup;
440 441 442
        }
        def->sockets = (unsigned int) ul;

J
Jiri Denemark 已提交
443
        if (virXPathULong("string(./topology[1]/@cores)", ctxt, &ul) < 0) {
444
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
445
                           _("Missing 'cores' attribute in CPU topology"));
J
Jiri Denemark 已提交
446
            goto cleanup;
447 448 449
        }
        def->cores = (unsigned int) ul;

J
Jiri Denemark 已提交
450
        if (virXPathULong("string(./topology[1]/@threads)", ctxt, &ul) < 0) {
451
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
452
                           _("Missing 'threads' attribute in CPU topology"));
J
Jiri Denemark 已提交
453
            goto cleanup;
454 455 456 457
        }
        def->threads = (unsigned int) ul;

        if (!def->sockets || !def->cores || !def->threads) {
458 459
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU topology"));
J
Jiri Denemark 已提交
460
            goto cleanup;
461 462 463
        }
    }

P
Peter Krempa 已提交
464
    if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
J
Jiri Denemark 已提交
465
        goto cleanup;
466 467

    if (n > 0) {
468
        if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM) {
469
            virReportError(VIR_ERR_XML_ERROR, "%s",
P
Peter Krempa 已提交
470 471
                           _("Non-empty feature list specified without "
                             "CPU model"));
J
Jiri Denemark 已提交
472
            goto cleanup;
473 474
        }

E
Eric Blake 已提交
475 476
        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                         def->nfeatures, n) < 0)
J
Jiri Denemark 已提交
477
            goto cleanup;
P
Peter Krempa 已提交
478

479 480 481
        def->nfeatures = n;
    }

482
    for (i = 0; i < n; i++) {
483 484
        char *name;
        int policy; /* enum virDomainCPUFeaturePolicy */
485
        size_t j;
486 487 488 489 490

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

            strpolicy = virXMLPropString(nodes[i], "policy");
491 492 493 494
            if (strpolicy == NULL)
                policy = VIR_CPU_FEATURE_REQUIRE;
            else
                policy = virCPUFeaturePolicyTypeFromString(strpolicy);
495 496 497
            VIR_FREE(strpolicy);

            if (policy < 0) {
498
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
499
                               _("Invalid CPU feature policy"));
J
Jiri Denemark 已提交
500
                goto cleanup;
501
            }
502
        } else {
503
            policy = -1;
504
        }
505 506 507

        if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
            VIR_FREE(name);
508 509
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Invalid CPU feature name"));
J
Jiri Denemark 已提交
510
            goto cleanup;
511 512
        }

513
        for (j = 0; j < i; j++) {
514
            if (STREQ(name, def->features[j].name)) {
515
                virReportError(VIR_ERR_XML_ERROR,
C
Chen Fan 已提交
516
                               _("CPU feature '%s' specified more than once"),
517
                               name);
518
                VIR_FREE(name);
J
Jiri Denemark 已提交
519
                goto cleanup;
520 521 522 523 524 525 526
            }
        }

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

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    if (virXPathInt("count(./cache)", ctxt, &n) < 0) {
        goto cleanup;
    } else if (n > 1) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("at most one CPU cache element may be specified"));
        goto cleanup;
    } else if (n == 1) {
        int level = -1;
        char *strmode;
        int mode;

        if (virXPathBoolean("boolean(./cache[1]/@level)", ctxt) == 1 &&
            (virXPathInt("string(./cache[1]/@level)", ctxt, &level) < 0 ||
             level < 1 || level > 3)) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("invalid CPU cache level, must be in range [1,3]"));
            goto cleanup;
        }

        if (!(strmode = virXPathString("string(./cache[1]/@mode)", ctxt)) ||
            (mode = virCPUCacheModeTypeFromString(strmode)) < 0) {
            VIR_FREE(strmode);
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("missing or invalid CPU cache mode"));
            goto cleanup;
        }
        VIR_FREE(strmode);

        if (VIR_ALLOC(def->cache) < 0)
            goto cleanup;

        def->cache->level = level;
        def->cache->mode = mode;
    }

J
Jiri Denemark 已提交
562 563 564
    VIR_STEAL_PTR(*cpu, def);
    ret = 0;

565
 cleanup:
566
    ctxt->node = oldnode;
567 568
    VIR_FREE(fallback);
    VIR_FREE(vendor_id);
569 570
    VIR_FREE(nodes);
    virCPUDefFree(def);
J
Jiri Denemark 已提交
571
    return ret;
572 573 574 575
}


char *
576
virCPUDefFormat(virCPUDefPtr def,
577
                virDomainNumaPtr numa)
578 579 580
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

581
    if (virCPUDefFormatBufFull(&buf, def, numa) < 0)
582 583
        goto cleanup;

584 585
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
586 587 588

    return virBufferContentAndReset(&buf);

589
 cleanup:
590
    virBufferFreeAndReset(&buf);
591 592 593 594
    return NULL;
}


595 596
int
virCPUDefFormatBufFull(virBufferPtr buf,
597
                       virCPUDefPtr def,
598
                       virDomainNumaPtr numa)
599
{
600
    int ret = -1;
601
    virBuffer attributeBuf = VIR_BUFFER_INITIALIZER;
M
Michal Privoznik 已提交
602
    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
603

604 605 606
    if (!def)
        return 0;

607 608 609 610
    /* Format attributes for guest CPUs unless they only specify
     * topology or cache. */
    if (def->type == VIR_CPU_TYPE_GUEST &&
        (def->mode != VIR_CPU_MODE_CUSTOM || def->model)) {
611 612
        const char *tmp;

613 614 615 616
        if (!(tmp = virCPUModeTypeToString(def->mode))) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unexpected CPU mode %d"), def->mode);
            goto cleanup;
617
        }
618
        virBufferAsprintf(&attributeBuf, " mode='%s'", tmp);
619

620
        if (def->mode == VIR_CPU_MODE_CUSTOM) {
621
            if (!(tmp = virCPUMatchTypeToString(def->match))) {
622 623 624
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU match policy %d"),
                               def->match);
625
                goto cleanup;
626
            }
627
            virBufferAsprintf(&attributeBuf, " match='%s'", tmp);
628
        }
629 630 631 632 633

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

636
    /* Format children */
637
    virBufferSetChildIndent(&childrenBuf, buf);
638
    if (def->type == VIR_CPU_TYPE_HOST && def->arch)
M
Michal Privoznik 已提交
639
        virBufferAsprintf(&childrenBuf, "<arch>%s</arch>\n",
640
                          virArchToString(def->arch));
641
    if (virCPUDefFormatBuf(&childrenBuf, def) < 0)
642
        goto cleanup;
643

644
    if (virDomainNumaDefCPUFormatXML(&childrenBuf, numa) < 0)
645
        goto cleanup;
646

647 648 649 650
    if (virBufferCheckError(&attributeBuf) < 0 ||
        virBufferCheckError(&childrenBuf) < 0)
        goto cleanup;

651 652 653 654 655 656 657 658 659 660 661 662 663 664
    /* 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 已提交
665
    }
666

667 668
    ret = 0;
 cleanup:
669
    virBufferFreeAndReset(&attributeBuf);
M
Michal Privoznik 已提交
670
    virBufferFreeAndReset(&childrenBuf);
671
    return ret;
672 673
}

674
int
675
virCPUDefFormatBuf(virBufferPtr buf,
676
                   virCPUDefPtr def)
677
{
678
    size_t i;
679 680
    bool formatModel;
    bool formatFallback;
681 682 683 684

    if (!def)
        return 0;

685
    formatModel = (def->mode == VIR_CPU_MODE_CUSTOM ||
686
                   def->mode == VIR_CPU_MODE_HOST_MODEL);
687 688 689 690
    formatFallback = (def->type == VIR_CPU_TYPE_GUEST &&
                      (def->mode == VIR_CPU_MODE_HOST_MODEL ||
                       (def->mode == VIR_CPU_MODE_CUSTOM && def->model)));

691
    if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM && def->nfeatures) {
692 693
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Non-empty feature list specified without CPU model"));
694 695 696
        return -1;
    }

697
    if ((formatModel && def->model) || formatFallback) {
698
        virBufferAddLit(buf, "<model");
699
        if (formatFallback) {
700 701 702 703
            const char *fallback;

            fallback = virCPUFallbackTypeToString(def->fallback);
            if (!fallback) {
704 705 706
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU fallback value: %d"),
                               def->fallback);
707 708 709
                return -1;
            }
            virBufferAsprintf(buf, " fallback='%s'", fallback);
710
            if (def->vendor_id)
711
                virBufferEscapeString(buf, " vendor_id='%s'", def->vendor_id);
712
        }
713
        if (formatModel && def->model) {
714
            virBufferEscapeString(buf, ">%s</model>\n", def->model);
715 716 717
        } else {
            virBufferAddLit(buf, "/>\n");
        }
718
    }
719

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

723
    if (def->sockets && def->cores && def->threads) {
724
        virBufferAddLit(buf, "<topology");
725 726 727
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
728 729 730
        virBufferAddLit(buf, "/>\n");
    }

731 732 733 734 735 736 737 738 739
    if (def->cache) {
        virBufferAddLit(buf, "<cache ");
        if (def->cache->level != -1)
            virBufferAsprintf(buf, "level='%d' ", def->cache->level);
        virBufferAsprintf(buf, "mode='%s'",
                          virCPUCacheModeTypeToString(def->cache->mode));
        virBufferAddLit(buf, "/>\n");
    }

740 741
    for (i = 0; i < def->nfeatures; i++) {
        virCPUFeatureDefPtr feature = def->features + i;
742

743 744 745 746 747
        if (!feature->name) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing CPU feature name"));
            return -1;
        }
748

749 750
        if (def->type == VIR_CPU_TYPE_GUEST) {
            const char *policy;
751

752 753 754 755 756 757
            policy = virCPUFeaturePolicyTypeToString(feature->policy);
            if (!policy) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU feature policy %d"),
                               feature->policy);
                return -1;
758
            }
759 760 761 762 763
            virBufferAsprintf(buf, "<feature policy='%s' name='%s'/>\n",
                              policy, feature->name);
        } else {
            virBufferAsprintf(buf, "<feature name='%s'/>\n",
                              feature->name);
764 765 766 767 768 769
        }
    }

    return 0;
}

P
Peter Krempa 已提交
770 771 772 773 774
static int
virCPUDefUpdateFeatureInternal(virCPUDefPtr def,
                               const char *name,
                               int policy,
                               bool update)
775
{
776
    virCPUFeatureDefPtr feat;
777

P
Peter Krempa 已提交
778 779 780
    if (def->type == VIR_CPU_TYPE_HOST)
        policy = -1;

781 782 783 784 785
    if ((feat = virCPUDefFindFeature(def, name))) {
        if (update) {
            feat->policy = policy;
            return 0;
        }
P
Peter Krempa 已提交
786

787 788 789
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU feature '%s' specified more than once"),
                       name);
P
Peter Krempa 已提交
790

791
        return -1;
792 793
    }

E
Eric Blake 已提交
794 795
    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
                     def->nfeatures, 1) < 0)
796
        return -1;
797

798 799
    if (VIR_STRDUP(def->features[def->nfeatures].name, name) < 0)
        return -1;
800 801 802 803 804 805

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

    return 0;
}
806

P
Peter Krempa 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
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);
}

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838

virCPUFeatureDefPtr
virCPUDefFindFeature(virCPUDefPtr def,
                     const char *name)
{
    size_t i;

    for (i = 0; i < def->nfeatures; i++) {
        if (STREQ(name, def->features[i].name))
            return def->features + i;
    }

    return NULL;
}


839 840
bool
virCPUDefIsEqual(virCPUDefPtr src,
841 842
                 virCPUDefPtr dst,
                 bool reportError)
843 844
{
    bool identical = false;
845
    size_t i;
846 847 848 849

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

850 851 852 853
#define MISMATCH(fmt, ...)                                              \
    if (reportError)                                                    \
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, fmt, __VA_ARGS__)

854
    if ((src && !dst) || (!src && dst)) {
855
        MISMATCH("%s", _("Target CPU does not match source"));
856 857 858 859
        goto cleanup;
    }

    if (src->type != dst->type) {
860 861 862
        MISMATCH(_("Target CPU type %s does not match source %s"),
                 virCPUTypeToString(dst->type),
                 virCPUTypeToString(src->type));
863 864 865
        goto cleanup;
    }

866
    if (src->mode != dst->mode) {
867 868 869
        MISMATCH(_("Target CPU mode %s does not match source %s"),
                 virCPUModeTypeToString(dst->mode),
                 virCPUModeTypeToString(src->mode));
870 871 872
        goto cleanup;
    }

873
    if (src->arch != dst->arch) {
874 875 876
        MISMATCH(_("Target CPU arch %s does not match source %s"),
                 virArchToString(dst->arch),
                 virArchToString(src->arch));
877 878 879 880
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(src->model, dst->model)) {
881 882
        MISMATCH(_("Target CPU model %s does not match source %s"),
                 NULLSTR(dst->model), NULLSTR(src->model));
883 884 885 886
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(src->vendor, dst->vendor)) {
887 888
        MISMATCH(_("Target CPU vendor %s does not match source %s"),
                 NULLSTR(dst->vendor), NULLSTR(src->vendor));
889 890 891
        goto cleanup;
    }

892
    if (STRNEQ_NULLABLE(src->vendor_id, dst->vendor_id)) {
893 894
        MISMATCH(_("Target CPU vendor id %s does not match source %s"),
                 NULLSTR(dst->vendor_id), NULLSTR(src->vendor_id));
895 896 897
        goto cleanup;
    }

898
    if (src->sockets != dst->sockets) {
899 900
        MISMATCH(_("Target CPU sockets %d does not match source %d"),
                 dst->sockets, src->sockets);
901 902 903 904
        goto cleanup;
    }

    if (src->cores != dst->cores) {
905 906
        MISMATCH(_("Target CPU cores %d does not match source %d"),
                 dst->cores, src->cores);
907 908 909 910
        goto cleanup;
    }

    if (src->threads != dst->threads) {
911 912
        MISMATCH(_("Target CPU threads %d does not match source %d"),
                 dst->threads, src->threads);
913 914 915 916
        goto cleanup;
    }

    if (src->nfeatures != dst->nfeatures) {
917 918
        MISMATCH(_("Target CPU feature count %zu does not match source %zu"),
                 dst->nfeatures, src->nfeatures);
919 920 921
        goto cleanup;
    }

922
    for (i = 0; i < src->nfeatures; i++) {
923
        if (STRNEQ(src->features[i].name, dst->features[i].name)) {
924 925
            MISMATCH(_("Target CPU feature %s does not match source %s"),
                     dst->features[i].name, src->features[i].name);
926 927 928 929
            goto cleanup;
        }

        if (src->features[i].policy != dst->features[i].policy) {
930 931 932
            MISMATCH(_("Target CPU feature policy %s does not match source %s"),
                     virCPUFeaturePolicyTypeToString(dst->features[i].policy),
                     virCPUFeaturePolicyTypeToString(src->features[i].policy));
933 934 935 936
            goto cleanup;
        }
    }

937 938 939 940 941
    if ((src->cache && !dst->cache) ||
        (!src->cache && dst->cache) ||
        (src->cache && dst->cache &&
         (src->cache->level != dst->cache->level ||
          src->cache->mode != dst->cache->mode))) {
942
        MISMATCH("%s", _("Target CPU cache does not match source"));
943 944 945
        goto cleanup;
    }

946 947
#undef MISMATCH

948 949
    identical = true;

950
 cleanup:
951 952
    return identical;
}
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027


/*
 * Parses a list of CPU XMLs into a NULL-terminated list of CPU defs.
 */
virCPUDefPtr *
virCPUDefListParse(const char **xmlCPUs,
                   unsigned int ncpus,
                   virCPUType cpuType)
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDefPtr *cpus = NULL;
    size_t i;

    VIR_DEBUG("xmlCPUs=%p, ncpus=%u", xmlCPUs, ncpus);

    if (xmlCPUs) {
        for (i = 0; i < ncpus; i++)
            VIR_DEBUG("xmlCPUs[%zu]=%s", i, NULLSTR(xmlCPUs[i]));
    }

    if (!xmlCPUs && ncpus != 0) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("nonzero ncpus doesn't match with NULL xmlCPUs"));
        goto error;
    }

    if (ncpus == 0) {
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("no CPUs given"));
        goto error;
    }

    if (VIR_ALLOC_N(cpus, ncpus + 1))
        goto error;

    for (i = 0; i < ncpus; i++) {
        if (!(doc = virXMLParseStringCtxt(xmlCPUs[i], _("(CPU_definition)"), &ctxt)))
            goto error;

        if (virCPUDefParseXML(ctxt, NULL, cpuType, &cpus[i]) < 0)
            goto error;

        xmlXPathFreeContext(ctxt);
        xmlFreeDoc(doc);
        ctxt = NULL;
        doc = NULL;
    }

    return cpus;

 error:
    virCPUDefListFree(cpus);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
    return NULL;
}


/*
 * Frees NULL-terminated list of CPUs created by virCPUDefListParse.
 */
void
virCPUDefListFree(virCPUDefPtr *cpus)
{
    virCPUDefPtr *cpu;

    if (!cpus)
        return;

    for (cpu = cpus; *cpu != NULL; cpu++)
        virCPUDefFree(*cpu);

    VIR_FREE(cpus);
}