You need to sign in or sign up before continuing.
capabilities.c 31.1 KB
Newer Older
1 2 3
/*
 * capabilities.c: hypervisor capabilities
 *
4
 * Copyright (C) 2006-2014 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 "viruuid.h"
32
#include "cpu_conf.h"
33
#include "virerror.h"
34
#include "virstring.h"
35 36 37

#define VIR_FROM_THIS VIR_FROM_CAPABILITIES

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

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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)

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

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

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

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

    return caps;
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
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;
    }
}

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

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

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

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

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

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

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

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

static void
virCapabilitiesFreeGuest(virCapsGuestPtr guest)
{
152
    size_t i;
153 154 155
    if (guest == NULL)
        return;

156
    VIR_FREE(guest->ostype);
157

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

164
    for (i = 0; i < guest->arch.ndomains; i++)
165
        virCapabilitiesFreeGuestDomain(guest->arch.domains[i]);
166
    VIR_FREE(guest->arch.domains);
167

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

172
    VIR_FREE(guest);
173 174
}

175 176 177
void
virCapabilitiesFreeNUMAInfo(virCapsPtr caps)
{
178
    size_t i;
179

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

186 187 188 189 190 191 192 193 194 195 196 197 198 199
static void
virCapabilitiesClearSecModel(virCapsHostSecModelPtr secmodel)
{
    size_t i;
    for (i = 0; i < secmodel->nlabels; i++) {
        VIR_FREE(secmodel->labels[i].type);
        VIR_FREE(secmodel->labels[i].label);
    }

    VIR_FREE(secmodel->labels);
    VIR_FREE(secmodel->model);
    VIR_FREE(secmodel->doi);
}

200 201 202 203
static void
virCapabilitiesDispose(void *object)
{
    virCapsPtr caps = object;
204
    size_t i;
205

206
    for (i = 0; i < caps->nguests; i++)
207
        virCapabilitiesFreeGuest(caps->guests[i]);
208
    VIR_FREE(caps->guests);
209

210
    for (i = 0; i < caps->host.nfeatures; i++)
211 212
        VIR_FREE(caps->host.features[i]);
    VIR_FREE(caps->host.features);
213 214

    virCapabilitiesFreeNUMAInfo(caps);
215

216
    for (i = 0; i < caps->host.nmigrateTrans; i++)
217 218
        VIR_FREE(caps->host.migrateTrans[i]);
    VIR_FREE(caps->host.migrateTrans);
219

220
    for (i = 0; i < caps->host.nsecModels; i++) {
221
        virCapabilitiesClearSecModel(&caps->host.secModels[i]);
222 223 224
    }
    VIR_FREE(caps->host.secModels);

225
    virCPUDefFree(caps->host.cpu);
226 227 228 229 230 231 232 233 234 235 236 237 238 239
}


/**
 * 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 已提交
240 241
    if (VIR_RESIZE_N(caps->host.features, caps->host.nfeatures_max,
                     caps->host.nfeatures, 1) < 0)
242 243
        return -1;

244
    if (VIR_STRDUP(caps->host.features[caps->host.nfeatures], name) < 0)
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        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 已提交
262 263
    if (VIR_RESIZE_N(caps->host.migrateTrans, caps->host.nmigrateTrans_max,
                     caps->host.nmigrateTrans, 1) < 0)
264 265
        return -1;

266
    if (VIR_STRDUP(caps->host.migrateTrans[caps->host.nmigrateTrans], name) < 0)
267 268 269 270 271 272 273 274 275 276 277 278
        return -1;
    caps->host.nmigrateTrans++;

    return 0;
}


/**
 * virCapabilitiesAddHostNUMACell:
 * @caps: capabilities to extend
 * @num: ID number of NUMA cell
 * @ncpus: number of CPUs in cell
279
 * @mem: Total size of memory in the NUMA node (in KiB)
280
 * @cpus: array of CPU definition structures, the pointer is stolen
281 282 283 284 285 286 287 288
 *
 * 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,
289
                               unsigned long long mem,
290
                               virCapsHostNUMACellCPUPtr cpus)
291
{
292
    virCapsHostNUMACellPtr cell;
293

E
Eric Blake 已提交
294 295
    if (VIR_RESIZE_N(caps->host.numaCell, caps->host.nnumaCell_max,
                     caps->host.nnumaCell, 1) < 0)
296 297
        return -1;

298
    if (VIR_ALLOC(cell) < 0)
299 300
        return -1;

301 302
    cell->ncpus = ncpus;
    cell->num = num;
303
    cell->mem = mem;
304
    cell->cpus = cpus;
305

E
Eric Blake 已提交
306
    caps->host.numaCell[caps->host.nnumaCell++] = cell;
307 308 309 310

    return 0;
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

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


332 333 334 335 336 337 338 339 340 341 342 343
/**
 * 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;
344
    size_t i;
345 346 347 348 349 350

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

    for (i = 0; i < nnames; i++) {
        if (VIR_ALLOC(machines[i]) < 0 ||
351
            VIR_STRDUP(machines[i]->name, names[i]) < 0) {
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
            virCapabilitiesFreeMachines(machines, nnames);
            return NULL;
        }
    }

    return machines;
}

/**
 * virCapabilitiesFreeMachines:
 * @machines: table of vircapsGuestMachinePtr
 *
 * Free a table of virCapsGuestMachinePtr
 */
