capabilities.c 38.2 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
#include "domain_conf.h"
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: true if offline migration is available
 * @liveMigrate: true if live migration is available
64
 *
65 66 67
 * Allocate a new capabilities object
 */
virCapsPtr
68
virCapabilitiesNew(virArch hostarch,
69 70
                   bool offlineMigrate,
                   bool liveMigrate)
71 72 73
{
    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
    VIR_FREE(cell->cpus);
111
    VIR_FREE(cell->siblings);
M
Michal Privoznik 已提交
112
    VIR_FREE(cell->pageinfo);
113
    VIR_FREE(cell);
114 115
}

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

126 127 128
static void
virCapabilitiesFreeGuestDomain(virCapsGuestDomainPtr dom)
{
129
    size_t i;
130 131 132
    if (dom == NULL)
        return;

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

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

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

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

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
    VIR_FREE(caps->host.secModels);

M
Michal Privoznik 已提交
224
    VIR_FREE(caps->host.pagesSize);
225
    virCPUDefFree(caps->host.cpu);
226 227 228 229 230 231 232 233 234 235 236 237 238
}

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

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

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

    return 0;
}


/**
 * virCapabilitiesAddHostNUMACell:
 * @caps: capabilities to extend
 * @num: ID number of NUMA cell
277
 * @mem: Total size of memory in the NUMA node (in KiB)
278
 * @ncpus: number of CPUs in cell
279
 * @cpus: array of CPU definition structures, the pointer is stolen
280 281
 * @nsiblings: number of sibling NUMA nodes
 * @siblings: info on sibling NUMA nodes
M
Michal Privoznik 已提交
282 283
 * @npageinfo: number of pages at node @num
 * @pageinfo: info on each single memory page
284 285 286 287 288 289 290
 *
 * 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,
291
                               unsigned long long mem,
292 293 294
                               int ncpus,
                               virCapsHostNUMACellCPUPtr cpus,
                               int nsiblings,
M
Michal Privoznik 已提交
295 296 297
                               virCapsHostNUMACellSiblingInfoPtr siblings,
                               int npageinfo,
                               virCapsHostNUMACellPageInfoPtr pageinfo)
298
{
299
    virCapsHostNUMACellPtr cell;
300

E
Eric Blake 已提交
301 302
    if (VIR_RESIZE_N(caps->host.numaCell, caps->host.nnumaCell_max,
                     caps->host.nnumaCell, 1) < 0)
303 304
        return -1;

305
    if (VIR_ALLOC(cell) < 0)
306 307
        return -1;

308
    cell->num = num;
309
    cell->mem = mem;
M
Michal Privoznik 已提交
310
    cell->ncpus = ncpus;
311
    cell->cpus = cpus;
312
    cell->nsiblings = nsiblings;
M
Michal Privoznik 已提交
313 314 315
    cell->siblings = siblings;
    cell->npageinfo = npageinfo;
    cell->pageinfo = pageinfo;
316

E
Eric Blake 已提交
317
    caps->host.numaCell[caps->host.nnumaCell++] = cell;
318 319 320 321

    return 0;
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

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


343 344 345 346 347 348 349 350 351 352 353 354
/**
 * 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;
355
    size_t i;
356 357 358 359 360 361

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

    for (i = 0; i < nnames; i++) {
        if (VIR_ALLOC(machines[i]) < 0 ||
362
            VIR_STRDUP(machines[i]->name, names[i]) < 0) {
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
            virCapabilitiesFreeMachines(machines, nnames);
            return NULL;
        }
    }

    return machines;
}

/**
 * virCapabilitiesFreeMachines:
 * @machines: table of vircapsGuestMachinePtr
 *
 * Free a table of virCapsGuestMachinePtr
 */
void
virCapabilitiesFreeMachines(virCapsGuestMachinePtr *machines,
                            int nmachines)
{
381
    size_t i;
382 383 384 385 386 387 388 389
    if (!machines)
        return;
    for (i = 0; i < nmachines && machines[i]; i++) {
        virCapabilitiesFreeGuestMachine(machines[i]);
        machines[i] = NULL;
    }
    VIR_FREE(machines);
}
390 391 392 393

