capabilities.c 34.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
#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 138
    VIR_FREE(dom->info.machines);
    VIR_FREE(dom->type);
139

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

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

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

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

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

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

173
    VIR_FREE(guest);
174 175
}

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

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

187 188 189 190 191 192 193 194 195 196 197 198 199 200
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);
}

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

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

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

    virCapabilitiesFreeNUMAInfo(caps);
216

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

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

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


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

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

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

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

307
    if (VIR_ALLOC(cell) < 0)
308 309
        return -1;

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

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

    return 0;
}

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

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


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

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

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

    return machines;
}

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

/**
 * virCapabilitiesAddGuest:
 * @caps: capabilities to extend
 * @ostype: guest operating system type ('hvm' or 'xen')
397
 * @arch: guest CPU architecture
398 399 400 401 402 403 404 405 406 407 408 409
 * @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,
410
                        const char *ostypestr,
411
                        virArch arch,
412 413 414
                        const char *emulator,
                        const char *loader,
                        int nmachines,
415
                        virCapsGuestMachinePtr *machines)
416
{
417
    virCapsGuestPtr guest;
418
    int ostype;
419

420
    if (VIR_ALLOC(guest) < 0)
421
        goto error;
422

423 424 425
    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unknown OS type '%s'"), ostypestr);
426
        goto error;
427
    }
428

429
    guest->ostype = ostype;
430 431
    guest->arch.id = arch;
    guest->arch.wordsize = virArchGetWordSize(arch);
432

433 434 435
    if (VIR_STRDUP(guest->arch.defaultInfo.emulator, emulator) < 0 ||
        VIR_STRDUP(guest->arch.defaultInfo.loader, loader) < 0)
        goto error;
436

E
Eric Blake 已提交
437 438
    if (VIR_RESIZE_N(caps->guests, caps->nguests_max,
                     caps->nguests, 1) < 0)
439
        goto error;
E
Eric Blake 已提交
440
    caps->guests[caps->nguests++] = guest;
441

D
Daniel P. Berrange 已提交
442 443 444 445 446
    if (nmachines) {
        guest->arch.defaultInfo.nmachines = nmachines;
        guest->arch.defaultInfo.machines = machines;
    }

447 448
    return guest;

449
 error:
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    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,
473
                              virCapsGuestMachinePtr *machines)
474
{
475
    virCapsGuestDomainPtr dom;
476

477
    if (VIR_ALLOC(dom) < 0)
478
        goto error;
479

480 481 482 483
    if (VIR_STRDUP(dom->type, hvtype) < 0 ||
        VIR_STRDUP(dom->info.emulator, emulator) < 0 ||
        VIR_STRDUP(dom->info.loader, loader) < 0)
        goto error;
484

E
Eric Blake 已提交
485 486
    if (VIR_RESIZE_N(guest->arch.domains, guest->arch.ndomains_max,
                     guest->arch.ndomains, 1) < 0)
487
        goto error;
488 489 490
    guest->arch.domains[guest->arch.ndomains] = dom;
    guest->arch.ndomains++;

D
Daniel P. Berrange 已提交
491 492 493 494
    if (nmachines) {
        dom->info.nmachines = nmachines;
        dom->info.machines = machines;
    }
495 496 497

    return dom;

498
 error:
499 500 501 502 503 504 505 506 507
    virCapabilitiesFreeGuestDomain(dom);
    return NULL;
}


/**
 * virCapabilitiesAddGuestFeature:
 * @guest: guest to associate feature with
 * @name: name of feature ('pae', 'acpi', 'apic')
508 509
 * @defaultOn: true if it defaults to on
 * @toggle: true if its state can be toggled
510
 *
511
 * Registers a feature for a guest domain.
512 513 514 515
 */
virCapsGuestFeaturePtr
virCapabilitiesAddGuestFeature(virCapsGuestPtr guest,
                               const char *name,
516 517
                               bool defaultOn,
                               bool toggle)
