capabilities.c 25.2 KB
Newer Older
1 2 3
/*
 * capabilities.c: hypervisor capabilities
 *
4
 * Copyright (C) 2006-2008, 2010-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

24 25
#include <config.h>

26 27
#include <strings.h>

28 29
#include "capabilities.h"
#include "buf.h"
30
#include "memory.h"
31
#include "util.h"
32
#include "uuid.h"
33
#include "cpu_conf.h"
34 35 36 37 38
#include "virterror_internal.h"


#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

/**
 * virCapabilitiesNew:
 * @arch: host machine architecture
 * @offlineMigrate: non-zero if offline migration is available
 * @liveMigrate: non-zero if live migration is available
48
 *
49 50 51 52 53 54 55 56 57
 * Allocate a new capabilities object
 */
virCapsPtr
virCapabilitiesNew(const char *arch,
                   int offlineMigrate,
                   int liveMigrate)
{
    virCapsPtr caps;

58
    if (VIR_ALLOC(caps) < 0)
59
        return NULL;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    if ((caps->host.arch = strdup(arch)) == NULL)
        goto no_memory;
    caps->host.offlineMigrate = offlineMigrate;
    caps->host.liveMigrate = liveMigrate;

    return caps;

 no_memory:
    virCapabilitiesFree(caps);
    return NULL;
}

static void
virCapabilitiesFreeHostNUMACell(virCapsHostNUMACellPtr cell)
{
76 77 78
    if (cell == NULL)
        return;

79 80
    VIR_FREE(cell->cpus);
    VIR_FREE(cell);
81 82
}

83 84 85 86 87 88 89 90 91 92
static void
virCapabilitiesFreeGuestMachine(virCapsGuestMachinePtr machine)
{
    if (machine == NULL)
        return;
    VIR_FREE(machine->name);
    VIR_FREE(machine->canonical);
    VIR_FREE(machine);
}

93 94 95 96
static void
virCapabilitiesFreeGuestDomain(virCapsGuestDomainPtr dom)
{
    int i;
97 98 99
    if (dom == NULL)
        return;

100 101
    VIR_FREE(dom->info.emulator);
    VIR_FREE(dom->info.loader);
102
    for (i = 0 ; i < dom->info.nmachines ; i++)
103
        virCapabilitiesFreeGuestMachine(dom->info.machines[i]);
104 105
    VIR_FREE(dom->info.machines);
    VIR_FREE(dom->type);
106

107
    VIR_FREE(dom);
108 109 110 111 112
}

static void
virCapabilitiesFreeGuestFeature(virCapsGuestFeaturePtr feature)
{
113 114
    if (feature == NULL)
        return;
115 116
    VIR_FREE(feature->name);
    VIR_FREE(feature);
117 118 119 120 121 122
}

static void
virCapabilitiesFreeGuest(virCapsGuestPtr guest)
{
    int i;
123 124 125
    if (guest == NULL)
        return;

126
    VIR_FREE(guest->ostype);
127

128 129 130
    VIR_FREE(guest->arch.name);
    VIR_FREE(guest->arch.defaultInfo.emulator);
    VIR_FREE(guest->arch.defaultInfo.loader);
131
    for (i = 0 ; i < guest->arch.defaultInfo.nmachines ; i++)
132
        virCapabilitiesFreeGuestMachine(guest->arch.defaultInfo.machines[i]);
133
    VIR_FREE(guest->arch.defaultInfo.machines);
134 135 136

    for (i = 0 ; i < guest->arch.ndomains ; i++)
        virCapabilitiesFreeGuestDomain(guest->arch.domains[i]);
137
    VIR_FREE(guest->arch.domains);
138 139 140

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

143
    VIR_FREE(guest);
144 145
}

146 147 148 149 150 151 152 153
void
virCapabilitiesFreeNUMAInfo(virCapsPtr caps)
{
    int i;

    for (i = 0 ; i < caps->host.nnumaCell ; i++)
        virCapabilitiesFreeHostNUMACell(caps->host.numaCell[i]);
    VIR_FREE(caps->host.numaCell);
154
    caps->host.nnumaCell = 0;
155
}
156 157 158 159 160 161 162 163 164 165

/**
 * virCapabilitiesFree:
 * @caps: object to free
 *
 * Free all memory associated with capabilities
 */