void
virCapabilitiesFreeMachines(virCapsGuestMachinePtr *machines,
                            int nmachines)
{
370
    size_t i;
371 372 373 374 375 376 377 378
    if (!machines)
        return;
    for (i = 0; i < nmachines && machines[i]; i++) {
        virCapabilitiesFreeGuestMachine(machines[i]);
        machines[i] = NULL;
    }
    VIR_FREE(machines);
}
379 380 381 382 383

/**
 * virCapabilitiesAddGuest:
 * @caps: capabilities to extend
 * @ostype: guest operating system type ('hvm' or 'xen')
384
 * @arch: guest CPU architecture
385 386 387 388 389 390 391 392 393 394 395 396 397
 * @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,
398
                        virArch arch,
399 400 401
                        const char *emulator,
                        const char *loader,
                        int nmachines,
402
                        virCapsGuestMachinePtr *machines)
403
{
404
    virCapsGuestPtr guest;
405

406
    if (VIR_ALLOC(guest) < 0)
407
        goto error;
408

409 410
    if (VIR_STRDUP(guest->ostype, ostype) < 0)
        goto error;
411

412 413
    guest->arch.id = arch;
    guest->arch.wordsize = virArchGetWordSize(arch);
414

415 416 417
    if (VIR_STRDUP(guest->arch.defaultInfo.emulator, emulator) < 0 ||
        VIR_STRDUP(guest->arch.defaultInfo.loader, loader) < 0)
        goto error;
418

E
Eric Blake 已提交
419 420
    if (VIR_RESIZE_N(caps->guests, caps->nguests_max,
                     caps->nguests, 1) < 0)
421
        goto error;
E
Eric Blake 已提交
422
    caps->guests[caps->nguests++] = guest;
423

D
Daniel P. Berrange 已提交
424 425 426 427 428
    if (nmachines) {
        guest->arch.defaultInfo.nmachines = nmachines;
        guest->arch.defaultInfo.machines = machines;
    }

429 430
    return guest;

431
 error:
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    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,
455
                              virCapsGuestMachinePtr *machines)
456
{
457
    virCapsGuestDomainPtr dom;
458

459
    if (VIR_ALLOC(dom) < 0)
460
        goto error;
461

462 463 464 465
    if (VIR_STRDUP(dom->type, hvtype) < 0 ||
        VIR_STRDUP(dom->info.emulator, emulator) < 0 ||
        VIR_STRDUP(dom->info.loader, loader) < 0)
        goto error;
466

E
Eric Blake 已提交
467 468
    if (VIR_RESIZE_N(guest->arch.domains, guest->arch.ndomains_max,
                     guest->arch.ndomains, 1) < 0)
469
        goto error;
470 471 472
    guest->arch.domains[guest->arch.ndomains] = dom;
    guest->arch.ndomains++;

D
Daniel P. Berrange 已提交
473 474 475 476
    if (nmachines) {
        dom->info.nmachines = nmachines;
        dom->info.machines = machines;
    }
477 478 479

    return dom;

480
 error:
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    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)
{
501
    virCapsGuestFeaturePtr feature;
502

503
    if (VIR_ALLOC(feature) < 0)
504 505
        goto no_memory;

506
    if (VIR_STRDUP(feature->name, name) < 0)
507 508 509 510
        goto no_memory;
    feature->defaultOn = defaultOn;
    feature->toggle = toggle;

E
Eric Blake 已提交
511 512
    if (VIR_RESIZE_N(guest->features, guest->nfeatures_max,
                     guest->nfeatures, 1) < 0)
513
        goto no_memory;
E
Eric Blake 已提交
514
    guest->features[guest->nfeatures++] = feature;
515 516 517 518 519 520 521 522

    return feature;

 no_memory:
    virCapabilitiesFreeGuestFeature(feature);
    return NULL;
}

523 524 525 526 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
/**
 * virCapabilitiesHostSecModelAddBaseLabel
 * @secmodel: Security model to add a base label for
 * @type: virtualization type
 * @label: base label
 *
 * Returns non-zero on error.
 */