518
{
519
    virCapsGuestFeaturePtr feature;
520

521
    if (VIR_ALLOC(feature) < 0)
522 523
        goto no_memory;

524
    if (VIR_STRDUP(feature->name, name) < 0)
525 526 527 528
        goto no_memory;
    feature->defaultOn = defaultOn;
    feature->toggle = toggle;

E
Eric Blake 已提交
529 530
    if (VIR_RESIZE_N(guest->features, guest->nfeatures_max,
                     guest->nfeatures, 1) < 0)
531
        goto no_memory;
E
Eric Blake 已提交
532
    guest->features[guest->nfeatures++] = feature;
533 534 535 536 537 538 539 540

    return feature;

 no_memory:
    virCapabilitiesFreeGuestFeature(feature);
    return NULL;
}

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
/**
 * 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;

573
 no_memory:
574 575 576 577 578
    VIR_FREE(l);
    VIR_FREE(t);
    return -1;
}

579 580 581
/**
 * virCapabilitiesSupportsGuestArch:
 * @caps: capabilities to query
582
 * @arch: Architecture to search for
583 584 585 586 587 588
 *
 * Returns non-zero if the capabilities support the
 * requested architecture
 */
extern int
virCapabilitiesSupportsGuestArch(virCapsPtr caps,
589
                                 virArch arch)
590
{
591
    size_t i;
592
    for (i = 0; i < caps->nguests; i++) {
593
        if (caps->guests[i]->arch.id == arch)
594 595 596 597 598
            return 1;
    }
    return 0;
}

599 600 601 602 603 604 605 606 607 608 609

/**
 * 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,
610
                                   const char *ostypestr)
611
{
612
    size_t i;
613 614 615 616 617 618 619 620
    int ostype;

    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unknown OS type '%s'"), ostypestr);
        return 0;
    }

621
    for (i = 0; i < caps->nguests; i++) {
622
        if (caps->guests[i]->ostype == ostype)
623 624 625 626 627 628
            return 1;
    }
    return 0;
}


629
/**
630
 * virCapabilitiesSupportsGuestOSTypeArch:
631 632
 * @caps: capabilities to query
 * @ostype: OS type to search for (eg 'hvm', 'xen')
633
 * @arch: Architecture to search for
634 635 636 637 638
 *
 * Returns non-zero if the capabilities support the
 * requested operating system type
 */
extern int
639
virCapabilitiesSupportsGuestOSTypeArch(virCapsPtr caps,
640
                                       const char *ostypestr,
641
                                       virArch arch)
642
{
643
    size_t i;
644 645 646 647 648 649 650 651
    int ostype;

    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unknown OS type '%s'"), ostypestr);
        return 0;
    }

652
    for (i = 0; i < caps->nguests; i++) {
653
        if (caps->guests[i]->ostype == ostype &&
654
            caps->guests[i]->arch.id == arch)
655 656 657 658 659 660
            return 1;
    }
    return 0;
}


661 662 663 664 665 666 667 668
/**
 * virCapabilitiesDefaultGuestArch:
 * @caps: capabilities to query
 * @ostype: OS type to search for
 *
 * Returns the first architecture able to run the
 * requested operating system type
 */
669
extern virArch
670
virCapabilitiesDefaultGuestArch(virCapsPtr caps,
671
                                const char *ostypestr,
672
                                const char *domain)
673
{
674
    size_t i, j;
675 676 677 678 679 680 681
    int ostype;

    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unknown OS type '%s'"), ostypestr);
        return VIR_ARCH_NONE;
    }
682 683

    /* First try to find one matching host arch */
684
    for (i = 0; i < caps->nguests; i++) {
685
        if (caps->guests[i]->ostype == ostype) {
686
            for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
687 688 689
                if (STREQ(caps->guests[i]->arch.domains[j]->type, domain) &&
                    caps->guests[i]->arch.id == caps->host.arch)
                    return caps->guests[i]->arch.id;
690 691
            }
        }
692
    }
693 694

    /* Otherwise find the first match */
695
    for (i = 0; i < caps->nguests; i++) {
696
        if (caps->guests[i]->ostype == ostype) {
697
            for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
698 699 700 701 702 703 704
                if (STREQ(caps->guests[i]->arch.domains[j]->type, domain))
                    return caps->guests[i]->arch.id;
            }
        }
    }

    return VIR_ARCH_NONE;
705 706 707 708 709 710 711
}

/**
 * virCapabilitiesDefaultGuestMachine:
 * @caps: capabilities to query
 * @ostype: OS type to search for
 * @arch: architecture to search for
712
 * @domain: domain type to search for
713 714
 *
 * Returns the first machine variant associated with
715 716
 * the requested operating system type, architecture
 * and domain type
717 718 719
 */