/**
 * virCapabilitiesAddGuest:
 * @caps: capabilities to extend
394
 * @ostype: guest operating system type, of enum VIR_DOMAIN_OSTYPE
395
 * @arch: guest CPU architecture
396 397 398 399 400 401 402 403 404 405 406 407
 * @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,
408
                        int ostype,
409
                        virArch arch,
410 411 412
                        const char *emulator,
                        const char *loader,
                        int nmachines,
413
                        virCapsGuestMachinePtr *machines)
414
{
415
    virCapsGuestPtr guest;
416

417
    if (VIR_ALLOC(guest) < 0)
418
        goto error;
419

420
    guest->ostype = ostype;
421 422
    guest->arch.id = arch;
    guest->arch.wordsize = virArchGetWordSize(arch);
423

424 425 426
    if (VIR_STRDUP(guest->arch.defaultInfo.emulator, emulator) < 0 ||
        VIR_STRDUP(guest->arch.defaultInfo.loader, loader) < 0)
        goto error;
427

E
Eric Blake 已提交
428 429
    if (VIR_RESIZE_N(caps->guests, caps->nguests_max,
                     caps->nguests, 1) < 0)
430
        goto error;
E
Eric Blake 已提交
431
    caps->guests[caps->nguests++] = guest;
432

D
Daniel P. Berrange 已提交
433 434 435 436 437
    if (nmachines) {
        guest->arch.defaultInfo.nmachines = nmachines;
        guest->arch.defaultInfo.machines = machines;
    }

438 439
    return guest;

440
 error:
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    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,
460
                              int hvtype,
461 462 463
                              const char *emulator,
                              const char *loader,
                              int nmachines,
464
                              virCapsGuestMachinePtr *machines)
465
{
466
    virCapsGuestDomainPtr dom;
467

468
    if (VIR_ALLOC(dom) < 0)
469
        goto error;
470

471 472
    dom->type = hvtype;
    if (VIR_STRDUP(dom->info.emulator, emulator) < 0 ||
473 474
        VIR_STRDUP(dom->info.loader, loader) < 0)
        goto error;
475

E
Eric Blake 已提交
476 477
    if (VIR_RESIZE_N(guest->arch.domains, guest->arch.ndomains_max,
                     guest->arch.ndomains, 1) < 0)
478
        goto error;
479 480 481
    guest->arch.domains[guest->arch.ndomains] = dom;
    guest->arch.ndomains++;

D
Daniel P. Berrange 已提交
482 483 484 485
    if (nmachines) {
        dom->info.nmachines = nmachines;
        dom->info.machines = machines;
    }
486 487 488

    return dom;

489
 error:
490 491 492 493 494 495 496 497 498
    virCapabilitiesFreeGuestDomain(dom);
    return NULL;
}


/**
 * virCapabilitiesAddGuestFeature:
 * @guest: guest to associate feature with
 * @name: name of feature ('pae', 'acpi', 'apic')
499 500
 * @defaultOn: true if it defaults to on
 * @toggle: true if its state can be toggled
501
 *
502
 * Registers a feature for a guest domain.
503 504 505 506
 */
virCapsGuestFeaturePtr
virCapabilitiesAddGuestFeature(virCapsGuestPtr guest,
                               const char *name,
507 508
                               bool defaultOn,
                               bool toggle)