extern int
virCapabilitiesHostSecModelAddBaseLabel(virCapsHostSecModelPtr secmodel,
                                        const char *type,
                                        const char *label)
{
    char *t = NULL, *l = NULL;

    if (type == NULL || label == NULL)
        return -1;

    if (VIR_STRDUP(t, type) < 0)
        goto no_memory;

    if (VIR_STRDUP(l, label) < 0)
        goto no_memory;

    if (VIR_EXPAND_N(secmodel->labels, secmodel->nlabels, 1) < 0)
        goto no_memory;

    secmodel->labels[secmodel->nlabels - 1].type = t;
    secmodel->labels[secmodel->nlabels - 1].label = l;

    return 0;

555
 no_memory:
556 557 558 559 560
    VIR_FREE(l);
    VIR_FREE(t);
    return -1;
}

561 562 563
/**
 * virCapabilitiesSupportsGuestArch:
 * @caps: capabilities to query
564
 * @arch: Architecture to search for
565 566 567 568 569 570
 *
 * Returns non-zero if the capabilities support the
 * requested architecture
 */
extern int
virCapabilitiesSupportsGuestArch(virCapsPtr caps,
571
                                 virArch arch)
572
{
573
    size_t i;
574
    for (i = 0; i < caps->nguests; i++) {
575
        if (caps->guests[i]->arch.id == arch)
576 577 578 579 580
            return 1;
    }
    return 0;
}

581 582 583 584 585 586 587 588 589 590 591 592 593

/**
 * 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)
{
594
    size_t i;
595
    for (i = 0; i < caps->nguests; i++) {
596 597 598 599 600 601 602
        if (STREQ(caps->guests[i]->ostype, ostype))
            return 1;
    }
    return 0;
}


603
/**
604
 * virCapabilitiesSupportsGuestOSTypeArch:
605 606
 * @caps: capabilities to query
 * @ostype: OS type to search for (eg 'hvm', 'xen')
607
 * @arch: Architecture to search for
608 609 610 611 612
 *
 * Returns non-zero if the capabilities support the
 * requested operating system type
 */
extern int
613 614
virCapabilitiesSupportsGuestOSTypeArch(virCapsPtr caps,
                                       const char *ostype,
615
                                       virArch arch)
616
{
617
    size_t i;
618
    for (i = 0; i < caps->nguests; i++) {
619
        if (STREQ(caps->guests[i]->ostype, ostype) &&
620
            caps->guests[i]->arch.id == arch)
621 622 623 624 625 626
            return 1;
    }
    return 0;
}