extern const char *
virCapabilitiesDefaultGuestMachine(virCapsPtr caps,
720
                                   const char *ostypestr,
721
                                   virArch arch,
722
                                   const char *domain)
723
{
724
    size_t i;
725 726 727 728 729 730 731
    int ostype;

    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unknown OS type '%s'"), ostypestr);
        return NULL;
    }
732

733
    for (i = 0; i < caps->nguests; i++) {
734
        virCapsGuestPtr guest = caps->guests[i];
735
        size_t j;
736

737
        if (guest->ostype != ostype ||
738
            guest->arch.id != arch)
739 740 741
            continue;

        for (j = 0; j < guest->arch.ndomains; j++) {
742
            virCapsGuestDomainPtr dom = guest->arch.domains[j];
743 744 745 746 747 748 749 750 751 752 753

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

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

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

        if (guest->arch.defaultInfo.nmachines)
754
            return caps->guests[i]->arch.defaultInfo.machines[0]->name;
755
    }
756

757 758 759 760
    return NULL;
}

/**
761
 * virCapabilitiesDefaultGuestEmulator:
762 763 764 765 766 767 768 769 770 771 772
 * @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,
773
                                    const char *ostypestr,
774
                                    virArch arch,
775 776
                                    const char *domain)
{
777
    size_t i, j;
778 779 780 781 782 783 784 785
    int ostype;

    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unknown OS type '%s'"), ostypestr);
        return NULL;
    }

786
    for (i = 0; i < caps->nguests; i++) {
787
        char *emulator;
788
        if (caps->guests[i]->ostype == ostype &&
789
            caps->guests[i]->arch.id == arch) {
790
            emulator = caps->guests[i]->arch.defaultInfo.emulator;
791
            for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
792 793 794 795 796 797 798 799 800 801 802
                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;
}

803
static int
804
virCapabilitiesFormatNUMATopology(virBufferPtr buf,
805 806 807
                                  size_t ncells,
                                  virCapsHostNUMACellPtr *cells)
{
808 809
    size_t i;
    size_t j;
810
    char *siblings;
811

812 813 814 815
    virBufferAddLit(buf, "<topology>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferAsprintf(buf, "<cells num='%zu'>\n", ncells);
    virBufferAdjustIndent(buf, 2);
816
    for (i = 0; i < ncells; i++) {
817 818
        virBufferAsprintf(buf, "<cell id='%d'>\n", cells[i]->num);
        virBufferAdjustIndent(buf, 2);
819 820 821

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

M
Michal Privoznik 已提交
825 826 827 828 829 830
        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);
        }

831 832 833 834 835 836 837 838 839 840 841 842
        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");
        }

843 844
        virBufferAsprintf(buf, "<cpus num='%d'>\n", cells[i]->ncpus);
        virBufferAdjustIndent(buf, 2);
845
        for (j = 0; j < cells[i]->ncpus; j++) {
846
            virBufferAsprintf(buf, "<cpu id='%d'", cells[i]->cpus[j].id);
847 848

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

852
                virBufferAsprintf(buf,
853 854 855 856 857 858
                                  " socket_id='%d' core_id='%d' siblings='%s'",
                                  cells[i]->cpus[j].socket_id,
                                  cells[i]->cpus[j].core_id,
                                  siblings);
                VIR_FREE(siblings);
            }
859
            virBufferAddLit(buf, "/>\n");
860
        }
861 862 863 864
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</cpus>\n");
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</cell>\n");
865
    }
866 867 868 869
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</cells>\n");
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</topology>\n");
870
    return 0;
871
}
872 873 874 875 876 877 878 879 880 881 882 883

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

888 889 890 891
    virBufferAddLit(&buf, "<capabilities>\n\n");
    virBufferAdjustIndent(&buf, 2);
    virBufferAddLit(&buf, "<host>\n");
    virBufferAdjustIndent(&buf, 2);
892 893
    if (virUUIDIsValid(caps->host.host_uuid)) {
        virUUIDFormat(caps->host.host_uuid, host_uuid);
894
        virBufferAsprintf(&buf, "<uuid>%s</uuid>\n", host_uuid);
895
    }
896 897 898
    virBufferAddLit(&buf, "<cpu>\n");
    virBufferAdjustIndent(&buf, 2);

899
    if (caps->host.arch)
900
        virBufferAsprintf(&buf, "<arch>%s</arch>\n",
901
                          virArchToString(caps->host.arch));
902
    if (caps->host.nfeatures) {
903 904
        virBufferAddLit(&buf, "<features>\n");
        virBufferAdjustIndent(&buf, 2);
905
        for (i = 0; i < caps->host.nfeatures; i++) {
906
            virBufferAsprintf(&buf, "<%s/>\n",
907
                              caps->host.features[i]);
908
        }
909 910
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</features>\n");
911
    }
912
    virCPUDefFormatBuf(&buf, caps->host.cpu, false);
913

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

919 920
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</cpu>\n");
921

922 923 924 925
    /* The PM query was successful. */
    if (caps->host.powerMgmt) {
        /* The host supports some PM features. */
        unsigned int pm = caps->host.powerMgmt;
926 927
        virBufferAddLit(&buf, "<power_management>\n");
        virBufferAdjustIndent(&buf, 2);
928 929
        while (pm) {
            int bit = ffs(pm) - 1;
930
            virBufferAsprintf(&buf, "<%s/>\n",
931 932
                              virCapsHostPMTargetTypeToString(bit));
            pm &= ~(1U << bit);
933
        }
934 935
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</power_management>\n");
936 937
    } else {
        /* The host does not support any PM feature. */
938
        virBufferAddLit(&buf, "<power_management/>\n");
939 940
    }