509
{
510
    virCapsGuestFeaturePtr feature;
511

512
    if (VIR_ALLOC(feature) < 0)
513 514
        goto no_memory;

515
    if (VIR_STRDUP(feature->name, name) < 0)
516 517 518 519
        goto no_memory;
    feature->defaultOn = defaultOn;
    feature->toggle = toggle;

E
Eric Blake 已提交
520 521
    if (VIR_RESIZE_N(guest->features, guest->nfeatures_max,
                     guest->nfeatures, 1) < 0)
522
        goto no_memory;
E
Eric Blake 已提交
523
    guest->features[guest->nfeatures++] = feature;
524 525 526 527 528 529 530 531

    return feature;

 no_memory:
    virCapabilitiesFreeGuestFeature(feature);
    return NULL;
}

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
/**
 * 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;

564
 no_memory:
565 566 567 568 569
    VIR_FREE(l);
    VIR_FREE(t);
    return -1;
}

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 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 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
static bool
virCapsDomainDataCompare(virCapsGuestPtr guest,
                         virCapsGuestDomainPtr domain,
                         virCapsGuestMachinePtr machine,
                         int ostype,
                         virArch arch,
                         int domaintype,
                         const char *emulator,
                         const char *machinetype)
{
    const char *check_emulator = NULL;

    if (ostype != -1 && guest->ostype != ostype)
        return false;
    if ((arch != VIR_ARCH_NONE) && (guest->arch.id != arch))
        return false;

    if (domaintype != -1 && (!domain || domain->type != domaintype))
        return false;

    if (emulator) {
        if (domain)
            check_emulator = domain->info.emulator;
        if (!check_emulator)
            check_emulator = guest->arch.defaultInfo.emulator;
        if (STRNEQ_NULLABLE(check_emulator, emulator))
            return false;
    }

    if (machinetype) {
        if (!machine)
            return false;
        if (STRNEQ(machine->name, machinetype) &&
            (STRNEQ_NULLABLE(machine->canonical, machinetype)))
            return false;
    }

    return true;
}

/**
 * virCapabilitiesDomainDataLookup:
 * @caps: capabilities to query
 * @ostype: guest operating system type, of enum VIR_DOMAIN_OSTYPE
 * @arch: Architecture to search for
 * @domaintype: domain type to search for, of enum VIR_DOMAIN_VIRT
 * @emulator: Emulator path to search for
 * @machinetype: Machine type to search for
 *
 * Search capabilities for the passed values, and if found return
 * virCapabilitiesDomainDataLookup filled in with the default values
 */
virCapsDomainDataPtr
virCapabilitiesDomainDataLookup(virCapsPtr caps,
                                int ostype,
                                virArch arch,
                                int domaintype,
                                const char *emulator,
                                const char *machinetype)
{
    virCapsGuestPtr foundguest = NULL;
    virCapsGuestDomainPtr founddomain = NULL;
    virCapsGuestMachinePtr foundmachine = NULL;
    virCapsDomainDataPtr ret = NULL;
    size_t i, j, k;

    for (i = 0; i < caps->nguests; i++) {
        virCapsGuestPtr guest = caps->guests[i];

        for (j = 0; j < guest->arch.ndomains; j++) {
            virCapsGuestDomainPtr domain = guest->arch.domains[j];
            virCapsGuestMachinePtr *machinelist;
            int nmachines;

            if (domain->info.nmachines) {
                nmachines = domain->info.nmachines;
                machinelist = domain->info.machines;
            } else {
                nmachines = guest->arch.defaultInfo.nmachines;
                machinelist = guest->arch.defaultInfo.machines;
            }

            for (k = 0; k < nmachines; k++) {
                virCapsGuestMachinePtr machine = machinelist[k];
                if (!virCapsDomainDataCompare(guest, domain, machine,
                                              ostype, arch, domaintype,
                                              emulator, machinetype))
                    continue;

                foundmachine = machine;
                break;
            }

            if (!foundmachine) {
                if (!virCapsDomainDataCompare(guest, domain, NULL,
                                              ostype, arch, domaintype,
                                              emulator, machinetype))
                    continue;
            }

            founddomain = domain;
            break;
        }

        if (!founddomain) {
            if (!virCapsDomainDataCompare(guest, NULL, NULL,
                                          ostype, arch, domaintype,
                                          emulator, machinetype))
                continue;
        }

        foundguest = guest;
        break;
    }

    /* XXX check default_emulator, see how it uses this */
    if (!foundguest) {
        virBuffer buf = VIR_BUFFER_INITIALIZER;
        if (ostype)
            virBufferAsprintf(&buf, "ostype=%s ",
                              virDomainOSTypeToString(ostype));
        if (arch)
            virBufferAsprintf(&buf, "arch=%s ", virArchToString(arch));
        if (domaintype)
            virBufferAsprintf(&buf, "domaintype=%s ",
                              virDomainVirtTypeToString(domaintype));
        if (emulator)
            virBufferAsprintf(&buf, "emulator=%s ", emulator);
        if (machinetype)
            virBufferAsprintf(&buf, "machine=%s ", machinetype);
        if (virBufferCurrentContent(&buf) &&
            !virBufferCurrentContent(&buf)[0])
            virBufferAsprintf(&buf, "%s", _("any configuration"));
        if (virBufferCheckError(&buf) < 0) {
            virBufferContentAndReset(&buf);
            goto error;
        }

        virReportError(VIR_ERR_INVALID_ARG,
                       _("could not find capabilities for %s"),
                       virBufferContentAndReset(&buf));
        goto error;
    }

    if (VIR_ALLOC(ret) < 0)
        goto error;

    ret->ostype = foundguest->ostype;
    ret->arch = foundguest->arch.id;
    if (founddomain) {
        ret->domaintype = founddomain->type;
        ret->emulator = founddomain->info.emulator;
    }
    if (!ret->emulator)
        ret->emulator = foundguest->arch.defaultInfo.emulator;
    if (foundmachine)
        ret->machinetype = foundmachine->name;

 error:
    return ret;
}

