capabilities.c 26.9 KB
Newer Older
1 2 3
/*
 * capabilities.c: hypervisor capabilities
 *
4
 * Copyright (C) 2006-2008, 2010-2011, 2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006-2008 Daniel P. Berrange
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

24 25
#include <config.h>

26 27
#include <strings.h>

28
#include "capabilities.h"
29
#include "virbuffer.h"
30
#include "viralloc.h"
31
#include "virutil.h"
32
#include "viruuid.h"
33
#include "cpu_conf.h"
34
#include "virerror.h"
35 36 37 38


#define VIR_FROM_THIS VIR_FROM_CAPABILITIES

39 40
VIR_ENUM_DECL(virCapsHostPMTarget)
VIR_ENUM_IMPL(virCapsHostPMTarget, VIR_NODE_SUSPEND_TARGET_LAST,
41
              "suspend_mem", "suspend_disk", "suspend_hybrid");
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static virClassPtr virCapsClass;
static void virCapabilitiesDispose(void *obj);

static int virCapabilitiesOnceInit(void)
{
    if (!(virCapsClass = virClassNew(virClassForObject(),
                                     "virCaps",
                                     sizeof(virCaps),
                                     virCapabilitiesDispose)))
        return -1;

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virCapabilities)

59 60
/**
 * virCapabilitiesNew:
61
 * @hostarch: host machine architecture
62 63
 * @offlineMigrate: non-zero if offline migration is available
 * @liveMigrate: non-zero if live migration is available
64
 *
65 66 67
 * Allocate a new capabilities object
 */