941
    if (caps->host.offlineMigrate) {
942 943
        virBufferAddLit(&buf, "<migration_features>\n");
        virBufferAdjustIndent(&buf, 2);
944
        if (caps->host.liveMigrate)
945
            virBufferAddLit(&buf, "<live/>\n");
946
        if (caps->host.nmigrateTrans) {
947 948
            virBufferAddLit(&buf, "<uri_transports>\n");
            virBufferAdjustIndent(&buf, 2);
949
            for (i = 0; i < caps->host.nmigrateTrans; i++) {
950
                virBufferAsprintf(&buf, "<uri_transport>%s</uri_transport>\n",
951
                                      caps->host.migrateTrans[i]);
952
            }
953 954
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</uri_transports>\n");
955
        }
956 957
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</migration_features>\n");
958 959
    }

960
    if (caps->host.nnumaCell &&
961
        virCapabilitiesFormatNUMATopology(&buf, caps->host.nnumaCell,
962 963
                                          caps->host.numaCell) < 0)
        return NULL;
964

965
    for (i = 0; i < caps->host.nsecModels; i++) {
966 967 968
        virBufferAddLit(&buf, "<secmodel>\n");
        virBufferAdjustIndent(&buf, 2);
        virBufferAsprintf(&buf, "<model>%s</model>\n",
969
                          caps->host.secModels[i].model);
970
        virBufferAsprintf(&buf, "<doi>%s</doi>\n",
971
                          caps->host.secModels[i].doi);
972
        for (j = 0; j < caps->host.secModels[i].nlabels; j++) {
973
            virBufferAsprintf(&buf, "<baselabel type='%s'>%s</baselabel>\n",
974 975 976
                              caps->host.secModels[i].labels[j].type,
                              caps->host.secModels[i].labels[j].label);
        }
977 978
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</secmodel>\n");
979 980
    }

981 982
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</host>\n\n");
983 984