732 733 734
/**
 * virCapabilitiesSupportsGuestArch:
 * @caps: capabilities to query
735
 * @arch: Architecture to search for
736 737 738 739 740 741
 *
 * Returns non-zero if the capabilities support the
 * requested architecture
 */
extern int
virCapabilitiesSupportsGuestArch(virCapsPtr caps,
742
                                 virArch arch)
743
{
744
    size_t i;
745
    for (i = 0; i < caps->nguests; i++) {
746
        if (caps->guests[i]->arch.id == arch)
747 748 749 750 751
            return 1;
    }
    return 0;
}

752 753 754 755

/**
 * virCapabilitiesSupportsGuestOSType:
 * @caps: capabilities to query
756
 * @ostype: guest operating system type, of enum VIR_DOMAIN_OSTYPE
757 758 759 760 761 762
 *
 * Returns non-zero if the capabilities support the
 * requested operating system type
 */
extern int
virCapabilitiesSupportsGuestOSType(virCapsPtr caps,
763
                                   int ostype)
764
{
765
    size_t i;
766

767
    for (i = 0; i < caps->nguests; i++) {
768
        if (caps->guests[i]->ostype == ostype)
769 770 771 772 773 774
            return 1;
    }
    return 0;
}


775
/**
776
 * virCapabilitiesSupportsGuestOSTypeArch:
777
 * @caps: capabilities to query
778
 * @ostype: guest operating system type, of enum VIR_DOMAIN_OSTYPE
779
 * @arch: Architecture to search for
780 781 782 783 784
 *
 * Returns non-zero if the capabilities support the
 * requested operating system type
 */
extern int
785
virCapabilitiesSupportsGuestOSTypeArch(virCapsPtr caps,
786
                                       int ostype,
787
                                       virArch arch)
788
{
789
    size_t i;
790

791
    for (i = 0; i < caps->nguests; i++) {
792
        if (caps->guests[i]->ostype == ostype &&
793
            caps->guests[i]->arch.id == arch)
794 795 796 797 798 799
            return 1;
    }
    return 0;
}


800 801 802
/**
 * virCapabilitiesDefaultGuestArch:
 * @caps: capabilities to query
803
 * @ostype: guest operating system type, of enum VIR_DOMAIN_OSTYPE
804
 * @domain: domain type to search for, of enum VIR_DOMAIN_VIRT
805 806 807 808
 *
 * Returns the first architecture able to run the
 * requested operating system type
 */
809
extern virArch
810
virCapabilitiesDefaultGuestArch(virCapsPtr caps,
811
                                int ostype,
812
                                int domain)