627 628 629 630 631 632 633 634
/**
 * virCapabilitiesDefaultGuestArch:
 * @caps: capabilities to query
 * @ostype: OS type to search for
 *
 * Returns the first architecture able to run the
 * requested operating system type
 */
635
extern virArch
636
virCapabilitiesDefaultGuestArch(virCapsPtr caps,
637 638
                                const char *ostype,
                                const char *domain)
639
{
640
    size_t i, j;
641 642

    /* First try to find one matching host arch */
643
    for (i = 0; i < caps->nguests; i++) {
644
        if (STREQ(caps->guests[i]->ostype, ostype)) {
645
            for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
646 647 648
                if (STREQ(caps->guests[i]->arch.domains[j]->type, domain) &&
                    caps->guests[i]->arch.id == caps->host.arch)
                    return caps->guests[i]->arch.id;
649 650
            }
        }
651
    }
652 653

    /* Otherwise find the first match */
654
    for (i = 0; i < caps->nguests; i++) {
655
        if (STREQ(caps->guests[i]->ostype, ostype)) {
656
            for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
657 658 659 660 661 662 663
                if (STREQ(caps->guests[i]->arch.domains[j]->type, domain))
                    return caps->guests[i]->arch.id;
            }
        }
    }

    return VIR_ARCH_NONE;
664 665 666 667 668 669 670
}

/**
 * virCapabilitiesDefaultGuestMachine:
 * @caps: capabilities to query
 * @ostype: OS type to search for
 * @arch: architecture to search for
671
 * @domain: domain type to search for
672 673
 *
 * Returns the first machine variant associated with
674 675
 * the requested operating system type, architecture
 * and domain type
676 677 678 679
 */
extern const char *
virCapabilitiesDefaultGuestMachine(virCapsPtr caps,
                                   const char *ostype,
680
                                   virArch arch,
681
                                   const char *domain)