virCapsPtr
68
virCapabilitiesNew(virArch hostarch,
69 70 71 72 73
                   int offlineMigrate,
                   int liveMigrate)
{
    virCapsPtr caps;

74 75 76 77
    if (virCapabilitiesInitialize() < 0)
        return NULL;

    if (!(caps = virObjectNew(virCapsClass)))
78
        return NULL;
79

80
    caps->host.arch = hostarch;
81 82 83 84 85 86
    caps->host.offlineMigrate = offlineMigrate;
    caps->host.liveMigrate = liveMigrate;

    return caps;
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
void
virCapabilitiesClearHostNUMACellCPUTopology(virCapsHostNUMACellCPUPtr cpus,
                                            size_t ncpus)
{
    size_t i;

    if (!cpus)
        return;

    for (i = 0; i < ncpus; i++) {
        virBitmapFree(cpus[i].siblings);
        cpus[i].siblings = NULL;
    }
}

102 103 104
static void
virCapabilitiesFreeHostNUMACell(virCapsHostNUMACellPtr cell)
{
105 106 107
    if (cell == NULL)
        return;

108 109
    virCapabilitiesClearHostNUMACellCPUTopology(cell->cpus, cell->ncpus);

110 111
    VIR_FREE(cell->cpus);
    VIR_FREE(cell);
112 113
}

114 115 116 117 118 119 120 121 122 123
static void
virCapabilitiesFreeGuestMachine(virCapsGuestMachinePtr machine)
{
    if (machine == NULL)
        return;
    VIR_FREE(machine->name);
    VIR_FREE(machine->canonical);
    VIR_FREE(machine);
}

124 125 126 127
static void
virCapabilitiesFreeGuestDomain(virCapsGuestDomainPtr dom)
{
    int i;
128 129 130
    if (dom == NULL)
        return;

131 132
    VIR_FREE(dom->info.emulator);
    VIR_FREE(dom->info.loader);
133
    for (i = 0 ; i < dom->info.nmachines ; i++)
134
        virCapabilitiesFreeGuestMachine(dom->info.machines[i]);
135 136
    VIR_FREE(dom->info.machines);
    VIR_FREE(dom->type);
137

138
    VIR_FREE(dom);
139 140 141 142 143
}

static void
virCapabilitiesFreeGuestFeature(virCapsGuestFeaturePtr feature)
{
144 145
    if (feature == NULL)
        return;
146 147
    VIR_FREE(feature->name);
    VIR_FREE(feature);
148 149 150 151 152 153
}

static void
virCapabilitiesFreeGuest(virCapsGuestPtr guest)
{
    int i;
154 155 156
    if (guest == NULL)
        return;

157
    VIR_FREE(guest->ostype);
158

159 160
    VIR_FREE(guest->arch.defaultInfo.emulator);
    VIR_FREE(guest->arch.defaultInfo.loader);
161
    for (i = 0 ; i < guest->arch.defaultInfo.nmachines ; i++)
162
        virCapabilitiesFreeGuestMachine(guest->arch.defaultInfo.machines[i]);
163
    VIR_FREE(guest->arch.defaultInfo.machines);
164 165 166

    for (i = 0 ; i < guest->arch.ndomains ; i++)
        virCapabilitiesFreeGuestDomain(guest->arch.domains[i]);
167
    VIR_FREE(guest->arch.domains);
168 169 170

    for (i = 0 ; i < guest->nfeatures ; i++)
        virCapabilitiesFreeGuestFeature(guest->features[i]);
171
    VIR_FREE(guest->features);
172

173
    VIR_FREE(guest);
174 175
}

176 177 178 179 180 181 182 183
void
virCapabilitiesFreeNUMAInfo(virCapsPtr caps)
{
    int i;

    for (i = 0 ; i < caps->host.nnumaCell ; i++)
        virCapabilitiesFreeHostNUMACell(caps->host.numaCell[i]);
    VIR_FREE(caps->host.numaCell);
184
    caps->host.nnumaCell = 0;
185
}
186

187 188 189 190
static void
virCapabilitiesDispose(void *object)
{
    virCapsPtr caps = object;
191 192 193 194
    int i;

    for (i = 0 ; i < caps->nguests ; i++)
        virCapabilitiesFreeGuest(caps->guests[i]);
195
    VIR_FREE(caps->guests);
196 197

    for (i = 0 ; i < caps->host.nfeatures ; i++)
198 199
        VIR_FREE(caps->host.features[i]);
    VIR_FREE(caps->host.features);
200 201

    virCapabilitiesFreeNUMAInfo(caps);
202

203
    for (i = 0 ; i < caps->host.nmigrateTrans ; i++)
204 205
        VIR_FREE(caps->host.migrateTrans[i]);
    VIR_FREE(caps->host.migrateTrans);
206

207 208 209 210 211 212
    for (i = 0; i < caps->host.nsecModels; i++) {
        VIR_FREE(caps->host.secModels[i].model);
        VIR_FREE(caps->host.secModels[i].doi);
    }
    VIR_FREE(caps->host.secModels);

213
    virCPUDefFree(caps->host.cpu);
214 215 216 217 218 219 220 221 222 223 224 225 226 227
}


/**
 * virCapabilitiesAddHostFeature:
 * @caps: capabilities to extend
 * @name: name of new feature
 *
 * Registers a new host CPU feature, eg 'pae', or 'vmx'
 */
int
virCapabilitiesAddHostFeature(virCapsPtr caps,
                              const char *name)
{
E
Eric Blake 已提交
228 229
    if (VIR_RESIZE_N(caps->host.features, caps->host.nfeatures_max,
                     caps->host.nfeatures, 1) < 0)
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        return -1;

    if ((caps->host.features[caps->host.nfeatures] = strdup(name)) == NULL)
        return -1;
    caps->host.nfeatures++;

    return 0;
}

/**
 * virCapabilitiesAddHostMigrateTransport:
 * @caps: capabilities to extend
 * @name: name of migration transport
 *
 * Registers a new domain migration transport URI
 */
int
virCapabilitiesAddHostMigrateTransport(virCapsPtr caps,
                                       const char *name)
{
E
Eric Blake 已提交
250 251
    if (VIR_RESIZE_N(caps->host.migrateTrans, caps->host.nmigrateTrans_max,
                     caps->host.nmigrateTrans, 1) < 0)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        return -1;

    if ((caps->host.migrateTrans[caps->host.nmigrateTrans] = strdup(name)) == NULL)
        return -1;
    caps->host.nmigrateTrans++;

    return 0;
}


/**
 * virCapabilitiesAddHostNUMACell:
 * @caps: capabilities to extend
 * @num: ID number of NUMA cell
 * @ncpus: number of CPUs in cell
267
 * @cpus: array of CPU definition structures, the pointer is stolen
268 269 270 271 272 273 274 275
 *
 * Registers a new NUMA cell for a host, passing in a
 * array of CPU IDs belonging to the cell
 */
int
virCapabilitiesAddHostNUMACell(virCapsPtr caps,
                               int num,
                               int ncpus,
276
                               unsigned long long mem,
277
                               virCapsHostNUMACellCPUPtr cpus)
278
{
279
    virCapsHostNUMACellPtr cell;
280

E
Eric Blake 已提交
281 282
    if (VIR_RESIZE_N(caps->host.numaCell, caps->host.nnumaCell_max,
                     caps->host.nnumaCell, 1) < 0)
283 284
        return -1;

285
    if (VIR_ALLOC(cell) < 0)
286 287
        return -1;

288 289
    cell->ncpus = ncpus;
    cell->num = num;
290
    cell->mem = mem;
291
    cell->cpus = cpus;
292

E
Eric Blake 已提交
293
    caps->host.numaCell[caps->host.nnumaCell++] = cell;
294 295 296 297

    return 0;
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

/**
 * virCapabilitiesSetHostCPU:
 * @caps: capabilities to extend
 * @cpu: CPU definition
 *
 * Sets host CPU specification
 */
int
virCapabilitiesSetHostCPU(virCapsPtr caps,
                          virCPUDefPtr cpu)
{
    if (cpu == NULL)
        return -1;

    caps->host.cpu = cpu;

    return 0;
}


319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
/**
 * virCapabilitiesAllocMachines:
 * @machines: machine variants for emulator ('pc', or 'isapc', etc)
 * @nmachines: number of machine variants for emulator
 *
 * Allocate a table of virCapsGuestMachinePtr from the supplied table
 * of machine names.
 */
virCapsGuestMachinePtr *
virCapabilitiesAllocMachines(const char *const *names, int nnames)
{
    virCapsGuestMachinePtr *machines;
    int i;

    if (VIR_ALLOC_N(machines, nnames) < 0)
        return NULL;

    for (i = 0; i < nnames; i++) {
        if (VIR_ALLOC(machines[i]) < 0 ||
            !(machines[i]->name = strdup(names[i]))) {
            virCapabilitiesFreeMachines(machines, nnames);
            return NULL;
        }
    }

    return machines;
}

/**
 * virCapabilitiesFreeMachines:
 * @machines: table of vircapsGuestMachinePtr
 *
 * Free a table of virCapsGuestMachinePtr
 */
void
virCapabilitiesFreeMachines(virCapsGuestMachinePtr *machines,
                            int nmachines)
{
    int i;
    if (!machines)
        return;
    for (i = 0; i < nmachines && machines[i]; i++) {
        virCapabilitiesFreeGuestMachine(machines[i]);
        machines[i] = NULL;
    }
    VIR_FREE(machines);
}
366 367 368 369 370

/**
 * virCapabilitiesAddGuest:
 * @caps: capabilities to extend
 * @ostype: guest operating system type ('hvm' or 'xen')
371
 * @arch: guest CPU architecture
372 373 374 375 376 377 378 379 380 381 382 383 384
 * @wordsize: number of bits in CPU word
 * @emulator: path to default device emulator for arch/ostype
 * @loader: path to default BIOS loader for arch/ostype
 * @nmachines: number of machine variants for emulator
 * @machines: machine variants for emulator ('pc', or 'isapc', etc)
 *
 * Registers a new guest operating system. This should be
 * followed by registration of at least one domain for
 * running the guest
 */
virCapsGuestPtr
virCapabilitiesAddGuest(virCapsPtr caps,
                        const char *ostype,
385
                        virArch arch,
386 387 388
                        const char *emulator,
                        const char *loader,
                        int nmachines,
389
                        virCapsGuestMachinePtr *machines)
390
{
391
    virCapsGuestPtr guest;
392

393
    if (VIR_ALLOC(guest) < 0)
394 395 396 397 398
        goto no_memory;

    if ((guest->ostype = strdup(ostype)) == NULL)
        goto no_memory;

399 400
    guest->arch.id = arch;
    guest->arch.wordsize = virArchGetWordSize(arch);
401 402 403 404 405 406 407 408

    if (emulator &&
        (guest->arch.defaultInfo.emulator = strdup(emulator)) == NULL)
        goto no_memory;
    if (loader &&
        (guest->arch.defaultInfo.loader = strdup(loader)) == NULL)
        goto no_memory;

E
Eric Blake 已提交
409 410
    if (VIR_RESIZE_N(caps->guests, caps->nguests_max,
                     caps->nguests, 1) < 0)
411
        goto no_memory;
E
Eric Blake 已提交
412
    caps->guests[caps->nguests++] = guest;
413

D
Daniel P. Berrange 已提交
414 415 416 417 418
    if (nmachines) {
        guest->arch.defaultInfo.nmachines = nmachines;
        guest->arch.defaultInfo.machines = machines;
    }

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    return guest;

 no_memory:
    virCapabilitiesFreeGuest(guest);
    return NULL;
}


/**
 * virCapabilitiesAddGuestDomain:
 * @guest: guest to support
 * @hvtype: hypervisor type ('xen', 'qemu', 'kvm')
 * @emulator: specialized device emulator for domain
 * @loader: specialized BIOS loader for domain
 * @nmachines: number of machine variants for emulator
 * @machines: specialized machine variants for emulator
 *
 * Registers a virtual domain capable of running a
 * guest operating system
 */
virCapsGuestDomainPtr
virCapabilitiesAddGuestDomain(virCapsGuestPtr guest,
                              const char *hvtype,
                              const char *emulator,
                              const char *loader,
                              int nmachines,
445
                              virCapsGuestMachinePtr *machines)
446
{
447
    virCapsGuestDomainPtr dom;
448

449
    if (VIR_ALLOC(dom) < 0)
450 451 452 453 454 455 456 457 458 459 460 461
        goto no_memory;

    if ((dom->type = strdup(hvtype)) == NULL)
        goto no_memory;

    if (emulator &&
        (dom->info.emulator = strdup(emulator)) == NULL)
        goto no_memory;
    if (loader &&
        (dom->info.loader = strdup(loader)) == NULL)
        goto no_memory;

E
Eric Blake 已提交
462 463
    if (VIR_RESIZE_N(guest->arch.domains, guest->arch.ndomains_max,
                     guest->arch.ndomains, 1) < 0)
464 465 466 467
        goto no_memory;
    guest->arch.domains[guest->arch.ndomains] = dom;
    guest->arch.ndomains++;

D
Daniel P. Berrange 已提交
468 469 470 471
    if (nmachines) {
        dom->info.nmachines = nmachines;
        dom->info.machines = machines;
    }
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

    return dom;

 no_memory:
    virCapabilitiesFreeGuestDomain(dom);
    return NULL;
}


/**
 * virCapabilitiesAddGuestFeature:
 * @guest: guest to associate feature with
 * @name: name of feature ('pae', 'acpi', 'apic')
 * @defaultOn: non-zero if it defaults to on
 * @toggle: non-zero if its state can be toggled
 *
 * Registers a feature for a guest domain
 */
virCapsGuestFeaturePtr
virCapabilitiesAddGuestFeature(virCapsGuestPtr guest,
                               const char *name,
                               int defaultOn,
                               int toggle)
{
496
    virCapsGuestFeaturePtr feature;
497

498
    if (VIR_ALLOC(feature) < 0)
499 500 501 502 503 504 505
        goto no_memory;

    if ((feature->name = strdup(name)) == NULL)
        goto no_memory;
    feature->defaultOn = defaultOn;
    feature->toggle = toggle;

E
Eric Blake 已提交
506 507
    if (VIR_RESIZE_N(guest->features, guest->nfeatures_max,
                     guest->nfeatures, 1) < 0)
508
        goto no_memory;
E
Eric Blake 已提交
509
    guest->features[guest->nfeatures++] = feature;
510 511 512 513 514 515 516 517

    return feature;

 no_memory:
    virCapabilitiesFreeGuestFeature(feature);
    return NULL;
}

518 519 520
/**
 * virCapabilitiesSupportsGuestArch:
 * @caps: capabilities to query
521
 * @arch: Architecture to search for
522 523 524 525 526 527
 *
 * Returns non-zero if the capabilities support the
 * requested architecture
 */
extern int
virCapabilitiesSupportsGuestArch(virCapsPtr caps,
528
                                 virArch arch)
529 530 531
{
    int i;
    for (i = 0 ; i < caps->nguests ; i++) {
532
        if (caps->guests[i]->arch.id == arch)
533 534 535 536 537
            return 1;
    }
    return 0;
}

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

/**
 * virCapabilitiesSupportsGuestOSType:
 * @caps: capabilities to query
 * @ostype: OS type to search for (eg 'hvm', 'xen')
 *
 * Returns non-zero if the capabilities support the
 * requested operating system type
 */
extern int
virCapabilitiesSupportsGuestOSType(virCapsPtr caps,
                                   const char *ostype)
{
    int i;
    for (i = 0 ; i < caps->nguests ; i++) {
        if (STREQ(caps->guests[i]->ostype, ostype))
            return 1;
    }
    return 0;
}


560
/**
561
 * virCapabilitiesSupportsGuestOSTypeArch:
562 563
 * @caps: capabilities to query
 * @ostype: OS type to search for (eg 'hvm', 'xen')
564
 * @arch: Architecture to search for
565 566 567 568 569
 *
 * Returns non-zero if the capabilities support the
 * requested operating system type
 */
extern int
570 571
virCapabilitiesSupportsGuestOSTypeArch(virCapsPtr caps,
                                       const char *ostype,
572
                                       virArch arch)
573 574 575 576
{
    int i;
    for (i = 0 ; i < caps->nguests ; i++) {
        if (STREQ(caps->guests[i]->ostype, ostype) &&
577
            caps->guests[i]->arch.id == arch)
578 579 580 581 582 583
            return 1;
    }
    return 0;
}


584 585 586 587 588 589 590 591
/**
 * virCapabilitiesDefaultGuestArch:
 * @caps: capabilities to query
 * @ostype: OS type to search for
 *
 * Returns the first architecture able to run the
 * requested operating system type
 */
592
extern virArch
593
virCapabilitiesDefaultGuestArch(virCapsPtr caps,
594 595
                                const char *ostype,
                                const char *domain)
596
{
597
    int i, j;
598 599

    /* First try to find one matching host arch */
600
    for (i = 0 ; i < caps->nguests ; i++) {
601 602
        if (STREQ(caps->guests[i]->ostype, ostype)) {
            for (j = 0 ; j < caps->guests[i]->arch.ndomains ; j++) {
603 604 605
                if (STREQ(caps->guests[i]->arch.domains[j]->type, domain) &&
                    caps->guests[i]->arch.id == caps->host.arch)
                    return caps->guests[i]->arch.id;
606 607
            }
        }
608
    }
609 610 611 612 613 614 615 616 617 618 619 620

    /* Otherwise find the first match */
    for (i = 0 ; i < caps->nguests ; i++) {
        if (STREQ(caps->guests[i]->ostype, ostype)) {
            for (j = 0 ; j < caps->guests[i]->arch.ndomains ; j++) {
                if (STREQ(caps->guests[i]->arch.domains[j]->type, domain))
                    return caps->guests[i]->arch.id;
            }
        }
    }

    return VIR_ARCH_NONE;
621 622 623 624 625 626 627
}

/**
 * virCapabilitiesDefaultGuestMachine:
 * @caps: capabilities to query
 * @ostype: OS type to search for
 * @arch: architecture to search for
628
 * @domain: domain type to search for
629 630
 *
 * Returns the first machine variant associated with
631 632
 * the requested operating system type, architecture
 * and domain type
633 634 635 636
 */
extern const char *
virCapabilitiesDefaultGuestMachine(virCapsPtr caps,
                                   const char *ostype,
637
                                   virArch arch,
638
                                   const char *domain)
639 640
{
    int i;
641

642
    for (i = 0 ; i < caps->nguests ; i++) {
643 644 645
        virCapsGuestPtr guest = caps->guests[i];
        int j;

646 647
        if (!STREQ(guest->ostype, ostype) ||
            guest->arch.id != arch)
648 649 650
            continue;

        for (j = 0; j < guest->arch.ndomains; j++) {
651
            virCapsGuestDomainPtr dom = guest->arch.domains[j];
652 653 654 655 656 657 658 659 660 661 662

            if (!STREQ(dom->type, domain))
                continue;

            if (!dom->info.nmachines)
                break;

            return dom->info.machines[0]->name;
        }

        if (guest->arch.defaultInfo.nmachines)
663
            return caps->guests[i]->arch.defaultInfo.machines[0]->name;
664
    }
665

666 667 668 669
    return NULL;
}

/**
670
 * virCapabilitiesDefaultGuestEmulator:
671 672 673 674 675 676 677 678 679 680 681 682
 * @caps: capabilities to query
 * @ostype: OS type to search for ('xen', 'hvm')
 * @arch: architecture to search for
 * @domain: domain type ('xen', 'qemu', 'kvm')
 *
 * Returns the first emulator path associated with
 * the requested operating system type, architecture
 * and domain type
 */
extern const char *
virCapabilitiesDefaultGuestEmulator(virCapsPtr caps,
                                    const char *ostype,
683
                                    virArch arch,
684 685 686 687 688 689
                                    const char *domain)
{
    int i, j;
    for (i = 0 ; i < caps->nguests ; i++) {
        char *emulator;
        if (STREQ(caps->guests[i]->ostype, ostype) &&
690
            caps->guests[i]->arch.id == arch) {
691 692 693 694 695 696 697 698 699 700 701 702 703
            emulator = caps->guests[i]->arch.defaultInfo.emulator;
            for (j = 0 ; j < caps->guests[i]->arch.ndomains ; j++) {
                if (STREQ(caps->guests[i]->arch.domains[j]->type, domain)) {
                    if (caps->guests[i]->arch.domains[j]->info.emulator)
                        emulator = caps->guests[i]->arch.domains[j]->info.emulator;
                }
            }
            return emulator;
        }
    }
    return NULL;
}

704
static int
705 706 707 708 709 710
virCapabilitiesFormatNUMATopology(virBufferPtr xml,
                                  size_t ncells,
                                  virCapsHostNUMACellPtr *cells)
{
    int i;
    int j;
711
    char *siblings;
712 713 714 715 716

    virBufferAddLit(xml, "    <topology>\n");
    virBufferAsprintf(xml, "      <cells num='%zu'>\n", ncells);
    for (i = 0; i < ncells; i++) {
        virBufferAsprintf(xml, "        <cell id='%d'>\n", cells[i]->num);
717 718 719 720 721 722 723

        /* Print out the numacell memory total if it is available */
        if (cells[i]->mem)
            virBufferAsprintf(xml,
                              "          <memory unit='KiB'>%llu</memory>\n",
                              cells[i]->mem);

724
        virBufferAsprintf(xml, "          <cpus num='%d'>\n", cells[i]->ncpus);
725 726
        for (j = 0; j < cells[i]->ncpus; j++) {
            virBufferAsprintf(xml, "            <cpu id='%d'",
727
                              cells[i]->cpus[j].id);
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

            if (cells[i]->cpus[j].siblings) {
                if (!(siblings = virBitmapFormat(cells[i]->cpus[j].siblings))) {
                    virReportOOMError();
                    return -1;
                }

                virBufferAsprintf(xml,
                                  " socket_id='%d' core_id='%d' siblings='%s'",
                                  cells[i]->cpus[j].socket_id,
                                  cells[i]->cpus[j].core_id,
                                  siblings);
                VIR_FREE(siblings);
            }
            virBufferAddLit(xml, "/>\n");
        }

745 746 747 748 749
        virBufferAddLit(xml, "          </cpus>\n");
        virBufferAddLit(xml, "        </cell>\n");
    }
    virBufferAddLit(xml, "      </cells>\n");
    virBufferAddLit(xml, "    </topology>\n");
750 751

    return 0;
752
}
753 754 755 756 757 758 759 760 761 762 763 764

/**
 * virCapabilitiesFormatXML:
 * @caps: capabilities to format
 *
 * Convert the capabilities object into an XML representation
 *
 * Returns the XML document as a string
 */
char *
virCapabilitiesFormatXML(virCapsPtr caps)
{
765
    virBuffer xml = VIR_BUFFER_INITIALIZER;
766
    int i, j, k;
767
    char host_uuid[VIR_UUID_STRING_BUFLEN];
768

769 770
    virBufferAddLit(&xml, "<capabilities>\n\n");
    virBufferAddLit(&xml, "  <host>\n");
771 772
    if (virUUIDIsValid(caps->host.host_uuid)) {
        virUUIDFormat(caps->host.host_uuid, host_uuid);
773
        virBufferAsprintf(&xml,"    <uuid>%s</uuid>\n", host_uuid);
774
    }
775
    virBufferAddLit(&xml, "    <cpu>\n");
776 777 778
    if (caps->host.arch)
        virBufferAsprintf(&xml, "      <arch>%s</arch>\n",
                          virArchToString(caps->host.arch));
779 780

    if (caps->host.nfeatures) {
781
        virBufferAddLit(&xml, "      <features>\n");
782
        for (i = 0 ; i < caps->host.nfeatures ; i++) {
783
            virBufferAsprintf(&xml, "        <%s/>\n",
784
                              caps->host.features[i]);
785
        }
786
        virBufferAddLit(&xml, "      </features>\n");
787
    }
788

789
    virBufferAdjustIndent(&xml, 6);
790
    virCPUDefFormatBuf(&xml, caps->host.cpu, 0);
791
    virBufferAdjustIndent(&xml, -6);
792

793
    virBufferAddLit(&xml, "    </cpu>\n");
794

795 796 797 798 799 800 801 802 803 804
    /* The PM query was successful. */
    if (caps->host.powerMgmt) {
        /* The host supports some PM features. */
        unsigned int pm = caps->host.powerMgmt;
        virBufferAddLit(&xml, "    <power_management>\n");
        while (pm) {
            int bit = ffs(pm) - 1;
            virBufferAsprintf(&xml, "      <%s/>\n",
                              virCapsHostPMTargetTypeToString(bit));
            pm &= ~(1U << bit);
805
        }
806 807 808 809
        virBufferAddLit(&xml, "    </power_management>\n");
    } else {
        /* The host does not support any PM feature. */
        virBufferAddLit(&xml, "    <power_management/>\n");
810 811
    }

812
    if (caps->host.offlineMigrate) {
813 814 815
        virBufferAddLit(&xml, "    <migration_features>\n");
        if (caps->host.liveMigrate)
            virBufferAddLit(&xml, "      <live/>\n");
816
        if (caps->host.nmigrateTrans) {
817
            virBufferAddLit(&xml, "      <uri_transports>\n");
818
            for (i = 0 ; i < caps->host.nmigrateTrans ; i++) {
819
                virBufferAsprintf(&xml, "        <uri_transport>%s</uri_transport>\n",
820
                                      caps->host.migrateTrans[i]);
821
            }
822
            virBufferAddLit(&xml, "      </uri_transports>\n");
823
        }
824
        virBufferAddLit(&xml, "    </migration_features>\n");
825 826
    }

827
    if (caps->host.nnumaCell &&
828
        virCapabilitiesFormatNUMATopology(&xml, caps->host.nnumaCell,
829 830
                                          caps->host.numaCell) < 0)
        return NULL;
831

832
    for (i = 0; i < caps->host.nsecModels; i++) {
833
        virBufferAddLit(&xml, "    <secmodel>\n");
834
        virBufferAsprintf(&xml, "      <model>%s</model>\n",
835
                          caps->host.secModels[i].model);
836
        virBufferAsprintf(&xml, "      <doi>%s</doi>\n",
837
                          caps->host.secModels[i].doi);
838 839 840
        virBufferAddLit(&xml, "    </secmodel>\n");
    }

841
    virBufferAddLit(&xml, "  </host>\n\n");
842 843 844


    for (i = 0 ; i < caps->nguests ; i++) {
845
        virBufferAddLit(&xml, "  <guest>\n");
846
        virBufferAsprintf(&xml, "    <os_type>%s</os_type>\n",
847
                          caps->guests[i]->ostype);
848 849 850
        if (caps->guests[i]->arch.id)
            virBufferAsprintf(&xml, "    <arch name='%s'>\n",
                              virArchToString(caps->guests[i]->arch.id));
851
        virBufferAsprintf(&xml, "      <wordsize>%d</wordsize>\n",
852 853
                          caps->guests[i]->arch.wordsize);
        if (caps->guests[i]->arch.defaultInfo.emulator)
854
            virBufferAsprintf(&xml, "      <emulator>%s</emulator>\n",
855 856
                              caps->guests[i]->arch.defaultInfo.emulator);
            if (caps->guests[i]->arch.defaultInfo.loader)
857
                virBufferAsprintf(&xml, "      <loader>%s</loader>\n",
858
                                  caps->guests[i]->arch.defaultInfo.loader);
859 860

        for (j = 0 ; j < caps->guests[i]->arch.defaultInfo.nmachines ; j++) {
861 862 863
            virCapsGuestMachinePtr machine = caps->guests[i]->arch.defaultInfo.machines[j];
            virBufferAddLit(&xml, "      <machine");
            if (machine->canonical)
864 865
                virBufferAsprintf(&xml, " canonical='%s'", machine->canonical);
            virBufferAsprintf(&xml, ">%s</machine>\n", machine->name);
866 867 868
        }

        for (j = 0 ; j < caps->guests[i]->arch.ndomains ; j++) {
869
            virBufferAsprintf(&xml, "      <domain type='%s'>\n",
870 871
                                  caps->guests[i]->arch.domains[j]->type);
            if (caps->guests[i]->arch.domains[j]->info.emulator)
872
                virBufferAsprintf(&xml, "        <emulator>%s</emulator>\n",
873 874
                                  caps->guests[i]->arch.domains[j]->info.emulator);
            if (caps->guests[i]->arch.domains[j]->info.loader)
875
                virBufferAsprintf(&xml, "        <loader>%s</loader>\n",
876
                                  caps->guests[i]->arch.domains[j]->info.loader);
877 878

            for (k = 0 ; k < caps->guests[i]->arch.domains[j]->info.nmachines ; k++) {
879
                virCapsGuestMachinePtr machine = caps->guests[i]->arch.domains[j]->info.machines[k];
880
                virBufferAddLit(&xml, "        <machine");
881
                if (machine->canonical)
882 883
                    virBufferAsprintf(&xml, " canonical='%s'", machine->canonical);
                virBufferAsprintf(&xml, ">%s</machine>\n", machine->name);
884
            }
885
            virBufferAddLit(&xml, "      </domain>\n");
886 887
        }

888
        virBufferAddLit(&xml, "    </arch>\n");
889 890

        if (caps->guests[i]->nfeatures) {
891
            virBufferAddLit(&xml, "    <features>\n");
892 893 894 895

            for (j = 0 ; j < caps->guests[i]->nfeatures ; j++) {
                if (STREQ(caps->guests[i]->features[j]->name, "pae") ||
                    STREQ(caps->guests[i]->features[j]->name, "nonpae") ||
896
                    STREQ(caps->guests[i]->features[j]->name, "ia64_be") ||
897 898
                    STREQ(caps->guests[i]->features[j]->name, "cpuselection") ||
                    STREQ(caps->guests[i]->features[j]->name, "deviceboot")) {
899
                    virBufferAsprintf(&xml, "      <%s/>\n",
900
                                      caps->guests[i]->features[j]->name);
901
                } else {
902
                    virBufferAsprintf(&xml, "      <%s default='%s' toggle='%s'/>\n",
903 904 905
                                      caps->guests[i]->features[j]->name,
                                      caps->guests[i]->features[j]->defaultOn ? "on" : "off",
                                      caps->guests[i]->features[j]->toggle ? "yes" : "no");
906 907 908
                }
            }

909
            virBufferAddLit(&xml, "    </features>\n");
910 911
        }

912
        virBufferAddLit(&xml, "  </guest>\n\n");
913 914
    }

915
    virBufferAddLit(&xml, "</capabilities>\n");
916

917 918
    if (virBufferError(&xml)) {
        virBufferFreeAndReset(&xml);
919
        return NULL;
920
    }
921

922
    return virBufferContentAndReset(&xml);
923
}