813
{
814
    size_t i, j;
815 816

    /* First try to find one matching host arch */
817
    for (i = 0; i < caps->nguests; i++) {
818
        if (caps->guests[i]->ostype == ostype) {
819
            for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
820
                if (caps->guests[i]->arch.domains[j]->type == domain &&
821 822
                    caps->guests[i]->arch.id == caps->host.arch)
                    return caps->guests[i]->arch.id;
823 824
            }
        }
825
    }
826 827

    /* Otherwise find the first match */
828
    for (i = 0; i < caps->nguests; i++) {
829
        if (caps->guests[i]->ostype == ostype) {
830
            for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
831
                if (caps->guests[i]->arch.domains[j]->type == domain)
832 833 834 835 836 837
                    return caps->guests[i]->arch.id;
            }
        }
    }

    return VIR_ARCH_NONE;
838 839 840 841 842
}

/**
 * virCapabilitiesDefaultGuestMachine:
 * @caps: capabilities to query
843
 * @ostype: guest operating system type, of enum VIR_DOMAIN_OSTYPE
844
 * @arch: architecture to search for
845
 * @domain: domain type to search for, of enum VIR_DOMAIN_VIRT
846 847
 *
 * Returns the first machine variant associated with
848 849
 * the requested operating system type, architecture
 * and domain type
850 851 852
 */
extern const char *
virCapabilitiesDefaultGuestMachine(virCapsPtr caps,
853
                                   int ostype,
854
                                   virArch arch,
855
                                   int domain)
856
{
857
    size_t i;
858

859
    for (i = 0; i < caps->nguests; i++) {
860
        virCapsGuestPtr guest = caps->guests[i];
861
        size_t j;
862

863
        if (guest->ostype != ostype ||
864
            guest->arch.id != arch)
865 866 867
            continue;

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

870
            if (dom->type != domain)
871 872 873 874 875 876 877 878 879
                continue;

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

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

        if (guest->arch.defaultInfo.nmachines)
880
            return caps->guests[i]->arch.defaultInfo.machines[0]->name;
881
    }
882

883 884 885 886
    return NULL;
}

/**
887
 * virCapabilitiesDefaultGuestEmulator:
888
 * @caps: capabilities to query
889
 * @ostype: guest operating system type, of enum VIR_DOMAIN_OSTYPE
890
 * @arch: architecture to search for
891
 * @domain: domain type to search for, of enum VIR_DOMAIN_VIRT
892 893 894 895 896 897 898
 *
 * Returns the first emulator path associated with
 * the requested operating system type, architecture
 * and domain type
 */
extern const char *
virCapabilitiesDefaultGuestEmulator(virCapsPtr caps,
899
                                    int ostype,
900
                                    virArch arch,
901
                                    int domain)
902
{
903
    size_t i, j;
904

905
    for (i = 0; i < caps->nguests; i++) {
906
        char *emulator;
907
        if (caps->guests[i]->ostype == ostype &&
908
            caps->guests[i]->arch.id == arch) {
909
            emulator = caps->guests[i]->arch.defaultInfo.emulator;
910
            for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
911
                if (caps->guests[i]->arch.domains[j]->type == domain) {
912 913 914 915 916 917 918 919 920 921
                    if (caps->guests[i]->arch.domains[j]->info.emulator)
                        emulator = caps->guests[i]->arch.domains[j]->info.emulator;
                }
            }
            return emulator;
        }
    }
    return NULL;
}

