capabilities.c 19.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * capabilities.c: hypervisor capabilities
 *
 * Copyright (C) 2006-2008 Red Hat, Inc.
 * Copyright (C) 2006-2008 Daniel P. Berrange
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

24 25
#include <config.h>

26 27 28 29 30 31 32 33 34
#include "capabilities.h"
#include "buf.h"


/**
 * virCapabilitiesNew:
 * @arch: host machine architecture
 * @offlineMigrate: non-zero if offline migration is available
 * @liveMigrate: non-zero if live migration is available
35
 *
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
 * Allocate a new capabilities object
 */
virCapsPtr
virCapabilitiesNew(const char *arch,
                   int offlineMigrate,
                   int liveMigrate)
{
    virCapsPtr caps;

    if ((caps = calloc(1, sizeof(*caps))) == NULL)
        goto no_memory;

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

    return caps;

 no_memory:
    virCapabilitiesFree(caps);
    return NULL;
}

static void
virCapabilitiesFreeHostNUMACell(virCapsHostNUMACellPtr cell)
{
    free(cell->cpus);
    free(cell);
}

static void
virCapabilitiesFreeGuestDomain(virCapsGuestDomainPtr dom)
{
    int i;
    free(dom->info.emulator);
    free(dom->info.loader);
    for (i = 0 ; i < dom->info.nmachines ; i++)
        free(dom->info.machines[i]);
    free(dom->info.machines);
76
    free(dom->type);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    free(dom);
}

static void
virCapabilitiesFreeGuestFeature(virCapsGuestFeaturePtr feature)
{
    free(feature->name);
    free(feature);
}

static void
virCapabilitiesFreeGuest(virCapsGuestPtr guest)
{
    int i;
    free(guest->ostype);

94
    free(guest->arch.name);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    free(guest->arch.defaultInfo.emulator);
    free(guest->arch.defaultInfo.loader);
    for (i = 0 ; i < guest->arch.defaultInfo.nmachines ; i++)
        free(guest->arch.defaultInfo.machines[i]);
    free(guest->arch.defaultInfo.machines);

    for (i = 0 ; i < guest->arch.ndomains ; i++)
        virCapabilitiesFreeGuestDomain(guest->arch.domains[i]);
    free(guest->arch.domains);

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

    free(guest);
}


/**
 * virCapabilitiesFree:
 * @caps: object to free
 *
 * Free all memory associated with capabilities
 */
void
virCapabilitiesFree(virCapsPtr caps) {
    int i;

    for (i = 0 ; i < caps->nguests ; i++)
        virCapabilitiesFreeGuest(caps->guests[i]);
    free(caps->guests);

    for (i = 0 ; i < caps->host.nfeatures ; i++)
        free(caps->host.features[i]);
    free(caps->host.features);
    for (i = 0 ; i < caps->host.nnumaCell ; i++)
        virCapabilitiesFreeHostNUMACell(caps->host.numaCell[i]);
    free(caps->host.numaCell);

134 135 136 137
    for (i = 0 ; i < caps->host.nmigrateTrans ; i++)
        free(caps->host.migrateTrans[i]);
    free(caps->host.migrateTrans);

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    free(caps->host.arch);
    free(caps);
}


/**
 * 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)
{
    char **features;

    if ((features = realloc(caps->host.features,
                            sizeof(*features) * (caps->host.nfeatures+1))) == NULL)
        return -1;
    caps->host.features = features;

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

    return 0;
}


/**
 * virCapabilitiesAddHostMigrateTransport:
 * @caps: capabilities to extend
 * @name: name of migration transport
 *
 * Registers a new domain migration transport URI
 */
int
virCapabilitiesAddHostMigrateTransport(virCapsPtr caps,
                                       const char *name)
{
    char **migrateTrans;

    if ((migrateTrans = realloc(caps->host.migrateTrans,
                                sizeof(*migrateTrans) * (caps->host.nmigrateTrans+1))) == NULL)
        return -1;
    caps->host.migrateTrans = migrateTrans;

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

    return 0;
}


/**
 * virCapabilitiesAddHostNUMACell:
 * @caps: capabilities to extend
 * @num: ID number of NUMA cell
 * @ncpus: number of CPUs in cell
 * @cpus: array of CPU ID numbers for cell
 *
 * Registers a new NUMA cell for a host, passing in a
 * array of CPU IDs belonging to the cell
 */