void
virCapabilitiesFree(virCapsPtr caps) {
    int i;
166 167
    if (caps == NULL)
        return;
168 169 170

    for (i = 0 ; i < caps->nguests ; i++)
        virCapabilitiesFreeGuest(caps->guests[i]);
171
    VIR_FREE(caps->guests);
172 173

    for (i = 0 ; i < caps->host.nfeatures ; i++)
174 175
        VIR_FREE(caps->host.features[i]);
    VIR_FREE(caps->host.features);
176 177

    virCapabilitiesFreeNUMAInfo(caps);
178

179
    for (i = 0 ; i < caps->host.nmigrateTrans ; i++)
180 181
        VIR_FREE(caps->host.migrateTrans[i]);
    VIR_FREE(caps->host.migrateTrans);
182

183
    VIR_FREE(caps->host.arch);
184 185
    VIR_FREE(caps->host.secModel.model);
    VIR_FREE(caps->host.secModel.doi);
186 187
    virCPUDefFree(caps->host.cpu);

188
    VIR_FREE(caps);
189 190 191 192 193 194 195 196 197 198 199 200 201 202
}


/**
 * 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 已提交
203 204
    if (VIR_RESIZE_N(caps->host.features, caps->host.nfeatures_max,
                     caps->host.nfeatures, 1) < 0)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
        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 已提交
225 226
    if (VIR_RESIZE_N(caps->host.migrateTrans, caps->host.nmigrateTrans_max,
                     caps->host.nmigrateTrans, 1) < 0)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        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
 * @cpus: array of CPU ID numbers for cell
 *
 * 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,
                               const int *cpus)
{
253
    virCapsHostNUMACellPtr cell;
254

E
Eric Blake 已提交
255 256
    if (VIR_RESIZE_N(caps->host.numaCell, caps->host.nnumaCell_max,
                     caps->host.nnumaCell, 1) < 0)
257 258
        return -1;

259
    if (VIR_ALLOC(cell) < 0)
260 261
        return -1;

262 263
    if (VIR_ALLOC_N(cell->cpus, ncpus) < 0) {
        VIR_FREE(cell);
264
        return -1;
265 266
    }
    memcpy(cell->cpus,
267 268 269
           cpus,
           ncpus * sizeof(*cpus));

270 271 272
    cell->ncpus = ncpus;
    cell->num = num;

E
Eric Blake 已提交
273
    caps->host.numaCell[caps->host.nnumaCell++] = cell;
274 275 276 277

    return 0;
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

/**
 * 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;
}


299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 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
/**
 * 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);
}
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

/**
 * virCapabilitiesAddGuest:
 * @caps: capabilities to extend
 * @ostype: guest operating system type ('hvm' or 'xen')
 * @arch: guest CPU architecture ('i686', or 'x86_64', etc)
 * @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,
                        const char *arch,
                        int wordsize,
                        const char *emulator,
                        const char *loader,
                        int nmachines,
370
                        virCapsGuestMachinePtr *machines)
371
{
372
    virCapsGuestPtr guest;
373

374
    if (VIR_ALLOC(guest) < 0)
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
        goto no_memory;

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

    if ((guest->arch.name = strdup(arch)) == NULL)
        goto no_memory;
    guest->arch.wordsize = wordsize;

    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 已提交
391 392
    if (VIR_RESIZE_N(caps->guests, caps->nguests_max,
                     caps->nguests, 1) < 0)
393
        goto no_memory;
E
Eric Blake 已提交
394
    caps->guests[caps->nguests++] = guest;
395

D
Daniel P. Berrange 已提交
396 397 398 399 400
    if (nmachines) {
        guest->arch.defaultInfo.nmachines = nmachines;
        guest->arch.defaultInfo.machines = machines;
    }

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    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,
427
                              virCapsGuestMachinePtr *machines)
428
{
429
    virCapsGuestDomainPtr dom;
430

431
    if (VIR_ALLOC(dom) < 0)
432 433 434 435 436 437 438 439 440 441 442 443
        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 已提交
444 445
    if (VIR_RESIZE_N(guest->arch.domains, guest->arch.ndomains_max,
                     guest->arch.ndomains, 1) < 0)
446 447 448 449
        goto no_memory;
    guest->arch.domains[guest->arch.ndomains] = dom;
    guest->arch.ndomains++;

D
Daniel P. Berrange 已提交
450 451 452 453
    if (nmachines) {
        dom->info.nmachines = nmachines;
        dom->info.machines = machines;
    }
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

    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)
{
478
    virCapsGuestFeaturePtr feature;
479

480
    if (VIR_ALLOC(feature) < 0)
481 482 483 484 485 486 487
        goto no_memory;

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

E
Eric Blake 已提交
488 489
    if (VIR_RESIZE_N(guest->features, guest->nfeatures_max,
                     guest->nfeatures, 1) < 0)
490
        goto no_memory;
E
Eric Blake 已提交
491
    guest->features[guest->nfeatures++] = feature;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

    return feature;

 no_memory:
    virCapabilitiesFreeGuestFeature(feature);
    return NULL;
}


/**
 * 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;
}


522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
/**
 * virCapabilitiesSupportsGuestOSType:
 * @caps: capabilities to query
 * @ostype: OS type to search for (eg 'hvm', 'xen')
 * @arch: Architecture to search for (eg, 'i686', 'x86_64')
 *
 * Returns non-zero if the capabilities support the
 * requested operating system type
 */