922
static int
923
virCapabilitiesFormatNUMATopology(virBufferPtr buf,
924 925 926
                                  size_t ncells,
                                  virCapsHostNUMACellPtr *cells)
{
927 928
    size_t i;
    size_t j;
929
    char *siblings;
930

931 932 933 934
    virBufferAddLit(buf, "<topology>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferAsprintf(buf, "<cells num='%zu'>\n", ncells);
    virBufferAdjustIndent(buf, 2);
935
    for (i = 0; i < ncells; i++) {
936 937
        virBufferAsprintf(buf, "<cell id='%d'>\n", cells[i]->num);
        virBufferAdjustIndent(buf, 2);
938 939 940

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

M
Michal Privoznik 已提交
944 945 946 947 948 949
        for (j = 0; j < cells[i]->npageinfo; j++) {
            virBufferAsprintf(buf, "<pages unit='KiB' size='%u'>%zu</pages>\n",
                              cells[i]->pageinfo[j].size,
                              cells[i]->pageinfo[j].avail);
        }

950 951 952 953 954 955 956 957 958 959 960 961
        if (cells[i]->nsiblings) {
            virBufferAddLit(buf, "<distances>\n");
            virBufferAdjustIndent(buf, 2);
            for (j = 0; j < cells[i]->nsiblings; j++) {
                virBufferAsprintf(buf, "<sibling id='%d' value='%d'/>\n",
                                  cells[i]->siblings[j].node,
                                  cells[i]->siblings[j].distance);
            }
            virBufferAdjustIndent(buf, -2);
            virBufferAddLit(buf, "</distances>\n");
        }

962 963
        virBufferAsprintf(buf, "<cpus num='%d'>\n", cells[i]->ncpus);
        virBufferAdjustIndent(buf, 2);
964
        for (j = 0; j < cells[i]->ncpus; j++) {
965
            virBufferAsprintf(buf, "<cpu id='%d'", cells[i]->cpus[j].id);
966 967

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

971
                virBufferAsprintf(buf,
972 973 974 975 976 977
                                  " socket_id='%d' core_id='%d' siblings='%s'",
                                  cells[i]->cpus[j].socket_id,
                                  cells[i]->cpus[j].core_id,
                                  siblings);
                VIR_FREE(siblings);
            }
978
            virBufferAddLit(buf, "/>\n");
979
        }
980 981 982 983
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</cpus>\n");
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</cell>\n");
984
    }
985 986 987 988
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</cells>\n");
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</topology>\n");
989
    return 0;
990
}
991 992 993 994 995 996 997 998 999 1000 1001 1002

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

1007 1008 1009 1010
    virBufferAddLit(&buf, "<capabilities>\n\n");
    virBufferAdjustIndent(&buf, 2);
    virBufferAddLit(&buf, "<host>\n");
    virBufferAdjustIndent(&buf, 2);
1011 1012
    if (virUUIDIsValid(caps->host.host_uuid)) {
        virUUIDFormat(caps->host.host_uuid, host_uuid);
1013
        virBufferAsprintf(&buf, "<uuid>%s</uuid>\n", host_uuid);
1014
    }
1015 1016 1017
    virBufferAddLit(&buf, "<cpu>\n");
    virBufferAdjustIndent(&buf, 2);

1018
    if (caps->host.arch)
1019
        virBufferAsprintf(&buf, "<arch>%s</arch>\n",
1020
                          virArchToString(caps->host.arch));
1021
    if (caps->host.nfeatures) {
1022 1023
        virBufferAddLit(&buf, "<features>\n");
        virBufferAdjustIndent(&buf, 2);
1024
        for (i = 0; i < caps->host.nfeatures; i++) {
1025
            virBufferAsprintf(&buf, "<%s/>\n",
1026
                              caps->host.features[i]);
1027
        }
1028 1029
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</features>\n");
1030
    }
1031
    virCPUDefFormatBuf(&buf, caps->host.cpu, false);
1032

M
Michal Privoznik 已提交
1033 1034 1035 1036 1037
    for (i = 0; i < caps->host.nPagesSize; i++) {
        virBufferAsprintf(&buf, "<pages unit='KiB' size='%u'/>\n",
                          caps->host.pagesSize[i]);
    }

1038 1039
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</cpu>\n");
1040

1041 1042 1043 1044
    /* The PM query was successful. */
    if (caps->host.powerMgmt) {
        /* The host supports some PM features. */
        unsigned int pm = caps->host.powerMgmt;
1045 1046
        virBufferAddLit(&buf, "<power_management>\n");
        virBufferAdjustIndent(&buf, 2);
1047 1048
        while (pm) {
            int bit = ffs(pm) - 1;
1049
            virBufferAsprintf(&buf, "<%s/>\n",
1050 1051
                              virCapsHostPMTargetTypeToString(bit));
            pm &= ~(1U << bit);
1052
        }
1053 1054
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</power_management>\n");
1055 1056
    } else {
        /* The host does not support any PM feature. */
1057
        virBufferAddLit(&buf, "<power_management/>\n");
1058 1059
    }