682
{
683
    size_t i;
684

685
    for (i = 0; i < caps->nguests; i++) {
686
        virCapsGuestPtr guest = caps->guests[i];
687
        size_t j;
688

689 690
        if (!STREQ(guest->ostype, ostype) ||
            guest->arch.id != arch)
691 692 693
            continue;

        for (j = 0; j < guest->arch.ndomains; j++) {
694
            virCapsGuestDomainPtr dom = guest->arch.domains[j];
695 696 697 698 699 700 701 702 703 704 705

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

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

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

        if (guest->arch.defaultInfo.nmachines)
706
            return caps->guests[i]->arch.defaultInfo.machines[0]->name;
707
    }
708

709 710 711 712
    return NULL;
}

/**
713
 * virCapabilitiesDefaultGuestEmulator:
714 715 716 717 718 719 720 721 722 723 724 725
 * @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,
726
                                    virArch arch,
727 728
                                    const char *domain)
{
729
    size_t i, j;
730
    for (i = 0; i < caps->nguests; i++) {
731 732
        char *emulator;
        if (STREQ(caps->guests[i]->ostype, ostype) &&
733
            caps->guests[i]->arch.id == arch) {
734
            emulator = caps->guests[i]->arch.defaultInfo.emulator;
735
            for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
736 737 738 739 740 741 742 743 744 745 746
                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;
}

747
static int
748
virCapabilitiesFormatNUMATopology(virBufferPtr buf,
749 750 751
                                  size_t ncells,
                                  virCapsHostNUMACellPtr *cells)
{
752 753
    size_t i;
    size_t j;
754
    char *siblings;
755

756 757 758 759
    virBufferAddLit(buf, "<topology>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferAsprintf(buf, "<cells num='%zu'>\n", ncells);
    virBufferAdjustIndent(buf, 2);
760
    for (i = 0; i < ncells; i++) {
761 762
        virBufferAsprintf(buf, "<cell id='%d'>\n", cells[i]->num);
        virBufferAdjustIndent(buf, 2);
763 764 765

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

769 770
        virBufferAsprintf(buf, "<cpus num='%d'>\n", cells[i]->ncpus);
        virBufferAdjustIndent(buf, 2);
771
        for (j = 0; j < cells[i]->ncpus; j++) {
772
            virBufferAsprintf(buf, "<cpu id='%d'", cells[i]->cpus[j].id);
773 774 775 776 777 778 779

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

780
                virBufferAsprintf(buf,
781 782 783 784 785 786
                                  " socket_id='%d' core_id='%d' siblings='%s'",
                                  cells[i]->cpus[j].socket_id,
                                  cells[i]->cpus[j].core_id,
                                  siblings);
                VIR_FREE(siblings);
            }
787
            virBufferAddLit(buf, "/>\n");
788
        }
789 790 791 792
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</cpus>\n");
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</cell>\n");
793
    }
794 795 796 797
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</cells>\n");
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</topology>\n");
798
    return 0;
799
}
800 801 802 803 804 805 806 807 808 809 810 811

/**
 * virCapabilitiesFormatXML:
 * @caps: capabilities to format
 *
 * Convert the capabilities object into an XML representation
 *
 * Returns the XML document as a string
 */
char *
virCapabilitiesFormatXML(virCapsPtr caps)
{
812
    virBuffer buf = VIR_BUFFER_INITIALIZER;
813
    size_t i, j, k;
814
    char host_uuid[VIR_UUID_STRING_BUFLEN];
815

816 817 818 819
    virBufferAddLit(&buf, "<capabilities>\n\n");
    virBufferAdjustIndent(&buf, 2);
    virBufferAddLit(&buf, "<host>\n");
    virBufferAdjustIndent(&buf, 2);
820 821
    if (virUUIDIsValid(caps->host.host_uuid)) {
        virUUIDFormat(caps->host.host_uuid, host_uuid);
822
        virBufferAsprintf(&buf, "<uuid>%s</uuid>\n", host_uuid);
823
    }
824 825 826
    virBufferAddLit(&buf, "<cpu>\n");
    virBufferAdjustIndent(&buf, 2);

827
    if (caps->host.arch)
828
        virBufferAsprintf(&buf, "<arch>%s</arch>\n",
829
                          virArchToString(caps->host.arch));
830
    if (caps->host.nfeatures) {
831 832
        virBufferAddLit(&buf, "<features>\n");
        virBufferAdjustIndent(&buf, 2);
833
        for (i = 0; i < caps->host.nfeatures; i++) {
834
            virBufferAsprintf(&buf, "<%s/>\n",
835
                              caps->host.features[i]);
836
        }
837 838
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</features>\n");
839
    }
840
    virCPUDefFormatBuf(&buf, caps->host.cpu, 0);
841

842 843
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</cpu>\n");
844

845 846 847 848
    /* The PM query was successful. */
    if (caps->host.powerMgmt) {
        /* The host supports some PM features. */
        unsigned int pm = caps->host.powerMgmt;
849 850
        virBufferAddLit(&buf, "<power_management>\n");
        virBufferAdjustIndent(&buf, 2);
851 852
        while (pm) {
            int bit = ffs(pm) - 1;
853
            virBufferAsprintf(&buf, "<%s/>\n",
854 855
                              virCapsHostPMTargetTypeToString(bit));
            pm &= ~(1U << bit);
856
        }
857 858
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</power_management>\n");
859 860
    } else {
        /* The host does not support any PM feature. */
861
        virBufferAddLit(&buf, "<power_management/>\n");
862 863
    }

864
    if (caps->host.offlineMigrate) {
865 866
        virBufferAddLit(&buf, "<migration_features>\n");
        virBufferAdjustIndent(&buf, 2);
867
        if (caps->host.liveMigrate)
868
            virBufferAddLit(&buf, "<live/>\n");
869
        if (caps->host.nmigrateTrans) {
870 871
            virBufferAddLit(&buf, "<uri_transports>\n");
            virBufferAdjustIndent(&buf, 2);
872
            for (i = 0; i < caps->host.nmigrateTrans; i++) {
873
                virBufferAsprintf(&buf, "<uri_transport>%s</uri_transport>\n",
874
                                      caps->host.migrateTrans[i]);
875
            }
876 877
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</uri_transports>\n");
878
        }
879 880
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</migration_features>\n");
881 882
    }

883
    if (caps->host.nnumaCell &&
884
        virCapabilitiesFormatNUMATopology(&buf, caps->host.nnumaCell,
885 886
                                          caps->host.numaCell) < 0)
        return NULL;
887

888
    for (i = 0; i < caps->host.nsecModels; i++) {
889 890 891
        virBufferAddLit(&buf, "<secmodel>\n");
        virBufferAdjustIndent(&buf, 2);
        virBufferAsprintf(&buf, "<model>%s</model>\n",
892
                          caps->host.secModels[i].model);
893
        virBufferAsprintf(&buf, "<doi>%s</doi>\n",
894
                          caps->host.secModels[i].doi);
895
        for (j = 0; j < caps->host.secModels[i].nlabels; j++) {
896
            virBufferAsprintf(&buf, "<baselabel type='%s'>%s</baselabel>\n",
897 898 899
                              caps->host.secModels[i].labels[j].type,
                              caps->host.secModels[i].labels[j].label);
        }
900 901
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</secmodel>\n");
902 903
    }

904 905
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</host>\n\n");
906 907


908
    for (i = 0; i < caps->nguests; i++) {
909 910 911
        virBufferAddLit(&buf, "<guest>\n");
        virBufferAdjustIndent(&buf, 2);
        virBufferAsprintf(&buf, "<os_type>%s</os_type>\n",
912
                          caps->guests[i]->ostype);
913
        if (caps->guests[i]->arch.id)
914
            virBufferAsprintf(&buf, "<arch name='%s'>\n",
915
                              virArchToString(caps->guests[i]->arch.id));
916 917
        virBufferAdjustIndent(&buf, 2);
        virBufferAsprintf(&buf, "<wordsize>%d</wordsize>\n",
918 919
                          caps->guests[i]->arch.wordsize);
        if (caps->guests[i]->arch.defaultInfo.emulator)
920
            virBufferAsprintf(&buf, "<emulator>%s</emulator>\n",
921 922
                              caps->guests[i]->arch.defaultInfo.emulator);
            if (caps->guests[i]->arch.defaultInfo.loader)
923
                virBufferAsprintf(&buf, "<loader>%s</loader>\n",
924
                                  caps->guests[i]->arch.defaultInfo.loader);
925

926
        for (j = 0; j < caps->guests[i]->arch.defaultInfo.nmachines; j++) {
927
            virCapsGuestMachinePtr machine = caps->guests[i]->arch.defaultInfo.machines[j];
928
            virBufferAddLit(&buf, "<machine");
929
            if (machine->canonical)
930
                virBufferAsprintf(&buf, " canonical='%s'", machine->canonical);
931
            if (machine->maxCpus > 0)
932 933
                virBufferAsprintf(&buf, " maxCpus='%d'", machine->maxCpus);
            virBufferAsprintf(&buf, ">%s</machine>\n", machine->name);
934 935
        }

936
        for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
937
            virBufferAsprintf(&buf, "<domain type='%s'>\n",
938
                                  caps->guests[i]->arch.domains[j]->type);
939
            virBufferAdjustIndent(&buf, 2);
940
            if (caps->guests[i]->arch.domains[j]->info.emulator)
941
                virBufferAsprintf(&buf, "<emulator>%s</emulator>\n",
942 943
                                  caps->guests[i]->arch.domains[j]->info.emulator);
            if (caps->guests[i]->arch.domains[j]->info.loader)
944
                virBufferAsprintf(&buf, "<loader>%s</loader>\n",
945
                                  caps->guests[i]->arch.domains[j]->info.loader);
946

947
            for (k = 0; k < caps->guests[i]->arch.domains[j]->info.nmachines; k++) {
948
                virCapsGuestMachinePtr machine = caps->guests[i]->arch.domains[j]->info.machines[k];
949
                virBufferAddLit(&buf, "<machine");
950
                if (machine->canonical)
951
                    virBufferAsprintf(&buf, " canonical='%s'", machine->canonical);
952
                if (machine->maxCpus > 0)
953 954
                    virBufferAsprintf(&buf, " maxCpus='%d'", machine->maxCpus);
                virBufferAsprintf(&buf, ">%s</machine>\n", machine->name);
955
            }
956 957
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</domain>\n");
958 959
        }

960 961
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</arch>\n");
962 963

        if (caps->guests[i]->nfeatures) {
964 965
            virBufferAddLit(&buf, "<features>\n");
            virBufferAdjustIndent(&buf, 2);
966

967
            for (j = 0; j < caps->guests[i]->nfeatures; j++) {
968 969
                if (STREQ(caps->guests[i]->features[j]->name, "pae") ||
                    STREQ(caps->guests[i]->features[j]->name, "nonpae") ||
970
                    STREQ(caps->guests[i]->features[j]->name, "ia64_be") ||
971 972
                    STREQ(caps->guests[i]->features[j]->name, "cpuselection") ||
                    STREQ(caps->guests[i]->features[j]->name, "deviceboot")) {
973
                    virBufferAsprintf(&buf, "<%s/>\n",
974
                                      caps->guests[i]->features[j]->name);
975
                } else {
976
                    virBufferAsprintf(&buf, "<%s default='%s' toggle='%s'/>\n",
977 978 979
                                      caps->guests[i]->features[j]->name,
                                      caps->guests[i]->features[j]->defaultOn ? "on" : "off",
                                      caps->guests[i]->features[j]->toggle ? "yes" : "no");
980 981 982
                }
            }

983 984
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</features>\n");
985
        }
986 987
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</guest>\n\n");
988
    }
989 990
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</capabilities>\n");
991

992 993
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
994
        return NULL;
995
    }