extern int
virCapabilitiesSupportsGuestArch(virCapsPtr caps,
                                 const char *ostype,
                                 const char *arch)
{
    int i;
    for (i = 0 ; i < caps->nguests ; i++) {
        if (STREQ(caps->guests[i]->ostype, ostype) &&
            STREQ(caps->guests[i]->arch.name, arch))
            return 1;
    }
    return 0;
}


546 547 548 549 550 551 552 553 554 555
/**
 * virCapabilitiesDefaultGuestArch:
 * @caps: capabilities to query
 * @ostype: OS type to search for
 *
 * Returns the first architecture able to run the
 * requested operating system type
 */
extern const char *
virCapabilitiesDefaultGuestArch(virCapsPtr caps,
556 557
                                const char *ostype,
                                const char *domain)
558
{
559 560
    int i, j;
    const char *arch = NULL;
561
    for (i = 0 ; i < caps->nguests ; i++) {
562 563 564 565 566 567 568 569 570 571 572 573
        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)) {
                    /* Use the first match... */
                    if (!arch)
                        arch = caps->guests[i]->arch.name;
                    /* ...unless we can match the host's architecture. */
                    if (STREQ(caps->guests[i]->arch.name, caps->host.arch))
                        return caps->guests[i]->arch.name;
                }
            }
        }
574
    }
575
    return arch;
576 577 578 579 580 581 582
}

/**
 * virCapabilitiesDefaultGuestMachine:
 * @caps: capabilities to query
 * @ostype: OS type to search for
 * @arch: architecture to search for
583
 * @domain: domain type to search for
584 585
 *
 * Returns the first machine variant associated with
586 587
 * the requested operating system type, architecture
 * and domain type
588 589 590 591
 */
extern const char *
virCapabilitiesDefaultGuestMachine(virCapsPtr caps,
                                   const char *ostype,
592 593
                                   const char *arch,
                                   const char *domain)
594 595
{
    int i;
596

597
    for (i = 0 ; i < caps->nguests ; i++) {
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
        virCapsGuestPtr guest = caps->guests[i];
        int j;

        if (!STREQ(guest->ostype, ostype) || !STREQ(guest->arch.name, arch))
            continue;

        for (j = 0; j < guest->arch.ndomains; j++) {
            virCapsGuestDomainPtr dom= guest->arch.domains[j];

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

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

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

        if (guest->arch.defaultInfo.nmachines)
617
            return caps->guests[i]->arch.defaultInfo.machines[0]->name;
618
    }
619

620 621 622 623
    return NULL;
}

/**
624
 * virCapabilitiesDefaultGuestEmulator:
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
 * @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,
                                    const char *arch,
                                    const char *domain)
{
    int i, j;
    for (i = 0 ; i < caps->nguests ; i++) {
        char *emulator;
        if (STREQ(caps->guests[i]->ostype, ostype) &&
            STREQ(caps->guests[i]->arch.name, arch)) {
            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;
}


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

674 675
    virBufferAddLit(&xml, "<capabilities>\n\n");
    virBufferAddLit(&xml, "  <host>\n");
676 677
    if (virUUIDIsValid(caps->host.host_uuid)) {
        virUUIDFormat(caps->host.host_uuid, host_uuid);
678
        virBufferAsprintf(&xml,"    <uuid>%s</uuid>\n", host_uuid);
679
    }
680
    virBufferAddLit(&xml, "    <cpu>\n");
681
    virBufferAsprintf(&xml, "      <arch>%s</arch>\n",
682
                      caps->host.arch);
683 684

    if (caps->host.nfeatures) {
685
        virBufferAddLit(&xml, "      <features>\n");
686
        for (i = 0 ; i < caps->host.nfeatures ; i++) {
687
            virBufferAsprintf(&xml, "        <%s/>\n",
688
                              caps->host.features[i]);
689
        }
690
        virBufferAddLit(&xml, "      </features>\n");
691
    }
692

693 694 695
    virBufferAdjustIndent(&xml, 6);
    virCPUDefFormatBuf(&xml, caps->host.cpu);
    virBufferAdjustIndent(&xml, -6);
696

697
    virBufferAddLit(&xml, "    </cpu>\n");
698

699 700 701 702 703 704 705 706 707 708
    /* 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);
709
        }
710 711 712 713
        virBufferAddLit(&xml, "    </power_management>\n");
    } else {
        /* The host does not support any PM feature. */
        virBufferAddLit(&xml, "    <power_management/>\n");
714 715
    }