int
virCapabilitiesAddHostNUMACell(virCapsPtr caps,
                               int num,
                               int ncpus,
                               const int *cpus)
{
    virCapsHostNUMACellPtr cell, *cells;

    if ((cells = realloc(caps->host.numaCell,
                         sizeof(*cells) * (caps->host.nnumaCell+1))) == NULL)
        return -1;
    caps->host.numaCell = cells;

    if ((cell = calloc(1, sizeof(cell))) == NULL)
        return -1;
    caps->host.numaCell[caps->host.nnumaCell] = cell;

    if ((caps->host.numaCell[caps->host.nnumaCell]->cpus =
         malloc(ncpus * sizeof(*cpus))) == NULL)
        return -1;
    memcpy(caps->host.numaCell[caps->host.nnumaCell]->cpus,
           cpus,
           ncpus * sizeof(*cpus));

    caps->host.numaCell[caps->host.nnumaCell]->ncpus = ncpus;
    caps->host.numaCell[caps->host.nnumaCell]->num = num;
    caps->host.nnumaCell++;

    return 0;
}


/**
 * virCapabilitiesAddGuest:
 * @caps: capabilities to extend
 * @ostype: guest operating system type ('hvm' or 'xen')
 * @arch: guest CPU architecture ('i686', or 'x86_64', etc)
 * @wordsize: number of bits in CPU word
 * @emulator: path to default device emulator for arch/ostype
 * @loader: path to default BIOS loader for arch/ostype
 * @nmachines: number of machine variants for emulator
 * @machines: machine variants for emulator ('pc', or 'isapc', etc)
 *
 * Registers a new guest operating system. This should be
 * followed by registration of at least one domain for
 * running the guest
 */
virCapsGuestPtr
virCapabilitiesAddGuest(virCapsPtr caps,
                        const char *ostype,
                        const char *arch,
                        int wordsize,
                        const char *emulator,
                        const char *loader,
                        int nmachines,
                        const char *const *machines)
{
    virCapsGuestPtr guest, *guests;
    int i;

    if ((guest = calloc(1, sizeof(*guest))) == NULL)
        goto no_memory;

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

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

    if (emulator &&
        (guest->arch.defaultInfo.emulator = strdup(emulator)) == NULL)
        goto no_memory;
    if (loader &&
        (guest->arch.defaultInfo.loader = strdup(loader)) == NULL)
        goto no_memory;
    if (nmachines) {
        if ((guest->arch.defaultInfo.machines =
             calloc(nmachines, sizeof(*guest->arch.defaultInfo.machines))) == NULL)
            goto no_memory;
        for (i = 0 ; i < nmachines ; i++) {
            if ((guest->arch.defaultInfo.machines[i] = strdup(machines[i])) == NULL)
                goto no_memory;
            guest->arch.defaultInfo.nmachines++;
        }
    }

    if ((guests = realloc(caps->guests,
                          sizeof(*guests) *
                          (caps->nguests + 1))) == NULL)
        goto no_memory;
    caps->guests = guests;
    caps->guests[caps->nguests] = guest;
    caps->nguests++;

    return guest;

 no_memory:
    virCapabilitiesFreeGuest(guest);
    return NULL;
}


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

    if ((dom = calloc(1, sizeof(*dom))) == NULL)
        goto no_memory;

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

    if (emulator &&
        (dom->info.emulator = strdup(emulator)) == NULL)
        goto no_memory;
    if (loader &&
        (dom->info.loader = strdup(loader)) == NULL)
        goto no_memory;
    if (nmachines) {
        if ((dom->info.machines =
             calloc(nmachines, sizeof(*dom->info.machines))) == NULL)
            goto no_memory;
        for (i = 0 ; i < nmachines ; i++) {
            if ((dom->info.machines[i] = strdup(machines[i])) == NULL)
                goto no_memory;
            dom->info.nmachines++;
        }
    }

    if ((doms = realloc(guest->arch.domains,
                        sizeof(*doms) *
                        (guest->arch.ndomains + 1))) == NULL)
        goto no_memory;
    guest->arch.domains = doms;
    guest->arch.domains[guest->arch.ndomains] = dom;
    guest->arch.ndomains++;


    return dom;

 no_memory:
    virCapabilitiesFreeGuestDomain(dom);
    return NULL;
}


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

    if ((feature = calloc(1, sizeof(*feature))) == NULL)
        goto no_memory;

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

    if ((features = realloc(guest->features,
                            sizeof(*features) *
                            (guest->nfeatures + 1))) == NULL)
        goto no_memory;
    guest->features = features;
    guest->features[guest->nfeatures] = feature;
    guest->nfeatures++;

    return feature;

 no_memory:
    virCapabilitiesFreeGuestFeature(feature);
    return NULL;
}


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


/**
 * virCapabilitiesDefaultGuestArch:
 * @caps: capabilities to query
 * @ostype: OS type to search for
 *
 * Returns the first architecture able to run the
 * requested operating system type
 */