996

997
    return virBufferContentAndReset(&buf);
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

/* get the maximum ID of cpus in the host */
static unsigned int
virCapabilitiesGetHostMaxcpu(virCapsPtr caps)
{
    unsigned int maxcpu = 0;
    size_t node;
    size_t cpu;

    for (node = 0; node < caps->host.nnumaCell; node++) {
        virCapsHostNUMACellPtr cell = caps->host.numaCell[node];

        for (cpu = 0; cpu < cell->ncpus; cpu++) {
            if (cell->cpus[cpu].id > maxcpu)
                maxcpu = cell->cpus[cpu].id;
        }
    }

    return maxcpu;
}

/* set cpus of a numa node in the bitmask */
static int
virCapabilitiesGetCpusForNode(virCapsPtr caps,
                              size_t node,
                              virBitmapPtr cpumask)
{
1026
    virCapsHostNUMACellPtr cell = NULL;
1027
    size_t cpu;
1028
    size_t i;
1029
    /* The numa node numbers can be non-contiguous. Ex: 0,1,16,17. */
1030 1031 1032
    for (i = 0; i < caps->host.nnumaCell; i++) {
        if (caps->host.numaCell[i]->num == node) {
            cell = caps->host.numaCell[i];
1033 1034 1035
            break;
        }
    }
1036

1037
    for (cpu = 0; cell && cpu < cell->ncpus; cpu++) {
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
        if (virBitmapSetBit(cpumask, cell->cpus[cpu].id) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Cpu '%u' in node '%zu' is out of range "
                             "of the provided bitmap"),
                           cell->cpus[cpu].id, node);
            return -1;
        }
    }

    return 0;
}

virBitmapPtr
virCapabilitiesGetCpusForNodemask(virCapsPtr caps,
                                  virBitmapPtr nodemask)
{
    virBitmapPtr ret = NULL;
    unsigned int maxcpu = virCapabilitiesGetHostMaxcpu(caps);
    ssize_t node = -1;

    if (!(ret = virBitmapNew(maxcpu + 1)))
        return NULL;


    while ((node = virBitmapNextSetBit(nodemask, node)) >= 0) {
        if (virCapabilitiesGetCpusForNode(caps, node, ret) < 0) {
            virBitmapFree(ret);
            return NULL;
        }
    }

    return ret;
}