716
    if (caps->host.offlineMigrate) {
717 718 719
        virBufferAddLit(&xml, "    <migration_features>\n");
        if (caps->host.liveMigrate)
            virBufferAddLit(&xml, "      <live/>\n");
720
        if (caps->host.nmigrateTrans) {
721
            virBufferAddLit(&xml, "      <uri_transports>\n");
722
            for (i = 0 ; i < caps->host.nmigrateTrans ; i++) {
723
                virBufferAsprintf(&xml, "        <uri_transport>%s</uri_transport>\n",
724
                                      caps->host.migrateTrans[i]);
725
            }
726
            virBufferAddLit(&xml, "      </uri_transports>\n");
727
        }
728
        virBufferAddLit(&xml, "    </migration_features>\n");
729 730 731
    }

    if (caps->host.nnumaCell) {
732
        virBufferAddLit(&xml, "    <topology>\n");
733
        virBufferAsprintf(&xml, "      <cells num='%zu'>\n",
734
                          caps->host.nnumaCell);
735
        for (i = 0 ; i < caps->host.nnumaCell ; i++) {
736
            virBufferAsprintf(&xml, "        <cell id='%d'>\n",
737
                              caps->host.numaCell[i]->num);
738
            virBufferAsprintf(&xml, "          <cpus num='%d'>\n",
739
                              caps->host.numaCell[i]->ncpus);
740
            for (j = 0 ; j < caps->host.numaCell[i]->ncpus ; j++)
741
                virBufferAsprintf(&xml, "            <cpu id='%d'/>\n",
742 743 744
                                  caps->host.numaCell[i]->cpus[j]);
            virBufferAddLit(&xml, "          </cpus>\n");
            virBufferAddLit(&xml, "        </cell>\n");
745
        }
746 747
        virBufferAddLit(&xml, "      </cells>\n");
        virBufferAddLit(&xml, "    </topology>\n");
748
    }
749 750 751

    if (caps->host.secModel.model) {
        virBufferAddLit(&xml, "    <secmodel>\n");
752 753
        virBufferAsprintf(&xml, "      <model>%s</model>\n", caps->host.secModel.model);
        virBufferAsprintf(&xml, "      <doi>%s</doi>\n", caps->host.secModel.doi);
754 755 756
        virBufferAddLit(&xml, "    </secmodel>\n");
    }

757
    virBufferAddLit(&xml, "  </host>\n\n");