1060
    if (caps->host.offlineMigrate) {
1061 1062
        virBufferAddLit(&buf, "<migration_features>\n");
        virBufferAdjustIndent(&buf, 2);
1063
        if (caps->host.liveMigrate)
1064
            virBufferAddLit(&buf, "<live/>\n");
1065
        if (caps->host.nmigrateTrans) {
1066 1067
            virBufferAddLit(&buf, "<uri_transports>\n");
            virBufferAdjustIndent(&buf, 2);
1068
            for (i = 0; i < caps->host.nmigrateTrans; i++) {
1069
                virBufferAsprintf(&buf, "<uri_transport>%s</uri_transport>\n",
1070
                                      caps->host.migrateTrans[i]);
1071
            }
1072 1073
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</uri_transports>\n");
1074
        }
1075 1076
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</migration_features>\n");
1077 1078
    }

1079
    if (caps->host.nnumaCell &&
1080
        virCapabilitiesFormatNUMATopology(&buf, caps->host.nnumaCell,
1081 1082
                                          caps->host.numaCell) < 0)
        return NULL;
1083

1084
    for (i = 0; i < caps->host.nsecModels; i++) {
1085 1086 1087
        virBufferAddLit(&buf, "<secmodel>\n");
        virBufferAdjustIndent(&buf, 2);
        virBufferAsprintf(&buf, "<model>%s</model>\n",
1088
                          caps->host.secModels[i].model);
1089
        virBufferAsprintf(&buf, "<doi>%s</doi>\n",
1090
                          caps->host.secModels[i].doi);
1091
        for (j = 0; j < caps->host.secModels[i].nlabels; j++) {
1092
            virBufferAsprintf(&buf, "<baselabel type='%s'>%s</baselabel>\n",
1093 1094 1095
                              caps->host.secModels[i].labels[j].type,
                              caps->host.secModels[i].labels[j].label);
        }
1096 1097
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</secmodel>\n");
1098 1099
    }

1100 1101
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</host>\n\n");
1102 1103