985
    for (i = 0; i < caps->nguests; i++) {
986 987 988
        virBufferAddLit(&buf, "<guest>\n");
        virBufferAdjustIndent(&buf, 2);
        virBufferAsprintf(&buf, "<os_type>%s</os_type>\n",
989
                          virDomainOSTypeToString(caps->guests[i]->ostype));
990
        if (caps->guests[i]->arch.id)
991
            virBufferAsprintf(&buf, "<arch name='%s'>\n",
992
                              virArchToString(caps->guests[i]->arch.id));
993 994
        virBufferAdjustIndent(&buf, 2);
        virBufferAsprintf(&buf, "<wordsize>%d</wordsize>\n",
995 996
                          caps->guests[i]->arch.wordsize);
        if (caps->guests[i]->arch.defaultInfo.emulator)
997
            virBufferAsprintf(&buf, "<emulator>%s</emulator>\n",
998 999
                              caps->guests[i]->arch.defaultInfo.emulator);
            if (caps->guests[i]->arch.defaultInfo.loader)
1000
                virBufferAsprintf(&buf, "<loader>%s</loader>\n",
1001
                                  caps->guests[i]->arch.defaultInfo.loader);
1002

1003
        for (j = 0; j < caps->guests[i]->arch.defaultInfo.nmachines; j++) {
1004
            virCapsGuestMachinePtr machine = caps->guests[i]->arch.defaultInfo.machines[j];
1005
            virBufferAddLit(&buf, "<machine");
1006
            if (machine->canonical)
1007
                virBufferAsprintf(&buf, " canonical='%s'", machine->canonical);
1008
            if (machine->maxCpus > 0)
1009 1010
                virBufferAsprintf(&buf, " maxCpus='%d'", machine->maxCpus);
            virBufferAsprintf(&buf, ">%s</machine>\n", machine->name);
1011 1012
        }

1013
        for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
1014
            virBufferAsprintf(&buf, "<domain type='%s'",
1015
                                  caps->guests[i]->arch.domains[j]->type);
1016 1017 1018 1019 1020 1021 1022
            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");
1023
            virBufferAdjustIndent(&buf, 2);
1024
            if (caps->guests[i]->arch.domains[j]->info.emulator)
1025
                virBufferAsprintf(&buf, "<emulator>%s</emulator>\n",
1026 1027
                                  caps->guests[i]->arch.domains[j]->info.emulator);
            if (caps->guests[i]->arch.domains[j]->info.loader)
1028
                virBufferAsprintf(&buf, "<loader>%s</loader>\n",
1029
                                  caps->guests[i]->arch.domains[j]->info.loader);
1030

1031
            for (k = 0; k < caps->guests[i]->arch.domains[j]->info.nmachines; k++) {
1032
                virCapsGuestMachinePtr machine = caps->guests[i]->arch.domains[j]->info.machines[k];
1033
                virBufferAddLit(&buf, "<machine");
1034
                if (machine->canonical)
1035
                    virBufferAsprintf(&buf, " canonical='%s'", machine->canonical);
1036
                if (machine->maxCpus > 0)
1037 1038
                    virBufferAsprintf(&buf, " maxCpus='%d'", machine->maxCpus);
                virBufferAsprintf(&buf, ">%s</machine>\n", machine->name);
1039
            }
1040 1041
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</domain>\n");
1042 1043
        }

1044 1045
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</arch>\n");
1046 1047

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

1051
            for (j = 0; j < caps->guests[i]->nfeatures; j++) {
1052 1053
                if (STREQ(caps->guests[i]->features[j]->name, "pae") ||
                    STREQ(caps->guests[i]->features[j]->name, "nonpae") ||
1054
                    STREQ(caps->guests[i]->features[j]->name, "ia64_be") ||
1055 1056
                    STREQ(caps->guests[i]->features[j]->name, "cpuselection") ||
                    STREQ(caps->guests[i]->features[j]->name, "deviceboot")) {
1057
                    virBufferAsprintf(&buf, "<%s/>\n",
1058
                                      caps->guests[i]->features[j]->name);
1059
                } else {
1060
                    virBufferAsprintf(&buf, "<%s default='%s' toggle='%s'/>\n",
1061 1062 1063
                                      caps->guests[i]->features[j]->name,
                                      caps->guests[i]->features[j]->defaultOn ? "on" : "off",
                                      caps->guests[i]->features[j]->toggle ? "yes" : "no");
1064 1065 1066
                }
            }

1067 1068
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</features>\n");
1069
        }
1070 1071
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</guest>\n\n");
1072
    }
1073 1074
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</capabilities>\n");
1075

1076
    if (virBufferCheckError(&buf) < 0)
1077
        return NULL;
1078

1079
    return virBufferContentAndReset(&buf);
1080
}
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

/* 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)
{
1108
    virCapsHostNUMACellPtr cell = NULL;
1109
    size_t cpu;
1110
    size_t i;
1111
    /* The numa node numbers can be non-contiguous. Ex: 0,1,16,17. */
1112 1113 1114
    for (i = 0; i < caps->host.nnumaCell; i++) {
        if (caps->host.numaCell[i]->num == node) {
            cell = caps->host.numaCell[i];
1115 1116 1117
            break;
        }
    }
1118

1119
    for (cpu = 0; cell && cpu < cell->ncpus; cpu++) {
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
        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;
}