758 759 760


    for (i = 0 ; i < caps->nguests ; i++) {
761
        virBufferAddLit(&xml, "  <guest>\n");
762
        virBufferAsprintf(&xml, "    <os_type>%s</os_type>\n",
763
                          caps->guests[i]->ostype);
764
        virBufferAsprintf(&xml, "    <arch name='%s'>\n",
765
                          caps->guests[i]->arch.name);
766
        virBufferAsprintf(&xml, "      <wordsize>%d</wordsize>\n",
767 768
                          caps->guests[i]->arch.wordsize);
        if (caps->guests[i]->arch.defaultInfo.emulator)
769
            virBufferAsprintf(&xml, "      <emulator>%s</emulator>\n",
770 771
                              caps->guests[i]->arch.defaultInfo.emulator);
            if (caps->guests[i]->arch.defaultInfo.loader)
772
                virBufferAsprintf(&xml, "      <loader>%s</loader>\n",
773
                                  caps->guests[i]->arch.defaultInfo.loader);
774 775

        for (j = 0 ; j < caps->guests[i]->arch.defaultInfo.nmachines ; j++) {
776 777 778
            virCapsGuestMachinePtr machine = caps->guests[i]->arch.defaultInfo.machines[j];
            virBufferAddLit(&xml, "      <machine");
            if (machine->canonical)
779 780
                virBufferAsprintf(&xml, " canonical='%s'", machine->canonical);
            virBufferAsprintf(&xml, ">%s</machine>\n", machine->name);
781 782 783
        }

        for (j = 0 ; j < caps->guests[i]->arch.ndomains ; j++) {
784
            virBufferAsprintf(&xml, "      <domain type='%s'>\n",
785 786
                                  caps->guests[i]->arch.domains[j]->type);
            if (caps->guests[i]->arch.domains[j]->info.emulator)
787
                virBufferAsprintf(&xml, "        <emulator>%s</emulator>\n",
788 789
                                  caps->guests[i]->arch.domains[j]->info.emulator);
            if (caps->guests[i]->arch.domains[j]->info.loader)
790
                virBufferAsprintf(&xml, "        <loader>%s</loader>\n",
791
                                  caps->guests[i]->arch.domains[j]->info.loader);
792 793

            for (k = 0 ; k < caps->guests[i]->arch.domains[j]->info.nmachines ; k++) {
794
                virCapsGuestMachinePtr machine = caps->guests[i]->arch.domains[j]->info.machines[k];
795
                virBufferAddLit(&xml, "        <machine");
796
                if (machine->canonical)
797 798
                    virBufferAsprintf(&xml, " canonical='%s'", machine->canonical);
                virBufferAsprintf(&xml, ">%s</machine>\n", machine->name);
799
            }
800
            virBufferAddLit(&xml, "      </domain>\n");
801 802
        }

803
        virBufferAddLit(&xml, "    </arch>\n");
804 805

        if (caps->guests[i]->nfeatures) {
806
            virBufferAddLit(&xml, "    <features>\n");
807 808 809 810

            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") ||
811
                    STREQ(caps->guests[i]->features[j]->name, "ia64_be") ||
812 813
                    STREQ(caps->guests[i]->features[j]->name, "cpuselection") ||
                    STREQ(caps->guests[i]->features[j]->name, "deviceboot")) {
814
                    virBufferAsprintf(&xml, "      <%s/>\n",
815
                                      caps->guests[i]->features[j]->name);
816
                } else {
817
                    virBufferAsprintf(&xml, "      <%s default='%s' toggle='%s'/>\n",
818 819 820
                                      caps->guests[i]->features[j]->name,
                                      caps->guests[i]->features[j]->defaultOn ? "on" : "off",
                                      caps->guests[i]->features[j]->toggle ? "yes" : "no");
821 822 823
                }
            }

824
            virBufferAddLit(&xml, "    </features>\n");
825 826
        }

827
        virBufferAddLit(&xml, "  </guest>\n\n");
828 829
    }

830
    virBufferAddLit(&xml, "</capabilities>\n");
831

832 833
    if (virBufferError(&xml)) {
        virBufferFreeAndReset(&xml);
834
        return NULL;
835
    }
836

837
    return virBufferContentAndReset(&xml);
838
}
839 840 841 842 843 844 845 846 847 848 849 850 851 852

extern void
virCapabilitiesSetMacPrefix(virCapsPtr caps,
                            unsigned char *prefix)
{
    memcpy(caps->macPrefix, prefix, sizeof(caps->macPrefix));
}

extern void
virCapabilitiesGenerateMac(virCapsPtr caps,
                           unsigned char *mac)
{
    virGenerateMacAddr(caps->macPrefix, mac);
}
853 854 855 856 857 858 859 860 861 862

extern void
virCapabilitiesSetEmulatorRequired(virCapsPtr caps) {
    caps->emulatorRequired = 1;
}

extern unsigned int
virCapabilitiesIsEmulatorRequired(virCapsPtr caps) {
    return caps->emulatorRequired;
}