1104
    for (i = 0; i < caps->nguests; i++) {
1105 1106 1107
        virBufferAddLit(&buf, "<guest>\n");
        virBufferAdjustIndent(&buf, 2);
        virBufferAsprintf(&buf, "<os_type>%s</os_type>\n",
1108
                          virDomainOSTypeToString(caps->guests[i]->ostype));
1109
        if (caps->guests[i]->arch.id)
1110
            virBufferAsprintf(&buf, "<arch name='%s'>\n",
1111
                              virArchToString(caps->guests[i]->arch.id));
1112 1113
        virBufferAdjustIndent(&buf, 2);
        virBufferAsprintf(&buf, "<wordsize>%d</wordsize>\n",
1114 1115
                          caps->guests[i]->arch.wordsize);
        if (caps->guests[i]->arch.defaultInfo.emulator)
1116
            virBufferAsprintf(&buf, "<emulator>%s</emulator>\n",
1117 1118
                              caps->guests[i]->arch.defaultInfo.emulator);
            if (caps->guests[i]->arch.defaultInfo.loader)
1119
                virBufferAsprintf(&buf, "<loader>%s</loader>\n",
1120
                                  caps->guests[i]->arch.defaultInfo.loader);
1121

1122
        for (j = 0; j < caps->guests[i]->arch.defaultInfo.nmachines; j++) {
1123
            virCapsGuestMachinePtr machine = caps->guests[i]->arch.defaultInfo.machines[j];
1124
            virBufferAddLit(&buf, "<machine");
1125
            if (machine->canonical)
1126
                virBufferAsprintf(&buf, " canonical='%s'", machine->canonical);
1127
            if (machine->maxCpus > 0)
1128 1129
                virBufferAsprintf(&buf, " maxCpus='%d'", machine->maxCpus);
            virBufferAsprintf(&buf, ">%s</machine>\n", machine->name);
1130 1131
        }

1132
        for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
1133
            virBufferAsprintf(&buf, "<domain type='%s'",
1134
                virDomainVirtTypeToString(caps->guests[i]->arch.domains[j]->type));
1135 1136 1137 1138 1139 1140 1141
            if (!caps->guests[i]->arch.domains[j]->info.emulator &&
                !caps->guests[i]->arch.domains[j]->info.loader &&
                !caps->guests[i]->arch.domains[j]->info.nmachines) {
                virBufferAddLit(&buf, "/>\n");
                continue;
            }
            virBufferAddLit(&buf, ">\n");
1142
            virBufferAdjustIndent(&buf, 2);
1143
            if (caps->guests[i]->arch.domains[j]->info.emulator)
1144
                virBufferAsprintf(&buf, "<emulator>%s</emulator>\n",
1145 1146
                                  caps->guests[i]->arch.domains[j]->info.emulator);
            if (caps->guests[i]->arch.domains[j]->info.loader)
1147
                virBufferAsprintf(&buf, "<loader>%s</loader>\n",
1148
                                  caps->guests[i]->arch.domains[j]->info.loader);
1149

1150
            for (k = 0; k < caps->guests[i]->arch.domains[j]->info.nmachines; k++) {
1151
                virCapsGuestMachinePtr machine = caps->guests[i]->arch.domains[j]->info.machines[k];
1152
                virBufferAddLit(&buf, "<machine");
1153
                if (machine->canonical)
1154
                    virBufferAsprintf(&buf, " canonical='%s'", machine->canonical);
1155
                if (machine->maxCpus > 0)
1156 1157
                    virBufferAsprintf(&buf, " maxCpus='%d'", machine->maxCpus);
                virBufferAsprintf(&buf, ">%s</machine>\n", machine->name);
1158
            }
1159 1160
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</domain>\n");
1161 1162
        }

1163 1164
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</arch>\n");
1165 1166

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

1170
            for (j = 0; j < caps->guests[i]->nfeatures; j++) {
1171 1172
                if (STREQ(caps->guests[i]->features[j]->name, "pae") ||
                    STREQ(caps->guests[i]->features[j]->name, "nonpae") ||
1173
                    STREQ(caps->guests[i]->features[j]->name, "ia64_be") ||
1174 1175
                    STREQ(caps->guests[i]->features[j]->name, "cpuselection") ||
                    STREQ(caps->guests[i]->features[j]->name, "deviceboot")) {
1176
                    virBufferAsprintf(&buf, "<%s/>\n",
1177
                                      caps->guests[i]->features[j]->name);
1178
                } else {
1179
                    virBufferAsprintf(&buf, "<%s default='%s' toggle='%s'/>\n",
1180 1181 1182
                                      caps->guests[i]->features[j]->name,
                                      caps->guests[i]->features[j]->defaultOn ? "on" : "off",
                                      caps->guests[i]->features[j]->toggle ? "yes" : "no");
1183 1184 1185
                }
            }

1186 1187
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</features>\n");
1188
        }
1189 1190
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</guest>\n\n");
1191
    }
1192 1193
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</capabilities>\n");
1194

1195
    if (virBufferCheckError(&buf) < 0)
1196
        return NULL;
1197

1198
    return virBufferContentAndReset(&buf);
1199
}
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226

/* 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)
{
1227
    virCapsHostNUMACellPtr cell = NULL;
1228
    size_t cpu;
1229
    size_t i;
1230
    /* The numa node numbers can be non-contiguous. Ex: 0,1,16,17. */
1231 1232 1233
    for (i = 0; i < caps->host.nnumaCell; i++) {
        if (caps->host.numaCell[i]->num == node) {
            cell = caps->host.numaCell[i];
1234 1235 1236
            break;
        }
    }
1237

1238
    for (cpu = 0; cell && cpu < cell->ncpus; cpu++) {
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
        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;
}