extern const char *
virCapabilitiesDefaultGuestArch(virCapsPtr caps,
                                const char *ostype)
{
    int i;
    for (i = 0 ; i < caps->nguests ; i++) {
        if (STREQ(caps->guests[i]->ostype, ostype))
            return caps->guests[i]->arch.name;
    }
    return NULL;
}

/**
 * virCapabilitiesDefaultGuestMachine:
 * @caps: capabilities to query
 * @ostype: OS type to search for
 * @arch: architecture to search for
 *
 * Returns the first machine variant associated with
 * the requested operating system type and architecture
 */
extern const char *
virCapabilitiesDefaultGuestMachine(virCapsPtr caps,
                                   const char *ostype,
                                   const char *arch)
{
    int i;
    for (i = 0 ; i < caps->nguests ; i++) {
        if (STREQ(caps->guests[i]->ostype, ostype) &&
            STREQ(caps->guests[i]->arch.name, arch) &&
            caps->guests[i]->arch.defaultInfo.nmachines)
            return caps->guests[i]->arch.defaultInfo.machines[0];
    }
    return NULL;
}

/**
 * virCapabilitiesDefaultGuestMachine:
 * @caps: capabilities to query
 * @ostype: OS type to search for ('xen', 'hvm')
 * @arch: architecture to search for
 * @domain: domain type ('xen', 'qemu', 'kvm')
 *
 * Returns the first emulator path associated with
 * the requested operating system type, architecture
 * and domain type
 */
extern const char *
virCapabilitiesDefaultGuestEmulator(virCapsPtr caps,
                                    const char *ostype,
                                    const char *arch,
                                    const char *domain)
{
    int i, j;
    for (i = 0 ; i < caps->nguests ; i++) {
        char *emulator;
        if (STREQ(caps->guests[i]->ostype, ostype) &&
            STREQ(caps->guests[i]->arch.name, arch)) {
            emulator = caps->guests[i]->arch.defaultInfo.emulator;
            for (j = 0 ; j < caps->guests[i]->arch.ndomains ; j++) {
                if (STREQ(caps->guests[i]->arch.domains[j]->type, domain)) {
                    if (caps->guests[i]->arch.domains[j]->info.emulator)
                        emulator = caps->guests[i]->arch.domains[j]->info.emulator;
                }
            }
            return emulator;
        }
    }
    return NULL;
}


/**
 * virCapabilitiesFormatXML:
 * @caps: capabilities to format
 *
 * Convert the capabilities object into an XML representation
 *
 * Returns the XML document as a string
 */
char *
virCapabilitiesFormatXML(virCapsPtr caps)
{
524
    virBuffer xml = VIR_BUFFER_INITIALIZER;
525 526
    int i, j, k;

527 528 529 530 531
    virBufferAddLit(&xml, "<capabilities>\n\n");
    virBufferAddLit(&xml, "  <host>\n");
    virBufferAddLit(&xml, "    <cpu>\n");
    virBufferVSprintf(&xml, "      <arch>%s</arch>\n",
                      caps->host.arch);
532 533

    if (caps->host.nfeatures) {
534
        virBufferAddLit(&xml, "      <features>\n");
535
        for (i = 0 ; i < caps->host.nfeatures ; i++) {
536 537
            virBufferVSprintf(&xml, "        <%s/>\n",
                              caps->host.features[i]);
538
        }
539
        virBufferAddLit(&xml, "      </features>\n");
540
    }
541
    virBufferAddLit(&xml, "    </cpu>\n");
542 543

    if (caps->host.offlineMigrate) {
544 545 546
        virBufferAddLit(&xml, "    <migration_features>\n");
        if (caps->host.liveMigrate)
            virBufferAddLit(&xml, "      <live/>\n");
547
        if (caps->host.nmigrateTrans) {
548
            virBufferAddLit(&xml, "      <uri_transports>\n");
549
            for (i = 0 ; i < caps->host.nmigrateTrans ; i++) {
550 551
                virBufferVSprintf(&xml, "        <uri_transport>%s</uri_transport>\n",
                                      caps->host.migrateTrans[i]);
552
            }
553
            virBufferAddLit(&xml, "      </uri_transports>\n");
554
        }
555
        virBufferAddLit(&xml, "    </migration_features>\n");
556 557 558
    }

    if (caps->host.nnumaCell) {
559 560 561
        virBufferAddLit(&xml, "    <topology>\n");
        virBufferVSprintf(&xml, "      <cells num='%d'>\n",
                          caps->host.nnumaCell);
562
        for (i = 0 ; i < caps->host.nnumaCell ; i++) {
563 564 565 566
            virBufferVSprintf(&xml, "        <cell id='%d'>\n",
                              caps->host.numaCell[i]->num);
            virBufferVSprintf(&xml, "          <cpus num='%d'>\n",
                              caps->host.numaCell[i]->ncpus);
567
            for (j = 0 ; j < caps->host.numaCell[i]->ncpus ; j++)
568 569 570 571
                virBufferVSprintf(&xml, "            <cpu id='%d'/>\n",
                                  caps->host.numaCell[i]->cpus[j]);
            virBufferAddLit(&xml, "          </cpus>\n");
            virBufferAddLit(&xml, "        </cell>\n");
572
        }
573 574
        virBufferAddLit(&xml, "      </cells>\n");
        virBufferAddLit(&xml, "    </topology>\n");
575
    }
576
    virBufferAddLit(&xml, "  </host>\n\n");
577 578 579


    for (i = 0 ; i < caps->nguests ; i++) {
580 581 582 583 584 585 586 587
        virBufferAddLit(&xml, "  <guest>\n");
        virBufferVSprintf(&xml, "    <os_type>%s</os_type>\n",
                          caps->guests[i]->ostype);
        virBufferVSprintf(&xml, "    <arch name='%s'>\n",
                          caps->guests[i]->arch.name);
        virBufferVSprintf(&xml, "      <wordsize>%d</wordsize>\n",
                          caps->guests[i]->arch.wordsize);
        if (caps->guests[i]->arch.defaultInfo.emulator)
588
            virBufferVSprintf(&xml, "      <emulator>%s</emulator>\n",
589 590 591 592
                              caps->guests[i]->arch.defaultInfo.emulator);
            if (caps->guests[i]->arch.defaultInfo.loader)
                virBufferVSprintf(&xml, "      <loader>%s</loader>\n",
                                  caps->guests[i]->arch.defaultInfo.loader);
593 594

        for (j = 0 ; j < caps->guests[i]->arch.defaultInfo.nmachines ; j++) {
595 596
            virBufferVSprintf(&xml, "      <machine>%s</machine>\n",
                              caps->guests[i]->arch.defaultInfo.machines[j]);
597 598 599
        }

        for (j = 0 ; j < caps->guests[i]->arch.ndomains ; j++) {
600 601 602
            virBufferVSprintf(&xml, "      <domain type='%s'>\n",
                                  caps->guests[i]->arch.domains[j]->type);
            if (caps->guests[i]->arch.domains[j]->info.emulator)
603
                virBufferVSprintf(&xml, "        <emulator>%s</emulator>\n",
604 605
                                  caps->guests[i]->arch.domains[j]->info.emulator);
            if (caps->guests[i]->arch.domains[j]->info.loader)
606
                virBufferVSprintf(&xml, "        <loader>%s</loader>\n",
607
                                  caps->guests[i]->arch.domains[j]->info.loader);
608 609

            for (k = 0 ; k < caps->guests[i]->arch.domains[j]->info.nmachines ; k++) {
610 611
                virBufferVSprintf(&xml, "        <machine>%s</machine>\n",
                                  caps->guests[i]->arch.domains[j]->info.machines[k]);
612
            }
613
            virBufferAddLit(&xml, "      </domain>\n");
614 615
        }

616
        virBufferAddLit(&xml, "    </arch>\n");
617 618

        if (caps->guests[i]->nfeatures) {
619
            virBufferAddLit(&xml, "    <features>\n");
620 621 622 623 624

            for (j = 0 ; j < caps->guests[i]->nfeatures ; j++) {
                if (STREQ(caps->guests[i]->features[j]->name, "pae") ||
                    STREQ(caps->guests[i]->features[j]->name, "nonpae") ||
                    STREQ(caps->guests[i]->features[j]->name, "ia64_be")) {
625 626
                    virBufferVSprintf(&xml, "      <%s/>\n",
                                      caps->guests[i]->features[j]->name);
627
                } else {
628 629 630 631
                    virBufferVSprintf(&xml, "      <%s default='%s' toggle='%s'/>\n",
                                      caps->guests[i]->features[j]->name,
                                      caps->guests[i]->features[j]->defaultOn ? "on" : "off",
                                      caps->guests[i]->features[j]->toggle ? "yes" : "no");
632 633 634
                }
            }

635
            virBufferAddLit(&xml, "    </features>\n");
636 637
        }

638
        virBufferAddLit(&xml, "  </guest>\n\n");
639 640
    }

641
    virBufferAddLit(&xml, "</capabilities>\n");
642

643 644
    if (virBufferError(&xml))
        return NULL;
645

646
    return virBufferContentAndReset(&xml);
647
}